mod actions;
mod boxes;
pub(super) mod forwarded;
mod lock;
mod orphans;
mod status;
pub(in crate::driver) use boxes::before_step as boxes_before_step;
pub(super) use lock::run_step;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
use isoloom_manifest::{Action, Machine, Native, Provider, Step, VagrantIdentity, VagrantProvider, contract};
use super::{Driver, GuestUse, Observed, native_name};
use crate::Result;
use crate::console::ShellCommand;
use crate::encoding::wire_name;
use crate::error::Error;
use crate::executor::{Cx, Echo, call_ok, with_delivered};
use crate::process::ProcessSpec;
use crate::state::Op;
use crate::status::{MachineStatus, PublishedPort};
const DOTFILE_VAR: &str = "VAGRANT_DOTFILE_PATH";
const READ_LIMIT: Duration = Duration::from_secs(120);
const ATTACH_LIMIT: Duration = Duration::from_secs(1800);
const RELOAD_LIMIT: Duration = Duration::from_secs(1800);
const PLUGINS: &str = "plugins";
#[derive(Debug, Clone)]
pub(crate) struct Vagrant {
identity: VagrantIdentity,
}
impl Vagrant {
pub(crate) fn new(identity: VagrantIdentity) -> Vagrant {
Vagrant { identity }
}
fn dotfile(&self, cx: &Cx<'_>) -> Result<PathBuf> {
match &self.identity.dotfile {
Some(t) => Ok(cx.run.dir().join(cx.vars.render(t.template(), "identity.vagrant.dotfile")?)),
None => Ok(cx.run.dir().join(".vagrant")),
}
}
pub(crate) fn elevates(&self, scope: &crate::scope::Scope) -> bool {
self.identity
.providers
.iter()
.any(|p| scope.runs_on(p.id) && p.elevation == isoloom_manifest::Elevation::Hyperv)
}
fn provider(&self, cx: &Cx<'_>) -> Result<&VagrantProvider> {
let theirs = || {
self.identity
.providers
.iter()
.map(|p| format!("`{}`", wire_name(&p.id)))
.collect::<Vec<_>>()
.join(", ")
};
let Some(asked) = cx.scope.provider else {
return Err(Error::NoProvider { providers: theirs() });
};
self.identity.providers.iter().find(|p| p.id == asked).ok_or_else(|| Error::Provider {
provider: wire_name(&asked),
reason: format!("isn't one of this run's providers ({})", theirs()),
})
}
fn vagrant(&self, cx: &Cx<'_>, id: &str, args: &[&str]) -> Result<ProcessSpec> {
let env = with_delivered(cx, id, self.env(cx)?);
Ok(ProcessSpec {
program: "vagrant".into(),
args: args.iter().map(|a| (*a).to_owned()).collect(),
cwd: Some(cx.run.dir().to_path_buf()),
env,
stdin: None,
tools: None,
})
}
fn console(&self, cx: &Cx<'_>, args: &[&str]) -> Result<ProcessSpec> {
let mut spec = ProcessSpec::new("vagrant", args.iter().copied());
spec.env = self.env(cx)?;
spec.env.retain(|(k, _)| k != contract::CONSOLES);
Ok(spec)
}
async fn guest_addresses(&self, cx: &Cx<'_>, read: &status::Reading) -> BTreeMap<String, String> {
let mut out = BTreeMap::new();
let manifest = cx.run.manifest();
for m in manifest.machines.iter().filter(|m| answers(manifest, m)) {
let define = native_name(m);
if read.states.get(define).map(|s| status::machine_state(s)) != Some(crate::status::MachineState::Running) {
continue;
}
let asked = match self.vagrant(cx, "status", &["ssh-config", define]) {
Ok(spec) => call_ok(cx, "status", &spec, READ_LIMIT, Echo::Quiet).await,
Err(e) => Err(e),
};
match asked.map(|text| status::host_name(&text)) {
Ok(Some(host)) => drop(out.insert(define.to_owned(), host)),
Ok(None) => tracing::warn!(define, "`vagrant ssh-config` named no host: the VM's address and ports are left out"),
Err(e) => tracing::warn!(define, error = %e, "the VM's address couldn't be read: it and its ports are left out"),
}
}
out
}
async fn plugins(&self, cx: &Cx<'_>) -> Result<Vec<String>> {
let spec = self.vagrant(cx, PLUGINS, &["plugin", "list", "--machine-readable"])?;
let out = call_ok(cx, PLUGINS, &spec, READ_LIMIT, Echo::Quiet).await?;
Ok(crate::vagrant_plugins(&out).into_iter().map(|(name, _)| name).collect())
}
}
impl Driver for Vagrant {
async fn preflight(&self, cx: &Cx<'_>) -> Result<()> {
let p = self.provider(cx)?;
let id = wire_name(&p.id);
let refuse = |reason: String| Error::Provider { provider: id.clone(), reason };
if !p.hosts.contains(&cx.scope.host) {
return Err(refuse("doesn't exist on this host".into()));
}
if let Some(why) = &p.refusal {
return Err(refuse(format!("this run can't run there: {why}")));
}
if let Some(plugin) = &p.plugin
&& !self.plugins(cx).await?.iter().any(|name| name == plugin.as_str())
{
return Err(refuse(format!("needs the Vagrant plugin `{plugin}`: `vagrant plugin install {plugin}`")));
}
Ok(())
}
fn prepare(&self, cx: &Cx<'_>) -> Result<()> {
let dotfile = self.dotfile(cx)?;
match dotfile.parent() {
Some(parent) if parent.starts_with(cx.run.runner.config.home.as_path()) => crate::store::create_dir(parent)?,
_ => {}
}
Ok(())
}
fn env(&self, cx: &Cx<'_>) -> Result<Vec<(String, String)>> {
let mut env = vec![(DOTFILE_VAR.to_owned(), self.dotfile(cx)?.display().to_string())];
let forwards = cx.vars.vm_pins();
if !forwards.is_empty() {
let json: serde_json::Map<String, serde_json::Value> = forwards.iter().map(|(k, port)| (k.clone(), (*port).into())).collect();
env.push((contract::HOSTPORTS.to_owned(), serde_json::Value::Object(json).to_string()));
}
env.extend(crate::graphical::chosen::var(cx)?);
Ok(env)
}
async fn action(&self, cx: &Cx<'_>, list: &str, step: &Step, action: Action) -> Result<()> {
match action {
Action::VagrantReconcileProvider => actions::reconcile_provider(self, cx, step).await,
Action::VagrantDiscardAbortedSaved => actions::discard_aborted_saved(cx, step).await,
Action::VagrantRemoveOrphans => orphans::remove(cx, step).await,
Action::VagrantResetState => actions::reset_state(self, cx),
Action::VagrantWakeController => actions::wake_controller(self, cx, list, step).await,
Action::VagrantParkController => actions::park_controller(self, cx, list, step).await,
Action::VagrantCheckForwardedPorts => forwarded::check(cx, step).await,
other => Err(super::foreign_action(isoloom_manifest::Driver::Vagrant, step, other)),
}
}
async fn observe(&self, cx: &Cx<'_>) -> Result<Observed> {
let out = match lock::read_status(cx).await? {
lock::StatusRead::Read(out) => out,
lock::StatusRead::Busy(defines) => return Ok(busy(cx.run.manifest(), &defines)),
};
let read = status::parse(&out);
let at_vm = cx.scope.runs_on(Provider::VmwareEsxi);
let hosts = if at_vm { self.guest_addresses(cx, &read).await } else { local_hosts(cx)? };
Ok(machines(cx.run.manifest(), &read, &hosts, at_vm, cx.vars.vm_pins()))
}
fn before_down(&self, _cx: &Cx<'_>) -> impl Future<Output = Result<()>> {
std::future::ready(Ok(()))
}
fn after_down(&self, cx: &Cx<'_>) -> impl Future<Output = Result<()>> {
std::future::ready(crate::graphical::chosen::forget(cx))
}
async fn restart(&self, cx: &Cx<'_>, machine: &Machine, id: &str) -> Result<()> {
let spec = self.vagrant(cx, id, &["reload", define_of(machine, "restarting")?, "--no-provision"])?;
lock::call_ok_retrying(cx, id, &spec, RELOAD_LIMIT, Echo::Shown).await.map(drop)
}
async fn attach(&self, cx: &Cx<'_>, machine: &Machine, image: Option<&str>, network: Option<&str>) -> Result<()> {
let define = define_of(machine, "hot-adding")?;
if let Some(network) = network {
return Err(Error::Unsupported {
what: format!(
"joining `{}` to network `{network}` on a Vagrant run (a VM's networks are the Vagrantfile's)",
machine.name
),
});
}
let provider = cx.scope.provider.map(|p| format!("--provider={}", wire_name(&p)));
let mut args = vec!["up", define];
if let Some(flag) = &provider {
args.push(flag);
}
let mut spec = self.vagrant(cx, Op::Attach.as_str(), &args)?;
if let Some(box_name) = image {
spec.env.push((contract::ACCESS_BOX.to_owned(), box_name.to_owned()));
}
lock::call_ok_retrying(cx, Op::Attach.as_str(), &spec, ATTACH_LIMIT, Echo::Shown)
.await
.map(drop)
}
async fn retire(&self, cx: &Cx<'_>, machine: &Machine) -> Result<()> {
let op = cx.events.call_id();
let spec = self.vagrant(cx, op, &["destroy", "-f", define_of(machine, "taking out")?])?;
lock::call_ok_retrying(cx, op, &spec, super::REMOVE_LIMIT, Echo::Shown).await.map(drop)
}
fn console_command(&self, cx: &Cx<'_>, machine: &Machine, user: Option<&str>) -> Result<ShellCommand> {
let define = define_of(machine, "a console into")?;
let spec = match user {
None => self.console(cx, &["ssh", define])?,
Some(user) => self.console(cx, &["ssh", define, "-c", &super::shells::login_shell_as(user)])?,
};
Ok(super::console_in_run(cx, spec))
}
fn in_guest(&self, cx: &Cx<'_>, vm: &Machine, script: &str, use_: GuestUse) -> Result<ProcessSpec> {
let args = ["ssh", define_of(vm, "running a script in")?, "-c", script];
match use_ {
GuestUse::Console => self.console(cx, &args),
GuestUse::Step => self.vagrant(cx, cx.events.call_id(), &args),
}
}
}
fn define_of<'m>(machine: &'m Machine, what: &str) -> Result<&'m str> {
match &machine.native {
Some(Native::Vagrant { vagrant, .. }) => Ok(vagrant.as_str()),
Some(Native::Compose { .. }) => Err(super::no_vm_for(what, machine)),
None => Err(Error::Unsupported {
what: format!("{what} `{}`: the run doesn't say how it runs", machine.name),
}),
}
}
fn answers(manifest: &isoloom_manifest::Manifest, m: &Machine) -> bool {
m.ports.iter().any(|p| p.publish.is_some()) || manifest.guest.as_ref().is_some_and(|g| g.machine == m.name)
}
fn local_hosts(cx: &Cx<'_>) -> Result<BTreeMap<String, String>> {
let mut out = BTreeMap::new();
for m in &cx.run.manifest().machines {
out.insert(native_name(m).to_owned(), super::publish_host(cx, m)?);
}
Ok(out)
}
fn machines(
manifest: &isoloom_manifest::Manifest,
read: &status::Reading,
hosts: &BTreeMap<String, String>,
at_vm: bool,
chosen: &BTreeMap<String, u16>,
) -> Observed {
let mut out = Observed::default();
for m in &manifest.machines {
let define = native_name(m);
let state = read
.states
.get(define)
.map_or(crate::status::MachineState::Absent, |s| status::machine_state(s));
let host = hosts.get(define).filter(|_| state != crate::status::MachineState::Absent);
if let Some(host) = host.filter(|_| answers(manifest, m)) {
out.hosts.insert(m.name.to_string(), host.clone());
}
let mut published = Vec::new();
if host.is_some() {
for (name, port) in published_ports(m, at_vm, chosen) {
out.published.insert((m.name.to_string(), name), port.host_port);
published.push(port);
}
}
out.machines.push(MachineStatus {
name: m.name.to_string(),
role: m.role,
state,
addresses: super::static_addresses(m),
published,
console: None,
});
}
out
}
fn busy(manifest: &isoloom_manifest::Manifest, defines: &[String]) -> Observed {
let named = manifest
.machines
.iter()
.filter(|m| defines.iter().any(|d| d == native_name(m)))
.map(|m| m.name.to_string())
.collect();
Observed {
busy: Some(named),
..Observed::default()
}
}
fn published_ports(m: &Machine, at_vm: bool, chosen: &BTreeMap<String, u16>) -> Vec<(String, PublishedPort)> {
m.ports
.iter()
.filter_map(|p| {
let host_port = if at_vm {
p.publish.as_ref().map(|_| p.port.get())?
} else {
forwarded::host_port(m, p, chosen)?
};
let port = PublishedPort {
port: p.port.get(),
protocol: p.kind.transport(),
host_port,
};
Some((p.name.to_string(), port))
})
.collect()
}
#[cfg(test)]
mod tests;