Deploy: install exactly the tested commit, refuse replays, roll back if unhealthy (#55, part 2) #77

Open
jared wants to merge 1 commits from deploy-pinned into main
pull from: deploy-pinned
Owner

Part 2 of #55. This PR is stacked on #76: its base is ci-deploy-gate. Once #76 merges, I'll point it at main.

deploy/gandalf_deploy.sh <sha> <ts>

Modelled on Pulse's deploy/pulse_deploy.sh, which has been in use there since #45. The webhook runs it from its installed copy, /usr/local/bin/gandalf_app_deploy.sh, so a bad commit can't replace the code that rolls it back. It:

  1. refuses a request signed more than 5 minutes ago, or one not newer than the last accepted request (a replay);
  2. refuses a commit that isn't on origin/main;
  3. checks out exactly that commit (detached), installs requirements.txt, and restarts gandalf and gandalf-monitor;
  4. waits up to 2 minutes for /health to report the commit as both sha and monitor_sha (added in #76) with status ok, and rolls back to the previous commit if it never does.

It runs one deploy at a time (flock) and logs to /var/log/gandalf-app-deploy.log.

Tests

tests/test_deploy_script.py runs the real script in a sandbox: throwaway git repos, with fake systemctl, pip3 and curl. 7 tests:

  • it deploys exactly the commit and leaves the untracked config.json alone;
  • it refuses replayed, stale, off-main and malformed requests;
  • an unhealthy build is rolled back and the previous commit is running again;
  • the commit already running is a no-op.

shellcheck, flake8 and bandit are clean.

Rollout, in this order (each server step needs your OK)

  1. Merge #76, then this PR. CI still deploys through today's script, which ignores sha and ts, so nothing changes yet.
  2. On CT 157, install the script and switch the hook:
    • install -m 755 /var/www/html/prod/deploy/gandalf_deploy.sh /usr/local/bin/gandalf_app_deploy.sh
    • in /etc/webhook/hooks.json, point the gandalf-deploy hook's execute-command at it, with pass-arguments-to-command for sha and ts;
    • restart webhook.
    • The 157-config-deploy hook, run by pushes to pve-infra, stays on the pve-infra script for now.
  3. Make pve-infra's containers/157-gandalf/deploy.sh config-only. It would keep the systemd units and the nginx site, and stop resetting the app to origin/main, so a pve-infra push can't deploy an untested Gandalf commit. That's a separate pve-infra PR.
  4. Verify: the next Gandalf merge deploys through the new script, CI's verify step sees the commit, and the log shows "deployed : healthy".

🤖 Generated with Claude Code

Part 2 of #55. This PR is stacked on #76: its base is `ci-deploy-gate`. Once #76 merges, I'll point it at `main`. ## `deploy/gandalf_deploy.sh <sha> <ts>` Modelled on Pulse's `deploy/pulse_deploy.sh`, which has been in use there since #45. The webhook runs it from its installed copy, `/usr/local/bin/gandalf_app_deploy.sh`, so a bad commit can't replace the code that rolls it back. It: 1. refuses a request signed more than 5 minutes ago, or one not newer than the last accepted request (a replay); 2. refuses a commit that isn't on `origin/main`; 3. checks out exactly that commit (detached), installs `requirements.txt`, and restarts `gandalf` and `gandalf-monitor`; 4. waits up to 2 minutes for `/health` to report the commit as both `sha` and `monitor_sha` (added in #76) with status ok, and **rolls back** to the previous commit if it never does. It runs one deploy at a time (`flock`) and logs to `/var/log/gandalf-app-deploy.log`. ## Tests `tests/test_deploy_script.py` runs the real script in a sandbox: throwaway git repos, with fake `systemctl`, `pip3` and `curl`. 7 tests: - it deploys exactly the commit and leaves the untracked `config.json` alone; - it refuses replayed, stale, off-`main` and malformed requests; - an unhealthy build is rolled back and the previous commit is running again; - the commit already running is a no-op. shellcheck, flake8 and bandit are clean. ## Rollout, in this order (each server step needs your OK) 1. **Merge #76, then this PR.** CI still deploys through today's script, which ignores `sha` and `ts`, so nothing changes yet. 2. **On CT 157, install the script and switch the hook:** - `install -m 755 /var/www/html/prod/deploy/gandalf_deploy.sh /usr/local/bin/gandalf_app_deploy.sh` - in `/etc/webhook/hooks.json`, point the `gandalf-deploy` hook's `execute-command` at it, with `pass-arguments-to-command` for `sha` and `ts`; - restart `webhook`. - The `157-config-deploy` hook, run by pushes to pve-infra, stays on the pve-infra script for now. 3. **Make pve-infra's `containers/157-gandalf/deploy.sh` config-only.** It would keep the systemd units and the nginx site, and stop resetting the app to `origin/main`, so a pve-infra push can't deploy an untested Gandalf commit. That's a separate pve-infra PR. 4. **Verify:** the next Gandalf merge deploys through the new script, CI's verify step sees the commit, and the log shows "deployed <sha>: healthy". 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jared changed target branch from ci-deploy-gate to main 2026-11-29 00:00:34 -05:00
jared added 1 commit 2026-11-29 00:00:34 -05:00
Deploy: install exactly the tested commit, refuse replays, roll back if unhealthy
CI / Lint (JS) (pull_request) Successful in 12s
CI / Lint (Python) (pull_request) Successful in 35s
CI / Test (pull_request) Successful in 59s
CI / Security (pull_request) Successful in 39s
CI / Scan (pull_request) Successful in 29s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
6e18a3262a
gandalf#55, part 2 (the server side; needs installing on CT 157).

deploy/gandalf_deploy.sh <sha> <ts>, run by the gandalf-deploy webhook
from its installed copy /usr/local/bin/gandalf_app_deploy.sh:
- refuses a request signed more than 5 minutes ago, or one not newer
  than the last accepted (replay), and any sha not on origin/main;
- checks out exactly that sha (detached), installs requirements.txt and
  restarts gandalf and gandalf-monitor;
- waits for /health to report the sha for both with status ok, and rolls
  back to the previous commit otherwise; one deploy at a time (flock).

tests/test_deploy_script.py runs the real script in a sandbox with fake
systemctl, pip3 and curl: exact commit, untracked config.json kept,
replay, stale, off-main and malformed requests refused, an unhealthy
build rolled back, and the running commit a no-op.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
All checks were successful
CI / Lint (JS) (pull_request) Successful in 12s
Required
Details
CI / Lint (Python) (pull_request) Successful in 35s
Required
Details
CI / Test (pull_request) Successful in 59s
Required
Details
CI / Security (pull_request) Successful in 39s
Required
Details
CI / Scan (pull_request) Successful in 29s
Required
Details
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
You are not authorized to merge this pull request.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin deploy-pinned:deploy-pinned
git checkout deploy-pinned
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/gandalf#77