Menu
AkurAI-Build
publicLatest change a3e6c2c8498f91b9503a56f8586ff88965ecf57a - Deploy to the systemd-managed service with health-gated rollback by Ólafur Búi Ólafsson
#!/bin/sh
# Deploy the freshly packaged akurai binary to the systemd-managed service.
# Swaps the binary atomically, restarts the unit, health-gates the result,
# and rolls back to the previous binary if the service does not come up.
set -eu
bin_dir="${AKURAI_SERVICE_LIB:-$HOME/.local/lib/akurai-build}"
bin="$bin_dir/akurai"
previous="$bin.previous"
health_url="${AKURAI_HEALTH_URL:-http://127.0.0.1:3210/api/health}"
test -x target/release/akurai
curl -fsS "$health_url" >/dev/null
# Preserve the running binary for rollback, then swap via rename so the
# replacement is atomic even while the old inode stays busy.
cp "$bin" "$previous"
install -m 755 target/release/akurai "$bin.next"
mv "$bin.next" "$bin"
rollback() {
status=$?
trap - EXIT HUP INT TERM
printf '%s\n' "deploy failed; rolling back to previous binary" >&2
mv "$previous" "$bin"
sudo -n systemctl restart akurai-build || true
exit "$status"
}
trap rollback EXIT HUP INT TERM
sudo -n systemctl restart akurai-build
attempt=0
while [ "$attempt" -lt 30 ]; do
if curl -fsS "$health_url" >/dev/null 2>&1; then
trap - EXIT HUP INT TERM
rm -f "$previous"
printf '%s\n' "deploy healthy"
exit 0
fi
attempt=$((attempt + 1))
sleep 2
done
printf '%s\n' "replacement service did not become healthy" >&2
exit 1