Can create networks

This commit is contained in:
2023-10-31 09:26:42 +01:00
parent 335aec788e
commit f2237d4f2f
12 changed files with 328 additions and 65 deletions

View File

@ -1,11 +1,12 @@
use crate::app_config::AppConfig;
use crate::libvirt_lib_structures::{DomainState, DomainXML, DomainXMLUuid};
use crate::libvirt_lib_structures::{DomainState, DomainXML, NetworkXML, XMLUuid};
use crate::libvirt_rest_structures::*;
use actix::{Actor, Context, Handler, Message};
use image::ImageOutputFormat;
use std::io::Cursor;
use virt::connect::Connect;
use virt::domain::Domain;
use virt::network::Network;
use virt::stream::Stream;
use virt::sys;
use virt::sys::VIR_DOMAIN_XML_SECURE;
@ -63,11 +64,11 @@ impl Handler<GetHypervisorInfo> for LibVirtActor {
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<Vec<DomainXMLUuid>>")]
#[rtype(result = "anyhow::Result<Vec<XMLUuid>>")]
pub struct GetDomainsListReq;
impl Handler<GetDomainsListReq> for LibVirtActor {
type Result = anyhow::Result<Vec<DomainXMLUuid>>;
type Result = anyhow::Result<Vec<XMLUuid>>;
fn handle(&mut self, _msg: GetDomainsListReq, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Get full list of domains");
@ -75,7 +76,7 @@ impl Handler<GetDomainsListReq> for LibVirtActor {
let mut ids = Vec::with_capacity(domains.len());
for d in domains {
ids.push(DomainXMLUuid::parse_from_str(&d.get_uuid_string()?)?);
ids.push(XMLUuid::parse_from_str(&d.get_uuid_string()?)?);
}
Ok(ids)
@ -84,7 +85,7 @@ impl Handler<GetDomainsListReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<DomainXML>")]
pub struct GetDomainXMLReq(pub DomainXMLUuid);
pub struct GetDomainXMLReq(pub XMLUuid);
impl Handler<GetDomainXMLReq> for LibVirtActor {
type Result = anyhow::Result<DomainXML>;
@ -99,11 +100,11 @@ impl Handler<GetDomainXMLReq> for LibVirtActor {
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<DomainXMLUuid>")]
#[rtype(result = "anyhow::Result<XMLUuid>")]
pub struct DefineDomainReq(pub DomainXML);
impl Handler<DefineDomainReq> for LibVirtActor {
type Result = anyhow::Result<DomainXMLUuid>;
type Result = anyhow::Result<XMLUuid>;
fn handle(&mut self, mut msg: DefineDomainReq, _ctx: &mut Self::Context) -> Self::Result {
// A issue with the disks definition serialization needs them to be serialized aside
@ -121,14 +122,14 @@ impl Handler<DefineDomainReq> for LibVirtActor {
log::debug!("Define domain:\n{}", xml);
let domain = Domain::define_xml(&self.m, &xml)?;
DomainXMLUuid::parse_from_str(&domain.get_uuid_string()?)
XMLUuid::parse_from_str(&domain.get_uuid_string()?)
}
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct DeleteDomainReq {
pub id: DomainXMLUuid,
pub id: XMLUuid,
pub keep_files: bool,
}
@ -162,7 +163,7 @@ impl Handler<DeleteDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<DomainState>")]
pub struct GetDomainStateReq(pub DomainXMLUuid);
pub struct GetDomainStateReq(pub XMLUuid);
impl Handler<GetDomainStateReq> for LibVirtActor {
type Result = anyhow::Result<DomainState>;
@ -188,7 +189,7 @@ impl Handler<GetDomainStateReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct StartDomainReq(pub DomainXMLUuid);
pub struct StartDomainReq(pub XMLUuid);
impl Handler<StartDomainReq> for LibVirtActor {
type Result = anyhow::Result<()>;
@ -203,7 +204,7 @@ impl Handler<StartDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct ShutdownDomainReq(pub DomainXMLUuid);
pub struct ShutdownDomainReq(pub XMLUuid);
impl Handler<ShutdownDomainReq> for LibVirtActor {
type Result = anyhow::Result<()>;
@ -218,7 +219,7 @@ impl Handler<ShutdownDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct KillDomainReq(pub DomainXMLUuid);
pub struct KillDomainReq(pub XMLUuid);
impl Handler<KillDomainReq> for LibVirtActor {
type Result = anyhow::Result<()>;
@ -233,7 +234,7 @@ impl Handler<KillDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct ResetDomainReq(pub DomainXMLUuid);
pub struct ResetDomainReq(pub XMLUuid);
impl Handler<ResetDomainReq> for LibVirtActor {
type Result = anyhow::Result<()>;
@ -248,7 +249,7 @@ impl Handler<ResetDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct SuspendDomainReq(pub DomainXMLUuid);
pub struct SuspendDomainReq(pub XMLUuid);
impl Handler<SuspendDomainReq> for LibVirtActor {
type Result = anyhow::Result<()>;
@ -263,7 +264,7 @@ impl Handler<SuspendDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct ResumeDomainReq(pub DomainXMLUuid);
pub struct ResumeDomainReq(pub XMLUuid);
impl Handler<ResumeDomainReq> for LibVirtActor {
type Result = anyhow::Result<()>;
@ -278,7 +279,7 @@ impl Handler<ResumeDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<Vec<u8>>")]
pub struct ScreenshotDomainReq(pub DomainXMLUuid);
pub struct ScreenshotDomainReq(pub XMLUuid);
impl Handler<ScreenshotDomainReq> for LibVirtActor {
type Result = anyhow::Result<Vec<u8>>;
@ -311,7 +312,7 @@ impl Handler<ScreenshotDomainReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<bool>")]
pub struct IsDomainAutostart(pub DomainXMLUuid);
pub struct IsDomainAutostart(pub XMLUuid);
impl Handler<IsDomainAutostart> for LibVirtActor {
type Result = anyhow::Result<bool>;
@ -328,7 +329,7 @@ impl Handler<IsDomainAutostart> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<()>")]
pub struct SetDomainAutostart(pub DomainXMLUuid, pub bool);
pub struct SetDomainAutostart(pub XMLUuid, pub bool);
impl Handler<SetDomainAutostart> for LibVirtActor {
type Result = anyhow::Result<()>;
@ -344,3 +345,35 @@ impl Handler<SetDomainAutostart> for LibVirtActor {
Ok(())
}
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<XMLUuid>")]
pub struct DefineNetwork(pub NetworkXML);
impl Handler<DefineNetwork> for LibVirtActor {
type Result = anyhow::Result<XMLUuid>;
fn handle(&mut self, mut msg: DefineNetwork, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Define network: {:?}", msg.0);
// A issue with the IPs definition serialization needs them to be serialized aside
let mut ips_xml = Vec::with_capacity(msg.0.ips.len());
for ip in msg.0.ips {
log::debug!("Serialize {ip:?}");
let ip_xml = serde_xml_rs::to_string(&ip)?;
let start_offset = ip_xml.find("<ip").unwrap();
ips_xml.push(ip_xml[start_offset..].to_string());
}
msg.0.ips = vec![];
let mut network_xml = serde_xml_rs::to_string(&msg.0)?;
let ips_xml = ips_xml.join("\n");
network_xml = network_xml.replacen("</network>", &format!("{ips_xml}</network>"), 1);
log::debug!("Define network XML: {network_xml}");
let network = Network::define_xml(&self.m, &network_xml)?;
XMLUuid::parse_from_str(&network.get_uuid_string()?)
}
}