From 03fd3faf03b72761285955e56400d2b58caed9c8 Mon Sep 17 00:00:00 2001 From: Manuel Sopena Ballesteros Date: Fri, 4 Oct 2024 18:26:43 +0200 Subject: [PATCH] feat: migrate to CFS configurations v3 --- src/bss.rs | 41 ++++++++++++------- src/cfs/configuration/mesa/http_client.rs | 16 ++++---- .../mesa/struct/cfs_configuration_request.rs | 16 +++++--- src/cfs/configuration/mesa/utils.rs | 11 +++-- src/cfs/configuration/shasta/http_client.rs | 12 ++++-- src/common/cluster_ops.rs | 2 +- src/common/utils.rs | 2 +- tests/bss_bootparameters.rs | 9 +++- 8 files changed, 67 insertions(+), 42 deletions(-) diff --git a/src/bss.rs b/src/bss.rs index 104dc40..0b64459 100644 --- a/src/bss.rs +++ b/src/bss.rs @@ -133,6 +133,8 @@ pub mod bootparameters { /// exists. Otherwise nothing is changed. This method updates both kernel params related to /// NCN and also CN pub fn update_boot_image(&mut self, new_image_id: &str) { + // replace image id in 'root' kernel param + // convert kernel params to a hashmap let mut params: HashMap<&str, &str> = self .params @@ -140,7 +142,6 @@ pub mod bootparameters { .map(|kernel_param| kernel_param.split_once('=').unwrap_or((kernel_param, ""))) .collect(); - // replace image id in 'root' kernel param let mut root_kernel_param: Vec<&str> = params .get("root") .expect("The 'root' kernel param does not exists") @@ -159,27 +160,37 @@ pub mod bootparameters { .entry("root") .and_modify(|root_param| *root_param = &new_root_kernel_param); + self.update_kernel_param("root", &new_root_kernel_param); + // replace image id in 'nmd_data' kernel param - let mut nmd_kernel_param: Vec<&str> = params - .get("nmd_data") - .expect("The 'nmd_data' kernel param does not exists") - .split("/") + + // convert kernel params to a hashmap + let mut params: HashMap<&str, &str> = self + .params + .split_whitespace() + .map(|kernel_param| kernel_param.split_once('=').unwrap_or((kernel_param, ""))) .collect(); - for substring in &mut nmd_kernel_param { - if let Ok(_) = uuid::Uuid::try_parse(substring) { - *substring = new_image_id; + // NOTE: NCN nodes may not have 'nmd_data' kernel parameter + let mut nmd_kernel_param: Vec<&str>; + if let Some(nmd_data) = params.get("nmd_data") { + nmd_kernel_param = nmd_data.split("/").collect(); + + for substring in &mut nmd_kernel_param { + if let Ok(_) = uuid::Uuid::try_parse(substring) { + *substring = new_image_id; + } } - } - let new_nmd_kernel_param = nmd_kernel_param.join("/"); + let new_nmd_kernel_param = nmd_kernel_param.join("/"); - params - .entry("nmd_data") - .and_modify(|nmd_param| *nmd_param = &new_nmd_kernel_param); + params + .entry("nmd_data") + .and_modify(|nmd_param| *nmd_param = &new_nmd_kernel_param); - self.update_kernel_param("root", &new_root_kernel_param); - self.update_kernel_param("nmd_data", &new_nmd_kernel_param); + self.update_kernel_param("nmd_data", &new_nmd_kernel_param); + } else { + }; /* self.params = self .params diff --git a/src/cfs/configuration/mesa/http_client.rs b/src/cfs/configuration/mesa/http_client.rs index 716b0f5..31cbd87 100644 --- a/src/cfs/configuration/mesa/http_client.rs +++ b/src/cfs/configuration/mesa/http_client.rs @@ -1,11 +1,9 @@ -use crate::{ - cfs::{ - self, configuration::mesa::r#struct::cfs_configuration_request::v2::CfsConfigurationRequest, - }, - error::Error, -}; +use crate::{cfs, error::Error}; -use super::r#struct::cfs_configuration_response::v2::CfsConfigurationResponse; +use super::r#struct::{ + cfs_configuration_request::v3::CfsConfigurationRequest, + cfs_configuration_response::v3::CfsConfigurationResponse, +}; pub async fn get( shasta_token: &str, @@ -13,7 +11,7 @@ pub async fn get( shasta_root_cert: &[u8], configuration_name_opt: Option<&str>, ) -> Result, Error> { - cfs::configuration::shasta::http_client::v2::get( + cfs::configuration::shasta::http_client::v3::get( shasta_token, shasta_base_url, shasta_root_cert, @@ -56,7 +54,7 @@ pub async fn put( configuration_name ); - cfs::configuration::shasta::http_client::v2::put( + cfs::configuration::shasta::http_client::v3::put( shasta_token, shasta_base_url, shasta_root_cert, diff --git a/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs b/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs index 78b7e7c..b2ce7da 100644 --- a/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs +++ b/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs @@ -556,16 +556,18 @@ pub mod v3 { }; let layer = Layer::new( - Some(repo_url), - commit_id_opt, Some(layer_name), + Some(repo_url), + layer_yaml["source"] + .as_str() + .and_then(|source_value| Some(source_value.to_string())), layer_yaml["playbook"] .as_str() .unwrap_or_default() .to_string(), + commit_id_opt, branch_name, None, - None, ); cfs_configuration.add_layer(layer); } else if layer_yaml.get("product").is_some() { @@ -643,13 +645,15 @@ pub mod v3 { // Create CFS configuration layer struct let layer = Layer::new( - Some(repo_url), - commit_id_opt, Some(product_name.to_string()), + Some(repo_url), + layer_yaml["source"] + .as_str() + .map(|source_value| source_value.to_string()), layer_yaml["playbook"].as_str().unwrap().to_string(), + commit_id_opt, branch_name, None, - None, ); cfs_configuration.add_layer(layer); } else { diff --git a/src/cfs/configuration/mesa/utils.rs b/src/cfs/configuration/mesa/utils.rs index f4686d7..55ca119 100644 --- a/src/cfs/configuration/mesa/utils.rs +++ b/src/cfs/configuration/mesa/utils.rs @@ -2,21 +2,24 @@ use crate::{ bos::{self, template::mesa::r#struct::v2::BosSessionTemplate}, cfs::{ self, component::shasta::r#struct::v2::ComponentResponse, - configuration::mesa::r#struct::cfs_configuration_response::v2::CfsConfigurationResponse, session::mesa::r#struct::v3::CfsSessionGetResponse, }, hsm, ims::image::r#struct::Image, }; -use super::r#struct::cfs_configuration_request::v2::CfsConfigurationRequest; - use globset::Glob; +use super::r#struct::{ + cfs_configuration_request::v3::CfsConfigurationRequest, + cfs_configuration_response::v3::CfsConfigurationResponse, +}; + pub async fn create( shasta_token: &str, shasta_base_url: &str, shasta_root_cert: &[u8], + cfs_configuration_name: &str, cfs_configuration: &CfsConfigurationRequest, ) -> Result { cfs::configuration::mesa::http_client::put( @@ -24,7 +27,7 @@ pub async fn create( shasta_base_url, shasta_root_cert, cfs_configuration, - &cfs_configuration.name, + cfs_configuration_name, ) .await } diff --git a/src/cfs/configuration/shasta/http_client.rs b/src/cfs/configuration/shasta/http_client.rs index ae6ce92..a8733e6 100644 --- a/src/cfs/configuration/shasta/http_client.rs +++ b/src/cfs/configuration/shasta/http_client.rs @@ -184,7 +184,9 @@ pub mod v3 { use crate::{ cfs::configuration::mesa::r#struct::{ cfs_configuration_request::v3::CfsConfigurationRequest, - cfs_configuration_response::v3::CfsConfigurationResponse, + cfs_configuration_response::v3::{ + CfsConfigurationResponse, CfsConfigurationVecResponse, + }, }, error::Error, }; @@ -238,10 +240,12 @@ pub mod v3 { Ok(vec![payload]) } else { - response - .json() + let payload = response + .json::() .await - .map_err(|error| Error::NetError(error)) + .map_err(|error| Error::NetError(error))?; + + Ok(payload.configurations) } } else { let payload = response diff --git a/src/common/cluster_ops.rs b/src/common/cluster_ops.rs index 86c2f47..fd2b821 100644 --- a/src/common/cluster_ops.rs +++ b/src/common/cluster_ops.rs @@ -1,5 +1,5 @@ use crate::cfs::{ - configuration::mesa::r#struct::cfs_configuration_response::v2::CfsConfigurationResponse, + configuration::mesa::r#struct::cfs_configuration_response::v3::CfsConfigurationResponse, session::mesa::r#struct::v3::CfsSessionGetResponse, }; diff --git a/src/common/utils.rs b/src/common/utils.rs index 2cfe0ba..a7fc22b 100644 --- a/src/common/utils.rs +++ b/src/common/utils.rs @@ -6,7 +6,7 @@ use crate::{ bos::{self, template::mesa::r#struct::v2::BosSessionTemplate}, cfs::{ self, component::shasta::r#struct::v2::ComponentResponse, - configuration::mesa::r#struct::cfs_configuration_response::v2::CfsConfigurationResponse, + configuration::mesa::r#struct::cfs_configuration_response::v3::CfsConfigurationResponse, session::mesa::r#struct::v3::CfsSessionGetResponse, }, ims::{self, image::r#struct::Image}, diff --git a/tests/bss_bootparameters.rs b/tests/bss_bootparameters.rs index a9a7364..11a0206 100644 --- a/tests/bss_bootparameters.rs +++ b/tests/bss_bootparameters.rs @@ -17,7 +17,7 @@ mod tests { let new_image_id = "my_new_image"; - boot_parameters.add_boot_image(new_image_id); + boot_parameters.update_boot_image(new_image_id); let kernel_param_iter = boot_parameters.params.split_whitespace(); @@ -26,19 +26,24 @@ mod tests { for kernel_param in kernel_param_iter { if kernel_param.contains("metal.server=s3://boot-images/") { pass = pass && kernel_param.contains(new_image_id); + println!("DEBUG - pass 1 {}", pass); } if kernel_param.contains("root=craycps-s3:s3://boot-images/") { pass = pass && kernel_param.contains(new_image_id); + println!("DEBUG - pass 2 {}", pass); } if kernel_param.contains("nmd_data=url=s3://boot-images/") { pass = pass && kernel_param.contains(new_image_id); + println!("DEBUG - pass 3 {}", pass); } } pass = pass && boot_parameters.kernel.contains(new_image_id); + println!("DEBUG - pass 4 {}", pass); pass = pass && boot_parameters.initrd.contains(new_image_id); + println!("DEBUG - pass 5 {}", pass); assert!(pass) } @@ -57,7 +62,7 @@ mod tests { let new_image_id = "my_new_image"; - boot_parameters.add_boot_image(new_image_id); + boot_parameters.update_boot_image(new_image_id); let kernel_param_iter = boot_parameters.params.split_whitespace();