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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::{collections::BTreeMap, net::Ipv4Addr};

use crate::VmId;

/// Type representing a VM
///
/// Note that the "other" property contains a lot of different data. In the Xen Orchestra the user may
/// even add additional values. For this reason the struct is made generic over the type `O`.
/// See the trait [`OtherInfo`] for more info
///
/// Also see https://github.com/vatesfr/xen-orchestra/blob/a505cd9567233aab7ca6488b2fb8a0b6c610fa08/packages/xo-server/src/xapi-object-to-xo.mjs#L273
#[derive(serde::Deserialize, Debug)]
pub struct Vm<O> {
    pub id: VmId,
    pub name_label: String,
    pub name_description: String,
    pub power_state: PowerState,

    #[serde(rename = "$pool")]
    pub pool: String,
    pub tags: Vec<String>,

    #[serde(default)]
    addresses: BTreeMap<String, String>,

    #[serde(deserialize_with = "map_from_optional_map", default)]
    pub os_version: BTreeMap<String, String>,

    pub other: O,
}

/// Type describing power state of VM
#[derive(Debug, Clone, Copy, serde::Deserialize, PartialEq, Eq)]
pub enum PowerState {
    Running,
    Halted,
    Suspended,
    Paused,
}

impl<'a, O: serde::de::DeserializeOwned> Vm<O> {
    /// Check if VM is running.
    pub fn is_running(&self) -> bool {
        matches!(self.power_state, PowerState::Running)
    }

    /// Try to guess OS distro of VM
    ///
    /// Note: This only works for running VMs, returns `None` when distro
    /// can not be determined.
    pub fn distro(&self) -> Option<&str> {
        match &self.os_version.get("distro") {
            Some(distro) => Some(distro),
            None if self.os_version.contains_key("spmajor") => Some("windows"),
            None => None,
        }
    }

    /// Get iterator of all valid IPv4 addresses for VM.
    ///
    /// Note: This only works for running VMs, returns empty iterator otherwise
    pub fn ipv4_addresses(&self) -> impl Iterator<Item = Ipv4Addr> + '_ {
        self.addresses
            .iter()
            .filter(|(tag, _ip)| tag.contains("ipv4"))
            .flat_map(|(_tag, ip)| ip.split(' '))
            .filter_map(move |ip| match ip.parse() {
                Ok(ip) => Some(ip),
                Err(e) => {
                    log::warn!("Invalid IP found for VM: {}, {:?}", self.name_label, e);
                    None
                }
            })
    }
}

fn map_from_optional_map<'de, D>(des: D) -> Result<BTreeMap<String, String>, D::Error>
where
    D: serde::de::Deserializer<'de>,
{
    let option: Option<_> = serde::de::Deserialize::deserialize(des)?;
    Ok(option.unwrap_or_default())
}

/// This is the "other" section of VM from XO.
///
/// This secton contains some XO specific data like backup related values and what template
/// the VM was created from. Other that that there is also the "Custom Fields" from the
/// "Advanced" tab of the VM in XO. However note that all fields added from there will have
/// "XenCenter.CustomFields." as prefix in their key.
///
/// A type that can be deserialized from a flat string to string object.
pub trait OtherInfo: serde::de::DeserializeOwned {}

impl OtherInfo for BTreeMap<String, String> {}
impl OtherInfo for std::collections::HashMap<String, String> {}

#[cfg(test)]
mod tests {
    macro_rules! file_to_vm {
        ($path:literal) => {{
            let s = include_str!($path);

            let hash_vm: super::Vm<HashMap<String, String>> = serde_json::from_str(&s).unwrap();
            let tree_vm: super::Vm<BTreeMap<String, String>> = serde_json::from_str(&s).unwrap();
            (hash_vm, tree_vm)
        }};
    }

    use std::{
        collections::{BTreeMap, HashMap},
        iter::FromIterator,
        net::Ipv4Addr,
    };

    use crate::vm::PowerState;

