forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.7 MiB37e985dTahinli2 hours ago363 commits

ci.rs430 lines, 10.6 KiBHistoryRawDownload
1use std::collections::BTreeMap;
2
3use forge_ci::proto::{MAX_CONCURRENCY, MIN_CONCURRENCY};
4
5pub use forge_ci::proto::RestartMode;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum RunState {
9 WaitingApproval,
10 Queued,
11 Running,
12 Success,
13 Failure,
14 Cancelled,
15 Error,
16}
17
18string_enum!(RunState {
19 WaitingApproval = "waiting_approval",
20 Queued = "queued",
21 Running = "running",
22 Success = "success",
23 Failure = "failure",
24 Cancelled = "cancelled",
25 Error = "error",
26});
27
28impl RunState {
29 pub fn is_terminal(self) -> bool {
30 matches!(
31 self,
32 RunState::Success | RunState::Failure | RunState::Cancelled | RunState::Error
33 )
34 }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum JobState {
39 Blocked,
40 Queued,
41 Running,
42 Success,
43 Failure,
44 Cancelled,
45 Error,
46 Skipped,
47}
48
49string_enum!(JobState {
50 Blocked = "blocked",
51 Queued = "queued",
52 Running = "running",
53 Success = "success",
54 Failure = "failure",
55 Cancelled = "cancelled",
56 Error = "error",
57 Skipped = "skipped",
58});
59
60impl JobState {
61 pub fn is_terminal(self) -> bool {
62 matches!(
63 self,
64 JobState::Success
65 | JobState::Failure
66 | JobState::Cancelled
67 | JobState::Error
68 | JobState::Skipped
69 )
70 }
71}
72
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct CiRunner {
75 pub id: String,
76 pub name: String,
77 pub labels: Vec<String>,
78 pub created_at: String,
79 pub last_seen_at: Option<String>,
80 pub stopped_at: Option<String>,
81 pub disabled: bool,
82 pub concurrency_override: Option<u32>,
83 pub reported: RunnerReport,
84 pub running_jobs: u32,
85 pub reported_config: BTreeMap<String, serde_json::Value>,
86 pub restart: Option<RunnerRestart>,
87 pub config_overrides: BTreeMap<String, serde_json::Value>,
88}
89
90#[derive(Debug, Clone, PartialEq, Eq)]
91pub struct RunnerRestart {
92 pub mode: RestartMode,
93 pub requested_at: String,
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97pub enum FieldKind {
98 Live,
99 Restart,
100 ReadOnly,
101}
102
103#[derive(Debug, Clone, Copy)]
104pub struct RunnerField {
105 pub key: &'static str,
106 pub kind: FieldKind,
107}
108
109pub const RUNNER_FIELDS: &[RunnerField] = &[
110 RunnerField {
111 key: "concurrency",
112 kind: FieldKind::Live,
113 },
114 RunnerField {
115 key: "labels",
116 kind: FieldKind::Live,
117 },
118 RunnerField {
119 key: "auto_update",
120 kind: FieldKind::Restart,
121 },
122 RunnerField {
123 key: "executor",
124 kind: FieldKind::Restart,
125 },
126 RunnerField {
127 key: "work_dir",
128 kind: FieldKind::Restart,
129 },
130 RunnerField {
131 key: "limits.memory_headroom",
132 kind: FieldKind::Restart,
133 },
134 RunnerField {
135 key: "limits.cpus",
136 kind: FieldKind::Restart,
137 },
138 RunnerField {
139 key: "limits.pids",
140 kind: FieldKind::Restart,
141 },
142 RunnerField {
143 key: "limits.image_max_gb",
144 kind: FieldKind::Restart,
145 },
146 RunnerField {
147 key: "limits.job_disk_gb",
148 kind: FieldKind::Restart,
149 },
150 RunnerField {
151 key: "images.keep_days",
152 kind: FieldKind::Restart,
153 },
154 RunnerField {
155 key: "cache_max_gib",
156 kind: FieldKind::Restart,
157 },
158 RunnerField {
159 key: "sandbox.userns",
160 kind: FieldKind::Restart,
161 },
162 RunnerField {
163 key: "name",
164 kind: FieldKind::Live,
165 },
166 RunnerField {
167 key: "url",
168 kind: FieldKind::ReadOnly,
169 },
170 RunnerField {
171 key: "runner_id",
172 kind: FieldKind::ReadOnly,
173 },
174 RunnerField {
175 key: "runner_secret",
176 kind: FieldKind::ReadOnly,
177 },
178 RunnerField {
179 key: "registry",
180 kind: FieldKind::ReadOnly,
181 },
182];
183
184pub fn runner_field(key: &str) -> Option<&'static RunnerField> {
185 RUNNER_FIELDS.iter().find(|f| f.key == key)
186}
187
188fn int_in(
189 raw: &str,
190 lo: i64,
191 hi: i64,
192 what: &str,
193) -> std::result::Result<serde_json::Value, String> {
194 match raw.trim().parse::<i64>() {
195 Ok(n) if (lo..=hi).contains(&n) => Ok(serde_json::Value::from(n)),
196 _ => Err(format!("{what} must be a whole number from {lo} to {hi}")),
197 }
198}
199
200pub fn validate_runner_field(
201 key: &str,
202 raw: &str,
203) -> std::result::Result<serde_json::Value, String> {
204 use serde_json::Value;
205 let raw = raw.trim();
206 match key {
207 "concurrency" => int_in(
208 raw,
209 i64::from(MIN_CONCURRENCY),
210 i64::from(MAX_CONCURRENCY),
211 "concurrency",
212 ),
213 "name" => {
214 if raw.is_empty() || raw.len() > 100 {
215 Err("a runner name needs 1 to 100 bytes".into())
216 } else {
217 Ok(Value::from(raw))
218 }
219 }
220 "labels" => {
221 let labels: Vec<String> = raw
222 .split(',')
223 .map(|l| l.trim().to_owned())
224 .filter(|l| !l.is_empty())
225 .collect();
226 if labels
227 .iter()
228 .any(|l| l.len() > 64 || l.contains(char::is_whitespace))
229 {
230 return Err("each label is 1 to 64 characters with no spaces".into());
231 }
232 Ok(Value::from(labels))
233 }
234 "auto_update" => match raw {
235 "true" => Ok(Value::Bool(true)),
236 "false" => Ok(Value::Bool(false)),
237 _ => Err("auto_update must be true or false".into()),
238 },
239 "executor" => match raw {
240 "builtin" | "podman" => Ok(Value::from(raw)),
241 _ => Err("executor must be builtin or podman".into()),
242 },
243 "sandbox.userns" => match raw {
244 "auto" | "single" | "subuid" => Ok(Value::from(raw)),
245 _ => Err("userns must be auto, single or subuid".into()),
246 },
247 "work_dir" => {
248 if raw.starts_with('/') && raw.len() <= 512 && !raw.contains('\0') {
249 Ok(Value::from(raw))
250 } else {
251 Err("work_dir must be an absolute path".into())
252 }
253 }
254 "limits.memory_headroom" => {
255 if size_in_range(raw, 0, 1 << 50) {
256 Ok(Value::from(raw))
257 } else {
258 Err("memory_headroom looks like 1g or 512m".into())
259 }
260 }
261 "limits.cpus" => {
262 let ok = raw.parse::<f64>().is_ok_and(|n| n > 0.0 && n <= 1024.0)
263 && raw.chars().all(|c| c.is_ascii_digit() || c == '.');
264 if ok {
265 Ok(Value::from(raw))
266 } else {
267 Err("cpus is a positive number such as 2 or 1.5".into())
268 }
269 }
270 "limits.pids" => int_in(raw, 1, 4_194_304, "pids"),
271 "limits.image_max_gb" => int_in(raw, 1, 100_000, "image_max_gb"),
272 "limits.job_disk_gb" => int_in(raw, 0, 1_000_000, "job_disk_gb"),
273 "images.keep_days" => int_in(raw, 0, 3650, "keep_days"),
274 "cache_max_gib" => int_in(raw, 0, 1_000_000, "cache_max_gib"),
275 _ => Err("this field cannot be changed from the forge".into()),
276 }
277}
278
279#[derive(Debug, Clone, Default, PartialEq, Eq)]
280pub struct RunnerReport {
281 pub concurrency: Option<u32>,
282 pub version: Option<String>,
283 pub cpus: Option<u32>,
284 pub memory_bytes: Option<u64>,
285 pub build: Option<String>,
286 pub boot_id: Option<String>,
287 pub capabilities: Vec<String>,
288}
289
290#[derive(Debug, Clone, Default, PartialEq, Eq)]
291pub struct QueueSkips {
292 pub queued: usize,
293 pub lacking_labels: usize,
294 pub missing_labels: Vec<String>,
295}
296
297fn size_in_range(raw: &str, lo: u64, hi: u64) -> bool {
298 forge_ci::size::parse_size(raw).is_some_and(|n| (lo..=hi).contains(&n))
299}
300
301pub const CAP_RESTART: &str = "restart";
302
303impl CiRunner {
304 pub fn supports_restart(&self) -> bool {
305 self.reported.capabilities.iter().any(|c| c == CAP_RESTART)
306 }
307}
308
309#[derive(Debug, Clone, PartialEq, Eq)]
310pub struct CiRun {
311 pub id: String,
312 pub repo_id: String,
313 pub number: i64,
314 pub trigger: String,
315 pub git_ref: String,
316 pub commit_oid: String,
317 pub pull_number: Option<i64>,
318 pub actor_id: String,
319 pub state: RunState,
320 pub config_error: Option<String>,
321 pub created_at: String,
322 pub finished_at: Option<String>,
323}
324
325#[derive(Debug, Clone, PartialEq, Eq)]
326pub struct CiJob {
327 pub id: String,
328 pub run_id: String,
329 pub name: String,
330 pub spec_json: String,
331 pub state: JobState,
332 pub runner_id: Option<String>,
333 pub exit_code: Option<i64>,
334 pub message: Option<String>,
335 pub queued_at: Option<String>,
336 pub started_at: Option<String>,
337 pub finished_at: Option<String>,
338 pub heartbeat_at: Option<String>,
339 pub cancel_requested: bool,
340 pub log_bytes: i64,
341 pub oom_kills: Option<u32>,
342}
343
344#[derive(Debug, Clone, PartialEq, Eq)]
345pub struct JobSample {
346 pub git_ref: String,
347 pub branch_rank: i64,
348 pub repo_rank: i64,
349 pub job: String,
350 pub secs: i64,
351}
352
353#[derive(Debug, Clone, PartialEq, Eq)]
354pub struct JobStep {
355 pub job_id: String,
356 pub seq: i64,
357 pub name: String,
358 pub started_at: String,
359 pub finished_at: Option<String>,
360 pub log_offset: i64,
361}
362
363#[derive(Debug, Clone, Copy, PartialEq, Eq)]
364pub enum StepState {
365 Running,
366 Passed,
367 Failed,
368 Cancelled,
369}
370
371impl JobStep {
372 pub fn state(&self, job: JobState) -> StepState {
373 if self.finished_at.is_some() {
374 return StepState::Passed;
375 }
376 match job {
377 JobState::Success => StepState::Passed,
378 JobState::Cancelled => StepState::Cancelled,
379 JobState::Failure | JobState::Error => StepState::Failed,
380 _ => StepState::Running,
381 }
382 }
383}
384
385#[derive(Debug, Clone, PartialEq, Eq)]
386pub struct StepSample {
387 pub git_ref: String,
388 pub branch_rank: i64,
389 pub repo_rank: i64,
390 pub job: String,
391 pub step: String,
392 pub seq: i64,
393 pub secs: i64,
394}
395
396#[derive(Debug, Clone, PartialEq, Eq)]
397pub struct RunnerJob {
398 pub job: CiJob,
399 pub run_number: i64,
400 pub owner: String,
401 pub repo: String,
402}
403
404#[derive(Debug, Clone, PartialEq, Eq)]
405pub struct CiArtifact {
406 pub id: String,
407 pub job_id: String,
408 pub name: String,
409 pub size: i64,
410 pub sha256: String,
411 pub created_at: String,
412}
413
414#[derive(Debug, Clone, Copy)]
415pub struct NewJob<'a> {
416 pub name: &'a str,
417 pub spec_json: &'a str,
418 pub needs: &'a [String],
419}
420
421pub struct NewRun<'a> {
422 pub repo_id: &'a str,
423 pub trigger: &'a str,
424 pub git_ref: &'a str,
425 pub commit_oid: &'a str,
426 pub pull_number: Option<i64>,
427 pub actor_id: &'a str,
428 pub needs_approval: bool,
429 pub config_error: Option<&'a str>,
430}