blackship 0.5.1

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
//! Status and dry-run operations

use crate::error::Result;
use crate::jail::jail_getid;
use crate::manifest::ConfigSource;
use crate::sickbay::{HealthStateRecord, HealthStateStore, HealthStatus};
use serde::Serialize;
use std::collections::BTreeSet;

use super::Bridge;

/// Stable jail status representation used by JSON consumers.
#[derive(Debug, Serialize)]
pub struct JailStatus {
    pub name: String,
    pub state: String,
    pub jid: Option<i32>,
    pub running: bool,
    pub hostname: Option<String>,
    pub ip: Option<String>,
    pub ips: Vec<String>,
    pub path: String,
    pub tags: Vec<String>,
    pub autostart: bool,
}

/// Host inventory for one manifest plus running jails it does not manage.
#[derive(Debug, Serialize)]
pub struct InventoryReport {
    pub project: String,
    pub config_path: String,
    pub config_paths: Vec<String>,
    pub jails: Vec<InventoryJail>,
}

/// One jail in the host inventory.
#[derive(Debug, Serialize)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "stable JSON uses explicit ownership and lifecycle flags"
)]
pub struct InventoryJail {
    pub service_name: Option<String>,
    pub full_name: String,
    pub manager: &'static str,
    pub managed: bool,
    pub configured: bool,
    pub running: bool,
    pub state: String,
    pub jid: Option<i32>,
    pub release: Option<String>,
    pub hostname: Option<String>,
    pub ip: Option<String>,
    pub ips: Vec<String>,
    pub path: Option<String>,
    pub networks: Vec<String>,
    pub depends_on: Vec<String>,
    pub tags: Vec<String>,
    pub autostart: bool,
    pub health: HealthSummary,
}

/// The health verdict an inventory row reports. JSON strings are stable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SummaryStatus {
    NotConfigured,
    Unknown,
    Starting,
    Healthy,
    Unhealthy,
    Failing,
    Suspended,
}

impl SummaryStatus {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NotConfigured => "not_configured",
            Self::Unknown => "unknown",
            Self::Starting => "starting",
            Self::Healthy => "healthy",
            Self::Unhealthy => "unhealthy",
            Self::Failing => "failing",
            Self::Suspended => "suspended",
        }
    }
}

impl std::fmt::Display for SummaryStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.pad(self.as_str())
    }
}

impl From<HealthStatus> for SummaryStatus {
    fn from(status: HealthStatus) -> Self {
        match status {
            HealthStatus::Starting => Self::Starting,
            HealthStatus::Healthy => Self::Healthy,
            HealthStatus::Unhealthy => Self::Unhealthy,
            HealthStatus::Failing => Self::Failing,
            HealthStatus::Suspended => Self::Suspended,
            HealthStatus::Unknown => Self::Unknown,
        }
    }
}

/// Why an inventory row's health is unknown. JSON strings are stable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UnknownReason {
    Stopped,
    NotChecked,
    StaleJid,
    StateUnavailable,
    Unmanaged,
}

/// Passive, last-known health information for a host inventory row.
///
/// The counts are the last results of each check, not the verdict itself. There is no
/// expiry: judge freshness by `checked_at`, and note that a paced or slow supervisor can
/// leave the data older than the check interval.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct HealthSummary {
    pub configured: bool,
    pub status: SummaryStatus,
    pub reason: Option<UnknownReason>,
    pub checked_at: Option<i64>,
    pub checks_total: usize,
    pub checks_passing: Option<usize>,
    pub checks_failing: Option<usize>,
    pub checks_pending: Option<usize>,
}

fn managed_health_summary(
    definition: &crate::manifest::JailDef,
    running: bool,
    jid: Option<i32>,
    state: Result<Option<HealthStateRecord>>,
) -> HealthSummary {
    let checks_total = definition.healthcheck.checks.len();
    if !definition.healthcheck.enabled || checks_total == 0 {
        return HealthSummary {
            configured: false,
            status: SummaryStatus::NotConfigured,
            reason: None,
            checked_at: None,
            checks_total: 0,
            checks_passing: None,
            checks_failing: None,
            checks_pending: None,
        };
    }

    let unknown = |reason| HealthSummary {
        configured: true,
        status: SummaryStatus::Unknown,
        reason: Some(reason),
        checked_at: None,
        checks_total,
        checks_passing: None,
        checks_failing: None,
        checks_pending: None,
    };

    if !running {
        return unknown(UnknownReason::Stopped);
    }

    match state {
        Ok(Some(record)) if record.jid == jid => HealthSummary {
            configured: true,
            status: record.status.into(),
            reason: None,
            checked_at: Some(record.checked_at),
            checks_total: record.checks_total,
            checks_passing: Some(record.checks_passing),
            checks_failing: Some(record.checks_failing),
            checks_pending: Some(
                record
                    .checks_total
                    .saturating_sub(record.checks_passing.saturating_add(record.checks_failing)),
            ),
        },
        Ok(Some(_)) => unknown(UnknownReason::StaleJid),
        Ok(None) => unknown(UnknownReason::NotChecked),
        Err(_) => unknown(UnknownReason::StateUnavailable),
    }
}

