validate_desired_state.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. tag = namlv2.get("tag")
  399. allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  400. if not isinstance(tag, str) or not tag or any(
  401. character not in allowed_tag_characters for character in tag
  402. ):
  403. errors.append("deployment.namlv2.tag must be a simple version tag")
  404. archive_url = namlv2.get("archive_url")
  405. expected_url = (
  406. f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/"
  407. "neural_amp_modeler_lv2_rpi5.tgz"
  408. )
  409. if archive_url != expected_url:
  410. errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset")
  411. if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
  412. errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2")
  413. sooperlooper = deployment.get("sooperlooper")
  414. if not isinstance(sooperlooper, dict):
  415. errors.append("deployment.sooperlooper must be a mapping")
  416. else:
  417. source_url = sooperlooper.get("source_url")
  418. if source_url != "https://github.com/abarker/sooperlooper-mods":
  419. errors.append("deployment.sooperlooper.source_url must match the approved GitHub repository")
  420. tag = sooperlooper.get("tag")
  421. if not isinstance(tag, str) or not tag or any(
  422. character not in allowed_tag_characters for character in tag
  423. ):
  424. errors.append("deployment.sooperlooper.tag must be a simple version tag")
  425. for key, expected in (
  426. ("binary_source", "artifacts/sooperlooper/arm64/current/sooperlooper"),
  427. (
  428. "service_source",
  429. "config/systemd/user/rigforge-sooperlooper.service.in",
  430. ),
  431. (
  432. "service_destination",
  433. ".config/systemd/user/rigforge-sooperlooper.service",
  434. ),
  435. ("user_service", "rigforge-sooperlooper.service"),
  436. ("binding_file", "config/sooperlooper/fcb1010.slb"),
  437. ("pw_jack_path", "/usr/bin/pw-jack"),
  438. ("oscsend_path", "/usr/bin/oscsend"),
  439. ):
  440. if sooperlooper.get(key) != expected:
  441. errors.append(f"deployment.sooperlooper.{key} must be {expected}")
  442. timeout_seconds = sooperlooper.get("timeout_seconds")
  443. if (
  444. not isinstance(timeout_seconds, int)
  445. or isinstance(timeout_seconds, bool)
  446. or not 1 <= timeout_seconds <= 300
  447. ):
  448. errors.append("deployment.sooperlooper.timeout_seconds must be between 1 and 300")
  449. osc = sooperlooper.get("osc")
  450. if not isinstance(osc, dict):
  451. errors.append("deployment.sooperlooper.osc must be a mapping")
  452. else:
  453. if osc.get("host") != "127.0.0.1":
  454. errors.append("deployment.sooperlooper.osc.host must be 127.0.0.1")
  455. port = osc.get("port")
  456. if (
  457. not isinstance(port, int)
  458. or isinstance(port, bool)
  459. or not 1 <= port <= 65535
  460. ):
  461. errors.append("deployment.sooperlooper.osc.port must be a valid UDP port")
  462. delay = osc.get("post_start_delay_seconds")
  463. if (
  464. not isinstance(delay, int)
  465. or isinstance(delay, bool)
  466. or not 0 <= delay <= 30
  467. ):
  468. errors.append(
  469. "deployment.sooperlooper.osc.post_start_delay_seconds must be between 0 and 30"
  470. )
  471. prefader = osc.get("discrete_prefader")
  472. if (
  473. not isinstance(prefader, int)
  474. or isinstance(prefader, bool)
  475. or prefader not in (0, 1)
  476. ):
  477. errors.append(
  478. "deployment.sooperlooper.osc.discrete_prefader must be 0 or 1"
  479. )
  480. mod_host = deployment.get("mod_host")
  481. if not isinstance(mod_host, dict):
  482. errors.append("deployment.mod_host must be a mapping")
  483. else:
  484. source_url = mod_host.get("source_url")
  485. if source_url != "https://github.com/mod-audio/mod-host":
  486. errors.append("deployment.mod_host.source_url must match the approved GitHub repository")
  487. tag = mod_host.get("tag")
  488. if not isinstance(tag, str) or not tag or any(
  489. character not in allowed_tag_characters for character in tag
  490. ):
  491. errors.append("deployment.mod_host.tag must be a simple version tag")
  492. for key, expected in (
  493. ("binary_source", "artifacts/mod-host/arm64/current/mod-host"),
  494. ("service_source", "config/systemd/user/rigforge-mod-host.service.in"),
  495. ("service_destination", ".config/systemd/user/rigforge-mod-host.service"),
  496. ("user_service", "rigforge-mod-host.service"),
  497. ("readiness_script", "scripts/wait_mod_host_ports.py"),
  498. ("pw_jack_path", "/usr/bin/pw-jack"),
  499. ("python_path", "/usr/bin/python3"),
  500. ):
  501. if mod_host.get(key) != expected:
  502. errors.append(f"deployment.mod_host.{key} must be {expected}")
  503. socket_port = mod_host.get("socket_port")
  504. if (
  505. not isinstance(socket_port, int)
  506. or isinstance(socket_port, bool)
  507. or not 1 <= socket_port <= 65535
  508. ):
  509. errors.append("deployment.mod_host.socket_port must be a valid TCP port")
  510. feedback_port = mod_host.get("feedback_port")
  511. if (
  512. not isinstance(feedback_port, int)
  513. or isinstance(feedback_port, bool)
  514. or not 1 <= feedback_port <= 65535
  515. ):
  516. errors.append("deployment.mod_host.feedback_port must be a valid TCP port")
  517. elif feedback_port == socket_port:
  518. errors.append("deployment.mod_host.feedback_port must differ from socket_port")
  519. port_wait_timeout = mod_host.get("port_wait_timeout_seconds")
  520. if (
  521. not isinstance(port_wait_timeout, int)
  522. or isinstance(port_wait_timeout, bool)
  523. or not 5 <= port_wait_timeout <= 300
  524. ):
  525. errors.append(
  526. "deployment.mod_host.port_wait_timeout_seconds must be between 5 and 300"
  527. )
  528. mod_ui = deployment.get("mod_ui")
  529. if not isinstance(mod_ui, dict):
  530. errors.append("deployment.mod_ui must be a mapping")
  531. else:
  532. if mod_ui.get("source_url") != "https://github.com/mod-audio/mod-ui":
  533. errors.append("deployment.mod_ui.source_url must match the approved GitHub repository")
  534. tag = mod_ui.get("tag")
  535. if not isinstance(tag, str) or not tag or any(
  536. character not in allowed_tag_characters for character in tag
  537. ):
  538. errors.append("deployment.mod_ui.tag must be a simple version tag")
  539. if mod_ui.get("artifact_directory") != "artifacts/mod-ui/arm64/current":
  540. errors.append(
  541. "deployment.mod_ui.artifact_directory must be artifacts/mod-ui/arm64/current"
  542. )
  543. for key, expected in (
  544. ("launcher", "artifacts/mod-ui/arm64/current/run-mod-ui"),
  545. ("runner_script", "scripts/run_mod_ui_service.py"),
  546. ("service_source", "config/systemd/user/rigforge-mod-ui.service.in"),
  547. ("service_destination", ".config/systemd/user/rigforge-mod-ui.service"),
  548. ("user_service", "rigforge-mod-ui.service"),
  549. ("pw_jack_path", "/usr/bin/pw-jack"),
  550. ("python_path", "/usr/bin/python3"),
  551. ("websocket_host", "127.0.0.1"),
  552. ("reference_directory", ".local/share/rigforge/mod-ui/references"),
  553. ("pedalboards_directory", ".local/share/rigforge/mod-ui/pedalboards"),
  554. ("last_state_file", ".local/share/rigforge/mod-ui/last-rigforge.json"),
  555. ("hardware_input_port", "rigforge.mod-host-input.capture:capture_AUX0"),
  556. ("hardware_output_port", "rigforge.mod-host-output.playback:playback_AUX0"),
  557. ):
  558. if mod_ui.get(key) != expected:
  559. errors.append(f"deployment.mod_ui.{key} must be {expected}")
  560. ui_port = mod_ui.get("websocket_port")
  561. if (
  562. not isinstance(ui_port, int)
  563. or isinstance(ui_port, bool)
  564. or not 1 <= ui_port <= 65535
  565. ):
  566. errors.append("deployment.mod_ui.websocket_port must be a valid TCP port")
  567. elif isinstance(mod_host, dict) and (
  568. ui_port == mod_host.get("socket_port")
  569. or ui_port == mod_host.get("feedback_port")
  570. ):
  571. errors.append("deployment.mod_ui.websocket_port must differ from mod-host TCP ports")
  572. mod_host_ports = deployment.get("mod_host_ports")
  573. if not isinstance(mod_host_ports, dict):
  574. errors.append("deployment.mod_host_ports must be a mapping")
  575. else:
  576. for key, expected in (
  577. ("binary_path", "/usr/bin/pw-loopback"),
  578. ("runner_script", "scripts/run_mod_host_ports.py"),
  579. ("service_source", "config/systemd/user/rigforge-mod-host-ports.service.in"),
  580. ("service_destination", ".config/systemd/user/rigforge-mod-host-ports.service"),
  581. ("user_service", "rigforge-mod-host-ports.service"),
  582. ("python_path", "/usr/bin/python3"),
  583. ("channel_map", "AUX0"),
  584. ("input_sink_name", "rigforge.mod-host-input"),
  585. ("input_source_name", "rigforge.mod-host-input.capture"),
  586. ("output_sink_name", "rigforge.mod-host-output.playback"),
  587. ("output_source_name", "rigforge.mod-host-output"),
  588. ):
  589. if mod_host_ports.get(key) != expected:
  590. errors.append(f"deployment.mod_host_ports.{key} must be {expected}")
  591. reconcile = deployment.get("reconcile")
  592. if not isinstance(reconcile, dict):
  593. errors.append("deployment.reconcile must be a mapping")
  594. else:
  595. for key, expected in (
  596. ("observer_script", "scripts/observe_pipewire.py"),
  597. (
  598. "service_source",
  599. "config/systemd/user/rigforge-pipewire-reconcile.service.in",
  600. ),
  601. (
  602. "service_destination",
  603. ".config/systemd/user/rigforge-pipewire-reconcile.service",
  604. ),
  605. ("user_service", "rigforge-pipewire-reconcile.service"),
  606. (
  607. "timer_source",
  608. "config/systemd/user/rigforge-pipewire-reconcile.timer.in",
  609. ),
  610. (
  611. "timer_destination",
  612. ".config/systemd/user/rigforge-pipewire-reconcile.timer",
  613. ),
  614. ("user_timer", "rigforge-pipewire-reconcile.timer"),
  615. ("python_path", "/usr/bin/python3"),
  616. ):
  617. if reconcile.get(key) != expected:
  618. errors.append(f"deployment.reconcile.{key} must be {expected}")
  619. interval = reconcile.get("interval_seconds")
  620. if (
  621. not isinstance(interval, int)
  622. or isinstance(interval, bool)
  623. or not 5 <= interval <= 300
  624. ):
  625. errors.append(
  626. "deployment.reconcile.interval_seconds must be between 5 and 300"
  627. )
  628. midi_bridge = deployment.get("midi_bridge")
  629. if not isinstance(midi_bridge, dict):
  630. errors.append("deployment.midi_bridge must be a mapping")
  631. else:
  632. for key, expected in (
  633. ("bridge_script", "scripts/midi_action_bridge.py"),
  634. (
  635. "service_source",
  636. "config/systemd/user/rigforge-midi-bridge.service.in",
  637. ),
  638. (
  639. "service_destination",
  640. ".config/systemd/user/rigforge-midi-bridge.service",
  641. ),
  642. ("user_service", "rigforge-midi-bridge.service"),
  643. ("python_path", "/usr/bin/python3"),
  644. ):
  645. if midi_bridge.get(key) != expected:
  646. errors.append(f"deployment.midi_bridge.{key} must be {expected}")
  647. pipewire = deployment.get("pipewire")
  648. if not isinstance(pipewire, dict):
  649. errors.append("deployment.pipewire must be a mapping")
  650. else:
  651. expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  652. if pipewire.get("fragment_source") != expected_fragment_source:
  653. errors.append("deployment.pipewire.fragment_source must name the managed repository fragment")
  654. expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  655. if pipewire.get("fragment_destination") != expected_fragment_destination:
  656. errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path")
  657. if pipewire.get("user_service") != "filter-chain.service":
  658. errors.append("deployment.pipewire.user_service must be filter-chain.service")
  659. return errors
  660. def main(argv=None):
  661. parser = argparse.ArgumentParser()
  662. parser.add_argument(
  663. "state_file",
  664. nargs="?",
  665. type=Path,
  666. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  667. )
  668. args = parser.parse_args(argv)
  669. try:
  670. with args.state_file.open(encoding="utf-8") as state_stream:
  671. state = yaml.safe_load(state_stream)
  672. except (OSError, yaml.YAMLError) as error:
  673. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  674. return 2
  675. errors = validate_state(state)
  676. if errors:
  677. for error in errors:
  678. print(f"ERROR: {error}", file=sys.stderr)
  679. return 1
  680. print(f"Valid desired state: {args.state_file}")
  681. return 0
  682. if __name__ == "__main__":
  683. raise SystemExit(main())