claude/skills/web-project-scaffold-containerized-python/scripts/verify.sh
Neil Bantoc 5201aa9bd5 Add web-frontend, web-project-audit and web-project-scaffold-containerized-python skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 16:26:33 -08:00

153 lines
10 KiB
Bash
Executable file

#!/usr/bin/env bash
# Prove the scaffold's contract end to end, on a throwaway copy (the project itself, its
# .env and its git history are not touched):
# 1. the image builds and the container comes up healthy, cloning from a "remote" repo
# named in .env
# 2. the tests pass in the dev setup
# 3. `docker exec <c> update` deploys a new commit (with a requirements change) without
# rebuilding the image or restarting the container
# 4. an update to code that fails to import is refused; the old commit keeps serving
# 5. an update to code that crashes at startup is rolled back; the old commit keeps serving
# 6. `update --rollback` returns to the commit before the last successful update
# 7. install.sh, piped into bash as on a fresh server, clones from a git:// remote, writes
# .env (uid 1000, mode 640), and brings the app up healthy; a re-run is safe and only
# replaces the .env keys passed to it. Docker is not reinstalled (it is present here).
# verify.sh <project-dir> [--keep] # --keep leaves the containers and temp dir for debugging
set -uo pipefail
project=$(cd "${1:?usage: verify.sh <project-dir> [--keep]}" && pwd)
keep=0; [ "${2:-}" = --keep ] && keep=1
W=$(mktemp -d "${TMPDIR:-/tmp}/scaffold-verify.XXXXXX")
P="verify-$(basename "$project" | tr -cd 'a-z0-9-')-$$"
C="$P-app"
branch=$(sed -n 's/^REPO_REF=//p' "$project/.env.example"); branch=${branch:-main}
fails=0
step() { printf '\n== %s\n' "$*"; }
pass() { printf ' PASS %s\n' "$*"; }
fail() { printf ' FAIL %s\n' "$*"; fails=$((fails + 1)); }
dc() { docker compose -p "$P" -f "$W/work/compose.yml" -f "$W/verify.yml" "$@"; }
cleanup() {
if [ "$keep" = 1 ]; then echo "kept: $W, compose project $P (docker compose -p $P down -v)"; return; fi
dc down -v --rmi local >/dev/null 2>&1
docker compose -p "$P-dev" -f "$W/work/compose.yml" -f "$W/work/compose.dev.yml" -f "$W/verify-dev.yml" down -v >/dev/null 2>&1
docker image rm -f "$P:latest" >/dev/null 2>&1
[ -f "$W/opt/compose.yml" ] && (cd "$W/opt" && docker compose down -v --rmi local >/dev/null 2>&1)
[ -n "${daemon:-}" ] && kill "$daemon" 2>/dev/null
rm -rf "$W"
}
trap cleanup EXIT
health() { docker exec "$C" python3 -c 'import json,urllib.request;print(json.load(urllib.request.urlopen("http://127.0.0.1:8000/healthz",timeout=2))["version"])' 2>/dev/null; }
wait_for() { # wait_for <version> <seconds>
for _ in $(seq 1 "$2"); do [ "$(health)" = "$1" ] && return 0; sleep 1; done; return 1; }
commit() { git -C "$W/work" add -A && git -C "$W/work" commit -qm "$1" && git -C "$W/work" push -q origin "$branch" && git -C "$W/work" rev-parse --short HEAD; }
step "copy project to $W and create a remote"
mkdir -p "$W/work"
tar -C "$project" --exclude=.git --exclude=.env --exclude=data -cf - . | tar -C "$W/work" -xf -
git -C "$W/work" init -q -b "$branch"
git -C "$W/work" -c user.name=verify -c user.email=verify@localhost add -A
git -C "$W/work" -c user.name=verify -c user.email=verify@localhost commit -qm "initial"
git clone -q --bare "$W/work" "$W/seed.git"
git -C "$W/work" remote add origin "$W/seed.git"
git -C "$W/work" config user.name verify; git -C "$W/work" config user.email verify@localhost
c1=$(git -C "$W/work" rev-parse --short HEAD)
sed -e "s|^REPO_URL=.*|REPO_URL=/seed.git|" -e "s|^REPO_REF=.*|REPO_REF=$branch|" \
-e "s|^GIT_TOKEN=.*|GIT_TOKEN=verify-dummy-token|" "$W/work/.env.example" > "$W/work/.env"
echo "VERIFY_SETTING=from-dotenv" >> "$W/work/.env"
chmod 600 "$W/work/.env"
cat > "$W/verify.yml" <<EOF
services:
app:
image: $P:latest
container_name: $C
environment:
CONTAINER_NAME: $C
ports: !reset []
volumes:
- $W/seed.git:/seed.git:ro
EOF
cat > "$W/verify-dev.yml" <<EOF
services:
app:
image: $P:latest
container_name: $C-dev
ports: !reset []
EOF
pass "remote at $W/seed.git, initial commit $c1"
step "1. build and start"
if dc up -d --build >"$W/build.log" 2>&1 && wait_for "$c1" 120; then pass "healthy at $c1 (cloned from REPO_URL in .env)"
else fail "did not come up healthy; build log and container log:"; tail -20 "$W/build.log"; docker logs --tail 40 "$C" 2>&1; exit 1; fi
image0=$(docker inspect -f '{{.Image}}' "$C"); started0=$(docker inspect -f '{{.State.StartedAt}}' "$C")
appenv=$(docker exec "$C" sh -c 'for d in /proc/[0-9]*; do grep -q uvicorn "$d/cmdline" 2>/dev/null && tr "\0" "\n" < "$d/environ"; done')
grep -qx 'VERIFY_SETTING=from-dotenv' <<<"$appenv" && pass "app sees settings from .env" || fail "app does not see VERIFY_SETTING from .env"
grep -q 'verify-dummy-token' <<<"$appenv" && fail "app process can see GIT_TOKEN" || pass "app process cannot see GIT_TOKEN"
docker exec "$C" grep -rqs 'verify-dummy-token' /srv/src/.git/config /srv/state && fail "token written to disk" || pass "token not written into the checkout or state"
[ "$(docker exec "$C" git -C /srv/src config --get remote.origin.url)" = /seed.git ] && pass "origin is the REPO_URL from .env" || fail "origin URL is not REPO_URL"
step "2. tests (dev setup)"
if docker compose -p "$P-dev" -f "$W/work/compose.yml" -f "$W/work/compose.dev.yml" -f "$W/verify-dev.yml" run --rm app pytest -q >"$W/test.log" 2>&1; then
pass "pytest: $(grep -E 'passed|failed' "$W/test.log" | tail -1)"
else fail "pytest failed:"; tail -30 "$W/test.log"; fi
step "3. update to a new commit"
echo "idna>=3" >> "$W/work/requirements.txt"
c2=$(commit "add a requirement")
if out=$(docker exec "$C" update 2>&1) && [ "$(health)" = "$c2" ]; then pass "update -> $c2"; else fail "update to $c2 failed:"; echo "$out"; fi
[ "$(docker inspect -f '{{.Image}}' "$C")" = "$image0" ] && pass "same image (no rebuild)" || fail "image changed"
[ "$(docker inspect -f '{{.State.StartedAt}}' "$C")" = "$started0" ] && pass "same container start time (no container restart)" || fail "container restarted"
docker exec "$C" /srv/venv/bin/python -c 'import idna' 2>/dev/null && pass "new requirement installed" || fail "new requirement missing"
out=$(docker exec "$C" update 2>&1); [[ $out == already* ]] && pass "second update is a no-op ($out)" || fail "second update: $out"
step "4. update to code that does not import"
cp "$W/work/app/main.py" "$W/main.py.good"
echo 'raise RuntimeError("broken on purpose by verify.sh")' >> "$W/work/app/main.py"
c3=$(commit "broken import")
if docker exec "$C" update >"$W/u4.log" 2>&1; then fail "update to broken $c3 reported success"; else pass "update refused (exit non-zero)"; fi
[ "$(health)" = "$c2" ] && pass "still serving $c2" || fail "serving '$(health)', expected $c2"
[ "$(docker exec "$C" git -C /srv/src rev-parse --short HEAD)" = "$c2" ] && pass "checkout back at $c2" || fail "checkout not restored"
step "5. update to code that crashes at startup"
cp "$W/main.py.good" "$W/work/app/main.py"
sed -i 's| DATA_DIR.mkdir(parents=True, exist_ok=True)| raise RuntimeError("startup failure on purpose by verify.sh")|' "$W/work/app/main.py"
grep -q 'startup failure on purpose' "$W/work/app/main.py" || fail "could not plant the startup failure"
c4=$(commit "broken startup")
if docker exec -e HEALTH_TIMEOUT=10 "$C" update >"$W/u5.log" 2>&1; then fail "update to crashing $c4 reported success"; else pass "update rolled back (exit non-zero)"; fi
wait_for "$c2" 30 && pass "still serving $c2" || { fail "serving '$(health)', expected $c2"; cat "$W/u5.log"; docker logs --tail 20 "$C"; }
[ "$(docker inspect -f '{{.State.StartedAt}}' "$C")" = "$started0" ] && pass "container stayed up" || fail "container restarted"
step "6. rollback"
cp "$W/main.py.good" "$W/work/app/main.py"; commit "fixed" >/dev/null
if docker exec "$C" update --rollback >"$W/u6.log" 2>&1 && [ "$(health)" = "$c1" ]; then pass "rolled back to $c1"; else fail "rollback failed:"; cat "$W/u6.log"; fi
step "7. install.sh on a fresh host"
iname="$P-i"
pname=$(sed -n 's/^name: //p' "$project/compose.yml")
mkdir -p "$W/isrc" "$W/bin"
tar -C "$project" --exclude=.git --exclude=.env --exclude=data -cf - . | tar -C "$W/isrc" -xf -
grep -rl -- "$pname" "$W/isrc" | xargs sed -i "s/$pname/$iname/g"
git -C "$W/isrc" init -q -b "$branch" && git -C "$W/isrc" add -A && git -C "$W/isrc" -c user.name=verify -c user.email=verify@localhost commit -qm initial
ic=$(git -C "$W/isrc" rev-parse --short HEAD)
git clone -q --bare "$W/isrc" "$W/serve/iseed.git"
gport=$(python3 -c 'import socket; s=socket.socket(); s.bind(("",0)); print(s.getsockname()[1])')
hport=$(python3 -c 'import socket; s=socket.socket(); s.bind(("",0)); print(s.getsockname()[1])')
hostip=$(ip -4 route get 1.1.1.1 2>/dev/null | sed -n 's/.* src \([0-9.]*\).*/\1/p')
git daemon --export-all --reuseaddr --listen=0.0.0.0 --port="$gport" --base-path="$W/serve" "$W/serve" >/dev/null 2>&1 & daemon=$!
sleep 1
printf '#!/bin/sh\necho "sudo was called: $*" >&2; exit 1\n' > "$W/bin/sudo"; chmod +x "$W/bin/sudo"
irun() { PATH="$W/bin:$PATH" INSTALL_DIR="$W/opt" REPO_URL="git://$hostip:$gport/iseed.git" "$@" bash < "$W/isrc/install.sh"; }
if irun env HOST_PORT="$hport" >"$W/i1.log" 2>&1; then pass "installer exit 0 (piped into bash, no sudo needed)"; else fail "installer failed:"; tail -30 "$W/i1.log"; fi
iv=$(docker exec "$iname" python3 -c 'import json,urllib.request;print(json.load(urllib.request.urlopen("http://127.0.0.1:8000/healthz",timeout=2))["version"])' 2>/dev/null)
[ "$iv" = "$ic" ] && pass "container $iname healthy at $ic" || fail "container not healthy at $ic (got '$iv')"
[ "$(stat -c '%u %a' "$W/opt/.env" 2>/dev/null)" = "1000 640" ] && pass ".env owned by uid 1000, mode 640" || fail ".env ownership/mode: $(stat -c '%u %a' "$W/opt/.env" 2>&1)"
grep -qx "REPO_URL=git://$hostip:$gport/iseed.git" "$W/opt/.env" && grep -qx "HOST_PORT=$hport" "$W/opt/.env" && pass ".env has REPO_URL and HOST_PORT" || fail ".env missing REPO_URL/HOST_PORT"
curl -fsS "http://127.0.0.1:$hport/healthz" >/dev/null && pass "reachable on 127.0.0.1:$hport" || fail "not reachable on host port $hport"
if PATH="$W/bin:$PATH" INSTALL_DIR="$W/opt" GIT_USERNAME=verify-user bash < "$W/isrc/install.sh" >"$W/i2.log" 2>&1; then pass "re-run exit 0"; else fail "re-run failed:"; tail -30 "$W/i2.log"; fi
grep -qx "GIT_USERNAME=verify-user" "$W/opt/.env" && grep -qx "HOST_PORT=$hport" "$W/opt/.env" && pass "re-run replaced only the passed key, kept the rest" || fail ".env after re-run: $(grep -E '^(GIT_USERNAME|HOST_PORT|REPO_URL)=' "$W/opt/.env")"
docker exec "$iname" update --check 2>&1 | grep -q "up to date" && pass "update works in the installed container" || fail "update --check in installed container"
(cd "$W/opt" && docker compose down -v --rmi local >/dev/null 2>&1)
echo
if [ "$fails" = 0 ]; then echo "VERIFY: PASS"; exit 0; fi
echo "VERIFY: FAIL ($fails)"; keep=1; exit 1