dsd-util 0.1.0

A simple helper for managing your docker-stack-deploy containers.
Documentation
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
use crate::printer::{color_println, color_println_fmt, Color};
use crate::utils::{
    get_containers_from_stack, get_timestamp, is_terminal, kill_containers, list_containers,
    parse_inspect_data, parse_stats_data, spawn_container_logger, update_container_by_name,
    InspectData, StatsData,
};
use anyhow::Context;
use std::collections::hash_map::HashMap;
use std::io::{self, BufRead, BufReader, Write};
use std::process::{Command, Stdio};

pub const DOCKER: &str = "docker";
const DSD: &str = "docker-stack-deploy";
const PATH_DSD_COMPOSE: &str = "/var/lib/docker-stack-deploy/compose.yml";

/// Initializes a new instance of docker-stack-deploy using bootstrap script
pub fn init(project_dir: String, git_url: String) -> anyhow::Result<()> {
    Command::new(DOCKER)
        .args(["run", "--rm", "-it"])
        .args(["-v", "/var/run/docker.sock:/var/run/docker.sock"])
        .args(["-v", &format!("{project_dir}:{project_dir}")])
        .args(["ghcr.io/wez/docker-stack-deploy"])
        .args([DSD, "bootstrap"])
        .args(["--project-dir", &project_dir])
        .args(["--git-url", &git_url])
        .status()
        .context("Failed to bootstrap docker-stack-deploy")?;

    println!();

    let use_color = is_terminal();

    if use_color {
        color_println(
            Color::Green,
            "Bootstrap success! Following docker-stack-deploy logs...",
        );
    } else {
        println!("Bootstrap success! Following docker-stack-deploy logs...")
    }

    println!();

    let start_time = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .context("Failed to get current time")?
        .as_secs();

    // follow docker-stack-deploy logs until first update check has happened
    let mut logs_process = Command::new(DOCKER)
        .args([
            "compose",
            "-f",
            PATH_DSD_COMPOSE,
            "logs",
            "--follow",
            "--no-log-prefix",
            "--since",
            &start_time.to_string(),
        ])
        .stdout(Stdio::piped())
        .spawn()
        .context("Failed to start following logs")?;

    if let Some(stdout) = logs_process.stdout.take() {
        let reader = BufReader::new(stdout);
        for (i, line) in reader.lines().map_while(Result::ok).enumerate() {
            if use_color {
                println!(
                    "[{} | {}] {}",
                    color_println_fmt(Color::Cyan, &get_timestamp()),
                    color_println_fmt(Color::Magenta, DSD),
                    line
                );
            } else {
                println!("[{} | {}] {}", &get_timestamp(), DSD, line);
            }
            if line.contains("Already up to date") && i > 0 {
                // first update check has happened after deployment
                break;
            }
        }
    }

    let _ = logs_process.kill();
    let _ = logs_process.wait();

    Ok(())
}

/// Shows logs for specified containers
pub fn logs(
    containers: Option<Vec<String>>,
    stacks: Option<Vec<String>>,
    tail: u32,
    all: bool,
) -> anyhow::Result<()> {
    let use_color = is_terminal();

    let containers = if all {
        let container_ids = list_containers()?;

        if container_ids.is_empty() {
            if use_color {
                color_println(Color::Red, "No containers running");
            } else {
                println!("No containers running");
            }
            return Ok(());
        }

        container_ids
    } else if let Some(containers) = containers {
        containers
    } else if let Some(stacks) = stacks {
        let mut containers = vec![];

        for stack in &stacks {
            let container_names = get_containers_from_stack(stack)?;
            containers.extend(container_names);
        }

        containers
    } else {
        anyhow::bail!("Must specify containers, use --stacks (-s) or use --all (-a)")
    };

    if use_color {
        color_println(
            Color::Cyan,
            &format!("Following logs for container: {}", &containers.len()),
        );
    } else {
        println!("Following logs for container: {}", &containers.len());
    }
    let (tx, rx) = std::sync::mpsc::channel::<String>();
    let mut handles: Vec<std::thread::JoinHandle<()>> = vec![];

    for container in containers {
        let tx = tx.clone();
        let is_container_id = all;
        let handle = spawn_container_logger(&container, is_container_id, use_color, tail, tx)
            .with_context(|| format!("Failed to spawn container logger for {container}"))?;
        handles.push(handle);
    }

    drop(tx);

    for log_line in rx {
        println!("{log_line}");
    }

    for handle in handles {
        let _ = handle.join();
    }

    Ok(())
}

