Skip to content

Commit

Permalink
feat: command 'get nodes' now accepts nids
Browse files Browse the repository at this point in the history
  • Loading branch information
Masber committed Jan 29, 2025
1 parent 98a5866 commit e0fa0f0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
21 changes: 11 additions & 10 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn subcommand_get_cluster_details() -> Command {
.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 - OFF: At least one node is OFF\n - ON: No nodes OFF and at least one is ON\n - STANDBY: At least one node's heartbeat is lost\n - UNCONFIGURED: All nodes are READY but at least one of them is being configured\n - FAILED: At least one node configuration failed"))
.arg(arg!(-s --status "Get cluster status:\n - OK: All nodes are operational (booted and configured)\n - OFF: At least one node is OFF\n - ON: No nodes OFF and at least one is ON\n - STANDBY: At least one node's heartbeat is lost\n - UNCONFIGURED: All nodes are READY but at least one of them is being configured\n - FAILED: At least one node configuration failed"))
.arg(arg!(-o --output <FORMAT> "Output format. If missing it will print output data in human readable (table) format").value_parser(["table", "table-wide", "json", "summary"]).default_value("table"))
.arg_required_else_help(true)
.arg(arg!(<HSM_GROUP_NAME> "hsm group name"))
Expand All @@ -244,10 +244,11 @@ pub fn subcommand_get_node_details() -> Command {
.visible_aliases(["n", "node", "nd"])
.about("Get node details")
.arg(arg!(-n --"nids-only-one-line" "Prints nids in one line eg nidxxxxxx,nidyyyyyy,nidzzzzzz,..."))
.arg(arg!(-s --"status" "Get cluster status:\n - OK: All nodes are operational (booted and configured)\n - OFF: At least one node is OFF\n - ON: No nodes OFF and at least one is ON\n - STANDBY: At least one node's heartbeat is lost\n - UNCONFIGURED: All nodes are READY but at least one of them is being configured\n - FAILED: At least one node configuration failed"))
.arg(arg!(-s --status "Get cluster status:\n - OK: All nodes are operational (booted and configured)\n - OFF: At least one node is OFF\n - ON: No nodes OFF and at least one is ON\n - STANDBY: At least one node's heartbeat is lost\n - UNCONFIGURED: All nodes are READY but at least one of them is being configured\n - FAILED: At least one node configuration failed"))
.arg(arg!(-r --regex "Input nodes in regex format.").action(ArgAction::SetTrue))
.arg(arg!(-o --output <FORMAT> "Output format. If missing it will print output data in human readable (table) format").value_parser(["table", "table-wide", "json", "summary"]).default_value("table"))
.arg_required_else_help(true)
.arg(arg!(<XNAMES> "Comma separated list of xnames to retreive the kernel parameters from.\neg: 'x1001c1s0b0n1,x1001c1s0b1n0'"))
.arg(arg!(<XNAMES> "List of xnames or nids.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0' or 'nid001313,nid001314'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0' or 'nid00131[0-9]'"))
}

pub fn subcommand_get_hsm_groups_details() -> Command {
Expand Down Expand Up @@ -500,7 +501,7 @@ pub fn subcommand_power() -> Command {
.arg(arg!(-r --regex "Input nodes in regex format.").action(ArgAction::SetTrue))
.arg(arg!(-y --"assume-yes" "Automatic yes to prompts; assume 'yes' as answer to all prompts and run non-interactively.").action(ArgAction::SetTrue))
.arg(arg!(-o --output <FORMAT> "Output format.").value_parser(["table", "json"]).default_value("table"))
.arg(arg!(<VALUE> "Comma separated list of xnames to power off.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0'")),
.arg(arg!(<VALUE> "List of xnames or nids.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0' or 'nid001313,nid001314'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0' or 'nid00131[0-9]'")),
),
)
.subcommand(
Expand Down Expand Up @@ -528,7 +529,7 @@ pub fn subcommand_power() -> Command {
.arg(arg!(-f --force "force").action(ArgAction::SetTrue))
.arg(arg!(-y --"assume-yes" "Automatic yes to prompts; assume 'yes' as answer to all prompts and run non-interactively.").action(ArgAction::SetTrue))
.arg(arg!(-o --output <FORMAT> "Output format.").value_parser(["table", "json"]).default_value("table"))
.arg(arg!(<VALUE> "Comma separated list of xnames to power off.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0'")),
.arg(arg!(<VALUE> "List of xnames or nids.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0' or 'nid001313,nid001314'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0' or 'nid00131[0-9]'")),
),
)
.subcommand(
Expand All @@ -554,7 +555,7 @@ pub fn subcommand_power() -> Command {
.arg(arg!(-f --force "force").action(ArgAction::SetTrue))
.arg(arg!(-y --"assume-yes" "Automatic yes to prompts; assume 'yes' as answer to all prompts and run non-interactively.").action(ArgAction::SetTrue))
.arg(arg!(-o --output <FORMAT> "Output format.").value_parser(["table", "json"]).default_value("table"))
.arg(arg!(<VALUE> "Comma separated list of xnames to power off.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0'")),
.arg(arg!(<VALUE> "List of xnames or nids.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0' or 'nid001313,nid001314'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0' or 'nid00131[0-9]'")),
),
)
}
Expand Down Expand Up @@ -676,8 +677,8 @@ pub fn subcommand_add_nodes_to_groups() -> Command {
.visible_aliases(["ag"])
.about("Add nodes to a list of groups")
.arg(arg!(-g --group <VALUE> "HSM group to assign the nodes to"))
.arg(arg!(-n --nodes <VALUE> "Comma separated list of nodes"))
.arg(arg!(-r --"regex" "Input nodes in regex format.").action(ArgAction::SetTrue))
.arg(arg!(-n --nodes <VALUE> "List of xnames or nids.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0' or 'nid001313,nid001314'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0' or 'nid00131[0-9]'"))
.arg(arg!(-r --regex "Input nodes in regex format.").action(ArgAction::SetTrue))
.arg(arg!(-d --"dry-run" "Simulates the execution of the command without making any actual changes.").action(ArgAction::SetTrue))
}

