diff --git a/scripts/archie-update-check b/scripts/archie-update-check index f4a828f1e..edba2e0d9 100755 --- a/scripts/archie-update-check +++ b/scripts/archie-update-check @@ -5,14 +5,15 @@ # via CommandCatalog (internal/releaseupdate/command.go), which never invokes # a shell, so this file must be directly executable. # -# "Installed" is the release the running binary was ACTUALLY built from, read -# via `archied -version` rather than trusted from git or filesystem state -- -# if a prior build/install step silently failed partway, the checkout can -# disagree with what's really running, and -version is the one source that -# cannot be wrong about that. The build stamp carries a git-describe suffix -# when HEAD has moved past the tag (1.7.0-4-g0736708); that suffix is -# stripped for the comparison, because unreleased commits are not an -# available update and would otherwise report one forever. +# "Installed" for the daemon is the release the running binary was ACTUALLY +# built from, read via `archied -version`. The agent value in that output is +# only the runtime version the daemon release was cut with; it is not the +# managed image that an installer later rebuilt. The installer therefore owns +# a sidecar beside BIN_DIR, archie-agent.version, and that is authoritative. +# Older installations without the sidecar use the agent line as a compatibility +# fallback. An existing malformed sidecar is reported as unknown, not replaced +# by a fabricated version. Build-stamp git-describe suffixes are stripped for +# comparison (1.7.0-4-g0736708 -> 1.7.0). # # "Available" is the newest release TAG, resolved without any dependency on a # local checkout. Two independent sources, in order: @@ -39,6 +40,8 @@ readonly REPO_SLUG="${ARCHIE_SOURCE_SLUG:-samcharles93/archie-core}" readonly API_BASE="${ARCHIE_FORGE_API:-https://api.github.com}" readonly SOURCE_URL="${ARCHIE_SOURCE_URL:-https://github.com/${REPO_SLUG}.git}" readonly BIN="${ARCHIE_BIN:-$HOME/.local/bin/archied}" +readonly BIN_DIR="${ARCHIE_BIN_DIR:-$(dirname "$BIN")}" +readonly AGENT_VERSION_FILE="${ARCHIE_AGENT_VERSION_FILE:-$BIN_DIR/archie-agent.version}" readonly CURL_TIMEOUT="${ARCHIE_FORGE_TIMEOUT:-20}" readonly TOKEN="${ARCHIE_FORGE_TOKEN:-}" @@ -120,7 +123,18 @@ artifact_state() { esc() { printf '%s' "${1:-}" | sed 's/\\/\\\\/g; s/"/\\"/g'; } daemon_installed="$(base "$(installed_for archied || true)")" -agent_installed="$(base "$(installed_for archie-agent || true)")" +agent_installed="" +if [ -f "$AGENT_VERSION_FILE" ]; then + # A sidecar is updater state: accept only one released semantic version and + # deliberately do not fall back if it is empty or malformed. + sidecar_version="$(awk 'NF { if (seen++) { bad=1; next } gsub(/^[[:space:]]+|[[:space:]]+$/, "", $0); print } END { if (bad) exit 1 }' "$AGENT_VERSION_FILE" 2>/dev/null || true)" + if [[ "$sidecar_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([+-][0-9A-Za-z.-]+)?$ ]]; then + agent_installed="$(base "$sidecar_version")" + fi +else + # Compatibility for hosts installed before the sidecar existed. + agent_installed="$(base "$(installed_for archie-agent || true)")" +fi daemon_available="$(newest_tag_git archied || true)" agent_available="$(newest_tag_git archie || true)" diff --git a/scripts/archie-update-install b/scripts/archie-update-install index 059cdd662..c13039a62 100755 --- a/scripts/archie-update-install +++ b/scripts/archie-update-install @@ -74,6 +74,9 @@ configured_task_db_path() { 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}" 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}" @@ -231,8 +234,23 @@ 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" + # Save explicit sidecar state, including the first-install (absent) case, + # before changing it. The watchdog restores this alongside the 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" . + # Only record a version after the image build succeeds. Write beside the + # state atomically so the check never observes a partial version. + 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" fi echo "==> handing off to the update watchdog for restart and health verification" @@ -252,6 +270,9 @@ systemd-run --user --unit=archied-update-restart --collect --quiet \ --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_TASK_DB_PATH="$TASK_DB_PATH" \ --setenv=ARCHIE_TASK_DB_BACKUP="$TASK_DB_BACKUP" \ "$BIN_DIR/archie-update-watchdog" diff --git a/scripts/archie-update-watchdog b/scripts/archie-update-watchdog index aa34fa9c3..5e023c60e 100755 --- a/scripts/archie-update-watchdog +++ b/scripts/archie-update-watchdog @@ -10,6 +10,9 @@ 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}" 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 health address (releaseupdate.CommandInstaller); @@ -139,6 +142,18 @@ rollback_agent_image() { docker image load --input "$AGENT_IMAGE_BACKUP" } +# 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" + fi +} + rollback_task_db() { if [ -z "$TASK_DB_PATH" ]; then echo "==> no task database path; cannot restore schema-compatible state" >&2 @@ -180,7 +195,9 @@ if stop_units; then ;; agent) if [ -f "$AGENT_IMAGE_BACKUP" ]; then - rollback_agent_image && rolled_back=true + 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 @@ -189,7 +206,7 @@ if stop_units; 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_gateway; then + if rollback_agent_image && rollback_agent_version && { [ -z "$TASK_DB_PATH" ] || rollback_task_db; } && rollback_gateway; then rolled_back=true fi else