Skip to main content

isothrum_machine/install/
interaction.rs

1//! What an install asks of the person at the computer: a password prompt the OS raises, a vendor's
2//! agreement they accept in the vendor's own window, a setting they allow, a restart, a long wait.
3//! A UI says these before the install starts, and again while the step that raises them runs;
4//! nothing here is worded, each is a code a UI says in its own words.
5//!
6//! No install accepts anything on its user's behalf: no licence, agreement or terms. Where a
7//! vendor asks for one, its own window shows it and the user accepts it there
8//! ([`Interaction::VendorAgreement`]).
9//!
10//! The one table of them is [`interactions`]: per OS and tool, from what the planner knows (the
11//! elevation decides whether a password prompt appears at all: an unattended step never raises
12//! one). [`super::plan`] hands each to the step that raises it ([`super::Step::interactions`]).
13
14use serde::Serialize;
15
16use super::{Dependency, Elevation};
17use crate::host::{Arch, Host, HostOs, Packages};
18
19/// One thing an install asks of the person at the computer. None at all (an empty list) means it
20/// installs on its own.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
22#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
23#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
24#[non_exhaustive]
25pub enum Interaction {
26    /// The OS asks for an administrator's password (or approval), in a window of its own.
27    AdminPassword {
28        /// Which prompt.
29        via: Prompt,
30    },
31    /// The vendor's app shows its agreement (at its first start), and the user accepts it there,
32    /// in the vendor's own window: nothing accepts it for them. The tool works once they have.
33    VendorAgreement {
34        /// The app that shows it, as its vendor names it.
35        app: &'static str,
36        /// The agreement's name, as its vendor writes it.
37        name: &'static str,
38        /// Where its vendor publishes it.
39        url: &'static str,
40    },
41    /// A vendor's installer opens a window of its own that waits for the user.
42    VendorWindow {
43        /// The app whose window it is.
44        app: &'static str,
45    },
46    /// The OS blocks the vendor's system extension until the user allows it in System Settings
47    /// (Privacy & Security); a restart then loads it ([`Interaction::Restart`] follows).
48    AllowSystemExtension {
49        /// Who signs it, as their Developer ID names them.
50        vendor: &'static str,
51    },
52    /// The computer is restarted before the tool works.
53    Restart,
54    /// The user signs out and back in before the tool works (a group they joined).
55    Relogin,
56    /// The install builds from source, which takes long.
57    LongBuild {
58        /// About how many minutes it takes, on a common computer.
59        minutes: u16,
60    },
61    /// Nothing installs it unattended: its vendor offers it only from its own page (behind an
62    /// account or a licence). The user downloads and installs it there, then checks again: the
63    /// tool's detection reads it as present once it is installed.
64    DownloadByHand {
65        /// The vendor, as it names itself.
66        vendor: &'static str,
67        /// Its official download page.
68        url: &'static str,
69    },
70}
71
72/// Where a tool's vendor offers it for its user to download themselves: its name, and its
73/// official download page (never a mirror).
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
75#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
76pub struct VendorPage {
77    /// The vendor, as it names itself.
78    pub name: &'static str,
79    /// The official download page.
80    pub url: &'static str,
81}
82
83impl VendorPage {
84    /// What it asks of the user: to download the tool there themselves.
85    #[must_use]
86    pub fn download(self) -> Interaction {
87        Interaction::DownloadByHand {
88            vendor: self.name,
89            url: self.url,
90        }
91    }
92}
93
94/// Broadcom's download portal, where VMware's desktop hypervisors are offered (behind a free
95/// Broadcom account): their own pages there are [`VMWARE_FUSION_PAGE`] and
96/// [`VMWARE_WORKSTATION_PAGE`].
97pub const BROADCOM_DOWNLOADS: VendorPage = VendorPage {
98    name: "Broadcom",
99    url: "https://support.broadcom.com/group/ecx/downloads",
100};
101
102/// VMware Fusion Pro (macOS), free, on Broadcom's download portal.
103pub const VMWARE_FUSION_PAGE: VendorPage = VendorPage {
104    name: "Broadcom",
105    url: "https://support.broadcom.com/group/ecx/productdownloads?subfamily=VMware%20Fusion&freeDownloads=true",
106};
107
108/// VMware Workstation Pro (Windows, Linux), free, on Broadcom's download portal.
109pub const VMWARE_WORKSTATION_PAGE: VendorPage = VendorPage {
110    name: "Broadcom",
111    url: "https://support.broadcom.com/group/ecx/productdownloads?subfamily=VMware%20Workstation%20Pro&freeDownloads=true",
112};
113
114/// VMware's OVF Tool, on Broadcom's developer portal.
115pub const OVFTOOL_PAGE: VendorPage = VendorPage {
116    name: "Broadcom",
117    url: "https://developer.broadcom.com/tools/open-virtualization-format-ovf-tool/latest",
118};
119
120/// The password prompt an administrator's step raises.
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
122#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
123#[serde(rename_all = "camelCase")]
124#[non_exhaustive]
125pub enum Prompt {
126    /// macOS's administrator prompt.
127    Mac,
128    /// Windows' User Account Control.
129    Uac,
130    /// pkexec's (polkit's) prompt, on Linux.
131    Pkexec,
132}
133
134/// Docker's Subscription Service Agreement, which Docker Desktop shows at its first start, on
135/// macOS and Windows alike: no install accepts it (macOS's `install` runs without
136/// `--accept-license`, winget's Docker install overrides the switches of Docker's manifest, which
137/// add it).
138pub(crate) const DOCKER_AGREEMENT: Interaction = Interaction::VendorAgreement {
139    app: super::mac_image::DOCKER.name,
140    name: super::mac_image::DOCKER_AGREEMENT.name,
141    url: super::mac_image::DOCKER_AGREEMENT.url,
142};
143
144/// About how long Homebrew takes to build QEMU and its libraries from source on an Intel Mac: a
145/// quarter of an hour or more.
146const BUILD_MINUTES: u16 = 15;
147
148/// What installing `dep` on `host` asks of its user (`user`: the account a Linux install joins to
149/// a tool's groups), its administrator's steps run `elevation`'s way: in the order they come. A
150/// prompt only where one is raised: never unattended (as root, or `sudo -n`, which refuses rather
151/// than asks). Empty for a tool that doesn't run there.
152#[must_use]
153pub fn interactions(dep: Dependency, host: &Host, elevation: Elevation, user: Option<&str>) -> Vec<Interaction> {
154    if !dep.runs_on(host.os) {
155        return Vec::new();
156    }
157    let prompt = |via| match elevation {
158        Elevation::Prompt => Some(Interaction::AdminPassword { via }),
159        Elevation::Unattended { .. } => None,
160    };
161    let found: Vec<Option<Interaction>> = match host.os {
162        HostOs::Macos => macos(dep, host.arch, prompt(Prompt::Mac)),
163        HostOs::Windows => windows(dep),
164        HostOs::Linux => linux(dep, host.packages, user, prompt(Prompt::Pkexec)),
165    };
166    found.into_iter().flatten().collect()
167}
168
169/// macOS: the vendors' packages (the apps', Vagrant's, the AWS CLI's, HashiCorp's utility's)
170/// install as an administrator (`admin`); Homebrew's formulae and casks as the user.
171fn macos(dep: Dependency, arch: Arch, admin: Option<Interaction>) -> Vec<Option<Interaction>> {
172    match dep {
173        Dependency::Docker => vec![admin, Some(DOCKER_AGREEMENT)],
174        Dependency::Vagrant | Dependency::Utm | Dependency::VmwareUtility | Dependency::Awscli => vec![admin],
175        // On an Intel Mac VirtualBox's drivers are kernel extensions, which macOS blocks until
176        // allowed and loads at the next restart; on Apple silicon it needs none.
177        Dependency::Virtualbox => match arch {
178            Arch::Amd64 => vec![
179                admin,
180                Some(Interaction::AllowSystemExtension {
181                    vendor: super::mac_image::VIRTUALBOX.signer,
182                }),
183                Some(Interaction::Restart),
184            ],
185            Arch::Arm64 => vec![admin],
186        },
187        // Homebrew's formula: Homebrew has no bottles for an Intel Mac, so it builds from source
188        // there.
189        Dependency::Qemu => match arch {
190            Arch::Amd64 => vec![Some(Interaction::LongBuild { minutes: BUILD_MINUTES })],
191            Arch::Arm64 => Vec::new(),
192        },
193        // Homebrew's own as the user, or nothing on macOS.
194        Dependency::Terraform | Dependency::Azurecli | Dependency::Gcloud | Dependency::Libvirt | Dependency::Wsl | Dependency::Hyperv => Vec::new(),
195    }
196}
197
198/// Windows: every install but Terraform's (a portable zip winget unpacks for the user) is a
199/// machine-wide one, which asks through UAC.
200fn windows(dep: Dependency) -> Vec<Option<Interaction>> {
201    let uac = Some(Interaction::AdminPassword { via: Prompt::Uac });
202    match dep {
203        // Docker Desktop shows its agreement at its first start (winget's install of it passes
204        // no `--accept-license`: [`super::windows::DOCKER_SWITCHES`]).
205        Dependency::Docker => vec![uac, Some(DOCKER_AGREEMENT)],
206        // HashiCorp's MSI asks for a restart to finish (its manifest's 3010); the Windows features
207        // start at the next one.
208        Dependency::Vagrant | Dependency::Wsl | Dependency::Hyperv => vec![uac, Some(Interaction::Restart)],
209        Dependency::Virtualbox | Dependency::Awscli | Dependency::Azurecli | Dependency::Gcloud | Dependency::VmwareUtility => vec![uac],
210        // Terraform: a portable zip winget unpacks for the user. The rest isn't on Windows.
211        Dependency::Terraform | Dependency::Qemu | Dependency::Utm | Dependency::Libvirt => Vec::new(),
212    }
213}
214
215/// Linux: every install is one root script (`admin`); the ones that join `user` to a tool's
216/// groups (Docker's `docker`, libvirt's, `/dev/kvm`'s, Fedora's VirtualBox's `vboxusers`) apply
217/// them from the next login.
218fn linux(dep: Dependency, packages: Option<Packages>, user: Option<&str>, admin: Option<Interaction>) -> Vec<Option<Interaction>> {
219    let dnf = packages.and_then(Packages::linux) == Some(crate::host::LinuxPackages::Dnf);
220    let joins = match dep {
221        Dependency::Docker | Dependency::Libvirt | Dependency::Qemu => true,
222        Dependency::Virtualbox => dnf,
223        _ => false,
224    };
225    let relogin = (joins && user.is_some_and(super::linux::scripts::valid_user)).then_some(Interaction::Relogin);
226    vec![admin, relogin]
227}
228
229#[cfg(test)]
230mod tests;