isothrum-machine 1.0.5

Machine-setup domain for Isothrum: detect Docker, Vagrant, the hypervisors and the cloud CLIs, and self-test them by booting a throwaway container or VM.
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! The Vagrant providers (hypervisors) this computer can drive, and detecting which are usable.
//! Vagrant abstracts the hypervisor; we detect what is installed here and let `vagrant up
//! --provider` do the rest. Ported from the launcher's `runtime/providers`.
//!
//! The Windows "may this account manage Hyper-V" refinement needs `unsafe` Win32 (a token and SID
//! check) that this crate forbids, so [`detect`] never computes it: a caller supplies the answer
//! as `may_manage_hyperv: Option<bool>`. The desktop app (and any default) passes `None`, which
//! adds no admin reason; the launcher passes `Some(its own unsafe check)`, and a `Some(false)`
//! folds the "needs administrator" reason into Hyper-V's status, exactly as the launcher did.

use std::path::Path;
use std::time::Duration;

use isothrum_runner::ProcessRunner;
use serde::{Deserialize, Serialize};

use crate::exec;

/// A Vagrant provider id (the value given to `vagrant up --provider`). The remote providers and
/// the clouds share the enum but are never detected here.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Provider {
    /// Oracle VirtualBox (built into Vagrant).
    Virtualbox,
    /// VMware Workstation or Fusion (plugin `vagrant-vmware-desktop`).
    VmwareDesktop,
    /// Microsoft Hyper-V (built into Vagrant, Windows only).
    Hyperv,
    /// Parallels Desktop (plugin `vagrant-parallels`, macOS only).
    Parallels,
    /// libvirt / KVM (plugin `vagrant-libvirt`, Linux only).
    Libvirt,
    /// QEMU (plugin `vagrant-qemu`).
    Qemu,
    /// UTM, a QEMU front end for macOS (plugin `vagrant_utm`).
    Utm,
    /// A remote VMware ESXi host (plugin `vagrant-vmware-esxi`).
    VmwareEsxi,
    /// A remote Proxmox VE host.
    Proxmox,
    /// The player's AWS account (Terraform, never a local Vagrant provider).
    Aws,
    /// The player's Azure subscription (Terraform).
    Azure,
    /// The player's Google Cloud project (Terraform).
    Gcp,
    /// The player's DigitalOcean account (Terraform).
    #[serde(rename = "digitalocean")]
    DigitalOcean,
    /// The player's Linode account (Terraform).
    Linode,
    /// The player's Oracle Cloud tenancy (Terraform).
    Oci,
}

impl Provider {
    /// Every provider that is a local or remote Vagrant hypervisor (the clouds are left out: they
    /// are never detected on this machine).
    pub const ALL: [Provider; 9] = [
        Provider::Virtualbox,
        Provider::VmwareDesktop,
        Provider::Hyperv,
        Provider::Parallels,
        Provider::Libvirt,
        Provider::Qemu,
        Provider::Utm,
        Provider::VmwareEsxi,
        Provider::Proxmox,
    ];

