Skip to content

Commit

Permalink
feat: prepare HSM goup operations for next version
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelSopenaBallesteros committed Oct 14, 2024
1 parent 95acc81 commit 1c20314
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish = false # cargo
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mesa = "0.41.16"
mesa = "0.41.17"
# mesa = { path = "../mesa" } # Only for development purposes
hostlist-parser = "0.1.6"
strum = "0.25.0"
Expand Down
128 changes: 71 additions & 57 deletions src/cli/commands/add_nodes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::HashMap;

pub async fn exec(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
target_hsm_group_name: &str,
parent_hsm_group_name: &str,
target_hsm_name_vec: Vec<String>,
parent_hsm_name_vec: Vec<String>,
xname_requested_hostlist: &str,
nodryrun: bool,
create_hsm_group: bool,
Expand All @@ -12,13 +14,17 @@ pub async fn exec(
// NOTE: the list of HSM groups are the ones the user has access to and containing nodes within
// the hostlist input. Also, each HSM goup member list is also curated so xnames not in
// hostlist have been removed
let hsm_group_summary = crate::common::node_ops::get_curated_hsm_group_from_hostlist(
shasta_token,
shasta_base_url,
shasta_root_cert,
xname_requested_hostlist,
)
.await;
let mut hsm_group_summary: HashMap<String, Vec<String>> =
crate::common::node_ops::get_curated_hsm_group_from_hostlist(
shasta_token,
shasta_base_url,
shasta_root_cert,
xname_requested_hostlist,
)
.await;

// Keep HSM groups based on list of parent HSM groups provided
hsm_group_summary.retain(|hsm_name, _xname_vec| parent_hsm_name_vec.contains(hsm_name));

// Get list of xnames available
let mut xname_to_move_vec: Vec<&String> = hsm_group_summary
Expand All @@ -29,69 +35,77 @@ pub async fn exec(
xname_to_move_vec.sort();
xname_to_move_vec.dedup();

if mesa::hsm::group::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(&target_hsm_group_name.to_string()),
)
.await
.is_ok()
{
log::debug!("The HSM group {} exists, good.", target_hsm_group_name);
} else {
if create_hsm_group {
log::info!("HSM group {} does not exist, but the option to create the group has been selected, creating it now.", target_hsm_group_name.to_string());
if nodryrun {
mesa::hsm::group::http_client::create_new_hsm_group(
// Check if there are any xname to migrate/move and exit otherwise
if xname_to_move_vec.is_empty() {
println!("No hosts to move. Exit");
std::process::exit(0);
}

for target_hsm_name in target_hsm_name_vec {
if mesa::hsm::group::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(&target_hsm_name),
)
.await
.is_ok()
{
log::debug!("The HSM group {} exists, good.", target_hsm_name);
} else {
if create_hsm_group {
log::info!(
"HSM group {} does not exist, it will be created",
target_hsm_name
);
/* mesa::hsm::group::http_client::create_new_hsm_group(
shasta_token,
shasta_base_url,
shasta_root_cert,
target_hsm_group_name,
&target_hsm_name,
&[],
"false",
"",
&[],
)
.await
.expect("Unable to create new HSM group");
.expect("Unable to create new HSM group"); */
} else {
log::error!("Dry-run selected, cannot create the new group continue.");
log::error!("HSM group {} does not exist, but the option to create the group was NOT specificied, cannot continue.", target_hsm_name);
std::process::exit(1);
}
} else {
log::error!("HSM group {} does not exist, but the option to create the group was NOT specificied, cannot continue.", target_hsm_group_name.to_string());
std::process::exit(1);
}
}

let node_migration_rslt = mesa::hsm::group::utils::migrate_hsm_members(
shasta_token,
shasta_base_url,
shasta_root_cert,
target_hsm_group_name,
parent_hsm_group_name,
xname_to_move_vec
.iter()
.map(|xname| xname.as_str())
.collect(),
nodryrun,
)
.await;
for (parent_hsm_name, xname_to_move_vec) in &hsm_group_summary {
let node_migration_rslt = mesa::hsm::group::utils::migrate_hsm_members(
shasta_token,
shasta_base_url,
shasta_root_cert,
&target_hsm_name,
&parent_hsm_name,
xname_to_move_vec
.iter()
.map(|xname| xname.as_str())
.collect(),
nodryrun,
)
.await;

match node_migration_rslt {
Ok((mut target_hsm_group_member_vec, mut parent_hsm_group_member_vec)) => {
target_hsm_group_member_vec.sort();
parent_hsm_group_member_vec.sort();
println!(
"HSM '{}' members: {:?}",
target_hsm_group_name, target_hsm_group_member_vec
);
println!(
"HSM '{}' members: {:?}",
parent_hsm_group_name, parent_hsm_group_member_vec
);
match node_migration_rslt {
Ok((mut target_hsm_group_member_vec, mut parent_hsm_group_member_vec)) => {
target_hsm_group_member_vec.sort();
parent_hsm_group_member_vec.sort();
println!(
"HSM '{}' members: {:?}",
target_hsm_name, target_hsm_group_member_vec
);
println!(
"HSM '{}' members: {:?}",
parent_hsm_name, parent_hsm_group_member_vec
);
}
Err(e) => eprintln!("{}", e),
}
}
Err(e) => eprintln!("{}", e),
}
}
154 changes: 85 additions & 69 deletions src/cli/commands/migrate_nodes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::HashMap;

pub async fn exec(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
target_hsm_group_name: &str,
// parent_hsm_group_name: &str,
target_hsm_name_vec: Vec<String>,
parent_hsm_name_vec: Vec<String>,
xname_requested_hostlist: &str,
nodryrun: bool,
create_hsm_group: bool,
Expand All @@ -12,13 +14,17 @@ pub async fn exec(
// NOTE: the list of HSM groups are the ones the user has access to and containing nodes within
// the hostlist input. Also, each HSM goup member list is also curated so xnames not in
// hostlist have been removed
let hsm_group_summary = crate::common::node_ops::get_curated_hsm_group_from_hostlist(
shasta_token,
shasta_base_url,
shasta_root_cert,
xname_requested_hostlist,
)
.await;
let mut hsm_group_summary: HashMap<String, Vec<String>> =
crate::common::node_ops::get_curated_hsm_group_from_hostlist(
shasta_token,
shasta_base_url,
shasta_root_cert,
xname_requested_hostlist,
)
.await;

// Keep HSM groups based on list of parent HSM groups provided
hsm_group_summary.retain(|hsm_name, _xname_vec| parent_hsm_name_vec.contains(hsm_name));

// Get list of xnames available
let mut xname_to_move_vec: Vec<&String> = hsm_group_summary
Expand All @@ -29,79 +35,89 @@ pub async fn exec(
xname_to_move_vec.sort();
xname_to_move_vec.dedup();

// Check if there are any xname to migrate/move and exit otherwise
if xname_to_move_vec.is_empty() {
println!("No hosts to move. Exit");
std::process::exit(0);
}

log::info!("List of all nodes to work on per HSM group:");
for (hsm_group_name, hsm_group_members) in &hsm_group_summary {
log::info!("{}: {}", hsm_group_name, hsm_group_members.len());
}
log::debug!("xnames to move: {:?}", xname_to_move_vec);

if mesa::hsm::group::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(&target_hsm_group_name.to_string()),
)
.await
.is_ok()
{
log::debug!("The HSM group {} exists, good.", target_hsm_group_name);
} else {
if create_hsm_group {
log::info!("HSM group {} does not exist, but the option to create the group has been selected, creating it now.", target_hsm_group_name.to_string());
if nodryrun {
mesa::hsm::group::http_client::create_new_hsm_group(
shasta_token,
shasta_base_url,
shasta_root_cert,
target_hsm_group_name,
&[],
"false",
"",
&[],
)
.await
.expect("Unable to create new HSM group");
for target_hsm_name in target_hsm_name_vec {
if mesa::hsm::group::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(&target_hsm_name),
)
.await
.is_ok()
{
log::debug!("The HSM group {} exists, good.", target_hsm_name);
} else {
if create_hsm_group {
log::info!(
"HSM group {} does not exist, it will be created",
target_hsm_name
);
if nodryrun {
/* mesa::hsm::group::http_client::create_new_hsm_group(
shasta_token,
shasta_base_url,
shasta_root_cert,
&target_hsm_name,
&[],
"false",
"",
&[],
)
.await
.expect("Unable to create new HSM group"); */
} else {
log::error!("Dry-run selected, cannot create the new group continue.");
std::process::exit(1);
}
} else {
log::error!("Dry-run selected, cannot create the new group continue.");
log::error!("HSM group {} does not exist, but the option to create the group was NOT specificied, cannot continue.", target_hsm_name);
std::process::exit(1);
}
} else {
log::error!("HSM group {} does not exist, but the option to create the group was NOT specificied, cannot continue.", target_hsm_group_name.to_string());
std::process::exit(1);
}
}

// Migrate nodes
for (parent_hsm_group_name, parent_members) in hsm_group_summary {
println!(
"DEBUG - migrate nodes {:?} from '{}' to '{}'",
parent_members, parent_hsm_group_name, target_hsm_group_name
);
let node_migration_rslt = mesa::hsm::group::utils::migrate_hsm_members(
shasta_token,
shasta_base_url,
shasta_root_cert,
target_hsm_group_name,
&parent_hsm_group_name,
parent_members.iter().map(|xname| xname.as_str()).collect(),
nodryrun,
)
.await;
// Migrate nodes
for (parent_hsm_name, xname_to_move_vec) in &hsm_group_summary {
let node_migration_rslt = mesa::hsm::group::utils::migrate_hsm_members(
shasta_token,
shasta_base_url,
shasta_root_cert,
&target_hsm_name,
&parent_hsm_name,
xname_to_move_vec
.iter()
.map(|xname| xname.as_str())
.collect(),
nodryrun,
)
.await;

match node_migration_rslt {
Ok((mut target_hsm_group_member_vec, mut parent_hsm_group_member_vec)) => {
target_hsm_group_member_vec.sort();
parent_hsm_group_member_vec.sort();
println!(
"HSM '{}' members: {:?}",
target_hsm_group_name, target_hsm_group_member_vec
);
println!(
"HSM '{}' members: {:?}",
parent_hsm_group_name, parent_hsm_group_member_vec
);
match node_migration_rslt {
Ok((mut target_hsm_group_member_vec, mut parent_hsm_group_member_vec)) => {
target_hsm_group_member_vec.sort();
parent_hsm_group_member_vec.sort();
println!(
"HSM '{}' members: {:?}",
target_hsm_name, target_hsm_group_member_vec
);
println!(
"HSM '{}' members: {:?}",
parent_hsm_name, parent_hsm_group_member_vec
);
}
Err(e) => eprintln!("{}", e),
}
Err(e) => eprintln!("{}", e),
}
}
}
Loading

0 comments on commit 1c20314

Please sign in to comment.