bump v0.5.0

This commit is contained in:
DataHearth 2022-03-16 23:57:15 +01:00
parent a64d2f56f2
commit 9e3b09d662
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
3 changed files with 43 additions and 14 deletions

View File

@ -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

View File

@ -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<String>,
@ -23,13 +21,15 @@ impl DockerActions {
repository: Option<String>,
tags: Vec<String>,
date: DateArgs,
) -> Self {
Self {
docker: Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION).unwrap(),
) -> Result<Self, bollard::errors::Error> {
let docker = Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION)?;
Ok(Self {
docker,
repository,
tags,
date,
}
})
}
pub async fn get(&self) -> Result<Vec<ImageSummary>, bollard::errors::Error> {
@ -52,6 +52,7 @@ impl DockerActions {
pub async fn delete(
&self,
images: Vec<ImageSummary>,
force: bool,
dry_run: bool,
) -> Result<i64, bollard::errors::Error> {
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()

View File

@ -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);
}
};