isb-core 1.9.0

isb's foundation: the incus client, the sandbox spec and planner, compose projects, stacks, orgs, the registry and ingress. Use the `isb` crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
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
//! The sandbox API and the apply engine.

use std::collections::BTreeMap;
use std::path::Path;
use std::time::{Duration, Instant};

use serde::Serialize;
use serde_json::{Value, json};

use crate::client::{Client, encode_segment};
use crate::error::{Error, Result};
use crate::exec::{self, ExecOptions, ExecOutput, ExecStream, Stdin};
use crate::idmap::SubIds;
use crate::lock::NameLock;
pub mod images;
use crate::plan::{
    self, Action, Actual, Desired, DesiredDevice, DiffOptions, HostFacts, Props, SandboxPlan,
    VolumeDefs, split_addr,
};
use crate::spec::{ExecDefaults, PortSpec, ReadyCheck, SandboxSpec};

/// Config key recording which isb call created an instance. A half-created
/// instance is only ever cleaned up by the call whose token it carries.
pub const CREATE_TOKEN_KEY: &str = "user.isb.create-token";

/// Progress callback: one human-readable line per step.
pub type Reporter<'a> = &'a mut dyn FnMut(&str);

/// A summary of an instance, as listed.
#[derive(Debug, Clone, Serialize)]
pub struct SandboxInfo {
    pub name: String,
    pub status: String,
    #[serde(rename = "type")]
    pub instance_type: String,
    /// `user.*` config keys with the prefix stripped.
    pub labels: BTreeMap<String, String>,
    pub config: BTreeMap<String, String>,
    /// Instance-local devices.
    pub devices: BTreeMap<String, Props>,
    pub profiles: Vec<String>,
    pub created_at: String,
    pub description: String,
}

impl SandboxInfo {
    pub fn from_api(v: &Value) -> SandboxInfo {
        let a = Actual::from_api(v);
        let labels = a
            .config
            .iter()
            .filter_map(|(k, v)| k.strip_prefix("user.").map(|k| (k.to_string(), v.clone())))
            // isb's own bookkeeping (user.isb.create-token) is not a label.
            .filter(|(k, _): &(String, String)| !k.starts_with("isb."))
            .collect();
        SandboxInfo {
            name: v.get("name").and_then(Value::as_str).unwrap_or("").into(),
            status: a.status.clone(),
            instance_type: a.instance_type.clone(),
            labels,
            config: a.config,
            devices: a.devices,
            profiles: a.profiles,
            created_at: v
                .get("created_at")
                .and_then(Value::as_str)
                .unwrap_or("")
                .into(),
            description: v
                .get("description")
                .and_then(Value::as_str)
                .unwrap_or("")
                .into(),
        }
    }

    /// Whether this instance matches every filter.
    pub fn matches(&self, filters: &[LabelFilter]) -> bool {
        filters
            .iter()
            .all(|f| match (&f.value, self.labels.get(&f.key)) {
                (_, None) => false,
                (None, Some(_)) => true,
                (Some(want), Some(have)) => want == have,
            })
    }
}

/// `key` (present) or `key=value` (equal).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LabelFilter {
    pub key: String,
    pub value: Option<String>,
}

impl LabelFilter {
    pub fn parse(s: &str) -> LabelFilter {
        match s.split_once('=') {
            Some((k, v)) => LabelFilter {
                key: k.into(),
                value: Some(v.into()),
            },
            None => LabelFilter {
                key: s.into(),
                value: None,
            },
        }
    }
}

/// What `apply` did.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ApplyReport {
    pub name: String,
    pub created: bool,
    pub applied: Vec<Action>,
    /// Device name -> listen address chosen for searched ports.
    pub ports: BTreeMap<String, String>,
    /// Config keys changed that only take effect after a restart.
    pub restart_needed: Vec<String>,
}

/// Options for ensure / up.
#[derive(Debug, Clone, Copy)]
pub struct EnsureOptions {
    pub diff: DiffOptions,
    /// Run readiness checks after applying.
    pub wait_ready: bool,
    /// How long to wait for another isb holding this sandbox's lock.
    pub lock_wait: Duration,
}

impl Default for EnsureOptions {
    fn default() -> Self {
        EnsureOptions {
            diff: DiffOptions::default(),
            wait_ready: true,
            lock_wait: Duration::from_secs(900),
        }
    }
}

fn inst_path(name: &str) -> String {
    format!("/1.0/instances/{}", encode_segment(name))
}

/// Gather host facts (pools, subids, path map).
pub fn host_facts(client: &Client) -> Result<HostFacts> {
    let pools = client
        .get("/1.0/storage-pools")?
        .as_array()
        .map(|a| {
            a.iter()
                .filter_map(|u| u.as_str())
                .filter_map(|u| u.rsplit('/').next())
                .map(|s| s.split('?').next().unwrap_or(s).to_string())
                .collect()
        })
        .unwrap_or_default();
    let info = client.server_info()?;
    let ext = |n: &str| {
        info["api_extensions"]
            .as_array()
            .is_some_and(|a| a.iter().any(|e| e == n))
    };
    Ok(HostFacts {
        subids: SubIds::read_host(),
        pools,
        path_map: HostFacts::detect_path_map(),
        initial_copy: ext("disk_initial_copy"),
        initial_owner: ext("storage_initial_owner"),
        incus_version: client.server_version()?,
        invoking_ids: crate::idmap::invoking_ids(),
        shared_root: shared_root(),
        org: crate::org::OrgId::from_incus_project(client.project_name()),
        registry: crate::registry::info(client)?.map(|i| i.addr),
        project: client.project_name().to_string(),
    })
}

