feat: Can now discover inventory agent and download its host definition, next up save it to db
This commit is contained in:
@@ -12,6 +12,9 @@ log.workspace = true
|
||||
env_logger.workspace = true
|
||||
tokio.workspace = true
|
||||
thiserror.workspace = true
|
||||
reqwest.workspace = true
|
||||
# mdns-sd = "0.14.1"
|
||||
mdns-sd = { git = "https://github.com/jggc/mdns-sd.git", branch = "patch-1" }
|
||||
local-ip-address = "0.6.5"
|
||||
harmony_types = { path = "../harmony_types" }
|
||||
harmony_macros = { path = "../harmony_macros" }
|
||||
|
||||
14
harmony_inventory_agent/src/client.rs
Normal file
14
harmony_inventory_agent/src/client.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use crate::hwinfo::PhysicalHost;
|
||||
|
||||
pub fn get_host_inventory(host: &str, port: u16) -> Result<PhysicalHost, String> {
|
||||
let url = format!("http://{host}:{port}/inventory");
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client
|
||||
.get(url)
|
||||
.send()
|
||||
.map_err(|e| format!("Failed to download file: {e}"))?;
|
||||
|
||||
let host = response.json().map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(host)
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use harmony_types::net::MacAddress;
|
||||
use log::{debug, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
@@ -63,10 +64,10 @@ pub struct Chipset {
|
||||
pub vendor: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct NetworkInterface {
|
||||
pub name: String,
|
||||
pub mac_address: String,
|
||||
pub mac_address: MacAddress,
|
||||
pub speed_mbps: Option<u32>,
|
||||
pub is_up: bool,
|
||||
pub mtu: u32,
|
||||
@@ -76,6 +77,25 @@ pub struct NetworkInterface {
|
||||
pub firmware_version: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl NetworkInterface {
|
||||
pub fn dummy() -> Self {
|
||||
use harmony_macros::mac_address;
|
||||
|
||||
Self {
|
||||
name: String::new(),
|
||||
mac_address: mac_address!("00:00:00:00:00:00"),
|
||||
speed_mbps: Some(0),
|
||||
is_up: false,
|
||||
mtu: 0,
|
||||
ipv4_addresses: vec![],
|
||||
ipv6_addresses: vec![],
|
||||
driver: String::new(),
|
||||
firmware_version: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ManagementInterface {
|
||||
pub kind: String,
|
||||
@@ -509,6 +529,7 @@ impl PhysicalHost {
|
||||
|
||||
let mac_address = Self::read_sysfs_string(&iface_path.join("address"))
|
||||
.map_err(|e| format!("Failed to read MAC address for {}: {}", iface_name, e))?;
|
||||
let mac_address = MacAddress::try_from(mac_address).map_err(|e| e.to_string())?;
|
||||
|
||||
let speed_mbps = if iface_path.join("speed").exists() {
|
||||
match Self::read_sysfs_u32(&iface_path.join("speed")) {
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
mod hwinfo;
|
||||
pub mod hwinfo;
|
||||
pub mod local_presence;
|
||||
pub mod client;
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
use log::{debug, error};
|
||||
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
||||
|
||||
use crate::local_presence::SERVICE_NAME;
|
||||
|
||||
pub type DiscoveryEvent = ServiceEvent;
|
||||
|
||||
pub fn discover_agents(timeout: Option<u64>, on_event: impl Fn(DiscoveryEvent) + Send + 'static) {
|
||||
pub fn discover_agents(
|
||||
timeout: Option<u64>,
|
||||
on_event: impl Fn(DiscoveryEvent) -> Result<(), String> + Send + 'static,
|
||||
) {
|
||||
// Create a new mDNS daemon.
|
||||
let mdns = ServiceDaemon::new().expect("Failed to create mDNS daemon");
|
||||
|
||||
@@ -14,13 +18,15 @@ pub fn discover_agents(timeout: Option<u64>, on_event: impl Fn(DiscoveryEvent) +
|
||||
|
||||
std::thread::spawn(move || {
|
||||
while let Ok(event) = receiver.recv() {
|
||||
on_event(event.clone());
|
||||
if let Err(e) = on_event(event.clone()) {
|
||||
error!("Event callback failed : {e}");
|
||||
}
|
||||
match event {
|
||||
ServiceEvent::ServiceResolved(resolved) => {
|
||||
println!("Resolved a new service: {}", resolved.fullname);
|
||||
debug!("Resolved a new service: {}", resolved.fullname);
|
||||
}
|
||||
other_event => {
|
||||
println!("Received other event: {:?}", &other_event);
|
||||
debug!("Received other event: {:?}", &other_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user