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
use std;
use std::error::Error;
use std::path::Path;
use tcp::TcpStream;
use unix::UnixStream;
use http::{self, Response};
use container::{Container, ContainerInfo};
use process::{Process, Top};
use stats::Stats;
use system::SystemInfo;
use image::{Image, ImageStatus};
use filesystem::FilesystemChange;
use version::Version;
use rustc_serialize::json;
pub struct Docker {
protocol: Protocol,
unix_stream: Option<UnixStream>,
tcp_stream: Option<TcpStream>,
}
enum Protocol {
UNIX,
TCP
}
impl Docker {
pub fn connect(addr: &str) -> std::io::Result<Docker> {
let components: Vec<&str> = addr.split("://").collect();
if components.len() != 2 {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
"The address is invalid.");
return Err(err);
}
let protocol = components[0];
let path = components[1].to_string();
let protocol = match protocol {
"unix" => Protocol::UNIX,
"tcp" => Protocol::TCP,
_ => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
"The protocol is not supported.");
return Err(err);
}
};
let unix_stream = match protocol {
Protocol::UNIX => {
let stream = try!(UnixStream::connect(&*path));
Some(stream)
}
_ => None
};
let tcp_stream = match protocol {
Protocol::TCP => {
let stream = try!(TcpStream::connect(&*path));
Some(stream)
}
_ => None
};
let docker = Docker {
protocol: protocol,
unix_stream: unix_stream,
tcp_stream: tcp_stream
};
return Ok(docker);
}
pub fn set_tls(&mut self, key: &Path, cert: &Path, ca: &Path) -> std::io::Result<()> {
match self.tcp_stream {
Some(_) => {
let mut tcp_stream = self.tcp_stream.as_mut().unwrap();
try!(tcp_stream.set_ssl_context(key, cert, ca));
}
None => {}
}
return Ok(());
}
pub fn get_containers(&mut self, all: bool) -> std::io::Result<Vec<Container>> {
let a = match all {
true => "1",
false => "0"
};
let request = format!("GET /containers/json?all={}&size=1 HTTP/1.1\r\n\r\n", a);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let body = try!(response.get_encoded_body());
let containers: Vec<Container> = match json::decode(&body) {
Ok(containers) => containers,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
return Ok(containers);
}
pub fn get_processes(&mut self, container: &Container) -> std::io::Result<Vec<Process>> {
let request = format!("GET /containers/{}/top HTTP/1.1\r\n\r\n", container.Id);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let body = try!(response.get_encoded_body());
let top: Top = match json::decode(&body) {
Ok(top) => top,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
let mut processes: Vec<Process> = Vec::new();
let mut process_iter = top.Processes.iter();
loop {
let process = match process_iter.next() {
Some(process) => process,
None => { break; }
};
let mut p = Process{
user: String::new(),
pid: String::new(),
cpu: None,
memory: None,
vsz: None,
rss: None,
tty: None,
stat: None,
start: None,
time: None,
command: String::new()
};
let mut value_iter = process.iter();
let mut i: usize = 0;
loop {
let value = match value_iter.next() {
Some(value) => value,
None => { break; }
};
let key = &top.Titles[i];
match key.as_ref() {
"USER" => { p.user = value.clone() },
"PID" => { p.pid = value.clone() },
"%CPU" => { p.cpu = Some(value.clone()) },
"%MEM" => { p.memory = Some(value.clone()) },
"VSZ" => { p.vsz = Some(value.clone()) },
"RSS" => { p.rss = Some(value.clone()) },
"TTY" => { p.tty = Some(value.clone()) },
"STAT" => { p.stat = Some(value.clone()) },
"START" => { p.start = Some(value.clone()) },
"TIME" => { p.time = Some(value.clone()) },
"COMMAND" => { p.command = value.clone() },
_ => {}
}
i = i + 1;
};
processes.push(p);
}
return Ok(processes);
}
pub fn get_stats(&mut self, container: &Container) -> std::io::Result<Stats> {
if container.Status.contains("Up") == false {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
"The container is already stopped.");
return Err(err);
}
let request = format!("GET /containers/{}/stats HTTP/1.1\r\n\r\n", container.Id);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let body = try!(response.get_encoded_body());
let stats: Stats = match json::decode(&body) {
Ok(stats) => stats,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
return Ok(stats);
}
pub fn create_image(&mut self, image: String, tag: String) -> std::io::Result<Vec<ImageStatus>> {
let request = format!("POST /images/create?fromImage={}&tag={} HTTP/1.1\r\n\r\n", image, tag);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
let body = format!("[{}]", try!(response.get_encoded_body()));
let fixed = body.replace("}{", "},{");
let statuses: Vec<ImageStatus> = match json::decode(&fixed) {
Ok(statuses) => statuses,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
return Ok(statuses);
}
pub fn get_images(&mut self, all: bool) -> std::io::Result<Vec<Image>> {
let a = match all {
true => "1",
false => "0"
};
let request = format!("GET /images/json?all={} HTTP/1.1\r\n\r\n", a);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let body = try!(response.get_encoded_body());
let images: Vec<Image> = match json::decode(&body) {
Ok(images) => images,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
return Ok(images);
}
pub fn get_system_info(&mut self) -> std::io::Result<SystemInfo> {
let request = "GET /info HTTP/1.1\r\n\r\n";
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let body = try!(response.get_encoded_body());
let info: SystemInfo = match json::decode(&body) {
Ok(info) => info,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
return Ok(info);
}
pub fn get_container_info(&mut self, container: &Container) -> std::io::Result<ContainerInfo> {
let request = format!("GET /containers/{}/json HTTP/1.1\r\n\r\n", container.Id);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let body = try!(response.get_encoded_body());
let container_info: ContainerInfo = match json::decode(&body) {
Ok(body) => body,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
return Ok(container_info);
}
pub fn get_filesystem_changes(&mut self, container: &Container) -> std::io::Result<Vec<FilesystemChange>> {
let request = format!("GET /containers/{}/changes HTTP/1.1\r\n\r\n", container.Id);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let body = try!(response.get_encoded_body());
let filesystem_changes: Vec<FilesystemChange> = match json::decode(&body) {
Ok(body) => body,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput,
e.description());
return Err(err);
}
};
return Ok(filesystem_changes);
}
pub fn export_container(&mut self, container: &Container) -> std::io::Result<Vec<u8>> {
let request = format!("GET /containers/{}/export HTTP/1.1\r\n\r\n", container.Id);
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
return Ok(response.body);
}
pub fn ping(&mut self) -> std::io::Result<String> {
let request = format!("GET /_ping HTTP/1.1\r\n\r\n");
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let encoded_body = try!(response.get_encoded_body());
return Ok(encoded_body);
}
pub fn get_version(&mut self) -> std::io::Result<Version> {
let request = format!("GET /version HTTP/1.1\r\n\r\n");
let raw = try!(self.read(request.as_bytes()));
let response = try!(self.get_response(&raw));
try!(self.get_status_code(&response));
let encoded_body = try!(response.get_encoded_body());
let version: Version = match json::decode(&encoded_body){
Ok(body) => body,
Err(e) => {
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput, e.description());
return Err(err);
}
};
return Ok(version);
}
fn read(&mut self, buf: &[u8]) -> std::io::Result<Vec<u8>> {
return match self.protocol {
Protocol::UNIX => {
let mut stream = self.unix_stream.as_mut().unwrap();
stream.read(buf)
}
Protocol::TCP => {
let mut stream = self.tcp_stream.as_mut().unwrap();
stream.read(buf)
}
};
}
fn get_response(&self, raw: &Vec<u8>) -> std::io::Result<Response> {
http::get_response(raw)
}
fn get_status_code(&self, response: &Response) -> std::io::Result<()> {
let status_code = response.status_code;
match status_code / 100 {
2 => { Ok(()) }
_ => {
let desc = format!("Docker returns an error with {} status code.", status_code);
let err = std::io::Error::new(std::io::ErrorKind::InvalidInput, desc);
return Err(err);
}
}
}
}
impl Clone for Docker {
fn clone(&self)-> Self {
let protocol = match self.protocol {
Protocol::UNIX => Protocol::UNIX,
Protocol::TCP => Protocol::TCP
};
let unix_stream = match self.unix_stream {
Some(ref unix_stream) => {
Some(unix_stream.clone())
}
None => None
};
let tcp_stream = match self.tcp_stream {
Some(ref tcp_stream) => {
Some(tcp_stream.clone())
}
None => None
};
let docker = Docker {
protocol: protocol,
unix_stream: unix_stream,
tcp_stream: tcp_stream
};
return docker;
}
}