Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow running the script one time #290

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ OPTIONS:
-t, --threshold <THRESHOLD>
Sets the maximum amount of space to be used for Docker images (default: 10 GB)

-s, --single_run <SINGLE_RUN>
Exits after first cleanup and doesn't wait for any more docker events (default: false)

-v, --version
Prints version information
```
Expand Down
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
const DEFAULT_DELETION_CHUNK_SIZE: usize = 1;
const DEFAULT_LOG_LEVEL: LevelFilter = LevelFilter::Debug;
const DEFAULT_THRESHOLD: &str = "10 GB";
const DEFAULT_SINGLE_RUN: bool = false;

// Command-line argument and option names
const DELETION_CHUNK_SIZE_OPTION: &str = "deletion-chunk-size";
const KEEP_OPTION: &str = "keep";
const THRESHOLD_OPTION: &str = "threshold";
const SINGLE_RUN: &str = "single-run";

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

// Set up the logger.
Expand Down Expand Up @@ -197,6 +200,18 @@ fn settings() -> io::Result<Settings> {
(default: {DEFAULT_DELETION_CHUNK_SIZE})",
)),
)
.arg(
Arg::with_name(SINGLE_RUN)
.value_name("SINGLE RUN")
.short("s")
.long(SINGLE_RUN)
.number_of_values(1)
.takes_value(false)
.help(&format!(
"Exits after first cleanup and doesn't wait for any more docker events \
(default: {DEFAULT_SINGLE_RUN})",
)),
)
.get_matches();

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

// Determine if we should do a single run or a loop.
let single_run = matches.is_present(SINGLE_RUN);

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

Expand Down
10 changes: 7 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
io::{self, BufRead, BufReader},
mem::drop,
ops::Deref,
process::{Command, Stdio},
process::{exit, Command, Stdio},
time::{Duration, SystemTime, UNIX_EPOCH},
},
};
Expand Down Expand Up @@ -655,8 +655,7 @@ fn vacuum(
for repository_tag in &image_node.image_record.repository_tags {
if regex_set.is_match(&format!(
"{}:{}",
repository_tag.repository,
repository_tag.tag,
repository_tag.repository, repository_tag.tag,
)) {
debug!(
"Ignored image {} due to the {} flag.",
Expand Down Expand Up @@ -763,6 +762,11 @@ pub fn run(settings: &Settings, state: &mut State, first_run: &mut bool) -> io::
state::save(state)?;
*first_run = false;

if settings.single_run {
info!("Single run. Exiting \u{2026}");
exit(0);
}

// Spawn `docker events --format '{{json .}}'`.
let mut child = guard(
Command::new("docker")
Expand Down
Loading