blackship 0.6.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
//! Jail migration between hosts over zfs send | ssh zfs receive.
//!
//! The snapshot carries the staged .blackship/ config, so the receiving
//! host gets the jail definition along with the data. A successful transfer
//! fences the source: its dataset goes read-only and starts refuse, so the
//! jail cannot silently run on two hosts. A failed transfer fences nothing and
//! removes the partial destination dataset it created.
//!
//! The sequence is a saga (`saga.rs`) recorded under `<data_root>/migrations`, so a
//! killed migrate can be resumed or aborted instead of leaving the jail runnable on
//! both hosts with no trace of the transfer.

use std::path::Path;
use std::process::Command;

use saga_machines::Saga;
use serde::Serialize;

use self::host::Host;
use self::record::{Lock, Record, RecordStore, refuse_second};
use self::saga::{Driver, STEPS, Settled, Step, saga_id};
use crate::error::{Error, Result};
use crate::manifest;

mod host;
mod record;
mod saga;

/// What one invocation of `blackship migrate` asks for.
#[derive(Debug, PartialEq, Eq)]
pub enum Mode {
    Start {
        jail: String,
        target: String,
        remote_dataset: Option<String>,
        keep: bool,
    },
    Resume(String),
    Abort(String),
}

/// Sort the arguments into one mode: `--resume` and `--abort` stand alone.
pub fn mode(
    jail: Option<String>,
    target: Option<String>,
    remote_dataset: Option<String>,
    keep: bool,
    resume: Option<String>,
    abort: Option<String>,
) -> Result<Mode> {
    let lone = |flag: &str| {
        if jail.is_some() || target.is_some() || remote_dataset.is_some() || keep {
            return Err(Error::InvalidArgument(format!(
                "{flag} takes only the jail name"
            )));
        }
        Ok(())
    };
    match (resume, abort) {
        (Some(_), Some(_)) => Err(Error::InvalidArgument(
            "--resume and --abort are exclusive".to_string(),
        )),
        (Some(jail), None) => lone("--resume").map(|()| Mode::Resume(jail)),
        (None, Some(jail)) => lone("--abort").map(|()| Mode::Abort(jail)),
        (None, None) => match (jail, target) {
            (Some(jail), Some(target)) => Ok(Mode::Start {
                jail,
                target,
                remote_dataset,
                keep,
            }),
            _ => Err(Error::InvalidArgument(
                "migrate needs <jail> <target>, or --resume <jail>, or --abort <jail>".to_string(),
            )),
        },
    }
}

pub fn handle_migrate(
    config_path: &(impl manifest::ConfigSource + ?Sized),
    mode: Mode,
) -> Result<()> {
    match mode {
        Mode::Start {
            jail,
            target,
            remote_dataset,
            keep,
        } => start(config_path, &jail, &target, remote_dataset, keep),
        Mode::Resume(jail) => resume(config_path, &jail),
        Mode::Abort(jail) => abort(config_path, &jail),
    }
}

/// The `--format json` result of `migrate --resume` and `migrate --abort`.
#[derive(Debug, Serialize)]
pub struct MigrateReport {
    pub jail: String,
    pub action: &'static str,
    pub outcome: &'static str,
    /// Where the jail went, for a completed migration.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination: Option<String>,
    pub messages: Vec<String>,
}

pub fn resume_data(
    config_path: &(impl manifest::ConfigSource + ?Sized),
    jail: &str,
) -> Result<MigrateReport> {
    let (ended, messages) = crate::output::capture_stdout(|| resume_inner(config_path, jail))?;
    Ok(MigrateReport {
        jail: jail.to_string(),
        action: "resume",
        outcome: ended.outcome,
        destination: ended.destination,
        messages,
    })
}

pub fn abort_data(
    config_path: &(impl manifest::ConfigSource + ?Sized),
    jail: &str,
) -> Result<MigrateReport> {
    let ((), messages) = crate::output::capture_stdout(|| abort(config_path, jail))?;
    Ok(MigrateReport {
        jail: jail.to_string(),
        action: "abort",
        outcome: "aborted",
        destination: None,
        messages,
    })
}

fn preflight(target: &str, remote_dataset: &str) -> Result<()> {
    let probe = Command::new("/usr/bin/ssh")
        .args(["-o", "BatchMode=yes", target])
        .args(["zfs", "list", "-H", "-o", "name", remote_dataset])
        .output()
        .map_err(|e| Error::JailOperation(format!("Failed to run ssh: {e}")))?;
    if probe.status.success() {
        return Err(Error::JailOperation(format!(
            "Dataset '{remote_dataset}' already exists on {target}"
        )));
    }
    let reachable = Command::new("/usr/bin/ssh")
        .args(["-o", "BatchMode=yes", target, "true"])
        .status()
        .map_err(|e| Error::JailOperation(format!("Failed to run ssh: {e}")))?;
    if !reachable.success() {
        return Err(Error::JailOperation(format!(
            "Cannot reach '{target}' over ssh (BatchMode)"
        )));
    }
    Ok(())
}

