feat(update): let every host binary report its version, and refuse skew

The updater is component-driven: it compares the daemon and agent versions and
acts only when they differ, so a host whose individual binaries disagree looks
healthy. Carina ran a hand-installed archie-messaging 1.35.0 against a 1.30.0
state store and crash-looped on an RPC the older store does not serve, while
the updater reported "no approved component changes; nothing to install".

The reason a comparison was impossible: three of the six host binaries carried
no version at all. archie-ui, archie-messaging and archie-playbooks did not link
internal/app/archied, so the release ldflags never reached them; the gateway and
state store linked the variable but exposed no flag. Only archied could say what
it was.

- internal/buildinfo carries Version, stamped by all three build sites: the
  zip, install.sh, and the updater itself.
- Every host binary answers -version ("<name> <version>"). archied's existing
  two-line output is unchanged, because something parses it.
- archie-update-install reads each installed binary before it can report
  "nothing to install" and refuses, naming every binary that disagrees with the
  daemon's version; after an install it verifies the same set against the
  release it just installed, so a binary the release did not rebuild is skew
  rather than success.
- TestDistZipShipsEveryHostCommand pins stamping across the three build sites
  and requires each cmd/ to report, so a seventh command cannot arrive unstamped.
This commit is contained in:
sam committed 2026-09-22 14:52:18 +10:00
1 parent 3902b7ecbc
commit 70ea9b2a44
10 files changed
+197 -7

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
// Package buildinfo carries the release version stamped into every host
// binary. The updater asks each installed binary what it is, rather than
// trusting one component's word for a whole install whose binaries may have
// been replaced by hand (archie-core-k94o).
package buildinfo
import (
"flag"
"fmt"
"os"
)
// Version and Runtime are stamped per release with
// -X github.com/samcharles93/archie-core/internal/buildinfo.Version=<version>.
// The unstamped value is "dev", which is itself informative: a binary built
// outside a release says so rather than guessing.
var (
Version = "dev"
Runtime = "dev"
)
// Print writes the machine-readable "name version" line the updater reads.
func Print(name string) {
fmt.Printf("%s %s\n", name, Version)
}
// RegisterVersionFlag adds -version to the standard flag set. Call the returned
// function after flag.Parse to print and exit when it was set.
func RegisterVersionFlag(name string) func() {
show := flag.Bool("version", false, "print the version and exit")
return func() {
if *show {
Print(name)
os.Exit(0)
}
}
}
+79 -6
View File
@@ -348,6 +348,14 @@ func runUpdateInstallWithConfig(t *testing.T, environment map[string]string, con
// 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()
return runUpdateInstallFull(t, environment, configBody, nil, edit, wantErr)
}
// runUpdateInstallFull is runUpdateInstallEdited with control over the versions
// already installed in the bin directory, so a skewed host can be built on
// purpose (archie-core-k94o).
func runUpdateInstallFull(t *testing.T, environment map[string]string, configBody string, installed map[string]string, edit func(string) string, wantErr bool) (Result, []string, string) {
t.Helper()
ctx := t.Context()
@@ -366,10 +374,16 @@ func runUpdateInstallEdited(t *testing.T, environment map[string]string, configB
if err := os.WriteFile(configPath, []byte(configBody), 0o600); err != nil {
t.Fatal(err)
}
for _, name := range []string{"archied", "archie-agent"} {
if err := os.WriteFile(filepath.Join(binDir, name), []byte("old "+name), 0o755); err != nil {
t.Fatal(err)
}
previousVersion := environment["ARCHIE_UPDATE_DAEMON_PREVIOUS"]
if previousVersion == "" {
previousVersion = "dev"
}
writeFakeBinary(t, binDir, "archied", previousVersion)
if err := os.WriteFile(filepath.Join(binDir, "archie-agent"), []byte("old archie-agent"), 0o755); err != nil {
t.Fatal(err)
}
for name, version := range installed {
writeFakeBinary(t, binDir, name, version)
}
writeFakeCommand(t, fakeDir, "git", `
printf '%s\n' "git $*" >> "$ARCHIE_TEST_CALLS"
@@ -399,12 +413,24 @@ if [ "$1" = "run" ]; then
exit 0
fi
out=""
name=""
version="dev"
while [ "$#" -gt 0 ]; do
if [ "$1" = "-o" ]; then out="$2"; break; fi
if [ "$1" = "-o" ]; then out="$2"; fi
case "$1" in
./cmd/*) name="${1#./cmd/}" ;;
*buildinfo.Version=*)
version="${1##*buildinfo.Version=}"
version="${version%% *}"
;;
esac
shift
done
mkdir -p "$(dirname "$out")"
printf 'built\n' > "$out"
# The installed binary must answer -version the way a real one does, or the
# updater's skew check cannot read it.
printf '#!/usr/bin/env bash\necho "%s %s"\n' "$name" "$version" > "$out"
chmod 755 "$out"
`)
writeFakeCommand(t, fakeDir, "docker", `
printf '%s\n' "docker $*" >> "$ARCHIE_TEST_CALLS"
@@ -522,6 +548,14 @@ fi
return result, calls, string(output)
}
func writeFakeBinary(t *testing.T, dir, name, version string) {
t.Helper()
body := "#!/usr/bin/env bash\necho " + strconv.Quote(name+" "+version) + "\n"
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o755); err != nil {
t.Fatal(err)
}
}
func writeFakeCommand(t *testing.T, dir, name, body string) {
t.Helper()
if err := os.MkdirAll(dir, 0o755); err != nil {
@@ -720,6 +754,45 @@ func TestUpdateInstallRefusesWhenTheServiceListOmitsAHostUnit(t *testing.T) {
assertCallAbsent(t, calls, "install -m755")
}
// Carina's actual pair (archie-core-k94o): archie-messaging 1.35.0 beside an
// archie-state-store 1.30.0, the daemon at 1.30.0, and nothing approved to
// install. The component-driven updater reported only "nothing to install"
// while the two disagreed; now it reads each installed binary and names them.
func TestUpdateInstallRefusesWhenInstalledBinariesDisagree(t *testing.T) {
_, calls, output := runUpdateInstallFull(t, map[string]string{
"ARCHIE_UPDATE_DAEMON_PREVIOUS": "1.30.0",
"ARCHIE_UPDATE_AGENT_PREVIOUS": "1.30.0",
}, "[containers]\nimage = 'registry.example/archie-agent:stable'\n", map[string]string{
"archie-messaging": "1.35.0",
"archie-state-store": "1.30.0",
}, nil, true)
if !strings.Contains(output, "archie-messaging=1.35.0") {
t.Errorf("refusal must name the skewed binary and its version; output = %q", output)
}
if strings.Contains(output, "archie-state-store=") {
t.Errorf("refusal named a binary that agrees; output = %q", output)
}
assertCallAbsent(t, calls, "go build")
assertCallAbsent(t, calls, "install -m755")
t.Logf("refusal output:\n%s", strings.TrimSpace(output))
}
// A binary built outside a release reports "dev"; on a released host that is
// skew too, and it must be named rather than read as agreement.
func TestUpdateInstallRefusesAnUnstampedBinary(t *testing.T) {
_, _, output := runUpdateInstallFull(t, map[string]string{
"ARCHIE_UPDATE_DAEMON_PREVIOUS": "1.30.0",
"ARCHIE_UPDATE_AGENT_PREVIOUS": "1.30.0",
}, "[containers]\nimage = 'registry.example/archie-agent:stable'\n", map[string]string{
"archie-ui": "dev",
}, nil, true)
if !strings.Contains(output, "archie-ui=dev") {
t.Errorf("refusal must name the unstamped binary; output = %q", output)
}
}
// 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).