OXYGENHUB
# 02b127d2c721 oxygenflow / crates / flow-core / src / api.rs
7.3 KiB · Rust · last changed A runner tells the hub which jobs it holds at each claim (refs oxygenhub#71) 1 month ago source blame history raw
1
//! The runner API between OxygenHub and `flow runner` (docs/m0.md §5).
2
//! Both sides use these types, so they cannot drift.
3
4
use crate::pipeline::{Job, Sandbox};
5
use crate::receipt::Receipt;
6
use serde::{Deserialize, Serialize};
7
8
/// `POST /api/v1/flow/claim`.
9
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
10
pub struct ClaimRequest {
11
    /// The runner's name, shown on job pages.
12
    pub runner: String,
13
    /// Tiers this runner can run (`microvm`, `syscall`).
14
    pub runtimes: Vec<String>,
15
    /// Memory the runner has free for another job, in MiB: the hub hands
16
    /// out only a job whose sandbox fits. Absent: no limit.
17
    #[serde(default, skip_serializing_if = "Option::is_none")]
18
    pub free_memory_mib: Option<u32>,
19
    /// vCPUs the runner has free for another job. Absent: no limit.
20
    #[serde(default, skip_serializing_if = "Option::is_none")]
21
    pub free_cpus: Option<u32>,
22
    /// What the runner is, for the hub's Runners page.
23
    #[serde(default, skip_serializing_if = "Option::is_none")]
24
    pub info: Option<RunnerInfo>,
25
    /// The rootfs images the runner holds (BLAKE3 hex, as pipelines pin
26
    /// them): the hub offers only jobs whose image is among them
27
    /// (oxygenhub#73). Absent: the runner does not say, and any job may be
28
    /// offered.
29
    #[serde(default, skip_serializing_if = "Option::is_none")]
30
    pub images: Option<Vec<String>>,
31
    /// The jobs this runner holds now: running, or finished with a result
32
    /// not yet reported. The hub ends any other job it has as running on
33
    /// this runner, which the runner lost by restarting or never received
34
    /// (oxygenhub#71). Absent: the runner does not say.
35
    #[serde(default, skip_serializing_if = "Option::is_none")]
36
    pub holding: Option<Vec<i64>>,
37
}
38
39
/// A runner's description: shown on the hub's Runners page, and what
40
/// scheduling by label and architecture will match.
41
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
42
pub struct RunnerInfo {
43
    /// `x86_64`, `aarch64`, `riscv64`, …
44
    pub arch: String,
45
    pub os: String,
46
    /// The `flow` version.
47
    pub version: String,
48
    /// From flow.toml `[runner] labels`.
49
    #[serde(default)]
50
    pub labels: Vec<String>,
51
    /// Jobs at once, and the memory (MiB) and vCPUs they share.
52
    pub max_jobs: u32,
53
    #[serde(default)]
54
    pub memory_mib: Option<u32>,
55
    #[serde(default)]
56
    pub cpus: Option<u32>,
57
}
58
59
impl RunnerInfo {
60
    /// This machine and build, with the given labels and capacity.
61
    pub fn here(labels: Vec<String>, max_jobs: u32, memory_mib: Option<u32>, cpus: Option<u32>) -> RunnerInfo {
62
        RunnerInfo {
63
            arch: std::env::consts::ARCH.into(),
64
            os: std::env::consts::OS.into(),
65
            version: env!("CARGO_PKG_VERSION").into(),
66
            labels,
67
            max_jobs,
68
            memory_mib,
69
            cpus,
70
        }
71
    }
72
}
73
74
/// `POST /api/v1/flow/runners/register`: exchange a one-time registration
75
/// token from the hub for a runner credential, binding the runner's
76
/// signing identity to it.
77
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
78
pub struct Register {
79
    pub token: String,
80
    pub name: String,
81
    /// The runner's Oxygen identity, hex of its encoding: its key signs
82
    /// every receipt, and the hub accepts no other for this runner.
83
    pub identity: String,
84
    pub info: RunnerInfo,
85
}
86
87
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
88
pub struct Registered {
89
    pub id: i64,
90
    pub name: String,
91
    /// The runner's bearer token from now on; shown once.
92
    pub credential: String,
93
}
94
95
/// A pinned subrepo to put in the workspace: `repo`'s state `heads`, at `path`.
96
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
97
pub struct SubrepoInput {
98
    pub path: String,
99
    /// `owner/name` on the hub.
100
    pub repo: String,
101
    pub clone_url: String,
102
    pub heads: Vec<String>,
103
}
104
105
/// A claimed job: everything the runner needs, nothing more.
106
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
107
pub struct Claim {
108
    pub job_id: i64,
109
    pub run_id: i64,
110
    /// `owner/name`.
111
    pub repo: String,
112
    /// Where to fetch the repository from (`oxy` wire protocol).
113
    pub clone_url: String,
114
    /// Read access to this repository only, until the job finishes.
115
    pub token: String,
116
    /// The state to build, as full change hashes.
117
    pub heads: Vec<String>,
118
    /// Hex BLAKE3 of the canonical pipeline.
119
    pub pipeline_hash: String,
120
    pub job: Job,
121
    pub sandbox: Sandbox,
122
    /// The hub's limit for this job.
123
    pub timeout_s: u64,
124
    /// Subrepos the state pins (Oxygen spec §7.8) that the job may read,
125
    /// fetched with the same token and written at their paths.
126
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
127
    pub subrepos: Vec<SubrepoInput>,
128
    /// The state's hash on the hub (what checks are keyed by).
129
    #[serde(default, skip_serializing_if = "String::is_empty")]
130
    pub state: String,
131
    /// The bookmark the run is for.
132
    #[serde(default, skip_serializing_if = "String::is_empty")]
133
    pub bookmark: String,
134
    /// The run's number within its repository.
135
    #[serde(default)]
136
    pub run_number: i64,
137
}
138
139
impl Claim {
140
    /// What a job is told about what it builds (docs/m0.md §4):
141
    /// `FLOW_REPO`, `FLOW_STATE`, `FLOW_HEADS` (full hashes joined with
142
    /// `+`), `FLOW_BOOKMARK`, `FLOW_RUN` and `FLOW_JOB`. Unknown values are
143
    /// left out.
144
    pub fn flow_env(&self) -> Vec<(String, String)> {
145
        let mut v = vec![
146
            ("FLOW_REPO".to_string(), self.repo.clone()),
147
            ("FLOW_HEADS".to_string(), self.heads.join("+")),
148
            ("FLOW_JOB".to_string(), self.job.name.clone()),
149
        ];
150
        if !self.state.is_empty() {
151
            v.push(("FLOW_STATE".into(), self.state.clone()));
152
        }
153
        if !self.bookmark.is_empty() {
154
            v.push(("FLOW_BOOKMARK".into(), self.bookmark.clone()));
155
        }
156
        if self.run_number > 0 {
157
            v.push(("FLOW_RUN".into(), self.run_number.to_string()));
158
        }
159
        v
160
    }
161
}
162
163
/// One line of output (docs/m0.md §2).
164
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
165
pub struct Record {
166
    /// Milliseconds since the job started.
167
    pub t: u64,
168
    /// `out`, `err`, `console` (the VM outside the job) or `flow` (the runner).
169
    pub stream: String,
170
    pub text: String,
171
}
172
173
/// `POST /api/v1/flow/jobs/{id}/log`.
174
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
175
pub struct LogBatch {
176
    pub records: Vec<Record>,
177
}
178
179
/// `POST /api/v1/flow/jobs/{id}/finish`.
180
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
181
pub struct Finish {
182
    /// The job's exit code; absent when the runner could not run it.
183
    pub exit_code: Option<i32>,
184
    pub duration_ms: u64,
185
    pub receipt: Option<Receipt>,
186
    /// The receipt bound to the job's state and signed by the runner
187
    /// (Oxygen spec §14.6), hex of the signed receipt's bytes.
188
    #[serde(default, skip_serializing_if = "Option::is_none")]
189
    pub signed_receipt: Option<String>,
190
    /// Why the runner could not run the job (rootfs missing, VM failed).
191
    pub error: Option<String>,
192
}
193
194
/// Longest text of one record the hub keeps; longer lines are cut.
195
pub const MAX_RECORD_TEXT: usize = 16 * 1024;
196
/// Most records the hub keeps per job; later ones are dropped with a note.
197
pub const MAX_RECORDS: usize = 50_000;