fn require_stopped(full_name: &str) -> Result<()> {
    if crate::jail::jail_getid(full_name).is_ok() {
        return Err(Error::JailOperation(format!(
            "Jail '{full_name}' is running; stop it before migrating"
        )));
    }
    Ok(())
}

fn start(
    config_path: &(impl manifest::ConfigSource + ?Sized),
    jail: &str,
    target: &str,
    remote_dataset: Option<String>,
    keep: bool,
) -> Result<()> {
    let config = manifest::load(config_path)?;
    let zfs = super::snapshot::require_zfs(&config)?;
    let (service_name, full_name) = config.require_jail_names(jail)?;
    require_stopped(&full_name)?;

    let store = RecordStore::from_config(&config);
    if store.exists(&full_name)? {
        return Err(refuse_second(&full_name, &store.path(&full_name)?));
    }
    let local_dataset = zfs.jail_dataset_name(&full_name);
    let remote_dataset =
        remote_dataset.unwrap_or_else(|| format!("zroot/blackship/jails/{full_name}"));
    preflight(target, &remote_dataset)?;

    let lock = store.lock(&full_name)?;
    if store.exists(&full_name)? {
        return Err(refuse_second(&full_name, &store.path(&full_name)?));
    }
    let record = Record {
        version: record::VERSION,
        jail: full_name.clone(),
        service: service_name,
        target: target.to_string(),
        remote_dataset,
        source_dataset: local_dataset,
        snapshot: crate::zfs::default_snapshot_name(),
        keep,
        started_at: crate::scope::now_unix(),
        fence: None,
        parked: None,
        compensation_failures: 0,
        saga: Saga::<STEPS>::new(saga_id(&full_name)).snapshot(),
    };
    let source_path = zfs.jail_path(&full_name);
    let host = Host::new(&config, zfs, &record);
    let mut driver = Driver::restore(host, record, &store)?;
    let settled = driver.drive();
    release(lock, &store, &full_name);
    conclude(settled?, driver.record(), &source_path)
}

/// Let go of the migration lock, and remove its file when no record is left.
fn release(lock: Lock, store: &RecordStore, jail: &str) {
    if store.exists(jail).is_ok_and(|recorded| !recorded) {
        lock.discard();
    }
}

/// Report how a start or resume ended: the destination steps, or why it did not.
fn conclude(settled: Settled, record: &Record, source_path: &Path) -> Result<()> {
    match settled {
        Settled::Completed => {
            for line in destination_steps(
                &record.target,
                &record.remote_dataset,
                &record.jail,
                &record.service,
                source_path,
            ) {
                println!("{line}");
            }
            if !record.keep {
                println!(
                    "Remove the source for good with: blackship rm {} --volumes",
                    record.service
                );
                println!(
                    "Undo the migration here with: blackship unfence {}",
                    record.service
                );
            }
            Ok(())
        }
        Settled::RolledBack {
            failed: Some((Step::Transfer, _)),
        } => Err(Error::JailOperation(
            "Migration transfer failed; the source is untouched and the partial remote dataset was removed"
                .to_string(),
        )),
        Settled::RolledBack {
            failed: Some((_, e)),
        } => Err(e),
        Settled::RolledBack { failed: None } => Err(Error::JailOperation(format!(
            "Migration of '{}' was rolled back; the source is untouched and nothing is left on {}",
            record.jail, record.target
        ))),
    }
}

/// A record to act on: the jail's name resolved, the record read and locked.
struct Recorded {
    config: manifest::BlackshipConfig,
    store: RecordStore,
    record: Record,
}

fn recorded(config_path: &(impl manifest::ConfigSource + ?Sized), jail: &str) -> Result<Recorded> {
    let config = manifest::load(config_path)?;
    let full_name = config
        .resolve_jail_names(jail)
        .map_or_else(|| jail.to_string(), |(_, full)| full);
    let store = RecordStore::from_config(&config);
    let Some(record) = store.load(&full_name)? else {
        return Err(Error::JailOperation(format!(
            "No migration is recorded for '{full_name}'"
        )));
    };
    Ok(Recorded {
        config,
        store,
        record,
    })
}

fn resume(config_path: &(impl manifest::ConfigSource + ?Sized), jail: &str) -> Result<()> {
    resume_inner(config_path, jail).map(drop)
}

