validate_desired_state.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. import argparse
  2. import sys
  3. from pathlib import Path, PurePosixPath
  4. import yaml
  5. def safe_relative_path(value):
  6. if not isinstance(value, str) or not value:
  7. return False
  8. path = PurePosixPath(value)
  9. return not path.is_absolute() and ".." not in path.parts
  10. def validate_mod_host_chain(routing, source_by_id):
  11. errors = []
  12. chains = routing.get("mod_host_chains")
  13. if not isinstance(chains, dict):
  14. return ["routing.mod_host_chains must be a mapping"]
  15. if set(chains) != {"guitar"}:
  16. errors.append("routing.mod_host_chains must declare the guitar chain exactly once")
  17. chain = chains.get("guitar")
  18. if not isinstance(chain, dict):
  19. return errors + ["routing.mod_host_chains.guitar must be a mapping"]
  20. guitar = source_by_id.get("guitar")
  21. source_port = guitar.get("capture_port") if isinstance(guitar, dict) else None
  22. if chain.get("source_port") != source_port:
  23. errors.append("routing.mod_host_chains.guitar.source_port must match the guitar capture port")
  24. bypass = chain.get("bypass_link")
  25. mix_input = chain.get("mix_input")
  26. if not isinstance(bypass, dict):
  27. errors.append("routing.mod_host_chains.guitar.bypass_link must be a mapping")
  28. elif bypass != {"from": source_port, "to": mix_input}:
  29. errors.append("routing.mod_host_chains.guitar.bypass_link must identify the direct capture-to-mix edge")
  30. if mix_input != "rigforge.direct-mix.input:playback_AUX0":
  31. errors.append("routing.mod_host_chains.guitar.mix_input must be the guitar mixer's AUX0 input")
  32. processors = chain.get("processors")
  33. if not isinstance(processors, list) or len(processors) != 2:
  34. return errors + ["routing.mod_host_chains.guitar.processors must contain NAM followed by one cabinet IR"]
  35. expected_processors = (
  36. {
  37. "id": "amp-head",
  38. "instance": 0,
  39. "plugin_uri": "http://github.com/mikeoliphant/neural-amp-modeler-lv2",
  40. "bundle_path": ".lv2/neural_amp_modeler.lv2",
  41. "file_property": "http://github.com/mikeoliphant/neural-amp-modeler-lv2#model",
  42. "asset_path": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam",
  43. "input_port": "effect_0:input",
  44. "output_port": "effect_0:output",
  45. },
  46. {
  47. "id": "cabinet-ir",
  48. "instance": 1,
  49. "plugin_uri": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono",
  50. "bundle_path": ".lv2/lsp-plugins.lv2",
  51. "file_property": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono/ports#ifn",
  52. "asset_path": "amp_emul/cabinet/VOX_AC30_1964/VOX_AC30_1964_celestion.wav",
  53. "input_port": "effect_1:in",
  54. "output_port": "effect_1:out",
  55. },
  56. )
  57. for index, (processor, expected) in enumerate(zip(processors, expected_processors)):
  58. location = f"routing.mod_host_chains.guitar.processors[{index}]"
  59. if not isinstance(processor, dict):
  60. errors.append(f"{location} must be a mapping")
  61. continue
  62. for key, value in expected.items():
  63. if processor.get(key) != value:
  64. errors.append(f"{location}.{key} must be {value}")
  65. for key in ("bundle_path", "asset_path"):
  66. if not safe_relative_path(processor.get(key)):
  67. errors.append(f"{location}.{key} must be a safe relative path")
  68. if index == 0:
  69. expected_models = {
  70. "crystal_clean_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crystal_Clean_TB.nam",
  71. "edge_of_breakup_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Edge_of_Breakup_TB.nam",
  72. "crunch_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam",
  73. "overdriven_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Overdriven_TB.nam",
  74. }
  75. models = processor.get("available_models")
  76. if not isinstance(models, dict) or models != expected_models:
  77. errors.append(f"{location}.available_models must declare the four approved Vox TB models")
  78. elif any(not safe_relative_path(path) for path in models.values()):
  79. errors.append(f"{location}.available_models paths must be safe relative paths")
  80. elif processor.get("asset_path") not in models.values():
  81. errors.append(f"{location}.asset_path must be one of the declared available_models")
  82. if isinstance(processors[0], dict):
  83. parameters = processors[0].get("parameters")
  84. if not isinstance(parameters, dict):
  85. errors.append("routing.mod_host_chains.guitar.processors[0].parameters must be a mapping")
  86. else:
  87. output_level = parameters.get("output_level")
  88. if (
  89. not isinstance(output_level, (int, float))
  90. or isinstance(output_level, bool)
  91. or not -20.0 <= output_level <= 20.0
  92. ):
  93. errors.append("NAM output_level must be between -20 and 20 dB")
  94. links = routing.get("managed_links")
  95. if isinstance(links, list) and len(processors) == 2:
  96. actual_edges = {
  97. (link.get("from"), link.get("to"))
  98. for link in links
  99. if (
  100. isinstance(link, dict)
  101. and isinstance(link.get("from"), str)
  102. and isinstance(link.get("to"), str)
  103. )
  104. }
  105. if isinstance(processors[0], dict) and isinstance(processors[1], dict):
  106. expected_candidates = (
  107. (source_port, processors[0].get("input_port")),
  108. (processors[0].get("output_port"), processors[1].get("input_port")),
  109. (processors[1].get("output_port"), mix_input),
  110. )
  111. for edge in expected_candidates:
  112. if not all(isinstance(endpoint, str) for endpoint in edge):
  113. continue
  114. if edge not in actual_edges:
  115. errors.append(f"mod-host guitar chain link must be declared: {edge[0]} -> {edge[1]}")
  116. if isinstance(bypass, dict):
  117. bypass_edge = (bypass.get("from"), bypass.get("to"))
  118. if all(isinstance(endpoint, str) for endpoint in bypass_edge) and bypass_edge in actual_edges:
  119. errors.append("the guitar capture-to-mix bypass must not be a managed link")
  120. managed_unlinks = routing.get("managed_unlinks")
  121. unlink_edges = {
  122. (item.get("from"), item.get("to"))
  123. for item in managed_unlinks
  124. if isinstance(item, dict)
  125. } if isinstance(managed_unlinks, list) else set()
  126. if (
  127. all(isinstance(endpoint, str) for endpoint in bypass_edge)
  128. and bypass_edge not in unlink_edges
  129. ):
  130. errors.append("the guitar capture-to-mix bypass must be declared in routing.managed_unlinks")
  131. return errors
  132. def validate_state(state):
  133. errors = []
  134. if not isinstance(state, dict):
  135. return ["document root must be a mapping"]
  136. # Establish the declared source map before validating looper references.
  137. sources = state.get("sources")
  138. if not isinstance(sources, list):
  139. return ["sources must be a list"]
  140. source_by_id = {}
  141. physical_inputs = set()
  142. capture_ports = set()
  143. for index, source in enumerate(sources):
  144. location = f"sources[{index}]"
  145. if not isinstance(source, dict):
  146. errors.append(f"{location} must be a mapping")
  147. continue
  148. source_id = source.get("id")
  149. if not isinstance(source_id, str) or not source_id:
  150. errors.append(f"{location}.id must be a non-empty string")
  151. elif source_id in source_by_id:
  152. errors.append(f"duplicate source id: {source_id}")
  153. else:
  154. source_by_id[source_id] = source
  155. physical_input = source.get("physical_input")
  156. if not isinstance(physical_input, int) or isinstance(physical_input, bool):
  157. errors.append(f"{location}.physical_input must be an integer")
  158. elif physical_input in physical_inputs:
  159. errors.append(f"duplicate physical input: {physical_input}")
  160. else:
  161. physical_inputs.add(physical_input)
  162. capture_port = source.get("capture_port")
  163. if not isinstance(capture_port, str) or not capture_port:
  164. errors.append(f"{location}.capture_port must be a non-empty string")
  165. elif capture_port in capture_ports:
  166. errors.append(f"duplicate capture port: {capture_port}")
  167. else:
  168. capture_ports.add(capture_port)
  169. # Keep the first four-channel milestone tied to its four named instruments.
  170. expected_source_ids = {"guitar", "bass", "voice", "synth"}
  171. if set(source_by_id) != expected_source_ids:
  172. errors.append("source ids must be guitar, bass, voice, and synth exactly once")
  173. if len(sources) != 4:
  174. errors.append(f"exactly four sources are required, got {len(sources)}")
  175. if physical_inputs != {1, 2, 3, 4}:
  176. errors.append("physical inputs must cover 1 through 4 exactly once")
  177. # The required looper is one instance containing one four-channel loop.
  178. system = state.get("system")
  179. components = system.get("components") if isinstance(system, dict) else None
  180. if not isinstance(components, list):
  181. errors.append("system.components must be a list")
  182. components = []
  183. loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"]
  184. if len(loopers) != 1:
  185. errors.append(f"exactly one looper component is required, got {len(loopers)}")
  186. elif loopers[0].get("instance_count") != 1:
  187. errors.append("looper.instance_count must be 1")
  188. elif loopers[0].get("loop_count") != 1:
  189. errors.append("looper.loop_count must be 1")
  190. elif loopers[0].get("channels_per_loop") != 4:
  191. errors.append("looper.channels_per_loop must be 4")
  192. routing = state.get("routing")
  193. if not isinstance(routing, dict):
  194. errors.append("routing must be a mapping")
  195. return errors
  196. looper_inputs = routing.get("looper_inputs")
  197. if not isinstance(looper_inputs, list):
  198. errors.append("routing.looper_inputs must be a list")
  199. looper_inputs = []
  200. # Check that each source is assigned exactly once across all loop channels.
  201. input_channels = set()
  202. input_sources = set()
  203. for index, loop_input in enumerate(looper_inputs):
  204. location = f"routing.looper_inputs[{index}]"
  205. if not isinstance(loop_input, dict):
  206. errors.append(f"{location} must be a mapping")
  207. continue
  208. source_id = loop_input.get("source")
  209. channel = loop_input.get("channel")
  210. if source_id not in source_by_id:
  211. errors.append(f"{location}.source references unknown source: {source_id}")
  212. if source_id in input_sources:
  213. errors.append(f"source is assigned to multiple looper inputs: {source_id}")
  214. input_sources.add(source_id)
  215. if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5):
  216. errors.append(f"{location}.channel must be an integer from 1 through 4")
  217. elif channel in input_channels:
  218. errors.append(f"duplicate looper input channel: {channel}")
  219. else:
  220. input_channels.add(channel)
  221. if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}:
  222. errors.append("looper inputs must cover channels 1 through 4 exactly once")
  223. if input_sources != set(source_by_id):
  224. errors.append("each declared source must map to exactly one looper input")
  225. mix = routing.get("mix")
  226. if not isinstance(mix, dict):
  227. errors.append("routing.mix must be a mapping")
  228. else:
  229. output_port = mix.get("output_port")
  230. if not isinstance(output_port, str) or not output_port:
  231. errors.append("routing.mix.output_port must be a non-empty string")
  232. mix_id = mix.get("id")
  233. if not isinstance(mix_id, str) or not mix_id:
  234. errors.append("routing.mix.id must be a non-empty string")
  235. includes = mix.get("includes")
  236. if not isinstance(includes, list) or "looper_returns" not in includes:
  237. errors.append("routing.mix.includes must include looper_returns")
  238. # Loop returns join the output mix but must never feed the loop inputs.
  239. returns = routing.get("looper_returns")
  240. if not isinstance(returns, dict):
  241. errors.append("routing.looper_returns must be a mapping")
  242. elif returns.get("feed_looper_inputs") is not False:
  243. errors.append("routing.looper_returns.feed_looper_inputs must be false")
  244. errors.extend(validate_mod_host_chain(routing, source_by_id))
  245. controls = state.get("controls")
  246. if not isinstance(controls, dict):
  247. errors.append("controls must be a mapping")
  248. else:
  249. midi_bridge = controls.get("midi_bridge")
  250. if not isinstance(midi_bridge, dict):
  251. errors.append("controls.midi_bridge must be a mapping")
  252. else:
  253. if midi_bridge.get("bindings_file") != "config/midi-bindings.yaml":
  254. errors.append("controls.midi_bridge.bindings_file must be config/midi-bindings.yaml")
  255. if (
  256. not isinstance(midi_bridge.get("input_port_name_contains"), str)
  257. or not midi_bridge["input_port_name_contains"].strip()
  258. ):
  259. errors.append("controls.midi_bridge.input_port_name_contains must be non-empty")
  260. mod_host = midi_bridge.get("mod_host")
  261. if not isinstance(mod_host, dict):
  262. errors.append("controls.midi_bridge.mod_host must be a mapping")
  263. else:
  264. if mod_host.get("host") != "127.0.0.1":
  265. errors.append("controls.midi_bridge.mod_host.host must be 127.0.0.1")
  266. port = mod_host.get("port")
  267. if (
  268. not isinstance(port, int)
  269. or isinstance(port, bool)
  270. or not 1 <= port <= 65535
  271. ):
  272. errors.append("controls.midi_bridge.mod_host.port must be a valid TCP port")
  273. elif isinstance(deployment := state.get("deployment"), dict):
  274. host_deployment = deployment.get("mod_host")
  275. if (
  276. isinstance(host_deployment, dict)
  277. and host_deployment.get("socket_port") != port
  278. ):
  279. errors.append(
  280. "controls.midi_bridge.mod_host.port must match deployment.mod_host.socket_port"
  281. )
  282. connections = state.get("connections", [])
  283. if not isinstance(connections, list):
  284. errors.append("connections must be a list")
  285. connections = []
  286. # An explicitly-forbidden connection documents policy; any other status is actionable.
  287. for index, connection in enumerate(connections):
  288. if not isinstance(connection, dict):
  289. errors.append(f"connections[{index}] must be a mapping")
  290. continue
  291. if (
  292. connection.get("from") == "looper_returns"
  293. and connection.get("to") in {
  294. "looper_inputs",
  295. "looper_input_1",
  296. "looper_input_2",
  297. "looper_input_3",
  298. "looper_input_4",
  299. }
  300. and connection.get("status") != "explicitly-forbidden"
  301. ):
  302. errors.append(f"connections[{index}] routes looper returns into looper inputs")
  303. deployment = state.get("deployment")
  304. if not isinstance(deployment, dict):
  305. errors.append("deployment must be a mapping")
  306. else:
  307. for component, repository, asset, archive_format, bundle_paths in (
  308. (
  309. "lsp",
  310. "lsp-plugins/lsp-plugins",
  311. "lsp-plugins-{tag}-Linux-aarch64.7z",
  312. "7z",
  313. {"lsp-plugins.lv2": "lsp-plugins-{tag}-Linux-aarch64/LV2/lsp-plugins.lv2"},
  314. ),
  315. (
  316. "dragonfly_reverb",
  317. "michaelwillis/dragonfly-reverb",
  318. "dragonfly-reverb-{tag}-linux-arm64.tar.xz",
  319. "tar.xz",
  320. {
  321. "DragonflyEarlyReflections.lv2": "dragonfly-reverb-{tag}/DragonflyEarlyReflections.lv2",
  322. "DragonflyHallReverb.lv2": "dragonfly-reverb-{tag}/DragonflyHallReverb.lv2",
  323. "DragonflyPlateReverb.lv2": "dragonfly-reverb-{tag}/DragonflyPlateReverb.lv2",
  324. "DragonflyRoomReverb.lv2": "dragonfly-reverb-{tag}/DragonflyRoomReverb.lv2",
  325. },
  326. ),
  327. ):
  328. plugin = deployment.get(component)
  329. if not isinstance(plugin, dict):
  330. errors.append(f"deployment.{component} must be a mapping")
  331. continue
  332. tag = plugin.get("tag")
  333. if not isinstance(tag, str) or not tag or any(
  334. character not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  335. for character in tag
  336. ):
  337. errors.append(f"deployment.{component}.tag must be a simple version tag")
  338. expected_url = f"https://github.com/{repository}/releases/download/{tag}/{asset.format(tag=tag)}"
  339. if plugin.get("archive_url") != expected_url:
  340. errors.append(f"deployment.{component}.archive_url must match its tag and approved release asset")
  341. if plugin.get("archive_format") != archive_format:
  342. errors.append(f"deployment.{component}.archive_format must be {archive_format}")
  343. expected_paths = {
  344. name: path.format(tag=tag) for name, path in bundle_paths.items()
  345. }
  346. if plugin.get("bundle_paths") != expected_paths:
  347. errors.append(f"deployment.{component}.bundle_paths are invalid")
  348. for bundle_name, bundle_path in expected_paths.items():
  349. if bundle_name not in bundle_path:
  350. errors.append(f"deployment.{component}.bundle_paths keys must name their bundle directories")
  351. namlv2 = deployment.get("namlv2")
  352. if not isinstance(namlv2, dict):
  353. errors.append("deployment.namlv2 must be a mapping")
  354. else:
  355. tag = namlv2.get("tag")
  356. allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  357. if not isinstance(tag, str) or not tag or any(
  358. character not in allowed_tag_characters for character in tag
  359. ):
  360. errors.append("deployment.namlv2.tag must be a simple version tag")
  361. archive_url = namlv2.get("archive_url")
  362. expected_url = (
  363. f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/"
  364. "neural_amp_modeler_lv2_rpi5.tgz"
  365. )
  366. if archive_url != expected_url:
  367. errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset")
  368. if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
  369. errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2")
  370. sooperlooper = deployment.get("sooperlooper")
  371. if not isinstance(sooperlooper, dict):
  372. errors.append("deployment.sooperlooper must be a mapping")
  373. else:
  374. source_url = sooperlooper.get("source_url")
  375. if source_url != "https://github.com/abarker/sooperlooper-mods":
  376. errors.append("deployment.sooperlooper.source_url must match the approved GitHub repository")
  377. tag = sooperlooper.get("tag")
  378. if not isinstance(tag, str) or not tag or any(
  379. character not in allowed_tag_characters for character in tag
  380. ):
  381. errors.append("deployment.sooperlooper.tag must be a simple version tag")
  382. mod_host = deployment.get("mod_host")
  383. if not isinstance(mod_host, dict):
  384. errors.append("deployment.mod_host must be a mapping")
  385. else:
  386. source_url = mod_host.get("source_url")
  387. if source_url != "https://github.com/mod-audio/mod-host":
  388. errors.append("deployment.mod_host.source_url must match the approved GitHub repository")
  389. tag = mod_host.get("tag")
  390. if not isinstance(tag, str) or not tag or any(
  391. character not in allowed_tag_characters for character in tag
  392. ):
  393. errors.append("deployment.mod_host.tag must be a simple version tag")
  394. for key, expected in (
  395. ("binary_source", "artifacts/mod-host/arm64/current/mod-host"),
  396. ("service_source", "config/systemd/user/rigforge-mod-host.service.in"),
  397. ("service_destination", ".config/systemd/user/rigforge-mod-host.service"),
  398. ("user_service", "rigforge-mod-host.service"),
  399. ("configure_script", "scripts/configure_mod_host.py"),
  400. ("readiness_script", "scripts/wait_mod_host_ports.py"),
  401. ("pw_jack_path", "/usr/bin/pw-jack"),
  402. ("python_path", "/usr/bin/python3"),
  403. ):
  404. if mod_host.get(key) != expected:
  405. errors.append(f"deployment.mod_host.{key} must be {expected}")
  406. socket_port = mod_host.get("socket_port")
  407. if (
  408. not isinstance(socket_port, int)
  409. or isinstance(socket_port, bool)
  410. or not 1 <= socket_port <= 65535
  411. ):
  412. errors.append("deployment.mod_host.socket_port must be a valid TCP port")
  413. port_wait_timeout = mod_host.get("port_wait_timeout_seconds")
  414. if (
  415. not isinstance(port_wait_timeout, int)
  416. or isinstance(port_wait_timeout, bool)
  417. or not 5 <= port_wait_timeout <= 300
  418. ):
  419. errors.append(
  420. "deployment.mod_host.port_wait_timeout_seconds must be between 5 and 300"
  421. )
  422. reconcile = deployment.get("reconcile")
  423. if not isinstance(reconcile, dict):
  424. errors.append("deployment.reconcile must be a mapping")
  425. else:
  426. for key, expected in (
  427. ("observer_script", "scripts/observe_pipewire.py"),
  428. (
  429. "service_source",
  430. "config/systemd/user/rigforge-pipewire-reconcile.service.in",
  431. ),
  432. (
  433. "service_destination",
  434. ".config/systemd/user/rigforge-pipewire-reconcile.service",
  435. ),
  436. ("user_service", "rigforge-pipewire-reconcile.service"),
  437. (
  438. "timer_source",
  439. "config/systemd/user/rigforge-pipewire-reconcile.timer.in",
  440. ),
  441. (
  442. "timer_destination",
  443. ".config/systemd/user/rigforge-pipewire-reconcile.timer",
  444. ),
  445. ("user_timer", "rigforge-pipewire-reconcile.timer"),
  446. ("python_path", "/usr/bin/python3"),
  447. ):
  448. if reconcile.get(key) != expected:
  449. errors.append(f"deployment.reconcile.{key} must be {expected}")
  450. interval = reconcile.get("interval_seconds")
  451. if (
  452. not isinstance(interval, int)
  453. or isinstance(interval, bool)
  454. or not 5 <= interval <= 300
  455. ):
  456. errors.append(
  457. "deployment.reconcile.interval_seconds must be between 5 and 300"
  458. )
  459. midi_bridge = deployment.get("midi_bridge")
  460. if not isinstance(midi_bridge, dict):
  461. errors.append("deployment.midi_bridge must be a mapping")
  462. else:
  463. for key, expected in (
  464. ("bridge_script", "scripts/midi_action_bridge.py"),
  465. (
  466. "service_source",
  467. "config/systemd/user/rigforge-midi-bridge.service.in",
  468. ),
  469. (
  470. "service_destination",
  471. ".config/systemd/user/rigforge-midi-bridge.service",
  472. ),
  473. ("user_service", "rigforge-midi-bridge.service"),
  474. ("python_path", "/usr/bin/python3"),
  475. ):
  476. if midi_bridge.get(key) != expected:
  477. errors.append(f"deployment.midi_bridge.{key} must be {expected}")
  478. pipewire = deployment.get("pipewire")
  479. if not isinstance(pipewire, dict):
  480. errors.append("deployment.pipewire must be a mapping")
  481. else:
  482. expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  483. if pipewire.get("fragment_source") != expected_fragment_source:
  484. errors.append("deployment.pipewire.fragment_source must name the managed repository fragment")
  485. expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  486. if pipewire.get("fragment_destination") != expected_fragment_destination:
  487. errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path")
  488. if pipewire.get("user_service") != "filter-chain.service":
  489. errors.append("deployment.pipewire.user_service must be filter-chain.service")
  490. return errors
  491. def main(argv=None):
  492. parser = argparse.ArgumentParser()
  493. parser.add_argument(
  494. "state_file",
  495. nargs="?",
  496. type=Path,
  497. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  498. )
  499. args = parser.parse_args(argv)
  500. try:
  501. with args.state_file.open(encoding="utf-8") as state_stream:
  502. state = yaml.safe_load(state_stream)
  503. except (OSError, yaml.YAMLError) as error:
  504. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  505. return 2
  506. errors = validate_state(state)
  507. if errors:
  508. for error in errors:
  509. print(f"ERROR: {error}", file=sys.stderr)
  510. return 1
  511. print(f"Valid desired state: {args.state_file}")
  512. return 0
  513. if __name__ == "__main__":
  514. raise SystemExit(main())