fix(update): install and cycle every process in the release

A self-update to 1.23.0 failed its health check and rolled back. The
adapter built and installed ./cmd/archied and nothing else, and the
watchdog restarted archied.service and nothing else, so the new daemon
came up against a State Store, Gateway and UI still on the previous
release. Since 1.23.0 archied cannot boot without a reachable State
Store (openStateStoreAdapter), which on a host that had never run one is
an immediate exit, a failed health check, and a rollback that looks like
a bad release rather than an incomplete deployment.

Install every binary the reference deployment runs, versioned off the
archied tag, and back each up so the rollback restores all of them
together. A binary with no .prev was introduced by the failed release, so
rollback removes it instead of leaving one behind that points at
processes the restored release does not run.

Restart in dependency order: the State Store owns archie.db and every
other process dials it, the Gateway serves the contract archied and the
UI dial, so each comes up before its consumers and stops after them.
archied itself is cycled unconditionally and a failed stop still aborts
the rollback, because a presence probe that guesses wrong must never
skip the stop that keeps a live process off the binary and the database
being restored. The siblings are best-effort, which is safe because the
adapter now refuses the update outright, before touching the host, when
a unit for one of them is missing, naming what to create.

install.sh had the same single-binary assumption, so a fresh install
produced a host that could not start either.

Closes archie-core-hbqk.
This commit is contained in:
sam committed 2026-09-12 09:52:01 +10:00
1 parent 2d228a00b4
commit 99afccc3b2
4 files changed
+285 -35

No files matched your search

