mod build_job; mod cli; mod config; mod deploy; mod help; mod plan; mod progress; mod render; mod ssh; mod status; mod ui; #[cfg(test)] mod tests; use anyhow::{bail, Result}; use clap::Parser; use cli::{Cli, Command, DeployTarget}; fn require_name(name: Option, verb: &str) -> Result { name.filter(|s| !s.is_empty()) .ok_or_else(|| anyhow::anyhow!("--name is required for {verb} satellite")) } fn main() -> Result<()> { let args: Vec = std::env::args().collect(); if help::maybe_print(&args) { return Ok(()); } let cli = Cli::parse(); let config_path = &cli.config; match cli.command { Command::Build { arch } => { let arch = match arch { Some(a) => deploy::TargetArch::parse(&a)?, None => deploy::TargetArch::host_default(), }; deploy::build_images_for(arch)?; } Command::Render { out } => { let cfg = config::load(config_path)?; let out_dir = out.unwrap_or_else(|| std::path::PathBuf::from(".sope")); render::render_all(&cfg, &out_dir)?; println!("Wrote artefacts to {}", out_dir.display()); } Command::Plan { target, name, refresh, } => { let cfg = config::load(config_path)?; let opts = plan::PlanOpts { config_path: config_path.clone(), refresh, }; let plans = match target { DeployTarget::All => plan::plan_all(&cfg, &opts)?, DeployTarget::Hub => vec![plan::plan_hub(&cfg, &opts)?], DeployTarget::Satellite => { vec![plan::plan_satellite( &cfg, &require_name(name, "plan")?, &opts, )?] } }; if matches!(target, DeployTarget::All) { plan::prune_state(config_path, &cfg)?; } plan::print_plans(&plans); println!("state: {}", plan::state_path(config_path).display()); } Command::Deploy { target, name, skip_build, full, refresh, } => { let cfg = config::load(config_path)?; let opts = plan::PlanOpts { config_path: config_path.clone(), refresh, }; let sat_name = match target { DeployTarget::Satellite => Some(require_name(name, "deploy")?), _ => name.filter(|s| !s.is_empty()), }; let plans = match target { DeployTarget::All => plan::plan_all(&cfg, &opts)?, DeployTarget::Hub => vec![plan::plan_hub(&cfg, &opts)?], DeployTarget::Satellite => { vec![plan::plan_satellite( &cfg, sat_name.as_deref().unwrap(), &opts, )?] } }; plan::print_plans(&plans); let needs_images = full || plans.iter().any(|p| p.needs_images()); if needs_images { let arch = deploy::resolve_deploy_arch(&cfg, target, sat_name.as_deref())?; if skip_build { deploy::assert_local_images_arch(arch)?; } else { deploy::build_images_for(arch)?; } } plan::apply_plans(&plans, full, config_path)?; if matches!(target, DeployTarget::All) { plan::prune_state(config_path, &cfg)?; } } Command::Status { target, name } => { let cfg = config::load(config_path)?; match target { DeployTarget::All => { deploy::status_hub(&cfg)?; for sat in &cfg.satellites { deploy::status_satellite(&cfg, &sat.name)?; } } DeployTarget::Hub => deploy::status_hub(&cfg)?, DeployTarget::Satellite => { deploy::status_satellite(&cfg, &require_name(name, "status")?)? } } } Command::Ui { bind, port } => { let cfg = config::load(config_path)?; ui::run(config_path.clone(), cfg, &bind, port)?; } Command::Destroy { target, name, yes } => { if !yes { bail!( "irreversible — re-run with --yes (e.g. sope destroy all --yes)" ); } let cfg = config::load(config_path)?; match target { DeployTarget::All => { deploy::destroy_all(&cfg)?; let _ = std::fs::remove_file(plan::state_path(config_path)); } DeployTarget::Hub => { deploy::destroy_hub(&cfg)?; plan::forget_unit(config_path, "hub")?; } DeployTarget::Satellite => { let n = require_name(name, "destroy")?; deploy::destroy_satellite(&cfg, &n)?; plan::forget_unit(config_path, &format!("satellite:{n}"))?; } } } } Ok(()) }