|
|
@@ -823,6 +823,263 @@ def render_mod_host_service(config):
|
|
|
return content.encode("utf-8")
|
|
|
|
|
|
|
|
|
+def render_mod_host_reconcile_units(config):
|
|
|
+ repository_root = ROOT.resolve()
|
|
|
+ state_file = repository_root / "config" / "desired-state.yaml"
|
|
|
+ observer_script = repository_root / config["observer_script"]
|
|
|
+ service_template = repository_root / config["reconcile_service_source"]
|
|
|
+ timer_template = repository_root / config["reconcile_timer_source"]
|
|
|
+ for description, path in (
|
|
|
+ ("PipeWire observer", observer_script),
|
|
|
+ ("reconciliation service template", service_template),
|
|
|
+ ("reconciliation timer template", timer_template),
|
|
|
+ ):
|
|
|
+ if not path.is_file():
|
|
|
+ raise RuntimeError(f"mod-host {description} is missing: {path}")
|
|
|
+
|
|
|
+ service_replacements = {
|
|
|
+ "@PYTHON@": systemd_quote(config["python_path"]),
|
|
|
+ "@OBSERVER_SCRIPT@": systemd_quote(observer_script),
|
|
|
+ "@DESIRED_STATE@": systemd_quote(state_file),
|
|
|
+ }
|
|
|
+ service_content = service_template.read_text(encoding="utf-8")
|
|
|
+ for placeholder, replacement in service_replacements.items():
|
|
|
+ service_content = service_content.replace(placeholder, replacement)
|
|
|
+ if re.search(r"@[A-Z_]+@", service_content):
|
|
|
+ raise RuntimeError("unexpanded placeholder remains in reconciliation service template")
|
|
|
+
|
|
|
+ timer_content = timer_template.read_text(encoding="utf-8").replace(
|
|
|
+ "@RECONCILE_INTERVAL_SECONDS@",
|
|
|
+ str(config["reconcile_interval_seconds"]),
|
|
|
+ )
|
|
|
+ if re.search(r"@[A-Z_]+@", timer_content):
|
|
|
+ raise RuntimeError("unexpanded placeholder remains in reconciliation timer template")
|
|
|
+
|
|
|
+ return {
|
|
|
+ "service": {
|
|
|
+ "name": config["reconcile_service"],
|
|
|
+ "destination": Path.home() / config["reconcile_service_destination"],
|
|
|
+ "content": service_content.encode("utf-8"),
|
|
|
+ },
|
|
|
+ "timer": {
|
|
|
+ "name": config["reconcile_timer"],
|
|
|
+ "destination": Path.home() / config["reconcile_timer_destination"],
|
|
|
+ "content": timer_content.encode("utf-8"),
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def atomic_install_bytes(content, destination):
|
|
|
+ destination.parent.mkdir(parents=True, exist_ok=True)
|
|
|
+ fd, temporary_name = tempfile.mkstemp(prefix=f".{destination.name}.", dir=destination.parent)
|
|
|
+ try:
|
|
|
+ with os.fdopen(fd, "wb") as stream:
|
|
|
+ stream.write(content)
|
|
|
+ os.chmod(temporary_name, 0o644)
|
|
|
+ os.replace(temporary_name, destination)
|
|
|
+ finally:
|
|
|
+ if os.path.exists(temporary_name):
|
|
|
+ os.unlink(temporary_name)
|
|
|
+
|
|
|
+
|
|
|
+def file_sha256(path):
|
|
|
+ return hashlib.sha256(path.read_bytes()).hexdigest() if path.is_file() else None
|
|
|
+
|
|
|
+
|
|
|
+def install_mod_host_reconcile_units(config, registry):
|
|
|
+ units = render_mod_host_reconcile_units(config)
|
|
|
+ managed = registry.get("mod_host_reconcile")
|
|
|
+ if managed is not None and managed.get("pending_uninstall"):
|
|
|
+ raise RuntimeError("reconciliation units are pending uninstall; retry uninstall mod-host first")
|
|
|
+ if managed is None and (
|
|
|
+ mod_host_unit_is_active(config["reconcile_service"])
|
|
|
+ or mod_host_unit_is_active(config["reconcile_timer"])
|
|
|
+ or mod_host_unit_is_enabled(config["reconcile_timer"])
|
|
|
+ ):
|
|
|
+ raise RuntimeError(
|
|
|
+ "an unmanaged Rigforge reconciliation unit is already active or enabled; "
|
|
|
+ "inspect it before installing the managed timer"
|
|
|
+ )
|
|
|
+
|
|
|
+ prior_files = managed.get("files", {}) if managed is not None else {}
|
|
|
+ if managed is not None and set(prior_files) != set(units):
|
|
|
+ raise RuntimeError("managed reconciliation unit registry has an unexpected file set")
|
|
|
+
|
|
|
+ records = {}
|
|
|
+ for key, unit in units.items():
|
|
|
+ destination = unit["destination"]
|
|
|
+ validate_user_destination(destination)
|
|
|
+ if destination.exists() and not destination.is_file():
|
|
|
+ raise RuntimeError(f"reconciliation unit destination has the wrong type: {destination}")
|
|
|
+ current_digest = file_sha256(destination)
|
|
|
+ prior = prior_files.get(key) if managed is not None else None
|
|
|
+ if prior is None:
|
|
|
+ if managed is not None:
|
|
|
+ raise RuntimeError(f"reconciliation unit is missing from deployment registry: {key}")
|
|
|
+ backup_path = destination.with_name(destination.name + ".rigforge-backup")
|
|
|
+ if backup_path.exists() or backup_path.is_symlink():
|
|
|
+ raise RuntimeError(f"backup path already exists: {backup_path}")
|
|
|
+ backup = str(backup_path) if destination.is_file() else None
|
|
|
+ backup_digest = current_digest
|
|
|
+ previous_digest = None
|
|
|
+ else:
|
|
|
+ if prior.get("destination") != str(destination):
|
|
|
+ raise RuntimeError(f"recorded reconciliation unit path changed: {key}")
|
|
|
+ backup_path = validate_backup_path(prior.get("backup"), destination)
|
|
|
+ backup = str(backup_path) if backup_path is not None else None
|
|
|
+ backup_digest = prior.get("backup_sha256")
|
|
|
+ if (backup_path is None) != (backup_digest is None):
|
|
|
+ raise RuntimeError(f"invalid reconciliation unit backup record: {key}")
|
|
|
+ previous_digest = prior.get("installed_sha256")
|
|
|
+ if backup_path is not None and backup_path.exists():
|
|
|
+ if not backup_path.is_file() or file_sha256(backup_path) != backup_digest:
|
|
|
+ raise RuntimeError(f"recorded reconciliation unit backup changed locally: {backup_path}")
|
|
|
+ elif (
|
|
|
+ backup_path is not None
|
|
|
+ and not managed.get("pending_install")
|
|
|
+ ):
|
|
|
+ raise RuntimeError(f"recorded reconciliation unit backup is missing: {backup_path}")
|
|
|
+ if managed.get("pending_install"):
|
|
|
+ accepted_digests = {
|
|
|
+ prior.get("installed_sha256"),
|
|
|
+ prior.get("previous_installed_sha256"),
|
|
|
+ backup_digest,
|
|
|
+ None,
|
|
|
+ }
|
|
|
+ if current_digest not in accepted_digests:
|
|
|
+ raise RuntimeError(
|
|
|
+ f"reconciliation unit changed locally during interrupted install: {destination}"
|
|
|
+ )
|
|
|
+ elif current_digest != prior.get("installed_sha256"):
|
|
|
+ raise RuntimeError(f"managed reconciliation unit changed locally: {destination}")
|
|
|
+
|
|
|
+ records[key] = {
|
|
|
+ "destination": str(destination),
|
|
|
+ "installed_sha256": hashlib.sha256(unit["content"]).hexdigest(),
|
|
|
+ "previous_installed_sha256": previous_digest,
|
|
|
+ "backup": backup,
|
|
|
+ "backup_sha256": backup_digest,
|
|
|
+ }
|
|
|
+
|
|
|
+ managed = managed or {}
|
|
|
+ managed["files"] = records
|
|
|
+ managed["pending_install"] = True
|
|
|
+ registry["mod_host_reconcile"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+
|
|
|
+ for key, unit in units.items():
|
|
|
+ record = records[key]
|
|
|
+ backup_path = validate_backup_path(record["backup"], unit["destination"])
|
|
|
+ if backup_path is not None:
|
|
|
+ if backup_path.exists():
|
|
|
+ if not backup_path.is_file() or file_sha256(backup_path) != record["backup_sha256"]:
|
|
|
+ raise RuntimeError(f"reconciliation unit backup changed locally: {backup_path}")
|
|
|
+ elif file_sha256(unit["destination"]) == record["backup_sha256"]:
|
|
|
+ shutil.copy2(unit["destination"], backup_path)
|
|
|
+ else:
|
|
|
+ raise RuntimeError(f"reconciliation unit backup is missing: {backup_path}")
|
|
|
+ unit_changed = (
|
|
|
+ not unit["destination"].is_file()
|
|
|
+ or unit["destination"].read_bytes() != unit["content"]
|
|
|
+ )
|
|
|
+ if unit_changed:
|
|
|
+ atomic_install_bytes(unit["content"], unit["destination"])
|
|
|
+
|
|
|
+ for record in records.values():
|
|
|
+ record.pop("previous_installed_sha256", None)
|
|
|
+ managed.pop("pending_install", None)
|
|
|
+ registry["mod_host_reconcile"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+
|
|
|
+ timer_name = units["timer"]["name"]
|
|
|
+ timer_changed = (
|
|
|
+ prior_files.get("timer", {}).get("installed_sha256")
|
|
|
+ != records["timer"]["installed_sha256"]
|
|
|
+ )
|
|
|
+ run(["systemctl", "--user", "daemon-reload"])
|
|
|
+ run(["systemctl", "--user", "enable", timer_name])
|
|
|
+ run(
|
|
|
+ [
|
|
|
+ "systemctl",
|
|
|
+ "--user",
|
|
|
+ "restart" if timer_changed else "start",
|
|
|
+ timer_name,
|
|
|
+ ]
|
|
|
+ )
|
|
|
+ run(["systemctl", "--user", "is-active", "--quiet", timer_name])
|
|
|
+ print(f"Installed and activated Rigforge PipeWire reconciliation timer ({timer_name}).")
|
|
|
+
|
|
|
+
|
|
|
+def uninstall_mod_host_reconcile_units(config, registry):
|
|
|
+ managed = registry.get("mod_host_reconcile")
|
|
|
+ if managed is None:
|
|
|
+ return
|
|
|
+ units = render_mod_host_reconcile_units(config)
|
|
|
+ records = managed.get("files")
|
|
|
+ if not isinstance(records, dict) or set(records) != set(units):
|
|
|
+ raise RuntimeError("managed reconciliation unit registry has an unexpected file set")
|
|
|
+
|
|
|
+ for key, unit in units.items():
|
|
|
+ record = records[key]
|
|
|
+ destination = unit["destination"]
|
|
|
+ validate_user_destination(destination)
|
|
|
+ if record.get("destination") != str(destination):
|
|
|
+ raise RuntimeError(f"recorded reconciliation unit path changed: {key}")
|
|
|
+ if destination.exists() and not destination.is_file():
|
|
|
+ raise RuntimeError(f"reconciliation unit destination has the wrong type: {destination}")
|
|
|
+ backup_path = validate_backup_path(record.get("backup"), destination)
|
|
|
+ if backup_path is not None and backup_path.exists():
|
|
|
+ if not backup_path.is_file() or file_sha256(backup_path) != record.get("backup_sha256"):
|
|
|
+ raise RuntimeError(f"recorded reconciliation unit backup changed locally: {backup_path}")
|
|
|
+ elif backup_path is not None and not managed.get("pending_uninstall"):
|
|
|
+ raise RuntimeError(f"recorded reconciliation unit backup is missing: {backup_path}")
|
|
|
+ if (backup_path is None) != (record.get("backup_sha256") is None):
|
|
|
+ raise RuntimeError(f"invalid reconciliation unit backup record: {key}")
|
|
|
+
|
|
|
+ current_digest = file_sha256(destination)
|
|
|
+ recoverable_digests = {
|
|
|
+ record.get("installed_sha256"),
|
|
|
+ record.get("backup_sha256"),
|
|
|
+ None,
|
|
|
+ }
|
|
|
+ if managed.get("pending_uninstall"):
|
|
|
+ if current_digest not in recoverable_digests:
|
|
|
+ raise RuntimeError(f"unexpected file blocks reconciliation unit uninstall: {destination}")
|
|
|
+ elif current_digest != record.get("installed_sha256"):
|
|
|
+ raise RuntimeError(f"managed reconciliation unit changed locally; refusing removal: {destination}")
|
|
|
+
|
|
|
+ managed["pending_uninstall"] = True
|
|
|
+ registry["mod_host_reconcile"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+
|
|
|
+ run(["systemctl", "--user", "disable", "--now", config["reconcile_timer"]])
|
|
|
+ run(["systemctl", "--user", "stop", config["reconcile_service"]])
|
|
|
+
|
|
|
+ for key, unit in units.items():
|
|
|
+ record = records[key]
|
|
|
+ destination = unit["destination"]
|
|
|
+ backup_path = validate_backup_path(record.get("backup"), destination)
|
|
|
+ current_digest = file_sha256(destination)
|
|
|
+ if backup_path is not None and current_digest == record.get("backup_sha256"):
|
|
|
+ if backup_path.exists():
|
|
|
+ backup_path.unlink()
|
|
|
+ continue
|
|
|
+ if current_digest == record.get("installed_sha256"):
|
|
|
+ destination.unlink()
|
|
|
+ elif current_digest is not None:
|
|
|
+ raise RuntimeError(f"unexpected file blocks reconciliation unit uninstall: {destination}")
|
|
|
+ if backup_path is not None:
|
|
|
+ if backup_path.exists():
|
|
|
+ os.replace(backup_path, destination)
|
|
|
+ elif file_sha256(destination) != record.get("backup_sha256"):
|
|
|
+ raise RuntimeError(f"recorded reconciliation unit backup is missing: {backup_path}")
|
|
|
+
|
|
|
+ run(["systemctl", "--user", "daemon-reload"])
|
|
|
+ registry.pop("mod_host_reconcile")
|
|
|
+ write_registry(registry)
|
|
|
+ print("Rigforge PipeWire reconciliation timer and units removed.")
|
|
|
+
|
|
|
+
|
|
|
def mod_host_runtime_digest(config):
|
|
|
state_path = ROOT / "config" / "desired-state.yaml"
|
|
|
script_path = ROOT / config["configure_script"]
|
|
|
@@ -918,6 +1175,8 @@ def deploy_mod_host(action, config, registry):
|
|
|
registry["mod_host"] = managed
|
|
|
write_registry(registry)
|
|
|
|
|
|
+ uninstall_mod_host_reconcile_units(config, registry)
|
|
|
+
|
|
|
destination_digest = (
|
|
|
hashlib.sha256(destination.read_bytes()).hexdigest()
|
|
|
if destination.is_file()
|
|
|
@@ -1054,6 +1313,7 @@ def deploy_mod_host(action, config, registry):
|
|
|
managed.pop("pending_activation", None)
|
|
|
registry["mod_host"] = managed
|
|
|
write_registry(registry)
|
|
|
+ install_mod_host_reconcile_units(config, registry)
|
|
|
print(f"Installed and activated Rigforge mod-host user service at {destination}.")
|
|
|
|
|
|
|