|
|
@@ -38,12 +38,17 @@ def load_deployment():
|
|
|
namlv2 = deployment.get("namlv2")
|
|
|
pipewire = deployment.get("pipewire")
|
|
|
reconcile = deployment.get("reconcile")
|
|
|
+ midi_bridge = deployment.get("midi_bridge")
|
|
|
if (
|
|
|
not isinstance(namlv2, dict)
|
|
|
or not isinstance(pipewire, dict)
|
|
|
or not isinstance(reconcile, dict)
|
|
|
+ or not isinstance(midi_bridge, dict)
|
|
|
):
|
|
|
- raise ValueError("deployment.namlv2, deployment.pipewire, and deployment.reconcile must be mappings")
|
|
|
+ raise ValueError(
|
|
|
+ "deployment.namlv2, deployment.pipewire, deployment.reconcile, "
|
|
|
+ "and deployment.midi_bridge must be mappings"
|
|
|
+ )
|
|
|
for component, archive_format in PLUGIN_ARCHIVES.items():
|
|
|
plugin = deployment.get(component)
|
|
|
if not isinstance(plugin, dict):
|
|
|
@@ -879,6 +884,36 @@ def render_reconcile_units(config):
|
|
|
}
|
|
|
|
|
|
|
|
|
+def render_midi_bridge_service(config):
|
|
|
+ repository_root = ROOT.resolve()
|
|
|
+ state_file = repository_root / "config" / "desired-state.yaml"
|
|
|
+ bridge_script = repository_root / config["bridge_script"]
|
|
|
+ service_template = repository_root / config["service_source"]
|
|
|
+ for description, path in (
|
|
|
+ ("MIDI action bridge script", bridge_script),
|
|
|
+ ("MIDI bridge service template", service_template),
|
|
|
+ ):
|
|
|
+ if not path.is_file():
|
|
|
+ raise RuntimeError(f"{description} is missing: {path}")
|
|
|
+
|
|
|
+ replacements = {
|
|
|
+ "@PYTHON@": systemd_quote(config["python_path"]),
|
|
|
+ "@BRIDGE_SCRIPT@": systemd_quote(bridge_script),
|
|
|
+ "@DESIRED_STATE@": systemd_quote(state_file),
|
|
|
+ "@REPOSITORY_ROOT@": systemd_quote(repository_root),
|
|
|
+ }
|
|
|
+ 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 MIDI bridge service template")
|
|
|
+ return {
|
|
|
+ "name": config["user_service"],
|
|
|
+ "destination": Path.home() / config["service_destination"],
|
|
|
+ "content": 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)
|
|
|
@@ -892,6 +927,178 @@ def atomic_install_bytes(content, destination):
|
|
|
os.unlink(temporary_name)
|
|
|
|
|
|
|
|
|
+def deploy_midi_bridge(action, config, registry):
|
|
|
+ unit = render_midi_bridge_service(config)
|
|
|
+ destination = unit["destination"]
|
|
|
+ service_name = unit["name"]
|
|
|
+ validate_user_destination(destination)
|
|
|
+ if destination.exists() and not destination.is_file():
|
|
|
+ raise RuntimeError(f"MIDI bridge service destination has the wrong type: {destination}")
|
|
|
+ managed = registry.get("midi_bridge")
|
|
|
+
|
|
|
+ if action == "reload":
|
|
|
+ if managed is None:
|
|
|
+ raise RuntimeError("MIDI bridge is not managed; install midi-bridge before reloading it")
|
|
|
+ if managed.get("pending_uninstall"):
|
|
|
+ raise RuntimeError("MIDI bridge uninstall is incomplete; retry uninstall midi-bridge first")
|
|
|
+ if not destination.is_file():
|
|
|
+ raise RuntimeError(f"managed MIDI bridge service file is missing: {destination}")
|
|
|
+ if file_sha256(destination) != managed.get("installed_sha256"):
|
|
|
+ raise RuntimeError(f"managed MIDI bridge service file changed locally: {destination}")
|
|
|
+ run(["systemctl", "--user", "restart", service_name])
|
|
|
+ run(["systemctl", "--user", "is-active", "--quiet", service_name])
|
|
|
+ print("Rigforge MIDI bridge user service restarted.")
|
|
|
+ return
|
|
|
+
|
|
|
+ if action == "uninstall":
|
|
|
+ if managed is None:
|
|
|
+ print("No managed MIDI bridge service to uninstall.")
|
|
|
+ return
|
|
|
+ backup_path = validate_backup_path(managed.get("backup"), destination)
|
|
|
+ backup_digest = managed.get("backup_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 MIDI bridge service backup changed locally: {backup_path}")
|
|
|
+ elif backup_path is not None and not managed.get("pending_uninstall"):
|
|
|
+ raise RuntimeError(f"recorded MIDI bridge service backup is missing: {backup_path}")
|
|
|
+ if (backup_path is None) != (backup_digest is None):
|
|
|
+ raise RuntimeError("invalid MIDI bridge service backup record")
|
|
|
+
|
|
|
+ current_digest = file_sha256(destination)
|
|
|
+ recoverable_digests = {managed.get("installed_sha256"), backup_digest, None}
|
|
|
+ if managed.get("pending_uninstall"):
|
|
|
+ if current_digest not in recoverable_digests:
|
|
|
+ raise RuntimeError(f"unexpected file blocks MIDI bridge uninstall: {destination}")
|
|
|
+ elif current_digest != managed.get("installed_sha256"):
|
|
|
+ raise RuntimeError(
|
|
|
+ f"managed MIDI bridge service file changed locally; refusing removal: {destination}"
|
|
|
+ )
|
|
|
+ if (
|
|
|
+ backup_path is not None
|
|
|
+ and not backup_path.exists()
|
|
|
+ and current_digest == managed.get("installed_sha256")
|
|
|
+ ):
|
|
|
+ raise RuntimeError(
|
|
|
+ f"recorded MIDI bridge service backup is missing; refusing removal: {backup_path}"
|
|
|
+ )
|
|
|
+
|
|
|
+ managed["pending_uninstall"] = True
|
|
|
+ registry["midi_bridge"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+ run(["systemctl", "--user", "disable", "--now", service_name])
|
|
|
+ if backup_path is not None and current_digest == backup_digest:
|
|
|
+ if backup_path.exists():
|
|
|
+ backup_path.unlink()
|
|
|
+ else:
|
|
|
+ if current_digest == managed.get("installed_sha256"):
|
|
|
+ destination.unlink()
|
|
|
+ elif current_digest is not None:
|
|
|
+ raise RuntimeError(f"unexpected file blocks MIDI bridge uninstall: {destination}")
|
|
|
+ if backup_path is not None:
|
|
|
+ if backup_path.exists():
|
|
|
+ os.replace(backup_path, destination)
|
|
|
+ elif file_sha256(destination) != backup_digest:
|
|
|
+ raise RuntimeError(f"recorded MIDI bridge service backup is missing: {backup_path}")
|
|
|
+ run(["systemctl", "--user", "daemon-reload"])
|
|
|
+ registry.pop("midi_bridge")
|
|
|
+ write_registry(registry)
|
|
|
+ print("Rigforge MIDI bridge user service removed; prior unit restored if present.")
|
|
|
+ return
|
|
|
+
|
|
|
+ content = unit["content"]
|
|
|
+ installed_digest = hashlib.sha256(content).hexdigest()
|
|
|
+ if managed is not None and managed.get("pending_uninstall"):
|
|
|
+ raise RuntimeError("MIDI bridge uninstall is incomplete; retry uninstall midi-bridge first")
|
|
|
+ if managed is None:
|
|
|
+ if user_unit_is_active(service_name) or user_unit_is_enabled(service_name):
|
|
|
+ raise RuntimeError(
|
|
|
+ f"an unmanaged {service_name} is already active or enabled; "
|
|
|
+ "inspect it before installing Rigforge's unit"
|
|
|
+ )
|
|
|
+ backup_path = (
|
|
|
+ destination.with_name(destination.name + ".rigforge-backup")
|
|
|
+ if destination.is_file()
|
|
|
+ else None
|
|
|
+ )
|
|
|
+ if backup_path is not None and (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 = file_sha256(destination)
|
|
|
+ previous_digest = None
|
|
|
+ pending_install = True
|
|
|
+ else:
|
|
|
+ if managed.get("destination") != str(destination):
|
|
|
+ raise RuntimeError("recorded MIDI bridge service path changed")
|
|
|
+ backup_path = validate_backup_path(managed.get("backup"), destination)
|
|
|
+ backup = str(backup_path) if backup_path is not None else None
|
|
|
+ backup_digest = managed.get("backup_sha256")
|
|
|
+ if (backup_path is None) != (backup_digest is None):
|
|
|
+ raise RuntimeError("invalid MIDI bridge service backup record")
|
|
|
+ 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 MIDI bridge service backup changed locally: {backup_path}")
|
|
|
+ elif backup_path is not None and not managed.get("pending_install"):
|
|
|
+ raise RuntimeError(f"recorded MIDI bridge service backup is missing: {backup_path}")
|
|
|
+ previous_digest = (
|
|
|
+ managed.get("previous_installed_sha256")
|
|
|
+ if managed.get("pending_install")
|
|
|
+ else managed.get("installed_sha256")
|
|
|
+ )
|
|
|
+ current_digest = file_sha256(destination)
|
|
|
+ if managed.get("pending_install"):
|
|
|
+ if current_digest not in {previous_digest, backup_digest, None}:
|
|
|
+ raise RuntimeError(f"MIDI bridge service changed locally during interrupted install: {destination}")
|
|
|
+ elif current_digest != previous_digest:
|
|
|
+ raise RuntimeError(f"managed MIDI bridge service file changed locally: {destination}")
|
|
|
+ pending_install = current_digest != installed_digest
|
|
|
+
|
|
|
+ unit_changed = file_sha256(destination) != installed_digest
|
|
|
+ restart_required = managed is None or unit_changed or bool(
|
|
|
+ managed and managed.get("pending_activation")
|
|
|
+ )
|
|
|
+ managed = managed or {}
|
|
|
+ managed.update({
|
|
|
+ "destination": str(destination),
|
|
|
+ "installed_sha256": installed_digest,
|
|
|
+ "previous_installed_sha256": previous_digest,
|
|
|
+ "backup": backup,
|
|
|
+ "backup_sha256": backup_digest,
|
|
|
+ "pending_install": pending_install,
|
|
|
+ "pending_activation": True,
|
|
|
+ })
|
|
|
+ registry["midi_bridge"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+
|
|
|
+ if backup_path is not None:
|
|
|
+ if backup_path.exists():
|
|
|
+ if not backup_path.is_file() or file_sha256(backup_path) != backup_digest:
|
|
|
+ raise RuntimeError(f"recorded MIDI bridge service backup changed locally: {backup_path}")
|
|
|
+ elif file_sha256(destination) == backup_digest:
|
|
|
+ shutil.copy2(destination, backup_path)
|
|
|
+ else:
|
|
|
+ raise RuntimeError(f"recorded MIDI bridge service backup is missing: {backup_path}")
|
|
|
+ if unit_changed:
|
|
|
+ atomic_install_bytes(content, destination)
|
|
|
+
|
|
|
+ managed.pop("previous_installed_sha256", None)
|
|
|
+ managed.pop("pending_install", None)
|
|
|
+ registry["midi_bridge"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+ run(["systemctl", "--user", "daemon-reload"])
|
|
|
+ run(["systemctl", "--user", "enable", service_name])
|
|
|
+ run([
|
|
|
+ "systemctl",
|
|
|
+ "--user",
|
|
|
+ "restart" if restart_required else "start",
|
|
|
+ service_name,
|
|
|
+ ])
|
|
|
+ run(["systemctl", "--user", "is-active", "--quiet", service_name])
|
|
|
+ managed.pop("pending_activation", None)
|
|
|
+ registry["midi_bridge"] = managed
|
|
|
+ write_registry(registry)
|
|
|
+ print(f"Installed and activated Rigforge MIDI bridge user service at {destination}.")
|
|
|
+
|
|
|
+
|
|
|
def file_sha256(path):
|
|
|
return hashlib.sha256(path.read_bytes()).hexdigest() if path.is_file() else None
|
|
|
|
|
|
@@ -1372,7 +1579,15 @@ def main(argv=None):
|
|
|
parser.add_argument("action", choices=("install", "uninstall", "reload"))
|
|
|
parser.add_argument(
|
|
|
"target",
|
|
|
- choices=("namlv2", "lsp", "dragonfly-reverb", "pipewire", "mod-host", "reconcile"),
|
|
|
+ choices=(
|
|
|
+ "namlv2",
|
|
|
+ "lsp",
|
|
|
+ "dragonfly-reverb",
|
|
|
+ "pipewire",
|
|
|
+ "mod-host",
|
|
|
+ "reconcile",
|
|
|
+ "midi-bridge",
|
|
|
+ ),
|
|
|
)
|
|
|
args = parser.parse_args(argv)
|
|
|
try:
|
|
|
@@ -1390,6 +1605,8 @@ def main(argv=None):
|
|
|
deploy_mod_host(args.action, deployment["mod_host"], registry)
|
|
|
elif args.target == "reconcile":
|
|
|
deploy_reconcile(args.action, deployment["reconcile"], registry)
|
|
|
+ elif args.target == "midi-bridge":
|
|
|
+ deploy_midi_bridge(args.action, deployment["midi_bridge"], registry)
|
|
|
else:
|
|
|
component = "dragonfly_reverb" if args.target == "dragonfly-reverb" else args.target
|
|
|
deploy_lv2_archives(args.action, component, deployment[component], registry)
|