main.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #![deny(clippy::wildcard_imports)]
  2. use anyhow::Result;
  3. use clap::Parser;
  4. mod before_serve;
  5. mod cli;
  6. mod config;
  7. mod docker;
  8. mod doctor;
  9. mod exec;
  10. mod health;
  11. mod home_assistant;
  12. mod host_keys;
  13. mod power;
  14. mod pre_serve;
  15. mod ssh;
  16. mod tmux;
  17. use crate::cli::{Cli, Commands};
  18. use crate::config::{
  19. Cluster, Config, default_config_dir, resolve_cluster_for_recipe, select_cluster_by_name,
  20. };
  21. use crate::ssh::fetch_host_key;
  22. /// Resolve the path to the config file.
  23. ///
  24. /// Search order:
  25. /// 1. `--config <path>` from CLI (always wins; error if missing)
  26. /// 2. `./config.toml` in cwd
  27. /// 3. `<default_config_dir>/config.toml`
  28. ///
  29. /// Returns an error if nothing is found, with a hint pointing the user
  30. /// to copy the example config to one of the two paths.
  31. fn resolve_config_path(cli_config: &str) -> std::path::PathBuf {
  32. use std::path::PathBuf;
  33. let explicit = PathBuf::from(cli_config);
  34. // User passed a non-default path: use it as-is.
  35. if cli_config != "config.toml" {
  36. return explicit;
  37. }
  38. // Default value (or no flag): try cwd first, then default folder.
  39. if explicit.exists() {
  40. return explicit;
  41. }
  42. if let Some(dir) = default_config_dir() {
  43. let candidate = dir.join("config.toml");
  44. if candidate.exists() {
  45. return candidate;
  46. }
  47. }
  48. // Fall through; the loader will report a clean error.
  49. explicit
  50. }
  51. #[tokio::main]
  52. async fn main() -> Result<()> {
  53. tracing_subscriber::fmt::init();
  54. let cli = Cli::parse();
  55. let config_path = resolve_config_path(&cli.config);
  56. let raw_config = tokio::fs::read_to_string(&config_path).await?;
  57. let config: Config = toml::from_str(&raw_config)?;
  58. let dry_run = cli.dry_run;
  59. match cli.command {
  60. Commands::Serve {
  61. recipe,
  62. cluster,
  63. keep,
  64. skip_before_serve,
  65. } => {
  66. docker::serve(&config, &recipe, &cluster, keep, skip_before_serve, dry_run).await?;
  67. }
  68. Commands::Logs {
  69. recipe: recipe_name,
  70. follow,
  71. } => {
  72. let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
  73. docker::show_logs(&config, &cluster, &recipe_name, follow, dry_run).await?;
  74. }
  75. Commands::Stop {
  76. recipe: recipe_name,
  77. } => {
  78. let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
  79. docker::stop_cluster(&config, &cluster, &recipe_name, dry_run).await?;
  80. }
  81. Commands::Health {
  82. cluster: cluster_name,
  83. fingerprint,
  84. } => {
  85. if fingerprint {
  86. let clusters_to_check: Vec<Cluster> = match &cluster_name {
  87. Some(name) => vec![select_cluster_by_name(&config, name)?],
  88. None => config.clusters.clone(),
  89. };
  90. for cluster in &clusters_to_check {
  91. println!("\n=== Кластер: {} ===", cluster.name);
  92. for node in &cluster.nodes {
  93. match fetch_host_key(&config, node).await {
  94. Ok(fp) => {
  95. println!("{} ({}) -> host_key = \"{}\"", node.name, node.ssh_ip, fp)
  96. }
  97. Err(e) => println!("{} ({}) -> error: {}", node.name, node.ssh_ip, e),
  98. }
  99. }
  100. }
  101. } else {
  102. match cluster_name {
  103. Some(ref name) => {
  104. let cluster = select_cluster_by_name(&config, name)?;
  105. docker::health_check(&config, &cluster, dry_run).await?;
  106. }
  107. None => {
  108. for cluster in &config.clusters {
  109. docker::health_check(&config, cluster, dry_run).await?;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. Commands::Doctor {
  116. recipe,
  117. cluster,
  118. repair,
  119. } => {
  120. doctor::doctor(&config, &recipe, &cluster, repair, dry_run).await?;
  121. }
  122. Commands::Tmux(tmux_cmd) => {
  123. crate::tmux::handle_tmux(&config, tmux_cmd, dry_run).await?;
  124. }
  125. Commands::InitHostKeys { cluster } => {
  126. host_keys::init_host_keys(
  127. &config,
  128. cluster.as_deref(),
  129. config_path.to_str().unwrap_or("config.toml"),
  130. )
  131. .await?;
  132. }
  133. Commands::Exec {
  134. cluster,
  135. recipe,
  136. node,
  137. alias,
  138. command,
  139. } => {
  140. let (cluster, _recipe) =
  141. exec::resolve_target(&config, cluster.as_deref(), recipe.as_deref()).await?;
  142. // Resolve command: alias takes precedence, otherwise join
  143. // the trailing positional args into a single command line.
  144. let cmd_str = exec::resolve_command(&config, alias.as_deref(), &command)?;
  145. exec::run(&config, &cluster, node.as_deref(), &cmd_str, dry_run).await?;
  146. }
  147. Commands::Power { action } => match action {
  148. cli::PowerAction::Off { cluster, node } => {
  149. let cluster = select_cluster_by_name(&config, &cluster)?;
  150. power::power_off(&config, &cluster, node.as_deref(), dry_run).await?;
  151. }
  152. cli::PowerAction::On { cluster, node } => {
  153. let cluster = select_cluster_by_name(&config, &cluster)?;
  154. power::power_on(&config, &cluster, node.as_deref(), dry_run).await?;
  155. }
  156. cli::PowerAction::Switch { from, to } => {
  157. let from = select_cluster_by_name(&config, &from)?;
  158. let to = select_cluster_by_name(&config, &to)?;
  159. power::power_switch(&config, &from, &to, dry_run).await?;
  160. }
  161. },
  162. }
  163. Ok(())
  164. }