Skip to main content

runnerd_core/
config.rs

1//! `runnerd.toml`: the repositories to serve, the runner profiles they use, the shared caches and the host
2//! budget they must fit in. Builds without the `host` feature, so clients can read a config; its only I/O is
3//! reading the file and asking `systemctl` for a slice's `MemoryMax`. It never talks to Docker or GitHub.
4
5use std::collections::BTreeMap;
6use std::fmt;
7use std::net::{Ipv4Addr, Ipv6Addr};
8use std::path::{Path, PathBuf};
9use std::process::Command;
10use std::str::FromStr;
11use std::time::Duration;
12
13use serde::{Deserialize, Deserializer, de};
14
15use crate::api;
16
17/// A parsed `runnerd.toml`. `validate` checks it against the host's budget.
18#[derive(Debug, Deserialize)]
19#[serde(deny_unknown_fields)]
20pub struct Config {
21    /// The host's budget and isolation.
22    pub host: Host,
23    /// The caches runners share.
24    pub caches: Caches,
25    /// Runner profiles by name.
26    pub profiles: BTreeMap<String, Profile>,
27    /// The repositories to serve.
28    pub repositories: Vec<Repository>,
29    /// What is kept of past jobs.
30    #[serde(default)]
31    pub history: History,
32    /// How much the records of failed jobs may keep.
33    #[serde(default)]
34    pub records: Records,
35    /// How sizing advice reads the job history.
36    #[serde(default)]
37    pub sizing: Sizing,
38    /// Who may use the dashboard through another address.
39    #[serde(default)]
40    pub web: Web,
41    /// Where the daemon sends notifications; none by default.
42    #[serde(default)]
43    pub notify: Notify,
44}
45
46/// `[host]`: where runner containers run and how they are isolated.
47#[derive(Debug, Deserialize)]
48#[serde(deny_unknown_fields)]
49pub struct Host {
50    /// The systemd slice every runner container is placed in; its `MemoryMax` is the budget.
51    pub slice: String,
52    /// How runner containers are kept from the host.
53    pub isolation: Isolation,
54    /// Must be set for `isolation = "unsafe-dind"`, so a copied config cannot turn it on silently.
55    #[serde(default)]
56    pub allow_unsafe: bool,
57    /// How often GitHub is asked for queued jobs.
58    #[serde(default = "default_poll_interval", deserialize_with = "duration")]
59    pub poll_interval: Duration,
60    /// What a runner must fit in before it starts. Default `caps`.
61    #[serde(default)]
62    pub admission: Admission,
63    /// Set, a runner also starts only when its predicted CPU fits the slice's CPUs times this; absent, CPU is not
64    /// admitted.
65    #[serde(default)]
66    pub cpu_overcommit: Option<f64>,
67}
68
69impl Host {
70    /// Refuses a `cpu_overcommit` that is not above 0 and at most `MAX_CPU_OVERCOMMIT`.
71    fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
72        match self.cpu_overcommit {
73            Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
74                Err(ConfigError::CpuOvercommit { value })
75            }
76            _ => Ok(()),
77        }
78    }
79}
80
81/// The largest `cpu_overcommit` the config check takes.
82pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
83
84/// `admission` under `[host]`: what a new runner must fit in the slice's `MemoryMax` beside the running ones.
85#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
86#[serde(rename_all = "kebab-case")]
87pub enum Admission {
88    /// Every runner's memory cap: `slots × memory` over all profiles, with the mirror, must fit `MemoryMax`.
89    #[default]
90    Caps,
91    /// Every runner's predicted peak, from the job history; `slots` only bounds how many run at once, and one
92    /// runner of each profile with the mirror must fit `MemoryMax`. Nothing starts while the slice is at 90% of it.
93    Measured,
94}
95
96fn default_poll_interval() -> Duration {
97    Duration::from_secs(15)
98}
99
100/// How runner containers are kept from the host: `isolation` under `[host]`.
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
102#[serde(rename_all = "kebab-case")]
103pub enum Isolation {
104    /// Sysbox: Docker inside an unprivileged container.
105    Sysbox,
106    /// Privileged Docker-in-Docker: a job can take over the host.
107    UnsafeDind,
108}
109
110/// `[caches]`: what runners share and keep between jobs.
111#[derive(Debug, Clone, Deserialize)]
112#[serde(deny_unknown_fields)]
113pub struct Caches {
114    /// The Docker Hub mirror.
115    pub mirror: Mirror,
116    /// The shared pnpm store.
117    pub pnpm: CacheLimit,
118    /// The shared Maven repository.
119    pub maven: CacheLimit,
120    /// When a profile has no job running and its slot 1 changed its layers by at least this much, slot 1's
121    /// layers become the profile's new snapshot.
122    #[serde(default = "default_reseed_at")]
123    pub reseed_at: ByteSize,
124    /// A reseed that would make a profile's snapshot larger than this starts it over from slot 1's changes alone,
125    /// or from the image when those are larger too.
126    #[serde(default = "default_snapshot_max")]
127    pub snapshot_max: ByteSize,
128    /// The build cache every runner shares.
129    #[serde(default)]
130    pub build: BuildCache,
131}
132
133/// `[caches.build]`: the build cache every runner shares (ADR 0052): Gradle's, and the other tools' the runner image
134/// points at it. Every field has a default.
135#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
136#[serde(deny_unknown_fields, default)]
137pub struct BuildCache {
138    /// Whether runners get it. Default true.
139    pub enabled: bool,
140    /// Above this size, the entries used least recently go when no job runs. Default 10G.
141    pub max_size: ByteSize,
142    /// The branches whose pushes, scheduled and manually started runs may write to it besides each repository's
143    /// default branch, which always may. Every other job only reads. Default empty.
144    pub branches: Vec<String>,
145    /// Where its entries are kept; default `$XDG_CACHE_HOME/runnerd/build-cache`.
146    pub directory: Option<PathBuf>,
147}
148
149impl BuildCache {
150    fn validate(&self) -> Result<(), ConfigError> {
151        if self.enabled && self.max_size.0 == 0 {
152            return Err(ConfigError::ZeroLimit {
153                field: "caches.build.max_size".to_owned(),
154            });
155        }
156        Ok(())
157    }
158}
159
160impl Default for BuildCache {
161    fn default() -> BuildCache {
162        BuildCache {
163            enabled: true,
164            max_size: ByteSize(10 << 30),
165            branches: Vec::new(),
166            directory: None,
167        }
168    }
169}
170
171fn default_reseed_at() -> ByteSize {
172    ByteSize(2 << 30)
173}
174
175/// Above the 8.7G a profile running Flutter and Android builds needs: its Flutter SDK, NDK and Gradle caches.
176fn default_snapshot_max() -> ByteSize {
177    ByteSize(10 << 30)
178}
179
180fn default_mirror_max_size() -> ByteSize {
181    ByteSize(10 << 30)
182}
183
184/// The Docker Hub pull-through mirror shared by all runners.
185#[derive(Debug, Clone, Deserialize)]
186#[serde(deny_unknown_fields)]
187pub struct Mirror {
188    /// Whether runners pull through the mirror.
189    pub enabled: bool,
190    /// The mirror container's memory limit.
191    pub memory: ByteSize,
192    /// The mirror container's CPUs.
193    pub cpus: u32,
194    /// How long content not pulled again is kept.
195    #[serde(deserialize_with = "duration")]
196    pub ttl: Duration,
197    /// Its storage is emptied above this size when no job runs.
198    #[serde(default = "default_mirror_max_size")]
199    pub max_size: ByteSize,
200}
201
202/// A size limit on a shared cache.
203#[derive(Debug, Clone, Deserialize)]
204#[serde(deny_unknown_fields)]
205pub struct CacheLimit {
206    /// The cache is emptied above this size when no job runs.
207    pub max_size: ByteSize,
208}
209
210/// Where runnerd's release workflow publishes the runner images, one tag per stage and version, such as
211/// `ghcr.io/ayagmar/runnerd-runner:jvm-0.0.11`.
212pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
213
214/// A runner image with its limits; every job of the profile gets one container with these limits.
215#[derive(Debug, Clone, Deserialize)]
216#[serde(try_from = "ProfileToml")]
217pub struct Profile {
218    /// The image its runners run: `image_stage` or `image`, exactly one of them.
219    pub image: Image,
220    /// CPUs per slot.
221    pub cpus: u32,
222    /// Memory per slot; the container's hard limit.
223    pub memory: ByteSize,
224    /// The most jobs of this profile running at once.
225    pub slots: u32,
226    /// Whether a failed job's runner is kept as a record, to open a shell in (`runnerctl shell`).
227    pub record_failures: bool,
228    /// The least `memory` auto sizing lowers it to; `None` for no bound.
229    pub min_memory: Option<ByteSize>,
230    /// The most `memory` auto sizing raises it to; `None` for what the slice's `MemoryMax` holds.
231    pub max_memory: Option<ByteSize>,
232    /// The most `slots` auto sizing raises them to; `None` for as many as the slice's `MemoryMax` holds.
233    pub max_slots: Option<u32>,
234}
235
236/// The image a profile's runners run.
237#[derive(Debug, Clone, PartialEq, Eq)]
238pub enum Image {
239    /// `image_stage`: a stage of `image/Dockerfile` built on this host as `runnerd-runner:<stage>`.
240    Stage(String),
241    /// `image`: a reference runnerd pulls when Docker lacks it, such as
242    /// `ghcr.io/ayagmar/runnerd-runner:jvm-0.0.11`.
243    Pulled(String),
244}
245
246impl Image {
247    /// The reference Docker runs.
248    pub fn reference(&self) -> String {
249        match self {
250            Image::Stage(stage) => format!("runnerd-runner:{stage}"),
251            Image::Pulled(reference) => reference.clone(),
252        }
253    }
254}
255
256/// `[profiles.<name>]` as written, before `Profile` checks it names one image.
257#[derive(Deserialize)]
258#[serde(deny_unknown_fields)]
259struct ProfileToml {
260    image_stage: Option<String>,
261    image: Option<String>,
262    cpus: u32,
263    memory: ByteSize,
264    slots: u32,
265    #[serde(default)]
266    record_failures: bool,
267    min_memory: Option<ByteSize>,
268    max_memory: Option<ByteSize>,
269    max_slots: Option<u32>,
270}
271
272impl TryFrom<ProfileToml> for Profile {
273    type Error = String;
274
275    fn try_from(profile: ProfileToml) -> Result<Profile, String> {
276        let image = match (profile.image_stage, profile.image) {
277            (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
278            (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
279            (Some(_), Some(_)) => {
280                return Err(
281                    "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
282                     not both"
283                        .to_owned(),
284                );
285            }
286            _ => {
287                return Err(
288                    "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
289                        .to_owned(),
290                );
291            }
292        };
293        Ok(Profile {
294            image,
295            cpus: profile.cpus,
296            memory: profile.memory,
297            slots: profile.slots,
298            record_failures: profile.record_failures,
299            min_memory: profile.min_memory,
300            max_memory: profile.max_memory,
301            max_slots: profile.max_slots,
302        })
303    }
304}
305
306/// A repository to serve: `[[repositories]]`.
307#[derive(Debug, Deserialize)]
308#[serde(deny_unknown_fields)]
309pub struct Repository {
310    /// `owner/name` on GitHub.
311    pub name: String,
312    /// The profile its jobs run in.
313    pub profile: String,
314    /// The labels its runners register with; a job runs on them when every label it asks for is one of these.
315    pub labels: Vec<String>,
316    /// How many idle runners it keeps registered for jobs not queued yet while it is active (ADR 0050); 0 turns warm
317    /// runners off. Default 1.
318    #[serde(default = "default_warm")]
319    pub warm: u32,
320    /// Whether its runners get the shared build cache, when `[caches.build]` is enabled. Default true.
321    #[serde(default = "default_true")]
322    pub build_cache: bool,
323}
324
325fn default_true() -> bool {
326    true
327}
328
329fn default_warm() -> u32 {
330    1
331}
332
333/// `[history]`: the job history database and how long it keeps what. Every field has a default.
334#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
335#[serde(deny_unknown_fields, default)]
336pub struct History {
337    /// The SQLite database; default `$XDG_STATE_HOME/runnerd/runnerd.db`.
338    pub database: Option<PathBuf>,
339    /// How long CPU and memory samples are kept; a job's totals and peaks stay. Default 7 days.
340    #[serde(deserialize_with = "duration")]
341    pub samples: Duration,
342    /// How long a finished job's log is kept. Default 30 days.
343    #[serde(deserialize_with = "duration")]
344    pub logs: Duration,
345    /// How long events are kept for clients resuming with `Last-Event-ID`. Default 30 days.
346    #[serde(deserialize_with = "duration")]
347    pub events: Duration,
348    /// How long a finished job's record is kept, with its steps, totals and runner. Default 90 days.
349    #[serde(deserialize_with = "duration")]
350    pub jobs: Duration,
351    /// The most log stored per job; later lines are dropped. Default 50M.
352    pub log_max: ByteSize,
353}
354
355impl Default for History {
356    fn default() -> History {
357        History {
358            database: None,
359            samples: Duration::from_hours(7 * 24),
360            logs: Duration::from_hours(30 * 24),
361            events: Duration::from_hours(30 * 24),
362            jobs: Duration::from_hours(90 * 24),
363            log_max: ByteSize(50 << 20),
364        }
365    }
366}
367
368/// `[records]`: how much the records of failed jobs may keep (`record_failures` under a profile turns them on).
369/// When a new record would pass a limit, the oldest records go first. Every field has a default.
370#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
371#[serde(deny_unknown_fields, default)]
372pub struct Records {
373    /// The most disk all records may take together. Default 20G.
374    pub max_total: ByteSize,
375    /// The most records kept. Default 10.
376    pub max_count: u32,
377    /// How long a record is kept. Default 7 days.
378    #[serde(deserialize_with = "duration")]
379    pub max_age: Duration,
380    /// The disk Docker must keep free: no record is made that would leave less. Default 10G.
381    pub min_free: ByteSize,
382}
383
384impl Default for Records {
385    fn default() -> Records {
386        Records {
387            max_total: ByteSize(20 << 30),
388            max_count: 10,
389            max_age: Duration::from_hours(7 * 24),
390            min_free: ByteSize(10 << 30),
391        }
392    }
393}
394
395/// `[sizing]`: how `runnerctl sizing` turns the job history's peaks into advice, and whether the daemon applies it
396/// itself. Every field has a default.
397#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
398#[serde(deny_unknown_fields, default)]
399pub struct Sizing {
400    /// The percent added to the largest sampled memory peak: a container is sampled every 2 s, so a spike between
401    /// two samples goes unseen. Default 15.
402    pub margin: u32,
403    /// The fewest jobs a profile, or a job of a repository's workflow, needs before its peaks become advice.
404    /// Default 10.
405    pub min_jobs: u32,
406    /// Whether the daemon writes each profile's advice into the config itself, within each profile's
407    /// `min_memory`, `max_memory` and `max_slots`. Default off.
408    pub auto: bool,
409    /// The least time between a change auto sizing makes to one profile and one that lowers anything in it. Default
410    /// 24 hours.
411    #[serde(deserialize_with = "duration")]
412    pub interval: Duration,
413    /// The least time between a change auto sizing makes to one profile and one that only raises its `memory`,
414    /// `slots` or `cpus`; at most `interval`, which a longer one counts as. Default 1 hour.
415    #[serde(deserialize_with = "duration")]
416    pub raise_interval: Duration,
417    /// The most a change moves a profile's `memory` or `cpus`, in percent of them. Default 25.
418    pub max_step: u32,
419}
420
421impl Sizing {
422    /// The least time between a change to one profile and one that only raises it: `raise_interval`, at most
423    /// `interval`.
424    pub fn raise_after(&self) -> Duration {
425        self.raise_interval.min(self.interval)
426    }
427}
428
429impl Default for Sizing {
430    fn default() -> Sizing {
431        Sizing {
432            margin: 15,
433            min_jobs: 10,
434            auto: false,
435            interval: Duration::from_hours(24),
436            raise_interval: Duration::from_hours(1),
437            max_step: 25,
438        }
439    }
440}
441
442/// `[web]`: the dashboard behind a reverse proxy, such as `tailscale serve`. Read when the daemon starts.
443#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
444#[serde(deny_unknown_fields, default)]
445pub struct Web {
446    /// The origins, besides the dashboard's own loopback address, whose pages may write: pause, reseed, save the
447    /// config. Default none.
448    pub origins: Vec<Origin>,
449    /// The one of `origins` that `runnerctl web` opens the dashboard on, such as a tailnet's address. Default none:
450    /// it opens the loopback address.
451    pub open: Option<Origin>,
452}
453
454impl Web {
455    /// Refuses an `open` that is not one of `origins`.
456    fn validate(&self) -> Result<(), ConfigError> {
457        match &self.open {
458            Some(open) if !self.origins.contains(open) => {
459                Err(ConfigError::OpenNotListed { open: open.clone() })
460            }
461            _ => Ok(()),
462        }
463    }
464}
465
466/// `[notify]`: where the daemon sends notifications (ADR 0051). Without targets nothing leaves the machine.
467#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
468#[serde(deny_unknown_fields, default)]
469pub struct Notify {
470    /// When the digest goes to the targets that want it, in the host's local time. Default `09:00`.
471    pub digest_at: DayTime,
472    /// The targets, each `[[notify.targets]]`.
473    pub targets: Vec<NotifyTarget>,
474}
475
476impl Default for Notify {
477    fn default() -> Notify {
478        Notify {
479            digest_at: DayTime { minutes: 9 * 60 },
480            targets: Vec::new(),
481        }
482    }
483}
484
485impl Notify {
486    /// Refuses a target without a name, with a name another has, with a URL that is not `http` or `https`, or with
487    /// a header that cannot be sent. Says which target and which field, never the URL or a header's value.
488    fn validate(&self) -> Result<(), ConfigError> {
489        let refuse = |target: &str, why: &'static str| {
490            Err(ConfigError::Notify {
491                target: target.to_owned(),
492                why,
493            })
494        };
495        for (index, target) in self.targets.iter().enumerate() {
496            let name = target.name.as_str();
497            if name.trim().is_empty() || name.chars().any(char::is_control) {
498                return refuse(name, "name is empty");
499            }
500            if self.targets[..index].iter().any(|other| other.name == name) {
501                return refuse(name, "another target has this name");
502            }
503            let url = target.url.expose();
504            if url == SECRET_SHOWN {
505                return refuse(
506                    name,
507                    "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
508                );
509            }
510            let Some(rest) = url
511                .strip_prefix("https://")
512                .or_else(|| url.strip_prefix("http://"))
513            else {
514                return refuse(name, "url does not start with https:// or http://");
515            };
516            let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
517            if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
518                return refuse(name, "url is not a URL");
519            }
520            if target.kind == NotifyKind::Ntfy
521                && (path.is_empty() || path.contains(['/', '?', '#']))
522            {
523                return refuse(
524                    name,
525                    "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
526                );
527            }
528            for (header, value) in &target.headers {
529                if value.expose() == SECRET_SHOWN {
530                    return refuse(
531                        name,
532                        "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
533                    );
534                }
535                let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
536                if header.is_empty() || !header.chars().all(token) {
537                    return refuse(name, "a header's name is not a header name");
538                }
539                if value.expose().chars().any(char::is_control) {
540                    return refuse(name, "a header's value has a control character");
541                }
542            }
543            if target.events.contains(&api::NotifyEvent::Test) {
544                return refuse(
545                    name,
546                    "events lists test, which only runnerctl notify test sends",
547                );
548            }
549        }
550        Ok(())
551    }
552}
553
554/// `[[notify.targets]]`: one place notifications go.
555#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
556#[serde(deny_unknown_fields)]
557pub struct NotifyTarget {
558    /// What logs and `runnerctl notify test` call it, such as `phone`; the history keeps where it got to by it.
559    pub name: String,
560    /// What it takes.
561    pub kind: NotifyKind,
562    /// Where it is posted: the webhook's URL, or the ntfy topic's, such as `https://ntfy.sh/<topic>`. A secret.
563    pub url: Secret,
564    /// Headers sent with each post, such as an ntfy access token's `Authorization`. Their values are secrets.
565    #[serde(default)]
566    pub headers: BTreeMap<String, Secret>,
567    /// What it is sent. Default `failed`, `lost`, `runner` and `insights`.
568    #[serde(default = "default_notify_events")]
569    pub events: Vec<api::NotifyEvent>,
570}
571
572fn default_notify_events() -> Vec<api::NotifyEvent> {
573    vec![
574        api::NotifyEvent::Failed,
575        api::NotifyEvent::Lost,
576        api::NotifyEvent::Runner,
577        api::NotifyEvent::Insights,
578    ]
579}
580
581/// What a notification target takes.
582#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
583#[serde(rename_all = "kebab-case")]
584pub enum NotifyKind {
585    /// `api::Notification` as JSON.
586    Webhook,
587    /// An ntfy topic: the title, message, priority and link, published as ntfy's JSON.
588    Ntfy,
589}
590
591/// A value no `Debug` output, log line or error shows: a notification target's URL or header.
592#[derive(Clone, PartialEq, Eq, Deserialize)]
593#[serde(transparent)]
594pub struct Secret(String);
595
596impl Secret {
597    /// The value, for the request that sends it.
598    pub fn expose(&self) -> &str {
599        &self.0
600    }
601}
602
603impl From<&str> for Secret {
604    fn from(value: &str) -> Secret {
605        Secret(value.to_owned())
606    }
607}
608
609impl fmt::Debug for Secret {
610    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
611        f.write_str("<secret>")
612    }
613}
614
615/// A time of day, `HH:MM`, 24-hour.
616#[derive(Debug, Clone, Copy, PartialEq, Eq)]
617pub struct DayTime {
618    /// Minutes after midnight.
619    pub minutes: u32,
620}
621
622impl FromStr for DayTime {
623    type Err = String;
624
625    fn from_str(text: &str) -> Result<DayTime, String> {
626        let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
627        let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
628        let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
629        if !digits(hour) || !digits(minute) {
630            return Err(invalid());
631        }
632        let (hour, minute): (u32, u32) = (
633            hour.parse().map_err(|_| invalid())?,
634            minute.parse().map_err(|_| invalid())?,
635        );
636        if hour > 23 || minute > 59 {
637            return Err(invalid());
638        }
639        Ok(DayTime {
640            minutes: hour * 60 + minute,
641        })
642    }
643}
644
645impl fmt::Display for DayTime {
646    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
647        write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
648    }
649}
650
651impl<'de> Deserialize<'de> for DayTime {
652    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
653        String::deserialize(deserializer)?
654            .parse()
655            .map_err(de::Error::custom)
656    }
657}
658
659/// An origin as a browser sends it in `Origin`: `https://host[:port]`, or `http://` for a loopback host, in lower
660/// case, without the scheme's default port, and an address written as the URL standard serializes it. Nothing after
661/// the port: no path, query or user.
662#[derive(Debug, Clone, PartialEq, Eq)]
663pub struct Origin(String);
664
665impl Origin {
666    /// The origin as a browser sends it, such as `https://host.example.ts.net:8443`.
667    pub fn as_str(&self) -> &str {
668        &self.0
669    }
670}
671
672impl FromStr for Origin {
673    type Err = String;
674
675    fn from_str(s: &str) -> Result<Origin, String> {
676        let invalid = |why: &str| {
677            format!(
678                "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
679                 \"https://host.example.ts.net:8443\""
680            )
681        };
682        let (scheme, rest) = s
683            .split_once("://")
684            .ok_or_else(|| invalid("it has no scheme"))?;
685        let scheme = scheme.to_ascii_lowercase();
686        if rest.contains(['/', '?', '#']) {
687            return Err(invalid("it has a path"));
688        }
689        if rest.contains('@') {
690            return Err(invalid("it names a user"));
691        }
692        let (host, port) = match rest.strip_prefix('[') {
693            Some(v6) => {
694                let (address, after) = v6
695                    .split_once(']')
696                    .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
697                let port = match after {
698                    "" => None,
699                    _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
700                };
701                (format!("[{address}]"), port)
702            }
703            None => match rest.split_once(':') {
704                Some((host, port)) => (host.to_owned(), Some(port)),
705                None => (rest.to_owned(), None),
706            },
707        };
708        let host = host.to_ascii_lowercase();
709        let not_a_host = || invalid("its host is not a name or an address");
710        // An address is kept as a browser serializes it, or it would never match the `Origin` the browser sends.
711        let (host, loopback) =
712            if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
713                let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
714                (format!("[{}]", ipv6_origin(address)), address.is_loopback())
715            } else {
716                if host.is_empty()
717                    || !host
718                        .chars()
719                        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
720                {
721                    return Err(not_a_host());
722                }
723                // As the URL standard reads a host: one whose last label is a number is an IPv4 address.
724                let name = host.strip_suffix('.').unwrap_or(&host);
725                let last = name.rsplit('.').next().unwrap_or_default();
726                let numeric = !last.is_empty()
727                    && (last.bytes().all(|byte| byte.is_ascii_digit())
728                        || last
729                            .strip_prefix("0x")
730                            .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
731                if numeric {
732                    let address: Ipv4Addr = name.parse().map_err(|_| {
733                        invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
734                    })?;
735                    (address.to_string(), address.is_loopback())
736                } else {
737                    let loopback = host == "localhost";
738                    (host, loopback)
739                }
740            };
741        let port = port
742            .map(|port| {
743                Some(port)
744                    .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
745                    .and_then(|port| port.parse::<u16>().ok())
746                    .filter(|port| *port != 0)
747                    .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
748            })
749            .transpose()?;
750        let default_port = match scheme.as_str() {
751            "https" => 443,
752            "http" if loopback => 80,
753            "http" => {
754                return Err(invalid(
755                    "http is for a loopback host only, since the session cookie would cross the network in \
756                     the clear; use https",
757                ));
758            }
759            _ => return Err(invalid("its scheme is not https")),
760        };
761        Ok(Origin(match port {
762            Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
763            _ => format!("{scheme}://{host}"),
764        }))
765    }
766}
767
768/// An IPv6 address as the URL standard serializes it: lower-case hex pieces, the first longest run of two or more
769/// zero pieces as `::`, and no dotted IPv4 tail.
770fn ipv6_origin(address: Ipv6Addr) -> String {
771    let pieces = address.segments();
772    let (mut zeros_at, mut zeros) = (0, 0);
773    let mut at = 0;
774    while at < pieces.len() {
775        let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
776        if run > zeros {
777            (zeros_at, zeros) = (at, run);
778        }
779        at += run.max(1);
780    }
781    let hex = |pieces: &[u16]| {
782        pieces
783            .iter()
784            .map(|piece| format!("{piece:x}"))
785            .collect::<Vec<_>>()
786            .join(":")
787    };
788    if zeros < 2 {
789        return hex(&pieces);
790    }
791    format!(
792        "{}::{}",
793        hex(&pieces[..zeros_at]),
794        hex(&pieces[zeros_at + zeros..])
795    )
796}
797
798impl fmt::Display for Origin {
799    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
800        f.write_str(&self.0)
801    }
802}
803
804impl<'de> Deserialize<'de> for Origin {
805    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
806        String::deserialize(deserializer)?
807            .parse()
808            .map_err(de::Error::custom)
809    }
810}
811
812/// Why a config cannot be used.
813#[derive(Debug, thiserror::Error)]
814pub enum ConfigError {
815    /// The file cannot be read.
816    #[error("cannot read {}: {source}", path.display())]
817    Read {
818        /// The config file.
819        path: PathBuf,
820        /// Why reading it failed.
821        source: std::io::Error,
822    },
823    /// The file is not valid TOML or does not match the config's shape.
824    #[error(transparent)]
825    Parse(#[from] toml::de::Error),
826    /// A repository names a profile `[profiles]` does not define.
827    #[error(
828        "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
829        defined_profiles(.defined)
830    )]
831    UnknownProfile {
832        /// The repository's `owner/name`.
833        repository: String,
834        /// The profile it names.
835        profile: String,
836        /// The profiles `[profiles]` defines.
837        defined: Vec<String>,
838    },
839    /// A repository has no labels, or an empty one.
840    #[error("repository {repository} needs at least one non-empty label")]
841    MissingLabels {
842        /// The repository's `owner/name`.
843        repository: String,
844    },
845    /// `unsafe-dind` without `allow_unsafe = true`.
846    #[error(
847        "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
848         set allow_unsafe = true under [host] to use it anyway"
849    )]
850    UnsafeNotAllowed,
851    /// A limit is 0.
852    #[error("{field} is 0; set it above 0")]
853    ZeroLimit {
854        /// The setting, such as `profiles.app.cpus`.
855        field: String,
856    },
857    /// `cpu_overcommit` is not a number above 0 and at most `MAX_CPU_OVERCOMMIT`.
858    #[error(
859        "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
860    )]
861    CpuOvercommit {
862        /// The value set.
863        value: f64,
864    },
865    /// `[web] open` is not one of `[web] origins`.
866    #[error(
867        "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
868    )]
869    OpenNotListed {
870        /// The origin `open` names.
871        open: Origin,
872    },
873    /// A profile's `memory` or `slots` are outside its `min_memory`, `max_memory` or `max_slots`, or its
874    /// `min_memory` is above its `max_memory`.
875    #[error("profiles.{profile}: {why}")]
876    SizingBounds {
877        /// The profile.
878        profile: String,
879        /// Which bound, such as `memory is above its max_memory`.
880        why: &'static str,
881    },
882    /// A `[[notify.targets]]` entry cannot be used. Never says its URL or a header's value.
883    #[error("notify target {target:?}: {why}")]
884    Notify {
885        /// The target's name.
886        target: String,
887        /// What is wrong, such as `url does not start with https:// or http://`.
888        why: &'static str,
889    },
890    /// The caps add up past `u64::MAX` bytes.
891    #[error("slots × memory over all profiles plus the mirror is too large to add up")]
892    BudgetOverflow,
893    /// `admission = "measured"` with a slice that has no `MemoryMax` to admit against.
894    #[error(
895        "admission = \"measured\" admits runners against the slice {slice}'s MemoryMax, and it has none; install \
896         a unit file for it (`runnerd doctor` prints one)"
897    )]
898    MeasuredWithoutMemoryMax {
899        /// The slice runners run in.
900        slice: String,
901    },
902    /// Under `admission = "measured"`, one runner of a profile and the mirror do not fit `MemoryMax`.
903    #[error(
904        "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
905         its `memory` or raise the slice's MemoryMax"
906    )]
907    ProfileOverBudget {
908        /// The profile.
909        profile: String,
910        /// Its `memory` and the mirror's.
911        needs: ByteSize,
912        /// The slice runners run in.
913        slice: String,
914        /// The slice's `MemoryMax`.
915        memory_max: ByteSize,
916    },
917    /// The caps add up to more than the slice's `MemoryMax`.
918    #[error(
919        "slots × memory over all profiles plus the mirror is {total}, \
920         the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
921         MemoryMax (`runnerd doctor` prints its unit)"
922    )]
923    OverBudget {
924        /// What every runner and the mirror may use at once.
925        total: ByteSize,
926        /// The slice runners run in.
927        slice: String,
928        /// The slice's `MemoryMax`.
929        memory_max: ByteSize,
930    },
931}
932
933fn defined_profiles(defined: &[String]) -> String {
934    if defined.is_empty() {
935        "it defines none".to_owned()
936    } else {
937        format!("defined: {}", defined.join(", "))
938    }
939}
940
941/// What a valid config should still fix.
942#[derive(Debug, Clone, PartialEq, Eq)]
943pub enum Warning {
944    /// The slice has no unit file, so systemd creates it without limits.
945    UnlimitedSlice {
946        /// The slice runners run in.
947        slice: String,
948        /// What every runner and the mirror may use at once.
949        total: ByteSize,
950    },
951}
952
953impl fmt::Display for Warning {
954    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
955        match self {
956            Warning::UnlimitedSlice { slice, total } => write!(
957                f,
958                "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
959                 install a unit file for it"
960            ),
961        }
962    }
963}
964
965impl Config {
966    /// Reads and parses a config file.
967    pub fn load(path: &Path) -> Result<Config, ConfigError> {
968        let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
969            path: path.to_owned(),
970            source,
971        })?;
972        Config::parse(&text)
973    }
974
975    /// Parses a config from TOML text.
976    pub fn parse(text: &str) -> Result<Config, ConfigError> {
977        Ok(toml::from_str(text)?)
978    }
979
980    /// Parses `text` and validates it against the `MemoryMax` that `memory_max` reads for the slice it names:
981    /// what `runnerd check-config` prints and `POST /v1/config/check` answers. Returns the config if it is valid.
982    pub fn check(
983        text: &str,
984        memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
985    ) -> (Option<Config>, api::ConfigCheck) {
986        let invalid = |message: String, memory_caps: Option<ByteSize>, max: Option<ByteSize>| {
987            api::ConfigCheck {
988                valid: false,
989                message,
990                warnings: Vec::new(),
991                memory_caps: memory_caps.map(|caps| caps.0),
992                memory_max: max.map(|max| max.0),
993                revision: None,
994            }
995        };
996        let config = match Config::parse(text) {
997            Ok(config) => config,
998            Err(error) => return (None, invalid(error.to_string(), None, None)),
999        };
1000        let caps = config.memory_caps().ok();
1001        let max = match memory_max(&config.host.slice) {
1002            Ok(max) => max,
1003            Err(error) => return (None, invalid(error.to_string(), caps, None)),
1004        };
1005        match config
1006            .validate(max)
1007            .and_then(|warnings| Ok((warnings, config.memory_caps()?)))
1008        {
1009            Ok((warnings, total)) => {
1010                let message = match (config.host.admission, max) {
1011                    (Admission::Measured, Some(max)) if total > max => format!(
1012                        "valid, memory caps sum to {total}, over the {max} MemoryMax: admission = \"measured\" \
1013                         starts what the predicted peaks fit"
1014                    ),
1015                    _ => format!("valid, memory caps sum to {total}"),
1016                };
1017                let check = api::ConfigCheck {
1018                    valid: true,
1019                    message,
1020                    warnings: warnings.iter().map(ToString::to_string).collect(),
1021                    memory_caps: Some(total.0),
1022                    memory_max: max.map(|max| max.0),
1023                    revision: None,
1024                };
1025                (Some(config), check)
1026            }
1027            Err(error) => (None, invalid(error.to_string(), caps, max)),
1028        }
1029    }
1030
1031    /// The mirror's memory cap, 0 when it is disabled.
1032    pub fn mirror_memory(&self) -> u64 {
1033        if self.caches.mirror.enabled {
1034            self.caches.mirror.memory.0
1035        } else {
1036            0
1037        }
1038    }
1039
1040    /// The memory every runner and the mirror may use at once.
1041    pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1042        let mirror = self.mirror_memory();
1043        self.profiles
1044            .values()
1045            .try_fold(mirror, |total, p| {
1046                p.memory
1047                    .0
1048                    .checked_mul(u64::from(p.slots))?
1049                    .checked_add(total)
1050            })
1051            .map(ByteSize)
1052            .ok_or(ConfigError::BudgetOverflow)
1053    }
1054
1055    /// Refuses a profile outside the bounds auto sizing keeps it in, or whose bounds cross.
1056    fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1057        for (name, p) in &self.profiles {
1058            let memory = p.memory.0;
1059            let field = if p
1060                .min_memory
1061                .zip(p.max_memory)
1062                .is_some_and(|(min, max)| min.0 > max.0)
1063            {
1064                "min_memory is above its max_memory"
1065            } else if p.min_memory.is_some_and(|min| memory < min.0) {
1066                "memory is below its min_memory"
1067            } else if p.max_memory.is_some_and(|max| memory > max.0) {
1068                "memory is above its max_memory"
1069            } else if p.max_slots.is_some_and(|max| p.slots > max) {
1070                "slots are above its max_slots"
1071            } else {
1072                continue;
1073            };
1074            return Err(ConfigError::SizingBounds {
1075                profile: name.clone(),
1076                why: field,
1077            });
1078        }
1079        Ok(())
1080    }
1081
1082    /// Checks the config against the slice's `MemoryMax` (`None` when the slice is unlimited).
1083    pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1084        if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1085            return Err(ConfigError::UnsafeNotAllowed);
1086        }
1087        let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1088            [
1089                (p.memory.0 == 0, format!("profiles.{name}.memory")),
1090                (p.cpus == 0, format!("profiles.{name}.cpus")),
1091                (p.slots == 0, format!("profiles.{name}.slots")),
1092                (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1093            ]
1094        });
1095        let mirror = &self.caches.mirror;
1096        let mirror_zero_limits = [
1097            (
1098                mirror.enabled && mirror.memory.0 == 0,
1099                "caches.mirror.memory".to_owned(),
1100            ),
1101            (
1102                mirror.enabled && mirror.cpus == 0,
1103                "caches.mirror.cpus".to_owned(),
1104            ),
1105            (
1106                self.host.poll_interval.is_zero(),
1107                "host.poll_interval".to_owned(),
1108            ),
1109            (self.records.max_count == 0, "records.max_count".to_owned()),
1110            (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1111            (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1112            (
1113                self.sizing.raise_interval.is_zero(),
1114                "sizing.raise_interval".to_owned(),
1115            ),
1116            (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1117            (
1118                self.records.max_total.0 == 0,
1119                "records.max_total".to_owned(),
1120            ),
1121        ];
1122        if let Some((_, field)) = zero_limits
1123            .chain(mirror_zero_limits)
1124            .find(|(zero, _)| *zero)
1125        {
1126            return Err(ConfigError::ZeroLimit { field });
1127        }
1128        self.check_sizing_bounds()?;
1129        self.caches.build.validate()?;
1130        self.host.check_cpu_overcommit()?;
1131        self.web.validate()?;
1132        self.notify.validate()?;
1133        for repository in &self.repositories {
1134            if !self.profiles.contains_key(&repository.profile) {
1135                return Err(ConfigError::UnknownProfile {
1136                    repository: repository.name.clone(),
1137                    profile: repository.profile.clone(),
1138                    defined: self.profiles.keys().cloned().collect(),
1139                });
1140            }
1141            if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1142            {
1143                return Err(ConfigError::MissingLabels {
1144                    repository: repository.name.clone(),
1145                });
1146            }
1147        }
1148        let total = self.memory_caps()?;
1149        if self.host.admission == Admission::Measured {
1150            let Some(memory_max) = memory_max else {
1151                return Err(ConfigError::MeasuredWithoutMemoryMax {
1152                    slice: self.host.slice.clone(),
1153                });
1154            };
1155            let mirror = self.mirror_memory();
1156            if let Some((profile, needs)) = self
1157                .profiles
1158                .iter()
1159                .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1160                .find(|(_, needs)| *needs > memory_max.0)
1161            {
1162                return Err(ConfigError::ProfileOverBudget {
1163                    profile: profile.clone(),
1164                    needs: ByteSize(needs),
1165                    slice: self.host.slice.clone(),
1166                    memory_max,
1167                });
1168            }
1169            return Ok(Vec::new());
1170        }
1171        match memory_max {
1172            Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1173                total,
1174                slice: self.host.slice.clone(),
1175                memory_max,
1176            }),
1177            Some(_) => Ok(Vec::new()),
1178            None => Ok(vec![Warning::UnlimitedSlice {
1179                slice: self.host.slice.clone(),
1180                total,
1181            }]),
1182        }
1183    }
1184}
1185
1186/// Why `resize_profile` cannot change a config.
1187#[derive(Debug, thiserror::Error)]
1188pub enum ResizeError {
1189    /// The text is not valid TOML.
1190    #[error(transparent)]
1191    Parse(#[from] toml_edit::TomlError),
1192    /// `[profiles]` does not define the profile.
1193    #[error("the config has no profile {0:?} under [profiles]")]
1194    NoProfile(String),
1195}
1196
1197/// `text` with the profile's `memory` and `slots` replaced, and its `cpus` when `resize` has them, everything else as
1198/// written, comments included: what applying a sizing suggestion sends to `PUT /v1/config`, which validates it.
1199pub fn resize_profile(
1200    text: &str,
1201    profile: &str,
1202    resize: api::Resize,
1203) -> Result<String, ResizeError> {
1204    let mut document: toml_edit::DocumentMut = text.parse()?;
1205    let table = document
1206        .get_mut("profiles")
1207        .and_then(|profiles| profiles.get_mut(profile))
1208        .and_then(toml_edit::Item::as_table_like_mut)
1209        .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1210    let values = [
1211        (
1212            "memory",
1213            toml_edit::Value::from(ByteSize(resize.memory).to_string()),
1214        ),
1215        ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1216    ];
1217    let cpus = resize
1218        .cpus
1219        .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1220    for (key, mut value) in values.into_iter().chain(cpus) {
1221        // The comment after the old value stays after the new one.
1222        if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1223            *value.decor_mut() = old.decor().clone();
1224        }
1225        table.insert(key, toml_edit::Item::Value(value));
1226    }
1227    Ok(document.to_string())
1228}
1229
1230/// What `GET /v1/config` shows in place of a notification target's URL and header values (ADR 0051).
1231pub const SECRET_SHOWN: &str = "<secret>";
1232
1233/// `text` with each `[[notify.targets]]` entry's `url` and header values shown as [`SECRET_SHOWN`], everything else
1234/// as written: what `GET /v1/config` answers, so no client, the dashboard's browsers included, holds them. `None`
1235/// when `text` is not TOML.
1236pub fn mask_secrets(text: &str) -> Option<String> {
1237    let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1238    let mut masked = false;
1239    for target in notify_targets(&mut document) {
1240        for (_, value) in secrets_of(target) {
1241            replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1242            masked = true;
1243        }
1244    }
1245    Some(if masked {
1246        document.to_string()
1247    } else {
1248        text.to_owned()
1249    })
1250}
1251
1252/// `text` with each [`SECRET_SHOWN`] in a target's `url` or headers replaced by the value `stored` has for the target
1253/// of the same name, everything else as written: what `PUT /v1/config` and `POST /v1/config/check` read, so a config
1254/// shown masked saves with its secrets. A value with nothing to keep stays [`SECRET_SHOWN`], which `Config::validate`
1255/// refuses. A text that does not parse as a config is answered as sent: a TOML error quotes the line it is on, and
1256/// that line must hold the mask, not the value put back. One that parses still does with the values put back, as they
1257/// are strings where strings were, and `Config::validate` names a target's field, never its value.
1258pub fn keep_secrets(text: &str, stored: &str) -> String {
1259    if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1260        return text.to_owned();
1261    }
1262    let (Ok(mut document), Ok(mut old)) = (
1263        text.parse::<toml_edit::DocumentMut>(),
1264        stored.parse::<toml_edit::DocumentMut>(),
1265    ) else {
1266        return text.to_owned();
1267    };
1268    let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1269    for target in notify_targets(&mut old) {
1270        let Some(name) = target_name(target) else {
1271            continue;
1272        };
1273        let secrets = secrets_of(target)
1274            .into_iter()
1275            .map(|(field, value)| (field, value.clone()))
1276            .collect();
1277        kept.insert(name, secrets);
1278    }
1279    for target in notify_targets(&mut document) {
1280        let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1281            continue;
1282        };
1283        for (field, value) in secrets_of(target) {
1284            if value.as_str() != Some(SECRET_SHOWN) {
1285                continue;
1286            }
1287            if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1288                replace_value(value, old.clone());
1289            }
1290        }
1291    }
1292    document.to_string()
1293}
1294
1295/// Where a target keeps a secret.
1296#[derive(Debug, PartialEq, Eq)]
1297enum SecretField {
1298    Url,
1299    Header(String),
1300}
1301
1302/// The tables of `[[notify.targets]]`, or of an inline `targets = [...]`.
1303fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1304    match document
1305        .get_mut("notify")
1306        .and_then(|notify| notify.get_mut("targets"))
1307    {
1308        Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1309            .iter_mut()
1310            .map(|table| table as &mut dyn toml_edit::TableLike)
1311            .collect(),
1312        Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1313            .iter_mut()
1314            .filter_map(toml_edit::Value::as_inline_table_mut)
1315            .map(|table| table as &mut dyn toml_edit::TableLike)
1316            .collect(),
1317        _ => Vec::new(),
1318    }
1319}
1320
1321fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1322    target
1323        .get("name")
1324        .and_then(toml_edit::Item::as_str)
1325        .map(str::to_owned)
1326}
1327
1328/// A target's `url` and header values.
1329fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1330    let mut found = Vec::new();
1331    for (key, item) in target.iter_mut() {
1332        match key.get() {
1333            "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1334            "headers" => {
1335                let Some(headers) = item.as_table_like_mut() else {
1336                    continue;
1337                };
1338                for (name, item) in headers.iter_mut() {
1339                    let field = SecretField::Header(name.get().to_owned());
1340                    found.extend(item.as_value_mut().map(|value| (field, value)));
1341                }
1342            }
1343            _ => {}
1344        }
1345    }
1346    found
1347}
1348
1349/// Puts `new` in place of `value`, with the spaces and comment around it as they were.
1350fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1351    *new.decor_mut() = value.decor().clone();
1352    *value = new;
1353}
1354
1355/// A config text's revision: a hash of its bytes (64-bit FNV-1a, in hex), the same in every runnerd. `PUT
1356/// /v1/config?revision=` refuses a text sent against another revision than the daemon's.
1357pub fn revision(text: &str) -> String {
1358    let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1359        (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1360    });
1361    format!("{hash:016x}")
1362}
1363
1364/// Reads a slice's `MemoryMax` from systemd; `None` when it is unlimited.
1365pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1366    let output = Command::new("systemctl")
1367        .args(["show", slice, "--property=MemoryMax", "--value"])
1368        .output()?;
1369    if !output.status.success() {
1370        return Err(std::io::Error::other(format!(
1371            "systemctl show {slice} failed: {}",
1372            String::from_utf8_lossy(&output.stderr).trim()
1373        )));
1374    }
1375    let value = String::from_utf8_lossy(&output.stdout);
1376    parse_memory_max(value.trim()).ok_or_else(|| {
1377        std::io::Error::other(format!(
1378            "systemctl show {slice}: unexpected MemoryMax {value:?}"
1379        ))
1380    })
1381}
1382
1383/// The CPU budget, in millicores, of `millicores` of CPUs overcommitted `overcommit` times.
1384pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1385    #[expect(
1386        clippy::cast_precision_loss,
1387        clippy::cast_possible_truncation,
1388        clippy::cast_sign_loss,
1389        reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1390                  most 16"
1391    )]
1392    let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1393    budget
1394}
1395
1396/// Reads the CPUs a slice's processes may use from systemd, in millicores: its `EffectiveCPUs` (or `AllowedCPUs`,
1397/// else the host's CPUs), less when its `CPUQuota` is lower.
1398pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1399    let output = Command::new("systemctl")
1400        .args([
1401            "show",
1402            slice,
1403            "--property=EffectiveCPUs",
1404            "--property=AllowedCPUs",
1405            "--property=CPUQuotaPerSecUSec",
1406        ])
1407        .output()?;
1408    if !output.status.success() {
1409        return Err(std::io::Error::other(format!(
1410            "systemctl show {slice} failed: {}",
1411            String::from_utf8_lossy(&output.stderr).trim()
1412        )));
1413    }
1414    let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1415    parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1416        std::io::Error::other(format!(
1417            "systemctl show {slice}: unexpected CPU properties {:?}",
1418            String::from_utf8_lossy(&output.stdout).trim()
1419        ))
1420    })
1421}
1422
1423/// The CPUs, in millicores, that `systemctl show`'s `EffectiveCPUs`, `AllowedCPUs` and `CPUQuotaPerSecUSec` lines
1424/// leave a slice on a host with `host` CPUs; `None` when a value does not parse.
1425pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1426    let value = |name: &str| {
1427        properties.lines().find_map(|line| {
1428            line.strip_prefix(name)
1429                .and_then(|rest| rest.strip_prefix('='))
1430                .map(str::trim)
1431        })
1432    };
1433    let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
1434        .into_iter()
1435        .flatten()
1436        .find(|cpus| !cpus.is_empty());
1437    let cpus = match set {
1438        Some(set) => cpu_count(set)?,
1439        None => host,
1440    };
1441    let quota = match value("CPUQuotaPerSecUSec") {
1442        None | Some("" | "infinity") => u64::MAX,
1443        Some(quota) => quota_millicores(quota)?,
1444    };
1445    Some(cpus.saturating_mul(1000).min(quota))
1446}
1447
1448/// How many CPUs a set names, as systemd prints it (`0-3 8-11`) or as it is written (`0-3,8,10-11`).
1449fn cpu_count(set: &str) -> Option<u64> {
1450    set.split([',', ' '])
1451        .filter(|range| !range.is_empty())
1452        .try_fold(0u64, |count, range| {
1453            let (first, last) = range.split_once('-').unwrap_or((range, range));
1454            let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
1455            Some(count + last.checked_sub(first)? + 1)
1456        })
1457}
1458
1459/// A `CPUQuotaPerSecUSec` as systemd prints a timespan, such as `800ms`, `2.500000s` or `1min 4s`, as the
1460/// millicores it allows: a second of CPU time each second is 1000.
1461fn quota_millicores(quota: &str) -> Option<u64> {
1462    // Each unit's length in milliseconds, as systemd's `format_timespan` names them.
1463    const UNITS: [(&str, f64); 10] = [
1464        ("y", 31_557_600_000.0),
1465        ("M", 2_629_800_000.0),
1466        ("w", 604_800_000.0),
1467        ("d", 86_400_000.0),
1468        ("h", 3_600_000.0),
1469        ("min", 60_000.0),
1470        ("s", 1000.0),
1471        ("ms", 1.0),
1472        ("us", 0.001),
1473        ("µs", 0.001),
1474    ];
1475    let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
1476        let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
1477        let (number, unit) = part.split_at(at);
1478        let per_unit = UNITS
1479            .iter()
1480            .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
1481        Some(total + number.parse::<f64>().ok()? * per_unit)
1482    })?;
1483    #[allow(
1484        clippy::cast_possible_truncation,
1485        clippy::cast_sign_loss,
1486        reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
1487    )]
1488    (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
1489        .then(|| millicores.round() as u64)
1490}
1491
1492/// Parses a `MemoryMax` value as `systemctl show` prints it: bytes, or `infinity`.
1493pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
1494    if value == "infinity" {
1495        Some(None)
1496    } else {
1497        value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
1498    }
1499}
1500
1501/// A size in bytes, written with a binary unit as Docker and systemd do: `512M`, `9G`, `32.5G`.
1502#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1503pub struct ByteSize(pub u64);
1504
1505const UNITS: [(char, u64); 4] = [
1506    ('T', 1 << 40),
1507    ('G', 1 << 30),
1508    ('M', 1 << 20),
1509    ('K', 1 << 10),
1510];
1511
1512impl FromStr for ByteSize {
1513    type Err = String;
1514
1515    #[allow(
1516        clippy::cast_precision_loss,
1517        clippy::cast_possible_truncation,
1518        clippy::cast_sign_loss,
1519        reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
1520    )]
1521    fn from_str(s: &str) -> Result<Self, Self::Err> {
1522        let invalid = || {
1523            format!(
1524                "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
1525            )
1526        };
1527        let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
1528        let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
1529        let number = &s[..s.len() - 1];
1530        if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
1531            return Err(invalid());
1532        }
1533        let number: f64 = number.parse().map_err(|_| invalid())?;
1534        let bytes = (number * *factor as f64).round();
1535        // `u64::MAX as f64` is 2^64, the first value out of range.
1536        if bytes >= u64::MAX as f64 {
1537            return Err(format!("size {s:?} is too large"));
1538        }
1539        Ok(ByteSize(bytes as u64))
1540    }
1541}
1542
1543impl fmt::Display for ByteSize {
1544    #[allow(
1545        clippy::cast_precision_loss,
1546        reason = "shown with two decimals, far coarser than f64's precision"
1547    )]
1548    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1549        let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
1550            return write!(f, "{}B", self.0);
1551        };
1552        let number = format!("{:.2}", self.0 as f64 / *factor as f64);
1553        let number = number.trim_end_matches('0').trim_end_matches('.');
1554        write!(f, "{number}{unit}")
1555    }
1556}
1557
1558impl<'de> Deserialize<'de> for ByteSize {
1559    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1560        String::deserialize(deserializer)?
1561            .parse()
1562            .map_err(de::Error::custom)
1563    }
1564}
1565
1566/// Parses a duration as Go and the registry write it, `30s`, `90m`, `168h`, or in days: `7d`.
1567pub fn parse_duration(s: &str) -> Result<Duration, String> {
1568    let invalid = || {
1569        format!(
1570            "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
1571        )
1572    };
1573    let (number, factor) = match s.chars().last() {
1574        Some('s') => (&s[..s.len() - 1], 1),
1575        Some('m') => (&s[..s.len() - 1], 60),
1576        Some('h') => (&s[..s.len() - 1], 3600),
1577        Some('d') => (&s[..s.len() - 1], 86400),
1578        _ => return Err(invalid()),
1579    };
1580    let number: u64 = number.parse().map_err(|_| invalid())?;
1581    number
1582        .checked_mul(factor)
1583        .map(Duration::from_secs)
1584        .ok_or_else(invalid)
1585}
1586
1587/// Parses an age as the clients' `--since` and `--older-than` take it: `30m`, `12h`, `7d`, `2w`.
1588pub fn parse_age(text: &str) -> Result<Duration, String> {
1589    let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
1590    let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
1591    let (number, unit) = text.split_at(split);
1592    let number: u64 = number.parse().map_err(|_| error())?;
1593    let unit = match unit {
1594        "s" => 1,
1595        "m" => 60,
1596        "h" => 3600,
1597        "d" => 86_400,
1598        "w" => 7 * 86_400,
1599        _ => return Err(error()),
1600    };
1601    Ok(Duration::from_secs(number.saturating_mul(unit)))
1602}
1603
1604fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
1605    parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
1606}
1607
1608#[cfg(test)]
1609mod tests;