rebuilderd-common 0.27.0

rebuilderd - common code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
mod models;

use crate::api::{Client, ZstdRequestBuilder};
use crate::errors::*;
use async_trait::async_trait;
pub use models::*;
use std::borrow::Cow;

#[cfg(feature = "diesel")]
use diesel::{
    deserialize::FromSql,
    serialize::{IsNull, Output, ToSql},
    sql_types::Integer,
    sqlite::{Sqlite, SqliteValue},
    {AsExpression, FromSqlRow},
};
use serde::{Deserialize, Serialize};

/// Represents the priority of an enqueued rebuild job. The job queue is sorted based on priority and
/// time, so the lower this number is, the more prioritized the job is. It's a little backwards, but
/// hey.
///
/// There are some utility functions on the type for accessing default values for well-defined use
/// cases. These map to constants in the same namespace as this type, and you can use either one.
/// ```
/// use rebuilderd_common::api::v1::Priority;
///
/// assert_eq!(Priority::from(1), Priority::default());
/// assert_eq!(Priority::from(2), Priority::retry());
/// assert_eq!(Priority::from(0), Priority::manual());
/// ```
///
/// You can also set a completely custom priority. This is mostly useful for external API calls that
/// orchestrate rebuilds.
/// ```
/// use rebuilderd_common::api::v1::Priority;
///
/// let custom = Priority::from(10);
/// assert_eq!(custom, Priority::from(10));
///
/// ```
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, AsExpression))]
#[cfg_attr(feature = "diesel", diesel(sql_type = Integer))]
#[cfg_attr(feature = "diesel", diesel(check_for_backend(diesel::sqlite::Sqlite)))]
pub struct Priority(i32);

impl Priority {
    /// The default priority for enqueued rebuilds. The job queue is sorted based on priority and time,
    /// so the lower this number is, the more prioritized the job is. It's a little backwards, but hey.
    const DEFAULT_QUEUE_PRIORITY: i32 = 1;

    /// The default priority used for automatically requeued jobs. This priority is lower than the one
    /// for untested packages.
    const DEFAULT_RETRY_PRIORITY: i32 = Self::DEFAULT_QUEUE_PRIORITY + 1;

    /// The default priority used for manually retried jobs. This priority is higher than the one for
    /// untested packages.
    const DEFAULT_MANUAL_PRIORITY: i32 = Self::DEFAULT_QUEUE_PRIORITY - 1;

    pub fn retry() -> Self {
        Priority(Self::DEFAULT_RETRY_PRIORITY)
    }

    pub fn manual() -> Self {
        Priority(Self::DEFAULT_MANUAL_PRIORITY)
    }
}

impl Default for Priority {
    fn default() -> Self {
        Priority(Self::DEFAULT_QUEUE_PRIORITY)
    }
}

#[cfg(feature = "diesel")]
impl FromSql<Integer, Sqlite> for Priority {
    fn from_sql(bytes: SqliteValue) -> diesel::deserialize::Result<Self> {
        let value = <i32 as FromSql<Integer, Sqlite>>::from_sql(bytes)?;
        Ok(Priority(value))
    }
}

#[cfg(feature = "diesel")]
impl ToSql<Integer, Sqlite> for Priority {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> diesel::serialize::Result {
        out.set_value(self.0);
        Ok(IsNull::No)
    }
}

impl From<i32> for Priority {
    fn from(value: i32) -> Self {
        Priority(value)
    }
}

#[async_trait]
pub trait BuildRestApi {
    async fn get_builds(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<ResultPage<Rebuild>>;

    async fn submit_build_report(&self, request: RebuildReport) -> Result<()>;
    async fn get_build(&self, id: i32) -> Result<Rebuild>;
    async fn get_build_log(&self, id: i32) -> Result<String>;
    async fn get_build_artifacts(&self, id: i32) -> Result<Vec<RebuildArtifact>>;
    async fn get_build_artifact(&self, id: i32, artifact_id: i32) -> Result<RebuildArtifact>;
    async fn get_build_artifact_diffoscope(&self, id: i32, artifact_id: i32) -> Result<String>;
    async fn get_build_artifact_attestation(&self, id: i32, artifact_id: i32) -> Result<Vec<u8>>;
}

#[async_trait]
pub trait DashboardRestApi {
    async fn get_dashboard(&self, origin_filter: Option<&OriginFilter>) -> Result<DashboardState>;
}

#[async_trait]
pub trait MetaRestApi {
    async fn get_distributions(&self) -> Result<Vec<String>>;
    async fn get_distribution_releases(&self, distribution: &str) -> Result<Vec<String>>;
    async fn get_distribution_architectures(&self, distribution: &str) -> Result<Vec<String>>;
    async fn get_distribution_release_architectures(
        &self,
        distribution: &str,
        release: &str,
    ) -> Result<Vec<String>>;

