devsandbox 0.7.0

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
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
//! Docker-CLI-compatible runtimes: `docker` and `podman`. Same subcommands and
//! flags, Go-template `--format`, `--filter`, `network connect`,
//! `--network-alias`, `top`. Podman's `ps --format {{json .}}` differs slightly
//! in field types (arrays/maps where docker emits strings); the parsers accept
//! both.

use std::collections::BTreeMap;

use anyhow::{Context, Result};
use serde::Deserialize;

use super::{
    split_kv, without_self_ps, Backend, ContainerRow, ProcList, ServiceEndpoint, StatsRow,
    PROC_PS_ARGS,
};

pub struct Dockerlike {
    name: &'static str,
    bin: &'static str,
}

impl Dockerlike {
    pub const DOCKER: Dockerlike = Dockerlike { name: "docker", bin: "docker" };
    pub const PODMAN: Dockerlike = Dockerlike { name: "podman", bin: "podman" };

    /// `inspect -f <template> <name>`, `None` when the object does not exist.
    fn inspect_template(&self, name: &str, template: &str) -> Result<Option<String>> {
        let out = std::process::Command::new(self.bin)
            .args(["inspect", "-f", template, name])
            .output()
            .map_err(|e| anyhow::anyhow!("failed to run {} (is it installed?): {e}", self.bin))?;
        if !out.status.success() {
            return Ok(None);
        }
        Ok(Some(String::from_utf8_lossy(&out.stdout).trim().to_string()))
    }
}

impl Backend for Dockerlike {
    fn name(&self) -> &'static str {
        self.name
    }

    fn bin(&self) -> &'static str {
        self.bin
    }

    fn is_running(&self, container: &str) -> Result<Option<bool>> {
        Ok(self
            .inspect_template(container, "{{.State.Running}}")?
            .map(|v| v == "true"))
    }

    fn label(&self, container: &str, key: &str) -> Result<Option<String>> {
        let template = format!("{{{{index .Config.Labels \"{key}\"}}}}");
        Ok(self
            .inspect_template(container, &template)?
            .filter(|v| !v.is_empty()))
    }

    fn network_exists(&self, network: &str) -> Result<bool> {
        Ok(self.inspect_template(network, "{{.Id}}")?.is_some())
    }

    fn inspect_json(&self, container: &str) -> Result<String> {
        self.output_quiet(&["inspect", container])
    }

    fn list(&self, all: bool, name_prefix: &str) -> Result<Vec<ContainerRow>> {
        let mut args = vec!["ps"];
        if all {
            args.push("--all");
        }
        // docker matches `name=` as a regex against `/name`, podman against the
        // bare name; a plain substring works for both and the prefix is
        // enforced below.
        let filter = format!("name={name_prefix}");
        if !name_prefix.is_empty() {
            args.extend(["--filter", &filter]);
        }
        args.extend(["--format", PS_FORMAT]);
        let out = self.output_quiet(&args)?;
        Ok(parse_ps(&out)
            .into_iter()
            .filter(|row| row.name.starts_with(name_prefix))
            .collect())
    }

    fn stats(&self) -> Result<Vec<StatsRow>> {
        let out = self.output_quiet(&["stats", "--no-stream", "--format", "{{json .}}"])?;
        Ok(parse_stats(&out))
    }

    fn server_version(&self) -> Result<String> {
        self.output_quiet(&["version", "--format", "{{.Server.Version}}"])
    }

    fn logs_tail(&self, container: &str, n: usize) -> Result<String> {
        self.output_merged(&["logs", "--tail", &n.to_string(), container])
    }

    fn proc_list(&self, container: &str) -> Result<ProcList> {
        // Prefer container-namespace pids via `exec ps` so process-row signals
        // can target the shown pid (`top` reports host-namespace pids, which
        // `exec kill` inside the container can't reach). Fall back to `top` for
        // images without `ps`: those still list, but as non-signalable host pids.
        let mut exec = vec!["exec", container];
        exec.extend_from_slice(&PROC_PS_ARGS);
        match self.output_quiet(&exec) {
            Ok(text) => Ok(ProcList { text: without_self_ps(&text), container_pids: true }),
            Err(_) => {
                let text = self.output_quiet(&["top", container, "-eo", "pid,ppid,args"])?;
                Ok(ProcList { text, container_pids: false })
            }
        }
    }

    fn remove_force(&self, container: &str) -> Result<i32> {
        self.run_inherit(&["rm", "-f", container])
    }

    fn network_run_args(&self, networks: &[String]) -> Vec<String> {
        match networks.first() {
            Some(first) => vec!["--network".into(), first.clone()],
            None => Vec::new(),
        }
    }

    fn connect_networks(&self, container: &str, networks: &[String]) -> Result<()> {
        for network in networks.iter().skip(1) {
            self.run_checked(&["network", "connect", network, container])?;
        }
        Ok(())
    }

    fn service_alias_args(&self, alias: &str) -> Vec<String> {
        vec!["--network-alias".into(), alias.to_string()]
    }

    fn wire_service_dns(&self, _container: &str, _endpoints: &[ServiceEndpoint]) -> Result<()> {
        Ok(())
    }

    fn supports_cache_from(&self) -> bool {
        true
    }

    fn supports_file_binds(&self) -> bool {
        true
    }

    fn supports_privileged(&self) -> bool {
        true
    }

    fn supports_restart_policy(&self) -> bool {
        true
    }

    fn restart_policy(&self, container: &str) -> Result<Option<String>> {
        self.inspect_template(container, "{{.HostConfig.RestartPolicy.Name}}")
    }

    fn set_restart_policy(&self, container: &str, policy: &str) -> Result<()> {
        self.output_quiet(&["update", "--restart", policy, container])?;
        Ok(())
    }

    fn image_entrypoint(&self, image: &str) -> Result<Vec<String>> {
        let out = self.output_quiet(&[
            "image",
            "inspect",
            "--format",
            "{{json .Config.Entrypoint}}",
            image,
        ])?;
        parse_entrypoint(&out)
    }

    fn remove_volume(&self, name: &str) -> Result<bool> {
        match self.output_quiet(&["volume", "rm", name]) {
            Ok(_) => Ok(true),
            Err(e) if super::is_missing_volume(&e.to_string()) => Ok(false),
            Err(e) => Err(e),
        }
    }
}

