mod actions;
mod adhoc;
mod adhoc_vm;
mod forwarded;
mod orphans;
mod status;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
use isoloom_manifest::{Action, Native, Role, SnakeName, Step, Template, VagrantIdentity, VagrantProvider, access};
use super::{ADHOC_NAME, Attached, Attacker, Driver, Observed};
use crate::Result;
use crate::error::Error;
use crate::executor::{Cx, Echo, call_ok, step_env};
use crate::process::ProcessSpec;
use crate::scope::provider_id;
use crate::state::Op;
use crate::status::{MachineStatus, PublishedPort, ShellCommand};
const DOTFILE_VAR: &str = "VAGRANT_DOTFILE_PATH";
const READ_TIMEOUT: Duration = Duration::from_secs(120);
const ATTACH_TIMEOUT: Duration = Duration::from_secs(1800);
const DETACH_TIMEOUT: Duration = Duration::from_secs(300);
const PLUGINS: &str = "plugins";
const ESXI: &str = "vmware_esxi";
const QEMU: &str = "qemu";
const LOOPBACK: &str = "127.0.0.1";
#[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 {
let asked = scope.provider.as_deref();
self.identity
.providers
.iter()
.any(|p| Some(provider_id(p.id).as_str()) == asked && p.elevation == isoloom_manifest::Elevation::Hyperv)
}
fn provider(&self, cx: &Cx<'_>) -> Result<&VagrantProvider> {
let asked = cx.scope.provider.as_deref().ok_or_else(|| Error::Provider {
provider: String::new(),
reason: "a Vagrant run needs a provider: pass one of the manifest's (`virtualbox`, `libvirt`, โฆ)".into(),
})?;
self.identity
.providers
.iter()
.find(|p| provider_id(p.id) == asked)
.ok_or_else(|| Error::Provider {
provider: asked.to_owned(),
reason: "isn't one of this run's providers".into(),
})
}
fn vagrant(&self, cx: &Cx<'_>, id: &str, args: &[&str]) -> Result<ProcessSpec> {
let mut env = self.env(cx)?;
env.extend(step_env(cx, id));
env.extend(cx.relayed.iter().flat_map(|r| r.env.iter().cloned()));
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,
})
}
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 = define_name(m);
if read.states.get(define).map(|s| status::machine_state(s)) != Some(crate::status::MachineState::Running) {
continue;
}
let Ok(spec) = self.vagrant(cx, "status", &["ssh-config", define]) else {
continue;
};
if let Ok(text) = call_ok(cx, "status", &spec, READ_TIMEOUT, Echo::Quiet).await
&& let Some(host) = status::host_name(&text)
{
out.insert(define.to_owned(), host);
}
}
out
}
async fn boot(&self, cx: &Cx<'_>, native: &str, image: Option<&str>) -> Result<()> {
let provider = cx.scope.provider.as_ref().map(|p| format!("--provider={p}"));
let mut args = vec!["up", native];
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((access::BOX_ENV.to_owned(), box_name.to_owned()));
}
call_ok(cx, Op::Attach.as_str(), &spec, ATTACH_TIMEOUT, Echo::Shown).await.map(drop)
}
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_TIMEOUT, Echo::Quiet).await?;
Ok(status::plugins(&out))
}
}
impl Driver for Vagrant {
async fn preflight(&self, cx: &Cx<'_>) -> Result<()> {
let p = self.provider(cx)?;
let id = provider_id(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::private_dir(parent).map_err(Error::io(parent, "created")),
_ => 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.forwards();
if !forwards.is_empty() {
let json = serde_json::to_string(forwards).map_err(|e| Error::Param {
name: super::HOSTPORTS.to_owned(),
reason: e.to_string(),
})?;
env.push((super::HOSTPORTS.to_owned(), json));
}
Ok(env)
}
async fn action(&self, cx: &Cx<'_>, step: &Step, action: Action, with: &BTreeMap<SnakeName, Template>) -> 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, step, with).await,
Action::VagrantParkController => actions::park_controller(self, cx, step, with).await,
Action::VagrantCheckForwardedPorts => forwarded::check(cx, step).await,
other => Err(super::foreign_action("Vagrant", step, other)),
}
}
async fn observe(&self, cx: &Cx<'_>) -> Result<Observed> {
let out = super::status_report(cx).await?;
let read = status::parse(&out);
let guests = if cx.scope.provider.as_deref() == Some(ESXI) {
Some(self.guest_addresses(cx, &read).await)
} else {
None
};
let mut observed = machines(cx.run.manifest(), &read, guests.as_ref(), cx.vars.forwards());
if !cx.run.manifest().machines.iter().any(|m| m.role == Role::Access) {
match adhoc::guest_vm(cx) {
Some(vm) => {
if read.states.get(&vm).map(|s| status::machine_state(s)) == Some(crate::status::MachineState::Running)
&& let Some(access) = adhoc::status(self, cx, &vm).await
{
observed.machines.push(access);
}
}
None => observed.machines.extend(adhoc_vm::status(self, cx, &read)),
}
}
Ok(observed)
}
fn after_down(&self, _cx: &Cx<'_>) -> Result<()> {
Ok(())
}
async fn attach(&self, cx: &Cx<'_>, attacker: Attacker<'_>, image: Option<&str>, network: Option<&str>) -> Result<Attached> {
let native = match target(cx, attacker) {
Target::AdHocIn(vm) => return adhoc::attach(self, cx, &vm, image, network).await,
Target::AdHocVm => return adhoc_vm::attach(self, cx, image, network).await,
Target::Machine(native) => native,
};
self.boot(cx, &native, image).await?;
Ok(Attached::default())
}
async fn detach(&self, cx: &Cx<'_>, attacker: Attacker<'_>) -> Result<()> {
let native = match target(cx, attacker) {
Target::AdHocIn(vm) => return adhoc::detach(self, cx, &vm).await,
Target::AdHocVm => return adhoc_vm::detach(self, cx).await,
Target::Machine(native) => native,
};
let spec = self.vagrant(cx, Op::Detach.as_str(), &["destroy", "-f", &native])?;
call_ok(cx, Op::Detach.as_str(), &spec, DETACH_TIMEOUT, Echo::Shown).await?;
Ok(())
}
fn shell_command(&self, cx: &Cx<'_>, attacker: Attacker<'_>) -> Result<ShellCommand> {
let native = match target(cx, attacker) {
Target::AdHocIn(vm) => return adhoc::shell_command(self, cx, &vm),
Target::AdHocVm => ADHOC_NAME.to_owned(),
Target::Machine(native) => native,
};
Ok(ShellCommand {
program: "vagrant".into(),
args: vec!["ssh".into(), native],
cwd: Some(cx.run.dir().to_path_buf()),
env: self.env(cx)?,
})
}
}
enum Target {
Machine(String),
AdHocIn(String),
AdHocVm,
}
fn target(cx: &Cx<'_>, attacker: Attacker<'_>) -> Target {
match attacker {
Attacker::Machine(native) => Target::Machine(native.to_owned()),
Attacker::AdHoc => adhoc::guest_vm(cx).map_or(Target::AdHocVm, Target::AdHocIn),
}
}
fn answers(manifest: &isoloom_manifest::Manifest, m: &isoloom_manifest::Machine) -> bool {
m.ports.iter().any(|p| p.publish.is_some()) || manifest.guest.as_ref().is_some_and(|g| g.machine == m.name)
}
fn define_name(m: &isoloom_manifest::Machine) -> &str {
match &m.native {
Some(Native::Vagrant { vagrant, .. }) => vagrant.as_str(),
_ => m.name.as_str(),
}
}
pub(super) fn pick_forwards(cx: &Cx<'_>) -> BTreeMap<String, u16> {
forwarded::pick(cx)
}
fn machines(
manifest: &isoloom_manifest::Manifest,
read: &status::Reading,
guests: Option<&BTreeMap<String, String>>,
chosen: &BTreeMap<String, u16>,
) -> Observed {
let mut out = Observed::default();
for m in &manifest.machines {
let define = define_name(m);
let state = read
.states
.get(define)
.map_or(crate::status::MachineState::Absent, |s| status::machine_state(s));
let host = match guests {
Some(guests) => guests.get(define).cloned(),
None => Some(LOOPBACK.to_owned()),
};
let mut published = Vec::new();
let host = host.filter(|_| state != crate::status::MachineState::Absent);
if let Some(host) = host.as_ref().filter(|_| answers(manifest, m)) {
out.hosts.insert(m.name.to_string(), host.clone());
}
if host.is_some() {
for p in &m.ports {
let Some(publish) = p.publish.as_ref() else {
continue;
};
let host_port = if guests.is_some() {
p.port.get()
} else {
chosen
.get(&forwarded::key(m, p))
.copied()
.or_else(|| publish.preferred.map(std::num::NonZeroU16::get))
.unwrap_or_else(|| p.port.get())
};
published.push(PublishedPort {
port: p.port.get(),
protocol: p.kind.transport(),
host_port,
});
out.published.insert((m.name.to_string(), p.name.to_string()), host_port);
}
}
out.machines.push(MachineStatus {
name: m.name.to_string(),
role: m.role,
state,
addresses: super::static_addresses(m),
published,
});
}
out
}
#[cfg(test)]
mod tests;