interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / hub / forge-edge-update.sh

57 lines · 3483 bytes Raw Blame History
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>.
10set -u
11SRC=${1:?usage: forge-edge-update.sh <new infra dir> <deployed infra dir>}
12OLD=${2:?usage: forge-edge-update.sh <new infra dir> <deployed infra dir>}
13N=/etc/nginx
14TS=$(date -u +%Y%m%dT%H%M%SZ)
15BK=$N/forge-backups/$TS
16HERE=$(cd "$(dirname "$0")" && pwd)
17FILES="conf.d/forge.conf snippets/forge-proxy.conf snippets/forge-headers.conf snippets/forge-archive.conf snippets/aienos-forge.conf sites-enabled/interplane.io"
18die() { echo "UPDATE ABORTED (nothing changed): $*" >&2; exit 1; }
19[ "$(id -u)" = 0 ] || die "run with sudo"
20
21# ---- preconditions (no change made yet) ----
22for 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"
25done
26[ -x "$HERE/forge-edge-rollback.sh" ] || die "missing $HERE/forge-edge-rollback.sh"
27grep -q 'server 127.0.0.1:4448;' $N/stream.d/sni-router.conf || die "forge edge not applied: router is not on 4448"
28grep -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.
30need=$(( (3 * 1024 + 6 * 512) * 1024 ))
31avail=$(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"
33code=$(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"
35nginx -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) ----
38mkdir -p "$BK"
39cp -a $N/sites-enabled/interplane.io "$BK/sites-enabled_interplane.io"
40cp -a $N/sites-enabled/aienos.com "$BK/sites-enabled_aienos.com"
41cp -a $N/stream.d/sni-router.conf "$BK/stream.d_sni-router.conf"
42: > "$BK/NEW_FILES"
43for f in $FILES; do cp -a "$N/$f" "$BK/$(echo "$f" | tr / _)"; done
44echo "forge edge update $TS from $SRC (was $OLD)" > "$BK/README"
45echo "backup: $BK"
46
47# ---- install ----
48for f in $FILES; do install -m 0644 "$SRC/nginx/$f" "$N/$f"; done
49
50# ---- gate ----
51if ! 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
53sleep 2
54
55# ---- live checks through the real :443 router ----
56if ! bash "$HERE/forge-edge-checks.sh"; then echo "live checks failed: rolling back" >&2; "$HERE/forge-edge-rollback.sh" "$BK"; exit 1; fi
57echo "UPDATE OK $TS (rollback: sudo bash $HERE/forge-edge-rollback.sh $BK)"