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
use std::error::Error;
use hyper::{body, Body, Method, Request};
use hyper_socket::UnixSocketConnector;
static DOCKERSOCKET : &'static str = "/var/run/docker.sock";
async fn make_http_request(endpoint: String, body_str: String) -> Result<String, Box<dyn Error + Send + Sync>> {
let socket = UnixSocketConnector::new(DOCKERSOCKET);
let client = hyper::Client::builder()
.build::<_, Body>(socket);
let req = Request::builder()
.method(Method::POST)
.header("content-type", "application/json")
.uri(endpoint)
.body(Body::from(body_str))?;
let mut response_str: String = "".to_string();
match client.request(req).await {
Ok(res) => {
let response_bytes = body::to_bytes(res.into_body()).await?;
response_str = String::from_utf8(response_bytes.to_vec()).expect("response was not valid utf-8");
}
Err(err) => eprintln!("shipshape Error: {}", err),
}
Ok(response_str)
}
pub async fn create_container(body_str: String) -> Result<String, Box<dyn Error + Send + Sync>> {
make_http_request("http://localhost/containers/create".to_string(), body_str).await
}
pub async fn start_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
make_http_request(format!("http://localhost/containers/{}/start", container_id), "".to_string()).await
}
pub async fn stop_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
make_http_request(format!("http://localhost/containers/{}/stop", container_id), "".to_string()).await
}
pub async fn pause_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
make_http_request(format!("http://localhost/containers/{}/pause", container_id), "".to_string()).await
}
pub async fn unpause_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
make_http_request(format!("http://localhost/containers/{}/unpause", container_id), "".to_string()).await
}