Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/infra/spark/edge-http-apply.sh # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / infra / spark / edge-http-apply.sh` 103 lines ยท 5675 bytes Raw Blame History 1 #!/usr/bin/env bash 2 # Spark front door: answer public http (TCP 80) with a 301 to https. 3 # forge-iplreach (L1008 QW1, card t_`0ef9cf5d`). Source of record: infra/spark/README-edge.md, "Plain http". 4 # 5 # sudo bash infra/spark/edge-http-apply.sh (from ~/work/forge/web, as the Spark user via sudo) 6 # 7 # Order, stopping at the first failure: 8 # 1. preconditions (nothing changed yet): files present, not applied before, the :443 front-door rule 9 # present exactly once, port 4480 free, 192.168.1.108 on this host, `nginx -t` and `nft -c` pass; 10 # 2. build the new firewall file from the live one (two insertions, nothing else) and `nft -c` it; 11 # 3. back up /etc/atlas/ingress.nft to /etc/nginx/forge-backups/http80-/ (+ NEW_FILES, README); 12 # 4. install sites-enabled/http-redirect, `nginx -t`, `systemctl reload nginx` (a reload, never a restart), 13 # wait for 192.168.1.108:4480 and check the redirect on it directly; 14 # 5. install the new firewall file and load it with `nft -f` (the same command atlas-ingress.service runs); 15 # 6. live checks from the house LAN (spark/edge-http-checks.sh, run as $SUDO_USER, who has the hub key). 16 # Any failure after step 3 runs spark/edge-http-rollback.sh on the backup. 17 set -euo pipefail 18 INFRA=$(cd "${1:?usage: edge-http-apply.sh }" && pwd) 19 NFT=${EDGE_NFT:-/etc/atlas/ingress.nft} 20 SITE_SRC=$INFRA/spark/nginx/sites-http-redirect 21 SITE_DST=${EDGE_SITE_DST:-/etc/nginx/sites-enabled/http-redirect} 22 BKROOT=${EDGE_BKROOT:-/etc/nginx/forge-backups} 23 ADDR=192.168.1.108 24 PORT=4480 25 RULE443='iifname "enP7s7" ip daddr 192.168.1.108 tcp dport 443 accept' 26 STAMP=$(date -u +%Y%m%dT%H%M%SZ) 27 BK=$BKROOT/http80-$STAMP 28 die() { echo "edge-http-apply: FAIL: $*" >&2; exit 1; } 29 say() { echo "edge-http-apply: $*"; } 30 31 [ "$(id -u)" = 0 ] || die "run as root (sudo)" 32 # 1. preconditions 33 for f in "$SITE_SRC" "$INFRA/spark/edge-http-nft.awk" "$INFRA/spark/edge-http-rollback.sh" "$INFRA/spark/edge-http-checks.sh"; do 34 [ -f "$f" ] || die "missing $f" 35 done 36 [ ! -e "$SITE_DST" ] || die "$SITE_DST exists (already applied? roll back first)" 37 grep -q 'http80_redirect' "$NFT" && die "$NFT already has chain http80_redirect" 38 [ "$(grep -cF "$RULE443" "$NFT")" = 1 ] || die "the :443 front-door rule is not in $NFT exactly once" 39 [ "$(tail -n 1 "$NFT")" = "}" ] || die "$NFT does not end with the table's closing brace" 40 [ "$(grep -c '^table ' "$NFT")" = 1 ] || die "$NFT has more than one table" 41 if ss -ltnH "sport = :$PORT" | grep -q .; then die "port $PORT is in use"; fi 42 ip -4 addr show | grep -q "inet $ADDR/" || die "$ADDR is not an address of this host" 43 nginx -t -q 2>/dev/null || die "nginx -t fails before any change" 44 nft -c -f "$NFT" || die "live $NFT does not pass nft -c before any change" 45 say "preconditions ok" 46 47 # 2. new firewall file: the 4480 accept right after the :443 rule, and the redirect chain before the 48 # table's closing brace. Every other line is byte-identical (checked below). 49 NEW=$(mktemp) 50 trap 'rm -f "$NEW"' EXIT 51 awk -f "$INFRA/spark/edge-http-nft.awk" "$NFT" > "$NEW" 52 added=$(diff "$NFT" "$NEW" | grep -c '^>' || true) 53 removed=$(diff "$NFT" "$NEW" | grep -c '^<' || true) 54 [ "$added" = 5 ] && [ "$removed" = 0 ] || die "new firewall file differs by +$added/-$removed lines (want +5/-0)" 55 nft -c -f "$NEW" || die "new firewall file fails nft -c" 56 say "new firewall file ok (+5 lines, nft -c passes)" 57 58 # 3. backup 59 mkdir -p "$BK" 60 cp -p "$NFT" "$BK/ingress.nft" 61 printf '%s\n' "$SITE_DST" > "$BK/NEW_FILES" 62 cat > "$BK/README" <&2 71 bash "$INFRA/spark/edge-http-rollback.sh" "$BK" >&2 || echo "edge-http-apply: ROLLBACK FAILED, see above" >&2 72 } 73 74 # 4. nginx 75 install -m 644 "$SITE_SRC" "$SITE_DST" || { rollback; die "install of the redirect site failed"; } 76 if ! nginx -t -q 2>/dev/null; then rollback; die "nginx -t fails with the new site"; fi 77 # set -e would exit here without rollback if the reload command itself fails. 78 if ! ${EDGE_RELOAD:-systemctl reload nginx}; then rollback; die "nginx reload failed"; fi 79 up=0 80 for _ in $(seq 1 20); do 81 if ss -ltnH "sport = :$PORT" | grep -q "$ADDR:$PORT"; then up=1; break; fi 82 sleep 0.5 83 done 84 [ "$up" = 1 ] || { rollback; die "nginx is not listening on $ADDR:$PORT after reload"; } 85 loc=$(curl -sS -o /dev/null -m 5 -w '%{http_code} %{redirect_url}' -H 'Host: interplane.io' "http://$ADDR:$PORT/quick-start/?a=1" || true) 86 [ "$loc" = "301 https://interplane.io/quick-start/?a=1" ] || { rollback; die "direct redirect check got '$loc'"; } 87 say "nginx reloaded; $ADDR:$PORT answers 301 https://interplane.io/quick-start/?a=1" 88 89 # 5. firewall 90 install -m 600 "$NEW" "$NFT" || { rollback; die "install of the firewall file failed"; } 91 if ! nft -f "$NFT"; then rollback; die "nft -f of the new file failed"; fi 92 nft list chain inet atlas_ingress http80_redirect > /dev/null || { rollback; die "chain http80_redirect not live"; } 93 say "firewall loaded (chain http80_redirect live)" 94 95 # 6. live checks from the LAN (EDGE_CHECKS: the rehearsal runs its own command, as root, instead) 96 if [ -n "${EDGE_CHECKS:-}" ]; then 97 if ! bash -c "$EDGE_CHECKS"; then rollback; die "live checks failed"; fi 98 else 99 U=${SUDO_USER:-} 100 [ -n "$U" ] || { rollback; die "no SUDO_USER to run the LAN checks as"; } 101 if ! sudo -u "$U" -H bash "$INFRA/spark/edge-http-checks.sh"; then rollback; die "live checks failed"; fi 102 fi 103 say "APPLIED. Backup $BK"