1use std::process::Command;23use crate::{4 command::CommandExt,5 db::{keys::list_hosts, secret::SecretDb, Db, DbData},6 nix::SYSTEMS_ATTRIBUTE,7};8use anyhow::Result;9use clap::Clap;10use log::{info, warn};1112#[derive(Clap)]13#[clap(group = clap::ArgGroup::new("target"))]14pub struct BuildSystems {15 16 #[clap(long, number_of_values = 1, group = "target")]17 skip: Vec<String>,18 19 #[clap(long, number_of_values = 1, group = "target")]20 only: Vec<String>,21 22 #[clap(long, env = "FLEET_LOCALHOST")]23 localhost: Option<String>,24 25 #[clap(long)]26 builders: Option<String>,27 28 #[clap(long)]29 jobs: Option<usize>,30 31 #[clap(long)]32 fail_fast: bool,33 #[clap(subcommand)]34 subcommand: Option<Subcommand>,35}3637#[derive(Clap)]38enum Subcommand {39 40 Test,41 42 Boot,43 44 Switch,45}46impl Subcommand {47 fn should_switch_profile(&self) -> bool {48 matches!(self, Self::Test | Self::Switch)49 }50 fn name(&self) -> &'static str {51 match self {52 Self::Test => "test",53 Self::Boot => "boot",54 Self::Switch => "switch",55 }56 }57}5859impl BuildSystems {60 pub fn run(self) -> Result<()> {61 let db = Db::new(".fleet")?;62 let hosts = list_hosts()?;63 let data = SecretDb::open(&db)?.generate_nix_data()?;6465 for host in hosts.iter() {66 if self.only.len() > 0 && !self.only.contains(host) || self.skip.contains(host) {67 warn!("Skipping host {}", host);68 continue;69 }70 let is_local = Some(host) == self.localhost.as_ref();71 info!("Building host {}", host);72 let built = {73 let dir = tempfile::tempdir()?;74 dir.path().to_owned()75 };7677 let mut nix_build = Command::new("nix");78 nix_build79 .args(&["build", "--impure", "--no-link", "--out-link"])80 .arg(&built)81 .arg(format!(82 "{}.{}.config.system.build.toplevel",83 SYSTEMS_ATTRIBUTE, host,84 ))85 .env("SECRET_DATA", data.clone());8687 if let Some(builders) = &self.builders {88 println!("Using builders: {}", builders);89 nix_build.arg("--builders").arg(builders);90 }91 if let Some(jobs) = &self.jobs {92 nix_build.arg("--max-jobs");93 nix_build.arg(format!("{}", jobs));94 }95 if !self.fail_fast {96 nix_build.arg("--keep-going");97 }9899 nix_build.inherit_stdio().run()?;100 let built = std::fs::canonicalize(built)?;101 info!("Built closure: {:?}", built);102 if !is_local {103 info!("Uploading system closure");104 Command::new("nix")105 .args(&["copy", "--to"])106 .arg(format!("ssh://root@{}", host))107 .arg(&built)108 .inherit_stdio()109 .run()?;110 }111 if let Some(subcommand) = &self.subcommand {112 if subcommand.should_switch_profile() {113 info!("Switching generation");114 if !is_local {115 Command::ssh_on(host, "sudo")116 } else {117 Command::new("sudo")118 }119 .args(&["nix-env", "-p", "/nix/var/nix/profiles/system", "--set"])120 .arg(&built)121 .inherit_stdio()122 .run()?;123 }124 info!("Executing activation script");125 let mut switch_script = built.clone();126 switch_script.push("bin");127 switch_script.push("switch-to-configuration");128 info!("{:?}", switch_script);129 if !is_local {130 Command::ssh_on(host, "sudo")131 } else {132 Command::new("sudo")133 }134 .arg(switch_script)135 .arg(subcommand.name())136 .inherit_stdio()137 .run()?;138 }139 }140 Ok(())141 }142}