builtin_scripts.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //! Built-in host scripts baked into the binary at compile time.
  2. //!
  3. //! Each script lives as a real `.sh` file under `host-scripts/` (so it stays
  4. //! diffable, shellcheck-able, and editor-friendly) and is embedded here via
  5. //! `include_str!`. Recipes reference a script by **alias** in a
  6. //! `before_serve` / `after_stop` list (e.g. `{ script = "stop_dgx_services" }`);
  7. //! the launcher resolves the alias to the script body and runs it over SSH on
  8. //! each node. No file copy from the operator's machine is needed.
  9. //!
  10. //! To add a new built-in:
  11. //! 1. Drop the `.sh` file into `host-scripts/`.
  12. //! 2. Add an `include_str!` line + a `(alias, body)` entry below.
  13. //! 3. Document the alias in AGENTS.md.
  14. /// A built-in script: its alias and the embedded body.
  15. pub struct BuiltinScript {
  16. pub alias: &'static str,
  17. pub body: &'static str,
  18. }
  19. /// The registry of all baked-in scripts. Order is the display order for
  20. /// error hints.
  21. pub static BUILTIN_SCRIPTS: &[BuiltinScript] = &[
  22. BuiltinScript {
  23. alias: "stop_dgx_services",
  24. body: include_str!("../host-scripts/stop-dgx-services.sh"),
  25. },
  26. BuiltinScript {
  27. alias: "restore_dgx_services",
  28. body: include_str!("../host-scripts/restore-dgx-services.sh"),
  29. },
  30. ];
  31. /// Resolve an alias to its script body. Returns `None` (and the caller
  32. /// formats the list of known aliases) when the alias is unknown.
  33. pub fn resolve(alias: &str) -> Option<&'static str> {
  34. BUILTIN_SCRIPTS
  35. .iter()
  36. .find(|s| s.alias == alias)
  37. .map(|s| s.body)
  38. }
  39. /// Comma-separated list of known aliases, for error messages.
  40. pub fn known_aliases() -> String {
  41. BUILTIN_SCRIPTS
  42. .iter()
  43. .map(|s| s.alias)
  44. .collect::<Vec<_>>()
  45. .join(", ")
  46. }
  47. #[cfg(test)]
  48. mod tests {
  49. use super::*;
  50. #[test]
  51. fn resolves_known_aliases() {
  52. assert!(resolve("stop_dgx_services").is_some());
  53. assert!(resolve("restore_dgx_services").is_some());
  54. let body = resolve("stop_dgx_services").unwrap();
  55. // Sanity: the embedded script is non-empty and starts with a shebang.
  56. assert!(body.starts_with("#!/usr/bin/env bash"));
  57. assert!(body.contains("systemctl"));
  58. }
  59. #[test]
  60. fn unknown_alias_returns_none() {
  61. assert!(resolve("nope").is_none());
  62. }
  63. #[test]
  64. fn known_aliases_lists_all() {
  65. let list = known_aliases();
  66. assert!(list.contains("stop_dgx_services"));
  67. assert!(list.contains("restore_dgx_services"));
  68. }
  69. #[test]
  70. fn scripts_reference_each_other_correctly() {
  71. // The restore script's docstring mentions the stop script by its
  72. // filename (`stop-dgx-services.sh`, not the old `glmnf3` name) —
  73. // regression guard for the rename.
  74. let restore = resolve("restore_dgx_services").unwrap();
  75. assert!(restore.contains("stop-dgx-services.sh"));
  76. assert!(!restore.contains("glmnf3"));
  77. let stop = resolve("stop_dgx_services").unwrap();
  78. assert!(!stop.contains("glmnf3"));
  79. }
  80. }