summaryrefslogtreecommitdiff
path: root/src/cmd/delete.rs
blob: ac8c5b43200ce29dba4cf78185333c1856c2d9dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::spec::Specification;
use log::{debug, info};
use std::thread;
use std::time::Duration;
use virt::connect::Connect;

#[derive(Debug, thiserror::Error)]
pub enum DeleteError {
    #[error(transparent)]
    VirtError(#[from] virt::error::Error),

    #[error(transparent)]
    IoError(#[from] std::io::Error),
}

pub fn delete(spec: &Specification) -> Result<(), DeleteError> {
    info!("deleting virtual machine {}", spec.name);

    debug!("connecting to libvirtd");
    let conn = Connect::open("qemu:///system")?;

    debug!("listing all domains");
    let domains = conn.list_all_domains(0)?;

    for domain in domains {
        debug!("considering {}", domain.get_name()?);
        if domain.get_name()? == spec.name {
            debug!("shutdown {}", spec.name);
            domain.shutdown().ok();

            let briefly = Duration::from_millis(1000);
            loop {
                thread::sleep(briefly);
                match domain.is_active() {
                    Ok(true) => (),
                    Ok(false) => break,
                    Err(err) => {
                        debug!("is_active: {}", err);
                    }
                }
                debug!("{} is still running", spec.name);
            }

            debug!("undefine {}", spec.name);
            domain.undefine()?;

            debug!("removing image file {}", spec.image.display());
            std::fs::remove_file(&spec.image)?;
        }
    }
    Ok(())
}