    async fn get_public_keys(&self) -> Result<PublicKey>;
}

#[async_trait]
pub trait PackageRestApi {
    async fn submit_package_report(&self, report: &PackageReport) -> Result<()>;

    async fn get_source_packages(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<ResultPage<SourcePackage>>;

    async fn get_source_package(&self, id: i32) -> Result<SourcePackage>;

    async fn get_binary_packages(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        binary_identity_filter: Option<&BinaryIdentityFilter>,
    ) -> Result<ResultPage<BinaryPackage>>;

    async fn get_binary_package(&self, id: i32) -> Result<BinaryPackage>;
}

#[async_trait]
pub trait QueueRestApi {
    async fn get_queued_jobs(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<ResultPage<QueuedJob>>;

    async fn request_rebuild(&self, request: QueueJobRequest) -> Result<()>;
    async fn get_queued_job(&self, id: i32) -> Result<QueuedJob>;
    async fn drop_queued_job(&self, id: i32) -> Result<()>;
    async fn drop_queued_jobs(
        &self,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<()>;
    async fn request_work(&self, request: PopQueuedJobRequest) -> Result<JobAssignment>;
    async fn ping_job(&self, id: i32) -> Result<()>;
}

#[async_trait]
pub trait WorkerRestApi {
    async fn get_workers(&self, page: Option<&Page>) -> Result<ResultPage<Worker>>;
    async fn register_worker(&self, request: RegisterWorkerRequest) -> Result<()>;
    async fn get_worker(&self, id: i32) -> Result<Worker>;
    async fn unregister_worker(&self, id: i32) -> Result<()>;
}

#[async_trait]
impl BuildRestApi for Client {
    async fn get_builds(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<ResultPage<Rebuild>> {
        let records = self
            .get(Cow::Borrowed("api/v1/builds"))
            .query(&page)
            .query(&origin_filter)
            .query(&source_identity_filter)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(records)
    }

    async fn submit_build_report(&self, request: RebuildReport) -> Result<()> {
        self.post(Cow::Borrowed("api/v1/builds"))
            .json(&request)
            .send_encoded()
            .await?
            .error_for_status()?;

        Ok(())
    }

    async fn get_build(&self, id: i32) -> Result<Rebuild> {
        let record = self
            .get(Cow::Owned(format!("api/v1/builds/{id}")))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(record)
    }

    async fn get_build_log(&self, id: i32) -> Result<String> {
        let data = self
            .get(Cow::Owned(format!("api/v1/builds/{id}/log")))
            .send()
            .await?
            .error_for_status()?
            .text()
            .await?;

        Ok(data)
    }

    async fn get_build_artifacts(&self, id: i32) -> Result<Vec<RebuildArtifact>> {
        let records = self
            .get(Cow::Owned(format!("api/v1/builds/{id}/artifacts")))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(records)
    }

    async fn get_build_artifact(&self, id: i32, artifact_id: i32) -> Result<RebuildArtifact> {
        let record = self
            .get(Cow::Owned(format!(
                "api/v1/builds/{id}/artifacts/{artifact_id}"
            )))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(record)
    }

    async fn get_build_artifact_diffoscope(&self, id: i32, artifact_id: i32) -> Result<String> {
        let data = self
            .get(Cow::Owned(format!(
                "api/v1/builds/{id}/artifacts/{artifact_id}/diffoscope"
            )))
            .send()
            .await?
            .error_for_status()?
            .text()
            .await?;

        Ok(data)
    }

    async fn get_build_artifact_attestation(&self, id: i32, artifact_id: i32) -> Result<Vec<u8>> {
        let data = self
            .get(Cow::Owned(format!(
                "api/v1/builds/{id}/artifacts/{artifact_id}/attestation"
            )))
            .send()
            .await?
            .error_for_status()?
            .bytes()
            .await?;

        Ok(Vec::from(data))
    }
}

#[async_trait]
impl DashboardRestApi for Client {
    async fn get_dashboard(&self, origin_filter: Option<&OriginFilter>) -> Result<DashboardState> {
        let dashboard = self
            .get(Cow::Borrowed("api/v1/dashboard"))
            .query(&origin_filter)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(dashboard)
    }
}

#[async_trait]
impl MetaRestApi for Client {
    async fn get_distributions(&self) -> Result<Vec<String>> {
        let results = self
            .get(Cow::Borrowed("api/v1/meta/distributions"))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(results)
    }

    async fn get_distribution_releases(&self, distribution: &str) -> Result<Vec<String>> {
        let results = self
            .get(Cow::Owned(format!(
                "api/v1/meta/distributions/{distribution}/releases"
            )))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(results)
    }

    async fn get_distribution_architectures(&self, distribution: &str) -> Result<Vec<String>> {
        let results = self
            .get(Cow::Owned(format!(
                "api/v1/meta/distributions/{distribution}/architectures"
            )))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(results)
    }

    async fn get_distribution_release_architectures(
        &self,
        distribution: &str,
        release: &str,
    ) -> Result<Vec<String>> {
        let results = self
            .get(Cow::Owned(format!(
                "api/v1/meta/distributions/{distribution}/releases/{release}/architectures"
            )))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(results)
    }

    async fn get_public_keys(&self) -> Result<PublicKey> {
        let public_key = self
            .get(Cow::Borrowed("api/v1/meta/public-keys"))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(public_key)
    }
}

#[async_trait]
impl PackageRestApi for Client {
    async fn submit_package_report(&self, report: &PackageReport) -> Result<()> {
        self.post(Cow::Borrowed("api/v1/packages"))
            .json(report)
            .send_encoded()
            .await?
            .error_for_status()?;

        Ok(())
    }

    async fn get_source_packages(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<ResultPage<SourcePackage>> {
        let records = self
            .get(Cow::Borrowed("api/v1/packages/source"))
            .query(&page)
            .query(&origin_filter)
            .query(&source_identity_filter)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(records)
    }

    async fn get_source_package(&self, id: i32) -> Result<SourcePackage> {
        let record = self
            .get(Cow::Owned(format!("api/v1/packages/source/{id}")))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(record)
    }

    async fn get_binary_packages(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        binary_identity_filter: Option<&BinaryIdentityFilter>,
    ) -> Result<ResultPage<BinaryPackage>> {
        let records = self
            .get(Cow::Borrowed("api/v1/packages/binary"))
            .query(&page)
            .query(&origin_filter)
            .query(&binary_identity_filter)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(records)
    }

    async fn get_binary_package(&self, id: i32) -> Result<BinaryPackage> {
        let record = self
            .get(Cow::Owned(format!("api/v1/packages/binary/{id}")))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(record)
    }
}

#[async_trait]
impl QueueRestApi for Client {
    async fn get_queued_jobs(
        &self,
        page: Option<&Page>,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<ResultPage<QueuedJob>> {
        let records = self
            .get(Cow::Borrowed("api/v1/queue"))
            .query(&page)
            .query(&origin_filter)
            .query(&source_identity_filter)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(records)
    }

    async fn request_rebuild(&self, request: QueueJobRequest) -> Result<()> {
        self.post(Cow::Borrowed("api/v1/queue"))
            .json(&request)
            .send_encoded()
            .await?
            .error_for_status()?;

        Ok(())
    }

    async fn get_queued_job(&self, id: i32) -> Result<QueuedJob> {
        let record = self
            .get(Cow::Owned(format!("api/v1/queue/{id}")))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(record)
    }

    async fn drop_queued_job(&self, id: i32) -> Result<()> {
        self.delete(Cow::Owned(format!("api/v1/queue/{id}")))
            .send()
            .await?
            .error_for_status()?;

        Ok(())
    }

    async fn drop_queued_jobs(
        &self,
        origin_filter: Option<&OriginFilter>,
        source_identity_filter: Option<&SourceIdentityFilter>,
    ) -> Result<()> {
        self.delete(Cow::Borrowed("api/v1/queue"))
            .query(&origin_filter)
            .query(&source_identity_filter)
            .send()
            .await?
            .error_for_status()?;

        Ok(())
    }

    async fn request_work(&self, request: PopQueuedJobRequest) -> Result<JobAssignment> {
        let record = self
            .post(Cow::Borrowed("api/v1/queue/pop"))
            .json(&request)
            .send_encoded()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(record)
    }

    async fn ping_job(&self, id: i32) -> Result<()> {
        // nginx dies if proxying a request without a Content-Length header
        self.post(Cow::Owned(format!("api/v1/queue/{id}/ping")))
            .header("Content-Length", 0)
            .send()
            .await?
            .error_for_status()?;

        Ok(())
    }
}

#[async_trait]
impl WorkerRestApi for Client {
    async fn get_workers(&self, page: Option<&Page>) -> Result<ResultPage<Worker>> {
        let workers = self
            .get(Cow::Borrowed("api/v1/workers"))
            .query(&page)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(workers)
    }

    async fn register_worker(&self, request: RegisterWorkerRequest) -> Result<()> {
        self.post(Cow::Borrowed("api/v1/workers"))
            .json(&request)
            .send_encoded()
            .await?
            .error_for_status()?;

        Ok(())
    }

    async fn get_worker(&self, id: i32) -> Result<Worker> {
        let worker = self
            .get(Cow::Owned(format!("api/v1/workers/{id}")))
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        Ok(worker)
    }

    async fn unregister_worker(&self, id: i32) -> Result<()> {
        self.delete(Cow::Owned(format!("api/v1/workers/{id}")))
            .send()
            .await?
            .error_for_status()?;

        Ok(())
    }
}