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