shep-daemon 0.15.2

The supervision engine behind shep: process spawning, restart policy, the log plane, watch and cron restarts, and the RPC server
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
//! The key set each committed handover fixture must hold.
//!
//! `crates/shep-daemon/fixtures/handover/README.md` states the policy: a shape
//! is the set of keys a serialised blob holds, at the blob, at `sheep[]`, at
//! `sheep[].fds` and at `sheep[].app`. This file is that policy as data, one
//! [`Shape`] per release, and [`assert_the_shape`] checks a fixture against it.
//!
//! A key set alone catches a key going missing and nothing else: an absent key
//! and a `null` read the same, and `AppConfig` carries `#[serde(default)]`, so
//! a renamed field hands the successor that field's own default. The value
//! half is in [`old_blob_state`](super::old_blob_state).

use std::path::PathBuf;

/// The oldest blob in the set: the first release that handed a flock over at
/// all. Four descriptors per sheep, and no marker of any kind.
pub(super) const V0_1_18: &str = "shep-daemon-v0.1.18";
/// Added `stdin` and `channel` to a sheep's descriptors.
pub(super) const V0_1_20: &str = "shep-daemon-v0.1.20";
/// Added `pending_delete`, `manual`, `reload`, `ready_failed` and `reloads`.
pub(super) const V0_1_21: &str = "shep-daemon-v0.1.21";
/// Added `restart_due`.
pub(super) const V0_1_22: &str = "shep-daemon-v0.1.22";
/// Added `dog`.
pub(super) const V0_1_23: &str = "shep-daemon-v0.1.23";
/// Added `pending` and `pending_reidentifies`.
pub(super) const V0_1_30: &str = "shep-daemon-v0.1.30";
/// Added `depends_on` to the carried `AppConfig`.
pub(super) const V0_5_0: &str = "shep-daemon-v0.5.0";
/// Added `environment` to the carried `AppConfig`.
pub(super) const V0_6_2: &str = "shep-daemon-v0.6.2";
/// Added `level_rules` to the carried `AppConfig`, and dropped `increment_var`.
pub(super) const V0_8_0: &str = "shep-daemon-v0.8.0";
/// Added `dogs` to the carried `AppConfig`.
pub(super) const V0_11_0: &str = "shep-daemon-v0.11.0";
/// Added `log_timestamps` to the carried `AppConfig`.
pub(super) const V0_12_0: &str = "shep-daemon-v0.12.0";
/// Added `disabled`.
pub(super) const V0_13_0: &str = "shep-daemon-v0.13.0";

/// The keys every blob in this set has at the top level, from the first
/// release to the one before `reloads`.
const BLOB_BEFORE_RELOADS: [&str; 7] = [
    "listener_fd",
    "next_action_stamp",
    "next_deadline",
    "next_id",
    "pidfile_fd",
    "sheep",
    "version",
];

/// The same, from the release that added `reloads`.
const BLOB_WITH_RELOADS: [&str; 8] = [
    "listener_fd",
    "next_action_stamp",
    "next_deadline",
    "next_id",
    "pidfile_fd",
    "reloads",
    "sheep",
    "version",
];

/// The keys a carried sheep has from the release that wrote no marker at all.
const SHEEP_BEFORE_MARKERS: [&str; 11] = [
    "app",
    "credentials",
    "epoch",
    "fds",
    "id",
    "instance",
    "last_exit",
    "name",
    "pid",
    "restarts",
    "status",
];

