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

difftreelog

source

src/cmds/build_systems.rs2.8 KiBsourcehistory
1use std::process::Command;23use crate::{command::CommandExt, host::Config, nix::SYSTEMS_ATTRIBUTE};4use anyhow::Result;5use log::info;6use structopt::StructOpt;78#[derive(StructOpt)]9pub struct BuildSystems {10	/// --builders arg for nix11	#[structopt(long)]12	builders: Option<String>,13	/// Jobs to run locally14	#[structopt(long)]15	jobs: Option<usize>,16	/// Do not continue on error17	#[structopt(long)]18	fail_fast: bool,19	#[structopt(long)]20	privileged_build: bool,21	#[structopt(subcommand)]22	subcommand: Option<Subcommand>,23}2425#[derive(StructOpt)]26enum Subcommand {27	/// Switch to built system until reboot28	Test,29	/// Switch to built system after reboot30	Boot,31	/// test + boot32	Switch,33}34impl Subcommand {35	fn should_switch_profile(&self) -> bool {36		matches!(self, Self::Test | Self::Switch)37	}38	fn name(&self) -> &'static str {39		match self {40			Self::Test => "test",41			Self::Boot => "boot",42			Self::Switch => "switch",43		}44	}45}4647impl BuildSystems {48	pub fn run(self, config: &Config) -> Result<()> {49		let hosts = config.list_hosts()?;5051		for host in hosts.iter() {52			if config.should_skip(host) {53				continue;54			}55			info!("Building host {}", host);56			let built = {57				let dir = tempfile::tempdir()?;58				dir.path().to_owned()59			};6061			let mut nix_build = if self.privileged_build {62				let mut out = Command::new("sudo");63				out.arg("nix");64				out65			} else {66				Command::new("nix")67			};68			nix_build69				.args(&["build", "--impure", "--no-link", "--out-link"])70				.arg(&built)71				.arg(format!(72					"{}.{}.config.system.build.toplevel",73					SYSTEMS_ATTRIBUTE, host,74				));7576			if let Some(builders) = &self.builders {77				nix_build.arg("--builders").arg(builders);78			}79			if let Some(jobs) = &self.jobs {80				nix_build.arg("--max-jobs");81				nix_build.arg(format!("{}", jobs));82			}83			if !self.fail_fast {84				nix_build.arg("--keep-going");85			}8687			nix_build.inherit_stdio().run()?;88			let built = std::fs::canonicalize(built)?;89			info!("Built closure: {:?}", built);90			if !config.is_local(host) {91				info!("Uploading system closure");92				Command::new("nix")93					.args(&["copy", "--to"])94					.arg(format!("ssh://root@{}", host))95					.arg(&built)96					.inherit_stdio()97					.run()?;98			}99			if let Some(subcommand) = &self.subcommand {100				if subcommand.should_switch_profile() {101					info!("Switching generation");102					config103						.command_on(host, "nix-env", true)104						.args(&["-p", "/nix/var/nix/profiles/system", "--set"])105						.arg(&built)106						.inherit_stdio()107						.run()?;108				}109				info!("Executing activation script");110				let mut switch_script = built.clone();111				switch_script.push("bin");112				switch_script.push("switch-to-configuration");113				config114					.command_on(host, switch_script, true)115					.arg(subcommand.name())116					.inherit_stdio()117					.run()?;118			}119		}120		Ok(())121	}122}