From 3db3caa9a903f3bf8c4a2e89c00727c53ccf3b10 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Mon, 12 Oct 2026 13:53:31 -0700 Subject: [PATCH] Restart a server on the host when install changes what it starts with Running install again rewrote server.env, the binary or the unit but only restarted the server when the front ends changed shape or it was down, so a fix to its environment (the webmail's secret, 2026.10.12.4) waited for a restart by hand. Install and WriteUnit now say whether they changed anything, and converge restarts on that; with nothing changed the server is left alone. e2e/cases/rerun-host-server.sh starts from v2026.10.12.3 and checks both. --- README.md | 2 ++ e2e/cases/rerun-host-server.sh | 62 ++++++++++++++++++++++++++++++++++ internal/apply/apply.go | 33 ++++++++++-------- internal/apply/hostserver.go | 38 +++++++++++---------- 4 files changed, 104 insertions(+), 31 deletions(-) create mode 100755 e2e/cases/rerun-host-server.sh diff --git a/README.md b/README.md index 803d4e6..64f4d61 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,8 @@ is tested on a throwaway virtual machine rather than on anybody's desk: 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 + OLD_REF=v2026.10.12.3 e2e/vm/run.sh e2e/cases/rerun-host-server.sh + install run again: restarted when it must be, only then e2e/vm/run.sh e2e/cases/uninstall-containers.sh uninstalled, installed again on the data, purged e2e/vm/run.sh e2e/cases/uninstall-host.sh the same with the server and the webmail on the host e2e/vm/run.sh e2e/cases/topology.sh growing and shrinking from a file diff --git a/e2e/cases/rerun-host-server.sh b/e2e/cases/rerun-host-server.sh new file mode 100755 index 0000000..6430945 --- /dev/null +++ b/e2e/cases/rerun-host-server.sh @@ -0,0 +1,62 @@ +#!/bin/bash +# SPDX-FileCopyrightText: 2026 Coffey Labs LLC +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# Running install again over a server on the host: when what the server reads +# at start changed, it is restarted to read it; when nothing changed, it is +# left alone. Starts from v2026.10.12.3, which left a host server beside a host +# webmail without the webmail's secret. +# +# Run from the host with: +# OLD_REF=v2026.10.12.3 e2e/vm/run.sh e2e/cases/rerun-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 +D=example.test +ARGS="--domain $D --server host --webmail host --proxy snippets --install-deps --yes" +pid() { systemctl show -p MainPID --value inbuxa-server; } +# A code for the webmail's own address is given only if the server knows the +# webmail's client, which it registers from the secret in its environment. +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\":\"https://webmail.$D/api/auth/callback\",\"codeChallenge\":\"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM\",\"codeChallengeMethod\":\"S256\"}" \ + | grep -q '"client_\{0,1\}[cC]ode"' +} + +echo "==> installed by the older installer" +DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx >/dev/null 2>&1 +OUT="$(/tmp/inbuxa-old install $ARGS 2>&1)"; rc=$? +[ $rc -eq 0 ] && ok "installed (exit 0)" || { bad "old install exit $rc"; echo "$OUT" | tail -8 | sed 's/^/ /'; } +grep -q '^INBUXA_WEBMAIL_CLIENT_SECRET=' $DIR/server.env && bad "the old installer wrote the secret after all" || ok "server.env has no webmail secret" +signs_in && bad "the server signs in for the webmail already" || ok "so the server does not sign in for the webmail" +BEFORE=$(pid) + +echo +echo "==> install again, with this installer" +OUT="$(/tmp/inbuxa install $ARGS 2>&1)"; rc=$? +echo "$OUT" | grep -v '^ |' | tail -8 | sed 's/^/ /' +[ $rc -eq 0 ] && ok "re-run (exit 0)" || bad "re-run exit $rc" +grep -q '^INBUXA_WEBMAIL_CLIENT_SECRET=.' $DIR/server.env && ok "server.env has the webmail secret" || bad "still no secret" +grep -q "restarting the server" <<<"$OUT" && ok "it said it restarted the server" || bad "it did not restart the server" +[ "$(pid)" != "$BEFORE" ] && ok "the server is a new process" || bad "the server is the same process ($BEFORE)" +signs_in && ok "the server signs in for the webmail, with no restart by hand" || bad "no sign-in for the webmail" + +echo +echo "==> install again, nothing changed" +BEFORE=$(pid) +OUT="$(/tmp/inbuxa install $ARGS 2>&1)"; rc=$? +[ $rc -eq 0 ] && ok "re-run (exit 0)" || bad "re-run exit $rc" +grep -q "restarting the server" <<<"$OUT" && bad "it restarted the server for nothing" || ok "it left the server alone" +[ "$(pid)" = "$BEFORE" ] && ok "the same process ($BEFORE)" || bad "the server was restarted ($BEFORE -> $(pid))" +signs_in && ok "and it still signs in for the webmail" || bad "sign-in broke" + +echo +echo "==> $pass passed, $fail failed" +[ "$fail" -eq 0 ] diff --git a/internal/apply/apply.go b/internal/apply/apply.go index d34d118..77fcd34 100644 --- a/internal/apply/apply.go +++ b/internal/apply/apply.go @@ -221,16 +221,22 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // The server on the host: what a container would have brought with it. var hs *hostServer var srvLogs logSource = cmp + // Whether the binary or its environment changed under a server that is + // already running, which only a restart makes it see. + serverChanged := false if onHost { hs = &hostServer{deploy: dir, log: log} srvLogs = hs log.Step("installing the mail server on this machine") - if err := hs.Install(ctx); err != nil { + replaced, err := hs.Install(ctx) + if err != nil { return nil, err } - if _, err := hs.WriteEnv(serverEnv(o, stack, sec)); err != nil { + envChanged, err := hs.WriteEnv(serverEnv(o, stack, sec)) + if err != nil { return nil, err } + serverChanged = replaced || envChanged log.Info("%s, %s", hostUnitPath, hs.envPath()) } @@ -269,7 +275,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // moment it was configured -- which is exactly what adding or removing a // front end used to do, and why it could not. if existing, err := state.Load(); err == nil && existing.Installed() { - res, err := converge(ctx, o, stack, cmp, hs, existing, f, log) + res, err := converge(ctx, o, stack, cmp, hs, serverChanged, existing, f, log) if err == nil && hw != nil { // Its files may be new, or what it reads at start; or it may // simply not be running. @@ -311,7 +317,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // Its default listeners until they are fitted to the host, behind a // drop-in that keeps everything but loopback out meanwhile. serverURL = hostSetupBind - if err := hs.WriteUnit(ctx, false); err != nil { + if _, err := hs.WriteUnit(ctx, false); err != nil { return nil, err } if err := hs.Start(ctx, modeBootstrap, bootPass); err != nil { @@ -410,7 +416,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro if o.Local { log.Info("the mail ports on loopback only") } - if err := hs.WriteUnit(ctx, true); err != nil { + if _, err := hs.WriteUnit(ctx, true); err != nil { return res, err } if err := hs.Start(ctx, modeNormal, ""); err != nil { @@ -629,7 +635,7 @@ func waitForCertificate(ctx context.Context, srv *jmap.Client, log Log, domainID // Nothing touches the mail: data volumes are left alone, and a component // that is removed can be added back with what it had. func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp docker.Compose, hs *hostServer, - st *state.State, f host.Facts, log Log) (*Result, error) { + serverChanged bool, st *state.State, f host.Facts, log Log) (*Result, error) { res := &Result{ Dir: o.Dir, ConsoleURL: stack.ConsoleURL, WebmailURL: stack.WebmailURL, @@ -644,7 +650,7 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock } if hs != nil { - return convergeHost(ctx, o, stack, cmp, hs, st, f, log, res) + return convergeHost(ctx, o, stack, cmp, hs, serverChanged, st, f, log, res) } // Before anything is recreated: an install from before #5 keeps the @@ -700,14 +706,13 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock // convergeHost is converge for a server on the host: the binary, the unit // and its environment are rewritten if they changed, whatever runs in // containers beside it is brought in line, and the server is restarted only -// if what it reads at start is different. +// if what it reads at start is different. serverChanged is Run's word on the +// binary and the environment, which it wrote before getting here. func convergeHost(ctx context.Context, o plan.Options, stack compose.Stack, cmp docker.Compose, hs *hostServer, - st *state.State, f host.Facts, log Log, res *Result) (*Result, error) { + serverChanged bool, st *state.State, f host.Facts, log Log, res *Result) (*Result, error) { - // Run already wrote the environment; reading it back as it was before is - // not possible, so whether it changed is decided by the shapes, as for a - // container. - if err := hs.WriteUnit(ctx, true); err != nil { + unitChanged, err := hs.WriteUnit(ctx, true) + if err != nil { return res, err } log.Step("bringing the stack in line with the file") @@ -725,7 +730,7 @@ func convergeHost(ctx context.Context, o plan.Options, stack compose.Stack, cmp } _, active := discover.HostServer(ctx) frontEnds := st.Shapes["console"] != string(o.Shape(plan.Console)) || st.Shapes["webmail"] != string(o.Shape(plan.Webmail)) - if frontEnds || !active { + if frontEnds || serverChanged || unitChanged || !active { log.Info("restarting the server") if err := hs.Restart(ctx); err != nil { return res, withLogs(ctx, err, hs, "server") diff --git a/internal/apply/hostserver.go b/internal/apply/hostserver.go index 0624e7e..595523c 100644 --- a/internal/apply/hostserver.go +++ b/internal/apply/hostserver.go @@ -79,31 +79,35 @@ func (h hostServer) envPath() string { return filepath.Join(h.deploy, "server func (h hostServer) DataPath() string { return filepath.Join(h.home(), "data") + "/" } // Install puts the release binary, the user and the directories in place. It -// changes nothing that is already right, so a converge can call it too. -func (h hostServer) Install(ctx context.Context) error { +// changes nothing that is already right, so a converge can call it too, and +// says whether it replaced the binary, which a running server only picks up +// on a restart. +func (h hostServer) Install(ctx context.Context) (replaced bool, err error) { if path := os.Getenv("INBUXA_SERVER_BINARY"); path != "" { // A server build to test, in place of the release. Said loudly, // because nothing vouches for it the way the pinned checksum // vouches for a release. h.log.Step("installing %s instead of the release, as INBUXA_SERVER_BINARY says (no checksum)", path) if out, err := exec.CommandContext(ctx, path, "--version").CombinedOutput(); err != nil { - return fmt.Errorf("%s does not run here: %s", path, strings.TrimSpace(string(out))) + return false, fmt.Errorf("%s does not run here: %s", path, strings.TrimSpace(string(out))) } if err := runQuiet(ctx, "install", "-m", "0755", path, HostServerBinary); err != nil { - return err + return false, err } + replaced = true } else if v := binaryVersion(ctx, HostServerBinary); strings.Contains(v, release.ServerVersion) { h.log.Info("%s is already %s", HostServerBinary, release.ServerVersion) } else { if err := h.fetchBinary(ctx); err != nil { - return err + return false, err } + replaced = true } if err := exec.CommandContext(ctx, "getent", "passwd", HostServerUser).Run(); err != nil { if err := runQuiet(ctx, "useradd", "--system", "--user-group", "--no-create-home", "--home-dir", h.home(), "--shell", nologin(), HostServerUser); err != nil { - return fmt.Errorf("creating the %s user: %w", HostServerUser, err) + return false, fmt.Errorf("creating the %s user: %w", HostServerUser, err) } h.log.Info("user %s (system, no login)", HostServerUser) } @@ -112,20 +116,20 @@ func (h hostServer) Install(ctx context.Context) error { // are 0600 on their own; the server only needs to pass through it to // reach its own directory. if err := os.MkdirAll(h.deploy, 0o750); err != nil { - return err + return false, err } if err := os.Chmod(h.deploy, 0o711); err != nil { - return err + return false, err } for _, d := range []string{h.home(), h.DataPath()} { if err := os.MkdirAll(d, 0o750); err != nil { - return err + return false, err } if err := runQuiet(ctx, "chown", HostServerUser+":"+HostServerUser, d); err != nil { - return err + return false, err } } - return nil + return replaced, nil } func (h hostServer) fetchBinary(ctx context.Context) error { @@ -179,9 +183,9 @@ func (h hostServer) WriteEnv(vars map[string]string) (bool, error) { return writeIfChanged(h.envPath(), b.String(), 0o600) } -// WriteUnit writes the unit. configured adds the condition that a finished -// install keeps: no configuration file, no start. -func (h hostServer) WriteUnit(ctx context.Context, configured bool) error { +// WriteUnit writes the unit and says whether it changed. configured adds the +// condition that a finished install keeps: no configuration file, no start. +func (h hostServer) WriteUnit(ctx context.Context, configured bool) (bool, error) { cond := "" if configured { cond = "# A server that has lost its configuration stays down rather than coming\n" + @@ -220,14 +224,14 @@ WantedBy=multi-user.target `, cond, HostServerUser, HostServerUser, h.envPath(), HostServerBinary, h.configPath(), h.home()) changed, err := writeIfChanged(hostUnitPath, unit, 0o644) if err != nil { - return err + return false, err } if changed { if err := runQuiet(ctx, "systemctl", "daemon-reload"); err != nil { - return err + return false, err } } - return runQuiet(ctx, "systemctl", "enable", "--quiet", HostServerUnit) + return changed, runQuiet(ctx, "systemctl", "enable", "--quiet", HostServerUnit) } // Start (re)starts the server for a step of first boot, or as it runs from -- 2.54.0