diff options
Diffstat (limited to '')
-rw-r--r-- | src/deploy.rs (renamed from src/utils/deploy.rs) | 111 |
1 files changed, 56 insertions, 55 deletions
diff --git a/src/utils/deploy.rs b/src/deploy.rs index 3371160..a33721c 100644 --- a/src/utils/deploy.rs +++ b/src/deploy.rs @@ -8,46 +8,48 @@ use tokio::process::Command; use thiserror::Error; -fn build_activate_command( - sudo: &Option<String>, - profile_path: &str, - closure: &str, +struct ActivateCommandData<'a> { + sudo: &'a Option<String>, + profile_path: &'a str, + closure: &'a str, auto_rollback: bool, - temp_path: &Cow<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>, +} + +fn build_activate_command(data: ActivateCommandData) -> String { + let mut self_activate_command = format!("{}/activate-rs", data.closure); - if debug_logs { + 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); } @@ -60,15 +62,15 @@ 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; 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,35 +79,37 @@ 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(), ); } -fn build_wait_command( - sudo: &Option<String>, - closure: &str, - temp_path: &Cow<str>, +struct WaitCommandData<'a> { + sudo: &'a Option<String>, + 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); } @@ -116,18 +120,18 @@ 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"); 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(), ); @@ -135,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), @@ -179,30 +180,20 @@ 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); - 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, @@ -231,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() |