    /// The id Vagrant knows the provider by.
    #[must_use]
    pub fn id(self) -> &'static str {
        match self {
            Provider::Virtualbox => "virtualbox",
            Provider::VmwareDesktop => "vmware_desktop",
            Provider::Hyperv => "hyperv",
            Provider::Parallels => "parallels",
            Provider::Libvirt => "libvirt",
            Provider::Qemu => "qemu",
            Provider::Utm => "utm",
            Provider::VmwareEsxi => "vmware_esxi",
            Provider::Proxmox => "proxmox",
            Provider::Aws => "aws",
            Provider::Azure => "azure",
            Provider::Gcp => "gcp",
            Provider::DigitalOcean => "digitalocean",
            Provider::Linode => "linode",
            Provider::Oci => "oci",
        }
    }

    /// Whether the provider runs VMs on another host (so it needs connection settings, not a local
    /// hypervisor).
    #[must_use]
    pub fn is_remote(self) -> bool {
        matches!(
            self,
            Provider::VmwareEsxi
                | Provider::Proxmox
                | Provider::Aws
                | Provider::Azure
                | Provider::Gcp
                | Provider::DigitalOcean
                | Provider::Linode
                | Provider::Oci
        )
    }

    /// Whether the provider is a cloud account billed to the player, as opposed to their own server.
    #[must_use]
    pub fn is_cloud(self) -> bool {
        self.is_remote() && !matches!(self, Provider::VmwareEsxi | Provider::Proxmox)
    }

    /// Whether this provider can run on the current OS at all: local hypervisors are
    /// platform-specific, remote ones apply anywhere.
    fn applicable(self) -> bool {
        use std::env::consts::{ARCH, OS};
        match self {
            Provider::Virtualbox | Provider::VmwareDesktop
                if OS == "windows" && ARCH == "aarch64" =>
            {
                false
            }
            Provider::Hyperv => OS == "windows",
            Provider::Parallels | Provider::Utm => OS == "macos",
            Provider::Libvirt => OS == "linux",
            _ => true,
        }
    }

    /// The Vagrant plugin the provider needs, if it is not built in.
    fn plugin(self) -> Option<&'static str> {
        match self {
            Provider::Virtualbox
            | Provider::Hyperv
            | Provider::Aws
            | Provider::Azure
            | Provider::Gcp
            | Provider::DigitalOcean
            | Provider::Linode
            | Provider::Oci => None,
            Provider::VmwareDesktop => Some("vagrant-vmware-desktop"),
            Provider::Parallels => Some("vagrant-parallels"),
            Provider::Libvirt => Some("vagrant-libvirt"),
            Provider::Qemu => Some("vagrant-qemu"),
            Provider::Utm => Some("vagrant_utm"),
            Provider::VmwareEsxi => Some("vagrant-vmware-esxi"),
            Provider::Proxmox => Some("vagrant-proxmox"),
        }
    }

    /// The local tool that proves the hypervisor itself is installed, if there is one.
    fn probe(self) -> Option<(&'static str, &'static [&'static str])> {
        match self {
            Provider::Virtualbox => Some(("VBoxManage", &["--version"])),
            Provider::VmwareDesktop => Some(("vmrun", &[])),
            Provider::Parallels => Some(("prlctl", &["--version"])),
            Provider::Libvirt => Some(("virsh", &["--version"])),
            Provider::Qemu => Some((qemu_binary(), &["--version"])),
            _ => None,
        }
    }

    /// Whether the hypervisor itself is installed here, `None` when there is nothing local to
    /// check (remote providers). UTM is an app bundle; Hyper-V is a Windows feature.
    async fn hypervisor_installed(self, runner: &dyn ProcessRunner) -> Option<bool> {
        match self {
            Provider::Utm => Some(Path::new(UTM_APP).exists()),
            Provider::Hyperv => Some(hyperv_enabled()),
            _ => {
                let (program, args) = self.probe()?;
                Some(tool_present(runner, program, args).await)
            }
        }
    }
}

/// Where UTM is installed on macOS.
const UTM_APP: &str = "/Applications/UTM.app";

/// Whether the Hyper-V feature is on: its Virtual Machine Management service is installed with it.
fn hyperv_enabled() -> bool {
    let root = std::env::var_os("SystemRoot").map_or_else(
        || std::path::PathBuf::from("C:\\Windows"),
        std::path::PathBuf::from,
    );
    root.join("System32").join("vmms.exe").is_file()
}

/// Why libvirt can't run VMs here although `virsh` is installed: vagrant-libvirt boots KVM guests,
/// so it needs `/dev/kvm`, and the player must be allowed to open it. `None` when it is fine.
#[must_use]
pub fn kvm_problem() -> Option<String> {
    let dev = Path::new("/dev/kvm");
    if !dev.exists() {
        return Some("KVM isn't available on this machine (no /dev/kvm): turn on virtualization in the firmware, or use another hypervisor".to_owned());
    }
    if std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open(dev)
        .is_err()
    {
        return Some("you can't use /dev/kvm yet: add yourself to the kvm and libvirt groups, then sign out and back in".to_owned());
    }
    None
}

/// The QEMU binary for this machine's CPU.
fn qemu_binary() -> &'static str {
    if std::env::consts::ARCH == "aarch64" {
        "qemu-system-aarch64"
    } else {
        "qemu-system-x86_64"
    }
}

/// One provider's readiness, as the setup screen reads it.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ProviderStatus {
    /// The provider.
    pub provider: Provider,
    /// Whether it runs VMs on another host.
    pub remote: bool,
    /// Whether it is ready to use here.
    pub available: bool,
    /// Whether the hypervisor itself is installed, `None` when there is nothing to probe.
    pub hypervisor: Option<bool>,
    /// The Vagrant plugin it needs, if it is not built in.
    pub plugin: Option<String>,
    /// Whether that plugin is present (true when the provider is built in).
    pub plugin_installed: bool,
    /// Why it is not available, `None` when it is.
    pub reason: Option<String>,
}

