feat(releaseupdate): install from release artifacts with journaled rollback, topology check and channels

This commit is contained in:
sam committed 2026-10-05 17:02:29 +11:00
1 parent 9bb20830f3
commit ee2c9cda97
24 files changed
+1186 -333

No files matched your search

+25 -27
View File
@@ -46,7 +46,7 @@ readonly CURL_TIMEOUT="${ARCHIE_FORGE_TIMEOUT:-20}"
readonly TOKEN="${ARCHIE_FORGE_TOKEN:-}"
# base 1.7.0-4-g0736708 -> 1.7.0
base() { printf '%s\n' "${1%%-*}"; }
base() { printf '%s\n' "$1" | sed -E 's/-[0-9]+-g[0-9a-f]+$//'; }
# installed_for reads the version this binary reports for a named component.
# archied stamps both its own version and the runtime version the release was
@@ -72,35 +72,33 @@ curl_api() {
fi
}
# newest_tag_git resolves the newest tag for a component prefix from the git
# remote. No local checkout, no working-tree state, no fetch -- `ls-remote`
# queries the remote directly, so this works on a host that has never cloned
# anything.
RELEASE_CHANNEL="${ARCHIE_UPDATE_RELEASE_CHANNEL:-stable}"
PIN="${ARCHIE_UPDATE_PIN:-}"
while [ "$#" -gt 0 ]; do
case "$1" in
--channel) RELEASE_CHANNEL="$2"; shift 2 ;;
--pin) PIN="$2"; shift 2 ;;
*) echo "unknown option: $1" >&2; exit 1 ;;
esac
done
select_release() { "$BIN" select-release -channel "$RELEASE_CHANNEL" -pin "$PIN"; }
newest_tag_git() {
local component="$1" tags
command -v git >/dev/null 2>&1 || return 1
tags="$(git ls-remote --tags --refs "$SOURCE_URL" "$component/v*" 2>/dev/null)" || return 1
[ -n "$tags" ] || return 1
printf '%s\n' "$tags" | sed 's|.*refs/tags/||' | sed "s|^$component/v||" | sort -V | tail -1
local tags
tags="$(git ls-remote --tags --refs "$SOURCE_URL" 'archied/v*' 'v*' 2>/dev/null)" || return 1
printf '%s\n' "$tags" | awk '{print $2}' | select_release
}
# newest_tag_api is the API equivalent, used when git is unavailable.
newest_tag_api() {
local component="$1" page payload found total
for page in 1 2 3; do
payload="$(curl_api "${API_BASE}/repos/${REPO_SLUG}/tags?per_page=100&page=${page}")" || return 1
[ -n "$payload" ] || return 1
found="$(printf '%s\n' "$payload" \
| grep -o "\"name\":[[:space:]]*\"${component}/v[^\"]*\"" \
| sed 's|.*"'"${component}"'/v||; s|"$||' | sort -V | tail -1)"
if [ -n "$found" ]; then
printf '%s\n' "$found"
return 0
fi
total="$(printf '%s\n' "$payload" | grep -c '"name"')"
[ "${total:-0}" -lt 100 ] && break
done
return 1
local page payload tags=""
for page in $(seq 1 100); do
payload="$(curl_api "${API_BASE}/repos/${REPO_SLUG}/tags?per_page=100&page=${page}")" || return 1
tags+="$(jq -er '.[].name' <<< "$payload")"$'\n'
if [ "$(jq 'length' <<< "$payload")" -lt 100 ]; then
printf '%s' "$tags" | select_release
return $?
fi
done
echo "tag catalog exceeded pagination limit" >&2
return 1
}
# artifact_state reports whether a tagged release is PUBLISHED with assets,
+135 -151
View File
@@ -16,34 +16,8 @@ BIN_DIR="${ARCHIE_BIN_DIR:-$HOME/.local/bin}"
CONFIG_PATH="${ARCHIE_CONFIG_PATH:-${XDG_CONFIG_HOME:-$HOME/.config}/archie/config.toml}"
export PATH="/usr/local/go/bin:$PATH"
configured_agent_image() {
local image
image="$(awk '
/^\[containers\][[:space:]]*(#.*)?$/ { in_containers = 1; next }
/^\[/ { in_containers = 0 }
in_containers && /^[[:space:]]*image[[:space:]]*=/ {
value = $0
sub(/^[[:space:]]*image[[:space:]]*=[[:space:]]*/, "", value)
sub(/[[:space:]]*(#.*)?$/, "", value)
if (value ~ /^".*"$/) {
sub(/^"/, "", value)
sub(/"$/, "", value)
print value
} else if (length(value) >= 2 && substr(value, 1, 1) == sprintf("%c", 39) && substr(value, length(value), 1) == sprintf("%c", 39)) {
print substr(value, 2, length(value) - 2)
}
exit
}
' "$CONFIG_PATH" 2>/dev/null || true)"
printf '%s' "$image"
}
AGENT_IMAGE="${ARCHIE_AGENT_IMAGE:-$(configured_agent_image)}"
AGENT_IMAGE="${AGENT_IMAGE:-ghcr.io/samcharles93/archie-agent:latest}"
AGENT_IMAGE_BACKUP="$BIN_DIR/archie-agent-image.prev.tar"
# State for the managed image, independent of archied's daemon build stamp.
AGENT_VERSION_FILE="${ARCHIE_AGENT_VERSION_FILE:-$BIN_DIR/archie-agent.version}"
AGENT_VERSION_BACKUP="${ARCHIE_AGENT_VERSION_BACKUP:-$BIN_DIR/archie-agent.version.prev}"
DB_SNAPSHOT="${ARCHIE_DB_SNAPSHOT:-$BIN_DIR/archie-db.pre-update.dump}"
# The address the daemon dials for its state, so the watchdog can wait for it to
# accept connections before it starts archied. Read with a narrow awk: the
@@ -67,9 +41,27 @@ configured_state_store_target() {
STATE_STORE_TARGET="${ARCHIE_UPDATE_STATE_STORE_TARGET:-$(configured_state_store_target)}"
GATEWAY_SERVICES="archie-state-store archie-gateway archied archie-ui archie-messaging"
GATEWAY_SERVICES="archie-state-store archie-gateway archie-ui archie-messaging archied"
GATEWAY_BINARIES="$GATEWAY_SERVICES"
SOURCE_BUILD="${ARCHIE_UPDATE_SOURCE_BUILD:-0}"
AUTO=0
for arg in "$@"; do
case "$arg" in
--from-source) SOURCE_BUILD=1 ;;
--auto) AUTO=1 ;;
*) echo "unknown option: $arg" >&2; exit 1 ;;
esac
done
if [ "$AUTO" = 1 ] && [ -z "${ARCHIE_UPDATE_DAEMON_VERSION:-}${ARCHIE_UPDATE_AGENT_VERSION:-}" ]; then
snapshot="$("$(dirname "$0")/archie-update-check")"
export ARCHIE_UPDATE_DAEMON_PREVIOUS="$(jq -r '.Components[] | select(.ID == "daemon") | .Installed' <<< "$snapshot")"
export ARCHIE_UPDATE_DAEMON_VERSION="$(jq -r '.Components[] | select(.ID == "daemon") | .Available' <<< "$snapshot")"
export ARCHIE_UPDATE_AGENT_PREVIOUS="$(jq -r '.Components[] | select(.ID == "agent") | .Installed' <<< "$snapshot")"
export ARCHIE_UPDATE_AGENT_VERSION="$(jq -r '.Components[] | select(.ID == "agent") | .Available' <<< "$snapshot")"
fi
PREV_GW="${ARCHIE_UPDATE_DAEMON_PREVIOUS:-}"
PREV_RT="${ARCHIE_UPDATE_AGENT_PREVIOUS:-}"
GW_TARGET="${ARCHIE_UPDATE_DAEMON_VERSION:-}"
@@ -132,146 +124,138 @@ for target in "$GW_TARGET" "$RT_TARGET"; do
fi
done
STATE_DIR="${ARCHIE_UPDATE_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/archie/update}"
mkdir -p "$STATE_DIR"
chmod 700 "$STATE_DIR"
exec 9>"$STATE_DIR/lock"
if ! flock -n 9; then
echo "update locked by $(cat "$STATE_DIR/holder" 2>/dev/null); journal: $STATE_DIR/transaction" >&2
exit 1
fi
printf 'installer PID %s\n' "$$" > "$STATE_DIR/holder"
if [ -d "$STATE_DIR/transaction" ]; then
echo "unfinished update: run $STATE_DIR/transaction/watchdog --recover before retrying" >&2
exit 1
fi
tmp="$(mktemp -d)"
source_dir="$tmp/source"
trap 'rm -rf "$tmp"' EXIT
echo "==> fetching approved component releases"
git clone --quiet --filter=blob:none --no-checkout "$SOURCE_URL" "$source_dir"
git -C "$source_dir" fetch --quiet --tags origin
gw_commit=""
rt_commit=""
if [[ ",$components," == *,daemon,* ]]; then
gw_commit="$(git -C "$source_dir" rev-parse --verify "refs/tags/archied/v$GW^{commit}")"
source_dir="$tmp/source"
release_dir="$tmp/release"
mkdir -p "$release_dir"
release_version="${GW_TARGET:-$RT_TARGET}"
if [ -n "$GW_TARGET" ] && [ -n "$RT_TARGET" ] && [ "$GW_TARGET" != "$RT_TARGET" ]; then
echo "daemon and agent must name the same release" >&2; exit 1
fi
if [ "$SOURCE_BUILD" = 1 ]; then
tag="v$release_version"
git ls-remote --exit-code --tags "$SOURCE_URL" "refs/tags/$tag" >/dev/null || tag="archied/v$release_version"
git clone --quiet --depth=1 --branch "$tag" "$SOURCE_URL" "$source_dir"
cp "$source_dir/release.json" "$release_dir/"
GATEWAY_BINARIES="$(jq -er '.required_topology.units | join(" ")' "$release_dir/release.json")"
cp "$source_dir"/scripts/archie-update-* "$release_dir/"
if [[ ",$components," == *,daemon,* ]]; then
for cmd in $GATEWAY_BINARIES; do
[[ "$cmd" =~ ^archie[a-z0-9-]*$ ]] || { echo "invalid release unit: $cmd" >&2; exit 1; }
(cd "$source_dir" && go build -ldflags "-X github.com/samcharles93/archie-core/internal/installtype.buildType=binary -X github.com/samcharles93/archie-core/internal/buildinfo.Version=$GW -X github.com/samcharles93/archie-core/internal/buildinfo.Runtime=$RT" -o "$release_dir/$cmd" "./cmd/$cmd")
done
fi
else
case "$(uname -s)/$(uname -m)" in
Linux/x86_64) platform=linux-amd64 ;;
*) echo "no published artifact for $(uname -s)/$(uname -m)" >&2; exit 1 ;;
esac
name="archie-core-archied-v$release_version-$platform"
base="${ARCHIE_RELEASE_BASE_URL:-https://github.com/samcharles93/archie-core/releases/download}/v$release_version"
if ! curl -fLSs --max-time 300 "$base/$name.zip" -o "$tmp/$name.zip"; then
base="${ARCHIE_RELEASE_BASE_URL:-https://github.com/samcharles93/archie-core/releases/download}/archied/v$release_version"
curl -fLSs --max-time 300 "$base/$name.zip" -o "$tmp/$name.zip"
fi
curl -fLSs --max-time 60 "$base/SHA256SUMS" -o "$tmp/SHA256SUMS"
expected="$(awk -v name="$name.zip" '$2 == name {print $1}' "$tmp/SHA256SUMS")"
[[ "$expected" =~ ^[a-fA-F0-9]{64}$ ]] || { echo "missing or ambiguous archive checksum" >&2; exit 1; }
printf '%s %s\n' "$expected" "$tmp/$name.zip" | sha256sum --check --status || { echo "release checksum mismatch" >&2; exit 1; }
# Reject paths outside the single distribution directory before extracting.
while IFS= read -r entry; do
case "$entry" in "$name/"*) ;; *) echo "unsafe archive path: $entry" >&2; exit 1 ;; esac
case "/$entry/" in */../*|*/./*|*\\*) echo "unsafe archive path: $entry" >&2; exit 1 ;; esac
done < <(unzip -Z1 "$tmp/$name.zip")
if unzip -Z -l "$tmp/$name.zip" | awk '$1 ~ /^l/ {found=1} END {exit !found}'; then
echo "release contains symlinks" >&2; exit 1
fi
unzip -q "$tmp/$name.zip" -d "$tmp/unpacked"
if find "$tmp/unpacked" -type l | grep -q .; then echo "release contains symlinks" >&2; exit 1; fi
cp -a "$tmp/unpacked/$name/." "$release_dir/"
fi
# Observation and config parsing belong to the daemon, including on old hosts.
observer="$release_dir/archied"
[ -x "$observer" ] || observer="$BIN_DIR/archied"
"$observer" update-topology -config "$CONFIG_PATH" -release "$release_dir/release.json"
GATEWAY_SERVICES="$(jq -er '.required_topology.units | join(" ")' "$release_dir/release.json")"
GATEWAY_BINARIES="$GATEWAY_SERVICES"
if [[ ",$components," == *,agent,* ]]; then
rt_commit="$(git -C "$source_dir" rev-parse --verify "refs/tags/archied/v$RT^{commit}")"
fi
build_commit="$gw_commit"
[ -n "$build_commit" ] || build_commit="$rt_commit"
git -C "$source_dir" checkout --quiet --detach "$build_commit"
cd "$source_dir"
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
unmanaged_units=""
for dir in "$source_dir"/cmd/*/; do
[ -d "$dir" ] || continue
svc="$(basename "$dir")"
case " $GATEWAY_SERVICES " in
*" $svc "*) continue ;;
esac
if systemctl --user list-unit-files "$svc.service" --no-legend 2>/dev/null | grep -q .; then
unmanaged_units="${unmanaged_units:+$unmanaged_units }$svc.service"
fi
done
if [ -n "$unmanaged_units" ]; then
echo "refusing to install archied $GW: this host runs units the update does not manage: $unmanaged_units" >&2
echo "a current install.sh writes units for: $GATEWAY_SERVICES. If the installed updater predates this release, replace $BIN_DIR/archie-update-install with scripts/archie-update-install from archied/v$GW; otherwise remove the extra unit, or add it to GATEWAY_SERVICES if this host is meant to run it." >&2
exit 1
if [ "$SOURCE_BUILD" = 1 ]; then
docker build --build-arg "RUNTIME_VERSION=$RT" --iidfile "$tmp/image-id" "$source_dir"
AGENT_IMAGE="$(cat "$tmp/image-id")"
else
AGENT_IMAGE="$(jq -er '.agent_image' "$release_dir/release.json")"
[[ "$AGENT_IMAGE" =~ @sha256:[a-f0-9]{64}$ ]] || { echo "refusing agent update: release has no digest-pinned image" >&2; exit 1; }
docker pull "$AGENT_IMAGE"
fi
fi
if [[ ",$components," == *,daemon,* ]]; then
echo "==> snapshotting the database before the update migrates it"
mkdir -p "$(dirname "$DB_SNAPSHOT")"
if ! go run ./cmd/archie-state-store backup -config "$CONFIG_PATH" -out "$DB_SNAPSHOT"; then
echo "refusing to install $GW: could not snapshot the database named by $CONFIG_PATH, so a failed migration could not be recovered" >&2
exit 1
fi
if [ "$AUTO" = 1 ]; then
status="$(curl -fsS --max-time 10 "${ARCHIE_HEALTH_URL:-http://127.0.0.1:8485}/health/detailed")"
[ "$(jq -r '.in_flight' <<< "$status")" = 0 ] || { echo "refusing auto-update: daemon has work in flight or cannot report it" >&2; exit 1; }
fi
install -m755 "$source_dir/scripts/archie-update-install" "$BIN_DIR/archie-update-install"
install -m755 "$source_dir/scripts/archie-update-watchdog" "$BIN_DIR/archie-update-watchdog"
install -m755 "$source_dir/scripts/archie-update-check" "$BIN_DIR/archie-update-check"
built=""
if [[ ",$components," == *,daemon,* ]]; then
ldflags="-X github.com/samcharles93/archie-core/internal/installtype.buildType=binary"
ldflags="$ldflags -X github.com/samcharles93/archie-core/internal/buildinfo.Version=$GW"
ldflags="$ldflags -X github.com/samcharles93/archie-core/internal/buildinfo.Runtime=$RT"
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"
reported="$("$release_dir/$cmd" -version | awk -v cmd="$cmd" '$1 == cmd {print $2; exit}')"
[ "$reported" = "$GW" ] || { echo "candidate $cmd reports $reported, expected $GW" >&2; exit 1; }
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
rm -f "$BIN_DIR/$cmd.prev"
fi
done
echo "==> installing $built"
for cmd in $built; do
install -m755 "$tmp/$cmd" "$BIN_DIR/$cmd"
done
if ! verify_installed_versions "$GW"; then
exit 1
fi
mkdir -p "$(dirname "$DB_SNAPSHOT")"
"$BIN_DIR/archie-state-store" backup -config "$CONFIG_PATH" -out "$DB_SNAPSHOT"
built="$GATEWAY_BINARIES archie-update-install archie-update-watchdog archie-update-check"
fi
# Freeze recovery code and every prior file before publishing the intent.
journal="$STATE_DIR/preparing"
rm -rf "$journal"
mkdir -p "$journal/previous"
cp "$release_dir/archie-update-watchdog" "$journal/watchdog"
cp "$observer" "$journal/helper"
if [[ ",$components," == *,agent,* ]]; then
git checkout --quiet --detach "$rt_commit"
echo "==> saving managed worker image $AGENT_IMAGE for rollback"
docker image save --output "$AGENT_IMAGE_BACKUP" "$AGENT_IMAGE"
if [ -f "$AGENT_VERSION_FILE" ]; then
cp -f "$AGENT_VERSION_FILE" "$AGENT_VERSION_BACKUP"
AGENT_VERSION_PREVIOUS_EXISTS=1
else
rm -f "$AGENT_VERSION_BACKUP"
AGENT_VERSION_PREVIOUS_EXISTS=0
fi
echo "==> rebuilding managed worker image $AGENT_IMAGE for runtime $RT"
docker build --build-arg "RUNTIME_VERSION=$RT" --tag "$AGENT_IMAGE" .
version_tmp="$(mktemp "$AGENT_VERSION_FILE.tmp.XXXXXX")"
printf '%s\n' "$RT" > "$version_tmp"
chmod 600 "$version_tmp"
mv -f "$version_tmp" "$AGENT_VERSION_FILE"
"$journal/helper" update-image -config "$CONFIG_PATH" -journal "$journal" -prepare "$AGENT_IMAGE"
fi
echo "==> handing off to the update watchdog for restart and health verification"
chmod 700 "$journal/watchdog"
for cmd in $built; do
if [ -f "$BIN_DIR/$cmd" ]; then cp -p "$BIN_DIR/$cmd" "$journal/previous/$cmd"; fi
install -m755 "$release_dir/$cmd" "$BIN_DIR/.$cmd.new"
done
if [ -f "$AGENT_VERSION_FILE" ]; then cp -p "$AGENT_VERSION_FILE" "$journal/agent.version"; fi
# No unit/config changes are made: topology migration is explicitly refused.
export ARCHIE_UPDATE_AUTO="$AUTO"
export ARCHIE_UPDATE_STATE_DIR="$STATE_DIR"
export ARCHIE_BIN_DIR="$BIN_DIR" ARCHIE_CONFIG_PATH="$CONFIG_PATH"
export ARCHIE_UPDATE_COMPONENTS="$components" ARCHIE_UPDATE_UNITS="$GATEWAY_SERVICES" ARCHIE_UPDATE_BINARIES="$built"
export ARCHIE_UPDATE_PREVIOUS_GATEWAY="$PREV_GW" ARCHIE_UPDATE_PREVIOUS_RUNTIME="$PREV_RT"
export ARCHIE_UPDATE_INSTALLED_GATEWAY="$GW" ARCHIE_UPDATE_INSTALLED_RUNTIME="$RT"
export ARCHIE_AGENT_VERSION_FILE="$AGENT_VERSION_FILE" ARCHIE_DB_SNAPSHOT="$DB_SNAPSHOT"
export ARCHIE_UPDATE_STATE_STORE_TARGET="$STATE_STORE_TARGET"
# Bash's quoted declarations preserve spaces without interpreting journal values.
while IFS= read -r var; do declare -p "$var"; done < <(compgen -e | grep -E '^ARCHIE_(UPDATE_|BIN_DIR$|CONFIG_PATH$|HEALTH_URL$|AGENT_VERSION_FILE$|DB_SNAPSHOT$)') > "$journal/environment"
printf 'prepared\n' > "$journal/phase"
sync -f "$BIN_DIR"
sync -f "$journal"
mv "$journal" "$STATE_DIR/transaction"
sync -f "$STATE_DIR"
journal="$STATE_DIR/transaction"
# The independent watchdog holds the lock and owns promotion, restart and rollback.
systemd-run --user --unit=archied-update-restart --collect --quiet \
--setenv=ARCHIE_BIN_DIR="$BIN_DIR" \
--setenv=ARCHIE_HEALTH_URL="${ARCHIE_HEALTH_URL:-}" \
--setenv=ARCHIE_UPDATE_REPORT_PATH="${ARCHIE_UPDATE_REPORT_PATH:-}" \
--setenv=ARCHIE_UPDATE_CHANNEL="${ARCHIE_UPDATE_CHANNEL:-}" \
--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" \
--setenv=ARCHIE_UPDATE_INSTALLED_RUNTIME="$RT" \
--setenv=ARCHIE_AGENT_IMAGE="$AGENT_IMAGE" \
--setenv=ARCHIE_AGENT_IMAGE_BACKUP="$AGENT_IMAGE_BACKUP" \
--setenv=ARCHIE_AGENT_VERSION_FILE="$AGENT_VERSION_FILE" \
--setenv=ARCHIE_AGENT_VERSION_BACKUP="$AGENT_VERSION_BACKUP" \
--setenv=ARCHIE_AGENT_VERSION_PREVIOUS_EXISTS="${AGENT_VERSION_PREVIOUS_EXISTS:-0}" \
--setenv=ARCHIE_DB_SNAPSHOT="$DB_SNAPSHOT" \
--setenv=ARCHIE_CONFIG_PATH="$CONFIG_PATH" \
--setenv=ARCHIE_UPDATE_STATE_STORE_TARGET="$STATE_STORE_TARGET" \
"$BIN_DIR/archie-update-watchdog"
--setenv=ARCHIE_UPDATE_STATE_DIR="$STATE_DIR" "$journal/watchdog"
case "$components" in
daemon)
previous="{\"daemon\":\"$(json_escape "$PREV_GW")\"}"
+116 -134
View File
@@ -7,36 +7,39 @@
# 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}"
AGENT_VERSION_FILE="${ARCHIE_AGENT_VERSION_FILE:-$BIN_DIR/archie-agent.version}"
AGENT_VERSION_BACKUP="${ARCHIE_AGENT_VERSION_BACKUP:-$BIN_DIR/archie-agent.version.prev}"
AGENT_VERSION_PREVIOUS_EXISTS="${ARCHIE_AGENT_VERSION_PREVIOUS_EXISTS:-0}"
DB_SNAPSHOT="${ARCHIE_DB_SNAPSHOT:-$BIN_DIR/archie-db.pre-update.dump}"
CONFIG_PATH="${ARCHIE_CONFIG_PATH:-${XDG_CONFIG_HOME:-$HOME/.config}/archie/config.toml}"
# The daemon exports its own health address (releaseupdate.CommandInstaller);
# this default only covers a hand-driven run, and must stay the address archied
# itself serves /healthz on -- [health] listen, defaulted in
# internal/infrastructure/configuration/defaults.go.
STATE_DIR="${ARCHIE_UPDATE_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/archie/update}"
journal="$STATE_DIR/transaction"
mkdir -p "$STATE_DIR"
exec 9>"$STATE_DIR/lock"
if [ "${1:-}" = --recover ]; then
flock -n 9 || exit 0 # A live installer/watchdog owns recovery.
else
flock -w 60 9 || { echo "update locked by $(cat "$STATE_DIR/holder")" >&2; exit 1; }
fi
[ -d "$journal" ] || exit 0
printf 'watchdog PID %s\n' "$$" > "$STATE_DIR/holder"
source "$journal/environment"
BIN_DIR="$ARCHIE_BIN_DIR"
CONFIG_PATH="$ARCHIE_CONFIG_PATH"
DB_SNAPSHOT="$ARCHIE_DB_SNAPSHOT"
AGENT_VERSION_FILE="$ARCHIE_AGENT_VERSION_FILE"
HEALTH_URL="${ARCHIE_HEALTH_URL:-http://127.0.0.1:8485}"
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}"
# 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}"
# The State Store's address, so the start order below can mean "ready" rather than
# "launched". Empty when the config names none or the installed updater predates
# passing one -- waiting is then skipped, which is the previous behaviour.
OBSERVE_SECONDS="${ARCHIE_UPDATE_OBSERVE_SECONDS:-120}"
POLL_INTERVAL_SECONDS="${ARCHIE_UPDATE_POLL_INTERVAL:-3}"
COMPONENTS="$ARCHIE_UPDATE_COMPONENTS"
UNITS="$ARCHIE_UPDATE_UNITS"
BINARIES="$ARCHIE_UPDATE_BINARIES"
STATE_STORE_TARGET="${ARCHIE_UPDATE_STATE_STORE_TARGET:-}"
STATE_STORE_TIMEOUT_SECONDS="${ARCHIE_UPDATE_STATE_STORE_TIMEOUT:-30}"
phase() {
printf '%s\n' "$1" > "$journal/phase.new" && sync -f "$journal" &&
mv "$journal/phase.new" "$journal/phase" && sync -f "$journal"
}
json_escape() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
}
@@ -75,10 +78,22 @@ EOF
mv -f "$tmp" "$REPORT_PATH"
}
healthy() {
local report
report="$(curl -fsS --max-time 10 "$HEALTH_URL/health/detailed")" || return 1
[ "$(jq -r '.version' <<< "$report")" = "$ARCHIE_UPDATE_INSTALLED_GATEWAY" ] &&
[ "$(jq -r '.status' <<< "$report")" = ok ]
}
wait_healthy() {
local deadline=$((SECONDS + HEALTH_TIMEOUT_SECONDS))
while [ "$SECONDS" -lt "$deadline" ]; do
if curl -sf -o /dev/null "$HEALTH_URL/healthz"; then
if healthy; then
deadline=$((SECONDS + OBSERVE_SECONDS))
while [ "$SECONDS" -lt "$deadline" ]; do
sleep "$POLL_INTERVAL_SECONDS"
healthy || return 1
done
return 0
fi
sleep "$POLL_INTERVAL_SECONDS"
@@ -86,20 +101,7 @@ wait_healthy() {
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
systemctl --user "$action" "$unit.service" || true
}
manage_unit() { systemctl --user "$1" "$2.service"; }
# port_accepts reports whether host:port accepts a connection. bash's /dev/tcp
# costs no process and no dependency, and accepting is exactly the signal this
@@ -109,21 +111,7 @@ port_accepts() {
(exec 3<>"/dev/tcp/${1%:*}/${1##*:}") 2>/dev/null
}
# wait_for_state_store blocks until the State Store accepts connections, or the
# budget runs out.
#
# Ordering the units is not enough on its own: `systemctl --user start` returns
# once the process is forked, not once its port accepts. Measured on a 1.37.1 ->
# 1.38.0 update, the State Store's unit reported started while it was still
# binding, archied was started next, dialled a closed port and exited 1, and only
# systemd's Restart=on-failure brought it back seven seconds later. A host whose
# unit lacks that restart policy gets a failed update behind a healthy-looking
# "build and install succeeded" line.
#
# A timeout does NOT fail the update. If the State Store never comes up, archied
# fails closed on its own and wait_healthy rolls the release back -- waiting is an
# optimisation for the ordinary case, and losing the race must stay a slower path
# to the same place rather than a second failure mode.
# Wait for the dependency before starting its callers.
wait_for_state_store() {
[ -n "$STATE_STORE_TARGET" ] || {
echo "==> no State Store target known; starting the rest without waiting for it" >&2
@@ -169,39 +157,70 @@ stop_units() {
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
rollback_files() {
local cmd
for cmd in $BINARIES; do
if [ -f "$BIN_DIR/$cmd.prev" ]; then
cp -f "$BIN_DIR/$cmd.prev" "$BIN_DIR/$cmd" || rc=1
if [ -f "$journal/previous/$cmd" ]; then
cp -p "$journal/previous/$cmd" "$BIN_DIR/.$cmd.rollback" &&
mv -f "$BIN_DIR/.$cmd.rollback" "$BIN_DIR/$cmd" || return 1
else
rm -f "$BIN_DIR/$cmd" || rc=1
rm -f "$BIN_DIR/$cmd" || return 1
fi
rm -f "$BIN_DIR/.$cmd.new"
done
return $rc
if [[ ",$COMPONENTS," == *,agent,* ]]; then
if [ -f "$journal/agent.version" ]; then
cp -p "$journal/agent.version" "$AGENT_VERSION_FILE.new" &&
mv -f "$AGENT_VERSION_FILE.new" "$AGENT_VERSION_FILE" || return 1
else
rm -f "$AGENT_VERSION_FILE" || return 1
fi
fi
sync -f "$BIN_DIR"
}
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"
finish() {
phase "$1" || return 1
# A terminal journal is harmless if power fails before removal.
rm -rf "$STATE_DIR/finished"
mv "$journal" "$STATE_DIR/finished" && sync -f "$STATE_DIR" || return 1
rm -rf "$STATE_DIR/finished"
}
# The sidecar describes the image restored above. Restore its prior state in
# the same rollback path; a first install has no prior file and must remove the
# newly-created metadata rather than leave a false installed version behind.
rollback_agent_version() {
if [ "$AGENT_VERSION_PREVIOUS_EXISTS" = 1 ]; then
[ -f "$AGENT_VERSION_BACKUP" ] || return 1
cp -f "$AGENT_VERSION_BACKUP" "$AGENT_VERSION_FILE"
else
rm -f "$AGENT_VERSION_FILE"
rollback() {
phase rolling-back || return 1
stop_units && rollback_files || return 1
if [ -f "$journal/image.json" ]; then
manage_unit start archie-state-store && wait_for_state_store &&
"$journal/helper" update-image -config "$CONFIG_PATH" -journal "$journal" -rollback || return 1
fi
phase rolled-back || return 1
start_units || return 1
write_report failed true || return 1
finish rolled-back
}
promote() {
if [ "${ARCHIE_UPDATE_AUTO:-0}" = 1 ]; then
local status
status="$(curl -fsS --max-time 10 "$HEALTH_URL/health/detailed")" || return 2
[ "$(jq -r '.in_flight' <<< "$status")" = 0 ] || return 2
fi
phase switching || return 1
stop_units || return 1
for cmd in $BINARIES; do
mv -f "$BIN_DIR/.$cmd.new" "$BIN_DIR/$cmd" || return 1
done
if [[ ",$COMPONENTS," == *,agent,* ]]; then
printf '%s\n' "$ARCHIE_UPDATE_INSTALLED_RUNTIME" > "$AGENT_VERSION_FILE.new" &&
mv -f "$AGENT_VERSION_FILE.new" "$AGENT_VERSION_FILE" || return 1
fi
sync -f "$BIN_DIR" || return 1
if [ -f "$journal/image.json" ]; then
manage_unit start archie-state-store && wait_for_state_store &&
"$journal/helper" update-image -config "$CONFIG_PATH" -journal "$journal" || return 1
fi
phase observing && start_units && wait_healthy
}
# explain_database_recovery states what a daemon rollback does not do. The
@@ -214,62 +233,25 @@ explain_database_recovery() {
echo "==> archie-state-store restore -config $CONFIG_PATH -from $DB_SNAPSHOT" >&2
}
start_units
if wait_healthy; then
echo "==> new version is healthy"
write_report "passed" false
case "$(cat "$journal/phase")" in
committed) start_units && finish committed; exit $? ;;
rolled-back) start_units && finish rolled-back; exit $? ;;
esac
if [ -z "${1:-}" ] && promote; then
write_report passed false && finish committed
exit $?
fi
if [ "$(cat "$journal/phase")" = prepared ]; then
echo "update was not promoted; leaving running work alone" >&2
for cmd in $BINARIES; do rm -f "$BIN_DIR/.$cmd.new"; done
write_report failed false && finish rolled-back || exit 1
if [ "${1:-}" = --boot-recover ]; then start_units; exit $?; fi
exit 1
fi
echo "==> restoring interrupted or unhealthy update" >&2
if rollback; then
if [[ ",$COMPONENTS," == *,daemon,* ]]; then explain_database_recovery; fi
exit 0
fi
echo "==> health check failed after ${HEALTH_TIMEOUT_SECONDS}s; rolling back changed components"
rolled_back=false
# Stop before touching the executable. The service uses Restart=on-failure in
# the supported unit, so copying rollback state while a crash-loop child is
# alive can execute the new binary after its files have been replaced.
if stop_units; then
case "$COMPONENTS" in
daemon)
if [ -f "$BIN_DIR/archied.prev" ]; then
if rollback_gateway; 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
if rollback_agent_image && rollback_agent_version; then
rolled_back=true
fi
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" ]; then
if rollback_agent_image && rollback_agent_version && rollback_gateway; 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 every unit; refusing rollback while one may be running" >&2
fi
if [ "$rolled_back" = true ]; then
start_units
fi
if [[ ",$COMPONENTS," == *,daemon,* ]]; then
explain_database_recovery
fi
write_report "failed" "$rolled_back"
echo "rollback incomplete; retained journal: $journal" >&2
exit 1