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
//! The words for what a run is doing and where it stands, shared by the API, the record and the
//! event stream.
use std::fmt;
use serde::{Deserialize, Serialize};
/// An operation on a run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Op {
/// Start the run.
Up,
/// Remove everything the run created.
Down,
/// Stop the run, keeping what it created.
Park,
/// Start a parked run again.
Resume,
/// Run the run's checks.
Check,
/// Run the set-up again on running machines.
Provision,
/// Hot-add the on-demand access machine into the running run.
Attach,
/// Hot-remove the on-demand access machine from the running run.
Detach,
/// Bring the running run up to date with its manifest.
Update,
/// Take a machine out of the running run, ahead of its removal from the manifest.
Retire,
}
impl Op {
/// Every operation, in the order they are declared.
pub(crate) const ALL: [Op; 10] = [
Op::Up,
Op::Down,
Op::Park,
Op::Resume,
Op::Check,
Op::Provision,
Op::Attach,
Op::Detach,
Op::Update,
Op::Retire,
];
/// The operation as written in events (`up`).
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Op::Up => "up",
Op::Down => "down",
Op::Park => "park",
Op::Resume => "resume",
Op::Check => "check",
Op::Provision => "provision",
Op::Attach => "attach",
Op::Detach => "detach",
Op::Update => "update",
Op::Retire => "retire",
}
}
/// The state a run is in while this operation runs.
pub(crate) fn during(self) -> Option<RunState> {
match self {
Op::Up => Some(RunState::Starting),
Op::Down => Some(RunState::Stopping),
Op::Park => Some(RunState::Parking),
Op::Resume => Some(RunState::Resuming),
// An update changes a run that keeps running: it stays ready meanwhile.
Op::Check | Op::Provision | Op::Attach | Op::Detach | Op::Update | Op::Retire => None,
}
}
}
impl fmt::Display for Op {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// Where a run stands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum RunState {
/// Being started.
Starting,
/// Up, every member machine running.
Ready,
/// Something of it exists, but not every member machine runs (a failed start, a machine down).
Failed,
/// Being parked.
Parking,
/// Parked: stopped, what it created kept.
Parked,
/// Being resumed.
Resuming,
/// Being removed.
Stopping,
/// Nothing of it exists.
Absent,
}
impl RunState {
/// Whether the run is in the middle of an operation that changes it: a state some operation
/// holds while it runs ([`Op::during`]).
pub(crate) fn is_moving(self) -> bool {
self.moved_by().is_some()
}
/// The operation that holds the run in this state while it runs ([`Op::during`]), if any:
/// the one a record left in it was cut short in, when no operation holds the run.
pub(crate) fn moved_by(self) -> Option<Op> {
Op::ALL.into_iter().find(|op| op.during() == Some(self))
}
/// The state as written in events (`ready`).
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
RunState::Starting => "starting",
RunState::Ready => "ready",
RunState::Failed => "failed",
RunState::Parking => "parking",
RunState::Parked => "parked",
RunState::Resuming => "resuming",
RunState::Stopping => "stopping",
RunState::Absent => "absent",
}
}
}
impl fmt::Display for RunState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// How a run is parked: the manifest's `lifecycle.park.pause` or `.shutdown` steps.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Park {
/// Suspended, memory kept where the target can (Docker: stopped).
Pause,
/// Shut down.
Shutdown,
}
impl Park {
/// The mode as the manifest names it.
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Park::Pause => "pause",
Park::Shutdown => "shutdown",
}
}
}
impl fmt::Display for Park {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_op_state_and_park_mode_is_written_as_serde_writes_it() {
crate::testing::assert_named_as_serde(Op::as_str);
crate::testing::assert_named_as_serde(RunState::as_str);
crate::testing::assert_named_as_serde(Park::as_str);
}
#[test]
fn every_op_is_listed_and_the_moving_states_are_those_an_op_holds() {
assert_eq!(crate::testing::every_variant::<Op>(), Op::ALL);
let moving: Vec<RunState> = crate::testing::every_variant::<RunState>().into_iter().filter(|s| s.is_moving()).collect();
assert_eq!(moving, [RunState::Starting, RunState::Parking, RunState::Resuming, RunState::Stopping]);
}
}