/// On macOS incusd runs in the `isb machine` VM, which sees only `$HOME`.
fn shared_root() -> Option<String> {
    if !cfg!(target_os = "macos") {
        return None;
    }
    let home = std::env::var_os("HOME").filter(|h| !h.is_empty())?;
    let p = std::path::PathBuf::from(home);
    Some(p.canonicalize().unwrap_or(p).to_string_lossy().into_owned())
}

/// Resolve a spec against this host. Relative bind paths anchor at `base`.
pub fn resolve(
    client: &Client,
    spec: &SandboxSpec,
    defs: &VolumeDefs,
    base: &Path,
) -> Result<Desired> {
    let host = host_facts(client)?;
    plan::resolve(spec, defs, &host, base)
}

fn get_actual(client: &Client, name: &str) -> Result<Option<Actual>> {
    Ok(client
        .get_opt(&inst_path(name))?
        .map(|v| Actual::from_api(&v)))
}

fn volume_exists(client: &Client, pool: &str, name: &str) -> Result<bool> {
    Ok(client
        .get_opt(&format!(
            "/1.0/storage-pools/{}/volumes/custom/{}",
            encode_segment(pool),
            encode_segment(name)
        ))?
        .is_some())
}

/// The fingerprint of a local image, by alias or fingerprint (prefix).
fn local_image(client: &Client, alias: &str) -> Result<Option<String>> {
    if let Some(a) = client.get_opt(&format!("/1.0/images/aliases/{}", encode_segment(alias)))? {
        return Ok(a.get("target").and_then(Value::as_str).map(String::from));
    }
    if alias.len() >= 12 && alias.chars().all(|c| c.is_ascii_hexdigit()) {
        if let Some(i) = client.get_opt(&format!("/1.0/images/{}", encode_segment(alias)))? {
            return Ok(i
                .get("fingerprint")
                .and_then(Value::as_str)
                .map(String::from));
        }
    }
    Ok(None)
}

/// Compute the plan for one resolved sandbox.
pub fn plan_desired(client: &Client, desired: &Desired, opts: DiffOptions) -> Result<SandboxPlan> {
    let actual = get_actual(client, &desired.name)?;
    if actual.is_none()
        && desired.image.server.is_none()
        && local_image(client, &desired.image.alias)?.is_none()
    {
        return Err(images::missing_on(client, &desired.image.alias));
    }
    let mut missing = Vec::new();
    for v in &desired.volumes {
        if !volume_exists(client, &v.pool, &v.name)? {
            missing.push((v.pool.clone(), v.name.clone()));
        }
    }
    let mut plan = plan::diff(desired, actual.as_ref(), &missing, opts)?;
    // The image is fixed at creation; say so when the local image has moved on
    // (e.g. dev-base was republished), so a recreate is a visible choice.
    if let (Some(a), None) = (&actual, &desired.image.server) {
        if let (Some(built), Some(now)) = (
            a.config.get("volatile.base_image"),
            local_image(client, &desired.image.alias)?,
        ) {
            if *built != now {
                plan.actions.push(Action::Note {
                    message: format!(
                        "image {} is now {} but this instance was built from {}; recreate to pick it up",
                        desired.image.alias,
                        &now[..12.min(now.len())],
                        &built[..12.min(built.len())]
                    ),
                });
            }
        }
    }
    Ok(plan)
}

fn random_token() -> String {
    let mut b = [0u8; 12];
    if let Ok(mut f) = std::fs::File::open("/dev/urandom") {
        use std::io::Read;
        let _ = f.read_exact(&mut b);
    }
    let t = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    format!(
        "{}{:x}",
        b.iter().map(|x| format!("{x:02x}")).collect::<String>(),
        t & 0xffff
    )
}

/// Retry a step once after a deadline.
fn retry_once<T>(report: &mut dyn FnMut(&str), mut f: impl FnMut() -> Result<T>) -> Result<T> {
    match f() {
        Err(e) if e.is_timeout() => {
            report(&format!("{e}; retrying once"));
            f()
        }
        r => r,
    }
}

