main.rs 6.4 KB

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