mod.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. pub mod commands;
  2. pub mod discover;
  3. pub mod ops;
  4. pub mod serve;
  5. pub mod session;
  6. use anyhow::Result;
  7. use crate::cli::TmuxCommands;
  8. use crate::config::{Config, NcclConfig, Node, Recipe};
  9. /// Manages tmux sessions for model inference across a DGX Spark cluster.
  10. #[derive(Clone)]
  11. pub struct TmuxManager;
  12. impl TmuxManager {
  13. /// Generates a namespaced session name for a recipe and node rank.
  14. /// Format: `{recipe_name}-{engine}-r{rank}`
  15. pub fn session_name(&self, recipe: &Recipe, rank: u32) -> String {
  16. // Replace shell-unsafe characters (parentheses, quotes, etc.)
  17. // with underscores so the session name doesn't trigger glob
  18. // expansion or shell injection when used in tmux commands.
  19. let name = recipe
  20. .name
  21. .chars()
  22. .map(|c| {
  23. if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
  24. c
  25. } else {
  26. '_'
  27. }
  28. })
  29. .collect::<String>();
  30. format!("{}-{}-r{}", name, recipe.engine, rank)
  31. }
  32. /// Builds the list of `export` commands for NCCL environment variables.
  33. pub fn build_nccl_setup(&self, nccl: Option<&NcclConfig>) -> Vec<String> {
  34. match nccl {
  35. Some(nccl) => vec![
  36. format!("export NCCL_IB_HCA=\"{}\"", nccl.ib_hca),
  37. format!("export NCCL_SOCKET_IFNAME=\"{}\"", nccl.socket_ifname),
  38. format!("export NCCL_IB_GID_INDEX={}", nccl.ib_gid_index),
  39. format!("export NCCL_IB_DISABLE={}", nccl.ib_disable),
  40. ],
  41. None => Vec::new(),
  42. }
  43. }
  44. /// Builds engine-specific host IP environment variable.
  45. pub fn build_host_ip_export(&self, engine: &str, node: &Node) -> Vec<String> {
  46. let ip = node.effective_ib_ip();
  47. match engine {
  48. "vllm" => vec![format!("export VLLM_HOST_IP={}", ip)],
  49. "sglang" => vec![format!("export SGLANG_WORKER_IP={}", ip)],
  50. _ => vec![
  51. format!("export VLLM_HOST_IP={}", ip),
  52. format!("export SGLANG_WORKER_IP={}", ip),
  53. ],
  54. }
  55. }
  56. }
  57. /// Detailed health status of a tmux session.
  58. #[derive(Debug, Clone)]
  59. pub struct SessionHealth {
  60. pub session_exists: bool,
  61. pub process_alive: bool,
  62. pub gpu_ok: bool,
  63. pub api_ok: bool,
  64. }
  65. impl SessionHealth {
  66. pub fn is_healthy(&self) -> bool {
  67. self.session_exists && self.process_alive && self.gpu_ok && self.api_ok
  68. }
  69. pub fn is_degraded(&self) -> bool {
  70. self.session_exists && !self.process_alive
  71. }
  72. }
  73. /// Top-level handler for all `cargo run tmux <subcommand>` commands.
  74. pub async fn handle_tmux(config: &Config, cmd: TmuxCommands, dry_run: bool) -> Result<()> {
  75. match cmd {
  76. TmuxCommands::Serve {
  77. recipe,
  78. cluster,
  79. skip_before_serve,
  80. } => {
  81. serve::tmux_serve(
  82. config,
  83. &recipe,
  84. &cluster,
  85. skip_before_serve,
  86. dry_run,
  87. false,
  88. false,
  89. )
  90. .await?;
  91. }
  92. TmuxCommands::Stop { recipe, cluster } => {
  93. ops::tmux_stop(config, &recipe, &cluster, dry_run).await?;
  94. }
  95. TmuxCommands::Status { recipe, cluster } => {
  96. ops::tmux_status(config, &recipe, &cluster, dry_run).await?;
  97. }
  98. TmuxCommands::Logs {
  99. recipe,
  100. follow,
  101. cluster,
  102. } => {
  103. ops::tmux_logs(config, &recipe, &cluster, follow, dry_run).await?;
  104. }
  105. TmuxCommands::Attach { recipe, cluster } => {
  106. ops::tmux_attach(config, &recipe, &cluster, dry_run).await?;
  107. }
  108. }
  109. Ok(())
  110. }