|
| 1 | +use std::io; |
| 2 | +use std::io::prelude::*; |
| 3 | +use std::mem; |
| 4 | +use std::process::{Output, Command, Stdio, ExitStatus}; |
| 5 | +use std::sync::{Arc, Mutex}; |
| 6 | +use std::thread; |
| 7 | +use std::time::Duration; |
| 8 | + |
| 9 | +use wait_timeout::ChildExt; |
| 10 | + |
| 11 | +pub struct Container { |
| 12 | + id: String, |
| 13 | +} |
| 14 | + |
| 15 | +impl Container { |
| 16 | + pub fn new(cmd: &str, |
| 17 | + args: &[String], |
| 18 | + name: &str) -> io::Result<Container> { |
| 19 | + let out = try!(run(Command::new("docker") |
| 20 | + .arg("create") |
| 21 | + .arg("--cap-drop=ALL") |
| 22 | + .arg("--memory=128m") |
| 23 | + .arg("--net=none") |
| 24 | + .arg("--pids-limit=5") |
| 25 | + .arg("--security-opt=no-new-privileges") |
| 26 | + .arg("--interactive") |
| 27 | + .arg(name) |
| 28 | + .arg(cmd) |
| 29 | + .stderr(Stdio::inherit()) |
| 30 | + .args(args))); |
| 31 | + let stdout = String::from_utf8_lossy(&out.stdout); |
| 32 | + Ok(Container { |
| 33 | + id: stdout.trim().to_string(), |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + pub fn run(&self, |
| 38 | + input: &[u8], |
| 39 | + timeout: Duration) |
| 40 | + -> io::Result<(ExitStatus, Vec<u8>, bool)> { |
| 41 | + let mut cmd = Command::new("docker"); |
| 42 | + cmd.arg("start") |
| 43 | + .arg("--attach") |
| 44 | + .arg("--interactive") |
| 45 | + .arg(&self.id) |
| 46 | + .stdin(Stdio::piped()) |
| 47 | + .stdout(Stdio::piped()) |
| 48 | + .stderr(Stdio::piped()); |
| 49 | + debug!("attaching with {:?}", cmd); |
| 50 | + let mut cmd = try!(cmd.spawn()); |
| 51 | + try!(cmd.stdin.take().unwrap().write_all(input)); |
| 52 | + debug!("input written, now waiting"); |
| 53 | + |
| 54 | + let mut stdout = cmd.stdout.take().unwrap(); |
| 55 | + let mut stderr = cmd.stderr.take().unwrap(); |
| 56 | + let sink = Arc::new(Mutex::new(Vec::new())); |
| 57 | + let sink2 = sink.clone(); |
| 58 | + let stdout = thread::spawn(move || append(&sink2, &mut stdout)); |
| 59 | + let sink2 = sink.clone(); |
| 60 | + let stderr = thread::spawn(move || append(&sink2, &mut stderr)); |
| 61 | + |
| 62 | + let (status, timeout) = match try!(cmd.wait_timeout(timeout)) { |
| 63 | + Some(status) => { |
| 64 | + debug!("finished before timeout"); |
| 65 | + // TODO: document this |
| 66 | + (unsafe { mem::transmute(status) }, false) |
| 67 | + } |
| 68 | + None => { |
| 69 | + debug!("timeout, going to kill"); |
| 70 | + try!(run(Command::new("docker").arg("kill").arg(&self.id))); |
| 71 | + (try!(cmd.wait()), true) |
| 72 | + } |
| 73 | + }; |
| 74 | + stdout.join().unwrap(); |
| 75 | + stderr.join().unwrap(); |
| 76 | + let mut lock = sink.lock().unwrap(); |
| 77 | + let output = mem::replace(&mut *lock, Vec::new()); |
| 78 | + debug!("status: {}", status); |
| 79 | + debug!("output: {}", String::from_utf8_lossy(&output)); |
| 80 | + Ok((status, output, timeout)) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +fn append(into: &Mutex<Vec<u8>>, from: &mut Read) { |
| 85 | + let mut buf = [0; 1024]; |
| 86 | + while let Ok(amt) = from.read(&mut buf) { |
| 87 | + if amt == 0 { |
| 88 | + break |
| 89 | + } |
| 90 | + into.lock().unwrap().extend_from_slice(&buf[..amt]); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Drop for Container { |
| 95 | + fn drop(&mut self) { |
| 96 | + run(Command::new("docker") |
| 97 | + .arg("rm") |
| 98 | + .arg("--force") |
| 99 | + .arg(&self.id)).unwrap(); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn run(cmd: &mut Command) -> io::Result<Output> { |
| 104 | + debug!("spawning: {:?}", cmd); |
| 105 | + let out = try!(cmd.output()); |
| 106 | + debug!("output: {:?}", out); |
| 107 | + if !out.status.success() { |
| 108 | + let msg = format!("process failed: {:?}\n{:?}", cmd, out); |
| 109 | + return Err(io::Error::new(io::ErrorKind::Other, msg)) |
| 110 | + } |
| 111 | + Ok(out) |
| 112 | +} |
0 commit comments