blackship 0.1.2

A FreeBSD jail manager with state machine control
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
//! Blackship - FreeBSD jail orchestrator
//!
//! A jail manager with TOML configuration, dependency management,
//! and state machine-controlled lifecycle.

#[cfg(not(target_os = "freebsd"))]
compile_error!(
    "blackship only supports FreeBSD. This crate requires jail(2) syscalls \
     which are only available on FreeBSD."
);

mod provision;
mod cli;
mod manifest;
mod console;
mod error;
mod export;
mod supply;
mod bulkhead;
mod sickbay;
mod hooks;
mod sys;
mod jail;
mod network;
mod bridge;
mod blueprint;
mod warden;
mod zfs;

use cli::{ArmadaAction, Cli, Commands, NetworkAction, ReleasesAction, SnapshotAction, TemplateAction};
use error::Result;

use std::sync::Arc;
use tokio::sync::Mutex;

fn main() {
    if let Err(e) = run() {
        eprintln!("Error: {}", e);
        std::process::exit(1);
    }
}

fn run() -> Result<()> {
    let cli = Cli::parse_args();

    // Execute command
    match cli.command {
        // Commands that don't require config
        Commands::Exec {
            jail,
            user,
            command,
        } => {
            let opts = console::ExecOptions {
                user,
                ..Default::default()
            };
            let status = console::exec_in_jail(&jail, &command, &opts)?;
            std::process::exit(status.code().unwrap_or(1));
        }
        Commands::Console { jail, user } => {
            let status = console::console(&jail, &user)?;
            std::process::exit(status.code().unwrap_or(1));
        }
        Commands::Completion { shell } => {
            cli::Cli::generate_completion(shell);
            return Ok(());
        }

        // Initialize a new Jailfile
        Commands::Init { file, release, toml, force } => {
            use std::fs;

            // Check if file exists
            if file.exists() && !force {
                eprintln!("Error: {} already exists. Use -y/--force to overwrite.", file.display());
                std::process::exit(1);
            }

            // Determine release to use
            let base_release = release.unwrap_or_else(|| "15.0-RELEASE".to_string());

            let content = if toml {
                format!(r#"[metadata]
name = "my-jail"
version = "1.0"
# description = "My jail description"

[build]
from = "{}"

# Build arguments
# [[build.args]]
# name = "VERSION"
# default = "1.0"

# Run commands
# [[build.run]]
# command = "pkg install -y <packages>"

# Copy files
# [[build.copy]]
# src = "config.conf"
# dest = "/usr/local/etc/"

# [start]
# cmd = "/usr/sbin/service myapp start"
# user = "root"
"#, base_release)
            } else {
                format!(r#"# Jailfile
FROM {}

# Build arguments
# ARG VERSION=1.0

# Install packages
# RUN pkg install -y <packages>

# Copy files from build context
# COPY config.conf /usr/local/etc/

# Set working directory
# WORKDIR /usr/local

# Expose ports
# EXPOSE 80/tcp

# Default command
# CMD /usr/sbin/service myapp start
"#, base_release)
            };

            fs::write(&file, content)?;
            println!("Created {}", file.display());
            println!("\nNext steps:");
            println!("  1. Edit {} to customize your jail", file.display());
            println!("  2. Run 'blackship build -f {}' to build the jail", file.display());
            return Ok(());
        }

        // Armada (docker-compose style orchestration)
        Commands::Armada { files, action } => {
            match action {
                ArmadaAction::Init { file, force } => {
                    use std::fs;

                    if file.exists() && !force {
                        eprintln!("Error: {} already exists. Use -y/--force to overwrite.", file.display());
                        std::process::exit(1);
                    }

                    let content = r#"# Blackship Armada Configuration
# https://github.com/seuros/blackship

[config]
data_dir = "/var/blackship"
# zfs_enabled = true
# zpool = "zroot"

# Example jail referencing a Jailfile:
# [[jails]]
# name = "web"
# build = "./web"              # Directory containing Jailfile
# depends_on = ["db"]
#
# [jails.network]
# ip_cidr = "10.0.1.10/24"
#
# [[jails.hooks]]
# phase = "post_start"
# command = "echo 'Web started'"

# Example jail without Jailfile (using release directly):
# [[jails]]
# name = "db"
# release = "15.0-RELEASE"
# path = "/jails/db"
"#;

                    fs::write(&file, content)?;
                    println!("Created {}", file.display());
                    println!("\nNext steps:");
                    println!("  1. Edit {} to define your jails", file.display());
                    println!("  2. Run 'blackship armada up' to start all jails");
                    return Ok(());
                }

                ArmadaAction::Up { detach, jails, build: _, no_build: _, dry_run } => {
                    // Load and merge configs
                    let config = manifest::load_merged(&files)?;
                    let mut bridge = bridge::Bridge::new(config)?.verbose(cli.verbose);

                    // TODO: Auto-build jails that have 'build' field set

                    if jails.is_empty() {
                        // Start all jails
                        if dry_run {
                            bridge.up_dry_run(None)?;
                        } else {
                            bridge.up(None)?;
                        }
                    } else {
                        // Start specific jails
                        for jail in &jails {
                            if dry_run {
                                bridge.up_dry_run(Some(jail))?;
                            } else {
                                bridge.up(Some(jail))?;
                            }
                        }
                    }

                    if detach {
                        println!("Jails started in background.");
                        println!("Use 'blackship supervise' for warden mode with auto-restart.");
                    }
                }

                ArmadaAction::Down { jails, dry_run } => {
                    let config = manifest::load_merged(&files)?;
                    let mut bridge = bridge::Bridge::new(config)?.verbose(cli.verbose);

                    if jails.is_empty() {
                        if dry_run {
                            bridge.down_dry_run(None)?;
                        } else {
                            bridge.down(None)?;
                        }
                    } else {
                        for jail in &jails {
                            if dry_run {
                                bridge.down_dry_run(Some(jail))?;
                            } else {
                                bridge.down(Some(jail))?;
                            }
                        }
                    }
                }

                ArmadaAction::Build { jails, dry_run } => {
                    use blueprint::{parse_jailfile, BuildContext, TemplateExecutor};

                    let config = manifest::load_merged(&files)?;

                    // Get jails to build
                    let jails_to_build: Vec<_> = if jails.is_empty() {
                        config.jails.iter().collect()
                    } else {
                        let mut service_names = Vec::new();
                        for name in &jails {
                            let (service_name, _full_name) = config
                                .resolve_jail_names(name)
                                .ok_or_else(|| error::Error::JailNotFound(name.clone()))?;
                            service_names.push(service_name);
                        }
                        config
                            .jails
                            .iter()
                            .filter(|j| service_names.contains(&j.name))
                            .collect()
                    };

                    if dry_run {
                        println!("=== DRY RUN - No changes will be made ===\n");
                    }

                    for jail_def in jails_to_build {
                        if let Some(build_path) = &jail_def.build {
                            let jailfile_path = build_path.join("Jailfile");
                            if jailfile_path.exists() {
                                let full_name = config.jail_name(&jail_def.name);
                                println!("Building jail '{}' from {}", full_name, jailfile_path.display());

                                // Parse the Jailfile
                                let content = std::fs::read_to_string(&jailfile_path).map_err(|e| {
                                    error::Error::TemplateParseFailed(format!(
                                        "Failed to read {}: {}",
                                        jailfile_path.display(),
                                        e
                                    ))
                                })?;
                                let jailfile = parse_jailfile(&content)?;

                                // Target path for the jail
                                let target_path = config
                                    .config
                                    .data_dir
                                    .join("jails")
                                    .join(&full_name);

                                // Copy base release if needed
                                if let Some(release) = &jailfile.from {
                                    let bs = provision::Provisioner::from_config(&config.config)?;
                                    let release_path = config.config.releases_dir.join(release);

                                    if !release_path.exists() {
                                        println!("  Base release '{}' not found. Bootstrapping...", release);
                                        if !dry_run {
                                            bs.bootstrap(release, false)?;
                                        }
                                    }

                                    // Copy release to target
                                    if !dry_run && !target_path.exists() {
                                        println!("  Creating jail root from {}...", release);
                                        std::fs::create_dir_all(&target_path)?;
                                        let status = std::process::Command::new("cp")
                                            .arg("-a")
                                            .arg(format!("{}/.", release_path.display()))
                                            .arg(&target_path)
                                            .status()
                                            .map_err(|e| error::Error::BuildFailed {
                                                step: "FROM".to_string(),
                                                message: format!("Failed to copy base release: {}", e),
                                            })?;
                                        if !status.success() {
                                            return Err(error::Error::BuildFailed {
                                                step: "FROM".to_string(),
                                                message: "cp command failed".to_string(),
                                            });
                                        }
                                    }
                                }

                                // Create build context and execute
                                let ctx = BuildContext::new(build_path, &target_path, &full_name)
                                    .verbose(cli.verbose);
                                let mut executor = TemplateExecutor::new(ctx).dry_run(dry_run);
                                executor.execute(&jailfile)?;

                                if !dry_run {
                                    println!("  Build complete: {}\n", target_path.display());
                                }
                            } else if let Some(jailfile_explicit) = &jail_def.jailfile {
                                if jailfile_explicit.exists() {
                                    let full_name = config.jail_name(&jail_def.name);
                                    println!(
                                        "Building jail '{}' from {}",
                                        full_name,
                                        jailfile_explicit.display()
                                    );
                                    // Similar logic for explicit jailfile path
                                    let content = std::fs::read_to_string(jailfile_explicit).map_err(|e| {
                                        error::Error::TemplateParseFailed(format!(
                                            "Failed to read {}: {}",
                                            jailfile_explicit.display(),
                                            e
                                        ))
                                    })?;
                                    let jailfile = parse_jailfile(&content)?;
                                    let target_path = config
                                        .config
                                        .data_dir
                                        .join("jails")
                                        .join(&full_name);
                                    let context_dir = jailfile_explicit.parent().unwrap_or(std::path::Path::new("."));

                                    if let Some(release) = &jailfile.from {
                                        let bs = provision::Provisioner::from_config(&config.config)?;
                                        let release_path = config.config.releases_dir.join(release);

                                        if !release_path.exists() && !dry_run {
                                            println!("  Bootstrapping {}...", release);
                                            bs.bootstrap(release, false)?;
                                        }

                                        if !dry_run && !target_path.exists() {
                                            println!("  Creating jail root from {}...", release);
                                            std::fs::create_dir_all(&target_path)?;
                                            let status = std::process::Command::new("cp")
                                                .arg("-a")
                                                .arg(format!("{}/.", release_path.display()))
                                                .arg(&target_path)
                                                .status()
                                                .map_err(|e| error::Error::BuildFailed {
                                                    step: "FROM".to_string(),
                                                    message: format!("Failed to copy base release: {}", e),
                                                })?;
                                            if !status.success() {
                                                return Err(error::Error::BuildFailed {
                                                    step: "FROM".to_string(),
                                                    message: "cp command failed".to_string(),
                                                });
                                            }
                                        }
                                    }

                                    let ctx = BuildContext::new(context_dir, &target_path, &full_name)
                                        .verbose(cli.verbose);
                                    let mut executor = TemplateExecutor::new(ctx).dry_run(dry_run);
                                    executor.execute(&jailfile)?;

                                    if !dry_run {
                                        println!("  Build complete: {}\n", target_path.display());
                                    }
                                } else {
                                    eprintln!("Warning: Jailfile not found at {}", jailfile_explicit.display());
                                }
                            } else {
                                eprintln!("Warning: No Jailfile found at {}", jailfile_path.display());
                            }
                        }
                    }
                }

                ArmadaAction::Ps { json } => {
                    let config = manifest::load_merged(&files)?;
                    let bridge = bridge::Bridge::new(config)?.verbose(cli.verbose);
                    bridge.ps(json)?;
                }

                ArmadaAction::Config { show } => {
                    let config = manifest::load_merged(&files)?;

                    if show {
                        // Print merged config as TOML
                        println!("# Merged configuration from: {:?}\n", files);
                        println!("[config]");
                        println!("data_dir = \"{}\"", config.config.data_dir.display());
                        if config.config.zfs_enabled {
                            println!("zfs_enabled = true");
                            if let Some(pool) = &config.config.zpool {
                                println!("zpool = \"{}\"", pool);
                            }
                        }
                        println!();
                        for jail in &config.jails {
                            println!("[[jails]]");
                            println!("name = \"{}\"", jail.name);
                            if let Some(path) = &jail.path {
                                println!("path = \"{}\"", path.display());
                            }
                            if let Some(release) = &jail.release {
                                println!("release = \"{}\"", release);
                            }
                            if let Some(build) = &jail.build {
                                println!("build = \"{}\"", build.display());
                            }
                            if !jail.depends_on.is_empty() {
                                println!("depends_on = {:?}", jail.depends_on);
                            }
                            println!();
                        }
                    } else {
                        // Just validate
                        println!("Configuration valid.");
                        println!("  Files: {:?}", files);
                        println!("  Jails: {}", config.jails.len());
                    }
                }
            }
            return Ok(());
        }

        Commands::Logs {
            jail,
            follow,
            lines,
        } => {
            let config = manifest::load(&cli.config)?;

            // Find jail config to get its path
            let (service_name, full_name) = config
                .resolve_jail_names(&jail)
                .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;
            let jail_def = config
                .get_jail(&service_name)
                .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;

            let jail_path = jail_def.effective_path(&config.config, &full_name);
            let log_dir = jail_path.join("var/log");

            // Common log files to look for
            let log_files = ["messages", "console.log", "daemon.log", "syslog"];

            // Find the first existing log file
            let log_file = log_files
                .iter()
                .map(|f| log_dir.join(f))
                .find(|p| p.exists());

            let log_path = match log_file {
                Some(p) => p,
                None => {
                    // Default to messages even if it doesn't exist yet
                    log_dir.join("messages")
                }
            };

            // Build tail command
            let mut tail_args = vec![];
            if follow {
                tail_args.push("-f".to_string());
            }
            tail_args.push("-n".to_string());
            tail_args.push(lines.to_string());

            // Get the path relative to jail root for jexec
            let relative_log_path = log_path
                .strip_prefix(&jail_path)
                .unwrap_or(&log_path);
            tail_args.push(format!("/{}", relative_log_path.display()));

            // Execute tail via jexec
            let opts = console::ExecOptions::default();
            let mut cmd = vec!["tail".to_string()];
            cmd.extend(tail_args);

            let status = console::exec_in_jail(&full_name, &cmd, &opts)?;
            std::process::exit(status.code().unwrap_or(1));
        }

        Commands::Supervise => {
            // Load config and save it for later use (before moving into async)
            let config = manifest::load(&cli.config)?;
            let project_name = config.config.project_name();
            let project_prefix = format!("{}-", project_name);
            let jails_for_health = config.jails.clone();
            let rate_limit = config.config.rate_limit.clone();

            let bridge = bridge::Bridge::new(config)?.verbose(cli.verbose);
            let bridge = Arc::new(Mutex::new(bridge));

            let rt = tokio::runtime::Runtime::new().map_err(|e| {
                error::Error::Io(std::io::Error::other(e))
            })?;

            rt.block_on(async {
                let warden = warden::Warden::new(Arc::clone(&bridge));
                let sender = warden.sender();

                // Create a WardenHandle before moving warden into spawn
                let warden_handle_for_orch = warden::WardenHandle::new(&warden);
                let warden_handle_for_health = warden::WardenHandle::new(&warden);

                // Wire WardenHandle to the bridge
                {
                    let mut br = bridge.lock().await;
                    br.set_warden_handle(warden_handle_for_orch);
                }

                // Start all jails
                {
                    let mut br = bridge.lock().await;
                    if let Err(e) = br.up(None) {
                        eprintln!("Warning: Failed to start some jails: {}", e);
                    }
                }

                // Spawn the Warden event loop
                let mut warden = warden;
                let warden_task = tokio::spawn(async move {
                    warden.run().await;
                });

                // Spawn health monitors for jails with health checks enabled
                // Collect stop signals to cleanly shutdown health checkers
                let mut health_stop_signals = Vec::new();

                for jail_def in &jails_for_health {
                    if jail_def.healthcheck.enabled && !jail_def.healthcheck.checks.is_empty() {
                        let full_name = if jail_def.name.starts_with(&project_prefix) {
                            jail_def.name.clone()
                        } else {
                            format!("{}-{}", project_name, jail_def.name)
                        };
                        let healthcheck_config = jail_def.healthcheck.clone();
                        let handle = warden_handle_for_health.clone();
                        let health_capacity = rate_limit.health_capacity;
                        let health_refill_rate = rate_limit.health_refill_rate;

                        // Create health checker with warden handle
                        let mut checker = sickbay::HealthChecker::with_rate_limit(
                            &full_name,
                            healthcheck_config,
                            health_capacity,
                            health_refill_rate,
                        ).with_warden_handle(handle);

                        // Try to get JID for the jail
                        if let Ok(jid) = jail::jail_getid(&full_name) {
                            checker = checker.with_jid(jid);
                        }

                        // Get stop signal before moving checker into spawned task
                        let stop_signal = checker.stop_signal();
                        health_stop_signals.push(stop_signal);

                        tokio::spawn(async move {
                            // Run health checks in a loop until stopped
                            while !checker.is_stopped() {
                                if let Err(e) = checker.run_checks() {
                                    eprintln!("Health check error for {}: {}", checker.jail_name(), e);
                                }

                                // Check status and log transitions
                                let status = checker.status();
                                if status == sickbay::HealthStatus::Failing {
                                    eprintln!("Health check failing for jail '{}'", checker.jail_name());
                                }

                                tokio::time::sleep(tokio::time::Duration::from_secs(
                                    checker.interval().as_secs()
                                )).await;
                            }
                            println!("Health monitor stopped for jail '{}'", checker.jail_name());
                        });

                        println!("Spawned health monitor for jail '{}'", full_name);
                    }
                }

                println!("Warden supervisor started. Press Ctrl+C to stop.");

                // Wait for Ctrl+C
                tokio::signal::ctrl_c().await.expect("Failed to listen for Ctrl+C");

                println!("\nShutting down...");

                // Stop all health checkers
                for stop_signal in &health_stop_signals {
                    stop_signal.store(true, std::sync::atomic::Ordering::SeqCst);
                }
                if !health_stop_signals.is_empty() {
                    println!("Stopped {} health monitor(s)", health_stop_signals.len());
                }

                // Request Warden shutdown
                warden::Warden::request_shutdown(&sender).await;

                // Wait for Warden to finish
                let _ = warden_task.await;
            });

            return Ok(());
        }

        // Bootstrap commands - require config but not bridge
        Commands::Bootstrap {
            release,
            force,
            archives,
        } => {
            let config = manifest::load(&cli.config)?;
            let mut bs = provision::Provisioner::from_config(&config.config)?;

            // Override archives if specified on command line
            if let Some(archives) = archives {
                bs = provision::Provisioner::new(
                    config.config.mirror_url.clone(),
                    config.config.releases_dir.clone(),
                    config.config.cache_dir.clone(),
                    archives,
                    config.config.retry.clone(),
                )?;
            }

            bs.bootstrap(&release, force)?;
        }

        Commands::Releases { action, json } => {
            let config = manifest::load(&cli.config)?;
            let bs = provision::Provisioner::from_config(&config.config)?;

            match action.unwrap_or(ReleasesAction::List) {
                ReleasesAction::List => {
                    let releases = bs.list_releases()?;
                    if json {
                        let json_data: Vec<_> = releases
                            .iter()
                            .map(|r| {
                                serde_json::json!({
                                    "name": r.name,
                                    "arch": r.arch.freebsd_name(),
                                    "path": r.path.display().to_string()
                                })
                            })
                            .collect();
                        println!("{}", serde_json::to_string_pretty(&json_data).unwrap());
                    } else if releases.is_empty() {
                        println!("No releases bootstrapped.");
                        println!("Use 'blackship bootstrap <release>' to bootstrap a release.");
                    } else {
                        println!("Bootstrapped releases:");
                        for release in releases {
                            println!("  {} ({}) - {}", release.name, release.arch.freebsd_name(), release.path.display());
                        }
                    }
                }
                ReleasesAction::Delete { release } => {
                    bs.delete(&release)?;
                }
                ReleasesAction::Verify { release } => {
                    if bs.verify(&release)? {
                        println!("Release '{}' is valid.", release);
                    } else {
                        println!("Release '{}' is corrupted or incomplete.", release);
                        std::process::exit(1);
                    }
                }
            }
        }

        Commands::Network { action } => {
            use ipnet::IpNet;
            use network::bridge::{destroy_bridge, list_bridges, Bridge};

            match action {
                NetworkAction::Create {
                    name,
                    subnet,
                    gateway,
                    bridge,
                } => {
                    let subnet: IpNet = subnet.parse().map_err(|e| {
                        error::Error::Network(format!("Invalid subnet: {}", e))
                    })?;

                    let gateway_ip: Option<std::net::IpAddr> = if let Some(gw) = gateway {
                        Some(gw.parse().map_err(|e| {
                            error::Error::Network(format!("Invalid gateway: {}", e))
                        })?)
                    } else {
                        None
                    };

                    // Create bridge
                    let br = Bridge::create_or_open(&bridge)?;

                    // Set gateway IP on bridge if provided
                    if let Some(gw) = &gateway_ip {
                        let prefix = subnet.prefix_len();
                        br.set_address(&format!("{}/{}", gw, prefix))?;
                    }

                    println!("Created network '{}' on bridge '{}'", name, bridge);
                    println!("  Subnet: {}", subnet);
                    if let Some(gw) = gateway_ip {
                        println!("  Gateway: {}", gw);
                    }
                }
                NetworkAction::Destroy { name, force } => {
                    destroy_bridge(&name, force)?;
                    println!("Destroyed bridge '{}'", name);
                }
                NetworkAction::List => {
                    let bridges = list_bridges()?;
                    if bridges.is_empty() {
                        println!("No bridge interfaces found.");
                    } else {
                        println!("Bridge interfaces:");
                        for bridge in bridges {
                            let br = Bridge::open(&bridge)?;
                            let members = br.members()?;
                            if members.is_empty() {
                                println!("  {} (no members)", bridge);
                            } else {
                                println!("  {} (members: {})", bridge, members.join(", "));
                            }
                        }
                    }
                }
                NetworkAction::Attach { jail, network, ip } => {
                    println!(
                        "Attaching jail '{}' to network '{}' (ip: {:?})",
                        jail, network, ip
                    );
                    println!("Note: Attach is done automatically during 'up' with network config.");
                }
                NetworkAction::Detach { jail, network } => {
                    println!("Detaching jail '{}' from network '{}'", jail, network);
                    println!("Note: Detach is done automatically during 'down'.");
                }
            }
        }

        Commands::Health {
            jail,
            watch,
            interval,
            json,
        } => {
            use sickbay::{HealthChecker, HealthStatus};

            let config = manifest::load(&cli.config)?;

            // Filter jails based on input
            let jails: Vec<_> = if let Some(jail_name) = &jail {
                let (service_name, _full_name) = config
                    .resolve_jail_names(jail_name)
                    .ok_or_else(|| error::Error::JailNotFound(jail_name.clone()))?;
                config
                    .jails
                    .iter()
                    .filter(|j| j.name == service_name)
                    .collect()
            } else {
                config.jails.iter().collect()
            };

            if jails.is_empty() {
                if json {
                    println!("[]");
                } else if let Some(name) = jail {
                    println!("Jail '{}' not found in configuration.", name);
                } else {
                    println!("No jails defined in configuration.");
                }
                return Ok(());
            }

            // Create health checkers for each jail
            let rate_limit = &config.config.rate_limit;
            let mut checkers: Vec<HealthChecker> = jails
                .iter()
                .filter(|j| j.healthcheck.enabled)
                .map(|j| {
                    let full_name = config.jail_name(&j.name);
                    let mut checker = HealthChecker::with_rate_limit(
                        &full_name,
                        j.healthcheck.clone(),
                        rate_limit.health_capacity,
                        rate_limit.health_refill_rate,
                    );
                    // Try to get JID for running jails
                    if let Ok(jid) = jail::jail_getid(&full_name) {
                        checker = checker.with_jid(jid);
                    }
                    checker
                })
                .collect();

            if checkers.is_empty() {
                if json {
                    println!("[]");
                } else {
                    println!("No jails have health checks enabled.");
                }
                return Ok(());
            }

            // Display header (non-JSON only)
            if !json {
                println!("{:<20} {:<12} {:<20}", "JAIL", "STATUS", "CHECKS");
                println!("{}", "-".repeat(54));
            }

            loop {
                let mut json_results: Vec<serde_json::Value> = Vec::new();

                for checker in &mut checkers {
                    let status = checker.run_checks()?;
                    let check_results = checker.get_check_results();

                    if json {
                        let status_str = match status {
                            HealthStatus::Healthy => "healthy",
                            HealthStatus::Unhealthy => "unhealthy",
                            HealthStatus::Failing => "failing",
                            HealthStatus::Starting => "starting",
                            HealthStatus::Suspended => "suspended",
                            HealthStatus::Unknown => "unknown",
                        };

                        let checks: Vec<_> = check_results
                            .iter()
                            .map(|(check, result, failures)| {
                                serde_json::json!({
                                    "name": check.name,
                                    "passed": result.as_ref().map(|r| r.passed),
                                    "output": result.as_ref().map(|r| r.output.clone()),
                                    "duration_ms": result.as_ref().map(|r| r.duration.as_millis()),
                                    "age_ms": result.as_ref().map(|r| r.age().as_millis()),
                                    "failures": failures
                                })
                            })
                            .collect();

                        json_results.push(serde_json::json!({
                            "jail": checker.jail_name(),
                            "status": status_str,
                            "checks": checks
                        }));
                    } else {
                        let checks_summary: String = check_results
                            .iter()
                            .map(|(_check, result, failures)| {
                                match result {
                                    Some(r) => {
                                        let summary = r.summary();
                                        if *failures > 0 {
                                            format!("{} ({}x)", summary, failures)
                                        } else {
                                            summary
                                        }
                                    }
                                    None => "?".to_string(),
                                }
                            })
                            .collect::<Vec<_>>()
                            .join(", ");

                        let status_str = match status {
                            HealthStatus::Healthy => "\x1b[32mhealthy\x1b[0m",
                            HealthStatus::Unhealthy => "\x1b[33munhealthy\x1b[0m",
                            HealthStatus::Failing => "\x1b[31mfailing\x1b[0m",
                            HealthStatus::Starting => "\x1b[34mstarting\x1b[0m",
                            HealthStatus::Suspended => "\x1b[35msuspended\x1b[0m",
                            HealthStatus::Unknown => "unknown",
                        };

                        println!(
                            "{:<20} {:<12} {:<20}",
                            checker.jail_name(),
                            status_str,
                            checks_summary
                        );
                    }
                }

                if json {
                    println!("{}", serde_json::to_string_pretty(&json_results).unwrap());
                }

                if !watch {
                    break;
                }

                std::thread::sleep(std::time::Duration::from_secs(interval));
                // Clear previous output for watch mode (move cursor up)
                print!("\x1b[{}A", checkers.len() + 2);
                println!("{:<20} {:<12} {:<20}", "JAIL", "STATUS", "CHECKS");
                println!("{}", "-".repeat(54));
            }
        }

        Commands::Build {
            file,
            name,
            build_args,
            context,
            dry_run,
        } => {
            use blueprint::{parse_jailfile, BuildContext, TemplateExecutor};

            // Determine context directory
            let context_dir = context.unwrap_or_else(|| {
                file.parent()
                    .map(|p| p.to_path_buf())
                    .unwrap_or_else(|| std::env::current_dir().unwrap())
            });

            // Parse the Jailfile
            let content = std::fs::read_to_string(&file).map_err(|e| {
                error::Error::TemplateParseFailed(format!(
                    "Failed to read {}: {}",
                    file.display(),
                    e
                ))
            })?;
            let jailfile = parse_jailfile(&content)?;

            // Determine jail name
            let service_name = name
                .or_else(|| jailfile.metadata.name.clone())
                .unwrap_or_else(|| "unnamed".to_string());

            // Determine target path
            let config = manifest::load(&cli.config)?;
            let full_name = config.jail_name(&service_name);
            let target_path = config.config.data_dir.join("jails").join(&full_name);

            // Check if base release exists and copy it
            if let Some(release) = &jailfile.from {
                let bs = provision::Provisioner::from_config(&config.config)?;
                let release_path = config.config.releases_dir.join(release);

                if !release_path.exists() {
                    println!("Base release '{}' not found. Bootstrapping...", release);
                    bs.bootstrap(release, false)?;
                }

                // Copy release to target (if not dry run)
                if !dry_run && !target_path.exists() {
                    println!("Creating jail root from {}...", release);
                    std::fs::create_dir_all(&target_path)?;
                    // Use cp -a for full copy preserving permissions
                    let status = std::process::Command::new("cp")
                        .arg("-a")
                        .arg(format!("{}/.", release_path.display()))
                        .arg(&target_path)
                        .status()
                        .map_err(|e| error::Error::BuildFailed {
                            step: "FROM".to_string(),
                            message: format!("Failed to copy base release: {}", e),
                        })?;
                    if !status.success() {
                        return Err(error::Error::BuildFailed {
                            step: "FROM".to_string(),
                            message: "cp command failed".to_string(),
                        });
                    }
                }
            }

            // Create build context
            let mut ctx =
                BuildContext::new(&context_dir, &target_path, &full_name).verbose(cli.verbose);

            // Set build arguments from command line
            for (key, value) in build_args {
                ctx.set_arg(&key, &value);
            }

            // Create and run executor
            let mut executor = TemplateExecutor::new(ctx).dry_run(dry_run);

            if dry_run {
                println!("=== DRY RUN - No changes will be made ===\n");
            }

            println!("Building jail '{}' from {}", full_name, file.display());
            executor.execute(&jailfile)?;

            if !dry_run {
                println!("\nBuild complete! Jail root: {}", target_path.display());
                println!("Add the jail to blackship.toml to manage it:");
                println!("  [[jails]]");
                println!("  name = \"{}\"", full_name);
                println!("  path = \"{}\"", target_path.display());
            }
        }

        Commands::Template { action } => {
            use blueprint::{parse_jailfile, Instruction};

            match action {
                TemplateAction::List => {
                    use std::path::Path;

                    /// Represents a discovered template file
                    struct TemplateInfo {
                        name: String,
                        path: std::path::PathBuf,
                        base_release: Option<String>,
                    }

                    /// Check if a file is a valid template based on its name
                    fn is_template_file(path: &Path) -> bool {
                        let file_name = match path.file_name().and_then(|n| n.to_str()) {
                            Some(name) => name,
                            None => return false,
                        };

                        // Match "Jailfile" or "Jailfile.*"
                        if file_name == "Jailfile" || file_name.starts_with("Jailfile.") {
                            return true;
                        }

                        // Match "*.jail" files
                        if file_name.ends_with(".jail") {
                            return true;
                        }

                        false
                    }

                    /// Try to extract base release from a template file
                    fn extract_base_release(path: &Path) -> Option<String> {
                        let content = std::fs::read_to_string(path).ok()?;
                        let jailfile = parse_jailfile(&content).ok()?;
                        jailfile.from
                    }

                    /// Scan a directory for template files
                    fn scan_directory(dir: &Path, templates: &mut Vec<TemplateInfo>) {
                        if !dir.exists() || !dir.is_dir() {
                            return;
                        }

                        if let Ok(entries) = std::fs::read_dir(dir) {
                            for entry in entries.flatten() {
                                let path = entry.path();
                                if path.is_file() && is_template_file(&path) {
                                    let name = path
                                        .file_name()
                                        .and_then(|n| n.to_str())
                                        .unwrap_or("unknown")
                                        .to_string();
                                    let base_release = extract_base_release(&path);
                                    templates.push(TemplateInfo {
                                        name,
                                        path,
                                        base_release,
                                    });
                                }
                            }
                        }
                    }

                    let mut templates: Vec<TemplateInfo> = Vec::new();
                    let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));

                    // 1. Check current directory for Jailfile
                    scan_directory(&cwd, &mut templates);

                    // 2. Check ./templates subdirectory if it exists
                    let templates_subdir = cwd.join("templates");
                    scan_directory(&templates_subdir, &mut templates);

                    // 3. Try to load config and check data_dir/templates
                    if let Ok(config) = manifest::load(&cli.config) {
                        let data_templates = config.config.data_dir.join("templates");
                        scan_directory(&data_templates, &mut templates);
                    }

                    if templates.is_empty() {
                        println!("No templates found.");
                        println!("Create a Jailfile or place .jail files in ./templates/");
                    } else {
                        println!("Available templates:");

                        // Calculate max name width for alignment
                        let max_name_width = templates.iter().map(|t| t.name.len()).max().unwrap_or(0);

                        for template in &templates {
                            // Format path relative to current directory if possible
                            let display_path = template
                                .path
                                .strip_prefix(&cwd)
                                .map(|p| format!("./{}", p.display()))
                                .unwrap_or_else(|_| template.path.display().to_string());

                            // Format with optional base release
                            if let Some(ref release) = template.base_release {
                                println!(
                                    "  {:<width$}  {}  ({})",
                                    template.name,
                                    display_path,
                                    release,
                                    width = max_name_width
                                );
                            } else {
                                println!(
                                    "  {:<width$}  {}",
                                    template.name,
                                    display_path,
                                    width = max_name_width
                                );
                            }
                        }

                        println!();
                        println!("Use 'blackship build --file <path>' to build from a template.");
                    }
                }
                TemplateAction::Inspect { template } => {
                    let path = std::path::Path::new(&template);
                    if path.exists() {
                        let content = std::fs::read_to_string(path)?;
                        let jailfile = parse_jailfile(&content)?;

                        println!("Jailfile: {}\n", template);

                        if let Some(name) = &jailfile.metadata.name {
                            println!("Name: {}", name);
                        }
                        if let Some(version) = &jailfile.metadata.version {
                            println!("Version: {}", version);
                        }
                        if let Some(desc) = &jailfile.metadata.description {
                            println!("Description: {}", desc);
                        }

                        if let Some(from) = &jailfile.from {
                            println!("\nBase release: {}", from);
                        }

                        if !jailfile.args.is_empty() {
                            println!("\nBuild arguments:");
                            for arg in &jailfile.args {
                                println!(
                                    "  {} = {}",
                                    arg.name,
                                    arg.default.as_deref().unwrap_or("<required>")
                                );
                            }
                        }

                        if !jailfile.expose.is_empty() {
                            println!("\nExposed ports:");
                            for port in &jailfile.expose {
                                println!("  {}/{}", port.port, port.protocol);
                            }
                        }

                        println!("\nInstructions ({}):", jailfile.instructions.len());
                        for instr in &jailfile.instructions {
                            match instr {
                                Instruction::Run(cmd) => println!("  RUN {}", cmd),
                                Instruction::Copy(spec) => {
                                    println!("  COPY {} -> {}", spec.src, spec.dest)
                                }
                                Instruction::Env(k, v) => println!("  ENV {}={}", k, v),
                                Instruction::Workdir(p) => println!("  WORKDIR {}", p),
                                _ => println!("  {}", instr.name()),
                            }
                        }

                        if let Some(cmd) = &jailfile.cmd {
                            println!("\nCMD: {}", cmd);
                        }
                        if let Some(ep) = &jailfile.entrypoint {
                            println!("ENTRYPOINT: {}", ep);
                        }
                    } else {
                        println!("Template or file '{}' not found.", template);
                    }
                }
                TemplateAction::Validate { file } => {
                    let content = std::fs::read_to_string(&file).map_err(|e| {
                        error::Error::TemplateParseFailed(format!(
                            "Failed to read {}: {}",
                            file.display(),
                            e
                        ))
                    })?;

                    match parse_jailfile(&content) {
                        Ok(jailfile) => {
                            println!("✓ Jailfile is valid");
                            println!("  Instructions: {}", jailfile.instructions.len());
                            println!("  Build args: {}", jailfile.args.len());
                            if let Some(from) = &jailfile.from {
                                println!("  Base release: {}", from);
                            }
                        }
                        Err(e) => {
                            println!("✗ Jailfile validation failed: {}", e);
                            std::process::exit(1);
                        }
                    }
                }
            }
        }

        Commands::Expose {
            jail,
            port,
            internal,
            proto,
            bind_ip,
        } => {
            use std::net::IpAddr;

            let config = manifest::load(&cli.config)?;
            let mut bridge = bridge::Bridge::new(config)?.verbose(cli.verbose);

            // Parse bind IP if provided
            let bind_addr: Option<IpAddr> = if let Some(ip_str) = bind_ip {
                Some(ip_str.parse().map_err(|e| {
                    error::Error::Network(format!("Invalid bind IP '{}': {}", ip_str, e))
                })?)
            } else {
                None
            };

            // Expose the port through the bridge (uses BulkheadManager)
            let forward = bridge.expose_port(&jail, port, internal, &proto, bind_addr)?;

            println!("Port forwarding configured:");
            println!(
                "  {}:{}/{} -> {}:{}",
                bind_addr
                    .map(|ip| ip.to_string())
                    .unwrap_or_else(|| "*".to_string()),
                port,
                proto,
                forward.jail_ip,
                internal.unwrap_or(port)
            );
            println!("\nPF rule applied: {}", forward.to_pf_rule());
            println!("\nNote: Ensure these lines are in /etc/pf.conf:");
            println!("  rdr-anchor \"blackship\"");
            println!("  anchor \"blackship\"");
        }

        Commands::Ports { jail } => {
            let config = manifest::load(&cli.config)?;
            let bridge = bridge::Bridge::new(config)?;

            println!("Port forwarding status:");
            println!(
                "{:<20} {:<12} {:<18} {:<18}",
                "JAIL", "PROTO", "EXTERNAL", "INTERNAL"
            );
            println!("{}", "-".repeat(70));

            // Get port forwards from bridge's bulkhead manager
            let forwards = if let Some(jail_name) = &jail {
                bridge.get_jail_port_forwards(jail_name)
            } else {
                bridge.list_port_forwards().iter().collect()
            };

            if forwards.is_empty() {
                println!("No port forwards configured.");
            } else {
                for forward in forwards {
                    let bind_str = forward
                        .bind_ip
                        .map(|ip| ip.to_string())
                        .unwrap_or_else(|| "*".to_string());
                    println!(
                        "{:<20} {:<12} {:<18} {:<18}",
                        forward.jail_name,
                        forward.protocol,
                        format!("{}:{}", bind_str, forward.external_port),
                        format!("{}:{}", forward.jail_ip, forward.internal_port)
                    );
                }
            }

            println!("\nTo expose a port:");
            println!("  blackship expose <jail> -p <port> [--bind-ip <ip>]");
        }

        Commands::Unexpose { jail } => {
            let config = manifest::load(&cli.config)?;
            let (_service_name, full_name) = config
                .resolve_jail_names(&jail)
                .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;
            let mut bridge = bridge::Bridge::new(config)?.verbose(cli.verbose);
            bridge.remove_port_forwards(&full_name)?;
            println!("Removed all port forwards for jail '{}'", full_name);
        }

        Commands::Cleanup { jail, force } => {
            let config = manifest::load(&cli.config)?;
            let mut bridge = bridge::Bridge::new(config)?;
            bridge.cleanup(&jail, force)?;
        }

        Commands::Export {
            jail,
            output,
            zfs_send,
        } => {
            let config = manifest::load(&cli.config)?;

            // Find jail config
            let (service_name, full_name) = config
                .resolve_jail_names(&jail)
                .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;
            let jail_def = config
                .get_jail(&service_name)
                .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;

            let jail_path = jail_def.effective_path(&config.config, &full_name);

            // Determine output path
            let output_path =
                output.unwrap_or_else(|| std::path::PathBuf::from(format!("{}.tar.zst", full_name)));

            let hostname = jail_def.hostname.as_deref();
            let ip = jail_def
                .network
                .as_ref()
                .and_then(|n| n.ip)
                .map(|ip| ip.to_string());

            if zfs_send {
                // Check if ZFS is enabled
                if !config.config.zfs_enabled {
                    return Err(error::Error::ZfsNotEnabled);
                }
                let pool = config
                    .config
                    .zpool
                    .as_ref()
                    .ok_or(error::Error::ZfsNotEnabled)?;
                let dataset = format!("{}/{}/jails/{}", pool, config.config.dataset, full_name);
                export::export_jail_zfs(&full_name, &dataset, &output_path, hostname, ip.as_deref())?;
            } else {
                export::export_jail(&full_name, &jail_path, &output_path, hostname, ip.as_deref())?;
            }
        }

        Commands::Import { file, name, force } => {
            let config = manifest::load(&cli.config)?;

            // Determine target path
            let metadata = export::read_metadata(&file)?;
            let target_name = name.as_deref().unwrap_or(metadata.name.as_str());
            let full_name = config.jail_name(target_name);
            let target_path = config.config.data_dir.join("jails").join(&full_name);

            // Check if target exists
            if target_path.exists() && !force {
                return Err(error::Error::JailOperation(format!(
                    "Jail path {} already exists. Use --force to overwrite.",
                    target_path.display()
                )));
            }

            let imported_name = export::import_jail(&file, &target_path, Some(target_name))?;

            println!("\nTo add the imported jail to your config:");
            println!("  [[jails]]");
            println!("  name = \"{}\"", imported_name);
            println!("  path = \"{}\"", target_path.display());
        }

        Commands::Snapshot { action } => {
            let config = manifest::load(&cli.config)?;

            // Require ZFS for snapshots
            if !config.config.zfs_enabled {
                return Err(error::Error::ZfsNotEnabled);
            }

            let pool = config
                .config
                .zpool
                .as_ref()
                .ok_or(error::Error::ZfsNotEnabled)?;
            let zfs = zfs::ZfsManager::new(pool, &config.config.dataset);

            match action {
                SnapshotAction::Create { jail, name } => {
                    // Verify jail exists in config
                    let (service_name, full_name) = config
                        .resolve_jail_names(&jail)
                        .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;
                    if config.get_jail(&service_name).is_none() {
                        return Err(error::Error::JailNotFound(jail.clone()));
                    }

                    let snap_name = zfs.create_snapshot(&full_name, name.as_deref())?;
                    println!("Created snapshot: {}@{}", full_name, snap_name);
                }
                SnapshotAction::List { jail, json } => {
                    // Verify jail exists in config
                    let (service_name, full_name) = config
                        .resolve_jail_names(&jail)
                        .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;
                    if config.get_jail(&service_name).is_none() {
                        return Err(error::Error::JailNotFound(jail.clone()));
                    }

                    let snapshots = zfs.list_snapshots(&full_name)?;

                    if json {
                        let json_data: Vec<_> = snapshots
                            .iter()
                            .map(|s| {
                                serde_json::json!({
                                    "name": s.name,
                                    "creation": s.creation,
                                    "used": s.used,
                                    "refer": s.refer
                                })
                            })
                            .collect();
                        println!("{}", serde_json::to_string_pretty(&json_data).unwrap());
                    } else if snapshots.is_empty() {
                        println!("No snapshots for jail '{}'.", full_name);
                    } else {
                        println!("Snapshots for jail '{}':", full_name);
                        println!(
                            "{:<30} {:<24} {:<10} {:<10}",
                            "NAME", "CREATED", "USED", "REFER"
                        );
                        println!("{}", "-".repeat(76));
                        for snap in snapshots {
                            println!(
                                "{:<30} {:<24} {:<10} {:<10}",
                                snap.name, snap.creation, snap.used, snap.refer
                            );
                        }
                    }
                }
                SnapshotAction::Rollback {
                    jail,
                    snapshot,
                    force,
                } => {
                    // Verify jail exists in config
                    let (service_name, full_name) = config
                        .resolve_jail_names(&jail)
                        .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;
                    if config.get_jail(&service_name).is_none() {
                        return Err(error::Error::JailNotFound(jail.clone()));
                    }

                    // Check if jail is running
                    if jail::jail_getid(&full_name).is_ok() {
                        return Err(error::Error::JailOperation(format!(
                            "Jail '{}' is running. Stop it first with 'blackship down {}'",
                            full_name, full_name
                        )));
                    }

                    zfs.rollback_snapshot(&full_name, &snapshot, force)?;
                    println!(
                        "Rolled back jail '{}' to snapshot '{}'",
                        full_name, snapshot
                    );
                }
                SnapshotAction::Delete { jail, snapshot } => {
                    // Verify jail exists in config
                    let (service_name, full_name) = config
                        .resolve_jail_names(&jail)
                        .ok_or_else(|| error::Error::JailNotFound(jail.clone()))?;
                    if config.get_jail(&service_name).is_none() {
                        return Err(error::Error::JailNotFound(jail.clone()));
                    }

                    zfs.delete_snapshot(&full_name, &snapshot)?;
                    println!("Deleted snapshot '{}@{}'", full_name, snapshot);
                }
            }
        }

        Commands::Clone { source, name } => {
            let config = manifest::load(&cli.config)?;

            // Require ZFS for cloning
            if !config.config.zfs_enabled {
                return Err(error::Error::ZfsNotEnabled);
            }

            // Parse source format: jail@snapshot
            let parts: Vec<&str> = source.split('@').collect();
            if parts.len() != 2 {
                return Err(error::Error::JailOperation(
                    "Source must be in format 'jail@snapshot'".into(),
                ));
            }
            let (source_jail, snapshot) = (parts[0], parts[1]);

            // Verify source jail exists
            let (source_service, source_full) = config
                .resolve_jail_names(source_jail)
                .ok_or_else(|| error::Error::JailNotFound(source_jail.to_string()))?;
            if config.get_jail(&source_service).is_none() {
                return Err(error::Error::JailNotFound(source_jail.to_string()));
            }

            // Check new name doesn't already exist
            if config.resolve_jail_names(&name).is_some() {
                return Err(error::Error::JailOperation(format!(
                    "Jail '{}' already exists in config",
                    name
                )));
            }

            let pool = config
                .config
                .zpool
                .as_ref()
                .ok_or(error::Error::ZfsNotEnabled)?;
            let zfs = zfs::ZfsManager::new(pool, &config.config.dataset);

            let new_full_name = config.jail_name(&name);
            let new_path = zfs.clone_from_snapshot(&source_full, snapshot, &new_full_name)?;

            println!(
                "Cloned '{}@{}' to new jail '{}'",
                source_full, snapshot, new_full_name
            );
            println!("Path: {}", new_path.display());
            println!("\nTo use this jail, add it to blackship.toml:");
            println!("  [[jails]]");
            println!("  name = \"{}\"", name);
            println!("  path = \"{}\"", new_path.display());
        }

        // Commands that require config and bridge
        _ => {
            let config = manifest::load(&cli.config)?;
            let mut bridge = bridge::Bridge::new(config)?.verbose(cli.verbose);

            match cli.command {
                Commands::Up { jail, all, dry_run } => {
                    // Require either jail name or --all
                    if jail.is_none() && !all {
                        eprintln!("Error: specify a jail name or use --all to start all jails");
                        std::process::exit(1);
                    }
                    if dry_run {
                        bridge.up_dry_run(jail.as_deref())?;
                    } else {
                        bridge.up(jail.as_deref())?;
                    }
                }
                Commands::Down { jail, all, dry_run } => {
                    // Require either jail name or --all
                    if jail.is_none() && !all {
                        eprintln!("Error: specify a jail name or use --all to stop all jails");
                        std::process::exit(1);
                    }
                    if dry_run {
                        bridge.down_dry_run(jail.as_deref())?;
                    } else {
                        bridge.down(jail.as_deref())?;
                    }
                }
                Commands::Restart { jail, all, dry_run } => {
                    // Require either jail name or --all
                    if jail.is_none() && !all {
                        eprintln!("Error: specify a jail name or use --all to restart all jails");
                        std::process::exit(1);
                    }
                    if dry_run {
                        bridge.down_dry_run(jail.as_deref())?;
                        bridge.up_dry_run(jail.as_deref())?;
                    } else {
                        bridge.restart(jail.as_deref())?;
                    }
                }
                Commands::Ps { json } => {
                    bridge.ps(json)?;
                }
                Commands::Check => {
                    bridge.check()?;
                }
                Commands::Setup => {
                    // Initialize PF firewall anchor for port forwarding
                    bridge.init_bulkhead()?;
                    println!("System setup complete.");
                    println!("PF anchor 'blackship' initialized for port forwarding.");
                }
                // Already handled above
                Commands::Exec { .. }
                | Commands::Console { .. }
                | Commands::Completion { .. }
                | Commands::Init { .. }
                | Commands::Armada { .. }
                | Commands::Logs { .. }
                | Commands::Supervise
                | Commands::Bootstrap { .. }
                | Commands::Releases { .. }
                | Commands::Network { .. }
                | Commands::Health { .. }
                | Commands::Build { .. }
                | Commands::Template { .. }
                | Commands::Expose { .. }
                | Commands::Ports { .. }
                | Commands::Unexpose { .. }
                | Commands::Cleanup { .. }
                | Commands::Export { .. }
                | Commands::Import { .. }
                | Commands::Snapshot { .. }
                | Commands::Clone { .. } => unreachable!(),
            }
        }
    }

    Ok(())
}