Keep the server's data on its named volumes, and move older installs there #7

Merged
jcoffey-dev merged 1 commits from fix/server-named-volumes into main 2026-10-05 06:13:05 +00:00
6 changed files with 229 additions and 3 deletions

No files matched your search

+2
View File
@@ -85,6 +85,8 @@ is tested on a throwaway virtual machine rather than on anybody's desk:
e2e/vm/run.sh e2e/cases/install-public.sh the same with real ports and certificates
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
an install from before #5, upgraded
e2e/vm/down.sh remove it
Each case starts from a copy of the machine taken when it was new, so a run
+84
View File
@@ -0,0 +1,84 @@
#!/bin/bash
# SPDX-FileCopyrightText: 2026 Coffey Labs
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# An install made before issue #5 keeps the server's configuration and mail
# in anonymous volumes, because its compose file mounted the named ones where
# the server never looks. This installs with that installer, upgrades with
# this one, and proves nothing was lost: on the upgrade, after a `down` and
# `up` (which used to start the server empty), and on a run after that.
#
# Run from the host with:
# OLD_REF=2fce971 e2e/vm/run.sh e2e/cases/upgrade-volumes.sh
# where OLD_REF is any commit from before the fix.
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; }
compose() {
if [ "$(rt)" = docker ]; then
docker compose -f "$DIR/compose.yaml" "$@"
else
DOCKER_HOST=unix:///run/podman/podman.sock \
/usr/local/lib/docker/cli-plugins/docker-compose -f "$DIR/compose.yaml" "$@"
fi
}
mounts() { "$(rt)" inspect --format '{{range .Mounts}}{{.Name}} {{.Destination}}{{println}}{{end}}' "$(compose ps -q server)"; }
wait_live() {
for _ in $(seq 1 60); do curl -fsS -o /dev/null http://127.0.0.1:8081/healthz/live 2>/dev/null && return 0; sleep 2; done
return 1
}
# Whether the server still knows the first mailbox and its password: what
# the server's own sign-in page does, as the webmail's registered client.
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\":\"$(head -c 32 /dev/urandom | base64 | tr '+/' '-_' | tr -d '=')\",\"codeChallengeMethod\":\"S256\"}" \
| grep -q '"type":"authenticated"'
}
[ -x /tmp/inbuxa-old ] || { echo "no /tmp/inbuxa-old: run with OLD_REF=<commit before the fix>" >&2; exit 2; }
echo "==> installing with the installer from before the fix"
OUT="$(/tmp/inbuxa-old install --local --domain example.test --install-deps --yes 2>&1)"; rc=$?
[ $rc -eq 0 ] && ok "installed (exit 0)" || { bad "exit $rc"; echo "$OUT" | grep -v '^ |' | tail -15 | sed 's/^/ /'; }
signs_in && ok "the first mailbox signs in" || bad "the first mailbox does not sign in"
M="$(mounts)"
grep -q "inbuxa_inbuxa-data /opt/stalwart/data" <<<"$M" && ok "it has the old layout: the named volume is at /opt/stalwart/data" \
|| { bad "not the old layout; this case proves nothing on it"; echo "$M" | sed 's/^/ /'; }
OLD_DATA=$(awk '$2 == "/var/lib/inbuxa" {print $1}' <<<"$M")
echo
echo "==> upgrading with this installer"
OUT="$(/tmp/inbuxa install --local --domain example.test --yes 2>&1)"; rc=$?
[ $rc -eq 0 ] && ok "upgraded (exit 0)" || { bad "exit $rc"; echo "$OUT" | grep -v '^ |' | tail -15 | sed 's/^/ /'; }
grep -q "moving the mail server's data onto its named volumes" <<<"$OUT" && ok "it moved the data" || bad "it did not say it moved the data"
grep -q "came back with its configuration" <<<"$OUT" && ok "and checked the server came back configured" || bad "no check that the server came back configured"
M="$(mounts)"
grep -q "inbuxa_inbuxa-etc /etc/inbuxa" <<<"$M" && grep -q "inbuxa_inbuxa-data /var/lib/inbuxa" <<<"$M" \
&& ok "the server's data is on its named volumes" || { bad "the mounts are not the named volumes"; echo "$M" | sed 's/^/ /'; }
signs_in && ok "the first mailbox still signs in" || bad "the first mailbox no longer signs in"
"$(rt)" volume inspect "$OLD_DATA" >/dev/null 2>&1 && ok "the old volume is kept, for undoing it" || bad "the old volume $OLD_DATA is gone"
echo
echo "==> down and up, which used to start the server empty"
compose down >/dev/null 2>&1 && compose up -d >/dev/null 2>&1 && wait_live && ok "the stack came back" || bad "the stack did not come back"
compose logs server 2>&1 | grep -q "bootstrap mode" && bad "the server came back in bootstrap mode" || ok "not in bootstrap mode"
signs_in && ok "the first mailbox still signs in" || bad "the first mailbox no longer signs in"
echo
echo "==> another run has nothing to move"
OUT="$(/tmp/inbuxa install --local --domain example.test --yes 2>&1)"; rc=$?
[ $rc -eq 0 ] && ok "ran again (exit 0)" || bad "exit $rc"
grep -q "moving the mail server's data" <<<"$OUT" && bad "it moved the data again" || ok "it moved nothing"
signs_in && ok "the first mailbox still signs in" || bad "the first mailbox no longer signs in"
echo
echo "==> $pass passed, $fail failed"
[ "$fail" -eq 0 ]
+12 -1
View File
@@ -27,10 +27,21 @@ vm_running || { echo "the lab is not up -- run e2e/vm/up.sh" >&2; exit 1; }
echo "==> building the installer"
(cd "$ROOT" && GOOS=linux GOARCH=amd64 go build -o "$LAB/inbuxa" ./cmd/inbuxa)
# An upgrade case needs the installer as it was, too: OLD_REF builds that
# commit as /tmp/inbuxa-old.
if [ -n "${OLD_REF:-}" ]; then
echo "==> building the installer at $OLD_REF"
OLD_SRC="$(mktemp -d)"
(cd "$ROOT" && git archive "$OLD_REF" | tar -x -C "$OLD_SRC")
(cd "$OLD_SRC" && GOOS=linux GOARCH=amd64 go build -o "$LAB/inbuxa-old" ./cmd/inbuxa)
rm -rf "$OLD_SRC"
fi
echo "==> copying it in"
vm_scp "$LAB/inbuxa" [email protected]:/tmp/inbuxa >/dev/null
[ -z "${OLD_REF:-}" ] || vm_scp "$LAB/inbuxa-old" [email protected]:/tmp/inbuxa-old >/dev/null
vm_scp "$ROOT/$CASE" [email protected]:/tmp/case.sh >/dev/null
vm_ssh 'chmod +x /tmp/inbuxa /tmp/case.sh'
vm_ssh 'chmod +x /tmp/inbuxa /tmp/case.sh; [ ! -f /tmp/inbuxa-old ] || chmod +x /tmp/inbuxa-old'
echo "==> running $(basename "$CASE")"
vm_ssh 'sudo -E bash /tmp/case.sh'
+22
View File
@@ -458,10 +458,32 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock
}
}
// Before anything is recreated: an install from before #5 keeps the
// server's data where the corrected compose file no longer mounts it.
moved, err := adoptServerVolumes(ctx, cmp, stack.Project, log)
if err != nil {
return res, err
}
log.Step("bringing the stack in line with the file")
if err := cmp.Run(ctx, "up", "-d", "--remove-orphans"); err != nil {
return res, withLogs(ctx, err, cmp, "server")
}
if moved {
// A server that finds no configuration starts in bootstrap mode and
// says so. That would mean the move missed something; the old
// volumes are still there to put back.
if err := waitFor(ctx, log, "the server to come back on its named volumes", 120*time.Second, func(ctx context.Context) error {
return live(ctx, "http://"+stack.ServerBind)
}); err != nil {
return res, withLogs(ctx, err, cmp, "server")
}
if strings.Contains(cmp.Logs(ctx, "server", 400), "bootstrap mode") {
return res, fmt.Errorf("the server came back in bootstrap mode after its data was moved; " +
"its previous volumes are untouched, see `docker volume ls` and issue #5")
}
log.Info("the server came back with its configuration")
}
// The server reads the front-end URLs from its environment, so a front
// end that was added or removed changes what it was started with. Only
+105
View File
@@ -0,0 +1,105 @@
// SPDX-FileCopyrightText: 2026 Coffey Labs
// SPDX-License-Identifier: AGPL-3.0-or-later
package apply
// Installs made before issue #5 was fixed mounted the server's named volumes
// at /opt/stalwart/etc and /opt/stalwart/data, which the server never reads.
// Its configuration and mail went into the anonymous volumes its image
// declares at /etc/inbuxa and /var/lib/inbuxa instead. Recreating the server
// with the corrected mounts would put the empty named volumes over those
// paths and bring it back in bootstrap mode, so the data is moved first.
import (
"context"
"encoding/json"
"fmt"
"strings"
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/docker"
)
// serverVolumes is where each of the server's named volumes belongs.
var serverVolumes = map[string]string{
"/etc/inbuxa": "inbuxa-etc",
"/var/lib/inbuxa": "inbuxa-data",
}
type volumeMove struct {
dest, from, to string
}
// serverVolumeMoves reads the server container's mounts and returns the data
// that sits in a volume other than the named one meant for its path. None
// means the server is already on its named volumes, or there is no server.
func serverVolumeMoves(ctx context.Context, cmp docker.Compose, project string) (moves []volumeMove, image string, err error) {
id, err := cmp.Output(ctx, "--project-directory", cmp.Dir, "-f", cmp.Dir+"/compose.yaml", "ps", "-a", "-q", "server")
if err != nil || id == "" {
return nil, "", err
}
id = strings.Fields(id)[0]
raw, err := docker.Output(ctx, "inspect", "--format", "{{json .Mounts}} {{.Config.Image}}", id)
if err != nil {
return nil, "", err
}
mountsJSON, image, _ := strings.Cut(raw, " ")
var mounts []struct {
Type, Name, Destination string
}
if err := json.Unmarshal([]byte(mountsJSON), &mounts); err != nil {
return nil, "", fmt.Errorf("reading the server's mounts: %w", err)
}
for _, m := range mounts {
named, ok := serverVolumes[m.Destination]
want := project + "_" + named
if ok && m.Type == "volume" && m.Name != want {
moves = append(moves, volumeMove{dest: m.Destination, from: m.Name, to: want})
}
}
return moves, strings.TrimSpace(image), nil
}
// adoptServerVolumes moves an older install's server data into its named
// volumes, with the server stopped. It never overwrites: a named volume that
// already holds something stops it, since that is not a state this installer
// leaves and guessing which copy is the real one could lose mail. The old
// anonymous volumes are left in place, so the move can be undone.
func adoptServerVolumes(ctx context.Context, cmp docker.Compose, project string, log Log) (moved bool, err error) {
moves, image, err := serverVolumeMoves(ctx, cmp, project)
if err != nil || len(moves) == 0 {
return false, err
}
log.Step("moving the mail server's data onto its named volumes")
if err := cmp.Run(ctx, "stop", "server"); err != nil {
return false, err
}
// The server's own image, which is already here and has a shell; run as
// root so cp -a keeps the server user's ownership.
helper := func(args ...string) (string, error) {
base := []string{"run", "--rm", "--user", "0", "--entrypoint", "sh"}
return docker.Output(ctx, append(base, args...)...)
}
for _, m := range moves {
out, err := helper("-v", m.to+":/to", image, "-c", "ls -A /to | head -n 1")
if err != nil {
return false, fmt.Errorf("looking in %s: %w", m.to, err)
}
if out != "" {
// `start`, not `up`: the stopped container still has its old
// mounts, and up would recreate it from the new file.
restart := ""
if err := cmp.Run(ctx, "start", "server"); err != nil {
restart = fmt.Sprintf(" (and starting it again failed: %v)", err)
}
return false, fmt.Errorf("the server's data for %s is in %s, but %s already holds something; "+
"not overwriting it. Nothing was moved, and the server runs as it did%s", m.dest, m.from, m.to, restart)
}
}
for _, m := range moves {
if _, err := helper("-v", m.from+":/from:ro", "-v", m.to+":/to", image, "-c", "cp -a /from/. /to/"); err != nil {
return false, fmt.Errorf("copying %s into %s: %w", m.from, m.to, err)
}
log.Info("%s: %s -> %s (the old volume is kept)", m.dest, m.from, m.to)
}
return true, nil
}
+4 -2
View File
@@ -20,8 +20,10 @@ services:
- "{{.}}:{{.}}"
{{- end}}
volumes:
- inbuxa-etc:/opt/stalwart/etc
- inbuxa-data:/opt/stalwart/data
# Where the image keeps its configuration and its data. Installs made
# before this was corrected are moved over on their next run.
- inbuxa-etc:/etc/inbuxa
- inbuxa-data:/var/lib/inbuxa
{{- if .CABundle}}
# The system roots plus the private ACME CA, so the server can reach it.
- ./ca-bundle.crt:/etc/ssl/certs/ca-certificates.crt:ro