|
|
@@ -4,6 +4,7 @@ import json
|
|
|
import os
|
|
|
import re
|
|
|
import shutil
|
|
|
+import socket
|
|
|
import subprocess
|
|
|
import sys
|
|
|
import tarfile
|
|
|
@@ -789,10 +790,280 @@ def deploy_namlv2(action, config, registry):
|
|
|
print(f"Downloaded archive SHA-256 (identity only, no publisher checksum): {archive_digest}")
|
|
|
|
|
|
|
|
|
+def systemd_quote(value):
|
|
|
+ value = str(value).replace("%", "%%").replace("\\", "\\\\").replace('"', '\\"')
|
|
|
+ return f'"{value}"'
|
|
|
+
|
|
|
+
|
|
|
+def render_mod_host_service(config):
|
|
|
+ binary = ROOT / config["binary_source"]
|
|
|
+ service_template = ROOT / config["service_source"]
|
|
|
+ configure_script = ROOT / config["configure_script"]
|
|
|
+ state_file = ROOT / "config" / "desired-state.yaml"
|
|
|
+ if not binary.is_file() or not os.access(binary, os.X_OK):
|
|
|
+ raise RuntimeError(f"ARM64 mod-host artifact is missing or not executable: {binary}")
|
|
|
+ if not service_template.is_file():
|
|
|
+ raise RuntimeError(f"mod-host service template is missing: {service_template}")
|
|
|
+ if not configure_script.is_file():
|
|
|
+ raise RuntimeError(f"mod-host startup configurator is missing: {configure_script}")
|
|
|
+ replacements = {
|
|
|
+ "@PW_JACK@": systemd_quote(config["pw_jack_path"]),
|
|
|
+ "@MOD_HOST_BINARY@": systemd_quote(binary.resolve()),
|
|
|
+ "@SOCKET_PORT@": str(config["socket_port"]),
|
|
|
+ "@PYTHON@": systemd_quote(config["python_path"]),
|
|
|
+ "@CONFIGURE_SCRIPT@": systemd_quote(configure_script.resolve()),
|
|
|
+ "@DESIRED_STATE@": systemd_quote(state_file.resolve()),
|
|
|
+ "@REPOSITORY_ROOT@": systemd_quote(ROOT.resolve()),
|
|
|
+ }
|
|
|
+ content = service_template.read_text(encoding="utf-8")
|
|
|
+ for placeholder, replacement in replacements.items():
|
|
|
+ content = content.replace(placeholder, replacement)
|
|
|
+ if re.search(r"@[A-Z_]+@", content):
|
|
|
+ raise RuntimeError("unexpanded placeholder remains in mod-host service template")
|
|
|
+ return content.encode("utf-8")
|
|
|
+
|
|
|
+
|
|
|
+def mod_host_runtime_digest(config):
|
|
|
+ state_path = ROOT / "config" / "desired-state.yaml"
|
|
|
+ script_path = ROOT / config["configure_script"]
|
|
|
+ binary_path = ROOT / config["binary_source"]
|
|
|
+ with state_path.open(encoding="utf-8") as stream:
|
|
|
+ state = yaml.safe_load(stream)
|
|
|
+ files = [state_path, script_path, binary_path]
|
|
|
+ for processor in state["routing"]["mod_host_chains"]["guitar"]["processors"]:
|
|
|
+ asset_path = PurePosixPath(processor["asset_path"])
|
|
|
+ if asset_path.is_absolute() or ".." in asset_path.parts:
|
|
|
+ raise RuntimeError(f"unsafe mod-host asset path: {processor['asset_path']!r}")
|
|
|
+ resolved = (ROOT / Path(*asset_path.parts)).resolve()
|
|
|
+ if not resolved.is_relative_to(ROOT.resolve()) or not resolved.is_file():
|
|
|
+ raise RuntimeError(f"declared mod-host asset is missing or escapes the repository: {resolved}")
|
|
|
+ files.append(resolved)
|
|
|
+
|
|
|
+ digest = hashlib.sha256()
|
|
|
+ for path in files:
|
|
|
+ if not path.is_file():
|
|
|
+ raise RuntimeError(f"mod-host runtime input is missing: {path}")
|
|
|
+ digest.update(str(path.relative_to(ROOT.resolve())).encode("utf-8") + b"\0")
|
|
|
+ digest.update(path.read_bytes())
|
|
|
+ digest.update(b"\0")
|
|
|
+ return digest.hexdigest()
|
|
|
+
|
|
|
+
|
|
|
+def mod_host_port_is_open(port):
|
|
|
+ try:
|
|
|
+ with socket.create_connection(("127.0.0.1", port), timeout=0.25):
|
|
|
+ return True
|
|
|
+ except OSError:
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
+def mod_host_unit_is_active(service_name):
|
|
|
+ result = subprocess.run(
|
|
|
+ ["systemctl", "--user", "is-active", "--quiet", service_name],
|
|
|
+ check=False,
|
|
|
+ stdout=subprocess.DEVNULL,
|
|
|
+ stderr=subprocess.DEVNULL,
|
|
|
+ )
|
|
|
+ return result.returncode == 0
|
|
|
+
|
|
|
+
|
|
|
+def mod_host_unit_is_enabled(service_name):
|
|
|
+ result = subprocess.run(
|
|
|
+ ["systemctl", "--user", "is-enabled", "--quiet", service_name],
|
|
|
+ check=False,
|
|
|
+ stdout=subprocess.DEVNULL,
|
|
|
+ stderr=subprocess.DEVNULL,
|
|
|
+ )
|
|
|
+ return result.returncode == 0
|
|
|
+
|
|
|
+
|
|
|
+def deploy_mod_host(action, config, registry):
|
|
|
+ destination = Path.home() / config["service_destination"]
|
|
|
+ validate_user_destination(destination)
|
|
|
+ service_name = config["user_service"]
|
|
|
+ managed = registry.get("mod_host")
|
|
|
+
|
|
|
+ if action == "reload":
|
|
|
+ if managed is None:
|
|
|
+ raise RuntimeError("mod-host is not managed; install mod-host before reloading it")
|
|
|
+ if managed.get("pending_uninstall"):
|
|
|
+ raise RuntimeError("mod-host uninstall is incomplete; retry uninstall mod-host first")
|
|
|
+ if not destination.is_file():
|
|
|
+ raise RuntimeError(f"managed mod-host service file is missing: {destination}")
|
|
|
+ digest = hashlib.sha256(destination.read_bytes()).hexdigest()
|
|
|
+ if digest != managed.get("installed_sha256"):
|
|
|
+ raise RuntimeError(f"managed mod-host service file changed locally: {destination}")
|
|
|
+ run(["systemctl", "--user", "restart", service_name])
|
|
|
+ run(["systemctl", "--user", "is-active", "--quiet", service_name])
|
|
|
+ print("Rigforge mod-host user service restarted.")
|
|
|
+ return
|
|
|
+
|
|
|
+ if action == "uninstall":
|
|
|
+ if managed is None:
|
|
|
+ print("No managed mod-host service to uninstall.")
|
|
|
+ return
|
|
|
+ backup_path = validate_backup_path(managed.get("backup"), destination)
|
|
|
+ if backup_path is not None and backup_path.exists():
|
|
|
+ if not backup_path.is_file():
|
|
|
+ raise RuntimeError(f"recorded mod-host service backup has the wrong type: {backup_path}")
|
|
|
+ if hashlib.sha256(backup_path.read_bytes()).hexdigest() != managed.get("backup_sha256"):
|
|
|
+ raise RuntimeError(f"recorded mod-host service backup changed locally: {backup_path}")
|
|
|
+ if not managed.get("pending_uninstall"):
|
|
|
+ if not destination.is_file():
|
|
|
+ raise RuntimeError(f"managed mod-host service file is missing: {destination}")
|
|
|
+ digest = hashlib.sha256(destination.read_bytes()).hexdigest()
|
|
|
+ if digest != managed.get("installed_sha256"):
|
|
|
+ raise RuntimeError(f"managed mod-host service file changed locally; refusing removal: {destination}")
|
|
|
+ managed["pending_uninstall"] = True
|
|
|
+ registry["mod_host"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+
|
|
|
+ destination_digest = (
|
|
|
+ hashlib.sha256(destination.read_bytes()).hexdigest()
|
|
|
+ if destination.is_file()
|
|
|
+ else None
|
|
|
+ )
|
|
|
+ backup_digest = (
|
|
|
+ hashlib.sha256(backup_path.read_bytes()).hexdigest()
|
|
|
+ if backup_path is not None and backup_path.is_file()
|
|
|
+ else None
|
|
|
+ )
|
|
|
+ installed_digest = managed.get("installed_sha256")
|
|
|
+ already_restored = (
|
|
|
+ backup_path is not None
|
|
|
+ and destination_digest == managed.get("backup_sha256")
|
|
|
+ )
|
|
|
+ already_removed = destination_digest is None and backup_digest is None and backup_path is None
|
|
|
+ if not already_restored and not already_removed:
|
|
|
+ if destination_digest not in {installed_digest, None}:
|
|
|
+ raise RuntimeError(f"unexpected file blocks mod-host uninstall: {destination}")
|
|
|
+ run(["systemctl", "--user", "disable", "--now", service_name])
|
|
|
+ if destination_digest == installed_digest:
|
|
|
+ destination.unlink()
|
|
|
+ if backup_path is not None and backup_path.exists():
|
|
|
+ os.replace(backup_path, destination)
|
|
|
+ elif backup_path is not None:
|
|
|
+ raise RuntimeError(f"recorded mod-host service backup is missing: {backup_path}")
|
|
|
+ run(["systemctl", "--user", "daemon-reload"])
|
|
|
+ registry.pop("mod_host")
|
|
|
+ write_registry(registry)
|
|
|
+ print("Rigforge mod-host user service removed; prior unit state restored if present.")
|
|
|
+ return
|
|
|
+
|
|
|
+ # Install is intentionally an explicit target, not part of "all": it
|
|
|
+ # activates the guitar processing chain and can make audio audible.
|
|
|
+ content = render_mod_host_service(config)
|
|
|
+ installed_digest = hashlib.sha256(content).hexdigest()
|
|
|
+ runtime_digest = mod_host_runtime_digest(config)
|
|
|
+ previous_runtime_digest = managed.get("runtime_sha256") if managed is not None else None
|
|
|
+ restart_required = managed is None or runtime_digest != previous_runtime_digest
|
|
|
+ if managed is not None:
|
|
|
+ if managed.get("pending_uninstall"):
|
|
|
+ raise RuntimeError("mod-host uninstall is incomplete; retry uninstall mod-host first")
|
|
|
+ backup_path = validate_backup_path(managed.get("backup"), destination)
|
|
|
+ if backup_path is not None:
|
|
|
+ if not backup_path.is_file():
|
|
|
+ raise RuntimeError(f"recorded mod-host service backup is missing: {backup_path}")
|
|
|
+ if hashlib.sha256(backup_path.read_bytes()).hexdigest() != managed.get("backup_sha256"):
|
|
|
+ raise RuntimeError(f"recorded mod-host service backup changed locally: {backup_path}")
|
|
|
+ if not destination.is_file():
|
|
|
+ if not managed.get("pending_install"):
|
|
|
+ raise RuntimeError(f"managed mod-host service file is missing: {destination}")
|
|
|
+ if destination.exists() and not destination.is_file():
|
|
|
+ raise RuntimeError(f"managed mod-host service destination has the wrong type: {destination}")
|
|
|
+ current_digest = (
|
|
|
+ hashlib.sha256(destination.read_bytes()).hexdigest()
|
|
|
+ if destination.is_file()
|
|
|
+ else None
|
|
|
+ )
|
|
|
+ if current_digest is not None and current_digest != managed.get("installed_sha256"):
|
|
|
+ prior_digest = managed.get("backup_sha256")
|
|
|
+ if not managed.get("pending_install") or current_digest != prior_digest:
|
|
|
+ raise RuntimeError(
|
|
|
+ f"managed mod-host service file changed locally; refusing overwrite: {destination}"
|
|
|
+ )
|
|
|
+ elif not destination.exists() and not managed.get("pending_install"):
|
|
|
+ raise RuntimeError(f"managed mod-host service file changed locally; refusing overwrite: {destination}")
|
|
|
+ backup = managed.get("backup")
|
|
|
+ backup_sha256 = managed.get("backup_sha256")
|
|
|
+ else:
|
|
|
+ if mod_host_unit_is_active(service_name) or mod_host_unit_is_enabled(service_name):
|
|
|
+ raise RuntimeError(
|
|
|
+ f"an unmanaged {service_name} is already active or enabled; "
|
|
|
+ "inspect and disable it before installing Rigforge's unit"
|
|
|
+ )
|
|
|
+ if mod_host_port_is_open(config["socket_port"]):
|
|
|
+ raise RuntimeError(
|
|
|
+ f"mod-host control port {config['socket_port']} is already in use; "
|
|
|
+ "stop the unmanaged test process before installing the user service"
|
|
|
+ )
|
|
|
+ backup = None
|
|
|
+ backup_sha256 = None
|
|
|
+ if destination.exists():
|
|
|
+ backup_path = destination.with_name(destination.name + ".rigforge-backup")
|
|
|
+ if backup_path.exists():
|
|
|
+ raise RuntimeError(f"backup path already exists: {backup_path}")
|
|
|
+ shutil.copy2(destination, backup_path)
|
|
|
+ backup = str(backup_path)
|
|
|
+ backup_sha256 = hashlib.sha256(backup_path.read_bytes()).hexdigest()
|
|
|
+
|
|
|
+ unit_changed = not destination.exists() or destination.read_bytes() != content
|
|
|
+ restart_required = (
|
|
|
+ restart_required
|
|
|
+ or unit_changed
|
|
|
+ or (managed is not None and managed.get("pending_activation", False))
|
|
|
+ )
|
|
|
+ if managed is None:
|
|
|
+ managed = {
|
|
|
+ "installed_sha256": installed_digest,
|
|
|
+ "runtime_sha256": runtime_digest,
|
|
|
+ "backup": backup,
|
|
|
+ "backup_sha256": backup_sha256,
|
|
|
+ "pending_install": unit_changed,
|
|
|
+ "pending_activation": True,
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ managed["installed_sha256"] = installed_digest
|
|
|
+ managed["runtime_sha256"] = runtime_digest
|
|
|
+ managed["pending_install"] = unit_changed
|
|
|
+ managed["pending_activation"] = True
|
|
|
+ registry["mod_host"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+ if unit_changed:
|
|
|
+ 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)
|
|
|
+
|
|
|
+ managed.pop("pending_install", None)
|
|
|
+ registry["mod_host"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+ run(["systemctl", "--user", "daemon-reload"])
|
|
|
+ run(["systemctl", "--user", "enable", service_name])
|
|
|
+ if restart_required:
|
|
|
+ run(["systemctl", "--user", "restart", service_name])
|
|
|
+ else:
|
|
|
+ run(["systemctl", "--user", "start", service_name])
|
|
|
+ run(["systemctl", "--user", "is-active", "--quiet", service_name])
|
|
|
+ managed.pop("pending_activation", None)
|
|
|
+ registry["mod_host"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+ print(f"Installed and activated Rigforge mod-host user service at {destination}.")
|
|
|
+
|
|
|
+
|
|
|
def main(argv=None):
|
|
|
parser = argparse.ArgumentParser()
|
|
|
parser.add_argument("action", choices=("install", "uninstall", "reload"))
|
|
|
- parser.add_argument("target", choices=("namlv2", "lsp", "dragonfly-reverb", "pipewire"))
|
|
|
+ parser.add_argument(
|
|
|
+ "target",
|
|
|
+ choices=("namlv2", "lsp", "dragonfly-reverb", "pipewire", "mod-host"),
|
|
|
+ )
|
|
|
args = parser.parse_args(argv)
|
|
|
try:
|
|
|
state = yaml.safe_load((ROOT / "config" / "desired-state.yaml").read_text(encoding="utf-8"))
|
|
|
@@ -805,6 +1076,8 @@ def main(argv=None):
|
|
|
deploy_namlv2(args.action, deployment["namlv2"], registry)
|
|
|
elif args.target == "pipewire":
|
|
|
deploy_pipewire(args.action, deployment["pipewire"], registry, quantum_frames)
|
|
|
+ elif args.target == "mod-host":
|
|
|
+ deploy_mod_host(args.action, deployment["mod_host"], registry)
|
|
|
else:
|
|
|
component = "dragonfly_reverb" if args.target == "dragonfly-reverb" else args.target
|
|
|
deploy_lv2_archives(args.action, component, deployment[component], registry)
|