/// Kills all running containers, and then redeploys docker-stack-deploy
pub fn nuke() -> anyhow::Result<()> {
    // ask user to confirm action
    color_println(
        Color::Yellow,
        "WARNING: All of your containers will be forcefully removed!",
    );
    println!(
        "After removal, {} will be restarted to redeploy all associated containers.\n",
        color_println_fmt(Color::Magenta, DSD)
    );
    print!("Are you sure you want to nuke your docker stacks? [y/N]: ");
    let _ = io::stdout().flush();

    // capture user input
    let mut input = String::new();
    let _ = io::stdin().read_line(&mut input);
    let response = input.trim().to_lowercase();

    // evaluate response
    match response.as_str() {
        "yes" | "y" => {
            color_println(Color::Yellow, "Nuking docker containers");
        }
        _ => {
            color_println(Color::Green, "Nuke aborted!");
            return Ok(());
        }
    };

    // get list of currently running docker containers by id
    let container_ids = list_containers()?;

    // if docker containers are running, kill them
    if container_ids.is_empty() {
        color_println(Color::Red, "No containers running");
        return Ok(());
    } else {
        kill_containers(container_ids)?
    }

    color_println(Color::Green, "Running docker-stack-deploy...");

    // run docker-stack-deploy
    Command::new(DOCKER)
        .args(["compose", "-f", PATH_DSD_COMPOSE, "up", "-d"])
        .status()
        .context("Failed to start docker-stack-deploy")?;

    color_println(
        Color::Green,
        "Following logs until all containers deployed...",
    );

    let start_time = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .context("Failed to get current time")?
        .as_secs();

    // follow docker-stack-deploy logs until first update check has happened
    let mut logs_process = Command::new(DOCKER)
        .args([
            "compose",
            "-f",
            PATH_DSD_COMPOSE,
            "logs",
            "--follow",
            "--no-log-prefix",
            "--since",
            &start_time.to_string(),
        ])
        .stdout(Stdio::piped())
        .spawn()
        .context("Failed to start following logs")?;

    if let Some(stdout) = logs_process.stdout.take() {
        let reader = BufReader::new(stdout);
        for (i, line) in reader.lines().map_while(Result::ok).enumerate() {
            println!(
                "[{} | {}] {}",
                color_println_fmt(Color::Cyan, &get_timestamp()),
                color_println_fmt(Color::Magenta, DSD),
                line
            );
            if line.contains("Already up to date") && i > 0 {
                // first update check has happened after deployment
                break;
            }
        }
    }

    let _ = logs_process.kill();
    let _ = logs_process.wait();

    Ok(())
}

/// Restarts specified docker containers
pub fn restart(
    containers: Option<Vec<String>>,
    stacks: Option<Vec<String>>,
    all: bool,
) -> anyhow::Result<()> {
    let containers = if all {
        list_containers()?
    } else if let Some(containers) = containers {
        containers
    } else if let Some(stacks) = stacks {
        let mut containers = vec![];

        for stack in &stacks {
            let container_names = get_containers_from_stack(stack)?;
            containers.extend(container_names);
        }

        containers
    } else {
        anyhow::bail!("Must specify containers, use --stacks (-s) or use --all (-a)")
    };

    let use_color = is_terminal();

    for container in &containers {
        if use_color {
            color_println(
                Color::Cyan,
                &format!("Restarting container: {}", &container),
            );
        } else {
            println!("Restarting container: {}", &container)
        }

        Command::new(DOCKER)
            .args(["restart", container])
            .status()
            .context(format!("Failed to restart {}", &container))?;
    }

    Ok(())
}

/// Container stats to be gathered
#[derive(Debug, Clone)]
struct ContainerStats {
    name: String,
    status: String,
    health: String,
    restart_policy: String,
    uptime: String,
    cpu_usage: String,
    memory_usage: String,
    ports: String,
}