/// Apply a plan. Changes only what the plan says; a correct device is never touched.
pub fn apply(
    client: &Client,
    desired: &Desired,
    plan: &SandboxPlan,
    report: Reporter<'_>,
) -> Result<ApplyReport> {
    let name = &desired.name;
    let mut out = ApplyReport {
        name: name.clone(),
        ..Default::default()
    };
    let mut pending: Vec<&Action> = Vec::new();
    if let Some(p) = &desired.egress {
        crate::egress::before_apply(client, p, report)?;
    }
    for action in &plan.actions {
        match action {
            Action::SetConfig { .. }
            | Action::AddDevice { .. }
            | Action::ReplaceDevice { .. }
            | Action::RemoveDevice { .. } => {
                pending.push(action);
                continue;
            }
            _ => {}
        }
        // Batched config/device changes land before whatever comes next (a start,
        // most importantly, so a stopped instance boots with the right devices).
        flush_updates(client, desired, &mut pending, &mut out, report)?;
        match action {
            Action::Note { message } => report(&format!("{name}: note: {message}")),
            Action::CreateVolume {
                pool,
                volume,
                config,
            } => {
                report(&format!("{name}: creating volume {volume} on {pool}"));
                retry_once(report, || {
                    crate::volume::ensure(client, pool, volume, config).map(|_| ())
                })?;
            }
            Action::CreateInstance { .. } => {
                report(&format!("{name}: creating from {}", desired.image.spec));
                create_instance(client, desired, report)?;
                out.created = true;
            }
            Action::StartInstance => {
                if let (true, Some(h)) = (out.created, &desired.before_start) {
                    (h.0)(client, name)?;
                }
                report(&format!("{name}: starting"));
                retry_once(report, || start_instance(client, name))?;
            }
            Action::AddPort {
                device,
                props,
                search,
            } => {
                let listen = add_port_searching(client, name, device, props, *search)?;
                report(&format!("{name}: port {device} listening on {listen}"));
                out.ports.insert(device.clone(), listen);
            }
            Action::FixOwner {
                path,
                owner,
                mode,
                fresh_only,
            } => {
                if desired.instance_type == crate::spec::InstanceType::VirtualMachine {
                    // In-guest work needs the VM's agent, which starts after boot.
                    wait_ready(
                        client,
                        name,
                        &[ReadyCheck::Agent],
                        desired.ready_timeout,
                        &desired.exec,
                    )?;
                }
                let fix = crate::owner::Fix {
                    path,
                    owner: owner.as_deref(),
                    mode: mode
                        .as_deref()
                        .map(crate::owner::parse_mode)
                        .transpose()
                        .map_err(Error::invalid)?,
                    fresh_only: *fresh_only,
                };
                if crate::owner::apply(client, name, &fix)? {
                    report(&format!("{name}: {action}"));
                } else {
                    report(&format!(
                        "{name}: {path} came seeded from the image; owner left as is"
                    ));
                }
            }
            _ => unreachable!("batched above"),
        }
        out.applied.push(action.clone());
    }
    flush_updates(client, desired, &mut pending, &mut out, report)?;
    Ok(out)
}

fn flush_updates(
    client: &Client,
    desired: &Desired,
    pending: &mut Vec<&Action>,
    out: &mut ApplyReport,
    report: &mut dyn FnMut(&str),
) -> Result<()> {
    if pending.is_empty() {
        return Ok(());
    }
    let name = desired.name.as_str();
    for a in pending.iter() {
        report(&format!("{name}: {a}"));
    }
    let actions: Vec<Action> = pending.iter().map(|a| (*a).clone()).collect();
    retry_once(report, || {
        update_instance(
            client,
            name,
            &format!("update {name}"),
            &mut |config, devices| {
                for a in &actions {
                    match a {
                        Action::SetConfig {
                            key, to, secret, ..
                        } => {
                            // A secret's action carries a placeholder.
                            let v = match (secret, desired.config.get(key)) {
                                (true, Some(v)) => v,
                                _ => to,
                            };
                            config.insert(key.clone(), json!(v));
                        }
                        Action::AddDevice { device, props } => {
                            devices.insert(device.clone(), json!(props));
                        }
                        Action::ReplaceDevice {
                            device,
                            replaces,
                            to,
                            ..
                        } => {
                            devices.remove(replaces);
                            devices.insert(device.clone(), json!(to));
                        }
                        Action::RemoveDevice { device, .. } => {
                            devices.remove(device);
                        }
                        _ => {}
                    }
                }
                Ok(())
            },
        )
    })?;
    for a in pending.drain(..) {
        if let Action::SetConfig {
            key, restart: true, ..
        } = a
        {
            out.restart_needed.push(key.clone());
        }
        out.applied.push(a.clone());
    }
    Ok(())
}

type Obj = serde_json::Map<String, Value>;

/// Read-modify-write of an instance under `If-Match`, so a concurrent change by
/// another tool is never silently overwritten (a 412 re-reads and retries).
#[doc(hidden)]
pub fn update_instance(
    client: &Client,
    name: &str,
    step: &str,
    modify: &mut dyn FnMut(&mut Obj, &mut Obj) -> Result<()>,
) -> Result<()> {
    let path = inst_path(name);
    for attempt in 0..5 {
        let (inst, etag) = client.get_etag(&path)?;
        let mut config = inst
            .get("config")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default();
        let mut devices = inst
            .get("devices")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default();
        modify(&mut config, &mut devices)?;
        let body = json!({
            "architecture": inst.get("architecture"),
            "config": config,
            "devices": devices,
            "ephemeral": inst.get("ephemeral"),
            "profiles": inst.get("profiles"),
            "stateful": inst.get("stateful"),
            "description": inst.get("description"),
        });
        match client.mutate_if_match(
            "PUT",
            &path,
            &body,
            etag.as_deref(),
            step,
            client.timeouts.other,
        ) {
            Err(Error::Api { status: 412, .. }) if attempt < 4 => continue,
            r => return r.map(|_| ()),
        }
    }
    unreachable!()
}