    #[test]
    fn debian() {
        let debian = file_to_vm!("../test_data/vm/debian_10.json").0;

        assert_eq!(&debian.id.0, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
        assert_eq!(
            debian.addresses,
            slice_to_map::<BTreeMap<_, _>>(&[
                ("0/ipv4/0", "10.0.1.52"),
                ("0/ipv6/0", "fe80::dead:beaf:dead:beaf")
            ])
        );
        assert_eq!(
            debian.ipv4_addresses().collect::<Vec<_>>(),
            vec!["10.0.1.52".parse::<Ipv4Addr>().unwrap()]
        );
        assert!(debian.is_running());
        assert_eq!(debian.power_state, PowerState::Running);
        assert_eq!(debian.name_label, "debian 10");
        assert_eq!(debian.name_description, "Some description");
        assert_eq!(debian.tags, vec!["Test"]);
        assert_eq!(
            debian.os_version,
            slice_to_map(&[
                ("distro", "debian"),
                ("major", "10"),
                ("minor", "10"),
                ("name", "Debian GNU/Linux 10 (buster)"),
                ("uname", "4.19.0-13-amd64")
            ])
        );
        assert_eq!(debian.distro().unwrap(), "debian");
        assert_eq!(debian.pool, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
    }

    #[test]
    fn pfsense() {
        let pfsense = file_to_vm!("../test_data/vm/pfsense_2_5_1.json").1;

        assert_eq!(&pfsense.id.0, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
        assert_eq!(
            pfsense.addresses,
            slice_to_map::<BTreeMap<_, _>>(&[
                ("0/ipv4/0", "10.0.0.13 10.0.0.12 10.0.0.16 10.0.0.7"),
                ("0/ipv4/1", "10.0.0.12"),
                ("0/ipv4/2", "10.0.0.16"),
                ("0/ipv4/3", "10.0.0.7"),
                ("1/ipv4/0", "192.168.71.2 192.168.71.1"),
                ("1/ipv4/1", "192.168.71.1"),
                ("2/ipv4/0", "192.168.72.2 192.168.72.1"),
                ("2/ipv4/1", "192.168.72.1")
            ])
        );
        assert_eq!(
            pfsense.ipv4_addresses().collect::<Vec<_>>(),
            vec![
                "10.0.0.13",
                "10.0.0.12",
                "10.0.0.16",
                "10.0.0.7",
                "10.0.0.12",
                "10.0.0.16",
                "10.0.0.7",
                "192.168.71.2",
                "192.168.71.1",
                "192.168.71.1",
                "192.168.72.2",
                "192.168.72.1",
                "192.168.72.1"
            ]
            .iter()
            .map(|x| x.parse::<Ipv4Addr>().unwrap())
            .collect::<Vec<_>>()
        );
        assert!(pfsense.is_running());
        assert_eq!(pfsense.power_state, PowerState::Running);
        assert_eq!(pfsense.name_label, "pfsense 2.5.1");
        assert_eq!(pfsense.name_description, "Foo description");
        assert_eq!(pfsense.tags, vec!["pfsense", "Test"]);
        assert_eq!(
            pfsense.os_version,
            slice_to_map(&[
                ("distro", "FreeBSD"),
                ("name", "FreeBSD 12.2-STABLE"),
                ("uname", "12.2-STABLE")
            ])
        );
        assert_eq!(pfsense.distro().unwrap(), "FreeBSD");
        assert_eq!(pfsense.pool, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
    }

    #[test]
    fn ubuntu() {
        let ubuntu = file_to_vm!("../test_data/vm/ubuntu_18_04.json").1;

        assert_eq!(&ubuntu.id.0, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
        assert_eq!(
            ubuntu.addresses,
            slice_to_map::<BTreeMap<_, _>>(&[
                ("0/ipv4/0", "10.0.3.25"),
                ("0/ipv6/0", "fe80::dead:beaf:dead:beaf")
            ])
        );
        assert_eq!(
            ubuntu.ipv4_addresses().collect::<Vec<_>>(),
            vec!["10.0.3.25".parse::<Ipv4Addr>().unwrap()]
        );
        assert!(ubuntu.is_running());
        assert_eq!(ubuntu.power_state, PowerState::Running);
        assert_eq!(ubuntu.name_label, "ubuntu 18.04");
        assert_eq!(ubuntu.name_description, "Ubuntu Linux (64-bit)");
        assert_eq!(ubuntu.tags, vec!["Important", "Other tag"]);
        assert_eq!(
            ubuntu.os_version,
            slice_to_map(&[
                ("distro", "ubuntu"),
                ("major", "18"),
                ("minor", "04"),
                ("name", "Ubuntu 18.04.5 LTS"),
                ("uname", "4.15.0-136-generic")
            ])
        );
        assert_eq!(ubuntu.distro().unwrap(), "ubuntu");
        assert_eq!(ubuntu.pool, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
    }

    #[test]
    fn windows() {
        let windows = file_to_vm!("../test_data/vm/windows_10.json").0;

        assert_eq!(&windows.id.0, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
        assert_eq!(
            windows.addresses,
            slice_to_map::<BTreeMap<_, _>>(&[
                ("0/ipv4/0", "192.168.7.42"),
                ("0/ipv6/0", "fe80:0000:0000:0000:dead:beaf:dead:beaa"),
                ("1/ipv4/0", "192.168.8.42"),
                ("1/ipv6/0", "fe80:0000:0000:0000:dead:beaf:dead:beab"),
                ("2/ipv4/0", "192.168.9.42"),
                ("2/ipv6/0", "fe80:0000:0000:0000:dead:beaf:dead:beac"),
                ("3/ipv4/0", "169.254.149.176"),
                ("3/ipv6/0", "fe80:0000:0000:0000:dead:beaf:dead:bead")
            ])
        );
        assert_eq!(
            windows.ipv4_addresses().collect::<Vec<_>>(),
            vec![
                "192.168.7.42",
                "192.168.8.42",
                "192.168.9.42",
                "169.254.149.176",
            ]
            .iter()
            .map(|x| x.parse::<Ipv4Addr>().unwrap())
            .collect::<Vec<_>>()
        );
        assert!(windows.is_running());
        assert_eq!(windows.power_state, PowerState::Running);
        assert_eq!(windows.name_label, "windows 10");
        assert_eq!(windows.name_description, "Here is a description");
        assert_eq!(windows.tags, [String::new(); 0]);
        assert_eq!(
            windows.os_version,
            slice_to_map(&[("spmajor", "0"), ("spminor", "0")])
        );
        assert_eq!(windows.distro().unwrap(), "windows");
        assert_eq!(windows.pool, "deadbeaf-dead-beaf-dead-beafdeadbeaf");
    }

    #[test]
    fn other_info() {
        let (debian_hash, debian_tree) = file_to_vm!("../test_data/vm/debian_10.json");
        let (pfsense_hash, pfsense_tree) = file_to_vm!("../test_data/vm/pfsense_2_5_1.json");
        let (ubuntu_hash, ubuntu_tree) = file_to_vm!("../test_data/vm/ubuntu_18_04.json");
        let (windows_hash, windows_tree) = file_to_vm!("../test_data/vm/windows_10.json");

        let debian_expected = [
            ("XenCenter.CustomFields.foo", "bar"),
            ("XenCenter.CustomFields.baz", "quix"),
            ("auto_poweron", "true"),
            ("base_template_name", "Debian Buster 10"),
            (
                "import_task",
                "OpaqueRef:deadbeaf-dead-beaf-dead-beafdeadbeaf",
            ),
            ("install-methods", "cdrom,nfs,http,ftp"),
            ("linux_template", "true"),
            ("mac_seed", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
            ("xo:copy_of", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
        ];
        assert_eq!(debian_hash.other, slice_to_map(&debian_expected));
        assert_eq!(debian_tree.other, slice_to_map(&debian_expected));

        let pfsense_expected = [
            ("auto_poweron", "true"),
            ("base_template_name", "Other install media"),
            (
                "import_task",
                "OpaqueRef:deadbeaf-dead-beaf-dead-beafdeadbeaf",
            ),
            ("install-methods", "cdrom"),
            ("mac_seed", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
            ("xo:copy_of", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
        ];
        assert_eq!(pfsense_hash.other, slice_to_map(&pfsense_expected));
        assert_eq!(pfsense_tree.other, slice_to_map(&pfsense_expected));

        let ubuntu_expected = [
            ("auto_poweron", "true"),
            ("base_template_name", "Other install media"),
            ("install-methods", "cdrom"),
            ("mac_seed", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
            ("vgpu_pci", ""),
            ("xo:backup:sr", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
            ("xo:base_delta", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
            ("xo:copy_of", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
        ];
        assert_eq!(ubuntu_hash.other, slice_to_map(&ubuntu_expected));
        assert_eq!(ubuntu_tree.other, slice_to_map(&ubuntu_expected));

        let windows_expected = [
            ("auto_poweron", "true"),
            ("base_template_name", "Windows 10 (64-bit)"),
            (
                "import_task",
                "OpaqueRef:deadbeaf-dead-beaf-dead-beafdeadbeaf",
            ),
            ("install-methods", "cdrom"),
            ("mac_seed", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
            ("xo:copy_of", "deadbeaf-dead-beaf-dead-beafdeadbeaf"),
        ];
        assert_eq!(windows_hash.other, slice_to_map(&windows_expected));
        assert_eq!(windows_tree.other, slice_to_map(&windows_expected));
    }

    fn slice_to_map<T: FromIterator<(String, String)>>(slice: &[(&str, &str)]) -> T {
        slice
            .iter()
            .map(|(key, value)| (key.to_string(), value.to_string()))
            .collect()
    }
}