Skip to main content

isothrum_machine/setup/
walk.rs

1//! A whole setup run on this computer, step by step ([`walk`]): each step of the plan run by the
2//! caller's own runner of it, the machine checked again after each (an install changes what comes
3//! next: Docker installed asks to start it, or for a new login). What a UI does one click at a
4//! time, for a caller without one: a command line, a headless setup.
5
6use serde::Serialize;
7
8use super::{Action, Machine, Options, Step, Wanted, plan};
9
10/// What a walk tells as it goes; `O`: how the caller's runner says a step ended.
11#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
12#[serde(tag = "event", rename_all = "camelCase")]
13pub enum Event<O> {
14    /// A step begins: its item, its kind ([`Action::kind`]) and what it does.
15    Started {
16        /// The item it is about (`docker`, `engine:docker-engine`, `virtualbox`, …).
17        item: String,
18        /// Its kind.
19        kind: &'static str,
20        /// The action.
21        action: Action,
22    },
23    /// A line of the step's output.
24    Line {
25        /// The step's item.
26        item: String,
27        /// The line.
28        line: String,
29    },
30    /// The step ended.
31    Ended {
32        /// The step's item.
33        item: String,
34        /// How.
35        #[serde(flatten)]
36        outcome: O,
37    },
38}
39
40/// What a walk does once a step failed.
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub enum OnFailure {
43    /// Stop there: a caller who fixes it and runs it again.
44    Stop,
45    /// Go on with the other steps: a pass that reports every item.
46    Continue,
47}
48
49/// Where a walk ended.
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub struct Walked {
52    /// The restart or the new login that ends the pass, when one does: the next pass picks up
53    /// after it.
54    pub ends: Option<Step>,
55    /// The items of the steps run, in order.
56    pub acted: Vec<String>,
57    /// The first step that failed, when one did.
58    pub failed: Option<Step>,
59}
60
61/// How a walk runs its steps: the caller's own runner of each, and its machine check.
62pub struct Steps<R, F, C> {
63    /// Runs a step, its output lines into the sink it is given: how it ended.
64    pub run: R,
65    /// Whether how a step ended is a failure.
66    pub failed: F,
67    /// Checks the machine again: what it found, with its options.
68    pub check: C,
69}
70
71/// Walks the setup `wanted` from `machine` and its options: the next step of the plan not done
72/// yet (a restart or a new login last, never run) run by `steps`, told to `on`, the machine
73/// checked again after it, until none is left, or (`on_failure`) one fails.
74pub async fn walk<O, R, F, C>(
75    wanted: &Wanted,
76    mut machine: (Machine, Options),
77    on_failure: OnFailure,
78    mut steps: Steps<R, F, C>,
79    mut on: impl FnMut(Event<O>),
80) -> Walked
81where
82    R: AsyncFnMut(&Step, &mut dyn FnMut(String)) -> O,
83    F: Fn(&O) -> bool,
84    C: AsyncFnMut() -> (Machine, Options),
85{
86    let mut done: Vec<Step> = Vec::new();
87    let mut first_failed = None;
88    loop {
89        let p = plan(&machine.0, &machine.1, wanted);
90        let next = p.steps.iter().find(|s| !s.action.ends_pass() && !done.contains(s)).cloned();
91        let Some(step) = next.filter(|_| first_failed.is_none() || on_failure == OnFailure::Continue) else {
92            let acted = done.into_iter().map(|s| s.item).collect();
93            return Walked {
94                ends: p.steps.into_iter().find(|s| s.action.ends_pass()),
95                acted,
96                failed: first_failed,
97            };
98        };
99        on(Event::Started {
100            item: step.item.clone(),
101            kind: step.action.kind(),
102            action: step.action.clone(),
103        });
104        let outcome = {
105            let mut line = |line| {
106                on(Event::Line { item: step.item.clone(), line });
107            };
108            (steps.run)(&step, &mut line).await
109        };
110        if (steps.failed)(&outcome) && first_failed.is_none() {
111            first_failed = Some(step.clone());
112        }
113        on(Event::Ended {
114            item: step.item.clone(),
115            outcome,
116        });
117        done.push(step);
118        machine = (steps.check)().await;
119    }
120}