scripts/maps: R2 publish step (env-var creds) for v5 pmtiles
- Commit
f210ea60c0f13490d2efc04c18d59d28149dfafa- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-17 00:44 UTC (3 months ago)
- Parents
fc9aee7
Diff
scripts/maps/README.md
diff --git a/scripts/maps/README.md b/scripts/maps/README.mdindex 50aa53b153..8298a68aa0 100644--- a/scripts/maps/README.md+++ b/scripts/maps/README.md@@ -114,6 +114,7 @@ base is reused verbatim. P13 points `MaxspeedSource.PMTILES_URL` at v5.pmtiles. | `build_safety_layer.sh` | osmium → GeoJSON → `normalize_safety.py` → tippecanoe → `safety.pmtiles` | | `build_maxspeed_layer.sh` | osmium → GeoJSON → `normalize_maxspeed.py` → tippecanoe → `maxspeed.pmtiles` | | `build_admin_layers.sh` | Natural Earth / OSM → `normalize_admin.py` → tippecanoe → `admin_*.pmtiles` |+| `publish_r2.sh` | Upload built `.pmtiles` to Cloudflare R2 (creds from env vars only) | | `normalize_safety.py` | OSM tags → `safety` layer schema (pure stdlib, unit-tested) | | `normalize_maxspeed.py` | OSM maxspeed ways → `maxspeed` layer schema (pure stdlib, unit-tested) | | `normalize_admin.py` | NE/OSM attrs → admin layer schema (pure stdlib, unit-tested) |@@ -197,18 +198,54 @@ The base dominates; the safety + admin overlays are a rounding error on top. --- -## Publish to data.vayunmathur.com+## Publishing to R2 -Upload with a bumped key so the app can switch atomically:+Upload the built `v5.pmtiles` to Cloudflare R2 with+[`publish_r2.sh`](publish_r2.sh). It reads **all** credentials/config from+**environment variables** — nothing secret is stored in the repo.++**Required env** (placeholders — set your real values):++| Var | Meaning |+|---|---|+| `R2_ENDPOINT` | S3 API endpoint, e.g. `https://<ACCOUNT_ID>.r2.cloudflarestorage.com` |+| `R2_ACCESS_KEY_ID` | R2 access key id |+| `R2_SECRET_ACCESS_KEY` | R2 secret access key |+| `R2_BUCKET` | *(optional)* bucket name, default `maps` (served at `data.vayunmathur.com`) | ```bash-R2_ENDPOINT=https://<ACCOUNT_ID>.r2.cloudflarestorage.com \-AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... \-R2_KEY=v5.pmtiles ./scripts/maps/vendor_pmtiles.sh --local v5.pmtiles --source v5.pmtiles+# export secrets (do NOT pass them as CLI args — they leak into shell history)+export R2_ENDPOINT=https://<ACCOUNT_ID>.r2.cloudflarestorage.com+export R2_ACCESS_KEY_ID=... # your key id+export R2_SECRET_ACCESS_KEY=... # your secret+# export R2_BUCKET=maps # optional; this is the default++# upload (uses rclone if present, else aws s3 cp --endpoint-url "$R2_ENDPOINT")+./scripts/maps/publish_r2.sh v5.pmtiles --key v5.pmtiles++# ...or let the build publish automatically after a successful merge:+./build_v5_pmtiles.sh --pbf planet-latest.osm.pbf --out v5.pmtiles --publish ``` -(`vendor_pmtiles.sh` handles multipart upload, cache headers and a Range-request-verification against the public custom domain.)+`publish_r2.sh` sets `Content-Type: application/octet-stream` and a long+immutable `Cache-Control`, and fails clearly if any required env var is unset.+It auto-detects the upload tool (`--tool auto|rclone|aws`):++* **rclone** — `rclone copyto … :s3:$R2_BUCKET/<key>` with `--s3-*` flags.+* **aws-cli** — `aws s3 cp … --endpoint-url "$R2_ENDPOINT"` (creds scoped to the+ invocation via `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` from the R2 vars).++Since v5 **bakes the maxspeed layer in**, you normally publish just the single+`v5.pmtiles`. The script also accepts multiple files+(`publish_r2.sh v5.pmtiles aux.pmtiles`) for any siblings built separately.++> **Secret hygiene:** never commit keys, never paste them on the command line+> (shell history), prefer a secret manager / `.env` that is git-ignored, and+> **rotate** R2 keys periodically and immediately if one is ever exposed.++> (The older [`vendor_pmtiles.sh`](vendor_pmtiles.sh) — which streams the 137 GB+> upstream base to R2 — remains for the base-vendoring workflow; `publish_r2.sh`+> is the general publisher for locally built `.pmtiles`.) Then **P13** (separate task, app code) points the style at the new file: @@ -217,7 +254,8 @@ Then **P13** (separate task, app code) points the style at the new file: "url": "pmtiles://https://data.vayunmathur.com/v5.pmtiles" ``` -…adds style layers for `safety`, and switches the country/state mask in+…adds style layers for `safety`, points `MaxspeedSource.PMTILES_URL` at the same+file (source-layer `maxspeed`), and switches the country/state mask in `CountryMap.kt` / `MyMapLayers.kt` from the `.fgb` assets to the `admin_country` / `admin_region` PMTiles layers (matching on `ISO_A2` / `iso_3166_2`, which are preserved).
scripts/maps/build_v5_pmtiles.sh
diff --git a/scripts/maps/build_v5_pmtiles.sh b/scripts/maps/build_v5_pmtiles.shindex 1383597848..0b7270d5ff 100644--- a/scripts/maps/build_v5_pmtiles.sh+++ b/scripts/maps/build_v5_pmtiles.sh@@ -40,6 +40,9 @@ set -euo pipefail # --skip-safety omit safety layer # --skip-maxspeed omit maxspeed layer # --skip-admin omit admin layers+# --publish after a successful build, upload $OUT to R2 (publish_r2.sh;+# reads R2_ENDPOINT/R2_ACCESS_KEY_ID/R2_SECRET_ACCESS_KEY from env)+# --publish-key K remote object key when publishing (default: basename of --out) # --keep-work keep intermediates # # Tools: tile-join + tippecanoe, osmium, ogr2ogr (GDAL), python3, and either@@ -58,6 +61,8 @@ SKIP_SAFETY=0 SKIP_MAXSPEED=0 SKIP_ADMIN=0 KEEP_WORK=0+PUBLISH=0+PUBLISH_KEY="" while [[ $# -gt 0 ]]; do case "$1" in@@ -73,7 +78,9 @@ while [[ $# -gt 0 ]]; do --skip-maxspeed) SKIP_MAXSPEED=1; shift ;; --skip-admin) SKIP_ADMIN=1; shift ;; --keep-work) KEEP_WORK=1; shift ;;- -h|--help) sed -n '4,60p' "$0" | sed 's/^# \?//'; exit 0 ;;+ --publish) PUBLISH=1; shift ;;+ --publish-key) PUBLISH_KEY="$2"; shift 2 ;;+ -h|--help) sed -n '4,62p' "$0" | sed 's/^# \?//'; exit 0 ;; *) echo "Unknown arg: $1" >&2; exit 1 ;; esac done@@ -139,8 +146,19 @@ echo "Layers now in $OUT:" echo " base : earth landcover landuse water roads buildings boundaries pois places" echo " new : safety maxspeed admin_country admin_region admin_city" echo ""-echo "Publish (bump key to v5.pmtiles):"-echo " R2_KEY=v5.pmtiles ./scripts/maps/vendor_pmtiles.sh --local $OUT --source $OUT"-echo "Then P13 updates style.json url -> pmtiles://https://data.vayunmathur.com/v5.pmtiles"++if [[ "$PUBLISH" == "1" ]]; then+ PUB_ARGS=("$OUT")+ [[ -n "$PUBLISH_KEY" ]] && PUB_ARGS+=(--key "$PUBLISH_KEY")+ echo "[v5] publishing to R2 (creds from env: R2_ENDPOINT/R2_ACCESS_KEY_ID/R2_SECRET_ACCESS_KEY)"+ "$HERE/publish_r2.sh" "${PUB_ARGS[@]}"+else+ echo "Publish (creds from environment variables — see README 'Publishing to R2'):"+ echo " export R2_ENDPOINT=https://<ACCOUNT_ID>.r2.cloudflarestorage.com"+ echo " export R2_ACCESS_KEY_ID=... R2_SECRET_ACCESS_KEY=..."+ echo " ./scripts/maps/publish_r2.sh $OUT --key v5.pmtiles"+ echo " # or re-run this build with --publish"+ echo "Then P13 updates style.json url -> pmtiles://https://data.vayunmathur.com/v5.pmtiles"+fi [[ "$KEEP_WORK" == "1" ]] || echo "(intermediates kept in $WORK; pass --keep-work to silence)"
scripts/maps/publish_r2.sh
diff --git a/scripts/maps/publish_r2.sh b/scripts/maps/publish_r2.shnew file mode 100644index 0000000000..9a33033f14--- /dev/null+++ b/scripts/maps/publish_r2.sh@@ -0,0 +1,171 @@+#!/bin/bash+set -euo pipefail++# publish_r2.sh — upload built PMTiles (e.g. v5.pmtiles) to Cloudflare R2.+#+# SECURITY: this script reads ALL credentials/config from ENVIRONMENT VARIABLES.+# No secrets are stored in the repo. Never paste keys on the command line (they+# leak into shell history) — `export` them instead, ideally from a secret store.+#+# Required env:+# R2_ENDPOINT S3 API endpoint, e.g. https://<ACCOUNT_ID>.r2.cloudflarestorage.com+# R2_ACCESS_KEY_ID R2 access key id+# R2_SECRET_ACCESS_KEY R2 secret access key+# Optional env:+# R2_BUCKET Default: maps (served at data.vayunmathur.com)+#+# Usage:+# export R2_ENDPOINT=https://<ACCOUNT_ID>.r2.cloudflarestorage.com+# export R2_ACCESS_KEY_ID=... R2_SECRET_ACCESS_KEY=...+# ./publish_r2.sh v5.pmtiles # -> s3://$R2_BUCKET/v5.pmtiles+# ./publish_r2.sh v5.pmtiles --key v5.pmtiles+# ./publish_r2.sh v5.pmtiles maxspeed.pmtiles # multiple files (aux/siblings)+#+# Options:+# --key NAME Remote object key for the FIRST file (default: its basename).+# Ignored when uploading multiple files (each keeps its name).+# --tool T Upload tool: auto|rclone|aws (default: auto — prefer rclone).+# --content-type C MIME type (default: application/octet-stream — fine for pmtiles).+# --dry-run Print what would happen; upload nothing.+# -h|--help Show this help.+#+# Note: v5.pmtiles BAKES the maxspeed layer in, so normally you only publish the+# single v5.pmtiles. The multi-file form exists for aux/sibling artifacts if you+# ever build them separately.++FILES=()+KEY=""+TOOL="auto"+CONTENT_TYPE="application/octet-stream"+CACHE_CONTROL="public, max-age=31536000, immutable"+DRY_RUN=0++while [[ $# -gt 0 ]]; do+ case "$1" in+ --key) KEY="$2"; shift 2 ;;+ --tool) TOOL="$2"; shift 2 ;;+ --content-type) CONTENT_TYPE="$2"; shift 2 ;;+ --dry-run) DRY_RUN=1; shift ;;+ -h|--help) sed -n '4,40p' "$0" | sed 's/^# \?//'; exit 0 ;;+ -*) echo "Unknown arg: $1" >&2; exit 1 ;;+ *) FILES+=("$1"); shift ;;+ esac+done++# Default to the canonical build output if no file was given.+if [[ ${#FILES[@]} -eq 0 ]]; then+ FILES=("v5.pmtiles")+fi++# --- Validate required environment (fail clearly; list everything missing) ---+MISSING=()+[[ -n "${R2_ENDPOINT:-}" ]] || MISSING+=("R2_ENDPOINT")+[[ -n "${R2_ACCESS_KEY_ID:-}" ]] || MISSING+=("R2_ACCESS_KEY_ID")+[[ -n "${R2_SECRET_ACCESS_KEY:-}" ]] || MISSING+=("R2_SECRET_ACCESS_KEY")+if [[ ${#MISSING[@]} -gt 0 ]]; then+ echo "ERROR: missing required environment variable(s): ${MISSING[*]}" >&2+ echo " export them before running (do NOT pass secrets as CLI args):" >&2+ echo " export R2_ENDPOINT=https://<ACCOUNT_ID>.r2.cloudflarestorage.com" >&2+ echo " export R2_ACCESS_KEY_ID=... R2_SECRET_ACCESS_KEY=..." >&2+ exit 1+fi++R2_BUCKET="${R2_BUCKET:-maps}"++if [[ "$R2_ENDPOINT" == *"<ACCOUNT_ID>"* || "$R2_ENDPOINT" == *"<"* ]]; then+ echo "ERROR: R2_ENDPOINT still contains a placeholder: $R2_ENDPOINT" >&2+ echo " Set the real endpoint for your account." >&2+ exit 1+fi++# --- Verify local files exist ---+for f in "${FILES[@]}"; do+ if [[ ! -f "$f" ]]; then+ echo "ERROR: file not found: $f" >&2+ echo " Build it first, e.g. ./build_v5_pmtiles.sh --pbf ... --out v5.pmtiles" >&2+ exit 1+ fi+done++# --- Select upload tool ---+resolve_tool() {+ case "$TOOL" in+ rclone) command -v rclone >/dev/null || { echo "ERROR: rclone requested but not installed" >&2; exit 1; }; echo rclone ;;+ aws) command -v aws >/dev/null || { echo "ERROR: aws requested but not installed" >&2; exit 1; }; echo aws ;;+ auto)+ if command -v rclone >/dev/null; then echo rclone+ elif command -v aws >/dev/null; then echo aws+ else+ echo "ERROR: neither rclone nor aws CLI found. Install one:" >&2+ echo " rclone: https://rclone.org/install/" >&2+ echo " aws-cli: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html" >&2+ exit 1+ fi+ ;;+ *) echo "ERROR: --tool must be auto|rclone|aws" >&2; exit 1 ;;+ esac+}+UPLOADER="$(resolve_tool)"++upload_rclone() { # src key+ local src="$1" key="$2"+ rclone copyto "$src" ":s3:$R2_BUCKET/$key" \+ --s3-provider Cloudflare \+ --s3-endpoint "$R2_ENDPOINT" \+ --s3-access-key-id "$R2_ACCESS_KEY_ID" \+ --s3-secret-access-key "$R2_SECRET_ACCESS_KEY" \+ --s3-region auto \+ --s3-upload-cutoff 100M \+ --s3-chunk-size 100M \+ --s3-no-check-bucket \+ --header-upload "Content-Type: $CONTENT_TYPE" \+ --header-upload "Cache-Control: $CACHE_CONTROL" \+ --progress+}++upload_aws() { # src key+ local src="$1" key="$2"+ # Scope creds to this invocation; aws reads them from the environment.+ AWS_ACCESS_KEY_ID="$R2_ACCESS_KEY_ID" \+ AWS_SECRET_ACCESS_KEY="$R2_SECRET_ACCESS_KEY" \+ AWS_REGION="auto" \+ aws s3 cp "$src" "s3://$R2_BUCKET/$key" \+ --endpoint-url "$R2_ENDPOINT" \+ --content-type "$CONTENT_TYPE" \+ --cache-control "$CACHE_CONTROL" \+ --only-show-errors+}++echo "=== publish_r2.sh ==="+echo "R2_ENDPOINT = $R2_ENDPOINT"+echo "R2_BUCKET = $R2_BUCKET"+echo "tool = $UPLOADER"+echo "files = ${FILES[*]}"++first=1+for src in "${FILES[@]}"; do+ if [[ ${#FILES[@]} -eq 1 && -n "$KEY" ]]; then+ key="$KEY"+ elif [[ $first -eq 1 && -n "$KEY" ]]; then+ key="$KEY"+ else+ key="$(basename "$src")"+ fi+ first=0++ echo "--- uploading $src -> s3://$R2_BUCKET/$key (Content-Type: $CONTENT_TYPE) ---"+ if [[ "$DRY_RUN" == "1" ]]; then+ echo "[dry-run] would upload via $UPLOADER"+ continue+ fi+ if [[ "$UPLOADER" == "rclone" ]]; then+ upload_rclone "$src" "$key"+ else+ upload_aws "$src" "$key"+ fi+ echo "uploaded: https://data.vayunmathur.com/$key"+done++echo ""+echo "Done. The app loads: pmtiles://https://data.vayunmathur.com/v5.pmtiles"+echo "(P13 points style.json + MaxspeedSource.PMTILES_URL at the v5 file.)"