Skip to content

Commit

Permalink
update now forces restart and adds dialogue to ask user is want to pr…
Browse files Browse the repository at this point in the history
…oceed
  • Loading branch information
Manuel Sopena Ballesteros committed Aug 13, 2023
1 parent 48a5e76 commit ad30a1d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 84 deletions.
6 changes: 2 additions & 4 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ pub fn subcommand_update_nodes(hsm_group: Option<&String>) -> Command {
.arg_required_else_help(true)
.about("Updates boot and configuration of a group of nodes. 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> --dessired-configuration <dessired cfs configuration name>")
.arg(arg!(-b --"boot-image" <CFS_CONFIG> "CFS configuration name related to the image to boot the nodes").required(true))
.arg(arg!(-d --"dessired-configuration" <CFS_CONFIG> "CFS configuration name to configure the nodes after booting").required(true))
.arg(arg!(-r --reboot "Reboots the nodes after updating the boot/configuration configuration").action(ArgAction::SetTrue));
.arg(arg!(-d --"dessired-configuration" <CFS_CONFIG> "CFS configuration name to configure the nodes after booting").required(true));

update_nodes = update_nodes
.arg(arg!(<XNAMES> "Comma separated list of xnames which boot image will be updated"));
Expand Down Expand Up @@ -407,8 +406,7 @@ pub fn subcommand_update_hsm_group(hsm_group: Option<&String>) -> Command {
.arg_required_else_help(true)
.about("Updates boot and configuration 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> --dessired-configuration <dessired cfs configuration name>")
.arg(arg!(-b --"boot-image" <CFS_CONFIG> "CFS configuration name related to the image to boot the nodes").required(true))
.arg(arg!(-d --"dessired-configuration" <CFS_CONFIG> "CFS configuration name to configure the nodes after booting").required(true))
.arg(arg!(-r --reboot "Reboots the nodes").action(ArgAction::SetTrue));
.arg(arg!(-d --"dessired-configuration" <CFS_CONFIG> "CFS configuration name to configure the nodes after booting").required(true));

update_hsm_group = match hsm_group {
Some(_) => update_hsm_group,
Expand Down
104 changes: 59 additions & 45 deletions src/cli/commands/update_hsm_group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::ArgMatches;

use dialoguer::{theme::ColorfulTheme, Confirm};
use mesa::shasta::{bos, capmc, hsm, ims};

use crate::common::ims_ops::get_image_id_from_cfs_configuration_name;
Expand All @@ -16,18 +17,6 @@ pub async fn exec(
Some(hsm_group_value) => hsm_group_value,
};

// Get boot-image configuration name
let boot_image_cfs_configuration_name = cli_update_hsm
.get_one::<String>("boot-image")
.unwrap()
.to_string();

// Get dessired-configuration CFS configurantion name
let dessired_configuration_cfs_configuration_name = cli_update_hsm
.get_one::<String>("dessired-configuration")
.unwrap()
.to_string();

// Get nodes members of HSM group
// Get HSM group details
let hsm_group_details =
Expand All @@ -42,11 +31,38 @@ pub async fn exec(
.iter()
.map(|node| node.as_str().unwrap().to_string())
.collect();

if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(format!(
"This operation will reboot all nodes in HSM group {}. Do you want to continue?",
hsm_group_name
))
.interact()
.unwrap()
{
log::info!("Continue",);
} else {
println!("Cancelled by user. Aborting.");
std::process::exit(0);
}

// Get boot-image configuration name
let boot_image_cfs_configuration_name = cli_update_hsm
.get_one::<String>("boot-image")
.unwrap()
.to_string();

log::info!(
"Looking for image ID related to CFS configuration {}",
boot_image_cfs_configuration_name
);

// Get dessired-configuration CFS configurantion name
let dessired_configuration_cfs_configuration_name = cli_update_hsm
.get_one::<String>("dessired-configuration")
.unwrap()
.to_string();

let boot_image_id_opt = Some(
get_image_id_from_cfs_configuration_name(
shasta_token,
Expand Down Expand Up @@ -133,43 +149,41 @@ pub async fn exec(
create_bos_session_template_resp.unwrap()
);

if let Some(true) = cli_update_hsm.get_one::<bool>("reboot") {
log::info!("Rebooting nodes {:?}", nodes);
log::debug!("Rebooting nodes {:?} through CAPMC", nodes);
log::info!("Rebooting nodes {:?}", nodes);
log::debug!("Rebooting nodes {:?} through CAPMC", nodes);

// Create CAPMC operation shutdown
let capmc_shutdown_nodes_resp = capmc::http_client::node_power_off::post_sync(
shasta_token,
shasta_base_url,
nodes.clone(),
Some("Update node boot params".to_string()),
true,
)
.await;
// Create CAPMC operation shutdown
let capmc_shutdown_nodes_resp = capmc::http_client::node_power_off::post_sync(
shasta_token,
shasta_base_url,
nodes.clone(),
Some("Update node boot params".to_string()),
true,
)
.await;

log::debug!(
"CAPMC shutdown nodes response:\n{:#?}",
capmc_shutdown_nodes_resp
);
log::debug!(
"CAPMC shutdown nodes response:\n{:#?}",
capmc_shutdown_nodes_resp
);

// Create BOS session operation start
let create_bos_boot_session_resp = bos::session::http_client::post(
shasta_token,
shasta_base_url,
&create_bos_session_template_payload.name,
"boot",
Some(&nodes.join(",")),
)
.await;
// Create BOS session operation start
let create_bos_boot_session_resp = bos::session::http_client::post(
shasta_token,
shasta_base_url,
&create_bos_session_template_payload.name,
"boot",
Some(&nodes.join(",")),
)
.await;

log::debug!(
"Create BOS boot session response:\n{:#?}",
create_bos_boot_session_resp
);
log::debug!(
"Create BOS boot session response:\n{:#?}",
create_bos_boot_session_resp
);

if create_bos_boot_session_resp.is_err() {
eprintln!("Error creating BOS boot session. Exit");
std::process::exit(1);
}
if create_bos_boot_session_resp.is_err() {
eprintln!("Error creating BOS boot session. Exit");
std::process::exit(1);
}
}
81 changes: 46 additions & 35 deletions src/cli/commands/update_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::common::{ims_ops::get_image_id_from_cfs_configuration_name, node_ops};

use clap::ArgMatches;
use dialoguer::{theme::ColorfulTheme, Confirm};
use mesa::shasta::{bos, capmc, ims};

pub async fn exec(
Expand All @@ -10,6 +11,19 @@ pub async fn exec(
cli_update_hsm: &ArgMatches,
xnames: Vec<&str>,
) {
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(format!("This operation will reboot nodes {:?}. Do you want to continue?", xnames))
.interact()
.unwrap()
{
log::info!(
"Continue",
);
} else {
println!("Cancelled by user. Aborting.");
std::process::exit(0);
}

// Get boot-image configuration name
let boot_image_cfs_configuration_name = cli_update_hsm
.get_one::<String>("boot-image")
Expand All @@ -24,8 +38,7 @@ pub async fn exec(

// Check user has provided valid XNAMES
if hsm_group_name.is_some() {
if !node_ops::validate_xnames(shasta_token, shasta_base_url, &xnames, hsm_group_name)
.await
if !node_ops::validate_xnames(shasta_token, shasta_base_url, &xnames, hsm_group_name).await
{
eprintln!("xname/s invalid. Exit");
std::process::exit(1);
Expand Down Expand Up @@ -113,40 +126,38 @@ pub async fn exec(

let nodes: Vec<String> = xnames.into_iter().map(|xname| xname.to_string()).collect();

if let Some(true) = cli_update_hsm.get_one::<bool>("reboot") {
// Create CAPMC operation shutdown
let capmc_shutdown_nodes_resp = capmc::http_client::node_power_off::post_sync(
shasta_token,
shasta_base_url,
nodes.clone(),
Some("Update node boot params".to_string()),
true,
)
.await;

log::debug!(
"CAPMC shutdown nodes response:\n{:#?}",
capmc_shutdown_nodes_resp
);
// Create CAPMC operation shutdown
let capmc_shutdown_nodes_resp = capmc::http_client::node_power_off::post_sync(
shasta_token,
shasta_base_url,
nodes.clone(),
Some("Update node boot params".to_string()),
true,
)
.await;

// Create BOS session operation start
let create_bos_boot_session_resp = bos::session::http_client::post(
shasta_token,
shasta_base_url,
&create_bos_session_template_payload.name,
"boot",
Some(&nodes.join(",")),
)
.await;

log::debug!(
"Create BOS boot session response:\n{:#?}",
create_bos_boot_session_resp
);
log::debug!(
"CAPMC shutdown nodes response:\n{:#?}",
capmc_shutdown_nodes_resp
);

if create_bos_boot_session_resp.is_err() {
eprintln!("Error creating BOS boot session. Exit");
std::process::exit(1);
}
// Create BOS session operation start
let create_bos_boot_session_resp = bos::session::http_client::post(
shasta_token,
shasta_base_url,
&create_bos_session_template_payload.name,
"boot",
Some(&nodes.join(",")),
)
.await;

log::debug!(
"Create BOS boot session response:\n{:#?}",
create_bos_boot_session_resp
);

if create_bos_boot_session_resp.is_err() {
eprintln!("Error creating BOS boot session. Exit");
std::process::exit(1);
}
}

0 comments on commit ad30a1d

Please sign in to comment.