From 651266d71c499854d51283135fde147bfe5a2cd7 Mon Sep 17 00:00:00 2001 From: Jean-Gabriel Gill-Couture Date: Sat, 25 Jan 2025 12:36:22 -0500 Subject: [PATCH] fix: update Score trait implementation and TUI initialization Update the `Score` trait implementations to return a `Box` instead of concrete types or clones where necessary. Additionally, refactor the initialization and cleanup in `HarmonyTUI` to use utility functions provided by `ratatui`. --- harmony-rs/demo/vbox-opnsense/src/main.rs | 2 +- .../harmony/src/domain/interpret/mod.rs | 2 +- harmony-rs/harmony/src/domain/maestro/mod.rs | 24 +++++++++++++++---- harmony-rs/harmony/src/domain/score.rs | 3 +-- .../src/domain/topology/load_balancer.rs | 4 ++-- harmony-rs/harmony/src/modules/dhcp.rs | 6 ++--- harmony-rs/harmony/src/modules/dns.rs | 6 ++--- harmony-rs/harmony/src/modules/http.rs | 6 ++--- .../harmony/src/modules/k8s/deployment.rs | 12 ++++------ .../harmony/src/modules/k8s/resource.rs | 4 +--- .../harmony/src/modules/load_balancer.rs | 8 +++---- .../harmony/src/modules/okd/bootstrap_dhcp.rs | 5 ++-- .../modules/okd/bootstrap_load_balancer.rs | 5 ++-- harmony-rs/harmony/src/modules/okd/dhcp.rs | 5 ++-- harmony-rs/harmony/src/modules/okd/dns.rs | 5 ++-- .../harmony/src/modules/okd/load_balancer.rs | 5 ++-- harmony-rs/harmony/src/modules/okd/upgrade.rs | 4 +--- harmony-rs/harmony/src/modules/tftp.rs | 6 ++--- harmony-rs/harmony_tui/src/lib.rs | 7 +++--- 19 files changed, 57 insertions(+), 62 deletions(-) diff --git a/harmony-rs/demo/vbox-opnsense/src/main.rs b/harmony-rs/demo/vbox-opnsense/src/main.rs index 97482c8..1f903cb 100644 --- a/harmony-rs/demo/vbox-opnsense/src/main.rs +++ b/harmony-rs/demo/vbox-opnsense/src/main.rs @@ -92,5 +92,5 @@ async fn main() { // maestro.interpret(dhcp_score).await.unwrap(); // maestro.interpret(load_balancer_score).await.unwrap(); // maestro.interpret(tftp_score).await.unwrap(); - maestro.interpret(http_score).await.unwrap(); + maestro.interpret(&http_score).await.unwrap(); } diff --git a/harmony-rs/harmony/src/domain/interpret/mod.rs b/harmony-rs/harmony/src/domain/interpret/mod.rs index 709ba0a..0860727 100644 --- a/harmony-rs/harmony/src/domain/interpret/mod.rs +++ b/harmony-rs/harmony/src/domain/interpret/mod.rs @@ -31,7 +31,7 @@ impl std::fmt::Display for InterpretName { } #[async_trait] -pub trait Interpret { +pub trait Interpret: std::fmt::Debug { async fn execute( &self, inventory: &Inventory, diff --git a/harmony-rs/harmony/src/domain/maestro/mod.rs b/harmony-rs/harmony/src/domain/maestro/mod.rs index 959aa33..d93c8cb 100644 --- a/harmony-rs/harmony/src/domain/maestro/mod.rs +++ b/harmony-rs/harmony/src/domain/maestro/mod.rs @@ -1,4 +1,5 @@ -use derive_new::new; +use std::sync::{Arc, RwLock}; + use log::info; use super::{ @@ -8,20 +9,35 @@ use super::{ topology::HAClusterTopology, }; -#[derive(new)] +type ScoreVec = Vec>; + pub struct Maestro { inventory: Inventory, topology: HAClusterTopology, + scores: Arc>, } impl Maestro { + pub fn new(inventory: Inventory, topology: HAClusterTopology) -> Self { + Self { + inventory, + topology, + scores: Arc::new(RwLock::new(Vec::new())), + } + } + pub fn start(&mut self) { info!("Starting Maestro"); } - pub async fn interpret(&self, score: S) -> Result { + pub fn register_all(&mut self, mut scores: ScoreVec) { + let mut score_mut = self.scores.write().expect("Should acquire lock"); + score_mut.append(&mut scores); + } + + pub async fn interpret(&self, score: &S) -> Result { info!("Running score {score:?}"); - let interpret: S::InterpretType = score.create_interpret(); + let interpret = score.create_interpret(); info!("Launching interpret {interpret:?}"); let result = interpret.execute(&self.inventory, &self.topology).await; info!("Got result {result:?}"); diff --git a/harmony-rs/harmony/src/domain/score.rs b/harmony-rs/harmony/src/domain/score.rs index fedc848..c9adcd5 100644 --- a/harmony-rs/harmony/src/domain/score.rs +++ b/harmony-rs/harmony/src/domain/score.rs @@ -1,6 +1,5 @@ use super::interpret::Interpret; pub trait Score: std::fmt::Debug { - type InterpretType: Interpret + std::fmt::Debug; - fn create_interpret(self) -> Self::InterpretType; + fn create_interpret(&self) -> Box; } diff --git a/harmony-rs/harmony/src/domain/topology/load_balancer.rs b/harmony-rs/harmony/src/domain/topology/load_balancer.rs index 4d4afda..3fd0d3a 100644 --- a/harmony-rs/harmony/src/domain/topology/load_balancer.rs +++ b/harmony-rs/harmony/src/domain/topology/load_balancer.rs @@ -33,14 +33,14 @@ impl std::fmt::Debug for dyn LoadBalancer { f.write_fmt(format_args!("LoadBalancer {}", self.get_ip())) } } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub struct LoadBalancerService { pub backend_servers: Vec, pub listening_port: SocketAddr, pub health_check: Option, } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub struct BackendServer { pub address: String, pub port: u16, diff --git a/harmony-rs/harmony/src/modules/dhcp.rs b/harmony-rs/harmony/src/modules/dhcp.rs index 017c60f..751b025 100644 --- a/harmony-rs/harmony/src/modules/dhcp.rs +++ b/harmony-rs/harmony/src/modules/dhcp.rs @@ -21,10 +21,8 @@ pub struct DhcpScore { } impl Score for DhcpScore { - type InterpretType = DhcpInterpret; - - fn create_interpret(self) -> DhcpInterpret { - DhcpInterpret::new(self) + fn create_interpret(&self) -> Box { + Box::new(DhcpInterpret::new(self.clone())) } } diff --git a/harmony-rs/harmony/src/modules/dns.rs b/harmony-rs/harmony/src/modules/dns.rs index 3bb005e..a935e2c 100644 --- a/harmony-rs/harmony/src/modules/dns.rs +++ b/harmony-rs/harmony/src/modules/dns.rs @@ -17,10 +17,8 @@ pub struct DnsScore { } impl Score for DnsScore { - type InterpretType = DnsInterpret; - - fn create_interpret(self) -> Self::InterpretType { - DnsInterpret::new(self) + fn create_interpret(&self) -> Box { + Box::new(DnsInterpret::new(self.clone())) } } diff --git a/harmony-rs/harmony/src/modules/http.rs b/harmony-rs/harmony/src/modules/http.rs index b5d0220..7f9d01a 100644 --- a/harmony-rs/harmony/src/modules/http.rs +++ b/harmony-rs/harmony/src/modules/http.rs @@ -15,10 +15,8 @@ pub struct HttpScore { } impl Score for HttpScore { - type InterpretType = HttpInterpret; - - fn create_interpret(self) -> Self::InterpretType { - HttpInterpret::new(self) + fn create_interpret(&self) -> Box { + Box::new(HttpInterpret::new(self.clone())) } } diff --git a/harmony-rs/harmony/src/modules/k8s/deployment.rs b/harmony-rs/harmony/src/modules/k8s/deployment.rs index cadcffb..28f559b 100644 --- a/harmony-rs/harmony/src/modules/k8s/deployment.rs +++ b/harmony-rs/harmony/src/modules/k8s/deployment.rs @@ -1,7 +1,7 @@ use k8s_openapi::api::apps::v1::Deployment; use serde_json::json; -use crate::score::Score; +use crate::{interpret::Interpret, score::Score}; use super::resource::{K8sResourceInterpret, K8sResourceScore}; @@ -12,9 +12,7 @@ pub struct K8sDeploymentScore { } impl Score for K8sDeploymentScore { - type InterpretType = K8sResourceInterpret; - - fn create_interpret(self) -> Self::InterpretType { + fn create_interpret(&self) -> Box { let deployment: Deployment = serde_json::from_value(json!( { "metadata": { @@ -45,8 +43,8 @@ impl Score for K8sDeploymentScore { } )) .unwrap(); - K8sResourceInterpret { - score: K8sResourceScore::single(deployment), - } + Box::new(K8sResourceInterpret { + score: K8sResourceScore::single(deployment.clone()), + }) } } diff --git a/harmony-rs/harmony/src/modules/k8s/resource.rs b/harmony-rs/harmony/src/modules/k8s/resource.rs index eccbe8f..546a3a7 100644 --- a/harmony-rs/harmony/src/modules/k8s/resource.rs +++ b/harmony-rs/harmony/src/modules/k8s/resource.rs @@ -36,9 +36,7 @@ impl< where ::DynamicType: Default, { - type InterpretType = K8sResourceInterpret; - - fn create_interpret(self) -> Self::InterpretType { + fn create_interpret(&self) -> Box { todo!() } } diff --git a/harmony-rs/harmony/src/modules/load_balancer.rs b/harmony-rs/harmony/src/modules/load_balancer.rs index 4653021..8d7186e 100644 --- a/harmony-rs/harmony/src/modules/load_balancer.rs +++ b/harmony-rs/harmony/src/modules/load_balancer.rs @@ -9,7 +9,7 @@ use crate::{ topology::{HAClusterTopology, LoadBalancerService}, }; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct LoadBalancerScore { pub public_services: Vec, pub private_services: Vec, @@ -20,10 +20,8 @@ pub struct LoadBalancerScore { } impl Score for LoadBalancerScore { - type InterpretType = LoadBalancerInterpret; - - fn create_interpret(self) -> Self::InterpretType { - LoadBalancerInterpret::new(self) + fn create_interpret(&self) -> Box { + Box::new(LoadBalancerInterpret::new(self.clone())) } } diff --git a/harmony-rs/harmony/src/modules/okd/bootstrap_dhcp.rs b/harmony-rs/harmony/src/modules/okd/bootstrap_dhcp.rs index 9b34a71..17a1ea7 100644 --- a/harmony-rs/harmony/src/modules/okd/bootstrap_dhcp.rs +++ b/harmony-rs/harmony/src/modules/okd/bootstrap_dhcp.rs @@ -1,4 +1,5 @@ use crate::{ + interpret::Interpret, inventory::Inventory, modules::dhcp::DhcpScore, score::Score, @@ -46,9 +47,7 @@ impl OKDBootstrapDhcpScore { } impl Score for OKDBootstrapDhcpScore { - type InterpretType = ::InterpretType; - - fn create_interpret(self) -> Self::InterpretType { + fn create_interpret(&self) -> Box { self.dhcp_score.create_interpret() } } diff --git a/harmony-rs/harmony/src/modules/okd/bootstrap_load_balancer.rs b/harmony-rs/harmony/src/modules/okd/bootstrap_load_balancer.rs index ec8e7eb..7170be7 100644 --- a/harmony-rs/harmony/src/modules/okd/bootstrap_load_balancer.rs +++ b/harmony-rs/harmony/src/modules/okd/bootstrap_load_balancer.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use crate::{ + interpret::Interpret, modules::load_balancer::LoadBalancerScore, score::Score, topology::{ @@ -69,9 +70,7 @@ impl OKDBootstrapLoadBalancerScore { } impl Score for OKDBootstrapLoadBalancerScore { - type InterpretType = ::InterpretType; - - fn create_interpret(self) -> Self::InterpretType { + fn create_interpret(&self) -> Box { self.load_balancer_score.create_interpret() } } diff --git a/harmony-rs/harmony/src/modules/okd/dhcp.rs b/harmony-rs/harmony/src/modules/okd/dhcp.rs index c837c02..c2b8e78 100644 --- a/harmony-rs/harmony/src/modules/okd/dhcp.rs +++ b/harmony-rs/harmony/src/modules/okd/dhcp.rs @@ -1,4 +1,5 @@ use crate::{ + interpret::Interpret, inventory::Inventory, modules::dhcp::DhcpScore, score::Score, @@ -38,9 +39,7 @@ impl OKDDhcpScore { } impl Score for OKDDhcpScore { - type InterpretType = ::InterpretType; - - fn create_interpret(self) -> Self::InterpretType { + fn create_interpret(&self) -> Box { self.dhcp_score.create_interpret() } } diff --git a/harmony-rs/harmony/src/modules/okd/dns.rs b/harmony-rs/harmony/src/modules/okd/dns.rs index b2e6978..80c8b02 100644 --- a/harmony-rs/harmony/src/modules/okd/dns.rs +++ b/harmony-rs/harmony/src/modules/okd/dns.rs @@ -1,4 +1,5 @@ use crate::{ + interpret::Interpret, modules::dns::DnsScore, score::Score, topology::{DnsRecord, DnsRecordType, HAClusterTopology}, @@ -40,9 +41,7 @@ impl OKDDnsScore { } impl Score for OKDDnsScore { - type InterpretType = ::InterpretType; - - fn create_interpret(self) -> Self::InterpretType { + fn create_interpret(&self) -> Box { self.dns_score.create_interpret() } } diff --git a/harmony-rs/harmony/src/modules/okd/load_balancer.rs b/harmony-rs/harmony/src/modules/okd/load_balancer.rs index 1f2927e..59d678b 100644 --- a/harmony-rs/harmony/src/modules/okd/load_balancer.rs +++ b/harmony-rs/harmony/src/modules/okd/load_balancer.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use crate::{ + interpret::Interpret, modules::load_balancer::LoadBalancerScore, score::Score, topology::{ @@ -80,9 +81,7 @@ impl OKDLoadBalancerScore { } impl Score for OKDLoadBalancerScore { - type InterpretType = ::InterpretType; - - fn create_interpret(self) -> Self::InterpretType { + fn create_interpret(&self) -> Box { self.load_balancer_score.create_interpret() } } diff --git a/harmony-rs/harmony/src/modules/okd/upgrade.rs b/harmony-rs/harmony/src/modules/okd/upgrade.rs index 8a101eb..5ffef54 100644 --- a/harmony-rs/harmony/src/modules/okd/upgrade.rs +++ b/harmony-rs/harmony/src/modules/okd/upgrade.rs @@ -16,9 +16,7 @@ impl OKDUpgradeScore { } // impl Score for OKDUpgradeScore { -// type InterpretType; -// -// fn create_interpret(self) -> Self::InterpretType { +// fn create_interpret(self) -> Box { // // Should this be a specialized interpret for OKD upgrades or rather a set of interprets // // such as : // // diff --git a/harmony-rs/harmony/src/modules/tftp.rs b/harmony-rs/harmony/src/modules/tftp.rs index df68974..c8a3250 100644 --- a/harmony-rs/harmony/src/modules/tftp.rs +++ b/harmony-rs/harmony/src/modules/tftp.rs @@ -15,10 +15,8 @@ pub struct TftpScore { } impl Score for TftpScore { - type InterpretType = TftpInterpret; - - fn create_interpret(self) -> Self::InterpretType { - TftpInterpret::new(self) + fn create_interpret(&self) -> Box { + Box::new(TftpInterpret::new(self.clone())) } } diff --git a/harmony-rs/harmony_tui/src/lib.rs b/harmony-rs/harmony_tui/src/lib.rs index 294eea1..e568ae1 100644 --- a/harmony-rs/harmony_tui/src/lib.rs +++ b/harmony-rs/harmony_tui/src/lib.rs @@ -1,7 +1,7 @@ -use std::io; use crossterm::event::{self, Event}; use harmony::maestro::Maestro; use ratatui::{self, layout::Position, prelude::CrosstermBackend, Frame, Terminal}; +use std::io; pub mod tui { // Export any necessary modules or types from the internal tui module @@ -43,8 +43,7 @@ impl HarmonyTUI { pub async fn init(self) -> Result<(), Box> { color_eyre::install()?; - let backend = CrosstermBackend::new(io::stdout()); - let mut terminal = Terminal::new(backend)?; + let mut terminal = ratatui::init(); loop { terminal.draw(|f| self.render(f))?; @@ -53,6 +52,8 @@ impl HarmonyTUI { } } + ratatui::restore(); + Ok(()) }