/// The keys a carried sheep holds beyond [`SHEEP_BEFORE_MARKERS`] from the
/// release that wrote the first markers. Cumulative: each list below names
/// every marker its release wrote.
const SHEEP_KEYS_FROM_V0_1_21: [&str; 4] = ["manual", "pending_delete", "ready_failed", "reload"];
/// The same, plus the respawn deadline.
const SHEEP_KEYS_FROM_V0_1_22: [&str; 5] = [
    "manual",
    "pending_delete",
    "ready_failed",
    "reload",
    "restart_due",
];
/// The same, plus the dog marker.
const SHEEP_KEYS_FROM_V0_1_23: [&str; 6] = [
    "manual",
    "pending_delete",
    "ready_failed",
    "reload",
    "restart_due",
    "dog",
];
/// The same, plus the pair `pending` and `pending_reidentifies`.
const SHEEP_KEYS_FROM_V0_1_30: [&str; 8] = [
    "manual",
    "pending_delete",
    "ready_failed",
    "reload",
    "restart_due",
    "dog",
    "pending",
    "pending_reidentifies",
];
/// The same, plus `disabled`.
const SHEEP_KEYS_FROM_V0_13_0: [&str; 9] = [
    "manual",
    "pending_delete",
    "ready_failed",
    "reload",
    "restart_due",
    "dog",
    "pending",
    "pending_reidentifies",
    "disabled",
];

/// The four descriptors a sheep's output travelled on before `stdin`.
const FOUR_DESCRIPTORS: [&str; 4] = ["err_log", "err_pipe", "out_log", "out_pipe"];

/// The six, from the release that carried a stdin pipe and a channel.
const SIX_DESCRIPTORS: [&str; 6] = [
    "channel", "err_log", "err_pipe", "out_log", "out_pipe", "stdin",
];

/// The keys the `AppConfig` a sheep carries had at `shep-daemon-v0.1.18`, the
/// oldest blob in the set. Every later release's config is this list plus the
/// keys it added, minus the one v0.8.0 dropped.
const APP_AT_V0_1_18: [&str; 40] = [
    "action_timeout",
    "args",
    "autorestart",
    "autostart",
    "channel",
    "cron_restart",
    "cron_timezone",
    "cwd",
    "env",
    "err_file",
    "exp_backoff_restart_delay",
    "fold",
    "graceful_timeout",
    "group",
    "ignore_watch",
    "increment_var",
    "instances",
    "interpreter",
    "kill_signal",
    "kill_timeout",
    "listen_timeout",
    "liveness_probe",
    "max_memory",
    "max_restarts",
    "merge_logs",
    "min_uptime",
    "name",
    "out_file",
    "readiness_probe",
    "restart_delay",
    "reuse_port",
    "script",
    "shutdown_with_message",
    "stdin",
    "stop_exit_codes",
    "user",
    "wait_ready",
    "watch",
    "watch_delay",
    "watch_options",
];

/// The keys the carried config holds beyond [`APP_AT_V0_1_18`], per release
/// stage. Cumulative, as the sheep lists above are.
const APP_KEYS_FROM_V0_5_0: [&str; 1] = ["depends_on"];
/// The same, plus the environment name.
const APP_KEYS_FROM_V0_6_2: [&str; 2] = ["depends_on", "environment"];
/// The same, plus the level rules.
const APP_KEYS_FROM_V0_8_0: [&str; 3] = ["depends_on", "environment", "level_rules"];
/// The same, plus the dog table.
const APP_KEYS_FROM_V0_11_0: [&str; 4] = ["depends_on", "environment", "level_rules", "dogs"];
/// The same, plus the log timestamp switch.
const APP_KEYS_FROM_V0_12_0: [&str; 5] = [
    "depends_on",
    "environment",
    "level_rules",
    "dogs",
    "log_timestamps",
];
/// The config key v0.8.0 stopped writing, and every release after it.
const APP_KEYS_DROPPED_AT_V0_8_0: [&str; 1] = ["increment_var"];

/// Where the committed bytes live.
pub(super) fn fixtures_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("fixtures/handover")
}

/// Where a release's committed bytes live. A fixture is named for the tag that
/// wrote it, so `stem` is that tag, or the tag plus `-populated`.
pub(super) fn fixture_path(stem: &str) -> PathBuf {
    fixtures_dir().join(format!("{stem}.json"))
}

