difftreelog
refactor remove useless logging
in: trunk
1 file changed
src/cmds/build_systems.rsdiffbeforeafterboth1use std::process::Command;23use crate::{command::CommandExt, host::Config, nix::SYSTEMS_ATTRIBUTE};4use anyhow::Result;5use clap::Clap;6use log::info;78#[derive(Clap)]9#[clap(group = clap::ArgGroup::new("target"))]10pub struct BuildSystems {11 /// --builders arg for nix12 #[clap(long)]13 builders: Option<String>,14 /// Jobs to run locally15 #[clap(long)]16 jobs: Option<usize>,17 /// Do not continue on error18 #[clap(long)]19 fail_fast: bool,20 #[clap(long)]21 privileged_build: bool,22 #[clap(subcommand)]23 subcommand: Option<Subcommand>,24}2526#[derive(Clap)]27enum Subcommand {28 /// Switch to built system until reboot29 Test,30 /// Switch to built system after reboot31 Boot,32 /// test + boot33 Switch,34}35impl Subcommand {36 fn should_switch_profile(&self) -> bool {37 matches!(self, Self::Test | Self::Switch)38 }39 fn name(&self) -> &'static str {40 match self {41 Self::Test => "test",42 Self::Boot => "boot",43 Self::Switch => "switch",44 }45 }46}4748impl BuildSystems {49 pub fn run(self, config: &Config) -> Result<()> {50 println!("Build");51 // let db = Db::new(".fleet")?;52 let hosts = config.list_hosts()?;53 dbg!(&hosts);54 // let data = SecretDb::open(&db)?.generate_nix_data()?;5556 for host in hosts.iter() {57 if config.should_skip(host) {58 continue;59 }60 info!("Building host {}", host);61 let built = {62 let dir = tempfile::tempdir()?;63 dir.path().to_owned()64 };6566 let mut nix_build = if self.privileged_build {67 let mut out = Command::new("sudo");68 out.arg("nix");69 out70 } else {71 Command::new("nix")72 };73 nix_build74 .args(&["build", "--impure", "--no-link", "--out-link"])75 .arg(&built)76 .arg(format!(77 "{}.{}.config.system.build.toplevel",78 SYSTEMS_ATTRIBUTE, host,79 ));80 // .env("SECRET_DATA", data.clone());8182 if let Some(builders) = &self.builders {83 println!("Using builders: {}", builders);84 nix_build.arg("--builders").arg(builders);85 }86 if let Some(jobs) = &self.jobs {87 nix_build.arg("--max-jobs");88 nix_build.arg(format!("{}", jobs));89 }90 if !self.fail_fast {91 nix_build.arg("--keep-going");92 }9394 nix_build.inherit_stdio().run()?;95 let built = std::fs::canonicalize(built)?;96 info!("Built closure: {:?}", built);97 if !config.is_local(host) {98 info!("Uploading system closure");99 Command::new("nix")100 .args(&["copy", "--to"])101 .arg(format!("ssh://root@{}", host))102 .arg(&built)103 .inherit_stdio()104 .run()?;105 }106 if let Some(subcommand) = &self.subcommand {107 if subcommand.should_switch_profile() {108 info!("Switching generation");109 dbg!(&mut config110 .command_on(host, "nix-env", true)111 .args(&["-p", "/nix/var/nix/profiles/system", "--set"])112 .arg(&built)113 .inherit_stdio())114 .run()?;115 }116 info!("Executing activation script");117 let mut switch_script = built.clone();118 switch_script.push("bin");119 switch_script.push("switch-to-configuration");120 info!("{:?}", switch_script);121 config122 .command_on(host, switch_script, true)123 .arg(subcommand.name())124 .inherit_stdio()125 .run()?;126 }127 }128 Ok(())129 }130}1use std::process::Command;23use crate::{command::CommandExt, host::Config, nix::SYSTEMS_ATTRIBUTE};4use anyhow::Result;5use clap::Clap;6use log::info;78#[derive(Clap)]9#[clap(group = clap::ArgGroup::new("target"))]10pub struct BuildSystems {11 /// --builders arg for nix12 #[clap(long)]13 builders: Option<String>,14 /// Jobs to run locally15 #[clap(long)]16 jobs: Option<usize>,17 /// Do not continue on error18 #[clap(long)]19 fail_fast: bool,20 #[clap(long)]21 privileged_build: bool,22 #[clap(subcommand)]23 subcommand: Option<Subcommand>,24}2526#[derive(Clap)]27enum Subcommand {28 /// Switch to built system until reboot29 Test,30 /// Switch to built system after reboot31 Boot,32 /// test + boot33 Switch,34}35impl Subcommand {36 fn should_switch_profile(&self) -> bool {37 matches!(self, Self::Test | Self::Switch)38 }39 fn name(&self) -> &'static str {40 match self {41 Self::Test => "test",42 Self::Boot => "boot",43 Self::Switch => "switch",44 }45 }46}4748impl BuildSystems {49 pub fn run(self, config: &Config) -> Result<()> {50 println!("Build");51 let hosts = config.list_hosts()?;5253 for host in hosts.iter() {54 if config.should_skip(host) {55 continue;56 }57 info!("Building host {}", host);58 let built = {59 let dir = tempfile::tempdir()?;60 dir.path().to_owned()61 };6263 let mut nix_build = if self.privileged_build {64 let mut out = Command::new("sudo");65 out.arg("nix");66 out67 } else {68 Command::new("nix")69 };70 nix_build71 .args(&["build", "--impure", "--no-link", "--out-link"])72 .arg(&built)73 .arg(format!(74 "{}.{}.config.system.build.toplevel",75 SYSTEMS_ATTRIBUTE, host,76 ));7778 if let Some(builders) = &self.builders {79 println!("Using builders: {}", builders);80 nix_build.arg("--builders").arg(builders);81 }82 if let Some(jobs) = &self.jobs {83 nix_build.arg("--max-jobs");84 nix_build.arg(format!("{}", jobs));85 }86 if !self.fail_fast {87 nix_build.arg("--keep-going");88 }8990 nix_build.inherit_stdio().run()?;91 let built = std::fs::canonicalize(built)?;92 info!("Built closure: {:?}", built);93 if !config.is_local(host) {94 info!("Uploading system closure");95 Command::new("nix")96 .args(&["copy", "--to"])97 .arg(format!("ssh://root@{}", host))98 .arg(&built)99 .inherit_stdio()100 .run()?;101 }102 if let Some(subcommand) = &self.subcommand {103 if subcommand.should_switch_profile() {104 info!("Switching generation");105 config106 .command_on(host, "nix-env", true)107 .args(&["-p", "/nix/var/nix/profiles/system", "--set"])108 .arg(&built)109 .inherit_stdio()110 .run()?;111 }112 info!("Executing activation script");113 let mut switch_script = built.clone();114 switch_script.push("bin");115 switch_script.push("switch-to-configuration");116 config117 .command_on(host, switch_script, true)118 .arg(subcommand.name())119 .inherit_stdio()120 .run()?;121 }122 }123 Ok(())124 }125}