Skip to content

Commit

Permalink
feat: migrate to CFS configurations v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Sopena Ballesteros committed Oct 4, 2024
1 parent 0608798 commit 03fd3fa
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 42 deletions.
41 changes: 26 additions & 15 deletions src/bss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,15 @@ 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
.split_whitespace()
.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")
Expand All @@ -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
Expand Down
16 changes: 7 additions & 9 deletions src/cfs/configuration/mesa/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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,
shasta_base_url: &str,
shasta_root_cert: &[u8],
configuration_name_opt: Option<&str>,
) -> Result<Vec<CfsConfigurationResponse>, Error> {
cfs::configuration::shasta::http_client::v2::get(
cfs::configuration::shasta::http_client::v3::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 10 additions & 6 deletions src/cfs/configuration/mesa/struct/cfs_configuration_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 7 additions & 4 deletions src/cfs/configuration/mesa/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ 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<CfsConfigurationResponse, crate::error::Error> {
cfs::configuration::mesa::http_client::put(
shasta_token,
shasta_base_url,
shasta_root_cert,
cfs_configuration,
&cfs_configuration.name,
cfs_configuration_name,
)
.await
}
Expand Down
12 changes: 8 additions & 4 deletions src/cfs/configuration/shasta/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -238,10 +240,12 @@ pub mod v3 {

Ok(vec![payload])
} else {
response
.json()
let payload = response
.json::<CfsConfigurationVecResponse>()
.await
.map_err(|error| Error::NetError(error))
.map_err(|error| Error::NetError(error))?;

Ok(payload.configurations)
}
} else {
let payload = response
Expand Down
2 changes: 1 addition & 1 deletion src/common/cluster_ops.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down
2 changes: 1 addition & 1 deletion src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
9 changes: 7 additions & 2 deletions tests/bss_bootparameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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)
}
Expand All @@ -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();

Expand Down

0 comments on commit 03fd3fa

Please sign in to comment.