1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::{collections::HashMap, env::var, io::Read};
use Docker;
use Image;

#[derive(Debug)]
pub struct Container<'d, D, I>
where
    D: 'd,
    D: Docker,
    I: Image,
{
    id: String,
    docker_client: &'d D,
    image: I,
}

impl<'d, D, I> Container<'d, D, I>
where
    D: Docker,
    I: Image,
{
    pub fn new(id: String, docker_client: &'d D, image: I) -> Self {
        Container {
            id,
            docker_client,
            image,
        }
    }

    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn logs(&self) -> Logs {
        self.docker_client.logs(&self.id)
    }

    pub fn get_host_port(&self, internal_port: u32) -> Option<u32> {
        let resolved_port = self
            .docker_client
            .ports(&self.id)
            .map_to_external_port(internal_port);

        match resolved_port {
            Some(port) => {
                debug!(
                    "Resolved port {} to {} for container {}",
                    internal_port, port, self.id
                );
            }
            None => {
                warn!(
                    "Unable to resolve port {} for container {}",
                    internal_port, self.id
                );
            }
        }

        resolved_port
    }

    pub fn block_until_ready(&self) {
        debug!("Waiting for container {} to be ready", self.id);

        self.image.wait_until_ready(self);

        debug!("Container {} is now ready!", self.id);
    }

    pub fn image(&self) -> &I {
        &self.image
    }

    pub fn stop(&self) {
        debug!("Stopping docker container {}", self.id);

        self.docker_client.stop(&self.id)
    }

    pub fn rm(&self) {
        debug!("Deleting docker container {}", self.id);

        self.docker_client.rm(&self.id)
    }
}

impl<'d, D, I> Drop for Container<'d, D, I>
where
    D: Docker,
    I: Image,
{
    fn drop(&mut self) {
        let keep_container = var("KEEP_CONTAINERS")
            .ok()
            .and_then(|var| var.parse().ok())
            .unwrap_or(false);

        match keep_container {
            true => self.stop(),
            false => self.rm(),
        }
    }
}

#[derive(Debug, PartialEq, Default)]
pub struct Ports {
    mapping: HashMap<u32, u32>,
}

impl Ports {
    pub fn add_mapping(&mut self, internal: u32, external: u32) -> &mut Self {
        debug!("Registering port mapping: {} -> {}", internal, external);

        self.mapping.insert(internal, external);

        self
    }

    pub fn map_to_external_port(&self, port: u32) -> Option<u32> {
        self.mapping.get(&port).map(|p| p.clone())
    }
}

#[derive(DebugStub)]
pub struct Logs {
    #[debug_stub = "stream"]
    pub stdout: Box<Read>,
    #[debug_stub = "stream"]
    pub stderr: Box<Read>,
}