OXYGENHUB

Coverage / crates/hub/src/web/runner_updates.rs 92.5%

308 of 333 lines run, 55 of 130 functions · run #473 on main · run missed

1
//! Admin → Runners → Updates (oxygenhub#322): where runner builds come
2
//! from, and who takes them: the site's, each owner's, each test group's
3
//! and each runner's settings, the nearest winning; test groups of chosen
4
//! runners or a percentage of them.
5
6
use super::settings::csrf;
7
use super::{filters, Error, Page, Shell};
8
use crate::auth::Viewer;
9
use crate::db::runner_updates::{self as rdb, Group, Policy};
10
use crate::db::{accounts, runners};
11
use crate::runner_updates::{self as ru, Levels};
12
use crate::Shared;
13
use askama::Template;
14
use axum::extract::{Form, Path, State};
15
use flow_core::api::UpdatePolicy;
16
use serde::Deserialize;
17
1815×
fn require_admin(viewer: &Viewer, shell: &Shell) -> Result<accounts::Account, Error> {
1915×
    match viewer.account() {
2014×
        Some(a) if a.is_admin => Ok(a.clone()),
211×
        _ => Err(Error::not_found(shell.clone())),
22
    }
2315×
}
24
25
/// One level's settings as its form shows them: `inherit`, `on` or `off`
26
/// for the switches, blank for an inherited channel or window.
27
pub struct PolicyView {
28
    pub scope: String,
29
    pub key: String,
30
    pub label: String,
31
    pub updates: &'static str,
32
    pub channel: String,
33
    pub window: String,
34
    pub dry_run: &'static str,
35
}
36
37
impl PolicyView {
38144×
    fn of(p: &Policy, label: String) -> PolicyView {
39288×
        let tri = |b: Option<bool>| match b {
4040×
            Some(true) => "on",
4114×
            Some(false) => "off",
42234×
            None => "inherit",
43288×
        };
44
        PolicyView {
45144×
            scope: p.scope.clone(),
46144×
            key: p.key.clone(),
47144×
            label,
48144×
            updates: tri(p.updates),
49144×
            channel: p.channel.clone().unwrap_or_default(),
50144×
            window: match (p.window_from, p.window_to) {
517×
                (Some(a), Some(b)) => format!("{a:02}-{b:02}"),
52137×
                _ => String::new(),
53
            },
54144×
            dry_run: tri(p.dry_run),
55
        }
56144×
    }
57
58
    /// The settings in words, for a table.
5921×
    pub fn summary(&self) -> String {
6021×
        let mut s = vec![];
6121×
        if self.updates != "inherit" {
6215×
            s.push(format!("updates {}", self.updates));
6315×
        }
6421×
        if !self.channel.is_empty() {
652×
            s.push(format!("channel {}", self.channel));
6619×
        }
6721×
        if !self.window.is_empty() {
681×
            s.push(format!("window {} UTC", self.window));
6920×
        }
7021×
        if self.dry_run != "inherit" {
7110×
            s.push(format!("dry run {}", self.dry_run));
7211×
        }
7321×
        if s.is_empty() {
745×
            "inherits everything".into()
75
        } else {
7616×
            s.join(", ")
77
        }
7821×
    }
79
}
80
81
pub struct GroupView {
82
    pub g: Group,
83
    /// Whom it can hold, in words.
84
    pub reach: String,
85
    /// Its members now, by name.
86
    pub members: Vec<String>,
87
    pub chosen: Vec<i64>,
88
    pub policy: PolicyView,
89
}
90
91
pub struct RunnerView {
92
    pub r: runners::Runner,
93
    pub groups: Vec<String>,
94
    pub effective: UpdatePolicy,
95
    /// What the hub would offer it now.
96
    pub offer: String,
97
    pub policy: PolicyView,
98
}
99
100
impl RunnerView {
101
    /// Its effective settings, each with where it came from.
10246×
    pub fn effective_words(&self) -> String {
10346×
        let e = &self.effective;
10446×
        let mut s = vec![format!("updates {} ({})", if e.updates { "on" } else { "off" }, e.from[0])];
10546×
        if e.updates {
10632×
            s.push(format!("channel {} ({})", e.channel, e.from[1]));
10732×
            if let Some((a, b)) = e.window {
1087×
                s.push(format!("window {a:02}–{b:02} UTC ({})", e.from[2]));
10925×
            }
11032×
            if e.dry_run {
11116×
                s.push(format!("dry run ({})", e.from[3]));
11216×
            }
11314×
        }
11446×
        s.join(" · ")
11546×
    }
116
}
117
118
#[derive(Template)]
119
#[template(path = "admin_runner_updates.html")]
120
pub struct UpdatesPage {
121
    shell: Shell,
122
    repo: String,
123
    site: PolicyView,
124
    owners: Vec<PolicyView>,
125
    groups: Vec<GroupView>,
126
    runners: Vec<RunnerView>,
127
    notice: Option<String>,
128
    error: Option<String>,
129
}
130
131
/// Who changes runner updates: the site's administrators anything; an
132
/// organisation's owners their organisation's setting, its test groups and
133
/// its runners (Andy: an organisation tries a build on some of its runners
134
/// before the rest).
135
#[derive(Clone, Debug, PartialEq, Eq)]
136
pub enum Reach {
137
    Site,
138
    Org(String),
139
}
140
141
impl Reach {
14262×
    fn holds_runner(&self, r: &runners::Runner) -> bool {
14362×
        match self {
14427×
            Reach::Site => true,
14535×
            Reach::Org(o) => r.scope_owner == *o,
146
        }
14762×
    }
148
14923×
    fn holds_group(&self, g: &Group) -> bool {
15023×
        match self {
15114×
            Reach::Site => true,
1529×
            Reach::Org(o) => g.owner == *o,
153
        }
15423×
    }
155
}
156
157
/// What a page shows of runner updates, within `reach`.
158
pub struct UpdateViews {
159
    pub site: PolicyView,
160
    pub owners: Vec<PolicyView>,
161
    pub groups: Vec<GroupView>,
162
    pub runners: Vec<RunnerView>,
163
}
164
16545×
pub async fn views(app: &Shared, reach: &Reach) -> sqlx::Result<UpdateViews> {
16645×
    let levels = Levels::load(&app.db).await?;
16745×
    let all = runners::list(&app.db).await?;
168137×
    let policy = |scope: &str, key: &str| {
169137×
        levels.policies.get(&(scope.to_string(), key.to_string())).cloned().unwrap_or(Policy {
170137×
            scope: scope.into(),
171137×
            key: key.into(),
172137×
            ..Policy::default()
173137×
        })
174137×
    };
17545×
    let mut owners: Vec<PolicyView> = match reach {
176
        Reach::Site => {
17726×
            levels.policies.values().filter(|p| p.scope == "owner").map(|p| PolicyView::of(p, format!("owner {}", p.key))).collect()
178
        }
17931×
        Reach::Org(o) => vec![PolicyView::of(&policy("owner", o), format!("owner {o}"))],
180
    };
18145×
    owners.sort_by(|a, b| a.key.cmp(&b.key));
18245×
    let groups = levels
18345×
        .groups
18445×
        .iter()
18545×
        .filter(|g| reach.holds_group(g))
18645×
        .map(|g| GroupView {
18715×
            g: g.clone(),
18815×
            reach: if g.owner.is_empty() { "the site's runners".into() } else { format!("{}'s runners", g.owner) },
18935×
            members: all.iter().filter(|r| levels.member(g, r)).map(|r| r.name.clone()).collect(),
19015×
            chosen: levels.chosen.iter().filter(|(gid, _)| *gid == g.id).map(|(_, r)| *r).collect(),
19115×
            policy: PolicyView::of(&policy("group", &g.id.to_string()), format!("group {}", g.name)),
19215×
        })
19345×
        .collect();
19445×
    let hour = time::OffsetDateTime::now_utc().hour();
19545×
    let mut out = vec![];
19659×
    for r in all.into_iter().filter(|r| reach.holds_runner(r)) {
19746×
        let effective = levels.resolve(&r);
19846×
        let (_, offer) = ru::offer(&app.db, &r, &effective, hour).await?;
19946×
        out.push(RunnerView {
20046×
            groups: levels.groups_of(&r).iter().map(|g| g.name.clone()).collect(),
20146×
            policy: PolicyView::of(&policy("runner", &r.id.to_string()), format!("runner {}", r.name)),
20246×
            effective,
20346×
            offer,
20446×
            r,
205
        });
206
    }
20745×
    Ok(UpdateViews { site: PolicyView::of(&policy("site", ""), "the site".into()), owners, groups, runners: out })
20845×
}
209
21014×
async fn page(app: &Shared, shell: Shell, notice: Option<String>, error: Option<String>) -> Result<Page<UpdatesPage>, Error> {
21114×
    let e = |e| Error::from_db(shell.clone(), e);
21214×
    let repo = accounts::setting(&app.db, ru::SETTING_REPO).await.map_err(e)?.unwrap_or_default();
21314×
    let v = views(app, &Reach::Site).await.map_err(e)?;
21414×
    Ok(Page(UpdatesPage { shell, repo, site: v.site, owners: v.owners, groups: v.groups, runners: v.runners, notice, error }))
21514×
}
216
217
/// What a change did, to say on the page: `Ok` a notice, `Err` what was wrong.
218
pub type Outcome = Result<String, String>;
219
220
/// [`page`] saying what `outcome` did.
22110×
async fn page_after(app: &Shared, shell: Shell, outcome: Outcome) -> Result<Page<UpdatesPage>, Error> {
22210×
    match outcome {
2237×
        Ok(n) => page(app, shell, Some(n), None).await,
2243×
        Err(why) => page(app, shell, None, Some(why)).await,
225
    }
22610×
}
227
2283×
pub async fn show(State(app): State<Shared>, viewer: Viewer) -> Result<Page<UpdatesPage>, Error> {
2293×
    let shell = Shell::new(&app, "/admin/runners/updates", &viewer);
2303×
    require_admin(&viewer, &shell)?;
2312×
    page(&app, shell, None, None).await
2323×
}
233
234
#[derive(Deserialize)]
235
pub struct SourceForm {
236
    #[serde(rename = "_csrf")]
237
    csrf_token: String,
238
    repo: String,
239
}
240
241
/// `POST /admin/runners/updates/source`: the repository whose releases carry runner builds.
2422×
pub async fn save_source(State(app): State<Shared>, viewer: Viewer, Form(f): Form<SourceForm>) -> Result<Page<UpdatesPage>, Error> {
2432×
    let shell = Shell::new(&app, "/admin/runners/updates", &viewer);
2442×
    let me = require_admin(&viewer, &shell)?;
2452×
    csrf(&viewer, &f.csrf_token, &shell)?;
2462×
    let repo = f.repo.trim().trim_matches('/').to_string();
2472×
    if !repo.is_empty() {
2482×
        let found = match repo.split_once('/') {
2492×
            Some((o, n)) => crate::db::repos::get(&app.db, o, n).await.map_err(|e| Error::from_db(shell.clone(), e))?,
2500×
            None => None,
251
        };
2522×
        if found.is_none() {
2531×
            return page(&app, shell, None, Some(format!("There is no repository {repo:?} here; give it as owner/name."))).await;
2541×
        }
2550×
    }
2561×
    accounts::set_setting(&app.db, ru::SETTING_REPO, &repo).await.map_err(|e| Error::from_db(shell.clone(), e))?;
2571×
    accounts::audit(&app.db, Some(&me), "runner.updates.source", &repo, "runner builds come from its releases").await.ok();
2581×
    let n = if repo.is_empty() {
2590×
        "Saved: no repository, so no runner is offered a build.".into()
260
    } else {
2611×
        format!("Saved: runner builds come from {repo}'s releases.")
262
    };
2631×
    page(&app, shell, Some(n), None).await
2642×
}
265
266
#[derive(Deserialize)]
267
pub struct PolicyForm {
268
    #[serde(rename = "_csrf")]
269
    pub(crate) csrf_token: String,
270
    scope: String,
271
    #[serde(default)]
272
    key: String,
273
    #[serde(default)]
274
    updates: String,
275
    #[serde(default)]
276
    channel: String,
277
    #[serde(default)]
278
    window: String,
279
    #[serde(default)]
280
    dry_run: String,
281
}
282
283
/// Set one level's settings, within `reach`.
28412×
pub async fn apply_policy(app: &Shared, me: &accounts::Account, reach: &Reach, f: &PolicyForm) -> sqlx::Result<Outcome> {
28512×
    let key = f.key.trim().to_string();
28612×
    let label = match (f.scope.as_str(), reach) {
28712×
        ("site", Reach::Site) if key.is_empty() => "the site".to_string(),
2888×
        ("owner", Reach::Org(o)) if key != *o => return Ok(Err("An organisation sets its own runners' updates only.".into())),
289
        ("owner", _) => {
2901×
            let known = accounts::account_by_name(&app.db, &key).await?.is_some() || accounts::org_by_name(&app.db, &key).await?.is_some();
2911×
            if !known {
2920×
                return Ok(Err(format!("There is no account or organisation {key:?}.")));
2931×
            }
2941×
            format!("owner {key}")
295
        }
2966×
        ("group", _) => match key.parse().ok() {
2973×
            Some(id) => match rdb::group(&app.db, id).await?.filter(|g| reach.holds_group(g)) {
2983×
                Some(g) => format!("group {}", g.name),
2990×
                None => return Ok(Err("That test group is gone.".into())),
300
            },
3010×
            None => return Ok(Err("That test group is gone.".into())),
302
        },
3033×
        ("runner", _) => match key.parse().ok() {
3043×
            Some(id) => match runners::get(&app.db, id).await?.filter(|r| reach.holds_runner(r)) {
3052×
                Some(r) => format!("runner {}", r.name),
3061×
                None => return Ok(Err("That runner is gone.".into())),
307
            },
3080×
            None => return Ok(Err("That runner is gone.".into())),
309
        },
3101×
        _ => return Ok(Err("Choose the site, an owner, a test group or a runner.".into())),
311
    };
31214×
    let tri = |v: &str| match v {
31314×
        "on" => Some(true),
3149×
        "off" => Some(false),
3157×
        _ => None,
31614×
    };
3179×
    let channel = f.channel.trim().to_string();
3189×
    if !channel.is_empty() && !ru::valid_channel(&channel) {
3191×
        return Ok(Err(format!("A channel is stable, canary or a version such as 0.3.4, not {channel:?}.")));
3208×
    }
3218×
    let window = match ru::parse_window(&f.window) {
3227×
        Ok(w) => w,
3231×
        Err(why) => return Ok(Err(format!("{why}."))),
324
    };
3257×
    let p = Policy {
3267×
        scope: f.scope.clone(),
3277×
        key,
3287×
        updates: tri(&f.updates),
3297×
        channel: Some(channel).filter(|c| !c.is_empty()),
3307×
        window_from: window.map(|w| w.0 as i64),
3317×
        window_to: window.map(|w| w.1 as i64),
3327×
        dry_run: tri(&f.dry_run),
333
    };
3347×
    rdb::set_policy(&app.db, &p).await?;
3357×
    let words = PolicyView::of(&p, label.clone()).summary();
3367×
    accounts::audit(&app.db, Some(me), "runner.updates.policy", &label, &words).await.ok();
3377×
    Ok(Ok(format!("Saved for {label}: {words}.")))
33812×
}
339
340
/// `POST /admin/runners/updates/policy`: one level's settings.
3416×
pub async fn save_policy(State(app): State<Shared>, viewer: Viewer, Form(f): Form<PolicyForm>) -> Result<Page<UpdatesPage>, Error> {
3426×
    let shell = Shell::new(&app, "/admin/runners/updates", &viewer);
3436×
    let me = require_admin(&viewer, &shell)?;
3446×
    csrf(&viewer, &f.csrf_token, &shell)?;
3456×
    let outcome = apply_policy(&app, &me, &Reach::Site, &f).await.map_err(|e| Error::from_db(shell.clone(), e))?;
3466×
    page_after(&app, shell, outcome).await
3476×
}
348
349
#[derive(Deserialize)]
350
pub struct GroupForm {
351
    #[serde(rename = "_csrf")]
352
    pub(crate) csrf_token: String,
353
    #[serde(default)]
354
    owner: String,
355
    #[serde(default)]
356
    name: String,
357
    /// `percent` or `chosen`.
358
    #[serde(default)]
359
    mode: String,
360
    #[serde(default)]
361
    percent: String,
362
    #[serde(default)]
363
    runner: Vec<i64>,
364
}
365
366
/// The percentage a form gives, or why it cannot be one.
3674×
fn percent_of(f: &GroupForm) -> Result<Option<i64>, String> {
3684×
    if f.mode != "percent" {
3692×
        return Ok(None);
3702×
    }
3712×
    match f.percent.trim().trim_end_matches('%').parse::<i64>() {
3722×
        Ok(p) if (1..=100).contains(&p) => Ok(Some(p)),
3731×
        _ => Err("A percentage is a whole number from 1 to 100.".into()),
374
    }
3754×
}
376
377
/// The chosen runners a form names that `owner`'s group may hold.
3782×
async fn chosen_in_reach(app: &Shared, owner: &str, f: &GroupForm) -> sqlx::Result<Vec<i64>> {
3792×
    let all = runners::list(&app.db).await?;
3806×
    Ok(f.runner.iter().copied().filter(|id| all.iter().any(|r| r.id == *id && (owner.is_empty() || r.scope_owner == owner))).collect())
3812×
}
382
383
/// Make a test group, within `reach` (an organisation's are its own).
3843×
pub async fn apply_create_group(app: &Shared, me: &accounts::Account, reach: &Reach, f: &GroupForm) -> sqlx::Result<Outcome> {
3853×
    let name = f.name.trim().to_string();
3863×
    if name.is_empty() || name.len() > 60 {
3870×
        return Ok(Err("A test group needs a name of up to 60 characters.".into()));
3883×
    }
3893×
    let owner = match reach {
3902×
        Reach::Site => f.owner.trim().to_string(),
3911×
        Reach::Org(o) => o.clone(),
392
    };
3933×
    if !owner.is_empty()
3941×
        && accounts::account_by_name(&app.db, &owner).await?.is_none()
3951×
        && accounts::org_by_name(&app.db, &owner).await?.is_none()
396
    {
3970×
        return Ok(Err(format!("There is no account or organisation {owner:?}.")));
3983×
    }
3993×
    let percent = match percent_of(f) {
4002×
        Ok(p) => p,
4011×
        Err(why) => return Ok(Err(why)),
402
    };
4032×
    if rdb::groups(&app.db).await?.iter().any(|g| g.owner == owner && g.name == name) {
4040×
        return Ok(Err(format!("There is already a test group {name:?} for those runners.")));
4052×
    }
4062×
    let chosen = chosen_in_reach(app, &owner, f).await?;
4072×
    let g = rdb::create_group(&app.db, &owner, &name, percent).await?;
4082×
    if percent.is_none() {
4092×
        rdb::set_members(&app.db, g.id, &chosen).await?;
4100×
    }
4112×
    let what = match percent {
4120×
        Some(p) => format!("{p}% of {}", if owner.is_empty() { "the site's runners".into() } else { format!("{owner}'s runners") }),
4132×
        None => format!("{} chosen runners", chosen.len()),
414
    };
4152×
    accounts::audit(&app.db, Some(me), "runner.updates.group", &name, &format!("created: {what}")).await.ok();
4162×
    Ok(Ok(format!("Test group {name} made: {what}. Give it its settings below.")))
4173×
}
418
419
/// `POST /admin/runners/updates/groups`: a new test group.
4202×
pub async fn create_group(
4212×
    State(app): State<Shared>,
4222×
    viewer: Viewer,
4232×
    axum_extra::extract::Form(f): axum_extra::extract::Form<GroupForm>,
4242×
) -> Result<Page<UpdatesPage>, Error> {
4252×
    let shell = Shell::new(&app, "/admin/runners/updates", &viewer);
4262×
    let me = require_admin(&viewer, &shell)?;
4272×
    csrf(&viewer, &f.csrf_token, &shell)?;
4282×
    let outcome = apply_create_group(&app, &me, &Reach::Site, &f).await.map_err(|e| Error::from_db(shell.clone(), e))?;
4292×
    page_after(&app, shell, outcome).await
4302×
}
431
432
/// Set who is in test group `id`, within `reach`.
4331×
pub async fn apply_save_group(app: &Shared, me: &accounts::Account, reach: &Reach, id: i64, f: &GroupForm) -> sqlx::Result<Outcome> {
4341×
    let Some(g) = rdb::group(&app.db, id).await?.filter(|g| reach.holds_group(g)) else {
4350×
        return Ok(Err("That test group is gone.".into()));
436
    };
4371×
    let percent = match percent_of(f) {
4381×
        Ok(p) => p,
4390×
        Err(why) => return Ok(Err(why)),
440
    };
4411×
    let chosen = if percent.is_some() { vec![] } else { chosen_in_reach(app, &g.owner, f).await? };
4421×
    rdb::set_percent(&app.db, id, percent).await?;
4431×
    rdb::set_members(&app.db, id, &chosen).await?;
4441×
    let what = match percent {
4451×
        Some(p) => format!("{p}% of its runners"),
4460×
        None => format!("{} chosen runners", chosen.len()),
447
    };
4481×
    accounts::audit(&app.db, Some(me), "runner.updates.group", &g.name, &what).await.ok();
4491×
    Ok(Ok(format!("Test group {} is now {what}.", g.name)))
4501×
}
451
452
/// `POST /admin/runners/updates/groups/{id}`: who is in a test group.
4531×
pub async fn save_group(
4541×
    State(app): State<Shared>,
4551×
    viewer: Viewer,
4561×
    Path(id): Path<i64>,
4571×
    axum_extra::extract::Form(f): axum_extra::extract::Form<GroupForm>,
4581×
) -> Result<Page<UpdatesPage>, Error> {
4591×
    let shell = Shell::new(&app, "/admin/runners/updates", &viewer);
4601×
    let me = require_admin(&viewer, &shell)?;
4611×
    csrf(&viewer, &f.csrf_token, &shell)?;
4621×
    let outcome = apply_save_group(&app, &me, &Reach::Site, id, &f).await.map_err(|e| Error::from_db(shell.clone(), e))?;
4631×
    page_after(&app, shell, outcome).await
4641×
}
465
466
#[derive(Deserialize)]
467
pub struct CsrfOnly {
468
    #[serde(rename = "_csrf")]
469
    pub(crate) csrf_token: String,
470
}
471
472
/// Remove test group `id`, within `reach`.
4732×
pub async fn apply_delete_group(app: &Shared, me: &accounts::Account, reach: &Reach, id: i64) -> sqlx::Result<Outcome> {
4742×
    let Some(g) = rdb::group(&app.db, id).await?.filter(|g| reach.holds_group(g)) else {
4751×
        return Ok(Err("That test group is gone.".into()));
476
    };
4771×
    rdb::delete_group(&app.db, id).await?;
4781×
    accounts::audit(&app.db, Some(me), "runner.updates.group", &g.name, "removed").await.ok();
4791×
    Ok(Ok(format!("Test group {} removed; its runners follow the levels above it.", g.name)))
4802×
}
481
482
/// `POST /admin/runners/updates/groups/{id}/delete`.
4831×
pub async fn delete_group(
4841×
    State(app): State<Shared>,
4851×
    viewer: Viewer,
4861×
    Path(id): Path<i64>,
4871×
    Form(f): Form<CsrfOnly>,
4881×
) -> Result<Page<UpdatesPage>, Error> {
4891×
    let shell = Shell::new(&app, "/admin/runners/updates", &viewer);
4901×
    let me = require_admin(&viewer, &shell)?;
4911×
    csrf(&viewer, &f.csrf_token, &shell)?;
4921×
    let outcome = apply_delete_group(&app, &me, &Reach::Site, id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
4931×
    page_after(&app, shell, outcome).await
4941×
}