fn create_instance(client: &Client, desired: &Desired, report: &mut dyn FnMut(&str)) -> Result<()> {
    let fingerprint = match &desired.image.server {
        None => Some(
            local_image(client, &desired.image.alias)?
                .ok_or_else(|| images::missing_on(client, &desired.image.alias))?,
        ),
        Some(_) => None,
    };
    let step = format!("create instance {}", desired.name);
    let mut last_err = None;
    for attempt in 0..2 {
        let token = random_token();
        let mut config = desired.config.clone();
        config.insert(CREATE_TOKEN_KEY.into(), token.clone());
        let devices: BTreeMap<&String, &Props> = desired
            .devices
            .iter()
            .filter(|(_, d)| d.search.is_none())
            .map(|(k, d)| (k, &d.props))
            .collect();
        let body = json!({
            "name": desired.name,
            "type": desired.instance_type.as_api(),
            "source": desired.image.to_api(fingerprint.as_deref()),
            "config": config,
            "devices": devices,
            "profiles": desired.profiles,
        });
        let body = crate::org::disk::sized_root(client, body);
        match client.mutate(
            "POST",
            "/1.0/instances",
            Some(&body),
            &step,
            client.timeouts.create,
        ) {
            Ok(_) => return Ok(()),
            Err(e) if e.is_timeout() => {
                report(&format!("{e}"));
                if let Error::OperationTimeout { operation, .. } = &e {
                    // Most create operations cannot be cancelled. Let it settle so
                    // the cleanup sees what it actually did.
                    if client
                        .wait_operation(operation, &step, client.timeouts.settle)
                        .is_ok()
                    {
                        report(&format!("{step}: finished late; keeping it"));
                        return Ok(());
                    }
                }
                if let Err(ce) = cleanup_half_created(client, &desired.name, &token, report) {
                    report(&format!("{}: cleanup failed: {ce}", desired.name));
                    if matches!(ce, Error::AlreadyExists(_)) {
                        return Err(e);
                    }
                }
                if attempt == 0 {
                    report(&format!("{step}: retrying once"));
                }
                last_err = Some(e);
            }
            Err(e) => {
                // A failed create may still have left a stub behind.
                if !e.is_conflict() {
                    let _ = cleanup_half_created(client, &desired.name, &token, report);
                }
                return Err(e);
            }
        }
    }
    Err(last_err.expect("loop ran"))
}

/// Delete a half-created instance, but only if it carries `token` in
/// `user.isb.create-token`, i.e. only if the call holding that token created it.
/// Anything else (another owner's instance, one created by hand) is refused with
/// [`Error::AlreadyExists`] and left alone. Missing is fine.
pub fn cleanup_half_created(
    client: &Client,
    name: &str,
    token: &str,
    report: &mut dyn FnMut(&str),
) -> Result<()> {
    let Some(inst) = client.get_opt(&inst_path(name))? else {
        return Ok(());
    };
    let a = Actual::from_api(&inst);
    if a.config.get(CREATE_TOKEN_KEY).map(String::as_str) != Some(token) {
        report(&format!(
            "{name}: exists but was not created by this call; leaving it alone"
        ));
        return Err(Error::AlreadyExists(name.to_string()));
    }
    report(&format!("{name}: removing half-created instance"));
    force_delete(client, name)
}

fn start_instance(client: &Client, name: &str) -> Result<()> {
    let r = client.mutate(
        "PUT",
        &format!("{}/state", inst_path(name)),
        Some(&json!({"action": "start", "timeout": 30})),
        &format!("start {name}"),
        client.timeouts.state,
    );
    match r {
        Ok(_) => Ok(()),
        // Someone else started it in between: that is the state we wanted.
        Err(e) => match get_actual(client, name) {
            Ok(Some(a)) if a.running() => Ok(()),
            _ => Err(e),
        },
    }
}

fn stop_instance(client: &Client, name: &str, force: bool, timeout: Duration) -> Result<()> {
    client
        .mutate(
            "PUT",
            &format!("{}/state", inst_path(name)),
            Some(&json!({"action": "stop", "force": force, "timeout": timeout.as_secs().max(1)})),
            &format!("stop {name}"),
            client.timeouts.state.max(timeout + Duration::from_secs(10)),
        )
        .map(|_| ())
}

fn force_delete(client: &Client, name: &str) -> Result<()> {
    if let Some(a) = get_actual(client, name)? {
        if !a.status.eq_ignore_ascii_case("stopped") {
            let _ = stop_instance(client, name, true, Duration::from_secs(5));
        }
    }
    // An instance mid-transition (rebooting, stopping) refuses deletion for a
    // moment; retry for a bounded while before giving up.
    let started = Instant::now();
    loop {
        let r = client.mutate(
            "DELETE",
            &inst_path(name),
            None,
            &format!("delete {name}"),
            client.timeouts.other,
        );
        match r {
            Ok(_) => return Ok(()),
            Err(e) if e.is_not_found() => return Ok(()),
            Err(e) if e.is_timeout() || started.elapsed() >= Duration::from_secs(60) => {
                return Err(e);
            }
            Err(_) => {
                std::thread::sleep(Duration::from_secs(2));
                if let Ok(Some(a)) = get_actual(client, name) {
                    if a.running() {
                        let _ = stop_instance(client, name, true, Duration::from_secs(5));
                    }
                }
            }
        }
    }
}

