fix(update): back up the task store the daemon actually uses, or refuse
configured_task_db_path() read the top-level db_path and handed that literal file to the watchdog as ARCHIE_TASK_DB_PATH (scripts/archie-update-install). The daemon's task store is not that file: taskDBPath() in internal/app/archied/main.go derives `<db_path>-tasks.sqlite`, and the configured path is commonly a zero-byte placeholder beside it. The updater therefore backed up an empty file, and a rollback would have restored that empty file over the real database and deleted its -wal/-shm -- destroying the task store the backup exists to protect. That awk also matched db_path in any section. [indexing] owns its own database, so a config listing it before the top-level key handed the watchdog an unrelated database as ARCHIE_TASK_DB_PATH and backed that up instead. Resolve the top-level key only, append the sibling suffix the daemon uses, and refuse the update when the backup cannot be taken. The backup is the only route back from a forward schema migration, so an update that cannot take one must not start; it now runs as pre-flight, ahead of the adapter refresh, so a refusal changes nothing on the host. The tests drive the real script with a config file the harness controls; all three fail against the previous adapter.
This commit is contained in:
1 parent
176533d181
commit
dad9389530
2 files changed
+150
-10
No files matched your search
@@ -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 '<path>'" 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
|
||||
// `<db_path>-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")
|
||||
}
|
||||
@@ -43,9 +43,22 @@ configured_agent_image() {
|
||||
printf '%s' "$image"
|
||||
}
|
||||
|
||||
# The task store is `<db_path>-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=""
|
||||
|
||||
Reference in new issue
Block a user