use std::time::Duration;
use isothrum_runner::{Arch, DownloadToAdopt, ProcessSpec, Provider};
use crate::Context;
const ADD_LIMIT: Duration = Duration::from_secs(1800);
const SAID_LINES: usize = 10;
#[must_use]
pub fn box_provider(p: Provider) -> &'static str {
match p {
Provider::Qemu => "libvirt",
_ => p.id(),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Listed {
name: String,
provider: String,
version: String,
arch: Option<String>,
}
fn listed(out: &str) -> Vec<Listed> {
out.lines()
.filter_map(|line| {
let (name, rest) = line.trim().split_once(char::is_whitespace)?;
let inner = rest.trim().strip_prefix('(')?;
let mut fields = inner.split(',').map(|f| f.trim().trim_end_matches(')'));
let provider = fields.next().filter(|p| !p.is_empty())?;
let version = fields.next().filter(|v| !v.is_empty())?;
let arch = fields.next().map(|a| a.trim_start_matches('(')).filter(|a| !a.is_empty()).map(str::to_owned);
Some(Listed {
name: name.to_owned(),
provider: provider.to_owned(),
version: version.to_owned(),
arch,
})
})
.collect()
}
pub(crate) fn arch_name(arch: Arch) -> &'static str {
match arch {
Arch::Amd64 => "amd64",
Arch::Arm64 => "arm64",
}
}
pub(crate) fn held(list: &str, name: &str, p: Provider, arch: Arch) -> Option<String> {
listed(list).into_iter().find_map(|b| {
let other_cpu = matches!(b.arch.as_deref(), Some(a @ ("amd64" | "arm64")) if a != arch_name(arch));
(b.name == name && b.provider == box_provider(p) && !other_cpu).then_some(b.version)
})
}
pub async fn add_box(cx: &Context, p: Provider, name: &str, line: &mut (dyn FnMut(&str) + Send)) -> Result<Option<DownloadToAdopt>, String> {
let args = ["box", "add", name, "--provider", box_provider(p), "--force"];
vagrant_add(cx, &args, &format!("vagrant box add {name}"), line).await?;
Ok(listed_as_added(cx, p, name).await)
}
pub async fn add_file(
cx: &Context,
p: Provider,
name: &str,
file: &std::path::Path,
line: &mut (dyn FnMut(&str) + Send),
) -> Result<Option<DownloadToAdopt>, String> {
let file = file.display().to_string();
let args = ["box", "add", "--name", name, file.as_str(), "--force"];
vagrant_add(cx, &args, &format!("vagrant box add --name {name}"), line).await?;
Ok(listed_as_added(cx, p, name).await)
}
async fn vagrant_add(cx: &Context, args: &[&str], what: &str, line: &mut (dyn FnMut(&str) + Send)) -> Result<(), String> {
let spec = ProcessSpec::new("vagrant", args.iter().copied());
let mut said: Vec<String> = Vec::new();
let mut on_line = |stream: isothrum_runner::Stream, l: &str| {
if stream == isothrum_runner::Stream::Stderr && !l.trim().is_empty() {
if said.len() == SAID_LINES {
said.remove(0);
}
said.push(l.trim().to_owned());
}
line(l);
};
let added = tokio::time::timeout(ADD_LIMIT, cx.runner.run(&spec, &mut on_line)).await;
let exit = added.map_err(|_| format!("vagrant box add stalled: nothing for {} minutes", ADD_LIMIT.as_secs() / 60))?;
let exit = exit.map_err(|e| e.to_string())?;
if exit.success() {
return Ok(());
}
Err(if said.is_empty() {
format!("{what} failed ({exit})")
} else {
format!("{what} failed: {} ({exit})", said.join("\n"))
})
}
pub async fn held_here(cx: &Context, p: Provider, name: &str) -> Option<String> {
let list = cx.read_ok("vagrant", &["box", "list"]).await.unwrap_or_default();
held(&list, name, p, cx.host.arch)
}
async fn listed_as_added(cx: &Context, p: Provider, name: &str) -> Option<DownloadToAdopt> {
held_here(cx, p, name).await.map(|version| adopted(name, p, version))
}
fn adopted(name: &str, provider: Provider, version: String) -> DownloadToAdopt {
DownloadToAdopt::Box {
name: name.to_owned(),
provider: box_provider(provider).to_owned(),
version,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_box_is_held_for_its_provider_and_this_cpu() {
let list = "generic/debian12 (virtualbox, 202510.26.0, (amd64))\r\ngeneric/alpine319\t(libvirt, 4.3.12)\nThere are no installed boxes!\nodd (\n";
assert_eq!(
held(list, "generic/debian12", Provider::Virtualbox, Arch::Amd64).as_deref(),
Some("202510.26.0")
);
assert_eq!(held(list, "generic/debian12", Provider::Virtualbox, Arch::Arm64), None, "another CPU's");
assert_eq!(
held(list, "generic/alpine319", Provider::Qemu, Arch::Arm64).as_deref(),
Some("4.3.12"),
"QEMU boots libvirt boxes, any CPU listed"
);
assert_eq!(held(list, "generic/alpine319", Provider::Virtualbox, Arch::Amd64), None);
}
#[tokio::test]
async fn a_failed_add_says_what_vagrant_said() {
use isothrum_runner::fake::{FakeProcesses, Reply};
let lock = "Download to global Vagrant location already in progress. This\nmay be caused by other Vagrant processes attempting to download\na file to the same location.\n\nDownload path: /home/u/.vagrant.d/tmp/boxd9\nLock file path: /home/u/.vagrant.d/tmp/boxd9.lock";
for (reply, expected) in [
(
Reply::fail(1, lock),
"vagrant box add bento/debian-12 failed: Download to global Vagrant location already in progress. This\nmay be caused by other Vagrant processes attempting to download\na file to the same location.\nDownload path: /home/u/.vagrant.d/tmp/boxd9\nLock file path: /home/u/.vagrant.d/tmp/boxd9.lock (exited with code 1)",
),
(Reply::fail(1, ""), "vagrant box add bento/debian-12 failed (exited with code 1)"),
] {
let fake = FakeProcesses::script().expect("vagrant box add bento/debian-12", reply).build();
let cx = crate::testing::answered_by(fake);
let mut lines = Vec::new();
let failed = add_box(&cx, Provider::Virtualbox, "bento/debian-12", &mut |l| lines.push(l.to_owned())).await;
assert_eq!(failed.err().as_deref(), Some(expected), "{expected}");
}
}
#[tokio::test]
async fn a_box_is_held_here_as_vagrant_lists_it() {
use isothrum_runner::fake::{FakeProcesses, Reply};
let fake = FakeProcesses::script().expect("vagrant box list", Reply::ok("local/tools (utm, 0)\n")).build();
let cx = crate::testing::answered_by(fake);
assert_eq!(held_here(&cx, Provider::Utm, "local/tools").await.as_deref(), Some("0"));
assert_eq!(held_here(&cx, Provider::Utm, "other/box").await, None);
}
#[tokio::test]
async fn a_saved_box_file_is_added_under_its_name() {
use isothrum_runner::fake::{FakeProcesses, Reply};
let fake = FakeProcesses::script()
.expect("vagrant box add --name local/tools /tmp/saved.box --force", Reply::ok(""))
.expect("vagrant box list", Reply::ok("local/tools (virtualbox, 0)\n"))
.build();
let cx = crate::testing::answered_by(fake);
let added = add_file(&cx, Provider::Virtualbox, "local/tools", std::path::Path::new("/tmp/saved.box"), &mut |_| {}).await;
assert_eq!(added, Ok(Some(adopted("local/tools", Provider::Virtualbox, "0".to_owned()))));
let refused = FakeProcesses::script()
.expect("vagrant box add --name local/tools", Reply::fail(1, "not a box"))
.build();
let failed = add_file(
&crate::testing::answered_by(refused),
Provider::Virtualbox,
"local/tools",
std::path::Path::new("/tmp/saved.box"),
&mut |_| {},
)
.await;
assert_eq!(
failed.err().as_deref(),
Some("vagrant box add --name local/tools failed: not a box (exited with code 1)")
);
}
}