Skip to main content

isothrum_runner/run/
graphical.rs

1//! A machine's graphical console: where it answers, found where the manifest says, reached on
2//! this computer's loopback (an SSH tunnel opened when it must be), for the embedder's viewer.
3
4use super::Running;
5use crate::Result;
6use crate::graphical::{Console, native, none, offered, reach};
7
8impl Running<'_> {
9    /// `machine`'s graphical console (its screen), as its manifest says it has one under the
10    /// run's provider (`machines[].consoles`): its protocol (VNC or RDP), where it answers
11    /// ([`crate::Endpoint`]: a port of this computer's loopback, or Proxmox's VNC websocket), the
12    /// account and password that sign in. A console the hypervisor says listens anywhere but the
13    /// loopback is refused, and so is one on the loopback that doesn't answer. A console reached
14    /// through an SSH tunnel holds it open: drop it once the viewer is done. Emits nothing, never
15    /// locks the run, and writes no state but a Proxmox request's credentials while `curl` reads
16    /// them. While an operation holds the run, the machine is as the runner last read it: the
17    /// hypervisor is asked where its console is all the same.
18    ///
19    /// # Errors
20    ///
21    /// `machine_unknown` for a machine the run doesn't have; `no_console` when it has none there
22    /// (the error says why, in the manifest's words: UTM's SPICE, VirtualBox without its
23    /// extension pack…), isn't running, another process acts on it (Vagrant's lock: its start or
24    /// its provisioning), its hypervisor doesn't say where its console is or started it without
25    /// one, it doesn't answer there, or the runner chose none for it yet (VMware's: the run's
26    /// next start chooses it); a tool's own failure (`tool_missing` for a missing `virsh`,
27    /// `VBoxManage`, `curl` or `ssh`).
28    pub async fn graphical_console(&self, machine: &str) -> Result<Console> {
29        let target = self.machine_named(machine)?;
30        let events = self.silent();
31        let (record, mut cx) = self.silent_cx(&events)?;
32        let console = offered(target, cx.scope.provider)?;
33        let (_, observed, _) = self.read_for_query(&mut cx, record.as_ref()).await?;
34        runs_now(target, &observed)?;
35        super::made(&cx)?;
36        self.driver.prepare(&cx)?;
37        if cx.relayed.is_none() {
38            self.relay(&mut cx).await?;
39        }
40        let outputs = self.bind_observed(&mut cx, observed).values;
41        reach(&cx, target, console, &outputs).await
42    }
43
44    /// What opens running VM `machine` in its hypervisor's own app, beside the embedder's viewer
45    /// ([`crate::Status::native`] names the app): a program and its arguments, a macOS app and
46    /// what it opens, or an `https` URL, best first, each argument checked; the embedder runs the
47    /// first this computer has, never through a shell. Reads as [`Running::graphical_console`]
48    /// does, and runs nothing but a read (Parallels' `prlctl`, libvirt's `virsh`).
49    ///
50    /// # Errors
51    ///
52    /// `machine_unknown` for a machine the run doesn't have; `no_console` when its hypervisor
53    /// has no app (vagrant-qemu), it isn't a VM, isn't running, another process acts on it, or
54    /// what names it can't be handed to the app as it is.
55    pub async fn native_console(&self, machine: &str) -> Result<crate::NativeConsole> {
56        let target = self.machine_named(machine)?;
57        let events = self.silent();
58        let (record, mut cx) = self.silent_cx(&events)?;
59        let app = native::app(self.manifest(), cx.scope.provider, cx.scope.host).ok_or_else(|| native::no_app(target))?;
60        let (_, observed, _) = self.read_for_query(&mut cx, record.as_ref()).await?;
61        runs_now(target, &observed)?;
62        self.bind_observed(&mut cx, observed);
63        native::console(&cx, target, &app).await
64    }
65}
66
67/// Refuses `target` unless `observed` has it running: busy (another process acts on it), or
68/// stopped, absent…, saying so.
69fn runs_now(target: &isoloom_manifest::Machine, observed: &crate::driver::Observed) -> Result<()> {
70    let state = observed.machines.iter().find(|m| m.name == target.name.as_str()).map(|m| m.state.clone());
71    match state {
72        Some(crate::MachineState::Busy) => Err(none(
73            target,
74            "another process is acting on it (starting or provisioning it): its screen can be shown once that is done",
75        )),
76        Some(s) if s.runs() => Ok(()),
77        other => {
78            let said = other.map_or_else(|| "absent".to_owned(), |s| s.to_string());
79            Err(none(target, format!("it isn't running ({said})")))
80        }
81    }
82}