OXYGENHUB
# 01043bfff9fd oxygenflow / crates / flow-core / src / api.rs
9.4 KiB · Rust · last changed Images by hash: flow rootfs push and pull, and runners fetch what they lack 24 days 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 architecture of each image it holds, by hash, as the image's
32
    /// manifest records it (spec §11.2): the hub offers a job only to a
33
    /// runner whose architecture its image is for.
34
    #[serde(default, skip_serializing_if = "Option::is_none")]
35
    pub image_archs: Option<std::collections::BTreeMap<String, String>>,
36
    /// Whether the runner fetches a job's image from the hub when it lacks
37
    /// it (oxygenflow#39): the hub may then offer it jobs whose image only
38
    /// the hub holds.
39
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
40
    pub fetches_images: bool,
41
    /// The jobs this runner holds now: running, or finished with a result
42
    /// not yet reported. The hub ends any other job it has as running on
43
    /// this runner, which the runner lost by restarting or never received
44
    /// (oxygenhub#71). Absent: the runner does not say.
45
    #[serde(default, skip_serializing_if = "Option::is_none")]
46
    pub holding: Option<Vec<i64>>,
47
}
48
49
/// A runner's description: shown on the hub's Runners page, and what
50
/// scheduling by label and architecture will match.
51
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
52
pub struct RunnerInfo {
53
    /// `x86_64`, `aarch64`, `riscv64`, …
54
    pub arch: String,
55
    pub os: String,
56
    /// The `flow` version.
57
    pub version: String,
58
    /// From flow.toml `[runner] labels`.
59
    #[serde(default)]
60
    pub labels: Vec<String>,
61
    /// Jobs at once, and the memory (MiB) and vCPUs they share.
62
    pub max_jobs: u32,
63
    #[serde(default)]
64
    pub memory_mib: Option<u32>,
65
    #[serde(default)]
66
    pub cpus: Option<u32>,
67
}
68
69
impl RunnerInfo {
70
    /// This machine and build, with the given labels and capacity.
71
    pub fn here(labels: Vec<String>, max_jobs: u32, memory_mib: Option<u32>, cpus: Option<u32>) -> RunnerInfo {
72
        RunnerInfo {
73
            arch: std::env::consts::ARCH.into(),
74
            os: std::env::consts::OS.into(),
75
            version: env!("CARGO_PKG_VERSION").into(),
76
            labels,
77
            max_jobs,
78
            memory_mib,
79
            cpus,
80
        }
81
    }
82
}
83
84
/// `POST /api/v1/flow/runners/register`: exchange a one-time registration
85
/// token from the hub for a runner credential, binding the runner's
86
/// signing identity to it.
87
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
88
pub struct Register {
89
    pub token: String,
90
    pub name: String,
91
    /// The runner's Oxygen identity, hex of its encoding: its key signs
92
    /// every receipt, and the hub accepts no other for this runner.
93
    pub identity: String,
94
    pub info: RunnerInfo,
95
}
96
97
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
98
pub struct Registered {
99
    pub id: i64,
100
    pub name: String,
101
    /// The runner's bearer token from now on; shown once.
102
    pub credential: String,
103
}
104
105
/// A pinned subrepo to put in the workspace: `repo`'s state `heads`, at `path`.
106
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
107
pub struct SubrepoInput {
108
    pub path: String,
109
    /// `owner/name` on the hub.
110
    pub repo: String,
111
    pub clone_url: String,
112
    pub heads: Vec<String>,
113
}
114
115
/// A secret for a job, as the hub supplies it (spec §9): written by the
116
/// runner at `path` in the sandbox, 0400, on an in-memory mount; never in
117
/// the environment, the receipt or an artifact.
118
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
119
pub struct SecretInput {
120
    pub name: String,
121
    pub path: String,
122
    pub value: String,
123
}
124
125
/// A claimed job: everything the runner needs, nothing more.
126
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
127
pub struct Claim {
128
    pub job_id: i64,
129
    pub run_id: i64,
130
    /// `owner/name`.
131
    pub repo: String,
132
    /// Where to fetch the repository from (`oxy` wire protocol).
133
    pub clone_url: String,
134
    /// Read access to this repository only, until the job finishes.
135
    pub token: String,
136
    /// The state to build, as full change hashes.
137
    pub heads: Vec<String>,
138
    /// Hex BLAKE3 of the canonical pipeline.
139
    pub pipeline_hash: String,
140
    pub job: Job,
141
    pub sandbox: Sandbox,
142
    /// The hub's limit for this job.
143
    pub timeout_s: u64,
144
    /// Subrepos the state pins (Oxygen spec §7.8) that the job may read,
145
    /// fetched with the same token and written at their paths.
146
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
147
    pub subrepos: Vec<SubrepoInput>,
148
    /// The state's hash on the hub (what checks are keyed by).
149
    #[serde(default, skip_serializing_if = "String::is_empty")]
150
    pub state: String,
151
    /// The bookmark the run is for.
152
    #[serde(default, skip_serializing_if = "String::is_empty")]
153
    pub bookmark: String,
154
    /// The run's number within its repository.
155
    #[serde(default)]
156
    pub run_number: i64,
157
    /// Variables the hub holds for the repository (its site's, its
158
    /// organisation's and its own, inner overriding outer; oxygenhub#102).
159
    /// Set under `FLOW_*` and under the pipeline's own `env`, so they are
160
    /// part of the job's environment and of its receipt. Never secrets:
161
    /// those are mounted, not put in the environment (spec §9).
162
    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
163
    pub env: std::collections::BTreeMap<String, String>,
164
    /// The secrets the job declares (`secret` blocks), as the hub holds
165
    /// them for the repository (oxygenhub#107).
166
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
167
    pub secrets: Vec<SecretInput>,
168
}
169
170
impl Claim {
171
    /// What a job is told about what it builds (docs/m0.md §4):
172
    /// `FLOW_REPO`, `FLOW_STATE`, `FLOW_HEADS` (full hashes joined with
173
    /// `+`), `FLOW_BOOKMARK`, `FLOW_RUN` and `FLOW_JOB`. Unknown values are
174
    /// left out.
175
    pub fn flow_env(&self) -> Vec<(String, String)> {
176
        let mut v = vec![
177
            ("FLOW_REPO".to_string(), self.repo.clone()),
178
            ("FLOW_HEADS".to_string(), self.heads.join("+")),
179
            ("FLOW_JOB".to_string(), self.job.name.clone()),
180
        ];
181
        if !self.state.is_empty() {
182
            v.push(("FLOW_STATE".into(), self.state.clone()));
183
        }
184
        if !self.bookmark.is_empty() {
185
            v.push(("FLOW_BOOKMARK".into(), self.bookmark.clone()));
186
        }
187
        if self.run_number > 0 {
188
            v.push(("FLOW_RUN".into(), self.run_number.to_string()));
189
        }
190
        for (k, val) in &self.env {
191
            if !v.iter().any(|(n, _)| n == k) {
192
                v.push((k.clone(), val.clone()));
193
            }
194
        }
195
        v
196
    }
197
}
198
199
/// One line of output (docs/m0.md §2).
200
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
201
pub struct Record {
202
    /// Milliseconds since the job started.
203
    pub t: u64,
204
    /// `out`, `err`, `console` (the VM outside the job) or `flow` (the runner).
205
    pub stream: String,
206
    pub text: String,
207
}
208
209
/// `POST /api/v1/flow/jobs/{id}/decline` (spec §11.5): the runner will
210
/// not run a job it was handed, and says why. The hub queues the job
211
/// again and offers it to this runner no more until its profile changes.
212
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
213
pub struct Decline {
214
    pub reason: String,
215
}
216
217
/// `POST /api/v1/flow/jobs/{id}/log`.
218
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
219
pub struct LogBatch {
220
    pub records: Vec<Record>,
221
}
222
223
/// `POST /api/v1/flow/jobs/{id}/finish`.
224
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
225
pub struct Finish {
226
    /// The job's exit code; absent when the runner could not run it.
227
    pub exit_code: Option<i32>,
228
    pub duration_ms: u64,
229
    pub receipt: Option<Receipt>,
230
    /// The receipt bound to the job's state and signed by the runner
231
    /// (Oxygen spec §14.6), hex of the signed receipt's bytes.
232
    #[serde(default, skip_serializing_if = "Option::is_none")]
233
    pub signed_receipt: Option<String>,
234
    /// Why the runner could not run the job (rootfs missing, VM failed).
235
    pub error: Option<String>,
236
}
237
238
/// Longest text of one record the hub keeps; longer lines are cut.
239
pub const MAX_RECORD_TEXT: usize = 16 * 1024;
240
/// Most records the hub keeps per job; later ones are dropped with a note.
241
pub const MAX_RECORDS: usize = 50_000;