emu_cli/
template.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
use super::vm::VM;
use anyhow::{anyhow, Result};
use serde::Serialize;
use tinytemplate::TinyTemplate;

const EMU_DEFAULT_PATH: &str = "/bin/emu";

const SYSTEMD_UNIT: &str = "
[Unit]
Description=Virtual Machine: {vm_name}

[Service]
Type=simple
ExecStart={emu_path} run -e {vm_name}
TimeoutStopSec=30
ExecStop={emu_path} shutdown {vm_name}
KillSignal=SIGCONT
FinalKillSignal=SIGKILL

[Install]
WantedBy=default.target
";

#[derive(Serialize)]
pub struct Data {
    vm_name: String,
    emu_path: String,
}

impl Data {
    pub fn new(vm_name: String) -> Self {
        Self {
            vm_name,
            emu_path: match std::env::current_exe() {
                Ok(path) => path.to_str().unwrap().to_string(),
                Err(_) => EMU_DEFAULT_PATH.to_string(),
            },
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct Systemd;

impl Systemd {
    pub fn template(&self, vm: &VM) -> Result<String> {
        let mut t = TinyTemplate::new();
        t.add_template("systemd", SYSTEMD_UNIT)?;
        let data = Data::new(vm.name());
        match t.render("systemd", &data) {
            Ok(x) => Ok(x),
            Err(e) => Err(anyhow!(e)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;

    #[test]
    fn test_template() -> Result<()> {
        let out = Systemd::template(&Systemd, &"vm1".to_string().into())?;
        assert!(out.contains("vm1"));

        Ok(())
    }
}