The watchdog polled http://127.0.0.1:8420/healthz. Nothing binds 8420: the daemon serves /healthz on [web].listen (127.0.0.1:8484 by default), so every dashboard- or Telegram-initiated update waited out the 60s health timeout and rolled back a release that had come up fine. The daemon is the only process that knows which address it binds, so it now derives the probe URL from its own config and hands it to the installer; CommandInstaller exports ARCHIE_HEALTH_URL and the install script forwards it into the watchdog unit, whose remaining default matches the dashboard's. The suite never dialled the URL: every watchdog test faked curl as an immediate failure, so only the unhealthy branch existed. One test now runs the real curl against a real listener, and another pins the script default.
161 lines
6.0 KiB
Bash
Executable File
161 lines
6.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# archie-update-watchdog -- restarts archied after an update, verifies health,
|
|
# and rolls back only the components named by ARCHIE_UPDATE_COMPONENTS.
|
|
#
|
|
# Runtime-only updates still restart archied: the restart is the existing
|
|
# phase-2 handoff that makes the daemon consume and deliver the health report.
|
|
# It does not rebuild or reinstall the gateway binary.
|
|
set -uo pipefail
|
|
|
|
BIN_DIR="${ARCHIE_BIN_DIR:-$HOME/.local/bin}"
|
|
AGENT_IMAGE="${ARCHIE_AGENT_IMAGE:-ghcr.io/samcharles93/archie-agent:latest}"
|
|
AGENT_IMAGE_BACKUP="${ARCHIE_AGENT_IMAGE_BACKUP:-$BIN_DIR/archie-agent-image.prev.tar}"
|
|
TASK_DB_PATH="${ARCHIE_TASK_DB_PATH:-}"
|
|
TASK_DB_BACKUP="${ARCHIE_TASK_DB_BACKUP:-$BIN_DIR/archie-task-db.prev.sqlite}"
|
|
# The daemon exports its own dashboard address (releaseupdate.CommandInstaller);
|
|
# this default only covers a hand-driven run, and must stay the address archied
|
|
# serves /healthz on -- config.example.toml's [web] listen.
|
|
HEALTH_URL="${ARCHIE_HEALTH_URL:-http://127.0.0.1:8484}"
|
|
HEALTH_URL="${HEALTH_URL%/}"
|
|
REPORT_PATH="${ARCHIE_UPDATE_REPORT_PATH:-}"
|
|
HEALTH_TIMEOUT_SECONDS="${ARCHIE_UPDATE_HEALTH_TIMEOUT:-60}"
|
|
POLL_INTERVAL_SECONDS=3
|
|
COMPONENTS="${ARCHIE_UPDATE_COMPONENTS:-daemon,agent}"
|
|
|
|
json_escape() {
|
|
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
|
|
}
|
|
|
|
report_versions() {
|
|
case "$COMPONENTS" in
|
|
daemon)
|
|
previous_json="{\"daemon\":\"$(json_escape "${ARCHIE_UPDATE_PREVIOUS_GATEWAY:-unknown}")\"}"
|
|
installed_json="{\"daemon\":\"$(json_escape "${ARCHIE_UPDATE_INSTALLED_GATEWAY:-unknown}")\"}"
|
|
;;
|
|
agent)
|
|
previous_json="{\"agent\":\"$(json_escape "${ARCHIE_UPDATE_PREVIOUS_RUNTIME:-unknown}")\"}"
|
|
installed_json="{\"agent\":\"$(json_escape "${ARCHIE_UPDATE_INSTALLED_RUNTIME:-unknown}")\"}"
|
|
;;
|
|
daemon,agent)
|
|
previous_json="{\"daemon\":\"$(json_escape "${ARCHIE_UPDATE_PREVIOUS_GATEWAY:-unknown}")\",\"agent\":\"$(json_escape "${ARCHIE_UPDATE_PREVIOUS_RUNTIME:-unknown}")\"}"
|
|
installed_json="{\"daemon\":\"$(json_escape "${ARCHIE_UPDATE_INSTALLED_GATEWAY:-unknown}")\",\"agent\":\"$(json_escape "${ARCHIE_UPDATE_INSTALLED_RUNTIME:-unknown}")\"}"
|
|
;;
|
|
*)
|
|
echo "invalid ARCHIE_UPDATE_COMPONENTS: $COMPONENTS" >&2
|
|
return 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
write_report() {
|
|
local health_check="$1" rolled_back="$2"
|
|
[ -z "$REPORT_PATH" ] && return 0
|
|
report_versions || return 1
|
|
mkdir -p "$(dirname "$REPORT_PATH")"
|
|
tmp="$(mktemp "$(dirname "$REPORT_PATH")/.update-report-XXXXXX")"
|
|
cat >"$tmp" <<EOF
|
|
{"channel":"$(json_escape "${ARCHIE_UPDATE_CHANNEL:-}")","chat_id":${ARCHIE_UPDATE_CHAT_ID:-0},"thread_id":${ARCHIE_UPDATE_THREAD_ID:-0},"previous":$previous_json,"installed":$installed_json,"health_check":"$health_check","rolled_back":$rolled_back}
|
|
EOF
|
|
chmod 600 "$tmp"
|
|
mv -f "$tmp" "$REPORT_PATH"
|
|
}
|
|
|
|
wait_healthy() {
|
|
local deadline=$((SECONDS + HEALTH_TIMEOUT_SECONDS))
|
|
while [ "$SECONDS" -lt "$deadline" ]; do
|
|
if curl -sf -o /dev/null "$HEALTH_URL/healthz"; then
|
|
return 0
|
|
fi
|
|
sleep "$POLL_INTERVAL_SECONDS"
|
|
done
|
|
return 1
|
|
}
|
|
|
|
rollback_component() {
|
|
local name="$1"
|
|
if [ ! -f "$BIN_DIR/$name.prev" ]; then
|
|
echo "==> no backup for $name; cannot roll it back automatically" >&2
|
|
return 1
|
|
fi
|
|
cp -f "$BIN_DIR/$name.prev" "$BIN_DIR/$name"
|
|
}
|
|
|
|
rollback_agent_image() {
|
|
if [ ! -f "$AGENT_IMAGE_BACKUP" ]; then
|
|
echo "==> no backup for managed worker image $AGENT_IMAGE; cannot roll it back automatically" >&2
|
|
return 1
|
|
fi
|
|
docker image load --input "$AGENT_IMAGE_BACKUP"
|
|
}
|
|
|
|
rollback_task_db() {
|
|
if [ -z "$TASK_DB_PATH" ]; then
|
|
echo "==> no task database path; cannot restore schema-compatible state" >&2
|
|
return 1
|
|
fi
|
|
if [ ! -f "$TASK_DB_BACKUP" ]; then
|
|
echo "==> no task database backup; cannot restore schema-compatible state" >&2
|
|
return 1
|
|
fi
|
|
sqlite3 "$TASK_DB_BACKUP" "PRAGMA wal_checkpoint(TRUNCATE);" >/dev/null 2>&1 || true
|
|
cp -f "$TASK_DB_BACKUP" "$TASK_DB_PATH"
|
|
rm -f "$TASK_DB_PATH-wal" "$TASK_DB_PATH-shm"
|
|
}
|
|
|
|
echo "==> restarting archied.service"
|
|
systemctl --user restart archied.service
|
|
|
|
if wait_healthy; then
|
|
echo "==> new version is healthy"
|
|
write_report "passed" false
|
|
exit 0
|
|
fi
|
|
|
|
echo "==> health check failed after ${HEALTH_TIMEOUT_SECONDS}s; rolling back changed components"
|
|
rolled_back=false
|
|
# Stop before touching either the executable or its database. The service
|
|
# uses Restart=on-failure in the supported unit, so copying rollback state
|
|
# while a crash-loop child is alive can race with SQLite and execute the new
|
|
# binary after its files have been replaced.
|
|
if systemctl --user stop archied.service; then
|
|
case "$COMPONENTS" in
|
|
daemon)
|
|
if [ -f "$BIN_DIR/archied.prev" ] && { [ -z "$TASK_DB_PATH" ] || [ -f "$TASK_DB_BACKUP" ]; }; then
|
|
if { [ -z "$TASK_DB_PATH" ] || rollback_task_db; } && rollback_component archied; then
|
|
rolled_back=true
|
|
fi
|
|
else
|
|
echo "==> no backup for archied; cannot roll it back automatically" >&2
|
|
fi
|
|
;;
|
|
agent)
|
|
if [ -f "$AGENT_IMAGE_BACKUP" ]; then
|
|
rollback_agent_image && rolled_back=true
|
|
else
|
|
echo "==> no backup for managed worker image $AGENT_IMAGE; cannot roll it back automatically" >&2
|
|
fi
|
|
;;
|
|
daemon,agent)
|
|
# Preserve the old all-or-nothing rollback guarantee: never restore one
|
|
# component and leave the other on the failed release.
|
|
if [ -f "$BIN_DIR/archied.prev" ] && [ -f "$AGENT_IMAGE_BACKUP" ] && { [ -z "$TASK_DB_PATH" ] || [ -f "$TASK_DB_BACKUP" ]; }; then
|
|
if rollback_agent_image && { [ -z "$TASK_DB_PATH" ] || rollback_task_db; } && rollback_component archied; then
|
|
rolled_back=true
|
|
fi
|
|
else
|
|
echo "==> not all changed-component backups exist; cannot roll back automatically" >&2
|
|
fi
|
|
;;
|
|
*)
|
|
echo "invalid ARCHIE_UPDATE_COMPONENTS: $COMPONENTS" >&2
|
|
;;
|
|
esac
|
|
else
|
|
echo "==> could not stop archied.service; refusing rollback while it may be running" >&2
|
|
fi
|
|
if [ "$rolled_back" = true ]; then
|
|
systemctl --user restart archied.service
|
|
fi
|
|
|
|
write_report "failed" "$rolled_back"
|