From 6b995785c780dd47cb0e02821001f446cf4ec211 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Wed, 20 Apr 2022 13:08:28 -0400 Subject: house: Allow for the introduction of new VMs Templates the XML and creates the disk images --- src/waifu.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/waifu.rs') diff --git a/src/waifu.rs b/src/waifu.rs index 55ff4ed..da9653f 100644 --- a/src/waifu.rs +++ b/src/waifu.rs @@ -15,6 +15,12 @@ impl From for Memory { Self(u) } } + +impl Memory { + pub fn count(&self) -> u64 { + self.0 + } +} /** * Defines the number of vCPUs a waifu has */ @@ -27,6 +33,11 @@ impl From for CpuCount { } } +impl CpuCount { + pub fn count(&self) -> u64 { + self.0 + } +} /** * Represents a virtual machine, that's active on some server * @@ -59,6 +70,47 @@ impl PartialEq for Waifu { } } +impl TryFrom for Waifu { + type Error = Error; + + fn try_from(d: Domain) -> Result { + let c = d.get_connect()?; + + // This... feels wrong + // + // I know it probably works + // + // Based on code by Cadey in waifud + let addr: Option = if d.is_active()? { + let mut addr: Vec = d + .interface_addresses(virt::domain::VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE, 0)? + .into_iter() + .map(|iface| iface.addrs.clone()) + .filter(|addrs| addrs.get(0).is_some()) + .map(|addrs| addrs.get(0).unwrap().clone().addr) + .collect(); + + if addr.get(0).is_none() { + Some(String::from("localhost")) + } else { + Some(addr.swap_remove(0)) + } + } else { + None + }; + + Ok(Self { + name: d.get_name()?, + host: c.get_hostname()?, + addr, + uuid: d.get_uuid_string()?, + mem: d.get_max_memory()?.into(), + cpu_count: d.get_max_vcpus()?.into(), + }) + } +} + + impl TryFrom<&Domain> for Waifu { type Error = Error; -- cgit v1.2.3