/// The JSON `outcome` of a resume that settled the record without a new failure;
/// `None` when the run failed, which is an error and a nonzero exit.
const fn clean_ending(settled: &Settled) -> Option<&'static str> {
    match settled {
        Settled::Completed => Some("completed"),
        Settled::RolledBack { failed: None } => Some("rolled_back"),
        Settled::RolledBack { failed: Some(_) } => None,
    }
}

/// How a resume that settled the record ended: it finished the migration, or it
/// finished rolling one back. Anything unfinished is an `Err`.
struct Ended {
    outcome: &'static str,
    destination: Option<String>,
}

fn resume_inner(config_path: &(impl manifest::ConfigSource + ?Sized), jail: &str) -> Result<Ended> {
    let Recorded {
        config,
        store,
        record,
    } = recorded(config_path, jail)?;
    let lock = store.lock(&record.jail)?;
    let zfs = super::snapshot::require_zfs(&config)?;
    require_stopped(&record.jail)?;
    println!(
        "Resuming the migration of '{}' to {}:{} ...",
        record.jail, record.target, record.remote_dataset
    );
    let source_path = zfs.jail_path(&record.jail);
    let jail = record.jail.clone();
    let host = Host::new(&config, zfs, &record);
    let mut driver = Driver::restore(host, record, &store)?;
    let settled = driver.resume();
    release(lock, &store, &jail);
    let settled = settled?;
    let outcome = clean_ending(&settled);
    if outcome == Some("rolled_back") {
        println!(
            "Rolled back the migration of '{jail}': nothing is left on {}, and '{jail}' here is as it was.",
            driver.record().target
        );
    } else {
        conclude(settled, driver.record(), &source_path)?;
    }
    Ok(Ended {
        outcome: outcome.unwrap_or("completed"),
        destination: driver
            .record()
            .fence
            .clone()
            .filter(|_| outcome == Some("completed")),
    })
}

fn abort(config_path: &(impl manifest::ConfigSource + ?Sized), jail: &str) -> Result<()> {
    let Recorded {
        config,
        store,
        record,
    } = recorded(config_path, jail)?;
    let lock = store.lock(&record.jail)?;
    let zfs = super::snapshot::require_zfs(&config)?;
    let jail = record.jail.clone();
    let target = record.target.clone();
    let host = Host::new(&config, zfs, &record);
    let mut driver = Driver::restore(host, record, &store)?;
    let aborted = driver.abort();
    release(lock, &store, &jail);
    aborted?;
    println!(
        "Aborted the migration of '{jail}': nothing is left on {target}, and '{jail}' here is as it was."
    );
    Ok(())
}

/// `zfs send` arguments for the snapshot. A jail cloned from a release is sent as a full
/// stream: `-R` would make the target look for the clone's origin snapshot, which it
/// does not have, and the receive would fail. The cost is that its older snapshots stay behind.
const fn send_args(snapshot: &str, cloned: bool) -> [&str; 3] {
    ["send", if cloned { "-p" } else { "-R" }, snapshot]
}

/// What to do on the target. The dataset keeps the mountpoint it had here; only a target
/// with a different data directory needs to move it, and only the target knows its layout.
fn destination_steps(
    target: &str,
    remote_dataset: &str,
    full_name: &str,
    service_name: &str,
    source_path: &Path,
) -> Vec<String> {
    vec![
        format!("On {target}, finish with:"),
        format!(
            "  # the dataset arrives with mountpoint {}; if the target's data_dir differs:",
            source_path.display()
        ),
        format!("  zfs set mountpoint=<data_dir>/jails/{full_name} {remote_dataset}"),
        "  # the jail definition travels at .blackship/jail.toml inside the dataset".to_string(),
        format!("  blackship up {service_name}"),
    ]
}

/// `<host>/<dataset>@<unix time>`: where a migrated jail went, and when.
fn fence_value(target: &str, remote_dataset: &str, at: i64) -> String {
    let host = target.rsplit('@').next().unwrap_or(target);
    format!("{host}/{remote_dataset}@{at}")
}

/// Lift a migration fence.
pub fn handle_unfence(
    config_path: &(impl manifest::ConfigSource + ?Sized),
    jail: &str,
) -> Result<()> {
    let config = manifest::load(config_path)?;
    let zfs = super::snapshot::require_zfs(&config)?;
    let (_, full_name) = config.require_jail_names(jail)?;

    let Some(fence) = zfs.jail_migration_fence(&full_name) else {
        return Err(Error::JailOperation(format!(
            "Jail '{full_name}' is not fenced"
        )));
    };
    zfs.clear_jail_migration_fence(&full_name)?;
    println!("Cleared the fence on '{full_name}' (it had been migrated to {fence}).");
    println!("It can start here again; make sure it does not also run there.");
    Ok(())
}

#[cfg(test)]
mod tests;