diff options
author | notgne2 | 2021-02-09 03:00:48 -0700 |
---|---|---|
committer | notgne2 | 2021-02-09 03:00:48 -0700 |
commit | c27b76e00101a1190c46c80fc1618c2732827d96 (patch) | |
tree | 6e1de8e143b1565d60798549ab235fe7d9993ccc | |
parent | 64f84aaf87b8771cd8050d932c96400df9ccd75b (diff) | |
parent | 4f956e0979d6e2617a0800062627a345337bff71 (diff) |
Merge branch 'master' into notgne2/set-tmp-in-nixos-activation
-rw-r--r-- | Cargo.lock | 4 | ||||
-rw-r--r-- | Cargo.toml | 23 | ||||
-rw-r--r-- | src/bin/activate.rs | 5 | ||||
-rw-r--r-- | src/bin/deploy.rs | 11 | ||||
-rw-r--r-- | src/data.rs | 2 | ||||
-rw-r--r-- | src/deploy.rs | 132 | ||||
-rw-r--r-- | src/lib.rs | 6 | ||||
-rw-r--r-- | src/push.rs | 4 |
8 files changed, 108 insertions, 79 deletions
@@ -144,7 +144,6 @@ dependencies = [ "notify", "rnix", "serde", - "serde_derive", "serde_json", "signal-hook", "smol_str", @@ -770,6 +769,9 @@ name = "serde" version = "1.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_derive" @@ -12,22 +12,21 @@ edition = "2018" [dependencies] clap = "3.0.0-beta.2" -tokio = { version = "0.3.5", features = [ "full" ] } -serde_json = "1.0.48" -serde_derive = "1.0.104" -serde = "1.0.104" -merge = "0.1.0" -whoami = "0.9.0" -log = "0.4" flexi_logger = "0.16" -notify = "5.0.0-pre.3" -futures-util = "0.3.6" fork = "0.1" +futures-util = "0.3.6" +log = "0.4" +merge = "0.1.0" +notify = "5.0.0-pre.3" +rnix = "0.8" +serde = { version = "1.0.104", features = [ "derive" ] } +serde_json = "1.0.48" +signal-hook = "0.3" thiserror = "1.0" +tokio = { version = "0.3.5", features = [ "full" ] } toml = "0.5" +whoami = "0.9.0" yn = "0.1" -rnix = "0.8" -signal-hook = "0.3" # smol_str is required by rnix, but 0.1.17 doesn't build on rustc # 1.45.2 (shipped in nixos-20.09); it requires rustc 1.46.0. See @@ -37,4 +36,4 @@ smol_str = "=0.1.16" [lib] name = "deploy" -path = "src/lib.rs"
\ No newline at end of file +path = "src/lib.rs" diff --git a/src/bin/activate.rs b/src/bin/activate.rs index 2f13b44..947e883 100644 --- a/src/bin/activate.rs +++ b/src/bin/activate.rs @@ -20,10 +20,7 @@ use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use thiserror::Error; -#[macro_use] -extern crate log; - -extern crate serde_derive; +use log::{debug, error, info, warn}; /// Remote activation utility for deploy-rs #[derive(Clap, Debug)] diff --git a/src/bin/deploy.rs b/src/bin/deploy.rs index 30ebd25..8bd8b18 100644 --- a/src/bin/deploy.rs +++ b/src/bin/deploy.rs @@ -7,16 +7,11 @@ use std::io::{stdin, stdout, Write}; use clap::Clap; +use log::{debug, error, info, warn}; +use serde::Serialize; use std::process::Stdio; -use tokio::process::Command; - use thiserror::Error; - -#[macro_use] -extern crate log; - -#[macro_use] -extern crate serde_derive; +use tokio::process::Command; /// Simple Rust rewrite of a simple Nix Flake deployment tool #[derive(Clap, Debug)] diff --git a/src/data.rs b/src/data.rs index f557e41..6fe7f75 100644 --- a/src/data.rs +++ b/src/data.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: MPL-2.0 use merge::Merge; - +use serde::Deserialize; use std::collections::HashMap; #[derive(Deserialize, Debug, Clone, Merge)] diff --git a/src/deploy.rs b/src/deploy.rs index a33721c..54d5ea7 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -3,10 +3,10 @@ // // SPDX-License-Identifier: MPL-2.0 +use log::{debug, info}; use std::borrow::Cow; -use tokio::process::Command; - use thiserror::Error; +use tokio::process::Command; struct ActivateCommandData<'a> { sudo: &'a Option<String>, @@ -138,6 +138,57 @@ fn test_wait_command_builder() { } #[derive(Error, Debug)] +pub enum ConfirmProfileError { + #[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<i32>), +} + +pub async fn confirm_profile( + deploy_data: &super::DeployData<'_>, + deploy_defs: &super::DeployDefs, + hostname: &str, + temp_path: Cow<'_, str>, +) -> Result<(), ConfirmProfileError> { + 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_confirm_exit_status = ssh_confirm_command + .arg(confirm_command) + .status() + .await + .map_err(ConfirmProfileError::SSHConfirmError)?; + + match ssh_confirm_exit_status.code() { + Some(0) => (), + a => return Err(ConfirmProfileError::SSHConfirmExitError(a)), + }; + + info!("Deployment confirmed."); + + Ok(()) +} + +#[derive(Error, Debug)] pub enum DeployProfileError { #[error("Failed to spawn activation command over SSH: {0}")] SSHSpawnActivateError(std::io::Error), @@ -152,12 +203,8 @@ pub enum DeployProfileError { #[error("Waiting over SSH resulted in a bad exit code: {0:?}")] SSHWaitExitError(Option<i32>), - #[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<i32>), + #[error("Error confirming deployment: {0}")] + ConfirmError(#[from] ConfirmProfileError), } pub async fn deploy_profile( @@ -246,51 +293,46 @@ pub async fn deploy_profile( 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)), - }; + let (send_activate, recv_activate) = tokio::sync::oneshot::channel(); + let (send_activated, recv_activated) = tokio::sync::oneshot::channel(); - info!("Success activating, attempting to confirm activation"); + tokio::spawn(async move { + let o = ssh_activate.wait_with_output().await; - let mut c = Command::new("ssh"); - let mut ssh_confirm_command = c.arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname)); + let maybe_err = match o { + Err(x) => Some(DeployProfileError::SSHActivateError(x)), + Ok(ref x) => match x.status.code() { + Some(0) => None, + a => Some(DeployProfileError::SSHActivateExitError(a)), + }, + }; - for ssh_opt in &deploy_data.merged_settings.ssh_opts { - ssh_confirm_command = ssh_confirm_command.arg(ssh_opt); - } + if let Some(err) = maybe_err { + send_activate.send(err).unwrap(); + } - let lock_path = - super::make_lock_path(&temp_path, &deploy_data.profile.profile_settings.path); + send_activated.send(()).unwrap(); + }); - let mut confirm_command = format!("rm {}", lock_path); - if let Some(sudo_cmd) = &deploy_defs.sudo { - confirm_command = format!("{} {}", sudo_cmd, confirm_command); + tokio::select! { + x = ssh_wait_command.arg(self_wait_command).status() => { + debug!("Wait command ended"); + match x.map_err(DeployProfileError::SSHWaitError)?.code() { + Some(0) => (), + a => return Err(DeployProfileError::SSHWaitExitError(a)), + }; + }, + x = recv_activate => { + debug!("Activate command exited with an error"); + return Err(x.unwrap()); + }, } - 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!("Success activating, attempting to confirm activation"); - info!("Deployment confirmed."); + let c = confirm_profile(deploy_data, deploy_defs, hostname, temp_path).await; + recv_activated.await.unwrap(); + c?; } Ok(()) @@ -11,12 +11,6 @@ use thiserror::Error; use flexi_logger::*; -#[macro_use] -extern crate log; - -#[macro_use] -extern crate serde_derive; - pub fn make_lock_path(temp_path: &str, closure: &str) -> String { let lock_hash = &closure["/nix/store/".len()..closure.find('-').unwrap_or_else(|| closure.len())]; diff --git a/src/push.rs b/src/push.rs index 2f83019..0963a9a 100644 --- a/src/push.rs +++ b/src/push.rs @@ -2,11 +2,11 @@ // // SPDX-License-Identifier: MPL-2.0 +use log::{debug, info}; use std::path::Path; use std::process::Stdio; -use tokio::process::Command; - use thiserror::Error; +use tokio::process::Command; #[derive(Error, Debug)] pub enum PushProfileError { |