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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use super::vm::VM;
use anyhow::Result;
use std::{fmt::Debug, path::PathBuf, process::ExitStatus, sync::Arc};

const DEFAULT_SNAPSHOT_TAG: &str = "[EMU-Suspend]";

#[derive(Debug, Clone, Default)]
pub enum Supervisors {
    Systemd,
    #[default]
    Pid,
}

pub trait ImageHandler: Debug {
    fn import(&self, new_file: PathBuf, orig_file: PathBuf, format: String) -> Result<()>;
    fn create(&self, target: PathBuf, gbs: usize) -> Result<()>;
    fn remove(&self, disk: PathBuf) -> Result<()>;
    fn clone_image(&self, description: String, old: PathBuf, new: PathBuf) -> Result<()>;
}

pub trait SupervisorHandler: Debug {
    fn reload(&self) -> Result<()>;
    fn pidof(&self, vm: &VM) -> Result<u32>;
    fn is_active(&self, vm: &VM) -> Result<bool>;
    fn supervised(&self) -> bool;
    fn storage(&self) -> Arc<Box<dyn SupervisorStorageHandler>>;
    fn kind(&self) -> Supervisors;
}

pub trait SupervisorStorageHandler: Debug {
    fn service_name(&self, vm: &VM) -> String;
    fn service_filename(&self, vm: &VM) -> PathBuf;
    fn remove(&self, vm: &VM) -> Result<()>;
    fn create(&self, vm: &VM) -> Result<()>;
    fn list(&self) -> Result<Vec<String>>;
    fn exists(&self, vm: &VM) -> bool;
}

pub trait ConfigStorageHandler: Debug {
    fn base_path(&self) -> PathBuf;
    fn config_path(&self, vm: &VM) -> PathBuf;
    fn vm_root(&self, vm: &VM) -> PathBuf;
    fn monitor_path(&self, vm: &VM) -> PathBuf;
    fn write_config(&self, vm: VM) -> Result<()>;
    fn vm_exists(&self, vm: &VM) -> bool;
    fn vm_list(&self) -> Result<Vec<VM>>;
    fn vm_path(&self, vm: &VM, filename: &str) -> PathBuf;
    fn vm_path_exists(&self, vm: &VM, filename: &str) -> bool;
    fn rename(&self, old: &VM, new: &VM) -> Result<()>;
    fn disk_list(&self, vm: &VM) -> Result<Vec<PathBuf>>;
    fn pidfile(&self, vm: &VM) -> PathBuf;
    fn size(&self, vm: &VM) -> Result<usize>;
}

pub trait Launcher: Debug {
    fn launch_attached(&self, vm: &VM) -> Result<ExitStatus>;
    fn launch_detached(&self, vm: &VM) -> Result<()>;
    fn shutdown_wait(&self, vm: &VM) -> Result<ExitStatus>;
    fn shutdown_immediately(&self, vm: &VM) -> Result<()>;
    fn snapshot(&self, vm: &VM, name: String) -> Result<()>;
    fn restore(&self, vm: &VM, name: String) -> Result<()>;
    fn delete_snapshot(&self, vm: &VM, name: String) -> Result<()>;

    fn save_state(&self, vm: &VM) -> Result<()> {
        self.snapshot(vm, DEFAULT_SNAPSHOT_TAG.to_string())
    }

    fn load_state(&self, vm: &VM) -> Result<()> {
        self.restore(vm, DEFAULT_SNAPSHOT_TAG.to_string())
    }

    fn clear_state(&self, vm: &VM) -> Result<()> {
        self.delete_snapshot(vm, DEFAULT_SNAPSHOT_TAG.to_string())
    }
}