agentbbs/scripts/self-update.sh
sh1pt-actions-fleet[bot] 29c14d4cb0
Some checks failed
CI / build (push) Has been cancelled
deploy / deploy (push) Has been cancelled
test / test (push) Has been cancelled
ci(deploy): pin the deploy key to one forced command; self-update runs the new setup.sh (#163)
The 10-27 incident showed what an unrestricted root deploy key is worth to an
attacker. The CI key is now installed on the box as
restrict,command="/usr/local/sbin/agentbbs-ci-deploy", so it gets no shell,
scp, sftp, pty or forwarding.

- deploy/ci-deploy.sh (installed by setup.sh): verbs `arch`, `upload
  agentbbs|ascii-live` (ELF on stdin, size-capped, root-only staging dir) and
  `deploy main|master` (app secrets as NAME=<base64> on stdin, allowlisted).
  It does what the old inline heredoc did: sync to origin, pause the
  self-update timer, install binaries, exec setup.sh SKIP_BUILD=1.
- deploy.yml: uses those verbs instead of scp + `sudo bash -s`.
- self-update.sh: reset to origin before exec'ing setup.sh, so a setup.sh
  change takes effect on the first timer run instead of the second.

Co-authored-by: Anthony Ettinger <anthony@profullstack.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 09:21:21 +00:00

46 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# self-update.sh — pull the tracked branch and, if it advanced (or the service
# is down, or --force), re-run the idempotent provisioner to redeploy.
#
# Designed to be safe to run on a timer: when origin has not moved and agentbbs
# is healthy it does nothing and exits 0, so it costs one `git fetch` per tick.
# setup.sh holds a flock, so this never races a concurrent CI deploy.
#
# sudo scripts/self-update.sh # redeploy only if origin/<branch> moved
# sudo scripts/self-update.sh --force # redeploy unconditionally
#
set -euo pipefail
REPO="${REPO:-https://github.com/profullstack/agentbbs.git}"
BRANCH="${BRANCH:-main}"
SRC_DIR="${SRC_DIR:-/opt/agentbbs}"
FORCE=0
[ "${1:-}" = "--force" ] && FORCE=1
[ "$(id -u)" -eq 0 ] || { echo "self-update.sh must run as root" >&2; exit 1; }
# First-ever run on a bare box: clone, then always provision.
if [ ! -d "$SRC_DIR/.git" ]; then
git clone --depth 1 -b "$BRANCH" "$REPO" "$SRC_DIR"
exec "$SRC_DIR/setup.sh"
fi
git -C "$SRC_DIR" fetch --depth 1 origin "$BRANCH"
local_rev="$(git -C "$SRC_DIR" rev-parse HEAD)"
remote_rev="$(git -C "$SRC_DIR" rev-parse "origin/${BRANCH}")"
if [ "$FORCE" -eq 0 ] \
&& [ "$local_rev" = "$remote_rev" ] \
&& systemctl is-active --quiet agentbbs; then
echo "agentbbs up to date at ${remote_rev:0:12} and healthy; nothing to do"
exit 0
fi
echo "redeploying: ${local_rev:0:12} -> ${remote_rev:0:12} (force=$FORCE)"
# Move the checkout to origin first so the NEW setup.sh runs. Exec'ing the
# on-disk copy before its own reset ran the previous commit's setup.sh, so a
# setup.sh change needed two timer runs to take effect.
git -C "$SRC_DIR" reset --hard "origin/${BRANCH}"
exec "$SRC_DIR/setup.sh"