/// Add a host-bound proxy, stepping past taken ports. Returns the listen address.
fn add_port_searching(
    client: &Client,
    name: &str,
    device: &str,
    props: &Props,
    search: u16,
) -> Result<String> {
    let listen = props
        .get("listen")
        .cloned()
        .ok_or_else(|| Error::invalid("port without listen"))?;
    let Some((proto, host, port)) = split_addr(&listen) else {
        return Err(Error::invalid(format!("cannot search from {listen}")));
    };
    let (proto, host) = (proto.to_string(), host.to_string());
    let last = port.saturating_add(search);
    let mut last_err = None;
    for p in port..=last {
        // Probe locally first: cheaper than a failed device add, and it steps
        // around listeners that are not incus devices. An address this process
        // cannot bind at all (not local here) is left to incus to judge.
        if proto == "tcp" {
            if let Err(e) = std::net::TcpListener::bind(format!("{host}:{p}")) {
                if e.kind() == std::io::ErrorKind::AddrInUse {
                    continue;
                }
            }
        }
        let addr = format!("{proto}:{host}:{p}");
        let mut dev = props.clone();
        dev.insert("listen".into(), addr.clone());
        let r = update_instance(
            client,
            name,
            &format!("add port {device}"),
            &mut |_, devices| {
                devices.insert(device.to_string(), json!(dev));
                Ok(())
            },
        );
        match r {
            Ok(()) => return Ok(addr),
            Err(e @ Error::Api { .. }) | Err(e @ Error::OperationFailed { .. }) => {
                last_err = Some(e)
            }
            Err(e) => return Err(e),
        }
    }
    Err(Error::invalid(format!(
        "{name}: no free port for {device} in {proto}:{host}:{port}-{last}{}",
        last_err
            .map(|e| format!(" (last error: {e})"))
            .unwrap_or_default()
    )))
}

/// Parse `/proc/net/route` and `/proc/net/ipv6_route` for a default route.
pub fn has_default_route(route_v4: &str, route_v6: &str) -> bool {
    let v4 = route_v4.lines().skip(1).any(|l| {
        let f: Vec<&str> = l.split_whitespace().collect();
        f.len() > 3
            && f[1] == "00000000"
            && u32::from_str_radix(f[3], 16)
                .map(|fl| fl & 1 == 1)
                .unwrap_or(false)
    });
    let v6 = route_v6.lines().any(|l| {
        let f: Vec<&str> = l.split_whitespace().collect();
        f.len() >= 10 && f[0].chars().all(|c| c == '0') && f[1] == "00" && f[9] != "lo"
    });
    v4 || v6
}

/// Run readiness checks in order, each polled until the shared deadline.
#[expect(
    clippy::excessive_nesting,
    reason = "predates the lint ratchet; split it when next changed"
)]
pub fn wait_ready(
    client: &Client,
    name: &str,
    checks: &[ReadyCheck],
    timeout: Duration,
    exec_defaults: &ExecDefaults,
) -> Result<()> {
    let started = Instant::now();
    let mut restarted = false;
    for check in checks {
        let mut last;
        let mut stopped_since: Option<Instant> = None;
        loop {
            match run_check(client, name, check, exec_defaults) {
                Ok(true) => break,
                Ok(false) => last = "not yet".into(),
                Err(e) => last = e.to_string(),
            }
            // An instance that stopped (crashed, powered off) will not get ready
            // by waiting. A reboot (common on a VM's first boot) passes through
            // Stopped briefly, so only a sustained stop counts. incus 7.0.1 sometimes
            // fails to complete a guest-initiated reboot (two concurrent onStop
            // hooks; fixed upstream in lxc/incus#3997), leaving it Stopped, so
            // start it once more before failing fast instead of at the deadline.
            if let Ok(Some(a)) = get_actual(client, name) {
                if a.running() || a.status.eq_ignore_ascii_case("starting") {
                    stopped_since = None;
                } else if stopped_since.get_or_insert_with(Instant::now).elapsed()
                    >= Duration::from_secs(30)
                {
                    if !restarted {
                        restarted = true;
                        stopped_since = None;
                        if start_instance(client, name).is_ok() {
                            continue;
                        }
                    }
                    return Err(Error::NotReady {
                        sandbox: name.into(),
                        check: check.to_string(),
                        detail: format!(
                            "instance is {} (it stopped while getting ready{}; see `incus info --show-log {name}`)",
                            a.status,
                            if restarted {
                                ", and a restart did not stick"
                            } else {
                                ""
                            }
                        ),
                        waited: started.elapsed(),
                    });
                }
            }
            if started.elapsed() >= timeout {
                return Err(Error::NotReady {
                    sandbox: name.into(),
                    check: check.to_string(),
                    detail: last,
                    waited: started.elapsed(),
                });
            }
            std::thread::sleep(Duration::from_millis(250));
        }
    }
    Ok(())
}

