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
#![allow(unused_imports)]
mod cli;
pub(crate) use clap::{Parser, Subcommand};
pub(crate) use log::error;
pub(crate) use std::fmt;
use std::{fmt::Display, str::FromStr};
// ---------------------------------------------------------------------------
// Output format enum (shared by all list commands)
// ---------------------------------------------------------------------------
#[derive(Clone, Debug, PartialEq, Eq)]
enum OutputFormat {
Table = 0,
Json = 1,
}
impl FromStr for OutputFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"table" => Ok(OutputFormat::Table),
"json" => Ok(OutputFormat::Json),
other => Err(format!(
"unknown format '{}': expected 'table' or 'json'",
other
)),
}
}
}
impl Display for OutputFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
OutputFormat::Table => {
write!(f, "table")
}
OutputFormat::Json => {
write!(f, "json")
}
}
}
}
// ---------------------------------------------------------------------------
// CLI structure
// ---------------------------------------------------------------------------
#[derive(Parser, Debug)]
#[clap(
author,
version = concat!(env!("CARGO_PKG_VERSION"), "+", env!("GIT_HASH")),
about = "Pelagos container runtime",
long_about = None,
)]
struct Cli {
#[clap(subcommand)]
command: CliCommand,
}
#[derive(Subcommand, Debug)]
pub(crate) enum CliCommand {
// ── Container lifecycle ────────────────────────────────────────────────
/// Create and start a container
Run(Box<cli::run::RunArgs>),
/// Run a command in a running container
Exec(cli::exec::ExecArgs),
/// List containers
Ps {
/// Show all containers (default: only running)
#[clap(long, short = 'a')]
all: bool,
/// Output format: table or json
#[clap(long, default_value = "table")]
format: OutputFormat,
/// Shorthand for --format json; takes precedence if both are given
#[clap(long)]
json: bool,
/// Filter containers (e.g. label=env=staging, label=managed, status=running)
#[clap(long = "filter")]
filter: Vec<String>,
},
/// Send SIGTERM to a running container
Stop {
/// Container name
name: String,
},
/// Stop then start a container (running or exited)
Restart {
/// Container name
name: String,
/// Seconds to wait for clean exit before sending SIGKILL (default: 10)
#[clap(long, short = 't', default_value = "10")]
time: u64,
},
/// Remove a container
Rm {
/// Container name
name: String,
/// Kill and remove even if running
#[clap(long, short = 'f')]
force: bool,
},
/// Print container logs
Logs {
/// Container name
name: String,
/// Follow log output
#[clap(long, short = 'f')]
follow: bool,
},
// ── Image build ─────────────────────────────────────────────────────
/// Build an image from a Remfile
Build(cli::build::BuildArgs),
// ── Rootfs management ─────────────────────────────────────────────────
/// Manage the rootfs image store
Rootfs {
#[clap(subcommand)]
cmd: RootfsCmd,
},
// ── Volume management ─────────────────────────────────────────────────
/// Manage named volumes
Volume {
#[clap(subcommand)]
cmd: VolumeCmd,
},
// ── Image management ─────────────────────────────────────────────────
/// Manage OCI images
Image {
#[clap(subcommand)]
cmd: ImageCmd,
},
// ── Network management ──────────────────────────────────────────────
/// Manage named networks
Network {
#[clap(subcommand)]
cmd: NetworkCmd,
},
// ── Compose ──────────────────────────────────────────────────────────
/// Multi-service orchestration
Compose {
#[clap(subcommand)]
cmd: cli::compose::ComposeCmd,
},
// ── Container management ─────────────────────────────────────────────
/// Manage containers
Container {
#[clap(subcommand)]
cmd: ContainerCmd,
},
// ── System maintenance ────────────────────────────────────────────────
/// System-wide maintenance commands (disk usage, pruning)
System {
#[clap(subcommand)]
cmd: cli::system::SystemCmd,
},
// ── Subscribe ─────────────────────────────────────────────────────────
/// Stream container state events as NDJSON (for TUI clients)
Subscribe,
// ── Cleanup ────────────────────────────────────────────────────────────
/// Remove stale network namespaces, overlay dirs, and temp dirs from dead containers
Cleanup,
/// Remove all stopped containers
Prune,
// ── OCI lifecycle ─────────────────────────────────────────────────────
/// OCI lifecycle: create a container (machine interface)
Create {
/// Container ID
id: String,
/// Path to the OCI bundle directory (overrides positional bundle arg)
#[clap(long, short = 'b')]
bundle: Option<std::path::PathBuf>,
/// Path to a Unix socket for console I/O (when process.terminal is true)
#[clap(long)]
console_socket: Option<std::path::PathBuf>,
/// Write the container PID to this file after create
#[clap(long)]
pid_file: Option<std::path::PathBuf>,
/// Bundle path as a positional arg (deprecated; use --bundle)
#[clap(hide = true)]
bundle_positional: Option<std::path::PathBuf>,
},
/// Start one or more stopped containers (or OCI lifecycle start for a single container)
Start {
/// Container name(s) to start
#[clap(required = true)]
id: Vec<String>,
/// Run with an interactive PTY (foreground instead of detached)
#[clap(long, short = 'i')]
interactive: bool,
/// Override the command for this run only; pass after -- (does not update SpawnConfig).
/// Example: pelagos start -i myapp -- /bin/sh
#[clap(last = true, value_name = "CMD")]
cmd: Vec<String>,
},
/// OCI lifecycle: print container state as JSON
State { id: String },
/// OCI lifecycle: send a signal to a container
Kill {
id: String,
#[clap(default_value = "SIGTERM")]
signal: String,
},
/// OCI lifecycle: delete a stopped container's state directory
Delete {
id: String,
/// Force-delete even if container is still running (kills it first)
#[clap(long)]
force: bool,
},
}
#[derive(Subcommand, Debug)]
pub(crate) enum ContainerCmd {
/// List containers
Ls {
/// Show all containers (default: only running)
#[clap(long, short = 'a')]
all: bool,
/// Output format: table or json
#[clap(long, default_value = "table")]
format: OutputFormat,
/// Shorthand for --format json; takes precedence if both are given
#[clap(long)]
json: bool,
/// Filter containers (e.g. label=env=staging, label=managed, status=running)
#[clap(long = "filter")]
filter: Vec<String>,
},
/// Show detailed information about a container (JSON)
Inspect {
/// Container name
name: String,
},
/// Send SIGTERM to a running container
Stop {
/// Container name
name: String,
},
/// Remove a container
Rm {
/// Container name
name: String,
/// Kill and remove even if running
#[clap(long, short = 'f')]
force: bool,
},
/// Print container logs
Logs {
/// Container name
name: String,
/// Follow log output
#[clap(long, short = 'f')]
follow: bool,
},
}
#[derive(Subcommand, Debug)]
pub(crate) enum RootfsCmd {
/// Import a local directory as a named rootfs image
Import {
/// Name for the rootfs image
name: String,
/// Path to the rootfs directory
path: String,
},
/// List available rootfs images
Ls {
/// Output format: table or json
#[clap(long, default_value = "table")]
format: OutputFormat,
},
/// Remove a rootfs image (removes the symlink, not the directory)
Rm {
/// Name of the rootfs image
name: String,
},
}
#[derive(Subcommand, Debug)]
pub(crate) enum VolumeCmd {
/// Create a named volume
Create {
/// Volume name
name: String,
},
/// List named volumes
Ls {
/// Output format: table or json
#[clap(long, default_value = "table")]
format: OutputFormat,
/// Shorthand for --format json; takes precedence if both are given
#[clap(long)]
json: bool,
},
/// Remove a named volume and its contents
Rm {
/// Volume name
name: String,
},
}
#[derive(Subcommand, Debug)]
pub(crate) enum ImageCmd {
/// Pull an image from an OCI registry
Pull {
/// Image reference (e.g. alpine, alpine:3.19, docker.io/library/alpine:latest)
reference: String,
/// Registry username
#[clap(long, short = 'u')]
username: Option<String>,
/// Registry password
#[clap(long)]
password: Option<String>,
/// Read password from stdin
#[clap(long)]
password_stdin: bool,
/// Allow insecure (HTTP) registries
#[clap(long)]
insecure: bool,
},
/// List locally stored images
Ls {
/// Output format: table or json
#[clap(long, default_value = "table")]
format: OutputFormat,
/// Shorthand for --format json; takes precedence if both are given
#[clap(long)]
json: bool,
},
/// Remove a locally stored image
Rm {
/// Image reference
reference: String,
},
/// Push a locally stored image to an OCI registry
Push {
/// Image reference (local)
reference: String,
/// Push to a different destination (default: same reference)
#[clap(long)]
dest: Option<String>,
/// Registry username
#[clap(long, short = 'u')]
username: Option<String>,
/// Registry password
#[clap(long)]
password: Option<String>,
/// Read password from stdin
#[clap(long)]
password_stdin: bool,
/// Allow insecure (HTTP) registries
#[clap(long)]
insecure: bool,
},
/// Log in to an OCI registry (writes ~/.docker/config.json)
Login {
/// Registry hostname (e.g. ghcr.io, docker.io)
registry: String,
/// Registry username
#[clap(long, short = 'u')]
username: Option<String>,
/// Read password from stdin
#[clap(long)]
password_stdin: bool,
},
/// Log out of an OCI registry
Logout {
/// Registry hostname
registry: String,
},
/// Tag a locally stored image with a new reference
Tag {
/// Source image reference
source: String,
/// New reference to assign
target: String,
},
/// Save a locally stored image to an OCI Image Layout tar archive
Save {
/// Image reference (e.g. alpine:latest)
reference: String,
/// Output file path (default: stdout)
#[clap(long, short = 'o')]
output: Option<String>,
},
/// Load an image from an OCI Image Layout tar archive
Load {
/// Input file path (default: stdin)
#[clap(long, short = 'i')]
input: Option<String>,
/// Tag to apply to the loaded image (overrides annotation in archive)
#[clap(long)]
tag: Option<String>,
},
}
#[derive(Subcommand, Debug)]
pub(crate) enum NetworkCmd {
/// Create a named network
Create {
/// Network name (alphanumeric + hyphen, max 12 chars)
name: String,
/// Explicit subnet in CIDR notation (e.g. 10.88.1.0/24).
/// When omitted, a /24 is carved from --alloc-from or the config
/// file's auto_alloc_pool (default 10.99.0.0/16).
#[clap(long)]
subnet: Option<String>,
/// Override the auto-allocation pool for this one create (CIDR, /16 or larger).
#[clap(long)]
alloc_from: Option<String>,
},
/// List networks
Ls {
/// Output format: table or json
#[clap(long, default_value = "table")]
format: OutputFormat,
/// Shorthand for --format json; takes precedence if both are given
#[clap(long)]
json: bool,
},
/// Remove a network
Rm {
/// Network name
name: String,
},
/// Show detailed information about a network (JSON)
Inspect {
/// Network name
name: String,
},
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
fn main() {
env_logger::init();
let cli = Cli::parse();
let result: Result<(), Box<dyn std::error::Error>> = match cli.command {
// Container lifecycle
CliCommand::Build(args) => cli::build::cmd_build(args),
CliCommand::Run(args) => cli::run::cmd_run(*args),
CliCommand::Exec(args) => cli::exec::cmd_exec(args),
CliCommand::Ps {
all,
format,
json,
filter,
} => cli::ps::cmd_ps(all, json || format == OutputFormat::Json, &filter),
CliCommand::Stop { name } => cli::stop::cmd_stop(&name),
CliCommand::Restart { name, time } => cli::restart::cmd_restart(&name, time),
CliCommand::Rm { name, force } => cli::rm::cmd_rm(&name, force),
CliCommand::Logs { name, follow } => cli::logs::cmd_logs(&name, follow),
// Container (noun subcommand)
CliCommand::Container { cmd } => match cmd {
ContainerCmd::Ls {
all,
format,
json,
filter,
} => cli::ps::cmd_ps(all, json || format == OutputFormat::Json, &filter),
ContainerCmd::Inspect { name } => cli::ps::cmd_inspect(&name),
ContainerCmd::Stop { name } => cli::stop::cmd_stop(&name),
ContainerCmd::Rm { name, force } => cli::rm::cmd_rm(&name, force),
ContainerCmd::Logs { name, follow } => cli::logs::cmd_logs(&name, follow),
},
// Rootfs
CliCommand::Rootfs { cmd } => match cmd {
RootfsCmd::Import { name, path } => cli::rootfs::cmd_rootfs_import(&name, &path),
RootfsCmd::Ls { format } => cli::rootfs::cmd_rootfs_ls(format == OutputFormat::Json),
RootfsCmd::Rm { name } => cli::rootfs::cmd_rootfs_rm(&name),
},
// Volume
CliCommand::Volume { cmd } => match cmd {
VolumeCmd::Create { name } => cli::volume::cmd_volume_create(&name),
VolumeCmd::Ls { format, json } => {
cli::volume::cmd_volume_ls(json || format == OutputFormat::Json)
}
VolumeCmd::Rm { name } => cli::volume::cmd_volume_rm(&name),
},
// Image
CliCommand::Image { cmd } => match cmd {
ImageCmd::Pull {
reference,
username,
password,
password_stdin,
insecure,
} => cli::image::cmd_image_pull(
&reference,
username.as_deref(),
password.as_deref(),
password_stdin,
insecure,
),
ImageCmd::Ls { format, json } => {
cli::image::cmd_image_ls(json || format == OutputFormat::Json)
}
ImageCmd::Rm { reference } => cli::image::cmd_image_rm(&reference),
ImageCmd::Push {
reference,
dest,
username,
password,
password_stdin,
insecure,
} => cli::image::cmd_image_push(
&reference,
dest.as_deref(),
username.as_deref(),
password.as_deref(),
password_stdin,
insecure,
),
ImageCmd::Login {
registry,
username,
password_stdin,
} => cli::image::cmd_image_login(®istry, username.as_deref(), password_stdin),
ImageCmd::Logout { registry } => cli::image::cmd_image_logout(®istry),
ImageCmd::Tag { source, target } => cli::image::cmd_image_tag(&source, &target),
ImageCmd::Save { reference, output } => {
cli::image::cmd_image_save(&reference, output.as_deref())
}
ImageCmd::Load { input, tag } => {
cli::image::cmd_image_load(input.as_deref(), tag.as_deref())
}
},
// Network
CliCommand::Network { cmd } => match cmd {
NetworkCmd::Create {
name,
subnet,
alloc_from,
} => cli::network::cmd_network_create(&name, subnet.as_deref(), alloc_from.as_deref()),
NetworkCmd::Ls { format, json } => {
cli::network::cmd_network_ls(json || format == OutputFormat::Json)
}
NetworkCmd::Rm { name } => cli::network::cmd_network_rm(&name),
NetworkCmd::Inspect { name } => cli::network::cmd_network_inspect(&name),
},
// Compose
CliCommand::Compose { cmd } => cli::compose::cmd_compose(cmd),
// System
CliCommand::System { cmd } => cli::system::cmd_system(cmd),
// Subscribe
CliCommand::Subscribe => cli::subscribe::cmd_subscribe(),
// Cleanup
CliCommand::Cleanup => cli::cleanup::cmd_cleanup(),
CliCommand::Prune => cli::prune::cmd_prune(),
// OCI lifecycle
CliCommand::Create {
id,
bundle,
bundle_positional,
console_socket,
pid_file,
} => match bundle.or(bundle_positional) {
None => Err("pelagos create: --bundle <path> is required".into()),
Some(bundle_path) => pelagos::oci::cmd_create(
&id,
&bundle_path,
console_socket.as_deref(),
pid_file.as_deref(),
)
.map_err(|e| e.to_string().into()),
},
CliCommand::Start {
id,
interactive,
cmd,
} => {
// Multi-name pelagos restart: if all names are known pelagos containers, use
// cmd_start (which handles multiple names). If the single argument is an OCI
// container ID, fall through to the OCI lifecycle handler.
// A pelagos container state lives at /run/pelagos/containers/<name>/state.json;
// an OCI container state lives at /run/pelagos/<id>/state.json (different dir).
let all_pelagos = id.iter().all(|n| cli::container_state_exists(n));
if all_pelagos {
cli::start::cmd_start(
&id,
interactive,
if cmd.is_empty() { None } else { Some(cmd) },
)
} else if id.len() == 1 {
pelagos::oci::cmd_start(&id[0]).map_err(|e| e.to_string().into())
} else {
// Mix of known and unknown names — report as an error.
let unknown: Vec<_> = id
.iter()
.filter(|n| !cli::container_state_exists(n))
.collect();
Err(format!(
"container(s) not found: {}",
unknown
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join(", ")
)
.into())
}
}
CliCommand::State { id } => pelagos::oci::cmd_state(&id).map_err(|e| e.to_string().into()),
CliCommand::Kill { id, signal } => {
pelagos::oci::cmd_kill(&id, &signal).map_err(|e| e.to_string().into())
}
CliCommand::Delete { id, force } => {
if force {
pelagos::oci::cmd_delete_force(&id).map_err(|e| e.to_string().into())
} else {
pelagos::oci::cmd_delete(&id).map_err(|e| e.to_string().into())
}
}
};
let code = match result {
Ok(()) => 0,
Err(e) => {
eprintln!("pelagos: error: {}", e);
1
}
};
std::process::exit(code);
}