emu_cli/
vm.rs

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use super::{
    config_storage::XDGConfigStorage,
    supervisor::{PidSupervisor, SystemdSupervisor},
    traits::{ConfigStorageHandler, SupervisorHandler, Supervisors},
};
use crate::config::Configuration;
use anyhow::Result;
use serde::{de::Visitor, Deserialize, Serialize};
use std::{fmt::Display, path::PathBuf, sync::Arc};

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct VM {
    name: String,
    cdrom: Option<PathBuf>,
    extra_disk: Option<PathBuf>,
    config: Configuration,
    headless: bool,
    supervisor: Supervisors,
}

impl std::hash::Hash for VM {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state)
    }
}

impl Display for VM {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.name())
    }
}

impl From<String> for VM {
    fn from(value: String) -> Self {
        Self::new(value, Arc::new(Box::new(XDGConfigStorage::default())))
    }
}

impl VM {
    pub fn new(name: String, storage: Arc<Box<dyn ConfigStorageHandler>>) -> Self {
        let mut obj = Self {
            name,
            ..Default::default()
        };

        if SystemdSupervisor::default().storage().exists(&obj) {
            obj.supervisor = Supervisors::Systemd;
        }

        obj.load_config(storage);
        obj
    }

    pub fn set_headless(&mut self, headless: bool) {
        self.headless = headless
    }

    pub fn headless(&self) -> bool {
        self.headless
    }

    pub fn name(&self) -> String {
        self.name.clone()
    }

    pub fn cdrom(&self) -> Option<PathBuf> {
        self.cdrom.clone()
    }

    pub fn set_cdrom(&mut self, cdrom: PathBuf) {
        self.cdrom = Some(cdrom)
    }

    pub fn extra_disk(&self) -> Option<PathBuf> {
        self.cdrom.clone()
    }

    pub fn set_extra_disk(&mut self, extra_disk: PathBuf) {
        self.extra_disk = Some(extra_disk)
    }

    pub fn config(&self) -> Configuration {
        self.config.clone()
    }

    pub fn supervisor(&self) -> Arc<Box<dyn SupervisorHandler>> {
        match self.supervisor {
            Supervisors::Systemd => Arc::new(Box::new(SystemdSupervisor::default())),
            _ => Arc::new(Box::new(PidSupervisor::default())),
        }
    }

    pub fn load_config(&mut self, storage: Arc<Box<dyn ConfigStorageHandler>>) {
        self.config = Configuration::from_file(storage.config_path(self));
    }

    pub fn save_config(&mut self, storage: Arc<Box<dyn ConfigStorageHandler>>) -> Result<()> {
        self.config.to_file(storage.config_path(self))
    }

    pub fn set_config(&mut self, config: Configuration) {
        self.config = config;
    }
}

impl Serialize for VM {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.name)
    }
}

struct VMVisitor;

impl Visitor<'_> for VMVisitor {
    type Value = VM;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("expecting a vm name")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> {
        Ok(v.to_string().into())
    }
}

impl<'de> Deserialize<'de> for VM {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(VMVisitor)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config_storage::XDGConfigStorage;
    use anyhow::Result;
    use std::sync::Arc;
    use tempfile::tempdir;

    #[test]
    fn test_into() -> Result<()> {
        let vm1: VM = "vm1".to_string().into();
        assert_eq!(vm1.name(), "vm1".to_string());
        Ok(())
    }

    #[test]
    fn test_serde() -> Result<()> {
        let vm1: VM = "vm1".to_string().into();
        assert_eq!(serde_json::to_string(&vm1)?, "\"vm1\"");
        let vm1: VM = serde_json::from_str("\"vm1\"")?;
        assert_eq!(vm1.name(), "vm1".to_string());
        Ok(())
    }

    #[test]
    fn test_vm_operations() -> Result<()> {
        let dir = tempdir()?;
        let base_path = dir.into_path();
        let storage: Arc<Box<dyn ConfigStorageHandler>> =
            Arc::new(Box::new(XDGConfigStorage::new(base_path)));

        let mut vm = VM::new("vm1".to_string(), storage.clone());
        storage.create(&vm)?;

        assert!(!vm.supervisor().supervised());
        assert!(!vm.supervisor().is_active(&vm)?);
        let mut config = vm.config();
        config.machine.ssh_port = 2000;
        vm.set_config(config.clone());
        vm.save_config(storage.clone())?;
        vm.load_config(storage.clone());
        assert_eq!(vm.config(), config);

        vm.set_cdrom(PathBuf::from("/cdrom"));
        assert_eq!(vm.cdrom(), Some(PathBuf::from("/cdrom")));
        vm.set_extra_disk(PathBuf::from("/cdrom"));
        assert_eq!(vm.extra_disk(), Some(PathBuf::from("/cdrom")));
        vm.set_headless(true);
        assert!(vm.headless());

        Ok(())
    }
}