shiplift 0.3.1

A Rust interface for maneuvering Docker containers
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Shiplift is a multi-transport utility for maneuvering [docker](https://www.docker.com/) containers
//!
//! # examples
//!
//! ```no_run
//! extern crate shiplift;
//!
//! let docker = shiplift::Docker::new();
//! let images = docker.images().list(&Default::default()).unwrap();
//! println!("docker images in stock");
//! for i in images {
//!   println!("{:?}", i.RepoTags);
//! }
//! ```

#[macro_use]
extern crate log;
#[macro_use]
#[macro_use]
extern crate hyper;
extern crate flate2;
extern crate hyperlocal;
extern crate jed;
extern crate openssl;
extern crate rustc_serialize;
extern crate url;
extern crate tar;

pub mod builder;
pub mod rep;
pub mod transport;
pub mod errors;

mod tarball;

pub use errors::Error;
pub use builder::{BuildOptions, ContainerOptions, ContainerListOptions, ContainerFilter,
                  EventsOptions, ImageFilter, ImageListOptions, LogsOptions,
                  PullOptions, RmContainerOptions
                  };
use hyper::{Client, Url};
use hyper::header::ContentType;
use hyper::net::{HttpsConnector, Openssl};
use hyper::method::Method;
use hyperlocal::UnixSocketConnector;
use openssl::x509::X509FileType;
use openssl::ssl::{SslContext, SslMethod};
use rep::Image as ImageRep;
use rep::{PullOutput, PullInfo, BuildOutput, Change, ContainerCreateInfo, ContainerDetails,
          Container as ContainerRep, Event, Exit, History, ImageDetails, Info, SearchResult,
          Stats, Status, Top, Version};
use rustc_serialize::json::{self, Json};
use std::env::{self, VarError};
use std::io::Read;
use std::iter::IntoIterator;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use transport::{tar, Transport};
use hyper::client::Body;
use url::{form_urlencoded};

/// Represents the result of all docker operations
pub type Result<T> = std::result::Result<T, Error>;

/// Entrypoint interface for communicating with docker daemon
pub struct Docker {
    transport: Transport,
}

/// Interface for accessing and manipulating a named docker image
pub struct Image<'a, 'b> {
    docker: &'a Docker,
    name: &'b str,
}

impl<'a, 'b> Image<'a, 'b> {
    /// Exports an interface for operations that may be performed against a named image
    pub fn new(docker: &'a Docker, name: &'b str) -> Image<'a, 'b> {
        Image {
            docker: docker,
            name: name,
        }
    }

    /// Inspects a named image's details
    pub fn inspect(&self) -> Result<ImageDetails> {
        let raw = try!(self.docker.get(&format!("/images/{}/json", self.name)[..]));
        Ok(try!(json::decode::<ImageDetails>(&raw)))
    }

    /// Lists the history of the images set of changes
    pub fn history(&self) -> Result<Vec<History>> {
        let raw = try!(self.docker.get(&format!("/images/{}/history", self.name)[..]));
        Ok(try!(json::decode::<Vec<History>>(&raw)))
    }

    /// Delete's an image
    pub fn delete(&self) -> Result<Vec<Status>> {
        let raw = try!(self.docker.delete(&format!("/images/{}", self.name)[..]));
        Ok(match try!(Json::from_str(&raw)) {
               Json::Array(ref xs) => {
                   xs.iter().map(|j| {
                       let obj = j.as_object().expect("expected json object");
                       obj.get("Untagged")
                          .map(|sha| {
                              Status::Untagged(sha.as_string()
                                                  .expect("expected Untagged to be a string")
                                                  .to_owned())
                          })
                          .or(obj.get("Deleted")
                                 .map(|sha| {
                                     Status::Deleted(sha.as_string()
                                                        .expect("expected Deleted to be a string")
                                                        .to_owned())
                                 }))
                          .expect("expected Untagged or Deleted")
                   })
               }
               _ => unreachable!(),
           }
           .collect())
    }

    /// Export this image to a tarball
    pub fn export(&self) -> Result<Box<Read>> {
        self.docker.stream_get(&format!("/images/{}/get", self.name)[..])
    }
}

/// Interface for docker images
pub struct Images<'a> {
    docker: &'a Docker,
}