fn run_check(
    client: &Client,
    name: &str,
    check: &ReadyCheck,
    defaults: &ExecDefaults,
) -> Result<bool> {
    let cap = |argv: &[&str]| -> Result<ExecOutput> {
        let argv: Vec<String> = argv.iter().map(|s| s.to_string()).collect();
        exec::run_captured(
            client,
            name,
            &argv,
            &exec::Request::default(),
            Stdin::Null,
            Some(Duration::from_secs(20)),
        )
    };
    Ok(match check {
        ReadyCheck::Running => get_actual(client, name)?.is_some_and(|a| a.running()),
        ReadyCheck::Agent => cap(&["true"])?.success(),
        ReadyCheck::DefaultRoute => {
            let v4 = cap(&["cat", "/proc/net/route"])?;
            let v6 = cap(&["cat", "/proc/net/ipv6_route"]).unwrap_or_default();
            has_default_route(&v4.stdout_text(), &v6.stdout_text())
        }
        ReadyCheck::UserExists(u) => cap(&["getent", "passwd", u])?.success(),
        ReadyCheck::PathWritable(p) => {
            let argv = vec!["test".to_string(), "-w".into(), p.clone()];
            let req = exec::build_request(
                client,
                name,
                &argv,
                &ExecDefaults {
                    user: defaults.user.clone(),
                    ..Default::default()
                },
                &ExecOptions::default(),
            )?;
            exec::start_with_timeout(
                client,
                name,
                req,
                Stdin::Null,
                Some(Duration::from_secs(20)),
            )?
            .collect_output()?
            .success()
        }
        ReadyCheck::Command(argv) => {
            let argv: Vec<&str> = argv.iter().map(String::as_str).collect();
            cap(&argv)?.success()
        }
    })
}

/// Lock, plan, apply, wait for readiness.
pub fn ensure(
    client: &Client,
    desired: &Desired,
    opts: EnsureOptions,
    report: Reporter<'_>,
) -> Result<ApplyReport> {
    let _lock = NameLock::acquire(
        client.project_name(),
        &desired.name,
        opts.lock_wait,
        &mut |p| {
            report(&format!(
                "{}: waiting for another isb holding {}",
                desired.name,
                p.display()
            ))
        },
    )?;
    let plan = plan_desired(client, desired, opts.diff)?;
    let mut out = apply(client, desired, &plan, report)?;
    // Report the settled listen address of every searched port, whether this
    // call added it or found it already correct somewhere in its range.
    if desired.devices.values().any(|d| d.search.is_some()) {
        if let Some(inst) = client.get_opt(&inst_path(&desired.name))? {
            let actual = Actual::from_api(&inst);
            for (dev, d) in &desired.devices {
                if d.search.is_none() {
                    continue;
                }
                if let Some(listen) = actual.devices.get(dev).and_then(|p| p.get("listen")) {
                    out.ports.insert(dev.clone(), listen.clone());
                }
            }
        }
    }
    if !out.restart_needed.is_empty() {
        report(&format!(
            "{}: {} changed; takes effect after `isb restart {}`",
            desired.name,
            out.restart_needed.join(", "),
            desired.name
        ));
    }
    if opts.wait_ready {
        wait_ready(
            client,
            &desired.name,
            &desired.ready,
            desired.ready_timeout,
            &desired.exec,
        )?;
        if let Some(p) = &desired.egress {
            crate::egress::after_ready(client, p)?;
        }
    }
    Ok(out)
}

/// A handle on one sandbox.
#[derive(Debug, Clone)]
pub struct Sandbox {
    client: Client,
    name: String,
    exec_defaults: ExecDefaults,
    ready: Vec<ReadyCheck>,
    ready_timeout: Duration,
}

impl Sandbox {
    pub(crate) fn from_desired(client: &Client, d: &Desired) -> Sandbox {
        Sandbox {
            client: client.clone(),
            name: d.name.clone(),
            exec_defaults: d.exec.clone(),
            ready: d.ready.clone(),
            ready_timeout: d.ready_timeout,
        }
    }

    /// A handle on `name` with the exec defaults and readiness of `d` (a
    /// sibling built from the same spec, such as another replica).
    pub(crate) fn like(client: &Client, name: &str, d: &Desired) -> Sandbox {
        Sandbox {
            client: client.clone(),
            name: name.to_string(),
            exec_defaults: d.exec.clone(),
            ready: d.ready.clone(),
            ready_timeout: d.ready_timeout,
        }
    }

    /// Create and start a new sandbox; fails if it already exists. Relative bind
    /// paths resolve against the current directory.
    pub fn create(client: &Client, spec: &SandboxSpec) -> Result<Sandbox> {
        Self::create_with(
            client,
            spec,
            &VolumeDefs::new(),
            EnsureOptions::default(),
            &mut |_| {},
        )
    }

    pub fn create_with(
        client: &Client,
        spec: &SandboxSpec,
        defs: &VolumeDefs,
        opts: EnsureOptions,
        report: Reporter<'_>,
    ) -> Result<Sandbox> {
        let d = resolve(client, spec, defs, &std::env::current_dir()?)?;
        let _lock = NameLock::acquire(
            client.project_name(),
            &d.name,
            Duration::from_secs(900),
            &mut |_| {},
        )?;
        if get_actual(client, &d.name)?.is_some() {
            return Err(Error::AlreadyExists(d.name.clone()));
        }
        let plan = plan_desired(client, &d, opts.diff)?;
        apply(client, &d, &plan, report)?;
        if opts.wait_ready {
            wait_ready(client, &d.name, &d.ready, d.ready_timeout, &d.exec)?;
            if let Some(p) = &d.egress {
                crate::egress::after_ready(client, p)?;
            }
        }
        Ok(Self::from_desired(client, &d))
    }

