| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #![deny(clippy::wildcard_imports)]
- use anyhow::Result;
- use clap::Parser;
- mod before_serve;
- mod cli;
- mod config;
- mod docker;
- mod doctor;
- mod exec;
- mod health;
- mod home_assistant;
- mod host_keys;
- mod power;
- mod pre_serve;
- mod ssh;
- mod tmux;
- use crate::cli::{Cli, Commands};
- use crate::config::{
- Cluster, Config, default_config_dir, resolve_cluster_for_recipe, select_cluster_by_name,
- };
- use crate::ssh::fetch_host_key;
- /// Resolve the path to the config file.
- ///
- /// Search order:
- /// 1. `--config <path>` from CLI (always wins; error if missing)
- /// 2. `./config.toml` in cwd
- /// 3. `<default_config_dir>/config.toml`
- ///
- /// Returns an error if nothing is found, with a hint pointing the user
- /// to copy the example config to one of the two paths.
- fn resolve_config_path(cli_config: &str) -> std::path::PathBuf {
- use std::path::PathBuf;
- let explicit = PathBuf::from(cli_config);
- // User passed a non-default path: use it as-is.
- if cli_config != "config.toml" {
- return explicit;
- }
- // Default value (or no flag): try cwd first, then default folder.
- if explicit.exists() {
- return explicit;
- }
- if let Some(dir) = default_config_dir() {
- let candidate = dir.join("config.toml");
- if candidate.exists() {
- return candidate;
- }
- }
- // Fall through; the loader will report a clean error.
- explicit
- }
- #[tokio::main]
- async fn main() -> Result<()> {
- tracing_subscriber::fmt::init();
- let cli = Cli::parse();
- let config_path = resolve_config_path(&cli.config);
- let raw_config = tokio::fs::read_to_string(&config_path).await?;
- let config: Config = toml::from_str(&raw_config)?;
- let dry_run = cli.dry_run;
- match cli.command {
- Commands::Serve {
- recipe,
- cluster,
- keep,
- skip_before_serve,
- } => {
- docker::serve(&config, &recipe, &cluster, keep, skip_before_serve, dry_run).await?;
- }
- Commands::Logs {
- recipe: recipe_name,
- follow,
- } => {
- let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
- docker::show_logs(&config, &cluster, &recipe_name, follow, dry_run).await?;
- }
- Commands::Stop {
- recipe: recipe_name,
- } => {
- let (cluster, _recipe) = resolve_cluster_for_recipe(&config, &recipe_name).await?;
- docker::stop_cluster(&config, &cluster, &recipe_name, dry_run).await?;
- }
- Commands::Health {
- cluster: cluster_name,
- fingerprint,
- } => {
- if fingerprint {
- let clusters_to_check: Vec<Cluster> = match &cluster_name {
- Some(name) => vec![select_cluster_by_name(&config, name)?],
- None => config.clusters.clone(),
- };
- for cluster in &clusters_to_check {
- println!("\n=== Кластер: {} ===", cluster.name);
- for node in &cluster.nodes {
- match fetch_host_key(&config, node).await {
- Ok(fp) => {
- println!("{} ({}) -> host_key = \"{}\"", node.name, node.ssh_ip, fp)
- }
- Err(e) => println!("{} ({}) -> error: {}", node.name, node.ssh_ip, e),
- }
- }
- }
- } else {
- match cluster_name {
- Some(ref name) => {
- let cluster = select_cluster_by_name(&config, name)?;
- docker::health_check(&config, &cluster, dry_run).await?;
- }
- None => {
- for cluster in &config.clusters {
- docker::health_check(&config, cluster, dry_run).await?;
- }
- }
- }
- }
- }
- Commands::Doctor {
- recipe,
- cluster,
- repair,
- } => {
- doctor::doctor(&config, &recipe, &cluster, repair, dry_run).await?;
- }
- Commands::Tmux(tmux_cmd) => {
- crate::tmux::handle_tmux(&config, tmux_cmd, dry_run).await?;
- }
- Commands::InitHostKeys { cluster } => {
- host_keys::init_host_keys(
- &config,
- cluster.as_deref(),
- config_path.to_str().unwrap_or("config.toml"),
- )
- .await?;
- }
- Commands::Exec {
- cluster,
- recipe,
- node,
- alias,
- command,
- } => {
- let (cluster, _recipe) =
- exec::resolve_target(&config, cluster.as_deref(), recipe.as_deref()).await?;
- // Resolve command: alias takes precedence, otherwise join
- // the trailing positional args into a single command line.
- let cmd_str = exec::resolve_command(&config, alias.as_deref(), &command)?;
- exec::run(&config, &cluster, node.as_deref(), &cmd_str, dry_run).await?;
- }
- Commands::Power { action } => match action {
- cli::PowerAction::Off { cluster, node } => {
- let cluster = select_cluster_by_name(&config, &cluster)?;
- power::power_off(&config, &cluster, node.as_deref(), dry_run).await?;
- }
- cli::PowerAction::On { cluster, node } => {
- let cluster = select_cluster_by_name(&config, &cluster)?;
- power::power_on(&config, &cluster, node.as_deref(), dry_run).await?;
- }
- cli::PowerAction::Switch { from, to } => {
- let from = select_cluster_by_name(&config, &from)?;
- let to = select_cluster_by_name(&config, &to)?;
- power::power_switch(&config, &from, &to, dry_run).await?;
- }
- },
- }
- Ok(())
- }
|