impl<'a> Images<'a> {
    /// Exports an interface for interacting with docker images
    pub fn new(docker: &'a Docker) -> Images<'a> {
        Images { docker: docker }
    }

    /// Builds a new image build by reading a Dockerfile in a target directory
    pub fn build(&self, opts: &BuildOptions) -> Result<Box<Iterator<Item = BuildOutput>>> {
        let mut path = vec!["/build".to_owned()];
        if let Some(query) = opts.serialize() {
            path.push(query)
        }

        let mut bytes = vec![];

        try!(tarball::dir(&mut bytes, &opts.path[..]));

        let raw = try!(self.docker.stream_post(&path.join("?"),
                                               Some((Body::BufBody(&bytes[..], bytes.len()),
                                                     tar()))));
        let it = jed::Iter::new(raw).into_iter().map(|j| {
            // fixme: better error handling
            debug!("{:?}", j);
            let obj = j.as_object().expect("expected json object");
            obj.get("stream")
               .map(|stream| {
                   BuildOutput::Stream(stream.as_string()
                                             .expect("expected stream to be a string")
                                             .to_owned())
               })
               .or(obj.get("error")
                      .map(|err| {
                          BuildOutput::Err(err.as_string()
                                              .expect("expected error to be a string")
                                              .to_owned())
                      }))
               .expect("expected build output stream or error")
        });
        Ok(Box::new(it))
    }

    /// Lists the docker images on the current docker host
    pub fn list(&self, opts: &ImageListOptions) -> Result<Vec<ImageRep>> {
        let mut path = vec!["/images/json".to_owned()];
        if let Some(query) = opts.serialize() {
            path.push(query);
        }
        let raw = try!(self.docker.get(&path.join("?")));
        Ok(try!(json::decode::<Vec<ImageRep>>(&raw)))
    }

    /// Returns a reference to a set of operations available for a named image
    pub fn get(&'a self, name: &'a str) -> Image {
        Image::new(self.docker, name)
    }

    /// Search for docker images by term
    pub fn search(&self, term: &str) -> Result<Vec<SearchResult>> {
        let query = form_urlencoded::serialize(vec![("term", term)]);
        let raw = try!(self.docker.get(&format!("/images/search?{}", query)[..]));
        Ok(try!(json::decode::<Vec<SearchResult>>(&raw)))
    }

    /// Pull and create a new docker images from an existing image
    pub fn pull(&self, opts: &PullOptions) -> Result<Box<Iterator<Item = PullOutput>>> {
        let mut path = vec!["/images/create".to_owned()];
        if let Some(query) = opts.serialize() {
            path.push(query);
        }
        let raw = try!(self.docker
                           .stream_post(&path.join("?"), None as Option<(&'a str, ContentType)>));
        let it = jed::Iter::new(raw).into_iter().map(|j| {
            // fixme: better error handling
            debug!("{:?}", j);
            let s = json::encode(&j).unwrap();
            json::decode::<PullInfo>(&s)
                .map(|info| {
                    PullOutput::Status {
                        id: info.id,
                        status: info.status,
                        progress: info.progress,
                        progress_detail: info.progressDetail,
                    }
                })
                .ok()
                .or(j.as_object()
                     .expect("expected json object")
                     .get("error")
                     .map(|err| {
                         PullOutput::Err(err.as_string()
                                            .expect("expected error to be a string")
                                            .to_owned())
                     }))
                .expect("expected pull status or error")
        });
        Ok(Box::new(it))
    }

    /// exports a collection of named images,
    /// either by name, name:tag, or image id, into a tarball
    pub fn export(&self, names: Vec<&str>) -> Result<Box<Read>> {
        let params = names.iter()
                          .map(|n| ("names", *n))
                          .collect::<Vec<(&str, &str)>>();
        let query = form_urlencoded::serialize(params);
        self.docker.stream_get(&format!("/images/get?{}", query)[..])
    }

    // pub fn import(self, tarball: Box<Read>) -> Result<()> {
    //  self.docker.post
    // }
}

/// Interface for accessing and manipulating a docker container
pub struct Container<'a, 'b> {
    docker: &'a Docker,
    id: &'b str,
}

impl<'a, 'b> Container<'a, 'b> {
    /// Exports an interface exposing operations against a container instance
    pub fn new(docker: &'a Docker, id: &'b str) -> Container<'a, 'b> {
        Container {
            docker: docker,
            id: id,
        }
    }

    /// a getter for the container id
    pub fn id(&self) -> &str { &self.id }

    /// Inspects the current docker container instance's details
    pub fn inspect(&self) -> Result<ContainerDetails> {
        let raw = try!(self.docker.get(&format!("/containers/{}/json", self.id)[..]));
        Ok(try!(json::decode::<ContainerDetails>(&raw)))
    }

    /// Returns a `top` view of information about the container process
    pub fn top(&self, psargs: Option<&str>) -> Result<Top> {
        let mut path = vec![format!("/containers/{}/top", self.id)];
        if let Some(ref args) = psargs {
            let encoded = form_urlencoded::serialize(vec![("ps_args", args)]);
            path.push(encoded)
        }
        let raw = try!(self.docker.get(&path.join("?")));

        Ok(try!(json::decode::<Top>(&raw)))
    }

    /// Returns a stream of logs emitted but the container instance
    pub fn logs(&self, opts: &LogsOptions) -> Result<Box<Read>> {
        let mut path = vec![format!("/containers/{}/logs", self.id)];
        if let Some(query) = opts.serialize() {
            path.push(query)
        }
        self.docker.stream_get(&path.join("?"))
    }

    /// Returns a set of changes made to the container instance
    pub fn changes(&self) -> Result<Vec<Change>> {
        let raw = try!(self.docker.get(&format!("/containers/{}/changes", self.id)[..]));
        Ok(try!(json::decode::<Vec<Change>>(&raw)))
    }

    /// Exports the current docker container into a tarball
    pub fn export(&self) -> Result<Box<Read>> {
        self.docker.stream_get(&format!("/containers/{}/export", self.id)[..])
    }

    /// Returns a stream of stats specific to this container instance
    pub fn stats(&self) -> Result<Box<Iterator<Item = Stats>>> {
        let raw = try!(self.docker.stream_get(&format!("/containers/{}/stats", self.id)[..]));
        let it = jed::Iter::new(raw).into_iter().map(|j| {
            // fixme: better error handling
            debug!("{:?}", j);
            let s = json::encode(&j).unwrap();
            json::decode::<Stats>(&s).unwrap()
        });
        Ok(Box::new(it))
    }

    /// Start the container instance
    pub fn start(&'a self) -> Result<()> {
        self.docker
            .post(&format!("/containers/{}/start", self.id)[..],
                  None as Option<(&'a str, ContentType)>)
            .map(|_| ())
    }

    /// Stop the container instance
    pub fn stop(&self, wait: Option<Duration>) -> Result<()> {
        let mut path = vec![format!("/containers/{}/stop", self.id)];
        if let Some(w) = wait {
            let encoded = form_urlencoded::serialize(vec![("t", w.as_secs().to_string())]);
            path.push(encoded)
        }
        self.docker.post(&path.join("?"), None as Option<(&'a str, ContentType)>).map(|_| ())
    }

    /// Restart the container instance
    pub fn restart(&self, wait: Option<Duration>) -> Result<()> {
        let mut path = vec![format!("/containers/{}/restart", self.id)];
        if let Some(w) = wait {
            let encoded = form_urlencoded::serialize(vec![("t", w.as_secs().to_string())]);
            path.push(encoded)
        }
        self.docker.post(&path.join("?"), None as Option<(&'a str, ContentType)>).map(|_| ())
    }

    /// Kill the container instance
    pub fn kill(&self, signal: Option<&str>) -> Result<()> {
        let mut path = vec![format!("/containers/{}/kill", self.id)];
        if let Some(sig) = signal {
            let encoded = form_urlencoded::serialize(vec![("signal", sig.to_owned())]);
            path.push(encoded)
        }
        self.docker.post(&path.join("?"), None as Option<(&'a str, ContentType)>).map(|_| ())
    }

    /// Rename the container instance
    pub fn rename(&self, name: &str) -> Result<()> {
        let query = form_urlencoded::serialize(vec![("name", name)]);
        self.docker
            .post(&format!("/containers/{}/rename?{}", self.id, query)[..],
                  None as Option<(&'a str, ContentType)>)
            .map(|_| ())
    }

    /// Pause the container instance
    pub fn pause(&self) -> Result<()> {
        self.docker
            .post(&format!("/containers/{}/pause", self.id)[..],
                  None as Option<(&'a str, ContentType)>)
            .map(|_| ())
    }

    /// Unpause the container instance
    pub fn unpause(&self) -> Result<()> {
        self.docker
            .post(&format!("/containers/{}/unpause", self.id)[..],
                  None as Option<(&'a str, ContentType)>)
            .map(|_| ())
    }

    /// Wait until the container stops
    pub fn wait(&self) -> Result<Exit> {
        let raw = try!(self.docker.post(&format!("/containers/{}/wait", self.id)[..],
                                        None as Option<(&'a str, ContentType)>));
        Ok(try!(json::decode::<Exit>(&raw)))
    }

    /// Delete the container instance
    /// use remove instead to use the force/v options
    pub fn delete(&self) -> Result<()> {
        self.docker.delete(&format!("/containers/{}", self.id)[..]).map(|_| ())
    }

    /// Delete the container instance (todo: force/v)
    pub fn remove(&self, opts: RmContainerOptions) -> Result<()> {
        let mut path = vec![format!("/containers/{}", self.id)];
        if let Some(query) = opts.serialize() {
            path.push(query)
        }
        try!(self.docker.delete(&path.join("?")));
        Ok(())
    }

    // todo attach, attach/ws, copy, archive
}

/// Interface for docker containers
pub struct Containers<'a> {
    docker: &'a Docker,
}

impl<'a> Containers<'a> {
    /// Exports an interface for interacting with docker containers
    pub fn new(docker: &'a Docker) -> Containers<'a> {
        Containers { docker: docker }
    }

    /// Lists the container instances on the docker host
    pub fn list(&self, opts: &ContainerListOptions) -> Result<Vec<ContainerRep>> {
        let mut path = vec!["/containers/json".to_owned()];
        if let Some(query) = opts.serialize() {
            path.push(query)
        }
        let raw = try!(self.docker.get(&path.join("?")));
        Ok(try!(json::decode::<Vec<ContainerRep>>(&raw)))
    }

    /// Returns a reference to a set of operations available to a specific container instance
    pub fn get(&'a self, name: &'a str) -> Container {
        Container::new(self.docker, name)
    }

    /// Returns a builder interface for creating a new container instance
    pub fn create(&'a self, opts: &ContainerOptions) -> Result<ContainerCreateInfo> {
        let data = try!(opts.serialize());
        let mut bytes = data.as_bytes();
        let mut path = vec!["/containers/create".to_owned()];

        if let Some(ref name) = opts.name {
            path.push(form_urlencoded::serialize(vec![("name", name)]));
        }

        let raw = try!(self.docker.post(&path.join("?"),
                                        Some((&mut bytes, ContentType::json()))));
        Ok(try!(json::decode::<ContainerCreateInfo>(&raw)))
    }
}

// https://docs.docker.com/reference/api/docker_remote_api_v1.17/
impl Docker {
    /// constructs a new Docker instance for a docker host listening at a url specified by an env var `DOCKER_HOST`,
    /// falling back on unix:///var/run/docker.sock
    pub fn new() -> Docker {
        let fallback: std::result::Result<String, VarError> = Ok("unix:///var/run/docker.sock"
                                                                     .to_owned());
        let host = env::var("DOCKER_HOST")
                       .or(fallback)
                       .map(|h| {
                           Url::parse(&h)
                               .ok()
                               .expect("invalid url")
                       })
                       .ok()
                       .expect("expected host");
        Docker::host(host)
    }

    /// constructs a new Docker instance for docker host listening at the given host url
    pub fn host(host: Url) -> Docker {
        match host.scheme() {
            "unix" => {
                Docker {
                    transport: Transport::Unix {
                        client: Client::with_connector(UnixSocketConnector),
                        path: host.path().to_owned(),
                    },
                }
            }
            _ => {
                let client = if let Some(ref certs) = env::var("DOCKER_CERT_PATH").ok() {
                    // fixme: don't unwrap before you know what's in the box
                    // https://github.com/hyperium/hyper/blob/master/src/net.rs#L427-L428
                    let mut ssl_ctx = SslContext::new(SslMethod::Sslv23).unwrap();
                    ssl_ctx.set_cipher_list("DEFAULT").unwrap();
                    let cert = &format!("{}/cert.pem", certs);
                    let key = &format!("{}/key.pem", certs);
                    let _ = ssl_ctx.set_certificate_file(&Path::new(cert), X509FileType::PEM);
                    let _ = ssl_ctx.set_private_key_file(&Path::new(key), X509FileType::PEM);
                    if let Some(_) = env::var("DOCKER_TLS_VERIFY").ok() {
                        let ca = &format!("{}/ca.pem", certs);
                        let _ = ssl_ctx.set_CA_file(&Path::new(ca));
                    };
                    Client::with_connector(HttpsConnector::new(Openssl {
                        context: Arc::new(ssl_ctx),
                    }))
                } else {
                    Client::new()
                };
                Docker {
                    transport: Transport::Tcp {
                        client: client,
                        host: format!("https://{}:{}", host.host_str().unwrap().to_owned(), host.port_or_known_default().unwrap()),
                    },
                }
            }
        }
    }

    /// Exports an interface for interacting with docker images
    pub fn images<'a>(&'a self) -> Images {
        Images::new(self)
    }

    /// Exports an interface for interacting with docker containers
    pub fn containers<'a>(&'a self) -> Containers {
        Containers::new(self)
    }

    /// Returns version information associated with the docker daemon
    pub fn version(&self) -> Result<Version> {
        let raw = try!(self.get("/version"));
        Ok(try!(json::decode::<Version>(&raw)))
    }

    /// Returns information associated with the docker daemon
    pub fn info(&self) -> Result<Info> {
        let raw = try!(self.get("/info"));
        Ok(try!(json::decode::<Info>(&raw)))
    }

    /// Returns a simple ping response indicating the docker daemon is accessible
    pub fn ping(&self) -> Result<String> {
        self.get("/_ping")
    }

    /// Returns an interator over streamed docker events
    pub fn events(&self, opts: &EventsOptions) -> Result<Box<Iterator<Item = Event>>> {
        let mut path = vec!["/events".to_owned()];
        if let Some(query) = opts.serialize() {
            path.push(query);
        }
        let raw = try!(self.stream_get(&path.join("?")[..]));
        let it = jed::Iter::new(raw).into_iter().map(|j| {
            debug!("{:?}", j);
            // fixme: better error handling
            let s = json::encode(&j).unwrap();
            json::decode::<Event>(&s).unwrap()
        });
        Ok(Box::new(it))
    }

    fn get<'a>(&self, endpoint: &str) -> Result<String> {
        self.transport.request(Method::Get,
                               endpoint,
                               None as Option<(&'a str, ContentType)>)
    }

    fn post<'a, B>(&'a self, endpoint: &str, body: Option<(B, ContentType)>) -> Result<String>
        where B: Into<Body<'a>>
    {
        self.transport.request(Method::Post, endpoint, body)
    }

    fn delete<'a>(&self, endpoint: &str) -> Result<String> {
        self.transport.request(Method::Delete,
                               endpoint,
                               None as Option<(&'a str, ContentType)>)
    }

    fn stream_post<'a, B>(&'a self,
                          endpoint: &str,
                          body: Option<(B, ContentType)>)
                          -> Result<Box<Read>>
        where B: Into<Body<'a>>
    {
        self.transport.stream(Method::Post, endpoint, body)
    }

    fn stream_get<'a>(&self, endpoint: &str) -> Result<Box<Read>> {
        self.transport.stream(Method::Get,
                              endpoint,
                              None as Option<(&'a str, ContentType)>)
    }
}