Commit 972f524d authored by MrMan's avatar MrMan

Working docker container start & stop

parent 5edf964d
Pipeline #47784682 passed with stage
in 10 minutes and 36 seconds
......@@ -13,7 +13,7 @@ use std::net::{TcpListener, Ipv4Addr};
const TMP_PORT_RANGE_START: u16 = 8000;
const TMP_PORT_RANGE_END: u16 = 9000;
const POSTMGR_DOCKER_IMAGE_NAME: &'static str = "postmgr";
const POSTMGR_TEST_IMAGE_NAME: &'static str = "postmgr-test";
const DOCKER_BINARY: &'static str = "docker";
fn get_available_port() -> Option<u16> {
......@@ -35,10 +35,10 @@ fn port_is_available(port: u16) -> bool {
pub struct DockerizedPostmgr {
id: String,
container_name: String,
smtp_port: u16,
submission_port: u16,
host: Ipv4Addr,
process: ChildProcess,
}
impl DockerizedPostmgr {
......@@ -48,13 +48,14 @@ impl DockerizedPostmgr {
let submission_port = get_available_port().unwrap();
let id = thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.take(5)
.collect::<String>();
// Create the docker child process
let smtp_port_assignment = format!("{}:25", smtp_port.to_string());
let submission_port_assignment = format!("{}:587", submission_port.to_string());
let mut process = Command::new(DOCKER_BINARY)
let container_name = format!("postmgr-test-{}", &id);
let process = Command::new(DOCKER_BINARY)
.arg("run")
.arg("--detach")
.arg("-p")
......@@ -62,32 +63,50 @@ impl DockerizedPostmgr {
.arg("-p")
.arg(submission_port_assignment)
.arg("--name")
.arg(format!("postmgr-test-{}", &id))
.arg(POSTMGR_DOCKER_IMAGE_NAME)
.arg(&container_name)
.arg(POSTMGR_TEST_IMAGE_NAME)
.stdin(Stdio::null())
.stdout(Stdio::null()) // NOTE: comment this out to get docker run output
.spawn()
.expect("failed to start docker run command");
let ecode = process.wait().expect("docker run command failed");
if !ecode.success() {
panic!("docker run returned a non-success exit code");
}
let output = process
.wait_with_output()
.expect("docker run command failed");
if !output.status.success() { panic!("docker run returned a non-success exit code"); }
// TODO: process is exiting already
DockerizedPostmgr{
id,
container_name,
smtp_port,
submission_port,
host,
process
}
}
}
impl Drop for DockerizedPostmgr {
fn drop(&mut self) {
// Kill the child process
self.process.kill().unwrap();
println!("dropping postmgr w/ id [{}]", self.id);
// TODO: Kill the child process docker container
// Stop the process
Command::new(DOCKER_BINARY)
.arg("stop")
.arg(&self.container_name)
.stdin(Stdio::null())
.stdout(Stdio::null()) // NOTE: comment this out to get docker stop output
.output()
.expect("failed to start docker stop command");
// Kill the process
Command::new(DOCKER_BINARY)
.arg("kill")
.arg(&self.container_name)
.stdin(Stdio::null())
.stdout(Stdio::null()) // NOTE: comment this out to get docker kill output
.output()
.expect("failed to start docker kill command");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment