OXYGENHUB
# c58efc6a74db oxygenflow / crates / flow-core / src / api.rs
7.9 KiB · Rust · last changed A claim carries the hub's variables for the job 1 month ago source blame history raw
e0918b88 Andy McCall 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>,
bb643c25 Andy McCall ↶ 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>,
f98a8f78 Andy McCall ↶ 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>,
8c0a4e3a Andy McCall ↶ 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>>,
1daca0b6 Andy McCall ↶ 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>>,
f98a8f78 Andy McCall ↶ 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,
e0918b88 Andy McCall 93
}
94
c23d5191 Andy McCall ↶ 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
e0918b88 Andy McCall 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,
c23d5191 Andy McCall ↶ 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>,
c4285535 Andy McCall ↶ 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,
c58efc6a Andy McCall ↶ 137
    /// Variables the hub holds for the repository (its site's, its
138
    /// organisation's and its own, inner overriding outer; oxygenhub#102).
139
    /// Set under `FLOW_*` and under the pipeline's own `env`, so they are
140
    /// part of the job's environment and of its receipt. Never secrets:
141
    /// those are mounted, not put in the environment (spec §9).
142
    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
143
    pub env: std::collections::BTreeMap<String, String>,
c4285535 Andy McCall ↶ 144
}
145
146
impl Claim {
147
    /// What a job is told about what it builds (docs/m0.md §4):
148
    /// `FLOW_REPO`, `FLOW_STATE`, `FLOW_HEADS` (full hashes joined with
149
    /// `+`), `FLOW_BOOKMARK`, `FLOW_RUN` and `FLOW_JOB`. Unknown values are
150
    /// left out.
151
    pub fn flow_env(&self) -> Vec<(String, String)> {
152
        let mut v = vec![
153
            ("FLOW_REPO".to_string(), self.repo.clone()),
154
            ("FLOW_HEADS".to_string(), self.heads.join("+")),
155
            ("FLOW_JOB".to_string(), self.job.name.clone()),
156
        ];
157
        if !self.state.is_empty() {
158
            v.push(("FLOW_STATE".into(), self.state.clone()));
159
        }
160
        if !self.bookmark.is_empty() {
161
            v.push(("FLOW_BOOKMARK".into(), self.bookmark.clone()));
162
        }
163
        if self.run_number > 0 {
164
            v.push(("FLOW_RUN".into(), self.run_number.to_string()));
165
        }
c58efc6a Andy McCall ↶ 166
        for (k, val) in &self.env {
167
            if !v.iter().any(|(n, _)| n == k) {
168
                v.push((k.clone(), val.clone()));
169
            }
170
        }
c4285535 Andy McCall ↶ 171
        v
172
    }
e0918b88 Andy McCall 173
}
174
175
/// One line of output (docs/m0.md §2).
176
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
177
pub struct Record {
178
    /// Milliseconds since the job started.
179
    pub t: u64,
180
    /// `out`, `err`, `console` (the VM outside the job) or `flow` (the runner).
181
    pub stream: String,
182
    pub text: String,
183
}
184
185
/// `POST /api/v1/flow/jobs/{id}/log`.
186
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
187
pub struct LogBatch {
188
    pub records: Vec<Record>,
189
}
190
191
/// `POST /api/v1/flow/jobs/{id}/finish`.
192
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
193
pub struct Finish {
194
    /// The job's exit code; absent when the runner could not run it.
195
    pub exit_code: Option<i32>,
196
    pub duration_ms: u64,
197
    pub receipt: Option<Receipt>,
47c1d64d Andy McCall ↶ 198
    /// The receipt bound to the job's state and signed by the runner
199
    /// (Oxygen spec §14.6), hex of the signed receipt's bytes.
200
    #[serde(default, skip_serializing_if = "Option::is_none")]
201
    pub signed_receipt: Option<String>,
e0918b88 Andy McCall 202
    /// Why the runner could not run the job (rootfs missing, VM failed).
203
    pub error: Option<String>,
204
}
205
206
/// Longest text of one record the hub keeps; longer lines are cut.
207
pub const MAX_RECORD_TEXT: usize = 16 * 1024;
208
/// Most records the hub keeps per job; later ones are dropped with a note.
209
pub const MAX_RECORDS: usize = 50_000;