    /// Reconcile a sandbox to `spec`, creating it if needed: only what differs is
    /// changed, and a correct device is never touched.
    pub fn connect_or_create(client: &Client, spec: &SandboxSpec) -> Result<Sandbox> {
        Self::connect_or_create_with(
            client,
            spec,
            &VolumeDefs::new(),
            EnsureOptions::default(),
            &mut |_| {},
        )
        .map(|(s, _)| s)
    }

    pub fn connect_or_create_with(
        client: &Client,
        spec: &SandboxSpec,
        defs: &VolumeDefs,
        opts: EnsureOptions,
        report: Reporter<'_>,
    ) -> Result<(Sandbox, ApplyReport)> {
        let d = resolve(client, spec, defs, &std::env::current_dir()?)?;
        let r = ensure(client, &d, opts, report)?;
        Ok((Self::from_desired(client, &d), r))
    }

    /// [`Sandbox::connect_or_create_with`], with relative bind paths
    /// resolving against `base` instead of the current directory.
    pub fn connect_or_create_with_base(
        client: &Client,
        spec: &SandboxSpec,
        defs: &VolumeDefs,
        base: &Path,
        opts: EnsureOptions,
        report: Reporter<'_>,
    ) -> Result<(Sandbox, ApplyReport)> {
        let d = resolve(client, spec, defs, base)?;
        let r = ensure(client, &d, opts, report)?;
        Ok((Self::from_desired(client, &d), r))
    }

    /// Handle on an existing sandbox.
    pub fn get(client: &Client, name: &str) -> Result<Sandbox> {
        if get_actual(client, name)?.is_none() {
            return Err(Error::NotFound(format!("sandbox {name}")));
        }
        Ok(Sandbox {
            client: client.clone(),
            name: name.into(),
            exec_defaults: ExecDefaults::default(),
            ready: vec![ReadyCheck::Running],
            ready_timeout: Duration::from_secs(60),
        })
    }

    /// All instances in the client's project.
    pub fn list(client: &Client) -> Result<Vec<SandboxInfo>> {
        Self::list_with(client, &[])
    }

    /// Instances whose labels match every filter.
    pub fn list_with(client: &Client, labels: &[LabelFilter]) -> Result<Vec<SandboxInfo>> {
        let v = client.get("/1.0/instances?recursion=1")?;
        let mut out: Vec<SandboxInfo> = v
            .as_array()
            .map(|a| a.iter().map(SandboxInfo::from_api).collect())
            .unwrap_or_default();
        out.retain(|i| i.matches(labels));
        out.sort_by(|a, b| a.name.cmp(&b.name));
        Ok(out)
    }

    /// Delete a sandbox. A running one needs `force` (it is stopped first).
    pub fn remove(client: &Client, name: &str, force: bool) -> Result<()> {
        let Some(a) = get_actual(client, name)? else {
            return Err(Error::NotFound(format!("sandbox {name}")));
        };
        if a.running() && !force {
            return Err(Error::invalid(format!(
                "{name} is running; stop it first or force removal"
            )));
        }
        force_delete(client, name)?;
        // The egress bridge and ACL go with the sandbox; a daemon sweeps any left behind.
        let _ = crate::egress::plumb::teardown(client, name);
        Ok(())
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn client(&self) -> &Client {
        &self.client
    }

    /// Use these exec defaults for this handle.
    pub fn with_exec_defaults(mut self, d: ExecDefaults) -> Self {
        self.exec_defaults = d;
        self
    }

    pub fn info(&self) -> Result<SandboxInfo> {
        let v = self
            .client
            .get_opt(&inst_path(&self.name))?
            .ok_or_else(|| Error::NotFound(format!("sandbox {}", self.name)))?;
        Ok(SandboxInfo::from_api(&v))
    }

    pub fn labels(&self) -> Result<BTreeMap<String, String>> {
        Ok(self.info()?.labels)
    }

    pub fn start(&self) -> Result<()> {
        if self.info()?.status.eq_ignore_ascii_case("running") {
            return Ok(());
        }
        let _lock = NameLock::acquire(
            self.client.project_name(),
            &self.name,
            Duration::from_secs(900),
            &mut |_| {},
        )?;
        retry_once(&mut |_| {}, || start_instance(&self.client, &self.name))?;
        self.wait_ready()
    }

    /// Stop. `force` kills instead of a clean shutdown; `timeout` bounds the
    /// clean shutdown.
    pub fn stop(&self, force: bool, timeout: Duration) -> Result<()> {
        if self.info()?.status.eq_ignore_ascii_case("stopped") {
            return Ok(());
        }
        stop_instance(&self.client, &self.name, force, timeout)
    }

    pub fn restart(&self) -> Result<()> {
        self.stop(false, Duration::from_secs(30))?;
        self.start()
    }

    /// Run this handle's readiness checks.
    pub fn wait_ready(&self) -> Result<()> {
        wait_ready(
            &self.client,
            &self.name,
            &self.ready,
            self.ready_timeout,
            &self.exec_defaults,
        )
    }

    /// Run a command and capture its output. `argv[0]` is the program; nothing is
    /// joined into a shell string.
    pub fn exec<I, S>(&self, argv: I) -> Result<ExecOutput>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.exec_with(argv, ExecOptions::default())
    }