const fn unmanaged_health_summary() -> HealthSummary {
    HealthSummary {
        configured: false,
        status: SummaryStatus::Unknown,
        reason: Some(UnknownReason::Unmanaged),
        checked_at: None,
        checks_total: 0,
        checks_passing: None,
        checks_failing: None,
        checks_pending: None,
    }
}

/// Result of validating the manifest and resolving its jail paths.
#[derive(Debug, Serialize)]
pub struct CheckReport {
    pub valid: bool,
    pub start_order: Vec<String>,
    pub zfs_enabled: bool,
    pub jail_paths: Vec<JailPathStatus>,
    /// Host state that will stop a start but not the manifest itself.
    pub warnings: Vec<String>,
}

#[derive(Debug, Serialize)]
pub struct JailPathStatus {
    pub name: String,
    pub path: String,
    pub exists: bool,
}

impl Bridge {
    /// The owning jail descriptor fd held for a jail, when there is one.
    pub fn jail_descriptor_fd(&self, full_name: &str) -> Option<i32> {
        self.instances
            .get(full_name)
            .and_then(|instance| instance.handle.as_ref())
            .and_then(crate::jail::JailHandle::descriptor_fd)
    }

    /// A handle on this bridge's audit log, for readers that outlive a borrow of it.
    pub fn audit_log(&self) -> crate::audit::AuditLog {
        self.audit.clone()
    }

    /// Report a health verdict into the jail's chart; ignored unless it is running.
    pub fn record_health(&mut self, full_name: &str, status: HealthStatus) {
        if let Some(instance) = self.instances.get_mut(full_name)
            && instance.is_running()
        {
            match instance.set_health(status) {
                Ok(()) if self.verbose => {
                    println!("  Health chart '{full_name}': {:?}", instance.health());
                }
                Ok(()) => {}
                Err(e) => {
                    eprintln!("Warning: health chart rejected '{status}' for '{full_name}': {e}");
                }
            }
        }
    }

    /// Print jail status
    pub fn ps(&self, json: bool) {
        let jails = self.ps_data();
        if json {
            println!(
                "{}",
                serde_json::to_string_pretty(&jails).expect("JailStatus always serializes")
            );
            return;
        }
        println!("{:<20} {:<10} {:<10}", "NAME", "STATE", "JID");
        println!("{}", "-".repeat(42));
        for jail in jails {
            let jid = jail.jid.map(|j| j.to_string()).unwrap_or_default();
            println!("{:<20} {:<10} {jid:<10}", jail.name, jail.state);
        }
    }

    /// Print configured Blackship jails and every other running host jail.
    pub fn ps_host(&self, config_path: &(impl ConfigSource + ?Sized), json: bool) {
        let report = self.inventory_data(config_path);
        if json {
            println!(
                "{}",
                serde_json::to_string_pretty(&report).expect("InventoryReport always serializes")
            );
            return;
        }

        println!(
            "{:<24} {:<12} {:<10} {:<14} {:<10}",
            "NAME", "MANAGER", "STATE", "HEALTH", "JID"
        );
        println!("{}", "-".repeat(75));
        for jail in report.jails {
            let jid = jail.jid.map(|j| j.to_string()).unwrap_or_default();
            println!(
                "{:<24} {:<12} {:<10} {:<14} {jid:<10}",
                jail.full_name, jail.manager, jail.state, jail.health.status
            );
        }
    }

    /// The effective autostart set, plus why it is empty when the graph cannot be ordered.
    pub(super) fn autostart_set(&self) -> (BTreeSet<String>, Option<crate::error::Error>) {
        match self.autostart_order() {
            Ok(order) => (order.into_iter().collect(), None),
            Err(e) => (BTreeSet::new(), Some(e)),
        }
    }

    /// Return jail status without writing terminal output.
    pub fn ps_data(&self) -> Vec<JailStatus> {
        let (autostart, problem) = self.autostart_set();
        if let Some(problem) = problem {
            eprintln!("Warning: autostart cannot be resolved, reporting none: {problem}");
        }
        self.config
            .jails
            .iter()
            .map(|jail_def| {
                let full_name = self.config.jail_name(&jail_def.name);
                let instance = self.instances.get(&full_name);

                let (state, jid, running) = match instance {
                    Some(instance) => (
                        format!("{:?}", instance.state()),
                        instance.jid,
                        instance.is_running(),
                    ),
                    None => match jail_getid(&full_name) {
                        Ok(jid) => ("Running".to_string(), Some(jid), true),
                        Err(_) => ("Stopped".to_string(), None, false),
                    },
                };

                let (name, path, hostname, ips) = match instance {
                    Some(instance) => (
                        instance.config.name.clone(),
                        instance.config.path.clone(),
                        instance.config.hostname.clone(),
                        instance
                            .config
                            .ips
                            .iter()
                            .map(std::string::ToString::to_string)
                            .collect::<Vec<_>>(),
                    ),
                    None => (
                        full_name,
                        jail_def.effective_path(
                            &self.config.config,
                            &self.config.jail_name(&jail_def.name),
                        ),
                        jail_def.hostname.clone(),
                        jail_def
                            .network
                            .as_ref()
                            .and_then(crate::manifest::JailNetworkConfig::static_ip)
                            .map(|ip| vec![ip.to_string()])
                            .unwrap_or_default(),
                    ),
                };
                let ip = ips.first().cloned();

                JailStatus {
                    name,
                    state,
                    jid,
                    running,
                    hostname,
                    ip,
                    ips,
                    path: path.to_string_lossy().into_owned(),
                    tags: jail_def.tags.clone(),
                    autostart: autostart.contains(&jail_def.name),
                }
            })
            .collect()
    }

