Move the server between a container and the host #33
No files matched your search
@@ -152,8 +152,19 @@ This is early. What is built:
|
||||
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: 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.
|
||||
- **Moving the server** -- `install` run again with the other `--server`
|
||||
moves the mail server between a container and the host, with its
|
||||
configuration and mail. It needs an API key made in the console (`--key -`
|
||||
or `INBUXA_API_KEY`; never written down), because the server's listeners
|
||||
and its auto-ban exemptions change with the shape. Everything here stops
|
||||
for a minute or two. The data is archived into `<dir>/backups` first and
|
||||
then copied, never moved: the old copy -- the volumes, or `<dir>/server` --
|
||||
stays where it was until you delete it, and a failure on the way puts the
|
||||
server back as it was. The interface doesn't offer it yet: it has no key
|
||||
to give.
|
||||
|
||||
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
|
||||
|
||||
@@ -209,6 +220,7 @@ is tested on a throwaway virtual machine rather than on anybody's desk:
|
||||
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/certs.sh a failed order, reported, then fixed with a key
|
||||
e2e/vm/run.sh e2e/cases/move-server.sh the server into the host and back, keeping its mail
|
||||
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
|
||||
|
||||
@@ -77,6 +77,11 @@ install flags:
|
||||
are made to trust
|
||||
--install-deps install what the chosen shapes need and this
|
||||
machine lacks, rather than refusing over it
|
||||
--key KEY an API key made in the console, needed only to
|
||||
move the server between a container and the host
|
||||
(run install again with the other --server);
|
||||
"-" reads it from standard input, and
|
||||
INBUXA_API_KEY works too
|
||||
--dry-run print the plan and stop
|
||||
--yes do not ask for confirmation
|
||||
|
||||
@@ -177,6 +182,7 @@ func install(args []string) int {
|
||||
webmail = fs.String("webmail", "container", "")
|
||||
dryRun = fs.Bool("dry-run", false, "")
|
||||
yes = fs.Bool("yes", false, "")
|
||||
key = fs.String("key", os.Getenv("INBUXA_API_KEY"), "")
|
||||
)
|
||||
fs.StringVar(&o.Domain, "domain", "", "")
|
||||
fs.StringVar(&o.MailHost, "mail-host", "", "")
|
||||
@@ -212,6 +218,13 @@ func install(args []string) int {
|
||||
return 1
|
||||
}
|
||||
fmt.Print(p.String())
|
||||
moving := false
|
||||
if st, err := state.Load(); err == nil {
|
||||
if from, ok := apply.MovingServer(st, p.Options); ok {
|
||||
moving = true
|
||||
fmt.Print("\n" + apply.MoveNote(from, p.Options.Shape(plan.Server)))
|
||||
}
|
||||
}
|
||||
if *dryRun {
|
||||
return 0
|
||||
}
|
||||
@@ -219,6 +232,14 @@ func install(args []string) int {
|
||||
fmt.Fprintln(os.Stderr, "\nNothing has happened yet. Pass --yes to carry this out.")
|
||||
return 1
|
||||
}
|
||||
if moving {
|
||||
k, err := readKey(*key)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err.Error())
|
||||
return 2
|
||||
}
|
||||
p.Options.Key = k
|
||||
}
|
||||
|
||||
fmt.Println("\nApplying:")
|
||||
log := &printer{}
|
||||
@@ -522,6 +543,7 @@ func topologyCmd(args []string, doIt bool) int {
|
||||
machineName = fs.String("machine", "", "")
|
||||
yes = fs.Bool("yes", false, "")
|
||||
installDeps = fs.Bool("install-deps", false, "")
|
||||
key = fs.String("key", os.Getenv("INBUXA_API_KEY"), "")
|
||||
)
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return 2
|
||||
@@ -598,6 +620,15 @@ func topologyCmd(args []string, doIt bool) int {
|
||||
fmt.Fprintln(os.Stderr, "\ncannot apply this file here: "+err.Error())
|
||||
return 1
|
||||
}
|
||||
if from, ok := apply.MovingServer(st, p.Options); ok {
|
||||
fmt.Print("\n" + apply.MoveNote(from, p.Options.Shape(plan.Server)))
|
||||
k, err := readKey(*key)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err.Error())
|
||||
return 2
|
||||
}
|
||||
p.Options.Key = k
|
||||
}
|
||||
fmt.Println("\nApplying:")
|
||||
log := &printer{}
|
||||
res, err := apply.Run(context.Background(), p, f, log)
|
||||
@@ -1077,3 +1108,16 @@ func shape(s string) (plan.Shape, error) {
|
||||
// render is the first screen, as text: what this machine is, and what that
|
||||
// allows. The interface's first screen is the same text.
|
||||
func render(f host.Facts) string { return plan.SurveyText(f) }
|
||||
|
||||
// readKey is an API key given as --key: "-" reads it from standard input,
|
||||
// so it stays out of the shell's history.
|
||||
func readKey(key string) (string, error) {
|
||||
if key != "-" {
|
||||
return key, nil
|
||||
}
|
||||
line, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil && line == "" {
|
||||
return "", fmt.Errorf("reading the key from standard input: %w", err)
|
||||
}
|
||||
return strings.TrimSpace(line), nil
|
||||
}
|
||||
@@ -124,8 +124,10 @@ OUT="$(/tmp/inbuxa install --local --domain example.test --server host --yes 2>&
|
||||
grep -q "is already" <<<"$OUT" && ok "and keeps the binary it has" || bad "the second run fetched the binary again"
|
||||
COUNT=$(compose ps --format '{{.Service}}' | sort -u | wc -l)
|
||||
[ "$COUNT" = 2 ] && ok "still two containers beside the server" || bad "$COUNT containers after a second run"
|
||||
OUT="$(/tmp/inbuxa install --local --domain example.test --server container --yes 2>&1)"; rc=$?
|
||||
[ $rc -ne 0 ] && grep -q "not built yet" <<<"$OUT" && ok "moving the server into a container is refused, with the reason" || bad "a shape change was not refused: exit $rc"
|
||||
# The move itself is move-server.sh's; without a key it changes nothing.
|
||||
OUT="$(INBUXA_API_KEY= /tmp/inbuxa install --local --domain example.test --server container --yes 2>&1)"; rc=$?
|
||||
[ $rc -ne 0 ] && grep -q "API key is needed" <<<"$OUT" && ok "moving the server into a container without a key is refused, with the reason" || bad "a move without a key was not refused: exit $rc"
|
||||
systemctl is-active --quiet inbuxa-server && ok "and the server is still on the host" || bad "the server stopped"
|
||||
|
||||
echo
|
||||
echo "==> a server without its configuration stays down"
|
||||
|
||||
Executable
+151
@@ -0,0 +1,151 @@
|
||||
#!/bin/bash
|
||||
# SPDX-FileCopyrightText: 2026 Coffey Labs LLC
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
#
|
||||
# Moving the mail server between a container and the host by running install
|
||||
# again with the other --server: refused without a key and with a wrong one,
|
||||
# changing nothing; put back when the host install fails before the move;
|
||||
# then into the host and back into a container, each time keeping its
|
||||
# accounts and mail, its front ends working, the old copy left in place and
|
||||
# an archive taken first.
|
||||
#
|
||||
# Run from the host with: e2e/vm/run.sh e2e/cases/move-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
|
||||
SERVER=http://127.0.0.1:8081
|
||||
INSTALL=(/tmp/inbuxa install --local --domain example.test --install-deps)
|
||||
|
||||
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
|
||||
}
|
||||
jmap() { # jmap TOKEN-or-user:pass CAPABILITY JSON-CALLS
|
||||
local auth
|
||||
case "$1" in *:*) auth=(-u "$1") ;; *) auth=(-H "Authorization: Bearer $1") ;; esac
|
||||
curl -s "${auth[@]}" -H 'Content-Type: application/json' \
|
||||
-d "{\"using\":[\"urn:ietf:params:jmap:core\",\"$2\"],\"methodCalls\":$3}" "$SERVER/jmap/"
|
||||
}
|
||||
live() { [ "$(curl -s -o /dev/null -w '%{http_code}' $SERVER/.well-known/jmap)" != 000 ]; }
|
||||
wait_server() { for _ in $(seq 1 60); do live && return 0; sleep 2; done; return 1; }
|
||||
in_container() { compose ps --status running --services 2>/dev/null | grep -qx server; }
|
||||
on_host() { systemctl is-active --quiet inbuxa-server; }
|
||||
|
||||
# The server keeps what it had: the account made before the move is still
|
||||
# there, the first mailbox still signs in over IMAP, and the webmail and the
|
||||
# console still answer.
|
||||
kept() {
|
||||
local where=$1
|
||||
wait_server && ok "$where: the server answers on $SERVER" || bad "$where: the server does not answer"
|
||||
local got
|
||||
got=$(jmap "$KEY" urn:inbuxa:jmap:registry '[["x:Account/get",{"ids":null,"properties":["name"]},"0"]]')
|
||||
grep -q '"name":"movetest"' <<<"$got" && ok "$where: the account made before the move is there" || { bad "$where: movetest is gone"; head -c 300 <<<"$got" | sed 's/^/ /'; }
|
||||
# A --local container install publishes no mail ports, so IMAP is asked
|
||||
# from inside the server's own network there; on the host it is on loopback.
|
||||
local imap=(curl -sk --max-time 10 "imaps://127.0.0.1:993/" -u "$FIRST:$FIRSTPASS")
|
||||
if in_container; then
|
||||
imap=($(rt) run --rm --network "container:$(compose ps -q server)" docker.io/curlimages/curl:8.11.1 "${imap[@]:1}")
|
||||
fi
|
||||
"${imap[@]}" 2>/dev/null | grep -q INBOX \
|
||||
&& ok "$where: $FIRST signs in over IMAP and sees its INBOX" || bad "$where: IMAP sign-in for $FIRST failed"
|
||||
curl -s --max-time 10 http://127.0.0.1:8080/api/health | grep -q '"ok":true' \
|
||||
&& ok "$where: the webmail answers" || bad "$where: the webmail does not answer"
|
||||
curl -s --max-time 10 http://127.0.0.1:8082/ | grep -q api-base-url \
|
||||
&& ok "$where: the console answers" || bad "$where: the console does not answer"
|
||||
/tmp/inbuxa status >/dev/null 2>&1 && ok "$where: status agrees with what runs" || bad "$where: status disagrees: $(/tmp/inbuxa status 2>&1 | tail -3)"
|
||||
}
|
||||
|
||||
echo "==> all three in containers"
|
||||
OUT="$("${INSTALL[@]}" --yes 2>&1)"; rc=$?
|
||||
[ $rc -eq 0 ] && ok "installed" || { bad "install exit $rc"; echo "$OUT" | tail -12 | sed 's/^/ /'; exit 1; }
|
||||
FIRST=$(awk '/^first mailbox/ {print $3}' "$DIR/credentials.txt")
|
||||
FIRSTPASS=$(awk '/^first mailbox/{getline; print $2}' "$DIR/credentials.txt")
|
||||
[ -n "$FIRST" ] && [ -n "$FIRSTPASS" ] || bad "no first mailbox in credentials.txt"
|
||||
|
||||
echo
|
||||
echo "==> an API key, as an administrator makes one in the console"
|
||||
# The console is a browser; here the server takes the administrator's
|
||||
# password for one restart, the way the installer's own setup does.
|
||||
AUSER=$(awk '/^administrator/ {print $2}' "$DIR/credentials.txt")
|
||||
APASS=$(awk '/^administrator/{getline; print $2}' "$DIR/credentials.txt")
|
||||
printf 'services:\n server:\n environment:\n INBUXA_HTTP_BASIC_AUTH: all\n' > /tmp/basic.yaml
|
||||
compose -f /tmp/basic.yaml up -d server >/dev/null 2>&1; wait_server
|
||||
EXPIRES=$(date -u -d '+1 hour' +%Y-%m-%dT%H:%M:%SZ)
|
||||
ANSWER=$(jmap "$AUSER:$APASS" urn:inbuxa:jmap:registry "[[\"x:ApiKey/set\",{\"create\":{\"k\":{\"description\":\"move test\",\"expiresAt\":\"$EXPIRES\",\"permissions\":{\"@type\":\"Inherit\"}}}},\"0\"]]")
|
||||
KEY=$(sed -n 's/.*"secret":"\([^"]*\)".*/\1/p' <<<"$ANSWER")
|
||||
compose up -d server >/dev/null 2>&1; wait_server
|
||||
[ -n "$KEY" ] && ok "an API key for the administrator" || { bad "no API key"; head -c 300 <<<"$ANSWER" | sed 's/^/ /'; exit 1; }
|
||||
DOMAIN_ID=$(jmap "$KEY" urn:inbuxa:jmap:registry '[["x:Domain/query",{},"0"]]' | sed -n 's/.*"ids":\["\([^"]*\)".*/\1/p')
|
||||
MADE=$(jmap "$KEY" urn:inbuxa:jmap:registry "[[\"x:Account/set\",{\"create\":{\"u\":{\"@type\":\"User\",\"name\":\"movetest\",\"domainId\":\"$DOMAIN_ID\",\"description\":null,\"credentials\":{\"0\":{\"@type\":\"Password\",\"secret\":\"move-test-pass-4410\"}},\"roles\":{\"@type\":\"User\"},\"permissions\":{\"@type\":\"Inherit\"},\"quotas\":{},\"aliases\":{},\"memberGroupIds\":{},\"encryptionAtRest\":{\"@type\":\"Disabled\"}}}},\"0\"]]")
|
||||
grep -q '"created"' <<<"$MADE" && ok "an account to look for after the move" || { bad "no movetest account"; head -c 300 <<<"$MADE" | sed 's/^/ /'; }
|
||||
kept "in a container"
|
||||
|
||||
echo
|
||||
echo "==> asked to move, without a key or with a wrong one: nothing changes"
|
||||
OUT="$("${INSTALL[@]}" --server host --dry-run 2>&1)"
|
||||
grep -q "Moving the mail server from a container install to a host install" <<<"$OUT" \
|
||||
&& ok "the plan says it is a move" || bad "the plan says nothing of a move"
|
||||
OUT="$(INBUXA_API_KEY= "${INSTALL[@]}" --server host --yes 2>&1)"; rc=$?
|
||||
[ $rc -ne 0 ] && grep -q "API key is needed" <<<"$OUT" && ok "without a key it stops and says one is needed" || { bad "no key: exit $rc"; echo "$OUT" | tail -4 | sed 's/^/ /'; }
|
||||
in_container && ! on_host && live && ok "the server is still in its container" || bad "the server moved or stopped without a key"
|
||||
OUT="$(INBUXA_API_KEY=wrong-key "${INSTALL[@]}" --server host --yes 2>&1)"; rc=$?
|
||||
[ $rc -ne 0 ] && grep -q "didn't take the API key" <<<"$OUT" && ok "a wrong key is refused, and says so" || { bad "wrong key: exit $rc"; echo "$OUT" | tail -4 | sed 's/^/ /'; }
|
||||
in_container && ! on_host && live && ok "the server is still in its container" || bad "the server moved or stopped with a wrong key"
|
||||
|
||||
echo
|
||||
echo "==> a host install that fails before the move puts the container back"
|
||||
printf '#!/bin/sh\necho "inbuxa 9.9.9 (broken)"\nexit 1\n' > /tmp/broken-server; chmod +x /tmp/broken-server
|
||||
OUT="$(echo "$KEY" | INBUXA_SERVER_BINARY=/tmp/broken-server "${INSTALL[@]}" --server host --key - --yes 2>&1)"; rc=$?
|
||||
[ $rc -ne 0 ] && grep -q "putting it back in its container" <<<"$OUT" && ok "it stopped and put the server back" || { bad "broken binary: exit $rc"; echo "$OUT" | tail -6 | sed 's/^/ /'; }
|
||||
in_container && ! on_host && ok "the server is in its container again" || bad "not back in its container"
|
||||
grep -q '"server": "container"' /etc/inbuxa/install.json && ok "the install record still says container" || bad "the record changed: $(grep -o '"server": "[a-z]*"' /etc/inbuxa/install.json)"
|
||||
kept "after the failed move"
|
||||
|
||||
echo
|
||||
echo "==> into the host"
|
||||
OUT="$(echo "$KEY" | "${INSTALL[@]}" --server host --key - --yes 2>&1)"; rc=$?
|
||||
echo "$OUT" | grep -v '^ |' | sed -n '/moving the mail server/,$p' | head -24 | sed 's/^/ /'
|
||||
[ $rc -eq 0 ] && ok "moved (exit 0)" || { bad "move exit $rc"; journalctl -u inbuxa-server -n 30 --no-pager -o cat | sed 's/^/ | /'; }
|
||||
on_host && ok "inbuxa-server is running on the host" || bad "inbuxa-server is not running"
|
||||
in_container && bad "a server container still runs" || ok "no server container runs"
|
||||
grep -q '"path":"/var/lib/inbuxa/server/data/"' $DIR/server/config.json && ok "its configuration points at $DIR/server/data/" || bad "config.json: $(cat $DIR/server/config.json)"
|
||||
[ "$(stat -c %U $DIR/server/data)" = inbuxa ] && ok "its data belongs to the inbuxa user" || bad "data owned by $(stat -c %U $DIR/server/data)"
|
||||
ls $DIR/backups/server-*.tar.gz >/dev/null 2>&1 && ok "the volumes were archived first" || bad "no archive"
|
||||
$(rt) volume inspect inbuxa_inbuxa-data >/dev/null 2>&1 && ok "the volumes are left in place" || bad "the volumes are gone"
|
||||
[ ! -e /run/systemd/system/inbuxa-server.service.d/inbuxa-setup.conf ] && ok "no loopback fence is left behind" || bad "the setup drop-in is still there"
|
||||
ss -ltn | grep -q '0.0.0.0:8080 \|\[::\]:8080 ' && bad "the server's HTTP is on every interface" || ok "the server's HTTP is not on every interface"
|
||||
grep -qF "$KEY" <<<"$(cat $DIR/* $DIR/server/*.json /etc/inbuxa/install.json 2>/dev/null)" && bad "the API key was written down" || ok "the API key was not kept"
|
||||
grep -q '"server": "host"' /etc/inbuxa/install.json && ok "the install record says host" || bad "the record: $(grep -o '"server": "[a-z]*"' /etc/inbuxa/install.json)"
|
||||
kept "on the host"
|
||||
OUT="$("${INSTALL[@]}" --server host --yes 2>&1)"; rc=$?
|
||||
[ $rc -eq 0 ] && ! grep -q "moving the mail server" <<<"$OUT" && ok "running it again is a converge, not another move" || bad "a second run: exit $rc"
|
||||
|
||||
echo
|
||||
echo "==> back into a container"
|
||||
OUT="$(echo "$KEY" | "${INSTALL[@]}" --server container --key - --yes 2>&1)"; rc=$?
|
||||
echo "$OUT" | grep -v '^ |' | sed -n '/moving the mail server/,$p' | head -24 | sed 's/^/ /'
|
||||
[ $rc -eq 0 ] && ok "moved back (exit 0)" || { bad "move back exit $rc"; compose logs --tail 30 server 2>&1 | sed 's/^/ | /'; }
|
||||
in_container && ok "the server runs in its container" || bad "no server container"
|
||||
on_host && bad "inbuxa-server still runs on the host" || ok "inbuxa-server is not running on the host"
|
||||
[ ! -e /etc/systemd/system/inbuxa-server.service ] && [ ! -e /usr/local/bin/inbuxa-server ] \
|
||||
&& ok "the unit and the binary are gone" || bad "the host install's unit or binary is left"
|
||||
CFG=$($(rt) run --rm --entrypoint cat -v inbuxa_inbuxa-etc:/etc/inbuxa "$(compose config --images | grep inbuxa-server)" /etc/inbuxa/config.json 2>/dev/null)
|
||||
grep -q '"path":"/var/lib/inbuxa/"' <<<"$CFG" && ok "its configuration points at /var/lib/inbuxa/ again" || bad "config.json in the volume: $CFG"
|
||||
ls $DIR/backups/server-host-*.tar.gz >/dev/null 2>&1 && ok "the host copy was archived first" || bad "no archive of the host copy"
|
||||
grep -q "earlier install" <<<"$OUT" && ok "the volumes' older data was archived, not mixed in" || bad "nothing said about the volumes' older data"
|
||||
[ -d $DIR/server/data ] && ok "$DIR/server is left in place" || bad "$DIR/server is gone"
|
||||
grep -q '"server": "container"' /etc/inbuxa/install.json && ok "the install record says container" || bad "the record: $(grep -o '"server": "[a-z]*"' /etc/inbuxa/install.json)"
|
||||
kept "back in a container"
|
||||
|
||||
echo
|
||||
echo "==> $pass passed, $fail failed"
|
||||
[ "$fail" -eq 0 ]
|
||||
+23
-3
@@ -83,9 +83,9 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
|
||||
return nil, fmt.Errorf("this build installs the front ends only alongside a server it installs too; use join once that is built")
|
||||
}
|
||||
onHost := o.Shape(plan.Server) == plan.Host
|
||||
if st, err := state.Load(); err == nil && st.Installed() && st.Shapes["server"] != "" && st.Shapes["server"] != string(o.Shape(plan.Server)) {
|
||||
return nil, fmt.Errorf("the server is installed here as a %s install; moving it to a %s install is not built yet",
|
||||
st.Shapes["server"], o.Shape(plan.Server))
|
||||
var moveFrom plan.Shape
|
||||
if st, err := state.Load(); err == nil {
|
||||
moveFrom, _ = MovingServer(st, o)
|
||||
}
|
||||
needContainers := !onHost || o.Shape(plan.Console) == plan.Container ||
|
||||
o.Shape(plan.Webmail) == plan.Container || (!o.Local && o.Proxy == "caddy")
|
||||
@@ -108,6 +108,19 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro
|
||||
}
|
||||
}
|
||||
|
||||
// A move stops the old shape and archives it before anything of the
|
||||
// new one is written; it is carried out once that is in place.
|
||||
var mv *serverMove
|
||||
if moveFrom != "" {
|
||||
var err error
|
||||
if mv, err = beginMove(ctx, o, moveFrom, f, log); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Whatever stops this run before the move is carried out puts the
|
||||
// old shape back; finish does its own putting back.
|
||||
defer mv.abort(ctx)
|
||||
}
|
||||
|
||||
dir := o.Dir
|
||||
stack := compose.Stack{
|
||||
Version: "dev",
|
||||
@@ -275,6 +288,13 @@ 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() {
|
||||
if mv != nil {
|
||||
if err := mv.finish(ctx, stack, !o.Local && o.Proxy == "snippets"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// What it reads at start has been read: nothing more to restart for.
|
||||
serverChanged = false
|
||||
}
|
||||
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
|
||||
|
||||
@@ -61,6 +61,7 @@ const (
|
||||
modeBootstrap setupMode = iota // the bootstrap credential, loopback only
|
||||
modeBasic // configured, passwords on /jmap, loopback only
|
||||
modeNormal // as it will run from now on
|
||||
modeFenced // as it is, loopback only: a server moved here, until its listeners are fitted
|
||||
)
|
||||
|
||||
// hostServer is the server as a host install, rooted in the deployment
|
||||
@@ -243,8 +244,11 @@ func (h hostServer) Start(ctx context.Context, mode setupMode, bootPass string)
|
||||
os.Remove(setupEnvPath)
|
||||
default:
|
||||
env := "INBUXA_HTTP_BASIC_AUTH=all\n"
|
||||
if mode == modeBootstrap {
|
||||
switch mode {
|
||||
case modeBootstrap:
|
||||
env = "INBUXA_RECOVERY_ADMIN=admin:" + bootPass + "\n"
|
||||
case modeFenced:
|
||||
env = ""
|
||||
}
|
||||
if err := os.WriteFile(setupEnvPath, []byte(env), 0o600); err != nil {
|
||||
return err
|
||||
@@ -252,7 +256,7 @@ func (h hostServer) Start(ctx context.Context, mode setupMode, bootPass string)
|
||||
if err := os.MkdirAll(filepath.Dir(setupDropIn), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
dropIn := "# The inbuxa installer, during first boot only. Gone at the next reboot.\n" +
|
||||
dropIn := "# The inbuxa installer, during first boot or a move only. Gone at the next reboot.\n" +
|
||||
"[Service]\n" +
|
||||
"EnvironmentFile=" + setupEnvPath + "\n" +
|
||||
"IPAddressAllow=localhost\n" +
|
||||
|
||||
@@ -0,0 +1,594 @@
|
||||
// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
package apply
|
||||
|
||||
// Moving the mail server between a container and the host, when `install`
|
||||
// is run again with the other --server.
|
||||
//
|
||||
// Its configuration and mail go with it. Only one thing in them names a
|
||||
// place on disk: config.json, the data store's own description, whose path
|
||||
// is /var/lib/inbuxa/ in a container and <dir>/server/data/ on the host.
|
||||
// That is rewritten in the copy. The rest of what differs between the two
|
||||
// shapes is the server's own settings, which only the server can change, so
|
||||
// a move needs an API key, as `certs` and `join` do:
|
||||
//
|
||||
// - its listeners: everything on [::] inside a container, plain HTTP on
|
||||
// loopback and no HTTPS of its own on the host (jmap.HostListeners);
|
||||
// - the addresses exempted from the auto-ban, which are where the front
|
||||
// ends' requests arrive from: the project network's in a container,
|
||||
// loopback on the host.
|
||||
//
|
||||
// Nothing is moved, only copied, and the original is left where it was: the
|
||||
// volumes after a move to the host, <dir>/server after a move into a
|
||||
// container. A failure on the way puts the server back as it was, and the
|
||||
// archive taken first is in <dir>/backups either way.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/compose"
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/docker"
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/host"
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/jmap"
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/plan"
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/state"
|
||||
)
|
||||
|
||||
const (
|
||||
containerDataPath = "/var/lib/inbuxa/"
|
||||
containerUser = "2000:2000" // the image's inbuxa user
|
||||
preMoveDir = ".pre-move"
|
||||
)
|
||||
|
||||
// ErrMoveNeedsKey is a move asked for without an API key.
|
||||
var ErrMoveNeedsKey = errors.New("an API key is needed to move the server")
|
||||
|
||||
// MovingServer says whether running install with these options moves the
|
||||
// server between shapes, and from which.
|
||||
func MovingServer(st *state.State, o plan.Options) (from plan.Shape, moving bool) {
|
||||
// Not after an uninstall either: nothing runs to move, and install
|
||||
// brings the recorded shape back, or sets up the new one, as it is told.
|
||||
if st == nil || !st.Installed() || st.Removed() {
|
||||
return "", false
|
||||
}
|
||||
was, wants := st.Shapes["server"], o.Shape(plan.Server)
|
||||
if was == "" || wants == plan.Skip || was == string(wants) {
|
||||
return "", false
|
||||
}
|
||||
return plan.Shape(was), true
|
||||
}
|
||||
|
||||
// MoveNote is what a plan says about a move before anything happens.
|
||||
func MoveNote(from, to plan.Shape) string {
|
||||
return fmt.Sprintf(`Moving the mail server from a %s install to a %s install:
|
||||
- everything here stops while it moves, a minute or two
|
||||
- its configuration and mail are archived into backups/ first, then copied
|
||||
- its listeners and the auto-ban exemptions are changed for the new shape,
|
||||
with an API key made in the console (--key - or INBUXA_API_KEY)
|
||||
- the %s copy is left where it is; anything failing puts it back as it was
|
||||
`, from, to, from)
|
||||
}
|
||||
|
||||
// serverMove is a move begun: the old shape stopped and archived, waiting
|
||||
// for the new shape's files to be written before finish carries it out.
|
||||
type serverMove struct {
|
||||
from, to plan.Shape
|
||||
dir string
|
||||
key string
|
||||
local bool
|
||||
cmp docker.Compose
|
||||
log Log
|
||||
archive string // the archive taken before anything changed
|
||||
oldImage string // the server image a container install ran
|
||||
done bool // finish ran: the move happened, or it put things back itself
|
||||
}
|
||||
|
||||
// beginMove checks the key, stops what runs here and archives the server's
|
||||
// data. Nothing of the new shape is written yet, so a failure here leaves
|
||||
// the old one exactly as it was.
|
||||
func beginMove(ctx context.Context, o plan.Options, from plan.Shape, f host.Facts, log Log) (*serverMove, error) {
|
||||
to := o.Shape(plan.Server)
|
||||
if o.Key == "" {
|
||||
return nil, fmt.Errorf("%w from a %s install to a %s install, as its listeners change with it: "+
|
||||
"make one in the console (your account, API keys) and run this again with --key - "+
|
||||
"(or INBUXA_API_KEY); it isn't kept", ErrMoveNeedsKey, from, to)
|
||||
}
|
||||
docker.UseRuntime(f.Runtime)
|
||||
m := &serverMove{from: from, to: to, dir: o.Dir, key: o.Key, local: o.Local, log: log,
|
||||
cmp: docker.Compose{Dir: o.Dir, Runtime: f.Runtime}}
|
||||
srv := &jmap.Client{BaseURL: "http://" + hostFinalBind, Token: o.Key}
|
||||
if _, err := srv.Listeners(ctx); err != nil {
|
||||
return nil, fmt.Errorf("the server on %s didn't take the API key, so nothing was changed: %w", hostFinalBind, err)
|
||||
}
|
||||
|
||||
log.Step("moving the mail server from a %s install to a %s install", from, to)
|
||||
stamp := time.Now().UTC().Format("20060102T150405Z")
|
||||
if err := os.MkdirAll(filepath.Join(m.dir, "backups"), 0o700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hasCompose := exists(filepath.Join(m.dir, "compose.yaml"))
|
||||
|
||||
if from == plan.Container {
|
||||
b, err := os.ReadFile(filepath.Join(m.dir, "compose.yaml"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tag := installedVersion(m.dir, plan.Server, plan.Container, string(b))
|
||||
m.oldImage = strings.TrimSuffix(DefaultServerImage, ":"+tagOf(DefaultServerImage)) + ":" + tag
|
||||
// The deployment as it is, to put back: the new shape's files are
|
||||
// written over these before the move is carried out.
|
||||
if err := m.keepDeployment(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Info("stopping everything in containers here while the server moves")
|
||||
if err := m.cmp.Run(ctx, "stop"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name, err := archiveVolumes(ctx, m.dir, tag)
|
||||
if err != nil {
|
||||
m.cmp.Run(ctx, "start")
|
||||
return nil, fmt.Errorf("archiving the server's data, so nothing was changed: %w", err)
|
||||
}
|
||||
m.archive = name
|
||||
log.Info("archived its configuration and data to %s", name)
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// From the host: archived while stopped, then started once more, as it
|
||||
// was, to be given the listeners a container needs. They take effect at
|
||||
// its next start, which is in the container.
|
||||
if hasCompose {
|
||||
if err := m.keepDeployment(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := runQuiet(ctx, "systemctl", "stop", HostServerUnit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.archive = filepath.Join(m.dir, "backups", "server-host-"+stamp+".tar.gz")
|
||||
if err := runQuiet(ctx, "tar", "-czf", m.archive, "-C", m.dir, "server"); err != nil {
|
||||
runQuiet(ctx, "systemctl", "start", HostServerUnit)
|
||||
return nil, fmt.Errorf("archiving the server's data, so nothing was changed: %w", err)
|
||||
}
|
||||
log.Info("archived its configuration and data to %s", m.archive)
|
||||
if err := runQuiet(ctx, "systemctl", "start", HostServerUnit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := waitFor(ctx, log, "the server to answer again", 120*time.Second, func(ctx context.Context) error {
|
||||
return live(ctx, "http://"+hostFinalBind)
|
||||
}); err != nil {
|
||||
return nil, withLogs(ctx, err, hostServer{deploy: m.dir}, "server")
|
||||
}
|
||||
if err := srv.ContainerListeners(ctx); err != nil {
|
||||
return nil, m.restoreHost(ctx, fmt.Errorf("changing the server's listeners for a container: %w", err))
|
||||
}
|
||||
log.Info("its listeners set for a container, from its next start")
|
||||
log.Info("stopping everything here while the server moves")
|
||||
if err := runQuiet(ctx, "systemctl", "disable", "--now", "--quiet", HostServerUnit); err != nil {
|
||||
return nil, m.restoreHost(ctx, err)
|
||||
}
|
||||
if hasCompose {
|
||||
m.cmp.Run(ctx, "stop")
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// abort puts the old shape back when the install stops between beginMove and
|
||||
// finish, writing or fetching the new shape's pieces.
|
||||
func (m *serverMove) abort(ctx context.Context) {
|
||||
if m.done {
|
||||
return
|
||||
}
|
||||
m.done = true
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
if m.from == plan.Container {
|
||||
m.log.Info("the move stopped before the server moved: putting it back in its container")
|
||||
removeUnit(ctx, HostServerUnit, hostUnitPath)
|
||||
m.putDeploymentBack(ctx)
|
||||
return
|
||||
}
|
||||
m.log.Info("the move stopped before the server moved: putting it back on the host")
|
||||
m.restoreHost(ctx, errors.New("aborted"))
|
||||
m.putDeploymentBack(ctx)
|
||||
}
|
||||
|
||||
// keepDeployment copies the files the new shape is about to be written
|
||||
// over, so a failed move can put them back.
|
||||
func (m *serverMove) keepDeployment() error {
|
||||
keep := filepath.Join(m.dir, preMoveDir)
|
||||
os.RemoveAll(keep)
|
||||
if err := os.MkdirAll(keep, 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range []string{"compose.yaml", "Caddyfile"} {
|
||||
b, err := os.ReadFile(filepath.Join(m.dir, name))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(keep, name), b, 0o640); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// putDeploymentBack restores what keepDeployment kept, and starts it.
|
||||
func (m *serverMove) putDeploymentBack(ctx context.Context) {
|
||||
keep := filepath.Join(m.dir, preMoveDir)
|
||||
for _, name := range []string{"compose.yaml", "Caddyfile"} {
|
||||
if b, err := os.ReadFile(filepath.Join(keep, name)); err == nil {
|
||||
os.WriteFile(filepath.Join(m.dir, name), b, 0o640)
|
||||
} else {
|
||||
os.Remove(filepath.Join(m.dir, name))
|
||||
}
|
||||
}
|
||||
if exists(filepath.Join(m.dir, "compose.yaml")) {
|
||||
m.cmp.Run(ctx, "up", "-d", "--remove-orphans")
|
||||
}
|
||||
}
|
||||
|
||||
// finish carries the move out once the new shape's deployment is written
|
||||
// and, for the host, its binary, user, directories and environment are in
|
||||
// place. The server answers in its new shape when it returns nil.
|
||||
func (m *serverMove) finish(ctx context.Context, stack compose.Stack, snippets bool) error {
|
||||
m.done = true
|
||||
var err error
|
||||
if m.to == plan.Host {
|
||||
err = m.toHost(ctx, stack, snippets)
|
||||
} else {
|
||||
err = m.toContainer(ctx, stack, snippets)
|
||||
}
|
||||
if err == nil {
|
||||
os.RemoveAll(filepath.Join(m.dir, preMoveDir))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *serverMove) toHost(ctx context.Context, stack compose.Stack, snippets bool) error {
|
||||
hs := hostServer{deploy: m.dir, log: m.log}
|
||||
back := func(why error) error {
|
||||
m.log.Info("putting the server back in its container")
|
||||
removeUnit(ctx, HostServerUnit, hostUnitPath)
|
||||
os.Remove(setupDropIn)
|
||||
os.Remove(setupEnvPath)
|
||||
m.putDeploymentBack(ctx)
|
||||
return fmt.Errorf("%w; the server is back in its container with its data as it was (archived in %s)", why, m.archive)
|
||||
}
|
||||
|
||||
m.log.Info("copying its configuration and mail into %s", hs.home())
|
||||
if err := m.clearForCopy(ctx, hs); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
stage, err := os.MkdirTemp(m.dir, ".move-")
|
||||
if err != nil {
|
||||
return back(err)
|
||||
}
|
||||
defer os.RemoveAll(stage)
|
||||
out := exec.CommandContext(ctx, docker.CLI, "run", "--rm", "--user", "0", "--entrypoint", "tar",
|
||||
"-v", "inbuxa_inbuxa-etc:/from/etc:ro", "-v", "inbuxa_inbuxa-data:/from/data:ro",
|
||||
m.oldImage, "-cf", "-", "-C", "/from", ".")
|
||||
in := exec.CommandContext(ctx, "tar", "-xf", "-", "-C", stage)
|
||||
if err := pipe(out, in); err != nil {
|
||||
return back(fmt.Errorf("copying the server's volumes: %w", err))
|
||||
}
|
||||
// Renamed into place, not copied: the stage is on the same filesystem,
|
||||
// and a large store should not need twice its size free.
|
||||
if err := moveContents(filepath.Join(stage, "data"), hs.DataPath()); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
if err := moveContents(filepath.Join(stage, "etc"), hs.home()); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
if err := rewriteDataPath(hs.configPath(), containerDataPath, hs.DataPath()); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
if err := runQuiet(ctx, "chown", "-R", HostServerUser+":"+HostServerUser, hs.home()); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
|
||||
// The first start is behind the same fence as first boot: its listeners
|
||||
// are still a container's, on every interface, until they are fitted.
|
||||
m.log.Info("starting it on the host, on loopback only until its listeners are fitted")
|
||||
if _, err := hs.WriteUnit(ctx, true); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
if err := hs.Start(ctx, modeFenced, ""); err != nil {
|
||||
return back(withLogs(ctx, err, hs, "server"))
|
||||
}
|
||||
if err := m.cameBack(ctx, hostSetupBind, func() string { return hs.Logs(ctx, "server", 400) }); err != nil {
|
||||
return back(withLogs(ctx, err, hs, "server"))
|
||||
}
|
||||
srv := &jmap.Client{BaseURL: hostSetupBind, Token: m.key}
|
||||
if err := srv.HostListeners(ctx, hostFinalBind, m.local); err != nil {
|
||||
return back(fmt.Errorf("fitting the server's listeners to the host: %w", err))
|
||||
}
|
||||
m.log.Info("plain HTTP on %s for the proxy and the webmail; 443 is left to the proxy", hostFinalBind)
|
||||
if stack.ServesWebmail() || stack.Proxy || snippets {
|
||||
if err := allowOnce(ctx, srv, "127.0.0.1", "the front ends on this machine: their requests arrive from loopback"); err != nil {
|
||||
return back(fmt.Errorf("exempting the front ends: %w", err))
|
||||
}
|
||||
}
|
||||
if err := hs.Start(ctx, modeNormal, ""); err != nil {
|
||||
return back(withLogs(ctx, err, hs, "server"))
|
||||
}
|
||||
if err := m.cameBack(ctx, "http://"+hostFinalBind, func() string { return hs.Logs(ctx, "server", 400) }); err != nil {
|
||||
return back(withLogs(ctx, err, hs, "server"))
|
||||
}
|
||||
m.log.Info("the server answers on the host; its volumes are left as they were, for `docker volume rm` once you are sure")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *serverMove) toContainer(ctx context.Context, stack compose.Stack, snippets bool) error {
|
||||
hs := hostServer{deploy: m.dir, log: m.log}
|
||||
back := func(why error) error {
|
||||
m.log.Info("putting the server back on the host")
|
||||
m.cmp.Run(ctx, "rm", "--stop", "--force", "server")
|
||||
m.restoreHost(ctx, why)
|
||||
m.putDeploymentBack(ctx)
|
||||
return fmt.Errorf("%w; the server is back on the host with its data as it was (archived in %s)", why, m.archive)
|
||||
}
|
||||
|
||||
// Its volumes, made by compose so they belong to the project. Volumes
|
||||
// left from an earlier install in a container hold older data: archived
|
||||
// and emptied, never mixed with this.
|
||||
if err := m.cmp.Run(ctx, "create", "server"); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
image := DefaultServerImage
|
||||
left, _ := docker.Output(ctx, "run", "--rm", "--user", "0", "--entrypoint", "sh",
|
||||
"-v", "inbuxa_inbuxa-etc:/to/etc", "-v", "inbuxa_inbuxa-data:/to/data", image,
|
||||
"-c", "ls -A /to/etc /to/data | grep -v '^/to\\|^$' | head -1")
|
||||
if strings.TrimSpace(left) != "" {
|
||||
name, err := archiveVolumes(ctx, m.dir, tagOf(image))
|
||||
if err != nil {
|
||||
return back(fmt.Errorf("archiving the volumes left from an earlier install: %w", err))
|
||||
}
|
||||
m.log.Info("the server's volumes held an earlier install's data: archived to %s and emptied", name)
|
||||
if _, err := docker.Output(ctx, "run", "--rm", "--user", "0", "--entrypoint", "sh",
|
||||
"-v", "inbuxa_inbuxa-etc:/to/etc", "-v", "inbuxa_inbuxa-data:/to/data", image,
|
||||
"-c", "find /to/etc /to/data -mindepth 1 -delete"); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Only the configuration is staged, to be rewritten; the store goes
|
||||
// straight from <dir>/server into its volume.
|
||||
m.log.Info("copying its configuration and mail into the volumes")
|
||||
stage, err := os.MkdirTemp(m.dir, ".move-")
|
||||
if err != nil {
|
||||
return back(err)
|
||||
}
|
||||
defer os.RemoveAll(stage)
|
||||
if err := os.MkdirAll(filepath.Join(stage, "etc"), 0o750); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
staged := filepath.Join(stage, "etc", "config.json")
|
||||
if err := copyFile(hs.configPath(), staged, 0o640); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
if err := rewriteDataPath(staged, hs.DataPath(), containerDataPath); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
out := exec.CommandContext(ctx, "tar", "-cf", "-", "-C", stage, "etc", "-C", hs.home(), "data")
|
||||
in := exec.CommandContext(ctx, docker.CLI, "run", "--rm", "-i", "--user", "0", "--entrypoint", "sh",
|
||||
"-v", "inbuxa_inbuxa-etc:/to/etc", "-v", "inbuxa_inbuxa-data:/to/data", image,
|
||||
"-c", "tar -xf - -C /to && chown -R "+containerUser+" /to/etc /to/data")
|
||||
if err := pipe(out, in); err != nil {
|
||||
return back(fmt.Errorf("filling the server's volumes: %w", err))
|
||||
}
|
||||
|
||||
m.log.Info("starting it in its container")
|
||||
if err := m.cmp.Run(ctx, "up", "-d", "server"); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
url := "http://" + stack.ServerBind
|
||||
if err := m.cameBack(ctx, url, func() string { return m.cmp.Logs(ctx, "server", 400) }); err != nil {
|
||||
return back(withLogs(ctx, err, m.cmp, "server"))
|
||||
}
|
||||
|
||||
// The front ends' requests now arrive from the project network.
|
||||
srv := &jmap.Client{BaseURL: url, Token: m.key}
|
||||
exempt := map[string]string{}
|
||||
if stack.Webmail || stack.WebmailUnit {
|
||||
exempt[stack.WebmailIP] = "the webmail: every request arrives from this address"
|
||||
}
|
||||
if stack.Proxy {
|
||||
exempt[stack.CaddyIP] = "the proxy: certificate renewals and autoconfig arrive from this address"
|
||||
}
|
||||
if snippets {
|
||||
exempt[stack.GatewayIP] = "your web server in front of the suite: its requests arrive from this address"
|
||||
}
|
||||
for addr, why := range exempt {
|
||||
if addr == "" {
|
||||
continue
|
||||
}
|
||||
if err := allowOnce(ctx, srv, addr, why); err != nil {
|
||||
return back(fmt.Errorf("exempting %s: %w", addr, err))
|
||||
}
|
||||
}
|
||||
if len(exempt) > 0 {
|
||||
// They take effect only on a restart.
|
||||
if err := m.cmp.Run(ctx, "restart", "server"); err != nil {
|
||||
return back(err)
|
||||
}
|
||||
if err := m.cameBack(ctx, url, func() string { return m.cmp.Logs(ctx, "server", 400) }); err != nil {
|
||||
return back(withLogs(ctx, err, m.cmp, "server"))
|
||||
}
|
||||
}
|
||||
|
||||
// Off the host: the unit and the binaries go, as uninstall takes them.
|
||||
removeUnit(ctx, HostServerUnit, hostUnitPath)
|
||||
old, _ := filepath.Glob(HostServerBinary + ".*")
|
||||
for _, p := range append(old, HostServerBinary) {
|
||||
os.Remove(p)
|
||||
}
|
||||
os.Remove(filepath.Join(m.dir, "server.env"))
|
||||
m.log.Info("the server answers in its container; %s is left as it was, to delete once you are sure", hs.home())
|
||||
return nil
|
||||
}
|
||||
|
||||
// restoreHost puts the archive taken by beginMove back into <dir>/server and
|
||||
// starts the server on the host again, as it was before the move began.
|
||||
func (m *serverMove) restoreHost(ctx context.Context, why error) error {
|
||||
home := hostServer{deploy: m.dir}.home()
|
||||
os.RemoveAll(home)
|
||||
if err := runQuiet(ctx, "tar", "-xzf", m.archive, "-C", m.dir); err != nil {
|
||||
return fmt.Errorf("%w; and putting %s back from %s failed too: %v", why, home, m.archive, err)
|
||||
}
|
||||
runQuiet(ctx, "systemctl", "enable", "--quiet", HostServerUnit)
|
||||
runQuiet(ctx, "systemctl", "restart", HostServerUnit)
|
||||
return fmt.Errorf("%w; the server was put back on the host as it was", why)
|
||||
}
|
||||
|
||||
// clearForCopy makes sure <dir>/server is empty before the volumes are
|
||||
// copied into it: what an earlier install on the host left there is
|
||||
// archived, never mixed with this.
|
||||
func (m *serverMove) clearForCopy(ctx context.Context, hs hostServer) error {
|
||||
entries, _ := os.ReadDir(hs.DataPath())
|
||||
if len(entries) == 0 && !exists(hs.configPath()) {
|
||||
return nil
|
||||
}
|
||||
name := filepath.Join(m.dir, "backups", "server-host-earlier-"+time.Now().UTC().Format("20060102T150405Z")+".tar.gz")
|
||||
if err := runQuiet(ctx, "tar", "-czf", name, "-C", m.dir, "server"); err != nil {
|
||||
return fmt.Errorf("archiving what an earlier install left in %s: %w", hs.home(), err)
|
||||
}
|
||||
m.log.Info("%s held an earlier install's data: archived to %s and emptied", hs.home(), name)
|
||||
os.Remove(hs.configPath())
|
||||
for _, e := range entries {
|
||||
if err := os.RemoveAll(filepath.Join(hs.DataPath(), e.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// cameBack waits for the server at url and makes sure it came back with its
|
||||
// configuration: a server that finds none starts in bootstrap mode and says
|
||||
// so, which means the copy missed something.
|
||||
func (m *serverMove) cameBack(ctx context.Context, url string, logs func() string) error {
|
||||
if err := waitFor(ctx, m.log, "the server to answer", 120*time.Second, func(ctx context.Context) error {
|
||||
return live(ctx, url)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.Contains(logs(), "bootstrap mode") {
|
||||
return errors.New("the server came back in bootstrap mode, without its configuration")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// allowOnce exempts an address unless it already is.
|
||||
func allowOnce(ctx context.Context, srv *jmap.Client, address, reason string) error {
|
||||
have, err := srv.AllowedIPs(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if have[address] {
|
||||
return nil
|
||||
}
|
||||
return srv.AllowIP(ctx, address, reason)
|
||||
}
|
||||
|
||||
// rewriteDataPath points the data store's description at its new home. Only
|
||||
// a path under the old home is changed: a store kept elsewhere (PostgreSQL,
|
||||
// a path the operator chose) is left alone.
|
||||
func rewriteDataPath(configPath, from, to string) error {
|
||||
b, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the server's configuration: %w", err)
|
||||
}
|
||||
var cfg map[string]any
|
||||
if err := json.Unmarshal(b, &cfg); err != nil {
|
||||
return fmt.Errorf("%s isn't JSON: %w", configPath, err)
|
||||
}
|
||||
p, ok := cfg["path"].(string)
|
||||
if !ok || !strings.HasPrefix(p, strings.TrimSuffix(from, "/")) {
|
||||
return nil
|
||||
}
|
||||
cfg["path"] = strings.TrimSuffix(to, "/") + strings.TrimPrefix(p, strings.TrimSuffix(from, "/"))
|
||||
out, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info, err := os.Stat(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(configPath, out, info.Mode().Perm())
|
||||
}
|
||||
|
||||
// moveContents renames everything in src into dest, which is made if need
|
||||
// be. Both are on one filesystem.
|
||||
func moveContents(src, dest string) error {
|
||||
entries, err := os.ReadDir(src)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(dest, 0o750); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, e := range entries {
|
||||
if err := os.Rename(filepath.Join(src, e.Name()), filepath.Join(dest, e.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyFile(src, dest string, mode os.FileMode) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
out, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(out, in); err != nil {
|
||||
out.Close()
|
||||
return err
|
||||
}
|
||||
return out.Close()
|
||||
}
|
||||
|
||||
// pipe runs out | in and reports the first of their failures, with what
|
||||
// each said on its standard error.
|
||||
func pipe(out, in *exec.Cmd) error {
|
||||
r, w := io.Pipe()
|
||||
var outErr, inErr strings.Builder
|
||||
out.Stdout, out.Stderr = w, &outErr
|
||||
in.Stdin, in.Stderr = r, &inErr
|
||||
if err := in.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
errOut := out.Run()
|
||||
w.CloseWithError(errOut)
|
||||
errIn := in.Wait()
|
||||
switch {
|
||||
case errOut != nil:
|
||||
return fmt.Errorf("%w: %s", errOut, strings.TrimSpace(outErr.String()))
|
||||
case errIn != nil:
|
||||
return fmt.Errorf("%w: %s", errIn, strings.TrimSpace(inErr.String()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
package apply
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/plan"
|
||||
"git.coffeylabs.org/inbuxa/inbuxa-installer/internal/state"
|
||||
)
|
||||
|
||||
func TestAMoveIsAnotherServerShapeOnAnInstalledMachine(t *testing.T) {
|
||||
opts := func(s plan.Shape) plan.Options {
|
||||
return plan.Options{Choices: []plan.Choice{{Component: plan.Server, Shape: s}}}
|
||||
}
|
||||
installed := &state.State{Shapes: map[string]string{"server": "container", "webmail": "container"}}
|
||||
if from, ok := MovingServer(installed, opts(plan.Host)); !ok || from != plan.Container {
|
||||
t.Errorf("container to host: from=%q ok=%v", from, ok)
|
||||
}
|
||||
if _, ok := MovingServer(installed, opts(plan.Container)); ok {
|
||||
t.Error("the same shape is a converge, not a move")
|
||||
}
|
||||
if _, ok := MovingServer(installed, opts(plan.Skip)); ok {
|
||||
t.Error("skipping the server is not a move")
|
||||
}
|
||||
if _, ok := MovingServer(&state.State{}, opts(plan.Host)); ok {
|
||||
t.Error("nothing installed: a first install, not a move")
|
||||
}
|
||||
removed := time.Now()
|
||||
gone := &state.State{Shapes: map[string]string{"server": "container"}, RemovedAt: &removed}
|
||||
if _, ok := MovingServer(gone, opts(plan.Host)); ok {
|
||||
t.Error("after an uninstall nothing runs to move")
|
||||
}
|
||||
frontEnds := &state.State{Shapes: map[string]string{"webmail": "container"}}
|
||||
if _, ok := MovingServer(frontEnds, opts(plan.Host)); ok {
|
||||
t.Error("a machine with only front ends has no server to move")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyTheStoresPathUnderTheOldHomeIsRewritten(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfg := filepath.Join(dir, "config.json")
|
||||
write := func(v map[string]any) {
|
||||
b, _ := json.Marshal(v)
|
||||
os.WriteFile(cfg, b, 0o640)
|
||||
}
|
||||
read := func() map[string]any {
|
||||
b, _ := os.ReadFile(cfg)
|
||||
var v map[string]any
|
||||
json.Unmarshal(b, &v)
|
||||
return v
|
||||
}
|
||||
|
||||
write(map[string]any{"@type": "RocksDb", "path": "/var/lib/inbuxa/", "poolWorkers": 4.0})
|
||||
if err := rewriteDataPath(cfg, containerDataPath, "/var/lib/inbuxa/server/data/"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := read()
|
||||
if got["path"] != "/var/lib/inbuxa/server/data/" || got["poolWorkers"] != 4.0 || got["@type"] != "RocksDb" {
|
||||
t.Errorf("into the host: %v", got)
|
||||
}
|
||||
if err := rewriteDataPath(cfg, "/var/lib/inbuxa/server/data/", containerDataPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := read(); got["path"] != "/var/lib/inbuxa/" {
|
||||
t.Errorf("back into a container: %v", got)
|
||||
}
|
||||
if info, _ := os.Stat(cfg); info.Mode().Perm() != 0o640 {
|
||||
t.Errorf("mode = %v, want the file's own", info.Mode().Perm())
|
||||
}
|
||||
|
||||
// A store somewhere else is the operator's, and stays where it is.
|
||||
write(map[string]any{"@type": "RocksDb", "path": "/srv/mail/store/"})
|
||||
rewriteDataPath(cfg, containerDataPath, "/var/lib/inbuxa/server/data/")
|
||||
if got := read(); got["path"] != "/srv/mail/store/" {
|
||||
t.Errorf("a store elsewhere moved: %v", got)
|
||||
}
|
||||
write(map[string]any{"@type": "PostgreSql", "host": "db"})
|
||||
rewriteDataPath(cfg, containerDataPath, "/var/lib/inbuxa/server/data/")
|
||||
if got := read(); got["path"] != nil || got["host"] != "db" {
|
||||
t.Errorf("a database store changed: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMovedContentsArriveAndLeaveTheStageEmpty(t *testing.T) {
|
||||
src, dest := t.TempDir(), filepath.Join(t.TempDir(), "made")
|
||||
os.MkdirAll(filepath.Join(src, "rocks"), 0o750)
|
||||
os.WriteFile(filepath.Join(src, "rocks", "CURRENT"), []byte("MANIFEST-1\n"), 0o640)
|
||||
os.WriteFile(filepath.Join(src, "top"), []byte("x"), 0o600)
|
||||
if err := moveContents(src, dest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, err := os.ReadFile(filepath.Join(dest, "rocks", "CURRENT")); err != nil || string(b) != "MANIFEST-1\n" {
|
||||
t.Errorf("CURRENT: %q %v", b, err)
|
||||
}
|
||||
if info, err := os.Stat(filepath.Join(dest, "top")); err != nil || info.Mode().Perm() != 0o600 {
|
||||
t.Errorf("top: %v %v", info, err)
|
||||
}
|
||||
if left, _ := os.ReadDir(src); len(left) != 0 {
|
||||
t.Errorf("left behind: %v", left)
|
||||
}
|
||||
if err := moveContents(filepath.Join(src, "absent"), dest); err != nil {
|
||||
t.Errorf("nothing to move is not an error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -379,6 +379,68 @@ func (c *Client) Listeners(ctx context.Context) ([]Listener, error) {
|
||||
return got.List, json.Unmarshal(rs[1].Args, &got)
|
||||
}
|
||||
|
||||
// ContainerListeners undoes HostListeners, for a server moving from the host
|
||||
// into a container: every listener back on [::], plain HTTP on 8080, which
|
||||
// is what the compose file publishes. The HTTPS listener HostListeners took
|
||||
// away is not put back: the proxy terminates TLS in front of a container as
|
||||
// well, and the container's 443 is not published. Like any listener change
|
||||
// it takes effect when the server next starts.
|
||||
func (c *Client) ContainerListeners(ctx context.Context) error {
|
||||
ls, err := c.Listeners(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
update := map[string]any{}
|
||||
for _, l := range ls {
|
||||
binds := map[string]bool{}
|
||||
for addr := range l.Bind {
|
||||
if i := strings.LastIndex(addr, ":"); i >= 0 {
|
||||
binds["[::]"+addr[i:]] = true
|
||||
}
|
||||
}
|
||||
if l.Protocol == "http" && !l.TLSImplicit {
|
||||
binds = map[string]bool{"[::]:8080": true}
|
||||
}
|
||||
update[l.ID] = map[string]any{"bind": binds}
|
||||
}
|
||||
if len(update) == 0 {
|
||||
return nil
|
||||
}
|
||||
rs, err := c.Do(ctx, Call{"x:NetworkListener/set", map[string]any{"update": update}, "0"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = DecodeSet(rs[0])
|
||||
return err
|
||||
}
|
||||
|
||||
// AllowedIPs is the set of addresses already exempted from the auto-ban.
|
||||
func (c *Client) AllowedIPs(ctx context.Context) (map[string]bool, error) {
|
||||
rs, err := c.Do(ctx,
|
||||
Call{"x:AllowedIp/query", map[string]any{}, "0"},
|
||||
Call{"x:AllowedIp/get", map[string]any{
|
||||
"#ids": map[string]any{"resultOf": "0", "name": "x:AllowedIp/query", "path": "/ids"},
|
||||
"properties": []string{"address"},
|
||||
}, "1"},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var got struct {
|
||||
List []struct {
|
||||
Address string `json:"address"`
|
||||
} `json:"list"`
|
||||
}
|
||||
if err := json.Unmarshal(rs[1].Args, &got); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
have := map[string]bool{}
|
||||
for _, a := range got.List {
|
||||
have[a.Address] = true
|
||||
}
|
||||
return have, nil
|
||||
}
|
||||
|
||||
// HostListeners fits the listeners a server creates on its first configured
|
||||
// start to a server running on the host rather than in a container.
|
||||
//
|
||||
|
||||
@@ -82,6 +82,9 @@ type Options struct {
|
||||
// ExemptAddress is the address the server sees the webmail's requests
|
||||
// come from, when it is not what the webmail's name resolves to.
|
||||
ExemptAddress string
|
||||
// Key is an API key for what changes a running server's own settings:
|
||||
// moving it between a container and the host. Never written down.
|
||||
Key string
|
||||
|
||||
// Where this came from, when it came from a topology file: the machine's
|
||||
// name in it, and the path, both recorded in the state file so a later
|
||||
|
||||
Reference in new issue
Block a user