1use std::collections::BTreeMap;
6use std::path::PathBuf;
7
8use isoloom_manifest::{Manifest, OutputRole, Protocol, Role};
9use serde::{Deserialize, Serialize};
10
11use crate::state::RunState;
12use crate::store::RunRecord;
13
14#[derive(Debug, Clone, Serialize)]
18pub struct ShellCommand {
19 pub program: String,
21 pub args: Vec<String>,
23 pub cwd: Option<PathBuf>,
25 #[serde(default)]
29 pub env: Vec<(String, String)>,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "snake_case")]
35#[non_exhaustive]
36pub enum MachineState {
37 Running,
39 Unhealthy,
41 Exited(i32),
43 Stopped,
45 Absent,
47 Unknown(String),
49}
50
51impl std::fmt::Display for MachineState {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 match self {
54 MachineState::Running => f.write_str("running"),
55 MachineState::Unhealthy => f.write_str("unhealthy"),
56 MachineState::Exited(code) => write!(f, "exited({code})"),
57 MachineState::Stopped => f.write_str("stopped"),
58 MachineState::Absent => f.write_str("absent"),
59 MachineState::Unknown(s) => f.write_str(s),
60 }
61 }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
66#[non_exhaustive]
67pub struct PublishedPort {
68 pub port: u16,
70 pub protocol: Protocol,
72 pub host_port: u16,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
78#[non_exhaustive]
79pub struct MachineStatus {
80 pub name: String,
82 pub role: Role,
84 pub state: MachineState,
86 pub addresses: BTreeMap<String, String>,
88 pub published: Vec<PublishedPort>,
90}
91
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
94#[non_exhaustive]
95pub struct NetworkView {
96 pub name: String,
98 pub cidr: String,
100 pub internet: bool,
102 pub native: Option<String>,
104}
105
106#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
108#[non_exhaustive]
109pub struct OutputValue {
110 pub name: String,
112 pub role: OutputRole,
114 pub sensitive: bool,
116 pub value: Option<String>,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122#[non_exhaustive]
123pub struct Status {
124 pub state: RunState,
126 pub provider: Option<String>,
128 pub machines: Vec<MachineStatus>,
130 pub networks: Vec<NetworkView>,
132 pub outputs: Vec<OutputValue>,
134 pub message: Option<String>,
136}
137
138#[derive(Debug, Clone, PartialEq, Eq)]
140#[non_exhaustive]
141pub struct Started {
142 pub record: RunRecord,
144 pub outputs: Vec<OutputValue>,
146 pub message: Option<String>,
148}
149
150pub(crate) fn derive_state(machines: &[MachineStatus], record: Option<&RunRecord>) -> RunState {
153 let present = machines.iter().any(|m| m.state != MachineState::Absent);
154 if !present {
155 return RunState::Absent;
156 }
157 let targets: Vec<&MachineStatus> = machines.iter().filter(|m| m.role == Role::Target).collect();
158 let running = |m: &&MachineStatus| m.state == MachineState::Running;
159 if record.is_some_and(|r| r.state == RunState::Parked) && !targets.iter().any(running) {
160 return RunState::Parked;
161 }
162 if record.is_some_and(|r| r.state == RunState::Failed) {
165 return RunState::Failed;
166 }
167 if !targets.is_empty() && targets.iter().all(running) {
168 RunState::Ready
169 } else {
170 RunState::Failed
171 }
172}
173
174pub(crate) fn any_target_running(machines: &[MachineStatus]) -> bool {
176 machines
177 .iter()
178 .any(|m| m.role == Role::Target && matches!(m.state, MachineState::Running | MachineState::Unhealthy))
179}
180
181pub(crate) fn networks(manifest: &Manifest, native: impl Fn(&isoloom_manifest::Template) -> Option<String>) -> Vec<NetworkView> {
183 manifest
184 .networks
185 .iter()
186 .map(|n| NetworkView {
187 name: n.name.to_string(),
188 cidr: n.cidr.to_string(),
189 internet: n.internet,
190 native: n.native.as_ref().and_then(&native),
191 })
192 .collect()
193}
194
195#[cfg(test)]
196mod tests {
197 use super::*;
198
199 fn machine(role: Role, state: MachineState) -> MachineStatus {
200 MachineStatus {
201 name: "m".into(),
202 role,
203 state,
204 addresses: BTreeMap::new(),
205 published: Vec::new(),
206 }
207 }
208
209 #[test]
210 fn a_run_is_ready_when_every_target_runs() {
211 use MachineState::{Absent, Exited, Running, Stopped};
212 let all = |ms: Vec<MachineStatus>| derive_state(&ms, None);
213 assert_eq!(all(vec![machine(Role::Target, Absent), machine(Role::Job, Absent)]), RunState::Absent);
214 assert_eq!(
215 all(vec![
216 machine(Role::Target, Running),
217 machine(Role::Job, Exited(0)),
218 machine(Role::Infra, Running)
219 ]),
220 RunState::Ready
221 );
222 assert_eq!(all(vec![machine(Role::Target, Running), machine(Role::Target, Exited(1))]), RunState::Failed);
223 assert_eq!(all(vec![machine(Role::Target, Stopped)]), RunState::Failed);
224 assert_eq!(all(vec![machine(Role::Target, MachineState::Unhealthy)]), RunState::Failed);
225 }
226
227 #[test]
228 fn a_parked_run_with_nothing_running_is_parked() {
229 let mut record = crate::store::RunRecord::fresh(
230 crate::name::RunName::new("r").unwrap(),
231 "/x".into(),
232 String::new(),
233 isoloom_manifest::Target::Docker,
234 isoloom_manifest::Driver::Compose,
235 );
236 record.state = RunState::Parked;
237 let stopped = vec![machine(Role::Target, MachineState::Exited(0))];
238 assert_eq!(derive_state(&stopped, Some(&record)), RunState::Parked);
239 assert!(!any_target_running(&stopped));
240 }
241}