main.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use anyhow::Result;
  2. use clap::Parser;
  3. mod before_serve;
  4. mod cli;
  5. mod config;
  6. mod docker;
  7. mod doctor;
  8. mod exec;
  9. mod health;
  10. mod host_keys;
  11. mod ssh;
  12. mod tmux;
  13. use crate::cli::{Cli, Commands};
  14. use crate::config::{Cluster, Config, resolve_cluster_for_recipe, select_cluster_by_name};
  15. use crate::ssh::fetch_host_key;
  16. #[tokio::main]
  17. async fn main() -> Result<()> {
  18. tracing_subscriber::fmt::init();
  19. let cli = Cli::parse();
  20. let raw_config = tokio::fs::read_to_string(&cli.config).await?;
  21. let config: Config = toml::from_str(&raw_config)?;
  22. let dry_run = cli.dry_run;
  23. match cli.command {
  24. Commands::Serve {
  25. recipe,
  26. cluster,
  27. keep,
  28. skip_before_serve,
  29. } => {
  30. docker::serve(&config, &recipe, &cluster, keep, skip_before_serve, dry_run).await?;
  31. }
  32. Commands::Logs {
  33. recipe: recipe_name,
  34. follow,
  35. } => {
  36. let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
  37. docker::show_logs(&config, &cluster, &recipe_name, follow, dry_run).await?;
  38. }
  39. Commands::Stop {
  40. recipe: recipe_name,
  41. } => {
  42. let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
  43. docker::stop_cluster(&config, &cluster, &recipe_name, dry_run).await?;
  44. }
  45. Commands::Health {
  46. cluster: cluster_name,
  47. fingerprint,
  48. } => {
  49. if fingerprint {
  50. let clusters_to_check: Vec<Cluster> = match &cluster_name {
  51. Some(name) => vec![select_cluster_by_name(&config, name)?],
  52. None => config.clusters.clone(),
  53. };
  54. for cluster in &clusters_to_check {
  55. println!("\n=== Кластер: {} ===", cluster.name);
  56. for node in &cluster.nodes {
  57. match fetch_host_key(&config, node).await {
  58. Ok(fp) => {
  59. println!("{} ({}) -> host_key = \"{}\"", node.name, node.ssh_ip, fp)
  60. }
  61. Err(e) => println!("{} ({}) -> error: {}", node.name, node.ssh_ip, e),
  62. }
  63. }
  64. }
  65. } else {
  66. match cluster_name {
  67. Some(ref name) => {
  68. let cluster = select_cluster_by_name(&config, name)?;
  69. docker::health_check(&config, &cluster, dry_run).await?;
  70. }
  71. None => {
  72. for cluster in &config.clusters {
  73. docker::health_check(&config, cluster, dry_run).await?;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. Commands::Doctor {
  80. recipe,
  81. cluster,
  82. repair,
  83. } => {
  84. doctor::doctor(&config, &recipe, &cluster, repair, dry_run).await?;
  85. }
  86. Commands::Tmux(tmux_cmd) => {
  87. crate::tmux::handle_tmux(&config, tmux_cmd, dry_run).await?;
  88. }
  89. Commands::InitHostKeys { cluster } => {
  90. host_keys::init_host_keys(&config, cluster.as_deref(), &cli.config).await?;
  91. }
  92. Commands::Exec {
  93. cluster,
  94. recipe,
  95. node,
  96. command,
  97. } => {
  98. let (cluster, _recipe) =
  99. exec::resolve_target(&config, cluster.as_deref(), recipe.as_deref()).await?;
  100. // Join the trailing args into a single command line.
  101. let cmd_str = command.join(" ");
  102. exec::run(&config, &cluster, node.as_deref(), &cmd_str, dry_run).await?;
  103. }
  104. }
  105. Ok(())
  106. }