Skip to content

Commit

Permalink
Remove images after container exits
Browse files Browse the repository at this point in the history
  • Loading branch information
msmith-techempower committed Jan 20, 2021
1 parent 754fddb commit a62050e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tfb_toolset"
version = "0.5.2"
version = "0.5.3"
authors = ["Mike Smith", "Nate Brady"]
edition = "2018"

Expand All @@ -22,5 +22,5 @@ regex = "1.4.3"
lazy_static = "1.4.0"
uuid = "0.8.2"
rand = "0.8.2"
dockurl = "0.1.3"
dockurl = "0.1.4"
#dockurl = { path = "../dockurl" }
22 changes: 13 additions & 9 deletions src/benchmarker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a> Benchmarker<'a> {

if mode != CICD {
let use_unix_socket = benchmarker.docker_config.use_unix_socket;
let remove_containers = benchmarker.docker_config.remove_containers;
let docker_cleanup = benchmarker.docker_config.clean_up;
let application_container_id = Arc::clone(&benchmarker.application_container_id);
let database_container_id = Arc::clone(&benchmarker.database_container_id);
let verifier_container_id = Arc::clone(&benchmarker.verifier_container_id);
Expand All @@ -119,22 +119,22 @@ impl<'a> Benchmarker<'a> {
ctrlc_received.store(true, Ordering::Release);
stop_docker_container_future(
use_unix_socket,
remove_containers,
docker_cleanup,
&verifier_container_id,
);
stop_docker_container_future(
use_unix_socket,
remove_containers,
docker_cleanup,
&benchmarker_container_id,
);
stop_docker_container_future(
use_unix_socket,
remove_containers,
docker_cleanup,
&application_container_id,
);
stop_docker_container_future(
use_unix_socket,
remove_containers,
docker_cleanup,
&database_container_id,
);
std::process::exit(0);
Expand Down Expand Up @@ -608,6 +608,10 @@ impl<'a> Benchmarker<'a> {

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

if let Ok(mut application_container_id) = self.application_container_id.lock() {
application_container_id.image_id(&image_id);
}

let container_id = create_container(
&self.docker_config,
&image_id,
Expand Down Expand Up @@ -681,22 +685,22 @@ impl<'a> Benchmarker<'a> {
fn stop_containers(&mut self) {
stop_docker_container_future(
self.docker_config.use_unix_socket,
self.docker_config.remove_containers,
self.docker_config.clean_up,
&self.verifier_container_id,
);
stop_docker_container_future(
self.docker_config.use_unix_socket,
self.docker_config.remove_containers,
self.docker_config.clean_up,
&self.benchmarker_container_id,
);
stop_docker_container_future(
self.docker_config.use_unix_socket,
self.docker_config.remove_containers,
self.docker_config.clean_up,
&self.application_container_id,
);
stop_docker_container_future(
self.docker_config.use_unix_socket,
self.docker_config.remove_containers,
self.docker_config.clean_up,
&self.database_container_id,
);
}
Expand Down
27 changes: 21 additions & 6 deletions src/docker/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use dockurl::container::{
attach_to_container, delete_container, get_container_logs, inspect_container, kill_container,
wait_for_container_to_exit,
};
use dockurl::image::delete_image;
use dockurl::network::NetworkMode;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -371,7 +372,7 @@ pub fn start_benchmark_command_retrieval_container(
BenchmarkCommandListener::new(test_type, logger),
)?;

if docker_config.remove_containers {
if docker_config.clean_up {
delete_container(
&container_id,
&docker_config.client_docker_host,
Expand Down Expand Up @@ -414,7 +415,7 @@ pub fn start_benchmarker_container(
Benchmarker::new(logger),
)?;

if docker_config.remove_containers {
if docker_config.clean_up {
delete_container(
&container_id,
&docker_config.client_docker_host,
Expand Down Expand Up @@ -460,7 +461,7 @@ pub fn start_verification_container(
Verifier::new(project, test, test_type, logger),
)?;

if docker_config.remove_containers {
if docker_config.clean_up {
delete_container(
&container_id,
&docker_config.client_docker_host,
Expand Down Expand Up @@ -494,7 +495,7 @@ pub fn block_until_database_is_ready(
Simple::new(),
)?;

if docker_config.remove_containers {
if docker_config.clean_up {
delete_container(
container_id,
&docker_config.client_docker_host,
Expand All @@ -516,7 +517,7 @@ pub fn block_until_database_is_ready(
/// Note: this function blocks until the given `container` is in a ready state.
pub fn stop_docker_container_future(
use_unix_socket: bool,
remove_containers: bool,
docker_clean_up: bool,
container_id: &Arc<Mutex<DockerContainerIdFuture>>,
) {
let mut requires_wait_to_stop = false;
Expand All @@ -543,7 +544,7 @@ pub fn stop_docker_container_future(
)
.unwrap_or(());

if remove_containers {
if docker_clean_up {
delete_container(
container_id,
&container.docker_host,
Expand All @@ -558,6 +559,20 @@ pub fn stop_docker_container_future(

container.unregister();
}
if let Some(image_id) = &container.image_id {
if docker_clean_up {
delete_image(
image_id,
true,
false,
&container.docker_host,
use_unix_socket,
Simple::new(),
)
.unwrap();
}
}
container.image_id = None;
}
}
}
6 changes: 3 additions & 3 deletions src/docker/docker_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct DockerConfig<'a> {
pub results_environment: &'a str,
pub results_upload_uri: Option<&'a str>,
pub logger: Logger,
pub remove_containers: bool,
pub clean_up: bool,
}
impl<'a> DockerConfig<'a> {
pub fn new(matches: &'a clap::ArgMatches) -> Self {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<'a> DockerConfig<'a> {
None => None,
Some(str) => Some(str),
};
let remove_containers = matches.is_present(options::args::REMOVE_CONTAINERS);
let clean_up = matches.is_present(options::args::DOCKER_CLEANUP);

Self {
use_unix_socket,
Expand All @@ -142,7 +142,7 @@ impl<'a> DockerConfig<'a> {
results_name,
results_environment,
results_upload_uri,
remove_containers,
clean_up,
}
}
}
6 changes: 6 additions & 0 deletions src/docker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ pub struct BenchmarkCommands {
pub struct DockerContainerIdFuture {
requires_wait_to_stop: bool,
container_id: Option<String>,
image_id: Option<String>,
docker_host: String,
}
impl DockerContainerIdFuture {
pub fn new(docker_host: &str) -> Self {
DockerContainerIdFuture {
requires_wait_to_stop: false,
container_id: None,
image_id: None,
docker_host: docker_host.to_string(),
}
}

pub fn image_id(&mut self, image_id: &str) {
self.image_id = Some(image_id.to_string());
}

pub fn register(&mut self, container_id: &str) {
self.requires_wait_to_stop = true;
self.container_id = Some(container_id.to_string());
Expand Down
6 changes: 3 additions & 3 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod args {
pub const QUERY_LEVELS: &str = "Query Levels";
pub const CACHED_QUERY_LEVELS: &str = "Cached Query Levels";
pub const NETWORK_MODE: &str = "Network Mode";
pub const REMOVE_CONTAINERS: &str = "Remove Containers";
pub const DOCKER_CLEANUP: &str = "Auto-Clean Docker Containers and Images";
}

pub mod network_modes {
Expand Down Expand Up @@ -104,8 +104,8 @@ pub fn parse<'app>() -> App<'app> {
.long("parse")
)
.arg(
Arg::new(args::REMOVE_CONTAINERS)
.about("Automatically remove containers after they have exited")
Arg::new(args::DOCKER_CLEANUP)
.about("Automatically remove containers and images after they have exited")
.long("rm")
)
// Test options
Expand Down

0 comments on commit a62050e

Please sign in to comment.