diff options
author | notgne2 | 2020-10-01 20:24:09 -0700 |
---|---|---|
committer | notgne2 | 2020-10-01 20:24:09 -0700 |
commit | 05803e0ebaf417d9ba40645b6548a48bf51f9213 (patch) | |
tree | 17af19223dd8b207e328ae1732fe0bf3078df5f6 /src | |
parent | e14acaf2bdc14bbdc30f3d558b62f64fe33ff5f9 (diff) |
Handle more command exits correctly
Diffstat (limited to '')
-rw-r--r-- | src/activate.rs | 39 | ||||
-rw-r--r-- | src/utils/deploy.rs | 6 | ||||
-rw-r--r-- | src/utils/push.rs | 30 |
3 files changed, 55 insertions, 20 deletions
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<dyn std::error::Error>> { .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(()) } |