SPB Git

Repository Public BSD-3-Clause

build-region.sh 9.4 KB 157 lines shellscript

1#!/usr/bin/env bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : ORIX Maps8# File             : Infrastructure/build-region.sh9# Module           : Infrastructure10# Repository       : https://git.spboucher.ai/orix-maps11# Created          : 2026-11-2512# Last Modified    : 2026-11-2513#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: support@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin   : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License          : Proprietary, all rights reserved (see license file)32# License File     : LICENSE33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041# ORIX Maps — reproducible region build. Runs on the build node (macOS, Apple Silicon).42#43#   build-region.sh setup                      install tools, clone and build the tile profile, build the Swift builders44#   build-region.sh fetch <source>             download (or refresh) a source extract and verify its MD545#   build-region.sh build <region-id>          extract → tiles → search index → routing graph → signed package46#   build-region.sh publish <region-id>...     verify, add to the local distribution directory, regenerate the catalog47#   build-region.sh deploy                     copy the distribution directory to the web node (artefacts first, catalog last)48#49# Layout under $ORIX_BUILD_ROOT (default ~/orix-maps-build): src/ (extracts), work/<region>/, out/<region>/, dist/ (distribution50# directory, same shape as https://www.orixlab.dev/maps/v1/), logs/, repo/ (a copy of this repository's builder sources).51# The signing key is read from ~/.config/orix-maps/signing/ (mode 600); it never leaves the build node.52set -euo pipefail53export PATH="/opt/homebrew/opt/openjdk/bin:/opt/homebrew/bin:$PATH"54ROOT="${ORIX_BUILD_ROOT:-$HOME/orix-maps-build}"55REPO="${ORIX_REPO:-$ROOT/repo}"56CONFIG="$REPO/Infrastructure/regions.json"57BASEMAPS_COMMIT="42ffaaa4a85a41bfcb23e43cc0f5b492a5eca123"58KEY_DIR="$HOME/.config/orix-maps/signing"59WEB_NODE="${ORIX_WEB_NODE:-m4mm}"60WEB_DIR="${ORIX_WEB_DIR:-apps-data/orix-maps/v1}"61mkdir -p "$ROOT"/{src,work,out,dist,logs,tools}6263cfg() { python3 - "$CONFIG" "$@" <<'PY'64import json, sys65c = json.load(open(sys.argv[1])); kind, key, field = sys.argv[2], sys.argv[3], sys.argv[4]66item = c["sources"][key] if kind == "source" else next(r for r in c["regions"] if r["id"] == key)67print(item.get(field, ""))68PY69}70bin() { echo "$REPO/Infrastructure/$1/.build/release/$2"; }71key_file() { ls "$KEY_DIR"/orixmap-*.key 2>/dev/null | head -1; }72trust_file() { ls "$KEY_DIR"/orixmap-*.trust.json 2>/dev/null | head -1; }7374case "${1:?usage: build-region.sh setup|fetch|build|publish|deploy}" in75setup)76  for f in openjdk maven osmium-tool; do brew list "$f" >/dev/null 2>&1 || brew install "$f"; done77  if [ ! -d "$ROOT/tools/basemaps" ]; then git clone https://github.com/protomaps/basemaps.git "$ROOT/tools/basemaps"; fi78  (cd "$ROOT/tools/basemaps" && git fetch -q origin && git checkout -q "$BASEMAPS_COMMIT" && cd tiles && { ls target/*-with-deps.jar >/dev/null 2>&1 || mvn -q clean package -DskipTests; })79  for p in geocoder-builder routing-builder package-builder; do (cd "$REPO/Infrastructure/$p" && swift build -c release 2>&1 | tail -1); done80  if [ -z "$(key_file)" ]; then81    mkdir -p "$KEY_DIR" && chmod 700 "$KEY_DIR"82    "$(bin package-builder orix-package)" keygen --out "$KEY_DIR" --label "ORIX Labs maps $(date +%Y)" | grep -v -i "private key"83  fi84  echo "signing key id: $(basename "$(key_file)" .key | sed 's/orixmap-//')"85  ;;86fetch)87  src="${2:?source}"; url=$(cfg source "$src" url); out="$ROOT/src/$src.osm.pbf"88  curl -L --fail -o "$out.md5" "$url.md5"89  if [ -f "$out" ] && [ "$(md5 -q "$out")" = "$(cut -d' ' -f1 "$out.md5")" ]; then echo "$src is up to date"; else curl -L --fail -o "$out" "$url"; fi90  [ "$(md5 -q "$out")" = "$(cut -d' ' -f1 "$out.md5")" ] || { echo "MD5 mismatch for $out" >&2; exit 1; }91  osmium fileinfo "$out" | grep -E "osmosis_replication_timestamp|Size" | tee "$ROOT/src/$src.info"92  ;;93build)94  id="${2:?region id}"; src=$(cfg region "$id" source); bbox=$(cfg region "$id" bbox); maxzoom=$(cfg region "$id" maxZoom)95  pbf="$ROOT/src/$src.osm.pbf"; work="$ROOT/work/$id"; out="$ROOT/out/$id"; log="$ROOT/logs/$id-$(date +%Y%m%d-%H%M%S).log"96  mkdir -p "$work" "$out"97  ts=$(osmium fileinfo -g header.option.osmosis_replication_timestamp "$pbf")98  [ -n "$ts" ] || { echo "the source extract has no replication timestamp" >&2; exit 1; }99  version=$(python3 -c "import sys; d=sys.argv[1][:10].split('-'); print(f'{int(d[0])}.{int(d[1])}.{int(d[2])}')" "$ts")100  echo "== $id: data $ts → version $version (log $log)"101  exec > >(tee -a "$log") 2>&1102  start=$(date +%s)103  if [ -n "$bbox" ]; then104    # Complete ways are kept so that roads crossing the edge stay routable; the data timestamp is carried over.105    osmium extract -b "$bbox" -s complete_ways --overwrite --output-header="osmosis_replication_timestamp=$ts" "$pbf" -o "$work/region.osm.pbf"106  else107    ln -sf "$pbf" "$work/region.osm.pbf"108  fi109  t1=$(date +%s); echo "extract: $((t1 - start)) s, $(stat -L -f %z "$work/region.osm.pbf") bytes"110  jar=$(ls "$ROOT"/tools/basemaps/tiles/target/*-with-deps.jar | head -1)111  bounds=(); [ -n "$bbox" ] && bounds=(--bounds="$bbox")112  java -Xmx24g -jar "$jar" --download --osm_path="$work/region.osm.pbf" ${bounds[@]+"${bounds[@]}"} --maxzoom="$maxzoom" --output="$out/map.pmtiles" --force \113    --tmpdir="$work/tiles-tmp" --download_dir="$ROOT/src/planetiler-sources" > "$work/tiles.log" 2>&1 || { tail -20 "$work/tiles.log"; exit 1; }114  t2=$(date +%s); echo "tiles: $((t2 - t1)) s, $(stat -f %z "$out/map.pmtiles") bytes"115  geo=(); [ -n "$bbox" ] && geo=(--bbox "$bbox")116  rm -f "$out/search.sqlite"117  /usr/bin/time -l "$(bin geocoder-builder orix-geocoder-build)" --pbf "$work/region.osm.pbf" --region "$id" --out "$out/search.sqlite" ${geo[@]+"${geo[@]}"} 2> "$work/geocoder.time" | tail -8118  t3=$(date +%s); echo "search index: $((t3 - t2)) s, $(stat -f %z "$out/search.sqlite") bytes, peak $(awk '/maximum resident set size/ {print $1}' "$work/geocoder.time") bytes"119  /usr/bin/time -l "$(bin routing-builder orix-routing-build)" --pbf "$work/region.osm.pbf" --region "$id" --out "$out/routing.orixgraph" ${geo[@]+"${geo[@]}"} 2> "$work/routing.time" | tail -8120  t4=$(date +%s); echo "routing graph: $((t4 - t3)) s, $(stat -f %z "$out/routing.orixgraph") bytes, peak $(awk '/maximum resident set size/ {print $1}' "$work/routing.time") bytes"121  package="$out/$id-$version.orixmap"122  box=(); [ -n "$bbox" ] && box=(--bbox "$bbox")123  "$(bin package-builder orix-package)" build --region "$id" --name-en "$(cfg region "$id" nameEN)" --name-fr "$(cfg region "$id" nameFR)" \124    --data-version "$version" --data-timestamp "$ts" ${box[@]+"${box[@]}"} --map "$out/map.pmtiles" --search "$out/search.sqlite" --routing "$out/routing.orixgraph" \125    --licences "$REPO/Infrastructure/package-builder/licences-osm.json" --builder "planetiler=0.10.2" --builder "protomaps-basemaps=${BASEMAPS_COMMIT:0:8}" \126    --key "$(key_file)" --out "$package"127  "$(bin package-builder orix-package)" verify "$package" --trust "$(trust_file)"128  echo "package: $package, $(stat -f %z "$package") bytes, total $(( $(date +%s) - start )) s"129  ;;130publish)131  shift132  for id in "$@"; do133    package=$(ls -t "$ROOT/out/$id"/"$id"-*.orixmap | head -1)134    "$(bin package-builder orix-package)" publish "$package" --dir "$ROOT/dist" --trust "$(trust_file)"135  done136  "$(bin package-builder orix-package)" catalog --dir "$ROOT/dist" --trust "$(trust_file)"137  ;;138deploy)139  # Artefacts and per-region documents first; the catalog last, so that it never lists a file that is not there yet.140  ssh "$WEB_NODE" "mkdir -p ~/$WEB_DIR"141  rsync -a --partial --exclude catalog.json --exclude pins.json "$ROOT/dist/" "$WEB_NODE:$WEB_DIR/"142  ssh "$WEB_NODE" "cd ~/$WEB_DIR && find files -name '*.orixmap' -exec shasum -a 256 {} \;" > "$ROOT/logs/deploy.sha256"143  python3 - "$ROOT/dist/catalog.json" "$ROOT/logs/deploy.sha256" <<'PY'144import json, sys145want = {r["latest"]["path"]: r["latest"]["sha256"] for r in json.load(open(sys.argv[1]))["regions"]}146have = {line.split()[1]: line.split()[0] for line in open(sys.argv[2]) if line.strip()}147bad = [p for p, s in want.items() if have.get(p) != s]148if bad: sys.exit("checksum mismatch on the web node: %s" % bad)149print("verified %d artefacts on the web node" % len(want))150PY151  rsync -a "$ROOT/dist/catalog.json" "$WEB_NODE:$WEB_DIR/catalog.json.new"152  ssh "$WEB_NODE" "mv ~/$WEB_DIR/catalog.json.new ~/$WEB_DIR/catalog.json"153  echo "deployed to $WEB_NODE:~/$WEB_DIR"154  ;;155*) echo "unknown step: $1" >&2; exit 2 ;;156esac157
Files main
Menu

Theme