main.rs 3.0 KB

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