builtin_scripts.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Temporary (per-boot) 2 GiB display-reserve unlock for the EXL3 2x
  31. // runtime (nvidia_drm modeset=1 fbdev=0 remod; no UEFI/boot changes).
  32. BuiltinScript {
  33. alias: "unlock_display_ram",
  34. body: include_str!("../host-scripts/unlock-display-ram.sh"),
  35. },
  36. BuiltinScript {
  37. alias: "restore_display_ram",
  38. body: include_str!("../host-scripts/restore-display-ram.sh"),
  39. },
  40. ];
  41. /// Resolve an alias to its script body. Returns `None` (and the caller
  42. /// formats the list of known aliases) when the alias is unknown.
  43. pub fn resolve(alias: &str) -> Option<&'static str> {
  44. BUILTIN_SCRIPTS
  45. .iter()
  46. .find(|s| s.alias == alias)
  47. .map(|s| s.body)
  48. }
  49. /// Comma-separated list of known aliases, for error messages.
  50. pub fn known_aliases() -> String {
  51. BUILTIN_SCRIPTS
  52. .iter()
  53. .map(|s| s.alias)
  54. .collect::<Vec<_>>()
  55. .join(", ")
  56. }
  57. #[cfg(test)]
  58. mod tests {
  59. use super::*;
  60. #[test]
  61. fn resolves_known_aliases() {
  62. assert!(resolve("stop_dgx_services").is_some());
  63. assert!(resolve("restore_dgx_services").is_some());
  64. let body = resolve("stop_dgx_services").unwrap();
  65. // Sanity: the embedded script is non-empty and starts with a shebang.
  66. assert!(body.starts_with("#!/usr/bin/env bash"));
  67. assert!(body.contains("systemctl"));
  68. }
  69. #[test]
  70. fn unknown_alias_returns_none() {
  71. assert!(resolve("nope").is_none());
  72. }
  73. #[test]
  74. fn known_aliases_lists_all() {
  75. let list = known_aliases();
  76. assert!(list.contains("stop_dgx_services"));
  77. assert!(list.contains("restore_dgx_services"));
  78. }
  79. #[test]
  80. fn scripts_reference_each_other_correctly() {
  81. // The restore script's docstring mentions the stop script by its
  82. // filename (`stop-dgx-services.sh`, not the old `glmnf3` name) —
  83. // regression guard for the rename.
  84. let restore = resolve("restore_dgx_services").unwrap();
  85. assert!(restore.contains("stop-dgx-services.sh"));
  86. assert!(!restore.contains("glmnf3"));
  87. let stop = resolve("stop_dgx_services").unwrap();
  88. assert!(!stop.contains("glmnf3"));
  89. }
  90. }