1use super::vm::VM;
2use anyhow::Result;
3use std::{fmt::Debug, path::PathBuf, process::ExitStatus, sync::Arc};
4
5const DEFAULT_SNAPSHOT_TAG: &str = "[EMU-Suspend]";
6
7#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
8pub enum Supervisors {
9 Systemd,
10 #[default]
11 Pid,
12}
13
14pub trait ImageHandler: Debug {
15 fn import(&self, new_file: PathBuf, orig_file: PathBuf, format: String) -> Result<()>;
16 fn create(&self, target: PathBuf, gbs: usize) -> Result<PathBuf>;
17 fn remove(&self, disk: PathBuf) -> Result<()>;
18 fn clone_image(&self, description: String, old: PathBuf, new: PathBuf) -> Result<()>;
19}
20
21pub trait SupervisorHandler: Debug {
22 fn reload(&self) -> Result<()>;
23 fn pidof(&self, vm: &VM) -> Result<u32>;
24 fn is_active(&self, vm: &VM) -> Result<bool>;
25 fn supervised(&self) -> bool;
26 fn storage(&self) -> Arc<Box<dyn SupervisorStorageHandler>>;
27 fn kind(&self) -> Supervisors;
28}
29
30pub trait SupervisorStorageHandler: Debug {
31 fn service_name(&self, vm: &VM) -> String;
32 fn service_filename(&self, vm: &VM) -> PathBuf;
33 fn remove(&self, vm: &VM) -> Result<()>;
34 fn create(&self, vm: &VM) -> Result<()>;
35 fn list(&self) -> Result<Vec<String>>;
36 fn exists(&self, vm: &VM) -> bool;
37}
38
39pub trait ConfigStorageHandler: Debug {
40 fn create(&self, vm: &VM) -> Result<()>;
41 fn delete(&self, vm: &VM, disk: Option<String>) -> Result<()>;
42 fn running_vms(&self) -> Result<Vec<VM>>;
43 fn base_path(&self) -> PathBuf;
44 fn config_path(&self, vm: &VM) -> PathBuf;
45 fn vm_root(&self, vm: &VM) -> PathBuf;
46 fn monitor_path(&self, vm: &VM) -> PathBuf;
47 fn write_config(&self, vm: VM) -> Result<()>;
48 fn vm_exists(&self, vm: &VM) -> bool;
49 fn vm_list(&self) -> Result<Vec<VM>>;
50 fn vm_path(&self, vm: &VM, filename: &str) -> PathBuf;
51 fn vm_path_exists(&self, vm: &VM, filename: &str) -> bool;
52 fn rename(&self, old: &VM, new: &VM) -> Result<()>;
53 fn disk_list(&self, vm: &VM) -> Result<Vec<PathBuf>>;
54 fn pidfile(&self, vm: &VM) -> PathBuf;
55 fn size(&self, vm: &VM) -> Result<usize>;
56}
57
58pub trait Launcher: Debug {
59 fn launch_attached(&self, vm: &VM) -> Result<ExitStatus>;
60 fn launch_detached(&self, vm: &VM) -> Result<()>;
61 fn shutdown_wait(&self, vm: &VM) -> Result<ExitStatus>;
62 fn shutdown_immediately(&self, vm: &VM) -> Result<()>;
63 fn reset(&self, vm: &VM) -> Result<()>;
64 fn snapshot(&self, vm: &VM, name: String) -> Result<()>;
65 fn restore(&self, vm: &VM, name: String) -> Result<()>;
66 fn delete_snapshot(&self, vm: &VM, name: String) -> Result<()>;
67
68 fn save_state(&self, vm: &VM) -> Result<()> {
69 self.snapshot(vm, DEFAULT_SNAPSHOT_TAG.to_string())
70 }
71
72 fn load_state(&self, vm: &VM) -> Result<()> {
73 self.restore(vm, DEFAULT_SNAPSHOT_TAG.to_string())
74 }
75
76 fn clear_state(&self, vm: &VM) -> Result<()> {
77 self.delete_snapshot(vm, DEFAULT_SNAPSHOT_TAG.to_string())
78 }
79
80 fn restart(&self, vm: &VM) -> Result<()> {
81 self.shutdown_wait(vm)?;
82 self.launch_detached(vm)
83 }
84}