Skip to main content

runnerd_core/api/
control.rs

1//! The control endpoints' types: pausing with or without cancelling running jobs, what a refused request names,
2//! the config file and its check, and the doctor's checks. Plain data; the engine and the daemon act on them.
3
4use std::fmt;
5
6use serde::{Deserialize, Serialize};
7
8/// A runner of this daemon that holds a slot, and the job it runs, if it took one.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct RunningJob {
11    /// `owner/name` the runner is registered on.
12    pub repository: String,
13    /// The runner's name, also its container's.
14    pub runner: String,
15    /// The job's ID; `None` while the runner waits for a job or before its log shows which it took.
16    pub job: Option<u64>,
17}
18
19impl fmt::Display for RunningJob {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self.job {
22            Some(job) => write!(f, "job {job} on {}", self.runner),
23            None => write!(f, "{}", self.runner),
24        }
25    }
26}
27
28/// The body of a 409 refused because runners use what the request would change. Has the `message` of an
29/// [`ErrorResponse`](super::ErrorResponse), so a client that reads only that still gets it.
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub struct Busy {
32    /// What went wrong, naming the jobs.
33    pub message: String,
34    /// The runners in the way.
35    pub jobs: Vec<RunningJob>,
36}
37
38/// `POST /v1/repositories/{owner}/{name}/pause` and `POST /v1/repositories/pause` query parameters.
39#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
40pub struct PauseQuery {
41    /// Cancel the repository's running jobs and remove their runners. Without it, a pause while the repository
42    /// runs a job is refused with [`Busy`].
43    #[serde(default)]
44    pub force: bool,
45}
46
47/// `POST /v1/repositories/{owner}/{name}/pause` and `POST /v1/repositories/pause`: the repository, or every one,
48/// is paused, and stays paused when the daemon restarts.
49#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
50pub struct Paused {
51    /// The runners a forced pause stopped: those whose runs it cancelled go once they report the job cancelled,
52    /// within 90 s, the others at once. Empty without `force`.
53    pub stopped: Vec<RunningJob>,
54}
55
56/// `GET /v1/config`: the config the daemon runs with. Parse `text` with `config::Config::parse` for its fields.
57#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
58pub struct ConfigFile {
59    /// The file it was read from, and `PUT /v1/config` writes.
60    pub path: String,
61    /// The TOML, as last loaded or written by the daemon.
62    pub text: String,
63    /// `config::revision` of `text`: send it as `PUT /v1/config?revision=` and a config replaced meanwhile, as
64    /// by auto sizing, is not overwritten (409). `None` from a daemon before it.
65    #[serde(default)]
66    pub revision: Option<String>,
67}
68
69/// `PUT /v1/config` query parameters.
70#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
71pub struct ReplaceQuery {
72    /// The revision of the config the new text was made from; absent, the text replaces whatever runs.
73    pub revision: Option<String>,
74}
75
76/// `POST /v1/config/check` and `PUT /v1/config`: what `runnerd check-config` prints for a config. `PUT` refuses
77/// an invalid one with 422 and this body, which has an [`ErrorResponse`](super::ErrorResponse)'s `message`.
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79pub struct ConfigCheck {
80    /// Whether the daemon can run it.
81    pub valid: bool,
82    /// `valid, memory caps sum to …`, or why it is not valid.
83    pub message: String,
84    /// What a valid config should still fix.
85    pub warnings: Vec<String>,
86    /// Bytes every runner and the mirror may use at once, when it parses.
87    pub memory_caps: Option<u64>,
88    /// The `MemoryMax` of the slice it names; `None` when unlimited or unknown.
89    pub memory_max: Option<u64>,
90    /// From `PUT /v1/config`, once written: the revision of the text written, to make the next change against.
91    /// `None` from a check, or from a daemon before it.
92    #[serde(default)]
93    pub revision: Option<String>,
94}
95
96/// `GET /v1/doctor`: what `runnerd doctor` checks, for the config the daemon runs with.
97#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
98pub struct Doctor {
99    /// In the order a new user fixes them.
100    pub checks: Vec<Check>,
101}
102
103/// One thing the host needs to run runners. Its `Display` is the line `runnerd doctor` prints.
104#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
105pub struct Check {
106    /// Such as `Docker` or `sysbox`.
107    pub name: String,
108    /// The outcome.
109    pub status: CheckStatus,
110    /// What was found.
111    pub detail: String,
112    /// What to run or change to pass, for a failed check.
113    pub fix: Option<String>,
114}
115
116/// How a [`Check`] came out.
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
118#[serde(rename_all = "kebab-case")]
119pub enum CheckStatus {
120    /// Ready.
121    Pass,
122    /// Runners cannot run until it is fixed.
123    Fail,
124    /// Works, but unsafe.
125    Warn,
126    /// Not checked, because a check it depends on failed or no config was given.
127    Skip,
128    /// For information.
129    Info,
130}
131
132impl fmt::Display for Check {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        let status = match self.status {
135            CheckStatus::Pass => "ok",
136            CheckStatus::Fail => "FAIL",
137            CheckStatus::Warn => "WARN",
138            CheckStatus::Skip => "skip",
139            CheckStatus::Info => "info",
140        };
141        write!(f, "{status:<5} {}: {}", self.name, self.detail)?;
142        if let Some(fix) = &self.fix {
143            for (i, line) in fix.lines().enumerate() {
144                let label = if i == 0 { "fix:" } else { "" };
145                write!(f, "\n      {label:<4} {line}")?;
146            }
147        }
148        Ok(())
149    }
150}