cli.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. use crate::runlock::RunlockCommands;
  2. use clap::{Parser, Subcommand, ValueEnum};
  3. pub use crate::cli_sub::{
  4. ConfigCommands, PowerAction, PublishArgs, RegistryCommands, ReposArgs, ReposCommands,
  5. SyncImageArgs, TmuxCommands,
  6. };
  7. /// Output format for human-readable reports (`--format`).
  8. #[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
  9. pub enum OutputFormat {
  10. Plain,
  11. Md,
  12. Html,
  13. }
  14. #[derive(Parser)]
  15. #[command(author, version, about = "DGX Spark Launcher for vLLM and SGLang")]
  16. pub struct Cli {
  17. #[arg(long, short, default_value = "config.toml")]
  18. pub config: String,
  19. #[arg(long, short, help = "Show commands without executing")]
  20. pub dry_run: bool,
  21. #[command(subcommand)]
  22. pub command: Commands,
  23. }
  24. #[derive(Subcommand)]
  25. pub enum Commands {
  26. /// Distribute a recipe's image across the cluster nodes (node-to-node,
  27. /// one save + parallel fan-out; no serve started).
  28. SyncImage(SyncImageArgs),
  29. /// Launch a cluster
  30. Serve {
  31. #[arg(long, required = true)]
  32. recipe: String,
  33. #[arg(long, default_value = "auto")]
  34. cluster: String,
  35. /// Target a specific node within the cluster (default: all).
  36. #[arg(long)]
  37. node: Option<String>,
  38. /// Keep containers after stop (default: auto-remove on stop)
  39. #[arg(long)]
  40. keep: bool,
  41. /// Skip pre-serve hooks (cluster-level + recipe-level).
  42. #[arg(long)]
  43. skip_before_serve: bool,
  44. /// Don't follow logs after serving (default: follow).
  45. #[arg(long, action = clap::ArgAction::SetTrue)]
  46. no_follow: bool,
  47. /// Skip the engine-readiness wait after launch (no /v1/models poll).
  48. #[arg(long)]
  49. no_wait: bool,
  50. /// Power on cluster if switch is off (requires HA config).
  51. #[arg(short = 'O', long = "on")]
  52. auto_power_on: bool,
  53. /// Sync Docker image across nodes before starting.
  54. #[arg(long, default_value_t = true)]
  55. sync_image: bool,
  56. /// Explicit source node for image sync (default: auto-detect).
  57. #[arg(long)]
  58. sync_image_source: Option<String>,
  59. /// Build image on this node if not found (requires docker-images/<recipe>/).
  60. #[arg(long)]
  61. build_node: Option<String>,
  62. /// Force rebuild even if checksum matches.
  63. #[arg(long)]
  64. force_build: bool,
  65. /// Run even on occupied nodes (dangerous for GPU recipes: OOM,
  66. /// port conflicts). Skips the occupancy check in cluster selection.
  67. #[arg(long)]
  68. force: bool,
  69. /// Serve single-node on the first free node (overrides recipe `solo`);
  70. /// `--no-solo` forces multi-node.
  71. #[arg(long)]
  72. solo: Option<bool>,
  73. /// Mount a per-node local HF mirror (bare = ~/.cache/hf-models;
  74. /// `--mirror <path>` = that path; first serve copies per node).
  75. /// Mutually exclusive with `--hf-cache` (both pick the HF mount source).
  76. #[arg(long, num_args = 0..=1, default_missing_value = "")]
  77. mirror: Option<String>,
  78. /// Override the host-side HF cache dir for this serve (config
  79. /// `hf_cache_dir`, else `~/.cache/huggingface`). `~` expands to the
  80. /// node's SSH-user home. Mutually exclusive with `--mirror`.
  81. #[arg(long)]
  82. hf_cache: Option<String>,
  83. /// How hard to run the recipe's `[[staging]]` blocks: `auto`
  84. /// (ledger-verified, copy what is new), `skip` (assume staged),
  85. /// `force` (re-verify everything).
  86. #[arg(long)]
  87. stage: Option<String>,
  88. /// Scale the recipe to N nodes (`--fit=N`; bare = whole cluster, TP
  89. /// tracks it; incompatible with --solo).
  90. #[arg(long, num_args = 0..=1, default_missing_value = "")]
  91. fit: Option<String>,
  92. /// Apply the recipe's `cold_args` over its `args` (overlay by flag
  93. /// name) — the first-launch / tight-node safety valve for memory
  94. /// knobs (GMU, KV/context limits). No-op if the recipe has no
  95. /// `cold_args`.
  96. #[arg(long)]
  97. cold: bool,
  98. /// `sudo rm -rf` the host-side extra_mounts folders on the launch
  99. /// nodes before containers start (repo-based mounts untouched).
  100. #[arg(long)]
  101. clear: bool,
  102. /// Skip the post-build registry push (only meaningful when `[registry]`
  103. /// is configured and the image needs (re)building).
  104. #[arg(long)]
  105. no_push: bool,
  106. /// Where the image comes from: build from `docker-images/` (default)
  107. /// or pull the prebuilt `{base}/{name}:{checksum}` from `[registry]`
  108. /// (skips the build; the image must already be pushed).
  109. #[arg(long, value_enum, default_value_t = crate::docker::ImageSource::Repo)]
  110. image_source: crate::docker::ImageSource,
  111. },
  112. /// Show container logs
  113. Logs {
  114. #[arg(long, required = true)]
  115. recipe: String,
  116. #[arg(long, action = clap::ArgAction::SetTrue)]
  117. follow: bool,
  118. #[arg(long, default_value = "auto")]
  119. cluster: String,
  120. /// Target a specific node within the cluster (default: head node if following, all nodes if not).
  121. #[arg(long)]
  122. node: Option<String>,
  123. },
  124. /// Stop a cluster
  125. Stop {
  126. #[arg(long, required = true)]
  127. recipe: String,
  128. /// Target specific nodes (repeatable, e.g. --node dgx1 --node dgx2).
  129. #[arg(long)]
  130. node: Vec<String>,
  131. #[arg(long, default_value = "auto")]
  132. cluster: String,
  133. /// Power off cluster after stopping containers (requires HA config).
  134. #[arg(long = "off")]
  135. power_off: bool,
  136. /// Don't wait until the containers are actually stopped and removed
  137. /// (default: wait and verify, up to 60 s per node, so an immediate
  138. /// re-serve on the same nodes doesn't race the dying containers).
  139. #[arg(long)]
  140. no_wait: bool,
  141. },
  142. /// Bench a live serve with tool-eval-bench: reports auto-land in
  143. /// recipes/<lane>/reports/<UTC-date>_<label>.{md,json,log} (seed 42, hardmode, perf).
  144. Bench {
  145. #[arg(long, required = true)]
  146. recipe: String,
  147. #[arg(long, required = true)]
  148. cluster: String,
  149. /// Bench target node within the cluster (default: rank 0). Use when
  150. /// the serve was launched with `serve --node <n>` — a pinned/solo
  151. /// run may live on a non-rank-0 node.
  152. #[arg(long)]
  153. node: Option<String>,
  154. /// Label for the run (campaign record). Optional for `--perf-only`
  155. /// runs (default `perf`); required for quality runs.
  156. #[arg(long)]
  157. label: Option<String>,
  158. #[arg(long, default_value = "1,2")]
  159. concurrency: String,
  160. #[arg(long, default_value_t = 42)]
  161. seed: u64,
  162. /// Run scenario evaluations over N concurrent streams (tool
  163. /// `--parallel`). Speeds up the scenario phase ~Nx at the cost of
  164. /// mild engine contention; perf numbers are measured separately.
  165. #[arg(long)]
  166. parallel: Option<u32>,
  167. /// Local tokenizer for llama-benchy (tokenizer.json or a dir with
  168. /// one) — needed when the served id isn't resolvable offline.
  169. #[arg(long)]
  170. tokenizer: Option<String>,
  171. /// Skip the scenario phase — run only the perf micro-benchmarks
  172. /// (use to re-baseline the perf table after a cold first row).
  173. #[arg(long)]
  174. perf_only: bool,
  175. /// Run the context-depth suite (16K..256K) after the main pass — off
  176. /// by default (a full sweep takes ~45 min on a 2x lane).
  177. #[arg(long)]
  178. depth: bool,
  179. /// Run tool-eval-bench on a remote host over SSH instead of locally,
  180. /// so the bench client's own network (a WiFi laptop) stays off the
  181. /// measurement path. The tokenizer is uploaded to the host; the
  182. /// report + run package still land locally. The host must have
  183. /// `tool-eval-bench` on PATH.
  184. #[arg(long)]
  185. ssh_host: Option<String>,
  186. /// Disable thinking/reasoning in the harness (`--no-think`, sets
  187. /// `enable_thinking=false`). Needed for thinking-by-default models
  188. /// (Ling, GLM-4.5-style) whose long default reasoning blows the
  189. /// per-turn timeout and excludes most scenarios.
  190. #[arg(long)]
  191. no_think: bool,
  192. /// Per-turn request timeout seconds forwarded to the harness
  193. /// (`--timeout`). Raise above the 120 s default for slow models.
  194. #[arg(long)]
  195. timeout: Option<u64>,
  196. },
  197. /// Check cluster health
  198. Health {
  199. #[arg(
  200. long,
  201. help = "Cluster name (e.g. 4nodes-switch). If omitted — checks all."
  202. )]
  203. cluster: Option<String>,
  204. #[arg(long, help = "Show node host keys (for adding to config.toml)")]
  205. fingerprint: bool,
  206. /// Output format (cluster-health mode only; ignored with --fingerprint).
  207. #[arg(long, value_enum, default_value_t = OutputFormat::Plain)]
  208. format: OutputFormat,
  209. },
  210. /// Check and repair a recipe (doctor mode)
  211. Doctor {
  212. #[arg(long, required = true, help = "Recipe name to check")]
  213. recipe: String,
  214. #[arg(
  215. long,
  216. default_value = "auto",
  217. help = "Cluster (if not auto — uses recipe.cluster)"
  218. )]
  219. cluster: String,
  220. #[arg(long, help = "Auto-fix issues (create Dockerfile + build image)")]
  221. repair: bool,
  222. },
  223. /// Publish a recipe (+ image overlay) as a standalone reproducible bundle
  224. Publish(PublishArgs),
  225. /// Browse and install recipes from the public registry
  226. #[command(subcommand)]
  227. Registry(RegistryCommands),
  228. /// Manage tmux sessions (Docker alternative)
  229. #[command(subcommand)]
  230. Tmux(TmuxCommands),
  231. /// Auto-discover host keys and write them into config.toml
  232. InitHostKeys {
  233. #[arg(long, help = "Cluster name (e.g. 4nodes-switch). If omitted — all.")]
  234. cluster: Option<String>,
  235. },
  236. /// Run an arbitrary shell command on a cluster or single node
  237. Exec {
  238. /// Cluster name (mutually exclusive with --recipe).
  239. #[arg(long, conflicts_with = "recipe")]
  240. cluster: Option<String>,
  241. /// Recipe name; resolves to the recipe's pinned cluster.
  242. #[arg(long, conflicts_with = "cluster")]
  243. recipe: Option<String>,
  244. /// Target a specific node within the cluster (default: all).
  245. #[arg(long)]
  246. node: Option<String>,
  247. /// Name of an alias from `[exec_aliases]` in config.toml.
  248. /// Mutually exclusive with a trailing command.
  249. #[arg(long)]
  250. alias: Option<String>,
  251. /// Command to run (everything after `--`).
  252. #[arg(trailing_var_arg = true)]
  253. command: Vec<String>,
  254. },
  255. /// Manage configuration (global config path, init)
  256. #[command(subcommand)]
  257. Config(ConfigCommands),
  258. /// Run-ledger management (per-recipe .lock; offline)
  259. #[command(subcommand)]
  260. Runlock(RunlockCommands),
  261. /// Manage the configured git recipe repos (clone/pull cache, archive
  262. /// registry). `sm121 repos` lists state; `sm121 repos sync [--force]`
  263. /// re-pulls.
  264. Repos(ReposArgs),
  265. /// Archive a recipe repo (it is skipped by sync; state in repos.json)
  266. Archive {
  267. /// The repo URL (a `[[recipe_repos]]` entry)
  268. url: String,
  269. },
  270. /// Un-archive a recipe repo (it takes part in sync again)
  271. Unarchive {
  272. /// The repo URL (a `[[recipe_repos]]` entry)
  273. url: String,
  274. },
  275. /// Move a recipe across recipe repos (git mv + commit + push; the
  276. /// target is updated first, so a failure duplicates, never deletes).
  277. /// Default (or `-d`) prints the exact commands; `--apply` executes.
  278. Promote {
  279. /// Recipe name (its directory under the repo's `recipes/`)
  280. recipe: String,
  281. /// Source repo URL (a `[[recipe_repos]]` entry)
  282. #[arg(long, required = true)]
  283. from: String,
  284. /// Target repo URL (a `[[recipe_repos]]` entry)
  285. #[arg(long, required = true)]
  286. to: String,
  287. /// Execute the move (default: print the plan only)
  288. #[arg(long)]
  289. apply: bool,
  290. },
  291. /// Launch the web dashboard (axum + SSE) on a local port
  292. Web {
  293. #[arg(long, default_value_t = 3100)]
  294. port: u16,
  295. #[arg(long, default_value = "127.0.0.1")]
  296. bind: String,
  297. /// Bearer token for data endpoints (required when --bind is not loopback,
  298. /// unless --insecure)
  299. #[arg(long)]
  300. token: Option<String>,
  301. /// Serve WITHOUT authentication (the token check is skipped entirely —
  302. /// anyone who can reach the port can serve/stop/power the cluster). For
  303. /// isolated LANs where the operator explicitly accepts that.
  304. #[arg(long)]
  305. insecure: bool,
  306. /// Open the browser once the server is up
  307. #[arg(long)]
  308. open: bool,
  309. /// Re-sync the configured `[[recipe_repos]]` in the background every
  310. /// N minutes so the dashboard serves fresh recipes without a restart
  311. /// (0 disables; default 10). The HA add-on relies on this instead of
  312. /// its own entrypoint loop.
  313. #[arg(long, default_value_t = 10)]
  314. repo_sync_interval: u32,
  315. },
  316. /// Table of all tested recipes and their benchmark history
  317. Report {
  318. /// Interactive HTML table via --serve (default: print Markdown)
  319. #[arg(long)]
  320. serve: bool,
  321. /// Port for --serve
  322. #[arg(long, default_value_t = 3101)]
  323. port: u16,
  324. /// Open the browser (implies --serve)
  325. #[arg(long)]
  326. open: bool,
  327. #[arg(long, help = "Archive a recipe (hidden from report output)")]
  328. archive: Option<String>,
  329. #[arg(long, help = "Un-archive a recipe")]
  330. unarchive: Option<String>,
  331. #[arg(long, help = "Show the archived recipes instead of the active ones")]
  332. archived: bool,
  333. },
  334. /// Build Docker image for a recipe on a remote node
  335. Build {
  336. #[arg(long, required = true, help = "Recipe name")]
  337. recipe: String,
  338. #[arg(long, required = true, help = "Node to build on (e.g. dgx1.lan)")]
  339. build_node: String,
  340. #[arg(long, default_value = "auto", help = "Cluster name")]
  341. cluster: String,
  342. /// Force rebuild even if checksum matches.
  343. #[arg(long)]
  344. force_build: bool,
  345. /// Skip the post-build registry push (only meaningful when `[registry]`
  346. /// is configured).
  347. #[arg(long)]
  348. no_push: bool,
  349. /// Where the image comes from: build from `docker-images/` (default)
  350. /// or pull the prebuilt tag from `[registry]` (no build, no push).
  351. #[arg(long, value_enum, default_value_t = crate::docker::ImageSource::Repo)]
  352. image_source: crate::docker::ImageSource,
  353. },
  354. /// Power on/off nodes via Home Assistant (smart plugs / Wake-on-LAN).
  355. Power {
  356. #[command(subcommand)]
  357. action: PowerAction,
  358. },
  359. /// Validate recipes against the launcher contract (build dirs exist,
  360. /// port convention, no floating :latest, floating model revisions,
  361. /// node counts). Errors fail the run (exit 1); archived lanes are
  362. /// grandfathered (their errors downgrade to warnings). Also runs as a
  363. /// `cargo test` gate over the repo's own `recipes/`.
  364. Validate,
  365. }
  366. #[cfg(test)]
  367. #[path = "cli_tests.rs"]
  368. mod tests;