Skip to content

Commit

Permalink
feat: integrate power and boot operations with CSM backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Masber committed Dec 6, 2024
1 parent 34b1f19 commit 7b1bf53
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 225 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ publish = false # cargo
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mesa = "0.42.1-beta.12"
# mesa = { path = "../mesa" } # Only for development purposes
infra-io = { path = "../infra-io" } # Only for development purposes
# mesa = "0.42.1-beta.12"
mesa = { path = "../mesa" } # Only for development purposes
hostlist-parser = "0.1.6"
strum = "0.25.0"
strum_macros = "0.25"
Expand Down
18 changes: 16 additions & 2 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub fn subcommand_delete() -> Command {
.arg_required_else_help(true)
.about("Deletes data")
.subcommand(subcommand_delete_kernel_parameter())
.subcommand(subcommand_delete_configuration())
.subcommand(subcommand_delete_session())
.subcommand(subcommand_delete_image())
.subcommand(subcommand_delete_hw_component())
Expand All @@ -114,16 +115,29 @@ pub fn subcommand_delete_hw_component() -> Command {

pub fn subcommand_delete_image() -> Command {
Command::new("images")
.visible_alias("di")
.visible_aliases(["image", "i"])
.arg_required_else_help(true)
.about("WIP - Deletes a list of images.")
.arg(arg!(-d --"dry-run" "Simulates the execution of the command without making any actual changes.").action(ArgAction::SetTrue))
.arg(arg!(<IMAGE_LIST> "Comma separated list of image ids to delete/\neg: e2ce82f0-e7ba-4f36-9f5c-750346599600,59e0180a-3fdd-4936-bba7-14ba914ffd34").required(true))
}

pub fn subcommand_delete_configuration() -> Command {
Command::new("configurations")
.visible_aliases(["configuration", "c"])
.arg_required_else_help(true)
.about("Deletes CFS configurations and all data related (CFS sessions, BOS sessiontemplates, BOS sessions and images).")
.arg(arg!(-n --"configuration-name" <CONFIGURATION> "CFS configuration, CFS sessions, BOS sessiontemplate, BOS sessions and IMS images related to the CFS configuration will be deleted.\neg:\nmanta delete --configuration-name my-config-v1.0\nDeletes all data related to CFS configuration with name 'my-config-v0.1'"))
.arg(arg!(-p --pattern <CONFIGURATION_NAME_PATTERN> "Glob pattern for configuration name"))
.arg(arg!(-s --since <DATE> "Deletes CFS configurations, CFS sessions, BOS sessiontemplate, BOS sessions and images related to CFS configurations with 'last updated' after since date. Note: date format is %Y-%m-%d\neg:\nmanta delete --since 2023-01-01 --until 2023-10-01\nDeletes all data related to CFS configurations created or updated between 01/01/2023T00:00:00Z and 01/10/2023T00:00:00Z"))
.arg(arg!(-u --until <DATE> "Deletes CFS configuration, CFS sessions, BOS sessiontemplate, BOS sessions and images related to the CFS configuration with 'last updated' before until date. Note: date format is %Y-%m-%d\neg:\nmanta delete --until 2023-10-01\nDeletes all data related to CFS configurations created or updated before 01/10/2023T00:00:00Z"))
.arg(arg!(-y --"assume-yes" "Automatic yes to prompts; assume 'yes' as answer to all prompts and run non-interactively. Image artifacts and configurations used by nodes will not be deleted").action(ArgAction::SetTrue))
.group(ArgGroup::new("since_and_until").args(["since", "until"]).multiple(true).requires("until").conflicts_with("configuration-name"))
}

pub fn subcommand_delete_session() -> Command {
Command::new("session")
.visible_alias("ds")
.visible_alias("s")
.arg_required_else_help(true)
.about("Deletes a session. For 'image' sessions, it also removes the associated image. For 'dynamic' sessions, it sets the 'error count' to its maximum value.")
.arg(arg!(-d --"dry-run" "Simulates the execution of the command without making any actual changes.").action(ArgAction::SetTrue))
Expand Down
35 changes: 15 additions & 20 deletions src/cli/commands/apply_boot_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use crate::{
use dialoguer::{theme::ColorfulTheme, Confirm};
use mesa::{
bss::{self, r#struct::BootParameters},
cfs,
csm::Csm,
iaas_ops::IaaSOps,
ims,
cfs, ims,
node::utils::validate_xnames_format_and_membership_agaisnt_multiple_hsm,
};

Expand Down Expand Up @@ -67,33 +64,33 @@ pub async fn exec(
std::process::exit(1);
}

// FIXME: move the iaas_ops creation to 'main' to not repeat this code
let iaas_ops_rslt = mesa::iaas_ops::new_iaas(
/* let backup_config_rslt = mesa::config::Config::new(
&shasta_base_url.to_string(),
Some(&shasta_token.to_string()),
&shasta_root_cert.to_vec(),
"csm", // FIXME: do not hardcode this value and move it to config file
shasta_base_url.to_string(),
shasta_token.to_string(),
shasta_root_cert.to_vec(),
);
)
.await;
let iaas_ops = match iaas_ops_rslt {
Ok(iaas_ops) => iaas_ops,
let backup_config = match backup_config_rslt {
Ok(backup_config) => backup_config,
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
};
let mut current_node_boot_param_vec: Vec<BootParameters> = iaas_ops
let mut current_node_boot_param_vec: Vec<BootParameters> = backup_config
.get_bootparameters(
&xname_vec
.iter()
.map(|xname| xname.to_string())
.collect::<Vec<String>>(),
)
.await
.unwrap();
.unwrap(); */

/* // Get current node boot params
// Get current node boot params
let mut current_node_boot_param_vec: Vec<BootParameters> = bss::http_client::get(
shasta_token,
shasta_base_url,
Expand All @@ -104,7 +101,7 @@ pub async fn exec(
.collect::<Vec<String>>(),
)
.await
.unwrap(); */
.unwrap();

// Get new boot image
let new_boot_image_id_opt: Option<String> =
Expand Down Expand Up @@ -248,15 +245,13 @@ pub async fn exec(

// Update boot params
for boot_parameter in current_node_boot_param_vec {
/* let component_patch_rep = bss::http_client::patch(
let component_patch_rep = bss::http_client::patch(
shasta_base_url,
shasta_token,
shasta_root_cert,
&boot_parameter,
)
.await; */

let component_patch_rep = iaas_ops.update_bootparameters(&boot_parameter).await;
.await;

log::debug!(
"Component boot parameters resp:\n{:#?}",
Expand Down
13 changes: 10 additions & 3 deletions src/cli/commands/apply_sat_file/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ pub async fn create_image_from_sat_file_serde_yaml(
);

if !dry_run {
let cfs_session = cfs::session::post_sync(
let cfs_session_rslt = cfs::session::post_sync(
shasta_token,
shasta_base_url,
shasta_root_cert,
Expand All @@ -1086,8 +1086,15 @@ pub async fn create_image_from_sat_file_serde_yaml(
&cfs_session,
watch_logs,
)
.await
.unwrap();
.await;

let cfs_session = match cfs_session_rslt {
Ok(cfs_session) => cfs_session,
Err(e) => {
eprintln!("ERROR - Could not create Image. Reason:\n{}", e);
std::process::exit(1);
}
};

if !cfs_session.is_success() {
eprintln!(
Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/apply_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ pub async fn exec(
Err(error_msg) => log::error!("{}", error_msg),
}

let mut logs_stream =
let _ =
kubernetes::get_cfs_session_container_ansible_logs_stream(client, &cfs_session_name)
.await
.unwrap();

while let Some(line) = logs_stream.try_next().await.unwrap() {
/* while let Some(line) = logs_stream.try_next().await.unwrap() {
println!("{}", line);
}
} */
}
// * End Create CFS session

Expand Down
58 changes: 42 additions & 16 deletions src/cli/commands/delete_data_related_to_cfs_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,62 @@ pub async fn delete_data_related_cfs_configuration(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
hsm_group_name_opt: Option<&String>,
hsm_name_available_vec: Vec<String>,
configuration_name_opt: Option<&String>,
configuration_name_pattern: Option<&String>,
since_opt: Option<NaiveDateTime>,
until_opt: Option<NaiveDateTime>,
yes: &bool,
) {
if !hsm_name_available_vec.contains(hsm_group_name_opt.unwrap()) {
/* if !hsm_name_available_vec.contains(hsm_group_name_opt.unwrap()) {
eprintln!(
"No access to HSM group {}. Exit",
hsm_group_name_opt.unwrap()
);
std::process::exit(1);
}
} */

let xname_vec = mesa::hsm::group::utils::get_member_vec_from_hsm_name_vec(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_name_available_vec.clone(),
)
.await;

// COLLECT SITE WIDE DATA FOR VALIDATION
//

// Check CFS configurations to delete not used as a desired configuration
//
// Get all CFS components in CSM
let cfs_components: Vec<Component> = cfs::component::http_client::v3::get_multiple_components(
shasta_token,
shasta_base_url,
shasta_root_cert,
None,
None,
)
.await
.unwrap();
let cfs_component_vec_rslt = if let Some(configuration_name) = configuration_name_opt {
cfs::component::http_client::v3::get_query(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(configuration_name),
None,
None,
)
.await
} else {
cfs::component::http_client::v3::get_parallel(
shasta_token,
shasta_base_url,
shasta_root_cert,
&xname_vec.as_slice(),
)
.await
};

let cfs_components: Vec<Component> = match cfs_component_vec_rslt {
Ok(cfs_component_vec) => cfs_component_vec,
Err(e) => {
eprintln!("ERROR - could not delete data. Reason:\n{:#?}", e);
std::process::exit(1);
}
};

// Check images related to CFS configurations to delete are not used to boot nodes. For
// this we need to get images from both CFS session and BOS sessiontemplate because CSCS staff
Expand All @@ -67,20 +92,21 @@ pub async fn delete_data_related_cfs_configuration(
shasta_token,
shasta_base_url,
shasta_root_cert,
None,
configuration_name_opt.map(|value| value.as_str()),
)
.await
.unwrap();

// Filter CFS configurations related to HSM group, configuration name or configuration name
// pattern
cfs::configuration::utils::filter(
cfs::configuration::utils::filter_2(
shasta_token,
shasta_base_url,
shasta_root_cert,
cfs_components.clone(),
&mut cfs_configuration_vec,
configuration_name_pattern.map(|elem| elem.as_str()),
&vec![hsm_group_name_opt.unwrap().to_string()],
&hsm_name_available_vec,
None,
)
.await;
Expand Down Expand Up @@ -493,7 +519,7 @@ pub async fn delete_data_related_cfs_configuration(
until_opt.unwrap()
);
}
print!(" in HSM '{}'. Exit", hsm_group_name_opt.unwrap());
print!(" in HSM '{:?}'. Exit", hsm_name_available_vec);
io::stdout().flush().unwrap();

std::process::exit(0);
Expand Down
35 changes: 14 additions & 21 deletions src/cli/commands/power_off_cluster.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use dialoguer::{theme::ColorfulTheme, Confirm};
use mesa::{error::Error, hsm, iaas_ops::IaaSOps};
use mesa::hsm;

use crate::common;

use infra_io::contracts::Power;

pub async fn exec(
shasta_token: &str,
shasta_base_url: &str,
Expand Down Expand Up @@ -36,42 +38,33 @@ pub async fn exec(
}
}

let iaas_ops_rslt = mesa::iaas_ops::new_iaas(
let backend_config_rslt = mesa::config::Config::new(
&shasta_base_url.to_string(),
Some(&shasta_token.to_string()),
&shasta_root_cert.to_vec(),
"csm", // FIXME: do not hardcode this value and move it to config file
shasta_base_url.to_string(),
shasta_token.to_string(),
shasta_root_cert.to_vec(),
);
)
.await;

let iaas_ops = match iaas_ops_rslt {
let backend_config = match backend_config_rslt {
Ok(iaas_ops) => iaas_ops,
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
};

let power_mgmt_summary_rslt = iaas_ops.power_off_sync(&xname_vec, force).await;
let power_mgmt_summary_rslt = backend_config.power_off_sync(&xname_vec, force).await;

let power_mgmt_summary = match power_mgmt_summary_rslt {
Ok(value) => value,
Err(e) => {
/* eprintln!(
"ERROR - Could not restart node/s '{:?}'. Reason:\n{}",
xname_vec, error_msg
);
std::process::exit(1); */
let error_msg = match e {
Error::CsmError(value) => serde_json::to_string_pretty(&value).unwrap(),
Error::SerdeError(value) => value.to_string(),
Error::IoError(value) => value.to_string(),
Error::NetError(value) => value.to_string(),
Error::Message(value) => value.to_string(),
};
eprintln!(
"ERROR - Could not power off node/s '{:?}'. Reason:\n{}",
xname_vec, error_msg
xname_vec,
e.to_string()
);

std::process::exit(1);
}
};
Expand Down
Loading

0 comments on commit 7b1bf53

Please sign in to comment.