Expand All @@ -686,7 +687,7 @@ pub fn subcommand_remove_nodes_from_groups() -> Command {
.visible_aliases(["rg"])
.about("Remove nodes from groups")
.arg(arg!(-g --group <VALUE> "HSM group to remove the nodes from"))
.arg(arg!(-n --nodes <VALUE> "Comma separated list of nodes"))
.arg(arg!(-r --"regex" "Input nodes in regex format.").action(ArgAction::SetTrue))
.arg(arg!(-n --nodes <VALUE> "List of xnames or nids.\neg 'x1003c1s7b0n0,1003c1s7b0n1,x1003c1s7b1n0' or 'nid001313,nid001314'\n Host list also accepted eg 'x1003c1s7b0n[0-1],x1003c1s7b1n0' or 'nid00131[0-9]'"))
.arg(arg!(-r --regex "Input nodes in regex format.").action(ArgAction::SetTrue))
.arg(arg!(-d --"dry-run" "Simulates the execution of the command without making any actual changes.").action(ArgAction::SetTrue))
}
54 changes: 52 additions & 2 deletions src/cli/commands/get_nodes.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
use crate::common::node_ops;
use std::collections::HashMap;

use crate::common::{self, node_ops};

use super::power_on_nodes::is_user_input_nids;

/// Get nodes status/configuration for some nodes filtered by a HSM group.
pub async fn exec(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
mut node_list: Vec<String>,
hosts_string: &str,
silent: bool,
silent_xname: bool,
output_opt: Option<&String>,
status: bool,
is_regex: bool,
) {
// Check if user input is 'nid' or 'xname' and convert to 'xname' if needed
let mut node_list = if is_user_input_nids(hosts_string) {
log::debug!("User input seems to be NID");
common::node_ops::nid_to_xname(
shasta_base_url,
shasta_token,
shasta_root_cert,
hosts_string,
is_regex,
)
.await
.expect("Could not convert NID to XNAME")
} else {
log::debug!("User input seems to be XNAME");
let hsm_group_summary: HashMap<String, Vec<String>> = if is_regex {
common::node_ops::get_curated_hsm_group_from_xname_regex(
shasta_token,
shasta_base_url,
shasta_root_cert,
&hosts_string,
)
.await
} else {
// Get HashMap with HSM groups and members curated for this request.
// 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
common::node_ops::get_curated_hsm_group_from_xname_hostlist(
shasta_token,
shasta_base_url,
shasta_root_cert,
&hosts_string,
)
.await
};

hsm_group_summary.values().flatten().cloned().collect()
};

if node_list.is_empty() {
eprintln!("The list of nodes to operate is empty. Nothing to do. Exit");
std::process::exit(0);
}

node_list.sort();
node_list.dedup();

let node_details_list = mesa::node::utils::get_node_details(
shasta_token,
Expand Down
22 changes: 14 additions & 8 deletions src/cli/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,13 +895,18 @@ pub async fn process_cli(
.await;
} else if let Some(cli_get_nodes) = cli_get.subcommand_matches("nodes") {
// Get list of nodes from cli argument
let node_vec: Vec<String> = cli_get_nodes
let xname_requested: &str = cli_get_nodes
.get_one::<String>("XNAMES")
.expect("ERROR - need list of xnames")
.clone()
.split(",")
.map(|xname_str| xname_str.trim().to_string())
.collect();
.expect("The 'xnames' argument must have values");

let is_regex = *cli_get_nodes.get_one::<bool>("regex").unwrap_or(&true);
/* let node_vec: Vec<String> = cli_get_nodes
.get_one::<String>("XNAMES")
.expect("ERROR - need list of xnames")
.clone()
.split(",")
.map(|xname_str| xname_str.trim().to_string())
.collect();
// Validate user has access to list of xnames
validate_target_hsm_members(
Expand All @@ -910,19 +915,20 @@ pub async fn process_cli(
shasta_root_cert,
node_vec.clone(),
)
.await;
.await; */

get_nodes::exec(
shasta_token,
shasta_base_url,
shasta_root_cert,
node_vec.clone(),
xname_requested,
*cli_get_nodes
.get_one::<bool>("nids-only-one-line")
.unwrap_or(&false),
false,
cli_get_nodes.get_one::<String>("output"),
*cli_get_nodes.get_one::<bool>("status").unwrap_or(&false),
is_regex,
)
.await;
} else if let Some(cli_get_hsm_groups) = cli_get.subcommand_matches("hsm-groups") {
Expand Down

0 comments on commit e0fa0f0

Please sign in to comment.