1use std::collections::BTreeSet;
13use std::fmt::Write as _;
14
15use crate::api;
16use crate::config;
17
18pub const IMAGE: &str = "runnerd-record";
20
21pub const LABEL: &str = "runnerd.record";
23
24pub const SHELL_LABEL: &str = "runnerd.shell";
26
27pub fn image(job: u64) -> String {
29 format!("{IMAGE}:{job}")
30}
31
32const FAILED: &str = "completed with result: Failed";
34
35pub fn failed(runner_log: &str) -> bool {
37 runner_log.lines().any(|line| line.ends_with(FAILED))
38}
39
40const HOME: &str = "/home/runner/";
44
45const SECRET_DIRS: &[&str] = &[
49 "/run/runnerd",
50 "/home/runner/_work/_temp",
51 "/tmp",
52 "/var/tmp",
53];
54
55const HARMLESS: &[&str] = &[
60 "/home/runner/.rustup",
61 "/home/runner/.cargo/bin",
62 "/home/runner/.cargo/registry",
63 "/home/runner/.cargo/git",
64];
65
66fn harmless(path: &str, changed: &[&str]) -> bool {
69 let below = |dir: &str, path: &str| {
70 path.strip_prefix(dir)
71 .is_some_and(|rest| rest.starts_with('/'))
72 };
73 let on_the_way = HARMLESS.iter().any(|dir| *dir == path || below(path, dir));
74 HARMLESS.iter().any(|dir| below(dir, path))
75 || on_the_way && changed.iter().any(|other| below(path, other))
76}
77
78pub fn secrets_left<'path>(changed: impl IntoIterator<Item = &'path str>) -> Vec<String> {
83 let changed: Vec<&str> = changed.into_iter().collect();
84 changed
85 .iter()
86 .copied()
87 .filter(|path| !harmless(path, &changed))
88 .filter(|path| {
89 path.strip_prefix(HOME)
90 .is_some_and(|rest| rest.starts_with('.'))
91 || SECRET_DIRS.iter().any(|dir| {
92 path.strip_prefix(dir)
93 .is_some_and(|rest| rest.starts_with('/'))
94 })
95 })
96 .map(str::to_owned)
97 .collect()
98}
99
100pub fn listed(paths: &[String]) -> String {
102 const SHOWN: usize = 5;
103 let mut text = paths
104 .iter()
105 .take(SHOWN)
106 .map(String::as_str)
107 .collect::<Vec<_>>()
108 .join(", ");
109 if paths.len() > SHOWN {
110 write!(text, " and {} more", paths.len() - SHOWN).ok();
111 }
112 text
113}
114
115pub fn make_room(
118 records: &[api::Record],
119 open: &BTreeSet<u64>,
120 limits: &config::Records,
121 incoming: u64,
122 free: Option<u64>,
123 now: u64,
124) -> Result<Vec<(u64, api::RecordRemoval)>, String> {
125 if incoming > limits.max_total.0 {
126 return Err(format!(
127 "it would take {}, more than records.max_total ({})",
128 config::ByteSize(incoming),
129 limits.max_total
130 ));
131 }
132 let mut removed = expired(records, open, limits, now);
133 let mut kept: Vec<&api::Record> = records
134 .iter()
135 .filter(|record| !removed.iter().any(|(job, _)| *job == record.job))
136 .collect();
137 kept.sort_by_key(|record| (record.created_at, record.job));
138 let mut removable = kept
139 .clone()
140 .into_iter()
141 .filter(|record| !open.contains(&record.job));
142 let max_count = usize::try_from(limits.max_count).unwrap_or(usize::MAX);
143 let mut count = kept.len();
144 let mut total: u64 = kept.iter().map(|record| record.bytes).sum();
145 while count >= max_count || total + incoming > limits.max_total.0 {
146 let why = if count >= max_count {
147 api::RecordRemoval::Count
148 } else {
149 api::RecordRemoval::Size
150 };
151 let Some(oldest) = removable.next() else {
152 return Err("the records kept within the limits have shells open".to_owned());
153 };
154 removed.push((oldest.job, why));
155 count -= 1;
156 total -= oldest.bytes;
157 }
158 if let Some(free) = free {
159 let freed: u64 = records
160 .iter()
161 .filter(|record| removed.iter().any(|(job, _)| *job == record.job))
162 .map(|record| record.bytes)
163 .sum();
164 if (free + freed).saturating_sub(incoming) < limits.min_free.0 {
165 return Err(format!(
166 "Docker has {} free; a record of {} would leave less than records.min_free ({})",
167 config::ByteSize(free),
168 config::ByteSize(incoming),
169 limits.min_free
170 ));
171 }
172 }
173 Ok(removed)
174}
175
176pub fn expired(
178 records: &[api::Record],
179 open: &BTreeSet<u64>,
180 limits: &config::Records,
181 now: u64,
182) -> Vec<(u64, api::RecordRemoval)> {
183 records
184 .iter()
185 .filter(|record| {
186 !open.contains(&record.job)
187 && now.saturating_sub(record.created_at) > limits.max_age.as_secs()
188 })
189 .map(|record| (record.job, api::RecordRemoval::Age))
190 .collect()
191}
192
193pub fn workspace(repository: &str) -> String {
195 let name = repository.rsplit('/').next().unwrap_or(repository);
196 format!("/home/runner/_work/{name}/{name}")
197}
198
199pub fn banner(job: &api::Job, log: &[String], step: Option<u32>) -> String {
203 let summary = &job.summary;
204 let mut text = format!(
205 "runnerd: the record of job {} of {}",
206 summary.id, summary.repository
207 );
208 match &summary.workflow {
209 Some(workflow) => write!(text, ", {workflow} / {}", summary.name),
210 None => write!(text, ", {}", summary.name),
211 }
212 .ok();
213 text.push('\n');
214 let failed = job
215 .steps
216 .iter()
217 .find(|candidate| candidate.conclusion.as_deref() == Some("failure"));
218 let shown = match step {
219 Some(number) => job
220 .steps
221 .iter()
222 .find(|candidate| candidate.number == number),
223 None => failed,
224 };
225 let sections = sections(log);
226 match shown {
227 Some(shown) => {
228 let verb = if failed.is_some_and(|failed| failed.number == shown.number) {
229 "failed at"
230 } else {
231 "shows"
232 };
233 write!(text, "It {verb} step {}: {}", shown.number, shown.name).ok();
234 let section = section_of(job, §ions, shown);
235 if let Some(code) = section.and_then(|section| section.exit_code) {
236 write!(text, " (exit code {code})").ok();
237 }
238 text.push('\n');
239 match section {
240 Some(section) => {
241 text.push_str("Its log shows it ran:\n");
242 for line in §ion.lines {
243 writeln!(text, " {line}").ok();
244 }
245 }
246 None => text.push_str("Its log does not show what it ran.\n"),
247 }
248 }
249 None if step.is_some() => text.push_str("The job has no such step.\n"),
250 None => text.push_str("The job's steps do not say which one failed.\n"),
251 }
252 writeln!(
253 text,
254 "You are in {}, as the job left it. Not kept: the job's Docker containers and images; what it changed \
255 in ~/.cache, the tool cache and the Android SDK, which show the profile's snapshot; and what it added or \
256 changed among your home's dot-files and in /tmp (logins, and tools in ~/.local or ~/.nvm), except \
257 ~/.rustup and ~/.cargo's bin, registry and git. Exit to remove this container.",
258 workspace(&summary.repository)
259 )
260 .ok();
261 text.replace('\n', "\r\n")
262}
263
264#[derive(Debug, Default)]
266struct Section {
267 title: String,
269 lines: Vec<String>,
271 output: Vec<String>,
273 exit_code: Option<i64>,
275}
276
277fn sections(log: &[String]) -> Vec<Section> {
279 let mut sections: Vec<Section> = Vec::new();
280 let (mut in_group, mut cleaning_up) = (false, false);
281 for line in log {
282 let text = plain(line);
283 if let Some(title) = text.strip_prefix("##[group]Run ") {
284 sections.push(Section {
285 title: title.to_owned(),
286 ..Section::default()
287 });
288 (in_group, cleaning_up) = (true, false);
289 } else if text.starts_with("##[endgroup]") {
290 in_group = false;
291 } else if text == "Post job cleanup." {
292 cleaning_up = true;
294 } else if let Some(section) = sections.last_mut().filter(|_| !cleaning_up) {
295 if in_group {
296 section.lines.push(text);
297 } else {
298 if let Some(code) = text
299 .strip_prefix("##[error]Process completed with exit code ")
300 .and_then(|code| code.strip_suffix('.'))
301 .and_then(|code| code.parse().ok())
302 {
303 section.exit_code.get_or_insert(code);
304 }
305 section.output.push(text);
306 }
307 }
308 }
309 sections
310}
311
312fn section_of<'log>(
315 job: &api::Job,
316 sections: &'log [Section],
317 step: &api::Step,
318) -> Option<&'log Section> {
319 if let Some(title) = step.name.strip_prefix("Run ")
320 && let Some(section) = sections.iter().find(|section| section.title == title)
321 {
322 return Some(section);
323 }
324 if step.conclusion.as_deref() == Some("failure")
325 && let Some(section) = sections.iter().rfind(|section| section.exit_code.is_some())
326 {
327 return Some(section);
328 }
329 let place = job
330 .steps
331 .iter()
332 .filter(|candidate| {
333 candidate.number > 1
334 && !matches!(candidate.conclusion.as_deref(), Some("skipped") | None)
335 })
336 .position(|candidate| candidate.number == step.number)?;
337 sections.get(place)
338}
339
340pub fn runner_fault(job: &api::Job, log: &[String]) -> bool {
349 let Some(failed) = job
350 .steps
351 .iter()
352 .find(|step| step.conclusion.as_deref() == Some("failure"))
353 else {
354 return false;
355 };
356 section_of(job, §ions(log), failed)
357 .is_some_and(|section| section.output.iter().any(|line| sysbox_fault(line)))
358}
359
360const FAULT_CALLS: [&str; 6] = [
362 "mount", "umount", "umount2", "unmount", "mounting", "openat2",
363];
364
365fn sysbox_fault(text: &str) -> bool {
369 let text = text.to_lowercase();
370 (text.contains("function not implemented")
371 && text
372 .split(|c: char| c.is_whitespace() || ":()\"',".contains(c))
373 .any(|word| FAULT_CALLS.contains(&word)))
374 || (text.contains("unsafe procfs detected") && text.contains("invalid argument"))
375}
376
377fn plain(line: &str) -> String {
379 let text = match line.split_once(' ') {
380 Some((stamp, rest)) if stamp.ends_with('Z') && stamp.contains('T') => rest,
381 _ => line,
382 };
383 let mut plain = String::with_capacity(text.len());
384 let mut chars = text.chars();
385 while let Some(c) = chars.next() {
386 if c == '\u{1b}' {
387 for c in chars.by_ref() {
389 if c == 'm' {
390 break;
391 }
392 }
393 } else {
394 plain.push(c);
395 }
396 }
397 plain
398}
399
400#[cfg(test)]
401mod tests;