use std::future::Future; use serde::Deserialize; use topcoat::context::Cx; use topcoat::router::{content::Form, href, page}; use topcoat::view::{View, ViewExt, view}; use topcoat::Result; use crate::cli::DeployTarget; use crate::deploy::{assert_local_images_arch, build_images_for, resolve_deploy_arch}; use crate::plan; use crate::ui::app; #[derive(Debug, Deserialize)] pub struct DeployForm { pub target: String, #[serde(default)] pub name: String, #[serde(default)] pub rebuild: Option, } impl DeployForm { fn skip_build(&self) -> bool { self.rebuild.as_deref() != Some("1") } } #[page(POST)] pub async fn action(cx: &Cx, Form(form): Form) -> Result> { let cfg = app(cx).config_snapshot(); let config_path = app(cx).config_path.clone(); let target = form.target.clone(); let name = form.name.clone(); let skip_build = form.skip_build(); let result = tokio::task::spawn_blocking(move || -> anyhow::Result { let opts = plan::PlanOpts { config_path: config_path.clone(), refresh: false, }; let plans = match target.as_str() { "hub" => vec![plan::plan_hub(&cfg, &opts)?], "satellite" => { if name.is_empty() { anyhow::bail!("name required for target=satellite"); } vec![plan::plan_satellite(&cfg, &name, &opts)?] } "all" => plan::plan_all(&cfg, &opts)?, other => anyhow::bail!("unknown target: {other}"), }; plan::print_plans(&plans); if plans.iter().any(|p| p.needs_images()) { let dt = match target.as_str() { "satellite" => DeployTarget::Satellite, "hub" => DeployTarget::Hub, _ => DeployTarget::All, }; let arch = resolve_deploy_arch(&cfg, dt, (!name.is_empty()).then_some(name.as_str()))?; if skip_build { assert_local_images_arch(arch)?; } else { build_images_for(arch)?; } } plan::apply_plans(&plans, false, &config_path)?; Ok(format!("Deploy finished ({target})")) }) .await .map_err(|e| topcoat::Error::msg(e.to_string()))?; match result { Ok(msg) => Ok(view! {

"Overview" "/" "Deploy"

"Deploy complete"

"Full details are also in the terminal running sope ui."

} .boxed()), Err(e) => Ok(view! {

"Overview" "/" "Deploy"

"Deploy failed"

"Fix the error below, then retry."

(format!("{e:#}"))
} .boxed()), } }