isothrum-runner 3.0.1

The Isothrum runner: runs a Compose project, a Vagrantfile or a Terraform module through its own tool (lifecycle, state and locks, the format drivers, events).
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
//! What a failure says besides its code and message, as typed fields an embedder acts on (the
//! port to free, the tool to install and the version it needs, how long a download stalled, the
//! server that refused a sign-in, the secret to give). Versioned ([`DETAIL_VERSION`]), so a field
//! a later runner adds is a new version, never a changed meaning.
//!
//! Never a secret. What a tool printed (a download's reason, a timed-out step's last line) is
//! masked as the message is, wherever the event stream masks it.

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use super::{Error, TimedOut};
use crate::state::RunState;

/// The version of [`ErrorDetail`]'s fields.
pub const DETAIL_VERSION: u32 = 2;

/// A failure's typed fields.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[non_exhaustive]
pub struct ErrorDetail {
    /// The version of the fields ([`DETAIL_VERSION`]).
    pub v: u32,
    /// The fields, by the failure's kind.
    #[serde(flatten)]
    pub data: Detail,
}

/// What a download fetches.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum DownloadKind {
    /// Container images (a Compose start's pull).
    Images,
    /// Vagrant boxes (what a Vagrant start boots).
    Boxes,
}

/// What a computer lacks for a Vagrant provider to run a run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ProviderNeed {
    /// The Vagrant plugin the provider runs through (`Provider::plugin`: vagrant-qemu,
    /// vagrant-libvirt, ...) isn't installed. A caller installs it for this account
    /// (`vagrant plugin install <plugin>`, no administrator), then starts again.
    Plugin,
    /// virtiofsd, which libvirt and QEMU start to share a host folder with a VM through virtiofs
    /// (Linux): none is installed where they look.
    Virtiofsd,
    /// The automation of the hypervisor's app: the program driving it through Apple events (UTM,
    /// by `vagrant_utm`) may not send them (`-1743`, `Not authorized to send Apple events`, macOS's
    /// Automation permission). Waiting doesn't fix it; the user's settings do.
    Automation,
    /// The system's approval of the hypervisor's app: it answered the machine's start, driven
    /// through Apple events, with the system's `Operation not permitted` (UTM's `utmctl start`:
    /// `OSStatus error -2700`) though the events reached it. The system refused to run the app's
    /// own helper that launches machines: an app downloaded and quarantined, never approved when
    /// opened, whose sandboxed helper gets no prompt. Waiting doesn't fix it; opening the app once
    /// from the system's file manager, or reinstalling it, does.
    AppApproval,
}

/// The process that holds a host port: what a `port_in_use` names so the user knows what to stop.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[non_exhaustive]
pub struct PortHolder {
    /// Its process id.
    pub pid: u32,
    /// Its program's name, as the system lists it (`nginx`, `com.docke`: `lsof` cuts it to 9
    /// characters).
    pub program: String,
}

impl PortHolder {
    /// Process `pid` running `program`.
    #[must_use]
    pub fn new(pid: u32, program: impl Into<String>) -> PortHolder {
        PortHolder { pid, program: program.into() }
    }
}

