nzrd: stop the virt domain when deleting

This commit is contained in:
snow flurry 2024-08-15 20:17:41 -07:00
parent deaaaa3d10
commit 4edbe1a46d
2 changed files with 19 additions and 2 deletions

View file

@ -82,6 +82,13 @@ impl Domain {
.unwrap()
}
/// Stops the libvirt domain forcefully.
///
/// In libvirt terminology, this is equivalent to `virsh destroy <vm>`.
pub async fn stop(&mut self) -> Result<(), VirtError> {
self.spawn_virt(|virt| virt.destroy()).await
}
/// Undefines the libvirt domain.
/// If `deep` is set to true, all connected volumes are deleted.
pub async fn undefine(&mut self, deep: bool) -> Result<(), VirtError> {

View file

@ -196,8 +196,18 @@ pub async fn delete_instance(ctx: Context, name: String) -> Result<(), Box<dyn s
let Some(inst_db) = Instance::get_by_name(&ctx, &name).await? else {
return Err(cmd_error!("Instance {name} not found"));
};
let mut inst = ctx.virt.conn.get_instance(name.clone()).await?;
inst.undefine(true).await?;
// First, destroy the instance
match ctx.virt.conn.get_instance(name.clone()).await {
Ok(mut inst) => {
inst.stop().await?;
inst.undefine(true).await?;
}
Err(DomainError::DomainNotFound) => {
warn!("Deleting instance that exists in DB but not libvirt");
}
Err(err) => Err(err)?,
}
// Then, delete the DB entity
inst_db.delete(&ctx).await?;
Ok(())