From 238480b57dc17642489972521e9f4c598aae5364 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 07 Feb 2021 11:45:08 +0000 Subject: [PATCH] refactor(build-systems): host abstraction --- --- a/src/cmds/build_systems.rs +++ b/src/cmds/build_systems.rs @@ -2,7 +2,8 @@ use crate::{ command::CommandExt, - db::{keys::list_hosts, secret::SecretDb, Db, DbData}, + db::{secret::SecretDb, Db, DbData}, + host::FleetOpts, nix::SYSTEMS_ATTRIBUTE, }; use anyhow::Result; @@ -12,15 +13,8 @@ #[derive(Clap)] #[clap(group = clap::ArgGroup::new("target"))] pub struct BuildSystems { - /// Hosts to skip - #[clap(long, number_of_values = 1, group = "target")] - skip: Vec, - /// Hosts to build - #[clap(long, number_of_values = 1, group = "target")] - only: Vec, - /// Host, which should be threaten as localhost - #[clap(long, env = "FLEET_LOCALHOST")] - localhost: Option, + #[clap(flatten)] + fleet_opts: FleetOpts, /// --builders arg for nix #[clap(long)] builders: Option, @@ -30,6 +24,8 @@ /// Do not continue on error #[clap(long)] fail_fast: bool, + #[clap(long)] + privileged_build: bool, #[clap(subcommand)] subcommand: Option, } @@ -58,29 +54,35 @@ impl BuildSystems { pub fn run(self) -> Result<()> { + let fleet = self.fleet_opts.build()?; let db = Db::new(".fleet")?; - let hosts = list_hosts()?; + let hosts = fleet.list_hosts()?; let data = SecretDb::open(&db)?.generate_nix_data()?; for host in hosts.iter() { - if self.only.len() > 0 && !self.only.contains(host) || self.skip.contains(host) { - warn!("Skipping host {}", host); + if host.skip() { + warn!("Skipping host {}", host.hostname); continue; } - let is_local = Some(host) == self.localhost.as_ref(); - info!("Building host {}", host); + info!("Building host {}", host.hostname); let built = { let dir = tempfile::tempdir()?; dir.path().to_owned() }; - let mut nix_build = Command::new("nix"); + let mut nix_build = if self.privileged_build { + let mut out = Command::new("sudo"); + out.arg("nix"); + out + } else { + Command::new("nix") + }; nix_build .args(&["build", "--impure", "--no-link", "--out-link"]) .arg(&built) .arg(format!( "{}.{}.config.system.build.toplevel", - SYSTEMS_ATTRIBUTE, host, + SYSTEMS_ATTRIBUTE, host.hostname, )) .env("SECRET_DATA", data.clone()); @@ -99,11 +101,11 @@ nix_build.inherit_stdio().run()?; let built = std::fs::canonicalize(built)?; info!("Built closure: {:?}", built); - if !is_local { + if !host.is_local() { info!("Uploading system closure"); Command::new("nix") .args(&["copy", "--to"]) - .arg(format!("ssh://root@{}", host)) + .arg(format!("ssh://root@{}", host.hostname)) .arg(&built) .inherit_stdio() .run()?; @@ -111,30 +113,21 @@ if let Some(subcommand) = &self.subcommand { if subcommand.should_switch_profile() { info!("Switching generation"); - if !is_local { - Command::ssh_on(host, "sudo") - } else { - Command::new("sudo") - } - .args(&["nix-env", "-p", "/nix/var/nix/profiles/system", "--set"]) - .arg(&built) - .inherit_stdio() - .run()?; + host.command_on("nix-env", true) + .args(&["-p", "/nix/var/nix/profiles/system", "--set"]) + .arg(&built) + .inherit_stdio() + .run()?; } info!("Executing activation script"); let mut switch_script = built.clone(); switch_script.push("bin"); switch_script.push("switch-to-configuration"); info!("{:?}", switch_script); - if !is_local { - Command::ssh_on(host, "sudo") - } else { - Command::new("sudo") - } - .arg(switch_script) - .arg(subcommand.name()) - .inherit_stdio() - .run()?; + host.command_on(switch_script, true) + .arg(subcommand.name()) + .inherit_stdio() + .run()?; } } Ok(()) -- gitstuff