1use std::{env::current_dir, os::unix::fs::symlink, path::PathBuf, time::Duration};23use anyhow::{anyhow, bail, Result};4use clap::{Parser, ValueEnum};5use fleet_base::{6 host::{Config, ConfigHost, DeployKind},7 opts::FleetOpts,8};9use itertools::Itertools as _;10use nix_eval::{nix_go, NixBuildBatch};11use tokio::{task::LocalSet, time::sleep};12use tracing::{error, field, info, info_span, warn, Instrument};1314#[derive(Parser)]15pub struct Deploy {16 17 #[clap(long)]18 disable_rollback: bool,19 20 action: DeployAction,21}2223#[derive(ValueEnum, Clone, Copy)]24enum DeployAction {25 26 Upload,27 28 Test,29 30 Boot,31 32 Switch,33}3435impl DeployAction {36 pub(crate) fn name(&self) -> Option<&'static str> {37 match self {38 Self::Upload => None,39 Self::Test => Some("test"),40 Self::Boot => Some("boot"),41 Self::Switch => Some("switch"),42 }43 }44 pub(crate) fn should_switch_profile(&self) -> bool {45 matches!(self, Self::Switch | Self::Boot)46 }47 pub(crate) fn should_activate(&self) -> bool {48 matches!(self, Self::Switch | Self::Test | Self::Boot)49 }50 pub(crate) fn should_create_rollback_marker(&self) -> bool {51 52 53 !matches!(self, Self::Upload)54 }55 pub(crate) fn should_schedule_rollback_run(&self) -> bool {56 matches!(self, Self::Switch | Self::Test)57 }58}5960#[derive(Parser, Clone)]61pub struct BuildSystems {62 63 64 #[clap(long, default_value = "toplevel")]65 build_attr: String,66}6768struct Generation {69 id: u32,70 current: bool,71 datetime: String,72}7374fn parse_generation_line(g: &str) -> Option<Generation> {75 let mut parts = g.split_whitespace();76 let id = parts.next()?;77 let id: u32 = id.parse().ok()?;78 let date = parts.next()?;79 let time = parts.next()?;80 let current = if let Some(current) = parts.next() {81 if current == "(current)" {82 Some(true)83 } else {84 None85 }86 } else {87 Some(false)88 };89 let current = current?;90 if parts.next().is_some() {91 warn!("unexpected text after generation: {g}");92 }93 Some(Generation {94 id,95 current,96 datetime: format!("{date} {time}"),97 })98}99100async fn get_current_generation(host: &ConfigHost) -> Result<Generation> {101 let mut cmd = host.cmd("nix-env").await?;102 cmd.comparg("--profile", "/nix/var/nix/profiles/system")103 .arg("--list-generations");104 105 let data = cmd.sudo().run_string().await?;106 let generations = data107 .split('\n')108 .map(|e| e.trim())109 .filter(|&l| !l.is_empty())110 .filter_map(|g| {111 let gen = parse_generation_line(g);112 if gen.is_none() {113 warn!("bad generation: {g}");114 }115 gen116 })117 .collect::<Vec<_>>();118 let current = generations119 .into_iter()120 .filter(|g| g.current)121 .at_most_one()122 .map_err(|_e| anyhow!("bad list-generations output"))?123 .ok_or_else(|| anyhow!("failed to find generation"))?;124 Ok(current)125}126127async fn deploy_task(128 action: DeployAction,129 host: &ConfigHost,130 built: PathBuf,131 specialisation: Option<String>,132 disable_rollback: bool,133) -> Result<()> {134 let deploy_kind = host.deploy_kind().await?;135 if deploy_kind == DeployKind::NixosInstall136 && !matches!(action, DeployAction::Boot | DeployAction::Upload)137 {138 bail!("nixos-install deploy kind only supports boot and upload actions");139 }140141 let mut failed = false;142143 144 145 146 147 148 if !disable_rollback && action.should_create_rollback_marker() {149 let _span = info_span!("preparing").entered();150 info!("preparing for rollback");151 let generation = get_current_generation(host).await?;152 info!(153 "rollback target would be {} {}",154 generation.id, generation.datetime155 );156 {157 let mut cmd = host.cmd("sh").await?;158 cmd.arg("-c").arg(format!("mark=$(mktemp -p /etc -t fleet_rollback_marker.XXXXX) && echo -n {} > $mark && mv --no-clobber $mark /etc/fleet_rollback_marker", generation.id));159 if let Err(e) = cmd.sudo().run().await {160 error!("failed to set rollback marker: {e}");161 failed = true;162 }163 }164 165 166 167 168 169170 171 172 173 174 if action.should_schedule_rollback_run() {175 let mut cmd = host.cmd("systemd-run").await?;176 cmd.comparg("--on-active", "3min")177 .comparg("--unit", "rollback-watchdog-run")178 .arg("systemctl")179 .arg("start")180 .arg("rollback-watchdog.service");181 if let Err(e) = cmd.sudo().run().await {182 error!("failed to schedule rollback run: {e}");183 failed = true;184 }185 }186 }187 if deploy_kind == DeployKind::NixosInstall {188 info!(189 "running nixos-install to switch profile, install bootloader, and perform activation"190 );191 let mut cmd = host.cmd("nixos-install").await?;192 cmd.arg("--system").arg(&built).args([193 194 195 "--no-channel-copy",196 "--root",197 "/mnt",198 ]);199 if let Err(e) = cmd.sudo().run().await {200 error!("failed to execute nixos-install: {e}");201 failed = true;202 }203 } else {204 if action.should_switch_profile() && !failed {205 info!("switching system profile generation");206207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 let mut cmd = host.nix_cmd().await?;227 cmd.arg("build");228 cmd.comparg("--profile", "/nix/var/nix/profiles/system");229 cmd.arg(&built);230 if let Err(e) = cmd.sudo().run_nix().await {231 error!("failed to switch system profile generation: {e}");232 failed = true;233 }234 }235236 237238 if action.should_activate() && !failed {239 let _span = info_span!("activating").entered();240 info!("executing activation script");241 let specialised = if let Some(specialisation) = specialisation {242 let mut specialised = built.join("specialisation");243 specialised.push(specialisation);244 specialised245 } else {246 built.clone()247 };248 let switch_script = specialised.join("bin/switch-to-configuration");249 let mut cmd = host.cmd(switch_script).in_current_span().await?;250 cmd.arg(action.name().expect("upload.should_activate == false"));251 if let Err(e) = cmd.sudo().run().in_current_span().await {252 error!("failed to activate: {e}");253 failed = true;254 }255 }256 }257 if action.should_create_rollback_marker() {258 if !disable_rollback {259 if failed {260 if action.should_schedule_rollback_run() {261 info!("executing rollback");262 if let Err(e) = host263 .systemctl_start("rollback-watchdog.service")264 .instrument(info_span!("rollback"))265 .await266 {267 error!("failed to trigger rollback: {e}")268 }269 }270 } else {271 info!("trying to mark upgrade as successful");272 if let Err(e) = host273 .rm_file("/etc/fleet_rollback_marker", true)274 .in_current_span()275 .await276 {277 error!("failed to remove rollback marker. This is bad, as the system will be rolled back by watchdog: {e}")278 }279 }280 info!("disarming watchdog, just in case");281 if let Err(_e) = host.systemctl_stop("rollback-watchdog.timer").await {282 283 }284 if action.should_schedule_rollback_run() {285 if let Err(e) = host.systemctl_stop("rollback-watchdog-run.timer").await {286 error!("failed to disarm rollback run: {e}");287 }288 }289 } else if let Err(_e) = host290 .rm_file("/etc/fleet_rollback_marker", true)291 .in_current_span()292 .await293 {294 295 }296 }297 Ok(())298}299300async fn build_task(301 config: Config,302 hostname: String,303 build_attr: &str,304 batch: Option<NixBuildBatch>,305) -> Result<PathBuf> {306 info!("building");307 let host = config.host(&hostname).await?;308 309 let nixos = host.nixos_config().await?;310 let drv = nix_go!(nixos.system.build[{ build_attr }]);311 let outputs = drv.build_maybe_batch(batch).await?;312 let out_output = outputs313 .get("out")314 .ok_or_else(|| anyhow!("system build should produce \"out\" output"))?;315316 {317 info!("adding gc root");318 let mut cmd = config.local_host().cmd("nix").await?;319 cmd.arg("build")320 .comparg(321 "--profile",322 format!(323 "/nix/var/nix/profiles/{}-{hostname}",324 config.data().gc_root_prefix325 ),326 )327 .arg(out_output);328 cmd.sudo().run_nix().await?;329 }330331 Ok(out_output.clone())332}333334impl BuildSystems {335 pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {336 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;337 let set = LocalSet::new();338 let build_attr = self.build_attr.clone();339 let batch = (hosts.len() > 1).then(|| {340 config341 .nix_session342 .new_build_batch("build-hosts".to_string())343 });344 for host in hosts {345 let config = config.clone();346 let span = info_span!("build", host = field::display(&host.name));347 let hostname = host.name;348 let build_attr = build_attr.clone();349 let batch = batch.clone();350 set.spawn_local(351 (async move {352 let built = match build_task(config, hostname.clone(), &build_attr, batch).await353 {354 Ok(path) => path,355 Err(e) => {356 error!("failed to deploy host: {}", e);357 return;358 }359 };360 361 let mut out = current_dir().expect("cwd exists");362 out.push(format!("built-{}", hostname));363364 info!("linking iso image to {:?}", out);365 if let Err(e) = symlink(built, out) {366 error!("failed to symlink: {e}")367 }368 })369 .instrument(span),370 );371 }372 drop(batch);373 set.await;374 Ok(())375 }376}377378impl Deploy {379 pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {380 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;381 let set = LocalSet::new();382 let batch = (hosts.len() > 1).then(|| {383 config384 .nix_session385 .new_build_batch("deploy-hosts".to_string())386 });387 for host in hosts.into_iter() {388 let config = config.clone();389 let span = info_span!("deploy", host = field::display(&host.name));390 let hostname = host.name.clone();391 let local_host = config.local_host();392 let opts = opts.clone();393 let batch = batch.clone();394 if let Some(deploy_kind) = opts.action_attr::<DeployKind>(&host, "deploy_kind").await? {395 host.set_deploy_kind(deploy_kind);396 };397398 set.spawn_local(399 (async move {400 let built =401 match build_task(config.clone(), hostname.clone(), "toplevel", batch).await402 {403 Ok(path) => path,404 Err(e) => {405 error!("failed to build host system closure: {}", e);406 return;407 }408 };409410 let deploy_kind = match host.deploy_kind().await {411 Ok(v) => v,412 Err(e) => {413 error!("failed to query target deploy kind: {e}");414 return;415 }416 };417418 419 let mut disable_rollback = self.disable_rollback;420 if !disable_rollback && deploy_kind != DeployKind::Fleet {421 warn!("disabling rollback, as not supported by non-fleet deployment kinds");422 disable_rollback = true;423 }424425 if !opts.is_local(&hostname) {426 info!("uploading system closure");427 {428 429 430 431 432 433 let Ok(mut sign) = local_host.cmd("nix").await else {434 error!("failed to setup local");435 return;436 };437 438 sign.arg("store")439 .arg("sign")440 .comparg("--key-file", "/etc/nix/private-key")441 .arg("-r")442 .arg(&built);443 if let Err(e) = sign.sudo().run_nix().await {444 warn!("failed to sign store paths: {e}");445 };446 }447 let mut tries = 0;448 loop {449 match host.remote_derivation(&built).await {450 Ok(remote) => {451 assert!(remote == built, "CA derivations aren't implemented");452 break;453 }454 Err(e) if tries < 3 => {455 tries += 1;456 warn!("copy failure ({}/3): {}", tries, e);457 sleep(Duration::from_millis(5000)).await;458 }459 Err(e) => {460 error!("upload failed: {e}");461 return;462 }463 }464 }465 }466 if let Err(e) = deploy_task(467 self.action,468 &host,469 built,470 if let Ok(v) = opts.action_attr(&host, "specialisation").await {471 v472 } else {473 error!("unreachable? failed to get specialization");474 return;475 },476 disable_rollback,477 )478 .await479 {480 error!("activation failed: {e}");481 }482 })483 .instrument(span),484 );485 }486 drop(batch);487 set.await;488 Ok(())489 }490}