Add web-frontend, web-project-audit and web-project-scaffold-containerized-python skills

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Neil Bantoc 2026-11-04 14:46:25 -08:00
parent 59fc08fa5f
commit 5201aa9bd5
63 changed files with 3042 additions and 0 deletions

View file

@ -0,0 +1,31 @@
#!/usr/bin/env bash
# Check that this machine can build and run the scaffold. The host needs only Docker
# (with Compose v2 and BuildKit) and git; Python runs inside the container.
# Exits non-zero if something required is missing. Installs nothing.
ok=1
pass() { printf ' ok %s\n' "$*"; }
fail() { printf ' MISS %s\n' "$*"; ok=0; }
note() { printf ' note %s\n' "$*"; }
echo "Host tools"
if command -v git >/dev/null; then pass "git $(git --version | awk '{print $3}')"; else
fail "git: install it (e.g. sudo apt install git)"; fi
if command -v docker >/dev/null; then pass "docker $(docker --version | sed 's/Docker version //; s/,.*//')"; else
fail "docker: install Docker Engine (https://docs.docker.com/engine/install/)"; fi
if command -v docker >/dev/null; then
if docker compose version >/dev/null 2>&1; then pass "compose $(docker compose version --short)"; else
fail "docker compose v2 plugin: sudo apt install docker-compose-plugin"; fi
if docker buildx version >/dev/null 2>&1; then pass "buildx (BuildKit; needed for COPY --chmod)"; else
fail "docker buildx plugin: sudo apt install docker-buildx-plugin"; fi
if docker info >/dev/null 2>&1; then pass "daemon reachable as $(id -un)"; else
fail "cannot talk to the Docker daemon: start it, or add yourself to the docker group (sudo usermod -aG docker $USER, then log in again)"; fi
fi
echo "Notes"
note "host uid $(id -u): the image's app user defaults to uid 1000; dev bind mounts are easiest when they match"
command -v python3 >/dev/null && note "host python3 $(python3 -V 2>&1 | awk '{print $2}') is not used; Python runs in the container"
[ "$ok" = 1 ] && { echo "Ready."; exit 0; }
echo "Fix the MISS lines above, then re-run."
exit 1

View file

