interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / test / hub-rehearsal-inner.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Runs INSIDE the throwaway container started by hub-rehearsal.sh. Never run on the hub. |
| 3 | set -u |
| 4 | PASS=0; FAIL=0 |
| 5 | ok() { PASS=$((PASS+1)); echo "PASS $*"; } |
| 6 | bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; } |
| 7 | [ -f /.dockerenv ] || { echo "refusing: not in a container"; exit 2; } |
| 8 | export DEBIAN_FRONTEND=noninteractive |
| 9 | apt-get update -qq >/dev/null && apt-get install -y -qq nginx libnginx-mod-stream curl python3 openssl iproute2 ca-certificates >/dev/null 2>&1 \ |
| 10 | || { echo "apt install failed"; exit 2; } |
| 11 | echo "container nginx: $(nginx -v 2>&1)" |
| 12 | |
| 13 | # Hub /etc/nginx (certs excluded) + stand-ins for certs, web roots, headscale, forged. |
| 14 | rm -rf /etc/nginx && tar -C /etc -xf /w/etc-nginx.tar |
| 15 | if [ -s /w/pre-apply.tar ]; then # the hub copy already carries the edge: undo it from the first apply's backup |
| 16 | mkdir -p /tmp/pre && tar -C /tmp/pre -xf /w/pre-apply.tar && B=$(ls -d /tmp/pre/*/ | head -1) |
| 17 | cp "$B/sites-enabled_interplane.io" /etc/nginx/sites-enabled/interplane.io |
| 18 | cp "$B/sites-enabled_aienos.com" /etc/nginx/sites-enabled/aienos.com |
| 19 | cp "$B/stream.d_sni-router.conf" /etc/nginx/stream.d/sni-router.conf |
| 20 | while read -r f; do [ -n "$f" ] && rm -f -- "$f"; done < "$B/NEW_FILES" |
| 21 | echo "rebuilt the pre-apply hub state from backup $(basename "$B")" |
| 22 | fi |
| 23 | for d in aienos.com interplane.io www.aienos.com; do |
| 24 | mkdir -p /etc/nginx/certs/$d |
| 25 | openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=$d" -addext "subjectAltName=DNS:$d,DNS:www.$d" \ |
| 26 | -keyout /etc/nginx/certs/$d/key.pem -out /etc/nginx/certs/$d/fullchain.pem >/dev/null 2>&1 |
| 27 | done |
| 28 | mkdir -p /var/www/interplane.io/quick-start /var/www/aienos.com/current /var/www/replicate /var/www/si /var/www/aienos-hive /var/log/nginx |
| 29 | echo IP-HOME > /var/www/interplane.io/index.html; echo IP-QS > /var/www/interplane.io/quick-start/index.html |
| 30 | echo AE-HOME > /var/www/aienos.com/current/index.html; echo AE-404 > /var/www/aienos.com/current/404.html |
| 31 | for f in /var/www/replicate/AGENT-PROMPT.md /var/www/replicate/replicate.sh /var/www/si/si.sh /var/www/aienos-hive/live.json; do echo x > $f; done |
| 32 | openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=hs.aienos.com" -addext "subjectAltName=DNS:hs.aienos.com" -keyout /tmp/hs.key -out /tmp/hs.pem >/dev/null 2>&1 |
| 33 | # The apply/rollback scripts verify TLS like a browser (no -k): trust the stand-in certs here. |
| 34 | for d in aienos.com interplane.io www.aienos.com; do cp /etc/nginx/certs/$d/fullchain.pem /usr/local/share/ca-certificates/rehearsal-$d.crt; done |
| 35 | cp /tmp/hs.pem /usr/local/share/ca-certificates/rehearsal-hs.crt; update-ca-certificates >/dev/null 2>&1 |
| 36 | cat > /tmp/stand.py <<'EOF' |
| 37 | import ssl, sys, threading |
| 38 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 39 | class F(BaseHTTPRequestHandler): # forged stand-in: 404 for withdrawn repos |
| 40 | protocol_version = "HTTP/1.0" |
| 41 | def log_message(self, *a): pass |
| 42 | def do_GET(self): |
| 43 | bad = "z2ft64" in self.path or "omega-pearl" in self.path |
| 44 | b = ("FORGED path=%s base=%s realip=%s\n" % (self.path, self.headers.get("X-Forge-Base"), self.headers.get("X-Real-IP"))).encode() |
| 45 | self.send_response(404 if bad else 200); self.send_header("Content-Length", str(len(b))); self.end_headers(); self.wfile.write(b) |
| 46 | class H(F): |
| 47 | def do_GET(self): |
| 48 | self.send_response(200); self.send_header("Content-Length", "2"); self.end_headers(); self.wfile.write(b"ok") |
| 49 | f = ThreadingHTTPServer(("127.0.0.1", 18480), F) |
| 50 | h = ThreadingHTTPServer(("127.0.0.1", 4443), H) |
| 51 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); ctx.load_cert_chain("/tmp/hs.pem", "/tmp/hs.key") |
| 52 | h.socket = ctx.wrap_socket(h.socket, server_side=True) |
| 53 | threading.Thread(target=h.serve_forever, daemon=True).start() |
| 54 | f.serve_forever() |
| 55 | EOF |
| 56 | python3 /tmp/stand.py 2>/dev/null & SPID=$! |
| 57 | sleep 1 |
| 58 | nginx -t -q && nginx || { echo "hub config does not start in container"; nginx -t; exit 2; } |
| 59 | sleep 1 |
| 60 | export FORGE_RELOAD="nginx -s reload" |
| 61 | R() { curl -sk -o /dev/null -w '%{http_code}' --max-time 10 --resolve "$1:443:127.0.0.1" "https://$1$2"; } |
| 62 | B() { curl -sk --max-time 10 --resolve "$1:443:127.0.0.1" "https://$1$2"; } |
| 63 | snap() { (cd /etc/nginx && find . -path ./forge-backups -prune -o -type f -print0 | sort -z | xargs -0 sha256sum); } |
| 64 | |
| 65 | [ "$(R interplane.io /)" = 200 ] && [ "$(R www.aienos.com /)" = 200 ] && [ "$(R hs.aienos.com /health)" = 200 ] \ |
| 66 | && ok "baseline: hub config serves interplane.io, www.aienos.com, hs in container" || bad "baseline sites" |
| 67 | [ "$(R www.aienos.com /forge/)" = 404 ] && ok "baseline: /forge/ is 404 before apply" || bad "baseline /forge/ not 404" |
| 68 | snap > /tmp/pristine |
| 69 | |
| 70 | # N1: gate - a config that fails nginx -t is rolled back automatically. |
| 71 | mkdir -p /tmp/broken && cp -a /infra/. /tmp/broken/ && echo 'this is not nginx syntax' >> /tmp/broken/nginx/conf.d/forge.conf |
| 72 | bash /infra/hub/forge-edge-apply.sh /tmp/broken > /tmp/n1.log 2>&1; rc=$? |
| 73 | [ $rc != 0 ] && grep -q 'nginx -t FAILED' /tmp/n1.log && grep -q 'ROLLBACK OK' /tmp/n1.log && ok "N1 broken config: apply refused + auto-rollback" || { bad "N1 rc=$rc"; cat /tmp/n1.log; } |
| 74 | snap | diff -q /tmp/pristine - >/dev/null && ok "N1 config identical to pristine after auto-rollback" || bad "N1 config drift" |
| 75 | |
| 76 | # N2: precondition - forged/tunnel not answering -> abort with nothing changed. |
| 77 | kill -STOP $SPID |
| 78 | bash /infra/hub/forge-edge-apply.sh /infra > /tmp/n2.log 2>&1; rc=$? |
| 79 | kill -CONT $SPID |
| 80 | [ $rc != 0 ] && grep -q 'APPLY ABORTED (nothing changed): forged via tunnel' /tmp/n2.log && ok "N2 forged down: apply aborted" || { bad "N2 rc=$rc"; cat /tmp/n2.log; } |
| 81 | snap | diff -q /tmp/pristine - >/dev/null && ok "N2 nothing changed" || bad "N2 config drift" |
| 82 | |
| 83 | # P1: the real apply. |
| 84 | bash /infra/hub/forge-edge-apply.sh /infra > /tmp/p1.log 2>&1; rc=$? |
| 85 | cat /tmp/p1.log | sed 's/^/ apply| /' |
| 86 | [ $rc = 0 ] && grep -q '^APPLY OK' /tmp/p1.log && ok "P1 apply OK with live checks" || bad "P1 apply rc=$rc" |
| 87 | b=$(B interplane.io /drakestapleton/cuda-crack); case "$b" in *"base=/ realip=127.0.0.1"*) ok "P1 interplane.io -> forged base=/ with client IP";; *) bad "P1 interplane body: $b";; esac |
| 88 | b=$(B www.aienos.com /forge/drakestapleton/cuda-crack); case "$b" in *"path=/drakestapleton/cuda-crack base=/forge/ realip=127.0.0.1"*) ok "P1 aienos /forge/ -> forged stripped, base=/forge/";; *) bad "P1 aienos body: $b";; esac |
| 89 | [ "$(B interplane.io /quick-start/)" = IP-QS ] && ok "P1 interplane static still served" || bad "P1 interplane static" |
| 90 | [ "$(B www.aienos.com /)" = AE-HOME ] && [ "$(R hs.aienos.com /health)" = 200 ] && ok "P1 aienos home + headscale unaffected" || bad "P1 aienos/hs" |
| 91 | h=$(curl -sk -D - -o /dev/null --resolve interplane.io:443:127.0.0.1 https://interplane.io/quick-start | tr A-Z a-z) |
| 92 | case "$h" in *"location: /quick-start/"*) ok "P1 :4444 port leak fixed (relative redirect)";; *) bad "P1 redirect: $(echo "$h" | grep location)";; esac |
| 93 | snap > /tmp/applied |
| 94 | |
| 95 | # N3: second apply refuses (idempotence guard) and changes nothing. |
| 96 | bash /infra/hub/forge-edge-apply.sh /infra > /tmp/n3.log 2>&1; rc=$? |
| 97 | [ $rc != 0 ] && grep -q 'APPLY ABORTED' /tmp/n3.log && ok "N3 re-apply refused" || { bad "N3 rc=$rc"; cat /tmp/n3.log; } |
| 98 | snap | diff -q /tmp/applied - >/dev/null && ok "N3 nothing changed" || bad "N3 config drift" |
| 99 | |
| 100 | # P2: rollback restores the exact pre-apply tree. |
| 101 | bash /infra/hub/forge-edge-rollback.sh > /tmp/p2.log 2>&1; rc=$? |
| 102 | cat /tmp/p2.log | sed 's/^/ rollback| /' |
| 103 | [ $rc = 0 ] && ok "P2 rollback OK" || bad "P2 rollback rc=$rc" |
| 104 | snap | diff /tmp/pristine - && ok "P2 config byte-identical to pristine" || bad "P2 config differs from pristine" |
| 105 | [ "$(R www.aienos.com /forge/)" = 404 ] && [ "$(R interplane.io /)" = 200 ] && ok "P2 /forge/ gone, interplane.io home back" || bad "P2 live state" |
| 106 | |
| 107 | kill $SPID 2>/dev/null; nginx -s quit 2>/dev/null |
| 108 | echo "hub-rehearsal: $PASS passed, $FAIL failed" |
| 109 | [ "$FAIL" = 0 ] |