aboutsummaryrefslogtreecommitdiff
path: root/src/star.rs
blob: 75368cd1f13a06a330f3ef2a2b4aed7a108f123f (plain) (blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*!
  * `Star`s are where [crate::planet::Planet]s orbit (the physical hypervisors that
  * libvirtd connects to
  */
use crate::planet::*;
use crate::errors::Error;
use crate::ship::Ship;
use serde::{Serialize, Deserialize};
use virt::{connect::Connect, domain::Domain};
use std::process::{ExitStatus};
use std::os::unix::process::ExitStatusExt;
use rand::Rng;

use std::{process::Command};

#[derive(Serialize, Deserialize, Debug)]
pub enum Address {
    IP(String),
    Domain(String)
}

impl ToString for Address {
    fn to_string(&self) -> String {
        match self {
            Address::IP(s) => s.clone(),
            Address::Domain(s) => s.clone(),
        }
    }
}

/// Defines a "star" where [crate::planet::Planet]s orbit
#[derive(Debug, Serialize, Deserialize)]
pub struct Star {
    /// Hostname
    pub name: String,
    /// FQDN or IP address, a way to talk to the planet
    pub address: String,

    /// Whether or not the Planet is local (same machine) or remote (networked machine)
    pub remote: bool,

    /// Connection to the House, if available
    #[serde(skip)]
    con: Option<Connect>,
}

/// The proper JSON request to make to a solard server to introduce a new planet
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NewPlanet {
    pub name: String,
    pub max_mem: Memory,
    pub max_cpus: CpuCount,
    pub disk_size_mb: u64,
    /// shasum of the ship
    pub ship: String,
}

impl Star { 
    /// Creates a new Planet based on a libvirtd connect URL
    ///
    /// Example:
    /// ```
    /// use waifulib::planet::Planet;
    /// let mut h = Planet::new("test:///default".to_string()).unwrap();
    /// ```
    pub fn new(url: String) -> Result<Self, Error> {
        let c = Connect::open(&url.clone())?;

        let remote = if url.contains("qemu:///") || url.contains("localhost") || url.contains("127.0.0.1") {
            false
        } else {
            true
        };

        // If the connection succeeds, we've got one!
        Ok(Self {
            name: c.get_hostname()?,
            address: c.get_uri()?,
            remote,
            con: Some(c),
        })
    }

    /// Lists the "inhabitants" of the House (the [Waifu]s on the machine
    ///
    /// ```
    /// use crate::star::Star;
    /// let mut h = Star::new("test:///default".to_string()).unwrap();
    ///
    /// assert_eq!(h.inhabitants().unwrap().len(), 1);
    /// ```
    pub fn inhabitants(&mut self) -> Result<Vec<Planet>, Error> {
        match &self.con {
            Some(c) => {
                let domains = c.list_all_domains(0)?;

                let mut planets: Vec<Planet> = Vec::new();

                for d in domains.iter() {
                    planets.push(d.clone().try_into()?);
                }
                Ok(planets)
            },
            None => {
                return Err(Error::Connection("Domain connection was None".to_string()));
            }
        }
    }

    pub fn find_planet(&mut self, uuid: String) -> Result<Planet, Error> {
        let inhab = self.inhabitants()?;

        for i in inhab {
            if i.uuid == uuid {
                return Ok(i);
            }
        }
        Err(Error::Other(String::from("Not found")))
    }

    /// Creates a new Planet orbiting the Star, taking care of everything needed to make the VM run
    /// fine
    ///
    /// If the installation image doesn't exist in the default libvirtd pool, this will fail with
    /// [`Error::MissingImage`][crate::errors::Error::MissingImage].
    ///
    pub fn planet(&mut self, name: String, max_mem: Memory, max_cpus: CpuCount, disk_size_mb: u64, ship: Ship) -> Result<Planet, Error> {
        // Check for image on host machine
        
        if self.remote {
            let mut output = Command::new("ssh")
                .args([
                      "-oStrictHostKeyChecking=accept-new",
                      &self.address.clone(),
                      "stat",
                      &format!("/var/lib/libvirt/images/{}", ship.make_pretty_name().clone())
                ])
                .output()?; 

            if output.status != ExitStatus::from_raw(0) {
               return Err(Error::MissingImage(ship.name.clone()));
            }

            // Allocate VM disk
            output = Command::new("ssh")
                .args([
                      "-oStrictHostKeyChecking=accept-new",
                      &self.address.clone(),
                      "qemu-img",
                      "create",
                      "-f",
                      "qcow2",
                      &format!("/var/lib/libvirt/images/{}.qcow2", name.clone()),
                      &format!("{}M", disk_size_mb)
                ])
                .output()?;

            if output.status != ExitStatus::from_raw(0) {
                return Err(Error::Allocation(String::from_utf8(output.stdout).unwrap()));
            }
        } else {
            // It's local
            let mut output = Command::new("stat")
                .args([
                      &format!("/var/lib/libvirt/images/{}", ship.make_pretty_name().clone()),
                ])
                .output()?;

            if output.status != ExitStatus::from_raw(0) {
                return Err(Error::MissingImage(ship.name.clone()));
            }

            output = Command::new("qemu-img")
                .args([
                      "create",
                      "-f",
                      "qcow2",
                      &format!("/var/lib/libvirt/images/{}.qcow2", name.clone()),
                      &format!("{}M", disk_size_mb)
                ])
                .output()?;

            if output.status != ExitStatus::from_raw(0) {
                return Err(Error::Allocation(String::from_utf8(output.stdout).unwrap()));
            }
        }

        let uuid = uuid::Uuid::new_v4();
        
        // Let's get that XML ready
        let mut buf: Vec<u8> = vec![];

        crate::templates::vm_xml(
            &mut buf,
            name.clone(),
            uuid.to_string(),
            random_mac().clone(),
            true,
            max_mem.0,
            max_cpus.0,
            "blank".to_string()
            )?;

        let buf = String::from_utf8(buf).unwrap();

        let dom: Domain = match &self.con {
                Some(c) => {
                    let dom = Domain::define_xml(&c, &buf)?;

                    dom.create()?;

                    dom
                },
                None => {
                    return Err(Error::Connection("Connection was None".to_string()));
                }
            };

        dom.try_into()
    }

}

fn random_mac() -> String {
    let mut addr = rand::thread_rng().gen::<[u8; 6]>();
    addr[0] = (addr[0] | 2) & 0xfe;
    mac_address::MacAddress::new(addr).to_string()
}

#[cfg(test)]
mod test {
    use super::*;
    /// Kind of a stupid test, but just a sanity check to make sure that [ToString] impl up above
    /// is still working
    #[test]
    fn addr_to_string() {
        let a: Address = Address::IP("127.0.0.1".to_string());
        let d: Address = Address::Domain("example.com".to_string());

        assert_eq!(String::from("127.0.0.1"), a.to_string());
        assert_eq!("example.com".to_string(), d.to_string());
    }

    #[test]
    fn connect_to_house() {
        let _: Star = Star::new("test:///default".to_string()).unwrap();
    }

    #[test]
    fn list_inhabitants() {
        let mut h: Star = Star::new("test:///default".to_string()).unwrap();

        assert_eq!(h.inhabitants().unwrap().len(), 1);        
    }

    #[test]
    fn get_planet() {
        let mut s: Star = Star::new("test:///default".to_string()).unwrap();

        let t = s.inhabitants().unwrap()[0].clone();

        assert_eq!(s.find_planet(t.uuid.clone()).unwrap(), t);
    }

}