@rizom/ops 0.2.0-alpha.776 → 0.2.0-alpha.777
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -905,6 +905,44 @@ export function predeployBackupNotices(stderr: string): string[] {
|
|
|
905
905
|
);
|
|
906
906
|
}
|
|
907
907
|
|
|
908
|
+
/**
|
|
909
|
+
* Before a deploy, a fresh online backup of the brain's homeserver, when one
|
|
910
|
+
* runs beside it: a SIGUSR2 makes Tuwunel back its database up into
|
|
911
|
+
* `<dir>/backup`, beside the configuration that holds its secrets, and the
|
|
912
|
+
* step waits until the new backup is there. Refuses the deploy when none
|
|
913
|
+
* appears, as the rest of the predeploy backup does.
|
|
914
|
+
*/
|
|
915
|
+
export function renderHomeserverBackupStep(
|
|
916
|
+
options: { dir?: string; waitSeconds?: number } = {},
|
|
917
|
+
): string {
|
|
918
|
+
const dir = options.dir ?? "/opt/brain-matrix";
|
|
919
|
+
const waitSeconds = options.waitSeconds ?? 120;
|
|
920
|
+
return `homeserver="\${SERVICE_NAME}-matrix"
|
|
921
|
+
if docker ps --filter "name=^\${homeserver}$" --format '{{.Names}}' | grep -qx "$homeserver"; then
|
|
922
|
+
# Tuwunel keeps a set number of backups and numbers each one, so a new
|
|
923
|
+
# backup shows as a higher id rather than as one more file.
|
|
924
|
+
latest_homeserver_backup() {
|
|
925
|
+
find "${dir}/backup/meta" -maxdepth 1 -type f -printf '%f\\n' 2>/dev/null | grep -E '^[0-9]+$' | sort -n | tail -n 1 || true
|
|
926
|
+
}
|
|
927
|
+
before="$(latest_homeserver_backup)"
|
|
928
|
+
before="\${before:-0}"
|
|
929
|
+
docker kill --signal USR2 "$homeserver" >/dev/null
|
|
930
|
+
after="$before"
|
|
931
|
+
for attempt in $(seq 1 ${waitSeconds}); do
|
|
932
|
+
after="$(latest_homeserver_backup)"
|
|
933
|
+
after="\${after:-0}"
|
|
934
|
+
[ "$after" -gt "$before" ] && break
|
|
935
|
+
sleep 1
|
|
936
|
+
done
|
|
937
|
+
if [ "$after" -le "$before" ]; then
|
|
938
|
+
echo "pre-deploy snapshot: the homeserver's backup did not appear in ${dir}/backup" >&2
|
|
939
|
+
exit 1
|
|
940
|
+
fi
|
|
941
|
+
echo "pre-deploy snapshot: homeserver backed up in ${dir}/backup"
|
|
942
|
+
fi
|
|
943
|
+
`;
|
|
944
|
+
}
|
|
945
|
+
|
|
908
946
|
export function renderPredeployBackupRemoteScript(options?: {
|
|
909
947
|
captureProgramBase64?: string;
|
|
910
948
|
}): string {
|
|
@@ -954,6 +992,7 @@ if [ "$status" != running ] || [ "$health" != healthy ]; then
|
|
|
954
992
|
exit 1
|
|
955
993
|
fi
|
|
956
994
|
|
|
995
|
+
${renderHomeserverBackupStep()}
|
|
957
996
|
docker exec "$container" bun -e '
|
|
958
997
|
${renderPredeployReadinessProgram()}'
|
|
959
998
|
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { requireEnv } from "./helpers";
|
|
2
|
+
|
|
3
|
+
/** Where the brain writes the homeserver's configuration on the server. */
|
|
4
|
+
const HOMESERVER_DIR = "/opt/brain-matrix";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Restarts the homeserver when the brain has written it a configuration
|
|
8
|
+
* it has not started on yet, such as after a domain change: Tuwunel reads
|
|
9
|
+
* its configuration only at start, and the brain cannot restart an
|
|
10
|
+
* accessory. The first deploy only records the configuration, because the
|
|
11
|
+
* homeserver's restart policy already started it on that file.
|
|
12
|
+
*/
|
|
13
|
+
export function buildHomeserverReloadScript(
|
|
14
|
+
serviceName: string,
|
|
15
|
+
dir: string = HOMESERVER_DIR,
|
|
16
|
+
options: { sudo?: boolean } = {},
|
|
17
|
+
): string {
|
|
18
|
+
const sudo =
|
|
19
|
+
options.sudo === false
|
|
20
|
+
? 'SUDO=""'
|
|
21
|
+
: `SUDO=""
|
|
22
|
+
if [ "$(id -u)" -ne 0 ]; then
|
|
23
|
+
SUDO=sudo
|
|
24
|
+
fi`;
|
|
25
|
+
return `#!/usr/bin/env bash
|
|
26
|
+
set -euo pipefail
|
|
27
|
+
${sudo}
|
|
28
|
+
|
|
29
|
+
config="${dir}/tuwunel.toml"
|
|
30
|
+
started="${dir}/.started-config.sha256"
|
|
31
|
+
if [ ! -f "$config" ]; then
|
|
32
|
+
echo "The brain has not written the homeserver configuration yet."
|
|
33
|
+
exit 0
|
|
34
|
+
fi
|
|
35
|
+
current="$(sha256sum "$config" | cut -d' ' -f1)"
|
|
36
|
+
if [ ! -f "$started" ]; then
|
|
37
|
+
echo "$current" | $SUDO tee "$started" >/dev/null
|
|
38
|
+
echo "The homeserver started on its first configuration."
|
|
39
|
+
exit 0
|
|
40
|
+
fi
|
|
41
|
+
if [ "$(cat "$started")" = "$current" ]; then
|
|
42
|
+
echo "The homeserver configuration is unchanged."
|
|
43
|
+
exit 0
|
|
44
|
+
fi
|
|
45
|
+
$SUDO docker restart ${serviceName}-matrix
|
|
46
|
+
echo "$current" | $SUDO tee "$started" >/dev/null
|
|
47
|
+
echo "Restarted the homeserver on its new configuration."
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function reloadHomeserver(): Promise<void> {
|
|
52
|
+
if (process.env["MATRIX_ENABLED"] !== "true") {
|
|
53
|
+
console.log("No homeserver in this deploy; nothing to reload.");
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const serverIp = requireEnv("SERVER_IP");
|
|
57
|
+
const sshUser = requireEnv("SSH_USER").trim();
|
|
58
|
+
const serviceName = requireEnv("SERVICE_NAME").trim();
|
|
59
|
+
if (!sshUser) throw new Error("Missing SSH_USER");
|
|
60
|
+
const child = Bun.spawn(["ssh", `${sshUser}@${serverIp}`, "bash -s"], {
|
|
61
|
+
stdin: new Blob([buildHomeserverReloadScript(serviceName)]),
|
|
62
|
+
stdout: "inherit",
|
|
63
|
+
stderr: "inherit",
|
|
64
|
+
});
|
|
65
|
+
const exitCode = await child.exited;
|
|
66
|
+
if (exitCode !== 0) {
|
|
67
|
+
throw new Error(`Reloading the homeserver failed with exit ${exitCode}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (import.meta.main) {
|
|
72
|
+
await reloadHomeserver();
|
|
73
|
+
}
|