diff --git a/deploy_workflow_test.go b/deploy_workflow_test.go index 7288e6bb5..0be9b8a95 100644 --- a/deploy_workflow_test.go +++ b/deploy_workflow_test.go @@ -95,21 +95,25 @@ var unpackagedCommands = map[string]string{ // they are not services. var cliOnlyCommands = map[string]bool{"archie-playbooks": true} -// TestHostCommandListsCannotDrift is why archie-core-1c01 went unnoticed for two -// days. The v1.30.0 extraction moved Telegram, email and webhook out of archied -// into a new cmd/archie-messaging that nothing packaged: the release zip omitted -// it, and scripts/archie-update-install omitted it from both of its lists, so a -// host install ran on with a dead Telegram bot and no error logged anywhere. The -// daemon looked healthy throughout. +// TestDistZipShipsEveryHostCommand is why archie-core-1c01 went unnoticed for +// two days. The v1.30.0 extraction moved Telegram, email and webhook out of +// archied into a new cmd/archie-messaging that nothing packaged: the release zip +// omitted it, scripts/archie-update-install omitted it from both of its lists, +// and install.sh's build loop omitted it too, so a host install ran on with a +// dead Telegram bot and no error logged anywhere. The daemon looked healthy +// throughout. // -// The host component set was enumerated independently in three places (the zip's -// build loop, the zip's copy list, and the installer's two lists), so two -// coordinated edits would have drifted again. Every list is pinned to cmd/ here -// instead: adding a command fails this test until it is packaged, and extracting -// one fails it until the previous command is removed. -func TestHostCommandListsCannotDrift(t *testing.T) { +// The host component set was enumerated independently in four places (the zip's +// build loop, the zip's copy list, install.sh's build loop, and the updater's +// two lists), so two coordinated edits would have drifted again. Every list is +// pinned to cmd/ here instead: adding a command fails this test until it is +// packaged, and extracting one fails it until the previous command is removed. +// The updater's service list is separately required to have a documented unit, +// and its runtime guard refuses a host unit the list omits. +func TestDistZipShipsEveryHostCommand(t *testing.T) { workflow := readDeploymentFile(t, ".github/workflows/deploy.yml") installer := readDeploymentFile(t, filepath.Join("scripts", "archie-update-install")) + installSh := readDeploymentFile(t, "install.sh") want := hostRunCommands(t) wantBinaries := want @@ -127,6 +131,7 @@ func TestHostCommandListsCannotDrift(t *testing.T) { } services := wordsAfter(t, installer, `GATEWAY_SERVICES="`, `"`, nil) binaries := wordsAfter(t, installer, `GATEWAY_BINARIES="`, `"`, map[string][]string{"$GATEWAY_SERVICES": services}) + installBuilt := installBuildCommands(t, installSh) for _, list := range []struct { name string @@ -135,6 +140,7 @@ func TestHostCommandListsCannotDrift(t *testing.T) { }{ {"deploy.yml dist-zip build loop", built, wantBinaries}, {"deploy.yml dist-zip copy list", shipped, wantBinaries}, + {"install.sh build loop", installBuilt, wantBinaries}, {"archie-update-install GATEWAY_BINARIES", binaries, wantBinaries}, {"archie-update-install GATEWAY_SERVICES", services, wantServices}, } { @@ -158,6 +164,29 @@ func TestHostCommandListsCannotDrift(t *testing.T) { } } +// installBuildCommands reads install.sh's build loop. The file has a second +// `for cmd in` (the git/go preflight), so the loop is located by the go build it +// drives rather than by the first marker -- and read from the real list, so a +// missing command is named instead of the marker merely going absent. +func installBuildCommands(t *testing.T, source string) []string { + t.Helper() + before, _, ok := strings.Cut(source, `go build -ldflags "${LDFLAGS}"`) + if !ok { + t.Fatal("install.sh has no go build loop to read") + } + prefix := before + start := strings.LastIndex(prefix, "for cmd in ") + if start < 0 { + t.Fatal("install.sh build loop has no for cmd list") + } + rest := prefix[start+len("for cmd in "):] + before0, _, ok0 := strings.Cut(rest, "; do") + if !ok0 { + t.Fatal("install.sh build loop is unterminated") + } + return strings.Fields(before0) +} + // hostRunCommands is every command under cmd/, minus the deliberately // unpackaged ones. Deriving it is the point: a list written out here would drift // from cmd/ exactly as the packaged lists did. diff --git a/install.sh b/install.sh index 02d05a9ba..ae10c61af 100755 --- a/install.sh +++ b/install.sh @@ -175,15 +175,18 @@ echo "==> Building native archie binaries..." LDFLAGS="${LDFLAGS} -X github.com/samcharles93/archie-core/internal/app/archied.runtimeVersion=${RUNTIME_VERSION}" LDFLAGS="${LDFLAGS} -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary" # archied does not run alone: the State Store owns archie.db, the Gateway - # serves the chat contract, and the dashboard is its own process. Building - # only archied leaves it unable to boot. Keep this list and the one in - # scripts/archie-update-install together. - for cmd in archied archie-gateway archie-state-store archie-ui archie-playbooks; do + # serves the chat contract, the dashboard is its own process, and the + # Messaging Service owns the chat channels. Building only archied leaves it + # unable to boot, and omitting archie-messaging leaves the Telegram, email and + # webhook channels dead with no error anywhere (archie-core-1c01). This list, + # the zip's two lists and the two in scripts/archie-update-install must agree; + # TestDistZipShipsEveryHostCommand fails when they do not. + for cmd in archied archie-gateway archie-state-store archie-ui archie-messaging archie-playbooks; do go build -ldflags "${LDFLAGS}" -o "${ARCHIE_BIN_DIR}/${cmd}" "./cmd/${cmd}" done install -m755 "${SRC_DIR}/scripts/archie-update-install" "${ARCHIE_BIN_DIR}/archie-update-install" ) -echo " Installed archied, archie-gateway, archie-state-store, archie-ui, archie-playbooks and updater to ${ARCHIE_BIN_DIR}/" +echo " Installed archied, archie-gateway, archie-state-store, archie-ui, archie-messaging, archie-playbooks and updater to ${ARCHIE_BIN_DIR}/" # 5. Interactive Configuration: Forge & LLM Provider Setup if [ ! -f "${ENV_FILE}" ]; then diff --git a/internal/releaseupdate/install_script_test.go b/internal/releaseupdate/install_script_test.go index 594a9d4ad..316069666 100644 --- a/internal/releaseupdate/install_script_test.go +++ b/internal/releaseupdate/install_script_test.go @@ -339,6 +339,15 @@ func runUpdateInstall(t *testing.T, environment map[string]string, wantErr bool) // the updater backs up, which image it rebuilds -- is otherwise untestable, // which is how the task-database backup path came to be wrong. func runUpdateInstallWithConfig(t *testing.T, environment map[string]string, configBody string, wantErr bool) (Result, []string, string) { + t.Helper() + return runUpdateInstallEdited(t, environment, configBody, nil, wantErr) +} + +// runUpdateInstallEdited is runUpdateInstallWithConfig with the ability to edit +// the installer's text before it runs. The service list is hardcoded, so the +// only way to test the coverage guard against the stale list it exists for is to +// change that text -- which is exactly the state the guard must catch. +func runUpdateInstallEdited(t *testing.T, environment map[string]string, configBody string, edit func(string) string, wantErr bool) (Result, []string, string) { t.Helper() ctx := t.Context() @@ -413,10 +422,16 @@ fi writeFakeCommand(t, fakeDir, "systemctl", ` printf '%s\n' "systemctl $*" >> "$ARCHIE_TEST_CALLS" if [ "$2" = list-unit-files ]; then + # The reference deployment runs exactly these five as units. A command with + # no unit -- archie-playbooks, archie-agent -- has to look unitless, or the + # coverage guard would mistake every cmd/ directory for a host service. + present="${ARCHIE_TEST_PRESENT_UNITS:-archied.service archie-gateway.service archie-state-store.service archie-ui.service archie-messaging.service}" case " ${ARCHIE_TEST_ABSENT_UNITS:-} " in *" $3 "*) exit 0 ;; esac - printf '%s enabled enabled\n' "$3" + case " $present " in + *" $3 "*) printf '%s enabled enabled\n' "$3" ;; + esac fi `) writeFakeCommand(t, fakeDir, "install", ` @@ -450,9 +465,13 @@ fi if err != nil { t.Fatal(err) } + script := string(scriptData) + if edit != nil { + script = edit(script) + } scriptPath := filepath.Join(work, "archie-update-install") - scriptData = []byte(strings.Replace(string(scriptData), `export PATH="/usr/local/go/bin:$PATH"`, `export PATH="$PATH"`, 1)) - if err := os.WriteFile(scriptPath, scriptData, 0o755); err != nil { + script = strings.Replace(script, `export PATH="/usr/local/go/bin:$PATH"`, `export PATH="$PATH"`, 1) + if err := os.WriteFile(scriptPath, []byte(script), 0o755); err != nil { t.Fatal(err) } cmd := exec.CommandContext(ctx, scriptPath) @@ -667,11 +686,40 @@ func TestUpdateInstallRefusesWhenAServiceUnitIsMissing(t *testing.T) { if !strings.Contains(output, "archie-state-store.service") { t.Errorf("refusal must name the missing unit; output = %q", output) } + t.Logf("refusal output:\n%s", strings.TrimSpace(output)) // Nothing may be replaced on a host it would leave broken. assertCallAbsent(t, calls, "go build") assertCallAbsent(t, calls, "install -m755") } +// The other half of the same contract, and the failure that actually happened +// (archie-core-1faq): the installed updater's GATEWAY_SERVICES predated +// archie-messaging, so the update neither backed it up nor restarted it and a +// hand-installed binary kept serving under an otherwise updated host. A unit +// this host runs must be named by the list, or the update refuses and says which. +func TestUpdateInstallRefusesWhenTheServiceListOmitsAHostUnit(t *testing.T) { + _, calls, output := runUpdateInstallEdited(t, map[string]string{ + "ARCHIE_UPDATE_DAEMON_PREVIOUS": "1.22.0", + "ARCHIE_UPDATE_DAEMON_VERSION": "1.23.0", + "ARCHIE_UPDATE_AGENT_PREVIOUS": "1.21.0", + }, "[containers]\nimage = 'registry.example/archie-agent:stable'\n", func(script string) string { + // Drop archie-messaging from the list without touching its unit: the + // stale installed updater's view of the host. + return strings.Replace(script, + `GATEWAY_SERVICES="archie-state-store archie-gateway archied archie-ui archie-messaging"`, + `GATEWAY_SERVICES="archie-state-store archie-gateway archied archie-ui"`, + 1) + }, true) + + if !strings.Contains(output, "archie-messaging.service") { + t.Errorf("refusal must name the unmanaged unit; output = %q", output) + } + t.Logf("refusal output:\n%s", strings.TrimSpace(output)) + // Nothing may be replaced on a host the update would leave partial. + assertCallAbsent(t, calls, "go build") + assertCallAbsent(t, calls, "install -m755") +} + // The deployment is several processes, so the watchdog must cycle all of them, // and in dependency order: the State Store owns archie.db and everything dials // it, so it starts first and stops last (archie-core-hbqk). diff --git a/scripts/archie-update-install b/scripts/archie-update-install index 04cb83ff1..c1fa695b3 100755 --- a/scripts/archie-update-install +++ b/scripts/archie-update-install @@ -92,7 +92,7 @@ TASK_DB_BACKUP="${ARCHIE_TASK_DB_BACKUP:-$BIN_DIR/archie-task-db.prev.sqlite}" # archie-messaging is a service, not a CLI: it owns the Telegram, email and # webhook channels since the v1.30.0 extraction, so a host that updates without # it keeps running with dead channels and no error anywhere -# (archie-core-1c01). TestUpdateInstallCoversEveryHostRunCommand fails if this +# (archie-core-1c01). TestDistZipShipsEveryHostCommand fails if this # list and GATEWAY_BINARIES stop covering the commands the release zip ships. GATEWAY_SERVICES="archie-state-store archie-gateway archied archie-ui archie-messaging" GATEWAY_BINARIES="$GATEWAY_SERVICES archie-playbooks" @@ -182,6 +182,30 @@ if [[ ",$components," == *,daemon,* ]]; then echo "create them from deployments/systemd-user-service.md, then retry the update" >&2 exit 1 fi + + # The other direction, and the one archie-core-1faq actually was: a unit this + # host runs that GATEWAY_SERVICES does not name is never backed up, built, + # installed or restarted, so it keeps running the previous release while + # everything else moves. The installed updater is refreshed only after an + # update proceeds, so a stale list cannot repair itself -- refuse and name + # what it left out rather than completing a partial update and reporting + # success. + 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 "the installed $BIN_DIR/archie-update-install predates them; install scripts/archie-update-install from archied/v$GW, then retry" >&2 + exit 1 + fi fi # Pre-flight, before any write to the host. The task database backup is the