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
use std::error::Error;

use hyper::{body, Body, Method, Request};
use hyper_socket::UnixSocketConnector;

//Socet to connect to
static DOCKERSOCKET : &'static str = "/var/run/docker.sock";

//Make HTTP requests
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);

	//Build request
	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();

	//Send request
	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");
			println!("Response: {}", response_str);
		}
		Err(err) => eprintln!("Docker-rust Error: {}", err),
	}

	Ok(response_str)
}


//Create a docker container from an image
pub async fn create_container(body_str: String) -> Result<String, Box<dyn Error + Send + Sync>> {
	let mut response_str: String = "".to_string();

	let endpoint = format!("http://localhost/containers/create");

	match make_http_request(endpoint, body_str).await {
		Ok(resp) => response_str = resp,
		Err(err) => eprintln!("Docker-rust http request error: {}", err),
	}

	Ok(response_str)
}

//Start a docker container from container ID
pub async fn start_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
	let mut response_str: String = "".to_string();

	let endpoint = format!("http://localhost/containers/{}/start", container_id);

	match make_http_request(endpoint, "".to_string()).await {
		Ok(resp) => response_str = resp,
		Err(err) => eprintln!("Docker-rust http request error: {}", err),
	}

	Ok(response_str)
}

//Stop a docker container from container ID
pub async fn stop_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
	let mut response_str: String = "".to_string();

	let endpoint = format!("http://localhost/containers/{}/stop", container_id);

	match make_http_request(endpoint, "".to_string()).await {
		Ok(resp) => response_str = resp,
		Err(err) => eprintln!("Docker-rust http request error: {}", err),
	}

	Ok(response_str)
}

//Pause a docker container from container ID
pub async fn pause_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
	let mut response_str: String = "".to_string();

	let endpoint = format!("http://localhost/containers/{}/pause", container_id);

	match make_http_request(endpoint, "".to_string()).await {
		Ok(resp) => response_str = resp,
		Err(err) => eprintln!("Docker-rust http request error: {}", err),
	}

	Ok(response_str)
}


//Unpause a docker container from container ID
pub async fn unpause_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
	let mut response_str: String = "".to_string();

	let endpoint = format!("http://localhost/containers/{}/unpause", container_id);

	match make_http_request(endpoint, "".to_string()).await {
		Ok(resp) => response_str = resp,
		Err(err) => eprintln!("Docker-rust http request error: {}", err),
	}

	Ok(response_str)
}