This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
clear-docker-images/src/images.rs

112 lines
3.0 KiB
Rust
Raw Permalink Normal View History

2022-03-16 12:17:15 +01:00
use bollard::image::ListImagesOptions;
2022-03-16 23:57:15 +01:00
use bollard::image::RemoveImageOptions;
2022-03-16 12:17:15 +01:00
use bollard::models::ImageSummary;
use bollard::Docker;
use bollard::API_DEFAULT_VERSION;
use log::info;
use std::collections::HashMap;
2022-02-20 23:06:15 +01:00
use crate::DateArgs;
2022-02-20 23:06:15 +01:00
2022-03-16 12:17:15 +01:00
pub struct DockerActions {
docker: Docker,
2022-02-20 23:06:15 +01:00
repository: Option<String>,
tags: Vec<String>,
2022-03-16 12:17:15 +01:00
date: DateArgs,
}
2022-02-20 23:06:15 +01:00
2022-03-16 12:17:15 +01:00
impl DockerActions {
pub fn new(
socket: String,
repository: Option<String>,
tags: Vec<String>,
date: DateArgs,
2022-03-16 23:57:15 +01:00
) -> Result<Self, bollard::errors::Error> {
let docker = Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION)?;
Ok(Self {
docker,
2022-03-16 12:17:15 +01:00
repository,
tags,
date,
2022-03-16 23:57:15 +01:00
})
2022-02-20 23:06:15 +01:00
}
2022-03-16 12:17:15 +01:00
pub async fn get(&self) -> Result<Vec<ImageSummary>, bollard::errors::Error> {
let mut image_filters = HashMap::new();
2022-02-20 23:06:15 +01:00
2022-03-16 12:17:15 +01:00
// why using &self.repository instead of selft.repository ?
if let Some(r) = &self.repository {
image_filters.insert("reference", vec![r.as_str()]);
}
2022-02-20 23:06:15 +01:00
2022-03-16 12:17:15 +01:00
self.docker
.list_images(Some(ListImagesOptions {
all: true,
filters: image_filters,
..Default::default()
}))
.await
}
2022-02-20 23:06:15 +01:00
2022-03-16 12:17:15 +01:00
pub async fn delete(
&self,
images: Vec<ImageSummary>,
2022-03-16 23:57:15 +01:00
force: bool,
2022-03-16 12:17:15 +01:00
dry_run: bool,
) -> Result<i64, bollard::errors::Error> {
let mut removed_size = 0;
for image in images {
info!("deleting: {}", image.id);
if !dry_run {
2022-03-16 23:57:15 +01:00
let res = self
.docker
.remove_image(
&image.id,
Some(RemoveImageOptions {
force,
..Default::default()
}),
None,
)
.await;
if let Err(e) = res {
2022-03-16 12:17:15 +01:00
return Err(e);
}
2022-02-20 23:06:15 +01:00
}
2022-03-16 12:17:15 +01:00
removed_size += image.size;
2022-02-20 23:06:15 +01:00
}
2022-03-16 12:17:15 +01:00
Ok(removed_size)
}
2022-02-20 23:06:15 +01:00
2022-03-16 12:17:15 +01:00
pub fn filter(&self, images: Vec<ImageSummary>) -> Vec<ImageSummary> {
let mut to_be_deleted: Vec<ImageSummary> = vec![];
for image in images {
if self
.date
.stop
.map_or(self.date.start > image.created, |stop| {
self.date.start > image.created && image.created > stop
})
&& image.repo_tags.iter().any(|tag| {
2022-03-16 23:57:15 +01:00
!tag.contains("ghcr.io/datahearth/clear-docker-images")
&& !tag.contains("datahearth/clear-docker-images")
2022-03-16 13:59:16 +01:00
&& !self
2022-03-16 12:17:15 +01:00
.tags
.iter()
2022-03-16 13:59:16 +01:00
.any(|excluded_tag| tag.contains(excluded_tag))
2022-03-16 12:17:15 +01:00
})
{
to_be_deleted.push(image);
}
}
2022-02-20 23:06:15 +01:00
2022-03-16 12:17:15 +01:00
return to_be_deleted;
2022-02-20 23:06:15 +01:00
}
}