/// View stats for docker containers
pub fn stats(
    containers: Option<Vec<String>>,
    stacks: Option<Vec<String>>,
    all: bool,
) -> anyhow::Result<()> {
    let use_color = is_terminal();
    let containers = if all {
        let container_ids = list_containers()?;

        if container_ids.is_empty() {
            if use_color {
                color_println(Color::Red, "No containers running");
            } else {
                println!("No containers running");
            }
            return Ok(());
        }

        container_ids
    } else if let Some(containers) = containers {
        containers
    } else if let Some(stacks) = stacks {
        let mut containers = vec![];

        for stack in &stacks {
            let container_names = get_containers_from_stack(stack)?;
            containers.extend(container_names);
        }

        containers
    } else {
        anyhow::bail!("Must specify containers, use --stacks (-s) or use --all (-a)")
    };

    // stats format from docker cli
    let stats_output = Command::new(DOCKER)
        .args([
            "stats",
            "--no-stream",
            "--format",
            "table {{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}",
        ])
        .args(&containers)
        .output()
        .context("Failed to get stats for containers")?;

    // inspect format from docker cli
    let inspect_format = concat!(
        "{{.Name}},",
        "{{.State.Status}},",
        "{{if .HostConfig.RestartPolicy}}{{if .HostConfig.RestartPolicy.Name}}{{.HostConfig.RestartPolicy.Name}}{{else}}no{{end}}{{else}}no{{end}},",
        "{{if index .State \"Health\"}}{{.State.Health.Status}}{{else}}N/A{{end}},",
        "{{.State.StartedAt}},",
        "{{if .NetworkSettings.Ports}}{{range $key, $value := .NetworkSettings.Ports}}{{$key}}{{if $value}}:{{(index $value 0).HostPort}}{{end}} {{end}}{{else}}N/A{{end}}"
        );

    let inspect_output = Command::new(DOCKER)
        .arg("inspect")
        .args(&containers)
        .args(["--format", inspect_format])
        .output()
        .context("Failed to inspect containers")?;

    let stats_string = String::from_utf8(stats_output.stdout)?;
    let inspect_string = String::from_utf8(inspect_output.stdout)?;

    let mut temp_stats_map: HashMap<String, StatsData> = HashMap::new();
    let mut temp_inspect_map: HashMap<String, InspectData> = HashMap::new();

    // skip header line
    for line in stats_string.lines().skip(1) {
        let parsed = parse_stats_data(line)?;
        temp_stats_map.insert(
            parsed.container_name.clone(),
            StatsData {
                container_name: parsed.container_name,
                cpu: parsed.cpu,
                memory: parsed.memory,
            },
        );
    }

    for line in inspect_string.lines() {
        let parsed = parse_inspect_data(line)?;
        temp_inspect_map.insert(
            parsed.container_name.clone(),
            InspectData {
                container_name: parsed.container_name,
                status: parsed.status,
                restart_policy: parsed.restart_policy,
                health: parsed.health,
                uptime: parsed.uptime,
                ports: parsed.ports,
            },
        );
    }

    assert_eq!(&temp_stats_map.len(), &temp_inspect_map.len());

    let mut total_stats_map: HashMap<String, ContainerStats> = HashMap::new();

    for key in temp_stats_map.keys() {
        let stats = temp_stats_map
            .get(key)
            .with_context(|| format!("Failed to get stats for {key}"))?;
        let inspect = temp_inspect_map
            .get(key)
            .with_context(|| format!("Failed to get stats for {key}"))?;

        let container_stats = if use_color {
            ContainerStats {
                name: color_println_fmt(Color::Cyan, &stats.container_name),
                status: {
                    if &inspect.status.to_lowercase() == "running" {
                        color_println_fmt(Color::Green, &inspect.status)
                    } else if &inspect.status.to_lowercase() == "created" {
                        color_println_fmt(Color::Cyan, &inspect.status)
                    } else if &inspect.status.to_lowercase() == "paused"
                        || &inspect.status.to_lowercase() == "restarting"
                    {
                        color_println_fmt(Color::Yellow, &inspect.status)
                    } else {
                        color_println_fmt(Color::Red, &inspect.status)
                    }
                },
                restart_policy: inspect.restart_policy.to_string(),
                health: {
                    if &inspect.health.to_lowercase() == "healthy" {
                        color_println_fmt(Color::Green, &inspect.health)
                    } else if &inspect.health.to_lowercase() == "unhealthy" {
                        color_println_fmt(Color::Red, &inspect.health)
                    } else if &inspect.health.to_lowercase() == "starting" {
                        color_println_fmt(Color::Cyan, &inspect.health)
                    } else {
                        color_println_fmt(Color::White, &inspect.health)
                    }
                },
                uptime: inspect.uptime.to_string(),
                cpu_usage: stats.cpu.to_string(),
                memory_usage: stats.memory.to_string(),
                ports: inspect.ports.to_string(),
            }
        } else {
            ContainerStats {
                name: stats.container_name.to_string(),
                status: inspect.status.to_string(),
                restart_policy: inspect.restart_policy.to_string(),
                health: inspect.health.to_string(),
                uptime: inspect.uptime.to_string(),
                cpu_usage: stats.cpu.to_string(),
                memory_usage: stats.memory.to_string(),
                ports: inspect.ports.to_string(),
            }
        };

        total_stats_map.insert(key.to_string(), container_stats);
    }
    if use_color {
        println!(
            "{:<35} {:<20} {:<16} {:<20} {:<18} {:<8} {:<8} {:<20}",
            &color_println_fmt(Color::White, "NAME"),
            &color_println_fmt(Color::White, "STATUS"),
            "RESTART",
            &color_println_fmt(Color::White, "HEALTH"),
            "UPTIME",
            "CPU %",
            "MEM %",
            "PORTS"
        );
    } else {
        println!(
            "{:<35} {:<20} {:<16} {:<20} {:<18} {:<8} {:<8} {:<20}",
            "NAME", "STATUS", "RESTART", "HEALTH", "UPTIME", "CPU %", "MEM %", "PORTS"
        );
    }

    println!();

    // TODO: sort - probably want to use BTreeMap instead
    for key in total_stats_map.keys() {
        let container = total_stats_map.get(key).context("Failed to get item")?;

        println!(
            "{:<35} {:<20} {:<16} {:<20} {:<18} {:<8} {:<8} {:<20}",
            container.name,
            container.status,
            container.restart_policy,
            container.health,
            container.uptime,
            container.cpu_usage,
            container.memory_usage,
            container.ports
        );
    }

    Ok(())
}

