difftreelog
refactor(build-systems) host abstraction
in: trunk
1 file changed
src/cmds/build_systems.rsdiffbeforeafterboth223use crate::{3use crate::{4 command::CommandExt,4 command::CommandExt,5 db::{keys::list_hosts, secret::SecretDb, Db, DbData},5 db::{secret::SecretDb, Db, DbData},6 host::FleetOpts,6 nix::SYSTEMS_ATTRIBUTE,7 nix::SYSTEMS_ATTRIBUTE,7};8};8use anyhow::Result;9use anyhow::Result;12#[derive(Clap)]13#[derive(Clap)]13#[clap(group = clap::ArgGroup::new("target"))]14#[clap(group = clap::ArgGroup::new("target"))]14pub struct BuildSystems {15pub struct BuildSystems {15 /// Hosts to skip16 #[clap(long, number_of_values = 1, group = "target")]16 #[clap(flatten)]17 skip: Vec<String>,17 fleet_opts: FleetOpts,18 /// Hosts to build18 /// --builders arg for nix19 #[clap(long, number_of_values = 1, group = "target")]19 #[clap(long)]20 only: Vec<String>,21 /// Host, which should be threaten as localhost22 #[clap(long, env = "FLEET_LOCALHOST")]23 localhost: Option<String>,20 builders: Option<String>,24 /// --builders arg for nix21 /// Jobs to run locally25 #[clap(long)]22 #[clap(long)]26 builders: Option<String>,23 jobs: Option<usize>,27 /// Jobs to run locally24 /// Do not continue on error28 #[clap(long)]25 #[clap(long)]29 jobs: Option<usize>,26 fail_fast: bool,30 /// Do not continue on error31 #[clap(long)]27 #[clap(long)]32 fail_fast: bool,28 privileged_build: bool,33 #[clap(subcommand)]29 #[clap(subcommand)]34 subcommand: Option<Subcommand>,30 subcommand: Option<Subcommand>,35}31}585459impl BuildSystems {55impl BuildSystems {60 pub fn run(self) -> Result<()> {56 pub fn run(self) -> Result<()> {57 let fleet = self.fleet_opts.build()?;61 let db = Db::new(".fleet")?;58 let db = Db::new(".fleet")?;62 let hosts = list_hosts()?;59 let hosts = fleet.list_hosts()?;63 let data = SecretDb::open(&db)?.generate_nix_data()?;60 let data = SecretDb::open(&db)?.generate_nix_data()?;646165 for host in hosts.iter() {62 for host in hosts.iter() {66 if self.only.len() > 0 && !self.only.contains(host) || self.skip.contains(host) {63 if host.skip() {67 warn!("Skipping host {}", host);64 warn!("Skipping host {}", host.hostname);68 continue;65 continue;69 }66 }70 let is_local = Some(host) == self.localhost.as_ref();71 info!("Building host {}", host);67 info!("Building host {}", host.hostname);72 let built = {68 let built = {73 let dir = tempfile::tempdir()?;69 let dir = tempfile::tempdir()?;74 dir.path().to_owned()70 dir.path().to_owned()75 };71 };767277 let mut nix_build = Command::new("nix");73 let mut nix_build = if self.privileged_build {74 let mut out = Command::new("sudo");75 out.arg("nix");76 out77 } else {78 Command::new("nix")79 };78 nix_build80 nix_build79 .args(&["build", "--impure", "--no-link", "--out-link"])81 .args(&["build", "--impure", "--no-link", "--out-link"])80 .arg(&built)82 .arg(&built)81 .arg(format!(83 .arg(format!(82 "{}.{}.config.system.build.toplevel",84 "{}.{}.config.system.build.toplevel",83 SYSTEMS_ATTRIBUTE, host,85 SYSTEMS_ATTRIBUTE, host.hostname,84 ))86 ))85 .env("SECRET_DATA", data.clone());87 .env("SECRET_DATA", data.clone());868899 nix_build.inherit_stdio().run()?;101 nix_build.inherit_stdio().run()?;100 let built = std::fs::canonicalize(built)?;102 let built = std::fs::canonicalize(built)?;101 info!("Built closure: {:?}", built);103 info!("Built closure: {:?}", built);102 if !is_local {104 if !host.is_local() {103 info!("Uploading system closure");105 info!("Uploading system closure");104 Command::new("nix")106 Command::new("nix")105 .args(&["copy", "--to"])107 .args(&["copy", "--to"])106 .arg(format!("ssh://root@{}", host))108 .arg(format!("ssh://root@{}", host.hostname))107 .arg(&built)109 .arg(&built)108 .inherit_stdio()110 .inherit_stdio()109 .run()?;111 .run()?;110 }112 }111 if let Some(subcommand) = &self.subcommand {113 if let Some(subcommand) = &self.subcommand {112 if subcommand.should_switch_profile() {114 if subcommand.should_switch_profile() {113 info!("Switching generation");115 info!("Switching generation");114 if !is_local {115 Command::ssh_on(host, "sudo")116 host.command_on("nix-env", true)116 } else {117 Command::new("sudo")118 }119 .args(&["nix-env", "-p", "/nix/var/nix/profiles/system", "--set"])117 .args(&["-p", "/nix/var/nix/profiles/system", "--set"])120 .arg(&built)118 .arg(&built)121 .inherit_stdio()119 .inherit_stdio()122 .run()?;120 .run()?;126 switch_script.push("bin");124 switch_script.push("bin");127 switch_script.push("switch-to-configuration");125 switch_script.push("switch-to-configuration");128 info!("{:?}", switch_script);126 info!("{:?}", switch_script);129 if !is_local {130 Command::ssh_on(host, "sudo")127 host.command_on(switch_script, true)131 } else {132 Command::new("sudo")133 }134 .arg(switch_script)135 .arg(subcommand.name())128 .arg(subcommand.name())136 .inherit_stdio()129 .inherit_stdio()137 .run()?;130 .run()?;