/// A fixture as raw JSON, so a test can name the keys it holds rather than
/// only the fields this image has a place for.
pub(super) fn fixture_value(stem: &str) -> serde_json::Value {
    let path = fixture_path(stem);
    let text = std::fs::read_to_string(&path)
        .unwrap_or_else(|error| panic!("the fixture {} is not readable: {error}", path.display()));
    serde_json::from_str(&text)
        .unwrap_or_else(|error| panic!("the fixture {} is not JSON: {error}", path.display()))
}

/// The keys one JSON object holds, sorted: the shape its writer painted at
/// that level of the blob.
fn keys(object: &serde_json::Value) -> Vec<String> {
    let mut found: Vec<String> = object
        .as_object()
        .expect("a blob, a carried sheep, its descriptors and its config are all objects")
        .keys()
        .cloned()
        .collect();
    found.sort_unstable();
    found
}

/// A literal list of key names, sorted the way [`keys`] sorts a real one.
fn sorted<T: AsRef<str>>(names: &[T]) -> Vec<String> {
    let mut found: Vec<String> = names.iter().map(|key| key.as_ref().to_owned()).collect();
    found.sort_unstable();
    found
}

/// `base`'s keys plus the ones `added` names, sorted: the shape a later release
/// wrote, read off the earlier shape this file pins as a literal.
fn sorted_plus<T: AsRef<str>>(base: &[T], added: &[&str]) -> Vec<String> {
    let mut found = sorted(base);
    found.extend(added.iter().map(|key| (*key).to_owned()));
    found.sort_unstable();
    found
}

/// The config keys a release wrote: [`APP_AT_V0_1_18`], plus the ones it added,
/// minus the ones it stopped writing.
fn app_shape(added: &[&str], dropped: &[&str]) -> Vec<String> {
    let mut found = sorted(&APP_AT_V0_1_18);
    found.extend(added.iter().map(|key| (*key).to_owned()));
    found.retain(|key| !dropped.contains(&key.as_str()));
    found.sort_unstable();
    found
}

/// What one release wrote at every level the policy in
/// `fixtures/handover/README.md` counts as part of a shape.
struct Shape {
    /// The keys the blob holds at its own level.
    blob: &'static [&'static str],
    /// The keys a carried sheep holds beyond [`SHEEP_BEFORE_MARKERS`].
    sheep: &'static [&'static str],
    /// The descriptors a carried sheep carries.
    fds: &'static [&'static str],
    /// The config keys it holds beyond [`APP_AT_V0_1_18`].
    app_added: &'static [&'static str],
    /// The config keys it no longer holds.
    app_dropped: &'static [&'static str],
}

impl Shape {
    /// One release's shape, each level named as the keys beyond the level's
    /// own base list.
    const fn at(
        blob: &'static [&'static str],
        sheep: &'static [&'static str],
        fds: &'static [&'static str],
        app_added: &'static [&'static str],
        app_dropped: &'static [&'static str],
    ) -> Self {
        Self {
            blob,
            sheep,
            fds,
            app_added,
            app_dropped,
        }
    }
}