/// Updates images of specified docker containers
pub fn update(
    containers: Option<Vec<String>>,
    stacks: Option<Vec<String>>,
    all: bool,
) -> anyhow::Result<()> {
    let containers = if all {
        list_containers()?
    } else if let Some(containers) = containers {
        containers
    } else if let Some(stacks) = stacks {
        let mut containers = vec![];

        for stack in &stacks {
            let container_names = get_containers_from_stack(stack)?;
            containers.extend(container_names);
        }

        containers
    } else {
        anyhow::bail!("Must specify containers, use --stacks (-s) or use --all (-a)")
    };

    let use_color = is_terminal();

    let mut num_containers_updated = 0;

    for container in &containers {
        num_containers_updated += update_container_by_name(container)?;
    }

    if num_containers_updated == 0 {
        if use_color {
            color_println(Color::Yellow, "No new container images to update");
        } else {
            println!("No new container images to pull");
        }

        return Ok(());
    }

    if use_color {
        println!(
            "{}: {}",
            &color_println_fmt(Color::Cyan, "New images pulled"),
            &color_println_fmt(Color::Green, &num_containers_updated.to_string())
        );
        println!();
        color_println(Color::Green, &format!("Restarting {DSD}"));
    } else {
        println!("New images pulled: {num_containers_updated}");
        println!();
        println!("Restarting {DSD}");
    }

    // containers updated, restart docker-stack-deploy to deploy new image
    Command::new(DOCKER)
        .args(["restart", DSD])
        .status()
        .context(format!("Failed to restart {DSD}"))?;

    Ok(())
}