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
17mod budget;
18
19pub use budget::{BudgetBlock, CheckBudget, Remainder, Verdict};
20
21/// A parsed `runnerd.toml`. `validate` checks it against the host's budget.
22#[derive(Debug, Deserialize)]
23#[serde(deny_unknown_fields)]
24pub struct Config {
25    /// The host's budget and isolation.
26    pub host: Host,
27    /// The caches runners share.
28    pub caches: Caches,
29    /// Runner profiles by name.
30    pub profiles: BTreeMap<String, Profile>,
31    /// The repositories to serve, one pool per entry: a repository named by several entries is served by each of
32    /// their profiles, a job by the one whose labels match it (ADR 0058).
33    pub repositories: Vec<Repository>,
34    /// What is kept of past jobs.
35    #[serde(default)]
36    pub history: History,
37    /// How much the records of failed jobs may keep.
38    #[serde(default)]
39    pub records: Records,
40    /// How sizing advice reads the job history.
41    #[serde(default)]
42    pub sizing: Sizing,
43    /// Who may use the dashboard through another address.
44    #[serde(default)]
45    pub web: Web,
46    /// Where the daemon sends notifications; none by default.
47    #[serde(default)]
48    pub notify: Notify,
49    /// What runnerd may ask of GitHub on the owner's behalf.
50    #[serde(default)]
51    pub github: Github,
52}
53
54/// `[host]`: where runner containers run and how they are isolated.
55#[derive(Debug, Deserialize)]
56#[serde(deny_unknown_fields)]
57pub struct Host {
58    /// The systemd slice every runner container is placed in; its `MemoryMax` is the budget.
59    pub slice: String,
60    /// How runner containers are kept from the host.
61    pub isolation: Isolation,
62    /// Must be set for `isolation = "unsafe-dind"`, so a copied config cannot turn it on silently.
63    #[serde(default)]
64    pub allow_unsafe: bool,
65    /// How often GitHub is asked for queued jobs.
66    #[serde(default = "default_poll_interval", deserialize_with = "duration")]
67    pub poll_interval: Duration,
68    /// What a runner must fit in before it starts. Default `measured` (ADR 0138).
69    #[serde(default)]
70    pub admission: Admission,
71    /// Set, a runner also starts only when its predicted CPU fits the slice's CPUs times this; absent, CPU is not
72    /// admitted.
73    #[serde(default)]
74    pub cpu_overcommit: Option<f64>,
75    /// Set, the most memory the host can grow to, for a VM whose hypervisor adds memory as the guest asks for it:
76    /// admission counts what it can still grow by past `MemTotal` as available, and keeps the margin of this
77    /// (ADR 0084). Absent, or not above `MemTotal`, the host is the memory it has now.
78    #[serde(default)]
79    pub memory_ceiling: Option<ByteSize>,
80    /// Under measured admission, whether `MemoryMax` and the host's limit count what runs uses and keep room for its
81    /// growth (ADR 0099, 0108, 0130) rather than every runner's worst case. Default on; off, both add every runner's
82    /// take, the way back if a host meets what the count did not foresee.
83    #[serde(default = "default_on")]
84    pub pooled_count: bool,
85    /// Whether runners whose image carries `runnerd-skip-syncs` are asked to answer `fsync` and the other sync calls
86    /// without syncing (ADR 0141). Default on: what a runner writes is a cache or goes with its container, and a host
87    /// crash empties those caches. A runner of an image without it keeps its syncs whatever this says. A reload
88    /// applies it from the next runner on.
89    #[serde(default = "default_on")]
90    pub skip_syncs: bool,
91}
92
93/// Whether a profile's image answers its runner's sync calls without syncing, as doctor reads it.
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub enum ImageSyncs {
96    /// It carries `runnerd-skip-syncs`, as its label says: runnerd asks its runners to skip their syncs.
97    Skips,
98    /// It lacks it, as runner images before it do: runnerd never asks its runners, and they sync.
99    Syncs,
100    /// Docker does not have it, or did not answer: each runner's start reads it.
101    Unread,
102}
103
104/// Whether the systemd journal tells runnerd how a past boot ended (`boots::past_boots`).
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum PastBoots {
107    /// It keeps past boots and runnerd's user reads `systemd`'s lines.
108    Read,
109    /// It keeps no boot before this one: it lives in memory, was vacuumed below one boot, or the host booted once.
110    NotKept,
111    /// runnerd's user reads none of `systemd`'s lines.
112    Unreadable,
113}
114
115impl Host {
116    /// Doctor's word on whether runners sync what they write, from what each profile's image carries (`images`, by
117    /// reference) and whether the journal shows how past boots ended (`boots`). For information, unless a crash guard
118    /// that cannot read the journal empties the caches after every boot.
119    pub fn syncs_check(
120        &self,
121        images: &BTreeMap<String, ImageSyncs>,
122        boots: PastBoots,
123    ) -> api::Check {
124        let check = |status, detail: String, fix: Option<&str>| api::Check {
125            name: "runner syncs".to_owned(),
126            status,
127            detail,
128            fix: fix.map(str::to_owned),
129        };
130        if !self.skip_syncs {
131            return check(
132                api::CheckStatus::Info,
133                "kept, as [host] skip_syncs = false sets".to_owned(),
134                None,
135            );
136        }
137        let of = |wanted: ImageSyncs| -> Vec<&str> {
138            images
139                .iter()
140                .filter(|(_, syncs)| **syncs == wanted)
141                .map(|(image, _)| image.as_str())
142                .collect()
143        };
144        let (skips, syncs, unread) = (
145            of(ImageSyncs::Skips),
146            of(ImageSyncs::Syncs),
147            of(ImageSyncs::Unread),
148        );
149        let mut parts = Vec::new();
150        if !skips.is_empty() {
151            parts.push(format!(
152                "skipped by the runners of {}, which carry runnerd-skip-syncs",
153                skips.join(", ")
154            ));
155        }
156        if !syncs.is_empty() {
157            parts.push(format!(
158                "kept by the runners of {}: they lack runnerd-skip-syncs, so runnerd never asks them",
159                syncs.join(", ")
160            ));
161        }
162        if !unread.is_empty() {
163            parts.push(format!(
164                "decided at each runner's start for {}, which Docker does not have yet",
165                unread.join(", ")
166            ));
167        }
168        if parts.is_empty() {
169            parts.push(
170                "decided at each runner's start, from its image's runnerd.skip-syncs label"
171                    .to_owned(),
172            );
173        }
174        let runners = parts.join("; ");
175        if skips.is_empty() && unread.is_empty() && !syncs.is_empty() {
176            return check(
177                api::CheckStatus::Info,
178                format!("{runners}. Nothing is emptied after a crash"),
179                None,
180            );
181        }
182        let crash = "a crash or power loss while such a runner ran empties the slots' volumes and the pnpm and Maven \
183                     stores before any runner starts";
184        match boots {
185            PastBoots::Read => check(
186                api::CheckStatus::Info,
187                format!(
188                    "{runners}. After {crash}; an orderly shutdown or reboot keeps them, as the journal shows its \
189                     final sync"
190                ),
191                None,
192            ),
193            PastBoots::NotKept => check(
194                api::CheckStatus::Warn,
195                format!(
196                    "{runners}. This host's journal keeps no past boot, so runnerd cannot tell an orderly shutdown \
197                     from a crash: every boot after such a runner ran empties the slots' volumes and the pnpm and \
198                     Maven stores before any runner starts"
199                ),
200                Some(
201                    "keep the journal across boots: set Storage=persistent in /etc/systemd/journald.conf, then \
202                     sudo systemctl restart systemd-journald; or set [host] skip_syncs = false",
203                ),
204            ),
205            PastBoots::Unreadable => check(
206                api::CheckStatus::Warn,
207                format!(
208                    "{runners}. runnerd's user cannot read the system journal, so runnerd cannot tell an orderly \
209                     shutdown from a crash: every boot after such a runner ran empties the slots' volumes and the \
210                     pnpm and Maven stores before any runner starts"
211                ),
212                Some(
213                    "let runnerd's user read the journal: sudo usermod -aG systemd-journal \"$USER\", then log in \
214                     again; or set [host] skip_syncs = false",
215                ),
216            ),
217        }
218    }
219
220    /// Refuses a `cpu_overcommit` that is not above 0 and at most `MAX_CPU_OVERCOMMIT`.
221    fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
222        match self.cpu_overcommit {
223            Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
224                Err(ConfigError::CpuOvercommit { value })
225            }
226            _ => Ok(()),
227        }
228    }
229}
230
231/// The largest `cpu_overcommit` the config check takes.
232pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
233
234/// `admission` under `[host]`: what a new runner must fit in the slice's `MemoryMax` beside the running ones.
235#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
236#[serde(rename_all = "kebab-case")]
237pub enum Admission {
238    /// Every runner's memory cap: `slots × memory` over all profiles, with the mirror, must fit `MemoryMax`. The
239    /// opt-out from measured admission.
240    Caps,
241    /// The default: every runner's predicted peak, from the job history; `slots` only bounds how many run at once,
242    /// and one runner of each profile with the mirror must fit `MemoryMax`. Nothing starts while the slice is at 90%
243    /// of it.
244    #[default]
245    Measured,
246}
247
248impl Admission {
249    /// The policy as the API names it.
250    pub fn policy(self) -> api::AdmissionPolicy {
251        match self {
252            Admission::Caps => api::AdmissionPolicy::Caps,
253            Admission::Measured => api::AdmissionPolicy::Measured,
254        }
255    }
256}
257
258fn default_on() -> bool {
259    true
260}
261
262fn default_poll_interval() -> Duration {
263    Duration::from_secs(15)
264}
265
266/// How runner containers are kept from the host: `isolation` under `[host]`.
267#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
268#[serde(rename_all = "kebab-case")]
269pub enum Isolation {
270    /// Sysbox: Docker inside an unprivileged container.
271    Sysbox,
272    /// Privileged Docker-in-Docker: a job can take over the host.
273    UnsafeDind,
274}
275
276/// `[caches]`: what runners share and keep between jobs.
277#[derive(Debug, Clone, Deserialize)]
278#[serde(deny_unknown_fields)]
279pub struct Caches {
280    /// The Docker Hub mirror.
281    pub mirror: Mirror,
282    /// The shared pnpm store.
283    pub pnpm: CacheLimit,
284    /// The shared Maven repository.
285    pub maven: CacheLimit,
286    /// When a profile has no job running and its slot 1 changed its layers by at least this much, slot 1's
287    /// layers become the profile's new snapshot.
288    #[serde(default = "default_reseed_at")]
289    pub reseed_at: ByteSize,
290    /// A reseed that would make a profile's snapshot larger than this starts it over from slot 1's changes alone,
291    /// or from the image when those are larger too.
292    #[serde(default = "default_snapshot_max")]
293    pub snapshot_max: ByteSize,
294    /// The build cache every runner shares.
295    #[serde(default)]
296    pub build: BuildCache,
297}
298
299/// `[caches.build]`: the build cache every runner shares (ADR 0052): Gradle's, and the other tools' the runner image
300/// points at it. Every field has a default.
301#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
302#[serde(deny_unknown_fields, default)]
303pub struct BuildCache {
304    /// Whether runners get it. Default true.
305    pub enabled: bool,
306    /// Above this size, the entries used least recently go when no job runs. Default 10G.
307    pub max_size: ByteSize,
308    /// The branches whose pushes, scheduled and manually started runs may write to it besides each repository's
309    /// default branch, which always may. Every other job only reads. Default empty.
310    pub branches: Vec<String>,
311    /// Where its entries are kept; default `$XDG_CACHE_HOME/runnerd/build-cache`.
312    pub directory: Option<PathBuf>,
313    /// Whether it also serves GitHub Actions' cache, for `actions/cache`, the `setup-*` actions' caches and
314    /// BuildKit's `type=gha`, in place of GitHub's (ADR 0082). Default true.
315    pub actions_cache: bool,
316}
317
318impl BuildCache {
319    /// Doctor's word on where jobs' GitHub Actions cache is: for information, it fails nothing.
320    pub fn actions_cache_check(&self) -> api::Check {
321        api::Check {
322            name: api::ACTIONS_CACHE.to_owned(),
323            status: api::CheckStatus::Info,
324            detail: if self.enabled {
325                api::actions_cache_place(self.actions_cache)
326            } else {
327                "GitHub's, as the build cache is off"
328            }
329            .to_owned(),
330            fix: None,
331        }
332    }
333
334    fn validate(&self) -> Result<(), ConfigError> {
335        if self.enabled && self.max_size.0 == 0 {
336            return Err(ConfigError::ZeroLimit {
337                field: "caches.build.max_size".to_owned(),
338            });
339        }
340        Ok(())
341    }
342}
343
344impl Default for BuildCache {
345    fn default() -> BuildCache {
346        BuildCache {
347            enabled: true,
348            max_size: ByteSize(10 << 30),
349            branches: Vec::new(),
350            directory: None,
351            actions_cache: true,
352        }
353    }
354}
355
356fn default_reseed_at() -> ByteSize {
357    ByteSize(2 << 30)
358}
359
360/// Above the 8.7G a profile running Flutter and Android builds needs: its Flutter SDK, NDK and Gradle caches.
361fn default_snapshot_max() -> ByteSize {
362    ByteSize(10 << 30)
363}
364
365fn default_mirror_max_size() -> ByteSize {
366    ByteSize(10 << 30)
367}
368
369/// The Docker Hub pull-through mirror shared by all runners.
370#[derive(Debug, Clone, Deserialize)]
371#[serde(deny_unknown_fields)]
372pub struct Mirror {
373    /// Whether runners pull through the mirror.
374    pub enabled: bool,
375    /// The mirror container's memory limit.
376    pub memory: ByteSize,
377    /// The mirror container's CPUs.
378    pub cpus: u32,
379    /// How long content not pulled again is kept.
380    #[serde(deserialize_with = "duration")]
381    pub ttl: Duration,
382    /// Its storage is emptied above this size when no job runs.
383    #[serde(default = "default_mirror_max_size")]
384    pub max_size: ByteSize,
385}
386
387/// A size limit on a shared cache.
388#[derive(Debug, Clone, Deserialize)]
389#[serde(deny_unknown_fields)]
390pub struct CacheLimit {
391    /// The cache is emptied above this size when no job runs.
392    pub max_size: ByteSize,
393}
394
395/// Where runnerd's release workflow publishes the runner images, one tag per stage and version, such as
396/// `ghcr.io/ayagmar/runnerd-runner:jvm-0.1.2`.
397pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
398
399/// A runner image with its limits; every job of the profile gets one container with these limits.
400#[derive(Debug, Clone, Deserialize)]
401#[serde(try_from = "ProfileToml")]
402pub struct Profile {
403    /// The image its runners run: `image_stage` or `image`, exactly one of them.
404    pub image: Image,
405    /// CPUs per slot.
406    pub cpus: u32,
407    /// Memory per slot; the container's hard limit.
408    pub memory: ByteSize,
409    /// The most jobs of this profile running at once.
410    pub slots: u32,
411    /// Whether a failed job's runner is kept as a record, to open a shell in (`runnerctl shell`).
412    pub record_failures: bool,
413    /// The least `memory` auto sizing lowers it to; `None` for no bound.
414    pub min_memory: Option<ByteSize>,
415    /// The most `memory` auto sizing raises it to; `None` for what the slice's `MemoryMax` holds.
416    pub max_memory: Option<ByteSize>,
417    /// The most `slots` auto sizing raises them to; `None` for as many as the slice's `MemoryMax` holds.
418    pub max_slots: Option<u32>,
419    /// The GitHub-hosted runner its jobs are priced at (ADR 0072); `None` for the daemon's architecture's 2-core
420    /// standard runner.
421    pub priced_as: Option<api::Sku>,
422}
423
424/// The image a profile's runners run.
425#[derive(Debug, Clone, PartialEq, Eq)]
426pub enum Image {
427    /// `image_stage`: a stage of `image/Dockerfile` built on this host as `runnerd-runner:<stage>`.
428    Stage(String),
429    /// `image`: a reference runnerd pulls when Docker lacks it, such as
430    /// `ghcr.io/ayagmar/runnerd-runner:jvm-0.1.2`.
431    Pulled(String),
432}
433
434impl Image {
435    /// The reference Docker runs.
436    pub fn reference(&self) -> String {
437        match self {
438            Image::Stage(stage) => format!("runnerd-runner:{stage}"),
439            Image::Pulled(reference) => reference.clone(),
440        }
441    }
442}
443
444/// `[profiles.<name>]` as written, before `Profile` checks it names one image.
445#[derive(Deserialize)]
446#[serde(deny_unknown_fields)]
447struct ProfileToml {
448    image_stage: Option<String>,
449    image: Option<String>,
450    cpus: u32,
451    memory: ByteSize,
452    slots: u32,
453    #[serde(default)]
454    record_failures: bool,
455    min_memory: Option<ByteSize>,
456    max_memory: Option<ByteSize>,
457    max_slots: Option<u32>,
458    priced_as: Option<api::Sku>,
459}
460
461impl TryFrom<ProfileToml> for Profile {
462    type Error = String;
463
464    fn try_from(profile: ProfileToml) -> Result<Profile, String> {
465        let image = match (profile.image_stage, profile.image) {
466            (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
467            (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
468            (Some(_), Some(_)) => {
469                return Err(
470                    "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
471                     not both"
472                        .to_owned(),
473                );
474            }
475            _ => {
476                return Err(
477                    "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
478                        .to_owned(),
479                );
480            }
481        };
482        Ok(Profile {
483            image,
484            cpus: profile.cpus,
485            memory: profile.memory,
486            slots: profile.slots,
487            record_failures: profile.record_failures,
488            min_memory: profile.min_memory,
489            max_memory: profile.max_memory,
490            max_slots: profile.max_slots,
491            priced_as: profile.priced_as,
492        })
493    }
494}
495
496/// Whether a job asking for `wanted` may run on runners registered with `labels`: every label it asks for is one of
497/// theirs. GitHub compares labels without regard to case. A job asking for no label, as one that names only a runner
498/// group, matches no runners: nothing says which pool it is for (ADR 0058).
499pub fn labels_match(wanted: &[String], labels: &[String]) -> bool {
500    !wanted.is_empty()
501        && wanted.iter().all(|wanted| {
502            labels
503                .iter()
504                .any(|label| label.eq_ignore_ascii_case(wanted))
505        })
506}
507
508/// A repository to serve: `[[repositories]]`. Each entry is one of the repository's pools; entries with the same
509/// `name` serve it in several profiles, each with its own labels, which no two of them share (ADR 0058).
510#[derive(Debug, Deserialize)]
511#[serde(deny_unknown_fields)]
512pub struct Repository {
513    /// `owner/name` on GitHub.
514    pub name: String,
515    /// The profile the jobs this pool's labels match run in.
516    pub profile: String,
517    /// The labels its runners register with; a job runs on them when every label it asks for is one of these.
518    pub labels: Vec<String>,
519    /// How many idle runners it keeps registered for jobs not queued yet while it is active (ADR 0050, 0107); 0 turns
520    /// warm runners and bursts off. Default 1.
521    #[serde(default = "default_warm")]
522    pub warm: u32,
523    /// Whether its runners get the shared build cache, when `[caches.build]` is enabled. Default true.
524    #[serde(default = "default_true")]
525    pub build_cache: bool,
526    /// Whose runs may write to the shared build cache besides the branches' (ADR 0053), the same in every pool of
527    /// the repository. Default `"default-branch"`.
528    #[serde(default)]
529    pub build_cache_writers: BuildCacheWriters,
530}
531
532/// `build_cache_writers` of a `[[repositories]]`: whose jobs' uploads the shared build cache keeps (ADR 0053).
533#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
534#[serde(rename_all = "kebab-case")]
535pub enum BuildCacheWriters {
536    /// Pushes, scheduled and manually started runs of the default branch and of `[caches.build] branches`.
537    #[default]
538    DefaultBranch,
539    /// Those, and `pull_request` runs of the repository's own branches that people, not bots, opened and pushed.
540    PullRequests,
541}
542
543fn default_true() -> bool {
544    true
545}
546
547fn default_warm() -> u32 {
548    1
549}
550
551/// `[history]`: the job history database and how long it keeps what. Every field has a default.
552#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
553#[serde(deny_unknown_fields, default)]
554pub struct History {
555    /// The SQLite database; default `$XDG_STATE_HOME/runnerd/runnerd.db`.
556    pub database: Option<PathBuf>,
557    /// How long CPU and memory samples are kept; a job's totals and peaks stay. Default 7 days.
558    #[serde(deserialize_with = "duration")]
559    pub samples: Duration,
560    /// How long a finished job's log is kept. Default 30 days.
561    #[serde(deserialize_with = "duration")]
562    pub logs: Duration,
563    /// How long events are kept for clients resuming with `Last-Event-ID`. Default 30 days.
564    #[serde(deserialize_with = "duration")]
565    pub events: Duration,
566    /// How long a finished job's record is kept, with its steps, totals and runner. Default 90 days.
567    #[serde(deserialize_with = "duration")]
568    pub jobs: Duration,
569    /// The most log stored per job; later lines are dropped. Default 50M.
570    pub log_max: ByteSize,
571}
572
573impl Default for History {
574    fn default() -> History {
575        History {
576            database: None,
577            samples: Duration::from_hours(7 * 24),
578            logs: Duration::from_hours(30 * 24),
579            events: Duration::from_hours(30 * 24),
580            jobs: Duration::from_hours(90 * 24),
581            log_max: ByteSize(50 << 20),
582        }
583    }
584}
585
586/// `[records]`: how much the records of failed jobs may keep (`record_failures` under a profile turns them on).
587/// When a new record would pass a limit, the oldest records go first. Every field has a default.
588#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
589#[serde(deny_unknown_fields, default)]
590pub struct Records {
591    /// The most disk all records may take together. Default 20G.
592    pub max_total: ByteSize,
593    /// The most records kept. Default 10.
594    pub max_count: u32,
595    /// How long a record is kept. Default 7 days.
596    #[serde(deserialize_with = "duration")]
597    pub max_age: Duration,
598    /// The disk Docker must keep free: no record is made that would leave less. Default 10G.
599    pub min_free: ByteSize,
600}
601
602impl Default for Records {
603    fn default() -> Records {
604        Records {
605            max_total: ByteSize(20 << 30),
606            max_count: 10,
607            max_age: Duration::from_hours(7 * 24),
608            min_free: ByteSize(10 << 30),
609        }
610    }
611}
612
613/// `[sizing]`: how `runnerctl sizing` turns the job history's peaks into advice, and whether the daemon applies it
614/// itself. Every field has a default.
615#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
616#[serde(deny_unknown_fields, default)]
617pub struct Sizing {
618    /// The percent added to the largest sampled memory peak: a container is sampled every 2 s, so a spike between
619    /// two samples goes unseen. Default 15. Advice adds at least `api::PAGE_CACHE_ROOM`.
620    pub margin: u32,
621    /// The fewest jobs a profile, or a job of a repository's workflow, needs before its peaks become advice.
622    /// Default 10.
623    pub min_jobs: u32,
624    /// Whether the daemon writes each profile's advice into the config itself, within each profile's
625    /// `min_memory`, `max_memory` and `max_slots`. Default off.
626    pub auto: bool,
627    /// The least time between a change auto sizing makes to one profile and one that lowers anything in it. Default
628    /// 1 hour: a lowering that went too far is taken back at its first kill (docs/adr/0071).
629    #[serde(deserialize_with = "duration")]
630    pub interval: Duration,
631    /// The least time between a change auto sizing makes to one profile and one that only raises its `memory`,
632    /// `slots` or `cpus`; at most `interval`, which a longer one counts as. Default 1 hour.
633    #[serde(deserialize_with = "duration")]
634    pub raise_interval: Duration,
635    /// The most a change lowers a profile's `memory` or moves its `cpus`, in percent of them; a raise of `memory`,
636    /// which only a run that met the cap gives, goes straight to the advice. Default 50.
637    pub max_step: u32,
638}
639
640impl Sizing {
641    /// The least time between a change to one profile and one that only raises it: `raise_interval`, at most
642    /// `interval`.
643    pub fn raise_after(&self) -> Duration {
644        self.raise_interval.min(self.interval)
645    }
646}
647
648impl Default for Sizing {
649    fn default() -> Sizing {
650        Sizing {
651            margin: 15,
652            min_jobs: 10,
653            auto: false,
654            interval: Duration::from_hours(1),
655            raise_interval: Duration::from_hours(1),
656            max_step: 50,
657        }
658    }
659}
660
661/// `[web]`: the dashboard behind a reverse proxy, such as `tailscale serve`. Read when the daemon starts.
662#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
663#[serde(deny_unknown_fields, default)]
664pub struct Web {
665    /// The origins, besides the dashboard's own loopback address, whose pages may write: pause, reseed, save the
666    /// config. Default none.
667    pub origins: Vec<Origin>,
668    /// The one of `origins` that `runnerctl web` opens the dashboard on, such as a tailnet's address. Default none:
669    /// it opens the loopback address.
670    pub open: Option<Origin>,
671}
672
673impl Web {
674    /// Refuses an `open` that is not one of `origins`.
675    fn validate(&self) -> Result<(), ConfigError> {
676        match &self.open {
677            Some(open) if !self.origins.contains(open) => {
678                Err(ConfigError::OpenNotListed { open: open.clone() })
679            }
680            _ => Ok(()),
681        }
682    }
683}
684
685/// `[github]`: what runnerd may ask of GitHub on the owner's behalf (ADR 0134). A config with it is one the daemons
686/// before 0.2 refuse.
687#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
688#[serde(deny_unknown_fields, default)]
689pub struct Github {
690    /// Whether `POST /v1/jobs/{id}/cancel` and `POST /v1/jobs/{id}/rerun` may cancel a job's run or re-run it on
691    /// GitHub. Default on; off, both answer 403. A forced pause cancels runs either way.
692    pub job_actions: bool,
693}
694
695impl Default for Github {
696    fn default() -> Github {
697        Github { job_actions: true }
698    }
699}
700
701/// `[notify]`: where the daemon sends notifications (ADR 0051). Without targets nothing leaves the machine.
702#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
703#[serde(deny_unknown_fields, default)]
704pub struct Notify {
705    /// When the digest goes to the targets that want it, in the host's local time. Default `09:00`.
706    pub digest_at: DayTime,
707    /// The targets, each `[[notify.targets]]`.
708    pub targets: Vec<NotifyTarget>,
709}
710
711impl Default for Notify {
712    fn default() -> Notify {
713        Notify {
714            digest_at: DayTime { minutes: 9 * 60 },
715            targets: Vec::new(),
716        }
717    }
718}
719
720impl Notify {
721    /// Refuses a target without a name, with a name another has, with a URL that is not `http` or `https`, or with
722    /// a header that cannot be sent. Says which target and which field, never the URL or a header's value.
723    fn validate(&self) -> Result<(), ConfigError> {
724        let refuse = |target: &str, why: &'static str| {
725            Err(ConfigError::Notify {
726                target: target.to_owned(),
727                why,
728            })
729        };
730        for (index, target) in self.targets.iter().enumerate() {
731            let name = target.name.as_str();
732            if name.trim().is_empty() || name.chars().any(char::is_control) {
733                return refuse(name, "name is empty");
734            }
735            if self.targets[..index].iter().any(|other| other.name == name) {
736                return refuse(name, "another target has this name");
737            }
738            let url = target.url.expose();
739            if url == SECRET_SHOWN {
740                return refuse(
741                    name,
742                    "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
743                );
744            }
745            let Some(rest) = url
746                .strip_prefix("https://")
747                .or_else(|| url.strip_prefix("http://"))
748            else {
749                return refuse(name, "url does not start with https:// or http://");
750            };
751            let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
752            if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
753                return refuse(name, "url is not a URL");
754            }
755            if target.kind == NotifyKind::Ntfy
756                && (path.is_empty() || path.contains(['/', '?', '#']))
757            {
758                return refuse(
759                    name,
760                    "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
761                );
762            }
763            for (header, value) in &target.headers {
764                if value.expose() == SECRET_SHOWN {
765                    return refuse(
766                        name,
767                        "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
768                    );
769                }
770                let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
771                if header.is_empty() || !header.chars().all(token) {
772                    return refuse(name, "a header's name is not a header name");
773                }
774                if value.expose().chars().any(char::is_control) {
775                    return refuse(name, "a header's value has a control character");
776                }
777            }
778            if target.events.contains(&api::NotifyEvent::Test) {
779                return refuse(
780                    name,
781                    "events lists test, which only runnerctl notify test sends",
782                );
783            }
784        }
785        Ok(())
786    }
787}
788
789/// `[[notify.targets]]`: one place notifications go.
790#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
791#[serde(deny_unknown_fields)]
792pub struct NotifyTarget {
793    /// What logs and `runnerctl notify test` call it, such as `phone`; the history keeps where it got to by it.
794    pub name: String,
795    /// What it takes.
796    pub kind: NotifyKind,
797    /// Where it is posted: the webhook's URL, Discord's included, or the ntfy topic's, such as `https://ntfy.sh/<topic>`.
798    /// A secret.
799    pub url: Secret,
800    /// Headers sent with each post, such as an ntfy access token's `Authorization`. Their values are secrets.
801    #[serde(default)]
802    pub headers: BTreeMap<String, Secret>,
803    /// What it is sent. Default `failed`, `lost`, `runner` and `insights`.
804    #[serde(default = "default_notify_events")]
805    pub events: Vec<api::NotifyEvent>,
806}
807
808fn default_notify_events() -> Vec<api::NotifyEvent> {
809    vec![
810        api::NotifyEvent::Failed,
811        api::NotifyEvent::Lost,
812        api::NotifyEvent::Runner,
813        api::NotifyEvent::Insights,
814    ]
815}
816
817/// What a notification target takes.
818#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
819#[serde(rename_all = "kebab-case")]
820pub enum NotifyKind {
821    /// `api::Notification` as JSON.
822    Webhook,
823    /// An ntfy topic: the title, message, priority and link, published as ntfy's JSON.
824    Ntfy,
825    /// A Discord channel's webhook: the title, message and link as one embed, coloured by event, that pings no one.
826    Discord,
827}
828
829/// A value no `Debug` output, log line or error shows: a notification target's URL or header.
830#[derive(Clone, PartialEq, Eq, Deserialize)]
831#[serde(transparent)]
832pub struct Secret(String);
833
834impl Secret {
835    /// The value, for the request that sends it.
836    pub fn expose(&self) -> &str {
837        &self.0
838    }
839}
840
841impl From<&str> for Secret {
842    fn from(value: &str) -> Secret {
843        Secret(value.to_owned())
844    }
845}
846
847impl fmt::Debug for Secret {
848    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
849        f.write_str("<secret>")
850    }
851}
852
853/// A time of day, `HH:MM`, 24-hour.
854#[derive(Debug, Clone, Copy, PartialEq, Eq)]
855pub struct DayTime {
856    /// Minutes after midnight.
857    pub minutes: u32,
858}
859
860impl FromStr for DayTime {
861    type Err = String;
862
863    fn from_str(text: &str) -> Result<DayTime, String> {
864        let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
865        let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
866        let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
867        if !digits(hour) || !digits(minute) {
868            return Err(invalid());
869        }
870        let (hour, minute): (u32, u32) = (
871            hour.parse().map_err(|_| invalid())?,
872            minute.parse().map_err(|_| invalid())?,
873        );
874        if hour > 23 || minute > 59 {
875            return Err(invalid());
876        }
877        Ok(DayTime {
878            minutes: hour * 60 + minute,
879        })
880    }
881}
882
883impl fmt::Display for DayTime {
884    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
885        write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
886    }
887}
888
889impl<'de> Deserialize<'de> for DayTime {
890    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
891        String::deserialize(deserializer)?
892            .parse()
893            .map_err(de::Error::custom)
894    }
895}
896
897/// An origin as a browser sends it in `Origin`: `https://host[:port]`, or `http://` for a loopback host, in lower
898/// case, without the scheme's default port, and an address written as the URL standard serializes it. Nothing after
899/// the port: no path, query or user.
900#[derive(Debug, Clone, PartialEq, Eq)]
901pub struct Origin(String);
902
903impl Origin {
904    /// The origin as a browser sends it, such as `https://host.example.ts.net:8443`.
905    pub fn as_str(&self) -> &str {
906        &self.0
907    }
908}
909
910impl FromStr for Origin {
911    type Err = String;
912
913    fn from_str(s: &str) -> Result<Origin, String> {
914        let invalid = |why: &str| {
915            format!(
916                "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
917                 \"https://host.example.ts.net:8443\""
918            )
919        };
920        let (scheme, rest) = s
921            .split_once("://")
922            .ok_or_else(|| invalid("it has no scheme"))?;
923        let scheme = scheme.to_ascii_lowercase();
924        if rest.contains(['/', '?', '#']) {
925            return Err(invalid("it has a path"));
926        }
927        if rest.contains('@') {
928            return Err(invalid("it names a user"));
929        }
930        let (host, port) = match rest.strip_prefix('[') {
931            Some(v6) => {
932                let (address, after) = v6
933                    .split_once(']')
934                    .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
935                let port = match after {
936                    "" => None,
937                    _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
938                };
939                (format!("[{address}]"), port)
940            }
941            None => match rest.split_once(':') {
942                Some((host, port)) => (host.to_owned(), Some(port)),
943                None => (rest.to_owned(), None),
944            },
945        };
946        let host = host.to_ascii_lowercase();
947        let not_a_host = || invalid("its host is not a name or an address");
948        // An address is kept as a browser serializes it, or it would never match the `Origin` the browser sends.
949        let (host, loopback) =
950            if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
951                let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
952                (format!("[{}]", ipv6_origin(address)), address.is_loopback())
953            } else {
954                if host.is_empty()
955                    || !host
956                        .chars()
957                        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
958                {
959                    return Err(not_a_host());
960                }
961                // As the URL standard reads a host: one whose last label is a number is an IPv4 address.
962                let name = host.strip_suffix('.').unwrap_or(&host);
963                let last = name.rsplit('.').next().unwrap_or_default();
964                let numeric = !last.is_empty()
965                    && (last.bytes().all(|byte| byte.is_ascii_digit())
966                        || last
967                            .strip_prefix("0x")
968                            .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
969                if numeric {
970                    let address: Ipv4Addr = name.parse().map_err(|_| {
971                        invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
972                    })?;
973                    (address.to_string(), address.is_loopback())
974                } else {
975                    let loopback = host == "localhost";
976                    (host, loopback)
977                }
978            };
979        let port = port
980            .map(|port| {
981                Some(port)
982                    .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
983                    .and_then(|port| port.parse::<u16>().ok())
984                    .filter(|port| *port != 0)
985                    .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
986            })
987            .transpose()?;
988        let default_port = match scheme.as_str() {
989            "https" => 443,
990            "http" if loopback => 80,
991            "http" => {
992                return Err(invalid(
993                    "http is for a loopback host only, since the session cookie would cross the network in \
994                     the clear; use https",
995                ));
996            }
997            _ => return Err(invalid("its scheme is not https")),
998        };
999        Ok(Origin(match port {
1000            Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
1001            _ => format!("{scheme}://{host}"),
1002        }))
1003    }
1004}
1005
1006/// An IPv6 address as the URL standard serializes it: lower-case hex pieces, the first longest run of two or more
1007/// zero pieces as `::`, and no dotted IPv4 tail.
1008fn ipv6_origin(address: Ipv6Addr) -> String {
1009    let pieces = address.segments();
1010    let (mut zeros_at, mut zeros) = (0, 0);
1011    let mut at = 0;
1012    while at < pieces.len() {
1013        let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
1014        if run > zeros {
1015            (zeros_at, zeros) = (at, run);
1016        }
1017        at += run.max(1);
1018    }
1019    let hex = |pieces: &[u16]| {
1020        pieces
1021            .iter()
1022            .map(|piece| format!("{piece:x}"))
1023            .collect::<Vec<_>>()
1024            .join(":")
1025    };
1026    if zeros < 2 {
1027        return hex(&pieces);
1028    }
1029    format!(
1030        "{}::{}",
1031        hex(&pieces[..zeros_at]),
1032        hex(&pieces[zeros_at + zeros..])
1033    )
1034}
1035
1036impl fmt::Display for Origin {
1037    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1038        f.write_str(&self.0)
1039    }
1040}
1041
1042impl<'de> Deserialize<'de> for Origin {
1043    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1044        String::deserialize(deserializer)?
1045            .parse()
1046            .map_err(de::Error::custom)
1047    }
1048}
1049
1050/// Where a config's text stops parsing ([`Config::parse_error`]).
1051#[derive(Debug, Clone, PartialEq, Eq)]
1052pub struct ParseError {
1053    /// The line, counted from 1.
1054    pub line: usize,
1055    /// What is wrong there, as the parser says it.
1056    pub message: String,
1057}
1058
1059/// Why a config cannot be used.
1060#[derive(Debug, thiserror::Error)]
1061pub enum ConfigError {
1062    /// The file cannot be read.
1063    #[error("cannot read {}: {source}", path.display())]
1064    Read {
1065        /// The config file.
1066        path: PathBuf,
1067        /// Why reading it failed.
1068        source: std::io::Error,
1069    },
1070    /// The file is not valid TOML or does not match the config's shape.
1071    #[error(transparent)]
1072    Parse(#[from] toml::de::Error),
1073    /// A repository names a profile `[profiles]` does not define.
1074    #[error(
1075        "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
1076        defined_profiles(.defined)
1077    )]
1078    UnknownProfile {
1079        /// The repository's `owner/name`.
1080        repository: String,
1081        /// The profile it names.
1082        profile: String,
1083        /// The profiles `[profiles]` defines.
1084        defined: Vec<String>,
1085    },
1086    /// A repository has no labels, or an empty one.
1087    #[error("repository {repository} needs at least one non-empty label")]
1088    MissingLabels {
1089        /// The repository's `owner/name`.
1090        repository: String,
1091    },
1092    /// Two `[[repositories]]` entries of one repository use the same profile.
1093    #[error(
1094        "repository {repository} has two entries with profile {profile:?}; give each of its pools its own profile"
1095    )]
1096    PoolProfileTwice {
1097        /// The repository's `owner/name`.
1098        repository: String,
1099        /// The profile both entries name.
1100        profile: String,
1101    },
1102    /// Two `[[repositories]]` entries of one repository share a label, so a job asking only for labels they share
1103    /// could run in either.
1104    #[error(
1105        "repository {repository}'s pools in {first} and {second} both have the label {label:?}, so a job asking for \
1106         it could land in either; give each pool labels of its own, such as only {second:?}'s own label for the \
1107         lighter one"
1108    )]
1109    PoolLabelsOverlap {
1110        /// The repository's `owner/name`.
1111        repository: String,
1112        /// The profile of the first entry.
1113        first: String,
1114        /// The profile of the later entry.
1115        second: String,
1116        /// A label both have.
1117        label: String,
1118    },
1119    /// Two `[[repositories]]` entries of one repository say differently whose runs write to the build cache.
1120    #[error(
1121        "repository {repository}'s entries set build_cache_writers differently; whose runs may write to the build \
1122         cache is the repository's, so set the same in each"
1123    )]
1124    PoolWritersDiffer {
1125        /// The repository's `owner/name`.
1126        repository: String,
1127    },
1128    /// `unsafe-dind` without `allow_unsafe = true`.
1129    #[error(
1130        "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
1131         set allow_unsafe = true under [host] to use it anyway"
1132    )]
1133    UnsafeNotAllowed,
1134    /// A limit is 0.
1135    #[error("{field} is 0; set it above 0")]
1136    ZeroLimit {
1137        /// The setting, such as `profiles.app.cpus`.
1138        field: String,
1139    },
1140    /// `cpu_overcommit` is not a number above 0 and at most `MAX_CPU_OVERCOMMIT`.
1141    #[error(
1142        "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
1143    )]
1144    CpuOvercommit {
1145        /// The value set.
1146        value: f64,
1147    },
1148    /// `[web] open` is not one of `[web] origins`.
1149    #[error(
1150        "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
1151    )]
1152    OpenNotListed {
1153        /// The origin `open` names.
1154        open: Origin,
1155    },
1156    /// A profile's `memory` or `slots` are outside its `min_memory`, `max_memory` or `max_slots`, or its
1157    /// `min_memory` is above its `max_memory`.
1158    #[error("profiles.{profile}: {why}")]
1159    SizingBounds {
1160        /// The profile.
1161        profile: String,
1162        /// Which bound, such as `memory is above its max_memory`.
1163        why: &'static str,
1164    },
1165    /// A `[[notify.targets]]` entry cannot be used. Never says its URL or a header's value.
1166    #[error("notify target {target:?}: {why}")]
1167    Notify {
1168        /// The target's name.
1169        target: String,
1170        /// What is wrong, such as `url does not start with https:// or http://`.
1171        why: &'static str,
1172    },
1173    /// `[host] memory_ceiling` is under one runner of a profile and the mirror, so that profile could never start.
1174    #[error(
1175        "host.memory_ceiling is {ceiling}, under the {needs} one runner of {profile} and the mirror take; set it \
1176         to the most memory the host can grow to, such as the VM's maximum"
1177    )]
1178    CeilingUnderProfile {
1179        /// The profile.
1180        profile: String,
1181        /// Its `memory` and the mirror's.
1182        needs: ByteSize,
1183        /// The ceiling set.
1184        ceiling: ByteSize,
1185    },
1186    /// The caps add up past `u64::MAX` bytes.
1187    #[error("slots × memory over all profiles plus the mirror is too large to add up")]
1188    BudgetOverflow,
1189    /// Measured admission, the default, with a slice that has no `MemoryMax` to admit against.
1190    #[error(
1191        "measured admission, the default, admits runners against the slice {slice}'s MemoryMax, and it has none; \
1192         install a unit file for it (`runnerd doctor` prints one), or set [host] admission = \"caps\""
1193    )]
1194    MeasuredWithoutMemoryMax {
1195        /// The slice runners run in.
1196        slice: String,
1197    },
1198    /// Under `admission = "measured"`, one runner of a profile and the mirror do not fit `MemoryMax`.
1199    #[error(
1200        "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
1201         its `memory` or raise the slice's MemoryMax"
1202    )]
1203    ProfileOverBudget {
1204        /// The profile.
1205        profile: String,
1206        /// Its `memory` and the mirror's.
1207        needs: ByteSize,
1208        /// The slice runners run in.
1209        slice: String,
1210        /// The slice's `MemoryMax`.
1211        memory_max: ByteSize,
1212    },
1213    /// The caps add up to more than the slice's `MemoryMax`.
1214    #[error(
1215        "slots × memory over all profiles plus the mirror is {total}, \
1216         the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
1217         MemoryMax (`runnerd doctor` prints its unit)"
1218    )]
1219    OverBudget {
1220        /// What every runner and the mirror may use at once.
1221        total: ByteSize,
1222        /// The slice runners run in.
1223        slice: String,
1224        /// The slice's `MemoryMax`.
1225        memory_max: ByteSize,
1226    },
1227}
1228
1229fn defined_profiles(defined: &[String]) -> String {
1230    if defined.is_empty() {
1231        "it defines none".to_owned()
1232    } else {
1233        format!("defined: {}", defined.join(", "))
1234    }
1235}
1236
1237/// What a valid config should still fix.
1238#[derive(Debug, Clone, PartialEq, Eq)]
1239pub enum Warning {
1240    /// The slice has no unit file, so systemd creates it without limits.
1241    UnlimitedSlice {
1242        /// The slice runners run in.
1243        slice: String,
1244        /// What every runner and the mirror may use at once.
1245        total: ByteSize,
1246    },
1247}
1248
1249impl fmt::Display for Warning {
1250    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1251        match self {
1252            Warning::UnlimitedSlice { slice, total } => write!(
1253                f,
1254                "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
1255                 install a unit file for it"
1256            ),
1257        }
1258    }
1259}
1260
1261impl Config {
1262    /// Reads and parses a config file.
1263    pub fn load(path: &Path) -> Result<Config, ConfigError> {
1264        let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
1265            path: path.to_owned(),
1266            source,
1267        })?;
1268        Config::parse(&text)
1269    }
1270
1271    /// Parses a config from TOML text.
1272    pub fn parse(text: &str) -> Result<Config, ConfigError> {
1273        Ok(toml::from_str(text)?)
1274    }
1275
1276    /// Why a save from a diff is refused when the text changed after the diff was shown: the save would write lines
1277    /// nobody reviewed.
1278    pub fn unreviewed() -> &'static str {
1279        "not saved: the text changed after its changes were shown; review them again"
1280    }
1281
1282    /// Where `text` stops parsing as a config: the line, counted from 1, and the parser's words without its own
1283    /// excerpt of the text, for an editor that marks the line. `None` when it parses, and for an error the parser
1284    /// places nowhere.
1285    pub fn parse_error(text: &str) -> Option<ParseError> {
1286        let Err(error) = toml::from_str::<Config>(text) else {
1287            return None;
1288        };
1289        let span = error.span()?;
1290        let before = text.get(..span.start)?;
1291        Some(ParseError {
1292            line: before.matches('\n').count() + 1,
1293            message: error.message().trim().to_owned(),
1294        })
1295    }
1296
1297    /// Parses `text` and validates it against the `MemoryMax` that `memory_max` reads for the slice it names:
1298    /// what `runnerd check-config` prints and `POST /v1/config/check` answers. Returns the config if it is valid.
1299    pub fn check(
1300        text: &str,
1301        memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
1302    ) -> (Option<Config>, api::ConfigCheck) {
1303        let config = match Config::parse(text) {
1304            Ok(config) => config,
1305            Err(error) => return (None, budget::invalid(error.to_string(), None, None)),
1306        };
1307        let max = match memory_max(&config.host.slice) {
1308            Ok(max) => max,
1309            Err(error) => {
1310                return (
1311                    None,
1312                    budget::invalid(error.to_string(), Some(&config), None),
1313                );
1314            }
1315        };
1316        let check = config.check_against(max);
1317        if check.valid {
1318            (Some(config), check)
1319        } else {
1320            (None, check)
1321        }
1322    }
1323
1324    /// The mirror's memory cap, 0 when it is disabled.
1325    pub fn mirror_memory(&self) -> u64 {
1326        if self.caches.mirror.enabled {
1327            self.caches.mirror.memory.0
1328        } else {
1329            0
1330        }
1331    }
1332
1333    /// The memory every runner and the mirror may use at once.
1334    pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1335        let mirror = self.mirror_memory();
1336        self.profiles
1337            .values()
1338            .try_fold(mirror, |total, p| {
1339                p.memory
1340                    .0
1341                    .checked_mul(u64::from(p.slots))?
1342                    .checked_add(total)
1343            })
1344            .map(ByteSize)
1345            .ok_or(ConfigError::BudgetOverflow)
1346    }
1347
1348    /// The memory `[host] admission` must fit in `MemoryMax`: every cap at once under caps; under measured, the
1349    /// largest single runner with the mirror, since `slots` there only bounds how many run at once.
1350    pub fn memory_needed(&self) -> Result<ByteSize, ConfigError> {
1351        match self.host.admission {
1352            Admission::Caps => self.memory_caps(),
1353            Admission::Measured => {
1354                let largest = self
1355                    .profiles
1356                    .values()
1357                    .map(|p| p.memory.0)
1358                    .max()
1359                    .unwrap_or(0);
1360                largest
1361                    .checked_add(self.mirror_memory())
1362                    .map(ByteSize)
1363                    .ok_or(ConfigError::BudgetOverflow)
1364            }
1365        }
1366    }
1367
1368    /// The most `memory` one runner of any profile may have under `[host] memory_ceiling`: the ceiling less the
1369    /// mirror while it is enabled, as no host of that size starts more beside it. `None` without a ceiling. The
1370    /// config check refuses a profile above it, and auto sizing raises none past it (ADR 0086).
1371    pub fn runner_ceiling(&self) -> Option<u64> {
1372        self.host
1373            .memory_ceiling
1374            .map(|ceiling| ceiling.0.saturating_sub(self.mirror_memory()))
1375    }
1376
1377    /// Refuses a `[host] memory_ceiling` under one runner of the largest profile beside the mirror
1378    /// (`runner_ceiling`): no host of that size starts one, so it is a typo, such as `24M` for `24G`. Whether the
1379    /// host's `MemTotal` is already past it, and what the margin leaves, `runnerd doctor` says, as the config check
1380    /// does not read the host.
1381    fn check_memory_ceiling(&self) -> Result<(), ConfigError> {
1382        let (Some(ceiling), Some(most)) = (self.host.memory_ceiling, self.runner_ceiling()) else {
1383            return Ok(());
1384        };
1385        let largest = self
1386            .profiles
1387            .iter()
1388            .max_by_key(|(_, profile)| profile.memory.0);
1389        match largest {
1390            Some((profile, largest)) if largest.memory.0 > most => {
1391                Err(ConfigError::CeilingUnderProfile {
1392                    profile: profile.clone(),
1393                    needs: ByteSize(largest.memory.0.saturating_add(self.mirror_memory())),
1394                    ceiling,
1395                })
1396            }
1397            _ => Ok(()),
1398        }
1399    }
1400
1401    /// Refuses a profile outside the bounds auto sizing keeps it in, or whose bounds cross.
1402    fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1403        for (name, p) in &self.profiles {
1404            let memory = p.memory.0;
1405            let field = if p
1406                .min_memory
1407                .zip(p.max_memory)
1408                .is_some_and(|(min, max)| min.0 > max.0)
1409            {
1410                "min_memory is above its max_memory"
1411            } else if p.min_memory.is_some_and(|min| memory < min.0) {
1412                "memory is below its min_memory"
1413            } else if p.max_memory.is_some_and(|max| memory > max.0) {
1414                "memory is above its max_memory"
1415            } else if p.max_slots.is_some_and(|max| p.slots > max) {
1416                "slots are above its max_slots"
1417            } else {
1418                continue;
1419            };
1420            return Err(ConfigError::SizingBounds {
1421                profile: name.clone(),
1422                why: field,
1423            });
1424        }
1425        Ok(())
1426    }
1427
1428    /// Checks that each repository's pools tell its jobs apart: each in its own profile, with labels no other of its
1429    /// pools has, so at most one pool matches a job; and that they agree on whose runs write to the build cache.
1430    fn check_pools(&self) -> Result<(), ConfigError> {
1431        for (at, first) in self.repositories.iter().enumerate() {
1432            for second in self.repositories[at + 1..]
1433                .iter()
1434                .filter(|second| second.name == first.name)
1435            {
1436                if second.profile == first.profile {
1437                    return Err(ConfigError::PoolProfileTwice {
1438                        repository: first.name.clone(),
1439                        profile: first.profile.clone(),
1440                    });
1441                }
1442                if let Some(label) = first.labels.iter().find(|label| {
1443                    second
1444                        .labels
1445                        .iter()
1446                        .any(|other| other.eq_ignore_ascii_case(label))
1447                }) {
1448                    return Err(ConfigError::PoolLabelsOverlap {
1449                        repository: first.name.clone(),
1450                        first: first.profile.clone(),
1451                        second: second.profile.clone(),
1452                        label: label.clone(),
1453                    });
1454                }
1455                if second.build_cache_writers != first.build_cache_writers {
1456                    return Err(ConfigError::PoolWritersDiffer {
1457                        repository: first.name.clone(),
1458                    });
1459                }
1460            }
1461        }
1462        Ok(())
1463    }
1464
1465    /// The pool of `repository` whose labels match a job asking for `labels`: every label it asks for is one of
1466    /// the pool's. `None` when no pool of it does.
1467    pub fn pool(&self, repository: &str, labels: &[String]) -> Option<&Repository> {
1468        self.repositories
1469            .iter()
1470            .find(|pool| pool.name == repository && labels_match(labels, &pool.labels))
1471    }
1472
1473    /// The pool of `repository` in `profile`, whose runners start in that profile's slots.
1474    pub fn pool_in(&self, repository: &str, profile: &str) -> Option<&Repository> {
1475        self.repositories
1476            .iter()
1477            .find(|pool| pool.name == repository && pool.profile == profile)
1478    }
1479
1480    /// The GitHub-hosted runner each profile's jobs are priced at (ADR 0072).
1481    pub fn pricing(&self) -> api::Pricing {
1482        api::Pricing {
1483            profiles: self
1484                .profiles
1485                .iter()
1486                .filter_map(|(name, profile)| Some((name.clone(), profile.priced_as?)))
1487                .collect(),
1488            default_sku: api::Sku::standard_for(std::env::consts::ARCH),
1489        }
1490    }
1491
1492    /// The repositories served, each once, in the order the config first names them.
1493    pub fn repository_names(&self) -> Vec<String> {
1494        let mut names: Vec<String> = Vec::new();
1495        for repository in &self.repositories {
1496            if !names.contains(&repository.name) {
1497                names.push(repository.name.clone());
1498            }
1499        }
1500        names
1501    }
1502
1503    /// Checks the config against the slice's `MemoryMax` (`None` when the slice is unlimited).
1504    pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1505        if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1506            return Err(ConfigError::UnsafeNotAllowed);
1507        }
1508        let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1509            [
1510                (p.memory.0 == 0, format!("profiles.{name}.memory")),
1511                (p.cpus == 0, format!("profiles.{name}.cpus")),
1512                (p.slots == 0, format!("profiles.{name}.slots")),
1513                (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1514            ]
1515        });
1516        let mirror = &self.caches.mirror;
1517        let mirror_zero_limits = [
1518            (
1519                mirror.enabled && mirror.memory.0 == 0,
1520                "caches.mirror.memory".to_owned(),
1521            ),
1522            (
1523                mirror.enabled && mirror.cpus == 0,
1524                "caches.mirror.cpus".to_owned(),
1525            ),
1526            (
1527                self.host.poll_interval.is_zero(),
1528                "host.poll_interval".to_owned(),
1529            ),
1530            (
1531                self.host.memory_ceiling == Some(ByteSize(0)),
1532                "host.memory_ceiling".to_owned(),
1533            ),
1534            (self.records.max_count == 0, "records.max_count".to_owned()),
1535            (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1536            (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1537            (
1538                self.sizing.raise_interval.is_zero(),
1539                "sizing.raise_interval".to_owned(),
1540            ),
1541            (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1542            (
1543                self.records.max_total.0 == 0,
1544                "records.max_total".to_owned(),
1545            ),
1546        ];
1547        if let Some((_, field)) = zero_limits
1548            .chain(mirror_zero_limits)
1549            .find(|(zero, _)| *zero)
1550        {
1551            return Err(ConfigError::ZeroLimit { field });
1552        }
1553        self.check_sizing_bounds()?;
1554        self.caches.build.validate()?;
1555        self.host.check_cpu_overcommit()?;
1556        self.check_memory_ceiling()?;
1557        self.web.validate()?;
1558        self.notify.validate()?;
1559        for repository in &self.repositories {
1560            if !self.profiles.contains_key(&repository.profile) {
1561                return Err(ConfigError::UnknownProfile {
1562                    repository: repository.name.clone(),
1563                    profile: repository.profile.clone(),
1564                    defined: self.profiles.keys().cloned().collect(),
1565                });
1566            }
1567            if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1568            {
1569                return Err(ConfigError::MissingLabels {
1570                    repository: repository.name.clone(),
1571                });
1572            }
1573        }
1574        self.check_pools()?;
1575        self.check_budget(memory_max)
1576    }
1577
1578    /// Holds what `[host] admission` needs to the slice's `MemoryMax`; warns when the slice has none to hold caps to.
1579    fn check_budget(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1580        let total = self.memory_caps()?;
1581        if self.host.admission == Admission::Measured {
1582            let Some(memory_max) = memory_max else {
1583                return Err(ConfigError::MeasuredWithoutMemoryMax {
1584                    slice: self.host.slice.clone(),
1585                });
1586            };
1587            let mirror = self.mirror_memory();
1588            if let Some((profile, needs)) = self
1589                .profiles
1590                .iter()
1591                .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1592                .find(|(_, needs)| *needs > memory_max.0)
1593            {
1594                return Err(ConfigError::ProfileOverBudget {
1595                    profile: profile.clone(),
1596                    needs: ByteSize(needs),
1597                    slice: self.host.slice.clone(),
1598                    memory_max,
1599                });
1600            }
1601            // Without a profile, the mirror alone must fit: the caps sum, which is all `memory_needed` holds.
1602            let needed = self.memory_needed()?;
1603            if needed > memory_max {
1604                return Err(ConfigError::OverBudget {
1605                    total: needed,
1606                    slice: self.host.slice.clone(),
1607                    memory_max,
1608                });
1609            }
1610            return Ok(Vec::new());
1611        }
1612        match memory_max {
1613            Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1614                total,
1615                slice: self.host.slice.clone(),
1616                memory_max,
1617            }),
1618            Some(_) => Ok(Vec::new()),
1619            None => Ok(vec![Warning::UnlimitedSlice {
1620                slice: self.host.slice.clone(),
1621                total,
1622            }]),
1623        }
1624    }
1625}
1626
1627/// Why `resize_profile` cannot change a config.
1628#[derive(Debug, thiserror::Error)]
1629pub enum ResizeError {
1630    /// The text is not valid TOML.
1631    #[error(transparent)]
1632    Parse(#[from] toml_edit::TomlError),
1633    /// `[profiles]` does not define the profile.
1634    #[error("the config has no profile {0:?} under [profiles]")]
1635    NoProfile(String),
1636}
1637
1638/// `text` with the profile's `memory` and `slots` replaced, and its `cpus` when `resize` has them, everything else as
1639/// written, comments included: what applying a sizing suggestion sends to `PUT /v1/config`, which validates it.
1640pub fn resize_profile(
1641    text: &str,
1642    profile: &str,
1643    resize: api::Resize,
1644) -> Result<String, ResizeError> {
1645    let mut document: toml_edit::DocumentMut = text.parse()?;
1646    let table = document
1647        .get_mut("profiles")
1648        .and_then(|profiles| profiles.get_mut(profile))
1649        .and_then(toml_edit::Item::as_table_like_mut)
1650        .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1651    let values = [
1652        (
1653            "memory",
1654            toml_edit::Value::from(ByteSize(resize.memory).exact()),
1655        ),
1656        ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1657    ];
1658    let cpus = resize
1659        .cpus
1660        .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1661    for (key, mut value) in values.into_iter().chain(cpus) {
1662        // The comment after the old value stays after the new one.
1663        if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1664            *value.decor_mut() = old.decor().clone();
1665        }
1666        table.insert(key, toml_edit::Item::Value(value));
1667    }
1668    Ok(document.to_string())
1669}
1670
1671/// `profile`'s `memory`, `slots` and `cpus` in the config `text`, as a sizing start changes them from; `None` when
1672/// `text` is not a config or has no such profile.
1673pub fn profile_sizes(text: &str, profile: &str) -> Option<api::Resize> {
1674    let config = Config::parse(text).ok()?;
1675    let sizes = config.profiles.get(profile)?;
1676    Some(api::Resize {
1677        memory: sizes.memory.0,
1678        slots: sizes.slots,
1679        cpus: Some(sizes.cpus),
1680    })
1681}
1682
1683/// Where `resize_profile` wrote `resize` in `text`: the bytes from the first of the profile's `memory`, `slots` and,
1684/// when `resize` has them, `cpus` settings to the end of the last, so a client can select them. `None` when `text` is
1685/// not TOML or the profile sets none of them.
1686pub fn resized_at(
1687    text: &str,
1688    profile: &str,
1689    resize: api::Resize,
1690) -> Option<std::ops::Range<usize>> {
1691    let document = toml_edit::Document::parse(text).ok()?;
1692    let table = document
1693        .as_table()
1694        .get("profiles")?
1695        .get(profile)?
1696        .as_table_like()?;
1697    let cpus = resize.cpus.map(|_| "cpus");
1698    ["memory", "slots"]
1699        .into_iter()
1700        .chain(cpus)
1701        .filter_map(|key| {
1702            let (key, item) = table.get_key_value(key)?;
1703            Some(key.span()?.start..item.span()?.end)
1704        })
1705        .reduce(|first, next| first.start.min(next.start)..first.end.max(next.end))
1706}
1707
1708/// What `GET /v1/config` shows in place of a notification target's URL and header values (ADR 0051).
1709pub const SECRET_SHOWN: &str = "<secret>";
1710
1711/// `text` with each `[[notify.targets]]` entry's `url` and header values shown as [`SECRET_SHOWN`], everything else
1712/// as written: what `GET /v1/config` answers, so no client, the dashboard's browsers included, holds them. `None`
1713/// when `text` is not TOML.
1714pub fn mask_secrets(text: &str) -> Option<String> {
1715    let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1716    let mut masked = false;
1717    for target in notify_targets(&mut document) {
1718        for (_, value) in secrets_of(target) {
1719            replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1720            masked = true;
1721        }
1722    }
1723    Some(if masked {
1724        document.to_string()
1725    } else {
1726        text.to_owned()
1727    })
1728}
1729
1730/// `text` with each [`SECRET_SHOWN`] in a target's `url` or headers replaced by the value `stored` has for the target
1731/// of the same name, everything else as written: what `PUT /v1/config` and `POST /v1/config/check` read, so a config
1732/// shown masked saves with its secrets. A value with nothing to keep stays [`SECRET_SHOWN`], which `Config::validate`
1733/// refuses. A text that does not parse as a config is answered as sent: a TOML error quotes the line it is on, and
1734/// that line must hold the mask, not the value put back. One that parses still does with the values put back, as they
1735/// are strings where strings were, and `Config::validate` names a target's field, never its value.
1736pub fn keep_secrets(text: &str, stored: &str) -> String {
1737    if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1738        return text.to_owned();
1739    }
1740    let (Ok(mut document), Ok(mut old)) = (
1741        text.parse::<toml_edit::DocumentMut>(),
1742        stored.parse::<toml_edit::DocumentMut>(),
1743    ) else {
1744        return text.to_owned();
1745    };
1746    let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1747    for target in notify_targets(&mut old) {
1748        let Some(name) = target_name(target) else {
1749            continue;
1750        };
1751        let secrets = secrets_of(target)
1752            .into_iter()
1753            .map(|(field, value)| (field, value.clone()))
1754            .collect();
1755        kept.insert(name, secrets);
1756    }
1757    for target in notify_targets(&mut document) {
1758        let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1759            continue;
1760        };
1761        for (field, value) in secrets_of(target) {
1762            if value.as_str() != Some(SECRET_SHOWN) {
1763                continue;
1764            }
1765            if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1766                replace_value(value, old.clone());
1767            }
1768        }
1769    }
1770    document.to_string()
1771}
1772
1773/// Where a target keeps a secret.
1774#[derive(Debug, PartialEq, Eq)]
1775enum SecretField {
1776    Url,
1777    Header(String),
1778}
1779
1780/// The tables of `[[notify.targets]]`, or of an inline `targets = [...]`.
1781fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1782    match document
1783        .get_mut("notify")
1784        .and_then(|notify| notify.get_mut("targets"))
1785    {
1786        Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1787            .iter_mut()
1788            .map(|table| table as &mut dyn toml_edit::TableLike)
1789            .collect(),
1790        Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1791            .iter_mut()
1792            .filter_map(toml_edit::Value::as_inline_table_mut)
1793            .map(|table| table as &mut dyn toml_edit::TableLike)
1794            .collect(),
1795        _ => Vec::new(),
1796    }
1797}
1798
1799fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1800    target
1801        .get("name")
1802        .and_then(toml_edit::Item::as_str)
1803        .map(str::to_owned)
1804}
1805
1806/// A target's `url` and header values.
1807fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1808    let mut found = Vec::new();
1809    for (key, item) in target.iter_mut() {
1810        match key.get() {
1811            "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1812            "headers" => {
1813                let Some(headers) = item.as_table_like_mut() else {
1814                    continue;
1815                };
1816                for (name, item) in headers.iter_mut() {
1817                    let field = SecretField::Header(name.get().to_owned());
1818                    found.extend(item.as_value_mut().map(|value| (field, value)));
1819                }
1820            }
1821            _ => {}
1822        }
1823    }
1824    found
1825}
1826
1827/// Puts `new` in place of `value`, with the spaces and comment around it as they were.
1828fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1829    *new.decor_mut() = value.decor().clone();
1830    *value = new;
1831}
1832
1833/// A config text's revision: a hash of its bytes (64-bit FNV-1a, in hex), the same in every runnerd. `PUT
1834/// /v1/config?revision=` refuses a text sent against another revision than the daemon's.
1835pub fn revision(text: &str) -> String {
1836    let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1837        (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1838    });
1839    format!("{hash:016x}")
1840}
1841
1842/// Reads a slice's `MemoryMax` from systemd; `None` when it is unlimited.
1843pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1844    let output = Command::new("systemctl")
1845        .args(["show", slice, "--property=MemoryMax", "--value"])
1846        .output()?;
1847    if !output.status.success() {
1848        return Err(std::io::Error::other(format!(
1849            "systemctl show {slice} failed: {}",
1850            String::from_utf8_lossy(&output.stderr).trim()
1851        )));
1852    }
1853    let value = String::from_utf8_lossy(&output.stdout);
1854    parse_memory_max(value.trim()).ok_or_else(|| {
1855        std::io::Error::other(format!(
1856            "systemctl show {slice}: unexpected MemoryMax {value:?}"
1857        ))
1858    })
1859}
1860
1861/// The CPU budget, in millicores, of `millicores` of CPUs overcommitted `overcommit` times.
1862pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1863    #[expect(
1864        clippy::cast_precision_loss,
1865        clippy::cast_possible_truncation,
1866        clippy::cast_sign_loss,
1867        reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1868                  most 16"
1869    )]
1870    let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1871    budget
1872}
1873
1874/// Reads the CPUs a slice's processes may use from systemd, in millicores: its `EffectiveCPUs` (or `AllowedCPUs`,
1875/// else the host's CPUs), less when its `CPUQuota` is lower.
1876pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1877    let output = Command::new("systemctl")
1878        .args([
1879            "show",
1880            slice,
1881            "--property=EffectiveCPUs",
1882            "--property=AllowedCPUs",
1883            "--property=CPUQuotaPerSecUSec",
1884        ])
1885        .output()?;
1886    if !output.status.success() {
1887        return Err(std::io::Error::other(format!(
1888            "systemctl show {slice} failed: {}",
1889            String::from_utf8_lossy(&output.stderr).trim()
1890        )));
1891    }
1892    let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1893    parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1894        std::io::Error::other(format!(
1895            "systemctl show {slice}: unexpected CPU properties {:?}",
1896            String::from_utf8_lossy(&output.stdout).trim()
1897        ))
1898    })
1899}
1900
1901/// The CPUs, in millicores, that `systemctl show`'s `EffectiveCPUs`, `AllowedCPUs` and `CPUQuotaPerSecUSec` lines
1902/// leave a slice on a host with `host` CPUs; `None` when a value does not parse.
1903pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1904    let value = |name: &str| {
1905        properties.lines().find_map(|line| {
1906            line.strip_prefix(name)
1907                .and_then(|rest| rest.strip_prefix('='))
1908                .map(str::trim)
1909        })
1910    };
1911    let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
1912        .into_iter()
1913        .flatten()
1914        .find(|cpus| !cpus.is_empty());
1915    let cpus = match set {
1916        Some(set) => cpu_count(set)?,
1917        None => host,
1918    };
1919    let quota = match value("CPUQuotaPerSecUSec") {
1920        None | Some("" | "infinity") => u64::MAX,
1921        Some(quota) => quota_millicores(quota)?,
1922    };
1923    Some(cpus.saturating_mul(1000).min(quota))
1924}
1925
1926/// How many CPUs a set names, as systemd prints it (`0-3 8-11`) or as it is written (`0-3,8,10-11`).
1927fn cpu_count(set: &str) -> Option<u64> {
1928    set.split([',', ' '])
1929        .filter(|range| !range.is_empty())
1930        .try_fold(0u64, |count, range| {
1931            let (first, last) = range.split_once('-').unwrap_or((range, range));
1932            let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
1933            Some(count + last.checked_sub(first)? + 1)
1934        })
1935}
1936
1937/// A `CPUQuotaPerSecUSec` as systemd prints a timespan, such as `800ms`, `2.500000s` or `1min 4s`, as the
1938/// millicores it allows: a second of CPU time each second is 1000.
1939fn quota_millicores(quota: &str) -> Option<u64> {
1940    // Each unit's length in milliseconds, as systemd's `format_timespan` names them.
1941    const UNITS: [(&str, f64); 10] = [
1942        ("y", 31_557_600_000.0),
1943        ("M", 2_629_800_000.0),
1944        ("w", 604_800_000.0),
1945        ("d", 86_400_000.0),
1946        ("h", 3_600_000.0),
1947        ("min", 60_000.0),
1948        ("s", 1000.0),
1949        ("ms", 1.0),
1950        ("us", 0.001),
1951        ("µs", 0.001),
1952    ];
1953    let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
1954        let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
1955        let (number, unit) = part.split_at(at);
1956        let per_unit = UNITS
1957            .iter()
1958            .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
1959        Some(total + number.parse::<f64>().ok()? * per_unit)
1960    })?;
1961    #[allow(
1962        clippy::cast_possible_truncation,
1963        clippy::cast_sign_loss,
1964        reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
1965    )]
1966    (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
1967        .then(|| millicores.round() as u64)
1968}
1969
1970/// Parses a `MemoryMax` value as `systemctl show` prints it: bytes, or `infinity`.
1971pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
1972    if value == "infinity" {
1973        Some(None)
1974    } else {
1975        value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
1976    }
1977}
1978
1979/// A size in bytes, written with a binary unit as Docker and systemd do: `512M`, `9G`, `32.5G`.
1980#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1981pub struct ByteSize(pub u64);
1982
1983const UNITS: [(char, u64); 4] = [
1984    ('T', 1 << 40),
1985    ('G', 1 << 30),
1986    ('M', 1 << 20),
1987    ('K', 1 << 10),
1988];
1989
1990impl FromStr for ByteSize {
1991    type Err = String;
1992
1993    #[allow(
1994        clippy::cast_precision_loss,
1995        clippy::cast_possible_truncation,
1996        clippy::cast_sign_loss,
1997        reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
1998    )]
1999    fn from_str(s: &str) -> Result<Self, Self::Err> {
2000        let invalid = || {
2001            format!(
2002                "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
2003            )
2004        };
2005        let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
2006        let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
2007        let number = &s[..s.len() - 1];
2008        if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
2009            return Err(invalid());
2010        }
2011        let number: f64 = number.parse().map_err(|_| invalid())?;
2012        let bytes = (number * *factor as f64).round();
2013        // `u64::MAX as f64` is 2^64, the first value out of range.
2014        if bytes >= u64::MAX as f64 {
2015            return Err(format!("size {s:?} is too large"));
2016        }
2017        Ok(ByteSize(bytes as u64))
2018    }
2019}
2020
2021impl fmt::Display for ByteSize {
2022    #[allow(
2023        clippy::cast_precision_loss,
2024        reason = "shown with two decimals, far coarser than f64's precision"
2025    )]
2026    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2027        let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
2028            return write!(f, "{}B", self.0);
2029        };
2030        let number = format!("{:.2}", self.0 as f64 / *factor as f64);
2031        let number = number.trim_end_matches('0').trim_end_matches('.');
2032        write!(f, "{number}{unit}")
2033    }
2034}
2035
2036impl ByteSize {
2037    /// The size as a config file writes it, reading back to the byte: `Display`'s form when that reads back as it,
2038    /// else the shortest exact decimal in one of the units, such as `1029.5G` where `Display` says `1.01T`. A size
2039    /// written from it, as auto sizing writes `memory`, is the size the config check then reads.
2040    #[allow(
2041        clippy::cast_precision_loss,
2042        reason = "a unit is a power of two, so the quotient is exact below 2^53 bytes; the parse checks it"
2043    )]
2044    pub fn exact(self) -> String {
2045        let reads_back = |text: &String| text.parse::<ByteSize>() == Ok(self);
2046        let shown = self.to_string();
2047        if reads_back(&shown) {
2048            return shown;
2049        }
2050        let mut shortest: Option<String> = None;
2051        for (unit, factor) in UNITS {
2052            let text = format!("{}{unit}", self.0 as f64 / factor as f64);
2053            if reads_back(&text) && shortest.as_ref().is_none_or(|best| text.len() < best.len()) {
2054                shortest = Some(text);
2055            }
2056        }
2057        shortest.unwrap_or(shown)
2058    }
2059
2060    /// The size rounded to the nearest, for a label too narrow for its `Display`: two significant digits below 10
2061    /// of its unit, whole units above, and the next unit up once it rounds to 1024: `3.3G` for 3.25G, `12G`,
2062    /// `512M`, and `1G` for 1023.5M. Every client's budget bar labels a block with it.
2063    #[allow(
2064        clippy::cast_precision_loss,
2065        reason = "shown with one decimal at most, far coarser than f64's precision"
2066    )]
2067    pub fn short(self) -> String {
2068        let Some(index) = UNITS.iter().position(|(_, factor)| self.0 >= *factor) else {
2069            return format!("{}B", self.0);
2070        };
2071        let (mut unit, factor) = UNITS[index];
2072        let mut value = self.0 as f64 / factor as f64;
2073        if value.round() >= 1024.0
2074            && let Some(larger) = index.checked_sub(1)
2075        {
2076            unit = UNITS[larger].0;
2077            value /= 1024.0;
2078        }
2079        // Halves round up, as `f64::round` does and formatting, which rounds them to even, does not.
2080        let tenths = (value * 10.0).round() / 10.0;
2081        if tenths < 10.0 {
2082            let number = format!("{tenths:.1}");
2083            format!("{}{unit}", number.trim_end_matches(".0"))
2084        } else {
2085            format!("{:.0}{unit}", value.round())
2086        }
2087    }
2088}
2089
2090impl<'de> Deserialize<'de> for ByteSize {
2091    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
2092        String::deserialize(deserializer)?
2093            .parse()
2094            .map_err(de::Error::custom)
2095    }
2096}
2097
2098/// Parses a duration as Go and the registry write it, `30s`, `90m`, `168h`, or in days: `7d`.
2099pub fn parse_duration(s: &str) -> Result<Duration, String> {
2100    let invalid = || {
2101        format!(
2102            "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
2103        )
2104    };
2105    let (number, factor) = match s.chars().last() {
2106        Some('s') => (&s[..s.len() - 1], 1),
2107        Some('m') => (&s[..s.len() - 1], 60),
2108        Some('h') => (&s[..s.len() - 1], 3600),
2109        Some('d') => (&s[..s.len() - 1], 86400),
2110        _ => return Err(invalid()),
2111    };
2112    let number: u64 = number.parse().map_err(|_| invalid())?;
2113    number
2114        .checked_mul(factor)
2115        .map(Duration::from_secs)
2116        .ok_or_else(invalid)
2117}
2118
2119/// Parses an age as the clients' `--since` and `--older-than` take it: `30m`, `12h`, `7d`, `2w`.
2120pub fn parse_age(text: &str) -> Result<Duration, String> {
2121    let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
2122    let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
2123    let (number, unit) = text.split_at(split);
2124    let number: u64 = number.parse().map_err(|_| error())?;
2125    let unit = match unit {
2126        "s" => 1,
2127        "m" => 60,
2128        "h" => 3600,
2129        "d" => 86_400,
2130        "w" => 7 * 86_400,
2131        _ => return Err(error()),
2132    };
2133    Ok(Duration::from_secs(number.saturating_mul(unit)))
2134}
2135
2136fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
2137    parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
2138}
2139
2140#[cfg(test)]
2141mod tests;