validate_desired_state.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. physical_input = guitar.get("capture_port") if isinstance(guitar, dict) else None
  22. if chain.get("physical_input_port") != physical_input:
  23. errors.append(
  24. "routing.mod_host_chains.guitar.physical_input_port must match the guitar capture port"
  25. )
  26. if chain.get("source_port") != "rigforge.mod-host-input.capture:capture_AUX0":
  27. errors.append(
  28. "routing.mod_host_chains.guitar.source_port must be the mod-ui-visible input boundary"
  29. )
  30. if chain.get("pipewire_input_port") != "rigforge.mod-host-input:playback_AUX0":
  31. errors.append(
  32. "routing.mod_host_chains.guitar.pipewire_input_port must be the Rigforge input sink"
  33. )
  34. if chain.get("pipewire_output_port") != "rigforge.mod-host-output:capture_AUX0":
  35. errors.append(
  36. "routing.mod_host_chains.guitar.pipewire_output_port must be the Rigforge output source"
  37. )
  38. if chain.get("mod_ui_output_port") != "rigforge.mod-host-output.playback:playback_AUX0":
  39. errors.append(
  40. "routing.mod_host_chains.guitar.mod_ui_output_port must be the mod-ui-visible output boundary"
  41. )
  42. bypass = chain.get("bypass_link")
  43. mix_input = chain.get("mix_input")
  44. if not isinstance(bypass, dict):
  45. errors.append("routing.mod_host_chains.guitar.bypass_link must be a mapping")
  46. elif bypass != {"from": physical_input, "to": mix_input}:
  47. errors.append("routing.mod_host_chains.guitar.bypass_link must identify the direct capture-to-mix edge")
  48. if mix_input != "rigforge.direct-mix.input:playback_AUX0":
  49. errors.append("routing.mod_host_chains.guitar.mix_input must be the guitar mixer's AUX0 input")
  50. processors = chain.get("processors")
  51. if not isinstance(processors, list) or len(processors) != 2:
  52. return errors + ["routing.mod_host_chains.guitar.processors must contain NAM followed by one cabinet IR"]
  53. expected_processors = (
  54. {
  55. "id": "amp-head",
  56. "instance": 0,
  57. "plugin_uri": "http://github.com/mikeoliphant/neural-amp-modeler-lv2",
  58. "mod_ui_instance": "/graph/rigforge_nam",
  59. "bundle_path": ".lv2/neural_amp_modeler.lv2",
  60. "file_property": "http://github.com/mikeoliphant/neural-amp-modeler-lv2#model",
  61. "asset_path": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam",
  62. "input_port": "effect_0:input",
  63. "output_port": "effect_0:output",
  64. },
  65. {
  66. "id": "cabinet-ir",
  67. "instance": 1,
  68. "plugin_uri": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono",
  69. "mod_ui_instance": "/graph/rigforge_ir",
  70. "bundle_path": ".lv2/lsp-plugins.lv2",
  71. "file_property": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono/ports#ifn",
  72. "asset_path": "amp_emul/cabinet/VOX_AC30_1964/VOX_AC30_1964_celestion.wav",
  73. "input_port": "effect_1:in",
  74. "output_port": "effect_1:out",
  75. },
  76. )
  77. for index, (processor, expected) in enumerate(zip(processors, expected_processors)):
  78. location = f"routing.mod_host_chains.guitar.processors[{index}]"
  79. if not isinstance(processor, dict):
  80. errors.append(f"{location} must be a mapping")
  81. continue
  82. for key, value in expected.items():
  83. if processor.get(key) != value:
  84. errors.append(f"{location}.{key} must be {value}")
  85. for key in ("bundle_path", "asset_path"):
  86. if not safe_relative_path(processor.get(key)):
  87. errors.append(f"{location}.{key} must be a safe relative path")
  88. if index == 0:
  89. expected_models = {
  90. "crystal_clean_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crystal_Clean_TB.nam",
  91. "edge_of_breakup_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Edge_of_Breakup_TB.nam",
  92. "crunch_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam",
  93. "overdriven_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Overdriven_TB.nam",
  94. }
  95. models = processor.get("available_models")
  96. if not isinstance(models, dict) or models != expected_models:
  97. errors.append(f"{location}.available_models must declare the four approved Vox TB models")
  98. elif any(not safe_relative_path(path) for path in models.values()):
  99. errors.append(f"{location}.available_models paths must be safe relative paths")
  100. elif processor.get("asset_path") not in models.values():
  101. errors.append(f"{location}.asset_path must be one of the declared available_models")
  102. if isinstance(processors[0], dict):
  103. parameters = processors[0].get("parameters")
  104. if not isinstance(parameters, dict):
  105. errors.append("routing.mod_host_chains.guitar.processors[0].parameters must be a mapping")
  106. else:
  107. output_level = parameters.get("output_level")
  108. if (
  109. not isinstance(output_level, (int, float))
  110. or isinstance(output_level, bool)
  111. or not -20.0 <= output_level <= 20.0
  112. ):
  113. errors.append("NAM output_level must be between -20 and 20 dB")
  114. links = routing.get("managed_links")
  115. if isinstance(links, list) and len(processors) == 2:
  116. actual_edges = {
  117. (link.get("from"), link.get("to"))
  118. for link in links
  119. if (
  120. isinstance(link, dict)
  121. and isinstance(link.get("from"), str)
  122. and isinstance(link.get("to"), str)
  123. )
  124. }
  125. if isinstance(processors[0], dict) and isinstance(processors[1], dict):
  126. expected_candidates = (
  127. (physical_input, chain.get("pipewire_input_port")),
  128. (chain.get("pipewire_output_port"), mix_input),
  129. (chain.get("pipewire_output_port"), "sooperlooper:loop0_in_1"),
  130. )
  131. for edge in expected_candidates:
  132. if not all(isinstance(endpoint, str) for endpoint in edge):
  133. continue
  134. if edge not in actual_edges:
  135. errors.append(f"mod-host guitar boundary link must be declared: {edge[0]} -> {edge[1]}")
  136. internal_edge = (
  137. processors[0].get("output_port"),
  138. processors[1].get("input_port"),
  139. )
  140. if internal_edge in actual_edges:
  141. errors.append(
  142. "the NAM-to-IR link is owned by mod-ui and must not be a Rigforge managed link"
  143. )
  144. if isinstance(bypass, dict):
  145. bypass_edge = (bypass.get("from"), bypass.get("to"))
  146. if all(isinstance(endpoint, str) for endpoint in bypass_edge) and bypass_edge in actual_edges:
  147. errors.append("the guitar capture-to-mix bypass must not be a managed link")
  148. managed_unlinks = routing.get("managed_unlinks")
  149. unlink_edges = {
  150. (item.get("from"), item.get("to"))
  151. for item in managed_unlinks
  152. if isinstance(item, dict)
  153. } if isinstance(managed_unlinks, list) else set()
  154. if (
  155. all(isinstance(endpoint, str) for endpoint in bypass_edge)
  156. and bypass_edge not in unlink_edges
  157. ):
  158. errors.append("the guitar capture-to-mix bypass must be declared in routing.managed_unlinks")
  159. return errors
  160. def validate_state(state):
  161. errors = []
  162. if not isinstance(state, dict):
  163. return ["document root must be a mapping"]
  164. # Establish the declared source map before validating looper references.
  165. sources = state.get("sources")
  166. if not isinstance(sources, list):
  167. return ["sources must be a list"]
  168. source_by_id = {}
  169. physical_inputs = set()
  170. capture_ports = set()
  171. for index, source in enumerate(sources):
  172. location = f"sources[{index}]"
  173. if not isinstance(source, dict):
  174. errors.append(f"{location} must be a mapping")
  175. continue
  176. source_id = source.get("id")
  177. if not isinstance(source_id, str) or not source_id:
  178. errors.append(f"{location}.id must be a non-empty string")
  179. elif source_id in source_by_id:
  180. errors.append(f"duplicate source id: {source_id}")
  181. else:
  182. source_by_id[source_id] = source
  183. physical_input = source.get("physical_input")
  184. if not isinstance(physical_input, int) or isinstance(physical_input, bool):
  185. errors.append(f"{location}.physical_input must be an integer")
  186. elif physical_input in physical_inputs:
  187. errors.append(f"duplicate physical input: {physical_input}")
  188. else:
  189. physical_inputs.add(physical_input)
  190. capture_port = source.get("capture_port")
  191. if not isinstance(capture_port, str) or not capture_port:
  192. errors.append(f"{location}.capture_port must be a non-empty string")
  193. elif capture_port in capture_ports:
  194. errors.append(f"duplicate capture port: {capture_port}")
  195. else:
  196. capture_ports.add(capture_port)
  197. # Keep the first four-channel milestone tied to its four named instruments.
  198. expected_source_ids = {"guitar", "bass", "voice", "synth"}
  199. if set(source_by_id) != expected_source_ids:
  200. errors.append("source ids must be guitar, bass, voice, and synth exactly once")
  201. if len(sources) != 4:
  202. errors.append(f"exactly four sources are required, got {len(sources)}")
  203. if physical_inputs != {1, 2, 3, 4}:
  204. errors.append("physical inputs must cover 1 through 4 exactly once")
  205. # The required looper is one instance containing one four-channel loop.
  206. system = state.get("system")
  207. components = system.get("components") if isinstance(system, dict) else None
  208. if not isinstance(components, list):
  209. errors.append("system.components must be a list")
  210. components = []
  211. loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"]
  212. if len(loopers) != 1:
  213. errors.append(f"exactly one looper component is required, got {len(loopers)}")
  214. elif loopers[0].get("instance_count") != 1:
  215. errors.append("looper.instance_count must be 1")
  216. elif loopers[0].get("loop_count") != 1:
  217. errors.append("looper.loop_count must be 1")
  218. elif loopers[0].get("channels_per_loop") != 4:
  219. errors.append("looper.channels_per_loop must be 4")
  220. routing = state.get("routing")
  221. if not isinstance(routing, dict):
  222. errors.append("routing must be a mapping")
  223. return errors
  224. looper_inputs = routing.get("looper_inputs")
  225. if not isinstance(looper_inputs, list):
  226. errors.append("routing.looper_inputs must be a list")
  227. looper_inputs = []
  228. # Check that each source is assigned exactly once across all loop channels.
  229. input_channels = set()
  230. input_sources = set()
  231. for index, loop_input in enumerate(looper_inputs):
  232. location = f"routing.looper_inputs[{index}]"
  233. if not isinstance(loop_input, dict):
  234. errors.append(f"{location} must be a mapping")
  235. continue
  236. source_id = loop_input.get("source")
  237. channel = loop_input.get("channel")
  238. if source_id not in source_by_id:
  239. errors.append(f"{location}.source references unknown source: {source_id}")
  240. if source_id in input_sources:
  241. errors.append(f"source is assigned to multiple looper inputs: {source_id}")
  242. input_sources.add(source_id)
  243. if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5):
  244. errors.append(f"{location}.channel must be an integer from 1 through 4")
  245. elif channel in input_channels:
  246. errors.append(f"duplicate looper input channel: {channel}")
  247. else:
  248. input_channels.add(channel)
  249. if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}:
  250. errors.append("looper inputs must cover channels 1 through 4 exactly once")
  251. if input_sources != set(source_by_id):
  252. errors.append("each declared source must map to exactly one looper input")
  253. mix = routing.get("mix")
  254. if not isinstance(mix, dict):
  255. errors.append("routing.mix must be a mapping")
  256. else:
  257. output_port = mix.get("output_port")
  258. if not isinstance(output_port, str) or not output_port:
  259. errors.append("routing.mix.output_port must be a non-empty string")
  260. mix_id = mix.get("id")
  261. if not isinstance(mix_id, str) or not mix_id:
  262. errors.append("routing.mix.id must be a non-empty string")
  263. includes = mix.get("includes")
  264. if not isinstance(includes, list) or "looper_returns" not in includes:
  265. errors.append("routing.mix.includes must include looper_returns")
  266. # Loop returns join the output mix but must never feed the loop inputs.
  267. returns = routing.get("looper_returns")
  268. if not isinstance(returns, dict):
  269. errors.append("routing.looper_returns must be a mapping")
  270. elif returns.get("feed_looper_inputs") is not False:
  271. errors.append("routing.looper_returns.feed_looper_inputs must be false")
  272. errors.extend(validate_mod_host_chain(routing, source_by_id))
  273. parameters = state.get("parameters")
  274. if not isinstance(parameters, dict):
  275. errors.append("parameters must be a mapping")
  276. else:
  277. for key in ("sample_rate_hz", "quantum_frames"):
  278. value = parameters.get(key)
  279. if (
  280. not isinstance(value, int)
  281. or isinstance(value, bool)
  282. or value <= 0
  283. ):
  284. errors.append(f"parameters.{key} must be a positive integer")
  285. controls = state.get("controls")
  286. if not isinstance(controls, dict):
  287. errors.append("controls must be a mapping")
  288. else:
  289. midi_bridge = controls.get("midi_bridge")
  290. if not isinstance(midi_bridge, dict):
  291. errors.append("controls.midi_bridge must be a mapping")
  292. else:
  293. if midi_bridge.get("bindings_file") != "config/midi-bindings.yaml":
  294. errors.append("controls.midi_bridge.bindings_file must be config/midi-bindings.yaml")
  295. if (
  296. not isinstance(midi_bridge.get("input_port_name_contains"), str)
  297. or not midi_bridge["input_port_name_contains"].strip()
  298. ):
  299. errors.append("controls.midi_bridge.input_port_name_contains must be non-empty")
  300. mod_ui = midi_bridge.get("mod_ui")
  301. if not isinstance(mod_ui, dict):
  302. errors.append("controls.midi_bridge.mod_ui must be a mapping")
  303. else:
  304. if mod_ui.get("host") != "127.0.0.1":
  305. errors.append("controls.midi_bridge.mod_ui.host must be 127.0.0.1")
  306. port = mod_ui.get("port")
  307. if (
  308. not isinstance(port, int)
  309. or isinstance(port, bool)
  310. or not 1 <= port <= 65535
  311. ):
  312. errors.append("controls.midi_bridge.mod_ui.port must be a valid TCP port")
  313. elif isinstance(deployment := state.get("deployment"), dict):
  314. ui_deployment = deployment.get("mod_ui")
  315. if (
  316. isinstance(ui_deployment, dict)
  317. and ui_deployment.get("websocket_port") != port
  318. ):
  319. errors.append(
  320. "controls.midi_bridge.mod_ui.port must match deployment.mod_ui.websocket_port"
  321. )
  322. websocket_path = mod_ui.get("websocket_path")
  323. if websocket_path != "/websocket":
  324. errors.append("controls.midi_bridge.mod_ui.websocket_path must be /websocket")
  325. connections = state.get("connections", [])
  326. if not isinstance(connections, list):
  327. errors.append("connections must be a list")
  328. connections = []
  329. # An explicitly-forbidden connection documents policy; any other status is actionable.
  330. for index, connection in enumerate(connections):
  331. if not isinstance(connection, dict):
  332. errors.append(f"connections[{index}] must be a mapping")
  333. continue
  334. if (
  335. connection.get("from") == "looper_returns"
  336. and connection.get("to") in {
  337. "looper_inputs",
  338. "looper_input_1",
  339. "looper_input_2",
  340. "looper_input_3",
  341. "looper_input_4",
  342. }
  343. and connection.get("status") != "explicitly-forbidden"
  344. ):
  345. errors.append(f"connections[{index}] routes looper returns into looper inputs")
  346. deployment = state.get("deployment")
  347. if not isinstance(deployment, dict):
  348. errors.append("deployment must be a mapping")
  349. else:
  350. for component, repository, asset, archive_format, bundle_paths in (
  351. (
  352. "lsp",
  353. "lsp-plugins/lsp-plugins",
  354. "lsp-plugins-{tag}-Linux-aarch64.7z",
  355. "7z",
  356. {"lsp-plugins.lv2": "lsp-plugins-{tag}-Linux-aarch64/LV2/lsp-plugins.lv2"},
  357. ),
  358. (
  359. "dragonfly_reverb",
  360. "michaelwillis/dragonfly-reverb",
  361. "dragonfly-reverb-{tag}-linux-arm64.tar.xz",
  362. "tar.xz",
  363. {
  364. "DragonflyEarlyReflections.lv2": "dragonfly-reverb-{tag}/DragonflyEarlyReflections.lv2",
  365. "DragonflyHallReverb.lv2": "dragonfly-reverb-{tag}/DragonflyHallReverb.lv2",
  366. "DragonflyPlateReverb.lv2": "dragonfly-reverb-{tag}/DragonflyPlateReverb.lv2",
  367. "DragonflyRoomReverb.lv2": "dragonfly-reverb-{tag}/DragonflyRoomReverb.lv2",
  368. },
  369. ),
  370. ):
  371. plugin = deployment.get(component)
  372. if not isinstance(plugin, dict):
  373. errors.append(f"deployment.{component} must be a mapping")
  374. continue
  375. tag = plugin.get("tag")
  376. if not isinstance(tag, str) or not tag or any(
  377. character not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  378. for character in tag
  379. ):
  380. errors.append(f"deployment.{component}.tag must be a simple version tag")
  381. expected_url = f"https://github.com/{repository}/releases/download/{tag}/{asset.format(tag=tag)}"
  382. if plugin.get("archive_url") != expected_url:
  383. errors.append(f"deployment.{component}.archive_url must match its tag and approved release asset")
  384. if plugin.get("archive_format") != archive_format:
  385. errors.append(f"deployment.{component}.archive_format must be {archive_format}")
  386. expected_paths = {
  387. name: path.format(tag=tag) for name, path in bundle_paths.items()
  388. }
  389. if plugin.get("bundle_paths") != expected_paths:
  390. errors.append(f"deployment.{component}.bundle_paths are invalid")
  391. for bundle_name, bundle_path in expected_paths.items():
  392. if bundle_name not in bundle_path:
  393. errors.append(f"deployment.{component}.bundle_paths keys must name their bundle directories")
  394. namlv2 = deployment.get("namlv2")
  395. if not isinstance(namlv2, dict):
  396. errors.append("deployment.namlv2 must be a mapping")
  397. else:
  398. if namlv2.get("source_url") != "https://github.com/mikeoliphant/neural-amp-modeler-lv2":
  399. errors.append(
  400. "deployment.namlv2.source_url must match the approved GitHub repository"
  401. )
  402. tag = namlv2.get("tag")
  403. allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  404. if not isinstance(tag, str) or not tag or any(
  405. character not in allowed_tag_characters for character in tag
  406. ):
  407. errors.append("deployment.namlv2.tag must be a simple version tag")
  408. archive_url = namlv2.get("archive_url")
  409. expected_url = (
  410. f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/"
  411. "neural_amp_modeler_lv2_rpi5.tgz"
  412. )
  413. if archive_url != expected_url:
  414. errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset")
  415. if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
  416. errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2")
  417. if namlv2.get("artifact_directory") != "artifacts/nam-lv2/arm64/current":
  418. errors.append(
  419. "deployment.namlv2.artifact_directory must be artifacts/nam-lv2/arm64/current"
  420. )
  421. if namlv2.get("backup_directory") != ".local/state/rigforge/namlv2-backups":
  422. errors.append(
  423. "deployment.namlv2.backup_directory must be "
  424. ".local/state/rigforge/namlv2-backups"
  425. )
  426. sooperlooper = deployment.get("sooperlooper")
  427. if not isinstance(sooperlooper, dict):
  428. errors.append("deployment.sooperlooper must be a mapping")
  429. else:
  430. source_url = sooperlooper.get("source_url")
  431. if source_url != "https://github.com/abarker/sooperlooper-mods":
  432. errors.append("deployment.sooperlooper.source_url must match the approved GitHub repository")
  433. tag = sooperlooper.get("tag")
  434. if not isinstance(tag, str) or not tag or any(
  435. character not in allowed_tag_characters for character in tag
  436. ):
  437. errors.append("deployment.sooperlooper.tag must be a simple version tag")
  438. for key, expected in (
  439. ("binary_source", "artifacts/sooperlooper/arm64/current/sooperlooper"),
  440. (
  441. "service_source",
  442. "config/systemd/user/rigforge-sooperlooper.service.in",
  443. ),
  444. (
  445. "service_destination",
  446. ".config/systemd/user/rigforge-sooperlooper.service",
  447. ),
  448. ("user_service", "rigforge-sooperlooper.service"),
  449. ("binding_file", "config/sooperlooper/fcb1010.slb"),
  450. ("pw_jack_path", "/usr/bin/pw-jack"),
  451. ("oscsend_path", "/usr/bin/oscsend"),
  452. ):
  453. if sooperlooper.get(key) != expected:
  454. errors.append(f"deployment.sooperlooper.{key} must be {expected}")
  455. timeout_seconds = sooperlooper.get("timeout_seconds")
  456. if (
  457. not isinstance(timeout_seconds, int)
  458. or isinstance(timeout_seconds, bool)
  459. or not 1 <= timeout_seconds <= 300
  460. ):
  461. errors.append("deployment.sooperlooper.timeout_seconds must be between 1 and 300")
  462. osc = sooperlooper.get("osc")
  463. if not isinstance(osc, dict):
  464. errors.append("deployment.sooperlooper.osc must be a mapping")
  465. else:
  466. if osc.get("host") != "127.0.0.1":
  467. errors.append("deployment.sooperlooper.osc.host must be 127.0.0.1")
  468. port = osc.get("port")
  469. if (
  470. not isinstance(port, int)
  471. or isinstance(port, bool)
  472. or not 1 <= port <= 65535
  473. ):
  474. errors.append("deployment.sooperlooper.osc.port must be a valid UDP port")
  475. delay = osc.get("post_start_delay_seconds")
  476. if (
  477. not isinstance(delay, int)
  478. or isinstance(delay, bool)
  479. or not 0 <= delay <= 30
  480. ):
  481. errors.append(
  482. "deployment.sooperlooper.osc.post_start_delay_seconds must be between 0 and 30"
  483. )
  484. prefader = osc.get("discrete_prefader")
  485. if (
  486. not isinstance(prefader, int)
  487. or isinstance(prefader, bool)
  488. or prefader not in (0, 1)
  489. ):
  490. errors.append(
  491. "deployment.sooperlooper.osc.discrete_prefader must be 0 or 1"
  492. )
  493. mod_host = deployment.get("mod_host")
  494. if not isinstance(mod_host, dict):
  495. errors.append("deployment.mod_host must be a mapping")
  496. else:
  497. source_url = mod_host.get("source_url")
  498. if source_url != "https://github.com/mod-audio/mod-host":
  499. errors.append("deployment.mod_host.source_url must match the approved GitHub repository")
  500. tag = mod_host.get("tag")
  501. if not isinstance(tag, str) or not tag or any(
  502. character not in allowed_tag_characters for character in tag
  503. ):
  504. errors.append("deployment.mod_host.tag must be a simple version tag")
  505. for key, expected in (
  506. ("binary_source", "artifacts/mod-host/arm64/current/mod-host"),
  507. ("service_source", "config/systemd/user/rigforge-mod-host.service.in"),
  508. ("service_destination", ".config/systemd/user/rigforge-mod-host.service"),
  509. ("user_service", "rigforge-mod-host.service"),
  510. ("readiness_script", "scripts/wait_mod_host_ports.py"),
  511. ("pw_jack_path", "/usr/bin/pw-jack"),
  512. ("python_path", "/usr/bin/python3"),
  513. ):
  514. if mod_host.get(key) != expected:
  515. errors.append(f"deployment.mod_host.{key} must be {expected}")
  516. socket_port = mod_host.get("socket_port")
  517. if (
  518. not isinstance(socket_port, int)
  519. or isinstance(socket_port, bool)
  520. or not 1 <= socket_port <= 65535
  521. ):
  522. errors.append("deployment.mod_host.socket_port must be a valid TCP port")
  523. feedback_port = mod_host.get("feedback_port")
  524. if (
  525. not isinstance(feedback_port, int)
  526. or isinstance(feedback_port, bool)
  527. or not 1 <= feedback_port <= 65535
  528. ):
  529. errors.append("deployment.mod_host.feedback_port must be a valid TCP port")
  530. elif feedback_port == socket_port:
  531. errors.append("deployment.mod_host.feedback_port must differ from socket_port")
  532. port_wait_timeout = mod_host.get("port_wait_timeout_seconds")
  533. if (
  534. not isinstance(port_wait_timeout, int)
  535. or isinstance(port_wait_timeout, bool)
  536. or not 5 <= port_wait_timeout <= 300
  537. ):
  538. errors.append(
  539. "deployment.mod_host.port_wait_timeout_seconds must be between 5 and 300"
  540. )
  541. mod_ui = deployment.get("mod_ui")
  542. if not isinstance(mod_ui, dict):
  543. errors.append("deployment.mod_ui must be a mapping")
  544. else:
  545. if mod_ui.get("source_url") != "https://github.com/mod-audio/mod-ui":
  546. errors.append("deployment.mod_ui.source_url must match the approved GitHub repository")
  547. tag = mod_ui.get("tag")
  548. if not isinstance(tag, str) or not tag or any(
  549. character not in allowed_tag_characters for character in tag
  550. ):
  551. errors.append("deployment.mod_ui.tag must be a simple version tag")
  552. if mod_ui.get("artifact_directory") != "artifacts/mod-ui/arm64/current":
  553. errors.append(
  554. "deployment.mod_ui.artifact_directory must be artifacts/mod-ui/arm64/current"
  555. )
  556. for key, expected in (
  557. ("launcher", "artifacts/mod-ui/arm64/current/run-mod-ui"),
  558. ("runner_script", "scripts/run_mod_ui_service.py"),
  559. ("service_source", "config/systemd/user/rigforge-mod-ui.service.in"),
  560. ("service_destination", ".config/systemd/user/rigforge-mod-ui.service"),
  561. ("user_service", "rigforge-mod-ui.service"),
  562. ("pw_jack_path", "/usr/bin/pw-jack"),
  563. ("python_path", "/usr/bin/python3"),
  564. ("websocket_host", "127.0.0.1"),
  565. ("reference_directory", ".local/share/rigforge/mod-ui/references"),
  566. ("pedalboards_directory", ".local/share/rigforge/mod-ui/pedalboards"),
  567. ("user_files_directory", ".local/share/rigforge/mod-ui/user-files"),
  568. ("last_state_file", ".local/share/rigforge/mod-ui/last-rigforge.json"),
  569. ("hardware_input_port", "rigforge.mod-host-input.capture:capture_AUX0"),
  570. ("hardware_output_port", "rigforge.mod-host-output.playback:playback_AUX0"),
  571. ):
  572. if mod_ui.get(key) != expected:
  573. errors.append(f"deployment.mod_ui.{key} must be {expected}")
  574. ui_port = mod_ui.get("websocket_port")
  575. if (
  576. not isinstance(ui_port, int)
  577. or isinstance(ui_port, bool)
  578. or not 1 <= ui_port <= 65535
  579. ):
  580. errors.append("deployment.mod_ui.websocket_port must be a valid TCP port")
  581. elif isinstance(mod_host, dict) and (
  582. ui_port == mod_host.get("socket_port")
  583. or ui_port == mod_host.get("feedback_port")
  584. ):
  585. errors.append("deployment.mod_ui.websocket_port must differ from mod-host TCP ports")
  586. mod_host_ports = deployment.get("mod_host_ports")
  587. if not isinstance(mod_host_ports, dict):
  588. errors.append("deployment.mod_host_ports must be a mapping")
  589. else:
  590. for key, expected in (
  591. ("binary_path", "/usr/bin/pw-loopback"),
  592. ("runner_script", "scripts/run_mod_host_ports.py"),
  593. ("service_source", "config/systemd/user/rigforge-mod-host-ports.service.in"),
  594. ("service_destination", ".config/systemd/user/rigforge-mod-host-ports.service"),
  595. ("user_service", "rigforge-mod-host-ports.service"),
  596. ("python_path", "/usr/bin/python3"),
  597. ("channel_map", "AUX0"),
  598. ("input_sink_name", "rigforge.mod-host-input"),
  599. ("input_source_name", "rigforge.mod-host-input.capture"),
  600. ("output_sink_name", "rigforge.mod-host-output.playback"),
  601. ("output_source_name", "rigforge.mod-host-output"),
  602. ):
  603. if mod_host_ports.get(key) != expected:
  604. errors.append(f"deployment.mod_host_ports.{key} must be {expected}")
  605. reconcile = deployment.get("reconcile")
  606. if not isinstance(reconcile, dict):
  607. errors.append("deployment.reconcile must be a mapping")
  608. else:
  609. for key, expected in (
  610. ("observer_script", "scripts/observe_pipewire.py"),
  611. (
  612. "service_source",
  613. "config/systemd/user/rigforge-pipewire-reconcile.service.in",
  614. ),
  615. (
  616. "service_destination",
  617. ".config/systemd/user/rigforge-pipewire-reconcile.service",
  618. ),
  619. ("user_service", "rigforge-pipewire-reconcile.service"),
  620. (
  621. "timer_source",
  622. "config/systemd/user/rigforge-pipewire-reconcile.timer.in",
  623. ),
  624. (
  625. "timer_destination",
  626. ".config/systemd/user/rigforge-pipewire-reconcile.timer",
  627. ),
  628. ("user_timer", "rigforge-pipewire-reconcile.timer"),
  629. ("python_path", "/usr/bin/python3"),
  630. ):
  631. if reconcile.get(key) != expected:
  632. errors.append(f"deployment.reconcile.{key} must be {expected}")
  633. interval = reconcile.get("interval_seconds")
  634. if (
  635. not isinstance(interval, int)
  636. or isinstance(interval, bool)
  637. or not 5 <= interval <= 300
  638. ):
  639. errors.append(
  640. "deployment.reconcile.interval_seconds must be between 5 and 300"
  641. )
  642. midi_bridge = deployment.get("midi_bridge")
  643. if not isinstance(midi_bridge, dict):
  644. errors.append("deployment.midi_bridge must be a mapping")
  645. else:
  646. for key, expected in (
  647. ("bridge_script", "scripts/midi_action_bridge.py"),
  648. (
  649. "service_source",
  650. "config/systemd/user/rigforge-midi-bridge.service.in",
  651. ),
  652. (
  653. "service_destination",
  654. ".config/systemd/user/rigforge-midi-bridge.service",
  655. ),
  656. ("user_service", "rigforge-midi-bridge.service"),
  657. ("python_path", "/usr/bin/python3"),
  658. ):
  659. if midi_bridge.get(key) != expected:
  660. errors.append(f"deployment.midi_bridge.{key} must be {expected}")
  661. pipewire = deployment.get("pipewire")
  662. if not isinstance(pipewire, dict):
  663. errors.append("deployment.pipewire must be a mapping")
  664. else:
  665. expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  666. if pipewire.get("fragment_source") != expected_fragment_source:
  667. errors.append("deployment.pipewire.fragment_source must name the managed repository fragment")
  668. expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  669. if pipewire.get("fragment_destination") != expected_fragment_destination:
  670. errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path")
  671. if pipewire.get("user_service") != "filter-chain.service":
  672. errors.append("deployment.pipewire.user_service must be filter-chain.service")
  673. return errors
  674. def main(argv=None):
  675. parser = argparse.ArgumentParser()
  676. parser.add_argument(
  677. "state_file",
  678. nargs="?",
  679. type=Path,
  680. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  681. )
  682. args = parser.parse_args(argv)
  683. try:
  684. with args.state_file.open(encoding="utf-8") as state_stream:
  685. state = yaml.safe_load(state_stream)
  686. except (OSError, yaml.YAMLError) as error:
  687. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  688. return 2
  689. errors = validate_state(state)
  690. if errors:
  691. for error in errors:
  692. print(f"ERROR: {error}", file=sys.stderr)
  693. return 1
  694. print(f"Valid desired state: {args.state_file}")
  695. return 0
  696. if __name__ == "__main__":
  697. raise SystemExit(main())