Skip to content

Commit

Permalink
refactor: apply clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Sopena Ballesteros committed Jan 23, 2024
1 parent 897e549 commit 06d37bb
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 91 deletions.
17 changes: 4 additions & 13 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,33 +648,24 @@ pub fn subcommand_update_hsm_group(hsm_group: Option<&String>) -> Command {
}

pub fn subcommand_migrate_backup() -> Command {
let migrate_backup = Command::new("backup")
Command::new("backup")
.aliases(["mb"])
.arg_required_else_help(true)
.about("Backup the configuration (BOS, CFS, image and HSM group) of a given vCluster/BOS session template.")
.arg(arg!(-b --"bos" <SESSIONTEMPLATE> "BOS Sessiontemplate to use to derive CFS, boot parameters and HSM group"))
.arg(arg!(-d --"destination" <FOLDER> "Destination folder to store the backup on"));

migrate_backup
.arg(arg!(-d --"destination" <FOLDER> "Destination folder to store the backup on"))
}

pub fn subcommand_migrate_restore() -> Command {
let migrate_restore = Command::new("restore")
Command::new("restore")
.aliases(["mr"])
.arg_required_else_help(true)
.about("MIGRATE RESTORE of all the nodes in a HSM group. Boot configuration means updating the image used to boot the machine. Configuration of a node means the CFS configuration with the ansible scripts running once a node has been rebooted.\neg:\nmanta update hsm-group --boot-image <boot cfs configuration name> --desired-configuration <desired cfs configuration name>")
.arg(arg!(-b --"bos-file" <BOS_session_template_file> "BOS session template of the cluster backed previously with migrate backup"))
.arg(arg!(-c --"cfs-file" <CFS_configuration_file> "CFS session template of the cluster backed previously with migrate backup"))
.arg(arg!(-j --"hsm-file" <HSM_group_description_file> "HSM group description file of the cluster backed previously with migrate backup"))
.arg(arg!(-m --"ims-file" <IMS_file> "IMS file backed previously with migrate backup"))
.arg(arg!(-i --"image-dir" <IMAGE_path> "Path where the image files are stored."));

// migrate_restore = match hsm_group {
// Some(_) => update_hsm_group,
// None => update_hsm_group.arg(arg!(<HSM_GROUP_NAME> "HSM group name").required(true)),
// };

migrate_restore
.arg(arg!(-i --"image-dir" <IMAGE_path> "Path where the image files are stored."))
}

