From 9e3b09d66207330c01238eb72904182c33966ca7 Mon Sep 17 00:00:00 2001 From: DataHearth Date: Wed, 16 Mar 2022 23:57:15 +0100 Subject: [PATCH] bump v0.5.0 --- README.md | 3 +++ src/images.rs | 33 +++++++++++++++++++++++---------- src/main.rs | 21 +++++++++++++++++---- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5501699..256b77f 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,9 @@ OPTIONS: --dry-run image cleanup will not be triggered [default: false] + -f, --force + should force image deletion [default: false] + -h, --help Print help information diff --git a/src/images.rs b/src/images.rs index 8345b71..82363c7 100644 --- a/src/images.rs +++ b/src/images.rs @@ -1,4 +1,5 @@ use bollard::image::ListImagesOptions; +use bollard::image::RemoveImageOptions; use bollard::models::ImageSummary; use bollard::Docker; use bollard::API_DEFAULT_VERSION; @@ -7,9 +8,6 @@ use std::collections::HashMap; use crate::DateArgs; -const GHCR_REPO: &str = "ghcr.io/datahearth/clear-docker-images"; -const DOCKER_REPO: &str = "datahearth/clear-docker-images"; - pub struct DockerActions { docker: Docker, repository: Option, @@ -23,13 +21,15 @@ impl DockerActions { repository: Option, tags: Vec, date: DateArgs, - ) -> Self { - Self { - docker: Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION).unwrap(), + ) -> Result { + let docker = Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION)?; + + Ok(Self { + docker, repository, tags, date, - } + }) } pub async fn get(&self) -> Result, bollard::errors::Error> { @@ -52,6 +52,7 @@ impl DockerActions { pub async fn delete( &self, images: Vec, + force: bool, dry_run: bool, ) -> Result { let mut removed_size = 0; @@ -59,7 +60,19 @@ impl DockerActions { info!("deleting: {}", image.id); if !dry_run { - if let Err(e) = self.docker.delete_service(&image.id).await { + let res = self + .docker + .remove_image( + &image.id, + Some(RemoveImageOptions { + force, + ..Default::default() + }), + None, + ) + .await; + + if let Err(e) = res { return Err(e); } } @@ -81,8 +94,8 @@ impl DockerActions { self.date.start > image.created && image.created > stop }) && image.repo_tags.iter().any(|tag| { - !tag.contains(GHCR_REPO) - && !tag.contains(DOCKER_REPO) + !tag.contains("ghcr.io/datahearth/clear-docker-images") + && !tag.contains("datahearth/clear-docker-images") && !self .tags .iter() diff --git a/src/main.rs b/src/main.rs index 65ed4bd..9727f5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,10 @@ struct Args { #[clap(long, takes_value = false)] dry_run: bool, + /// should force image deletion [default: false] + #[clap(short, long, takes_value = false)] + force: bool, + /// where is located the docker socket (can be a UNIX socket or TCP protocol) #[clap(short, long, default_value = "/var/run/docker.sock")] socket: String, @@ -54,7 +58,7 @@ async fn main() { exit(1); } - let actions = DockerActions::new( + let actions = match DockerActions::new( args.socket, args.repository, args.tags.map_or(vec![], |t| t), @@ -65,7 +69,13 @@ async fn main() { }, |d| d, ), - ); + ) { + Ok(d) => d, + Err(e) => { + error!("failed to connect to docker socket: {}", e); + exit(1); + } + }; let images = match actions.get().await { Ok(i) => i, @@ -75,10 +85,13 @@ async fn main() { } }; - let saved = match actions.delete(actions.filter(images), args.dry_run).await { + let saved = match actions + .delete(actions.filter(images), args.force, args.dry_run) + .await + { Ok(s) => s, Err(e) => { - error!("failed to retrieve docker images: {}", e); + error!("failed to delete docker images: {}", e); exit(1); } };