/// A failure's typed fields, by its kind.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Detail {
    /// A host port the run publishes is taken.
    PortInUse {
        /// The port.
        port: u16,
        /// `tcp` or `udp`.
        protocol: String,
        /// The process listening on it, when it was found.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        holder: Option<PortHolder>,
    },
    /// A tool isn't installed.
    ToolMissing {
        /// The tool.
        tool: String,
    },
    /// A tool is older than the run needs.
    ToolTooOld {
        /// The tool.
        tool: String,
        /// The version it answered with.
        found: String,
        /// The oldest version the run takes.
        required: String,
    },
    /// A tool's service didn't answer (Docker's engine).
    ToolUnanswered {
        /// The tool.
        tool: String,
    },
    /// A step ran out of time.
    Timeout {
        /// Which limit it ran into.
        timed_out: TimedOut,
    },
    /// A download showed no progress for as long as a download may.
    DownloadStalled {
        /// What it downloads.
        what: DownloadKind,
        /// How long it went with no progress, in seconds.
        after_s: u64,
    },
    /// A download couldn't be made.
    DownloadFailed {
        /// What it downloads.
        what: DownloadKind,
        /// The last try's last error lines, or its exit status (masked).
        reason: String,
    },
    /// The elevation the run's commands need couldn't be had.
    Elevation {
        /// Why (the prompt declined, the elevated session ended).
        reason: String,
    },
    /// A ready condition didn't hold.
    NotReady {
        /// The condition (`every machine of the run`, `tcp 127.0.0.1:8080`).
        condition: String,
        /// Why it didn't.
        detail: String,
    },
    /// The run is recorded on another place than the one asked for.
    RunningElsewhere {
        /// Where it is recorded.
        recorded: String,
        /// Where it was asked for.
        asked: String,
    },
    /// The run can't be parked in that mode.
    NotParkable {
        /// The mode.
        mode: String,
    },
    /// Machines of the run are gone, so it can't be resumed whole.
    PartlyGone {
        /// The machines gone.
        machines: Vec<String>,
    },
    /// A secret the run needs has no value.
    SecretMissing {
        /// Its name.
        name: String,
    },
    /// A value the run was given can't be used.
    InputInvalid {
        /// Its name.
        name: String,
        /// Why (masked: it may quote the value).
        reason: String,
    },
    /// A step's tool ran and failed.
    ToolFailed {
        /// Its last error lines, or its exit status (masked).
        reason: String,
    },
    /// The folder isn't a project to run.
    ProjectInvalid {
        /// The folder.
        dir: PathBuf,
    },
    /// A machine the run doesn't have.
    MachineUnknown {
        /// The machine asked for.
        machine: String,
    },
    /// A network the run doesn't have.
    NetworkUnknown {
        /// The network asked for.
        network: String,
    },
    /// A Vagrant provider refused.
    ProviderRefused {
        /// The provider.
        provider: String,
        /// What this computer lacks for it, when that is what refused it.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        need: Option<ProviderNeed>,
    },
    /// A tool refused this account.
    ToolDenied {
        /// The tool.
        tool: String,
    },
    /// A cloud refused the sign-in the run's tools use.
    CredentialsExpired {
        /// The cloud, when the failure names its Terraform provider.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        cloud: Option<crate::project::platform::Cloud>,
        /// What the tool said (masked).
        reason: String,
    },
    /// A network the run makes needs an address block another network holds.
    AddressPoolOverlap {
        /// What the tool said (masked).
        reason: String,
    },
    /// A machine has no graphical console to reach.
    NoConsole {
        /// The machine.
        machine: String,
    },
    /// The server the run is on refused its credentials.
    ServerAuthFailed {
        /// The server, when known.
        server: Option<String>,
    },
    /// A file or folder couldn't be read or written.
    Io {
        /// The path.
        path: PathBuf,
        /// What was being done (`read`, `create`).
        action: String,
    },
    /// The run moved between a read and the write that would have moved it.
    StateConflict {
        /// Where the write expected it.
        expected: RunState,
        /// Where it was.
        found: Option<RunState>,
    },
    /// A live process holds the lease asked for.
    LeaseHeld {
        /// What the lease covers.
        scope: String,
        /// The holder's process id.
        pid: u32,
    },
}

impl Error {
    /// The failure's typed fields, for one that has any beyond its code and message.
    #[must_use]
    pub fn detail(&self) -> Option<ErrorDetail> {
        let data = self.tool_detail().or_else(|| self.run_detail())?;
        Some(ErrorDetail { v: DETAIL_VERSION, data })
    }

    /// The fields of a failure of a tool, a download or a step.
    fn tool_detail(&self) -> Option<Detail> {
        Some(match self {
            Error::PortInUse { port, protocol, holder } => Detail::PortInUse {
                port: port.get(),
                protocol: protocol.clone(),
                holder: holder.clone(),
            },
            Error::ToolMissing { tool, .. } => Detail::ToolMissing { tool: tool.clone() },
            Error::ToolTooOld { tool, found, required } => Detail::ToolTooOld {
                tool: tool.clone(),
                found: found.clone(),
                required: required.clone(),
            },
            Error::ToolUnanswered { tool, .. } => Detail::ToolUnanswered { tool: tool.clone() },
            Error::Timeout { timed_out, .. } => Detail::Timeout { timed_out: timed_out.clone() },
            Error::DownloadStalled { what, after_s, .. } => Detail::DownloadStalled {
                what: *what,
                after_s: *after_s,
            },
            Error::DownloadFailed { what, reason, .. } => Detail::DownloadFailed {
                what: *what,
                reason: reason.clone(),
            },
            Error::Elevation { reason } => Detail::Elevation { reason: reason.clone() },
            Error::NotReady { condition, detail } => Detail::NotReady {
                condition: condition.clone(),
                detail: detail.clone(),
            },
            Error::ToolFailed { reason, .. } => Detail::ToolFailed { reason: reason.clone() },
            Error::ToolDenied { tool, .. } => Detail::ToolDenied { tool: tool.clone() },
            Error::CredentialsExpired { cloud, reason, .. } => Detail::CredentialsExpired {
                cloud: *cloud,
                reason: reason.clone(),
            },
            Error::AddressPoolOverlap { reason, .. } => Detail::AddressPoolOverlap { reason: reason.clone() },
            _ => return None,
        })
    }

