validate_desired_state.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. namlv2 = deployment.get("namlv2")
  143. if not isinstance(namlv2, dict):
  144. errors.append("deployment.namlv2 must be a mapping")
  145. else:
  146. tag = namlv2.get("tag")
  147. allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  148. if not isinstance(tag, str) or not tag or any(
  149. character not in allowed_tag_characters for character in tag
  150. ):
  151. errors.append("deployment.namlv2.tag must be a simple version tag")
  152. archive_url = namlv2.get("archive_url")
  153. expected_url = (
  154. f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/"
  155. "neural_amp_modeler_lv2_rpi5.tgz"
  156. )
  157. if archive_url != expected_url:
  158. errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset")
  159. if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
  160. errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2")
  161. pipewire = deployment.get("pipewire")
  162. if not isinstance(pipewire, dict):
  163. errors.append("deployment.pipewire must be a mapping")
  164. else:
  165. expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  166. if pipewire.get("fragment_source") != expected_fragment_source:
  167. errors.append("deployment.pipewire.fragment_source must name the managed repository fragment")
  168. expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  169. if pipewire.get("fragment_destination") != expected_fragment_destination:
  170. errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path")
  171. if pipewire.get("user_service") != "filter-chain.service":
  172. errors.append("deployment.pipewire.user_service must be filter-chain.service")
  173. return errors
  174. def main(argv=None):
  175. parser = argparse.ArgumentParser()
  176. parser.add_argument(
  177. "state_file",
  178. nargs="?",
  179. type=Path,
  180. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  181. )
  182. args = parser.parse_args(argv)
  183. try:
  184. with args.state_file.open(encoding="utf-8") as state_stream:
  185. state = yaml.safe_load(state_stream)
  186. except (OSError, yaml.YAMLError) as error:
  187. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  188. return 2
  189. errors = validate_state(state)
  190. if errors:
  191. for error in errors:
  192. print(f"ERROR: {error}", file=sys.stderr)
  193. return 1
  194. print(f"Valid desired state: {args.state_file}")
  195. return 0
  196. if __name__ == "__main__":
  197. raise SystemExit(main())