From 0ef5e86b964ab3aa98345864d0fd9af177a86478 Mon Sep 17 00:00:00 2001 From: Jason Norch Date: Fri, 20 Nov 2026 16:26:01 -0500 Subject: [PATCH 1/3] Add deploy-dev.sh: run the dev stack on another Docker host A workstation that builds but does not run the Compose stack had no supported way to get it onto a separate Docker machine. The obvious route, pointing a Docker context at the other host and running `docker compose up --build`, sends the whole build context there. .dockerignore does not exclude .env, so the production keys would land in that host's build cache, and the relative bind mounts would resolve to this machine's paths on the other one. The script builds the image on the local engine, ships it with `docker save | ssh docker load`, copies only what Compose bind-mounts (docker-compose.yaml, migrations/, deploy/), and starts the stack with `--no-build`. The source tree and .env never leave the workstation. Local docker calls name the `default` context, so it stays correct in a shell where a remote context is selected. It checks for the config file on the host before building, waits for /v1/health with the same bounded loop as docker-reset.sh, then confirms the running container holds the image just built. That last check compares layers and config rather than image IDs: each build gets a new ID from its provenance attestation, and Compose correctly keeps the container when the content is unchanged, so an ID comparison fails a rebuild of unchanged code. The host is an argument or DEV_DEPLOY_HOST; no machine name is in the repo. docs/operations.md gains a section for it, including why the smoke test has to reach such a host through a tunnel to localhost. Assisted-by: Claude Fable 5.2 --- AGENTS.md | 7 ++- docs/operations.md | 33 ++++++++++++++ scripts/deploy-dev.sh | 100 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100755 scripts/deploy-dev.sh diff --git a/AGENTS.md b/AGENTS.md index 1810f75..19c4985 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -39,7 +39,7 @@ internal/config/ Viper, YAML + env overrides, StatusSnapshot migrations/ PostgreSQL schema deploy/ Grafana dashboard (deploy/grafana/intelligence-api.json), plus the Prometheus and Grafana config the local Compose stack mounts -scripts/ docker-reset.sh, smoke-test.sh, status.sh, info.sh — dev only +scripts/ docker-reset.sh, deploy-dev.sh, smoke-test.sh, status.sh, info.sh — dev only changelog.yaml symlink to internal/handler/changelog.yaml; edit from the root ``` @@ -78,6 +78,11 @@ open the file. `SMOKE_FREE_KEYS`) because the limiter is per key per minute, and reserves `SMOKE_RATELIMIT_KEY` for the rate-limit check, which must exhaust one key's window; without it that check is skipped, not run wrongly. +- `./scripts/deploy-dev.sh ` — **dev only**: builds the image here, ships it, and + runs the Compose stack on another Docker host. Source and `.env` stay here; never + replace it with `docker compose up --build` against a remote context, which sends both. + Smoke-test that host through an SSH tunnel to `localhost:8080`, since any other host + makes the smoke test load the production keys from `.env`. - `./scripts/docker-reset.sh` bounds its readiness wait with `RESET_TIMEOUT` (300s) and names the dependency that timed out. `./scripts/status.sh` checks Fly machine state and endpoint reachability. diff --git a/docs/operations.md b/docs/operations.md index c15a462..c40cf93 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -160,6 +160,37 @@ It also starts Prometheus and Grafana, so the dashboard can be checked against r | `http://localhost:9090` | Prometheus, scraping the API every 15s | | `http://localhost:3000` | Grafana, no login, opening on the Intelligence API dashboard | +### Run the dev stack on another Docker host + +For a workstation that builds but does not run the stack. `scripts/deploy-dev.sh` builds the API image here, ships it over SSH, and starts the same Compose stack on the other machine: + +```sh +./scripts/deploy-dev.sh # or: DEV_DEPLOY_HOST= +``` + +Only the image and the files Compose bind-mounts (`docker-compose.yaml`, `migrations/`, `deploy/`) reach the host, under `~/docker/intelligence-api`. The source tree and `.env` stay here. The script waits for `/v1/health` and then checks that the running container holds the image it just built. + +The host needs Docker with the Compose plugin, key-based SSH, and the config file, installed once: + +```sh +ssh 'sudo mkdir -p /etc/security-accelerated && sudo tee /etc/security-accelerated/intelligence-api.yaml >/dev/null' < internal/config/sample-config.yml +``` + +| Variable | Default | What | +|----------|---------|------| +| `DEV_DEPLOY_HOST` | — | SSH host, when not given as the argument | +| `DEV_DEPLOY_DIR` | `docker/intelligence-api` | Directory on the host, relative to the SSH user's home | +| `DEPLOY_TIMEOUT` | `120` | Seconds to wait for the API to answer | + +Things to know: + +- **Migrations are not re-applied.** As with a local stack, Postgres reads `migrations/` only when its volume is first created. Apply a new one by hand, or remove the volume on the host. +- **The API is on the host's network.** Port 8080 is published on every interface, and so are Redis (6379) and Postgres (5432, with the Compose file's default password). Use a host on a network you trust. +- **Prometheus, Grafana and `/metrics` stay on the host's loopback.** Forward them: `ssh -L 3000:localhost:3000 -L 9090:localhost:9090 -L 9091:localhost:9091 `. +- **Smoke-test through a tunnel**, not at `http://:8080`: `ssh -N -L 8080:localhost:8080 -L 9091:localhost:9091 `, then `./scripts/smoke-test.sh`. For any host other than localhost the smoke test loads the keys in `.env`, which are the production ones. +- **Do not run `docker compose up` from the checkout against a remote Docker context.** It would send the build context, `.env` included, to the host, and the relative bind mounts would resolve to this machine's paths there. `docker --context ps` and `logs` are fine. The script names the `default` context for its own local calls, so it is safe to run while a remote context is selected. +- Each deploy of changed code leaves the previous image untagged on the host; `docker image prune` there reclaims the space. + ### Run from GitHub Container Registry On any machine with Docker — no code checkout needed: @@ -237,6 +268,7 @@ make run # builds and starts the server | `make deploy` | Deploy to Fly.io with git version baked in | | `make docker-reset` | **Dev only.** Tear down containers, wipe volumes, rebuild, and wait for healthy startup. The wait is bounded by `RESET_TIMEOUT` (default 300s), after which it names the dependency that never came up | | `./scripts/smoke-test.sh [host]` | Hit every endpoint with every key/plan combo and check expected status codes. Defaults to `http://localhost:8080`. Also checks that `/metrics` is a 404 on the public port, and reads the metrics listener where it is reachable: `localhost:9091` for a local host, `SMOKE_METRICS_URL` otherwise, skipped when neither applies | +| `./scripts/deploy-dev.sh ` | **Dev only.** Build the image here and run the Compose stack on another Docker host over SSH. See [Run the dev stack on another Docker host](#run-the-dev-stack-on-another-docker-host) | | `./scripts/status.sh` | Check Fly.io deployment health, Postgres, and Redis status | | `./scripts/info.sh [host]` | Pretty-print the `/v1/info` admin endpoint. Defaults to production | @@ -417,6 +449,7 @@ docker reset; the rest overlap with `sactl` commands. They load API keys from ```sh ./scripts/smoke-test.sh https://api.securityaccelerated.com ./scripts/docker-reset.sh # or: make docker-reset +./scripts/deploy-dev.sh # dev stack on another Docker host ./scripts/status.sh # or: sactl status ./scripts/info.sh # or: sactl info ./scripts/usage.sh 30 # or: sactl usage --days 30 diff --git a/scripts/deploy-dev.sh b/scripts/deploy-dev.sh new file mode 100755 index 0000000..66f5758 --- /dev/null +++ b/scripts/deploy-dev.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# DEV ONLY — builds the API image on this machine and runs the Compose stack on +# another Docker host, reached over SSH. For a workstation that builds but does +# not run the stack. Not a production deploy: that is a `v*` tag, see RELEASE.md. +# +# ./scripts/deploy-dev.sh or: DEV_DEPLOY_HOST= +# +# Only the finished image and the files Compose bind-mounts reach the host. The +# source tree and .env stay here, which is the reason this is not a +# `docker compose up --build` against a remote context: that would send the +# whole build context, .env included, to the other machine. +set -euo pipefail + +cd "$(dirname "$0")/.." + +HOST="${1:-${DEV_DEPLOY_HOST:-}}" +# Relative to the SSH user's home on the host. Its last component is the Compose +# project name, so keeping it equal to this directory's name lets +# `docker --context compose ps` work from the checkout. +REMOTE_DIR="${DEV_DEPLOY_DIR:-docker/intelligence-api}" +CONFIG=/etc/security-accelerated/intelligence-api.yaml +TIMEOUT="${DEPLOY_TIMEOUT:-120}" +PROBE_TIMEOUT=5 + +if [ -z "$HOST" ]; then + echo "usage: $0 (or set DEV_DEPLOY_HOST)" >&2 + exit 2 +fi + +# Every local docker call names the `default` context, which is always this +# machine's engine. Without it, a shell where `docker context use ` is in +# effect would build on the remote host and ship it the source after all. +local_docker() { docker --context default "$@"; } + +REMOTE_DIR_Q=$(printf '%q' "$REMOTE_DIR") +remote() { ssh -o BatchMode=yes "$HOST" "$@"; } + +VERSION=$(git describe --tags --always --dirty) +export VERSION + +echo "==> Deploying $VERSION to $HOST:~/$REMOTE_DIR" + +# Checked before the build so a host that was never set up fails in a second, +# not after an image transfer. The config is not installed from here: writing +# under /etc needs root on the host, and an existing file may carry edits. +if ! remote test -r "$CONFIG"; then + echo "error: $HOST has no readable $CONFIG" >&2 + echo " install it once with:" >&2 + echo " ssh $HOST 'sudo mkdir -p ${CONFIG%/*} && sudo tee $CONFIG >/dev/null' < internal/config/sample-config.yml" >&2 + exit 1 +fi + +echo "==> Building image locally..." +local_docker compose build api + +echo "==> Shipping image..." +local_docker save intelligence-api | remote docker load + +# From the working tree, not HEAD, so these match the image just built from it. +# Extracting over the old copy leaves a file deleted here in place over there; +# Postgres only reads migrations/ when its volume is first created, so that is +# harmless for it, and the other mounts are named files. +echo "==> Syncing compose file, migrations and deploy config..." +tar -c docker-compose.yaml migrations deploy | + remote "mkdir -p $REMOTE_DIR_Q && tar -x -C $REMOTE_DIR_Q" + +# --no-build: the host has no source to build from. Compose recreates the api +# container because the image behind its tag changed. +echo "==> Starting the stack..." +remote "cd $REMOTE_DIR_Q && docker compose up -d --no-build" + +# Bounded the same way as docker-reset.sh: an overall deadline, plus a timeout on +# each probe so one that hangs cannot outlive it. +echo "==> Waiting for the API to be ready..." +deadline=$((SECONDS + TIMEOUT)) +until remote "curl -sf --max-time $PROBE_TIMEOUT http://localhost:8080/v1/health" >/dev/null 2>&1; do + if [ "$SECONDS" -ge "$deadline" ]; then + echo "error: api did not become ready within ${TIMEOUT}s" >&2 + echo " check logs with: ssh $HOST docker logs intelligence-api" >&2 + exit 1 + fi + sleep 1 +done + +# A healthy answer alone could come from a container still on an older image, so +# compare what is running with what was built. By content (layers and config), +# not by image ID: every build gets a new ID because its provenance attestation +# differs, while Compose rightly keeps the container when the image inside is +# byte-for-byte the same. Comparing IDs fails a rebuild of unchanged code. +content='{{json .RootFS.Layers}} {{json .Config}}' +built=$(local_docker image inspect -f "$content" intelligence-api) +running=$(remote "docker image inspect -f '$content' \$(docker inspect -f '{{.Image}}' intelligence-api)") +if [ "$built" != "$running" ]; then + echo "error: the api container on $HOST is not running the image just built" >&2 + echo " check with: ssh $HOST docker ps -a" >&2 + exit 1 +fi + +echo "" +echo "==> $VERSION is running on $HOST (http://$HOST:8080)" From a55462ae54063760a86b3ceb968706bfc079de0c Mon Sep 17 00:00:00 2001 From: Jason Norch Date: Fri, 20 Nov 2026 16:37:56 -0500 Subject: [PATCH 2/3] Fix four review findings on deploy-dev.sh Pin BUILDX_BUILDER alongside the context. `--context default` picks the engine, but a builder named in BUILDX_BUILDER can be remote, and the build context, .env included, goes wherever the build runs. That is the one thing the script exists to prevent. Stop before building when this machine and the host have different platforms. A wrong-architecture image loads cleanly and then fails to start with an exec format error. Cross-building needs emulation and is not attempted; the script says so instead. Put timeouts on the ssh process. The readiness loop only re-checks its deadline between probes, and the limit on the remote curl does nothing for a connection that never completes or a session that stops answering. Remember the image ID this run built. The intelligence-api tag is shared by every checkout on the machine, so a build elsewhere could move it between this run's build and its save; that image would then be shipped, pass the final check against the same moved tag, and be reported under this run's version. The tag is re-read after the transfer, and the final comparison uses the remembered ID. Assisted-by: Claude Fable 5.2 --- scripts/deploy-dev.sh | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/scripts/deploy-dev.sh b/scripts/deploy-dev.sh index 66f5758..9bd6b04 100755 --- a/scripts/deploy-dev.sh +++ b/scripts/deploy-dev.sh @@ -30,10 +30,19 @@ fi # Every local docker call names the `default` context, which is always this # machine's engine. Without it, a shell where `docker context use ` is in # effect would build on the remote host and ship it the source after all. -local_docker() { docker --context default "$@"; } +# BUILDX_BUILDER is pinned for the same reason: the context picks the engine, but +# a builder selected by that variable can be a remote one, and the build context +# (.env included, .dockerignore does not exclude it) goes wherever the build runs. +local_docker() { BUILDX_BUILDER=default docker --context default "$@"; } REMOTE_DIR_Q=$(printf '%q' "$REMOTE_DIR") -remote() { ssh -o BatchMode=yes "$HOST" "$@"; } +# The timeouts are on the ssh process itself. A limit on the command run over +# there does nothing for a connection that never completes or a session that +# stops answering, and either would outlast the readiness deadline below. +remote() { + ssh -o BatchMode=yes -o ConnectTimeout=10 \ + -o ServerAliveInterval=5 -o ServerAliveCountMax=3 "$HOST" "$@" +} VERSION=$(git describe --tags --always --dirty) export VERSION @@ -50,11 +59,32 @@ if ! remote test -r "$CONFIG"; then exit 1 fi +# An image built for another architecture loads without complaint and then fails +# to start with an exec format error. Cross-building is not attempted here (it +# needs emulation set up on this machine), so a mismatch stops before the build. +platform='{{.Server.Os}}/{{.Server.Arch}}' +local_platform=$(local_docker version -f "$platform") +remote_platform=$(remote "docker version -f '$platform'") +if [ "$local_platform" != "$remote_platform" ]; then + echo "error: this machine builds $local_platform images, $HOST runs $remote_platform" >&2 + echo " cross-platform builds are not supported by this script" >&2 + exit 1 +fi + echo "==> Building image locally..." local_docker compose build api +# The tag is shared by every checkout on this machine. Remember which image this +# run built, so that a build elsewhere moving the tag is caught instead of being +# shipped and reported under this run's version. +built_id=$(local_docker image inspect -f '{{.Id}}' intelligence-api) echo "==> Shipping image..." local_docker save intelligence-api | remote docker load +if [ "$(local_docker image inspect -f '{{.Id}}' intelligence-api)" != "$built_id" ]; then + echo "error: the intelligence-api tag was rebuilt while this deploy was running" >&2 + echo " the image sent to $HOST may not be this checkout's; run the deploy again" >&2 + exit 1 +fi # From the working tree, not HEAD, so these match the image just built from it. # Extracting over the old copy leaves a file deleted here in place over there; @@ -88,7 +118,7 @@ done # differs, while Compose rightly keeps the container when the image inside is # byte-for-byte the same. Comparing IDs fails a rebuild of unchanged code. content='{{json .RootFS.Layers}} {{json .Config}}' -built=$(local_docker image inspect -f "$content" intelligence-api) +built=$(local_docker image inspect -f "$content" "$built_id") running=$(remote "docker image inspect -f '$content' \$(docker inspect -f '{{.Image}}' intelligence-api)") if [ "$built" != "$running" ]; then echo "error: the api container on $HOST is not running the image just built" >&2 From 05537ce6dac566b9362bba140ba9600bf6055141 Mon Sep 17 00:00:00 2001 From: Jason Norch Date: Fri, 20 Nov 2026 16:45:24 -0500 Subject: [PATCH 3/3] Refuse a DOCKER_HOST that points at another machine Second review round. `--context default` does not mean the local engine when DOCKER_HOST is set: the default context is whatever that variable names, and the pinned `default` builder runs there too. A build would send the source and .env to that engine, which is what the script is for preventing. The variable is checked rather than unset. A unix socket is still this machine, and rootless Docker and Colima rely on it; unsetting it would point those setups at a socket that does not exist. Assisted-by: Claude Fable 5.2 --- scripts/deploy-dev.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/scripts/deploy-dev.sh b/scripts/deploy-dev.sh index 9bd6b04..7e7fd88 100755 --- a/scripts/deploy-dev.sh +++ b/scripts/deploy-dev.sh @@ -35,6 +35,18 @@ fi # (.env included, .dockerignore does not exclude it) goes wherever the build runs. local_docker() { BUILDX_BUILDER=default docker --context default "$@"; } +# DOCKER_HOST is the remaining way to move the build: the `default` context is +# whatever that variable points at. A unix socket is still this machine (rootless +# Docker, Colima), so it is left alone rather than unset; anything else is refused. +case "${DOCKER_HOST:-}" in + "" | unix://*) ;; + *) + echo "error: DOCKER_HOST=$DOCKER_HOST points at another machine's engine" >&2 + echo " the build would send it the source and .env; unset it and run again" >&2 + exit 1 + ;; +esac + REMOTE_DIR_Q=$(printf '%q' "$REMOTE_DIR") # The timeouts are on the ssh process itself. A limit on the command run over # there does nothing for a connection that never completes or a session that