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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! # Wharf ⚓🦀
//!
//! ## Example
//! ```ignore
//! use failure::Error;
//! use wharf::Docker;
//! use wharf::opts::{ContainerBuilderOpts, ListContainersOpts};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Error> {
//!     // create docker api instance
//!     let d = Docker::new("http://0.0.0.0:2376")?;
//!     // get containers api handle from d
//!     let containers = d.containers();
//!     // Create instance of query options
//!     let mut opts = ListContainersOpts::new();
//!     opts.all(true);
//!     // iterate over containers
//!     for container in containers.list(opts).await? {
//!         // manipulate container
//!         container.start().await?;
//!         container.stop().await?;
//!     }
//!     // Create a container
//!     let mut container_opts = ContainerBuilderOpts::new();
//!     container_opts
//! 	.image("ubuntu")
//! 	.cmd(&["/bin/echo".into(), "hello".into()])
//! 	.env(&["HTTPS_PROXY=proxy.domain.com:1337"]);
//!
//!     containers.create("jimmy-falcon", &container_opts).await?;
//!
//!     Ok(())
//! }
//! ```

#![allow(non_snake_case)]
#[macro_use]
extern crate failure;
#[macro_use]
pub mod api;
pub mod opts;
pub mod result;
use crate::api::*;
use crate::opts::*;
use crate::result::ExecInspect;
use failure::Error;
use http::header::HeaderValue;
use http::uri::PathAndQuery;
use hyper::{body::to_bytes, client::HttpConnector, Body, Method, Request, Response, Uri};
use log::*;
use serde::{Deserialize, Serialize};
use std::str;
use std::str::FromStr;

/// The main interface to interact with an instance of Docker.
#[derive(Debug)]
pub struct Docker {
    client: hyper::Client<HttpConnector>,
    url: Uri,
}

impl Docker {
    /// Creates a new instance of docker interface.  
    /// May return an error in case of a bad url.
    pub fn new(url: &str) -> Result<Self, Error> {
        Ok(Docker {
            url: url.parse()?,
            client: hyper::Client::new(),
        })
    }
    /// Get reference to a specific container interface
    pub fn container(&self, id: &str) -> Container {
        Container::new(&self, id)
    }
    /// Get reference to api interface of containers
    pub fn containers(&self) -> Containers {
        Containers::new(&self)
    }
    /// Get reference to api interface of images
    pub fn images(&self) -> Images {
        Images::new(&self)
    }
    /// Get reference to api interface of networks
    pub fn networks(&self) -> Networks {
        Networks::new(&self)
    }
    async fn req(
        &self,
        method: Method,
        path: String,
        query: Option<String>,
        body: Body,
        headers: Option<Vec<(&'static str, String)>>,
    ) -> Result<Response<Body>, Error> {
        let mut uri = self.url.clone().into_parts();
        match query {
            Some(q) => {
                uri.path_and_query = Some(PathAndQuery::from_str(&format!("{}?{}", path, q))?)
            }
            None => uri.path_and_query = Some(PathAndQuery::from_str(&path)?),
        }
        let uri = Uri::from_parts(uri)?;
        let mut req = Request::builder().method(method).uri(uri);
        if let Some(req_h) = req.headers_mut() {
            if let Some(h) = headers {
                h.iter().for_each(|header| {
                    req_h.insert(header.0, HeaderValue::from_str(&header.1).unwrap());
                });
            }
        }
        let req = req.body(body).expect("failed to build a request");

        trace!("{:?}", req);
        let res = self.client.request(req).await?;

        trace!("{:?}", res);
        Ok(res)
    }
    /// Get auth token for authorized operations  
    /// Returns a base64 encoded json with user data.
    pub async fn authenticate(&self, opts: &AuthOpts) -> Result<String, Error> {
        let res = self
            .req(
                Method::POST,
                "/auth".into(),
                None,
                Body::from(serde_json::to_string(opts.opts())?),
                None,
            )
            .await?;
        debug!("{:?}", res);
        let status = res.status().as_u16();
        let text = to_bytes(res.into_body()).await?;
        trace!("{}", str::from_utf8(text.as_ref())?);
        match status {
            200 => {
                let msg: AuthMsg = serde_json::from_slice(&text)?;
                Ok(msg.token())
            }
            204 => Ok("".to_string()),
            500 => err_msg!(text, "server error"),
            _ => err_msg!(text, "unknown error"),
        }
    }
    pub async fn exec_inspect(&self, exec_id: &str) -> Result<ExecInspect, Error> {
        trace!("{}", exec_id);
        let res = self
            .req(
                Method::GET,
                format!("/exec/{}/json", exec_id),
                None,
                Body::from(""),
                None,
            )
            .await?;
        debug!("{:?}", res);
        let status = res.status().as_u16();
        let slice = to_bytes(res.into_body()).await?;
        if let Ok(text) = str::from_utf8(&slice) {
            trace!("{}", text);
        }
        match status {
            200 => {
                let exec = serde_json::from_slice::<ExecInspect>(&slice)?;
                Ok(exec)
            }
            404 => err_msg!(slice, "no such exec instance"),
            500 => err_msg!(slice, "server error"),
            _ => err_msg!(slice, "unknown error"),
        }
    }
}

#[derive(Serialize, Deserialize)]
struct Msg {
    message: String,
}
impl Msg {
    fn msg(self) -> String {
        self.message
    }
}
#[derive(Serialize, Deserialize)]
struct AuthMsg {
    Status: String,
    IdentityToken: String,
}
impl AuthMsg {
    #[allow(dead_code)]
    fn status(&self) -> String {
        self.Status.clone()
    }
    fn token(&self) -> String {
        self.IdentityToken.clone()
    }
}