mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 17:14:08 +02:00
improve cleanup of leftover networks and workdirs
This commit is contained in:
parent
4bb6a21d7c
commit
755573ba4b
2 changed files with 32 additions and 9 deletions
27
src/main.rs
27
src/main.rs
|
|
@ -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?;
|
||||
|
|
|
|||
|
|
@ -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-") => {
|
||||
docker.remove_network(name).await.ok();
|
||||
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(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue