Skip to content
Permalink
Browse files

Copy into container

- add function copy_into to container
- add example
  • Loading branch information...
schrieveslaach committed Feb 19, 2019
1 parent 2a40dc0 commit ae8606cb0172c1a3fa87bed0f672e4c454f8ff68
Showing with 65 additions and 1 deletion.
  1. +28 −0 examples/containercopyinto.rs
  2. +37 −1 src/lib.rs
@@ -0,0 +1,28 @@
use shiplift::Docker;
use std::{env, path};
use tokio::prelude::Future;

fn main() {
let docker = Docker::new();
let path = env::args()
.nth(1)
.expect("Usage: cargo run --example containercopyinto -- <local path> <container>");
let id = env::args()
.nth(2)
.expect("Usage: cargo run --example containercopyinto -- <local path> <container>");


use std::fs::File;
use std::io::prelude::*;

let mut file = File::open(&path).unwrap();
let mut bytes = Vec::new();
file.read_to_end(&mut bytes).expect("Cannot read file on the localhost.");

let fut = docker
.containers()
.get(&id)
.copy_into(path::Path::new(&path), &bytes)
.map_err(|e| eprintln!("Error: {}", e));
tokio::run(fut);
}
@@ -497,7 +497,32 @@ impl<'a, 'b> Container<'a, 'b> {
.map(|c| c.to_vec())
}

// TODO: copy_into
/// Copy a file into (see `bytes`) the container.
pub fn copy_into(
&self,
path: &Path,
bytes: &Vec<u8>
) -> impl Future<Item = (), Error = Error> {

let mut ar = tar::Builder::new(Vec::new());
let mut header = tar::Header::new_gnu();
header.set_size(bytes.len() as u64);
ar.append_data(&mut header,
path.file_name().map(|f| f.to_str().unwrap()).unwrap(),
bytes.as_slice()
).unwrap();
let data = ar.into_inner().unwrap();

let body = Some((data, "application/x-tar".parse::<Mime>().unwrap()));

let path_arg = form_urlencoded::Serializer::new(String::new())
.append_pair("path", &path.parent().map(|p| p.to_string_lossy()).unwrap())
.finish();

self.docker
.put(&format!("/containers/{}/archive?{}", self.id, path_arg), body)
.map(|_| ())
}
}

/// Interface for docker containers
@@ -961,6 +986,17 @@ impl Docker {
self.transport.request(Method::POST, endpoint, body)
}

fn put<B>(
&self,
endpoint: &str,
body: Option<(B, Mime)>,
) -> impl Future<Item = String, Error = Error>
where
B: Into<Body>,
{
self.transport.request(Method::PUT, endpoint, body)
}

fn post_json<B, T>(
&self,
endpoint: &str,

0 comments on commit ae8606c

Please sign in to comment.
You can’t perform that action at this time.