Skip to content

Commit

Permalink
feat: update command no longer reboot nodes if boot image did not change
Browse files Browse the repository at this point in the history
refactor: clean code
feat: update manta version
  • Loading branch information
Manuel Sopena Ballesteros committed Mar 1, 2024
1 parent c55364d commit 9d8208c
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 261 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish = false # cargo
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mesa = "0.31.0"
mesa = "0.31.2"
# mesa = { path = "../mesa" } # Only for development purposes
strum = "0.25.0"
strum_macros = "0.25"
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/get_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub async fn get_configuration_layer_details(
// check if layer commit is the most recent
if commit_sha.eq(layer.commit.as_ref().unwrap()) {
// CFS layer commit is the same as the HEAD of the branch
most_recent_commit = most_recent_commit || true;
most_recent_commit = true;
}
}

Expand Down
198 changes: 21 additions & 177 deletions src/cli/commands/update_hsm_group.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use dialoguer::{theme::ColorfulTheme, Confirm};
use mesa::{capmc, cfs, hsm};
use mesa::hsm;

use crate::common::ims_ops::get_image_id_from_cfs_configuration_name;
use crate::cli::commands::update_node;

/// Updates boot params and desired configuration for all nodes that belongs to a HSM group
/// If boot params defined, then nodes in HSM group will be rebooted
Expand All @@ -13,194 +12,39 @@ pub async fn exec(
desired_configuration_opt: Option<&String>,
hsm_group_name: &String,
) {
let need_restart = boot_image_configuration_opt.is_some();

let desired_configuration_detail_list_rslt = cfs::configuration::mesa::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
desired_configuration_opt.map(|elem| elem.as_str()),
)
.await;

// Check desired configuration exists
if desired_configuration_detail_list_rslt.is_ok()
&& !desired_configuration_detail_list_rslt
.as_ref()
.unwrap()
.is_empty()
{
let desired_configuration_detail_list = desired_configuration_detail_list_rslt.unwrap();

log::debug!(
"CFS configuration resp:\n{:#?}",
desired_configuration_detail_list
);

let _ = desired_configuration_detail_list
.first()
.unwrap()
.name
.clone();
} else {
eprintln!(
"Desired configuration {} does not exists. Exit",
desired_configuration_opt.unwrap()
);
std::process::exit(1);
};

// Get nodes members of HSM group
// Get HSM group details
let hsm_group_details = hsm::group::shasta::http_client::get(
let hsm_group_details_rslt = hsm::group::shasta::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(hsm_group_name),
)
.await;

log::debug!("HSM group response:\n{:#?}", hsm_group_details);
let hsm_group_details = if let Ok(hsm_group_details) = hsm_group_details_rslt {
hsm_group_details
} else {
eprintln!("Cluster '{}' not found. Exit", hsm_group_name);
std::process::exit(1);
};

// Get list of xnames in HSM group
let nodes: Vec<String> = hsm_group_details.unwrap().first().unwrap()["members"]["ids"]
let nodes: Vec<&str> = hsm_group_details[0]["members"]["ids"]
.as_array()
.unwrap()
.iter()
.map(|node| node.as_str().unwrap().to_string())
.map(|node| node.as_str().unwrap())
.collect();

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

// Process boot parameters
if let Some(boot_image_cfs_configuration_name) = boot_image_configuration_opt {
let image_id_opt = get_image_id_from_cfs_configuration_name(
shasta_token,
shasta_base_url,
shasta_root_cert,
boot_image_cfs_configuration_name.clone(),
)
.await;

let image_value = if let Some(image_id) = image_id_opt {
mesa::ims::image::shasta::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(&image_id),
)
.await
.unwrap()
} else {
eprintln!(
"Image ID related to CFS configuration name {} not found. Exit",
boot_image_cfs_configuration_name
);
std::process::exit(1);
};

log::debug!("image_details:\n{:#?}", image_value);

let image_path = image_value.first().unwrap()["link"]["path"]
.as_str()
.unwrap()
.to_string();

let image_id = image_path
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap()
.to_string();

let component_patch_rep = mesa::bss::http_client::put(
shasta_base_url,
shasta_token,
shasta_root_cert,
&nodes,
&format!("console=ttyS0,115200 bad_page=panic crashkernel=360M hugepagelist=2m-2g intel_iommu=off intel_pstate=disable iommu.passthrough=on numa_interleave_omit=headless oops=panic pageblock_order=14 rd.neednet=1 rd.retry=10 rd.shell ip=dhcp quiet ksocklnd.skip_mr_route_setup=1 cxi_core.disable_default_svc=0 cxi_core.enable_fgfc=1 cxi_core.disable_default_svc=0 cxi_core.sct_pid_mask=0xf spire_join_token=${{SPIRE_JOIN_TOKEN}} root=craycps-s3:s3://boot-images/{}/rootfs:37df9a2dc2c4b50679def2193c193c40-230:dvs:api-gw-service-nmn.local:300:nmn0", image_id),
&format!("s3://boot-images/{}/kernel", image_id),
&format!("s3://boot-images/{}/initrd", image_id),
)
.await;

log::debug!(
"Component boot parameters resp:\n{:#?}",
component_patch_rep
);
}

// Process dessired configuration
if let Some(desired_configuration_name) = desired_configuration_opt {
log::info!(
"Updating desired configuration. Need restart? {}",
need_restart
);

mesa::cfs::component::shasta::utils::update_component_list_desired_configuration(
shasta_token,
shasta_base_url,
shasta_root_cert,
nodes.clone(),
// for this field so it accepts
// Vec<&str> instead of
// Vec<String>
desired_configuration_name,
!need_restart,
)
.await;
}

// Check if need to reboot
if need_restart {
// Create BOS session. Note: reboot operation shuts down the nodes and don't bring them back
// up... hence we will split the reboot into 2 operations shutdown and start

log::info!("Restarting nodes");

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

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

// Create CAPMC operation to start
let capmc_start_nodes_resp = capmc::http_client::node_power_on::post(
shasta_token,
shasta_base_url,
shasta_root_cert,
nodes,
Some("Update node boot params and/or desired configuration".to_string()),
)
.await;

log::debug!(
"CAPMC starting nodes response:\n{:#?}",
capmc_start_nodes_resp
);
}
update_node::exec(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(hsm_group_name),
boot_image_configuration_opt,
desired_configuration_opt,
nodes.clone(),
)
.await;
}
Loading

0 comments on commit 9d8208c

Please sign in to comment.