nzrd: stop the virt domain when deleting
This commit is contained in:
parent
deaaaa3d10
commit
4edbe1a46d
2 changed files with 19 additions and 2 deletions
|
@ -82,6 +82,13 @@ impl Domain {
|
||||||
.unwrap()
|
.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.
|
/// Undefines the libvirt domain.
|
||||||
/// If `deep` is set to true, all connected volumes are deleted.
|
/// If `deep` is set to true, all connected volumes are deleted.
|
||||||
pub async fn undefine(&mut self, deep: bool) -> Result<(), VirtError> {
|
pub async fn undefine(&mut self, deep: bool) -> Result<(), VirtError> {
|
||||||
|
|
|
@ -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 {
|
let Some(inst_db) = Instance::get_by_name(&ctx, &name).await? else {
|
||||||
return Err(cmd_error!("Instance {name} not found"));
|
return Err(cmd_error!("Instance {name} not found"));
|
||||||
};
|
};
|
||||||
let mut inst = ctx.virt.conn.get_instance(name.clone()).await?;
|
// First, destroy the instance
|
||||||
inst.undefine(true).await?;
|
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?;
|
inst_db.delete(&ctx).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue