Skip to main content

isothrum_runner/run/
console.rs

1//! The way into a machine of a run: the command that opens a console into it, built by the
2//! runner (it alone knows the runtime) and run by the embedder in its own terminal.
3
4use isoloom_manifest::{Machine, Native};
5
6use super::Running;
7use crate::Result;
8use crate::console::{ShellCommand, check_user};
9use crate::executor::Cx;
10
11impl Running<'_> {
12    /// The command that opens an interactive console into `machine`, any machine of the run (an
13    /// SSH login on a cloud's), by its native name (the Compose service, the Vagrant define), so
14    /// the embedder never reconstructs a container name. A query: it runs nothing, emits nothing,
15    /// writes only what the command needs (the SSH configuration in the run's state folder, for a
16    /// way in over SSH; nothing for a container's), and doesn't check the machine is up (the embedder runs what it returns in its own
17    /// terminal once the machine is up or attached).
18    ///
19    /// With `user`, the console opens as that account of the machine, in its home, rather than as
20    /// the account the way in logs in as (a container's own user, Vagrant's `vagrant`, a module's
21    /// SSH user): `exec --user` into a container, a `sudo -i` login on a VM (its way in's account
22    /// may `sudo`, as a Vagrant box's and a cloud image's do). The machine must have the account.
23    ///
24    /// # Errors
25    ///
26    /// Fails with `machine_unknown` for a machine the run doesn't have, `not_running` when the
27    /// run gives it no address yet, and `unsupported` where the target has no way in yet, for a
28    /// `user` that can't name an account, and for a `user` on a Windows machine.
29    pub fn console_command(&self, machine: &str, user: Option<&str>) -> Result<ShellCommand> {
30        let target = self.machine_named(machine)?;
31        let user = user.map(|u| check_user(u, target)).transpose()?;
32        self.query(|cx| self.console_of(cx, target, user))
33    }
34
35    /// The console into `target`, as `user` when given: into the run's VM, then into its
36    /// container, for a Compose service in that VM ([`crate::in_vm::console`]); the driver's
37    /// otherwise.
38    fn console_of(&self, cx: &Cx<'_>, target: &Machine, user: Option<&str>) -> Result<ShellCommand> {
39        match (&self.manifest().guest, &target.native) {
40            (Some(guest), Some(Native::Compose { service })) => crate::in_vm::console(cx, &self.driver, guest, service.as_str(), user),
41            _ => self.driver.console_command(cx, target, user),
42        }
43    }
44}