fix(selfhost): retire deprecated Matrix services during updates
This commit is contained in:
@@ -29,6 +29,8 @@ REGISTRY = "git.federspiel.tech/flfeders/fedeo"
|
||||
VERSION = re.compile(r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\Z")
|
||||
FILES = ("release-manifest.json", "docker-compose.selfhost.yml", ".env.example", "selfhost-setup.sh", "CHANGELOG.md")
|
||||
GIB = 1024 ** 3
|
||||
LEGACY_MATRIX_SERVICES = {"matrix-db", "matrix-synapse", "matrix-redis", "matrix-element",
|
||||
"matrix-livekit", "matrix-rtc-jwt", "matrix-turn", "matrix-well-known"}
|
||||
|
||||
|
||||
class UpdateError(Exception):
|
||||
@@ -346,6 +348,18 @@ class Updater:
|
||||
ids = self.compose(False, "ps", "--all", "--quiet").split()
|
||||
self.containers = [self.inspect(container) for container in ids]
|
||||
write_json(self.job / "original-containers.json", self.containers)
|
||||
self.retired_containers = [c for c in self.containers
|
||||
if c["Config"]["Labels"].get("com.docker.compose.service") in self.retired_services]
|
||||
if any(c.get("HostConfig", {}).get("AutoRemove") for c in self.retired_containers):
|
||||
raise UpdateError("Matrix-Container mit automatischer Löschung können nicht zur Wiederherstellung erhalten werden.")
|
||||
for container in self.retired_containers:
|
||||
name = container["Config"]["Labels"]["com.docker.compose.service"]
|
||||
expected = {v["target"]: v["source"] for v in self.current["services"][name].get("volumes", [])}
|
||||
actual = {v["Destination"]: v["Source"] for v in container.get("Mounts", []) if v["Type"] == "bind"}
|
||||
if actual != expected:
|
||||
raise UpdateError(f"{name}: Tatsächliche Matrix-Datenpfade weichen von der Compose-Konfiguration ab.")
|
||||
self.save(retiredServices=self.retired_services,
|
||||
retiredContainerIds=[c["Id"] for c in self.retired_containers])
|
||||
if any(c["State"]["Running"] and c["Config"]["Labels"].get("com.docker.compose.oneoff", "False").lower() == "true" for c in self.containers):
|
||||
raise UpdateError("Ein Einmalcontainer läuft noch; dessen Arbeit zuerst abschließen.")
|
||||
self.running = [c["Config"]["Labels"]["com.docker.compose.service"] for c in self.containers
|
||||
@@ -360,11 +374,14 @@ class Updater:
|
||||
'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Atc "SELECT pg_database_size(current_database())"').strip())
|
||||
minio_size = int(self.runner.run(["du", "-sb", str(self.root / "minio")]).split()[0])
|
||||
self.backup_size = database_size * 2 + minio_size + GIB
|
||||
if self.matrix_backup_paths:
|
||||
self.backup_size += int(self.runner.run(["du", "-sb", str(self.root / "matrix")]).split()[0])
|
||||
self.check_space(self.backup_size, self.home)
|
||||
plan = {"version": self.target, "revision": self.manifest["commit"], "previousVersion": old_version,
|
||||
"images": self.images, "composeDiff": str(self.job / "compose.diff"),
|
||||
"releaseNotes": str(self.job / "CHANGELOG.md"), "requiresComposeAdoption": not self.managed,
|
||||
"estimatedBackupBytes": self.backup_size, "services": list(self.candidate["services"])}
|
||||
"estimatedBackupBytes": self.backup_size, "services": list(self.candidate["services"]),
|
||||
"retiredServices": self.retired_services, "retainedDataPaths": self.matrix_backup_paths}
|
||||
write_json(self.job / "plan.json", plan)
|
||||
return plan
|
||||
|
||||
@@ -374,6 +391,8 @@ class Updater:
|
||||
differences = []
|
||||
for label, services in (("Bestehende Konfiguration", old), ("Zielkonfiguration", new)):
|
||||
missing, additional = required - set(services), set(services) - required
|
||||
if services is old:
|
||||
additional -= LEGACY_MATRIX_SERVICES
|
||||
details = []
|
||||
if missing:
|
||||
details.append("fehlend: " + ", ".join(sorted(missing)))
|
||||
@@ -384,6 +403,9 @@ class Updater:
|
||||
if differences:
|
||||
raise UpdateError("Dieser Updater unterstützt den Standard-Selfhost-Stack mit lokalen Daten und MinIO. "
|
||||
+ ". ".join(differences) + ". Zusätzliche Dienste benötigen Unterstützung für Erhalt, Backup und Wiederherstellung.")
|
||||
self.retired_services = sorted(set(old) & LEGACY_MATRIX_SERVICES)
|
||||
self.matrix_backup_paths = []
|
||||
self.validate_legacy_matrix()
|
||||
for name in required:
|
||||
if old[name].get("profiles") or new[name].get("profiles") or new[name].get("build"):
|
||||
raise UpdateError("Build-Kontexte und Compose-Profile werden nicht unterstützt.")
|
||||
@@ -437,7 +459,46 @@ class Updater:
|
||||
if value is None or (value == "" and key in ("DOMAIN", "POSTGRES_PASSWORD", "COOKIE_SECRET", "JWT_SECRET", "ENCRYPTION_KEY")):
|
||||
raise UpdateError(f"Fehlende Umgebungsvariable: {name}.{key}.")
|
||||
|
||||
def validate_legacy_matrix(self):
|
||||
"""Only retire the known, local legacy stack; never silently drop arbitrary services."""
|
||||
if not self.retired_services:
|
||||
return
|
||||
matrix_root = self.root / "matrix"
|
||||
for name in self.retired_services:
|
||||
service = self.current["services"][name]
|
||||
if any(service.get(key) for key in ("profiles", "build", "volumes_from", "configs", "secrets")):
|
||||
raise UpdateError(f"{name}: Abweichende Matrix-Konfiguration benötigt eine manuelle Stilllegung.")
|
||||
volumes = service.get("volumes", [])
|
||||
for volume in volumes:
|
||||
source = Path(volume.get("source", ""))
|
||||
if (volume.get("type") != "bind" or not source.is_relative_to(matrix_root)
|
||||
or source.resolve() != source or not source.exists()):
|
||||
raise UpdateError(f"{name}: Matrix-Daten müssen ohne symbolische Links unter {matrix_root} liegen.")
|
||||
expected = {"matrix-db": ("postgres", "/var/lib/postgresql/data"),
|
||||
"matrix-synapse": ("synapse", "/data")}.get(name)
|
||||
if expected and (not (matrix_root / expected[0]).is_dir() or not any(
|
||||
v.get("source") == str(matrix_root / expected[0]) and v.get("target") == expected[1] for v in volumes)):
|
||||
raise UpdateError(f"{name}: Das lokale Standard-Matrix-Datenverzeichnis fehlt.")
|
||||
if matrix_root.exists() or matrix_root.is_symlink():
|
||||
if matrix_root.is_symlink() or not matrix_root.is_dir():
|
||||
raise UpdateError("Das Matrix-Datenverzeichnis muss ein reguläres lokales Verzeichnis sein.")
|
||||
# A cold PostgreSQL backup must include WAL and tablespaces, not external symlink targets.
|
||||
if any(path.is_symlink() for path in matrix_root.rglob("*")):
|
||||
raise UpdateError("Symbolische Links in Matrix-Daten benötigen eine manuelle Sicherung/Stilllegung.")
|
||||
self.matrix_backup_paths = ["matrix"]
|
||||
|
||||
def verify_matrix_stopped(self, *, retired=False):
|
||||
for container_id in self.state.get("retiredContainerIds", []):
|
||||
container = self.inspect(container_id)
|
||||
state = container.get("State", {})
|
||||
if state.get("Running") or state.get("Restarting") or state.get("Paused"):
|
||||
raise UpdateError("Ein alter Matrix-Container läuft noch; Stilllegung nicht abgeschlossen.")
|
||||
if retired and container.get("HostConfig", {}).get("RestartPolicy", {}).get("Name") != "no":
|
||||
raise UpdateError("Ein alter Matrix-Container könnte automatisch neu starten; Stilllegung nicht abgeschlossen.")
|
||||
|
||||
def backup(self):
|
||||
self.verify_matrix_stopped()
|
||||
self.validate_legacy_matrix()
|
||||
directory = self.job / "backup"
|
||||
directory.mkdir(mode=0o700)
|
||||
for name in (".env", "docker-compose.yml", ".env.example", "scripts/selfhost-setup.sh"):
|
||||
@@ -451,7 +512,7 @@ class Updater:
|
||||
self.compose(False, "exec", "-T", "db", "pg_restore", "--list", input_file=directory / "database.dump")
|
||||
self.compose(False, "exec", "-T", "db", "sh", "-ec",
|
||||
'exec pg_dumpall -U "$POSTGRES_USER" --globals-only', output=directory / "globals.sql")
|
||||
paths = ["minio"]
|
||||
paths = ["minio", *self.matrix_backup_paths]
|
||||
if (self.root / "traefik/letsencrypt").is_dir():
|
||||
paths.append("traefik/letsencrypt")
|
||||
self.runner.run(["tar", "-cpf", str(directory / "files.tar"), "-C", str(self.root), *paths], timeout=7200)
|
||||
@@ -465,7 +526,10 @@ class Updater:
|
||||
digest.update(chunk)
|
||||
os.fsync(stream.fileno())
|
||||
checksums[str(path.relative_to(directory))] = digest.hexdigest()
|
||||
write_json(directory / "complete.json", {"createdAt": now(), "database": "database.dump", "files": "files.tar", "sha256": checksums})
|
||||
self.verify_matrix_stopped()
|
||||
write_json(directory / "complete.json", {"createdAt": now(), "database": "database.dump", "files": "files.tar",
|
||||
"retiredServices": self.retired_services, "retainedContainerIds": self.state["retiredContainerIds"],
|
||||
"matrixDataPaths": self.matrix_backup_paths, "sha256": checksums})
|
||||
self.save("backed_up", backup=str(directory))
|
||||
|
||||
def verify(self, version, revision=None):
|
||||
@@ -498,6 +562,13 @@ class Updater:
|
||||
raise UpdateError("Konfiguration wurde während des Backups verändert; erneut prüfen.")
|
||||
# From here recovery must be explicit: migrations or infrastructure can alter persisted data.
|
||||
self.save("applying", mutationStarted=True)
|
||||
if self.retired_containers:
|
||||
self.save("retiring_matrix")
|
||||
self.verify_matrix_stopped()
|
||||
# Keep containers, writable layers and volumes for recovery; prevent restart on host reboot.
|
||||
for container in self.retired_containers:
|
||||
self.runner.run(["docker", "update", "--restart=no", container["Id"]])
|
||||
self.verify_matrix_stopped(retired=True)
|
||||
atomic(self.root / "docker-compose.yml", (self.job / "target.compose.yml").read_bytes())
|
||||
atomic(self.root / ".env", (self.job / "target.env").read_bytes())
|
||||
atomic(self.root / ".env.example", (self.job / ".env.example").read_bytes())
|
||||
@@ -511,6 +582,7 @@ class Updater:
|
||||
self.save("starting")
|
||||
self.compose(True, "up", "-d", "--wait", "--wait-timeout", "600", timeout=900)
|
||||
self.save("verifying")
|
||||
self.verify_matrix_stopped(retired=True)
|
||||
for name, image in self.images.items():
|
||||
container_id = self.compose(True, "ps", "--quiet", name).strip()
|
||||
container = self.inspect(container_id)
|
||||
@@ -596,10 +668,22 @@ class Updater:
|
||||
if not expected or container.get("Image") != expected:
|
||||
raise UpdateError(f"{name} entspricht weder dem gewählten Zielrelease noch dem gesicherten Originalstand.")
|
||||
if version == manifest["version"]:
|
||||
self.verify_matrix_stopped(retired=True)
|
||||
self.verify(version, manifest["commit"])
|
||||
write_json(self.home / "installed.json", {"manifest": manifest,
|
||||
"composeSha256": sha((self.job / "target.compose.yml").read_bytes()), "installedAt": now()})
|
||||
else:
|
||||
for original in read_json(self.job / "original-containers.json", []):
|
||||
if original.get("Id") not in state.get("retiredContainerIds", []):
|
||||
continue
|
||||
name = original["Config"]["Labels"]["com.docker.compose.service"]
|
||||
container_id = self.compose(False, "ps", "--all", "--quiet", name).strip()
|
||||
restored = self.inspect(container_id) if container_id else {}
|
||||
if (restored.get("Image") != original["Image"]
|
||||
or restored.get("State", {}).get("Running") != original["State"]["Running"]
|
||||
or (original["State"]["Running"] and restored.get("State", {}).get("Health", {}).get("Status") not in (None, "healthy"))
|
||||
or restored.get("HostConfig", {}).get("RestartPolicy") != original["HostConfig"]["RestartPolicy"]):
|
||||
raise UpdateError("Die ursprünglichen Matrix-Container und Neustartrichtlinien müssen zuerst wiederhergestellt werden.")
|
||||
previous = read_json(self.job / "original-installed.json", {})
|
||||
if previous:
|
||||
write_json(self.home / "installed.json", previous)
|
||||
|
||||
Reference in New Issue
Block a user