/// The Vagrant plugin names in `vagrant plugin list` lines like `vagrant-libvirt (0.12.2, global)`.
#[must_use]
pub fn parse_plugins(out: &str) -> Vec<String> {
    out.lines()
        .filter_map(|l| l.split_whitespace().next())
        .filter(|name| name.starts_with("vagrant-") || name.starts_with("vagrant_"))
        .map(str::to_owned)
        .collect()
}

/// Whether `program` is installed: it is, as long as it could be started (a tool that prints usage
/// and exits non-zero, like `vmrun`, still exists).
async fn tool_present(runner: &dyn ProcessRunner, program: &str, args: &[&str]) -> bool {
    exec::read(runner, program, args, None).await.is_ok()
}

/// The Vagrant plugins installed, empty when `vagrant plugin list` can't run.
async fn installed_plugins(runner: &dyn ProcessRunner) -> Vec<String> {
    match exec::read(runner, "vagrant", &["plugin", "list"], None).await {
        Ok(out) => parse_plugins(&out),
        Err(_) => Vec::new(),
    }
}

/// How long the VirtualBox liveness probe may take before VirtualBox counts as wedged.
const VBOX_PROBE: Duration = Duration::from_secs(8);

/// Why Hyper-V can't be used by this account yet, and the two ways out. Folded into Hyper-V's
/// status when the caller's `may_manage_hyperv` is `Some(false)`.
pub const HYPERV_NEEDS_ADMIN: &str = "Vagrant drives Hyper-V only for an administrator or a member of the Hyper-V Administrators group: \
add this account to that group and sign out of Windows and back in, or run the app as administrator";

/// The readiness of every provider usable on this machine: its hypervisor, its plugin, and why it
/// is not available. `vagrant_installed` is global, so it is a plain argument.
///
/// `may_manage_hyperv` is the one fact this crate cannot work out without `unsafe` (see the module
/// docs): `Some(false)` folds the "needs administrator" reason into Hyper-V, `Some(true)` and
/// `None` add nothing. The desktop app passes `None`; the launcher passes its own token/SID check.
pub async fn detect(
    runner: &dyn ProcessRunner,
    vagrant_installed: bool,
    may_manage_hyperv: Option<bool>,
) -> Vec<ProviderStatus> {
    let plugins = if vagrant_installed {
        installed_plugins(runner).await
    } else {
        Vec::new()
    };
    let mut statuses = Vec::new();
    for provider in Provider::ALL {
        if !provider.applicable() {
            continue;
        }
        let hypervisor = provider.hypervisor_installed(runner).await;
        let plugin = provider.plugin().map(str::to_owned);
        let plugin_installed = match &plugin {
            None => true,
            Some(needed) => vagrant_installed && plugins.iter().any(|i| i == needed),
        };
        let unresponsive = provider == Provider::Virtualbox
            && hypervisor == Some(true)
            && vbox_wedged(runner).await;
        // Vagrant's Hyper-V provider refuses every command (status reads included) from an account
        // that may not manage Hyper-V: said here with the way out, not as a raw error mid-boot.
        let needs_admin = provider == Provider::Hyperv
            && hypervisor == Some(true)
            && may_manage_hyperv == Some(false);
        let detection = Detection {
            hypervisor,
            unresponsive,
            plugin: plugin.as_deref(),
            plugin_installed,
            needs_admin,
        };
        let reason = unavailable_reason(provider, vagrant_installed, detection);
        statuses.push(ProviderStatus {
            provider,
            remote: provider.is_remote(),
            available: reason.is_none(),
            hypervisor,
            plugin,
            plugin_installed,
            reason,
        });
    }
    statuses
}

/// Whether VirtualBox's background service is wedged: `VBoxManage --version` never talks to it, so
/// ask it something that does and see whether that times out.
async fn vbox_wedged(runner: &dyn ProcessRunner) -> bool {
    let probe = exec::read(runner, "VBoxManage", &["list", "runningvms"], None);
    matches!(
        exec::within("VBoxManage", VBOX_PROBE, probe).await,
        Err(crate::Error::Timeout { .. })
    )
}

/// What `detect` learned about one provider, grouped so `unavailable_reason` reads one value
/// rather than a handful of flags that always travel together.
#[derive(Clone, Copy)]
struct Detection<'a> {
    /// Whether the hypervisor is installed, `None` when there is nothing to probe.
    hypervisor: Option<bool>,
    /// Whether the hypervisor's service is wedged (its liveness probe timed out).
    unresponsive: bool,
    /// The Vagrant plugin it needs, if it is not built in.
    plugin: Option<&'a str>,
    /// Whether that plugin is installed.
    plugin_installed: bool,
    /// Whether this account may not manage Hyper-V (Hyper-V only; the caller's injected answer).
    needs_admin: bool,
}

