From 79d65c286025c551a775c0964d168e6feb4b3409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20B=C3=BCsch?= Date: Wed, 14 Nov 2018 20:36:14 +1100 Subject: Async api (#128) * Refactored Transport for better async use Still a bit rough, but it now builds a big future using combinators. It still does one `Runtime::block_on()` to keep the existing API, but this is a first up before making the whole API async. * Migrate most APIs to be Future-based I still need to finish a few of the more tricky ones that I've commented out for now, but most of it compiles and some examples work. In particular, `Docker::stats()` now properly returns an async stream of stats. * Fix events and containerinspect examples * Fix imageinspect, images, info and top examples * Fix containercreate, imagedelete and imagepull examples * Fix more examples * Add back debug statement in Transport::request * De-glob imports in examples * Remove unused imports in examples * Fix NetworkCreateOptions serialization * Add back error message extraction in Transport * Fix Container::create serialization of options * Add containerdelete example * Simplify result * Fix some error handling to remove unwrap() * Fix Image::export() * Fix imagebuild example * Add adapter from Stream of Chunks to AsyncRead Having an `AsyncRead` is required to be able to use the `FramedRead` and `Decoder` stuff from tokio_codec. This code is "borrowed" from https:/github.com/ferristseng/rust-ipfs-api though should probably be moved to its own crate or to tokio_codec. * Fix Container::logs() It now properly demuxes stdout/stderr, and returns a `Stream`. * Fix Container::export() * Use LineCodec for streaming JSON Although in my limited testing it seemed to work fine, there is no guarantee that 1 chunk == 1 piece of valid JSON. However, each JSON structure seems to be serialized on one line, so use LineCodec to turn the body into a stream of lines, then deserialize over this. * Fix serialization of ExecContainerOptions * Fix Container::exec() (kind of...) * Simplify deserialisation in Image::delete() * Small clean-ups * More clean ups * Fix rustdoc + remove extraneous "extern crate" * Fix doc example * Fix formatting --- examples/containercreate.rs | 18 +++++++++++------- examples/containerdelete.rs | 19 +++++++++++++++++++ examples/containerexec.rs | 29 ++++++++++++++++++++--------- examples/containerinspect.rs | 16 ++++++++++++---- examples/containers.rs | 16 +++++++++++++--- examples/events.rs | 14 +++++++++++--- examples/export.rs | 38 ++++++++++++++++++++++++-------------- examples/imagebuild.rs | 21 +++++++++++++-------- examples/imagedelete.rs | 22 ++++++++++++++++------ examples/imageinspect.rs | 16 ++++++++++++---- examples/imagepull.rs | 21 +++++++++++++-------- examples/images.rs | 20 +++++++++++++++----- examples/info.rs | 9 ++++++++- examples/logs.rs | 29 ++++++++++++++++++++--------- examples/networkconnect.rs | 12 ++++++++---- examples/networkcreate.rs | 26 +++++++++++++++----------- examples/networkdelete.rs | 16 +++++++++++++--- examples/networkdisconnect.rs | 12 ++++++++---- examples/networkinspect.rs | 16 ++++++++++++---- examples/networks.rs | 15 ++++++++++++--- examples/stats.rs | 20 ++++++++++++++------ examples/top.rs | 20 ++++++++++++-------- 22 files changed, 301 insertions(+), 124 deletions(-) create mode 100644 examples/containerdelete.rs (limited to 'examples') diff --git a/examples/containercreate.rs b/examples/containercreate.rs index 71307b8..f5baf39 100644 --- a/examples/containercreate.rs +++ b/examples/containercreate.rs @@ -1,15 +1,19 @@ extern crate shiplift; +extern crate tokio; use shiplift::{ContainerOptions, Docker}; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - let containers = docker.containers(); - if let Some(image) = env::args().nth(1) { - let info = containers - .create(&ContainerOptions::builder(image.as_ref()).build()) - .unwrap(); - println!("{:?}", info); - } + let image = env::args() + .nth(1) + .expect("You need to specify an image name"); + let fut = docker + .containers() + .create(&ContainerOptions::builder(image.as_ref()).build()) + .map(|info| println!("{:?}", info)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/containerdelete.rs b/examples/containerdelete.rs new file mode 100644 index 0000000..cc2e9b7 --- /dev/null +++ b/examples/containerdelete.rs @@ -0,0 +1,19 @@ +extern crate shiplift; +extern crate tokio; + +use shiplift::Docker; +use std::env; +use tokio::prelude::Future; + +fn main() { + let docker = Docker::new(); + let id = env::args() + .nth(1) + .expect("You need to specify an container id"); + let fut = docker + .containers() + .get(&id) + .delete() + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); +} diff --git a/examples/containerexec.rs b/examples/containerexec.rs index e10d245..0b3575b 100644 --- a/examples/containerexec.rs +++ b/examples/containerexec.rs @@ -1,10 +1,16 @@ extern crate shiplift; +extern crate tokio; -use shiplift::{Docker, ExecContainerOptions}; +use shiplift::{tty::StreamType, Docker, ExecContainerOptions}; use std::env; +use tokio::prelude::{Future, Stream}; fn main() { let docker = Docker::new(); + let id = env::args() + .nth(1) + .expect("You need to specify a container id"); + let options = ExecContainerOptions::builder() .cmd(vec![ "bash", @@ -15,13 +21,18 @@ fn main() { .attach_stdout(true) .attach_stderr(true) .build(); - if let Some(id) = env::args().nth(1) { - match docker.containers().get(&id).exec(&options) { - Ok(res) => { - println!("Stdout: {}", res.stdout); - println!("Stderr: {}", res.stderr); + let fut = docker + .containers() + .get(&id) + .exec(&options) + .for_each(|line| { + match line.stream_type { + StreamType::StdOut => println!("Stdout: {}", line.data), + StreamType::StdErr => eprintln!("Stderr: {}", line.data), } - Err(err) => println!("An error occured: {:?}", err), - } - } + Ok(()) + }) + .map_err(|e| eprintln!("Error: {}", e)); + + tokio::run(fut); } diff --git a/examples/containerinspect.rs b/examples/containerinspect.rs index a34b5f3..ebd37ef 100644 --- a/examples/containerinspect.rs +++ b/examples/containerinspect.rs @@ -1,12 +1,20 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - if let Some(id) = env::args().nth(1) { - let container = docker.containers().get(&id).inspect().unwrap(); - println!("{:?}", container); - } + let id = env::args() + .nth(1) + .expect("Usage: cargo run --example containerinspect -- "); + let fut = docker + .containers() + .get(&id) + .inspect() + .map(|container| println!("{:#?}", container)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/containers.rs b/examples/containers.rs index 27a5d20..71da705 100644 --- a/examples/containers.rs +++ b/examples/containers.rs @@ -1,12 +1,22 @@ extern crate env_logger; extern crate shiplift; +extern crate tokio; use shiplift::Docker; +use tokio::prelude::Future; fn main() { env_logger::init(); let docker = Docker::new(); - for c in docker.containers().list(&Default::default()).unwrap() { - println!("container -> {:?}", c) - } + let fut = docker + .containers() + .list(&Default::default()) + .map(|containers| { + for c in containers { + println!("container -> {:#?}", c) + } + }) + .map_err(|e| eprintln!("Error: {}", e)); + + tokio::run(fut); } diff --git a/examples/events.rs b/examples/events.rs index f1518d3..ced2f29 100644 --- a/examples/events.rs +++ b/examples/events.rs @@ -1,11 +1,19 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; +use tokio::prelude::{Future, Stream}; fn main() { let docker = Docker::new(); println!("listening for events"); - for e in docker.events(&Default::default()).unwrap() { - println!("event -> {:?}", e) - } + + let fut = docker + .events(&Default::default()) + .for_each(|e| { + println!("event -> {:?}", e); + Ok(()) + }) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/export.rs b/examples/export.rs index b12e2f8..b8d8eaf 100644 --- a/examples/export.rs +++ b/examples/export.rs @@ -1,22 +1,32 @@ extern crate shiplift; +extern crate tokio; -use shiplift::Docker; +use shiplift::{errors::Error, Docker}; use std::env; use std::fs::OpenOptions; -use std::io::copy; +use std::io::Write; +use tokio::prelude::{Future, Stream}; fn main() { let docker = Docker::new(); - if let Some(id) = env::args().nth(1) { - let mut export = OpenOptions::new() - .write(true) - .create(true) - .open(format!("{}.tgz", &id)) - .unwrap(); - let images = docker.images(); - let mut exported = images.get(&id).export().unwrap(); - println!("copying"); - copy(&mut exported, &mut export).unwrap(); - println!("copied"); - } + let id = env::args().nth(1).expect("You need to specify an image id"); + + let mut export_file = OpenOptions::new() + .write(true) + .create(true) + .open(format!("{}.tar", &id)) + .unwrap(); + let images = docker.images(); + let fut = images + .get(&id) + .export() + .for_each(move |bytes| { + export_file + .write(&bytes[..]) + .map(|n| println!("copied {} bytes", n)) + .map_err(Error::IO) + }) + .map_err(|e| eprintln!("Error: {}", e)); + + tokio::run(fut) } diff --git a/examples/imagebuild.rs b/examples/imagebuild.rs index 788634e..4b67219 100644 --- a/examples/imagebuild.rs +++ b/examples/imagebuild.rs @@ -1,17 +1,22 @@ extern crate shiplift; +extern crate tokio; use shiplift::{BuildOptions, Docker}; use std::env; +use tokio::prelude::{Future, Stream}; fn main() { let docker = Docker::new(); - if let Some(path) = env::args().nth(1) { - let image = docker - .images() - .build(&BuildOptions::builder(path).tag("shiplift_test").build()) - .unwrap(); - for output in image { + let path = env::args().nth(1).expect("You need to specify a path"); + + let fut = docker + .images() + .build(&BuildOptions::builder(path).tag("shiplift_test").build()) + .for_each(|output| { println!("{:?}", output); - } - } + Ok(()) + }) + .map_err(|e| eprintln!("Error: {}", e)); + + tokio::run(fut); } diff --git a/examples/imagedelete.rs b/examples/imagedelete.rs index e239865..5e507a2 100644 --- a/examples/imagedelete.rs +++ b/examples/imagedelete.rs @@ -1,14 +1,24 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - if let Some(img) = env::args().nth(1) { - let image = docker.images().get(&img[..]).delete().unwrap(); - for status in image { - println!("{:?}", status); - } - } + let img = env::args() + .nth(1) + .expect("You need to specify an image name"); + let fut = docker + .images() + .get(&img[..]) + .delete() + .map(|statuses| { + for status in statuses { + println!("{:?}", status); + } + }) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/imageinspect.rs b/examples/imageinspect.rs index 50144f0..0cd8450 100644 --- a/examples/imageinspect.rs +++ b/examples/imageinspect.rs @@ -1,12 +1,20 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - if let Some(id) = env::args().nth(1) { - let image = docker.images().get(&id).inspect().unwrap(); - println!("{:?}", image); - } + let id = env::args() + .nth(1) + .expect("Usage: cargo run --example imageinspect -- "); + let fut = docker + .images() + .get(&id) + .inspect() + .map(|image| println!("{:#?}", image)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/imagepull.rs b/examples/imagepull.rs index d460a41..1039080 100644 --- a/examples/imagepull.rs +++ b/examples/imagepull.rs @@ -1,17 +1,22 @@ extern crate shiplift; +extern crate tokio; use shiplift::{Docker, PullOptions}; use std::env; +use tokio::prelude::{Future, Stream}; fn main() { let docker = Docker::new(); - if let Some(img) = env::args().nth(1) { - let image = docker - .images() - .pull(&PullOptions::builder().image(img).build()) - .unwrap(); - for output in image { + let img = env::args() + .nth(1) + .expect("You need to specify an image name"); + let fut = docker + .images() + .pull(&PullOptions::builder().image(img).build()) + .for_each(|output| { println!("{:?}", output); - } - } + Ok(()) + }) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/images.rs b/examples/images.rs index 195ea06..b3e80a8 100644 --- a/examples/images.rs +++ b/examples/images.rs @@ -1,10 +1,20 @@ extern crate shiplift; +extern crate tokio; + +use shiplift::Docker; +use tokio::prelude::Future; fn main() { - let docker = shiplift::Docker::new(); - let images = docker.images().list(&Default::default()).unwrap(); + let docker = Docker::new(); println!("docker images in stock"); - for i in images { - println!("{:?}", i.repo_tags); - } + let fut = docker + .images() + .list(&Default::default()) + .map(|images| { + for i in images { + println!("{:?}", i.repo_tags); + } + }) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/info.rs b/examples/info.rs index 372b603..b608818 100644 --- a/examples/info.rs +++ b/examples/info.rs @@ -1,8 +1,15 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - println!("info {:?}", docker.info().unwrap()); + tokio::run( + docker + .info() + .map(|info| println!("info {:?}", info)) + .map_err(|e| eprintln!("Error: {}", e)), + ); } diff --git a/examples/logs.rs b/examples/logs.rs index 18e68d0..b35b0a3 100644 --- a/examples/logs.rs +++ b/examples/logs.rs @@ -1,16 +1,27 @@ extern crate shiplift; +extern crate tokio; -use shiplift::{Docker, LogsOptions}; +use shiplift::{tty::StreamType, Docker, LogsOptions}; use std::env; +use tokio::prelude::{Future, Stream}; fn main() { let docker = Docker::new(); - if let Some(id) = env::args().nth(1) { - let mut logs = docker - .containers() - .get(&id) - .logs(&LogsOptions::builder().stdout(true).build()) - .unwrap(); - std::io::copy(&mut logs, &mut std::io::stdout()).unwrap(); - } + let id = env::args() + .nth(1) + .expect("You need to specify a container id"); + let fut = docker + .containers() + .get(&id) + .logs(&LogsOptions::builder().stdout(true).stderr(true).build()) + .for_each(|line| { + match line.stream_type { + StreamType::StdOut => println!("Stdout: {}", line.data), + StreamType::StdErr => eprintln!("Stderr: {}", line.data), + } + Ok(()) + }) + .map_err(|e| eprintln!("Error: {}", e)); + + tokio::run(fut); } diff --git a/examples/networkconnect.rs b/examples/networkconnect.rs index e203e18..86a2ac1 100644 --- a/examples/networkconnect.rs +++ b/examples/networkconnect.rs @@ -1,18 +1,22 @@ extern crate shiplift; +extern crate tokio; use shiplift::{ContainerConnectionOptions, Docker}; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); let networks = docker.networks(); match (env::args().nth(1), env::args().nth(2)) { - (Some(container_id), Some(network_id)) => println!( - "{:?}", - networks + (Some(container_id), Some(network_id)) => { + let fut = networks .get(&network_id) .connect(&ContainerConnectionOptions::new(&container_id)) - ), + .map(|v| println!("{:?}", v)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); + } _ => eprintln!("please provide a container_id and network_id"), } } diff --git a/examples/networkcreate.rs b/examples/networkcreate.rs index e64d826..a7b7662 100644 --- a/examples/networkcreate.rs +++ b/examples/networkcreate.rs @@ -1,19 +1,23 @@ extern crate shiplift; +extern crate tokio; use shiplift::{Docker, NetworkCreateOptions}; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - let networks = docker.networks(); - if let Some(network_name) = env::args().nth(1) { - let info = networks - .create( - &NetworkCreateOptions::builder(network_name.as_ref()) - .driver("bridge") - .build(), - ) - .unwrap(); - println!("{:?}", info); - } + let network_name = env::args() + .nth(1) + .expect("You need to specify a network name"); + let fut = docker + .networks() + .create( + &NetworkCreateOptions::builder(network_name.as_ref()) + .driver("bridge") + .build(), + ) + .map(|info| println!("{:?}", info)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/networkdelete.rs b/examples/networkdelete.rs index 1ce4c4d..8f1cdca 100644 --- a/examples/networkdelete.rs +++ b/examples/networkdelete.rs @@ -1,11 +1,21 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - if let Some(id) = env::args().nth(1) { - println!("{:?}", docker.networks().get(&id).delete()); - } + let id = env::args() + .nth(1) + .expect("You need to specify a network id"); + let fut = docker + .networks() + .get(&id) + .delete() + .map(|network| println!("{:?}", network)) + .map_err(|e| eprintln!("Error: {}", e)); + + tokio::run(fut); } diff --git a/examples/networkdisconnect.rs b/examples/networkdisconnect.rs index 9676dc9..9c529a1 100644 --- a/examples/networkdisconnect.rs +++ b/examples/networkdisconnect.rs @@ -1,18 +1,22 @@ extern crate shiplift; +extern crate tokio; use shiplift::{ContainerConnectionOptions, Docker}; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); let networks = docker.networks(); match (env::args().nth(1), env::args().nth(2)) { - (Some(container_id), Some(network_id)) => println!( - "{:?}", - networks + (Some(container_id), Some(network_id)) => { + let fut = networks .get(&network_id) .disconnect(&ContainerConnectionOptions::new(&container_id)) - ), + .map(|v| println!("{:?}", v)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); + } _ => eprintln!("please provide a container_id and network_id"), } } diff --git a/examples/networkinspect.rs b/examples/networkinspect.rs index f88b3c2..37992a5 100644 --- a/examples/networkinspect.rs +++ b/examples/networkinspect.rs @@ -1,12 +1,20 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - if let Some(id) = env::args().nth(1) { - let network = docker.networks().get(&id).inspect().unwrap(); - println!("{:?}", network); - } + let id = env::args() + .nth(1) + .expect("You need to specify a network id"); + let fut = docker + .networks() + .get(&id) + .inspect() + .map(|network| println!("{:#?}", network)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/networks.rs b/examples/networks.rs index 03da617..57cd566 100644 --- a/examples/networks.rs +++ b/examples/networks.rs @@ -1,12 +1,21 @@ extern crate env_logger; extern crate shiplift; +extern crate tokio; use shiplift::Docker; +use tokio::prelude::Future; fn main() { env_logger::init(); let docker = Docker::new(); - for c in docker.networks().list(&Default::default()).unwrap() { - println!("network -> {:?}", c) - } + let fut = docker + .networks() + .list(&Default::default()) + .map(|networks| { + for network in networks { + println!("network -> {:#?}", network); + } + }) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/stats.rs b/examples/stats.rs index 9a0f38d..f136326 100644 --- a/examples/stats.rs +++ b/examples/stats.rs @@ -1,15 +1,23 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; use std::env; +use tokio::prelude::{Future, Stream}; fn main() { let docker = Docker::new(); let containers = docker.containers(); - if let Some(id) = env::args().nth(1) { - let stats = containers.get(&id).stats(); - for s in stats.unwrap() { - println!("{:?}", s); - } - } + let id = env::args() + .nth(1) + .expect("Usage: cargo run --example stats -- "); + let fut = containers + .get(&id) + .stats() + .for_each(|stat| { + println!("{:?}", stat); + Ok(()) + }) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } diff --git a/examples/top.rs b/examples/top.rs index 79a7f07..1bdf6ca 100644 --- a/examples/top.rs +++ b/examples/top.rs @@ -1,16 +1,20 @@ extern crate shiplift; +extern crate tokio; use shiplift::Docker; use std::env; +use tokio::prelude::Future; fn main() { let docker = Docker::new(); - if let Some(id) = env::args().nth(1) { - let top = docker - .containers() - .get(&id) - .top(Default::default()) - .unwrap(); - println!("{:?}", top); - } + let id = env::args() + .nth(1) + .expect("Usage: cargo run --example top -- "); + let fut = docker + .containers() + .get(&id) + .top(Default::default()) + .map(|top| println!("{:#?}", top)) + .map_err(|e| eprintln!("Error: {}", e)); + tokio::run(fut); } -- cgit v1.2.3