git.delta.rocks / jrsonnet / refs/commits / fc5bb23406b2

difftreelog

fix(build-systems) jobs argument name

Yaroslav Bolyukin2020-12-02parent: #1d31a62.patch.diff
in: trunk

1 file changed

modifiedsrc/cmds/build_systems.rsdiffbeforeafterboth
before · src/cmds/build_systems.rs
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)]13pub struct BuildSystems {14	/// Hosts to skip15	#[clap(long, number_of_values = 1)]16	skip: Vec<String>,17	/// Host, which should be threaten as localhost18	#[clap(long, env = "FLEET_LOCALHOST")]19	localhost: Option<String>,20	/// --builders arg for nix21	#[clap(long)]22	builders: Option<String>,23	/// Jobs to run locally24	#[clap(long)]25	jobs: Option<usize>,26	/// Do not continue on error27	#[clap(long)]28	fail_fast: bool,29	#[clap(subcommand)]30	subcommand: Option<Subcommand>,31}3233#[derive(Clap)]34enum Subcommand {35	/// Switch to built system until reboot36	Test,37	/// Switch to built system after reboot38	Boot,39	/// test + boot40	Switch,41}42impl Subcommand {43	fn should_switch_profile(&self) -> bool {44		matches!(self, Self::Test | Self::Switch)45	}46	fn name(&self) -> &'static str {47		match self {48			Self::Test => "test",49			Self::Boot => "boot",50			Self::Switch => "switch",51		}52	}53}5455impl BuildSystems {56	pub fn run(self) -> Result<()> {57		let db = Db::new(".fleet")?;58		let hosts = list_hosts()?;59		let data = SecretDb::open(&db)?.generate_nix_data()?;6061		for host in hosts.iter() {62			if self.skip.contains(host) {63				warn!("Skipping host {}", host);64				continue;65			}66			let is_local = Some(host) == self.localhost.as_ref();67			info!("Building host {}", host);68			let built = {69				let dir = tempfile::tempdir()?;70				dir.path().to_owned()71			};7273			let mut nix_build = Command::new("nix");74			nix_build75				.args(&["build", "--impure", "--no-link", "--out-link"])76				.arg(&built)77				.arg(format!(78					"{}.{}.config.system.build.toplevel",79					SYSTEMS_ATTRIBUTE, host,80				))81				.env("SECRET_DATA", data.clone());8283			if let Some(builders) = &self.builders {84				println!("Using builders: {}", builders);85				nix_build.arg("--builders").arg(builders);86			}87			if let Some(jobs) = &self.jobs {88				nix_build.arg("--jobs");89				nix_build.arg(format!("{}", jobs));90			}91			if !self.fail_fast {92				nix_build.arg("--keep-going");93			}9495			nix_build.inherit_stdio().run()?;96			let built = std::fs::canonicalize(built)?;97			info!("Built closure: {:?}", built);98			if !is_local {99				info!("Uploading system closure");100				Command::new("nix")101					.args(&["copy", "--to"])102					.arg(format!("ssh://root@{}", host))103					.arg(&built)104					.inherit_stdio()105					.run()?;106			}107			if let Some(subcommand) = &self.subcommand {108				if subcommand.should_switch_profile() {109					info!("Switching generation");110					if !is_local {111						Command::ssh_on(host, "sudo")112					} else {113						Command::new("sudo")114					}115					.args(&["nix-env", "-p", "/nix/var/nix/profiles/system", "--set"])116					.arg(&built)117					.inherit_stdio()118					.run()?;119				}120				info!("Executing activation script");121				let mut switch_script = built.clone();122				switch_script.push("bin");123				switch_script.push("switch-to-configuration");124				info!("{:?}", switch_script);125				if !is_local {126					Command::ssh_on(host, "sudo")127				} else {128					Command::new("sudo")129				}130				.arg(switch_script)131				.arg(subcommand.name())132				.inherit_stdio()133				.run()?;134			}135		}136		Ok(())137	}138}
after · src/cmds/build_systems.rs
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)]13pub struct BuildSystems {14	/// Hosts to skip15	#[clap(long, number_of_values = 1)]16	skip: Vec<String>,17	/// Host, which should be threaten as localhost18	#[clap(long, env = "FLEET_LOCALHOST")]19	localhost: Option<String>,20	/// --builders arg for nix21	#[clap(long)]22	builders: Option<String>,23	/// Jobs to run locally24	#[clap(long)]25	jobs: Option<usize>,26	/// Do not continue on error27	#[clap(long)]28	fail_fast: bool,29	#[clap(subcommand)]30	subcommand: Option<Subcommand>,31}3233#[derive(Clap)]34enum Subcommand {35	/// Switch to built system until reboot36	Test,37	/// Switch to built system after reboot38	Boot,39	/// test + boot40	Switch,41}42impl Subcommand {43	fn should_switch_profile(&self) -> bool {44		matches!(self, Self::Test | Self::Switch)45	}46	fn name(&self) -> &'static str {47		match self {48			Self::Test => "test",49			Self::Boot => "boot",50			Self::Switch => "switch",51		}52	}53}5455impl BuildSystems {56	pub fn run(self) -> Result<()> {57		let db = Db::new(".fleet")?;58		let hosts = list_hosts()?;59		let data = SecretDb::open(&db)?.generate_nix_data()?;6061		for host in hosts.iter() {62			if self.skip.contains(host) {63				warn!("Skipping host {}", host);64				continue;65			}66			let is_local = Some(host) == self.localhost.as_ref();67			info!("Building host {}", host);68			let built = {69				let dir = tempfile::tempdir()?;70				dir.path().to_owned()71			};7273			let mut nix_build = Command::new("nix");74			nix_build75				.args(&["build", "--impure", "--no-link", "--out-link"])76				.arg(&built)77				.arg(format!(78					"{}.{}.config.system.build.toplevel",79					SYSTEMS_ATTRIBUTE, host,80				))81				.env("SECRET_DATA", data.clone());8283			if let Some(builders) = &self.builders {84				println!("Using builders: {}", builders);85				nix_build.arg("--builders").arg(builders);86			}87			if let Some(jobs) = &self.jobs {88				nix_build.arg("--max-jobs");89				nix_build.arg(format!("{}", jobs));90			}91			if !self.fail_fast {92				nix_build.arg("--keep-going");93			}9495			nix_build.inherit_stdio().run()?;96			let built = std::fs::canonicalize(built)?;97			info!("Built closure: {:?}", built);98			if !is_local {99				info!("Uploading system closure");100				Command::new("nix")101					.args(&["copy", "--to"])102					.arg(format!("ssh://root@{}", host))103					.arg(&built)104					.inherit_stdio()105					.run()?;106			}107			if let Some(subcommand) = &self.subcommand {108				if subcommand.should_switch_profile() {109					info!("Switching generation");110					if !is_local {111						Command::ssh_on(host, "sudo")112					} else {113						Command::new("sudo")114					}115					.args(&["nix-env", "-p", "/nix/var/nix/profiles/system", "--set"])116					.arg(&built)117					.inherit_stdio()118					.run()?;119				}120				info!("Executing activation script");121				let mut switch_script = built.clone();122				switch_script.push("bin");123				switch_script.push("switch-to-configuration");124				info!("{:?}", switch_script);125				if !is_local {126					Command::ssh_on(host, "sudo")127				} else {128					Command::new("sudo")129				}130				.arg(switch_script)131				.arg(subcommand.name())132				.inherit_stdio()133				.run()?;134			}135		}136		Ok(())137	}138}