Files
CatoandClaude 4c78b0dc3b fix(previews): a preview address must answer, and is kept answering (#1195)
francesco.orangecat.ch showed ERR_SSL_PROTOCOL_ERROR: wildcard DNS sends every
label to the box, but a label with no Caddy vhost has no certificate. Found on
a phone four times.

- createPreview waits until the address answers over HTTPS and says plainly
  when it does not, instead of reporting success on hope
- previews.yml + ensure-preview.sh: `create` makes a preview as the operator on
  the box and fails unless its address answers; `heal` (every six hours)
  re-attaches and re-checks every preview folder so a lost vhost is restored
  before anyone opens the link. No access code goes through a public repo's
  Actions
- A preview with no code yet answers 503 ("switched off"): the address and
  certificate work, the code is set in the app
- set_preview_code chat tool ("set the preview code to cat"); create_website
  still insists on a code
- Tests: ensure scripts parse, fail loudly, run on a clock, carry no code


Claude-Session: https://claude.ai/code/session_01XpakL9C3TESU4H45fXMFfc

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-14 10:58:18 -07:00

74 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Full-fleet verification sweep: systemd state + public HTTPS for every app
# in apps.conf plus the four pre-existing services. Exit 1 if anything fails.
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
source "$(dirname "${BASH_SOURCE[0]}")/../_brand.sh" # APP_SLUG / APP_DOMAIN / BRIDGE_DOMAIN
set +e # we aggregate failures ourselves; set -e from lib.sh aborts the sweep
fail=0
check() { # name domain port
local name=$1 domain=$2 port=$3 svc state code pub
svc="$name-app"
case "$name" in loki-bridge) svc="loki-bridge";; esac
state=$(box "systemctl is-active $svc" 2>/dev/null | tr -d '[:space:]')
code=$(box "curl -s -o /dev/null -w '%{http_code}' --max-time 10 http://localhost:$port/" 2>/dev/null | tr -d '[:space:]')
if [ "$domain" != "-" ]; then
pub=$(curl -sL -o /dev/null -w '%{http_code}' --max-time 20 "https://${domain%%,*}/" 2>/dev/null)
else
pub="n/a"
fi
local ok="OK "
if [ "$state" != "active" ]; then ok="FAIL"; fail=1; fi
if [ "$pub" != "n/a" ] && [ "$pub" != "200" ]; then ok="FAIL"; fail=1; fi
printf "%-4s %-22s systemd=%-8s local=%-4s public=%s\n" "$ok" "$name" "$state" "$code" "$pub"
}
# Pre-existing services (handcrafted units).
# The bridge serves SSE endpoints only — any HTTP answer (even 404 on /)
# proves liveness, so it gets its own check.
bridge_state=$(box "systemctl is-active loki-bridge" 2>/dev/null | tr -d '[:space:]')
bridge_pub=$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 "https://${BRIDGE_DOMAIN}/" 2>/dev/null)
if [ "$bridge_state" = "active" ] && [ "$bridge_pub" != "000" ]; then
printf "%-4s %-22s systemd=%-8s public=%s (SSE service, 404 on / is normal)\n" "OK " loki-bridge "$bridge_state" "$bridge_pub"
else
printf "%-4s %-22s systemd=%-8s public=%s\n" FAIL loki-bridge "$bridge_state" "$bridge_pub"; fail=1
fi
check "$APP_SLUG" "$APP_DOMAIN" 4002
check orangecat orangecat.ch 4003
check evig evig.orangecat.ch 4004
# Manifest apps
# The trailing `rest` is load-bearing: bash assigns the remainder of the line to
# the LAST variable, so without it $db reads "-|S-Ink|client-site|live|-|-|-" on
# every row. Harmless only for as long as this loop keeps ignoring $db — which
# is precisely the kind of latent trap app_lookup() in lib.sh already documents
# having been bitten by. Named here rather than left for the next reader.
while IFS='|' read -r name port domains repo app_dir db rest; do
case "$name" in \#*|"") continue;; esac
check "$name" "$domains" "$port"
done < <(grep -v '^#' "$MANIFEST")
# Supabase stack
sb=$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 https://supabase.orangecat.ch/auth/v1/settings -H "apikey: invalid" 2>/dev/null)
printf "%-4s %-22s public=%s (401/200 expected)\n" "$([ "$sb" = 401 ] || [ "$sb" = 200 ] && echo OK || { echo FAIL; fail=1; })" "supabase-stack" "$sb"
# SSE-block guard: the bridge (4001) MUST NOT have `encode` — compression
# buffers the event stream so the disconnect never fires and presence goes
# stale. See scripts/hetzner/box-units/Caddyfile.snippet + connection-presence.
enc=$(box "awk '/bridge\\.orangecat\\.ch/,/^}/' /etc/caddy/Caddyfile 2>/dev/null | grep -c '^[[:space:]]*encode'" 2>/dev/null | tr -d '[:space:]')
printf "%-4s %-22s %s\n" "$([ "${enc:-1}" = 0 ] && echo 'OK ' || { echo FAIL; fail=1; })" "bridge-sse-no-encode" "encode lines in bridge block: ${enc:-?}"
# Restart-window guard: a proxied block without `lb_try_duration` turns every
# deploy into user-visible 502s, because Caddy gives up the instant the
# restarting upstream refuses a connection. This was measured at 784 502s in 7
# days on loki alone, all inside restart windows, and it is invisible
# without this check — the app is healthy before and after, so nothing else
# reports it. Covers the two blocks this repo owns in the handcrafted
# Caddyfile; the rest are generated by sync-infra.sh, which emits the directive.
for blk in bridge loki; do
n=$(box "awk '/^${blk}\\.orangecat\\.ch/,/^}/' /etc/caddy/Caddyfile 2>/dev/null | grep -c 'lb_try_duration'" 2>/dev/null | tr -d '[:space:]')
printf "%-4s %-22s %s\n" "$([ "${n:-0}" -ge 1 ] 2>/dev/null && echo 'OK ' || { echo FAIL; fail=1; })" "${blk}-restart-retry" "lb_try_duration in ${blk} block: ${n:-?}"
done
exit $fail