From 76dbef54af3bbfbf81edd8a45f6ba8dea0db47f9 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 10:48:21 -0700 Subject: stuff --- src/main.rs | 598 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 598 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2184392 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,598 @@ +use clap::Clap; +use merge::Merge; +use std::collections::HashMap; + +use std::borrow::Cow; + +use std::process::Stdio; +use tokio::process::Command; + +use std::path::Path; + +use std::process; + +extern crate pretty_env_logger; +#[macro_use] +extern crate log; + +#[macro_use] +extern crate serde_derive; + +macro_rules! good_panic { + ($($tts:tt)*) => {{ + error!($($tts)*); + process::exit(1); + }} +} + +/// Simple Rust rewrite of a simple Nix Flake deployment tool +#[derive(Clap, Debug)] +#[clap(version = "1.0", author = "notgne2 ")] +struct Opts { + /// Log verbosity + #[clap(short, long, parse(from_occurrences))] + verbose: i32, + + #[clap(subcommand)] + subcmd: SubCommand, +} + +/// Deploy profiles +#[derive(Clap, Debug)] +struct DeployOpts { + /// The flake to deploy + #[clap(default_value = ".")] + flake: String, + /// Prepare server (for first deployments) + #[clap(short, long)] + prime: bool, +} + +/// Activate a profile on your current machine +#[derive(Clap, Debug)] +struct ActivateOpts { + profile_path: String, + closure: String, + + /// Command for activating the given profile + #[clap(short, long)] + activate_cmd: Option, + + /// Command for bootstrapping + #[clap(short, long)] + bootstrap_cmd: Option, + + /// Auto rollback if failure + #[clap(short, long)] + auto_rollback: bool, +} + +#[derive(Clap, Debug)] +enum SubCommand { + Deploy(DeployOpts), + Activate(ActivateOpts), +} + +#[derive(Deserialize, Debug, Clone, Merge)] +pub struct GenericSettings { + #[serde(rename(deserialize = "sshUser"))] + pub ssh_user: Option, + pub user: Option, + #[serde( + skip_serializing_if = "Vec::is_empty", + default, + rename(deserialize = "sshOpts") + )] + #[merge(strategy = merge::vec::append)] + pub ssh_opts: Vec, + #[serde(rename(deserialize = "fastConnection"))] + pub fast_connection: Option, + #[serde(rename(deserialize = "autoRollback"))] + pub auto_rollback: Option, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct NodeSettings { + pub hostname: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ProfileSettings { + pub path: String, + pub activate: Option, + pub bootstrap: Option, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Profile { + #[serde(flatten)] + pub profile_settings: ProfileSettings, + #[serde(flatten)] + pub generic_settings: GenericSettings, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Node { + #[serde(flatten)] + pub generic_settings: GenericSettings, + #[serde(flatten)] + pub node_settings: NodeSettings, + + pub profiles: HashMap, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Data { + #[serde(flatten)] + pub generic_settings: GenericSettings, + pub nodes: HashMap, +} + +async fn deploy_profile( + profile: &Profile, + profile_name: &str, + node: &Node, + node_name: &str, + top_settings: &GenericSettings, + supports_flakes: bool, + repo: &str, +) -> Result<(), Box> { + info!( + "Deploying profile `{}` for node `{}`", + profile_name, node_name + ); + + let mut merged_settings = top_settings.clone(); + merged_settings.merge(node.generic_settings.clone()); + merged_settings.merge(profile.generic_settings.clone()); + + let ssh_user: Cow = match &merged_settings.ssh_user { + Some(u) => u.into(), + None => whoami::username().into(), + }; + + let profile_user: Cow = match &merged_settings.user { + Some(x) => x.into(), + None => match &merged_settings.ssh_user { + Some(x) => x.into(), + None => good_panic!( + "Neither user nor sshUser set for profile `{}` of node `{}`", + profile_name, + node_name + ), + }, + }; + + let sudo: Option = match merged_settings.user { + Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user).into()), + _ => None, + }; + + let profile_path = match &profile_user[..] { + "root" => format!("/nix/var/nix/profiles/{}", profile_name), + _ => format!( + "/nix/var/nix/profiles/per-user/{}/{}", + profile_user, profile_name + ), + }; + + info!( + "Building profile `{}` for node `{}`", + profile_name, node_name + ); + if supports_flakes { + Command::new("nix") + .arg("build") + .arg("--no-link") + .arg(format!( + "{}#deploy.nodes.{}.profiles.{}.path", + repo, node_name, profile_name + )) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + } else { + Command::new("nix-build") + .arg(&repo) + .arg("-A") + .arg(format!( + "deploy.nodes.{}.profiles.{}.path", + node_name, profile_name + )) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + } + + let current_exe = std::env::current_exe().expect("Expected to find current executable path"); + + if !current_exe.starts_with("/nix/store/") { + good_panic!("The deploy binary must be in the Nix store"); + } + + if let Ok(local_key) = std::env::var("LOCAL_KEY") { + info!( + "Signing key present! Signing profile `{}` for node `{}`", + profile_name, node_name + ); + + Command::new("nix") + .arg("sign-paths") + .arg("-r") + .arg("-k") + .arg(local_key) + .arg(&profile.profile_settings.path) + .arg(¤t_exe) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + } + + info!("Copying profile `{} for node `{}`", profile_name, node_name); + + let mut copy_command_ = Command::new("nix"); + let mut copy_command = copy_command_.arg("copy"); + + if let Some(true) = merged_settings.fast_connection { + copy_command = copy_command.arg("--substitute-on-destination"); + } + + let ssh_opts_str = merged_settings + .ssh_opts + // This should provide some extra safety, but it also breaks for some reason, oh well + // .iter() + // .map(|x| format!("'{}'", x)) + // .collect::>() + .join(" "); + + copy_command + .arg("--no-check-sigs") + .arg("--to") + .arg(format!( + "ssh://{}@{}", + ssh_user, node.node_settings.hostname + )) + .arg(&profile.profile_settings.path) + .arg(¤t_exe) + .env("NIX_SSHOPTS", ssh_opts_str) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + info!( + "Activating profile `{}` for node `{}`", + profile_name, node_name + ); + + let mut self_activate_command = format!( + "{} activate '{}' '{}'", + current_exe.as_path().to_str().unwrap(), + profile_path, + profile.profile_settings.path, + ); + + if let Some(sudo_cmd) = sudo { + self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); + } + + if let Some(ref bootstrap_cmd) = profile.profile_settings.bootstrap { + self_activate_command = format!( + "{} --bootstrap-cmd '{}'", + self_activate_command, bootstrap_cmd + ); + } + + if let Some(ref activate_cmd) = profile.profile_settings.activate { + self_activate_command = format!( + "{} --activate-cmd '{}'", + self_activate_command, activate_cmd + ); + } + + let mut c = Command::new("ssh"); + let mut ssh_command = c.arg(format!( + "ssh://{}@{}", + ssh_user, node.node_settings.hostname + )); + + for ssh_opt in merged_settings.ssh_opts { + ssh_command = ssh_command.arg(ssh_opt); + } + + ssh_command.arg(self_activate_command).spawn()?.await?; + + Ok(()) +} + +#[inline] +async fn deploy_all_profiles( + node: &Node, + node_name: &str, + supports_flakes: bool, + repo: &str, + top_settings: &GenericSettings, + prime: bool, +) -> Result<(), Box> { + info!("Deploying all profiles for `{}`", node_name); + + if prime { + info!("Bootstrapping {}", node_name); + + let profile = match node.profiles.get("system") { + Some(x) => x, + None => good_panic!("No system profile was found, needed for priming"), + }; + + deploy_profile( + &profile, + "system", + node, + node_name, + top_settings, + supports_flakes, + repo, + ) + .await?; + } + + for (profile_name, profile) in &node.profiles { + // This will have already been deployed + if prime && profile_name == "system" { + continue; + } + + deploy_profile( + &profile, + profile_name, + node, + node_name, + top_settings, + supports_flakes, + repo, + ) + .await?; + } + + Ok(()) +} + +#[tokio::main] + +async fn main() -> Result<(), Box> { + if let Err(_) = std::env::var("DEPLOY_LOG") { + std::env::set_var("DEPLOY_LOG", "info"); + } + + pretty_env_logger::init_custom_env("DEPLOY_LOG"); + + let opts: Opts = Opts::parse(); + + match opts.subcmd { + SubCommand::Deploy(deploy_opts) => { + let flake_fragment_start = deploy_opts.flake.find('#'); + + let (repo, maybe_fragment) = match flake_fragment_start { + Some(s) => (&deploy_opts.flake[..s], Some(&deploy_opts.flake[s + 1..])), + None => (deploy_opts.flake.as_str(), None), + }; + + let (maybe_node, maybe_profile) = match maybe_fragment { + Some(fragment) => { + let fragment_profile_start = fragment.find('.'); + match fragment_profile_start { + Some(s) => (Some(&fragment[..s]), Some(&fragment[s + 1..])), + None => (Some(fragment), None), + } + } + None => (None, None), + }; + + let test_flake_status = Command::new("nix") + .arg("eval") + .arg("--expr") + .arg("builtins.getFlake") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await?; + + let supports_flakes = test_flake_status.success(); + + let data_json = match supports_flakes { + true => { + let c = Command::new("nix") + .arg("eval") + .arg("--json") + .arg(format!("{}#deploy", repo)) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + // TODO forward input args? + .output() + .await?; + + String::from_utf8(c.stdout)? + } + false => { + let c = Command::new("nix-instanciate") + .arg("--strict") + .arg("--read-write-mode") + .arg("--json") + .arg("--eval") + .arg("--E") + .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", repo)) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .output() + .await?; + + String::from_utf8(c.stdout)? + } + }; + + let data: Data = serde_json::from_str(&data_json)?; + + match (maybe_node, maybe_profile) { + (Some(node_name), Some(profile_name)) => { + let node = match data.nodes.get(node_name) { + Some(x) => x, + None => good_panic!("No node was found named `{}`", node_name), + }; + let profile = match node.profiles.get(profile_name) { + Some(x) => x, + None => good_panic!("No profile was found named `{}`", profile_name), + }; + + deploy_profile( + &profile, + profile_name, + node, + node_name, + &data.generic_settings, + supports_flakes, + repo, + ) + .await?; + } + (Some(node_name), None) => { + let node = match data.nodes.get(node_name) { + Some(x) => x, + None => good_panic!("No node was found named `{}`", node_name), + }; + + deploy_all_profiles( + node, + node_name, + supports_flakes, + repo, + &data.generic_settings, + deploy_opts.prime, + ) + .await?; + } + (None, None) => { + info!("Deploying all profiles on all nodes"); + + for (node_name, node) in &data.nodes { + deploy_all_profiles( + node, + node_name, + supports_flakes, + repo, + &data.generic_settings, + deploy_opts.prime, + ) + .await?; + } + } + (None, Some(_)) => good_panic!( + "Profile provided without a node, this is not (currently) supported" + ), + }; + } + SubCommand::Activate(activate_opts) => { + info!("Activating profile"); + + Command::new("nix-env") + .arg("-p") + .arg(&activate_opts.profile_path) + .arg("--set") + .arg(&activate_opts.closure) + .stdout(Stdio::null()) + .spawn()? + .await?; + + if let (Some(bootstrap_cmd), false) = ( + activate_opts.bootstrap_cmd, + !Path::new(&activate_opts.profile_path).exists(), + ) { + let bootstrap_status = Command::new("bash") + .arg("-c") + .arg(&bootstrap_cmd) + .env("PROFILE", &activate_opts.profile_path) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await; + + match bootstrap_status { + Ok(s) if s.success() => (), + _ => { + tokio::fs::remove_file(&activate_opts.profile_path).await?; + good_panic!("Failed to execute bootstrap command"); + } + } + } + + if let Some(activate_cmd) = activate_opts.activate_cmd { + let activate_status = Command::new("bash") + .arg("-c") + .arg(&activate_cmd) + .env("PROFILE", &activate_opts.profile_path) + .status() + .await; + + match activate_status { + Ok(s) if s.success() => (), + _ if activate_opts.auto_rollback => { + Command::new("nix-env") + .arg("-p") + .arg(&activate_opts.profile_path) + .arg("--rollback") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + let c = Command::new("nix-env") + .arg("-p") + .arg(&activate_opts.profile_path) + .arg("--list-generations") + .output() + .await?; + let generations_list = String::from_utf8(c.stdout)?; + + let last_generation_line = generations_list + .lines() + .last() + .expect("Expected to find a generation in list"); + + let last_generation_id = last_generation_line + .split_whitespace() + .next() + .expect("Expected to get ID from generation entry"); + + debug!("Removing generation entry {}", last_generation_line); + warn!("Removing generation by ID {}", last_generation_id); + + Command::new("nix-env") + .arg("-p") + .arg(&activate_opts.profile_path) + .arg("--delete-generations") + .arg(last_generation_id) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + // TODO: why are we doing this? + // to run the older version as long as the command is the same? + Command::new("bash") + .arg("-c") + .arg(&activate_cmd) + .spawn()? + .await?; + + good_panic!("Failed to execute activation command"); + } + _ => {} + } + } + } + } + + Ok(()) +} -- cgit v1.2.3 From 1b9cb58802cd295bd91b822812f195c02367e350 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 12:17:36 -0700 Subject: add check sigs flag --- src/main.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 2184392..0599b11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,9 @@ struct DeployOpts { /// Prepare server (for first deployments) #[clap(short, long)] prime: bool, + /// Check signatures when using `nix copy` + #[clap(short, long)] + checksigs: bool, } /// Activate a profile on your current machine @@ -135,6 +138,7 @@ async fn deploy_profile( node_name: &str, top_settings: &GenericSettings, supports_flakes: bool, + check_sigs: bool, repo: &str, ) -> Result<(), Box> { info!( @@ -240,6 +244,10 @@ async fn deploy_profile( copy_command = copy_command.arg("--substitute-on-destination"); } + if !check_sigs { + copy_command = copy_command.arg("--no-check-sigs"); + } + let ssh_opts_str = merged_settings .ssh_opts // This should provide some extra safety, but it also breaks for some reason, oh well @@ -249,7 +257,6 @@ async fn deploy_profile( .join(" "); copy_command - .arg("--no-check-sigs") .arg("--to") .arg(format!( "ssh://{}@{}", @@ -316,6 +323,7 @@ async fn deploy_all_profiles( repo: &str, top_settings: &GenericSettings, prime: bool, + check_sigs: bool, ) -> Result<(), Box> { info!("Deploying all profiles for `{}`", node_name); @@ -334,6 +342,7 @@ async fn deploy_all_profiles( node_name, top_settings, supports_flakes, + check_sigs, repo, ) .await?; @@ -352,6 +361,7 @@ async fn deploy_all_profiles( node_name, top_settings, supports_flakes, + check_sigs, repo, ) .await?; @@ -374,7 +384,6 @@ async fn main() -> Result<(), Box> { match opts.subcmd { SubCommand::Deploy(deploy_opts) => { let flake_fragment_start = deploy_opts.flake.find('#'); - let (repo, maybe_fragment) = match flake_fragment_start { Some(s) => (&deploy_opts.flake[..s], Some(&deploy_opts.flake[s + 1..])), None => (deploy_opts.flake.as_str(), None), @@ -453,6 +462,7 @@ async fn main() -> Result<(), Box> { node_name, &data.generic_settings, supports_flakes, + deploy_opts.checksigs, repo, ) .await?; @@ -470,6 +480,7 @@ async fn main() -> Result<(), Box> { repo, &data.generic_settings, deploy_opts.prime, + deploy_opts.checksigs, ) .await?; } @@ -484,6 +495,7 @@ async fn main() -> Result<(), Box> { repo, &data.generic_settings, deploy_opts.prime, + deploy_opts.checksigs, ) .await?; } -- cgit v1.2.3 From 00f75951211bacc5bab4317e56376f03c74dabb7 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 12:25:30 -0700 Subject: Add (untested) profiles order support --- src/main.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 0599b11..9d55efc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,6 +122,12 @@ pub struct Node { pub node_settings: NodeSettings, pub profiles: HashMap, + #[serde( + skip_serializing_if = "Vec::is_empty", + default, + rename(deserialize = "profilesOrder") + )] + pub profiles_order: Vec, } #[derive(Deserialize, Debug, Clone)] @@ -348,7 +354,21 @@ async fn deploy_all_profiles( .await?; } - for (profile_name, profile) in &node.profiles { + let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); + + // Add any profiles which weren't in the provided order list + for (profile_name, _) in &node.profiles { + if !profiles_list.contains(&profile_name.as_str()) { + profiles_list.push(&profile_name); + } + } + + for profile_name in profiles_list { + let profile = match node.profiles.get(profile_name) { + Some(x) => x, + None => good_panic!("No profile was found named `{}`", profile_name), + }; + // This will have already been deployed if prime && profile_name == "system" { continue; -- cgit v1.2.3 From a71eaa3cec59e8084f890bf89efbffc5f765fd9e Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 13:37:57 -0700 Subject: Mildly modularize and seperate deploy and push (untested) --- src/main.rs | 164 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 118 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 9d55efc..a3c526f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use clap::Clap; use merge::Merge; -use std::collections::HashMap; +use std::{collections::HashMap, path::PathBuf}; use std::borrow::Cow; @@ -137,25 +137,19 @@ pub struct Data { pub nodes: HashMap, } -async fn deploy_profile( - profile: &Profile, +struct DeployData<'a> { + pub sudo: Option, + pub ssh_user: Cow<'a, str>, + pub profile_user: Cow<'a, str>, + pub profile_path: String, + pub current_exe: PathBuf, +} + +async fn make_deploy_data<'a>( profile_name: &str, - node: &Node, node_name: &str, - top_settings: &GenericSettings, - supports_flakes: bool, - check_sigs: bool, - repo: &str, -) -> Result<(), Box> { - info!( - "Deploying profile `{}` for node `{}`", - profile_name, node_name - ); - - let mut merged_settings = top_settings.clone(); - merged_settings.merge(node.generic_settings.clone()); - merged_settings.merge(profile.generic_settings.clone()); - + merged_settings: &'a GenericSettings, +) -> Result, Box> { let ssh_user: Cow = match &merged_settings.ssh_user { Some(u) => u.into(), None => whoami::username().into(), @@ -173,11 +167,6 @@ async fn deploy_profile( }, }; - let sudo: Option = match merged_settings.user { - Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user).into()), - _ => None, - }; - let profile_path = match &profile_user[..] { "root" => format!("/nix/var/nix/profiles/{}", profile_name), _ => format!( @@ -186,6 +175,42 @@ async fn deploy_profile( ), }; + let sudo: Option = match merged_settings.user { + Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user).into()), + _ => None, + }; + + let current_exe = std::env::current_exe().expect("Expected to find current executable path"); + + if !current_exe.starts_with("/nix/store/") { + good_panic!("The deploy binary must be in the Nix store"); + } + + Ok(DeployData { + sudo, + ssh_user, + profile_user, + profile_path, + current_exe, + }) +} + +async fn push_profile( + profile: &Profile, + profile_name: &str, + node: &Node, + node_name: &str, + supports_flakes: bool, + check_sigs: bool, + repo: &str, + merged_settings: &GenericSettings, + deploy_data: &DeployData<'_>, +) -> Result<(), Box> { + info!( + "Deploying profile `{}` for node `{}`", + profile_name, node_name + ); + info!( "Building profile `{}` for node `{}`", profile_name, node_name @@ -216,12 +241,6 @@ async fn deploy_profile( .await?; } - let current_exe = std::env::current_exe().expect("Expected to find current executable path"); - - if !current_exe.starts_with("/nix/store/") { - good_panic!("The deploy binary must be in the Nix store"); - } - if let Ok(local_key) = std::env::var("LOCAL_KEY") { info!( "Signing key present! Signing profile `{}` for node `{}`", @@ -234,7 +253,7 @@ async fn deploy_profile( .arg("-k") .arg(local_key) .arg(&profile.profile_settings.path) - .arg(¤t_exe) + .arg(&deploy_data.current_exe) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn()? @@ -266,16 +285,27 @@ async fn deploy_profile( .arg("--to") .arg(format!( "ssh://{}@{}", - ssh_user, node.node_settings.hostname + deploy_data.ssh_user, node.node_settings.hostname )) .arg(&profile.profile_settings.path) - .arg(¤t_exe) + .arg(&deploy_data.current_exe) .env("NIX_SSHOPTS", ssh_opts_str) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn()? .await?; + Ok(()) +} + +async fn deploy_profile( + profile: &Profile, + profile_name: &str, + node: &Node, + node_name: &str, + merged_settings: &GenericSettings, + deploy_data: &DeployData<'_>, +) -> Result<(), Box> { info!( "Activating profile `{}` for node `{}`", profile_name, node_name @@ -283,12 +313,12 @@ async fn deploy_profile( let mut self_activate_command = format!( "{} activate '{}' '{}'", - current_exe.as_path().to_str().unwrap(), - profile_path, + deploy_data.current_exe.as_path().to_str().unwrap(), + deploy_data.profile_path, profile.profile_settings.path, ); - if let Some(sudo_cmd) = sudo { + if let Some(sudo_cmd) = &deploy_data.sudo { self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); } @@ -309,10 +339,10 @@ async fn deploy_profile( let mut c = Command::new("ssh"); let mut ssh_command = c.arg(format!( "ssh://{}@{}", - ssh_user, node.node_settings.hostname + deploy_data.ssh_user, node.node_settings.hostname )); - for ssh_opt in merged_settings.ssh_opts { + for ssh_opt in &merged_settings.ssh_opts { ssh_command = ssh_command.arg(ssh_opt); } @@ -321,6 +351,48 @@ async fn deploy_profile( Ok(()) } +async fn deploy_profile_todo( + top_settings: &GenericSettings, + profile: &Profile, + profile_name: &str, + node: &Node, + node_name: &str, + supports_flakes: bool, + check_sigs: bool, + repo: &str, +) -> Result<(), Box> { + let mut merged_settings = top_settings.clone(); + merged_settings.merge(node.generic_settings.clone()); + merged_settings.merge(profile.generic_settings.clone()); + + let deploy_data = make_deploy_data(profile_name, node_name, &merged_settings).await?; + + push_profile( + profile, + profile_name, + node, + node_name, + supports_flakes, + check_sigs, + repo, + &merged_settings, + &deploy_data, + ) + .await?; + + deploy_profile( + profile, + profile_name, + node, + node_name, + &merged_settings, + &deploy_data, + ) + .await?; + + Ok(()) +} + #[inline] async fn deploy_all_profiles( node: &Node, @@ -341,12 +413,12 @@ async fn deploy_all_profiles( None => good_panic!("No system profile was found, needed for priming"), }; - deploy_profile( - &profile, + deploy_profile_todo( + top_settings, + profile, "system", node, node_name, - top_settings, supports_flakes, check_sigs, repo, @@ -374,12 +446,12 @@ async fn deploy_all_profiles( continue; } - deploy_profile( - &profile, + deploy_profile_todo( + top_settings, + profile, profile_name, node, node_name, - top_settings, supports_flakes, check_sigs, repo, @@ -475,12 +547,12 @@ async fn main() -> Result<(), Box> { None => good_panic!("No profile was found named `{}`", profile_name), }; - deploy_profile( - &profile, + deploy_profile_todo( + &data.generic_settings, + profile, profile_name, node, node_name, - &data.generic_settings, supports_flakes, deploy_opts.checksigs, repo, -- cgit v1.2.3 From ff347d4204369d40b6b04eedf8e2421bcd7018ab Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 13:55:46 -0700 Subject: Crudely perform pushes before deploys, with little data re-use (untested) --- src/main.rs | 135 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 69 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index a3c526f..59b79a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -351,81 +351,63 @@ async fn deploy_profile( Ok(()) } -async fn deploy_profile_todo( - top_settings: &GenericSettings, - profile: &Profile, - profile_name: &str, - node: &Node, - node_name: &str, - supports_flakes: bool, - check_sigs: bool, - repo: &str, -) -> Result<(), Box> { - let mut merged_settings = top_settings.clone(); - merged_settings.merge(node.generic_settings.clone()); - merged_settings.merge(profile.generic_settings.clone()); - - let deploy_data = make_deploy_data(profile_name, node_name, &merged_settings).await?; - - push_profile( - profile, - profile_name, - node, - node_name, - supports_flakes, - check_sigs, - repo, - &merged_settings, - &deploy_data, - ) - .await?; - - deploy_profile( - profile, - profile_name, - node, - node_name, - &merged_settings, - &deploy_data, - ) - .await?; - - Ok(()) -} - #[inline] -async fn deploy_all_profiles( +async fn push_all_profiles( node: &Node, node_name: &str, supports_flakes: bool, repo: &str, top_settings: &GenericSettings, - prime: bool, check_sigs: bool, ) -> Result<(), Box> { info!("Deploying all profiles for `{}`", node_name); - if prime { - info!("Bootstrapping {}", node_name); + let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); + + // Add any profiles which weren't in the provided order list + for (profile_name, _) in &node.profiles { + if !profiles_list.contains(&profile_name.as_str()) { + profiles_list.push(&profile_name); + } + } - let profile = match node.profiles.get("system") { + for profile_name in profiles_list { + let profile = match node.profiles.get(profile_name) { Some(x) => x, - None => good_panic!("No system profile was found, needed for priming"), + None => good_panic!("No profile was found named `{}`", profile_name), }; - deploy_profile_todo( - top_settings, + let mut merged_settings = top_settings.clone(); + merged_settings.merge(node.generic_settings.clone()); + merged_settings.merge(profile.generic_settings.clone()); + + let deploy_data = make_deploy_data(profile_name, node_name, &merged_settings).await?; + + push_profile( profile, - "system", + profile_name, node, node_name, supports_flakes, check_sigs, repo, + &merged_settings, + &deploy_data, ) .await?; } + Ok(()) +} + +#[inline] +async fn deploy_all_profiles( + node: &Node, + node_name: &str, + top_settings: &GenericSettings, +) -> Result<(), Box> { + info!("Deploying all profiles for `{}`", node_name); + let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); // Add any profiles which weren't in the provided order list @@ -441,20 +423,19 @@ async fn deploy_all_profiles( None => good_panic!("No profile was found named `{}`", profile_name), }; - // This will have already been deployed - if prime && profile_name == "system" { - continue; - } + let mut merged_settings = top_settings.clone(); + merged_settings.merge(node.generic_settings.clone()); + merged_settings.merge(profile.generic_settings.clone()); + + let deploy_data = make_deploy_data(profile_name, node_name, &merged_settings).await?; - deploy_profile_todo( - top_settings, + deploy_profile( profile, profile_name, node, node_name, - supports_flakes, - check_sigs, - repo, + &merged_settings, + &deploy_data, ) .await?; } @@ -547,8 +528,14 @@ async fn main() -> Result<(), Box> { None => good_panic!("No profile was found named `{}`", profile_name), }; - deploy_profile_todo( - &data.generic_settings, + let mut merged_settings = data.generic_settings.clone(); + merged_settings.merge(node.generic_settings.clone()); + merged_settings.merge(profile.generic_settings.clone()); + + let deploy_data = + make_deploy_data(profile_name, node_name, &merged_settings).await?; + + push_profile( profile, profile_name, node, @@ -556,6 +543,18 @@ async fn main() -> Result<(), Box> { supports_flakes, deploy_opts.checksigs, repo, + &merged_settings, + &deploy_data, + ) + .await?; + + deploy_profile( + profile, + profile_name, + node, + node_name, + &merged_settings, + &deploy_data, ) .await?; } @@ -565,32 +564,36 @@ async fn main() -> Result<(), Box> { None => good_panic!("No node was found named `{}`", node_name), }; - deploy_all_profiles( + push_all_profiles( node, node_name, supports_flakes, repo, &data.generic_settings, - deploy_opts.prime, deploy_opts.checksigs, ) .await?; + + deploy_all_profiles(node, node_name, &data.generic_settings).await?; } (None, None) => { info!("Deploying all profiles on all nodes"); for (node_name, node) in &data.nodes { - deploy_all_profiles( + push_all_profiles( node, node_name, supports_flakes, repo, &data.generic_settings, - deploy_opts.prime, deploy_opts.checksigs, ) .await?; } + + for (node_name, node) in &data.nodes { + deploy_all_profiles(node, node_name, &data.generic_settings).await?; + } } (None, Some(_)) => good_panic!( "Profile provided without a node, this is not (currently) supported" -- cgit v1.2.3 From 622ae7c1b46b188171121486ef93d582a4180495 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 13:59:49 -0700 Subject: fix some logging --- src/main.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 59b79a5..cd57692 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,9 +43,6 @@ struct DeployOpts { /// The flake to deploy #[clap(default_value = ".")] flake: String, - /// Prepare server (for first deployments) - #[clap(short, long)] - prime: bool, /// Check signatures when using `nix copy` #[clap(short, long)] checksigs: bool, @@ -207,14 +204,15 @@ async fn push_profile( deploy_data: &DeployData<'_>, ) -> Result<(), Box> { info!( - "Deploying profile `{}` for node `{}`", + "Pushing profile `{}` for node `{}`", profile_name, node_name ); - info!( - "Building profile `{}` for node `{}`", + debug!( + "Building profile `{} for node `{}`", profile_name, node_name ); + if supports_flakes { Command::new("nix") .arg("build") @@ -260,7 +258,7 @@ async fn push_profile( .await?; } - info!("Copying profile `{} for node `{}`", profile_name, node_name); + debug!("Copying profile `{} for node `{}`", profile_name, node_name); let mut copy_command_ = Command::new("nix"); let mut copy_command = copy_command_.arg("copy"); @@ -360,7 +358,7 @@ async fn push_all_profiles( top_settings: &GenericSettings, check_sigs: bool, ) -> Result<(), Box> { - info!("Deploying all profiles for `{}`", node_name); + info!("Pushing all profiles for `{}`", node_name); let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); -- cgit v1.2.3 From 294a40a4ac3bb098f5a6d77f1323c1e5eca260d6 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 14:30:54 -0700 Subject: some basic modularization --- src/main.rs | 320 ++++-------------------------------------------------------- 1 file changed, 20 insertions(+), 300 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index cd57692..7e96941 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,11 @@ use clap::Clap; -use merge::Merge; -use std::{collections::HashMap, path::PathBuf}; - -use std::borrow::Cow; use std::process::Stdio; use tokio::process::Command; -use std::path::Path; +use merge::Merge; -use std::process; +use std::path::Path; extern crate pretty_env_logger; #[macro_use] @@ -18,12 +14,10 @@ extern crate log; #[macro_use] extern crate serde_derive; -macro_rules! good_panic { - ($($tts:tt)*) => {{ - error!($($tts)*); - process::exit(1); - }} -} +#[macro_use] +mod utils; + +// use utils::*; /// Simple Rust rewrite of a simple Nix Flake deployment tool #[derive(Clap, Debug)] @@ -73,289 +67,13 @@ enum SubCommand { Activate(ActivateOpts), } -#[derive(Deserialize, Debug, Clone, Merge)] -pub struct GenericSettings { - #[serde(rename(deserialize = "sshUser"))] - pub ssh_user: Option, - pub user: Option, - #[serde( - skip_serializing_if = "Vec::is_empty", - default, - rename(deserialize = "sshOpts") - )] - #[merge(strategy = merge::vec::append)] - pub ssh_opts: Vec, - #[serde(rename(deserialize = "fastConnection"))] - pub fast_connection: Option, - #[serde(rename(deserialize = "autoRollback"))] - pub auto_rollback: Option, -} - -#[derive(Deserialize, Debug, Clone)] -pub struct NodeSettings { - pub hostname: String, -} - -#[derive(Deserialize, Debug, Clone)] -pub struct ProfileSettings { - pub path: String, - pub activate: Option, - pub bootstrap: Option, -} - -#[derive(Deserialize, Debug, Clone)] -pub struct Profile { - #[serde(flatten)] - pub profile_settings: ProfileSettings, - #[serde(flatten)] - pub generic_settings: GenericSettings, -} - -#[derive(Deserialize, Debug, Clone)] -pub struct Node { - #[serde(flatten)] - pub generic_settings: GenericSettings, - #[serde(flatten)] - pub node_settings: NodeSettings, - - pub profiles: HashMap, - #[serde( - skip_serializing_if = "Vec::is_empty", - default, - rename(deserialize = "profilesOrder") - )] - pub profiles_order: Vec, -} - -#[derive(Deserialize, Debug, Clone)] -pub struct Data { - #[serde(flatten)] - pub generic_settings: GenericSettings, - pub nodes: HashMap, -} - -struct DeployData<'a> { - pub sudo: Option, - pub ssh_user: Cow<'a, str>, - pub profile_user: Cow<'a, str>, - pub profile_path: String, - pub current_exe: PathBuf, -} - -async fn make_deploy_data<'a>( - profile_name: &str, - node_name: &str, - merged_settings: &'a GenericSettings, -) -> Result, Box> { - let ssh_user: Cow = match &merged_settings.ssh_user { - Some(u) => u.into(), - None => whoami::username().into(), - }; - - let profile_user: Cow = match &merged_settings.user { - Some(x) => x.into(), - None => match &merged_settings.ssh_user { - Some(x) => x.into(), - None => good_panic!( - "Neither user nor sshUser set for profile `{}` of node `{}`", - profile_name, - node_name - ), - }, - }; - - let profile_path = match &profile_user[..] { - "root" => format!("/nix/var/nix/profiles/{}", profile_name), - _ => format!( - "/nix/var/nix/profiles/per-user/{}/{}", - profile_user, profile_name - ), - }; - - let sudo: Option = match merged_settings.user { - Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user).into()), - _ => None, - }; - - let current_exe = std::env::current_exe().expect("Expected to find current executable path"); - - if !current_exe.starts_with("/nix/store/") { - good_panic!("The deploy binary must be in the Nix store"); - } - - Ok(DeployData { - sudo, - ssh_user, - profile_user, - profile_path, - current_exe, - }) -} - -async fn push_profile( - profile: &Profile, - profile_name: &str, - node: &Node, - node_name: &str, - supports_flakes: bool, - check_sigs: bool, - repo: &str, - merged_settings: &GenericSettings, - deploy_data: &DeployData<'_>, -) -> Result<(), Box> { - info!( - "Pushing profile `{}` for node `{}`", - profile_name, node_name - ); - - debug!( - "Building profile `{} for node `{}`", - profile_name, node_name - ); - - if supports_flakes { - Command::new("nix") - .arg("build") - .arg("--no-link") - .arg(format!( - "{}#deploy.nodes.{}.profiles.{}.path", - repo, node_name, profile_name - )) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - } else { - Command::new("nix-build") - .arg(&repo) - .arg("-A") - .arg(format!( - "deploy.nodes.{}.profiles.{}.path", - node_name, profile_name - )) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - } - - if let Ok(local_key) = std::env::var("LOCAL_KEY") { - info!( - "Signing key present! Signing profile `{}` for node `{}`", - profile_name, node_name - ); - - Command::new("nix") - .arg("sign-paths") - .arg("-r") - .arg("-k") - .arg(local_key) - .arg(&profile.profile_settings.path) - .arg(&deploy_data.current_exe) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - } - - debug!("Copying profile `{} for node `{}`", profile_name, node_name); - - let mut copy_command_ = Command::new("nix"); - let mut copy_command = copy_command_.arg("copy"); - - if let Some(true) = merged_settings.fast_connection { - copy_command = copy_command.arg("--substitute-on-destination"); - } - - if !check_sigs { - copy_command = copy_command.arg("--no-check-sigs"); - } - - let ssh_opts_str = merged_settings - .ssh_opts - // This should provide some extra safety, but it also breaks for some reason, oh well - // .iter() - // .map(|x| format!("'{}'", x)) - // .collect::>() - .join(" "); - - copy_command - .arg("--to") - .arg(format!( - "ssh://{}@{}", - deploy_data.ssh_user, node.node_settings.hostname - )) - .arg(&profile.profile_settings.path) - .arg(&deploy_data.current_exe) - .env("NIX_SSHOPTS", ssh_opts_str) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - - Ok(()) -} - -async fn deploy_profile( - profile: &Profile, - profile_name: &str, - node: &Node, - node_name: &str, - merged_settings: &GenericSettings, - deploy_data: &DeployData<'_>, -) -> Result<(), Box> { - info!( - "Activating profile `{}` for node `{}`", - profile_name, node_name - ); - - let mut self_activate_command = format!( - "{} activate '{}' '{}'", - deploy_data.current_exe.as_path().to_str().unwrap(), - deploy_data.profile_path, - profile.profile_settings.path, - ); - - if let Some(sudo_cmd) = &deploy_data.sudo { - self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); - } - - if let Some(ref bootstrap_cmd) = profile.profile_settings.bootstrap { - self_activate_command = format!( - "{} --bootstrap-cmd '{}'", - self_activate_command, bootstrap_cmd - ); - } - - if let Some(ref activate_cmd) = profile.profile_settings.activate { - self_activate_command = format!( - "{} --activate-cmd '{}'", - self_activate_command, activate_cmd - ); - } - - let mut c = Command::new("ssh"); - let mut ssh_command = c.arg(format!( - "ssh://{}@{}", - deploy_data.ssh_user, node.node_settings.hostname - )); - - for ssh_opt in &merged_settings.ssh_opts { - ssh_command = ssh_command.arg(ssh_opt); - } - - ssh_command.arg(self_activate_command).spawn()?.await?; - - Ok(()) -} - #[inline] async fn push_all_profiles( - node: &Node, + node: &utils::data::Node, node_name: &str, supports_flakes: bool, repo: &str, - top_settings: &GenericSettings, + top_settings: &utils::data::GenericSettings, check_sigs: bool, ) -> Result<(), Box> { info!("Pushing all profiles for `{}`", node_name); @@ -379,9 +97,10 @@ async fn push_all_profiles( merged_settings.merge(node.generic_settings.clone()); merged_settings.merge(profile.generic_settings.clone()); - let deploy_data = make_deploy_data(profile_name, node_name, &merged_settings).await?; + let deploy_data = + utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; - push_profile( + utils::push::push_profile( profile, profile_name, node, @@ -400,9 +119,9 @@ async fn push_all_profiles( #[inline] async fn deploy_all_profiles( - node: &Node, + node: &utils::data::Node, node_name: &str, - top_settings: &GenericSettings, + top_settings: &utils::data::GenericSettings, ) -> Result<(), Box> { info!("Deploying all profiles for `{}`", node_name); @@ -425,9 +144,10 @@ async fn deploy_all_profiles( merged_settings.merge(node.generic_settings.clone()); merged_settings.merge(profile.generic_settings.clone()); - let deploy_data = make_deploy_data(profile_name, node_name, &merged_settings).await?; + let deploy_data = + utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; - deploy_profile( + utils::deploy::deploy_profile( profile, profile_name, node, @@ -513,7 +233,7 @@ async fn main() -> Result<(), Box> { } }; - let data: Data = serde_json::from_str(&data_json)?; + let data: utils::data::Data = serde_json::from_str(&data_json)?; match (maybe_node, maybe_profile) { (Some(node_name), Some(profile_name)) => { @@ -531,9 +251,9 @@ async fn main() -> Result<(), Box> { merged_settings.merge(profile.generic_settings.clone()); let deploy_data = - make_deploy_data(profile_name, node_name, &merged_settings).await?; + utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; - push_profile( + utils::push::push_profile( profile, profile_name, node, @@ -546,7 +266,7 @@ async fn main() -> Result<(), Box> { ) .await?; - deploy_profile( + utils::deploy::deploy_profile( profile, profile_name, node, -- cgit v1.2.3 From 73b99043a71f27f98bf11510fb8db46fa086383c Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 14:36:48 -0700 Subject: minor patches --- src/main.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 7e96941..74f7475 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,6 @@ extern crate serde_derive; #[macro_use] mod utils; -// use utils::*; - /// Simple Rust rewrite of a simple Nix Flake deployment tool #[derive(Clap, Debug)] #[clap(version = "1.0", author = "notgne2 ")] @@ -81,7 +79,7 @@ async fn push_all_profiles( let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); // Add any profiles which weren't in the provided order list - for (profile_name, _) in &node.profiles { + for profile_name in node.profiles.keys() { if !profiles_list.contains(&profile_name.as_str()) { profiles_list.push(&profile_name); } @@ -128,7 +126,7 @@ async fn deploy_all_profiles( let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); // Add any profiles which weren't in the provided order list - for (profile_name, _) in &node.profiles { + for profile_name in node.profiles.keys() { if !profiles_list.contains(&profile_name.as_str()) { profiles_list.push(&profile_name); } @@ -164,7 +162,7 @@ async fn deploy_all_profiles( #[tokio::main] async fn main() -> Result<(), Box> { - if let Err(_) = std::env::var("DEPLOY_LOG") { + if std::env::var("DEPLOY_LOG").is_err() { std::env::set_var("DEPLOY_LOG", "info"); } @@ -403,8 +401,7 @@ async fn main() -> Result<(), Box> { .spawn()? .await?; - // TODO: why are we doing this? - // to run the older version as long as the command is the same? + // TODO: Find some way to make sure this command never changes, otherwise this will not work Command::new("bash") .arg("-c") .arg(&activate_cmd) -- cgit v1.2.3 From f73e393a75fcad939a240ff3b72cbc75813e90e3 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 14:37:43 -0700 Subject: Add missing files --- src/utils/data.rs | 64 +++++++++++++++++++++++++++++++ src/utils/deploy.rs | 57 +++++++++++++++++++++++++++ src/utils/mod.rs | 71 ++++++++++++++++++++++++++++++++++ src/utils/push.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 300 insertions(+) create mode 100644 src/utils/data.rs create mode 100644 src/utils/deploy.rs create mode 100644 src/utils/mod.rs create mode 100644 src/utils/push.rs (limited to 'src') diff --git a/src/utils/data.rs b/src/utils/data.rs new file mode 100644 index 0000000..779d913 --- /dev/null +++ b/src/utils/data.rs @@ -0,0 +1,64 @@ +use merge::Merge; + +use std::{collections::HashMap}; + +#[derive(Deserialize, Debug, Clone, Merge)] +pub struct GenericSettings { + #[serde(rename(deserialize = "sshUser"))] + pub ssh_user: Option, + pub user: Option, + #[serde( + skip_serializing_if = "Vec::is_empty", + default, + rename(deserialize = "sshOpts") + )] + #[merge(strategy = merge::vec::append)] + pub ssh_opts: Vec, + #[serde(rename(deserialize = "fastConnection"))] + pub fast_connection: Option, + #[serde(rename(deserialize = "autoRollback"))] + pub auto_rollback: Option, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct NodeSettings { + pub hostname: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ProfileSettings { + pub path: String, + pub activate: Option, + pub bootstrap: Option, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Profile { + #[serde(flatten)] + pub profile_settings: ProfileSettings, + #[serde(flatten)] + pub generic_settings: GenericSettings, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Node { + #[serde(flatten)] + pub generic_settings: GenericSettings, + #[serde(flatten)] + pub node_settings: NodeSettings, + + pub profiles: HashMap, + #[serde( + skip_serializing_if = "Vec::is_empty", + default, + rename(deserialize = "profilesOrder") + )] + pub profiles_order: Vec, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Data { + #[serde(flatten)] + pub generic_settings: GenericSettings, + pub nodes: HashMap, +} diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs new file mode 100644 index 0000000..7260e55 --- /dev/null +++ b/src/utils/deploy.rs @@ -0,0 +1,57 @@ +use super::data; + + +use tokio::process::Command; + +pub async fn deploy_profile( + profile: &data::Profile, + profile_name: &str, + node: &data::Node, + node_name: &str, + merged_settings: &data::GenericSettings, + deploy_data: &super::DeployData<'_>, +) -> Result<(), Box> { + info!( + "Activating profile `{}` for node `{}`", + profile_name, node_name + ); + + let mut self_activate_command = format!( + "{} activate '{}' '{}'", + deploy_data.current_exe.as_path().to_str().unwrap(), + deploy_data.profile_path, + profile.profile_settings.path, + ); + + if let Some(sudo_cmd) = &deploy_data.sudo { + self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); + } + + if let Some(ref bootstrap_cmd) = profile.profile_settings.bootstrap { + self_activate_command = format!( + "{} --bootstrap-cmd '{}'", + self_activate_command, bootstrap_cmd + ); + } + + if let Some(ref activate_cmd) = profile.profile_settings.activate { + self_activate_command = format!( + "{} --activate-cmd '{}'", + self_activate_command, activate_cmd + ); + } + + let mut c = Command::new("ssh"); + let mut ssh_command = c.arg(format!( + "ssh://{}@{}", + deploy_data.ssh_user, node.node_settings.hostname + )); + + for ssh_opt in &merged_settings.ssh_opts { + ssh_command = ssh_command.arg(ssh_opt); + } + + ssh_command.arg(self_activate_command).spawn()?.await?; + + Ok(()) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..764e2e9 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,71 @@ +use std::borrow::Cow; +use std::path::PathBuf; + +pub mod data; +pub mod deploy; +pub mod push; + +macro_rules! good_panic { + ($($tts:tt)*) => {{ + error!($($tts)*); + std::process::exit(1); + }} +} + +pub struct DeployData<'a> { + pub sudo: Option, + pub ssh_user: Cow<'a, str>, + pub profile_user: Cow<'a, str>, + pub profile_path: String, + pub current_exe: PathBuf, +} + +pub async fn make_deploy_data<'a>( + profile_name: &str, + node_name: &str, + merged_settings: &'a data::GenericSettings, +) -> Result, Box> { + let ssh_user: Cow = match &merged_settings.ssh_user { + Some(u) => u.into(), + None => whoami::username().into(), + }; + + let profile_user: Cow = match &merged_settings.user { + Some(x) => x.into(), + None => match &merged_settings.ssh_user { + Some(x) => x.into(), + None => good_panic!( + "Neither user nor sshUser set for profile `{}` of node `{}`", + profile_name, + node_name + ), + }, + }; + + let profile_path = match &profile_user[..] { + "root" => format!("/nix/var/nix/profiles/{}", profile_name), + _ => format!( + "/nix/var/nix/profiles/per-user/{}/{}", + profile_user, profile_name + ), + }; + + let sudo: Option = match merged_settings.user { + Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)), + _ => None, + }; + + let current_exe = std::env::current_exe().expect("Expected to find current executable path"); + + if !current_exe.starts_with("/nix/store/") { + good_panic!("The deploy binary must be in the Nix store"); + } + + Ok(DeployData { + sudo, + ssh_user, + profile_user, + profile_path, + current_exe, + }) +} diff --git a/src/utils/push.rs b/src/utils/push.rs new file mode 100644 index 0000000..54ae013 --- /dev/null +++ b/src/utils/push.rs @@ -0,0 +1,108 @@ +use super::data; + +use std::process::Stdio; +use tokio::process::Command; + +pub async fn push_profile( + profile: &data::Profile, + profile_name: &str, + node: &data::Node, + node_name: &str, + supports_flakes: bool, + check_sigs: bool, + repo: &str, + merged_settings: &data::GenericSettings, + deploy_data: &super::DeployData<'_>, +) -> Result<(), Box> { + info!( + "Pushing profile `{}` for node `{}`", + profile_name, node_name + ); + + debug!( + "Building profile `{} for node `{}`", + profile_name, node_name + ); + + if supports_flakes { + Command::new("nix") + .arg("build") + .arg("--no-link") + .arg(format!( + "{}#deploy.nodes.{}.profiles.{}.path", + repo, node_name, profile_name + )) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + } else { + Command::new("nix-build") + .arg(&repo) + .arg("-A") + .arg(format!( + "deploy.nodes.{}.profiles.{}.path", + node_name, profile_name + )) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + } + + if let Ok(local_key) = std::env::var("LOCAL_KEY") { + info!( + "Signing key present! Signing profile `{}` for node `{}`", + profile_name, node_name + ); + + Command::new("nix") + .arg("sign-paths") + .arg("-r") + .arg("-k") + .arg(local_key) + .arg(&profile.profile_settings.path) + .arg(&deploy_data.current_exe) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + } + + debug!("Copying profile `{} for node `{}`", profile_name, node_name); + + let mut copy_command_ = Command::new("nix"); + let mut copy_command = copy_command_.arg("copy"); + + if let Some(true) = merged_settings.fast_connection { + copy_command = copy_command.arg("--substitute-on-destination"); + } + + if !check_sigs { + copy_command = copy_command.arg("--no-check-sigs"); + } + + let ssh_opts_str = merged_settings + .ssh_opts + // This should provide some extra safety, but it also breaks for some reason, oh well + // .iter() + // .map(|x| format!("'{}'", x)) + // .collect::>() + .join(" "); + + copy_command + .arg("--to") + .arg(format!( + "ssh://{}@{}", + deploy_data.ssh_user, node.node_settings.hostname + )) + .arg(&profile.profile_settings.path) + .arg(&deploy_data.current_exe) + .env("NIX_SSHOPTS", ssh_opts_str) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + Ok(()) +} -- cgit v1.2.3 From 916631d6319aa3125ededd548b174eb188c43e83 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 14:47:05 -0700 Subject: separate and add tests for flake parsing --- src/main.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 74f7475..7818652 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,6 +159,68 @@ async fn deploy_all_profiles( Ok(()) } +#[derive(PartialEq, Debug)] +struct DeployFlake<'a> { + repo: &'a str, + node: Option<&'a str>, + profile: Option<&'a str>, +} + +fn parse_flake(flake: &str) -> DeployFlake { + let flake_fragment_start = flake.find('#'); + let (repo, maybe_fragment) = match flake_fragment_start { + Some(s) => (&flake[..s], Some(&flake[s + 1..])), + None => (flake, None), + }; + + let (node, profile) = match maybe_fragment { + Some(fragment) => { + let fragment_profile_start = fragment.find('.'); + match fragment_profile_start { + Some(s) => (Some(&fragment[..s]), Some(&fragment[s + 1..])), + None => (Some(fragment), None), + } + } + None => (None, None), + }; + + DeployFlake { + repo, + node, + profile, + } +} + +#[test] +fn test_parse_flake() { + assert_eq!( + parse_flake("../deploy/examples/system#example"), + DeployFlake { + repo: "../deploy/examples/system", + node: Some("example"), + profile: None + } + ); + + assert_eq!( + parse_flake("../deploy/examples/system#example.system"), + DeployFlake { + repo: "../deploy/examples/system", + node: Some("example"), + profile: Some("system") + } + ); + + assert_eq!( + parse_flake("../deploy/examples/system"), + DeployFlake { + repo: "../deploy/examples/system", + node: None, + profile: None, + } + ); +} + #[tokio::main] async fn main() -> Result<(), Box> { @@ -172,22 +234,7 @@ async fn main() -> Result<(), Box> { match opts.subcmd { SubCommand::Deploy(deploy_opts) => { - let flake_fragment_start = deploy_opts.flake.find('#'); - let (repo, maybe_fragment) = match flake_fragment_start { - Some(s) => (&deploy_opts.flake[..s], Some(&deploy_opts.flake[s + 1..])), - None => (deploy_opts.flake.as_str(), None), - }; - - let (maybe_node, maybe_profile) = match maybe_fragment { - Some(fragment) => { - let fragment_profile_start = fragment.find('.'); - match fragment_profile_start { - Some(s) => (Some(&fragment[..s]), Some(&fragment[s + 1..])), - None => (Some(fragment), None), - } - } - None => (None, None), - }; + let deploy_flake = parse_flake(deploy_opts.flake.as_str()); let test_flake_status = Command::new("nix") .arg("eval") @@ -205,7 +252,7 @@ async fn main() -> Result<(), Box> { let c = Command::new("nix") .arg("eval") .arg("--json") - .arg(format!("{}#deploy", repo)) + .arg(format!("{}#deploy", deploy_flake.repo)) .stdout(Stdio::null()) .stderr(Stdio::null()) // TODO forward input args? @@ -221,7 +268,7 @@ async fn main() -> Result<(), Box> { .arg("--json") .arg("--eval") .arg("--E") - .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", repo)) + .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", deploy_flake.repo)) .stdout(Stdio::null()) .stderr(Stdio::null()) .output() @@ -233,7 +280,7 @@ async fn main() -> Result<(), Box> { let data: utils::data::Data = serde_json::from_str(&data_json)?; - match (maybe_node, maybe_profile) { + match (deploy_flake.node, deploy_flake.profile) { (Some(node_name), Some(profile_name)) => { let node = match data.nodes.get(node_name) { Some(x) => x, @@ -258,7 +305,7 @@ async fn main() -> Result<(), Box> { node_name, supports_flakes, deploy_opts.checksigs, - repo, + deploy_flake.repo, &merged_settings, &deploy_data, ) @@ -284,7 +331,7 @@ async fn main() -> Result<(), Box> { node, node_name, supports_flakes, - repo, + deploy_flake.repo, &data.generic_settings, deploy_opts.checksigs, ) @@ -300,7 +347,7 @@ async fn main() -> Result<(), Box> { node, node_name, supports_flakes, - repo, + deploy_flake.repo, &data.generic_settings, deploy_opts.checksigs, ) -- cgit v1.2.3 From 889fb0d3f9eee9085883fe1f31e05b07be0939ec Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 15:00:16 -0700 Subject: separate out activation logic --- src/main.rs | 169 +++----------------------------------------------- src/utils/activate.rs | 108 ++++++++++++++++++++++++++++++++ src/utils/mod.rs | 72 +++++++++++++++++++-- 3 files changed, 184 insertions(+), 165 deletions(-) create mode 100644 src/utils/activate.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 7818652..cde4fc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,6 @@ use tokio::process::Command; use merge::Merge; -use std::path::Path; - extern crate pretty_env_logger; #[macro_use] extern crate log; @@ -158,69 +156,6 @@ async fn deploy_all_profiles( Ok(()) } - -#[derive(PartialEq, Debug)] -struct DeployFlake<'a> { - repo: &'a str, - node: Option<&'a str>, - profile: Option<&'a str>, -} - -fn parse_flake(flake: &str) -> DeployFlake { - let flake_fragment_start = flake.find('#'); - let (repo, maybe_fragment) = match flake_fragment_start { - Some(s) => (&flake[..s], Some(&flake[s + 1..])), - None => (flake, None), - }; - - let (node, profile) = match maybe_fragment { - Some(fragment) => { - let fragment_profile_start = fragment.find('.'); - match fragment_profile_start { - Some(s) => (Some(&fragment[..s]), Some(&fragment[s + 1..])), - None => (Some(fragment), None), - } - } - None => (None, None), - }; - - DeployFlake { - repo, - node, - profile, - } -} - -#[test] -fn test_parse_flake() { - assert_eq!( - parse_flake("../deploy/examples/system#example"), - DeployFlake { - repo: "../deploy/examples/system", - node: Some("example"), - profile: None - } - ); - - assert_eq!( - parse_flake("../deploy/examples/system#example.system"), - DeployFlake { - repo: "../deploy/examples/system", - node: Some("example"), - profile: Some("system") - } - ); - - assert_eq!( - parse_flake("../deploy/examples/system"), - DeployFlake { - repo: "../deploy/examples/system", - node: None, - profile: None, - } - ); -} - #[tokio::main] async fn main() -> Result<(), Box> { @@ -234,7 +169,7 @@ async fn main() -> Result<(), Box> { match opts.subcmd { SubCommand::Deploy(deploy_opts) => { - let deploy_flake = parse_flake(deploy_opts.flake.as_str()); + let deploy_flake = utils::parse_flake(deploy_opts.flake.as_str()); let test_flake_status = Command::new("nix") .arg("eval") @@ -364,102 +299,14 @@ async fn main() -> Result<(), Box> { }; } SubCommand::Activate(activate_opts) => { - info!("Activating profile"); - - Command::new("nix-env") - .arg("-p") - .arg(&activate_opts.profile_path) - .arg("--set") - .arg(&activate_opts.closure) - .stdout(Stdio::null()) - .spawn()? - .await?; - - if let (Some(bootstrap_cmd), false) = ( + utils::activate::activate( + activate_opts.profile_path, + activate_opts.closure, + activate_opts.activate_cmd, activate_opts.bootstrap_cmd, - !Path::new(&activate_opts.profile_path).exists(), - ) { - let bootstrap_status = Command::new("bash") - .arg("-c") - .arg(&bootstrap_cmd) - .env("PROFILE", &activate_opts.profile_path) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .await; - - match bootstrap_status { - Ok(s) if s.success() => (), - _ => { - tokio::fs::remove_file(&activate_opts.profile_path).await?; - good_panic!("Failed to execute bootstrap command"); - } - } - } - - if let Some(activate_cmd) = activate_opts.activate_cmd { - let activate_status = Command::new("bash") - .arg("-c") - .arg(&activate_cmd) - .env("PROFILE", &activate_opts.profile_path) - .status() - .await; - - match activate_status { - Ok(s) if s.success() => (), - _ if activate_opts.auto_rollback => { - Command::new("nix-env") - .arg("-p") - .arg(&activate_opts.profile_path) - .arg("--rollback") - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - - let c = Command::new("nix-env") - .arg("-p") - .arg(&activate_opts.profile_path) - .arg("--list-generations") - .output() - .await?; - let generations_list = String::from_utf8(c.stdout)?; - - let last_generation_line = generations_list - .lines() - .last() - .expect("Expected to find a generation in list"); - - let last_generation_id = last_generation_line - .split_whitespace() - .next() - .expect("Expected to get ID from generation entry"); - - debug!("Removing generation entry {}", last_generation_line); - warn!("Removing generation by ID {}", last_generation_id); - - Command::new("nix-env") - .arg("-p") - .arg(&activate_opts.profile_path) - .arg("--delete-generations") - .arg(last_generation_id) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - - // TODO: Find some way to make sure this command never changes, otherwise this will not work - Command::new("bash") - .arg("-c") - .arg(&activate_cmd) - .spawn()? - .await?; - - good_panic!("Failed to execute activation command"); - } - _ => {} - } - } + activate_opts.auto_rollback, + ) + .await?; } } diff --git a/src/utils/activate.rs b/src/utils/activate.rs new file mode 100644 index 0000000..33774fd --- /dev/null +++ b/src/utils/activate.rs @@ -0,0 +1,108 @@ +use std::process::Stdio; +use tokio::process::Command; + +use std::path::Path; + +pub async fn activate( + profile_path: String, + closure: String, + activate_cmd: Option, + bootstrap_cmd: Option, + auto_rollback: bool, +) -> Result<(), Box> { + info!("Activating profile"); + + Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--set") + .arg(&closure) + .stdout(Stdio::null()) + .spawn()? + .await?; + + if let (Some(bootstrap_cmd), false) = (bootstrap_cmd, !Path::new(&profile_path).exists()) { + let bootstrap_status = Command::new("bash") + .arg("-c") + .arg(&bootstrap_cmd) + .env("PROFILE", &profile_path) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await; + + match bootstrap_status { + Ok(s) if s.success() => (), + _ => { + tokio::fs::remove_file(&profile_path).await?; + good_panic!("Failed to execute bootstrap command"); + } + } + } + + if let Some(activate_cmd) = activate_cmd { + let activate_status = Command::new("bash") + .arg("-c") + .arg(&activate_cmd) + .env("PROFILE", &profile_path) + .status() + .await; + + match activate_status { + Ok(s) if s.success() => (), + _ if auto_rollback => { + Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--rollback") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + let c = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--list-generations") + .output() + .await?; + let generations_list = String::from_utf8(c.stdout)?; + + let last_generation_line = generations_list + .lines() + .last() + .expect("Expected to find a generation in list"); + + let last_generation_id = last_generation_line + .split_whitespace() + .next() + .expect("Expected to get ID from generation entry"); + + debug!("Removing generation entry {}", last_generation_line); + warn!("Removing generation by ID {}", last_generation_id); + + Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--delete-generations") + .arg(last_generation_id) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + // TODO: Find some way to make sure this command never changes, otherwise this will not work + Command::new("bash") + .arg("-c") + .arg(&activate_cmd) + .spawn()? + .await?; + + good_panic!("Failed to execute activation command"); + } + _ => {} + } + } + + Ok(()) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 764e2e9..935f470 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,10 +1,7 @@ use std::borrow::Cow; use std::path::PathBuf; -pub mod data; -pub mod deploy; -pub mod push; - +#[macro_export] macro_rules! good_panic { ($($tts:tt)*) => {{ error!($($tts)*); @@ -12,6 +9,73 @@ macro_rules! good_panic { }} } +pub mod activate; +pub mod data; +pub mod deploy; +pub mod push; + +#[derive(PartialEq, Debug)] +pub struct DeployFlake<'a> { + pub repo: &'a str, + pub node: Option<&'a str>, + pub profile: Option<&'a str>, +} + +pub fn parse_flake(flake: &str) -> DeployFlake { + let flake_fragment_start = flake.find('#'); + let (repo, maybe_fragment) = match flake_fragment_start { + Some(s) => (&flake[..s], Some(&flake[s + 1..])), + None => (flake, None), + }; + + let (node, profile) = match maybe_fragment { + Some(fragment) => { + let fragment_profile_start = fragment.find('.'); + match fragment_profile_start { + Some(s) => (Some(&fragment[..s]), Some(&fragment[s + 1..])), + None => (Some(fragment), None), + } + } + None => (None, None), + }; + + DeployFlake { + repo, + node, + profile, + } +} + +#[test] +fn test_parse_flake() { + assert_eq!( + parse_flake("../deploy/examples/system#example"), + DeployFlake { + repo: "../deploy/examples/system", + node: Some("example"), + profile: None + } + ); + + assert_eq!( + parse_flake("../deploy/examples/system#example.system"), + DeployFlake { + repo: "../deploy/examples/system", + node: Some("example"), + profile: Some("system") + } + ); + + assert_eq!( + parse_flake("../deploy/examples/system"), + DeployFlake { + repo: "../deploy/examples/system", + node: None, + profile: None, + } + ); +} + pub struct DeployData<'a> { pub sudo: Option, pub ssh_user: Cow<'a, str>, -- cgit v1.2.3 From a22063343e54da9f589c7235f2f64b57fe5c257b Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 15:12:42 -0700 Subject: More functions --- src/main.rs | 89 +++++++++++++++++++++++++++++------------------------ src/utils/data.rs | 2 +- src/utils/deploy.rs | 1 - 3 files changed, 50 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index cde4fc3..e084f0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,64 +156,73 @@ async fn deploy_all_profiles( Ok(()) } -#[tokio::main] - -async fn main() -> Result<(), Box> { - if std::env::var("DEPLOY_LOG").is_err() { - std::env::set_var("DEPLOY_LOG", "info"); - } - pretty_env_logger::init_custom_env("DEPLOY_LOG"); - - let opts: Opts = Opts::parse(); - - match opts.subcmd { - SubCommand::Deploy(deploy_opts) => { - let deploy_flake = utils::parse_flake(deploy_opts.flake.as_str()); +async fn test_flake_support() -> Result> { + Ok(Command::new("nix") + .arg("eval") + .arg("--expr") + .arg("builtins.getFlake") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await? + .success()) +} - let test_flake_status = Command::new("nix") +async fn get_deployment_data( + supports_flakes: bool, + repo: &str, +) -> Result> { + let data_json = match supports_flakes { + true => { + let c = Command::new("nix") .arg("eval") - .arg("--expr") - .arg("builtins.getFlake") + .arg("--json") + .arg(format!("{}#deploy", repo)) .stdout(Stdio::null()) .stderr(Stdio::null()) - .status() + // TODO forward input args? + .output() .await?; - let supports_flakes = test_flake_status.success(); - - let data_json = match supports_flakes { - true => { - let c = Command::new("nix") - .arg("eval") - .arg("--json") - .arg(format!("{}#deploy", deploy_flake.repo)) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - // TODO forward input args? - .output() - .await?; - - String::from_utf8(c.stdout)? - } - false => { - let c = Command::new("nix-instanciate") + String::from_utf8(c.stdout)? + } + false => { + let c = Command::new("nix-instanciate") .arg("--strict") .arg("--read-write-mode") .arg("--json") .arg("--eval") .arg("--E") - .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", deploy_flake.repo)) + .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", repo)) .stdout(Stdio::null()) .stderr(Stdio::null()) .output() .await?; - String::from_utf8(c.stdout)? - } - }; + String::from_utf8(c.stdout)? + } + }; + + Ok(serde_json::from_str(&data_json)?) +} +#[tokio::main] +async fn main() -> Result<(), Box> { + if std::env::var("DEPLOY_LOG").is_err() { + std::env::set_var("DEPLOY_LOG", "info"); + } + + pretty_env_logger::init_custom_env("DEPLOY_LOG"); + + let opts: Opts = Opts::parse(); + + match opts.subcmd { + SubCommand::Deploy(deploy_opts) => { + let deploy_flake = utils::parse_flake(deploy_opts.flake.as_str()); + + let supports_flakes = test_flake_support().await?; - let data: utils::data::Data = serde_json::from_str(&data_json)?; + let data = get_deployment_data(supports_flakes, deploy_flake.repo).await?; match (deploy_flake.node, deploy_flake.profile) { (Some(node_name), Some(profile_name)) => { diff --git a/src/utils/data.rs b/src/utils/data.rs index 779d913..b28b6cd 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -1,6 +1,6 @@ use merge::Merge; -use std::{collections::HashMap}; +use std::collections::HashMap; #[derive(Deserialize, Debug, Clone, Merge)] pub struct GenericSettings { diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 7260e55..900743c 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -1,6 +1,5 @@ use super::data; - use tokio::process::Command; pub async fn deploy_profile( -- cgit v1.2.3 From 239d0f8999b47e9e76589ee1fa2d9f3459c47335 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 15:45:53 -0700 Subject: use separate binary for activation, more cleanup --- src/activate.rs | 54 +++++++++++++ src/main.rs | 220 +++++++++++++++++++++------------------------------- src/utils/data.rs | 10 ++- src/utils/deploy.rs | 17 +++- src/utils/push.rs | 2 +- 5 files changed, 163 insertions(+), 140 deletions(-) create mode 100644 src/activate.rs (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs new file mode 100644 index 0000000..c446ee0 --- /dev/null +++ b/src/activate.rs @@ -0,0 +1,54 @@ +use clap::Clap; + +extern crate pretty_env_logger; +#[macro_use] +extern crate log; + +#[macro_use] +extern crate serde_derive; + +#[macro_use] +mod utils; + +/// Activation portion of the simple Rust Nix deploy tool +#[derive(Clap, Debug)] +#[clap(version = "1.0", author = "notgne2 ")] +struct Opts { + profile_path: String, + closure: String, + + /// Command for activating the given profile + #[clap(long)] + activate_cmd: Option, + + /// Command for bootstrapping + #[clap(long)] + bootstrap_cmd: Option, + + /// Auto rollback if failure + #[clap(long)] + auto_rollback: bool, +} + + +#[tokio::main] +async fn main() -> Result<(), Box> { + if std::env::var("DEPLOY_LOG").is_err() { + std::env::set_var("DEPLOY_LOG", "info"); + } + + pretty_env_logger::init_custom_env("DEPLOY_LOG"); + + let opts: Opts = Opts::parse(); + + utils::activate::activate( + opts.profile_path, + opts.closure, + opts.activate_cmd, + opts.bootstrap_cmd, + opts.auto_rollback, + ) + .await?; + + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e084f0f..6dcb885 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,17 +19,6 @@ mod utils; #[derive(Clap, Debug)] #[clap(version = "1.0", author = "notgne2 ")] struct Opts { - /// Log verbosity - #[clap(short, long, parse(from_occurrences))] - verbose: i32, - - #[clap(subcommand)] - subcmd: SubCommand, -} - -/// Deploy profiles -#[derive(Clap, Debug)] -struct DeployOpts { /// The flake to deploy #[clap(default_value = ".")] flake: String, @@ -38,31 +27,6 @@ struct DeployOpts { checksigs: bool, } -/// Activate a profile on your current machine -#[derive(Clap, Debug)] -struct ActivateOpts { - profile_path: String, - closure: String, - - /// Command for activating the given profile - #[clap(short, long)] - activate_cmd: Option, - - /// Command for bootstrapping - #[clap(short, long)] - bootstrap_cmd: Option, - - /// Auto rollback if failure - #[clap(short, long)] - auto_rollback: bool, -} - -#[derive(Clap, Debug)] -enum SubCommand { - Deploy(DeployOpts), - Activate(ActivateOpts), -} - #[inline] async fn push_all_profiles( node: &utils::data::Node, @@ -150,6 +114,7 @@ async fn deploy_all_profiles( node_name, &merged_settings, &deploy_data, + merged_settings.auto_rollback, ) .await?; } @@ -157,6 +122,7 @@ async fn deploy_all_profiles( Ok(()) } +/// Returns if the available Nix installation supports flakes async fn test_flake_support() -> Result> { Ok(Command::new("nix") .arg("eval") @@ -169,6 +135,7 @@ async fn test_flake_support() -> Result> { .success()) } +/// Evaluates the Nix in the given `repo` and return the processed Data from it async fn get_deployment_data( supports_flakes: bool, repo: &str, @@ -216,108 +183,95 @@ async fn main() -> Result<(), Box> { let opts: Opts = Opts::parse(); - match opts.subcmd { - SubCommand::Deploy(deploy_opts) => { - let deploy_flake = utils::parse_flake(deploy_opts.flake.as_str()); - - let supports_flakes = test_flake_support().await?; - - let data = get_deployment_data(supports_flakes, deploy_flake.repo).await?; - - match (deploy_flake.node, deploy_flake.profile) { - (Some(node_name), Some(profile_name)) => { - let node = match data.nodes.get(node_name) { - Some(x) => x, - None => good_panic!("No node was found named `{}`", node_name), - }; - let profile = match node.profiles.get(profile_name) { - Some(x) => x, - None => good_panic!("No profile was found named `{}`", profile_name), - }; - - let mut merged_settings = data.generic_settings.clone(); - merged_settings.merge(node.generic_settings.clone()); - merged_settings.merge(profile.generic_settings.clone()); - - let deploy_data = - utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; - - utils::push::push_profile( - profile, - profile_name, - node, - node_name, - supports_flakes, - deploy_opts.checksigs, - deploy_flake.repo, - &merged_settings, - &deploy_data, - ) - .await?; - - utils::deploy::deploy_profile( - profile, - profile_name, - node, - node_name, - &merged_settings, - &deploy_data, - ) - .await?; - } - (Some(node_name), None) => { - let node = match data.nodes.get(node_name) { - Some(x) => x, - None => good_panic!("No node was found named `{}`", node_name), - }; - - push_all_profiles( - node, - node_name, - supports_flakes, - deploy_flake.repo, - &data.generic_settings, - deploy_opts.checksigs, - ) - .await?; - - deploy_all_profiles(node, node_name, &data.generic_settings).await?; - } - (None, None) => { - info!("Deploying all profiles on all nodes"); - - for (node_name, node) in &data.nodes { - push_all_profiles( - node, - node_name, - supports_flakes, - deploy_flake.repo, - &data.generic_settings, - deploy_opts.checksigs, - ) - .await?; - } - - for (node_name, node) in &data.nodes { - deploy_all_profiles(node, node_name, &data.generic_settings).await?; - } - } - (None, Some(_)) => good_panic!( - "Profile provided without a node, this is not (currently) supported" - ), + let deploy_flake = utils::parse_flake(opts.flake.as_str()); + + let supports_flakes = test_flake_support().await?; + + let data = get_deployment_data(supports_flakes, deploy_flake.repo).await?; + + match (deploy_flake.node, deploy_flake.profile) { + (Some(node_name), Some(profile_name)) => { + let node = match data.nodes.get(node_name) { + Some(x) => x, + None => good_panic!("No node was found named `{}`", node_name), + }; + let profile = match node.profiles.get(profile_name) { + Some(x) => x, + None => good_panic!("No profile was found named `{}`", profile_name), }; + + let mut merged_settings = data.generic_settings.clone(); + merged_settings.merge(node.generic_settings.clone()); + merged_settings.merge(profile.generic_settings.clone()); + + let deploy_data = + utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; + + utils::push::push_profile( + profile, + profile_name, + node, + node_name, + supports_flakes, + opts.checksigs, + deploy_flake.repo, + &merged_settings, + &deploy_data, + ) + .await?; + + utils::deploy::deploy_profile( + profile, + profile_name, + node, + node_name, + &merged_settings, + &deploy_data, + merged_settings.auto_rollback, + ) + .await?; } - SubCommand::Activate(activate_opts) => { - utils::activate::activate( - activate_opts.profile_path, - activate_opts.closure, - activate_opts.activate_cmd, - activate_opts.bootstrap_cmd, - activate_opts.auto_rollback, + (Some(node_name), None) => { + let node = match data.nodes.get(node_name) { + Some(x) => x, + None => good_panic!("No node was found named `{}`", node_name), + }; + + push_all_profiles( + node, + node_name, + supports_flakes, + deploy_flake.repo, + &data.generic_settings, + opts.checksigs, ) .await?; + + deploy_all_profiles(node, node_name, &data.generic_settings).await?; } - } + (None, None) => { + info!("Deploying all profiles on all nodes"); + + for (node_name, node) in &data.nodes { + push_all_profiles( + node, + node_name, + supports_flakes, + deploy_flake.repo, + &data.generic_settings, + opts.checksigs, + ) + .await?; + } + + for (node_name, node) in &data.nodes { + deploy_all_profiles(node, node_name, &data.generic_settings).await?; + } + } + (None, Some(_)) => { + good_panic!("Profile provided without a node, this is not (currently) supported") + } + }; Ok(()) } diff --git a/src/utils/data.rs b/src/utils/data.rs index b28b6cd..0753508 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -14,10 +14,12 @@ pub struct GenericSettings { )] #[merge(strategy = merge::vec::append)] pub ssh_opts: Vec, - #[serde(rename(deserialize = "fastConnection"))] - pub fast_connection: Option, - #[serde(rename(deserialize = "autoRollback"))] - pub auto_rollback: Option, + #[serde(rename(deserialize = "fastConnection"), default)] + #[merge(strategy = merge::bool::overwrite_false)] + pub fast_connection: bool, + #[serde(rename(deserialize = "autoRollback"), default)] + #[merge(strategy = merge::bool::overwrite_false)] + pub auto_rollback: bool, } #[derive(Deserialize, Debug, Clone)] diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 900743c..42bd0b4 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -9,6 +9,7 @@ pub async fn deploy_profile( node_name: &str, merged_settings: &data::GenericSettings, deploy_data: &super::DeployData<'_>, + auto_rollback: bool, ) -> Result<(), Box> { info!( "Activating profile `{}` for node `{}`", @@ -16,8 +17,16 @@ pub async fn deploy_profile( ); let mut self_activate_command = format!( - "{} activate '{}' '{}'", - deploy_data.current_exe.as_path().to_str().unwrap(), + "{} '{}' '{}'", + deploy_data + .current_exe + .as_path() + .parent() + .unwrap() + .to_str() + .unwrap() + .to_owned() + + "/activate", deploy_data.profile_path, profile.profile_settings.path, ); @@ -40,6 +49,10 @@ pub async fn deploy_profile( ); } + if auto_rollback { + self_activate_command = format!("{} --auto-rollback", self_activate_command); + } + let mut c = Command::new("ssh"); let mut ssh_command = c.arg(format!( "ssh://{}@{}", diff --git a/src/utils/push.rs b/src/utils/push.rs index 54ae013..0e1b9ba 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -74,7 +74,7 @@ pub async fn push_profile( let mut copy_command_ = Command::new("nix"); let mut copy_command = copy_command_.arg("copy"); - if let Some(true) = merged_settings.fast_connection { + if merged_settings.fast_connection { copy_command = copy_command.arg("--substitute-on-destination"); } -- cgit v1.2.3 From edaed825650eea32878441d3b8c7eb40e8877882 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 28 Sep 2020 16:17:31 -0700 Subject: Add examples --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 6dcb885..513fbf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,6 +123,7 @@ async fn deploy_all_profiles( } /// Returns if the available Nix installation supports flakes +#[inline] async fn test_flake_support() -> Result> { Ok(Command::new("nix") .arg("eval") @@ -136,6 +137,7 @@ async fn test_flake_support() -> Result> { } /// Evaluates the Nix in the given `repo` and return the processed Data from it +#[inline] async fn get_deployment_data( supports_flakes: bool, repo: &str, -- cgit v1.2.3 From 93a04f7e3037f69bdeab777d2fc6c4fb37795f4e Mon Sep 17 00:00:00 2001 From: notgne2 Date: Tue, 29 Sep 2020 12:36:26 -0700 Subject: Pass extra arguments to the Nix build command --- src/main.rs | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 513fbf2..668b697 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,8 @@ struct Opts { /// Check signatures when using `nix copy` #[clap(short, long)] checksigs: bool, + /// Extra arguments to be passed to nix build + extra_build_args: Vec, } #[inline] @@ -141,38 +143,47 @@ async fn test_flake_support() -> Result> { async fn get_deployment_data( supports_flakes: bool, repo: &str, + extra_build_args: Vec, ) -> Result> { - let data_json = match supports_flakes { - true => { - let c = Command::new("nix") - .arg("eval") - .arg("--json") - .arg(format!("{}#deploy", repo)) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - // TODO forward input args? - .output() - .await?; + let mut c = match supports_flakes { + true => Command::new("nix"), + false => Command::new("nix-instanciate"), + }; - String::from_utf8(c.stdout)? + let mut build_command = match supports_flakes { + true => { + c.arg("eval").arg("--json").arg(format!("{}#deploy", repo)) } false => { - let c = Command::new("nix-instanciate") + c .arg("--strict") .arg("--read-write-mode") .arg("--json") .arg("--eval") .arg("--E") .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", repo)) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .output() - .await?; - - String::from_utf8(c.stdout)? } }; + for extra_arg in extra_build_args { + build_command = build_command.arg(extra_arg); + } + + let build_output = build_command + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .output() + .await?; + + if !build_output.status.success() { + good_panic!( + "Error building deploy props for the provided flake: {}", + repo + ); + } + + let data_json = String::from_utf8(build_output.stdout)?; + Ok(serde_json::from_str(&data_json)?) } #[tokio::main] @@ -189,7 +200,8 @@ async fn main() -> Result<(), Box> { let supports_flakes = test_flake_support().await?; - let data = get_deployment_data(supports_flakes, deploy_flake.repo).await?; + let data = + get_deployment_data(supports_flakes, deploy_flake.repo, opts.extra_build_args).await?; match (deploy_flake.node, deploy_flake.profile) { (Some(node_name), Some(profile_name)) => { -- cgit v1.2.3 From e3c55575ca6bfd0c9166c52b4aac76b3761bb313 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Tue, 29 Sep 2020 12:40:32 -0700 Subject: Move all activation logic to activate.rs (the unused warnings got annoying) --- src/activate.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++- src/utils/activate.rs | 108 ------------------------------------------------- src/utils/mod.rs | 1 - 3 files changed, 109 insertions(+), 110 deletions(-) delete mode 100644 src/utils/activate.rs (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index c446ee0..8e22aea 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -1,5 +1,10 @@ use clap::Clap; +use std::process::Stdio; +use tokio::process::Command; + +use std::path::Path; + extern crate pretty_env_logger; #[macro_use] extern crate log; @@ -30,6 +35,109 @@ struct Opts { auto_rollback: bool, } +pub async fn activate( + profile_path: String, + closure: String, + activate_cmd: Option, + bootstrap_cmd: Option, + auto_rollback: bool, +) -> Result<(), Box> { + info!("Activating profile"); + + Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--set") + .arg(&closure) + .stdout(Stdio::null()) + .spawn()? + .await?; + + if let (Some(bootstrap_cmd), false) = (bootstrap_cmd, !Path::new(&profile_path).exists()) { + let bootstrap_status = Command::new("bash") + .arg("-c") + .arg(&bootstrap_cmd) + .env("PROFILE", &profile_path) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await; + + match bootstrap_status { + Ok(s) if s.success() => (), + _ => { + tokio::fs::remove_file(&profile_path).await?; + good_panic!("Failed to execute bootstrap command"); + } + } + } + + if let Some(activate_cmd) = activate_cmd { + let activate_status = Command::new("bash") + .arg("-c") + .arg(&activate_cmd) + .env("PROFILE", &profile_path) + .status() + .await; + + match activate_status { + Ok(s) if s.success() => (), + _ if auto_rollback => { + Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--rollback") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + let c = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--list-generations") + .output() + .await?; + let generations_list = String::from_utf8(c.stdout)?; + + let last_generation_line = generations_list + .lines() + .last() + .expect("Expected to find a generation in list"); + + let last_generation_id = last_generation_line + .split_whitespace() + .next() + .expect("Expected to get ID from generation entry"); + + debug!("Removing generation entry {}", last_generation_line); + warn!("Removing generation by ID {}", last_generation_id); + + Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--delete-generations") + .arg(last_generation_id) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()? + .await?; + + // TODO: Find some way to make sure this command never changes, otherwise this will not work + Command::new("bash") + .arg("-c") + .arg(&activate_cmd) + .spawn()? + .await?; + + good_panic!("Failed to execute activation command"); + } + _ => {} + } + } + + Ok(()) +} #[tokio::main] async fn main() -> Result<(), Box> { @@ -41,7 +149,7 @@ async fn main() -> Result<(), Box> { let opts: Opts = Opts::parse(); - utils::activate::activate( + activate( opts.profile_path, opts.closure, opts.activate_cmd, diff --git a/src/utils/activate.rs b/src/utils/activate.rs deleted file mode 100644 index 33774fd..0000000 --- a/src/utils/activate.rs +++ /dev/null @@ -1,108 +0,0 @@ -use std::process::Stdio; -use tokio::process::Command; - -use std::path::Path; - -pub async fn activate( - profile_path: String, - closure: String, - activate_cmd: Option, - bootstrap_cmd: Option, - auto_rollback: bool, -) -> Result<(), Box> { - info!("Activating profile"); - - Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--set") - .arg(&closure) - .stdout(Stdio::null()) - .spawn()? - .await?; - - if let (Some(bootstrap_cmd), false) = (bootstrap_cmd, !Path::new(&profile_path).exists()) { - let bootstrap_status = Command::new("bash") - .arg("-c") - .arg(&bootstrap_cmd) - .env("PROFILE", &profile_path) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .await; - - match bootstrap_status { - Ok(s) if s.success() => (), - _ => { - tokio::fs::remove_file(&profile_path).await?; - good_panic!("Failed to execute bootstrap command"); - } - } - } - - if let Some(activate_cmd) = activate_cmd { - let activate_status = Command::new("bash") - .arg("-c") - .arg(&activate_cmd) - .env("PROFILE", &profile_path) - .status() - .await; - - match activate_status { - Ok(s) if s.success() => (), - _ if auto_rollback => { - Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--rollback") - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - - let c = Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--list-generations") - .output() - .await?; - let generations_list = String::from_utf8(c.stdout)?; - - let last_generation_line = generations_list - .lines() - .last() - .expect("Expected to find a generation in list"); - - let last_generation_id = last_generation_line - .split_whitespace() - .next() - .expect("Expected to get ID from generation entry"); - - debug!("Removing generation entry {}", last_generation_line); - warn!("Removing generation by ID {}", last_generation_id); - - Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--delete-generations") - .arg(last_generation_id) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn()? - .await?; - - // TODO: Find some way to make sure this command never changes, otherwise this will not work - Command::new("bash") - .arg("-c") - .arg(&activate_cmd) - .spawn()? - .await?; - - good_panic!("Failed to execute activation command"); - } - _ => {} - } - } - - Ok(()) -} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 935f470..8861692 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -9,7 +9,6 @@ macro_rules! good_panic { }} } -pub mod activate; pub mod data; pub mod deploy; pub mod push; -- cgit v1.2.3 From 8d21dd335e5259dadf832a5d1a7c72b9dd1f4400 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Tue, 29 Sep 2020 15:10:06 -0700 Subject: Add license information, reformat Nix files, clean up --- src/activate.rs | 4 ++++ src/main.rs | 6 +++++- src/utils/data.rs | 4 ++++ src/utils/deploy.rs | 4 ++++ src/utils/mod.rs | 4 ++++ src/utils/push.rs | 4 ++++ 6 files changed, 25 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index 8e22aea..3c7c16d 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: 2020 Serokell +// +// SPDX-License-Identifier: MPL-2.0 + use clap::Clap; use std::process::Stdio; diff --git a/src/main.rs b/src/main.rs index 668b697..9e50674 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: 2020 Serokell +// +// SPDX-License-Identifier: MPL-2.0 + use clap::Clap; use std::process::Stdio; @@ -17,7 +21,7 @@ mod utils; /// Simple Rust rewrite of a simple Nix Flake deployment tool #[derive(Clap, Debug)] -#[clap(version = "1.0", author = "notgne2 ")] +#[clap(version = "1.0", author = "Serokell ")] struct Opts { /// The flake to deploy #[clap(default_value = ".")] diff --git a/src/utils/data.rs b/src/utils/data.rs index 0753508..d1dae5b 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: 2020 Serokell +// +// SPDX-License-Identifier: MPL-2.0 + use merge::Merge; use std::collections::HashMap; diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 42bd0b4..247d5e5 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: 2020 Serokell +// +// SPDX-License-Identifier: MPL-2.0 + use super::data; use tokio::process::Command; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8861692..5802627 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: 2020 Serokell +// +// SPDX-License-Identifier: MPL-2.0 + use std::borrow::Cow; use std::path::PathBuf; diff --git a/src/utils/push.rs b/src/utils/push.rs index 0e1b9ba..c87c32b 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: 2020 Serokell +// +// SPDX-License-Identifier: MPL-2.0 + use super::data; use std::process::Stdio; -- cgit v1.2.3 From a0328dbcf76b7c551e92fd25060cfc7d7e4d9ebe Mon Sep 17 00:00:00 2001 From: notgne2 Date: Tue, 29 Sep 2020 21:27:49 -0700 Subject: More separation and component testing --- src/main.rs | 57 +++++++++++++++----------- src/utils/deploy.rs | 116 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 122 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 668b697..f8b03a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,7 +143,7 @@ async fn test_flake_support() -> Result> { async fn get_deployment_data( supports_flakes: bool, repo: &str, - extra_build_args: Vec, + extra_build_args: &[String], ) -> Result> { let mut c = match supports_flakes { true => Command::new("nix"), @@ -156,12 +156,12 @@ async fn get_deployment_data( } false => { c - .arg("--strict") - .arg("--read-write-mode") - .arg("--json") - .arg("--eval") - .arg("--E") - .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", repo)) + .arg("--strict") + .arg("--read-write-mode") + .arg("--json") + .arg("--eval") + .arg("--E") + .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", repo)) } }; @@ -186,23 +186,13 @@ async fn get_deployment_data( Ok(serde_json::from_str(&data_json)?) } -#[tokio::main] -async fn main() -> Result<(), Box> { - if std::env::var("DEPLOY_LOG").is_err() { - std::env::set_var("DEPLOY_LOG", "info"); - } - - pretty_env_logger::init_custom_env("DEPLOY_LOG"); - - let opts: Opts = Opts::parse(); - - let deploy_flake = utils::parse_flake(opts.flake.as_str()); - - let supports_flakes = test_flake_support().await?; - - let data = - get_deployment_data(supports_flakes, deploy_flake.repo, opts.extra_build_args).await?; +async fn run_deploy( + deploy_flake: utils::DeployFlake<'_>, + data: utils::data::Data, + supports_flakes: bool, + opts: &Opts, +) -> Result<(), Box> { match (deploy_flake.node, deploy_flake.profile) { (Some(node_name), Some(profile_name)) => { let node = match data.nodes.get(node_name) { @@ -289,3 +279,24 @@ async fn main() -> Result<(), Box> { Ok(()) } +#[tokio::main] +async fn main() -> Result<(), Box> { + if std::env::var("DEPLOY_LOG").is_err() { + std::env::set_var("DEPLOY_LOG", "info"); + } + + pretty_env_logger::init_custom_env("DEPLOY_LOG"); + + let opts: Opts = Opts::parse(); + + let deploy_flake = utils::parse_flake(opts.flake.as_str()); + + let supports_flakes = test_flake_support().await?; + + let data = + get_deployment_data(supports_flakes, deploy_flake.repo, &opts.extra_build_args).await?; + + run_deploy(deploy_flake, data, supports_flakes, &opts).await?; + + Ok(()) +} diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 42bd0b4..c44c0d3 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -2,47 +2,54 @@ use super::data; use tokio::process::Command; -pub async fn deploy_profile( - profile: &data::Profile, - profile_name: &str, - node: &data::Node, - node_name: &str, - merged_settings: &data::GenericSettings, - deploy_data: &super::DeployData<'_>, - auto_rollback: bool, -) -> Result<(), Box> { - info!( - "Activating profile `{}` for node `{}`", - profile_name, node_name - ); - - let mut self_activate_command = format!( - "{} '{}' '{}'", - deploy_data - .current_exe - .as_path() +fn deploy_path_to_activate_path_str( + deploy_path: &std::path::Path, +) -> Result> { + Ok(format!( + "{}/activate", + deploy_path .parent() - .unwrap() + .ok_or("Deploy path too short")? .to_str() - .unwrap() + .ok_or("Deploy path is not valid utf8")? .to_owned() - + "/activate", - deploy_data.profile_path, - profile.profile_settings.path, - ); + )) +} - if let Some(sudo_cmd) = &deploy_data.sudo { +#[test] +fn test_activate_path_generation() { + match deploy_path_to_activate_path_str(&std::path::PathBuf::from( + "/blah/blah/deploy-rs/bin/deploy", + )) { + Err(_) => panic!(""), + Ok(x) => assert_eq!(x, "/blah/blah/deploy-rs/bin/activate".to_string()), + } +} + +fn build_activate_command( + activate_path_str: String, + sudo: &Option, + profile_path: &str, + closure: &str, + activate_cmd: &Option, + bootstrap_cmd: &Option, + auto_rollback: bool, +) -> Result> { + let mut self_activate_command = + format!("{} '{}' '{}'", activate_path_str, profile_path, closure); + + if let Some(sudo_cmd) = &sudo { self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); } - if let Some(ref bootstrap_cmd) = profile.profile_settings.bootstrap { + if let Some(ref bootstrap_cmd) = bootstrap_cmd { self_activate_command = format!( "{} --bootstrap-cmd '{}'", self_activate_command, bootstrap_cmd ); } - if let Some(ref activate_cmd) = profile.profile_settings.activate { + if let Some(ref activate_cmd) = activate_cmd { self_activate_command = format!( "{} --activate-cmd '{}'", self_activate_command, activate_cmd @@ -53,6 +60,59 @@ pub async fn deploy_profile( self_activate_command = format!("{} --auto-rollback", self_activate_command); } + Ok(self_activate_command) +} + +#[test] +fn test_activation_command_builder() { + let activate_path_str = "/blah/bin/activate".to_string(); + let sudo = Some("sudo -u test".to_string()); + let profile_path = "/blah/profiles/test"; + let closure = "/blah/etc"; + let activate_cmd = Some("$THING/bin/aaaaaaa".to_string()); + let bootstrap_cmd = None; + let auto_rollback = true; + + match build_activate_command( + activate_path_str, + &sudo, + profile_path, + closure, + &activate_cmd, + &bootstrap_cmd, + auto_rollback, + ) { + Err(_) => panic!(""), + Ok(x) => assert_eq!(x, "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' --activate-cmd '$THING/bin/aaaaaaa' --auto-rollback".to_string()), + } +} + +pub async fn deploy_profile( + profile: &data::Profile, + profile_name: &str, + node: &data::Node, + node_name: &str, + merged_settings: &data::GenericSettings, + deploy_data: &super::DeployData<'_>, + auto_rollback: bool, +) -> Result<(), Box> { + info!( + "Activating profile `{}` for node `{}`", + profile_name, node_name + ); + + let activate_path_str = deploy_path_to_activate_path_str(&deploy_data.current_exe)?; + + let self_activate_command = build_activate_command( + activate_path_str, + &deploy_data.sudo, + &deploy_data.profile_path, + &profile.profile_settings.path, + &profile.profile_settings.activate, + &profile.profile_settings.bootstrap, + auto_rollback, + )?; + let mut c = Command::new("ssh"); let mut ssh_command = c.arg(format!( "ssh://{}@{}", -- cgit v1.2.3 From ea5aab76849ba3ce9ff2b7eba2a391d4ea33fa3a Mon Sep 17 00:00:00 2001 From: notgne2 Date: Thu, 1 Oct 2020 12:43:33 -0700 Subject: Improve nix copy stuff --- src/main.rs | 2 ++ src/utils/deploy.rs | 26 +------------------------- src/utils/mod.rs | 24 ++++++++++++++++++++++++ src/utils/push.rs | 10 ++++++---- 4 files changed, 33 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 75842e5..ba87e46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,6 +149,8 @@ async fn get_deployment_data( repo: &str, extra_build_args: &[String], ) -> Result> { + info!("Evaluating flake in {}", repo); + let mut c = match supports_flakes { true => Command::new("nix"), false => Command::new("nix-instanciate"), diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 9c258fd..1abae64 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -6,30 +6,6 @@ use super::data; use tokio::process::Command; -fn deploy_path_to_activate_path_str( - deploy_path: &std::path::Path, -) -> Result> { - Ok(format!( - "{}/activate", - deploy_path - .parent() - .ok_or("Deploy path too short")? - .to_str() - .ok_or("Deploy path is not valid utf8")? - .to_owned() - )) -} - -#[test] -fn test_activate_path_generation() { - match deploy_path_to_activate_path_str(&std::path::PathBuf::from( - "/blah/blah/deploy-rs/bin/deploy", - )) { - Err(_) => panic!(""), - Ok(x) => assert_eq!(x, "/blah/blah/deploy-rs/bin/activate".to_string()), - } -} - fn build_activate_command( activate_path_str: String, sudo: &Option, @@ -105,7 +81,7 @@ pub async fn deploy_profile( profile_name, node_name ); - let activate_path_str = deploy_path_to_activate_path_str(&deploy_data.current_exe)?; + let activate_path_str = super::deploy_path_to_activate_path_str(&deploy_data.current_exe)?; let self_activate_command = build_activate_command( activate_path_str, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 5802627..30201c3 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -136,3 +136,27 @@ pub async fn make_deploy_data<'a>( current_exe, }) } + +pub fn deploy_path_to_activate_path_str( + deploy_path: &std::path::Path, +) -> Result> { + Ok(format!( + "{}/activate", + deploy_path + .parent() + .ok_or("Deploy path too short")? + .to_str() + .ok_or("Deploy path is not valid utf8")? + .to_owned() + )) +} + +#[test] +fn test_activate_path_generation() { + match deploy_path_to_activate_path_str(&std::path::PathBuf::from( + "/blah/blah/deploy-rs/bin/deploy", + )) { + Err(_) => panic!(""), + Ok(x) => assert_eq!(x, "/blah/blah/deploy-rs/bin/activate".to_string()), + } +} diff --git a/src/utils/push.rs b/src/utils/push.rs index c87c32b..38a576f 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -66,7 +66,9 @@ pub async fn push_profile( .arg("-k") .arg(local_key) .arg(&profile.profile_settings.path) - .arg(&deploy_data.current_exe) + .arg(&super::deploy_path_to_activate_path_str( + &deploy_data.current_exe, + )?) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn()? @@ -101,10 +103,10 @@ pub async fn push_profile( deploy_data.ssh_user, node.node_settings.hostname )) .arg(&profile.profile_settings.path) - .arg(&deploy_data.current_exe) + .arg(&super::deploy_path_to_activate_path_str( + &deploy_data.current_exe, + )?) .env("NIX_SSHOPTS", ssh_opts_str) - .stdout(Stdio::null()) - .stderr(Stdio::null()) .spawn()? .await?; -- cgit v1.2.3 From e14acaf2bdc14bbdc30f3d558b62f64fe33ff5f9 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Thu, 1 Oct 2020 18:21:40 -0700 Subject: Rework system for deploy properties, add CLI override flags --- src/main.rs | 140 ++++++++++++++++++++++++++++++-------------- src/utils/deploy.rs | 37 +++++------- src/utils/mod.rs | 166 +++++++++++++++++++++++++++++++++++++++------------- src/utils/push.rs | 46 +++++++-------- 4 files changed, 260 insertions(+), 129 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ba87e46..0d80e42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,25 @@ struct Opts { checksigs: bool, /// Extra arguments to be passed to nix build extra_build_args: Vec, + + /// Override the SSH user with the given value + #[clap(long)] + ssh_user: Option, + /// Override the profile user with the given value + #[clap(long)] + profile_user: Option, + /// Override the SSH options used + #[clap(long)] + ssh_opts: Option, + /// Override if the connecting to the target node should be considered fast + #[clap(long)] + fast_connection: Option, + /// Override if a rollback should be attempted if activation fails + #[clap(long)] + auto_rollback: Option, + /// Override hostname used for the node + #[clap(long)] + hostname: Option, } #[inline] @@ -41,6 +60,7 @@ async fn push_all_profiles( repo: &str, top_settings: &utils::data::GenericSettings, check_sigs: bool, + cmd_overrides: &utils::CmdOverrides, ) -> Result<(), Box> { info!("Pushing all profiles for `{}`", node_name); @@ -63,19 +83,23 @@ async fn push_all_profiles( merged_settings.merge(node.generic_settings.clone()); merged_settings.merge(profile.generic_settings.clone()); - let deploy_data = - utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; - - utils::push::push_profile( - profile, - profile_name, + let deploy_data = utils::make_deploy_data( + top_settings, node, node_name, + profile, + profile_name, + cmd_overrides, + )?; + + let deploy_defs = deploy_data.defs(); + + utils::push::push_profile( supports_flakes, check_sigs, repo, - &merged_settings, &deploy_data, + &deploy_defs, ) .await?; } @@ -88,6 +112,7 @@ async fn deploy_all_profiles( node: &utils::data::Node, node_name: &str, top_settings: &utils::data::GenericSettings, + cmd_overrides: &utils::CmdOverrides, ) -> Result<(), Box> { info!("Deploying all profiles for `{}`", node_name); @@ -110,19 +135,18 @@ async fn deploy_all_profiles( merged_settings.merge(node.generic_settings.clone()); merged_settings.merge(profile.generic_settings.clone()); - let deploy_data = - utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; - - utils::deploy::deploy_profile( - profile, - profile_name, + let deploy_data = utils::make_deploy_data( + top_settings, node, node_name, - &merged_settings, - &deploy_data, - merged_settings.auto_rollback, - ) - .await?; + profile, + profile_name, + cmd_overrides, + )?; + + let deploy_defs = deploy_data.defs(); + + utils::deploy::deploy_profile(&deploy_data, &deploy_defs).await?; } Ok(()) @@ -197,7 +221,8 @@ async fn run_deploy( deploy_flake: utils::DeployFlake<'_>, data: utils::data::Data, supports_flakes: bool, - opts: &Opts, + check_sigs: bool, + cmd_overrides: utils::CmdOverrides, ) -> Result<(), Box> { match (deploy_flake.node, deploy_flake.profile) { (Some(node_name), Some(profile_name)) => { @@ -210,36 +235,27 @@ async fn run_deploy( None => good_panic!("No profile was found named `{}`", profile_name), }; - let mut merged_settings = data.generic_settings.clone(); - merged_settings.merge(node.generic_settings.clone()); - merged_settings.merge(profile.generic_settings.clone()); + let deploy_data = utils::make_deploy_data( + &data.generic_settings, + node, + node_name, + profile, + profile_name, + &cmd_overrides, + )?; - let deploy_data = - utils::make_deploy_data(profile_name, node_name, &merged_settings).await?; + let deploy_defs = deploy_data.defs(); utils::push::push_profile( - profile, - profile_name, - node, - node_name, supports_flakes, - opts.checksigs, + check_sigs, deploy_flake.repo, - &merged_settings, &deploy_data, + &deploy_defs, ) .await?; - utils::deploy::deploy_profile( - profile, - profile_name, - node, - node_name, - &merged_settings, - &deploy_data, - merged_settings.auto_rollback, - ) - .await?; + utils::deploy::deploy_profile(&deploy_data, &deploy_defs).await?; } (Some(node_name), None) => { let node = match data.nodes.get(node_name) { @@ -253,11 +269,12 @@ async fn run_deploy( supports_flakes, deploy_flake.repo, &data.generic_settings, - opts.checksigs, + check_sigs, + &cmd_overrides, ) .await?; - deploy_all_profiles(node, node_name, &data.generic_settings).await?; + deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides).await?; } (None, None) => { info!("Deploying all profiles on all nodes"); @@ -269,13 +286,15 @@ async fn run_deploy( supports_flakes, deploy_flake.repo, &data.generic_settings, - opts.checksigs, + check_sigs, + &cmd_overrides, ) .await?; } for (node_name, node) in &data.nodes { - deploy_all_profiles(node, node_name, &data.generic_settings).await?; + deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides) + .await?; } } (None, Some(_)) => { @@ -285,6 +304,7 @@ async fn run_deploy( Ok(()) } + #[tokio::main] async fn main() -> Result<(), Box> { if std::env::var("DEPLOY_LOG").is_err() { @@ -297,12 +317,42 @@ async fn main() -> Result<(), Box> { let deploy_flake = utils::parse_flake(opts.flake.as_str()); + let cmd_overrides = utils::CmdOverrides { + ssh_user: opts.ssh_user, + profile_user: opts.profile_user, + ssh_opts: opts.ssh_opts, + fast_connection: opts.fast_connection, + auto_rollback: opts.auto_rollback, + hostname: opts.hostname, + }; + + match (cmd_overrides.purity(), deploy_flake.node, deploy_flake.profile) { + (utils::OverridePurity::ErrorProfile, _, None) => good_panic!( + "You have specified an override not suitible for deploying to multiple profiles, please specify your target profile explicitly" + ), + (utils::OverridePurity::Error, None, _) => good_panic!( + "You have specified an override not suitible for deploying to multiple nodes, please specify your target node explicitly" + ), + + (utils::OverridePurity::Warn, None, _) => warn!( + "Certain overrides you have provided might be dangerous when used on multiple nodes or profiles, be cautious" + ), + _ => (), + }; + let supports_flakes = test_flake_support().await?; let data = get_deployment_data(supports_flakes, deploy_flake.repo, &opts.extra_build_args).await?; - run_deploy(deploy_flake, data, supports_flakes, &opts).await?; + run_deploy( + deploy_flake, + data, + supports_flakes, + opts.checksigs, + cmd_overrides, + ) + .await?; Ok(()) } diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 1abae64..d46f2db 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -2,8 +2,6 @@ // // SPDX-License-Identifier: MPL-2.0 -use super::data; - use tokio::process::Command; fn build_activate_command( @@ -68,38 +66,35 @@ fn test_activation_command_builder() { } pub async fn deploy_profile( - profile: &data::Profile, - profile_name: &str, - node: &data::Node, - node_name: &str, - merged_settings: &data::GenericSettings, deploy_data: &super::DeployData<'_>, - auto_rollback: bool, + deploy_defs: &super::DeployDefs<'_>, ) -> Result<(), Box> { info!( "Activating profile `{}` for node `{}`", - profile_name, node_name + deploy_data.profile_name, deploy_data.node_name ); - let activate_path_str = super::deploy_path_to_activate_path_str(&deploy_data.current_exe)?; + let activate_path_str = super::deploy_path_to_activate_path_str(&deploy_defs.current_exe)?; let self_activate_command = build_activate_command( activate_path_str, - &deploy_data.sudo, - &deploy_data.profile_path, - &profile.profile_settings.path, - &profile.profile_settings.activate, - &profile.profile_settings.bootstrap, - auto_rollback, + &deploy_defs.sudo, + &deploy_defs.profile_path, + &deploy_data.profile.profile_settings.path, + &deploy_data.profile.profile_settings.activate, + &deploy_data.profile.profile_settings.bootstrap, + deploy_data.merged_settings.auto_rollback, )?; + let hostname = match deploy_data.cmd_overrides.hostname { + Some(ref x) => x, + None => &deploy_data.node.node_settings.hostname, + }; + let mut c = Command::new("ssh"); - let mut ssh_command = c.arg(format!( - "ssh://{}@{}", - deploy_data.ssh_user, node.node_settings.hostname - )); + let mut ssh_command = c.arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)); - for ssh_opt in &merged_settings.ssh_opts { + for ssh_opt in &deploy_data.merged_settings.ssh_opts { ssh_command = ssh_command.arg(ssh_opt); } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 30201c3..bfdbc5e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -5,6 +5,8 @@ use std::borrow::Cow; use std::path::PathBuf; +use merge::Merge; + #[macro_export] macro_rules! good_panic { ($($tts:tt)*) => {{ @@ -17,6 +19,40 @@ pub mod data; pub mod deploy; pub mod push; +pub struct CmdOverrides { + pub ssh_user: Option, + pub profile_user: Option, + pub ssh_opts: Option, + pub fast_connection: Option, + pub auto_rollback: Option, + pub hostname: Option, +} + +pub enum OverridePurity { + ErrorProfile, + Error, + Warn, + Pure, +} + +impl CmdOverrides { + pub fn purity(&self) -> OverridePurity { + if self.profile_user.is_some() { + return OverridePurity::ErrorProfile; + } + + if self.hostname.is_some() || self.ssh_user.is_some() { + return OverridePurity::Error; + } + + if self.ssh_opts.is_some() || self.fast_connection.is_some() { + return OverridePurity::Warn; + } + + OverridePurity::Pure + } +} + #[derive(PartialEq, Debug)] pub struct DeployFlake<'a> { pub repo: &'a str, @@ -80,60 +116,110 @@ fn test_parse_flake() { } pub struct DeployData<'a> { - pub sudo: Option, + pub node_name: &'a str, + pub node: &'a data::Node, + pub profile_name: &'a str, + pub profile: &'a data::Profile, + + pub cmd_overrides: &'a CmdOverrides, + + pub merged_settings: data::GenericSettings, +} + +pub struct DeployDefs<'a> { pub ssh_user: Cow<'a, str>, pub profile_user: Cow<'a, str>, pub profile_path: String, pub current_exe: PathBuf, + pub sudo: Option, } -pub async fn make_deploy_data<'a>( - profile_name: &str, - node_name: &str, - merged_settings: &'a data::GenericSettings, -) -> Result, Box> { - let ssh_user: Cow = match &merged_settings.ssh_user { - Some(u) => u.into(), - None => whoami::username().into(), - }; - - let profile_user: Cow = match &merged_settings.user { - Some(x) => x.into(), - None => match &merged_settings.ssh_user { - Some(x) => x.into(), - None => good_panic!( - "Neither user nor sshUser set for profile `{}` of node `{}`", - profile_name, - node_name +impl<'a> DeployData<'a> { + pub fn defs(&'a self) -> DeployDefs<'a> { + let ssh_user: Cow = match self.merged_settings.ssh_user { + Some(ref u) => u.into(), + None => whoami::username().into(), + }; + + let profile_user: Cow = match self.merged_settings.user { + Some(ref x) => x.into(), + None => match self.merged_settings.ssh_user { + Some(ref x) => x.into(), + None => good_panic!( + "Neither user nor sshUser set for profile `{}` of node `{}`", + self.profile_name, + self.node_name + ), + }, + }; + + let profile_path = match &profile_user[..] { + "root" => format!("/nix/var/nix/profiles/{}", self.profile_name), + _ => format!( + "/nix/var/nix/profiles/per-user/{}/{}", + profile_user, self.profile_name ), - }, - }; + }; - let profile_path = match &profile_user[..] { - "root" => format!("/nix/var/nix/profiles/{}", profile_name), - _ => format!( - "/nix/var/nix/profiles/per-user/{}/{}", - profile_user, profile_name - ), - }; + let sudo: Option = match self.merged_settings.user { + Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)), + _ => None, + }; - let sudo: Option = match merged_settings.user { - Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)), - _ => None, - }; + let current_exe = + std::env::current_exe().expect("Expected to find current executable path"); + + if !current_exe.starts_with("/nix/store/") { + good_panic!("The deploy binary must be in the Nix store"); + } + + DeployDefs { + ssh_user, + profile_user, + profile_path, + current_exe, + sudo, + } + } +} - let current_exe = std::env::current_exe().expect("Expected to find current executable path"); +pub fn make_deploy_data<'a, 's>( + top_settings: &'s data::GenericSettings, + node: &'a data::Node, + node_name: &'a str, + profile: &'a data::Profile, + profile_name: &'a str, + cmd_overrides: &'a CmdOverrides, +) -> Result, Box> { + let mut merged_settings = top_settings.clone(); + merged_settings.merge(node.generic_settings.clone()); + merged_settings.merge(profile.generic_settings.clone()); - if !current_exe.starts_with("/nix/store/") { - good_panic!("The deploy binary must be in the Nix store"); + if cmd_overrides.ssh_user.is_some() { + merged_settings.ssh_user = cmd_overrides.ssh_user.clone(); + } + if cmd_overrides.profile_user.is_some() { + merged_settings.user = cmd_overrides.profile_user.clone(); + } + if let Some(ref ssh_opts) = cmd_overrides.ssh_opts { + merged_settings.ssh_opts = ssh_opts.split(' ').map(|x| x.to_owned()).collect(); + } + if let Some(fast_connection) = cmd_overrides.fast_connection { + merged_settings.fast_connection = fast_connection; + } + if let Some(auto_rollback) = cmd_overrides.auto_rollback { + merged_settings.auto_rollback = auto_rollback; } Ok(DeployData { - sudo, - ssh_user, - profile_user, - profile_path, - current_exe, + profile, + profile_name, + node, + node_name, + + cmd_overrides, + + merged_settings, }) } diff --git a/src/utils/push.rs b/src/utils/push.rs index 38a576f..9a6748e 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -2,30 +2,24 @@ // // SPDX-License-Identifier: MPL-2.0 -use super::data; - use std::process::Stdio; use tokio::process::Command; pub async fn push_profile( - profile: &data::Profile, - profile_name: &str, - node: &data::Node, - node_name: &str, supports_flakes: bool, check_sigs: bool, repo: &str, - merged_settings: &data::GenericSettings, deploy_data: &super::DeployData<'_>, + deploy_defs: &super::DeployDefs<'_>, ) -> Result<(), Box> { info!( "Pushing profile `{}` for node `{}`", - profile_name, node_name + deploy_data.profile_name, deploy_data.node_name ); debug!( "Building profile `{} for node `{}`", - profile_name, node_name + deploy_data.profile_name, deploy_data.node_name ); if supports_flakes { @@ -34,7 +28,7 @@ pub async fn push_profile( .arg("--no-link") .arg(format!( "{}#deploy.nodes.{}.profiles.{}.path", - repo, node_name, profile_name + repo, deploy_data.node_name, deploy_data.profile_name )) .stdout(Stdio::null()) .stderr(Stdio::null()) @@ -46,7 +40,7 @@ pub async fn push_profile( .arg("-A") .arg(format!( "deploy.nodes.{}.profiles.{}.path", - node_name, profile_name + deploy_data.node_name, deploy_data.profile_name )) .stdout(Stdio::null()) .stderr(Stdio::null()) @@ -57,7 +51,7 @@ pub async fn push_profile( if let Ok(local_key) = std::env::var("LOCAL_KEY") { info!( "Signing key present! Signing profile `{}` for node `{}`", - profile_name, node_name + deploy_data.profile_name, deploy_data.node_name ); Command::new("nix") @@ -65,9 +59,9 @@ pub async fn push_profile( .arg("-r") .arg("-k") .arg(local_key) - .arg(&profile.profile_settings.path) + .arg(&deploy_data.profile.profile_settings.path) .arg(&super::deploy_path_to_activate_path_str( - &deploy_data.current_exe, + &deploy_defs.current_exe, )?) .stdout(Stdio::null()) .stderr(Stdio::null()) @@ -75,12 +69,15 @@ pub async fn push_profile( .await?; } - debug!("Copying profile `{} for node `{}`", profile_name, node_name); + debug!( + "Copying profile `{} for node `{}`", + deploy_data.profile_name, deploy_data.node_name + ); let mut copy_command_ = Command::new("nix"); let mut copy_command = copy_command_.arg("copy"); - if merged_settings.fast_connection { + if deploy_data.merged_settings.fast_connection { copy_command = copy_command.arg("--substitute-on-destination"); } @@ -88,7 +85,8 @@ pub async fn push_profile( copy_command = copy_command.arg("--no-check-sigs"); } - let ssh_opts_str = merged_settings + let ssh_opts_str = deploy_data + .merged_settings .ssh_opts // This should provide some extra safety, but it also breaks for some reason, oh well // .iter() @@ -96,15 +94,17 @@ pub async fn push_profile( // .collect::>() .join(" "); + let hostname = match deploy_data.cmd_overrides.hostname { + Some(ref x) => x, + None => &deploy_data.node.node_settings.hostname, + }; + copy_command .arg("--to") - .arg(format!( - "ssh://{}@{}", - deploy_data.ssh_user, node.node_settings.hostname - )) - .arg(&profile.profile_settings.path) + .arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)) + .arg(&deploy_data.profile.profile_settings.path) .arg(&super::deploy_path_to_activate_path_str( - &deploy_data.current_exe, + &deploy_defs.current_exe, )?) .env("NIX_SSHOPTS", ssh_opts_str) .spawn()? -- cgit v1.2.3 From 05803e0ebaf417d9ba40645b6548a48bf51f9213 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Thu, 1 Oct 2020 20:24:09 -0700 Subject: Handle more command exits correctly --- src/activate.rs | 39 +++++++++++++++++++++++++++++---------- src/utils/deploy.rs | 6 +++++- src/utils/push.rs | 30 +++++++++++++++++++++--------- 3 files changed, 55 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index 3c7c16d..f75303f 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -87,22 +87,33 @@ pub async fn activate( match activate_status { Ok(s) if s.success() => (), _ if auto_rollback => { - Command::new("nix-env") + error!("Failed to execute activation command"); + + let nix_env_rollback_exit_status = Command::new("nix-env") .arg("-p") .arg(&profile_path) .arg("--rollback") .stdout(Stdio::null()) .stderr(Stdio::null()) - .spawn()? + .status() .await?; - let c = Command::new("nix-env") + if !nix_env_rollback_exit_status.success() { + good_panic!("`nix-env --rollback` failed"); + } + + let nix_env_list_generations_out = Command::new("nix-env") .arg("-p") .arg(&profile_path) .arg("--list-generations") .output() .await?; - let generations_list = String::from_utf8(c.stdout)?; + + if !nix_env_list_generations_out.status.success() { + good_panic!("Listing `nix-env` generations failed"); + } + + let generations_list = String::from_utf8(nix_env_list_generations_out.stdout)?; let last_generation_line = generations_list .lines() @@ -117,24 +128,32 @@ pub async fn activate( debug!("Removing generation entry {}", last_generation_line); warn!("Removing generation by ID {}", last_generation_id); - Command::new("nix-env") + let nix_env_delete_generation_exit_status = Command::new("nix-env") .arg("-p") .arg(&profile_path) .arg("--delete-generations") .arg(last_generation_id) .stdout(Stdio::null()) .stderr(Stdio::null()) - .spawn()? + .status() .await?; + if !nix_env_delete_generation_exit_status.success() { + good_panic!("Failed to delete failed generation"); + } + // TODO: Find some way to make sure this command never changes, otherwise this will not work - Command::new("bash") + let re_activate_exit_status = Command::new("bash") .arg("-c") .arg(&activate_cmd) - .spawn()? + .status() .await?; - good_panic!("Failed to execute activation command"); + if !re_activate_exit_status.success() { + good_panic!("Failed to re-activate the last generation"); + } + + std::process::exit(1); } _ => {} } @@ -163,4 +182,4 @@ async fn main() -> Result<(), Box> { .await?; Ok(()) -} \ No newline at end of file +} diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index d46f2db..f1f4210 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -98,7 +98,11 @@ pub async fn deploy_profile( ssh_command = ssh_command.arg(ssh_opt); } - ssh_command.arg(self_activate_command).spawn()?.await?; + let ssh_exit_status = ssh_command.arg(self_activate_command).status().await?; + + if !ssh_exit_status.success() { + good_panic!("Activation over SSH failed"); + } Ok(()) } diff --git a/src/utils/push.rs b/src/utils/push.rs index 9a6748e..a973572 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -22,7 +22,7 @@ pub async fn push_profile( deploy_data.profile_name, deploy_data.node_name ); - if supports_flakes { + let build_exit_status = if supports_flakes { Command::new("nix") .arg("build") .arg("--no-link") @@ -32,8 +32,8 @@ pub async fn push_profile( )) .stdout(Stdio::null()) .stderr(Stdio::null()) - .spawn()? - .await?; + .status() + .await? } else { Command::new("nix-build") .arg(&repo) @@ -44,8 +44,12 @@ pub async fn push_profile( )) .stdout(Stdio::null()) .stderr(Stdio::null()) - .spawn()? - .await?; + .status() + .await? + }; + + if !build_exit_status.success() { + good_panic!("`nix build` failed"); } if let Ok(local_key) = std::env::var("LOCAL_KEY") { @@ -54,7 +58,7 @@ pub async fn push_profile( deploy_data.profile_name, deploy_data.node_name ); - Command::new("nix") + let sign_exit_status = Command::new("nix") .arg("sign-paths") .arg("-r") .arg("-k") @@ -65,8 +69,12 @@ pub async fn push_profile( )?) .stdout(Stdio::null()) .stderr(Stdio::null()) - .spawn()? + .status() .await?; + + if !sign_exit_status.success() { + good_panic!("`nix sign-paths` failed"); + } } debug!( @@ -99,7 +107,7 @@ pub async fn push_profile( None => &deploy_data.node.node_settings.hostname, }; - copy_command + let copy_exit_status = copy_command .arg("--to") .arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)) .arg(&deploy_data.profile.profile_settings.path) @@ -107,8 +115,12 @@ pub async fn push_profile( &deploy_defs.current_exe, )?) .env("NIX_SSHOPTS", ssh_opts_str) - .spawn()? + .status() .await?; + if !copy_exit_status.success() { + good_panic!("`nix copy` failed"); + } + Ok(()) } -- cgit v1.2.3 From 5674670a59168fb05f26e5b4fb41dd2662810e94 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 2 Oct 2020 12:58:11 -0700 Subject: General improvements, deprecate `activate` profile option in favor of executing $PROFILE/activate (Wrap It Yourself) to ensure successful rollback activations --- src/activate.rs | 168 +++++++++++++++++++++++++--------------------------- src/utils/data.rs | 1 - src/utils/deploy.rs | 40 +++++-------- src/utils/push.rs | 5 -- 4 files changed, 97 insertions(+), 117 deletions(-) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index f75303f..64baa4f 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -26,10 +26,6 @@ struct Opts { profile_path: String, closure: String, - /// Command for activating the given profile - #[clap(long)] - activate_cmd: Option, - /// Command for bootstrapping #[clap(long)] bootstrap_cmd: Option, @@ -42,21 +38,24 @@ struct Opts { pub async fn activate( profile_path: String, closure: String, - activate_cmd: Option, bootstrap_cmd: Option, auto_rollback: bool, ) -> Result<(), Box> { info!("Activating profile"); - Command::new("nix-env") + let nix_env_set_exit_status = Command::new("nix-env") .arg("-p") .arg(&profile_path) .arg("--set") .arg(&closure) .stdout(Stdio::null()) - .spawn()? + .status() .await?; + if !nix_env_set_exit_status.success() { + good_panic!("Failed to update nix-env generation"); + } + if let (Some(bootstrap_cmd), false) = (bootstrap_cmd, !Path::new(&profile_path).exists()) { let bootstrap_status = Command::new("bash") .arg("-c") @@ -76,87 +75,85 @@ pub async fn activate( } } - if let Some(activate_cmd) = activate_cmd { - let activate_status = Command::new("bash") - .arg("-c") - .arg(&activate_cmd) - .env("PROFILE", &profile_path) - .status() - .await; + let activate_status = Command::new(format!("{}/activate", profile_path)) + .env("PROFILE", &profile_path) + .status() + .await; + + match activate_status { + Ok(s) if s.success() => (), + _ if auto_rollback => { + error!("Failed to execute activation command"); + + let nix_env_rollback_exit_status = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--rollback") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await?; + + if !nix_env_rollback_exit_status.success() { + good_panic!("`nix-env --rollback` failed"); + } - match activate_status { - Ok(s) if s.success() => (), - _ if auto_rollback => { - error!("Failed to execute activation command"); - - let nix_env_rollback_exit_status = Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--rollback") - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .await?; - - if !nix_env_rollback_exit_status.success() { - good_panic!("`nix-env --rollback` failed"); - } - - let nix_env_list_generations_out = Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--list-generations") - .output() - .await?; - - if !nix_env_list_generations_out.status.success() { - good_panic!("Listing `nix-env` generations failed"); - } - - let generations_list = String::from_utf8(nix_env_list_generations_out.stdout)?; - - let last_generation_line = generations_list - .lines() - .last() - .expect("Expected to find a generation in list"); - - let last_generation_id = last_generation_line - .split_whitespace() - .next() - .expect("Expected to get ID from generation entry"); - - debug!("Removing generation entry {}", last_generation_line); - warn!("Removing generation by ID {}", last_generation_id); - - let nix_env_delete_generation_exit_status = Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--delete-generations") - .arg(last_generation_id) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .await?; - - if !nix_env_delete_generation_exit_status.success() { - good_panic!("Failed to delete failed generation"); - } - - // TODO: Find some way to make sure this command never changes, otherwise this will not work - let re_activate_exit_status = Command::new("bash") - .arg("-c") - .arg(&activate_cmd) - .status() - .await?; - - if !re_activate_exit_status.success() { - good_panic!("Failed to re-activate the last generation"); - } - - std::process::exit(1); + debug!("Listing generations"); + + let nix_env_list_generations_out = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--list-generations") + .output() + .await?; + + if !nix_env_list_generations_out.status.success() { + good_panic!("Listing `nix-env` generations failed"); } - _ => {} + + let generations_list = String::from_utf8(nix_env_list_generations_out.stdout)?; + + let last_generation_line = generations_list + .lines() + .last() + .expect("Expected to find a generation in list"); + + let last_generation_id = last_generation_line + .split_whitespace() + .next() + .expect("Expected to get ID from generation entry"); + + debug!("Removing generation entry {}", last_generation_line); + warn!("Removing generation by ID {}", last_generation_id); + + let nix_env_delete_generation_exit_status = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--delete-generations") + .arg(last_generation_id) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await?; + + if !nix_env_delete_generation_exit_status.success() { + good_panic!("Failed to delete failed generation"); + } + + info!("Attempting re-activate last generation"); + + let re_activate_exit_status = Command::new(format!("{}/activate", profile_path)) + .env("PROFILE", &profile_path) + .status() + .await?; + + if !re_activate_exit_status.success() { + good_panic!("Failed to re-activate the last generation"); + } + + std::process::exit(1); } + _ => {} } Ok(()) @@ -175,7 +172,6 @@ async fn main() -> Result<(), Box> { activate( opts.profile_path, opts.closure, - opts.activate_cmd, opts.bootstrap_cmd, opts.auto_rollback, ) diff --git a/src/utils/data.rs b/src/utils/data.rs index d1dae5b..de6adfc 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -34,7 +34,6 @@ pub struct NodeSettings { #[derive(Deserialize, Debug, Clone)] pub struct ProfileSettings { pub path: String, - pub activate: Option, pub bootstrap: Option, } diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index f1f4210..7301967 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -9,10 +9,9 @@ fn build_activate_command( sudo: &Option, profile_path: &str, closure: &str, - activate_cmd: &Option, bootstrap_cmd: &Option, auto_rollback: bool, -) -> Result> { +) -> String { let mut self_activate_command = format!("{} '{}' '{}'", activate_path_str, profile_path, closure); @@ -27,18 +26,11 @@ fn build_activate_command( ); } - if let Some(ref activate_cmd) = activate_cmd { - self_activate_command = format!( - "{} --activate-cmd '{}'", - self_activate_command, activate_cmd - ); - } - if auto_rollback { self_activate_command = format!("{} --auto-rollback", self_activate_command); } - Ok(self_activate_command) + self_activate_command } #[test] @@ -47,22 +39,21 @@ fn test_activation_command_builder() { let sudo = Some("sudo -u test".to_string()); let profile_path = "/blah/profiles/test"; let closure = "/blah/etc"; - let activate_cmd = Some("$THING/bin/aaaaaaa".to_string()); let bootstrap_cmd = None; let auto_rollback = true; - match build_activate_command( - activate_path_str, - &sudo, - profile_path, - closure, - &activate_cmd, - &bootstrap_cmd, - auto_rollback, - ) { - Err(_) => panic!(""), - Ok(x) => assert_eq!(x, "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' --activate-cmd '$THING/bin/aaaaaaa' --auto-rollback".to_string()), - } + assert_eq!( + build_activate_command( + activate_path_str, + &sudo, + profile_path, + closure, + &bootstrap_cmd, + auto_rollback, + ), + "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' --auto-rollback" + .to_string(), + ); } pub async fn deploy_profile( @@ -81,10 +72,9 @@ pub async fn deploy_profile( &deploy_defs.sudo, &deploy_defs.profile_path, &deploy_data.profile.profile_settings.path, - &deploy_data.profile.profile_settings.activate, &deploy_data.profile.profile_settings.bootstrap, deploy_data.merged_settings.auto_rollback, - )?; + ); let hostname = match deploy_data.cmd_overrides.hostname { Some(ref x) => x, diff --git a/src/utils/push.rs b/src/utils/push.rs index a973572..3f48d68 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -17,11 +17,6 @@ pub async fn push_profile( deploy_data.profile_name, deploy_data.node_name ); - debug!( - "Building profile `{} for node `{}`", - deploy_data.profile_name, deploy_data.node_name - ); - let build_exit_status = if supports_flakes { Command::new("nix") .arg("build") -- cgit v1.2.3 From 7c00fd2761e6efffe763ece5d08d9a6d3fb95092 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 5 Oct 2020 19:46:28 -0700 Subject: Add interface with json schema, fix flake-less issues, put setActivate and jsonSchema check in flake lib --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 0d80e42..219c3e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use tokio::process::Command; use merge::Merge; extern crate pretty_env_logger; + #[macro_use] extern crate log; @@ -177,12 +178,14 @@ async fn get_deployment_data( let mut c = match supports_flakes { true => Command::new("nix"), - false => Command::new("nix-instanciate"), + false => Command::new("nix-instantiate"), }; let mut build_command = match supports_flakes { true => { - c.arg("eval").arg("--json").arg(format!("{}#deploy", repo)) + c.arg("eval") + .arg("--json") + .arg(format!("{}#deploy", repo)) } false => { c @@ -190,7 +193,7 @@ async fn get_deployment_data( .arg("--read-write-mode") .arg("--json") .arg("--eval") - .arg("--E") + .arg("-E") .arg(format!("let r = import {}/.; in if builtins.isFunction r then (r {{}}).deploy else r.deploy", repo)) } }; -- cgit v1.2.3 From aabcf6b77d4159100a49b143cbb8da4bad194f14 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 5 Oct 2020 20:10:41 -0700 Subject: Improve schema a bit, fix flake locks for examples --- src/main.rs | 28 +++++++++++++++++++--------- src/utils/data.rs | 15 +++++++-------- 2 files changed, 26 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 219c3e5..fb3fb66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,17 +65,22 @@ async fn push_all_profiles( ) -> Result<(), Box> { info!("Pushing all profiles for `{}`", node_name); - let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); + let mut profiles_list: Vec<&str> = node + .node_settings + .profiles_order + .iter() + .map(|x| x.as_ref()) + .collect(); // Add any profiles which weren't in the provided order list - for profile_name in node.profiles.keys() { + for profile_name in node.node_settings.profiles.keys() { if !profiles_list.contains(&profile_name.as_str()) { profiles_list.push(&profile_name); } } for profile_name in profiles_list { - let profile = match node.profiles.get(profile_name) { + let profile = match node.node_settings.profiles.get(profile_name) { Some(x) => x, None => good_panic!("No profile was found named `{}`", profile_name), }; @@ -117,17 +122,22 @@ async fn deploy_all_profiles( ) -> Result<(), Box> { info!("Deploying all profiles for `{}`", node_name); - let mut profiles_list: Vec<&str> = node.profiles_order.iter().map(|x| x.as_ref()).collect(); + let mut profiles_list: Vec<&str> = node + .node_settings + .profiles_order + .iter() + .map(|x| x.as_ref()) + .collect(); // Add any profiles which weren't in the provided order list - for profile_name in node.profiles.keys() { + for profile_name in node.node_settings.profiles.keys() { if !profiles_list.contains(&profile_name.as_str()) { profiles_list.push(&profile_name); } } for profile_name in profiles_list { - let profile = match node.profiles.get(profile_name) { + let profile = match node.node_settings.profiles.get(profile_name) { Some(x) => x, None => good_panic!("No profile was found named `{}`", profile_name), }; @@ -203,8 +213,8 @@ async fn get_deployment_data( } let build_output = build_command - .stdout(Stdio::null()) - .stderr(Stdio::null()) + // .stdout(Stdio::null()) + // .stderr(Stdio::null()) .output() .await?; @@ -233,7 +243,7 @@ async fn run_deploy( Some(x) => x, None => good_panic!("No node was found named `{}`", node_name), }; - let profile = match node.profiles.get(profile_name) { + let profile = match node.node_settings.profiles.get(profile_name) { Some(x) => x, None => good_panic!("No profile was found named `{}`", profile_name), }; diff --git a/src/utils/data.rs b/src/utils/data.rs index de6adfc..371c82d 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -29,6 +29,13 @@ pub struct GenericSettings { #[derive(Deserialize, Debug, Clone)] pub struct NodeSettings { pub hostname: String, + pub profiles: HashMap, + #[serde( + skip_serializing_if = "Vec::is_empty", + default, + rename(deserialize = "profilesOrder") + )] + pub profiles_order: Vec, } #[derive(Deserialize, Debug, Clone)] @@ -51,14 +58,6 @@ pub struct Node { pub generic_settings: GenericSettings, #[serde(flatten)] pub node_settings: NodeSettings, - - pub profiles: HashMap, - #[serde( - skip_serializing_if = "Vec::is_empty", - default, - rename(deserialize = "profilesOrder") - )] - pub profiles_order: Vec, } #[derive(Deserialize, Debug, Clone)] -- cgit v1.2.3 From 3a92593bf9c4ca07a2b09888e4a3f7dff6c9c510 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Tue, 6 Oct 2020 11:08:40 -0700 Subject: Add skip-push flag --- src/main.rs | 61 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index fb3fb66..04d7868 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,6 +51,9 @@ struct Opts { /// Override hostname used for the node #[clap(long)] hostname: Option, + /// Skip pushing step (useful for local testing) + #[clap(short, long)] + skip_push: bool, } #[inline] @@ -235,6 +238,7 @@ async fn run_deploy( data: utils::data::Data, supports_flakes: bool, check_sigs: bool, + skip_push: bool, cmd_overrides: utils::CmdOverrides, ) -> Result<(), Box> { match (deploy_flake.node, deploy_flake.profile) { @@ -259,14 +263,16 @@ async fn run_deploy( let deploy_defs = deploy_data.defs(); - utils::push::push_profile( - supports_flakes, - check_sigs, - deploy_flake.repo, - &deploy_data, - &deploy_defs, - ) - .await?; + if !skip_push { + utils::push::push_profile( + supports_flakes, + check_sigs, + deploy_flake.repo, + &deploy_data, + &deploy_defs, + ) + .await?; + } utils::deploy::deploy_profile(&deploy_data, &deploy_defs).await?; } @@ -276,23 +282,7 @@ async fn run_deploy( None => good_panic!("No node was found named `{}`", node_name), }; - push_all_profiles( - node, - node_name, - supports_flakes, - deploy_flake.repo, - &data.generic_settings, - check_sigs, - &cmd_overrides, - ) - .await?; - - deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides).await?; - } - (None, None) => { - info!("Deploying all profiles on all nodes"); - - for (node_name, node) in &data.nodes { + if !skip_push { push_all_profiles( node, node_name, @@ -305,6 +295,26 @@ async fn run_deploy( .await?; } + deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides).await?; + } + (None, None) => { + info!("Deploying all profiles on all nodes"); + + if !skip_push { + for (node_name, node) in &data.nodes { + push_all_profiles( + node, + node_name, + supports_flakes, + deploy_flake.repo, + &data.generic_settings, + check_sigs, + &cmd_overrides, + ) + .await?; + } + } + for (node_name, node) in &data.nodes { deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides) .await?; @@ -363,6 +373,7 @@ async fn main() -> Result<(), Box> { data, supports_flakes, opts.checksigs, + opts.skip_push, cmd_overrides, ) .await?; -- cgit v1.2.3 From 518f7f5b4f1db83cab61941ab8887b0df76ce8d8 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Thu, 8 Oct 2020 18:13:26 -0700 Subject: Update documentation --- src/main.rs | 14 -------------- src/utils/mod.rs | 25 ------------------------- 2 files changed, 39 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 04d7868..b28a520 100644 --- a/src/main.rs +++ b/src/main.rs @@ -349,20 +349,6 @@ async fn main() -> Result<(), Box> { hostname: opts.hostname, }; - match (cmd_overrides.purity(), deploy_flake.node, deploy_flake.profile) { - (utils::OverridePurity::ErrorProfile, _, None) => good_panic!( - "You have specified an override not suitible for deploying to multiple profiles, please specify your target profile explicitly" - ), - (utils::OverridePurity::Error, None, _) => good_panic!( - "You have specified an override not suitible for deploying to multiple nodes, please specify your target node explicitly" - ), - - (utils::OverridePurity::Warn, None, _) => warn!( - "Certain overrides you have provided might be dangerous when used on multiple nodes or profiles, be cautious" - ), - _ => (), - }; - let supports_flakes = test_flake_support().await?; let data = diff --git a/src/utils/mod.rs b/src/utils/mod.rs index bfdbc5e..97e4550 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -28,31 +28,6 @@ pub struct CmdOverrides { pub hostname: Option, } -pub enum OverridePurity { - ErrorProfile, - Error, - Warn, - Pure, -} - -impl CmdOverrides { - pub fn purity(&self) -> OverridePurity { - if self.profile_user.is_some() { - return OverridePurity::ErrorProfile; - } - - if self.hostname.is_some() || self.ssh_user.is_some() { - return OverridePurity::Error; - } - - if self.ssh_opts.is_some() || self.fast_connection.is_some() { - return OverridePurity::Warn; - } - - OverridePurity::Pure - } -} - #[derive(PartialEq, Debug)] pub struct DeployFlake<'a> { pub repo: &'a str, -- cgit v1.2.3 From db8301a45796cd919cbfa085f85ac6288e73a8db Mon Sep 17 00:00:00 2001 From: notgne2 Date: Sat, 10 Oct 2020 10:31:55 -0700 Subject: Add profile path option to profiles --- src/utils/data.rs | 2 ++ src/utils/mod.rs | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/utils/data.rs b/src/utils/data.rs index 371c82d..f72f9a7 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -42,6 +42,8 @@ pub struct NodeSettings { pub struct ProfileSettings { pub path: String, pub bootstrap: Option, + #[serde(rename(deserialize = "profilePath"))] + pub profile_path: Option, } #[derive(Deserialize, Debug, Clone)] diff --git a/src/utils/mod.rs b/src/utils/mod.rs index bfdbc5e..51f977f 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -129,7 +129,7 @@ pub struct DeployData<'a> { pub struct DeployDefs<'a> { pub ssh_user: Cow<'a, str>, pub profile_user: Cow<'a, str>, - pub profile_path: String, + pub profile_path: Cow<'a, str>, pub current_exe: PathBuf, pub sudo: Option, } @@ -153,12 +153,16 @@ impl<'a> DeployData<'a> { }, }; - let profile_path = match &profile_user[..] { - "root" => format!("/nix/var/nix/profiles/{}", self.profile_name), - _ => format!( - "/nix/var/nix/profiles/per-user/{}/{}", - profile_user, self.profile_name - ), + let profile_path: Cow = match self.profile.profile_settings.profile_path { + None => match &profile_user[..] { + "root" => format!("/nix/var/nix/profiles/{}", self.profile_name).into(), + _ => format!( + "/nix/var/nix/profiles/per-user/{}/{}", + profile_user, self.profile_name + ) + .into(), + }, + Some(ref x) => x.into(), }; let sudo: Option = match self.merged_settings.user { -- cgit v1.2.3 From e463c62922ad09f016e4f1dd1d6d0cabccb0ff79 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Sun, 11 Oct 2020 15:08:02 -0700 Subject: Move activate script location, use buildEnv for setActivate --- src/activate.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index 64baa4f..38fc832 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -75,7 +75,7 @@ pub async fn activate( } } - let activate_status = Command::new(format!("{}/activate", profile_path)) + let activate_status = Command::new(format!("{}/deploy-rs-activate", profile_path)) .env("PROFILE", &profile_path) .status() .await; @@ -142,7 +142,7 @@ pub async fn activate( info!("Attempting re-activate last generation"); - let re_activate_exit_status = Command::new(format!("{}/activate", profile_path)) + let re_activate_exit_status = Command::new(format!("{}/deploy-rs-activate", profile_path)) .env("PROFILE", &profile_path) .status() .await?; -- cgit v1.2.3 From e5bd558c5b6505621d3b5a27e9b39bf54f6788a1 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Sun, 11 Oct 2020 15:19:09 -0700 Subject: Set working directory during activation to the profile path --- src/activate.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index 38fc832..0b7d28d 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -77,6 +77,7 @@ pub async fn activate( let activate_status = Command::new(format!("{}/deploy-rs-activate", profile_path)) .env("PROFILE", &profile_path) + .current_dir(&profile_path) .status() .await; @@ -144,6 +145,7 @@ pub async fn activate( let re_activate_exit_status = Command::new(format!("{}/deploy-rs-activate", profile_path)) .env("PROFILE", &profile_path) + .current_dir(&profile_path) .status() .await?; -- cgit v1.2.3 From 3bd43f92e6c59f65b6120886c4ee75b6a9391522 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Tue, 13 Oct 2020 18:27:27 -0700 Subject: Auto rollback if deployment is not confirmed --- src/activate.rs | 245 +++++++++++++++++++++++++++++++++++++--------------- src/utils/data.rs | 4 + src/utils/deploy.rs | 52 ++++++++++- 3 files changed, 226 insertions(+), 75 deletions(-) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index 0b7d28d..ce6b286 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -4,11 +4,20 @@ use clap::Clap; +use futures_util::FutureExt; use std::process::Stdio; +use tokio::fs; use tokio::process::Command; +use tokio::time::timeout; + +use std::time::Duration; + +use futures_util::StreamExt; use std::path::Path; +use inotify::Inotify; + extern crate pretty_env_logger; #[macro_use] extern crate log; @@ -25,6 +34,8 @@ mod utils; struct Opts { profile_path: String, closure: String, + temp_path: String, + max_time: u16, /// Command for bootstrapping #[clap(long)] @@ -35,11 +46,162 @@ struct Opts { auto_rollback: bool, } +pub async fn deactivate(profile_path: &str) -> Result<(), Box> { + error!("De-activating due to error"); + + let nix_env_rollback_exit_status = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--rollback") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await?; + + if !nix_env_rollback_exit_status.success() { + good_panic!("`nix-env --rollback` failed"); + } + + debug!("Listing generations"); + + let nix_env_list_generations_out = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--list-generations") + .output() + .await?; + + if !nix_env_list_generations_out.status.success() { + good_panic!("Listing `nix-env` generations failed"); + } + + let generations_list = String::from_utf8(nix_env_list_generations_out.stdout)?; + + let last_generation_line = generations_list + .lines() + .last() + .expect("Expected to find a generation in list"); + + let last_generation_id = last_generation_line + .split_whitespace() + .next() + .expect("Expected to get ID from generation entry"); + + debug!("Removing generation entry {}", last_generation_line); + warn!("Removing generation by ID {}", last_generation_id); + + let nix_env_delete_generation_exit_status = Command::new("nix-env") + .arg("-p") + .arg(&profile_path) + .arg("--delete-generations") + .arg(last_generation_id) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .await?; + + if !nix_env_delete_generation_exit_status.success() { + good_panic!("Failed to delete failed generation"); + } + + info!("Attempting re-activate last generation"); + + let re_activate_exit_status = Command::new(format!("{}/deploy-rs-activate", profile_path)) + .env("PROFILE", &profile_path) + .current_dir(&profile_path) + .status() + .await?; + + if !re_activate_exit_status.success() { + good_panic!("Failed to re-activate the last generation"); + } + + Ok(()) +} + +async fn deactivate_on_err(profile_path: &str, r: Result) -> A { + match r { + Ok(x) => x, + Err(err) => { + error!("Deactivating due to error: {:?}", err); + match deactivate(profile_path).await { + Ok(_) => (), + Err(err) => { + error!("Error de-activating, uh-oh: {:?}", err); + } + }; + + std::process::exit(1); + } + } +} + +pub async fn activation_confirmation( + profile_path: String, + temp_path: String, + max_time: u16, + closure: String, +) -> Result<(), Box> { + let lock_hash = &closure[11 /* /nix/store/ */ ..]; + let lock_path = format!("{}/activating-{}", temp_path, lock_hash); + + if let Some(parent) = Path::new(&lock_path).parent() { + fs::create_dir_all(parent).await?; + } + + fs::File::create(&lock_path).await?; + + let mut inotify = Inotify::init()?; + inotify.add_watch(lock_path, inotify::WatchMask::DELETE)?; + + match fork::daemon(false, false).map_err(|x| x.to_string())? { + fork::Fork::Child => { + std::thread::spawn(move || { + let mut rt = tokio::runtime::Runtime::new().unwrap(); + + rt.block_on(async move { + info!("Waiting for confirmation event..."); + + let mut buffer = [0; 32]; + let mut stream = + deactivate_on_err(&profile_path, inotify.event_stream(&mut buffer)).await; + + deactivate_on_err( + &profile_path, + deactivate_on_err( + &profile_path, + deactivate_on_err( + &profile_path, + timeout(Duration::from_secs(max_time as u64), stream.next()).await, + ) + .await + .ok_or("Watcher ended prematurely"), + ) + .await, + ) + .await; + }); + }) + .join() + .unwrap(); + + info!("Confirmation successful!"); + + std::process::exit(0); + } + fork::Fork::Parent(_) => { + std::process::exit(0); + } + } +} + pub async fn activate( profile_path: String, closure: String, bootstrap_cmd: Option, auto_rollback: bool, + temp_path: String, + max_time: u16, ) -> Result<(), Box> { info!("Activating profile"); @@ -83,80 +245,17 @@ pub async fn activate( match activate_status { Ok(s) if s.success() => (), - _ if auto_rollback => { - error!("Failed to execute activation command"); - - let nix_env_rollback_exit_status = Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--rollback") - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .await?; - - if !nix_env_rollback_exit_status.success() { - good_panic!("`nix-env --rollback` failed"); - } - - debug!("Listing generations"); - - let nix_env_list_generations_out = Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--list-generations") - .output() - .await?; - - if !nix_env_list_generations_out.status.success() { - good_panic!("Listing `nix-env` generations failed"); - } - - let generations_list = String::from_utf8(nix_env_list_generations_out.stdout)?; - - let last_generation_line = generations_list - .lines() - .last() - .expect("Expected to find a generation in list"); - - let last_generation_id = last_generation_line - .split_whitespace() - .next() - .expect("Expected to get ID from generation entry"); - - debug!("Removing generation entry {}", last_generation_line); - warn!("Removing generation by ID {}", last_generation_id); - - let nix_env_delete_generation_exit_status = Command::new("nix-env") - .arg("-p") - .arg(&profile_path) - .arg("--delete-generations") - .arg(last_generation_id) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .await?; - - if !nix_env_delete_generation_exit_status.success() { - good_panic!("Failed to delete failed generation"); - } - - info!("Attempting re-activate last generation"); - - let re_activate_exit_status = Command::new(format!("{}/deploy-rs-activate", profile_path)) - .env("PROFILE", &profile_path) - .current_dir(&profile_path) - .status() - .await?; + _ if auto_rollback => return Ok(deactivate(&profile_path).await?), + _ => (), + } - if !re_activate_exit_status.success() { - good_panic!("Failed to re-activate the last generation"); - } + info!("Activation succeeded, now performing post-activation checks"); - std::process::exit(1); - } - _ => {} - } + deactivate_on_err( + &profile_path, + activation_confirmation(profile_path.clone(), temp_path, max_time, closure).await, + ) + .await; Ok(()) } @@ -176,6 +275,8 @@ async fn main() -> Result<(), Box> { opts.closure, opts.bootstrap_cmd, opts.auto_rollback, + opts.temp_path, + opts.max_time, ) .await?; diff --git a/src/utils/data.rs b/src/utils/data.rs index f72f9a7..351b9ae 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -44,6 +44,10 @@ pub struct ProfileSettings { pub bootstrap: Option, #[serde(rename(deserialize = "profilePath"))] pub profile_path: Option, + #[serde(rename(deserialize = "maxTime"))] + pub max_time: Option, + #[serde(rename(deserialize = "tempPath"))] + pub temp_path: Option, } #[derive(Deserialize, Debug, Clone)] diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 7301967..e3493ba 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: MPL-2.0 +use std::borrow::Cow; use tokio::process::Command; fn build_activate_command( @@ -11,9 +12,13 @@ fn build_activate_command( closure: &str, bootstrap_cmd: &Option, auto_rollback: bool, + temp_path: &Cow, + max_time: u16, ) -> String { - let mut self_activate_command = - format!("{} '{}' '{}'", activate_path_str, profile_path, closure); + let mut self_activate_command = format!( + "{} '{}' '{}' {} {}", + activate_path_str, profile_path, closure, temp_path, max_time + ); if let Some(sudo_cmd) = &sudo { self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); @@ -41,6 +46,8 @@ fn test_activation_command_builder() { let closure = "/blah/etc"; let bootstrap_cmd = None; let auto_rollback = true; + let temp_path = &"/tmp/deploy-rs".into(); + let max_time = 30; assert_eq!( build_activate_command( @@ -50,8 +57,10 @@ fn test_activation_command_builder() { closure, &bootstrap_cmd, auto_rollback, + temp_path, + max_time ), - "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' --auto-rollback" + "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' /tmp/deploy-rs 30 --auto-rollback" .to_string(), ); } @@ -67,6 +76,13 @@ pub async fn deploy_profile( let activate_path_str = super::deploy_path_to_activate_path_str(&deploy_defs.current_exe)?; + let temp_path: Cow = match &deploy_data.profile.profile_settings.temp_path { + Some(x) => x.into(), + None => "/tmp/deploy-rs".into(), + }; + + let max_time = deploy_data.profile.profile_settings.max_time.unwrap_or(30); + let self_activate_command = build_activate_command( activate_path_str, &deploy_defs.sudo, @@ -74,6 +90,8 @@ pub async fn deploy_profile( &deploy_data.profile.profile_settings.path, &deploy_data.profile.profile_settings.bootstrap, deploy_data.merged_settings.auto_rollback, + &temp_path, + max_time, ); let hostname = match deploy_data.cmd_overrides.hostname { @@ -94,5 +112,33 @@ pub async fn deploy_profile( good_panic!("Activation over SSH failed"); } + info!("Success, attempting to connect to the node to confirm deployment"); + + let mut c = Command::new("ssh"); + let mut ssh_confirm_command = c.arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)); + + for ssh_opt in &deploy_data.merged_settings.ssh_opts { + ssh_confirm_command = ssh_confirm_command.arg(ssh_opt); + } + + let lock_hash = &deploy_data.profile.profile_settings.path[11 /* /nix/store/ */ ..]; + let lock_path = format!("{}/activating-{}", temp_path, lock_hash); + + let mut confirm_command = format!("rm {}", lock_path); + if let Some(sudo_cmd) = &deploy_defs.sudo { + confirm_command = format!("{} {}", sudo_cmd, confirm_command); + } + + let ssh_exit_status = ssh_confirm_command.arg(confirm_command).status().await?; + + if !ssh_exit_status.success() { + good_panic!( + "Failed to confirm deployment, the node will roll back in <{} seconds", + max_time + ); + } + + info!("Deployment confirmed."); + Ok(()) } -- cgit v1.2.3 From ea717911bac5ff29d730d80d4b774fe17ed1e851 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Tue, 13 Oct 2020 19:06:40 -0700 Subject: Clean up some CLI arguments, make magic rollback optional --- src/activate.rs | 39 ++++++++++++++++++------- src/main.rs | 73 +++++++++++++++++++++++----------------------- src/utils/data.rs | 16 +++++----- src/utils/deploy.rs | 84 +++++++++++++++++++++++++++++++---------------------- src/utils/mod.rs | 10 +++++-- src/utils/push.rs | 2 +- 6 files changed, 132 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index ce6b286..55ceb27 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -34,8 +34,18 @@ mod utils; struct Opts { profile_path: String, closure: String, + + /// Temp path for any temporary files that may be needed during activation + #[clap(long)] temp_path: String, - max_time: u16, + + /// Maximum time to wait for confirmation after activation + #[clap(long)] + confirm_timeout: u16, + + /// Wait for confirmation after deployment and rollback if not confirmed + #[clap(long)] + magic_rollback: bool, /// Command for bootstrapping #[clap(long)] @@ -139,7 +149,7 @@ async fn deactivate_on_err(profile_path: &str, r: Result pub async fn activation_confirmation( profile_path: String, temp_path: String, - max_time: u16, + confirm_timeout: u16, closure: String, ) -> Result<(), Box> { let lock_hash = &closure[11 /* /nix/store/ */ ..]; @@ -172,7 +182,8 @@ pub async fn activation_confirmation( &profile_path, deactivate_on_err( &profile_path, - timeout(Duration::from_secs(max_time as u64), stream.next()).await, + timeout(Duration::from_secs(confirm_timeout as u64), stream.next()) + .await, ) .await .ok_or("Watcher ended prematurely"), @@ -201,7 +212,8 @@ pub async fn activate( bootstrap_cmd: Option, auto_rollback: bool, temp_path: String, - max_time: u16, + confirm_timeout: u16, + magic_rollback: bool, ) -> Result<(), Box> { info!("Activating profile"); @@ -249,13 +261,17 @@ pub async fn activate( _ => (), } - info!("Activation succeeded, now performing post-activation checks"); + info!("Activation succeeded!"); - deactivate_on_err( - &profile_path, - activation_confirmation(profile_path.clone(), temp_path, max_time, closure).await, - ) - .await; + if magic_rollback { + info!("Performing activation confirmation steps"); + deactivate_on_err( + &profile_path, + activation_confirmation(profile_path.clone(), temp_path, confirm_timeout, closure) + .await, + ) + .await; + } Ok(()) } @@ -276,7 +292,8 @@ async fn main() -> Result<(), Box> { opts.bootstrap_cmd, opts.auto_rollback, opts.temp_path, - opts.max_time, + opts.confirm_timeout, + opts.magic_rollback, ) .await?; diff --git a/src/main.rs b/src/main.rs index b28a520..cedf684 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,9 +51,15 @@ struct Opts { /// Override hostname used for the node #[clap(long)] hostname: Option, - /// Skip pushing step (useful for local testing) - #[clap(short, long)] - skip_push: bool, + /// Make activation wait for confirmation, or roll back after a period of time + #[clap(long)] + magic_rollback: Option, + /// How long activation should wait for confirmation (if using magic-rollback) + #[clap(long)] + confirm_timeout: Option, + /// Where to store temporary files (only used by magic-rollback) + #[clap(long)] + temp_path: Option, } #[inline] @@ -238,7 +244,6 @@ async fn run_deploy( data: utils::data::Data, supports_flakes: bool, check_sigs: bool, - skip_push: bool, cmd_overrides: utils::CmdOverrides, ) -> Result<(), Box> { match (deploy_flake.node, deploy_flake.profile) { @@ -263,16 +268,14 @@ async fn run_deploy( let deploy_defs = deploy_data.defs(); - if !skip_push { - utils::push::push_profile( - supports_flakes, - check_sigs, - deploy_flake.repo, - &deploy_data, - &deploy_defs, - ) - .await?; - } + utils::push::push_profile( + supports_flakes, + check_sigs, + deploy_flake.repo, + &deploy_data, + &deploy_defs, + ) + .await?; utils::deploy::deploy_profile(&deploy_data, &deploy_defs).await?; } @@ -282,7 +285,23 @@ async fn run_deploy( None => good_panic!("No node was found named `{}`", node_name), }; - if !skip_push { + push_all_profiles( + node, + node_name, + supports_flakes, + deploy_flake.repo, + &data.generic_settings, + check_sigs, + &cmd_overrides, + ) + .await?; + + deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides).await?; + } + (None, None) => { + info!("Deploying all profiles on all nodes"); + + for (node_name, node) in &data.nodes { push_all_profiles( node, node_name, @@ -295,26 +314,6 @@ async fn run_deploy( .await?; } - deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides).await?; - } - (None, None) => { - info!("Deploying all profiles on all nodes"); - - if !skip_push { - for (node_name, node) in &data.nodes { - push_all_profiles( - node, - node_name, - supports_flakes, - deploy_flake.repo, - &data.generic_settings, - check_sigs, - &cmd_overrides, - ) - .await?; - } - } - for (node_name, node) in &data.nodes { deploy_all_profiles(node, node_name, &data.generic_settings, &cmd_overrides) .await?; @@ -347,6 +346,9 @@ async fn main() -> Result<(), Box> { fast_connection: opts.fast_connection, auto_rollback: opts.auto_rollback, hostname: opts.hostname, + magic_rollback: opts.magic_rollback, + temp_path: opts.temp_path, + confirm_timeout: opts.confirm_timeout, }; let supports_flakes = test_flake_support().await?; @@ -359,7 +361,6 @@ async fn main() -> Result<(), Box> { data, supports_flakes, opts.checksigs, - opts.skip_push, cmd_overrides, ) .await?; diff --git a/src/utils/data.rs b/src/utils/data.rs index 351b9ae..5c58e3b 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -19,11 +19,15 @@ pub struct GenericSettings { #[merge(strategy = merge::vec::append)] pub ssh_opts: Vec, #[serde(rename(deserialize = "fastConnection"), default)] - #[merge(strategy = merge::bool::overwrite_false)] - pub fast_connection: bool, + pub fast_connection: Option, #[serde(rename(deserialize = "autoRollback"), default)] - #[merge(strategy = merge::bool::overwrite_false)] - pub auto_rollback: bool, + pub auto_rollback: Option, + #[serde(rename(deserialize = "confirmTimeout"))] + pub confirm_timeout: Option, + #[serde(rename(deserialize = "tempPath"))] + pub temp_path: Option, + #[serde(rename(deserialize = "magicRollback"))] + pub magic_rollback: Option, } #[derive(Deserialize, Debug, Clone)] @@ -44,10 +48,6 @@ pub struct ProfileSettings { pub bootstrap: Option, #[serde(rename(deserialize = "profilePath"))] pub profile_path: Option, - #[serde(rename(deserialize = "maxTime"))] - pub max_time: Option, - #[serde(rename(deserialize = "tempPath"))] - pub temp_path: Option, } #[derive(Deserialize, Debug, Clone)] diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index e3493ba..9b2f685 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -13,15 +13,20 @@ fn build_activate_command( bootstrap_cmd: &Option, auto_rollback: bool, temp_path: &Cow, - max_time: u16, + confirm_timeout: u16, + magic_rollback: bool, ) -> String { let mut self_activate_command = format!( - "{} '{}' '{}' {} {}", - activate_path_str, profile_path, closure, temp_path, max_time + "{} '{}' '{}' --temp-path {} --confirm-timeout {}", + activate_path_str, profile_path, closure, temp_path, confirm_timeout ); - if let Some(sudo_cmd) = &sudo { - self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); + if magic_rollback { + self_activate_command = format!("{} --magic-rollback", self_activate_command); + } + + if auto_rollback { + self_activate_command = format!("{} --auto-rollback", self_activate_command); } if let Some(ref bootstrap_cmd) = bootstrap_cmd { @@ -31,8 +36,8 @@ fn build_activate_command( ); } - if auto_rollback { - self_activate_command = format!("{} --auto-rollback", self_activate_command); + if let Some(sudo_cmd) = &sudo { + self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); } self_activate_command @@ -47,7 +52,8 @@ fn test_activation_command_builder() { let bootstrap_cmd = None; let auto_rollback = true; let temp_path = &"/tmp/deploy-rs".into(); - let max_time = 30; + let confirm_timeout = 30; + let magic_rollback = true; assert_eq!( build_activate_command( @@ -58,9 +64,10 @@ fn test_activation_command_builder() { &bootstrap_cmd, auto_rollback, temp_path, - max_time + confirm_timeout, + magic_rollback ), - "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' /tmp/deploy-rs 30 --auto-rollback" + "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' --temp-path /tmp/deploy-rs --confirm-timeout 30 --magic-rollback --auto-rollback" .to_string(), ); } @@ -76,12 +83,16 @@ pub async fn deploy_profile( let activate_path_str = super::deploy_path_to_activate_path_str(&deploy_defs.current_exe)?; - let temp_path: Cow = match &deploy_data.profile.profile_settings.temp_path { + let temp_path: Cow = match &deploy_data.merged_settings.temp_path { Some(x) => x.into(), None => "/tmp/deploy-rs".into(), }; - let max_time = deploy_data.profile.profile_settings.max_time.unwrap_or(30); + let confirm_timeout = deploy_data.merged_settings.confirm_timeout.unwrap_or(30); + + let magic_rollback = deploy_data.merged_settings.magic_rollback.unwrap_or(false); + + let auto_rollback = deploy_data.merged_settings.auto_rollback.unwrap_or(true); let self_activate_command = build_activate_command( activate_path_str, @@ -89,9 +100,10 @@ pub async fn deploy_profile( &deploy_defs.profile_path, &deploy_data.profile.profile_settings.path, &deploy_data.profile.profile_settings.bootstrap, - deploy_data.merged_settings.auto_rollback, + auto_rollback, &temp_path, - max_time, + confirm_timeout, + magic_rollback, ); let hostname = match deploy_data.cmd_overrides.hostname { @@ -112,33 +124,37 @@ pub async fn deploy_profile( good_panic!("Activation over SSH failed"); } - info!("Success, attempting to connect to the node to confirm deployment"); + info!("Success activating!"); - let mut c = Command::new("ssh"); - let mut ssh_confirm_command = c.arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)); + if magic_rollback { + info!("Attempting to confirm activation"); - for ssh_opt in &deploy_data.merged_settings.ssh_opts { - ssh_confirm_command = ssh_confirm_command.arg(ssh_opt); - } + let mut c = Command::new("ssh"); + let mut ssh_confirm_command = c.arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)); - let lock_hash = &deploy_data.profile.profile_settings.path[11 /* /nix/store/ */ ..]; - let lock_path = format!("{}/activating-{}", temp_path, lock_hash); + for ssh_opt in &deploy_data.merged_settings.ssh_opts { + ssh_confirm_command = ssh_confirm_command.arg(ssh_opt); + } - let mut confirm_command = format!("rm {}", lock_path); - if let Some(sudo_cmd) = &deploy_defs.sudo { - confirm_command = format!("{} {}", sudo_cmd, confirm_command); - } + let lock_hash = &deploy_data.profile.profile_settings.path[11 /* /nix/store/ */ ..]; + let lock_path = format!("{}/activating-{}", temp_path, lock_hash); - let ssh_exit_status = ssh_confirm_command.arg(confirm_command).status().await?; + let mut confirm_command = format!("rm {}", lock_path); + if let Some(sudo_cmd) = &deploy_defs.sudo { + confirm_command = format!("{} {}", sudo_cmd, confirm_command); + } - if !ssh_exit_status.success() { - good_panic!( - "Failed to confirm deployment, the node will roll back in <{} seconds", - max_time - ); - } + let ssh_exit_status = ssh_confirm_command.arg(confirm_command).status().await?; - info!("Deployment confirmed."); + if !ssh_exit_status.success() { + good_panic!( + "Failed to confirm deployment, the node will roll back in <{} seconds", + confirm_timeout + ); + } + + info!("Deployment confirmed."); + } Ok(()) } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 672a9ba..a0e62e1 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -26,6 +26,9 @@ pub struct CmdOverrides { pub fast_connection: Option, pub auto_rollback: Option, pub hostname: Option, + pub magic_rollback: Option, + pub temp_path: Option, + pub confirm_timeout: Option, } #[derive(PartialEq, Debug)] @@ -184,10 +187,13 @@ pub fn make_deploy_data<'a, 's>( merged_settings.ssh_opts = ssh_opts.split(' ').map(|x| x.to_owned()).collect(); } if let Some(fast_connection) = cmd_overrides.fast_connection { - merged_settings.fast_connection = fast_connection; + merged_settings.fast_connection = Some(fast_connection); } if let Some(auto_rollback) = cmd_overrides.auto_rollback { - merged_settings.auto_rollback = auto_rollback; + merged_settings.auto_rollback = Some(auto_rollback); + } + if let Some(magic_rollback) = cmd_overrides.magic_rollback { + merged_settings.magic_rollback = Some(magic_rollback); } Ok(DeployData { diff --git a/src/utils/push.rs b/src/utils/push.rs index 3f48d68..f80f9f8 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -80,7 +80,7 @@ pub async fn push_profile( let mut copy_command_ = Command::new("nix"); let mut copy_command = copy_command_.arg("copy"); - if deploy_data.merged_settings.fast_connection { + if let Some(true) = deploy_data.merged_settings.fast_connection { copy_command = copy_command.arg("--substitute-on-destination"); } -- cgit v1.2.3 From 48d1e48429d72b50c72282e47a68be130ea506ad Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 23 Oct 2020 15:06:14 -0700 Subject: Remove redundant default --- src/utils/data.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/utils/data.rs b/src/utils/data.rs index 5c58e3b..6cf2c5a 100644 --- a/src/utils/data.rs +++ b/src/utils/data.rs @@ -18,9 +18,9 @@ pub struct GenericSettings { )] #[merge(strategy = merge::vec::append)] pub ssh_opts: Vec, - #[serde(rename(deserialize = "fastConnection"), default)] + #[serde(rename(deserialize = "fastConnection"))] pub fast_connection: Option, - #[serde(rename(deserialize = "autoRollback"), default)] + #[serde(rename(deserialize = "autoRollback"))] pub auto_rollback: Option, #[serde(rename(deserialize = "confirmTimeout"))] pub confirm_timeout: Option, -- cgit v1.2.3 From 47e94f8dfd2ec9c6aacc6ba8d74f629e70433567 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 23 Oct 2020 21:54:13 -0700 Subject: Warn when flakes are not available, forward stderr of Nix evaluation --- src/main.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index cedf684..5d76e3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -221,11 +221,11 @@ async fn get_deployment_data( build_command = build_command.arg(extra_arg); } - let build_output = build_command - // .stdout(Stdio::null()) - // .stderr(Stdio::null()) - .output() - .await?; + let build_child = build_command + .stdout(Stdio::piped()) + .spawn()?; + + let build_output = build_child.wait_with_output().await?; if !build_output.status.success() { good_panic!( @@ -353,6 +353,10 @@ async fn main() -> Result<(), Box> { let supports_flakes = test_flake_support().await?; + if !supports_flakes { + warn!("A Nix version without flakes support was detected, support for this is work in progress"); + } + let data = get_deployment_data(supports_flakes, deploy_flake.repo, &opts.extra_build_args).await?; -- cgit v1.2.3 From c55471f1a52fc7cb4c467a3c9718640cdb950a22 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 23 Oct 2020 22:03:15 -0700 Subject: Fix log messages, prevent non-flake builds writing to result, unmute stderr on nix builds --- src/utils/push.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/utils/push.rs b/src/utils/push.rs index f80f9f8..a82c9d4 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -13,7 +13,7 @@ pub async fn push_profile( deploy_defs: &super::DeployDefs<'_>, ) -> Result<(), Box> { info!( - "Pushing profile `{}` for node `{}`", + "Building profile `{}` for node `{}`", deploy_data.profile_name, deploy_data.node_name ); @@ -26,19 +26,18 @@ pub async fn push_profile( repo, deploy_data.node_name, deploy_data.profile_name )) .stdout(Stdio::null()) - .stderr(Stdio::null()) .status() .await? } else { Command::new("nix-build") .arg(&repo) + .arg("--no-out-link") .arg("-A") .arg(format!( "deploy.nodes.{}.profiles.{}.path", deploy_data.node_name, deploy_data.profile_name )) .stdout(Stdio::null()) - .stderr(Stdio::null()) .status() .await? }; @@ -73,7 +72,7 @@ pub async fn push_profile( } debug!( - "Copying profile `{} for node `{}`", + "Copying profile `{}` to node `{}`", deploy_data.profile_name, deploy_data.node_name ); -- cgit v1.2.3 From 4084e0516d3903acd80ca465f9906e5d3bce2659 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 23 Oct 2020 22:14:14 -0700 Subject: Enable color for activation command --- src/utils/deploy.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 9b2f685..1ef7f11 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -112,7 +112,9 @@ pub async fn deploy_profile( }; let mut c = Command::new("ssh"); - let mut ssh_command = c.arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)); + let mut ssh_command = c + .arg("-t") + .arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)); for ssh_opt in &deploy_data.merged_settings.ssh_opts { ssh_command = ssh_command.arg(ssh_opt); -- cgit v1.2.3 From 72b066b293befec048f6a1b2f8d7a4b103ae4edf Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 23 Oct 2020 22:44:52 -0700 Subject: Add an option to keep build results --- src/main.rs | 27 ++++++++++++++++++++++++--- src/utils/push.rs | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 5d76e3d..1358a83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,13 @@ struct Opts { /// Extra arguments to be passed to nix build extra_build_args: Vec, + /// Keep the build outputs of each built profile + #[clap(short, long)] + keep_result: bool, + /// Location to keep outputs from built profiles in + #[clap(short, long)] + result_path: Option, + /// Override the SSH user with the given value #[clap(long)] ssh_user: Option, @@ -71,6 +78,8 @@ async fn push_all_profiles( top_settings: &utils::data::GenericSettings, check_sigs: bool, cmd_overrides: &utils::CmdOverrides, + keep_result: bool, + result_path: Option<&str>, ) -> Result<(), Box> { info!("Pushing all profiles for `{}`", node_name); @@ -115,6 +124,8 @@ async fn push_all_profiles( repo, &deploy_data, &deploy_defs, + keep_result, + result_path, ) .await?; } @@ -221,9 +232,7 @@ async fn get_deployment_data( build_command = build_command.arg(extra_arg); } - let build_child = build_command - .stdout(Stdio::piped()) - .spawn()?; + let build_child = build_command.stdout(Stdio::piped()).spawn()?; let build_output = build_child.wait_with_output().await?; @@ -245,6 +254,8 @@ async fn run_deploy( supports_flakes: bool, check_sigs: bool, cmd_overrides: utils::CmdOverrides, + keep_result: bool, + result_path: Option<&str>, ) -> Result<(), Box> { match (deploy_flake.node, deploy_flake.profile) { (Some(node_name), Some(profile_name)) => { @@ -274,6 +285,8 @@ async fn run_deploy( deploy_flake.repo, &deploy_data, &deploy_defs, + keep_result, + result_path, ) .await?; @@ -293,6 +306,8 @@ async fn run_deploy( &data.generic_settings, check_sigs, &cmd_overrides, + keep_result, + result_path, ) .await?; @@ -310,6 +325,8 @@ async fn run_deploy( &data.generic_settings, check_sigs, &cmd_overrides, + keep_result, + result_path, ) .await?; } @@ -360,12 +377,16 @@ async fn main() -> Result<(), Box> { let data = get_deployment_data(supports_flakes, deploy_flake.repo, &opts.extra_build_args).await?; + let result_path = opts.result_path.as_deref(); + run_deploy( deploy_flake, data, supports_flakes, opts.checksigs, cmd_overrides, + opts.keep_result, + result_path, ) .await?; diff --git a/src/utils/push.rs b/src/utils/push.rs index a82c9d4..5e87d5c 100644 --- a/src/utils/push.rs +++ b/src/utils/push.rs @@ -11,25 +11,27 @@ pub async fn push_profile( repo: &str, deploy_data: &super::DeployData<'_>, deploy_defs: &super::DeployDefs<'_>, + keep_result: bool, + result_path: Option<&str>, ) -> Result<(), Box> { info!( "Building profile `{}` for node `{}`", deploy_data.profile_name, deploy_data.node_name ); - let build_exit_status = if supports_flakes { + let mut build_c = if supports_flakes { Command::new("nix") - .arg("build") - .arg("--no-link") - .arg(format!( - "{}#deploy.nodes.{}.profiles.{}.path", - repo, deploy_data.node_name, deploy_data.profile_name - )) - .stdout(Stdio::null()) - .status() - .await? } else { Command::new("nix-build") + }; + + let mut build_command = if supports_flakes { + build_c.arg("build").arg("--no-link").arg(format!( + "{}#deploy.nodes.{}.profiles.{}.path", + repo, deploy_data.node_name, deploy_data.profile_name + )) + } else { + build_c .arg(&repo) .arg("--no-out-link") .arg("-A") @@ -37,11 +39,26 @@ pub async fn push_profile( "deploy.nodes.{}.profiles.{}.path", deploy_data.node_name, deploy_data.profile_name )) - .stdout(Stdio::null()) - .status() - .await? }; + build_command = match (keep_result, supports_flakes) { + (true, _) => { + let result_path = match result_path { + Some(x) => x, + None => "./.deploy-gc", + }; + + build_command.arg("--out-link").arg(format!( + "{}/{}/{}", + result_path, deploy_data.node_name, deploy_data.profile_name + )) + } + (false, false) => build_command.arg("--no-out-link"), + (false, true) => build_command.arg("--no-link"), + }; + + let build_exit_status = build_command.stdout(Stdio::null()).status().await?; + if !build_exit_status.success() { good_panic!("`nix build` failed"); } -- cgit v1.2.3 From 155f4bf45204b3090cc07271d2648ac7c024bbd1 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Sun, 25 Oct 2020 13:15:35 -0700 Subject: Fail correctly if initial activation fails --- src/activate.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/activate.rs b/src/activate.rs index 55ceb27..4fdb59c 100644 --- a/src/activate.rs +++ b/src/activate.rs @@ -114,7 +114,7 @@ pub async fn deactivate(profile_path: &str) -> Result<(), Box (), - _ if auto_rollback => return Ok(deactivate(&profile_path).await?), - _ => (), - } + let activate_status_all = match activate_status { + Ok(s) if s.success() => Ok(()), + Ok(_) => Err(std::io::Error::new(std::io::ErrorKind::Other, "Activation did not succeed")), + Err(x) => Err(x), + }; + + deactivate_on_err(&profile_path, activate_status_all).await; info!("Activation succeeded!"); -- cgit v1.2.3 From 426fb3c489dcbb4ccbf98a3ab6a7fe25e71b95ca Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 26 Oct 2020 12:24:57 -0700 Subject: Automatically run checks when deploying --- src/main.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 1358a83..48fb482 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,10 @@ struct Opts { #[clap(short, long)] result_path: Option, + /// Skip the automatic pre-build checks + #[clap(short, long)] + skip_checks: bool, + /// Override the SSH user with the given value #[clap(long)] ssh_user: Option, @@ -197,8 +201,42 @@ async fn test_flake_support() -> Result> { .success()) } +async fn check_deployment(supports_flakes: bool, repo: &str, extra_build_args: &[String]) -> () { + let mut c = match supports_flakes { + true => Command::new("nix"), + false => Command::new("nix-build"), + }; + + let mut check_command = match supports_flakes { + true => { + c.arg("flake") + .arg("check") + .arg(repo) + } + false => { + c.arg("-E") + .arg("--no-out-link") + .arg(format!("let r = import {}/.; in (if builtins.isFunction r then (r {{}}) else r).checks.${{builtins.currentSystem}}", repo)) + } + }; + + for extra_arg in extra_build_args { + check_command = check_command.arg(extra_arg); + } + + let check_status = match check_command.status().await { + Ok(x) => x, + Err(err) => good_panic!("Error running checks for the given flake repo: {:?}", err), + }; + + if !check_status.success() { + good_panic!("Checks failed for the given flake repo"); + } + + () +} + /// Evaluates the Nix in the given `repo` and return the processed Data from it -#[inline] async fn get_deployment_data( supports_flakes: bool, repo: &str, @@ -374,6 +412,10 @@ async fn main() -> Result<(), Box> { warn!("A Nix version without flakes support was detected, support for this is work in progress"); } + if !opts.skip_checks { + check_deployment(supports_flakes, deploy_flake.repo, &opts.extra_build_args).await; + } + let data = get_deployment_data(supports_flakes, deploy_flake.repo, &opts.extra_build_args).await?; -- cgit v1.2.3 From df002c31a64409350a3cb8825364542c65a4d00a Mon Sep 17 00:00:00 2001 From: notgne2 Date: Mon, 26 Oct 2020 12:44:19 -0700 Subject: Add more debug logs --- src/main.rs | 5 ++++- src/utils/deploy.rs | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 48fb482..5dc6bb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,6 @@ struct Opts { temp_path: Option, } -#[inline] async fn push_all_profiles( node: &utils::data::Node, node_name: &str, @@ -190,6 +189,8 @@ async fn deploy_all_profiles( /// Returns if the available Nix installation supports flakes #[inline] async fn test_flake_support() -> Result> { + debug!("Checking for flake support"); + Ok(Command::new("nix") .arg("eval") .arg("--expr") @@ -202,6 +203,8 @@ async fn test_flake_support() -> Result> { } async fn check_deployment(supports_flakes: bool, repo: &str, extra_build_args: &[String]) -> () { + info!("Running checks for flake in {}", repo); + let mut c = match supports_flakes { true => Command::new("nix"), false => Command::new("nix-build"), diff --git a/src/utils/deploy.rs b/src/utils/deploy.rs index 1ef7f11..59217df 100644 --- a/src/utils/deploy.rs +++ b/src/utils/deploy.rs @@ -106,6 +106,8 @@ pub async fn deploy_profile( magic_rollback, ); + debug!("Constructed activation command: {}", self_activate_command); + let hostname = match deploy_data.cmd_overrides.hostname { Some(ref x) => x, None => &deploy_data.node.node_settings.hostname, @@ -146,6 +148,11 @@ pub async fn deploy_profile( confirm_command = format!("{} {}", sudo_cmd, confirm_command); } + debug!( + "Attempting to run command to confirm deployment: {}", + confirm_command + ); + let ssh_exit_status = ssh_confirm_command.arg(confirm_command).status().await?; if !ssh_exit_status.success() { -- cgit v1.2.3