    pub fn exec_with<I, S>(&self, argv: I, opts: ExecOptions) -> Result<ExecOutput>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.exec_stream(argv, opts)?.collect_output()
    }

    /// Start a command and stream its output as it is produced.
    pub fn exec_stream<I, S>(&self, argv: I, opts: ExecOptions) -> Result<ExecStream>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let argv: Vec<String> = argv.into_iter().map(Into::into).collect();
        let req = exec::build_request(&self.client, &self.name, &argv, &self.exec_defaults, &opts)?;
        exec::start_with_timeout(
            &self.client,
            &self.name,
            req,
            opts.stdin.clone(),
            opts.timeout,
        )
    }

    /// Run attached to this process's stdio (and terminal, with `opts.tty`),
    /// forwarding signals. Returns the exit code.
    pub fn attach<I, S>(&self, argv: I, opts: ExecOptions) -> Result<i32>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let argv: Vec<String> = argv.into_iter().map(Into::into).collect();
        let req = exec::build_request(&self.client, &self.name, &argv, &self.exec_defaults, &opts)?;
        exec::attach(
            &self.client,
            &self.name,
            req,
            opts.stdin.clone(),
            opts.timeout,
        )
    }

    /// Add (or correct) a proxy device. Host-bound ports with `search` step past
    /// taken ports. Returns the listen address in use. A correct device is left
    /// untouched.
    pub fn add_port(&self, port: &PortSpec) -> Result<String> {
        // Same rules as the spec path: a VM gets NAT mode and no bind=guest.
        let instance_type = if self.info()?.instance_type == "virtual-machine" {
            crate::spec::InstanceType::VirtualMachine
        } else {
            crate::spec::InstanceType::Container
        };
        let spec = SandboxSpec {
            name: Some(self.name.clone()),
            image: "unused".into(),
            instance_type,
            ports: vec![port.clone()],
            ..Default::default()
        };
        let host = HostFacts {
            pools: vec!["unused".into()],
            ..Default::default()
        };
        let d = plan::resolve(&spec, &VolumeDefs::new(), &host, Path::new("/"))?;
        let (dname, want): (&String, &DesiredDevice) = d
            .devices
            .iter()
            .find(|(k, _)| k.as_str() != "root")
            .expect("one port");
        let _lock = NameLock::acquire(
            self.client.project_name(),
            &self.name,
            Duration::from_secs(900),
            &mut |_| {},
        )?;
        let info = self.info()?;
        if let Some(have) = info.devices.get(dname) {
            if plan::device_matches(want, have) {
                return Ok(have.get("listen").cloned().unwrap_or_default());
            }
            self.remove_device_unlocked(dname)?;
        }
        match want.search {
            Some(n) => add_port_searching(&self.client, &self.name, dname, &want.props, n),
            None => {
                let props = want.props.clone();
                update_instance(
                    &self.client,
                    &self.name,
                    &format!("add port {dname}"),
                    &mut |_, devices| {
                        devices.insert(dname.clone(), json!(props));
                        Ok(())
                    },
                )?;
                Ok(want.props["listen"].clone())
            }
        }
    }

    /// Remove an instance-local device. `Ok(false)` if it was not there.
    pub fn remove_device(&self, device: &str) -> Result<bool> {
        let _lock = NameLock::acquire(
            self.client.project_name(),
            &self.name,
            Duration::from_secs(900),
            &mut |_| {},
        )?;
        self.remove_device_unlocked(device)
    }

    fn remove_device_unlocked(&self, device: &str) -> Result<bool> {
        if device == "root" {
            return Err(Error::invalid("refusing to remove the root disk"));
        }
        let mut found = false;
        update_instance(
            &self.client,
            &self.name,
            &format!("remove device {device}"),
            &mut |_, devices| {
                found = devices.remove(device).is_some();
                Ok(())
            },
        )?;
        Ok(found)
    }
}

/// One instance `prune` considered.
#[derive(Debug, Clone, Serialize)]
pub struct PruneItem {
    pub name: String,
    pub path: String,
    pub deleted: bool,
}

/// Delete instances whose `label` value is a host path that no longer exists.
/// Instances without the label, or whose path exists, are never touched.
/// With `dry_run`, nothing is deleted.
pub fn prune_missing_path(
    client: &Client,
    label: &str,
    dry_run: bool,
    report: Reporter<'_>,
) -> Result<Vec<PruneItem>> {
    let mut out = Vec::new();
    for i in Sandbox::list_with(client, &[LabelFilter::parse(label)])? {
        let Some(path) = i.labels.get(label) else {
            continue;
        };
        if path.is_empty() || !path.starts_with('/') || Path::new(path).exists() {
            continue;
        }
        if dry_run {
            report(&format!(
                "would delete {}  ({label}={path} is gone)",
                i.name
            ));
        } else {
            report(&format!("deleting {}  ({label}={path} is gone)", i.name));
            force_delete(client, &i.name)?;
        }
        out.push(PruneItem {
            name: i.name,
            path: path.clone(),
            deleted: !dry_run,
        });
    }
    Ok(out)
}

#[cfg(test)]
mod tests;