From 7cbd8eb7da4ece3416a790dbd077ac416d84c0c6 Mon Sep 17 00:00:00 2001 From: darkstardevx <19244308+darkstardevx@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:51:09 -0800 Subject: [PATCH] fix(demo): reset unit dollar quoting, plus deploy follow-ups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Found while deploying the live demo on blackbox: - novastack-demo-reset.service: systemd unescapes $$ to $, so Postgres got `DO $ BEGIN` (syntax error) and the nightly reset failed. The SQL dollar quotes are now written $$$$. Verified live: items, todos and accounts wiped, ids restart at 1. - demo/tools/check-reset.py + CI step: runs the unit's ExecStart SQL the way systemd does ($$ -> $, quote-aware split, no shell expansion) against a throwaway Postgres, before and after the tables exist. It fails on the old unit with the same error seen live. - docker-smoke.sh: the WebSocket check forces --http1.1. Over https, curl negotiates HTTP/2, where WebSockets don't use Upgrade headers, so the check failed against the public hostname though WebSockets work (101 and an echoed frame through the tunnel). - demo/README: troubleshooting for a rootless netns left without a default route after a network change (the cause of the tunnel's DNS timeouts on first deploy), the /metrics route order, app restarts. 👻 Code & Design by Darkstardevx 🤖 Generated with Claude Code Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 3 + demo/README.md | 8 ++- demo/systemd/novastack-demo-reset.service | 7 +- demo/tools/check-reset.py | 85 +++++++++++++++++++++++ scripts/docker-smoke.sh | 2 +- 5 files changed, 100 insertions(+), 5 deletions(-) create mode 100755 demo/tools/check-reset.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12ef78f..e2debdb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -206,3 +206,6 @@ jobs: echo "$out" # Only judge our units: the runner's own system units print unrelated warnings. if echo "$out" | grep -E 'novastack-demo-reset|demo/systemd/' | grep -v 'Unit demo-postgres.service not found' | grep -q .; then exit 1; fi + - name: Reset SQL runs as systemd would run it + # Unescapes $$ like systemd, then runs it against a throwaway Postgres (before and after tables exist) + run: python3 demo/tools/check-reset.py docker diff --git a/demo/README.md b/demo/README.md index 0d28b2a..bde73cb 100644 --- a/demo/README.md +++ b/demo/README.md @@ -74,8 +74,14 @@ demo/install.sh # install or update the units, generate secrets once, | Update units after a `git pull` | `demo/install.sh` | | Stop and remove | `demo/install.sh --uninstall` (keeps the secrets and the `novastack-demo-pg` volume) | +## Troubleshooting + +- **Tunnel won't connect, `cloudflared` logs `dial udp 10.89.x.1:53: i/o timeout`, often after the host changed networks.** Rootless Podman routes every bridge-networked container through one shared network namespace, which copies the host's routes when it's created. After a network change it can be left without a default route. Check with `podman unshare --rootless-netns ip route`: there must be a `default via …` line. To fix it, stop every rootless container that uses a bridge network (the demo units plus any other stacks, e.g. `systemctl --user stop 'demo-*'`), confirm `podman ps` is empty, then start them again. The namespace is rebuilt with the current routes. +- **`/metrics` is public.** In the tunnel's Routes, the `^/metrics` → `http_status:404` route must be above the plain `rest-demo` route (routes match top to bottom). `cloudflared`'s log line `Updated to new configuration` lists the order it uses. +- **An app keeps restarting:** `journalctl --user -u demo-` and `podman logs demo-`. Its settings are in `~/.config/novastack-demo/.env`. + ## Checks -- **CI, every PR** (`demo units` job): Podman 5.8's Quadlet generator accepts every unit, all six services are generated, no unit publishes a port, `install.sh` passes shellcheck, and the reset units pass `systemd-analyze verify`. +- **CI, every PR** (`demo units` job): Podman 5.8's Quadlet generator accepts every unit, all six services are generated, no unit publishes a port, `install.sh` passes shellcheck, the reset units pass `systemd-analyze verify`, and the reset unit's SQL (after systemd's `$$` unescaping) runs against Postgres, both before and after the tables exist. - **Each image build:** the image runs read-only with no capabilities (and Postgres where needed) and passes the same HTTP checks as the Docker smoke tests. - **After deploying:** `curl -4 https://rest-demo.cybercoretech.net/health` and the other hostnames, plus `grpcurl grpc-demo.cybercoretech.net:443 list`. diff --git a/demo/systemd/novastack-demo-reset.service b/demo/systemd/novastack-demo-reset.service index bdb5422..c5852d3 100644 --- a/demo/systemd/novastack-demo-reset.service +++ b/demo/systemd/novastack-demo-reset.service @@ -5,6 +5,7 @@ After=demo-postgres.service [Service] Type=oneshot -# Tables exist once the apps have run their migrations; IF EXISTS keeps a fresh install quiet. -ExecStart=/usr/bin/podman exec demo-postgres psql -U novastack -d rest -v ON_ERROR_STOP=1 -c "DO $$ BEGIN IF to_regclass('public.items') IS NOT NULL THEN TRUNCATE items, users RESTART IDENTITY; END IF; END $$;" -ExecStart=/usr/bin/podman exec demo-postgres psql -U novastack -d htmx -v ON_ERROR_STOP=1 -c "DO $$ BEGIN IF to_regclass('public.todos') IS NOT NULL THEN TRUNCATE todos, users RESTART IDENTITY; END IF; END $$;" +# Tables exist once the apps have run their migrations; the check keeps a fresh install quiet. +# systemd turns $$ into $, so the SQL dollar quotes are written $$$$. +ExecStart=/usr/bin/podman exec demo-postgres psql -U novastack -d rest -v ON_ERROR_STOP=1 -c "DO $$$$ BEGIN IF to_regclass('public.items') IS NOT NULL THEN TRUNCATE items, users RESTART IDENTITY; END IF; END $$$$;" +ExecStart=/usr/bin/podman exec demo-postgres psql -U novastack -d htmx -v ON_ERROR_STOP=1 -c "DO $$$$ BEGIN IF to_regclass('public.todos') IS NOT NULL THEN TRUNCATE todos, users RESTART IDENTITY; END IF; END $$$$;" diff --git a/demo/tools/check-reset.py b/demo/tools/check-reset.py new file mode 100755 index 0000000..3227fa0 --- /dev/null +++ b/demo/tools/check-reset.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +"""Runs the reset unit's SQL against a throwaway Postgres container, the way systemd would. + + demo/tools/check-reset.py [docker|podman] + +systemd unescapes `$$` to `$` in ExecStart and splits words on quotes (no shell expansion), +so this does the same, then swaps `podman exec demo-postgres` for the chosen engine and a +temporary container. It checks the reset twice: on a fresh database (tables not created yet) +and after inserting rows (they must be gone). +""" +import shlex +import subprocess +import sys +import time +from pathlib import Path + +ENGINE = sys.argv[1] if len(sys.argv) > 1 else "docker" +UNIT = Path(__file__).resolve().parent.parent / "systemd" / "novastack-demo-reset.service" +NAME = f"reset-check-{int(time.time())}" +PREFIX = ["/usr/bin/podman", "exec", "demo-postgres"] + + +def run(*args, check=True): + return subprocess.run(args, check=check, capture_output=True, text=True) + + +def psql(db, sql): + return run(ENGINE, "exec", NAME, "psql", "-U", "novastack", "-d", db, "-tAc", sql).stdout.strip() + + +def reset_commands(): + commands = [] + for line in UNIT.read_text().splitlines(): + if line.startswith("ExecStart="): + argv = shlex.split(line[len("ExecStart="):].replace("$$", "$")) + assert argv[:3] == PREFIX, f"unexpected ExecStart: {argv[:3]}" + commands.append([ENGINE, "exec", NAME, *argv[3:]]) + assert len(commands) == 2, f"expected 2 ExecStart lines, found {len(commands)}" + return commands + + +def reset(): + for cmd in reset_commands(): + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode != 0: + sys.exit(f"reset failed: {result.stderr.strip()}") + + +def main(): + init = Path(__file__).resolve().parent.parent / "postgres" / "init.sql" + run(ENGINE, "run", "-d", "--name", NAME, + "-e", "POSTGRES_USER=novastack", "-e", "POSTGRES_PASSWORD=check", "-e", "POSTGRES_DB=rest", + "-v", f"{init}:/docker-entrypoint-initdb.d/init.sql:ro,Z", + "docker.io/library/postgres:17.10-alpine") + try: + for _ in range(60): + if run(ENGINE, "exec", NAME, "psql", "-U", "novastack", "-d", "htmx", "-tAc", "select 1", + check=False).returncode == 0: + break + time.sleep(1) + else: + sys.exit("postgres did not come up") + + reset() + print("ok reset on a fresh database (no tables yet)") + + psql("rest", "create table items(id bigserial primary key, name text);" + "create table users(id bigserial primary key, username text);" + "insert into items(name) values ('a'),('b'); insert into users(username) values ('u');") + psql("htmx", "create table todos(id bigserial primary key, title text);" + "create table users(id bigserial primary key, username text);" + "insert into todos(title) values ('t'); insert into users(username) values ('u');") + reset() + counts = [psql("rest", "select count(*) from items"), psql("rest", "select count(*) from users"), + psql("htmx", "select count(*) from todos"), psql("htmx", "select count(*) from users")] + assert counts == ["0"] * 4, f"rows left after reset: {counts}" + psql("rest", "insert into items(name) values ('c')") + assert psql("rest", "select max(id) from items") == "1", "ids did not restart at 1" + print("ok reset empties items, todos and users and restarts ids") + finally: + run(ENGINE, "rm", "-f", NAME, check=False) + + +if __name__ == "__main__": + main() diff --git a/scripts/docker-smoke.sh b/scripts/docker-smoke.sh index 2bf0da2..07df401 100755 --- a/scripts/docker-smoke.sh +++ b/scripts/docker-smoke.sh @@ -109,7 +109,7 @@ checks_realtime() { expect "publish accepted (202)" sh -c "[ \"\$(curl -s -o /dev/null -w %{http_code} -H 'content-type: application/json' -d '{\"message\":\"smoke-hello\"}' $b/publish)\" = 202 ]" wait "$sse" || true expect "SSE stream delivered the message" grep -q 'data: smoke-hello' "$events" - expect "websocket upgrade (101)" sh -c "[ \"\$(curl -s -o /dev/null -w %{http_code} --max-time 3 -H 'Connection: Upgrade' -H 'Upgrade: websocket' -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' $b/ws)\" = 101 ]" + expect "websocket upgrade (101)" sh -c "[ \"\$(curl -s -o /dev/null -w %{http_code} --max-time 3 --http1.1 -H 'Connection: Upgrade' -H 'Upgrade: websocket' -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' $b/ws)\" = 101 ]" } checks_minimal() {