pub fn subcommand_power() -> Command {
Expand Down
1 change: 0 additions & 1 deletion src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ pub mod remove_hw_component_cluster;
pub mod remove_nodes;
pub mod update_hsm_group;
pub mod update_node;

4 changes: 2 additions & 2 deletions src/cli/commands/add_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub async fn exec(
xname_string: &str,
) {
let new_target_hsm_members = xname_string
.split(",")
.split(',')
.map(|xname| xname.trim())
.collect::<Vec<&str>>();

Expand Down Expand Up @@ -39,7 +39,7 @@ pub async fn exec(
// merge HSM group list with the list of xnames provided by the user
target_hsm_group_member_vec.extend(
xname_string
.split(",")
.split(',')
.map(|xname| xname.trim().to_string())
.collect::<Vec<String>>(),
);
Expand Down
28 changes: 9 additions & 19 deletions src/cli/commands/apply_hw_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,8 @@ pub mod utils {
let combined_target_parent_hsm_hw_component_type_scores_based_on_scarcity_hashmap: HashMap<
String,
f32,
> = calculate_scarcity_scores(
&combined_target_parent_hsm_node_hw_component_count_vec,
)
.await;
> = calculate_scarcity_scores(&combined_target_parent_hsm_node_hw_component_count_vec)
.await;

// *********************************************************************************************************
// CALCULATE FINAL HSM SUMMARY COUNTERS AFTER REMOVING THE NODES THAT NEED TO GO TO TARGET
Expand All @@ -291,9 +289,7 @@ pub mod utils {
let hw_component_counters_to_move_out_from_combined_hsm =
crate::cli::commands::apply_hw_cluster::utils::downscale_from_final_hsm_group(
&final_combined_target_parent_hsm_hw_component_summary.clone(),
&final_combined_target_parent_hsm_hw_component_summary
.into_iter()
.map(|(hw_component, _)| hw_component)
&final_combined_target_parent_hsm_hw_component_summary.into_keys()
.collect::<Vec<String>>(),
&mut combined_target_parent_hsm_node_hw_component_count_vec,
&combined_target_parent_hsm_hw_component_type_scores_based_on_scarcity_hashmap,
Expand Down Expand Up @@ -606,18 +602,14 @@ pub mod utils {
} */

pub async fn calculate_scarcity_scores(
hsm_node_hw_component_count: &Vec<(
String,
HashMap<String, usize>,
)>,
hsm_node_hw_component_count: &Vec<(String, HashMap<String, usize>)>,
) -> HashMap<String, f32> {
let total_num_nodes = hsm_node_hw_component_count.len();

let mut hw_component_vec: Vec<&String> =
hsm_node_hw_component_count
.iter()
.flat_map(|(_, hw_component_counter_hashmap)| hw_component_counter_hashmap.keys())
.collect();
let mut hw_component_vec: Vec<&String> = hsm_node_hw_component_count
.iter()
.flat_map(|(_, hw_component_counter_hashmap)| hw_component_counter_hashmap.keys())
.collect();

hw_component_vec.sort();
hw_component_vec.dedup();
Expand All @@ -626,9 +618,7 @@ pub mod utils {
for hw_component in hw_component_vec {
let mut node_count = 0;

for (_, hw_component_counter_hashmap) in
hsm_node_hw_component_count
{
for (_, hw_component_counter_hashmap) in hsm_node_hw_component_count {
if hw_component_counter_hashmap.contains_key(hw_component) {
node_count += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/apply_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub async fn check_nodes_are_ready_to_run_cfs_configuration_and_run_cfs_session(
shasta_token,
shasta_base_url,
shasta_root_cert,
&vec![xname.clone()],
&[xname.clone()],
)
.await?;

Expand Down
1 change: 0 additions & 1 deletion src/cli/commands/get_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub async fn exec(
output_opt: Option<&String>,
status: bool,
) {

// Take all nodes for all hsm_groups found and put them in a Vec
let mut hsm_groups_node_list: Vec<String> =
hsm::group::shasta::utils::get_member_vec_from_hsm_name_vec(
Expand Down
29 changes: 13 additions & 16 deletions src/cli/commands/migrate_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ pub async fn exec(
let dest_path = Path::new(destination.unwrap());
let bucket_name = "boot-images";
let files2download = ["manifest.json", "initrd", "kernel", "rootfs"];
let countfiles2download = files2download.len() + 3; // BOS + CFS + HSM
// let files2download = ["manifest.json"];

log::debug!("Create directory '{}'", destination.unwrap());
match std::fs::create_dir_all(dest_path) {
Ok(_ok) => _ok,
Err(error) => panic!(
"Unable to create directory {}. Error returned: {}",
&dest_path.to_string_lossy(),
error.to_string()
error
),
};

Expand Down Expand Up @@ -146,7 +144,7 @@ pub async fn exec(
download_counter += 1;

// Image ----------------------------------------------------------------------------------
for (_boot_sets_param, boot_sets_value) in bos_templates[0].boot_sets.as_ref().unwrap() {
for boot_sets_value in bos_templates[0].boot_sets.as_ref().unwrap().values() {
if let Some(path) = &boot_sets_value.path {
let image_id_related_to_bos_sessiontemplate = path
.trim_start_matches("s3://boot-images/")
Expand Down Expand Up @@ -175,7 +173,7 @@ pub async fn exec(
);

let sts_value =
match s3_auth(&shasta_token, &shasta_base_url, &shasta_root_cert).await {
match s3_auth(shasta_token, shasta_base_url, shasta_root_cert).await {
Ok(sts_value) => {
log::debug!("Debug - STS token:\n{:#?}", sts_value);
sts_value
Expand All @@ -194,17 +192,16 @@ pub async fn exec(
&download_counter,
&files2download.len() + 2
);
let _result =
match s3_download_object(&sts_value, &src, &bucket_name, &dest).await {
Ok(_result) => {
download_counter = download_counter + 1;
}
Err(error) => panic!(
"Unable to download file {} from s3. Error returned: {}",
&src,
error.to_string()
),
};

match s3_download_object(&sts_value, &src, bucket_name, &dest).await {
Ok(_result) => {
download_counter += 1;
}
Err(error) => panic!(
"Unable to download file {} from s3. Error returned: {}",
&src, error
),
};
}
} else {
panic!(
Expand Down
57 changes: 27 additions & 30 deletions src/cli/commands/migrate_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ pub async fn exec(
artifacts: vec![],
};

let backup_ims_file = ims_file.clone().unwrap().to_string();
let backup_cfs_file = cfs_file.clone().unwrap().to_string();
let backup_bos_file = bos_file.clone().unwrap().to_string();
let backup_hsm_file = hsm_file.clone().unwrap().to_string();
let backup_ims_file = ims_file.unwrap().to_string();
let backup_cfs_file = cfs_file.unwrap().to_string();
let backup_bos_file = bos_file.unwrap().to_string();
let backup_hsm_file = hsm_file.unwrap().to_string();

let ims_image_name: String = get_image_name_from_ims_file(&backup_ims_file);
println!("\tImage name: {}", ims_image_name);
Expand Down Expand Up @@ -114,9 +114,9 @@ pub async fn exec(
// Do we have another image with this name?
println!("Registering new image with IMS...");
let ims_image_id: String = ims_register_image(
&shasta_token,
&shasta_base_url,
&shasta_root_cert,
shasta_token,
shasta_base_url,
shasta_root_cert,
&ims_image_name,
)
.await;
Expand Down Expand Up @@ -224,7 +224,7 @@ async fn create_bos_sessiontemplate(
// This is as ugly as it can be
// println!("Path: {}", bos_sessiontemplate.clone().boot_sets.clone().unwrap().compute.clone().unwrap().path.clone().unwrap().to_string());
let path_modified = format!("s3://boot-images/{}/manifest.json", ims_image_id);
let bos_sessiontemplate_modified = bos_sessiontemplate
bos_sessiontemplate
.boot_sets
.as_mut()
.unwrap()
Expand All @@ -234,10 +234,7 @@ async fn create_bos_sessiontemplate(
.path = Some(path_modified);

log::debug!("BOS sessiontemplate loaded:\n{:#?}", bos_sessiontemplate);
log::debug!(
"BOS sessiontemplate modified:\n{:#?}",
&bos_sessiontemplate_modified
);
log::debug!("BOS sessiontemplate modified:\n{:#?}", &bos_sessiontemplate);

match bos::template::shasta::http_client::post(
shasta_token,
Expand Down Expand Up @@ -312,7 +309,7 @@ async fn create_cfs_config(
shasta_base_url,
shasta_root_cert,
&cfs_config,
&cfs_config_name.as_str(),
cfs_config_name.as_str(),
)
.await
{
Expand All @@ -338,10 +335,11 @@ async fn ims_update_image_add_manifest(
ims_image_name: &String,
ims_image_id: &String,
) {
match mesa::ims::image::shasta::http_client::get(&shasta_token,
&shasta_base_url,
&shasta_root_cert,
Some(ims_image_name)
match mesa::ims::image::shasta::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(ims_image_name)
).await {
Ok(_vector) => {
if _vector.is_empty() {
Expand Down Expand Up @@ -433,7 +431,7 @@ async fn s3_upload_image_artifacts(
};

for file in vec_image_files {
let filename = Path::new(file).file_name().clone().unwrap();
let filename = Path::new(file).file_name().unwrap();
let file_size = match fs::metadata(file) {
Ok(_file_metadata) => {
let res: String = humansize::format_size(_file_metadata.len(), DECIMAL);
Expand Down Expand Up @@ -550,7 +548,6 @@ fn file_md5sum(filename: PathBuf) -> Digest {
let buf_len = len.min(100_000_000) as usize;
let mut buf = BufReader::with_capacity(buf_len, f);
let mut context = md5::Context::new();
let mut i = 0;
let bar = ProgressBar::new(len / buf_len as u64);

loop {
Expand All @@ -565,7 +562,6 @@ fn file_md5sum(filename: PathBuf) -> Digest {
// Tell the buffer that the chunk is consumed
let part_len = part.len();
buf.consume(part_len);
i += 1;
bar.inc(1);
// println!("Consumed {} out of {}; step {}/{}",
// humansize::format_size(part_len, DECIMAL),
Expand All @@ -585,7 +581,7 @@ fn calculate_image_checksums(
vec_backup_image_files: &Vec<String>,
) {
for file in vec_backup_image_files {
let file_size = match fs::metadata(&file) {
let file_size = match fs::metadata(file) {
Ok(_file_metadata) => {
let res: String = humansize::format_size(_file_metadata.len(), DECIMAL);
res
Expand Down Expand Up @@ -654,11 +650,12 @@ async fn ims_register_image(
link: None,
arch: None,
};
let list_images_with_same_name = match mesa::ims::image::mesa::http_client::get(&shasta_token,
&shasta_base_url,
&shasta_root_cert,
Some(ims_image_name)
).await {
let list_images_with_same_name = match mesa::ims::image::mesa::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(ims_image_name)
).await {
Ok(vector) => vector,
Err(error) => panic!("Error: Unable to determine if there are other images in IMS with the name {}. Error code: {}", &ims_image_name, &error),
};
Expand All @@ -678,9 +675,9 @@ async fn ims_register_image(
}

let json_response = match register_new_image(
&shasta_token,
&shasta_base_url,
&shasta_root_cert,
shasta_token,
shasta_base_url,
shasta_root_cert,
&ims_record,
)
.await
Expand All @@ -689,7 +686,7 @@ async fn ims_register_image(
Err(error) => panic!(
"Error: Unable to register a new image {} into IMS {}",
&ims_image_name.to_string(),
error.to_string()
error
),
};
json_response["id"].to_string().replace('"', "")
Expand Down
6 changes: 1 addition & 5 deletions src/cli/commands/remove_hw_component_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ pub async fn exec(
]
.concat();

let combined_target_parent_hsm_hw_component_summary_hashmap =
calculate_hsm_hw_component_summary(&combined_target_parent_hsm_node_hw_component_count_vec);

// *********************************************************************************************************
// CALCULATE HW COMPONENT TYPE SCORE BASED ON SCARCITY

Expand Down Expand Up @@ -193,8 +190,7 @@ pub async fn exec(
crate::cli::commands::apply_hw_cluster::utils::downscale_from_final_hsm_group(
&final_target_hsm_hw_component_summary.clone(),
&final_target_hsm_hw_component_summary
.into_iter()
.map(|(hw_component, _)| hw_component)
.into_keys()
.collect::<Vec<String>>(),
&mut target_hsm_node_hw_component_count_vec,
&combined_target_parent_hsm_hw_component_type_scores_based_on_scarcity_hashmap,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/remove_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub async fn exec(
xname_string: &str,
) {
let new_target_hsm_members = xname_string
.split(",")
.split(',')
.map(|xname| xname.trim())
.collect::<Vec<&str>>();

Expand Down
5 changes: 3 additions & 2 deletions src/cli/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,9 @@ pub async fn process_cli(
cfs_file,
hsm_file,
ims_file,
image_dir
).await;
image_dir,
)
.await;
}
} else if let Some(cli_migrate) = cli_root.subcommand_matches("migrate") {
if let Some(cli_migrate) = cli_migrate.subcommand_matches("backup") {
Expand Down

0 comments on commit 06d37bb

Please sign in to comment.