Skip to content

Commit

Permalink
Refactor & simplify the target setting resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Aug 26, 2021
1 parent 4a796d6 commit 29fba1e
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 193 deletions.
18 changes: 15 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ async fn run_deploy(
// Rollbacks adhere to the global seeting to auto_rollback and secondary
// the profile's configuration
for (deploy_data, deploy_defs) in &parts {
if let Err(e) = deploy::deploy::deploy_profile(deploy_data, deploy_defs, cmd_flags.dry_activate).await
if let Err(e) = deploy::deploy::deploy_profile(
&deploy_data.node_name,
&deploy_data.profile_name,
deploy::deploy::SshCommand::from_data(&deploy_data)?,
deploy::deploy::ActivateCommand::from_data(&deploy_data),
deploy::deploy::WaitCommand::from_data(&deploy_data),
deploy::deploy::ConfirmCommand::from_data(&deploy_data),
).await
{
error!("{}", e);
if cmd_flags.dry_activate {
Expand All @@ -246,9 +253,14 @@ async fn run_deploy(
// revoking all previous deploys
// (adheres to profile configuration if not set explicitely by
// the command line)
for (deploy_data, deploy_defs) in &succeeded {
for (deploy_data, _) in &succeeded {
if deploy_data.merged_settings.auto_rollback.unwrap_or(true) {
deploy::deploy::revoke(*deploy_data, *deploy_defs).await?;
deploy::deploy::revoke(
&deploy_data.node_name,
&deploy_data.profile_name,
deploy::deploy::SshCommand::from_data(&deploy_data)?,
deploy::deploy::RevokeCommand::from_data(&deploy_data),
).await?;
}
}
}
Expand Down
86 changes: 71 additions & 15 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum ResolveTargetError {
ProfileNotFound(String, String, String),
#[error("Profile was provided without a node name for repo `{0}`")]
ProfileWithoutNode(String),
#[error("Deployment data invalid: {0}")]
InvalidDeployDataError(#[from] DeployDataError),
}

impl<'a> Target {
Expand Down Expand Up @@ -70,7 +72,7 @@ impl<'a> Target {
node_,
profile_,
hostname,
);
)?;
vec![d]
})
} else {
Expand Down Expand Up @@ -257,12 +259,18 @@ pub struct DeployData<'a> {
pub repo: String,
pub node_name: String,
pub profile_name: String,
pub node: &'a settings::Node,
pub profile: &'a settings::Profile,

pub hostname: Option<&'a str>,

pub flags: &'a Flags,
pub node: &'a settings::Node,
pub profile: &'a settings::Profile,
pub merged_settings: settings::GenericSettings,

pub ssh_user: String,
pub temp_path: String,
pub profile_path: String,
pub sudo: Option<String>,
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -330,7 +338,7 @@ impl<'a> DeployData<'a> {
node: &'a settings::Node,
profile: &'a settings::Profile,
hostname: Option<&'a str>,
) -> DeployData<'a> {
) -> Result<DeployData<'a>, DeployDataError> {
let mut merged_settings = cmd_settings.clone();
merged_settings.merge(profile.generic_settings.clone());
merged_settings.merge(node.generic_settings.clone());
Expand All @@ -339,33 +347,59 @@ impl<'a> DeployData<'a> {
// if let Some(ref ssh_opts) = cmd_overrides.ssh_opts {
// merged_settings.ssh_opts = ssh_opts.split(' ').map(|x| x.to_owned()).collect();
// }
let temp_path = match merged_settings.temp_path {
Some(ref x) => x.to_owned(),
None => "/tmp".to_string(),
};
let profile_user = match merged_settings.user {
Some(ref x) => x.to_owned(),
None => match merged_settings.ssh_user {
Some(ref x) => x.to_owned(),
None => {
return Err(DeployDataError::NoProfileUser(profile_name, node_name))
}
},
};
let profile_path = match profile.profile_settings.profile_path {
None => format!("/nix/var/nix/profiles/{}", match &profile_user[..] {
"root" => profile_name.to_owned(),
_ => format!("per-user/{}/{}", profile_user, profile_name),
}),
Some(ref x) => x.to_owned(),
};
let ssh_user = match merged_settings.ssh_user {
Some(ref u) => u.to_owned(),
None => whoami::username(),
};
let sudo = match merged_settings.user {
Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)),
_ => None,
};

DeployData {
Ok(DeployData {
repo,
node_name,
profile_name,
node,
profile,
hostname,
ssh_user,
temp_path,
profile_path,
sudo,
flags,
merged_settings,
}
})
}

pub fn defs(&'a self) -> Result<DeployDefs, DeployDataError> {
let ssh_user = match self.merged_settings.ssh_user {
Some(ref u) => u.clone(),
None => whoami::username(),
};

let profile_user = self.get_profile_user()?;

let profile_path = self.get_profile_path()?;

let sudo: Option<String> = match self.merged_settings.user {
Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)),
_ => None,
};
let sudo = self.sudo()?;

Ok(DeployDefs {
ssh_user,
Expand All @@ -375,6 +409,28 @@ impl<'a> DeployData<'a> {
})
}

pub fn sudo(&'a self) -> Result<Option<String>, DeployDataError> {
let ssh_user = match self.merged_settings.ssh_user {
Some(ref u) => u.clone(),
None => whoami::username(),
};
Ok(
match self.merged_settings.user {
Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)),
_ => None,
}
)
}

pub fn temp_path(&'a self) -> Result<String, DeployDataError> {
Ok(
match self.merged_settings.temp_path {
Some(ref x) => x.to_owned(),
None => "/tmp".to_string(),
}
)
}

pub fn ssh_uri(&'a self) -> Result<String, DeployDataError> {

let hostname = match self.hostname {
Expand Down Expand Up @@ -404,8 +460,8 @@ impl<'a> DeployData<'a> {
Ok(format!("{}@{}", ssh_user, hostname))
}

pub fn ssh_opts(&'a self) -> impl Iterator<Item = &String> {
self.merged_settings.ssh_opts.iter()
pub fn ssh_opts(&'a self) -> Result<impl Iterator<Item = &String>, DeployDataError> {
Ok(self.merged_settings.ssh_opts.iter())
}

pub fn get_profile_path(&'a self) -> Result<String, DeployDataError> {
Expand Down
Loading

0 comments on commit 29fba1e

Please sign in to comment.