OXYGENHUB
◆ v0.9.3 oxygenhub / crates / hub / src / main.rs
16.0 KiB · Rust · last changed Drops: per-user gists, each a small hidden Oxygen repository (#279) 15 days ago source blame history raw
1
//! `oxygenhub`: `init`, `serve`, `admin`, `check` (hub spec §17.3).
2
3
use clap::{Parser, Subcommand};
4
use hub_api::Visibility;
5
use oxygenhub::config::Config;
6
use oxygenhub::repos::RepoName;
7
use oxygenhub::{db, keys};
8
use std::path::PathBuf;
9
10
#[derive(Parser)]
11
#[command(name = "oxygenhub", version, about = "A forge for Oxygen repositories")]
12
struct Cli {
13
    /// The data directory (hub spec §3.2)
14
    #[arg(long, global = true, env = "OXYHUB_DATA", default_value = "data")]
15
    data: PathBuf,
16
    #[command(subcommand)]
17
    cmd: Cmd,
18
}
19
20
#[derive(Subcommand)]
21
enum Cmd {
22
    /// Create the data directory, keys, database and a default hub.toml
23
    Init {
24
        /// What links and clone URLs start with
25
        #[arg(long)]
26
        base_url: Option<String>,
27
        /// Generate a self-signed TLS certificate (developer edition)
28
        #[arg(long)]
29
        self_signed: bool,
30
    },
31
    /// Run the hub
32
    Serve {
33
        /// What this process does: web (pages, API, wire protocol, runner
34
        /// API), worker (the background tasks), or both, comma-separated.
35
        /// Overrides [server] roles; every role when neither is given
36
        /// (docs/roles.md)
37
        #[arg(long = "role", value_name = "ROLE", value_delimiter = ',')]
38
        roles: Vec<oxygenhub::config::Role>,
39
    },
40
    /// Offline administration
41
    Admin {
42
        #[command(subcommand)]
43
        cmd: AdminCmd,
44
    },
45
    /// Verify every repository's objects and signatures
46
    Check,
47
    /// Garbage-collect every repository now, as the nightly job does
48
    Gc,
49
}
50
51
#[derive(Subcommand)]
52
enum AdminCmd {
53
    /// Repositories
54
    Repo {
55
        #[command(subcommand)]
56
        cmd: RepoCmd,
57
    },
58
    /// Accounts
59
    User {
60
        #[command(subcommand)]
61
        cmd: UserCmd,
62
    },
63
    /// Backups (docs/backups.md)
64
    Backup {
65
        #[command(subcommand)]
66
        cmd: BackupCmd,
67
    },
68
    /// Copy a SQLite hub's records into this hub's empty PostgreSQL
69
    /// database, to move a hub (docs/postgres.md)
70
    #[cfg(feature = "postgres")]
71
    CopyDb {
72
        /// The old hub's `hub.db`, from a stopped hub (or a copy of one)
73
        #[arg(long)]
74
        from: PathBuf,
75
        /// Empty this database's tables first (a fresh hub's, a rehearsal's)
76
        #[arg(long)]
77
        replace: bool,
78
    },
79
}
80
81
#[derive(Subcommand)]
82
enum BackupCmd {
83
    /// Back up a user's or organisation's repositories now, as its settings say
84
    Run { owner: String },
85
    /// Restore every repository of a whole run from a backup directory
86
    /// (the one holding LATEST, runs/ and blobs/) into this hub
87
    Restore {
88
        #[arg(long)]
89
        from: std::path::PathBuf,
90
        /// The run (runs/<RUN>); the latest whole one when not given
91
        #[arg(long)]
92
        run: Option<String>,
93
    },
94
}
95
96
#[derive(Subcommand)]
97
enum UserCmd {
98
    /// Create an account with a generated password, printed once
99
    Create {
100
        username: String,
101
        #[arg(long, default_value = "")]
102
        email: String,
103
        /// Make it a site administrator
104
        #[arg(long)]
105
        admin: bool,
106
    },
107
    /// Reset an account's password to a new generated one, printed once
108
    ResetPassword { username: String },
109
    /// List accounts
110
    List,
111
}
112
113
#[derive(Subcommand)]
114
enum RepoCmd {
115
    /// Create an empty repository OWNER/NAME
116
    Create {
117
        slug: String,
118
        #[arg(long, default_value = "")]
119
        description: String,
120
        #[arg(long, default_value = "public")]
121
        visibility: String,
122
        /// Bind to this repository id now (32 hex characters, from `.oxy/id`);
123
        /// otherwise the first push binds it
124
        #[arg(long)]
125
        id: Option<String>,
126
    },
127
    /// List repositories
128
    List,
129
    /// Move a repository to the trash
130
    Delete { slug: String },
131
    /// Change the description, default bookmark or visibility
132
    Set {
133
        slug: String,
134
        #[arg(long)]
135
        description: Option<String>,
136
        #[arg(long)]
137
        default_bookmark: Option<String>,
138
        #[arg(long)]
139
        visibility: Option<String>,
140
    },
141
}
142
143
fn main() -> anyhow::Result<()> {
144
    tune_allocator();
145
    let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
146
    rt.spawn(return_freed_memory());
147
    let out = rt.block_on(run());
148
    oxygenhub::shutdown::finish(rt);
149
    out
150
}
151
152
/// glibc's allocator gives each thread that allocates an arena of its own
153
/// and keeps what is freed in it: under the hub's worker threads, loaded
154
/// states that the cache had let go still held their pages, and the hub
155
/// grew to 12 GB with its state cache capped at 3 GiB (oxygenhub#117).
156
/// Two arenas, as oxy found for its own threads; MALLOC_ARENA_MAX in the
157
/// environment still has the last word. Called before any thread starts.
158
fn tune_allocator() {
159
    #[cfg(all(target_os = "linux", target_env = "gnu"))]
160
    if std::env::var_os("MALLOC_ARENA_MAX").is_none() {
161
        // SAFETY: mallopt is called once, before any other thread exists.
162
        unsafe {
163
            libc::mallopt(libc::M_ARENA_MAX, 2);
164
        }
165
    }
166
}
167
168
/// Hand freed memory back to the system every minute, so the hub's size
169
/// follows what it holds rather than its peak (oxygenhub#117).
170
async fn return_freed_memory() {
171
    #[cfg(all(target_os = "linux", target_env = "gnu"))]
172
    loop {
173
        tokio::time::sleep(std::time::Duration::from_secs(60)).await;
174
        // SAFETY: malloc_trim only releases free pages.
175
        let _ = tokio::task::spawn_blocking(|| unsafe { libc::malloc_trim(0) }).await;
176
    }
177
}
178
179
async fn run() -> anyhow::Result<()> {
180
    tracing_subscriber::fmt()
181
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env().add_directive("oxygenhub=info".parse()?))
182
        .init();
183
    let cli = Cli::parse();
184
    match cli.cmd {
185
        Cmd::Init { base_url, self_signed } => init(&cli.data, base_url, self_signed).await,
186
        Cmd::Serve { roles } => {
187
            let mut config = Config::load(&cli.data)?;
188
            if !roles.is_empty() {
189
                config.server.roles = roles.iter().map(|r| r.name().to_string()).collect();
190
            }
191
            let app = oxygenhub::build(config).await?;
192
            oxygenhub::serve(app).await
193
        }
194
        Cmd::Admin { cmd: AdminCmd::Repo { cmd } } => {
195
            let config = Config::load(&cli.data)?;
196
            let app = oxygenhub::build(config).await?;
197
            repo_admin(&app, cmd).await
198
        }
199
        Cmd::Admin { cmd: AdminCmd::Backup { cmd } } => {
200
            let config = Config::load(&cli.data)?;
201
            let app = oxygenhub::build(config).await?;
202
            match cmd {
203
                BackupCmd::Run { owner } => {
204
                    let r = oxygenhub::backups::run(&app, &owner, "now").await?;
205
                    println!(
206
                        "{} {}: {} repositories, {} of {} files sent ({} KiB){}",
207
                        r.status,
208
                        r.at,
209
                        r.repos,
210
                        r.sent,
211
                        r.files,
212
                        r.bytes / 1024,
213
                        if r.detail.is_empty() { String::new() } else { format!(": {}", r.detail) }
214
                    );
215
                    if r.status == "failed" {
216
                        std::process::exit(1);
217
                    }
218
                    Ok(())
219
                }
220
                BackupCmd::Restore { from, run } => {
221
                    let dest: std::sync::Arc<dyn oxygenhub::backups::Destination> =
222
                        std::sync::Arc::new(oxygenhub::backups::Directory { root: from });
223
                    for d in oxygenhub::backups::restore_into_hub(&app, dest, run).await? {
224
                        println!("restored {d}");
225
                    }
226
                    Ok(())
227
                }
228
            }
229
        }
230
        #[cfg(feature = "postgres")]
231
        Cmd::Admin { cmd: AdminCmd::CopyDb { from, replace } } => {
232
            let config = Config::load(&cli.data)?;
233
            let pool = db::open(&config).await?;
234
            let copied = db::copy::from_sqlite(&pool, &from, replace).await?;
235
            for (table, n) in &copied {
236
                println!("{n:>9}  {table}");
237
            }
238
            println!("copied {} rows in {} tables", copied.iter().map(|(_, n)| n).sum::<i64>(), copied.len());
239
            Ok(())
240
        }
241
        Cmd::Admin { cmd: AdminCmd::User { cmd } } => {
242
            let config = Config::load(&cli.data)?;
243
            let app = oxygenhub::build(config).await?;
244
            user_admin(&app, cmd).await
245
        }
246
        Cmd::Check => {
247
            let config = Config::load(&cli.data)?;
248
            let app = oxygenhub::build(config).await?;
249
            check(&app).await
250
        }
251
        Cmd::Gc => {
252
            let config = Config::load(&cli.data)?;
253
            let app = oxygenhub::build(config).await?;
254
            let done = oxygenhub::maintenance::gc_all(&app).await;
255
            for d in &done {
256
                println!("{d}");
257
            }
258
            if done.iter().any(|d| d.failed) {
259
                anyhow::bail!("a repository could not be garbage collected");
260
            }
261
            Ok(())
262
        }
263
    }
264
}
265
266
async fn init(data: &PathBuf, base_url: Option<String>, self_signed: bool) -> anyhow::Result<()> {
267
    std::fs::create_dir_all(data)?;
268
    let toml_path = data.join("hub.toml");
269
    if !toml_path.exists() {
270
        let base = base_url.clone().unwrap_or_else(|| "http://127.0.0.1:7300".into());
271
        let mut text = String::from("# OxygenHub configuration (docs/spec.md section 17.1)\n\n[site]\nname = \"OxygenHub\"\n\n[server]\nbind = \"127.0.0.1:7300\"\n");
272
        if self_signed {
273
            text.push_str("tls_bind = \"0.0.0.0:7343\"\n");
274
        }
275
        text.push_str(&format!("base_url = \"{base}\"\n\n[tls]\nself_signed = {self_signed}\n\n[auth]\n# Bearer tokens allowed to push until accounts exist.\npush_tokens = []\n"));
276
        std::fs::write(&toml_path, text)?;
277
        println!("wrote {}", toml_path.display());
278
    }
279
    let mut config = Config::load(data)?;
280
    if let Some(b) = base_url {
281
        config.server.base_url = b;
282
    }
283
    let app = oxygenhub::build(config).await?;
284
    println!("hub key {}", app.hub_key.key.to_hex());
285
    if (self_signed || app.config.tls.self_signed)
286
        && keys::ensure_self_signed(&app.config.tls_cert(), &app.config.tls_key(), app.config.base_url())?
287
    {
288
        println!("wrote self-signed certificate {}", app.config.tls_cert().display());
289
    }
290
    println!("data directory {} is ready", data.display());
291
    Ok(())
292
}
293
294
async fn repo_admin(app: &oxygenhub::Shared, cmd: RepoCmd) -> anyhow::Result<()> {
295
    match cmd {
296
        RepoCmd::Create { slug, description, visibility, id } => {
297
            let name = RepoName::parse(&slug).ok_or_else(|| anyhow::anyhow!("{slug:?} is not a valid owner/name"))?;
298
            let visibility = Visibility::parse(&visibility).ok_or_else(|| anyhow::anyhow!("visibility is public, internal or private"))?;
299
            let id = match id {
300
                Some(h) => {
301
                    let bytes = hex::decode(h.trim()).ok().and_then(|v| <[u8; 16]>::try_from(v).ok());
302
                    Some(bytes.ok_or_else(|| anyhow::anyhow!("--id takes 32 hex characters"))?)
303
                }
304
                None => None,
305
            };
306
            if db::repos::get(&app.db, &name.owner, &name.name).await?.is_some() {
307
                anyhow::bail!("{name} already exists");
308
            }
309
            app.repos.create(&name, id)?;
310
            db::repos::create(&app.db, &name.owner, &name.name, &description, visibility).await?;
311
            println!("created {name}: oxy clone {}/{name}", app.config.base_url());
312
        }
313
        RepoCmd::List => {
314
            for r in db::repos::list_with_drops(&app.db).await? {
315
                let what = if r.is_drop() { format!("drop: {}", r.title) } else { r.description.clone() };
316
                println!("{:<40} {:<8} {what}", r.slug(), r.visibility);
317
            }
318
        }
319
        RepoCmd::Delete { slug } => {
320
            let name = RepoName::parse(&slug).ok_or_else(|| anyhow::anyhow!("{slug:?} is not a valid owner/name"))?;
321
            let row = db::repos::get(&app.db, &name.owner, &name.name).await?.ok_or_else(|| anyhow::anyhow!("{name} does not exist"))?;
322
            let dest = app.repos.trash(&name)?;
323
            db::repos::delete(&app.db, row.id).await?;
324
            println!("moved {name} to {}", dest.display());
325
        }
326
        RepoCmd::Set { slug, description, default_bookmark, visibility } => {
327
            let name = RepoName::parse(&slug).ok_or_else(|| anyhow::anyhow!("{slug:?} is not a valid owner/name"))?;
328
            let row = db::repos::get(&app.db, &name.owner, &name.name).await?.ok_or_else(|| anyhow::anyhow!("{name} does not exist"))?;
329
            if let Some(d) = description {
330
                db::repos::set_description(&app.db, row.id, &d).await?;
331
            }
332
            if let Some(b) = default_bookmark {
333
                db::repos::set_default_bookmark(&app.db, row.id, &b).await?;
334
            }
335
            if let Some(v) = visibility {
336
                let v = Visibility::parse(&v).ok_or_else(|| anyhow::anyhow!("visibility is public, internal or private"))?;
337
                db::repos::set_visibility(&app.db, row.id, v).await?;
338
            }
339
            println!("updated {name}");
340
        }
341
    }
342
    Ok(())
343
}
344
345
async fn user_admin(app: &oxygenhub::Shared, cmd: UserCmd) -> anyhow::Result<()> {
346
    use oxygenhub::auth;
347
    use oxygenhub::db::accounts;
348
    match cmd {
349
        UserCmd::Create { username, email, admin } => {
350
            if !oxygenhub::repos::names::valid_owner(&username) {
351
                anyhow::bail!("{username:?} is not a valid username");
352
            }
353
            if accounts::account_by_name(&app.db, &username).await?.is_some() || accounts::org_by_name(&app.db, &username).await?.is_some()
354
            {
355
                anyhow::bail!("{username} is taken");
356
            }
357
            let password = auth::random_hex(12);
358
            let hash = auth::hash_password(&password)?;
359
            accounts::create_account(&app.db, &username, &email, &hash, &auth::random_hex(16), admin).await?;
360
            accounts::audit(&app.db, None, "admin.user.create", &username, if admin { "administrator" } else { "" }).await?;
361
            println!("created {username}{}; one-time password: {password}", if admin { " (administrator)" } else { "" });
362
            println!("sign in at {}/login and change it under Settings", app.config.base_url());
363
        }
364
        UserCmd::ResetPassword { username } => {
365
            let account = accounts::account_by_name(&app.db, &username).await?.ok_or_else(|| anyhow::anyhow!("no such account"))?;
366
            let password = auth::random_hex(12);
367
            let hash = auth::hash_password(&password)?;
368
            accounts::set_password(&app.db, account.id, &hash, &auth::random_hex(16)).await?;
369
            accounts::audit(&app.db, None, "admin.user.reset_password", &username, "").await?;
370
            println!("new one-time password for {username}: {password}");
371
        }
372
        UserCmd::List => {
373
            for a in accounts::accounts(&app.db).await? {
374
                println!(
375
                    "{:<24} {:<32} {}{}{}",
376
                    a.username,
377
                    a.email,
378
                    if a.is_admin { "admin " } else { "" },
379
                    if a.banned { "banned " } else { "" },
380
                    if a.disabled { "disabled" } else { "" }
381
                );
382
            }
383
        }
384
    }
385
    Ok(())
386
}
387
388
async fn check(app: &oxygenhub::Shared) -> anyhow::Result<()> {
389
    let mut problems = 0;
390
    for r in db::repos::list_with_drops(&app.db).await? {
391
        let name = RepoName::new(&r.owner, &r.name).unwrap();
392
        let report = app.repos.read(&name, oxygenhub::repos::view::verification).await;
393
        match report {
394
            Ok(v) => {
395
                println!(
396
                    "{:<40} {} changes, {} signed, {} sealed, {} unsigned, {} invalid",
397
                    name,
398
                    v.total,
399
                    v.signed,
400
                    v.sealed,
401
                    v.unsigned,
402
                    v.invalid.len()
403
                );
404
                problems += v.invalid.len();
405
            }
406
            Err(e) => {
407
                println!("{name:<40} ERROR {e}");
408
                problems += 1;
409
            }
410
        }
411
    }
412
    if problems > 0 {
413
        anyhow::bail!("{problems} problem(s)");
414
    }
415
    Ok(())
416
}