interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / hub-rehearsal-inner.sh

109 lines · 7780 bytes Raw Blame History
1#!/usr/bin/env bash
2# Runs INSIDE the throwaway container started by hub-rehearsal.sh. Never run on the hub.
3set -u
4PASS=0; FAIL=0
5ok() { PASS=$((PASS+1)); echo "PASS $*"; }
6bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
7[ -f /.dockerenv ] || { echo "refusing: not in a container"; exit 2; }
8export DEBIAN_FRONTEND=noninteractive
9apt-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; }
11echo "container nginx: $(nginx -v 2>&1)"
12
13# Hub /etc/nginx (certs excluded) + stand-ins for certs, web roots, headscale, forged.
14rm -rf /etc/nginx && tar -C /etc -xf /w/etc-nginx.tar
15if [ -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")"
22fi
23for 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
27done
28mkdir -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
29echo IP-HOME > /var/www/interplane.io/index.html; echo IP-QS > /var/www/interplane.io/quick-start/index.html
30echo AE-HOME > /var/www/aienos.com/current/index.html; echo AE-404 > /var/www/aienos.com/current/404.html
31for 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
32openssl 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.
34for 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
35cp /tmp/hs.pem /usr/local/share/ca-certificates/rehearsal-hs.crt; update-ca-certificates >/dev/null 2>&1
36cat > /tmp/stand.py <<'EOF'
37import ssl, sys, threading
38from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
39class 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)
46class 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")
49f = ThreadingHTTPServer(("127.0.0.1", 18480), F)
50h = ThreadingHTTPServer(("127.0.0.1", 4443), H)
51ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); ctx.load_cert_chain("/tmp/hs.pem", "/tmp/hs.key")
52h.socket = ctx.wrap_socket(h.socket, server_side=True)
53threading.Thread(target=h.serve_forever, daemon=True).start()
54f.serve_forever()
55EOF
56python3 /tmp/stand.py 2>/dev/null & SPID=$!
57sleep 1
58nginx -t -q && nginx || { echo "hub config does not start in container"; nginx -t; exit 2; }
59sleep 1
60export FORGE_RELOAD="nginx -s reload"
61R() { curl -sk -o /dev/null -w '%{http_code}' --max-time 10 --resolve "$1:443:127.0.0.1" "https://$1$2"; }
62B() { curl -sk --max-time 10 --resolve "$1:443:127.0.0.1" "https://$1$2"; }
63snap() { (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"
68snap > /tmp/pristine
69
70# N1: gate - a config that fails nginx -t is rolled back automatically.
71mkdir -p /tmp/broken && cp -a /infra/. /tmp/broken/ && echo 'this is not nginx syntax' >> /tmp/broken/nginx/conf.d/forge.conf
72bash /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; }
74snap | 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.
77kill -STOP $SPID
78bash /infra/hub/forge-edge-apply.sh /infra > /tmp/n2.log 2>&1; rc=$?
79kill -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; }
81snap | diff -q /tmp/pristine - >/dev/null && ok "N2 nothing changed" || bad "N2 config drift"
82
83# P1: the real apply.
84bash /infra/hub/forge-edge-apply.sh /infra > /tmp/p1.log 2>&1; rc=$?
85cat /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"
87b=$(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
88b=$(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"
91h=$(curl -sk -D - -o /dev/null --resolve interplane.io:443:127.0.0.1 https://interplane.io/quick-start | tr A-Z a-z)
92case "$h" in *"location: /quick-start/"*) ok "P1 :4444 port leak fixed (relative redirect)";; *) bad "P1 redirect: $(echo "$h" | grep location)";; esac
93snap > /tmp/applied
94
95# N3: second apply refuses (idempotence guard) and changes nothing.
96bash /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; }
98snap | diff -q /tmp/applied - >/dev/null && ok "N3 nothing changed" || bad "N3 config drift"
99
100# P2: rollback restores the exact pre-apply tree.
101bash /infra/hub/forge-edge-rollback.sh > /tmp/p2.log 2>&1; rc=$?
102cat /tmp/p2.log | sed 's/^/ rollback| /'
103[ $rc = 0 ] && ok "P2 rollback OK" || bad "P2 rollback rc=$rc"
104snap | 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
107kill $SPID 2>/dev/null; nginx -s quit 2>/dev/null
108echo "hub-rehearsal: $PASS passed, $FAIL failed"
109[ "$FAIL" = 0 ]