OXYGENHUB
# 720eb8191059 oxygenflow / crates / flow-core / src / api.rs
8.5 KiB · Rust · last changed Secrets mount as files: secret blocks, the claim's secrets, flow-init writes them 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 secret for a job, as the hub supplies it (spec §9): written by the
106
/// runner at `path` in the sandbox, 0400, on an in-memory mount; never in
107
/// the environment, the receipt or an artifact.
108
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
109
pub struct SecretInput {
110
    pub name: String,
111
    pub path: String,
112
    pub value: String,
113
}
114
115
/// A claimed job: everything the runner needs, nothing more.
116
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
117
pub struct Claim {
118
    pub job_id: i64,
119
    pub run_id: i64,
120
    /// `owner/name`.
121
    pub repo: String,
122
    /// Where to fetch the repository from (`oxy` wire protocol).
123
    pub clone_url: String,
124
    /// Read access to this repository only, until the job finishes.
125
    pub token: String,
126
    /// The state to build, as full change hashes.
127
    pub heads: Vec<String>,
128
    /// Hex BLAKE3 of the canonical pipeline.
129
    pub pipeline_hash: String,
130
    pub job: Job,
131
    pub sandbox: Sandbox,
132
    /// The hub's limit for this job.
133
    pub timeout_s: u64,
134
    /// Subrepos the state pins (Oxygen spec §7.8) that the job may read,
135
    /// fetched with the same token and written at their paths.
136
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
137
    pub subrepos: Vec<SubrepoInput>,
138
    /// The state's hash on the hub (what checks are keyed by).
139
    #[serde(default, skip_serializing_if = "String::is_empty")]
140
    pub state: String,
141
    /// The bookmark the run is for.
142
    #[serde(default, skip_serializing_if = "String::is_empty")]
143
    pub bookmark: String,
144
    /// The run's number within its repository.
145
    #[serde(default)]
146
    pub run_number: i64,
147
    /// Variables the hub holds for the repository (its site's, its
148
    /// organisation's and its own, inner overriding outer; oxygenhub#102).
149
    /// Set under `FLOW_*` and under the pipeline's own `env`, so they are
150
    /// part of the job's environment and of its receipt. Never secrets:
151
    /// those are mounted, not put in the environment (spec §9).
152
    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
153
    pub env: std::collections::BTreeMap<String, String>,
154
    /// The secrets the job declares (`secret` blocks), as the hub holds
155
    /// them for the repository (oxygenhub#107).
156
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
157
    pub secrets: Vec<SecretInput>,
158
}
159
160
impl Claim {
161
    /// What a job is told about what it builds (docs/m0.md §4):
162
    /// `FLOW_REPO`, `FLOW_STATE`, `FLOW_HEADS` (full hashes joined with
163
    /// `+`), `FLOW_BOOKMARK`, `FLOW_RUN` and `FLOW_JOB`. Unknown values are
164
    /// left out.
165
    pub fn flow_env(&self) -> Vec<(String, String)> {
166
        let mut v = vec![
167
            ("FLOW_REPO".to_string(), self.repo.clone()),
168
            ("FLOW_HEADS".to_string(), self.heads.join("+")),
169
            ("FLOW_JOB".to_string(), self.job.name.clone()),
170
        ];
171
        if !self.state.is_empty() {
172
            v.push(("FLOW_STATE".into(), self.state.clone()));
173
        }
174
        if !self.bookmark.is_empty() {
175
            v.push(("FLOW_BOOKMARK".into(), self.bookmark.clone()));
176
        }
177
        if self.run_number > 0 {
178
            v.push(("FLOW_RUN".into(), self.run_number.to_string()));
179
        }
180
        for (k, val) in &self.env {
181
            if !v.iter().any(|(n, _)| n == k) {
182
                v.push((k.clone(), val.clone()));
183
            }
184
        }
185
        v
186
    }
187
}
188
189
/// One line of output (docs/m0.md §2).
190
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
191
pub struct Record {
192
    /// Milliseconds since the job started.
193
    pub t: u64,
194
    /// `out`, `err`, `console` (the VM outside the job) or `flow` (the runner).
195
    pub stream: String,
196
    pub text: String,
197
}
198
199
/// `POST /api/v1/flow/jobs/{id}/log`.
200
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
201
pub struct LogBatch {
202
    pub records: Vec<Record>,
203
}
204
205
/// `POST /api/v1/flow/jobs/{id}/finish`.
206
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
207
pub struct Finish {
208
    /// The job's exit code; absent when the runner could not run it.
209
    pub exit_code: Option<i32>,
210
    pub duration_ms: u64,
211
    pub receipt: Option<Receipt>,
212
    /// The receipt bound to the job's state and signed by the runner
213
    /// (Oxygen spec §14.6), hex of the signed receipt's bytes.
214
    #[serde(default, skip_serializing_if = "Option::is_none")]
215
    pub signed_receipt: Option<String>,
216
    /// Why the runner could not run the job (rootfs missing, VM failed).
217
    pub error: Option<String>,
218
}
219
220
/// Longest text of one record the hub keeps; longer lines are cut.
221
pub const MAX_RECORD_TEXT: usize = 16 * 1024;
222
/// Most records the hub keeps per job; later ones are dropped with a note.
223
pub const MAX_RECORDS: usize = 50_000;