From 8e895f3ac290136ca13d3f9ae40a6bac61c6811a Mon Sep 17 00:00:00 2001 From: Manuel Sopena Ballesteros Date: Thu, 16 Nov 2023 12:50:57 +0100 Subject: [PATCH] feat: Duplicate get nodes subcommand into get cluster feat: Get cluster status --- src/cli/build.rs | 26 +++++++- src/cli/commands/get_nodes.rs | 31 +++++++++- src/cli/process.rs | 113 ++++++++++++++++++++++++++-------- 3 files changed, 142 insertions(+), 28 deletions(-) diff --git a/src/cli/build.rs b/src/cli/build.rs index c1e636a6..55066345 100644 --- a/src/cli/build.rs +++ b/src/cli/build.rs @@ -235,10 +235,31 @@ pub fn subcommand_get_bos_template(hsm_group: Option<&String>) -> Command { get_bos_template } +pub fn subcommand_get_cluster_details(hsm_group: Option<&String>) -> Command { + let mut get_node = Command::new("cluster") + .aliases(["C", "clstr"]) + .about("Get cluster details") + .arg(arg!(-n --"nids-only-one-line" "Prints nids in one line eg nidxxxxxx,nidyyyyyy,nidzzzzzz,...")) + .arg(arg!(-x --"xnames-only-one-line" "Prints xnames in one line eg x1001c1s5b0n0,x1001c1s5b0n1,...")) + .arg(arg!(-s --"status" "Get cluster status:\n - OK: All nodes are operational (booted and configured)\n - ON: At least one node is not yet booted\n - STANDBY: At lesat one node's heartbeat is lost\n - UNCONFIGURED: All nodes are booted but at least one of them is being configured\n - FAILED: At least one node configuration failed")) + .arg(arg!(-o --output "Output format. If missing it will print output data in human redeable (tabular) format").value_parser(["json"])); + + match hsm_group { + None => { + get_node = get_node + .arg_required_else_help(true) + .arg(arg!( "hsm group name")) + } + Some(_) => {} + } + + get_node +} + pub fn subcommand_get_node(hsm_group: Option<&String>) -> Command { let mut get_node = Command::new("nodes") .aliases(["n", "node", "nd"]) - .about("Get members of a HSM group") + .about("This command will be DEPRECATED in manta v1.15.0. the new command to use will be replaced by 'manta get cluster'. Get members of a HSM group") .arg(arg!(-n --"nids-only-one-line" "Prints nids in one line eg nidxxxxxx,nidyyyyyy,nidzzzzzz,...")) .arg(arg!(-x --"xnames-only-one-line" "Prints xnames in one line eg x1001c1s5b0n0,x1001c1s5b0n1,...")) .arg(arg!(-o --output "Output format. If missing it will print output data in human redeable (tabular) format").value_parser(["json"])); @@ -258,7 +279,7 @@ pub fn subcommand_get_node(hsm_group: Option<&String>) -> Command { pub fn subcommand_get_hsm_groups_details(hsm_group: Option<&String>) -> Command { let mut get_hsm_group = Command::new("hsm-groups") .aliases(["h", "hg", "hsm", "hsmgrps"]) - .about("Get HSM groups details"); + .about("This command will be DEPRECATED in manta v1.15.0. the new command to use will be called 'manta get cluster' and the output will be similar to manta get nodes. Get HSM groups details"); match hsm_group { None => { @@ -303,6 +324,7 @@ pub fn subcommand_get(hsm_group: Option<&String>) -> Command { .subcommand(subcommand_get_cfs_configuration(hsm_group)) .subcommand(subcommand_get_bos_template(hsm_group)) .subcommand(subcommand_get_node(hsm_group)) + .subcommand(subcommand_get_cluster_details(hsm_group)) .subcommand(subcommand_get_hsm_groups_details(hsm_group)) .subcommand(subcommand_get_images(hsm_group)) } diff --git a/src/cli/commands/get_nodes.rs b/src/cli/commands/get_nodes.rs index edeafa28..91b65d87 100644 --- a/src/cli/commands/get_nodes.rs +++ b/src/cli/commands/get_nodes.rs @@ -13,6 +13,7 @@ pub async fn exec( silent: bool, silent_xname: bool, 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 = hsm::utils::get_member_vec_from_hsm_name_vec( @@ -33,7 +34,35 @@ pub async fn exec( ) .await; - if silent { + if status { + let status_output = if node_details_list.iter().any(|node_details| { + node_details + .configuration_status + .eq_ignore_ascii_case("failed") + }) { + "FAILED" + } else if node_details_list + .iter() + .any(|node_details| node_details.power_status.eq_ignore_ascii_case("standby")) + { + "STANDBY" + }else if node_details_list + .iter() + .any(|node_details| node_details.power_status.eq_ignore_ascii_case("on")) + { + "ON" + } else if !node_details_list.iter().any(|node_details| { + node_details + .configuration_status + .eq_ignore_ascii_case("configured") + }) { + "UNCONFIGURED" + } else { + "OK" + }; + + println!("{}", status_output); + } else if silent { let node_nid_list = node_details_list .iter() .map(|node_details| node_details.nid.clone()) diff --git a/src/cli/process.rs b/src/cli/process.rs index eb01bd67..7b5e9b9a 100644 --- a/src/cli/process.rs +++ b/src/cli/process.rs @@ -19,11 +19,11 @@ use crate::{ }; use super::commands::{ - self, apply_cluster, apply_ephemeral_env, apply_image, apply_node_off, apply_node_on, - apply_node_reset, apply_session, config_set_hsm, config_set_log, config_set_site, config_show, - config_unset_auth, config_unset_hsm, console_cfs_session_image_target_ansible, console_node, - get_configuration, get_hsm, get_images, get_nodes, get_session, get_template, update_hsm_group, - update_node, apply_configuration, + self, apply_cluster, apply_configuration, apply_ephemeral_env, apply_image, apply_node_off, + apply_node_on, apply_node_reset, apply_session, config_set_hsm, config_set_log, + config_set_site, config_show, config_unset_auth, config_unset_hsm, + console_cfs_session_image_target_ansible, console_node, get_configuration, get_hsm, get_images, + get_nodes, get_session, get_template, update_hsm_group, update_node, }; pub async fn process_cli( @@ -119,7 +119,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_get_configuration.get_one::("hsm-group"), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_get_configuration.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -144,7 +148,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_get_session.get_one::("hsm-group"), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_get_session.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -176,7 +184,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_get_template.get_one::("hsm-group"), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_get_template.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -200,12 +212,45 @@ pub async fn process_cli( cli_get_template.get_one::("limit"), ) .await; + } else if let Some(cli_get_node) = cli_get.subcommand_matches("cluster") { + let hsm_group_target_vec = validate_target_hsm_name( + shasta_token, + shasta_base_url, + shasta_root_cert, + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_get_node.get_one::("HSM_GROUP_NAME") + }, + hsm_group_name_opt, + ) + .await; + + get_nodes::exec( + shasta_token, + shasta_base_url, + shasta_root_cert, + &hsm_group_target_vec, + *cli_get_node + .get_one::("nids-only-one-line") + .unwrap_or(&false), + *cli_get_node + .get_one::("xnames-only-one-line") + .unwrap_or(&false), + cli_get_node.get_one::("output"), + *cli_get_node.get_one::("status").unwrap_or(&false), + ) + .await; } else if let Some(cli_get_node) = cli_get.subcommand_matches("nodes") { let hsm_group_target_vec = validate_target_hsm_name( shasta_token, shasta_base_url, shasta_root_cert, - cli_get_node.get_one::("HSM_GROUP_NAME"), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_get_node.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -222,6 +267,7 @@ pub async fn process_cli( .get_one::("xnames-only-one-line") .unwrap_or(&false), cli_get_node.get_one::("output"), + false, ) .await; } else if let Some(cli_get_hsm_groups) = cli_get.subcommand_matches("hsm-groups") { @@ -229,7 +275,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_get_hsm_groups.get_one::("HSM_GROUP_NAME"), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_get_hsm_groups.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -246,7 +296,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_get_images.get_one::("hsm-group"), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_get_images.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -266,14 +320,17 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_apply_configuration - .try_get_one::("hsm-group") - .unwrap_or(None), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_apply_configuration.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; - let tag = if let Some(input_tag) = cli_apply_configuration.get_one::("tag") { + let tag = if let Some(input_tag) = cli_apply_configuration.get_one::("tag") + { input_tag.clone() } else { chrono::Utc::now().format("%Y%m%d%H%M%S").to_string() @@ -293,7 +350,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_apply_session.get_one::("HSM_GROUP_NAME"), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_apply_session.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -346,9 +407,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_apply_image - .try_get_one::("hsm-group") - .unwrap_or(None), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_apply_image.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -395,9 +458,11 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_apply_cluster - .try_get_one::("hsm-group") - .unwrap_or(None), + if let Some(hsm_group_name) = hsm_group_name_opt { + Some(hsm_group_name) + } else { + cli_apply_cluster.get_one::("HSM_GROUP_NAME") + }, hsm_group_name_opt, ) .await; @@ -557,9 +622,7 @@ pub async fn process_cli( shasta_token, shasta_base_url, shasta_root_cert, - cli_log - .try_get_one::("hsm-group") - .unwrap_or(None), + cli_log.try_get_one::("hsm-group").unwrap_or(None), hsm_group_name_opt, ) .await;