1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 09:04:12 +02:00

improve cleanup of leftover networks and workdirs

This commit is contained in:
Robin Appelman 2026-02-09 17:30:41 +01:00
commit 755573ba4b
2 changed files with 32 additions and 9 deletions

View file

@ -17,7 +17,7 @@ use bollard::Docker;
use itertools::Itertools;
use miette::{IntoDiagnostic, Report, Result, WrapErr};
use std::env::{var, vars};
use std::fs::{create_dir_all, write};
use std::fs::{create_dir_all, remove_dir_all, write};
use std::io::stdout;
use std::os::unix::process::CommandExt;
use std::process::{Command, ExitCode};
@ -66,12 +66,33 @@ async fn main() -> Result<ExitCode> {
match args {
HazeArgs::Clean => {
let list = Cloud::list(&docker, None, &config).await?;
for cloud in list.into_iter().filter(|cloud| !cloud.pinned) {
let (retain, remove) = list
.into_iter()
.partition::<Vec<_>, _>(|cloud| cloud.pinned);
for cloud in remove {
if let Err(e) = cloud.destroy(&docker).await {
eprintln!("Error while removing cloud: {:#}", e);
}
}
clear_networks(&docker).await?;
clear_networks(&docker, &retain).await?;
for cache_dir in config.work_dir.read_dir().into_diagnostic()? {
let cache_dir = cache_dir.into_diagnostic()?;
if let Some(id) = cache_dir
.file_name()
.to_str()
.and_then(|name| name.strip_prefix("haze-"))
{
if !retain.iter().any(|cloud| cloud.id == id) {
let path = cache_dir.path();
remove_dir_all(&path).into_diagnostic().wrap_err_with(|| {
format!("Failed to cleanup stray workdir: {}", path.display())
})?;
}
}
}
}
HazeArgs::List { filter } => {
let list = Cloud::list(&docker, filter, &config).await?;

View file

@ -1,19 +1,21 @@
use bollard::Docker;
use crate::cloud::Cloud;
use bollard::network::CreateNetworkOptions;
use bollard::Docker;
use miette::{IntoDiagnostic, Result, WrapErr};
pub async fn clear_networks(docker: &Docker) -> Result<()> {
pub async fn clear_networks(docker: &Docker, instances: &[Cloud]) -> Result<()> {
let networks = docker
.list_networks::<&str>(None)
.await
.into_diagnostic()
.wrap_err("Failed to list docker networks")?;
for network in networks {
match network.name.as_deref() {
Some(name) if name.starts_with("haze-") => {
if let Some(name) = network.name.as_deref() {
if let Some(id) = name.strip_prefix("haze-") {
if !instances.iter().any(|cloud| cloud.id == id) {
docker.remove_network(name).await.ok();
}
_ => {}
}
}
}
Ok(())