| e0918b88 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::pipeline::{Job, Sandbox}; |
| 5 |
use crate::receipt::Receipt; |
| 6 |
use serde::{Deserialize, Serialize}; |
| 7 |
|
| 8 |
|
| 9 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 10 |
pub struct ClaimRequest { |
| 11 |
|
| 12 |
pub runner: String, |
| 13 |
|
| 14 |
pub runtimes: Vec<String>, |
| bb643c25 Andy McCall ↶ |
15 |
|
| 16 |
|
| 17 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 18 |
pub free_memory_mib: Option<u32>, |
| 19 |
|
| 20 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 21 |
pub free_cpus: Option<u32>, |
| f98a8f78 Andy McCall ↶ |
22 |
|
| 23 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 24 |
pub info: Option<RunnerInfo>, |
| 8c0a4e3a Andy McCall ↶ |
25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 30 |
pub images: Option<Vec<String>>, |
| 1abadd4a Andy McCall ↶ |
31 |
|
| 32 |
|
| 33 |
|
| 34 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 35 |
pub image_archs: Option<std::collections::BTreeMap<String, String>>, |
| 4cf0c99a Andy McCall ↶ |
36 |
|
| 37 |
|
| 38 |
|
| 39 |
#[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 40 |
pub fetches_images: bool, |
| 1daca0b6 Andy McCall ↶ |
41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 46 |
pub holding: Option<Vec<i64>>, |
| 62829d15 Andy McCall ↶ |
47 |
|
| 48 |
|
| 49 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 50 |
pub facts: Option<Facts>, |
| b14aeaae Andy McCall ↶ |
51 |
|
| 52 |
|
| 53 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 54 |
pub swarm: Option<SwarmAddr>, |
| 82cf6e91 Andy McCall ↶ |
55 |
|
| 56 |
|
| 57 |
|
| 58 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 59 |
pub caches: Option<Vec<String>>, |
| 1e6b9fb1 Andy McCall ↶ |
60 |
|
| 61 |
|
| 62 |
|
| 63 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 64 |
pub cache_times: Option<std::collections::BTreeMap<String, u64>>, |
| 82cf6e91 Andy McCall ↶ |
65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
pub fn segment(s: &str) -> String { |
| 71 |
let clean: String = s.chars().map(|c| if c.is_ascii_alphanumeric() || "._-".contains(c) { c } else { '_' }).take(48).collect(); |
| 72 |
format!("{clean}-{}", &blake3::hash(s.as_bytes()).to_hex()[..8]) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
pub fn cache_slot(repo: &str, job: &str) -> String { |
| 79 |
format!("{}/{}", segment(repo), segment(job)) |
| b14aeaae Andy McCall ↶ |
80 |
} |
| 81 |
|
| ecce3719 Andy McCall ↶ |
82 |
|
| 83 |
|
| 84 |
|
| b14aeaae Andy McCall ↶ |
85 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 86 |
pub struct SwarmAddr { |
| 87 |
pub addr: String, |
| 88 |
#[serde(default)] |
| 89 |
pub site: String, |
| ecce3719 Andy McCall ↶ |
90 |
#[serde(default)] |
| 91 |
pub cert: String, |
| b14aeaae Andy McCall ↶ |
92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 97 |
pub struct Announce { |
| 98 |
pub image: String, |
| 99 |
|
| 100 |
pub state: String, |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 106 |
pub struct Announced { |
| 107 |
pub ticket: String, |
| 108 |
pub peers: Vec<SwarmPeer>, |
| 109 |
|
| 110 |
pub interval_s: u64, |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 115 |
pub struct SwarmPeer { |
| 116 |
pub runner: i64, |
| 117 |
pub name: String, |
| 118 |
pub addr: String, |
| ecce3719 Andy McCall ↶ |
119 |
|
| 120 |
|
| 121 |
pub cert: String, |
| b14aeaae Andy McCall ↶ |
122 |
|
| 123 |
pub complete: bool, |
| 62829d15 Andy McCall ↶ |
124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 130 |
pub struct Facts { |
| 131 |
|
| 132 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 133 |
pub free_disk_mib: Option<u64>, |
| 134 |
|
| 135 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 136 |
pub load_x100: Option<u32>, |
| 137 |
|
| 138 |
|
| 139 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 140 |
pub speed: Option<u32>, |
| 141 |
|
| 142 |
|
| 143 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 144 |
pub speed_class: Option<u8>, |
| 145 |
|
| 146 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 147 |
pub kernel: Option<String>, |
| 148 |
} |
| 149 |
|
| 150 |
impl Facts { |
| 151 |
|
| 152 |
|
| 153 |
pub fn class_of(speed: u32) -> u8 { |
| 154 |
match speed { |
| 155 |
0..=34 => 1, |
| 156 |
35..=69 => 2, |
| 157 |
70..=139 => 3, |
| 158 |
140..=279 => 4, |
| 159 |
_ => 5, |
| 160 |
} |
| 161 |
} |
| f98a8f78 Andy McCall ↶ |
162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 167 |
pub struct RunnerInfo { |
| 168 |
|
| 169 |
pub arch: String, |
| 170 |
pub os: String, |
| 171 |
|
| 172 |
pub version: String, |
| 173 |
|
| 174 |
#[serde(default)] |
| 175 |
pub labels: Vec<String>, |
| 176 |
|
| 177 |
pub max_jobs: u32, |
| 178 |
#[serde(default)] |
| 179 |
pub memory_mib: Option<u32>, |
| 180 |
#[serde(default)] |
| 181 |
pub cpus: Option<u32>, |
| 182 |
} |
| 183 |
|
| 184 |
impl RunnerInfo { |
| 185 |
|
| 186 |
pub fn here(labels: Vec<String>, max_jobs: u32, memory_mib: Option<u32>, cpus: Option<u32>) -> RunnerInfo { |
| 187 |
RunnerInfo { |
| 188 |
arch: std::env::consts::ARCH.into(), |
| 189 |
os: std::env::consts::OS.into(), |
| 190 |
version: env!("CARGO_PKG_VERSION").into(), |
| 191 |
labels, |
| 192 |
max_jobs, |
| 193 |
memory_mib, |
| 194 |
cpus, |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 203 |
pub struct Register { |
| 204 |
pub token: String, |
| 205 |
pub name: String, |
| 206 |
|
| 207 |
|
| 208 |
pub identity: String, |
| 209 |
pub info: RunnerInfo, |
| 210 |
} |
| 211 |
|
| 212 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 213 |
pub struct Registered { |
| 214 |
pub id: i64, |
| 215 |
pub name: String, |
| 216 |
|
| 217 |
pub credential: String, |
| e0918b88 Andy McCall |
218 |
} |
| 219 |
|
| c23d5191 Andy McCall ↶ |
220 |
|
| 221 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 222 |
pub struct SubrepoInput { |
| 223 |
pub path: String, |
| 224 |
|
| 225 |
pub repo: String, |
| 226 |
pub clone_url: String, |
| 227 |
pub heads: Vec<String>, |
| 228 |
} |
| 229 |
|
| e2c12561 Andy McCall ↶ |
230 |
|
| 231 |
|
| 232 |
|
| 233 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 234 |
pub struct SecretInput { |
| 235 |
pub name: String, |
| 236 |
pub path: String, |
| 237 |
pub value: String, |
| 238 |
} |
| 239 |
|
| e0918b88 Andy McCall |
240 |
|
| 241 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 242 |
pub struct Claim { |
| 243 |
pub job_id: i64, |
| 244 |
pub run_id: i64, |
| 245 |
|
| 246 |
pub repo: String, |
| 247 |
|
| 248 |
pub clone_url: String, |
| 249 |
|
| 250 |
pub token: String, |
| 251 |
|
| 252 |
pub heads: Vec<String>, |
| 253 |
|
| 254 |
pub pipeline_hash: String, |
| 255 |
pub job: Job, |
| 256 |
pub sandbox: Sandbox, |
| 257 |
|
| 258 |
pub timeout_s: u64, |
| c23d5191 Andy McCall ↶ |
259 |
|
| 260 |
|
| 261 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 262 |
pub subrepos: Vec<SubrepoInput>, |
| c4285535 Andy McCall ↶ |
263 |
|
| 264 |
#[serde(default, skip_serializing_if = "String::is_empty")] |
| 265 |
pub state: String, |
| 266 |
|
| 267 |
#[serde(default, skip_serializing_if = "String::is_empty")] |
| 268 |
pub bookmark: String, |
| 269 |
|
| 270 |
#[serde(default)] |
| 271 |
pub run_number: i64, |
| c58efc6a Andy McCall ↶ |
272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")] |
| 278 |
pub env: std::collections::BTreeMap<String, String>, |
| e2c12561 Andy McCall ↶ |
279 |
|
| 280 |
|
| 281 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 282 |
pub secrets: Vec<SecretInput>, |
| 46fe2c81 Andy McCall ↶ |
283 |
|
| 284 |
|
| 285 |
|
| 286 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 287 |
pub inputs: Vec<ArtifactInput>, |
| 1b160d37 Andy McCall ↶ |
288 |
|
| 289 |
|
| 290 |
|
| 291 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 292 |
pub expected_ms: Option<u64>, |
| f201e1bc Andy McCall ↶ |
293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 298 |
pub checkpoint: Option<String>, |
| 46fe2c81 Andy McCall ↶ |
299 |
} |
| 300 |
|
| 301 |
|
| 302 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 303 |
pub struct ArtifactInput { |
| 304 |
pub artifact: String, |
| 305 |
|
| 306 |
pub from: String, |
| 307 |
|
| 308 |
pub path: String, |
| 309 |
|
| 310 |
pub blob: String, |
| c4285535 Andy McCall ↶ |
311 |
} |
| 312 |
|
| 313 |
impl Claim { |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
pub fn flow_env(&self) -> Vec<(String, String)> { |
| 319 |
let mut v = vec![ |
| 320 |
("FLOW_REPO".to_string(), self.repo.clone()), |
| 321 |
("FLOW_HEADS".to_string(), self.heads.join("+")), |
| 322 |
("FLOW_JOB".to_string(), self.job.name.clone()), |
| 323 |
]; |
| 324 |
if !self.state.is_empty() { |
| 325 |
v.push(("FLOW_STATE".into(), self.state.clone())); |
| 326 |
} |
| 327 |
if !self.bookmark.is_empty() { |
| 328 |
v.push(("FLOW_BOOKMARK".into(), self.bookmark.clone())); |
| 329 |
} |
| 330 |
if self.run_number > 0 { |
| 331 |
v.push(("FLOW_RUN".into(), self.run_number.to_string())); |
| 332 |
} |
| c58efc6a Andy McCall ↶ |
333 |
for (k, val) in &self.env { |
| 334 |
if !v.iter().any(|(n, _)| n == k) { |
| 335 |
v.push((k.clone(), val.clone())); |
| 336 |
} |
| 337 |
} |
| c4285535 Andy McCall ↶ |
338 |
v |
| 339 |
} |
| e0918b88 Andy McCall |
340 |
} |
| 341 |
|
| 342 |
|
| 343 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 344 |
pub struct Record { |
| 345 |
|
| 346 |
pub t: u64, |
| 347 |
|
| 348 |
pub stream: String, |
| 349 |
pub text: String, |
| 350 |
} |
| 351 |
|
| b11e30f5 Andy McCall ↶ |
352 |
|
| 353 |
|
| 354 |
|
| 355 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 356 |
pub struct Decline { |
| 357 |
pub reason: String, |
| 358 |
} |
| 359 |
|
| e0918b88 Andy McCall |
360 |
|
| 361 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 362 |
pub struct LogBatch { |
| 363 |
pub records: Vec<Record>, |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 368 |
pub struct Finish { |
| 369 |
|
| 370 |
pub exit_code: Option<i32>, |
| 371 |
pub duration_ms: u64, |
| 372 |
pub receipt: Option<Receipt>, |
| 47c1d64d Andy McCall ↶ |
373 |
|
| 374 |
|
| 375 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 376 |
pub signed_receipt: Option<String>, |
| e0918b88 Andy McCall |
377 |
|
| 378 |
pub error: Option<String>, |
| 8f211e20 Andy McCall ↶ |
379 |
|
| 380 |
|
| 381 |
|
| 382 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 383 |
pub peak_disk_mib: Option<u64>, |
| 384 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 385 |
pub peak_memory_mib: Option<u64>, |
| e0918b88 Andy McCall |
386 |
} |
| 387 |
|
| 388 |
|
| 389 |
pub const MAX_RECORD_TEXT: usize = 16 * 1024; |
| 390 |
|
| 391 |
pub const MAX_RECORDS: usize = 50_000; |