Skip to content

Commit f3a60b4

Browse files
committed
Refactor create deploy.rs views into data.rs owned data
1 parent 390aea6 commit f3a60b4

File tree

4 files changed

+297
-187
lines changed

4 files changed

+297
-187
lines changed

src/cli.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,14 @@ async fn run_deploy(
235235
// Rollbacks adhere to the global seeting to auto_rollback and secondary
236236
// the profile's configuration
237237
for (deploy_data, deploy_defs) in &parts {
238-
if let Err(e) = deploy::deploy::deploy_profile(deploy_data, deploy_defs, cmd_flags.dry_activate).await
238+
if let Err(e) = deploy::deploy::deploy_profile(
239+
&deploy_data.node_name,
240+
&deploy_data.profile_name,
241+
deploy::deploy::SshCommand::from_data(&deploy_data)?,
242+
deploy::deploy::ActivateCommand::from_data(&deploy_data),
243+
deploy::deploy::WaitCommand::from_data(&deploy_data),
244+
deploy::deploy::ConfirmCommand::from_data(&deploy_data),
245+
).await
239246
{
240247
error!("{}", e);
241248
if cmd_flags.dry_activate {
@@ -246,9 +253,14 @@ async fn run_deploy(
246253
// revoking all previous deploys
247254
// (adheres to profile configuration if not set explicitely by
248255
// the command line)
249-
for (deploy_data, deploy_defs) in &succeeded {
256+
for (deploy_data, _) in &succeeded {
250257
if deploy_data.merged_settings.auto_rollback.unwrap_or(true) {
251-
deploy::deploy::revoke(*deploy_data, *deploy_defs).await?;
258+
deploy::deploy::revoke(
259+
&deploy_data.node_name,
260+
&deploy_data.profile_name,
261+
deploy::deploy::SshCommand::from_data(&deploy_data)?,
262+
deploy::deploy::RevokeCommand::from_data(&deploy_data),
263+
).await?;
252264
}
253265
}
254266
}

src/data.rs

+71-15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub enum ResolveTargetError {
3434
ProfileNotFound(String, String, String),
3535
#[error("Profile was provided without a node name for repo `{0}`")]
3636
ProfileWithoutNode(String),
37+
#[error("Deployment data invalid: {0}")]
38+
InvalidDeployDataError(#[from] DeployDataError),
3739
}
3840

3941
impl<'a> Target {
@@ -70,7 +72,7 @@ impl<'a> Target {
7072
node_,
7173
profile_,
7274
hostname,
73-
);
75+
)?;
7476
vec![d]
7577
})
7678
} else {
@@ -257,12 +259,18 @@ pub struct DeployData<'a> {
257259
pub repo: String,
258260
pub node_name: String,
259261
pub profile_name: String,
260-
pub node: &'a settings::Node,
261-
pub profile: &'a settings::Profile,
262+
262263
pub hostname: Option<&'a str>,
263264

264265
pub flags: &'a Flags,
266+
pub node: &'a settings::Node,
267+
pub profile: &'a settings::Profile,
265268
pub merged_settings: settings::GenericSettings,
269+
270+
pub ssh_user: String,
271+
pub temp_path: String,
272+
pub profile_path: String,
273+
pub sudo: Option<String>,
266274
}
267275

268276
#[derive(Error, Debug)]
@@ -330,7 +338,7 @@ impl<'a> DeployData<'a> {
330338
node: &'a settings::Node,
331339
profile: &'a settings::Profile,
332340
hostname: Option<&'a str>,
333-
) -> DeployData<'a> {
341+
) -> Result<DeployData<'a>, DeployDataError> {
334342
let mut merged_settings = cmd_settings.clone();
335343
merged_settings.merge(profile.generic_settings.clone());
336344
merged_settings.merge(node.generic_settings.clone());
@@ -339,33 +347,59 @@ impl<'a> DeployData<'a> {
339347
// if let Some(ref ssh_opts) = cmd_overrides.ssh_opts {
340348
// merged_settings.ssh_opts = ssh_opts.split(' ').map(|x| x.to_owned()).collect();
341349
// }
350+
let temp_path = match merged_settings.temp_path {
351+
Some(ref x) => x.to_owned(),
352+
None => "/tmp".to_string(),
353+
};
354+
let profile_user = match merged_settings.user {
355+
Some(ref x) => x.to_owned(),
356+
None => match merged_settings.ssh_user {
357+
Some(ref x) => x.to_owned(),
358+
None => {
359+
return Err(DeployDataError::NoProfileUser(profile_name, node_name))
360+
}
361+
},
362+
};
363+
let profile_path = match profile.profile_settings.profile_path {
364+
None => format!("/nix/var/nix/profiles/{}", match &profile_user[..] {
365+
"root" => profile_name.to_owned(),
366+
_ => format!("per-user/{}/{}", profile_user, profile_name),
367+
}),
368+
Some(ref x) => x.to_owned(),
369+
};
370+
let ssh_user = match merged_settings.ssh_user {
371+
Some(ref u) => u.to_owned(),
372+
None => whoami::username(),
373+
};
374+
let sudo = match merged_settings.user {
375+
Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)),
376+
_ => None,
377+
};
342378

343-
DeployData {
379+
Ok(DeployData {
344380
repo,
345381
node_name,
346382
profile_name,
347383
node,
348384
profile,
349385
hostname,
386+
ssh_user,
387+
temp_path,
388+
profile_path,
389+
sudo,
350390
flags,
351391
merged_settings,
352-
}
392+
})
353393
}
354394

355395
pub fn defs(&'a self) -> Result<DeployDefs, DeployDataError> {
356396
let ssh_user = match self.merged_settings.ssh_user {
357397
Some(ref u) => u.clone(),
358398
None => whoami::username(),
359399
};
360-
361400
let profile_user = self.get_profile_user()?;
362-
363401
let profile_path = self.get_profile_path()?;
364-
365-
let sudo: Option<String> = match self.merged_settings.user {
366-
Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)),
367-
_ => None,
368-
};
402+
let sudo = self.sudo()?;
369403

370404
Ok(DeployDefs {
371405
ssh_user,
@@ -375,6 +409,28 @@ impl<'a> DeployData<'a> {
375409
})
376410
}
377411

412+
pub fn sudo(&'a self) -> Result<Option<String>, DeployDataError> {
413+
let ssh_user = match self.merged_settings.ssh_user {
414+
Some(ref u) => u.clone(),
415+
None => whoami::username(),
416+
};
417+
Ok(
418+
match self.merged_settings.user {
419+
Some(ref user) if user != &ssh_user => Some(format!("sudo -u {}", user)),
420+
_ => None,
421+
}
422+
)
423+
}
424+
425+
pub fn temp_path(&'a self) -> Result<String, DeployDataError> {
426+
Ok(
427+
match self.merged_settings.temp_path {
428+
Some(ref x) => x.to_owned(),
429+
None => "/tmp".to_string(),
430+
}
431+
)
432+
}
433+
378434
pub fn ssh_uri(&'a self) -> Result<String, DeployDataError> {
379435

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

407-
pub fn ssh_opts(&'a self) -> impl Iterator<Item = &String> {
408-
self.merged_settings.ssh_opts.iter()
463+
pub fn ssh_opts(&'a self) -> Result<impl Iterator<Item = &String>, DeployDataError> {
464+
Ok(self.merged_settings.ssh_opts.iter())
409465
}
410466

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

0 commit comments

Comments
 (0)