Skip to content

Commit

Permalink
Merge pull request #303 from Glubiz/main
Browse files Browse the repository at this point in the history
Adding min-age command line option (-m, --min-age)
  • Loading branch information
stepchowfun authored May 3, 2024
2 parents 0fcf7aa + 359a245 commit 943f787
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.25.0] - 2024-05-02

### Added
- Added `--min-age` argument.

## [0.24.0] - 2024-04-05

### Fixed
Expand Down
81 changes: 80 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "docuum"
version = "0.24.0"
version = "0.25.0"
authors = ["Stephan Boyer <[email protected]>"]
edition = "2021"
description = "LRU eviction of Docker images."
Expand Down Expand Up @@ -28,6 +28,7 @@ regex = { version = "1.5.5", default-features = false, features = ["std", "unico
serde_json = "1.0"
serde_yaml = "0.8"
tempfile = "3"
parse_duration = "2.1.1"

[target.'cfg(target_os = "linux")'.dependencies]
sysinfo = "0.23.5"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ OPTIONS:
-k, --keep <REGEX>...
Prevents deletion of images for which repository:tag matches <REGEX>
-m, --min-age <MIN AGE>
Sets the minimum age of images to be considered for deletion
-t, --threshold <THRESHOLD>
Sets the maximum amount of space to be used for Docker images (default: 10 GB)
Expand Down
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use {
clap::{App, AppSettings, Arg},
env_logger::{fmt::Color, Builder},
log::{Level, LevelFilter},
parse_duration::parse,
regex::RegexSet,
std::{
env,
Expand Down Expand Up @@ -37,6 +38,7 @@ const DEFAULT_THRESHOLD: &str = "10 GB";
const DELETION_CHUNK_SIZE_OPTION: &str = "deletion-chunk-size";
const KEEP_OPTION: &str = "keep";
const THRESHOLD_OPTION: &str = "threshold";
const MIN_AGE_OPTION: &str = "min-age";

// Size threshold argument, absolute or relative to filesystem size
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -110,6 +112,7 @@ pub struct Settings {
threshold: Threshold,
keep: Option<RegexSet>,
deletion_chunk_size: usize,
min_age: Option<Duration>,
}

// Set up the logger.
Expand Down Expand Up @@ -197,6 +200,13 @@ fn settings() -> io::Result<Settings> {
(default: {DEFAULT_DELETION_CHUNK_SIZE})",
)),
)
.arg(
Arg::with_name(MIN_AGE_OPTION)
.value_name("MIN AGE")
.short("m")
.long(MIN_AGE_OPTION)
.help("Sets the minimum age of images to be considered for deletion"),
)
.get_matches();

// Read the threshold.
Expand Down Expand Up @@ -225,10 +235,20 @@ fn settings() -> io::Result<Settings> {
None => DEFAULT_DELETION_CHUNK_SIZE,
};

// Determine the minimum age for images to be considered for deletion.
let min_age = match matches.value_of(MIN_AGE_OPTION) {
Some(value) => match parse(value) {
Ok(duration) => Some(duration),
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)),
},
None => None,
};

Ok(Settings {
threshold,
keep,
deletion_chunk_size,
min_age,
})
}

Expand Down
26 changes: 26 additions & 0 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ fn vacuum(
threshold: Byte,
keep: &Option<RegexSet>,
deletion_chunk_size: usize,
min_age: &Option<Duration>,
) -> io::Result<()> {
// Find all images.
let image_records = list_image_records(state)?;
Expand Down Expand Up @@ -670,6 +671,29 @@ fn vacuum(
});
}

// If the `--min-age` argument is provided, we need to filter out images
// which are newer than the provided duration.
if let Some(duration) = min_age {
match (SystemTime::now() - *duration).duration_since(UNIX_EPOCH) {
Ok(time_stamp) => {
sorted_image_nodes.retain(|(image_id, image_node)| {
if image_node.last_used_since_epoch > time_stamp {
debug!(
"Ignored image {} due to the {} flag.",
image_id.code_str(),
"--min-age".code_str(),
);

return false;
}

true
});
}
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)),
};
}

// Check if we're over the threshold.
let mut deleted_image_ids = HashSet::new();
let space = space_usage()?;
Expand Down Expand Up @@ -764,6 +788,7 @@ pub fn run(
threshold,
&settings.keep,
settings.deletion_chunk_size,
&settings.min_age,
)?;
state::save(state)?;
*first_run = false;
Expand Down Expand Up @@ -842,6 +867,7 @@ pub fn run(
threshold,
&settings.keep,
settings.deletion_chunk_size,
&settings.min_age,
)?;
}

Expand Down

0 comments on commit 943f787

Please sign in to comment.