1use std::{env::current_dir, os::unix::fs::symlink, path::PathBuf};23use anyhow::Result;4use clap::Parser;5use fleet_base::{6 deploy::{DeployAction, deploy_task, upload_task},7 host::{Config, DeployKind, GenerationStorage},8 opts::FleetOpts,9};10use nix_eval::nix_go;11use tokio::task::{LocalSet, spawn_blocking};12use tracing::{Instrument, error, field, info, info_span, warn};1314#[derive(Parser)]15pub struct Deploy {16 17 #[clap(long)]18 disable_rollback: bool,19 20 action: DeployAction,21}2223#[derive(Parser, Clone)]24pub struct BuildSystems {25 26 27 #[clap(long, default_value = "toplevel-fleet")]28 build_attr: String,29}3031async fn build_task(config: Config, hostname: String, build_attr: &str) -> Result<PathBuf> {32 info!("building");33 let host = config.host(&hostname).await?;34 35 let nixos = host.nixos_config().await?;36 let drv = nix_go!(nixos.system.build[{ build_attr }]);37 let out_output = spawn_blocking(move || drv.build("out"))38 .await39 .expect("system derivation build should not panic")?;4041 42 if !host.local {43 info!("adding gc root");44 let mut cmd = config.local_host().cmd("nix").await?;45 cmd.arg("build")46 .comparg(47 "--profile",48 format!(49 "/nix/var/nix/profiles/{}-{hostname}",50 config.data().gc_root_prefix51 ),52 )53 .arg(&out_output);54 cmd.sudo().run_nix().await?;55 }5657 Ok(out_output)58}5960impl BuildSystems {61 pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {62 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;63 let set = LocalSet::new();64 let build_attr = self.build_attr.clone();65 for host in hosts {66 let config = config.clone();67 let span = info_span!("build", host = field::display(&host.name));68 let hostname = host.name;69 let build_attr = build_attr.clone();70 set.spawn_local(71 (async move {72 let built = match build_task(config, hostname.clone(), &build_attr).await {73 Ok(path) => path,74 Err(e) => {75 error!("failed to deploy host: {}", e);76 return;77 }78 };79 80 let mut out = current_dir().expect("cwd exists");81 out.push(format!("built-{hostname}"));8283 info!("linking iso image to {:?}", out);84 if let Err(e) = symlink(built, out) {85 error!("failed to symlink: {e}")86 }87 })88 .instrument(span),89 );90 }91 set.await;92 Ok(())93 }94}9596impl Deploy {97 pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {98 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;99 let set = LocalSet::new();100 for host in hosts.into_iter() {101 let config = config.clone();102 let span = info_span!("deploy", host = field::display(&host.name));103 let hostname = host.name.clone();104 let opts = opts.clone();105 if let Some(deploy_kind) = opts.action_attr::<DeployKind>(&host, "deploy_kind").await? {106 host.set_deploy_kind(deploy_kind);107 };108 if let Some(destination) = opts.action_attr::<String>(&host, "dest").await? {109 host.set_session_destination(destination);110 };111 if let Some(legacy) = opts.action_attr::<bool>(&host, "legacy_ssh_store").await? {112 host.set_legacy_ssh_store(legacy);113 };114115 set.spawn_local(116 (async move {117 let built = match build_task(config.clone(), hostname.clone(), "toplevel-fleet").await118 {119 Ok(path) => path,120 Err(e) => {121 error!("failed to build host system closure: {:?}", e);122 return;123 }124 };125126 let deploy_kind = match host.deploy_kind().await {127 Ok(v) => v,128 Err(e) => {129 error!("failed to query target deploy kind: {e}");130 return;131 }132 };133134 135 let mut disable_rollback = self.disable_rollback;136 if !disable_rollback && deploy_kind != DeployKind::Fleet {137 warn!("disabling rollback, as not supported by non-fleet deployment kinds");138 disable_rollback = true;139 }140141 let remote_path =142 match upload_task(&config, &host, GenerationStorage::Deployer, built).await143 {144 Ok(v) => v,145 Err(e) => {146 error!("upload failed: {e}");147 return;148 }149 };150151 if let Err(e) = deploy_task(152 self.action,153 &host,154 remote_path,155 match opts.action_attr(&host, "specialisation").await {156 Ok(v) => v,157 _ => {158 error!("unreachable? failed to get specialization");159 return;160 }161 },162 disable_rollback,163 )164 .await165 {166 error!("activation failed: {e}");167 }168 })169 .instrument(span),170 );171 }172 set.await;173 Ok(())174 }175}