interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / hub / forge-edge-update.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Update the forge edge on a hub where forge-edge-apply.sh has already run: replace the |
| 3 | # forge-owned nginx files with a new version. The SNI router and sites-enabled/aienos.com are |
| 4 | # not touched. ONLY on Manager's GO (see infra/DEPLOY.md). |
| 5 | # Usage: sudo bash forge-edge-update.sh <new infra dir> <deployed infra dir> |
| 6 | # <deployed infra dir> holds the versions that are live now; the update refuses if any live |
| 7 | # file differs from it (someone else edited it: merge first, never overwrite). |
| 8 | # Same discipline as the apply: backup to /etc/nginx/forge-backups/<UTC>/ (rollback format), |
| 9 | # `nginx -t` gate, reload, live checks; any failure runs forge-edge-rollback.sh <backup>. |
| 10 | set -u |
| 11 | SRC=${1:?usage: forge-edge-update.sh <new infra dir> <deployed infra dir>} |
| 12 | OLD=${2:?usage: forge-edge-update.sh <new infra dir> <deployed infra dir>} |
| 13 | N=/etc/nginx |
| 14 | TS=$(date -u +%Y%m%dT%H%M%SZ) |
| 15 | BK=$N/forge-backups/$TS |
| 16 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 17 | FILES="conf.d/forge.conf snippets/forge-proxy.conf snippets/forge-headers.conf snippets/forge-archive.conf snippets/aienos-forge.conf sites-enabled/interplane.io" |
| 18 | die() { echo "UPDATE ABORTED (nothing changed): $*" >&2; exit 1; } |
| 19 | [ "$(id -u)" = 0 ] || die "run with sudo" |
| 20 | |
| 21 | # ---- preconditions (no change made yet) ---- |
| 22 | for f in $FILES; do |
| 23 | [ -s "$SRC/nginx/$f" ] || die "missing $SRC/nginx/$f" |
| 24 | cmp -s "$N/$f" "$OLD/nginx/$f" || die "live $N/$f differs from $OLD/nginx/$f" |
| 25 | done |
| 26 | [ -x "$HERE/forge-edge-rollback.sh" ] || die "missing $HERE/forge-edge-rollback.sh" |
| 27 | grep -q 'server 127.0.0.1:4448;' $N/stream.d/sni-router.conf || die "forge edge not applied: router is not on 4448" |
| 28 | grep -q 'include /etc/nginx/snippets/aienos-forge.conf;' $N/sites-enabled/aienos.com || die "forge edge not applied: aienos.com has no forge include" |
| 29 | # Download spool: 6 concurrent downloads x 512 MiB (forged's cap) must still leave 3 GiB free. |
| 30 | need=$(( (3 * 1024 + 6 * 512) * 1024 )) |
| 31 | avail=$(df -Pk /var/lib/nginx | awk 'NR == 2 { print $4 }') |
| 32 | [ "${avail:-0}" -ge "$need" ] || die "only ${avail:-?} KiB free under /var/lib/nginx, need $need KiB" |
| 33 | code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 -H 'X-Forge-Base: /' http://127.0.0.1:18480/drakestapleton) |
| 34 | [ "$code" = 200 ] || die "forged via tunnel (hub 127.0.0.1:18480) answered '$code', want 200" |
| 35 | nginx -t -q || die "nginx -t fails BEFORE any change; fix that first" |
| 36 | |
| 37 | # ---- backup (same layout as forge-edge-apply.sh, so forge-edge-rollback.sh restores it) ---- |
| 38 | mkdir -p "$BK" |
| 39 | cp -a $N/sites-enabled/interplane.io "$BK/sites-enabled_interplane.io" |
| 40 | cp -a $N/sites-enabled/aienos.com "$BK/sites-enabled_aienos.com" |
| 41 | cp -a $N/stream.d/sni-router.conf "$BK/stream.d_sni-router.conf" |
| 42 | : > "$BK/NEW_FILES" |
| 43 | for f in $FILES; do cp -a "$N/$f" "$BK/$(echo "$f" | tr / _)"; done |
| 44 | echo "forge edge update $TS from $SRC (was $OLD)" > "$BK/README" |
| 45 | echo "backup: $BK" |
| 46 | |
| 47 | # ---- install ---- |
| 48 | for f in $FILES; do install -m 0644 "$SRC/nginx/$f" "$N/$f"; done |
| 49 | |
| 50 | # ---- gate ---- |
| 51 | if ! nginx -t; then echo "nginx -t FAILED after install" >&2; "$HERE/forge-edge-rollback.sh" "$BK"; exit 1; fi |
| 52 | ${FORGE_RELOAD:-systemctl reload nginx} || { "$HERE/forge-edge-rollback.sh" "$BK"; exit 1; } # FORGE_RELOAD: container tests only |
| 53 | sleep 2 |
| 54 | |
| 55 | # ---- live checks through the real :443 router ---- |
| 56 | if ! bash "$HERE/forge-edge-checks.sh"; then echo "live checks failed: rolling back" >&2; "$HERE/forge-edge-rollback.sh" "$BK"; exit 1; fi |
| 57 | echo "UPDATE OK $TS (rollback: sudo bash $HERE/forge-edge-rollback.sh $BK)" |