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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#![allow(non_snake_case)]
use std::collections::HashMap;

use api::api_utils;
use api::DockerApiClient;
use utils::Response;

use serde_json;

#[derive(Serialize, Deserialize, Debug)]
pub struct Container {
    pub Id: String,
    pub Names: Vec<String>,
    pub Image: String,
    pub ImageID: String,
    pub Command: String,
    pub State: String,
    pub Status: String,
    pub Ports: Vec<Port>,
    pub Labels: Option<HashMap<String, String>>,

    #[serde(default)]
    pub SizeRw: Option<u64>,

    #[serde(default)]
    pub SizeRootFs: u64,
    pub HostConfig: HostConfig,
    pub Mounts: Vec<Mounts>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Port {
    pub PrivatePort: u32,
    pub PublicPort: u32,
    pub Type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct HostConfig {
    pub NetworkMode: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Mounts {
    pub Name: String,
    pub Source: String,
    pub Destination: String,
    pub Driver: String,
    pub Mode: String,
    pub RW: bool,
    pub Propagation: String,
}

/// Structure for implementing Container Config
/// Derives Default fot being able to get started even with minimal
/// config.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ContainerConfig {
    pub Image: String,
    pub Cmd: Vec<String>,

    pub Hostname: String,
    pub Domainname: String,
    pub User: String,
    pub AttachStdin: bool,
    pub AttachStdout: bool,
    pub AttachStderr: bool,
    pub Tty: bool,
    pub OpenStdin: bool,
    pub StdinOnce: bool,
    pub Env: Vec<String>,
    pub Entrypoint: Option<String>,
    pub Labels: Option<HashMap<String, String>>,
    pub WorkingDir: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CreateContainerResponse {
    pub Id: String,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ContainerState {
    pub Status: String,
    pub Running: bool,
    pub Paused: bool,
    pub Restarting: bool,
    pub OOMKilled: bool,
    pub Dead: bool,
    pub Pid: u64,
    pub ExitCode: u64,
    pub Error: String,
    pub StartedAt: String,
    pub FinishedAt: String,
}

/// * To use HostConfig use serde_json
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ContainerDetails {
    pub Id: String,
    pub Created: String,
    pub Path: String,
    pub Platform: Option<String>,
    pub Args: Vec<String>,
    pub State: ContainerState,
    pub Image: String,
    pub ResolvConfPath: String,
    pub Name: String,
    pub HostnamePath: String,
    pub HostsPath: String,
    pub LogPath: String,
    pub RestartCount: u64,
    pub Driver: String,
    pub MountLabel: String,
    pub ProcessLabel: String,
    pub AppArmorProfile: String,
    pub ExecIDs: Option<String>,
    pub HostConfig: serde_json::Value,
    pub Config: ContainerConfig,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ContainerFsChange {
    Path: String,
    Kind: u8,
}

pub trait Containers: DockerApiClient {
    /// Just a helper function for the Containers DockerApiClient.
    /// It formats the API request using the given parameters, and using
    /// this request the docker daemon and sends back the response of the request
    /// if the request was successful else an err.
    fn get_response_from_api(
        &self,
        api_endpoint: &str,
        method: &str,
        body: &str,
    ) -> Result<Response, String> {
        let req = match api_utils::get_formatted_api_request(
            api_endpoint,
            method,
            body,
        ) {
            Some(req) => req,
            None => return Err("Error while preparing request".to_string()),
        };

        match self.request(&req) {
            Some(resp) => match Response::parse_http_response(resp) {
                Ok(response) => Ok(response),
                Err(err) => {
                    Err(format!("Response body was not valid : {}", err))
                }
            },
            None => Err("Got no response from docker host.".to_string()),
        }
    }

    /// Get Containers from the API endpoint with the method and query_param.
    /// Helper function for Container trait.
    fn get_containers(
        &self,
        api_endpoint: &str,
        method: &str,
        query_param: &str,
    ) -> Result<Vec<Container>, String> {
        let json_resp =
            match self.get_response_from_api(api_endpoint, method, query_param)
            {
                Ok(resp) => {
                    if resp.status_code == 200 {
                        resp.body
                    } else {
                        return Err(format!(
                            "Invalid Response : {} :: {}",
                            resp.status_code, resp.body
                        ));
                    }
                }
                Err(err) => return Err(err),
            };

        let containers: Vec<Container> = match serde_json::from_str(&json_resp)
        {
            Ok(info) => info,
            Err(err) => {
                return Err(format!(
                    "Error while deserializing JSON response : {}",
                    err
                ))
            }
        };

        return Ok(containers);
    }

    /// List all the running containers
    /// Return an instance of Vector of container
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate docker_rs;
    ///
    /// use docker_rs::api::containers::Containers;
    /// use docker_rs::client::DockerClient;
    ///
    /// let client = match DockerClient::new("unix:///var/run/docker.sock") {
    ///     Ok(a) => a,
    ///     Err(err) => {
    ///         println!("{}", err);
    ///         std::process::exit(1);
    ///     }
    /// };
    ///
    /// match client.list_running_containers(None) {
    ///     Ok(containers) => println!("{:?}", containers),
    ///     Err(err) => println!("An error occured : {}", err),
    /// }
    /// ```
    fn list_running_containers(
        &self,
        limit: Option<u32>,
    ) -> Result<Vec<Container>, String> {
        let api_endpoint = "/containers/json";
        let method = "GET";

        let query_params = match limit {
            Some(limit) => format!("?size=true&limit={}", limit),
            None => "?size=true".to_string(),
        };

        self.get_containers(api_endpoint, method, &query_params)
    }

    /// List all containers whether running or stopped.
    fn list_all_containers(
        &self,
        limit: Option<u32>,
    ) -> Result<Vec<Container>, String> {
        let api_endpoint = "/containers/json";
        let method = "GET";

        let query_params = match limit {
            Some(limit) => format!("?all=true&size=true&limit={}", limit),
            None => "?all=true&size=true".to_string(),
        };

        self.get_containers(api_endpoint, method, &query_params)
    }

    /// List container with the filter provided, the filter can be looked from
    /// Docker engine official API documentation.
    /// https://docs.docker.com/engine/api/v1.37/#operation/ContainerList
    fn get_container_details_with_filter(
        &self,
        filter: &str,
        limit: Option<u32>,
    ) -> Result<Vec<Container>, String> {
        let api_endpoint = "/containers/json";
        let method = "GET";

        let query_params = match limit {
            Some(limit) => format!(
                "?all=true&size=true&limit={}&filter={}",
                limit, filter
            ),
            None => format!("?all=true&size=true&filter={}", filter),
        };

        self.get_containers(api_endpoint, method, &query_params)
    }

    /// Create a container from the ContainerConfig structure with the provided
    /// `name`. The response for the request is the CreateContaierResponse struct
    /// which contains the ID for the container which we created.
    fn create_container(
        &self,
        name: &str,
        config: ContainerConfig,
    ) -> Result<CreateContainerResponse, String> {
        let api_endpoint = format!("/containers/create?name={}", name);
        let method = "POST";
        let body = match serde_json::to_string(&config) {
            Ok(body) => body,
            Err(err) => {
                return Err(format!(
                    "Error while serialize Cotainer config : {}",
                    err
                ))
            }
        };

        match self.get_response_from_api(&api_endpoint, method, &body) {
            Ok(resp) => {
                if resp.status_code != 201 {
                    return Err(format!(
                        "Invalid Request : {} :: {}",
                        resp.status_code, resp.body
                    ));
                }
                match serde_json::from_str(&resp.body) {
                    Ok(info) => return Ok(info),
                    Err(err) => {
                        return Err(format!(
                            "Error while deserializing JSON response : {}",
                            err
                        ))
                    }
                };
            }
            Err(err) => Err(err),
        }
    }

    /// Creates/Spawn docker container from the configuration provided. It only
    ///
    /// * Rust does not provide named arguments, so we are doing it this way
    /// Currently rust structures does not have default values, so all the
    /// values for the structure needs to be specified.
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate docker_rs;
    ///
    /// use docker_rs::api::containers::Containers;
    /// use docker_rs::client::DockerClient;
    ///
    /// let client = match DockerClient::new("unix:///var/run/docker.sock") {
    ///     Ok(a) => a,
    ///     Err(err) => {
    ///         println!("{}", err);
    ///         std::process::exit(1);
    ///     }
    /// };
    ///
    /// let mut cmd: Vec<String> = Vec::new();
    /// cmd.push("ls".to_string());
    ///
    /// match client.create_container_minimal("my_container", "debian:jessie", cmd) {
    ///     Ok(containers) => println!("{:?}", containers),
    ///     Err(err) => println!("An error occured : {}", err),
    /// }
    /// ```
    fn create_container_minimal(
        &self,
        name: &str,
        image: &str,
        cmd: Vec<String>,
    ) -> Result<CreateContainerResponse, String> {
        let config = ContainerConfig {
            Image: image.to_string(),
            Cmd: cmd,
            ..Default::default()
        };

        self.create_container(name, config)
    }

    /// Inspects the container with the provided ID
    /// Returns Low level information about the container.
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate docker_rs;
    ///
    /// use docker_rs::api::containers::Containers;
    /// use docker_rs::client::DockerClient;
    ///
    /// let client = match DockerClient::new("unix:///var/run/docker.sock") {
    ///     Ok(a) => a,
    ///     Err(err) => {
    ///         println!("{}", err);
    ///         std::process::exit(1);
    ///     }
    /// };
    ///
    /// // ID of the container passed as an argument.
    /// match client.inspect_container("f808ca...") {
    ///     Ok(info) => println!("{:?}", info),
    ///     Err(err) => println!("An error occured : {}", err),
    /// }
    /// ```
    fn inspect_container(&self, id: &str) -> Result<ContainerDetails, String> {
        let api_endpoint = format!("/containers/{id}/json", id = id);
        let method = "GET";

        match self.get_response_from_api(&api_endpoint, method, "") {
            Ok(resp) => {
                if resp.status_code != 200 {
                    return Err(format!(
                        "Invalid Request : {} :: {}",
                        resp.status_code, resp.body
                    ));
                }
                match serde_json::from_str(&resp.body) {
                    Ok(info) => return Ok(info),
                    Err(err) => {
                        return Err(format!(
                            "Error while deserializing JSON response : {}",
                            err
                        ))
                    }
                };
            }
            Err(err) => Err(err),
        }
    }

    /// Gives the changes done to somewhere in the filesystem in the docker container as a list of
    /// files with the kind of changes.
    fn get_container_filesystem_changes(
        &self,
        id: &str,
    ) -> Result<Vec<ContainerFsChange>, String> {
        let api_endpoint = format!("/containers/{id}/changes", id = id);
        let method = "GET";

        match self.get_response_from_api(&api_endpoint, method, "") {
            Ok(resp) => {
                // If the response is null, then there is no changes in the file
                // system so just return and empty vector. Serializing this will
                // result in error.
                if resp.status_code != 200 {
                    return Err(format!(
                        "Invalid Request : {} :: {}",
                        resp.status_code, resp.body
                    ));
                }
                if resp.body == "null" {
                    return Ok(Vec::new());
                }

                match serde_json::from_str(&resp.body) {
                    Ok(info) => return Ok(info),
                    Err(err) => {
                        return Err(format!(
                            "Error while deserializing JSON response : {}",
                            err
                        ))
                    }
                };
            }
            Err(err) => Err(err),
        }
    }

    /// Function to manipulate container status
    /// It is a parent function for all the commands which result in a status change
    /// of the container.
    ///
    /// This includes the following:
    /// * `start_container`
    /// * `stop_container`
    /// * `pause_container`
    /// * `unpause_container`
    /// * `restart_container`
    /// * `kill_container`
    /// * `rename_container`
    ///
    /// You can call any of these function or directly manipulate_container_status
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate docker_rs;
    ///
    /// use docker_rs::api::containers::Containers;
    /// use docker_rs::client::DockerClient;
    ///
    /// let client = match DockerClient::new("unix:///var/run/docker.sock") {
    ///     Ok(a) => a,
    ///     Err(err) => {
    ///         println!("{}", err);
    ///         std::process::exit(1);
    ///     }
    /// };
    ///
    /// // ID of the container passed as an argument.
    /// match client.manipulate_container_status("start", "f808ca...", "") {
    ///     Ok(info) => println!("{:?}", info),
    ///     Err(err) => println!("An error occured : {}", err),
    /// }
    ///
    /// // Or alternatively you can also directly use
    /// match client.start_container("f808ca...") {
    ///     Ok(info) => println!("{}", info),
    ///     Err(err) => println!("An error occured : {}", err),
    /// }
    ///
    /// // Similarly other function can also be used
    /// ```
    fn manipulate_container_status(
        &self,
        action: &str,
        id: &str,
        params: &str,
    ) -> Result<String, String> {
        let api_endpoint = format!(
            "/containers/{id}/{action}",
            id = id,
            action = action
        );
        let method = "GET";

        match self.get_response_from_api(&api_endpoint, method, params) {
            Ok(resp) => {
                if resp.status_code == 204 {
                    Ok(format!("Container {} successful", action))
                } else if resp.status_code == 304 {
                    Err(format!("Container already {}ed", action))
                } else {
                    Err(format!(
                        "Error while requesting Docker API : {} :: {}",
                        resp.status_code, resp.body
                    ))
                }
            }
            Err(err) => return Err(err),
        }
    }

    fn start_container(&self, id: &str) -> Result<String, String> {
        self.manipulate_container_status("start", id, "")
    }

    fn stop_container(
        &self,
        id: &str,
        delay: Option<&str>,
    ) -> Result<String, String> {
        let param = match delay {
            Some(d) => format!("t={}", d),
            None => String::new(),
        };
        self.manipulate_container_status("stop", id, &param)
    }

    fn pause_container(&self, id: &str) -> Result<String, String> {
        self.manipulate_container_status("pause", id, "")
    }

    fn unpause_container(&self, id: &str) -> Result<String, String> {
        self.manipulate_container_status("unpause", id, "")
    }

    fn restart_container(
        &self,
        id: &str,
        delay: Option<&str>,
    ) -> Result<String, String> {
        let param = match delay {
            Some(d) => format!("t={}", d),
            None => String::new(),
        };
        self.manipulate_container_status("restart", id, &param)
    }

    fn kill_container(
        &self,
        id: &str,
        signal: Option<&str>,
    ) -> Result<String, String> {
        let param = match signal {
            Some(sig) => format!("signal={}", sig),
            None => String::new(),
        };
        self.manipulate_container_status("kill", id, &param)
    }

    fn rename_container(&self, id: &str, name: &str) -> Result<String, String> {
        let name_param = &format!("name={}", name);
        self.manipulate_container_status("rename", id, name_param)
    }
}