From 44bf21718c3223f68c1d60f8dc75019102be2322 Mon Sep 17 00:00:00 2001 From: Willem Date: Tue, 28 Oct 2025 14:41:15 -0400 Subject: [PATCH] wip: example score with impl topolgy for opnsense topology --- examples/opnsense_node_exporter/Cargo.toml | 1 + examples/opnsense_node_exporter/src/main.rs | 111 +++++++------------- harmony/src/infra/opnsense/node_exporter.rs | 2 +- opnsense-config-xml/src/data/opnsense.rs | 34 +++--- 4 files changed, 58 insertions(+), 90 deletions(-) diff --git a/examples/opnsense_node_exporter/Cargo.toml b/examples/opnsense_node_exporter/Cargo.toml index 957bdd9..5cc5c10 100644 --- a/examples/opnsense_node_exporter/Cargo.toml +++ b/examples/opnsense_node_exporter/Cargo.toml @@ -18,3 +18,4 @@ log = { workspace = true } env_logger = { workspace = true } url = { workspace = true } serde.workspace = true +async-trait.workspace = true diff --git a/examples/opnsense_node_exporter/src/main.rs b/examples/opnsense_node_exporter/src/main.rs index 4f1219d..15664ab 100644 --- a/examples/opnsense_node_exporter/src/main.rs +++ b/examples/opnsense_node_exporter/src/main.rs @@ -3,99 +3,66 @@ use std::{ sync::Arc, }; +use async_trait::async_trait; use cidr::Ipv4Cidr; use harmony::{ + executors::ExecutorError, hardware::{HostCategory, Location, PhysicalHost, SwitchGroup}, infra::opnsense::OPNSenseManagementInterface, inventory::Inventory, modules::opnsense::node_exporter::NodeExporterScore, - topology::{HAClusterTopology, LogicalHost, UnmanagedRouter}, + topology::{ + HAClusterTopology, LogicalHost, PreparationError, PreparationOutcome, Topology, + UnmanagedRouter, node_exporter::NodeExporter, + }, }; use harmony_macros::{ip, ipv4, mac_address}; +struct OpnSenseTopology { + node_exporter: Arc, +} + +#[async_trait] +impl Topology for OpnSenseTopology { + async fn ensure_ready(&self) -> Result { + Ok(PreparationOutcome::Success{ details: "Success".to_string() }) + } + fn name(&self) -> &str { + "OpnsenseTopology" + } +} + +#[async_trait] +impl NodeExporter for OpnSenseTopology { + async fn ensure_initialized(&self) -> Result<(), ExecutorError> { + self.node_exporter.ensure_initialized().await + } + + async fn commit_config(&self) -> Result<(), ExecutorError> { + self.node_exporter.commit_config().await + } + + async fn reload_restart(&self) -> Result<(), ExecutorError> { + self.node_exporter.reload_restart().await + } +} + #[tokio::main] async fn main() { let firewall = harmony::topology::LogicalHost { - ip: ip!("192.168.33.1"), + ip: ip!("192.168.1.1"), name: String::from("fw0"), }; let opnsense = Arc::new( harmony::infra::opnsense::OPNSenseFirewall::new(firewall, None, "root", "opnsense").await, ); - let lan_subnet = Ipv4Addr::new(192, 168, 33, 0); - let gateway_ipv4 = Ipv4Addr::new(192, 168, 33, 1); - let gateway_ip = IpAddr::V4(gateway_ipv4); - let topology = harmony::topology::HAClusterTopology { - domain_name: "ncd0.harmony.mcd".to_string(), - router: Arc::new(UnmanagedRouter::new( - gateway_ip, - Ipv4Cidr::new(lan_subnet, 24).unwrap(), - )), - load_balancer: opnsense.clone(), - firewall: opnsense.clone(), - tftp_server: opnsense.clone(), - http_server: opnsense.clone(), - dhcp_server: opnsense.clone(), - dns_server: opnsense.clone(), - control_plane: vec![ - LogicalHost { - ip: ip!("192.168.33.20"), - name: "cp0".to_string(), - }, - LogicalHost { - ip: ip!("192.168.33.21"), - name: "cp1".to_string(), - }, - LogicalHost { - ip: ip!("192.168.33.22"), - name: "cp2".to_string(), - }, - ], - bootstrap_host: LogicalHost { - ip: ip!("192.168.33.66"), - name: "bootstrap".to_string(), - }, - workers: vec![ - LogicalHost { - ip: ip!("192.168.33.30"), - name: "wk0".to_string(), - }, - LogicalHost { - ip: ip!("192.168.33.31"), - name: "wk1".to_string(), - }, - LogicalHost { - ip: ip!("192.168.33.32"), - name: "wk2".to_string(), - }, - ], - switch: vec![], + + let topology = OpnSenseTopology { node_exporter: opnsense.clone(), }; - let inventory = Inventory { - location: Location::new("I am mobile".to_string(), "earth".to_string()), - switch: SwitchGroup::from([]), - firewall_mgmt: Box::new(OPNSenseManagementInterface::new()), - storage_host: vec![], - worker_host: vec![ - PhysicalHost::empty(HostCategory::Server) - .mac_address(mac_address!("C4:62:37:02:61:0F")), - PhysicalHost::empty(HostCategory::Server) - .mac_address(mac_address!("C4:62:37:02:61:26")), - PhysicalHost::empty(HostCategory::Server) - .mac_address(mac_address!("C4:62:37:02:61:70")), - ], - control_plane_host: vec![ - PhysicalHost::empty(HostCategory::Server) - .mac_address(mac_address!("C4:62:37:02:60:FA")), - PhysicalHost::empty(HostCategory::Server) - .mac_address(mac_address!("C4:62:37:02:61:1A")), - PhysicalHost::empty(HostCategory::Server) - .mac_address(mac_address!("C4:62:37:01:BC:68")), - ], - }; + let inventory = Inventory::empty(); let node_exporter_score = NodeExporterScore {}; diff --git a/harmony/src/infra/opnsense/node_exporter.rs b/harmony/src/infra/opnsense/node_exporter.rs index 2c27b26..3a16ffc 100644 --- a/harmony/src/infra/opnsense/node_exporter.rs +++ b/harmony/src/infra/opnsense/node_exporter.rs @@ -25,7 +25,7 @@ impl NodeExporter for OPNSenseFirewall { })?; } - config.node_exporter().enable(true); + config.node_exporter().enable(true).map_err(|e|ExecutorError::UnexpectedError(e.to_string()))?; Ok(()) } async fn commit_config(&self) -> Result<(), ExecutorError> { diff --git a/opnsense-config-xml/src/data/opnsense.rs b/opnsense-config-xml/src/data/opnsense.rs index 4b384d4..a4bba5e 100644 --- a/opnsense-config-xml/src/data/opnsense.rs +++ b/opnsense-config-xml/src/data/opnsense.rs @@ -17,7 +17,7 @@ pub struct OPNsense { pub interfaces: NamedList, pub dhcpd: NamedList, pub snmpd: Snmpd, - pub syslog: Syslog, + pub syslog: Option, pub nat: Nat, pub filter: Filters, pub load_balancer: Option, @@ -190,7 +190,7 @@ pub struct System { pub webgui: WebGui, pub usevirtualterminal: u8, pub disablenatreflection: Option, - pub disableconsolemenu: u8, + pub disableconsolemenu: Option, pub disablevlanhwfilter: u8, pub disablechecksumoffloading: u8, pub disablesegmentationoffloading: u8, @@ -216,7 +216,7 @@ pub struct System { pub maximumfrags: Option, pub aliasesresolveinterval: Option, pub maximumtableentries: Option, - pub language: String, + pub language: Option, pub dnsserver: Option, pub dns1gw: Option, pub dns2gw: Option, @@ -233,16 +233,16 @@ pub struct System { #[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)] pub struct Ssh { pub group: String, - pub noauto: u8, - pub interfaces: MaybeString, - pub kex: MaybeString, - pub ciphers: MaybeString, - pub macs: MaybeString, - pub keys: MaybeString, - pub enabled: String, - pub passwordauth: u8, - pub keysig: MaybeString, - pub permitrootlogin: u8, + pub noauto: Option, + pub interfaces: Option, + pub kex: Option, + pub ciphers: Option, + pub macs: Option, + pub keys: Option, + pub enabled: Option, + pub passwordauth: Option, + pub keysig: Option, + pub permitrootlogin: Option, pub rekeylimit: Option, } @@ -306,11 +306,11 @@ pub struct WebGui { pub protocol: String, #[yaserde(rename = "ssl-certref")] pub ssl_certref: String, - pub port: MaybeString, + pub port: Option, #[yaserde(rename = "ssl-ciphers")] - pub ssl_ciphers: MaybeString, - pub interfaces: MaybeString, - pub compression: MaybeString, + pub ssl_ciphers: Option, + pub interfaces: Option, + pub compression: Option, pub nohttpreferercheck: Option, }