1mod kvm;
18mod plan;
19mod walk;
20mod windows;
21
22pub use kvm::{OPTION, OPTION_FILE, ShareFailed, Shared, share_script, share_step, share_virtualization, sharing};
23pub use plan::{LeftAlone, PassedOver, Plan, SetupPath, Step, Wanted, left_alone, plan};
24pub use walk::{Event, OnFailure, Steps, Walked, walk};
25pub use windows::{RESTART_NOTE_MAX, Restart, RestartFailed, join_hyperv_admins, join_hyperv_admins_step, schedule_restart};
26
27use serde::{Deserialize, Serialize};
28
29use crate::check::Check;
30use crate::docker::{self, Engine};
31use crate::host::{Host, HostOs};
32use crate::install::{Dependency, Elevation, Interaction};
33use crate::providers::{self, Need, Provider, ProviderStatus, Unavailable};
34use crate::wsl::WslState;
35
36#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
38#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS), ts(rename = "SetupAction"))]
39#[serde(tag = "kind", rename_all = "camelCase")]
40pub enum Action {
41 Install {
44 dependency: Dependency,
46 interactions: Vec<Interaction>,
49 },
50 Plugin {
52 plugin: String,
54 },
55 StartEngine {
57 engine: Engine,
59 },
60 UseEngine {
62 engine: Engine,
64 },
65 Restart {
67 feature: WindowsFeature,
69 },
70 ShareVirtualization {
75 interactions: Vec<Interaction>,
77 },
78 Relogin,
81 Manual {
84 interactions: Vec<Interaction>,
87 reason: ManualReason,
89 dependency: Option<Dependency>,
91 url: Option<String>,
93 },
94 Blocked {
96 reason: Unavailable,
98 },
99}
100
101impl Action {
102 #[must_use]
107 pub fn ends_pass(&self) -> bool {
108 matches!(
109 self,
110 Action::Restart { .. }
111 | Action::Relogin
112 | Action::Blocked {
113 reason: Unavailable::KernelRestart { .. } | Unavailable::KvmSharesAfterRestart
114 }
115 )
116 }
117
118 #[must_use]
120 pub fn kind(&self) -> &'static str {
121 match self {
122 Action::Install { .. } => "install",
123 Action::Plugin { .. } => "plugin",
124 Action::StartEngine { .. } => "start",
125 Action::UseEngine { .. } => "use",
126 Action::ShareVirtualization { .. } => "share",
127 Action::Restart { .. } => "restart",
128 Action::Relogin => "relogin",
129 Action::Manual { .. } => "manual",
130 Action::Blocked { .. } => "blocked",
131 }
132 }
133
134 #[must_use]
138 pub fn interactions(&self) -> Vec<Interaction> {
139 match self {
140 Action::Install { interactions, .. } | Action::ShareVirtualization { interactions } | Action::Manual { interactions, .. } => interactions.clone(),
141 Action::Restart { .. } => vec![Interaction::Restart],
142 Action::Relogin => vec![Interaction::Relogin],
143 Action::Plugin { .. } | Action::StartEngine { .. } | Action::UseEngine { .. } | Action::Blocked { .. } => Vec::new(),
144 }
145 }
146}
147
148#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
150#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
151#[serde(rename_all = "camelCase")]
152pub enum WindowsFeature {
153 Wsl,
155 Hyperv,
157}
158
159#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
161#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
162#[serde(rename_all = "camelCase")]
163pub enum ManualReason {
164 Vendor,
166 Distribution,
168}
169
170#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
172#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS), ts(rename = "SetupOptions"))]
173#[serde(rename_all = "camelCase")]
174pub struct Options {
175 pub wsl: Vec<Action>,
177 pub recommended_engine: Engine,
179 pub engine: Engine,
182 pub engines: Vec<EngineOption>,
185 pub hypervisor: Option<Provider>,
188 pub hypervisors: Vec<HypervisorOption>,
190}
191
192impl Options {
193 #[must_use]
195 pub fn empty() -> Options {
196 Options {
197 wsl: Vec::new(),
198 recommended_engine: Engine::Native,
199 engine: Engine::Native,
200 engines: Vec::new(),
201 hypervisor: None,
202 hypervisors: Vec::new(),
203 }
204 }
205}
206
207#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
209#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
210#[serde(rename_all = "camelCase")]
211pub struct EngineOption {
212 pub engine: Engine,
214 pub actions: Vec<Action>,
216}
217
218#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
221#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
222#[serde(rename_all = "camelCase")]
223pub struct HypervisorOption {
224 pub provider: Provider,
226 pub get: Vec<Action>,
228 pub vagrant: Vec<Action>,
230}
231
232#[derive(Clone, Debug, PartialEq, Eq)]
235pub struct Machine {
236 pub host: Host,
238 pub packages_installed: bool,
240 pub docker: DockerFound,
242 pub wsl: Option<WslState>,
244 pub providers: Vec<ProviderStatus>,
246 pub groups_pending: Vec<String>,
248 pub elevation: Elevation,
250 pub user: Option<String>,
252}
253
254#[derive(Clone, Debug, Default, PartialEq, Eq)]
256pub struct DockerFound {
257 pub installed: bool,
259 pub running: bool,
261 pub denied: bool,
263 pub engine: Option<Engine>,
265 pub engines_running: Vec<Engine>,
267}
268
269impl DockerFound {
270 #[must_use]
272 pub fn ready(&self) -> bool {
273 self.installed && self.running
274 }
275}
276
277impl Machine {
278 #[must_use]
281 pub fn of(check: &Check, host: Host, elevation: Elevation, user: Option<String>) -> Machine {
282 Machine {
283 host,
284 packages_installed: check.packages.installed,
285 docker: DockerFound {
286 installed: check.docker.installed,
287 running: check.daemon.running(),
288 denied: check.daemon.denied(),
289 engine: check.daemon.engine,
290 engines_running: docker::running_of(&check.engines),
291 },
292 wsl: check.wsl.as_ref().map(|w| w.state),
293 providers: check.providers.clone(),
294 groups_pending: check.groups_pending.clone(),
295 elevation,
296 user,
297 }
298 }
299
300 fn local(&self) -> impl Iterator<Item = &ProviderStatus> {
302 self.providers.iter().filter(|p| !p.remote)
303 }
304}
305
306#[must_use]
308pub fn engines_on(os: HostOs) -> &'static [Engine] {
309 match os {
310 HostOs::Macos => &[Engine::DockerDesktop, Engine::Orbstack, Engine::Colima],
311 HostOs::Windows => &[Engine::DockerDesktop],
312 HostOs::Linux => &[Engine::DockerDesktop, Engine::Native, Engine::Colima],
313 }
314}
315
316#[must_use]
319pub fn recommended_engine(os: HostOs) -> Engine {
320 match os {
321 HostOs::Macos | HostOs::Windows => Engine::DockerDesktop,
322 HostOs::Linux => Engine::Native,
323 }
324}
325
326#[must_use]
328pub fn installable(provider: Provider, os: HostOs) -> Option<Dependency> {
329 Dependency::of_hypervisor(provider).filter(|d| d.runs_on(os))
330}
331
332#[must_use]
335pub fn cant_run(p: &ProviderStatus) -> bool {
336 p.hypervisor == Some(true) && !p.available && p.reason.is_some() && p.needs.is_empty()
337}
338
339fn install(m: &Machine, dependency: Dependency) -> Action {
342 if m.host.os == HostOs::Linux && !m.packages_installed {
343 return Action::Manual {
344 interactions: Vec::new(),
345 reason: ManualReason::Distribution,
346 dependency: Some(dependency),
347 url: dependency.row().own_url.map(str::to_owned),
348 };
349 }
350 Action::Install {
351 dependency,
352 interactions: crate::install::interactions(dependency, &m.host, m.elevation, m.user.as_deref()),
353 }
354}
355
356fn wsl(m: &Machine) -> Vec<Action> {
360 if m.host.os != HostOs::Windows || m.docker.ready() {
361 return Vec::new();
362 }
363 match m.wsl.unwrap_or(WslState::Unknown) {
364 WslState::Ready => Vec::new(),
365 WslState::NeedsReboot => vec![Action::Restart { feature: WindowsFeature::Wsl }],
366 WslState::Missing | WslState::Unknown => vec![install(m, Dependency::Wsl)],
367 }
368}
369
370fn engine_actions(m: &Machine, engine: Engine) -> Vec<Action> {
375 let action = if m.docker.ready() && m.docker.engine == Some(engine) {
376 return Vec::new();
377 } else if m.docker.engines_running.contains(&engine) {
378 Action::UseEngine { engine }
379 } else if m.docker.denied && engine == Engine::Native {
380 Action::Relogin
381 } else if m.docker.installed && !m.docker.ready() {
382 Action::StartEngine { engine }
383 } else if engine == recommended_engine(m.host.os) && !m.docker.installed {
384 install(m, Dependency::Docker)
385 } else {
386 by_hand(crate::install::VendorPage {
387 name: engine.label(),
388 url: engine.url(),
389 })
390 };
391 vec![action]
392}
393
394fn hypervisor_get(m: &Machine, p: &ProviderStatus, hyperv_restart: bool) -> Vec<Action> {
400 if let Some(reason @ (Unavailable::KvmHoldsVirtualization | Unavailable::KvmSharesAfterRestart)) = &p.reason {
401 let mut get: Vec<Action> = (p.hypervisor != Some(true)).then(|| get_hypervisor(m, p)).into_iter().collect();
402 get.push(match reason {
403 Unavailable::KvmHoldsVirtualization => share_virtualization_action(m),
404 _ => Action::Blocked { reason: reason.clone() },
405 });
406 return get;
407 }
408 if p.hypervisor == Some(true) {
409 let groups = providers::login_groups(p.provider, &m.host);
412 if m.groups_pending.iter().any(|g| groups.contains(g)) {
413 return vec![Action::Relogin];
414 }
415 return match &p.reason {
416 Some(reason) if cant_run(p) => vec![Action::Blocked { reason: reason.clone() }],
417 _ => Vec::new(),
418 };
419 }
420 if p.provider == Provider::Hyperv && hyperv_restart {
421 return vec![Action::Restart {
422 feature: WindowsFeature::Hyperv,
423 }];
424 }
425 vec![get_hypervisor(m, p)]
426}
427
428fn share_virtualization_action(m: &Machine) -> Action {
431 let prompt = Interaction::AdminPassword {
432 via: crate::install::Prompt::Pkexec,
433 };
434 Action::ShareVirtualization {
435 interactions: (m.elevation == Elevation::Prompt).then_some(prompt).into_iter().collect(),
436 }
437}
438
439fn get_hypervisor(m: &Machine, p: &ProviderStatus) -> Action {
441 match installable(p.provider, m.host.os) {
442 Some(dependency) => install(m, dependency),
443 None => match providers::download_page(p.provider, m.host.os) {
444 Some(page) => by_hand(page),
445 None => Action::Manual {
446 interactions: Vec::new(),
447 reason: ManualReason::Vendor,
448 dependency: None,
449 url: None,
450 },
451 },
452 }
453}
454
455fn by_hand(page: crate::install::VendorPage) -> Action {
458 Action::Manual {
459 interactions: vec![page.download()],
460 reason: ManualReason::Vendor,
461 dependency: None,
462 url: Some(page.url.to_owned()),
463 }
464}
465
466fn vagrant_steps(m: &Machine, p: &ProviderStatus) -> Vec<(String, Action)> {
472 let through_utility = providers::needs_vmware_utility(p.provider);
473 if through_utility && p.hypervisor != Some(true) {
474 return p
475 .needs
476 .contains(&Need::Vagrant)
477 .then(|| ("vagrant".to_owned(), install(m, Dependency::Vagrant)))
478 .into_iter()
479 .collect();
480 }
481 let utility = install(m, Dependency::VmwareUtility);
482 let utility_installs = !p.needs.contains(&Need::VmwareUtility) || matches!(utility, Action::Install { .. });
483 p.needs
484 .iter()
485 .filter_map(|need| {
486 Some(match need {
487 Need::Vagrant => ("vagrant".to_owned(), install(m, Dependency::Vagrant)),
488 Need::VmwareUtility => ("vmware-utility".to_owned(), utility.clone()),
489 Need::Plugin if through_utility && !utility_installs => return None,
490 Need::Plugin => {
491 let plugin = p.plugin.clone()?;
492 (format!("plugin:{plugin}"), Action::Plugin { plugin })
493 }
494 })
495 })
496 .collect()
497}
498
499#[must_use]
502pub fn options(m: &Machine, hyperv_restart: bool) -> Options {
503 let offered = engines_on(m.host.os);
504 let recommended = recommended_engine(m.host.os);
505 let engine = match m.docker.engine {
506 Some(e) if offered.contains(&e) => e,
507 Some(_) => offered.first().copied().unwrap_or(recommended),
508 None => recommended,
509 };
510 let hypervisors: Vec<HypervisorOption> = m
511 .local()
512 .map(|p| HypervisorOption {
513 provider: p.provider,
514 get: hypervisor_get(m, p, hyperv_restart),
515 vagrant: vagrant_steps(m, p).into_iter().map(|(_, action)| action).collect(),
516 })
517 .collect();
518 let installed = |o: &&HypervisorOption| m.local().any(|p| p.provider == o.provider && p.hypervisor == Some(true));
519 let installs = |o: &&HypervisorOption| matches!(o.get.first(), Some(Action::Install { .. }));
522 let shares = |o: &&HypervisorOption| o.get.iter().any(|a| matches!(a, Action::ShareVirtualization { .. }));
523 let hypervisor = hypervisors
524 .iter()
525 .find(|o| installed(o) && o.get.is_empty())
526 .or_else(|| hypervisors.iter().find(installed))
527 .or_else(|| hypervisors.iter().find(|o| installs(o) && !shares(o)))
528 .or_else(|| hypervisors.iter().find(installs))
529 .or_else(|| hypervisors.first())
530 .map(|o| o.provider);
531 Options {
532 wsl: wsl(m),
533 recommended_engine: recommended,
534 engine,
535 engines: offered
536 .iter()
537 .map(|&e| EngineOption {
538 engine: e,
539 actions: engine_actions(m, e),
540 })
541 .collect(),
542 hypervisor,
543 hypervisors,
544 }
545}
546
547#[cfg(test)]
548mod tests;