From 70c55363a91572790ba5d49b70c58040f112e55c Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 8 Jan 2021 18:24:04 -0700 Subject: Restructure project --- src/deploy.rs | 296 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 src/deploy.rs (limited to 'src/deploy.rs') diff --git a/src/deploy.rs b/src/deploy.rs new file mode 100644 index 0000000..3371160 --- /dev/null +++ b/src/deploy.rs @@ -0,0 +1,296 @@ +// SPDX-FileCopyrightText: 2020 Serokell +// SPDX-FileCopyrightText: 2020 Andreas Fuchs +// +// SPDX-License-Identifier: MPL-2.0 + +use std::borrow::Cow; +use tokio::process::Command; + +use thiserror::Error; + +fn build_activate_command( + sudo: &Option, + profile_path: &str, + closure: &str, + auto_rollback: bool, + temp_path: &Cow, + confirm_timeout: u16, + magic_rollback: bool, + debug_logs: bool, + log_dir: Option<&str>, +) -> String { + let mut self_activate_command = format!("{}/activate-rs", closure); + + if debug_logs { + self_activate_command = format!("{} --debug-logs", self_activate_command); + } + + if let Some(log_dir) = log_dir { + self_activate_command = format!("{} --log-dir {}", self_activate_command, log_dir); + } + + self_activate_command = format!( + "{} --temp-path '{}' activate '{}' '{}'", + self_activate_command, temp_path, closure, profile_path + ); + + self_activate_command = format!( + "{} --confirm-timeout {}", + self_activate_command, confirm_timeout + ); + + 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(sudo_cmd) = &sudo { + self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); + } + + self_activate_command +} + +#[test] +fn test_activation_command_builder() { + let sudo = Some("sudo -u test".to_string()); + let profile_path = "/blah/profiles/test"; + let closure = "/nix/store/blah/etc"; + let auto_rollback = true; + let temp_path = &"/tmp".into(); + let confirm_timeout = 30; + let magic_rollback = true; + let debug_logs = true; + let log_dir = Some("/tmp/something.txt"); + + assert_eq!( + build_activate_command( + &sudo, + profile_path, + closure, + auto_rollback, + temp_path, + confirm_timeout, + magic_rollback, + debug_logs, + log_dir + ), + "sudo -u test /nix/store/blah/etc/activate-rs --debug-logs --log-dir /tmp/something.txt --temp-path '/tmp' activate '/nix/store/blah/etc' '/blah/profiles/test' --confirm-timeout 30 --magic-rollback --auto-rollback" + .to_string(), + ); +} + +fn build_wait_command( + sudo: &Option, + closure: &str, + temp_path: &Cow, + debug_logs: bool, + log_dir: Option<&str>, +) -> String { + let mut self_activate_command = format!("{}/activate-rs", closure); + + if debug_logs { + self_activate_command = format!("{} --debug-logs", self_activate_command); + } + + if let Some(log_dir) = log_dir { + self_activate_command = format!("{} --log-dir {}", self_activate_command, log_dir); + } + + self_activate_command = format!( + "{} --temp-path '{}' wait '{}'", + self_activate_command, temp_path, closure + ); + + if let Some(sudo_cmd) = &sudo { + self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); + } + + self_activate_command +} + +#[test] +fn test_wait_command_builder() { + let sudo = Some("sudo -u test".to_string()); + let closure = "/nix/store/blah/etc"; + let temp_path = &"/tmp".into(); + let debug_logs = true; + let log_dir = Some("/tmp/something.txt"); + + assert_eq!( + build_wait_command( + &sudo, + closure, + temp_path, + debug_logs, + log_dir + ), + "sudo -u test /nix/store/blah/etc/activate-rs --debug-logs --log-dir /tmp/something.txt --temp-path '/tmp' wait '/nix/store/blah/etc'" + .to_string(), + ); +} + +#[derive(Error, Debug)] +pub enum DeployProfileError { + #[error("Failed to calculate activate bin path from deploy bin path: {0}")] + DeployPathToActivatePathError(#[from] super::DeployPathToActivatePathError), + + #[error("Failed to spawn activation command over SSH: {0}")] + SSHSpawnActivateError(std::io::Error), + + #[error("Failed to run activation command over SSH: {0}")] + SSHActivateError(std::io::Error), + #[error("Activating over SSH resulted in a bad exit code: {0:?}")] + SSHActivateExitError(Option), + + #[error("Failed to run wait command over SSH: {0}")] + SSHWaitError(std::io::Error), + #[error("Waiting over SSH resulted in a bad exit code: {0:?}")] + SSHWaitExitError(Option), + + #[error("Failed to run confirmation command over SSH (the server should roll back): {0}")] + SSHConfirmError(std::io::Error), + #[error( + "Confirming activation over SSH resulted in a bad exit code (the server should roll back): {0:?}" + )] + SSHConfirmExitError(Option), +} + +pub async fn deploy_profile( + deploy_data: &super::DeployData<'_>, + deploy_defs: &super::DeployDefs, +) -> Result<(), DeployProfileError> { + info!( + "Activating profile `{}` for node `{}`", + deploy_data.profile_name, deploy_data.node_name + ); + + let temp_path: Cow = match &deploy_data.merged_settings.temp_path { + Some(x) => x.into(), + None => "/tmp".into(), + }; + + let confirm_timeout = deploy_data.merged_settings.confirm_timeout.unwrap_or(30); + + let magic_rollback = deploy_data.merged_settings.magic_rollback.unwrap_or(true); + + let auto_rollback = deploy_data.merged_settings.auto_rollback.unwrap_or(true); + + let self_activate_command = build_activate_command( + &deploy_defs.sudo, + &deploy_defs.profile_path, + &deploy_data.profile.profile_settings.path, + auto_rollback, + &temp_path, + confirm_timeout, + magic_rollback, + deploy_data.debug_logs, + deploy_data.log_dir, + ); + + debug!("Constructed activation command: {}", self_activate_command); + + let self_wait_command = build_wait_command( + &deploy_defs.sudo, + &deploy_data.profile.profile_settings.path, + &temp_path, + deploy_data.debug_logs, + deploy_data.log_dir, + ); + + debug!("Constructed wait command: {}", self_wait_command); + + let hostname = match deploy_data.cmd_overrides.hostname { + Some(ref x) => x, + None => &deploy_data.node.node_settings.hostname, + }; + + let ssh_addr = format!("ssh://{}@{}", deploy_defs.ssh_user, hostname); + + let mut ssh_activate_command_ = Command::new("ssh"); + let ssh_activate_command = ssh_activate_command_.arg(&ssh_addr); + + for ssh_opt in &deploy_data.merged_settings.ssh_opts { + ssh_activate_command.arg(&ssh_opt); + } + + if !magic_rollback { + let ssh_activate_exit_status = ssh_activate_command + .arg(self_activate_command) + .status() + .await + .map_err(DeployProfileError::SSHActivateError)?; + + match ssh_activate_exit_status.code() { + Some(0) => (), + a => return Err(DeployProfileError::SSHActivateExitError(a)), + }; + + info!("Success activating, done!"); + } else { + let ssh_activate = ssh_activate_command + .arg(self_activate_command) + .spawn() + .map_err(DeployProfileError::SSHSpawnActivateError)?; + + info!("Creating activation waiter"); + + let mut ssh_wait_command_ = Command::new("ssh"); + let ssh_wait_command = ssh_wait_command_.arg(&ssh_addr); + + for ssh_opt in &deploy_data.merged_settings.ssh_opts { + ssh_wait_command.arg(ssh_opt); + } + + let ssh_wait_exit_status = ssh_wait_command + .arg(self_wait_command) + .status() + .await + .map_err(DeployProfileError::SSHWaitError)?; + + match ssh_wait_exit_status.code() { + Some(0) => (), + a => return Err(DeployProfileError::SSHWaitExitError(a)), + }; + + info!("Success activating, attempting to confirm activation"); + + 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_path = + super::make_lock_path(&temp_path, &deploy_data.profile.profile_settings.path); + + let mut confirm_command = format!("rm {}", lock_path); + if let Some(sudo_cmd) = &deploy_defs.sudo { + 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 + .map_err(DeployProfileError::SSHConfirmError)?; + + match ssh_exit_status.code() { + Some(0) => (), + a => return Err(DeployProfileError::SSHConfirmExitError(a)), + }; + + info!("Deployment confirmed."); + } + + Ok(()) +} -- cgit v1.2.3 From 1789551855a8290caad59d3849bab16c56158c70 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 8 Jan 2021 18:28:45 -0700 Subject: Fix trivial lint issues --- src/deploy.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/deploy.rs') diff --git a/src/deploy.rs b/src/deploy.rs index 3371160..4d13330 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -13,7 +13,7 @@ fn build_activate_command( profile_path: &str, closure: &str, auto_rollback: bool, - temp_path: &Cow, + temp_path: &str, confirm_timeout: u16, magic_rollback: bool, debug_logs: bool, @@ -60,7 +60,7 @@ fn test_activation_command_builder() { let profile_path = "/blah/profiles/test"; let closure = "/nix/store/blah/etc"; let auto_rollback = true; - let temp_path = &"/tmp".into(); + let temp_path = "/tmp"; let confirm_timeout = 30; let magic_rollback = true; let debug_logs = true; @@ -86,7 +86,7 @@ fn test_activation_command_builder() { fn build_wait_command( sudo: &Option, closure: &str, - temp_path: &Cow, + temp_path: &str, debug_logs: bool, log_dir: Option<&str>, ) -> String { @@ -116,7 +116,7 @@ fn build_wait_command( fn test_wait_command_builder() { let sudo = Some("sudo -u test".to_string()); let closure = "/nix/store/blah/etc"; - let temp_path = &"/tmp".into(); + let temp_path = "/tmp"; let debug_logs = true; let log_dir = Some("/tmp/something.txt"); -- cgit v1.2.3 From f7e5768712e77faf75c0df2417cff34ad90ac386 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 8 Jan 2021 18:33:34 -0700 Subject: Struct-ify arguments to build_activate_command --- src/deploy.rs | 54 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'src/deploy.rs') diff --git a/src/deploy.rs b/src/deploy.rs index 4d13330..18f52e2 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -8,46 +8,48 @@ use tokio::process::Command; use thiserror::Error; -fn build_activate_command( - sudo: &Option, - profile_path: &str, - closure: &str, +struct ActivateCommandData<'a> { + sudo: &'a Option, + profile_path: &'a str, + closure: &'a str, auto_rollback: bool, - temp_path: &str, + temp_path: &'a str, confirm_timeout: u16, magic_rollback: bool, debug_logs: bool, - log_dir: Option<&str>, -) -> String { - let mut self_activate_command = format!("{}/activate-rs", closure); + log_dir: Option<&'a str>, +} - if debug_logs { +fn build_activate_command(data: ActivateCommandData) -> String { + let mut self_activate_command = format!("{}/activate-rs", data.closure); + + if data.debug_logs { self_activate_command = format!("{} --debug-logs", self_activate_command); } - if let Some(log_dir) = log_dir { + if let Some(log_dir) = data.log_dir { self_activate_command = format!("{} --log-dir {}", self_activate_command, log_dir); } self_activate_command = format!( "{} --temp-path '{}' activate '{}' '{}'", - self_activate_command, temp_path, closure, profile_path + self_activate_command, data.temp_path, data.closure, data.profile_path ); self_activate_command = format!( "{} --confirm-timeout {}", - self_activate_command, confirm_timeout + self_activate_command, data.confirm_timeout ); - if magic_rollback { + if data.magic_rollback { self_activate_command = format!("{} --magic-rollback", self_activate_command); } - if auto_rollback { + if data.auto_rollback { self_activate_command = format!("{} --auto-rollback", self_activate_command); } - if let Some(sudo_cmd) = &sudo { + if let Some(sudo_cmd) = &data.sudo { self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); } @@ -67,8 +69,8 @@ fn test_activation_command_builder() { let log_dir = Some("/tmp/something.txt"); assert_eq!( - build_activate_command( - &sudo, + build_activate_command(ActivateCommandData { + sudo: &sudo, profile_path, closure, auto_rollback, @@ -77,7 +79,7 @@ fn test_activation_command_builder() { magic_rollback, debug_logs, log_dir - ), + }), "sudo -u test /nix/store/blah/etc/activate-rs --debug-logs --log-dir /tmp/something.txt --temp-path '/tmp' activate '/nix/store/blah/etc' '/blah/profiles/test' --confirm-timeout 30 --magic-rollback --auto-rollback" .to_string(), ); @@ -179,17 +181,17 @@ pub async fn deploy_profile( let auto_rollback = deploy_data.merged_settings.auto_rollback.unwrap_or(true); - let self_activate_command = build_activate_command( - &deploy_defs.sudo, - &deploy_defs.profile_path, - &deploy_data.profile.profile_settings.path, + let self_activate_command = build_activate_command(ActivateCommandData { + sudo: &deploy_defs.sudo, + profile_path: &deploy_defs.profile_path, + closure: &deploy_data.profile.profile_settings.path, auto_rollback, - &temp_path, + temp_path: &temp_path, confirm_timeout, magic_rollback, - deploy_data.debug_logs, - deploy_data.log_dir, - ); + debug_logs: deploy_data.debug_logs, + log_dir: deploy_data.log_dir, + }); debug!("Constructed activation command: {}", self_activate_command); -- cgit v1.2.3 From 6854fea5c6994ec39adb3ec12ddd9e5c600df114 Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 8 Jan 2021 18:36:10 -0700 Subject: Struct-ify arguments to build_wait_command --- src/deploy.rs | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'src/deploy.rs') diff --git a/src/deploy.rs b/src/deploy.rs index 18f52e2..cc31a62 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -85,29 +85,31 @@ fn test_activation_command_builder() { ); } -fn build_wait_command( - sudo: &Option, - closure: &str, - temp_path: &str, +struct WaitCommandData<'a> { + sudo: &'a Option, + closure: &'a str, + temp_path: &'a str, debug_logs: bool, - log_dir: Option<&str>, -) -> String { - let mut self_activate_command = format!("{}/activate-rs", closure); + log_dir: Option<&'a str>, +} - if debug_logs { +fn build_wait_command(data: WaitCommandData) -> String { + let mut self_activate_command = format!("{}/activate-rs", data.closure); + + if data.debug_logs { self_activate_command = format!("{} --debug-logs", self_activate_command); } - if let Some(log_dir) = log_dir { + if let Some(log_dir) = data.log_dir { self_activate_command = format!("{} --log-dir {}", self_activate_command, log_dir); } self_activate_command = format!( "{} --temp-path '{}' wait '{}'", - self_activate_command, temp_path, closure + self_activate_command, data.temp_path, data.closure ); - if let Some(sudo_cmd) = &sudo { + if let Some(sudo_cmd) = &data.sudo { self_activate_command = format!("{} {}", sudo_cmd, self_activate_command); } @@ -123,13 +125,13 @@ fn test_wait_command_builder() { let log_dir = Some("/tmp/something.txt"); assert_eq!( - build_wait_command( - &sudo, + build_wait_command(WaitCommandData { + sudo: &sudo, closure, temp_path, debug_logs, log_dir - ), + }), "sudo -u test /nix/store/blah/etc/activate-rs --debug-logs --log-dir /tmp/something.txt --temp-path '/tmp' wait '/nix/store/blah/etc'" .to_string(), ); @@ -195,13 +197,13 @@ pub async fn deploy_profile( debug!("Constructed activation command: {}", self_activate_command); - let self_wait_command = build_wait_command( - &deploy_defs.sudo, - &deploy_data.profile.profile_settings.path, - &temp_path, - deploy_data.debug_logs, - deploy_data.log_dir, - ); + let self_wait_command = build_wait_command(WaitCommandData { + sudo: &deploy_defs.sudo, + closure: &deploy_data.profile.profile_settings.path, + temp_path: &temp_path, + debug_logs: deploy_data.debug_logs, + log_dir: deploy_data.log_dir, + }); debug!("Constructed wait command: {}", self_wait_command); -- cgit v1.2.3 From dc26fba1bd908fbdf1b3d702ad67cbb351952e7a Mon Sep 17 00:00:00 2001 From: notgne2 Date: Fri, 8 Jan 2021 19:08:34 -0700 Subject: Remove unused DeployPathToActivatePathError --- src/deploy.rs | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/deploy.rs') diff --git a/src/deploy.rs b/src/deploy.rs index cc31a62..03c3623 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -139,9 +139,6 @@ fn test_wait_command_builder() { #[derive(Error, Debug)] pub enum DeployProfileError { - #[error("Failed to calculate activate bin path from deploy bin path: {0}")] - DeployPathToActivatePathError(#[from] super::DeployPathToActivatePathError), - #[error("Failed to spawn activation command over SSH: {0}")] SSHSpawnActivateError(std::io::Error), -- cgit v1.2.3 From 91f505401ef338a04da4a6e7256ea8b801a0a68a Mon Sep 17 00:00:00 2001 From: notgne2 Date: Thu, 14 Jan 2021 12:37:59 -0700 Subject: Only build wait command if magic_rollback is enabled --- src/deploy.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/deploy.rs') diff --git a/src/deploy.rs b/src/deploy.rs index 03c3623..a33721c 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -194,16 +194,6 @@ pub async fn deploy_profile( debug!("Constructed activation command: {}", self_activate_command); - let self_wait_command = build_wait_command(WaitCommandData { - sudo: &deploy_defs.sudo, - closure: &deploy_data.profile.profile_settings.path, - temp_path: &temp_path, - debug_logs: deploy_data.debug_logs, - log_dir: deploy_data.log_dir, - }); - - debug!("Constructed wait command: {}", self_wait_command); - let hostname = match deploy_data.cmd_overrides.hostname { Some(ref x) => x, None => &deploy_data.node.node_settings.hostname, @@ -232,6 +222,16 @@ pub async fn deploy_profile( info!("Success activating, done!"); } else { + let self_wait_command = build_wait_command(WaitCommandData { + sudo: &deploy_defs.sudo, + closure: &deploy_data.profile.profile_settings.path, + temp_path: &temp_path, + debug_logs: deploy_data.debug_logs, + log_dir: deploy_data.log_dir, + }); + + debug!("Constructed wait command: {}", self_wait_command); + let ssh_activate = ssh_activate_command .arg(self_activate_command) .spawn() -- cgit v1.2.3