Skip to content

Commit b527f50

Browse files
committed
Add keep_container option for debugging
1 parent 4fdd1a2 commit b527f50

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export DOCKER_CONTAINER_CAP_DROP="MKNOD NET_RAW NET_BIND_SERVICE"
2323
export RUN_MAX_EXECUTION_TIME="10"
2424
export RUN_MAX_OUTPUT_SIZE="100000"
2525

26+
export DEBUG_KEEP_CONTAINER="false"
27+
2628
export RUST_LOG=debug
2729

2830
cargo run

src/docker_run/api/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn handle(config: &config::Config, request: &mut tiny_http::Request) -> Resu
2222
container_config,
2323
payload: req_body.payload,
2424
limits: config.run.clone(),
25-
}).map_err(handle_error)?;
25+
}, config.debug.clone()).map_err(handle_error)?;
2626

2727
api::prepare_json_response(&run_result, api::JsonFormat::Minimal)
2828
}

src/docker_run/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::docker_run::unix_stream;
22
use crate::docker_run::run;
33
use crate::docker_run::api;
4+
use crate::docker_run::debug;
45

56
#[derive(Clone, Debug)]
67
pub struct Config {
@@ -9,6 +10,7 @@ pub struct Config {
910
pub unix_socket: unix_stream::Config,
1011
pub container: run::ContainerConfig,
1112
pub run: run::Limits,
13+
pub debug: debug::Config,
1214
}
1315

1416

src/docker_run/debug.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[derive(Clone, Debug)]
2+
pub struct Config {
3+
pub keep_container: bool,
4+
}

src/docker_run/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod config;
55
pub mod environment;
66
pub mod api;
77
pub mod unix_stream;
8+
pub mod debug;

src/docker_run/run.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::net;
88

99
use crate::docker_run::docker;
1010
use crate::docker_run::unix_stream;
11+
use crate::docker_run::debug;
1112

1213

1314
#[derive(Debug)]
@@ -26,7 +27,7 @@ pub struct Limits {
2627

2728

2829

29-
pub fn run<T: Serialize>(stream_config: unix_stream::Config, run_request: RunRequest<T>) -> Result<Map<String, Value>, Error> {
30+
pub fn run<T: Serialize>(stream_config: unix_stream::Config, run_request: RunRequest<T>, debug: debug::Config) -> Result<Map<String, Value>, Error> {
3031
let container_response = unix_stream::with_stream(&stream_config, Error::UnixStream, |stream| {
3132
docker::create_container(stream, &run_request.container_config)
3233
.map_err(Error::CreateContainer)
@@ -36,17 +37,19 @@ pub fn run<T: Serialize>(stream_config: unix_stream::Config, run_request: RunReq
3637

3738
let result = run_with_container(&stream_config, run_request, &container_id);
3839

39-
let _ = unix_stream::with_stream(&stream_config, Error::UnixStream, |stream| {
40-
match docker::remove_container(stream, &container_id) {
41-
Ok(_) => {}
40+
if !debug.keep_container {
41+
let _ = unix_stream::with_stream(&stream_config, Error::UnixStream, |stream| {
42+
match docker::remove_container(stream, &container_id) {
43+
Ok(_) => {}
4244

43-
Err(err) => {
44-
log::error!("Failed to remove container: {}", err);
45+
Err(err) => {
46+
log::error!("Failed to remove container: {}", err);
47+
}
4548
}
46-
}
4749

48-
Ok(())
49-
});
50+
Ok(())
51+
});
52+
}
5053

5154
result
5255
}

src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use docker_run::environment;
1313
use docker_run::unix_stream;
1414
use docker_run::run;
1515
use docker_run::api;
16+
use docker_run::debug;
1617

1718

1819
fn main() {
@@ -148,13 +149,15 @@ fn build_config(env: &environment::Environment) -> Result<config::Config, enviro
148149
let unix_socket = build_unix_socket_config(env)?;
149150
let container = build_container_config(env)?;
150151
let run = build_run_config(env)?;
152+
let debug = build_debug_config(env)?;
151153

152154
Ok(config::Config{
153155
server,
154156
api,
155157
unix_socket,
156158
container,
157159
run,
160+
debug,
158161
})
159162
}
160163

@@ -225,3 +228,11 @@ fn build_run_config(env: &environment::Environment) -> Result<run::Limits, envir
225228
max_output_size,
226229
})
227230
}
231+
232+
fn build_debug_config(env: &environment::Environment) -> Result<debug::Config, environment::Error> {
233+
let keep_container = environment::lookup(env, "DEBUG_KEEP_CONTAINER").unwrap_or(false);
234+
235+
Ok(debug::Config{
236+
keep_container,
237+
})
238+
}

0 commit comments

Comments
 (0)