diff --git a/internal/releaseupdate/install_script_test.go b/internal/releaseupdate/install_script_test.go index 03e8bb24f..6c52450f1 100644 --- a/internal/releaseupdate/install_script_test.go +++ b/internal/releaseupdate/install_script_test.go @@ -282,6 +282,15 @@ func runUpdateInstallScript(t *testing.T, environment map[string]string) (Result // returns the parsed result (empty when the run was expected to fail), the // recorded calls, and the combined output. func runUpdateInstall(t *testing.T, environment map[string]string, wantErr bool) (Result, []string, string) { + t.Helper() + return runUpdateInstallWithConfig(t, environment, "[containers]\nimage = 'registry.example/archie-agent:stable'\n", wantErr) +} + +// runUpdateInstallWithConfig is runUpdateInstall with the operator's config +// file under the caller's control. Config-derived behaviour -- which database +// 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() ctx := t.Context() @@ -297,7 +306,7 @@ func runUpdateInstall(t *testing.T, environment map[string]string, wantErr bool) if err := os.MkdirAll(binDir, 0o755); err != nil { t.Fatal(err) } - if err := os.WriteFile(configPath, []byte("[containers]\nimage = 'registry.example/archie-agent:stable'\n"), 0o600); err != nil { + if err := os.WriteFile(configPath, []byte(configBody), 0o600); err != nil { t.Fatal(err) } for _, name := range []string{"archied", "archie-agent"} { @@ -359,6 +368,21 @@ printf '%s\n' "install $*" >> "$ARCHIE_TEST_CALLS" writeFakeCommand(t, fakeDir, "cp", ` printf '%s\n' "cp $*" >> "$ARCHIE_TEST_CALLS" /usr/bin/cp "$@" +`) + writeFakeCommand(t, fakeDir, "sqlite3", ` +printf '%s\n' "sqlite3 $*" >> "$ARCHIE_TEST_CALLS" +[ "${ARCHIE_TEST_SQLITE3_FAILS:-}" = "1" ] && exit 1 +# Reproduce ".backup ''" by writing a plausible backup beside the +# source, so the watchdog's existence check behaves as it would in production. +for arg in "$@"; do + case "$arg" in + .backup*) target="$(printf '%s' "$arg" | sed "s/^\.backup[[:space:]]*//; s/^'//; s/'$//")" ;; + esac +done +if [ -n "${target:-}" ]; then + mkdir -p "$(dirname "$target")" + printf 'backup\n' > "$target" +fi `) // The production adapter prepends /usr/local/go/bin. Run an otherwise @@ -662,3 +686,96 @@ func TestUpdateWatchdogCyclesEveryUnitInDependencyOrder(t *testing.T) { } } } + +// The updater must back up the database the daemon actually uses, which is +// `-tasks.sqlite` (taskDBPath in internal/app/archied/main.go), not +// the configured path itself. The configured path may be a zero-byte +// placeholder while the real store sits beside it -- so backing the literal +// value up produces an empty backup, and a rollback then restores that empty +// file over the database and deletes its -wal/-shm. A rollback that destroys +// the task store is worse than no rollback. +func TestUpdateInstallBacksUpTheSiblingTaskDatabase(t *testing.T) { + work := t.TempDir() + configured := filepath.Join(work, "archie.db") + realStore := configured + "-tasks.sqlite" + if err := os.WriteFile(configured, nil, 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(realStore, []byte("real task data"), 0o600); err != nil { + t.Fatal(err) + } + backup := filepath.Join(work, "backup.sqlite") + + _, calls, _ := runUpdateInstallWithConfig(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", + "ARCHIE_TASK_DB_BACKUP": backup, + }, "db_path = \""+configured+"\"\n\n[containers]\nimage = 'registry.example/archie-agent:stable'\n", false) + + assertCallContains(t, calls, "sqlite3", realStore, ".backup") + assertCallContains(t, calls, "systemd-run", "--setenv=ARCHIE_TASK_DB_PATH="+realStore) + // The zero-byte placeholder is not the store, and must never be the thing + // handed to the watchdog as ARCHIE_TASK_DB_PATH. + assertCallAbsent(t, calls, "--setenv=ARCHIE_TASK_DB_PATH="+configured+" ") +} + +// db_path also appears in other sections -- [indexing] owns its own database. +// Reading the first match in the file can select an unrelated database, so the +// value must come from the daemon's own top-level key. +func TestUpdateInstallIgnoresDbPathFromOtherSections(t *testing.T) { + work := t.TempDir() + configured := filepath.Join(work, "archie.db") + realStore := configured + "-tasks.sqlite" + indexStore := filepath.Join(work, "workspace-indexes.db") + for path, content := range map[string]string{realStore: "real task data", indexStore: "index data"} { + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + } + backup := filepath.Join(work, "backup.sqlite") + + _, calls, _ := runUpdateInstallWithConfig(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", + "ARCHIE_TASK_DB_BACKUP": backup, + }, "db_path = \""+configured+"\"\n\n[indexing]\ndb_path = \""+indexStore+"\"\n\n[containers]\nimage = 'registry.example/archie-agent:stable'\n", false) + + assertCallContains(t, calls, "sqlite3", realStore) + for _, call := range calls { + if strings.Contains(call, "sqlite3") && strings.Contains(call, indexStore) { + t.Errorf("backed up the indexing database %s: %q", indexStore, call) + } + } + assertCallAbsent(t, calls, "--setenv=ARCHIE_TASK_DB_PATH="+indexStore) +} + +// A store that cannot be backed up must fail the update before anything is +// replaced. Installing with no usable rollback is how a failed update becomes +// a destroyed task store. +func TestUpdateInstallRefusesWhenTaskDatabaseCannotBeBackedUp(t *testing.T) { + work := t.TempDir() + configured := filepath.Join(work, "archie.db") + if err := os.WriteFile(configured+"-tasks.sqlite", []byte("data"), 0o600); err != nil { + t.Fatal(err) + } + _, calls, output := runUpdateInstallWithConfig(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", + "ARCHIE_TEST_SQLITE3_FAILS": "1", + }, "db_path = \""+configured+"\"\n\n[containers]\nimage = 'registry.example/archie-agent:stable'\n", true) + + if !strings.Contains(output, "task database") { + t.Errorf("refusal must name the task database; output = %q", output) + } + // Nothing of the release may be placed on the host, and no backup of the + // failure path may be attempted either. + for _, call := range calls { + if strings.Contains(call, "install -m755") && !strings.Contains(call, "/scripts/") { + t.Errorf("refused update still installed %q", call) + } + } + assertCallAbsent(t, calls, "arhied.prev") +} diff --git a/scripts/archie-update-install b/scripts/archie-update-install index da8ce6cfb..0e4972a20 100755 --- a/scripts/archie-update-install +++ b/scripts/archie-update-install @@ -43,9 +43,22 @@ configured_agent_image() { printf '%s' "$image" } +# The task store is `-tasks.sqlite`, not db_path itself: the daemon +# derives it with taskDBPath() in internal/app/archied/main.go, and the +# configured path may be a zero-byte placeholder while the real store sits +# beside it. Backing up the literal value would produce an empty backup, and a +# rollback would then restore that empty file over the database and delete its +# -wal/-shm -- destroying the task store it exists to protect. +# +# Only the top-level key counts. db_path also appears in other sections +# ([indexing] owns its own database), and the first match in the file can +# easily be one of those. configured_task_db_path() { - awk ' - /^[[:space:]]*db_path[[:space:]]*=/ { + local base + base="$(awk ' + BEGIN { top = 1 } + /^[[:space:]]*\[/ { top = 0 } + top && /^[[:space:]]*db_path[[:space:]]*=/ { value = $0 sub(/^[[:space:]]*db_path[[:space:]]*=[[:space:]]*/, "", value) sub(/[[:space:]]*(#.*)?$/, "", value) @@ -53,7 +66,9 @@ configured_task_db_path() { 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 + ' "$CONFIG_PATH" 2>/dev/null || true)" + [ -n "$base" ] || return 0 + printf '%s' "$base-tasks.sqlite" } AGENT_IMAGE="${ARCHIE_AGENT_IMAGE:-$(configured_agent_image)}" @@ -163,18 +178,26 @@ if [[ ",$components," == *,daemon,* ]]; then fi fi +# Pre-flight, before any write to the host. The task database backup is the +# watchdog's only route back from a forward schema migration, so an update +# that cannot take one must not start: once binaries are replaced, a failure +# leaves the previous release unable to read the store it just migrated. This +# runs ahead of the adapter refresh below so a refusal changes nothing at all. +if [[ ",$components," == *,daemon,* ]] && [ -n "$TASK_DB_PATH" ] && [ -f "$TASK_DB_PATH" ]; then + echo "==> backing up task database for schema-safe rollback" + mkdir -p "$(dirname "$TASK_DB_BACKUP")" + if ! sqlite3 "$TASK_DB_PATH" ".backup '$TASK_DB_BACKUP'"; then + echo "refusing to install $GW: could not back up the task database at $TASK_DB_PATH, so a failed update could not be rolled back" >&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" 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" -if [[ ",$components," == *,daemon,* ]] && [ -n "$TASK_DB_PATH" ] && [ -f "$TASK_DB_PATH" ]; then - echo "==> backing up task database for schema-safe rollback" - mkdir -p "$(dirname "$TASK_DB_BACKUP")" - 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=""