OXYGENHUB
# 0e04dfc20211 oxygenhub / crates / hub / src / lib.rs
7.8 KiB · Rust · last changed Diffusion reconciler: every 10 minutes the pin index is checked and mended 20 days ago source blame history raw
1
//! OxygenHub: a forge for Oxygen repositories (`docs/spec.md`).
2
//!
3
//! One process: an axum router serving pages, the JSON API and the wire
4
//! protocol; SQLite for hub state; repositories as bare Oxygen stores.
5
6
pub mod api;
7
pub mod apt;
8
pub mod assistants;
9
pub mod auth;
10
pub mod config;
11
pub mod coverage;
12
pub mod db;
13
pub mod diffusion;
14
pub mod events;
15
pub mod flow;
16
pub mod hooks;
17
pub mod images;
18
pub mod junit;
19
pub mod keys;
20
pub mod levels;
21
pub mod live;
22
pub mod mail;
23
pub mod maintenance;
24
pub mod mirror;
25
pub mod notify;
26
pub mod oidc;
27
pub mod protocol;
28
pub mod render;
29
pub mod reports;
30
pub mod repos;
31
pub mod secrets;
32
pub mod signin;
33
pub mod social;
34
pub mod storage;
35
pub mod swarm;
36
pub mod variables;
37
pub mod web;
38
pub mod wiki;
39
40
use crate::config::Config;
41
use crate::repos::Repos;
42
use std::sync::Arc;
43
44
/// Everything a request handler can reach.
45
pub struct App {
46
    pub config: Config,
47
    pub db: db::Pool,
48
    pub repos: Arc<Repos>,
49
    /// Wikis, one Oxygen repository per repository (hub spec §6.11).
50
    pub wikis: Arc<Repos>,
51
    pub server: Arc<oxy_server::Server>,
52
    pub hub_key: keys::HubKey,
53
    pub cookie_key: auth::CookieKey,
54
    /// Seals AI assistants' tokens at rest (hub spec §13.4).
55
    pub assistant_key: assistants::SecretKey,
56
    /// Seals Flow secrets at rest (hub spec §13.2, #107).
57
    pub secret_key: assistants::SecretKey,
58
    /// The OIDC issuer for jobs (hub spec §13.5), made on first use.
59
    pub oidc: tokio::sync::OnceCell<oidc::Issuer>,
60
    /// Which assistants are answering which proposals, and recent calls
61
    /// per repository (hub spec §13.4).
62
    pub assistant_gate: std::sync::Mutex<assistants::Gate>,
63
    pub events: events::Events,
64
    /// Who is resolving which conflict together (hub spec §6.10).
65
    pub rooms: Arc<live::Rooms>,
66
    /// Outgoing mail: hub.toml's `[email]`, overridden by what the admin
67
    /// page saved (#101).
68
    pub mail: std::sync::RwLock<config::Email>,
69
    /// The image swarm's tracker (oxygenhub#144).
70
    pub swarm: swarm::Tracker,
71
    pub started: std::time::Instant,
72
}
73
74
impl App {
75
    /// The rootfs images this hub keeps for runners (oxygenflow#39).
76
    pub fn images(&self) -> images::Store {
77
        images::Store::new(&self.config.data)
78
    }
79
80
    /// The OIDC issuer, its key loaded or made the first time it is asked for.
81
    pub async fn oidc(&self) -> anyhow::Result<&oidc::Issuer> {
82
        let keys = self.config.keys_dir();
83
        self.oidc.get_or_try_init(|| async move { tokio::task::spawn_blocking(move || oidc::Issuer::load_or_create(&keys)).await? }).await
84
    }
85
}
86
87
pub type Shared = Arc<App>;
88
89
/// Open the data directory and build the application state. `init` must
90
/// have run once; `serve` and the tests call this.
91
pub async fn build(config: Config) -> anyhow::Result<Shared> {
92
    std::fs::create_dir_all(config.repos_dir())?;
93
    let db = db::open(&config.db_path()).await?;
94
    let repos = Arc::new(Repos::new(config.repos_dir()));
95
    let wikis = Arc::new(Repos::new(config.data.join("wikis")));
96
    let hub_key = keys::ensure_identity(&config.keys_dir(), &config.site.name, config.base_url())?;
97
    let cookie_key = auth::CookieKey::load_or_create(&config.keys_dir())?;
98
    let assistant_key = assistants::SecretKey::load_or_create(&config.keys_dir())?;
99
    let secret_key = assistants::SecretKey::load_or_create_named(&config.keys_dir(), "secrets.key")?;
100
    let backend = protocol::HubBackend::new(
101
        repos.clone(),
102
        wikis.clone(),
103
        db.clone(),
104
        config.auth.push_tokens.clone(),
105
        tokio::runtime::Handle::current(),
106
    );
107
    let slot = backend.app_slot();
108
    let server = oxy_server::Server::new(backend);
109
    let mail = mail::load(&db, &config.email).await;
110
    let app = Arc::new(App {
111
        mail: std::sync::RwLock::new(mail),
112
        config,
113
        db,
114
        repos,
115
        wikis,
116
        server,
117
        hub_key,
118
        cookie_key,
119
        assistant_key,
120
        secret_key,
121
        oidc: tokio::sync::OnceCell::new(),
122
        assistant_gate: std::sync::Mutex::new(assistants::Gate::default()),
123
        events: events::Events::default(),
124
        rooms: Arc::new(live::Rooms::default()),
125
        swarm: Default::default(),
126
        started: std::time::Instant::now(),
127
    });
128
    *slot.lock().unwrap() = Some(Arc::downgrade(&app));
129
    storage::load(&app).await?;
130
    coverage::load(&app).await?;
131
    junit::load(&app).await?;
132
    reports::load(&app).await?;
133
    tokio::spawn(hooks::worker(app.clone()));
134
    tokio::spawn(flow::reaper(app.clone()));
135
    tokio::spawn(flow::scheduler(app.clone()));
136
    tokio::spawn(flow::placement(app.clone()));
137
    tokio::spawn(maintenance::nightly(app.clone()));
138
    // The reverse pin index for repositories pushed before it existed (#148).
139
    tokio::spawn({
140
        let app = app.clone();
141
        async move { diffusion::backfill(&app).await }
142
    });
143
    // Mend the pin index and the bumps it drives (oxygenhub#210).
144
    tokio::spawn(diffusion::reconciler(app.clone()));
145
    // What each image is for, read or learned once (oxygenhub#173).
146
    tokio::spawn({
147
        let app = app.clone();
148
        async move {
149
            match images::learn_archs(&app).await {
150
                Ok(n) if n > 0 => tracing::info!(images = n, "read the architecture of kept images"),
151
                Ok(_) => {}
152
                Err(e) => tracing::warn!(error = %e, "learning image architectures"),
153
            }
154
        }
155
    });
156
    Ok(app)
157
}
158
159
/// The full router: pages, API, protocol and static assets.
160
pub fn router(app: Shared) -> axum::Router {
161
    web::router(app).layer(axum::middleware::from_fn(slow_requests))
162
}
163
164
/// Requests slower than this are logged, with their path (#145).
165
const SLOW: std::time::Duration = std::time::Duration::from_secs(2);
166
167
/// Log a slow request's method, path and time, so a slow page can be
168
/// found afterwards. Never the query, which may hold a token.
169
async fn slow_requests(req: axum::extract::Request, next: axum::middleware::Next) -> axum::response::Response {
170
    let (method, path) = (req.method().clone(), req.uri().path().to_string());
171
    let start = std::time::Instant::now();
172
    let res = next.run(req).await;
173
    let took = start.elapsed();
174
    if took >= SLOW {
175
        tracing::warn!("slow request: {method} {path} took {:.1} s ({})", took.as_secs_f64(), res.status().as_u16());
176
    }
177
    res
178
}
179
180
/// Serve on the configured addresses until the process is told to stop.
181
pub async fn serve(app: Shared) -> anyhow::Result<()> {
182
    let router = router(app.clone());
183
    let http = app.config.bind_addr()?;
184
    let https = app.config.tls_bind_addr()?;
185
    if http.is_none() && https.is_none() {
186
        anyhow::bail!("nothing to bind: set server.bind or server.tls_bind");
187
    }
188
    let mut tasks = tokio::task::JoinSet::new();
189
    if let Some(addr) = http {
190
        let listener = tokio::net::TcpListener::bind(addr).await?;
191
        tracing::info!("http on {addr}");
192
        let r = router.clone();
193
        tasks.spawn(async move {
194
            axum::serve(listener, r.into_make_service_with_connect_info::<std::net::SocketAddr>()).await.map_err(anyhow::Error::from)
195
        });
196
    }
197
    if let Some(addr) = https {
198
        let _ = rustls::crypto::ring::default_provider().install_default();
199
        let tls = axum_server::tls_rustls::RustlsConfig::from_pem_file(app.config.tls_cert(), app.config.tls_key())
200
            .await
201
            .map_err(|e| anyhow::anyhow!("loading TLS files: {e}"))?;
202
        tracing::info!("https on {addr}");
203
        let r = router.clone();
204
        tasks.spawn(async move {
205
            axum_server::bind_rustls(addr, tls)
206
                .serve(r.into_make_service_with_connect_info::<std::net::SocketAddr>())
207
                .await
208
                .map_err(anyhow::Error::from)
209
        });
210
    }
211
    tokio::select! {
212
        r = tasks.join_next() => { r.transpose()?.transpose()?; Ok(()) }
213
        _ = tokio::signal::ctrl_c() => { tracing::info!("stopping"); Ok(()) }
214
    }
215
}