1
//! Brings every tenant database to the current schema.
2
//!
3
//! Runs as its own process — a k8s Job, not a serving container. See
4
//! [`server::fleet`] for why this must never live inside `boot()`.
5
//!
6
//! Exit codes are the Job's contract: `0` means every tenant was verified at the
7
//! current schema, `1` means the run left something undone — a tenant
8
//! quarantined, one still held by another worker, or a verdict that could not be
9
//! written — and `2` means the run could not be performed at all. Only `0` may
10
//! gate a rollout: the other two all mean some customer's database is in a state
11
//! nobody has confirmed.
12

            
13
use server::bootstrap::ADMIN_USER_ID;
14
use server::db::DBError;
15
use server::fleet::migrate_fleet;
16
use sqlx::types::Uuid;
17
use std::process::ExitCode;
18

            
19
#[cfg(test)]
20
mod tests;
21

            
22
/// Every tenant verified at the current schema.
23
const HEALTHY: u8 = 0;
24
/// The run finished but left something unconfirmed.
25
const DEGRADED: u8 = 1;
26
/// The run could not be performed.
27
const UNUSABLE: u8 = 2;
28

            
29
/// The Job's contract with Flux, kept separate from the logging so it can be
30
/// tested: a wrong code here is the difference between a rollout that stops and
31
/// one that proceeds over a database nobody has confirmed.
32
6
fn verdict(outcome: &Result<server::fleet::FleetReport, DBError>) -> u8 {
33
6
    match outcome {
34
5
        Ok(report) if report.is_healthy() => HEALTHY,
35
3
        Ok(_) => DEGRADED,
36
1
        Err(_) => UNUSABLE,
37
    }
38
6
}
39

            
40
5
fn report_outcome(outcome: &Result<server::fleet::FleetReport, DBError>) {
41
5
    match outcome {
42
4
        Ok(report) if report.is_healthy() => {
43
1
            tracing::info!("fleet is at the current schema: {}", report.summary());
44
        }
45
        Ok(report) => {
46
3
            tracing::error!("fleet migration degraded: {}", report.summary());
47
3
            for (id, reason) in &report.quarantined {
48
1
                tracing::error!("  {id} quarantined: {reason}");
49
            }
50
3
            for (id, reason) in &report.job_failed {
51
                tracing::error!("  {id} is at the current schema but a data job failed: {reason}");
52
            }
53
3
            for id in &report.unresolved {
54
1
                tracing::error!("  {id} was held by another worker and never verified here");
55
            }
56
3
            for (id, reason) in &report.unpublished {
57
1
                tracing::error!("  {id} has a verdict that could not be recorded: {reason}");
58
            }
59
        }
60
1
        Err(e) => tracing::error!("fleet migration could not run: {e}"),
61
    }
62
5
}
63

            
64
#[tokio::main]
65
async fn main() -> ExitCode {
66
    if let Err(e) = server::telemetry::init_logging(None, tracing::level_filters::LevelFilter::INFO)
67
    {
68
        eprintln!("logging init failed: {e}");
69
    }
70

            
71
    let outcome = run().await;
72
    report_outcome(&outcome);
73
    ExitCode::from(verdict(&outcome))
74
}
75

            
76
async fn run() -> Result<server::fleet::FleetReport, DBError> {
77
    sqlx::any::install_default_drivers();
78

            
79
    let admin = ADMIN_USER_ID
80
        .parse::<Uuid>()
81
        .map_err(|e| DBError::FleetWorkerLost(format!("admin id is not a uuid: {e}")))?;
82

            
83
    migrate_fleet(admin).await
84
}