    /// The fields of a failure of the run, its inputs or the home.
    fn run_detail(&self) -> Option<Detail> {
        Some(match self {
            Error::RunningElsewhere { recorded, asked, .. } => Detail::RunningElsewhere {
                recorded: recorded.clone(),
                asked: asked.clone(),
            },
            Error::NotParkable { mode, .. } => Detail::NotParkable { mode: (*mode).to_owned() },
            Error::PartlyGone { machines, .. } => Detail::PartlyGone { machines: machines.clone() },
            Error::SecretMissing { name } => Detail::SecretMissing { name: name.clone() },
            Error::Input { name, reason } => Detail::InputInvalid {
                name: name.clone(),
                reason: reason.clone(),
            },
            Error::Project { dir, .. } => Detail::ProjectInvalid { dir: dir.clone() },
            Error::MachineUnknown { machine, .. } => Detail::MachineUnknown { machine: machine.clone() },
            Error::NetworkUnknown { network, .. } => Detail::NetworkUnknown { network: network.clone() },
            Error::Provider { provider, need, .. } => Detail::ProviderRefused {
                provider: provider.clone(),
                need: *need,
            },
            Error::NoConsole { machine, .. } => Detail::NoConsole { machine: machine.clone() },
            Error::ServerAuthFailed { server, .. } => Detail::ServerAuthFailed { server: server.clone() },
            Error::Io { path, action, .. } => Detail::Io {
                path: path.clone(),
                action: (*action).to_owned(),
            },
            Error::StateConflict { expected, found, .. } => Detail::StateConflict {
                expected: *expected,
                found: *found,
            },
            Error::LeaseHeld { scope, pid } => Detail::LeaseHeld {
                scope: scope.clone(),
                pid: *pid,
            },
            _ => return None,
        })
    }
}

impl ErrorDetail {
    /// The same, the text a tool printed passed through `mask`.
    pub(crate) fn masked(self, mask: impl Fn(&str) -> String) -> ErrorDetail {
        let data = match self.data {
            Detail::Timeout { timed_out } => Detail::Timeout {
                timed_out: timed_out.masked(&mask),
            },
            Detail::DownloadFailed { what, reason } => Detail::DownloadFailed { what, reason: mask(&reason) },
            Detail::Elevation { reason } => Detail::Elevation { reason: mask(&reason) },
            Detail::ToolFailed { reason } => Detail::ToolFailed { reason: mask(&reason) },
            Detail::CredentialsExpired { cloud, reason } => Detail::CredentialsExpired { cloud, reason: mask(&reason) },
            Detail::AddressPoolOverlap { reason } => Detail::AddressPoolOverlap { reason: mask(&reason) },
            Detail::InputInvalid { name, reason } => Detail::InputInvalid { name, reason: mask(&reason) },
            Detail::NotReady { condition, detail } => Detail::NotReady {
                condition,
                detail: mask(&detail),
            },
            other => other,
        };
        ErrorDetail { data, ..self }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_port_in_use_says_its_port_versioned_and_flat() {
        let e = Error::PortInUse {
            port: crate::port::HostPort::new(8080),
            protocol: "tcp".to_owned(),
            holder: None,
        };
        let json = serde_json::to_value(e.detail().expect("a detail")).expect("json");
        assert_eq!(json, serde_json::json!({ "v": 2, "kind": "port_in_use", "port": 8080, "protocol": "tcp" }));
        let held = Error::PortInUse {
            port: crate::port::HostPort::new(8080),
            protocol: "tcp".to_owned(),
            holder: Some(PortHolder::new(4123, "nginx")),
        };
        let held_json = serde_json::to_value(held.detail().expect("a detail")).expect("json");
        assert_eq!(held_json["holder"], serde_json::json!({ "pid": 4123, "program": "nginx" }));
        assert!(held.to_string().ends_with(": `nginx` (pid 4123) listens on it"), "{held}");
    }

    #[test]
    fn what_a_tool_printed_in_the_fields_is_masked_as_the_message_is() {
        let timeout = Error::Timeout {
            step: "up".to_owned(),
            timed_out: TimedOut::NoProgress {
                after_s: 30,
                last_line: Some("token=s3cret".to_owned()),
            },
        };
        let failed = Error::DownloadFailed {
            what: DownloadKind::Images,
            step: "pull-up".to_owned(),
            reason: "denied for s3cret".to_owned(),
        };
        for e in [timeout, failed] {
            let detail = e.detail().expect("a detail");
            assert!(serde_json::to_string(&detail).expect("json").contains("s3cret"));
            let masked = serde_json::to_string(&detail.masked(|t| t.replace("s3cret", "***"))).expect("json");
            assert!(!masked.contains("s3cret"), "{masked}");
        }
    }

    #[test]
    fn a_download_says_what_it_fetches() {
        let e = Error::DownloadStalled {
            what: DownloadKind::Boxes,
            step: "up".to_owned(),
            after_s: 900,
            last_line: None,
        };
        let json = serde_json::to_value(e.detail().expect("a detail")).expect("json");
        assert_eq!(json, serde_json::json!({ "v": 2, "kind": "download_stalled", "what": "boxes", "after_s": 900 }));
    }

    #[test]
    fn a_failure_with_only_a_message_has_no_detail() {
        let e = Error::Locked { run: "r".to_owned() };
        assert!(e.detail().is_none());
    }
}