From 2f7c4924c11e79e6a65fa2b88f3fc9de5adea219 Mon Sep 17 00:00:00 2001 From: Ian Letourneau Date: Tue, 29 Apr 2025 15:55:58 -0400 Subject: [PATCH] wip --- examples/lamp/src/main.rs | 7 ++++++- harmony/src/domain/interpret/mod.rs | 8 ++++++-- harmony/src/domain/maestro/mod.rs | 13 +++++++++---- harmony/src/domain/score.rs | 3 +++ harmony/src/modules/dummy.rs | 2 ++ harmony/src/modules/lamp.rs | 29 ++++++++++++++++++++++++++++- harmony_tui/src/lib.rs | 2 +- 7 files changed, 55 insertions(+), 9 deletions(-) diff --git a/examples/lamp/src/main.rs b/examples/lamp/src/main.rs index 27e5df6..3ac5f8f 100644 --- a/examples/lamp/src/main.rs +++ b/examples/lamp/src/main.rs @@ -2,9 +2,10 @@ use harmony::{ data::Version, inventory::Inventory, maestro::Maestro, - modules::lamp::{LAMPConfig, LAMPScore}, + modules::lamp::{LAMPConfig, LAMPProfile, LAMPScore}, topology::{K8sAnywhereTopology, Url}, }; +use std::collections::HashMap; #[tokio::main] async fn main() { @@ -17,6 +18,10 @@ async fn main() { project_root: "./php".into(), ..Default::default() }, + profiles: HashMap::from([ + ("dev", LAMPProfile { ssl_enabled: false }), + ("prod", LAMPProfile { ssl_enabled: true }), + ]), }; let mut maestro = Maestro::::initialize( diff --git a/harmony/src/domain/interpret/mod.rs b/harmony/src/domain/interpret/mod.rs index 789edc6..4323127 100644 --- a/harmony/src/domain/interpret/mod.rs +++ b/harmony/src/domain/interpret/mod.rs @@ -39,8 +39,12 @@ impl std::fmt::Display for InterpretName { #[async_trait] pub trait Interpret: std::fmt::Debug + Send { - async fn execute(&self, inventory: &Inventory, topology: &T) - -> Result; + async fn execute( + &self, + inventory: &Inventory, + topology: &T, + profile: &String, + ) -> Result; fn get_name(&self) -> InterpretName; fn get_version(&self) -> Version; fn get_status(&self) -> InterpretStatus; diff --git a/harmony/src/domain/maestro/mod.rs b/harmony/src/domain/maestro/mod.rs index f53fbee..e6a2ba3 100644 --- a/harmony/src/domain/maestro/mod.rs +++ b/harmony/src/domain/maestro/mod.rs @@ -16,20 +16,23 @@ pub struct Maestro { topology: T, scores: Arc>>, topology_preparation_result: Mutex>, + profile: String, } impl Maestro { - pub fn new(inventory: Inventory, topology: T) -> Self { + pub fn new(inventory: Inventory, topology: T, profile: String) -> Self { Self { inventory, topology, scores: Arc::new(RwLock::new(Vec::new())), topology_preparation_result: None.into(), + profile, } } pub async fn initialize(inventory: Inventory, topology: T) -> Result { - let instance = Self::new(inventory, topology); + let profile = "dev".to_string(); // TODO: retrieve from env? + let instance = Self::new(inventory, topology, profile); instance.prepare_topology().await?; Ok(instance) } @@ -78,9 +81,11 @@ impl Maestro { ); } info!("Running score {score:?}"); - let interpret = score.create_interpret(); + let interpret = score.apply_profile(&self.profile).create_interpret(); info!("Launching interpret {interpret:?}"); - let result = interpret.execute(&self.inventory, &self.topology).await; + let result = interpret + .execute(&self.inventory, &self.topology, &self.profile) + .await; info!("Got result {result:?}"); result } diff --git a/harmony/src/domain/score.rs b/harmony/src/domain/score.rs index 0dac87b..7cae9ad 100644 --- a/harmony/src/domain/score.rs +++ b/harmony/src/domain/score.rs @@ -8,6 +8,9 @@ use super::{interpret::Interpret, topology::Topology}; pub trait Score: std::fmt::Debug + ScoreToString + Send + Sync + CloneBoxScore + SerializeScore { + fn apply_profile(&self, profile: &String) -> Box> { + Box::new(self.clone()) + } fn create_interpret(&self) -> Box>; fn name(&self) -> String; } diff --git a/harmony/src/modules/dummy.rs b/harmony/src/modules/dummy.rs index 2e63797..842dac5 100644 --- a/harmony/src/modules/dummy.rs +++ b/harmony/src/modules/dummy.rs @@ -75,6 +75,7 @@ impl Interpret for DummyInterpret { &self, _inventory: &Inventory, _topology: &T, + _profile: &String, ) -> Result { self.result.clone() } @@ -121,6 +122,7 @@ impl Interpret for PanicInterpret { &self, _inventory: &Inventory, _topology: &T, + _profile: &String, ) -> Result { panic!("Panic interpret always panics when executed") } diff --git a/harmony/src/modules/lamp.rs b/harmony/src/modules/lamp.rs index f83ded2..95ff9b2 100644 --- a/harmony/src/modules/lamp.rs +++ b/harmony/src/modules/lamp.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; +use std::fmt::Debug; use std::path::{Path, PathBuf}; use async_trait::async_trait; @@ -19,6 +21,7 @@ pub struct LAMPScore { pub domain: Url, pub config: LAMPConfig, pub php_version: Version, + pub profiles: HashMap<&'static str, LAMPProfile>, } #[derive(Debug, Clone, Serialize)] @@ -27,6 +30,11 @@ pub struct LAMPConfig { pub ssl_enabled: bool, } +#[derive(Debug, Clone, Serialize)] +pub struct LAMPProfile { + pub ssl_enabled: bool, +} + impl Default for LAMPConfig { fn default() -> Self { LAMPConfig { @@ -37,6 +45,23 @@ impl Default for LAMPConfig { } impl Score for LAMPScore { + fn apply_profile(&self, profile: &String) -> Box> { + let profile = match self.profiles.get(profile.as_str()) { + Some(profile) => profile, + None => panic!("Not good"), // TODO: better handling + }; + + let config = LAMPConfig { + ssl_enabled: profile.ssl_enabled, + ..self.config.clone() + }; + + Box::new(LAMPScore { + config, + ..self.clone() + }) + } + fn create_interpret(&self) -> Box> { Box::new(LAMPInterpret { score: self.clone(), @@ -59,6 +84,7 @@ impl Interpret for LAMPInterpret { &self, inventory: &Inventory, topology: &T, + profile: &String, ) -> Result { let image_name = match self.build_docker_image() { Ok(name) => name, @@ -78,8 +104,9 @@ impl Interpret for LAMPInterpret { info!("LAMP deployment_score {deployment_score:?}"); todo!(); deployment_score + .apply_profile(profile) .create_interpret() - .execute(inventory, topology) + .execute(inventory, topology, profile) .await?; todo!() } diff --git a/harmony_tui/src/lib.rs b/harmony_tui/src/lib.rs index 180d608..a6bfb00 100644 --- a/harmony_tui/src/lib.rs +++ b/harmony_tui/src/lib.rs @@ -41,7 +41,7 @@ pub mod tui { /// async fn main() { /// let inventory = Inventory::autoload(); /// let topology = HAClusterTopology::autoload(); -/// let mut maestro = Maestro::new(inventory, topology); +/// let mut maestro = Maestro::new(inventory, topology, "local"); /// /// maestro.register_all(vec![ /// Box::new(SuccessScore {}),