/// `{{json .Config.Entrypoint}}`: `null` (unset) or a string array.
fn parse_entrypoint(out: &str) -> Result<Vec<String>> {
    let value: Option<Vec<String>> = serde_json::from_str(out.trim())
        .with_context(|| format!("unexpected image entrypoint `{}`", out.trim()))?;
    Ok(value.unwrap_or_default())
}

/// A string, or a list of strings (podman emits `Names` as an array).
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum StringOrList {
    One(String),
    Many(Vec<String>),
}

impl StringOrList {
    fn first(self) -> String {
        match self {
            Self::One(s) => s,
            Self::Many(v) => v.into_iter().next().unwrap_or_default(),
        }
    }
}

/// `k=v,k2=v2` (docker) or a JSON object (podman).
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum LabelsField {
    Csv(String),
    Map(BTreeMap<String, String>),
}

impl LabelsField {
    fn into_map(self) -> BTreeMap<String, String> {
        match self {
            Self::Map(m) => m,
            Self::Csv(s) => s
                .split(',')
                .filter(|p| !p.trim().is_empty())
                .map(split_kv)
                .collect(),
        }
    }
}

/// One line of `ps --format {{json .}}`.
#[derive(Debug, Deserialize)]
struct PsLine {
    #[serde(rename = "Names")]
    names: StringOrList,
    #[serde(rename = "Image", default)]
    image: String,
    #[serde(rename = "Status", default)]
    status: String,
    #[serde(rename = "State", default)]
    state: String,
    #[serde(rename = "Labels", default)]
    labels: Option<LabelsField>,
    #[serde(rename = "Ports", default)]
    ports: Option<serde_json::Value>,
}

/// `ps --format` template naming only the [`PsLine`] fields. Never `{{json .}}`:
/// a template that touches `.Size` (as `{{json .}}` does) makes the docker CLI
/// request `size=1`, so the daemon computes every listed container's disk usage
/// on each call (~300ms and a dockerd CPU spike per TUI tick, vs ~20ms).
const PS_FORMAT: &str = concat!(
    r#"{"Names":{{json .Names}},"Image":{{json .Image}},"Status":{{json .Status}},"#,
    r#""State":{{json .State}},"Labels":{{json .Labels}},"Ports":{{json .Ports}}}"#,
);

/// Parse `ps --format` [`PS_FORMAT`] output (one JSON object per line).
/// Unparseable lines are skipped rather than failing the whole listing.
fn parse_ps(out: &str) -> Vec<ContainerRow> {
    out.lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| serde_json::from_str::<PsLine>(l).ok())
        .map(|line| ContainerRow {
            name: line.names.first(),
            image: line.image,
            status: line.status,
            state: line.state,
            labels: line.labels.map(LabelsField::into_map).unwrap_or_default(),
            host_ports: host_ports(line.ports.as_ref()),
        })
        .collect()
}

/// Host ports out of docker's `Ports` string (`0.0.0.0:5432->5432/tcp, …`) or
/// podman's list of `{host_port, …}` objects.
fn host_ports(ports: Option<&serde_json::Value>) -> Vec<String> {
    let mut out = Vec::new();
    match ports {
        Some(serde_json::Value::String(s)) => {
            for part in s.split(',') {
                let Some((host, _)) = part.trim().split_once("->") else { continue };
                if let Some(port) = host.rsplit(':').next()
                    && !port.is_empty()
                {
                    out.push(port.to_string());
                }
            }
        }
        Some(serde_json::Value::Array(items)) => {
            for item in items {
                if let Some(port) = item.get("host_port").and_then(|p| p.as_u64()) {
                    out.push(port.to_string());
                }
            }
        }
        _ => {}
    }
    out.sort();
    out.dedup();
    out
}

