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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use crate::api::{ConfigMap, Driver, DriverData, Labels, NetworkSettings, Options, PublishPort};
use serde::{de, Deserialize, Serialize};
use std::{
collections::HashMap,
str::{self, FromStr},
};
#[cfg(feature = "chrono")]
use crate::util::datetime::datetime_from_unix_timestamp;
#[cfg(feature = "chrono")]
use chrono::{DateTime, Utc};
use serde::ser::SerializeMap;
use serde_json::Value;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContainerInfo {
#[cfg(feature = "chrono")]
#[serde(deserialize_with = "datetime_from_unix_timestamp")]
pub created: DateTime<Utc>,
#[cfg(not(feature = "chrono"))]
pub created: u64,
pub command: String,
pub id: String,
pub image: String,
#[serde(rename = "ImageID")]
pub image_id: String,
pub labels: Labels,
pub names: Vec<String>,
pub ports: Vec<Port>,
pub state: String,
pub status: String,
pub size_rw: Option<i64>,
pub size_root_fs: Option<i64>,
pub mounts: Vec<Mount>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContainerDetails {
pub id: String,
#[cfg(feature = "chrono")]
pub created: DateTime<Utc>,
#[cfg(not(feature = "chrono"))]
pub created: String,
pub path: String,
pub args: Vec<String>,
pub state: ContainerState,
pub image: String,
pub resolv_conf_path: String,
pub hostname_path: String,
pub hosts_path: String,
pub log_path: String,
pub name: String,
pub restart_count: i64,
pub driver: String,
pub platform: String,
pub mount_label: String,
pub process_label: String,
pub app_armor_profile: String,
#[serde(rename = "ExecIDs")]
pub exec_ids: Option<Vec<String>>,
pub host_config: HostConfig,
pub graph_driver: DriverData,
pub size_rw: Option<i64>,
pub size_root_fs: Option<i64>,
pub mounts: Vec<MountPoint>,
pub config: ContainerConfig,
pub network_settings: NetworkSettings,
}
fn deserialize_exposed_ports<'de, D>(deserializer: D) -> Result<Vec<PublishPort>, D::Error>
where
D: serde::Deserializer<'de>,
{
let port_map = HashMap::<String, serde_json::Value>::deserialize(deserializer)?;
let mut ports = Vec::new();
for (port, _) in port_map {
ports.push(PublishPort::from_str(port.as_str()).map_err(serde::de::Error::custom)?);
}
Ok(ports)
}
fn serialize_exposed_ports<S>(ports: &[PublishPort], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut map = serializer.serialize_map(Some(ports.len()))?;
for port in ports {
map.serialize_entry(
&port.to_string(),
&serde_json::Value::Object(serde_json::Map::new()),
)?;
}
map.end()
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContainerConfig {
pub hostname: String,
pub domainname: String,
pub user: String,
pub attach_stdin: bool,
pub attach_stdout: bool,
pub attach_stderr: bool,
#[serde(deserialize_with = "deserialize_exposed_ports")]
#[serde(serialize_with = "serialize_exposed_ports")]
#[serde(default)]
pub exposed_ports: Vec<PublishPort>,
pub tty: bool,
pub open_stdin: bool,
pub stdin_once: bool,
pub env: Vec<String>,
pub cmd: Option<Vec<String>>,
pub healthcheck: Option<HealthConfig>,
pub args_escaped: Option<bool>,
pub image: String,
pub volumes: Option<VolumesMap>,
pub working_dir: String,
pub entrypoint: Option<Vec<String>>,
pub network_disabled: Option<bool>,
pub mac_address: Option<String>,
pub on_build: Option<Vec<String>>,
pub labels: Option<Labels>,
pub stop_signal: Option<String>,
pub stop_timeout: Option<isize>,
pub shell: Option<Vec<String>>,
}
pub type VolumesMap = HashMap<String, Value>;
impl ContainerConfig {
pub fn env(&self) -> HashMap<String, String> {
let mut map = HashMap::new();
for e in &self.env {
let pair: Vec<&str> = e.split('=').collect();
map.insert(pair[0].to_owned(), pair[1].to_owned());
}
map
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MountType {
Bind,
Volume,
TmpFs,
NPipe,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MountConsistency {
Default,
Consistent,
Cached,
Delegated,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Mount {
#[serde(rename = "Type")]
pub type_: Option<MountType>,
pub source: Option<String>,
pub target: Option<String>,
pub read_only: Option<bool>,
pub consistency: Option<MountConsistency>,
pub bind_options: Option<BindOptions>,
pub volume_options: Option<VolumeOptions>,
pub tmpfs_options: Option<TmpfsOptions>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct TmpfsOptions {
pub size_bytes: Option<i64>,
pub mode: Option<isize>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct VolumeOptions {
pub no_copy: Option<bool>,
pub labels: Option<Labels>,
pub driver_config: Option<Driver>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BindPropagation {
Private,
RPrivate,
Shared,
RShared,
Slave,
RSlave,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct BindOptions {
pub propagation: Option<BindPropagation>,
pub non_recursive: Option<bool>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct MountPoint {
#[serde(rename = "Type")]
pub type_: Option<String>,
pub name: Option<String>,
pub source: String,
pub destination: String,
pub driver: Option<String>,
pub mode: String,
#[serde(rename = "RW")]
pub rw: bool,
pub propagation: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ContainerStatus {
Created,
Restarting,
Running,
Removing,
Paused,
Exited,
Dead,
}
impl AsRef<str> for ContainerStatus {
fn as_ref(&self) -> &str {
use ContainerStatus::*;
match &self {
Created => "created",
Restarting => "restarting",
Running => "running",
Removing => "removing",
Paused => "paused",
Exited => "exited",
Dead => "dead",
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContainerState {
pub status: ContainerStatus,
pub running: bool,
pub paused: bool,
pub restarting: bool,
#[serde(rename = "OOMKilled")]
pub oom_killed: bool,
pub dead: bool,
pub pid: u64,
pub exit_code: u64,
pub error: String,
#[cfg(feature = "chrono")]
pub started_at: DateTime<Utc>,
#[cfg(not(feature = "chrono"))]
pub started_at: String,
#[cfg(feature = "chrono")]
pub finished_at: DateTime<Utc>,
#[cfg(not(feature = "chrono"))]
pub finished_at: String,
pub health: Option<ContainerHealth>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContainerHealth {
pub status: HealthStatus,
pub failing_streak: isize,
pub log: Vec<HealthcheckResult>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct HealthConfig {
pub test: Option<Vec<String>>,
pub interval: Option<isize>,
pub timeout: Option<isize>,
pub retries: Option<isize>,
pub start_period: Option<isize>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HealthStatus {
None,
Starting,
Healthy,
Unhealthy,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct HealthcheckResult {
#[cfg(feature = "chrono")]
pub start: DateTime<Utc>,
#[cfg(not(feature = "chrono"))]
pub start: String,
#[cfg(feature = "chrono")]
pub end: DateTime<Utc>,
#[cfg(not(feature = "chrono"))]
pub end: String,
pub exit_code: isize,
pub output: String,
}
pub type Sysctls = HashMap<String, String>;
pub type Tmpfs = HashMap<String, String>;
pub type StorageOpt = HashMap<String, String>;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct HostConfig {
pub cpu_shares: Option<i64>,
pub memory: Option<i64>,
pub cgroup_parent: Option<String>,
pub blkio_weight: u16,
pub blkio_weight_device: Option<Vec<ThrottleWeightDevice>>,
pub blkio_device_read_bps: Option<Vec<ThrottleDevice>>,
pub blkio_device_write_bps: Option<Vec<ThrottleDevice>>,
#[serde(rename = "BlkioDeviceReadIOps")]
pub blkio_device_read_iops: Option<Vec<ThrottleDevice>>,
#[serde(rename = "BlkioDeviceWriteIOps")]
pub blkio_device_write_iops: Option<Vec<ThrottleDevice>>,
pub cpu_period: Option<i64>,
pub cpu_quota: Option<i64>,
pub cpu_realtime_period: Option<i64>,
pub cpu_realtime_runtime: Option<i64>,
pub cpuset_cpus: Option<String>,
pub cpuset_mems: Option<String>,
pub devices: Option<Vec<DeviceMapping>>,
pub device_cgroup_rules: Option<Vec<String>>,
pub device_requests: Option<Vec<DeviceRequest>>,
pub kernel_memory: i64,
#[serde(rename = "KernelMemoryTCP")]
pub kernel_memory_tcp: i64,
pub memory_reservation: Option<i64>,
pub memory_swap: Option<i64>,
pub memory_swappiness: Option<i64>,
#[serde(rename = "NanoCPUs")]
pub nano_cpus: Option<i64>,
pub oom_kill_disable: Option<bool>,
pub init: Option<bool>,
pub pids_limit: Option<i64>,
pub ulimits: Option<Vec<Ulimit>>,
pub cpu_count: i64,
pub cpu_percent: i64,
#[serde(rename = "IOMaximumIOps")]
pub io_maximum_iops: u64,
#[serde(rename = "IOMaximumBandwith")]
pub io_maximum_bandwith: Option<u64>,
pub binds: Option<Vec<String>>,
#[serde(rename = "ContainerIDFile")]
pub container_id_file: String,
pub log_config: LogConfig,
pub network_mode: String,
pub port_bindings: Option<PortMap>,
pub restart_policy: RestartPolicy,
pub auto_remove: bool,
pub volume_driver: String,
pub volumes_from: Option<Vec<String>>,
pub mounts: Option<Vec<MountPoint>>,
pub cap_add: Option<Vec<String>>,
pub cap_drop: Option<Vec<String>>,
pub dns: Option<Vec<String>>,
pub dns_options: Option<Vec<String>>,
pub dns_search: Option<Vec<String>>,
pub extra_hosts: Option<Vec<String>>,
pub group_add: Option<Vec<String>>,
pub ipc_mode: String,
pub cgroup: String,
pub links: Option<Vec<String>>,
pub oom_score_adj: i64,
pub pid_mode: Option<String>,
pub privileged: bool,
pub publish_all_ports: bool,
pub readonly_rootfs: Option<bool>,
pub security_opt: Option<Vec<String>>,
pub storage_opt: Option<StorageOpt>,
pub tmpfs: Option<Tmpfs>,
#[serde(rename = "UTSMode")]
pub uts_mode: String,
pub userns_mode: String,
pub shm_size: u64,
pub sysctls: Option<Sysctls>,
pub runtime: Option<String>,
pub console_size: Option<Vec<u64>>,
pub isolation: String,
pub masked_paths: Option<Vec<String>>,
pub readonly_paths: Option<Vec<String>>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ThrottleDevice {
pub path: String,
pub rate: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ThrottleWeightDevice {
pub path: String,
pub weight: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct RestartPolicy {
pub name: String,
pub maximum_retry_count: u64,
}
pub type PortMap = HashMap<String, Option<Vec<PortBinding>>>;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct PortBinding {
pub host_ip: String,
pub host_port: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LogConfig {
#[serde(rename = "Type")]
pub type_: String,
#[serde(rename = "Config")]
pub config: ConfigMap,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Ulimit {
pub name: String,
pub soft: isize,
pub hard: isize,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DeviceMapping {
pub path_on_host: String,
pub path_in_container: String,
pub cgroup_permissions: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct DeviceRequest {
pub driver: String,
pub count: u64,
#[serde(rename = "DeviceIDs")]
pub device_ids: Vec<String>,
pub capabilities: Vec<String>,
pub options: Options,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Port {
pub ip: Option<String>,
pub private_port: u64,
pub public_port: Option<u64>,
#[serde(rename = "Type")]
pub typ: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Stats {
pub read: String,
pub num_procs: u32,
pub memory_stats: Option<MemoryStats>,
pub blkio_stats: Option<BlkioStats>,
pub cpu_stats: Option<CpuStats>,
pub precpu_stats: Option<CpuStats>,
pub pids_stats: Option<PidsStats>,
pub storage_stats: Option<StorageStats>,
pub networks: Option<HashMap<String, NetworkStats>>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StorageStats {
pub read_count_normalized: Option<u64>,
pub read_size_bytes: Option<u64>,
pub write_count_normalized: Option<u64>,
pub write_size_bytes: Option<u64>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PidsStats {
pub current: Option<u64>,
pub limit: Option<u64>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NetworkStats {
pub rx_dropped: u64,
pub rx_bytes: u64,
pub rx_errors: u64,
pub tx_packets: u64,
pub tx_dropped: u64,
pub rx_packets: u64,
pub tx_errors: u64,
pub tx_bytes: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MemoryStats {
pub max_usage: Option<u64>,
pub usage: Option<u64>,
pub failcnt: Option<u64>,
pub limit: Option<u64>,
pub stats: Option<MemoryStat>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MemoryStat {
pub total_pgmajfault: Option<u64>,
pub cache: Option<u64>,
pub mapped_file: Option<u64>,
pub total_inactive_file: Option<u64>,
pub pgpgout: Option<u64>,
pub rss: Option<u64>,
pub total_mapped_file: Option<u64>,
pub writeback: Option<u64>,
pub unevictable: Option<u64>,
pub pgpgin: Option<u64>,
pub total_unevictable: Option<u64>,
pub pgmajfault: Option<u64>,
pub total_rss: Option<u64>,
pub total_rss_huge: Option<u64>,
pub total_writeback: Option<u64>,
pub total_inactive_anon: Option<u64>,
pub rss_huge: Option<u64>,
pub hierarchical_memory_limit: Option<u64>,
pub hierarchical_memsw_limit: Option<u64>,
pub total_pgfault: Option<u64>,
pub total_active_file: Option<u64>,
pub active_anon: Option<u64>,
pub total_active_anon: Option<u64>,
pub total_pgpgout: Option<u64>,
pub total_cache: Option<u64>,
pub inactive_anon: Option<u64>,
pub active_file: Option<u64>,
pub pgfault: Option<u64>,
pub inactive_file: Option<u64>,
pub total_pgpgin: Option<u64>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CpuStats {
pub cpu_usage: CpuUsage,
pub system_cpu_usage: Option<u64>,
pub throttling_data: Option<ThrottlingData>,
pub online_cpus: Option<u32>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CpuUsage {
pub percpu_usage: Option<Vec<u64>>,
pub usage_in_usermode: u64,
pub total_usage: u64,
pub usage_in_kernelmode: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ThrottlingData {
pub periods: u64,
pub throttled_periods: u64,
pub throttled_time: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BlkioStats {
pub io_service_bytes_recursive: Option<Vec<BlkioStat>>,
pub io_serviced_recursive: Option<Vec<BlkioStat>>,
pub io_queue_recursive: Option<Vec<BlkioStat>>,
pub io_service_time_recursive: Option<Vec<BlkioStat>>,
pub io_wait_time_recursive: Option<Vec<BlkioStat>>,
pub io_merged_recursive: Option<Vec<BlkioStat>>,
pub io_time_recursive: Option<Vec<BlkioStat>>,
pub sectors_recursive: Option<Vec<BlkioStat>>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BlkioStat {
pub major: u64,
pub minor: u64,
pub op: String,
pub value: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, PartialOrd)]
pub enum ChangeKind {
Modified,
Added,
Deleted,
}
fn deserialize_change_kind<'de, D>(deserializer: D) -> Result<ChangeKind, D::Error>
where
D: de::Deserializer<'de>,
{
use de::Error;
let num: u8 = de::Deserialize::deserialize(deserializer)?;
match num {
0 => Ok(ChangeKind::Modified),
1 => Ok(ChangeKind::Added),
2 => Ok(ChangeKind::Deleted),
_ => Err(D::Error::custom("invalid change kind")),
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Change {
#[serde(deserialize_with = "deserialize_change_kind")]
pub kind: ChangeKind,
pub path: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Top {
pub titles: Vec<String>,
pub processes: Vec<Vec<String>>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContainerCreateInfo {
pub id: String,
pub warnings: Option<Vec<String>>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Exit {
pub status_code: i64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ContainersPruneInfo {
pub containers_deleted: Vec<String>,
pub space_reclaimed: u64,
}