Skip to content

Commit a62050e

Browse files
Remove images after container exits
1 parent 754fddb commit a62050e

File tree

7 files changed

+51
-26
lines changed

7 files changed

+51
-26
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tfb_toolset"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
authors = ["Mike Smith", "Nate Brady"]
55
edition = "2018"
66

@@ -22,5 +22,5 @@ regex = "1.4.3"
2222
lazy_static = "1.4.0"
2323
uuid = "0.8.2"
2424
rand = "0.8.2"
25-
dockurl = "0.1.3"
25+
dockurl = "0.1.4"
2626
#dockurl = { path = "../dockurl" }

src/benchmarker.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a> Benchmarker<'a> {
9595

9696
if mode != CICD {
9797
let use_unix_socket = benchmarker.docker_config.use_unix_socket;
98-
let remove_containers = benchmarker.docker_config.remove_containers;
98+
let docker_cleanup = benchmarker.docker_config.clean_up;
9999
let application_container_id = Arc::clone(&benchmarker.application_container_id);
100100
let database_container_id = Arc::clone(&benchmarker.database_container_id);
101101
let verifier_container_id = Arc::clone(&benchmarker.verifier_container_id);
@@ -119,22 +119,22 @@ impl<'a> Benchmarker<'a> {
119119
ctrlc_received.store(true, Ordering::Release);
120120
stop_docker_container_future(
121121
use_unix_socket,
122-
remove_containers,
122+
docker_cleanup,
123123
&verifier_container_id,
124124
);
125125
stop_docker_container_future(
126126
use_unix_socket,
127-
remove_containers,
127+
docker_cleanup,
128128
&benchmarker_container_id,
129129
);
130130
stop_docker_container_future(
131131
use_unix_socket,
132-
remove_containers,
132+
docker_cleanup,
133133
&application_container_id,
134134
);
135135
stop_docker_container_future(
136136
use_unix_socket,
137-
remove_containers,
137+
docker_cleanup,
138138
&database_container_id,
139139
);
140140
std::process::exit(0);
@@ -608,6 +608,10 @@ impl<'a> Benchmarker<'a> {
608608

609609
let image_id = build_image(&self.docker_config, project, test, logger)?;
610610

611+
if let Ok(mut application_container_id) = self.application_container_id.lock() {
612+
application_container_id.image_id(&image_id);
613+
}
614+
611615
let container_id = create_container(
612616
&self.docker_config,
613617
&image_id,
@@ -681,22 +685,22 @@ impl<'a> Benchmarker<'a> {
681685
fn stop_containers(&mut self) {
682686
stop_docker_container_future(
683687
self.docker_config.use_unix_socket,
684-
self.docker_config.remove_containers,
688+
self.docker_config.clean_up,
685689
&self.verifier_container_id,
686690
);
687691
stop_docker_container_future(
688692
self.docker_config.use_unix_socket,
689-
self.docker_config.remove_containers,
693+
self.docker_config.clean_up,
690694
&self.benchmarker_container_id,
691695
);
692696
stop_docker_container_future(
693697
self.docker_config.use_unix_socket,
694-
self.docker_config.remove_containers,
698+
self.docker_config.clean_up,
695699
&self.application_container_id,
696700
);
697701
stop_docker_container_future(
698702
self.docker_config.use_unix_socket,
699-
self.docker_config.remove_containers,
703+
self.docker_config.clean_up,
700704
&self.database_container_id,
701705
);
702706
}

src/docker/container.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use dockurl::container::{
2424
attach_to_container, delete_container, get_container_logs, inspect_container, kill_container,
2525
wait_for_container_to_exit,
2626
};
27+
use dockurl::image::delete_image;
2728
use dockurl::network::NetworkMode;
2829
use std::collections::HashMap;
2930
use std::sync::{Arc, Mutex};
@@ -371,7 +372,7 @@ pub fn start_benchmark_command_retrieval_container(
371372
BenchmarkCommandListener::new(test_type, logger),
372373
)?;
373374

374-
if docker_config.remove_containers {
375+
if docker_config.clean_up {
375376
delete_container(
376377
&container_id,
377378
&docker_config.client_docker_host,
@@ -414,7 +415,7 @@ pub fn start_benchmarker_container(
414415
Benchmarker::new(logger),
415416
)?;
416417

417-
if docker_config.remove_containers {
418+
if docker_config.clean_up {
418419
delete_container(
419420
&container_id,
420421
&docker_config.client_docker_host,
@@ -460,7 +461,7 @@ pub fn start_verification_container(
460461
Verifier::new(project, test, test_type, logger),
461462
)?;
462463

463-
if docker_config.remove_containers {
464+
if docker_config.clean_up {
464465
delete_container(
465466
&container_id,
466467
&docker_config.client_docker_host,
@@ -494,7 +495,7 @@ pub fn block_until_database_is_ready(
494495
Simple::new(),
495496
)?;
496497

497-
if docker_config.remove_containers {
498+
if docker_config.clean_up {
498499
delete_container(
499500
container_id,
500501
&docker_config.client_docker_host,
@@ -516,7 +517,7 @@ pub fn block_until_database_is_ready(
516517
/// Note: this function blocks until the given `container` is in a ready state.
517518
pub fn stop_docker_container_future(
518519
use_unix_socket: bool,
519-
remove_containers: bool,
520+
docker_clean_up: bool,
520521
container_id: &Arc<Mutex<DockerContainerIdFuture>>,
521522
) {
522523
let mut requires_wait_to_stop = false;
@@ -543,7 +544,7 @@ pub fn stop_docker_container_future(
543544
)
544545
.unwrap_or(());
545546

546-
if remove_containers {
547+
if docker_clean_up {
547548
delete_container(
548549
container_id,
549550
&container.docker_host,
@@ -558,6 +559,20 @@ pub fn stop_docker_container_future(
558559

559560
container.unregister();
560561
}
562+
if let Some(image_id) = &container.image_id {
563+
if docker_clean_up {
564+
delete_image(
565+
image_id,
566+
true,
567+
false,
568+
&container.docker_host,
569+
use_unix_socket,
570+
Simple::new(),
571+
)
572+
.unwrap();
573+
}
574+
}
575+
container.image_id = None;
561576
}
562577
}
563578
}

src/docker/docker_config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct DockerConfig<'a> {
2626
pub results_environment: &'a str,
2727
pub results_upload_uri: Option<&'a str>,
2828
pub logger: Logger,
29-
pub remove_containers: bool,
29+
pub clean_up: bool,
3030
}
3131
impl<'a> DockerConfig<'a> {
3232
pub fn new(matches: &'a clap::ArgMatches) -> Self {
@@ -119,7 +119,7 @@ impl<'a> DockerConfig<'a> {
119119
None => None,
120120
Some(str) => Some(str),
121121
};
122-
let remove_containers = matches.is_present(options::args::REMOVE_CONTAINERS);
122+
let clean_up = matches.is_present(options::args::DOCKER_CLEANUP);
123123

124124
Self {
125125
use_unix_socket,
@@ -142,7 +142,7 @@ impl<'a> DockerConfig<'a> {
142142
results_name,
143143
results_environment,
144144
results_upload_uri,
145-
remove_containers,
145+
clean_up,
146146
}
147147
}
148148
}

src/docker/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,23 @@ pub struct BenchmarkCommands {
4444
pub struct DockerContainerIdFuture {
4545
requires_wait_to_stop: bool,
4646
container_id: Option<String>,
47+
image_id: Option<String>,
4748
docker_host: String,
4849
}
4950
impl DockerContainerIdFuture {
5051
pub fn new(docker_host: &str) -> Self {
5152
DockerContainerIdFuture {
5253
requires_wait_to_stop: false,
5354
container_id: None,
55+
image_id: None,
5456
docker_host: docker_host.to_string(),
5557
}
5658
}
5759

60+
pub fn image_id(&mut self, image_id: &str) {
61+
self.image_id = Some(image_id.to_string());
62+
}
63+
5864
pub fn register(&mut self, container_id: &str) {
5965
self.requires_wait_to_stop = true;
6066
self.container_id = Some(container_id.to_string());

src/options.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub mod args {
3737
pub const QUERY_LEVELS: &str = "Query Levels";
3838
pub const CACHED_QUERY_LEVELS: &str = "Cached Query Levels";
3939
pub const NETWORK_MODE: &str = "Network Mode";
40-
pub const REMOVE_CONTAINERS: &str = "Remove Containers";
40+
pub const DOCKER_CLEANUP: &str = "Auto-Clean Docker Containers and Images";
4141
}
4242

4343
pub mod network_modes {
@@ -104,8 +104,8 @@ pub fn parse<'app>() -> App<'app> {
104104
.long("parse")
105105
)
106106
.arg(
107-
Arg::new(args::REMOVE_CONTAINERS)
108-
.about("Automatically remove containers after they have exited")
107+
Arg::new(args::DOCKER_CLEANUP)
108+
.about("Automatically remove containers and images after they have exited")
109109
.long("rm")
110110
)
111111
// Test options

0 commit comments

Comments
 (0)