use std::path::{Path, PathBuf};
use isothrum_runner::{ErrorReport, Provider, RunOptions, SecretValue, Secrets};
use super::{Course, Failure, Found, Step, TestVm};
const BOX: &str = "bento/debian-12";
const BOX_ARCH: &str = "amd64";
const PASSWORD: &str = "ESXI_PASSWORD";
const MACHINE: &str = "default";
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Esxi {
pub host: String,
pub port: u16,
pub username: String,
pub password: SecretValue,
pub datastore: Option<String>,
pub network: Option<String>,
pub boxes: Option<PathBuf>,
}
impl Esxi {
#[must_use]
pub fn new(host: impl Into<String>, username: impl Into<String>, password: SecretValue) -> Esxi {
Esxi {
host: host.into(),
port: 22,
username: username.into(),
password,
datastore: None,
network: None,
boxes: None,
}
}
pub(super) fn ssh_address(&self) -> String {
if self.host.contains(':') && !self.host.starts_with('[') {
format!("[{}]:{}", self.host, self.port)
} else {
format!("{}:{}", self.host, self.port)
}
}
pub(super) fn project(&self, vm: &TestVm<'_>) -> (&'static str, String) {
let mut provider = vec![
format!(" v.esxi_hostname = {}", ruby(&self.host)),
format!(" v.esxi_hostport = {}", self.port),
format!(" v.esxi_username = {}", ruby(&self.username)),
format!(" v.esxi_password = \"env:{PASSWORD}\""),
format!(" v.guest_name = {}", ruby(vm.name)),
" v.guest_numvcpus = 1".to_owned(),
" v.guest_memsize = 512".to_owned(),
];
if let Some(datastore) = &self.datastore {
provider.push(format!(" v.esxi_disk_store = {}", ruby(datastore)));
}
if let Some(network) = &self.network {
provider.push(format!(" v.esxi_virtual_network = [{}]", ruby(network)));
}
let text = [
"Vagrant.configure(\"2\") do |config|".to_owned(),
format!(" config.vm.box = \"{BOX}\""),
format!(" config.vm.box_architecture = \"{BOX_ARCH}\""),
format!(" config.vm.hostname = {}", ruby(vm.hostname)),
" config.vm.synced_folder \".\", \"/vagrant\", disabled: true".to_owned(),
" config.vm.provider \"vmware_esxi\" do |v|".to_owned(),
provider.join("\n"),
" end".to_owned(),
"end\n".to_owned(),
]
.join("\n");
("Vagrantfile", text)
}
pub(super) fn options(&self) -> RunOptions {
let mut secrets = Secrets::new();
secrets.insert(PASSWORD, self.password.clone());
RunOptions::new().with_provider(Provider::VmwareEsxi).with_secrets(secrets)
}
}
fn ruby(s: &str) -> String {
format!("'{}'", s.replace('\\', "\\\\").replace('\'', "\\'"))
}
pub(super) fn course(e: &Esxi) -> OnEsxi {
OnEsxi::new(e.boxes.clone().or_else(vagrant_boxes))
}
fn vagrant_boxes() -> Option<PathBuf> {
let var = |name: &str| std::env::var_os(name).filter(|v| !v.is_empty()).map(PathBuf::from);
let home = var("VAGRANT_HOME").or_else(|| var("HOME").or_else(|| var("USERPROFILE")).map(|h| h.join(".vagrant.d")))?;
Some(home.join("boxes"))
}
#[derive(Debug, Default)]
struct Named {
name: Option<String>,
version: Option<String>,
arch: Option<String>,
}
#[derive(Debug)]
pub(super) struct OnEsxi {
boxes: Option<PathBuf>,
named: Named,
size: Option<u64>,
ovftool_said: Option<String>,
}
impl OnEsxi {
fn new(boxes: Option<PathBuf>) -> OnEsxi {
OnEsxi {
boxes,
named: Named::default(),
size: None,
ovftool_said: None,
}
}
fn note_box(&mut self, line: &str) {
if let Some((_, rest)) = line.split_once("Adding box '") {
let Some((name, rest)) = rest.split_once('\'') else { return };
self.named.name = Some(name.to_owned());
self.named.version = rest.split_once("(v").and_then(|(_, v)| v.split_once(')')).map(|(v, _)| v.to_owned());
self.named.arch = rest
.split_once("for provider:")
.and_then(|(_, p)| p.rsplit_once('('))
.and_then(|(_, a)| a.split_once(')'))
.map(|(a, _)| a.trim().to_owned());
} else if let Some((key, value)) = summary(line) {
match key {
"Box" => self.named.name = Some(value.to_owned()),
"Box Ver" => self.named.version = Some(value.to_owned()),
_ => {}
}
}
if self.size.is_none()
&& let (
Some(boxes),
Named {
name: Some(name),
version: Some(version),
arch,
},
) = (self.boxes.as_deref(), &self.named)
{
self.size = stored_size(boxes, name, version, arch.as_deref());
}
}
fn box_found(&self) -> Found {
let Named {
name: Some(name),
version,
arch,
} = &self.named
else {
return Found::Nothing;
};
let arch = arch.clone().or_else(|| stored_arch(self.boxes.as_deref()?, name, version.as_deref()?));
Found::Box {
name: name.clone(),
version: version.clone(),
arch,
}
}
}
fn summary(line: &str) -> Option<(&str, &str)> {
let (_, said) = line.split_once("--- ")?;
let (key, value) = said.split_once(':')?;
Some((key.trim(), value.trim()))
}
fn stored_arch(boxes: &Path, name: &str, version: &str) -> Option<String> {
let arches = vmware_builds(boxes, name, version);
(!arches.is_empty()).then(|| arches.into_keys().collect::<Vec<_>>().join(" or "))
}
fn stored_size(boxes: &Path, name: &str, version: &str, arch: Option<&str>) -> Option<u64> {
let builds = vmware_builds(boxes, name, version);
let build = match arch {
Some(arch) => builds.get(arch)?,
None if builds.len() == 1 => builds.values().next()?,
None => return None,
};
let size: u64 = std::fs::read_dir(build)
.ok()?
.flatten()
.filter_map(|f| f.metadata().ok())
.filter(std::fs::Metadata::is_file)
.map(|m| m.len())
.sum();
(size > 0).then_some(size)
}
fn vmware_builds(boxes: &Path, name: &str, version: &str) -> std::collections::BTreeMap<String, PathBuf> {
let dir = boxes.join(name.replace('/', "-VAGRANTSLASH-")).join(version);
let vmware = |arch: &Path| {
std::fs::read_dir(arch)
.into_iter()
.flatten()
.flatten()
.map(|p| p.path())
.find(|p| p.file_name().is_some_and(|n| n.to_string_lossy().starts_with("vmware")))
};
std::fs::read_dir(dir)
.into_iter()
.flatten()
.flatten()
.filter_map(|a| Some((a.file_name().to_string_lossy().into_owned(), vmware(&a.path())?)))
.collect()
}
impl Course for OnEsxi {
const UP: &'static [Step] = &[Step::Image, Step::Vm, Step::Address, Step::Ssh];
fn machine(&self) -> &'static str {
MACHINE
}
fn ends(&mut self, line: &str) -> Option<usize> {
self.note_box(line);
if line.trim_start().starts_with("Error:") {
self.ovftool_said = Some(line.trim().to_owned());
}
if summary(line).is_some_and(|(key, _)| key == "VMID") {
Some(0)
} else if line.contains("VM has been Powered On") {
Some(1)
} else if line.contains("state is now \"running\"") {
Some(2)
} else {
None
}
}
fn bytes(&self, at: usize) -> Option<u64> {
(at == 0).then_some(self.size).flatten()
}
fn found(&self, at: usize) -> Found {
if at == 0 { self.box_found() } else { Found::Nothing }
}
fn explain(&self, report: &ErrorReport) -> Option<(usize, Failure)> {
let raw = &report.message;
let said = self.ovftool_said.as_deref().unwrap_or_default();
let boxed = || Box::new(report.clone());
if said.contains("Duplicate name") || raw.contains("Duplicate name") {
return Some((0, Failure::NameTaken));
}
if raw.contains("OVF Tool error") || raw.contains("ovftool not found") {
let why = if said.is_empty() { raw.trim() } else { said };
return Some((0, Failure::Upload(why.to_owned())));
}
if raw.contains("box") && (raw.contains("could not be found") || raw.contains("doesn't support the provider") || raw.contains("architecture")) {
return Some((0, Failure::NoBox(boxed())));
}
if raw.contains("Unable to power on VM") {
return Some((1, Failure::PowerOn(boxed())));
}
if raw.contains("timeout waiting for \"running\"") {
return Some((2, Failure::NoAddress(boxed())));
}
if [
"Timed out while waiting for the machine to boot",
"SSH authentication failed",
"The SSH connection was unexpectedly closed",
]
.iter()
.any(|s| raw.contains(s))
{
return Some((3, Failure::NoSsh(boxed())));
}
None
}
}
#[cfg(test)]
mod tests;