use isoloom_manifest::Machine;
use super::state;
use crate::Result;
use crate::console::ShellCommand;
use crate::driver::{GuestUse, console_in_run, shells};
use crate::error::Error;
use crate::executor::Cx;
use crate::process::ProcessSpec;
use crate::ssh::{self, Login};
pub(super) fn command(cx: &Cx<'_>, machine: &Machine, user: Option<&str>) -> Result<ShellCommand> {
if crate::driver::is_compose_service(machine) {
return Err(crate::driver::no_vm_for("a console into", machine));
}
let outputs = state::read(cx.state_dir())?.outputs;
let login = ssh::machine_login(|name| outputs.get(name).cloned(), machine.name.as_str(), cx.ssh_jump.as_deref())?.ok_or_else(|| Error::NotRunning {
run: cx.run.name().to_string(),
})?;
let shell = user.map_or_else(|| shells::LOGIN_SHELL.to_owned(), shells::login_shell_as);
Ok(console_in_run(cx, over_ssh(cx, &login, &shell, GuestUse::Console)?))
}
pub(super) fn in_guest(cx: &Cx<'_>, script: &str, use_: GuestUse) -> Result<ProcessSpec> {
let login = super::guest::login(cx, &state::read(cx.state_dir())?)?.ok_or_else(|| Error::Unsupported {
what: "running a script in the run's VM: this run has none".to_owned(),
})?;
over_ssh(cx, &login, script, use_)
}
fn over_ssh(cx: &Cx<'_>, login: &Login, command: &str, use_: GuestUse) -> Result<ProcessSpec> {
let mut spec = ssh::spec(cx, login, command, Vec::new())?;
if use_ == GuestUse::Console {
spec.args.insert(0, "-t".to_owned());
}
Ok(spec)
}