bump v0.5.0
This commit is contained in:
parent
a64d2f56f2
commit
9e3b09d662
|
@ -57,6 +57,9 @@ OPTIONS:
|
||||||
--dry-run
|
--dry-run
|
||||||
image cleanup will not be triggered [default: false]
|
image cleanup will not be triggered [default: false]
|
||||||
|
|
||||||
|
-f, --force
|
||||||
|
should force image deletion [default: false]
|
||||||
|
|
||||||
-h, --help
|
-h, --help
|
||||||
Print help information
|
Print help information
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use bollard::image::ListImagesOptions;
|
use bollard::image::ListImagesOptions;
|
||||||
|
use bollard::image::RemoveImageOptions;
|
||||||
use bollard::models::ImageSummary;
|
use bollard::models::ImageSummary;
|
||||||
use bollard::Docker;
|
use bollard::Docker;
|
||||||
use bollard::API_DEFAULT_VERSION;
|
use bollard::API_DEFAULT_VERSION;
|
||||||
|
@ -7,9 +8,6 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::DateArgs;
|
use crate::DateArgs;
|
||||||
|
|
||||||
const GHCR_REPO: &str = "ghcr.io/datahearth/clear-docker-images";
|
|
||||||
const DOCKER_REPO: &str = "datahearth/clear-docker-images";
|
|
||||||
|
|
||||||
pub struct DockerActions {
|
pub struct DockerActions {
|
||||||
docker: Docker,
|
docker: Docker,
|
||||||
repository: Option<String>,
|
repository: Option<String>,
|
||||||
|
@ -23,13 +21,15 @@ impl DockerActions {
|
||||||
repository: Option<String>,
|
repository: Option<String>,
|
||||||
tags: Vec<String>,
|
tags: Vec<String>,
|
||||||
date: DateArgs,
|
date: DateArgs,
|
||||||
) -> Self {
|
) -> Result<Self, bollard::errors::Error> {
|
||||||
Self {
|
let docker = Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION)?;
|
||||||
docker: Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION).unwrap(),
|
|
||||||
|
Ok(Self {
|
||||||
|
docker,
|
||||||
repository,
|
repository,
|
||||||
tags,
|
tags,
|
||||||
date,
|
date,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(&self) -> Result<Vec<ImageSummary>, bollard::errors::Error> {
|
pub async fn get(&self) -> Result<Vec<ImageSummary>, bollard::errors::Error> {
|
||||||
|
@ -52,6 +52,7 @@ impl DockerActions {
|
||||||
pub async fn delete(
|
pub async fn delete(
|
||||||
&self,
|
&self,
|
||||||
images: Vec<ImageSummary>,
|
images: Vec<ImageSummary>,
|
||||||
|
force: bool,
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
) -> Result<i64, bollard::errors::Error> {
|
) -> Result<i64, bollard::errors::Error> {
|
||||||
let mut removed_size = 0;
|
let mut removed_size = 0;
|
||||||
|
@ -59,7 +60,19 @@ impl DockerActions {
|
||||||
info!("deleting: {}", image.id);
|
info!("deleting: {}", image.id);
|
||||||
|
|
||||||
if !dry_run {
|
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);
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,8 +94,8 @@ impl DockerActions {
|
||||||
self.date.start > image.created && image.created > stop
|
self.date.start > image.created && image.created > stop
|
||||||
})
|
})
|
||||||
&& image.repo_tags.iter().any(|tag| {
|
&& image.repo_tags.iter().any(|tag| {
|
||||||
!tag.contains(GHCR_REPO)
|
!tag.contains("ghcr.io/datahearth/clear-docker-images")
|
||||||
&& !tag.contains(DOCKER_REPO)
|
&& !tag.contains("datahearth/clear-docker-images")
|
||||||
&& !self
|
&& !self
|
||||||
.tags
|
.tags
|
||||||
.iter()
|
.iter()
|
||||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -31,6 +31,10 @@ struct Args {
|
||||||
#[clap(long, takes_value = false)]
|
#[clap(long, takes_value = false)]
|
||||||
dry_run: bool,
|
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)
|
/// where is located the docker socket (can be a UNIX socket or TCP protocol)
|
||||||
#[clap(short, long, default_value = "/var/run/docker.sock")]
|
#[clap(short, long, default_value = "/var/run/docker.sock")]
|
||||||
socket: String,
|
socket: String,
|
||||||
|
@ -54,7 +58,7 @@ async fn main() {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let actions = DockerActions::new(
|
let actions = match DockerActions::new(
|
||||||
args.socket,
|
args.socket,
|
||||||
args.repository,
|
args.repository,
|
||||||
args.tags.map_or(vec![], |t| t),
|
args.tags.map_or(vec![], |t| t),
|
||||||
|
@ -65,7 +69,13 @@ async fn main() {
|
||||||
},
|
},
|
||||||
|d| d,
|
|d| d,
|
||||||
),
|
),
|
||||||
);
|
) {
|
||||||
|
Ok(d) => d,
|
||||||
|
Err(e) => {
|
||||||
|
error!("failed to connect to docker socket: {}", e);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let images = match actions.get().await {
|
let images = match actions.get().await {
|
||||||
Ok(i) => i,
|
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,
|
Ok(s) => s,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("failed to retrieve docker images: {}", e);
|
error!("failed to delete docker images: {}", e);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Reference in New Issue