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:
refuses a request signed more than 5 minutes ago, or one not newer than the last accepted request (a replay);
refuses a commit that isn't on origin/main;
checks out exactly that commit (detached), installs requirements.txt, and restarts gandalf and gandalf-monitor;
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)
Merge #76, then this PR. CI still deploys through today's script, which ignores sha and ts, so nothing changes yet.
On CT 157, install the script and switch the hook:
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.
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.
Verify: the next Gandalf merge deploys through the new script, CI's verify step sees the commit, and the log shows "deployed : healthy".
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)
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]>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Part 2 of #55. This PR is stacked on #76: its base is
ci-deploy-gate. Once #76 merges, I'll point it atmain.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:origin/main;requirements.txt, and restartsgandalfandgandalf-monitor;/healthto report the commit as bothshaandmonitor_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.pyruns the real script in a sandbox: throwaway git repos, with fakesystemctl,pip3andcurl. 7 tests:config.jsonalone;mainand malformed requests;shellcheck, flake8 and bandit are clean.
Rollout, in this order (each server step needs your OK)
shaandts, so nothing changes yet.install -m 755 /var/www/html/prod/deploy/gandalf_deploy.sh /usr/local/bin/gandalf_app_deploy.sh/etc/webhook/hooks.json, point thegandalf-deployhook'sexecute-commandat it, withpass-arguments-to-commandforshaandts;webhook.157-config-deployhook, run by pushes to pve-infra, stays on the pve-infra script for now.containers/157-gandalf/deploy.shconfig-only. It would keep the systemd units and the nginx site, and stop resetting the app toorigin/main, so a pve-infra push can't deploy an untested Gandalf commit. That's a separate pve-infra PR.🤖 Generated with Claude Code