@ -0,0 +1,111 @@
#!/usr/bin/env bash
# Write a new containerized Python web project from templates/project.
# scaffold.sh --dir <path> --name <slug> --repo-url <url> [--title "My App"] [--python 3.14]
# [--port 8000] [--branch main] [--install-url <url>] [--no-resolve]
# --repo-url is required: the remote must exist, since servers install and update from it.
# --install-url is the raw URL of install.sh on the default branch; derived for GitHub and
# GitLab, required for other hosts.
# Resolves current fastapi/uvicorn/pytest/httpx2 versions inside python:<ver>-slim and pins
# them (==X.Y.*), unless --no-resolve (then the fallback pins below are used).
# Writes .env from .env.example, runs `git init` and adds the remote as origin.
# Does not commit, build or start anything; run verify.sh for that.
set -euo pipefail
SKILL_DIR=$(cd "$(dirname "$0")/.." && pwd)
TEMPLATE="$SKILL_DIR/templates/project"
TOKENS="$SKILL_DIR/../web-frontend/assets/tokens.css"
dir= name= title= python=3.14 port=8000 branch=main repo_url= install_url= resolve=1
while [ $# -gt 0 ]; do
case $1 in
--dir) dir=$2; shift ;;
--name) name=$2; shift ;;
--title) title=$2; shift ;;
--python) python=$2; shift ;;
--port) port=$2; shift ;;
--branch) branch=$2; shift ;;
--repo-url) repo_url=$2; shift ;;
--install-url) install_url=$2; shift ;;
--no-resolve) resolve=0 ;;
-h|--help) sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
shift
done
[ -n "$dir" ] && [ -n "$name" ] && [ -n "$repo_url" ] || { echo "usage: scaffold.sh --dir <path> --name <slug> --repo-url <url> [...]" >&2; exit 2; }
[[ $repo_url =~ ^(https://|ssh://|git@[^:]+:) ]] || { echo "--repo-url must be https://..., ssh://... or git@host:owner/repo.git" >&2; exit 2; }
[[ $repo_url =~ ://[^/]*@ ]] && { echo "--repo-url must not contain credentials; they go in the server's .env" >&2; exit 2; }
# Raw URL of install.sh, and the header a private repo's token goes in.
token_header="Authorization: Bearer "
if [[ $repo_url =~ github\.com[:/]([^/]+)/([^/]+)$ ]]; then
derived="https://raw.githubusercontent.com/${BASH_REMATCH[1]}/${BASH_REMATCH[2]%.git}/$branch/install.sh"
token_header="Authorization: token "
elif [[ $repo_url =~ gitlab\.com[:/](.+)/([^/]+)$ ]]; then
gl_path="${BASH_REMATCH[1]}/${BASH_REMATCH[2]%.git}"
derived="https://gitlab.com/api/v4/projects/${gl_path//\//%2F}/repository/files/install.sh/raw?ref=$branch"
token_header="PRIVATE-TOKEN: "
else derived=; fi
install_url=${install_url:-$derived}
[ -n "$install_url" ] || { echo "can't derive the raw install.sh URL for $repo_url; pass --install-url" >&2; exit 2; }
[[ $name =~ ^[a-z][a-z0-9-]*$ ]] || { echo "--name must be a lowercase slug (letters, digits, -)" >&2; exit 2; }
[ -n "$title" ] || title=$(echo "$name" | tr '-' ' ' | sed -E 's/(^| )([a-z])/\1\u\2/g')
if [ -e "$dir" ] && [ -n "$(ls -A "$dir" | grep -v '^\.git$' || true)" ]; then
echo "$dir exists and is not empty; refusing to overwrite" >&2; exit 1
fi
# Fallback pins, verified 2026-11 on Python 3.14.
fastapi=0.162 uvicorn=0.54 pytest=9.2 httpx=2.16
if [ "$resolve" = 1 ]; then
echo "resolving current versions in python:$python-slim ..."
if out=$(docker run --rm "python:$python-slim" sh -c \
'pip install -q --disable-pip-version-check --root-user-action=ignore fastapi "uvicorn[standard]" pytest httpx2 >/dev/null 2>&1 && pip freeze'); then
v() { echo "$out" | sed -n "s/^$1==\([0-9]*\.[0-9]*\).*/\1/p"; }
fastapi=$(v fastapi) uvicorn=$(v uvicorn) pytest=$(v pytest) httpx=$(v httpx2)
else
echo "could not resolve versions (offline, or no python:$python-slim image); using fallback pins" >&2
fi
fi
mkdir -p "$dir"
cp -r "$TEMPLATE/." "$dir/"
if [ -f "$TOKENS" ]; then
cp "$TOKENS" "$dir/app/static/css/tokens.css"
else
echo "web-frontend skill not found; writing minimal tokens.css" >&2
cat > "$dir/app/static/css/tokens.css" <<'CSS'
:root { color-scheme: light dark; --hue: 250; --bg: oklch(0.985 0.01 var(--hue)); --surface: oklch(1 0 0);
--surface-2: oklch(0.965 0.01 var(--hue)); --border: oklch(0.9 0.015 var(--hue)); --fg: oklch(0.22 0.02 var(--hue));
--fg-muted: oklch(0.45 0.02 var(--hue)); --ok: oklch(0.55 0.13 150); --danger: oklch(0.55 0.19 25);
--font-body: ui-sans-serif, system-ui, sans-serif; --font-mono: ui-monospace, Menlo, monospace;
--text-xs: .75rem; --text-lg: 1.125rem; --weight-medium: 500; --weight-semibold: 600; --measure: 65ch;
--space-2: .5rem; --space-4: 1rem; --space-5: 1.25rem; --space-6: 1.5rem; --space-12: 3rem;
--gutter: max(16px, 4vw); --content-max: 72rem; --radius-md: 8px; --radius-full: 999px; }
@media (prefers-color-scheme: dark) { :root { --bg: oklch(0.17 0.015 var(--hue)); --surface: oklch(0.21 0.015 var(--hue));
--surface-2: oklch(0.245 0.015 var(--hue)); --border: oklch(0.3 0.02 var(--hue)); --fg: oklch(0.95 0.01 var(--hue));
--fg-muted: oklch(0.74 0.015 var(--hue)); --ok: oklch(0.72 0.14 150); --danger: oklch(0.7 0.17 25); } }
body { margin: 0; background: var(--bg); color: var(--fg); font-family: var(--font-body); line-height: 1.6; }
.label { font-size: var(--text-xs); font-weight: var(--weight-semibold); text-transform: uppercase; letter-spacing: .06em; color: var(--fg-muted); }
CSS
fi
# Fill placeholders in every text file.
find "$dir" -type f ! -path '*/.git/*' -print0 | xargs -0 sed -i \
-e "s|__NAME__|$name|g" -e "s|__TITLE__|$title|g" -e "s|__PYTHON__|$python|g" \
-e "s|__PORT__|$port|g" -e "s|__BRANCH__|$branch|g" \
-e "s|__REPO_URL__|$repo_url|g" -e "s|__INSTALL_URL__|$install_url|g" -e "s|__TOKEN_HEADER__|$token_header|g" \
-e "s|__FASTAPI__|$fastapi.*|g" -e "s|__UVICORN__|$uvicorn.*|g" \
-e "s|__PYTEST__|$pytest.*|g" -e "s|__HTTPX__|$httpx.*|g"
chmod +x "$dir/container/entrypoint.sh" "$dir/container/update.sh" "$dir/install.sh"
# The machine's own .env: gitignored, read by the container.
cp "$dir/.env.example" "$dir/.env"
chmod 600 "$dir/.env"
[ -d "$dir/.git" ] || git -C "$dir" init -q -b "$branch"
git -C "$dir" remote get-url origin >/dev/null 2>&1 || git -C "$dir" remote add origin "$repo_url"
left=$(grep -rl '__[A-Z]*__' "$dir" --exclude-dir=.git || true)
[ -z "$left" ] || { echo "unfilled placeholders in: $left" >&2; exit 1; }
echo "scaffolded $name in $dir (Python $python, fastapi $fastapi, uvicorn $uvicorn)"
echo "install command (after the first push): curl -fsSL $install_url | bash"
echo "next: bash $SKILL_DIR/scripts/verify.sh $dir"

View file

@ -0,0 +1,153 @@
#!/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