aboutsummaryrefslogtreecommitdiff
path: root/src/utils/mod.rs
diff options
context:
space:
mode:
authornotgne22020-09-28 14:37:43 -0700
committernotgne22020-09-28 14:37:43 -0700
commitf73e393a75fcad939a240ff3b72cbc75813e90e3 (patch)
tree39304318db967f39ed6925f5206174dd5b14dbab /src/utils/mod.rs
parent73b99043a71f27f98bf11510fb8db46fa086383c (diff)
Add missing files
Diffstat (limited to '')
-rw-r--r--src/utils/mod.rs71
1 files changed, 71 insertions, 0 deletions
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<String>,
+ 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<DeployData<'a>, Box<dyn std::error::Error>> {
+ let ssh_user: Cow<str> = match &merged_settings.ssh_user {
+ Some(u) => u.into(),
+ None => whoami::username().into(),
+ };
+
+ let profile_user: Cow<str> = 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<String> = 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,
+ })
+}