From b2b595f496b7881130676e880d4cea7a13913abb Mon Sep 17 00:00:00 2001 From: John Coffey Date: Mon, 12 Oct 2026 12:52:22 -0700 Subject: [PATCH] inbuxa upgrade: what is here, to this installer's releases `inbuxa upgrade [server|console|webmail] [--dry-run] [--yes] [--no-backup]` moves what is installed to the releases this installer pins; a newer installer is what brings newer releases. What is installed is read from the machine: the image tags in compose.yaml, the server binary's --version, the console's release marker, the webmail's VERSION. status now shows each component's version, and what this installer would make it. One component at a time, the server first, each checked once it is running again and put back if not, which stops the upgrade: - a container by its tag in compose.yaml, and its old tag to undo it; - the server on the host keeps its old binary as inbuxa-server.; - the console and the webmail on the host keep their old directory until the new one is in and, for the webmail, answering; its environment's IHASMAIL_VERSION follows the files. Before the server, which may convert its store on first start, it is stopped and its own data archived into /backups -- the volumes with the server's own image, or /server -- unless --no-backup. A store kept elsewhere is said to be backed up where it lives. The release marker now records the version beside the checksum, and "local" for a tarball given by hand, which upgrade always replaces. Options and components can come in any order. Tests: the plan read from compose.yaml and the host's files, one image tag changed and no other, a directory put back when its replacement fails, options after components. e2e: upgrade-containers 17/17 (installed by v2026.10.12.1; archive, new images, the same mailbox signing in, a second run with nothing to do), upgrade-host-server 11/11 (a binary that won't start put back with the server answering, then the real release with the old binary kept and the data archived), upgrade-frontends-host 10/10 (older tarballs to the releases, the webmail answering as its new version, the console still pointed at the server). install-local, install-host, webmail-host, all-host and interface pass unchanged. --- README.md | 22 +- cmd/inbuxa/main.go | 116 +++++++++ cmd/inbuxa/main_test.go | 34 +++ e2e/cases/upgrade-containers.sh | 66 +++++ e2e/cases/upgrade-frontends-host.sh | 43 ++++ e2e/cases/upgrade-host-server.sh | 50 ++++ internal/apply/frontends.go | 30 ++- internal/apply/upgrade.go | 378 ++++++++++++++++++++++++++++ internal/apply/upgrade_test.go | 118 +++++++++ 9 files changed, 852 insertions(+), 5 deletions(-) create mode 100644 cmd/inbuxa/main_test.go create mode 100644 e2e/cases/upgrade-containers.sh create mode 100644 e2e/cases/upgrade-frontends-host.sh create mode 100644 e2e/cases/upgrade-host-server.sh create mode 100644 internal/apply/upgrade.go create mode 100644 internal/apply/upgrade_test.go diff --git a/README.md b/README.md index 23adedb..59ef6bc 100644 --- a/README.md +++ b/README.md @@ -110,14 +110,26 @@ This is early. What is built: diffs the file against what is actually installed here and changes nothing; `apply` converges to it, adding and removing components; `export` writes the file from what is already here. +- **`upgrade [server|console|webmail]`** -- moves what is installed here to + the releases this installer installs; a newer installer brings newer + releases. `--dry-run` shows each component's move first (so does + `status`). The server goes first, then the console, then the webmail, + each checked once it is running again and put back as it was if not, + which stops the upgrade there: a container by its old image tag, the + server on the host by its old binary (kept as `inbuxa-server.`), + the console and the webmail on the host by their old directory. Before + the server, which may convert its store on first start, it is stopped and + its own data archived into `/backups` (`--no-backup` to skip; a store + kept elsewhere, PostgreSQL or S3, is backed up where it lives). + - **`status`** -- what is installed, what is running, and where those two disagree: a container stopped by hand, or a deployment nothing recorded installing. It exits non-zero when they disagree, so a machine can be asked in a script whether it still matches itself. Not built yet: `join` in the interface (it is a command for now) and of -front ends on the host, `upgrade`, `uninstall`, and moving the server -between a container and the host. The design is in the inbuxa specification (§6.1 and +front ends on the host, `uninstall`, `certs`, and moving the server between +a container and the host. The design is in the inbuxa specification (§6.1 and the installer draft); the phases are there too. inbuxa install --local --domain example.test --install-deps --yes @@ -162,6 +174,12 @@ is tested on a throwaway virtual machine rather than on anybody's desk: e2e/vm/run.sh e2e/cases/proxy-snippets.sh behind nginx, from the configuration it writes e2e/vm/run.sh e2e/cases/webmail-host.sh the webmail on the host, and signing in to it e2e/vm/run.sh e2e/cases/all-host.sh everything on the host, behind nginx, no containers + OLD_REF=v2026.10.12.1 e2e/vm/run.sh e2e/cases/upgrade-containers.sh + an older install moved to these releases + OLD_REF=v2026.10.12.1 e2e/vm/run.sh e2e/cases/upgrade-host-server.sh + the same on the host, and a bad release put back + CONSOLE_TARBALL=… WEBMAIL_TARBALL=… e2e/vm/run.sh e2e/cases/upgrade-frontends-host.sh + the console and the webmail on the host, moved on e2e/vm/run.sh e2e/cases/topology.sh growing and shrinking from a file e2e/vm/run.sh e2e/cases/status-export.sh intent against reality, and the file OLD_REF= e2e/vm/run.sh e2e/cases/upgrade-volumes.sh diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go index 4874fcd..1cec139 100644 --- a/cmd/inbuxa/main.go +++ b/cmd/inbuxa/main.go @@ -47,6 +47,9 @@ const usage = `inbuxa -- install the inbuxa suite on this machine inbuxa plan -f FILE what a topology file would change here inbuxa apply -f FILE make this machine match that file inbuxa status what is installed here, and whether it agrees + inbuxa upgrade [component…] move what is here to the releases this + installer installs: --dry-run, --yes, + --no-backup inbuxa export [-o FILE] write a topology file from what is here inbuxa version this program's version @@ -125,6 +128,8 @@ func main() { os.Exit(exportCmd(os.Args[2:])) case "status": os.Exit(statusCmd()) + case "upgrade": + os.Exit(upgradeCmd(os.Args[2:])) case "version": fmt.Println(version) case "-h", "--help", "help": @@ -650,6 +655,106 @@ func exportCmd(args []string) int { return 0 } +// upgradeCmd moves what is installed here to the releases this installer +// pins: the server first, each checked and put back if it fails, the +// server's data archived before it unless --no-backup says not to. +func upgradeCmd(args []string) int { + fs := flag.NewFlagSet("upgrade", flag.ContinueOnError) + fs.Usage = func() { fmt.Print(usage) } + dryRun := fs.Bool("dry-run", false, "") + yes := fs.Bool("yes", false, "") + noBackup := fs.Bool("no-backup", false, "") + // Components and options in any order: `upgrade server --yes` as well as + // `upgrade --yes server`. The flag package stops at the first argument + // that isn't an option, so parse again after each one. + var names []string + for { + if err := fs.Parse(args); err != nil { + return 2 + } + if fs.NArg() == 0 { + break + } + names = append(names, fs.Arg(0)) + args = fs.Args()[1:] + } + want := map[string]bool{} + for _, a := range names { + c, err := shapeComponent(a) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + return 2 + } + want[c] = true + } + st, err := state.Load() + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + return 1 + } + if !st.Installed() { + fmt.Fprintln(os.Stderr, "nothing is installed here to upgrade") + return 1 + } + var steps []apply.Step + for _, s := range apply.UpgradePlan(st) { + if len(want) == 0 || want[string(s.Component)] { + steps = append(steps, s) + } + } + due := 0 + fmt.Printf("Upgrade with installer %s\n\n", version) + for _, s := range steps { + mark := "already " + s.To + if s.Due() { + mark = s.From + " -> " + s.To + due++ + } + fmt.Printf(" %-8s %-10s %s\n", s.Component, s.Shape, mark) + } + if due == 0 { + fmt.Println("\nNothing to do: this is what this installer installs. A newer installer brings newer releases.") + return 0 + } + backup := !*noBackup + for _, s := range steps { + if s.Component == plan.Server && s.Due() { + if backup { + fmt.Printf("\nThe server is stopped and its data archived into %s first: a new release\nmay convert its store on first start, and that can't be undone.\n", filepath.Join(st.Dir, "backups")) + } else { + fmt.Println("\nThe server's data will not be archived first (--no-backup).") + } + } + } + if *dryRun { + return 0 + } + if !*yes { + fmt.Fprintln(os.Stderr, "\nNothing has happened yet. Pass --yes to carry this out.") + return 1 + } + f := host.Survey(context.Background()) + if !f.Root { + fmt.Fprintln(os.Stderr, "upgrading needs root") + return 1 + } + fmt.Println("\nUpgrading:") + if err := apply.RunUpgrade(context.Background(), st, steps, backup, f, &printer{}); err != nil { + fmt.Fprintln(os.Stderr, "\nstopped: "+err.Error()) + return 1 + } + fmt.Println("\nDone.") + return 0 +} + +func shapeComponent(s string) (string, error) { + switch strings.ToLower(s) { + case "server", "console", "webmail": + return strings.ToLower(s), nil + } + return "", fmt.Errorf("%q is not a component: server, console or webmail", s) +} + // statusCmd is the same reading as export, shown to a person: what is // installed, what is running, and where the two disagree. func statusCmd() int { @@ -708,6 +813,17 @@ func statusCmd() int { } } + if steps := apply.UpgradePlan(st); len(steps) > 0 { + fmt.Println("\nVersions") + for _, s := range steps { + line := s.From + if s.Due() { + line += " (this installer installs " + s.To + ": inbuxa upgrade)" + } + fmt.Printf(" %-8s %s\n", s.Component, line) + } + } + if d := discover.Drift(st.Shapes, found); len(d) > 0 { fmt.Println("\nWorth a look") for _, line := range d { diff --git a/cmd/inbuxa/main_test.go b/cmd/inbuxa/main_test.go new file mode 100644 index 0000000..c29f86b --- /dev/null +++ b/cmd/inbuxa/main_test.go @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package main + +import ( + "os" + "path/filepath" + "testing" +) + +// upgrade takes its components and its options in any order; with nothing +// installed it stops at that, so what is checked is that the options were +// understood, not taken for components. +func TestUpgradeTakesOptionsAfterComponents(t *testing.T) { + if _, err := os.Stat(filepath.Join("/etc/inbuxa", "install.json")); err == nil { + t.Skip("an install is recorded on this machine") + } + for _, args := range [][]string{ + {"server", "--no-backup", "--dry-run"}, + {"--dry-run", "server", "console"}, + {"webmail", "--dry-run", "--no-backup"}, + } { + if code := upgradeCmd(args); code == 2 { + t.Errorf("upgrade %v: taken as a usage error", args) + } + } + if code := upgradeCmd([]string{"server", "--bogus"}); code != 2 { + t.Errorf("an unknown option was accepted: %d", code) + } + if code := upgradeCmd([]string{"database"}); code != 2 { + t.Errorf("an unknown component was accepted: %d", code) + } +} diff --git a/e2e/cases/upgrade-containers.sh b/e2e/cases/upgrade-containers.sh new file mode 100644 index 0000000..652d6be --- /dev/null +++ b/e2e/cases/upgrade-containers.sh @@ -0,0 +1,66 @@ +#!/bin/bash +# SPDX-FileCopyrightText: 2026 Coffey Labs LLC +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# inbuxa upgrade, everything in containers: installed by an older installer, +# moved to this one's releases, the server's data archived first, and the +# same mailbox still signing in afterwards. +# +# Run from the host with: +# OLD_REF=v2026.10.12.1 e2e/vm/run.sh e2e/cases/upgrade-containers.sh +set -uo pipefail + +pass=0; fail=0 +ok() { echo " ok $*"; pass=$((pass+1)); } +bad() { echo " FAIL $*"; fail=$((fail+1)); } +DIR=/var/lib/inbuxa +rt() { command -v docker >/dev/null 2>&1 && echo docker || echo podman; } +img() { grep -o "inbuxa-$1:[^ ]*" $DIR/compose.yaml; } +signs_in() { + local user pass + user=$(awk '/^first mailbox/ {print $3}' "$DIR/credentials.txt") + pass=$(awk '/^first mailbox/{getline; print $2}' "$DIR/credentials.txt") + curl -s -X POST http://127.0.0.1:8081/api/auth -H 'Content-Type: application/json' -H 'Accept: application/json' \ + -d "{\"type\":\"authCode\",\"accountName\":\"$user\",\"accountSecret\":\"$pass\",\"clientId\":\"ihasmail-inbuxa\",\"redirectUri\":\"http://127.0.0.1:8080/api/auth/callback\",\"codeChallenge\":\"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM\",\"codeChallengeMethod\":\"S256\"}" \ + | grep -q '"client_\{0,1\}[cC]ode"' +} + +echo "==> installed by the older installer" +OUT="$(/tmp/inbuxa-old install --local --domain example.test --install-deps --yes 2>&1)"; rc=$? +[ $rc -eq 0 ] && ok "installed with $(/tmp/inbuxa-old version)" || { bad "old install exit $rc"; echo "$OUT" | tail -8 | sed 's/^/ /'; } +BEFORE="$(img server) $(img admin) $(img webmail)" +echo " $BEFORE" +signs_in && ok "the first mailbox signs in" || bad "the first mailbox does not sign in before" + +echo +echo "==> what this installer says about it" +STATUS="$(/tmp/inbuxa status 2>&1)" +grep -q "this installer installs" <<<"$STATUS" && ok "status says newer releases are here" || { bad "status says nothing about versions"; echo "$STATUS" | sed 's/^/ /'; } +DRY="$(/tmp/inbuxa upgrade --dry-run 2>&1)"; rc=$? +[ $rc -eq 0 ] && [ "$(grep -c -- ' -> ' <<<"$DRY")" = 3 ] && ok "upgrade --dry-run shows three to move" || { bad "dry run exit $rc"; echo "$DRY" | sed 's/^/ /'; } +grep -q "archived into" <<<"$DRY" && ok "and that the server's data is archived first" || bad "no word of the backup" +[ "$BEFORE" = "$(img server) $(img admin) $(img webmail)" ] && ok "a dry run changed nothing" || bad "a dry run changed the compose file" + +echo +echo "==> upgrading" +OUT="$(/tmp/inbuxa upgrade --yes 2>&1)"; rc=$? +echo "$OUT" | grep -v '^ |' | tail -16 | sed 's/^/ /' +[ $rc -eq 0 ] && ok "upgraded (exit 0)" || bad "upgrade exit $rc" +ls $DIR/backups/server-*.tar.gz >/dev/null 2>&1 && ok "the server's data was archived: $(ls $DIR/backups)" || bad "no archive in $DIR/backups" +tar -tzf $DIR/backups/server-*.tar.gz 2>/dev/null | grep -q "config.json" && ok "with its configuration in it" || bad "the archive has no config.json" +AFTER="$(img server) $(img admin) $(img webmail)" +[ "$AFTER" != "$BEFORE" ] && ok "now: $AFTER" || bad "the images did not change" +STATE="$($(rt) compose -f $DIR/compose.yaml ps --format '{{.Service}} {{.State}}')" +for s in server console webmail; do grep -q "^$s running" <<<"$STATE" && ok "$s is running" || bad "$s is not running"; done +signs_in && ok "the same mailbox still signs in" || bad "the first mailbox no longer signs in" +curl -fsS http://127.0.0.1:8080/api/health | grep -q '"ok":true' && ok "the webmail is healthy" || bad "the webmail is not healthy" + +echo +echo "==> again" +OUT="$(/tmp/inbuxa upgrade --yes 2>&1)"; rc=$? +[ $rc -eq 0 ] && grep -q "Nothing to do" <<<"$OUT" && ok "a second upgrade has nothing to do" || bad "second upgrade: exit $rc" +/tmp/inbuxa status >/dev/null 2>&1 && ok "status: intent and reality agree" || bad "status disagrees" + +echo +echo "==> $pass passed, $fail failed" +[ "$fail" -eq 0 ] diff --git a/e2e/cases/upgrade-frontends-host.sh b/e2e/cases/upgrade-frontends-host.sh new file mode 100644 index 0000000..d8b5f00 --- /dev/null +++ b/e2e/cases/upgrade-frontends-host.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# SPDX-FileCopyrightText: 2026 Coffey Labs LLC +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# inbuxa upgrade, the console and the webmail on the host: installed from +# older tarballs, moved to this installer's releases, the webmail answering +# as the new version afterwards and the console still pointed at the server. +# +# Run from the host with older tarballs: +# CONSOLE_TARBALL=… WEBMAIL_TARBALL=… e2e/vm/run.sh e2e/cases/upgrade-frontends-host.sh +set -uo pipefail + +pass=0; fail=0 +ok() { echo " ok $*"; pass=$((pass+1)); } +bad() { echo " FAIL $*"; fail=$((fail+1)); } +DIR=/var/lib/inbuxa + +echo "==> installed from older tarballs" +OUT="$(INBUXA_CONSOLE_TARBALL=/tmp/inbuxa-console-test.tar.gz INBUXA_WEBMAIL_TARBALL=/tmp/inbuxa-webmail-test.tar.gz \ + /tmp/inbuxa install --domain example.test --console host --webmail host --proxy snippets --install-deps --yes 2>&1)"; rc=$? +[ $rc -eq 0 ] && ok "installed (exit 0)" || { bad "install exit $rc"; echo "$OUT" | tail -8 | sed 's/^/ /'; } +OLDW=$(cat $DIR/webmail/VERSION) +echo " webmail $OLDW, console $(awk '{print $2}' $DIR/console/.inbuxa-release)" + +echo +echo "==> upgrading" +DRY="$(/tmp/inbuxa upgrade --dry-run 2>&1)" +grep -q "console.*local -> " <<<"$DRY" && grep -q "webmail.*-> " <<<"$DRY" && ok "the dry run shows both to move" || { bad "dry run"; echo "$DRY" | sed 's/^/ /'; } +OUT="$(/tmp/inbuxa upgrade --yes 2>&1)"; rc=$? +echo "$OUT" | grep -v '^ |' | tail -10 | sed 's/^/ /' +[ $rc -eq 0 ] && ok "upgraded (exit 0)" || bad "upgrade exit $rc" +NEWW=$(cat $DIR/webmail/VERSION) +[ "$NEWW" != "$OLDW" ] && ok "the webmail's files are $NEWW" || bad "the webmail's files are still $OLDW" +curl -fsS http://127.0.0.1:8080/api/health | grep -q "\"version\":\"$NEWW\"" && ok "and it answers as $NEWW" || bad "it doesn't answer as $NEWW" +grep -q "^IHASMAIL_VERSION=$NEWW$" $DIR/webmail.env && ok "its environment says so" || bad "webmail.env still says $(grep IHASMAIL_VERSION $DIR/webmail.env)" +[ "$(stat -c %U $DIR/webmail/server/dist/index.js)" = root ] && ok "its files are still root's" || bad "owner changed" +grep -q 'name="api-base-url" content="https://mail.example.test"' $DIR/console/index.html && ok "the console is still pointed at the server" || bad "the console lost the server's address" +[ "$(awk '{print $2}' $DIR/console/.inbuxa-release)" != local ] && ok "the console is release $(awk '{print $2}' $DIR/console/.inbuxa-release)" || bad "the console is still the local tarball" +[ ! -e $DIR/console.prev ] && [ ! -e $DIR/webmail.prev ] && ok "no previous copies left behind" || bad "a .prev directory was left" + +echo +echo "==> $pass passed, $fail failed" +[ "$fail" -eq 0 ] diff --git a/e2e/cases/upgrade-host-server.sh b/e2e/cases/upgrade-host-server.sh new file mode 100644 index 0000000..9899988 --- /dev/null +++ b/e2e/cases/upgrade-host-server.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# SPDX-FileCopyrightText: 2026 Coffey Labs LLC +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# inbuxa upgrade, the server on the host: a binary that won't run is put back +# with the server answering as before, then the real release goes in, with +# the old binary kept beside it and the data archived first. +# +# Run from the host with: +# OLD_REF=v2026.10.12.1 e2e/vm/run.sh e2e/cases/upgrade-host-server.sh +set -uo pipefail + +pass=0; fail=0 +ok() { echo " ok $*"; pass=$((pass+1)); } +bad() { echo " FAIL $*"; fail=$((fail+1)); } +DIR=/var/lib/inbuxa +BIN=/usr/local/bin/inbuxa-server +ver() { $BIN --version 2>/dev/null | awk '{print $1}'; } +live() { curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8081/.well-known/jmap | grep -qE '^(200|401|30[0-9])$'; } + +echo "==> installed by the older installer, the server on the host" +OUT="$(/tmp/inbuxa-old install --local --domain example.test --server host --install-deps --yes 2>&1)"; rc=$? +[ $rc -eq 0 ] && ok "installed: server $(ver)" || { bad "old install exit $rc"; echo "$OUT" | tail -8 | sed 's/^/ /'; } +OLD=$(ver) + +echo +echo "==> a release that won't start is put back" +printf '#!/bin/sh\necho "inbuxa 9.9.9 (broken)"\nexit 1\n' > /tmp/broken-server; chmod +x /tmp/broken-server +OUT="$(INBUXA_SERVER_BINARY=/tmp/broken-server /tmp/inbuxa upgrade server --no-backup --yes 2>&1)"; rc=$? +[ $rc -ne 0 ] && grep -q "put back" <<<"$OUT" && ok "the upgrade stopped and said it put the server back" || { bad "exit $rc"; echo "$OUT" | tail -6 | sed 's/^/ /'; } +[ "$(ver)" = "$OLD" ] && ok "the binary is $OLD again" || bad "the binary is now $(ver)" +for _ in $(seq 1 30); do live && break; sleep 2; done +live && ok "and the server answers" || bad "the server is down after the rollback" + +echo +echo "==> the real release" +OUT="$(/tmp/inbuxa upgrade --yes 2>&1)"; rc=$? +echo "$OUT" | grep -v '^ |' | tail -10 | sed 's/^/ /' +[ $rc -eq 0 ] && ok "upgraded (exit 0)" || bad "upgrade exit $rc" +[ "$(ver)" != "$OLD" ] && ok "the server is $(ver) now" || bad "still $OLD" +[ -x $BIN.$OLD ] && ok "the old binary is kept as $BIN.$OLD" || bad "no $BIN.$OLD" +ls $DIR/backups/server-$OLD-*.tar.gz >/dev/null 2>&1 && ok "the data was archived first" || bad "no archive" +live && ok "the server answers" || bad "the server does not answer" +systemctl is-active --quiet inbuxa-server && ok "inbuxa-server is running" || bad "not running" +OUT="$(/tmp/inbuxa upgrade --yes 2>&1)" +grep -q "Nothing to do" <<<"$OUT" && ok "a second upgrade has nothing to do" || bad "a second upgrade did something" + +echo +echo "==> $pass passed, $fail failed" +[ "$fail" -eq 0 ] diff --git a/internal/apply/frontends.go b/internal/apply/frontends.go index 49cf178..2dea5a9 100644 --- a/internal/apply/frontends.go +++ b/internal/apply/frontends.go @@ -99,9 +99,33 @@ func unpack(ctx context.Context, tarball, dest, marker string) error { return os.RemoveAll(old) } +// markerFor is what the marker records: the checksum and its release, or +// "local" for a tarball given by hand, which upgrade then always replaces. +func markerFor(sum, version string) string { + if strings.HasPrefix(sum, "local:") { + return sum + " local" + } + return sum + " " + version +} + func unpacked(dest, marker string) bool { + return markerSum(dest) == marker && !strings.HasPrefix(marker, "local:") +} + +// The marker is the tarball's checksum and, after it, the release it came +// from: the checksum decides whether to unpack again, the version is what +// status and upgrade show. An install from before the version was written +// has the checksum alone. +func markerSum(dest string) string { b, err := os.ReadFile(filepath.Join(dest, releaseMarker)) - return err == nil && strings.TrimSpace(string(b)) == marker && !strings.HasPrefix(marker, "local:") + if err != nil { + return "" + } + f := strings.Fields(string(b)) + if len(f) == 0 { + return "" + } + return f[0] } // ---- the console ---- @@ -131,7 +155,7 @@ func (c hostConsole) Install(ctx context.Context, serverURL string) error { if err := os.MkdirAll(c.deploy, 0o750); err != nil { return err } - if err := unpack(ctx, path, c.dir(), mark); err != nil { + if err := unpack(ctx, path, c.dir(), markerFor(mark, release.ConsoleVersion)); err != nil { return err } } @@ -191,7 +215,7 @@ func (w hostWebmail) Install(ctx context.Context) error { if err := os.MkdirAll(w.deploy, 0o750); err != nil { return err } - if err := unpack(ctx, path, w.dir(), mark); err != nil { + if err := unpack(ctx, path, w.dir(), markerFor(mark, release.WebmailVersion)); err != nil { return err } w.log.Info("%s (owned by root: the webmail reads it and can't change it)", w.dir()) diff --git a/internal/apply/upgrade.go b/internal/apply/upgrade.go new file mode 100644 index 0000000..f46fee1 --- /dev/null +++ b/internal/apply/upgrade.go @@ -0,0 +1,378 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +// Upgrade moves what is installed here to the releases this installer pins. +// A newer installer is what brings newer releases; upgrading is running it. +// +// One component at a time, server first (the front ends follow the server's +// API, not the other way round), each checked once it is running again and +// put back as it was if the check fails, which stops the upgrade there: +// +// - a container gets its new image by its tag in the compose file the +// installer wrote, and the old tag back to undo it; +// - the server on the host keeps its old binary beside the new one, as +// /usr/local/bin/inbuxa-server.; +// - the console and the webmail on the host keep their old directory +// until the new one answers. +// +// Before the server, which may convert its store on first start and can't +// convert it back, it is stopped and its own data archived into +// /backups, unless the operator says not to. + +import ( + "context" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "regexp" + "strings" + "time" + + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/docker" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/host" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/plan" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/release" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/state" +) + +// Step is one component's move: what it is now, and what it would be. +type Step struct { + Component plan.Component + Shape plan.Shape + From, To string +} + +// Due is whether there is anything to do. +func (s Step) Due() bool { return s.From != s.To } + +// repos are the image repositories in the compose file, by component. +var repos = map[plan.Component]string{plan.Server: "inbuxa-server", plan.Console: "inbuxa-admin", plan.Webmail: "inbuxa-webmail"} + +func tagOf(image string) string { return image[strings.LastIndex(image, ":")+1:] } + +// targets are what this installer installs, by component and shape. +func target(c plan.Component, s plan.Shape) string { + switch { + case c == plan.Server && s == plan.Container: + return tagOf(DefaultServerImage) + case c == plan.Console && s == plan.Container: + return tagOf(DefaultConsoleImage) + case c == plan.Webmail && s == plan.Container: + return tagOf(DefaultWebmailImage) + case c == plan.Server: + return release.ServerVersion + case c == plan.Console: + return release.ConsoleVersion + default: + return release.WebmailVersion + } +} + +// Plan reads what each installed component is, from the machine rather +// than from notes, and pairs it with what this installer would put there. +func UpgradePlan(st *state.State) []Step { + var steps []Step + compose, _ := os.ReadFile(filepath.Join(st.Dir, "compose.yaml")) + for _, c := range []plan.Component{plan.Server, plan.Console, plan.Webmail} { + shape := plan.Shape(st.Shapes[string(c)]) + if shape != plan.Container && shape != plan.Host { + continue + } + steps = append(steps, Step{Component: c, Shape: shape, From: installedVersion(st.Dir, c, shape, string(compose)), To: target(c, shape)}) + } + return steps +} + +func installedVersion(dir string, c plan.Component, s plan.Shape, compose string) string { + if s == plan.Container { + m := regexp.MustCompile(`(?m)^\s+image:\s+\S+/` + repos[c] + `:(\S+)\s*$`).FindStringSubmatch(compose) + if m == nil { + return "unknown" + } + return m[1] + } + switch c { + case plan.Server: + if f := strings.Fields(binaryVersion(context.Background(), HostServerBinary)); len(f) > 0 { + return f[0] + } + case plan.Console: + d := filepath.Join(dir, "console") + if markerSum(d) == release.ConsoleSHA256 { + return release.ConsoleVersion + } + if b, err := os.ReadFile(filepath.Join(d, releaseMarker)); err == nil { + if f := strings.Fields(string(b)); len(f) > 1 { + return f[1] + } + return "an earlier release" + } + case plan.Webmail: + // Its own VERSION says 2026.10.12+g6e1a1cb; its release tag, and + // so the pin, the same with a hyphen. + if b, err := os.ReadFile(filepath.Join(dir, "webmail", "VERSION")); err == nil { + return strings.ReplaceAll(strings.TrimSpace(string(b)), "+", "-") + } + } + return "unknown" +} + +// RunUpgrade carries out the steps that are due, in order. +func RunUpgrade(ctx context.Context, st *state.State, steps []Step, backup bool, f host.Facts, log Log) error { + dir := st.Dir + docker.UseRuntime(f.Runtime) + cmp := docker.Compose{Dir: dir, Runtime: f.Runtime} + for _, s := range steps { + if !s.Due() { + continue + } + log.Step("%s: %s -> %s (%s)", s.Component, s.From, s.To, s.Shape) + var err error + switch { + case s.Component == plan.Server && s.Shape == plan.Container: + err = upgradeServerContainer(ctx, dir, s, cmp, backup, log) + case s.Component == plan.Server: + err = upgradeServerHost(ctx, dir, s, backup, log) + case s.Shape == plan.Container: + err = upgradeFrontEndContainer(ctx, dir, s, cmp, log) + case s.Component == plan.Console: + err = upgradeConsoleHost(ctx, dir, log) + default: + err = upgradeWebmailHost(ctx, dir, log) + } + if err != nil { + return fmt.Errorf("%s: %w", s.Component, err) + } + } + st.Installer = "server " + release.ServerVersion + if err := st.Save(); err != nil { + log.Info("could not write %s: %v", state.Path, err) + } + return nil +} + +// ---- containers ---- + +// setTag points a component's service at another tag of its image, and +// returns the tag it had. +func setTag(dir string, c plan.Component, tag string) (string, error) { + path := filepath.Join(dir, "compose.yaml") + b, err := os.ReadFile(path) + if err != nil { + return "", err + } + re := regexp.MustCompile(`(?m)^(\s+image:\s+\S+/` + repos[c] + `):(\S+)\s*$`) + m := re.FindStringSubmatch(string(b)) + if m == nil { + return "", fmt.Errorf("no %s image in %s", repos[c], path) + } + out := re.ReplaceAllString(string(b), "${1}:"+tag) + return m[2], os.WriteFile(path, []byte(out), 0o640) +} + +func service(c plan.Component) string { return string(c) } + +func upgradeServerContainer(ctx context.Context, dir string, s Step, cmp docker.Compose, backup bool, log Log) error { + if backup { + log.Info("stopping the server to archive its data") + if err := cmp.Run(ctx, "stop", "server"); err != nil { + return err + } + name, err := archiveVolumes(ctx, dir, s.From) + if err != nil { + cmp.Run(ctx, "start", "server") + return fmt.Errorf("archiving the server's data, so nothing was changed: %w", err) + } + log.Info("archived its configuration and data to %s", name) + log.Info("a store this server keeps elsewhere (PostgreSQL, S3) is backed up where it lives, not here") + } + return swapContainer(ctx, dir, s, cmp, log, func(ctx context.Context) error { + return live(ctx, "http://127.0.0.1:8081") + }) +} + +// archiveVolumes tars the server's two named volumes, read-only, with the +// server's own (old) image, which has tar: nothing else is pulled. +func archiveVolumes(ctx context.Context, dir, from string) (string, error) { + backups := filepath.Join(dir, "backups") + if err := os.MkdirAll(backups, 0o700); err != nil { + return "", err + } + name := fmt.Sprintf("server-%s-%s.tar.gz", from, time.Now().UTC().Format("20060102T150405Z")) + image := strings.TrimSuffix(DefaultServerImage, ":"+tagOf(DefaultServerImage)) + ":" + from + _, err := docker.Output(ctx, "run", "--rm", "--user", "0", "--entrypoint", "tar", + "-v", "inbuxa_inbuxa-etc:/archive/etc:ro", "-v", "inbuxa_inbuxa-data:/archive/data:ro", + "-v", backups+":/backups", image, "-czf", "/backups/"+name, "-C", "/archive", ".") + return filepath.Join(backups, name), err +} + +func upgradeFrontEndContainer(ctx context.Context, dir string, s Step, cmp docker.Compose, log Log) error { + check := func(ctx context.Context) error { return answers(ctx, "http://127.0.0.1:8082/", "api-base-url") } + if s.Component == plan.Webmail { + check = func(ctx context.Context) error { return answers(ctx, "http://127.0.0.1:8080/api/health", `"ok":true`) } + } + return swapContainer(ctx, dir, s, cmp, log, check) +} + +// swapContainer moves one service to its new tag, and back if it doesn't +// pass its check. +func swapContainer(ctx context.Context, dir string, s Step, cmp docker.Compose, log Log, check func(context.Context) error) error { + old, err := setTag(dir, s.Component, s.To) + if err != nil { + return err + } + back := func(why error) error { + log.Info("putting %s back to %s", s.Component, old) + setTag(dir, s.Component, old) + cmp.Run(ctx, "up", "-d", service(s.Component)) + return fmt.Errorf("%w; it was put back to %s", why, old) + } + if err := cmp.Run(ctx, "pull", "--quiet", service(s.Component)); err != nil { + return back(err) + } + if err := cmp.Run(ctx, "up", "-d", service(s.Component)); err != nil { + return back(err) + } + if err := waitFor(ctx, log, string(s.Component)+" "+s.To, 120*time.Second, check); err != nil { + return back(withLogs(ctx, err, cmp, service(s.Component))) + } + log.Info("%s %s answers", s.Component, s.To) + return nil +} + +// ---- on the host ---- + +func upgradeServerHost(ctx context.Context, dir string, s Step, backup bool, log Log) error { + hs := hostServer{deploy: dir, log: log} + kept := HostServerBinary + "." + s.From + if err := runQuiet(ctx, "cp", "-a", HostServerBinary, kept); err != nil { + return err + } + log.Info("kept %s, to go back to", kept) + if err := runQuiet(ctx, "systemctl", "stop", HostServerUnit); err != nil { + return err + } + if backup { + backups := filepath.Join(dir, "backups") + if err := os.MkdirAll(backups, 0o700); err != nil { + return err + } + name := filepath.Join(backups, fmt.Sprintf("server-%s-%s.tar.gz", s.From, time.Now().UTC().Format("20060102T150405Z"))) + if err := runQuiet(ctx, "tar", "-czf", name, "-C", dir, "server"); err != nil { + runQuiet(ctx, "systemctl", "start", HostServerUnit) + return fmt.Errorf("archiving the server's data, so nothing was changed: %w", err) + } + log.Info("archived its configuration and data to %s", name) + log.Info("a store this server keeps elsewhere (PostgreSQL, S3) is backed up where it lives, not here") + } + back := func(why error) error { + log.Info("putting the server back to %s", s.From) + runQuiet(ctx, "install", "-m", "0755", kept, HostServerBinary) + runQuiet(ctx, "systemctl", "restart", HostServerUnit) + return fmt.Errorf("%w; it was put back to %s", why, s.From) + } + var err error + if p := os.Getenv("INBUXA_SERVER_BINARY"); p != "" { + log.Info("installing %s instead of the release, as INBUXA_SERVER_BINARY says (no checksum)", p) + err = runQuiet(ctx, "install", "-m", "0755", p, HostServerBinary) + } else { + err = hs.fetchBinary(ctx) + } + if err != nil { + return back(err) + } + if err := runQuiet(ctx, "systemctl", "start", HostServerUnit); err != nil { + return back(err) + } + if err := waitFor(ctx, log, "the server "+s.To, 120*time.Second, func(ctx context.Context) error { + return live(ctx, "http://"+hostFinalBind) + }); err != nil { + return back(withLogs(ctx, err, hs, "server")) + } + log.Info("the server %s answers", s.To) + return nil +} + +// replaceDir puts a new release in dest, keeping the old one as dest.prev +// until install says the new one works, and putting it back if not -- then +// undo, for whatever else install changed. +func replaceDir(dest string, install func() error, undo func(), log Log) error { + prev := dest + ".prev" + os.RemoveAll(prev) + if err := os.Rename(dest, prev); err != nil { + return err + } + if err := install(); err != nil { + os.RemoveAll(dest) + os.Rename(prev, dest) + undo() + log.Info("put the previous release back in %s", dest) + return fmt.Errorf("%w; the previous release was put back", err) + } + return os.RemoveAll(prev) +} + +func upgradeConsoleHost(ctx context.Context, dir string, log Log) error { + c := hostConsole{deploy: dir, log: log} + index := filepath.Join(c.dir(), "index.html") + b, err := os.ReadFile(index) + if err != nil { + return err + } + m := regexp.MustCompile(`