Skip to content

Commit eb49ff1

Browse files
Impl the -rm flag to autoclear containers
1 parent 617b6d0 commit eb49ff1

File tree

6 files changed

+106
-15
lines changed

6 files changed

+106
-15
lines changed

Cargo.lock

Lines changed: 0 additions & 2 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
@@ -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.2"
26-
#dockurl = { path = "../dockurl" }
25+
#dockurl = "0.1.2"
26+
dockurl = { path = "../dockurl" }

src/benchmarker.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +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;
9899
let application_container_id = Arc::clone(&benchmarker.application_container_id);
99100
let database_container_id = Arc::clone(&benchmarker.database_container_id);
100101
let verifier_container_id = Arc::clone(&benchmarker.verifier_container_id);
@@ -116,10 +117,26 @@ impl<'a> Benchmarker<'a> {
116117
let ctrlc_received = Arc::clone(&ctrlc_received);
117118
thread::spawn(move || {
118119
ctrlc_received.store(true, Ordering::Release);
119-
stop_docker_container_future(use_unix_socket, &verifier_container_id);
120-
stop_docker_container_future(use_unix_socket, &benchmarker_container_id);
121-
stop_docker_container_future(use_unix_socket, &application_container_id);
122-
stop_docker_container_future(use_unix_socket, &database_container_id);
120+
stop_docker_container_future(
121+
use_unix_socket,
122+
remove_containers,
123+
&verifier_container_id,
124+
);
125+
stop_docker_container_future(
126+
use_unix_socket,
127+
remove_containers,
128+
&benchmarker_container_id,
129+
);
130+
stop_docker_container_future(
131+
use_unix_socket,
132+
remove_containers,
133+
&application_container_id,
134+
);
135+
stop_docker_container_future(
136+
use_unix_socket,
137+
remove_containers,
138+
&database_container_id,
139+
);
123140
std::process::exit(0);
124141
});
125142
}
@@ -664,18 +681,22 @@ impl<'a> Benchmarker<'a> {
664681
fn stop_containers(&mut self) {
665682
stop_docker_container_future(
666683
self.docker_config.use_unix_socket,
684+
self.docker_config.remove_containers,
667685
&self.verifier_container_id,
668686
);
669687
stop_docker_container_future(
670688
self.docker_config.use_unix_socket,
689+
self.docker_config.remove_containers,
671690
&self.benchmarker_container_id,
672691
);
673692
stop_docker_container_future(
674693
self.docker_config.use_unix_socket,
694+
self.docker_config.remove_containers,
675695
&self.application_container_id,
676696
);
677697
stop_docker_container_future(
678698
self.docker_config.use_unix_socket,
699+
self.docker_config.remove_containers,
679700
&self.database_container_id,
680701
);
681702
}

src/docker/container.rs

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use dockurl::container::create::networking_config::{
2121
};
2222
use dockurl::container::create::options::Options;
2323
use dockurl::container::{
24-
attach_to_container, get_container_logs, inspect_container, kill_container,
24+
attach_to_container, delete_container, get_container_logs, inspect_container, kill_container,
2525
wait_for_container_to_exit,
2626
};
2727
use dockurl::network::NetworkMode;
@@ -370,6 +370,18 @@ pub fn start_benchmark_command_retrieval_container(
370370
docker_config.use_unix_socket,
371371
BenchmarkCommandListener::new(test_type, logger),
372372
)?;
373+
374+
if docker_config.remove_containers {
375+
delete_container(
376+
&container_id,
377+
&docker_config.client_docker_host,
378+
docker_config.use_unix_socket,
379+
Simple::new(),
380+
true,
381+
true,
382+
false,
383+
)?;
384+
}
373385
if let Some(commands) = listener.benchmark_commands {
374386
Ok(commands)
375387
} else {
@@ -402,6 +414,18 @@ pub fn start_benchmarker_container(
402414
Benchmarker::new(logger),
403415
)?;
404416

417+
if docker_config.remove_containers {
418+
delete_container(
419+
&container_id,
420+
&docker_config.client_docker_host,
421+
docker_config.use_unix_socket,
422+
Simple::new(),
423+
true,
424+
true,
425+
false,
426+
)?;
427+
}
428+
405429
benchmarker.parse_wrk_output()
406430
}
407431

@@ -436,6 +460,18 @@ pub fn start_verification_container(
436460
Verifier::new(project, test, test_type, logger),
437461
)?;
438462

463+
if docker_config.remove_containers {
464+
delete_container(
465+
&container_id,
466+
&docker_config.client_docker_host,
467+
docker_config.use_unix_socket,
468+
Simple::new(),
469+
true,
470+
true,
471+
false,
472+
)?;
473+
}
474+
439475
Ok(verification.verification)
440476
}
441477

