Skip to content

Commit

Permalink
Refactor create push.rs views into data.rs owned data & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Aug 13, 2021
1 parent 7e6f729 commit 2f99eef
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 261 deletions.
51 changes: 19 additions & 32 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,19 @@ struct PromptPart<'a> {
}

fn print_deployment(
parts: &[(
&data::DeployData,
data::DeployDefs,
)],
parts: &[&data::DeployData],
) -> Result<(), toml::ser::Error> {
let mut part_map: HashMap<String, HashMap<String, PromptPart>> = HashMap::new();

for (data, defs) in parts {
for data in parts {
part_map
.entry(data.node_name.to_string())
.or_insert_with(HashMap::new)
.insert(
data.profile_name.to_string(),
PromptPart {
user: &defs.profile_user,
ssh_user: &defs.ssh_user,
user: &data.profile_user,
ssh_user: &data.ssh_user,
path: &data.profile.profile_settings.path,
hostname: &data.node.node_settings.hostname,
ssh_opts: &data.merged_settings.ssh_opts,
Expand All @@ -108,10 +105,7 @@ pub enum PromptDeploymentError {
}

fn prompt_deployment(
parts: &[(
&data::DeployData,
data::DeployDefs,
)],
parts: &[&data::DeployData],
) -> Result<(), PromptDeploymentError> {
print_deployment(parts)?;

Expand Down Expand Up @@ -198,14 +192,10 @@ async fn run_deploy(
.collect::<Result<Vec<Vec<data::DeployData<'_>>>, data::ResolveTargetError>>()?;
let deploy_datas: Vec<&data::DeployData<'_>> = deploy_datas_.iter().flatten().collect();

let mut parts: Vec<(
&data::DeployData,
data::DeployDefs,
)> = Vec::new();
let mut parts: Vec<&data::DeployData> = Vec::new();

for deploy_data in deploy_datas {
let deploy_defs = deploy_data.defs()?;
parts.push((deploy_data, deploy_defs));
parts.push(deploy_data);
}

if cmd_flags.interactive {
Expand All @@ -214,27 +204,24 @@ async fn run_deploy(
print_deployment(&parts[..])?;
}

for (deploy_data, deploy_defs) in &parts {
deploy::push::push_profile(deploy::push::PushProfileData {
supports_flakes: &supports_flakes,
check_sigs: &cmd_flags.checksigs,
repo: &deploy_data.repo,
deploy_data: &deploy_data,
deploy_defs: &deploy_defs,
keep_result: &cmd_flags.keep_result,
result_path: cmd_flags.result_path.as_deref(),
extra_build_args: &cmd_flags.extra_build_args,
})
for deploy_data in &parts {
deploy::push::push_profile(
supports_flakes,
deploy::push::ShowDerivationCommand::from_data(&deploy_data),
deploy::push::BuildCommand::from_data(&deploy_data),
deploy::push::SignCommand::from_data(&deploy_data),
deploy::push::CopyCommand::from_data(&deploy_data),
)
.await?;
}

let mut succeeded: Vec<(&data::DeployData, &data::DeployDefs)> = vec![];
let mut succeeded: Vec<&data::DeployData> = vec![];

// Run all deployments
// In case of an error rollback any previoulsy made deployment.
// Rollbacks adhere to the global seeting to auto_rollback and secondary
// the profile's configuration
for (deploy_data, deploy_defs) in &parts {
for deploy_data in &parts {
if let Err(e) = deploy::deploy::deploy_profile(
&deploy_data.node_name,
&deploy_data.profile_name,
Expand All @@ -253,7 +240,7 @@ async fn run_deploy(
// revoking all previous deploys
// (adheres to profile configuration if not set explicitely by
// the command line)
for (deploy_data, _) in &succeeded {
for deploy_data in &succeeded {
if deploy_data.merged_settings.auto_rollback.unwrap_or(true) {
deploy::deploy::revoke(
&deploy_data.node_name,
Expand All @@ -266,7 +253,7 @@ async fn run_deploy(
}
break;
}
succeeded.push((deploy_data, deploy_defs))
succeeded.push(deploy_data)
}

Ok(())
Expand Down
136 changes: 15 additions & 121 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,18 @@ pub struct DeployData<'a> {
pub node_name: String,
pub profile_name: String,

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 hostname: &'a str,

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

Expand Down Expand Up @@ -318,14 +320,6 @@ pub struct Flags {
pub rollback_succeeded: bool,
}

#[derive(Debug)]
pub struct DeployDefs {
pub ssh_user: String,
pub profile_user: String,
pub profile_path: String,
pub sudo: Option<String>,
}

impl<'a> DeployData<'a> {

fn new(
Expand All @@ -351,14 +345,10 @@ impl<'a> DeployData<'a> {
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_user = if let Some(ref x) = merged_settings.user { x.to_owned() } else {
if let Some(ref x) = merged_settings.ssh_user { x.to_owned() } else {
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[..] {
Expand All @@ -375,6 +365,11 @@ impl<'a> DeployData<'a> {
Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)),
_ => None,
};
let hostname = match hostname {
Some(x) => x,
None => &node.node_settings.hostname,
};
let ssh_uri = format!("ssh://{}@{}", &ssh_user, &hostname);

Ok(DeployData {
repo,
Expand All @@ -384,114 +379,13 @@ impl<'a> DeployData<'a> {
profile,
hostname,
ssh_user,
ssh_uri,
temp_path,
profile_path,
profile_user,
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 = self.sudo()?;

Ok(DeployDefs {
ssh_user,
profile_user,
profile_path,
sudo,
})
}

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 {
Some(x) => x,
None => &self.node.node_settings.hostname,
};
let curr_user = &whoami::username();
let ssh_user = match self.merged_settings.ssh_user {
Some(ref u) => u,
None => curr_user,
};
Ok(format!("ssh://{}@{}", ssh_user, hostname))
}

// can be dropped once ssh fully supports ipv6 uris
pub fn ssh_non_uri(&'a self) -> Result<String, DeployDataError> {

let hostname = match self.hostname {
Some(x) => x,
None => &self.node.node_settings.hostname,
};
let curr_user = &whoami::username();
let ssh_user = match self.merged_settings.ssh_user {
Some(ref u) => u,
None => curr_user,
};
Ok(format!("{}@{}", ssh_user, hostname))
}

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> {
let profile_user = self.get_profile_user()?;
let profile_path = match self.profile.profile_settings.profile_path {
None => match &profile_user[..] {
"root" => format!("/nix/var/nix/profiles/{}", self.profile_name),
_ => format!(
"/nix/var/nix/profiles/per-user/{}/{}",
profile_user, self.profile_name
),
},
Some(ref x) => x.clone(),
};
Ok(profile_path)
}

pub fn get_profile_user(&'a self) -> Result<String, DeployDataError> {
let profile_user = match self.merged_settings.user {
Some(ref x) => x.clone(),
None => match self.merged_settings.ssh_user {
Some(ref x) => x.clone(),
None => {
return Err(DeployDataError::NoProfileUser(
self.profile_name.to_owned(),
self.node_name.to_owned(),
))
}
},
};
Ok(profile_user)
}
}
6 changes: 1 addition & 5 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ pub struct SshCommand<'a> {

impl<'a> SshCommand<'a> {
pub fn from_data(d: &'a data::DeployData) -> Result<Self, data::DeployDataError> {
let hostname = match d.hostname {
Some(x) => x,
None => &d.node.node_settings.hostname,
};
let hoststring = format!("{}@{}", &d.ssh_user, hostname);
let hoststring = format!("{}@{}", &d.ssh_user, d.hostname);
let opts = d.merged_settings.ssh_opts.as_ref();
Ok(SshCommand {hoststring, opts})
}
Expand Down
Loading

0 comments on commit 2f99eef

Please sign in to comment.