    /// Return manifest-owned jails and running jails outside that manifest.
    ///
    /// Stopped unmanaged jails are intentionally absent: without a known
    /// manifest there is no authoritative host-wide registry for them.
    pub fn inventory_data(&self, config_path: &(impl ConfigSource + ?Sized)) -> InventoryReport {
        let statuses = self.ps_data();
        let health_store = HealthStateStore::from_config(Some(&self.config));
        let managed_names: BTreeSet<String> = self
            .config
            .jails
            .iter()
            .map(|jail| self.config.jail_name(&jail.name))
            .collect();

        let mut jails = self
            .config
            .jails
            .iter()
            .zip(statuses)
            .map(|(definition, status)| {
                let health = managed_health_summary(
                    definition,
                    status.running,
                    status.jid,
                    health_store.get(&status.name),
                );
                InventoryJail {
                    service_name: Some(definition.name.clone()),
                    full_name: status.name,
                    manager: "blackship",
                    managed: true,
                    configured: true,
                    running: status.running,
                    state: status.state,
                    jid: status.jid,
                    release: definition.release.clone(),
                    hostname: status.hostname,
                    ip: status.ip,
                    ips: status.ips,
                    path: Some(status.path),
                    networks: definition
                        .network
                        .as_ref()
                        .map_or_else(Vec::new, |network| network.networks.clone()),
                    depends_on: definition.depends_on.clone(),
                    tags: status.tags,
                    autostart: status.autostart,
                    health,
                }
            })
            .collect::<Vec<_>>();

        jails.extend(crate::jail::RunningJails::new().filter_map(|jid| {
            let name = crate::jail::jail_getname(jid).ok()?;
            if managed_names.contains(&name) {
                return None;
            }
            Some(InventoryJail {
                service_name: None,
                full_name: name,
                manager: "native",
                managed: false,
                configured: false,
                running: true,
                state: "Running".to_string(),
                jid: Some(jid),
                release: None,
                hostname: None,
                ip: None,
                ips: Vec::new(),
                path: None,
                networks: Vec::new(),
                depends_on: Vec::new(),
                tags: Vec::new(),
                autostart: false,
                health: unmanaged_health_summary(),
            })
        }));

        InventoryReport {
            project: self.config.config.project_name().to_string(),
            config_path: config_path.primary_path().display().to_string(),
            config_paths: config_path
                .paths()
                .into_iter()
                .map(|path| path.display().to_string())
                .collect(),
            jails,
        }
    }

    /// Validate configuration
    pub fn check(&self) -> Result<()> {
        let report = self.check_data()?;
        println!("Configuration is valid.");
        println!("\nStart order:");
        for (i, name) in report.start_order.iter().enumerate() {
            println!("  {}. {}", i + 1, name);
        }

        if report.zfs_enabled {
            println!("\nZFS: enabled");
        } else {
            println!("\nZFS: disabled");
        }

        println!("\nJail paths:");
        for jail in report.jail_paths {
            let status = if jail.exists { "exists" } else { "missing" };
            println!("  {}: {} ({})", jail.name, jail.path, status);
        }

        if !report.warnings.is_empty() {
            println!("\nWarnings:");
            for warning in &report.warnings {
                println!("  {warning}");
            }
        }

        Ok(())
    }

    /// Validate configuration and return a structured report.
    pub fn check_data(&self) -> Result<CheckReport> {
        let start_order = self
            .start_order()?
            .into_iter()
            .map(|name| self.config.jail_name(name))
            .collect();
        let jail_paths = self
            .config
            .jails
            .iter()
            .map(|jail| {
                let name = self.config.jail_name(&jail.name);
                let path = jail.effective_path(&self.config.config, &name);
                JailPathStatus {
                    name,
                    exists: path.exists(),
                    path: path.to_string_lossy().into_owned(),
                }
            })
            .collect();

        Ok(CheckReport {
            valid: true,
            start_order,
            zfs_enabled: self.zfs.is_some(),
            jail_paths,
            warnings: crate::provision::unbootstrapped_releases(&self.config),
        })
    }
}

#[cfg(test)]
mod tests;