+59 -9
View File
@@ -62,6 +62,17 @@ AGENT_IMAGE_BACKUP="$BIN_DIR/archie-agent-image.prev.tar"
TASK_DB_PATH="${ARCHIE_TASK_DB_PATH:-$(configured_task_db_path)}"
TASK_DB_BACKUP="${ARCHIE_TASK_DB_BACKUP:-$BIN_DIR/archie-task-db.prev.sqlite}"
# The archied release is a set of processes, not one binary. Listed in start
# order: the State Store owns archie.db and every other process dials it, and
# the Gateway serves the chat contract archied and the UI dial, so each comes
# up before its consumers. archie-playbooks is a CLI, so it is installed but
# never restarted. deployments/systemd-user-service.md is the reference
# deployment; installing archied alone leaves the rest on the previous
# release, which is why a 1.23.0 update failed its health check
# (archie-core-hbqk).
GATEWAY_SERVICES="archie-state-store archie-gateway archied archie-ui"
GATEWAY_BINARIES="$GATEWAY_SERVICES archie-playbooks"
PREV_GW="${ARCHIE_UPDATE_DAEMON_PREVIOUS:-}"
PREV_RT="${ARCHIE_UPDATE_AGENT_PREVIOUS:-}"
GW_TARGET="${ARCHIE_UPDATE_DAEMON_VERSION:-}"
@@ -133,6 +144,25 @@ esac
git -C "$source_dir" checkout --quiet --detach "$build_commit"
cd "$source_dir"
# Refuse before touching anything if the release runs processes this host has
# no unit for. Installing the binaries anyway would leave archied dialling a
# State Store that nothing starts: it fails its health check and rolls back,
# which reads as a broken release rather than an incomplete deployment.
if [[ ",$components," == *,daemon,* ]]; then
missing_units=""
for svc in $GATEWAY_SERVICES; do
[ -d "$source_dir/cmd/$svc" ] || continue
if ! systemctl --user list-unit-files "$svc.service" --no-legend 2>/dev/null | grep -q .; then
missing_units="${missing_units:+$missing_units }$svc.service"
fi
done
if [ -n "$missing_units" ]; then
echo "refusing to install archied $GW: it runs as separate processes and this host has no unit for: $missing_units" >&2
echo "create them from deployments/systemd-user-service.md, then retry the update" >&2
exit 1
fi
fi
# Refresh adapters from the approved release source. The running shell
# continues with this copy; subsequent updates use the current contract.
install -m755 "$source_dir/scripts/archie-update-install" "$BIN_DIR/archie-update-install"
@@ -145,16 +175,34 @@ if [[ ",$components," == *,daemon,* ]] && [ -n "$TASK_DB_PATH" ] && [ -f "$TASK_
sqlite3 "$TASK_DB_PATH" ".backup '$TASK_DB_BACKUP'"
fi
# built accumulates only the binaries this release actually ships, so an
# update to a release predating one of these processes stays buildable.
built=""
if [[ ",$components," == *,daemon,* ]]; then
echo "==> building archied $GW"
go build -ldflags "-X github.com/samcharles93/archie-core/internal/app/archied.gatewayVersion=$GW -X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion=$RT -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary" \
-o "$tmp/archied" ./cmd/archied
fi
if [[ ",$components," == *,daemon,* ]]; then
echo "==> backing up current archied binary"
[ -f "$BIN_DIR/archied" ] && cp -f "$BIN_DIR/archied" "$BIN_DIR/archied.prev"
echo "==> installing archied"
install -m755 "$tmp/archied" "$BIN_DIR/archied"
ldflags="-X github.com/samcharles93/archie-core/internal/app/archied.gatewayVersion=$GW"
ldflags="$ldflags -X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion=$RT"
ldflags="$ldflags -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary"
for cmd in $GATEWAY_BINARIES; do
[ -d "./cmd/$cmd" ] || continue
echo "==> building $cmd $GW"
go build -ldflags "$ldflags" -o "$tmp/$cmd" "./cmd/$cmd"
built="${built:+$built }$cmd"
done
echo "==> backing up current binaries"
for cmd in $built; do
if [ -f "$BIN_DIR/$cmd" ]; then
cp -f "$BIN_DIR/$cmd" "$BIN_DIR/$cmd.prev"
else
# New in this release: record that there was nothing here, so a
# rollback removes it instead of leaving a stray binary behind.
rm -f "$BIN_DIR/$cmd.prev"
fi
done
echo "==> installing $built"
for cmd in $built; do
install -m755 "$tmp/$cmd" "$BIN_DIR/$cmd"
done
fi
if [[ ",$components," == *,agent,* ]]; then
echo "==> saving managed worker image $AGENT_IMAGE for rollback"
@@ -172,6 +220,8 @@ systemd-run --user --unit=archied-update-restart --collect --quiet \
--setenv=ARCHIE_UPDATE_CHAT_ID="${ARCHIE_UPDATE_CHAT_ID:-0}" \
--setenv=ARCHIE_UPDATE_THREAD_ID="${ARCHIE_UPDATE_THREAD_ID:-0}" \
--setenv=ARCHIE_UPDATE_COMPONENTS="$components" \
--setenv=ARCHIE_UPDATE_UNITS="$GATEWAY_SERVICES" \
--setenv=ARCHIE_UPDATE_BINARIES="$built" \
--setenv=ARCHIE_UPDATE_PREVIOUS_GATEWAY="$PREV_GW" \
--setenv=ARCHIE_UPDATE_PREVIOUS_RUNTIME="$PREV_RT" \
--setenv=ARCHIE_UPDATE_INSTALLED_GATEWAY="$GW" \
+62 -13
View File
@@ -22,6 +22,11 @@ REPORT_PATH="${ARCHIE_UPDATE_REPORT_PATH:-}"
HEALTH_TIMEOUT_SECONDS="${ARCHIE_UPDATE_HEALTH_TIMEOUT:-60}"
POLL_INTERVAL_SECONDS=3
COMPONENTS="${ARCHIE_UPDATE_COMPONENTS:-daemon,agent}"
# Set by archie-update-install: the units to restart, in start order, and the
# binaries it replaced. The defaults keep a hand-driven run on a pre-
# decomposition host working.
UNITS="${ARCHIE_UPDATE_UNITS:-archied}"
BINARIES="${ARCHIE_UPDATE_BINARIES:-archied}"
json_escape() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
@@ -72,13 +77,58 @@ wait_healthy() {
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
# manage_unit runs one systemctl action. archied is required: this whole
# mechanism exists to restart it, and a failed stop must abort the rollback
# rather than let it race a live process over the binary and the database.
# The sibling processes are best-effort, because archie-update-install has
# already refused the update if a unit for one of them was missing, and a
# probe that guesses wrong must not silently skip the required stop.
manage_unit() {
local action="$1" unit="$2"
if [ "$unit" = archied ]; then
systemctl --user "$action" "$unit.service"
return
fi
cp -f "$BIN_DIR/$name.prev" "$BIN_DIR/$name"
systemctl --user "$action" "$unit.service" || true
}
# start_units brings the deployment up in dependency order: the State Store
# before anything that dials it, the Gateway before archied and the UI.
start_units() {
local unit rc=0
for unit in $UNITS; do
echo "==> restarting $unit.service"
manage_unit restart "$unit" || rc=1
done
return $rc
}
# stop_units tears down in reverse, so consumers are gone before the services
# they depend on.
stop_units() {
local unit ordered="" rc=0
for unit in $UNITS; do
ordered="$unit${ordered:+ $ordered}"
done
for unit in $ordered; do
manage_unit stop "$unit" || rc=1
done
return $rc
}
# rollback_gateway restores every binary the install replaced. A binary with
# no .prev was introduced by the failed release, so it is removed rather than
# left behind pointing at processes the restored release does not run.
rollback_gateway() {
local cmd rc=0
for cmd in $BINARIES; do
if [ -f "$BIN_DIR/$cmd.prev" ]; then
cp -f "$BIN_DIR/$cmd.prev" "$BIN_DIR/$cmd" || rc=1
else
rm -f "$BIN_DIR/$cmd" || rc=1
fi
done
return $rc
}
rollback_agent_image() {
@@ -103,8 +153,7 @@ rollback_task_db() {
rm -f "$TASK_DB_PATH-wal" "$TASK_DB_PATH-shm"
}
echo "==> restarting archied.service"
systemctl --user restart archied.service
start_units
if wait_healthy; then
echo "==> new version is healthy"
@@ -118,11 +167,11 @@ rolled_back=false
# 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
if stop_units; 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
if { [ -z "$TASK_DB_PATH" ] || rollback_task_db; } && rollback_gateway; then
rolled_back=true
fi
else
@@ -140,7 +189,7 @@ if systemctl --user stop archied.service; then
# 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
if rollback_agent_image && { [ -z "$TASK_DB_PATH" ] || rollback_task_db; } && rollback_gateway; then
rolled_back=true
fi
else
@@ -152,10 +201,10 @@ if systemctl --user stop archied.service; then
;;
esac
else
echo "==> could not stop archied.service; refusing rollback while it may be running" >&2
echo "==> could not stop every unit; refusing rollback while one may be running" >&2
fi
if [ "$rolled_back" = true ]; then
systemctl --user restart archied.service
start_units
fi
write_report "failed" "$rolled_back"