handlers.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //! Per-command handlers extracted from main.rs (the 500-line cap).
  2. //! `main.rs` keeps the clap dispatch; each `handle_*` does the command work.
  3. use crate::cli::{OutputFormat, PowerAction};
  4. use crate::config::Config;
  5. use crate::config::load_recipe;
  6. use crate::config::resolve_cluster;
  7. use crate::config::{Cluster, select_cluster_by_name};
  8. use crate::docker;
  9. use crate::exec;
  10. use crate::power;
  11. use crate::ssh::fetch_host_key;
  12. use anyhow::Result;
  13. use std::collections::HashSet;
  14. use tracing::info;
  15. /// Handle `logs` command: show container logs.
  16. pub(crate) async fn handle_logs(
  17. config: &Config,
  18. recipe_name: &str,
  19. cluster: &str,
  20. follow: bool,
  21. node: Option<String>,
  22. dry_run: bool,
  23. ) -> Result<()> {
  24. let cluster = resolve_cluster_for_op(config, recipe_name, cluster).await?;
  25. // FLARE has no docker container to tail: its log is the orbit session on
  26. // the controller (`flare orbit attach`).
  27. let recipe_obj = load_recipe(recipe_name, &config.recipe_sources).await?;
  28. if recipe_obj.engine == "flare" {
  29. let head = cluster
  30. .nodes
  31. .iter()
  32. .find(|n| n.rank == 0)
  33. .ok_or_else(|| anyhow::anyhow!("no head node (rank 0) in cluster"))?;
  34. let orbit = crate::native::flare::orbit_cluster_for(config, &cluster.name);
  35. let cmd = crate::native::flare::attach_cmd(config, &recipe_obj, &orbit)?;
  36. // `orbit attach` is a long-lived stream; `run_ssh`'s 5 s inactivity
  37. // timeout killed it before any log line reached the operator.
  38. crate::ssh::run_ssh_follow(config, head, cmd, dry_run).await?;
  39. return Ok(());
  40. }
  41. docker::show_logs(
  42. config,
  43. &cluster,
  44. recipe_name,
  45. follow,
  46. node.as_deref(),
  47. dry_run,
  48. )
  49. .await?;
  50. Ok(())
  51. }
  52. /// Handle `health` command: check cluster health or show host key fingerprints.
  53. pub(crate) async fn handle_health(
  54. config: &Config,
  55. cluster_name: Option<String>,
  56. fingerprint: bool,
  57. format: OutputFormat,
  58. ) -> Result<()> {
  59. if fingerprint {
  60. let clusters_to_check: Vec<Cluster> = match &cluster_name {
  61. Some(name) => vec![select_cluster_by_name(config, name)?],
  62. None => config.clusters.clone(),
  63. };
  64. for cluster in &clusters_to_check {
  65. println!(
  66. "\n{}",
  67. fluent_i18n::t!("cluster-section", { "name" => &cluster.name })
  68. );
  69. for node in &cluster.nodes {
  70. match fetch_host_key(config, node).await {
  71. Ok(fp) => println!(
  72. "{}",
  73. fluent_i18n::t!("host-key-line", { "name" => &node.name, "ip" => &node.ssh_ip, "key" => &fp })
  74. ),
  75. Err(e) => println!(
  76. "{}",
  77. fluent_i18n::t!("host-key-error", { "name" => &node.name, "ip" => &node.ssh_ip, "error" => &e.to_string() })
  78. ),
  79. }
  80. }
  81. }
  82. } else {
  83. match cluster_name {
  84. Some(ref name) => {
  85. let cluster = select_cluster_by_name(config, name)?;
  86. docker::health::health_check(config, &cluster, false, format).await?;
  87. }
  88. None => {
  89. let fleet = docker::health::collect(config, None, false).await?;
  90. docker::health::print_fleet(&fleet, format);
  91. }
  92. }
  93. }
  94. Ok(())
  95. }
  96. /// Handle `exec` command: run a command on cluster nodes via SSH.
  97. pub(crate) async fn handle_exec(
  98. config: &Config,
  99. cluster: Option<String>,
  100. recipe: Option<String>,
  101. node: Option<String>,
  102. alias: Option<String>,
  103. command: Vec<String>,
  104. dry_run: bool,
  105. ) -> Result<()> {
  106. let (cluster, _recipe) =
  107. exec::resolve_target(config, cluster.as_deref(), recipe.as_deref()).await?;
  108. let cmd_str = exec::resolve_command(config, alias.as_deref(), &command)?;
  109. exec::run(config, &cluster, node.as_deref(), &cmd_str, dry_run).await?;
  110. Ok(())
  111. }
  112. /// Handle `power` command: power on/off/switch nodes.
  113. pub(crate) async fn handle_power(
  114. config: &Config,
  115. action: PowerAction,
  116. dry_run: bool,
  117. ) -> Result<()> {
  118. match action {
  119. PowerAction::Off { cluster, node } => {
  120. let cluster = select_cluster_by_name(config, &cluster)?;
  121. let nodes = node.into_iter().collect::<Vec<_>>();
  122. power::power_off(config, &cluster, &nodes, dry_run).await?;
  123. }
  124. PowerAction::On { cluster, node } => {
  125. let cluster = select_cluster_by_name(config, &cluster)?;
  126. let nodes = node.into_iter().collect::<Vec<_>>();
  127. let targets = power::select_targets(&cluster, &nodes, "power on")?;
  128. let stale = if dry_run {
  129. Vec::new()
  130. } else {
  131. power::stale_nodes(&targets).await
  132. };
  133. let report = power::power_on(config, &cluster, &nodes, dry_run, &stale).await?;
  134. for line in report {
  135. info!("{}", line);
  136. }
  137. }
  138. PowerAction::Switch { from, to } => {
  139. let from = select_cluster_by_name(config, &from)?;
  140. let to = select_cluster_by_name(config, &to)?;
  141. power::power_switch(config, &from, &to, dry_run).await?;
  142. }
  143. }
  144. Ok(())
  145. }
  146. /// Resolve a cluster for container operations (stop/logs).
  147. /// When `auto`, tries live container label lookup first, then falls back
  148. /// to config-based auto-selection (for dry-run / offline scenarios).
  149. pub(crate) async fn resolve_cluster_for_op(
  150. config: &Config,
  151. recipe_name: &str,
  152. cluster_name: &str,
  153. ) -> Result<Cluster> {
  154. if cluster_name != "auto" {
  155. let recipe = load_recipe(recipe_name, &config.recipe_sources).await?;
  156. let (cluster, _) = resolve_cluster(config, &recipe, cluster_name, &HashSet::new())?;
  157. return Ok(cluster);
  158. }
  159. // Try live container label lookup first
  160. if let Ok(c) = docker::resolve_cluster_by_container_label(config, recipe_name).await {
  161. return Ok(c.clone());
  162. }
  163. // Fall back to config-based auto (dry-run / containers not running)
  164. let recipe = load_recipe(recipe_name, &config.recipe_sources).await?;
  165. let (cluster, _) = resolve_cluster(config, &recipe, "auto", &HashSet::new())?;
  166. Ok(cluster)
  167. }