inbuxa upgrade: what is here, to this installer's releases #21

Merged
jcoffey-dev merged 1 commits from feat/upgrade into main 2026-10-12 19:56:09 +00:00
9 changed files with 852 additions and 5 deletions

No files matched your search

+20 -2
View File
@@ -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.<version>`),
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 `<dir>/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=<commit> e2e/vm/run.sh e2e/cases/upgrade-volumes.sh
+116
View File
@@ -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 {
+34
View File
@@ -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)
}
}
+66
View File
@@ -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 ]
+43
View File
@@ -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 ]
+50
View File
@@ -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 ]
+27 -3
View File
@@ -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())
+378
View File
@@ -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.<version>;
// - 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
// <dir>/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(`<meta name="api-base-url" content="([^"]*)"`).FindStringSubmatch(string(b))
if m == nil || m[1] == "" {
return fmt.Errorf("%s names no server to keep pointing at", index)
}
server := m[1]
return replaceDir(c.dir(), func() error {
if err := c.Install(ctx, server); err != nil {
return err
}
_, err := os.Stat(index)
return err
}, func() {}, log)
}
func upgradeWebmailHost(ctx context.Context, dir string, log Log) error {
w := hostWebmail{deploy: dir, log: log}
envBefore, err := os.ReadFile(w.envPath())
if err != nil {
return err
}
return replaceDir(w.dir(), func() error {
if err := w.Install(ctx); err != nil {
return err
}
// What it reports itself as follows the files.
env := regexp.MustCompile(`(?m)^IHASMAIL_VERSION=.*$`).ReplaceAllString(string(envBefore), "IHASMAIL_VERSION="+w.Version())
if err := os.WriteFile(w.envPath(), []byte(env), 0o600); err != nil {
return err
}
return w.Start(ctx)
}, func() {
// The old release, with the environment it ran with.
os.WriteFile(w.envPath(), envBefore, 0o600)
w.Start(ctx)
}, log)
}
// answers is whether url answers 200 with want in its body.
func answers(ctx context.Context, url, want string) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
resp, err := (&http.Client{}).Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
if resp.StatusCode != http.StatusOK || !strings.Contains(string(body), want) {
return fmt.Errorf("%s answered %s without %q", url, resp.Status, want)
}
return nil
}
+118
View File
@@ -0,0 +1,118 @@
// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
// SPDX-License-Identifier: AGPL-3.0-or-later
package apply
import (
"errors"
"io"
"os"
"path/filepath"
"strings"
"testing"
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/plan"
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/release"
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/state"
)
const composeFile = `name: inbuxa
services:
server:
image: registry.coffeylabs.org/inbuxa/inbuxa-server:2026.10.12
console:
image: registry.coffeylabs.org/inbuxa/inbuxa-admin:2026.10.5
webmail:
image: registry.coffeylabs.org/inbuxa/inbuxa-webmail:2026.10.9-g62936ab
caddy:
image: docker.io/library/caddy:2-alpine
`
func TestUpgradePlanReadsTheMachine(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "compose.yaml"), []byte(composeFile), 0o640)
st := &state.State{Dir: dir, Shapes: map[string]string{"server": "container", "console": "container", "webmail": "container"}}
steps := UpgradePlan(st)
if len(steps) != 3 {
t.Fatalf("steps = %+v", steps)
}
want := map[plan.Component]string{plan.Server: "2026.10.12", plan.Console: "2026.10.5", plan.Webmail: "2026.10.9-g62936ab"}
for _, s := range steps {
if s.From != want[s.Component] || s.To != target(s.Component, plan.Container) || !s.Due() {
t.Errorf("%+v", s)
}
}
}
func TestHostVersionsComeFromTheirFiles(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(filepath.Join(dir, "webmail"), 0o755)
os.WriteFile(filepath.Join(dir, "webmail", "VERSION"), []byte(release.WebmailVersion[:10]+"+g6e1a1cb\n"), 0o644)
if got := installedVersion(dir, plan.Webmail, plan.Host, ""); got != release.WebmailVersion[:10]+"-g6e1a1cb" {
t.Errorf("webmail = %q", got)
}
os.MkdirAll(filepath.Join(dir, "console"), 0o755)
os.WriteFile(filepath.Join(dir, "console", releaseMarker), []byte(release.ConsoleSHA256+"\n"), 0o644)
if got := installedVersion(dir, plan.Console, plan.Host, ""); got != release.ConsoleVersion {
t.Errorf("console with the pinned checksum = %q", got)
}
os.WriteFile(filepath.Join(dir, "console", releaseMarker), []byte("abc 2026.10.5\n"), 0o644)
if got := installedVersion(dir, plan.Console, plan.Host, ""); got != "2026.10.5" {
t.Errorf("console from an earlier release = %q", got)
}
}
func TestSetTagChangesOneServiceOnly(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "compose.yaml")
os.WriteFile(path, []byte(composeFile), 0o640)
old, err := setTag(dir, plan.Console, "2026.10.12.2")
if err != nil || old != "2026.10.5" {
t.Fatalf("old = %q, %v", old, err)
}
b, _ := os.ReadFile(path)
got := string(b)
if !strings.Contains(got, "inbuxa-admin:2026.10.12.2\n") || !strings.Contains(got, "inbuxa-server:2026.10.12\n") ||
!strings.Contains(got, "caddy:2-alpine") {
t.Errorf("compose.yaml:\n%s", got)
}
if _, err := setTag(dir, plan.Webmail, old); err != nil {
t.Fatal(err)
}
}
func TestReplaceDirPutsTheOldOneBack(t *testing.T) {
dir := t.TempDir()
dest := filepath.Join(dir, "console")
os.MkdirAll(dest, 0o755)
os.WriteFile(filepath.Join(dest, "old"), []byte("1"), 0o644)
undone := false
err := replaceDir(dest, func() error {
os.MkdirAll(dest, 0o755)
os.WriteFile(filepath.Join(dest, "new"), []byte("2"), 0o644)
return errors.New("did not answer")
}, func() { undone = true }, &nopLog{})
if err == nil || !undone {
t.Fatalf("err = %v, undone = %v", err, undone)
}
if _, err := os.Stat(filepath.Join(dest, "old")); err != nil {
t.Error("the old release was not put back")
}
if _, err := os.Stat(filepath.Join(dest, "new")); err == nil {
t.Error("the failed release was left in place")
}
// ...and a good one replaces it, leaving no .prev
if err := replaceDir(dest, func() error { return os.MkdirAll(dest, 0o755) }, func() {}, &nopLog{}); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(dest + ".prev"); err == nil {
t.Error("the previous release was kept after a good upgrade")
}
}
type nopLog struct{}
func (nopLog) Step(string, ...any) {}
func (nopLog) Info(string, ...any) {}
func (nopLog) Out() io.Writer { return io.Discard }