main.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. node,
  76. keep,
  77. skip_before_serve,
  78. } => {
  79. docker::serve(
  80. &config,
  81. &recipe,
  82. &cluster,
  83. node.as_deref(),
  84. keep,
  85. skip_before_serve,
  86. dry_run,
  87. )
  88. .await?;
  89. }
  90. Commands::Logs {
  91. recipe: recipe_name,
  92. follow,
  93. } => {
  94. let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
  95. docker::show_logs(&config, &cluster, &recipe_name, follow, dry_run).await?;
  96. }
  97. Commands::Stop {
  98. recipe: recipe_name,
  99. node,
  100. } => {
  101. let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
  102. docker::stop_cluster(&config, &cluster, &recipe_name, node.as_deref(), dry_run).await?;
  103. }
  104. Commands::Health {
  105. cluster: cluster_name,
  106. fingerprint,
  107. } => {
  108. if fingerprint {
  109. let clusters_to_check: Vec<Cluster> = match &cluster_name {
  110. Some(name) => vec![select_cluster_by_name(&config, name)?],
  111. None => config.clusters.clone(),
  112. };
  113. for cluster in &clusters_to_check {
  114. println!("\n=== Кластер: {} ===", cluster.name);
  115. for node in &cluster.nodes {
  116. match fetch_host_key(&config, node).await {
  117. Ok(fp) => {
  118. println!("{} ({}) -> host_key = \"{}\"", node.name, node.ssh_ip, fp)
  119. }
  120. Err(e) => println!("{} ({}) -> error: {}", node.name, node.ssh_ip, e),
  121. }
  122. }
  123. }
  124. } else {
  125. match cluster_name {
  126. Some(ref name) => {
  127. let cluster = select_cluster_by_name(&config, name)?;
  128. docker::health::health_check(&config, &cluster, dry_run).await?;
  129. }
  130. None => {
  131. for cluster in &config.clusters {
  132. docker::health::health_check(&config, cluster, dry_run).await?;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. Commands::Doctor {
  139. recipe,
  140. cluster,
  141. repair,
  142. } => {
  143. doctor::doctor(&config, &recipe, &cluster, repair, dry_run).await?;
  144. }
  145. Commands::Tmux(tmux_cmd) => {
  146. crate::tmux::handle_tmux(&config, tmux_cmd, dry_run).await?;
  147. }
  148. Commands::InitHostKeys { cluster } => {
  149. host_keys::init_host_keys(
  150. &config,
  151. cluster.as_deref(),
  152. config_path.to_str().unwrap_or("config.toml"),
  153. )
  154. .await?;
  155. }
  156. Commands::Exec {
  157. cluster,
  158. recipe,
  159. node,
  160. alias,
  161. command,
  162. } => {
  163. let (cluster, _recipe) =
  164. exec::resolve_target(&config, cluster.as_deref(), recipe.as_deref()).await?;
  165. // Resolve command: alias takes precedence, otherwise join
  166. // the trailing positional args into a single command line.
  167. let cmd_str = exec::resolve_command(&config, alias.as_deref(), &command)?;
  168. exec::run(&config, &cluster, node.as_deref(), &cmd_str, dry_run).await?;
  169. }
  170. Commands::Power { action } => match action {
  171. cli::PowerAction::Off { cluster, node } => {
  172. let cluster = select_cluster_by_name(&config, &cluster)?;
  173. power::power_off(&config, &cluster, node.as_deref(), dry_run).await?;
  174. }
  175. cli::PowerAction::On { cluster, node } => {
  176. let cluster = select_cluster_by_name(&config, &cluster)?;
  177. power::power_on(&config, &cluster, node.as_deref(), dry_run).await?;
  178. }
  179. cli::PowerAction::Switch { from, to } => {
  180. let from = select_cluster_by_name(&config, &from)?;
  181. let to = select_cluster_by_name(&config, &to)?;
  182. power::power_switch(&config, &from, &to, dry_run).await?;
  183. }
  184. },
  185. }
  186. Ok(())
  187. }