Automatically backup source network and VM configuration

This commit is contained in:
2023-12-23 18:12:46 +01:00
parent d053490a47
commit d8a6b58c52
7 changed files with 106 additions and 51 deletions

View File

@ -115,17 +115,24 @@ impl Handler<GetSourceDomainXMLReq> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<XMLUuid>")]
pub struct DefineDomainReq(pub DomainXML);
pub struct DefineDomainReq(pub VMInfo, pub DomainXML);
impl Handler<DefineDomainReq> for LibVirtActor {
type Result = anyhow::Result<XMLUuid>;
fn handle(&mut self, msg: DefineDomainReq, _ctx: &mut Self::Context) -> Self::Result {
let xml = msg.0.into_xml()?;
fn handle(&mut self, mut msg: DefineDomainReq, _ctx: &mut Self::Context) -> Self::Result {
let xml = msg.1.into_xml()?;
log::debug!("Define domain:\n{}", xml);
let domain = Domain::define_xml(&self.m, &xml)?;
XMLUuid::parse_from_str(&domain.get_uuid_string()?)
let uuid = XMLUuid::parse_from_str(&domain.get_uuid_string()?)?;
// Save a copy of the source definition
msg.0.uuid = Some(uuid);
let json = serde_json::to_string(&msg.0)?;
std::fs::write(AppConfig::get().vm_definition_path(&msg.0.name), json)?;
Ok(uuid)
}
}
@ -155,6 +162,12 @@ impl Handler<DeleteDomainReq> for LibVirtActor {
std::fs::remove_file(vnc_socket)?;
}
// Remove backup definition
let backup_definition = AppConfig::get().vm_definition_path(&domain_name);
if backup_definition.exists() {
std::fs::remove_file(backup_definition)?;
}
// Delete the domain
domain.undefine_flags(match msg.keep_files {
true => sys::VIR_DOMAIN_UNDEFINE_KEEP_NVRAM,
@ -360,20 +373,27 @@ impl Handler<SetDomainAutostart> for LibVirtActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<XMLUuid>")]
pub struct DefineNetwork(pub NetworkXML);
pub struct DefineNetwork(pub NetworkInfo, pub NetworkXML);
impl Handler<DefineNetwork> for LibVirtActor {
type Result = anyhow::Result<XMLUuid>;
fn handle(&mut self, msg: DefineNetwork, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Define network: {:?}", msg.0);
fn handle(&mut self, mut msg: DefineNetwork, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Define network: {:?}", msg.1);
log::debug!("Source network structure: {:#?}", msg.0);
let network_xml = msg.0.into_xml()?;
log::debug!("Source network structure: {:#?}", msg.1);
let network_xml = msg.1.into_xml()?;
log::debug!("Define network XML: {network_xml}");
let network = Network::define_xml(&self.m, &network_xml)?;
XMLUuid::parse_from_str(&network.get_uuid_string()?)
let uuid = XMLUuid::parse_from_str(&network.get_uuid_string()?)?;
// Save a copy of the source definition
msg.0.uuid = Some(uuid);
let json = serde_json::to_string(&msg.0)?;
std::fs::write(AppConfig::get().net_definition_path(&msg.0.name), json)?;
Ok(uuid)
}
}
@ -437,7 +457,15 @@ impl Handler<DeleteNetwork> for LibVirtActor {
fn handle(&mut self, msg: DeleteNetwork, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Delete network: {}\n", msg.0.as_string());
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
let network_name = network.get_name()?;
network.undefine()?;
// Remove backup definition
let backup_definition = AppConfig::get().net_definition_path(&network_name);
if backup_definition.exists() {
std::fs::remove_file(backup_definition)?;
}
Ok(())
}
}