/// Why a provider can't be used here, `None` when it can: the first layer that is missing.
fn unavailable_reason(
    provider: Provider,
    vagrant_installed: bool,
    detection: Detection<'_>,
) -> Option<String> {
    let Detection {
        hypervisor,
        unresponsive,
        plugin,
        plugin_installed,
        needs_admin,
    } = detection;
    if !vagrant_installed {
        Some("Vagrant is not installed".to_owned())
    } else if unresponsive {
        Some("VirtualBox isn't responding (its background service is stuck). Quit VirtualBox and any VM windows, or restart the computer, then re-check.".to_owned())
    } else if hypervisor == Some(false) && provider == Provider::Hyperv {
        Some("Hyper-V isn't turned on (Windows Features > Hyper-V), or this edition of Windows doesn't have it".to_owned())
    } else if needs_admin {
        Some(HYPERV_NEEDS_ADMIN.to_owned())
    } else if hypervisor == Some(false) {
        Some(provider.probe().map_or_else(
            || "not installed".to_owned(),
            |(program, _)| format!("`{program}` was not found"),
        ))
    } else if let Some(why) = (provider == Provider::Libvirt).then(kvm_problem).flatten() {
        Some(why)
    } else if !plugin_installed {
        plugin.map(|p| format!("Vagrant plugin `{p}` is not installed"))
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use isothrum_runner::process::fake::{FakeProcesses, Reply};

    use super::*;

    #[test]
    fn plugin_lines_are_parsed_to_names() {
        let out = "vagrant-libvirt (0.12.2, global)\nvagrant_utm (1.0.0)\nnot-a-plugin (1)\n";
        assert_eq!(parse_plugins(out), ["vagrant-libvirt", "vagrant_utm"]);
    }

    #[test]
    fn remote_and_cloud_are_classified() {
        assert!(Provider::Proxmox.is_remote() && !Provider::Proxmox.is_cloud());
        assert!(Provider::Aws.is_remote() && Provider::Aws.is_cloud());
        assert!(!Provider::Qemu.is_remote());
    }

    #[tokio::test]
    async fn detect_reports_a_ready_qemu_and_an_unavailable_one_by_its_plugin() {
        // QEMU installed, its plugin present: available. A missing plugin makes it unavailable.
        let with = FakeProcesses::rules(&[
            ("qemu-system", Reply::ok("QEMU 10.0")),
            (
                "vagrant plugin list",
                Reply::ok("vagrant-qemu (0.6.3, global)"),
            ),
        ]);
        let qemu = detect(&with, true, None)
            .await
            .into_iter()
            .find(|s| s.provider == Provider::Qemu)
            .unwrap();
        assert!(
            qemu.hypervisor == Some(true) && qemu.plugin_installed && qemu.available,
            "{qemu:?}"
        );

        let without = FakeProcesses::rules(&[
            ("qemu-system", Reply::ok("QEMU 10.0")),
            ("vagrant plugin list", Reply::ok("")),
        ]);
        let no_plugin = detect(&without, true, None)
            .await
            .into_iter()
            .find(|s| s.provider == Provider::Qemu)
            .unwrap();
        assert!(
            !no_plugin.available && no_plugin.reason.is_some(),
            "{no_plugin:?}"
        );
    }

    #[test]
    fn hyperv_needs_admin_only_when_the_caller_says_the_account_cannot_manage_it() {
        // Hyper-V's `applicable()` gate keeps it out of `detect` off Windows, so the injected
        // `needs_admin` is tested through `unavailable_reason` directly, as the launcher did.
        let hyperv = |needs_admin| Detection {
            hypervisor: Some(true),
            unresponsive: false,
            plugin: None,
            plugin_installed: true,
            needs_admin,
        };
        assert_eq!(
            unavailable_reason(Provider::Hyperv, true, hyperv(true)).as_deref(),
            Some(HYPERV_NEEDS_ADMIN)
        );
        assert_eq!(
            unavailable_reason(Provider::Hyperv, true, hyperv(false)),
            None
        );
        // `Some(false)` is the only value that raises the reason; `None`/`Some(true)` leave it off.
        let needs = |mmh: Option<bool>| mmh == Some(false);
        assert!(needs(Some(false)) && !needs(None) && !needs(Some(true)));
    }
}