use isothrum_runner::{HostOs, Provider};
use crate::Context;
const LIBVIRT: [&str; 2] = ["-c", "qemu:///system"];
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub(super) struct Networks {
vbox_ifs: Vec<String>,
vbox_nets: Vec<String>,
libvirt: Vec<String>,
}
fn vbox_names(out: &str) -> Vec<String> {
out.lines()
.filter_map(|l| l.strip_prefix("Name:"))
.map(|n| n.trim().to_owned())
.filter(|n| !n.is_empty())
.collect()
}
fn lines(out: &str) -> Vec<String> {
out.lines().map(str::trim).filter(|l| !l.is_empty()).map(str::to_owned).collect()
}
fn virsh<'a>(args: &[&'a str]) -> Vec<&'a str> {
LIBVIRT.iter().copied().chain(args.iter().copied()).collect()
}
impl Networks {
pub(super) async fn here(cx: &Context, provider: Provider) -> Networks {
if cx.host.os == HostOs::Windows {
return Networks::default();
}
let listed =
async |program: &str, args: &[&str], parse: fn(&str) -> Vec<String>| cx.read_ok(program, args).await.map(|out| parse(&out)).unwrap_or_default();
match provider {
Provider::Virtualbox => Networks {
vbox_ifs: listed("VBoxManage", &["list", "hostonlyifs"], vbox_names).await,
vbox_nets: listed("VBoxManage", &["list", "hostonlynets"], vbox_names).await,
libvirt: Vec::new(),
},
Provider::Libvirt => Networks {
libvirt: listed("virsh", &virsh(&["net-list", "--all", "--name"]), lines).await,
..Networks::default()
},
_ => Networks::default(),
}
}
fn made_since(&self, before: &Networks) -> Networks {
let new = |now: &[String], then: &[String]| now.iter().filter(|n| !then.contains(n)).cloned().collect();
Networks {
vbox_ifs: new(&self.vbox_ifs, &before.vbox_ifs),
vbox_nets: new(&self.vbox_nets, &before.vbox_nets),
libvirt: new(&self.libvirt, &before.libvirt),
}
}
}
pub(super) async fn remove_made_since(cx: &Context, provider: Provider, before: &Networks) -> Result<Vec<String>, String> {
if cx.host.os == HostOs::Windows {
return Ok(Vec::new());
}
let made = Networks::here(cx, provider).await.made_since(before);
let mut done = Removal::default();
remove_vbox(cx, &made, &mut done).await;
remove_libvirt(cx, &made.libvirt, &mut done).await;
if done.failed.is_empty() {
Ok(done.removed)
} else {
Err(done.failed.join("; "))
}
}
#[derive(Default)]
struct Removal {
removed: Vec<String>,
failed: Vec<String>,
}
impl Removal {
fn note<T>(&mut self, name: &str, went: Result<T, String>) {
match went {
Ok(_) => self.removed.push(name.to_owned()),
Err(e) => self.failed.push(format!("{name}: {e}")),
}
}
}
async fn remove_vbox(cx: &Context, made: &Networks, done: &mut Removal) {
if made.vbox_ifs.is_empty() && made.vbox_nets.is_empty() {
return;
}
let vms = cx.read_ok("VBoxManage", &["list", "vms", "--long"]).await.unwrap_or_default();
for name in made.vbox_ifs.iter().filter(|n| !vbox_uses(&vms, "Host-only Interface", n)) {
let went = cx.read_ok("VBoxManage", &["hostonlyif", "remove", name]).await;
if went.is_ok() {
drop(
cx.read_ok("VBoxManage", &["dhcpserver", "remove", &format!("--network=HostInterfaceNetworking-{name}")])
.await,
);
}
done.note(name, went);
}
for name in made.vbox_nets.iter().filter(|n| !vbox_uses(&vms, "Host-only Network", n)) {
done.note(name, cx.read_ok("VBoxManage", &["hostonlynet", "remove", &format!("--name={name}")]).await);
}
}
async fn remove_libvirt(cx: &Context, made: &[String], done: &mut Removal) {
for name in made {
if libvirt_in_use(cx, name).await {
continue;
}
drop(cx.read_ok("virsh", &virsh(&["net-destroy", name])).await);
done.note(name, cx.read_ok("virsh", &virsh(&["net-undefine", name])).await);
}
}
fn vbox_uses(vms: &str, kind: &str, name: &str) -> bool {
vms.lines().any(|l| l.contains(kind) && l.contains(&format!("'{name}'")))
}
async fn libvirt_in_use(cx: &Context, name: &str) -> bool {
let Ok(domains) = cx.read_ok("virsh", &virsh(&["list", "--all", "--name"])).await else {
return true;
};
for domain in lines(&domains) {
let Ok(ifs) = cx.read_ok("virsh", &virsh(&["domiflist", &domain])).await else {
return true;
};
if ifs.lines().any(|l| l.split_whitespace().nth(2) == Some(name)) {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_names_listed_are_read_and_what_is_new_is_told_apart() {
let listing = "Name: vboxnet0\nGUID: 786f\n\nName: vboxnet1\n";
assert_eq!(vbox_names(listing), ["vboxnet0", "vboxnet1"]);
let before = Networks {
vbox_ifs: vec!["vboxnet0".into()],
libvirt: vec!["default".into()],
..Networks::default()
};
let now = Networks {
vbox_ifs: vec!["vboxnet0".into(), "vboxnet1".into()],
libvirt: vec!["default".into(), "vagrant-libvirt".into()],
..Networks::default()
};
let made = Networks {
vbox_ifs: vec!["vboxnet1".into()],
libvirt: vec!["vagrant-libvirt".into()],
..Networks::default()
};
assert_eq!(now.made_since(&before), made);
assert!(vbox_uses(
"NIC 2: MAC: 0800, Attachment: Host-only Interface 'vboxnet1', Cable connected: on",
"Host-only Interface",
"vboxnet1"
));
assert!(!vbox_uses(
"NIC 2: MAC: 0800, Attachment: Host-only Interface 'vboxnet10'",
"Host-only Interface",
"vboxnet1"
));
}
}