@@ -445,19 +481,31 @@ pub fn block_until_database_is_ready(
445481
container_id: &str,
446482
) -> ToolsetResult<()> {
447483
dockurl::container::start_container(
448-
&container_id,
484+
container_id,
449485
&docker_config.client_docker_host,
450486
docker_config.use_unix_socket,
451487
Simple::new(),
452488
)?;
453489

454490
wait_for_container_to_exit(
455-
&container_id,
491+
container_id,
456492
&docker_config.client_docker_host,
457493
docker_config.use_unix_socket,
458494
Simple::new(),
459495
)?;
460496

497+
if docker_config.remove_containers {
498+
delete_container(
499+
container_id,
500+
&docker_config.client_docker_host,
501+
docker_config.use_unix_socket,
502+
Simple::new(),
503+
true,
504+
true,
505+
false,
506+
)?;
507+
}
508+
461509
Ok(())
462510
}
463511

@@ -468,23 +516,24 @@ pub fn block_until_database_is_ready(
468516
/// Note: this function blocks until the given `container` is in a ready state.
469517
pub fn stop_docker_container_future(
470518
use_unix_socket: bool,
471-
container: &Arc<Mutex<DockerContainerIdFuture>>,
519+
remove_containers: bool,
520+
container_id: &Arc<Mutex<DockerContainerIdFuture>>,
472521
) {
473522
let mut requires_wait_to_stop = false;
474-
if let Ok(container) = container.lock() {
523+
if let Ok(container) = container_id.lock() {
475524
requires_wait_to_stop = container.requires_wait_to_stop;
476525
}
477526
if requires_wait_to_stop {
478527
let mut poll = Poll::Pending;
479528
while poll == Poll::Pending {
480-
if let Ok(container) = container.lock() {
529+
if let Ok(container) = container_id.lock() {
481530
poll = container.poll();
482531
if poll == Poll::Pending {
483532
thread::sleep(Duration::from_secs(1));
484533
}
485534
}
486535
}
487-
if let Ok(mut container) = container.lock() {
536+
if let Ok(mut container) = container_id.lock() {
488537
if let Some(container_id) = &container.container_id {
489538
kill_container(
490539
container_id,
@@ -493,6 +542,20 @@ pub fn stop_docker_container_future(
493542
Simple::new(),
494543
)
495544
.unwrap_or(());
545+
546+
if remove_containers {
547+
delete_container(
548+
container_id,
549+
&container.docker_host,
550+
use_unix_socket,
551+
Simple::new(),
552+
true,
553+
true,
554+
false,
555+
)
556+
.unwrap_or(());
557+
}
558+
496559
container.unregister();
497560
}
498561
}

src/docker/docker_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct DockerConfig<'a> {
2525
pub results_environment: &'a str,
2626
pub results_upload_uri: Option<&'a str>,
2727
pub logger: Logger,
28+
pub remove_containers: bool,
2829
}
2930
impl<'a> DockerConfig<'a> {
3031
pub fn new(matches: &'a clap::ArgMatches) -> Self {
@@ -113,6 +114,7 @@ impl<'a> DockerConfig<'a> {
113114
None => None,
114115
Some(str) => Some(str),
115116
};
117+
let remove_containers = matches.is_present(options::args::REMOVE_CONTAINERS);
116118

117119
Self {
118120
use_unix_socket,
@@ -135,6 +137,7 @@ impl<'a> DockerConfig<'a> {
135137
results_name,
136138
results_environment,
137139
results_upload_uri,
140+
remove_containers,
138141
}
139142
}
140143
}

src/options.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +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";
4041
}
4142

4243
pub mod network_modes {
@@ -102,6 +103,11 @@ pub fn parse<'app>() -> App<'app> {
102103
.about("Parses the results of the given timestamp and merges that with the latest results")
103104
.long("parse")
104105
)
106+
.arg(
107+
Arg::new(args::REMOVE_CONTAINERS)
108+
.about("Automatically remove containers after they have exited")
109+
.long("rm")
110+
)
105111
// Test options
106112
.arg(
107113
Arg::new(args::TEST_NAMES)

0 commit comments

Comments
 (0)