validate_desired_state.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import argparse
  2. import sys
  3. from pathlib import Path
  4. import yaml
  5. def validate_state(state):
  6. errors = []
  7. if not isinstance(state, dict):
  8. return ["document root must be a mapping"]
  9. # Establish the declared source map before validating looper references.
  10. sources = state.get("sources")
  11. if not isinstance(sources, list):
  12. return ["sources must be a list"]
  13. source_by_id = {}
  14. physical_inputs = set()
  15. capture_ports = set()
  16. for index, source in enumerate(sources):
  17. location = f"sources[{index}]"
  18. if not isinstance(source, dict):
  19. errors.append(f"{location} must be a mapping")
  20. continue
  21. source_id = source.get("id")
  22. if not isinstance(source_id, str) or not source_id:
  23. errors.append(f"{location}.id must be a non-empty string")
  24. elif source_id in source_by_id:
  25. errors.append(f"duplicate source id: {source_id}")
  26. else:
  27. source_by_id[source_id] = source
  28. physical_input = source.get("physical_input")
  29. if not isinstance(physical_input, int) or isinstance(physical_input, bool):
  30. errors.append(f"{location}.physical_input must be an integer")
  31. elif physical_input in physical_inputs:
  32. errors.append(f"duplicate physical input: {physical_input}")
  33. else:
  34. physical_inputs.add(physical_input)
  35. capture_port = source.get("capture_port")
  36. if not isinstance(capture_port, str) or not capture_port:
  37. errors.append(f"{location}.capture_port must be a non-empty string")
  38. elif capture_port in capture_ports:
  39. errors.append(f"duplicate capture port: {capture_port}")
  40. else:
  41. capture_ports.add(capture_port)
  42. # Keep the first four-channel milestone tied to its four named instruments.
  43. expected_source_ids = {"guitar", "bass", "voice", "synth"}
  44. if set(source_by_id) != expected_source_ids:
  45. errors.append("source ids must be guitar, bass, voice, and synth exactly once")
  46. if len(sources) != 4:
  47. errors.append(f"exactly four sources are required, got {len(sources)}")
  48. if physical_inputs != {1, 2, 3, 4}:
  49. errors.append("physical inputs must cover 1 through 4 exactly once")
  50. # The required looper is one instance containing one four-channel loop.
  51. system = state.get("system")
  52. components = system.get("components") if isinstance(system, dict) else None
  53. if not isinstance(components, list):
  54. errors.append("system.components must be a list")
  55. components = []
  56. loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"]
  57. if len(loopers) != 1:
  58. errors.append(f"exactly one looper component is required, got {len(loopers)}")
  59. elif loopers[0].get("instance_count") != 1:
  60. errors.append("looper.instance_count must be 1")
  61. elif loopers[0].get("loop_count") != 1:
  62. errors.append("looper.loop_count must be 1")
  63. elif loopers[0].get("channels_per_loop") != 4:
  64. errors.append("looper.channels_per_loop must be 4")
  65. routing = state.get("routing")
  66. if not isinstance(routing, dict):
  67. errors.append("routing must be a mapping")
  68. return errors
  69. looper_inputs = routing.get("looper_inputs")
  70. if not isinstance(looper_inputs, list):
  71. errors.append("routing.looper_inputs must be a list")
  72. looper_inputs = []
  73. # Check that each source is assigned exactly once across all loop channels.
  74. input_channels = set()
  75. input_sources = set()
  76. for index, loop_input in enumerate(looper_inputs):
  77. location = f"routing.looper_inputs[{index}]"
  78. if not isinstance(loop_input, dict):
  79. errors.append(f"{location} must be a mapping")
  80. continue
  81. source_id = loop_input.get("source")
  82. channel = loop_input.get("channel")
  83. if source_id not in source_by_id:
  84. errors.append(f"{location}.source references unknown source: {source_id}")
  85. if source_id in input_sources:
  86. errors.append(f"source is assigned to multiple looper inputs: {source_id}")
  87. input_sources.add(source_id)
  88. if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5):
  89. errors.append(f"{location}.channel must be an integer from 1 through 4")
  90. elif channel in input_channels:
  91. errors.append(f"duplicate looper input channel: {channel}")
  92. else:
  93. input_channels.add(channel)
  94. if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}:
  95. errors.append("looper inputs must cover channels 1 through 4 exactly once")
  96. if input_sources != set(source_by_id):
  97. errors.append("each declared source must map to exactly one looper input")
  98. mix = routing.get("mix")
  99. if not isinstance(mix, dict):
  100. errors.append("routing.mix must be a mapping")
  101. else:
  102. output_port = mix.get("output_port")
  103. if not isinstance(output_port, str) or not output_port:
  104. errors.append("routing.mix.output_port must be a non-empty string")
  105. mix_id = mix.get("id")
  106. if not isinstance(mix_id, str) or not mix_id:
  107. errors.append("routing.mix.id must be a non-empty string")
  108. includes = mix.get("includes")
  109. if not isinstance(includes, list) or "looper_returns" not in includes:
  110. errors.append("routing.mix.includes must include looper_returns")
  111. # Loop returns join the output mix but must never feed the loop inputs.
  112. returns = routing.get("looper_returns")
  113. if not isinstance(returns, dict):
  114. errors.append("routing.looper_returns must be a mapping")
  115. elif returns.get("feed_looper_inputs") is not False:
  116. errors.append("routing.looper_returns.feed_looper_inputs must be false")
  117. connections = state.get("connections", [])
  118. if not isinstance(connections, list):
  119. errors.append("connections must be a list")
  120. connections = []
  121. # An explicitly-forbidden connection documents policy; any other status is actionable.
  122. for index, connection in enumerate(connections):
  123. if not isinstance(connection, dict):
  124. errors.append(f"connections[{index}] must be a mapping")
  125. continue
  126. if (
  127. connection.get("from") == "looper_returns"
  128. and connection.get("to") in {
  129. "looper_inputs",
  130. "looper_input_1",
  131. "looper_input_2",
  132. "looper_input_3",
  133. "looper_input_4",
  134. }
  135. and connection.get("status") != "explicitly-forbidden"
  136. ):
  137. errors.append(f"connections[{index}] routes looper returns into looper inputs")
  138. deployment = state.get("deployment")
  139. if not isinstance(deployment, dict):
  140. errors.append("deployment must be a mapping")
  141. else:
  142. for component, repository, asset, archive_format, bundle_paths in (
  143. (
  144. "lsp",
  145. "lsp-plugins/lsp-plugins",
  146. "lsp-plugins-{tag}-Linux-aarch64.7z",
  147. "7z",
  148. {"lsp-plugins.lv2": "lsp-plugins-{tag}-Linux-aarch64/LV2/lsp-plugins.lv2"},
  149. ),
  150. (
  151. "dragonfly_reverb",
  152. "michaelwillis/dragonfly-reverb",
  153. "dragonfly-reverb-{tag}-linux-arm64.tar.xz",
  154. "tar.xz",
  155. {
  156. "DragonflyEarlyReflections.lv2": "dragonfly-reverb-{tag}/DragonflyEarlyReflections.lv2",
  157. "DragonflyHallReverb.lv2": "dragonfly-reverb-{tag}/DragonflyHallReverb.lv2",
  158. "DragonflyPlateReverb.lv2": "dragonfly-reverb-{tag}/DragonflyPlateReverb.lv2",
  159. "DragonflyRoomReverb.lv2": "dragonfly-reverb-{tag}/DragonflyRoomReverb.lv2",
  160. },
  161. ),
  162. ):
  163. plugin = deployment.get(component)
  164. if not isinstance(plugin, dict):
  165. errors.append(f"deployment.{component} must be a mapping")
  166. continue
  167. tag = plugin.get("tag")
  168. if not isinstance(tag, str) or not tag or any(
  169. character not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  170. for character in tag
  171. ):
  172. errors.append(f"deployment.{component}.tag must be a simple version tag")
  173. expected_url = f"https://github.com/{repository}/releases/download/{tag}/{asset.format(tag=tag)}"
  174. if plugin.get("archive_url") != expected_url:
  175. errors.append(f"deployment.{component}.archive_url must match its tag and approved release asset")
  176. if plugin.get("archive_format") != archive_format:
  177. errors.append(f"deployment.{component}.archive_format must be {archive_format}")
  178. expected_paths = {
  179. name: path.format(tag=tag) for name, path in bundle_paths.items()
  180. }
  181. if plugin.get("bundle_paths") != expected_paths:
  182. errors.append(f"deployment.{component}.bundle_paths are invalid")
  183. for bundle_name, bundle_path in expected_paths.items():
  184. if bundle_name not in bundle_path:
  185. errors.append(f"deployment.{component}.bundle_paths keys must name their bundle directories")
  186. namlv2 = deployment.get("namlv2")
  187. if not isinstance(namlv2, dict):
  188. errors.append("deployment.namlv2 must be a mapping")
  189. else:
  190. tag = namlv2.get("tag")
  191. allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  192. if not isinstance(tag, str) or not tag or any(
  193. character not in allowed_tag_characters for character in tag
  194. ):
  195. errors.append("deployment.namlv2.tag must be a simple version tag")
  196. archive_url = namlv2.get("archive_url")
  197. expected_url = (
  198. f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/"
  199. "neural_amp_modeler_lv2_rpi5.tgz"
  200. )
  201. if archive_url != expected_url:
  202. errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset")
  203. if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
  204. errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2")
  205. sooperlooper = deployment.get("sooperlooper")
  206. if not isinstance(sooperlooper, dict):
  207. errors.append("deployment.sooperlooper must be a mapping")
  208. else:
  209. source_url = sooperlooper.get("source_url")
  210. if source_url != "https://github.com/abarker/sooperlooper-mods":
  211. errors.append("deployment.sooperlooper.source_url must match the approved GitHub repository")
  212. tag = sooperlooper.get("tag")
  213. if not isinstance(tag, str) or not tag or any(
  214. character not in allowed_tag_characters for character in tag
  215. ):
  216. errors.append("deployment.sooperlooper.tag must be a simple version tag")
  217. mod_host = deployment.get("mod_host")
  218. if not isinstance(mod_host, dict):
  219. errors.append("deployment.mod_host must be a mapping")
  220. else:
  221. source_url = mod_host.get("source_url")
  222. if source_url != "https://github.com/mod-audio/mod-host":
  223. errors.append("deployment.mod_host.source_url must match the approved GitHub repository")
  224. tag = mod_host.get("tag")
  225. if not isinstance(tag, str) or not tag or any(
  226. character not in allowed_tag_characters for character in tag
  227. ):
  228. errors.append("deployment.mod_host.tag must be a simple version tag")
  229. pipewire = deployment.get("pipewire")
  230. if not isinstance(pipewire, dict):
  231. errors.append("deployment.pipewire must be a mapping")
  232. else:
  233. expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  234. if pipewire.get("fragment_source") != expected_fragment_source:
  235. errors.append("deployment.pipewire.fragment_source must name the managed repository fragment")
  236. expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  237. if pipewire.get("fragment_destination") != expected_fragment_destination:
  238. errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path")
  239. if pipewire.get("user_service") != "filter-chain.service":
  240. errors.append("deployment.pipewire.user_service must be filter-chain.service")
  241. return errors
  242. def main(argv=None):
  243. parser = argparse.ArgumentParser()
  244. parser.add_argument(
  245. "state_file",
  246. nargs="?",
  247. type=Path,
  248. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  249. )
  250. args = parser.parse_args(argv)
  251. try:
  252. with args.state_file.open(encoding="utf-8") as state_stream:
  253. state = yaml.safe_load(state_stream)
  254. except (OSError, yaml.YAMLError) as error:
  255. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  256. return 2
  257. errors = validate_state(state)
  258. if errors:
  259. for error in errors:
  260. print(f"ERROR: {error}", file=sys.stderr)
  261. return 1
  262. print(f"Valid desired state: {args.state_file}")
  263. return 0
  264. if __name__ == "__main__":
  265. raise SystemExit(main())