/// One line of `stats --no-stream --format {{json .}}`.
#[derive(Debug, Deserialize)]
struct StatsLine {
    #[serde(rename = "Name")]
    name: String,
    #[serde(rename = "CPUPerc")]
    cpu: String,
    #[serde(rename = "MemUsage")]
    mem: String,
}

fn parse_stats(out: &str) -> Vec<StatsRow> {
    out.lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| serde_json::from_str::<StatsLine>(l).ok())
        .map(|s| StatsRow { name: s.name, cpu: s.cpu, mem: s.mem })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Every field `PsLine` reads, and nothing that triggers `size=1`.
    #[test]
    fn ps_format_names_fields_without_size() {
        for field in ["Names", "Image", "Status", "State", "Labels", "Ports"] {
            assert!(PS_FORMAT.contains(&format!("\"{field}\":{{{{json .{field}}}}}")), "{field}");
        }
        assert!(!PS_FORMAT.contains("Size") && !PS_FORMAT.contains("json .}}"));
    }

    #[test]
    fn parse_ps_docker_shape() {
        let out = concat!(
            r#"{"Names":"devsandbox-repo","Image":"node:22","Status":"Up 3 minutes","State":"running","Labels":"devsandbox.sandbox=repo,devsandbox.instance=repo","Ports":"0.0.0.0:5432->5432/tcp, :::5432->5432/tcp"}"#,
            "\n",
            r#"{"Names":"devsandbox-x","Image":"alpine","Status":"Exited (0) 1 hour ago","State":"exited","Labels":"","Ports":""}"#,
            "\n",
            "not json\n",
        );
        let rows = parse_ps(out);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].name, "devsandbox-repo");
        assert!(rows[0].is_running());
        assert_eq!(rows[0].label("devsandbox.sandbox"), Some("repo"));
        assert_eq!(rows[0].host_ports, vec!["5432"]);
        assert!(!rows[1].is_running());
        assert!(rows[1].labels.is_empty());
    }

    #[test]
    fn parse_ps_podman_shape() {
        let out = r#"{"Names":["devsandbox-repo"],"Image":"node:22","Status":"Up 5 seconds ago","State":"running","Labels":{"devsandbox.sandbox":"repo"},"Ports":[{"host_ip":"","container_port":80,"host_port":8080,"range":1,"protocol":"tcp"}]}"#;
        let rows = parse_ps(out);
        assert_eq!(rows[0].name, "devsandbox-repo");
        assert_eq!(rows[0].label("devsandbox.sandbox"), Some("repo"));
        assert_eq!(rows[0].host_ports, vec!["8080"]);
    }

    #[test]
    fn parse_stats_maps_fields() {
        let out = concat!(
            r#"{"Name":"devsandbox-repo","CPUPerc":"1.20%","MemUsage":"50MiB / 2GiB"}"#,
            "\n",
            "garbage\n",
        );
        let stats = parse_stats(out);
        assert_eq!(
            stats,
            vec![StatsRow {
                name: "devsandbox-repo".into(),
                cpu: "1.20%".into(),
                mem: "50MiB / 2GiB".into(),
            }]
        );
    }

    #[test]
    fn parse_entrypoint_null_or_array() {
        assert!(parse_entrypoint("null\n").unwrap().is_empty());
        assert!(parse_entrypoint("[]").unwrap().is_empty());
        assert_eq!(
            parse_entrypoint(r#"["/usr/bin/tini","--"]"#).unwrap(),
            ["/usr/bin/tini", "--"]
        );
        assert!(parse_entrypoint("garbage").is_err());
    }

    /// Docker-gated: a removed volume reports `true`, a second removal hits the
    /// real "no such volume" error and reports `false` instead of failing.
    #[test_utils::docker_test]
    fn remove_volume_reports_missing() -> Result<(), &'static str> {
        let name = format!(
            "devsandbox-vol-test-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        );
        Dockerlike::DOCKER.run_checked(&["volume", "create", &name]).map_err(|_| "volume create failed")?;
        assert!(Dockerlike::DOCKER.remove_volume(&name).unwrap());
        assert!(!Dockerlike::DOCKER.remove_volume(&name).unwrap());
        Ok(())
    }

    #[test]
    fn network_args_first_at_run_rest_connected() {
        let nets = vec!["a".to_string(), "b".to_string()];
        assert_eq!(Dockerlike::DOCKER.network_run_args(&nets), vec!["--network", "a"]);
        assert!(Dockerlike::DOCKER.network_run_args(&[]).is_empty());
    }
}