/// Each committed fixture's shape, oldest first. Every fixture in
/// `fixtures/handover/` has an entry here, which the guard in
/// [`old_blobs`](super::old_blobs) checks against the files on disk.
const SHAPES: [(&str, Shape); 12] = [
    (
        V0_1_18,
        Shape::at(&BLOB_BEFORE_RELOADS, &[], &FOUR_DESCRIPTORS, &[], &[]),
    ),
    (
        V0_1_20,
        Shape::at(&BLOB_BEFORE_RELOADS, &[], &SIX_DESCRIPTORS, &[], &[]),
    ),
    (
        V0_1_21,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_21,
            &SIX_DESCRIPTORS,
            &[],
            &[],
        ),
    ),
    (
        V0_1_22,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_22,
            &SIX_DESCRIPTORS,
            &[],
            &[],
        ),
    ),
    (
        V0_1_23,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_23,
            &SIX_DESCRIPTORS,
            &[],
            &[],
        ),
    ),
    (
        V0_1_30,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_30,
            &SIX_DESCRIPTORS,
            &[],
            &[],
        ),
    ),
    (
        V0_5_0,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_30,
            &SIX_DESCRIPTORS,
            &APP_KEYS_FROM_V0_5_0,
            &[],
        ),
    ),
    (
        V0_6_2,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_30,
            &SIX_DESCRIPTORS,
            &APP_KEYS_FROM_V0_6_2,
            &[],
        ),
    ),
    (
        V0_8_0,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_30,
            &SIX_DESCRIPTORS,
            &APP_KEYS_FROM_V0_8_0,
            &APP_KEYS_DROPPED_AT_V0_8_0,
        ),
    ),
    (
        V0_11_0,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_30,
            &SIX_DESCRIPTORS,
            &APP_KEYS_FROM_V0_11_0,
            &APP_KEYS_DROPPED_AT_V0_8_0,
        ),
    ),
    (
        V0_12_0,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_1_30,
            &SIX_DESCRIPTORS,
            &APP_KEYS_FROM_V0_12_0,
            &APP_KEYS_DROPPED_AT_V0_8_0,
        ),
    ),
    (
        V0_13_0,
        Shape::at(
            &BLOB_WITH_RELOADS,
            &SHEEP_KEYS_FROM_V0_13_0,
            &SIX_DESCRIPTORS,
            &APP_KEYS_FROM_V0_12_0,
            &APP_KEYS_DROPPED_AT_V0_8_0,
        ),
    ),
];

/// Assert a committed fixture holds exactly the key set the release it is
/// named for wrote, at the blob, at every sheep it carries, at that sheep's
/// descriptors and at its config. This is the issue's "each fails if its file's
/// bytes change shape": trimming a key, adding one, or swapping in another
/// release's bytes fails here rather than loading quietly at a
/// `#[serde(default)]`.
///
/// `stem` is a fixture's file name without its extension, so a populated
/// fixture carries its tag plus `-populated`; the shape it must hold is the one
/// named by the tag in front of that suffix. `sheep_count` is how many sheep
/// that fixture carries: one for a plain fixture, three for a populated one.
///
/// # Panics
///
/// Panics if `stem` names no fixture in [`SHAPES`], if the file is unreadable
/// or not JSON, or if its `sheep` is not an array.
#[track_caller]
pub(super) fn assert_the_shape(stem: &str, sheep_count: usize) {
    let tag = stem.strip_suffix("-populated").unwrap_or(stem);
    let (_, shape) = SHAPES
        .iter()
        .find(|(name, _)| *name == tag)
        .unwrap_or_else(|| panic!("{stem} names no release in SHAPES"));
    let raw = fixture_value(stem);

    assert_eq!(
        keys(&raw),
        sorted(shape.blob),
        "{stem}: the keys the blob holds at its own level"
    );

    let flock = raw["sheep"]
        .as_array()
        .expect("a blob's sheep are an array");
    assert_eq!(
        flock.len(),
        sheep_count,
        "{stem}: how many sheep the blob carries"
    );
    for (index, carried) in flock.iter().enumerate() {
        assert_eq!(
            keys(carried),
            sorted_plus(&SHEEP_BEFORE_MARKERS, shape.sheep),
            "{stem}: the keys the sheep at index {index} holds"
        );
        assert_eq!(
            keys(&carried["fds"]),
            sorted(shape.fds),
            "{stem}: the descriptors the sheep at index {index} carries"
        );
        assert_eq!(
            keys(&carried["app"]),
            app_shape(shape.app_added, shape.app_dropped),
            "{stem}: the config keys the sheep at index {index} carries"
        );
    }
}