1use 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#[derive(Debug, Deserialize)]
23#[serde(deny_unknown_fields)]
24pub struct Config {
25 pub host: Host,
27 pub caches: Caches,
29 pub profiles: BTreeMap<String, Profile>,
31 pub repositories: Vec<Repository>,
34 #[serde(default)]
36 pub history: History,
37 #[serde(default)]
39 pub records: Records,
40 #[serde(default)]
42 pub sizing: Sizing,
43 #[serde(default)]
45 pub web: Web,
46 #[serde(default)]
48 pub notify: Notify,
49}
50
51#[derive(Debug, Deserialize)]
53#[serde(deny_unknown_fields)]
54pub struct Host {
55 pub slice: String,
57 pub isolation: Isolation,
59 #[serde(default)]
61 pub allow_unsafe: bool,
62 #[serde(default = "default_poll_interval", deserialize_with = "duration")]
64 pub poll_interval: Duration,
65 #[serde(default)]
67 pub admission: Admission,
68 #[serde(default)]
71 pub cpu_overcommit: Option<f64>,
72 #[serde(default)]
76 pub memory_ceiling: Option<ByteSize>,
77}
78
79impl Host {
80 fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
82 match self.cpu_overcommit {
83 Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
84 Err(ConfigError::CpuOvercommit { value })
85 }
86 _ => Ok(()),
87 }
88 }
89}
90
91pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
93
94#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
96#[serde(rename_all = "kebab-case")]
97pub enum Admission {
98 #[default]
100 Caps,
101 Measured,
104}
105
106impl Admission {
107 pub fn policy(self) -> api::AdmissionPolicy {
109 match self {
110 Admission::Caps => api::AdmissionPolicy::Caps,
111 Admission::Measured => api::AdmissionPolicy::Measured,
112 }
113 }
114}
115
116fn default_poll_interval() -> Duration {
117 Duration::from_secs(15)
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
122#[serde(rename_all = "kebab-case")]
123pub enum Isolation {
124 Sysbox,
126 UnsafeDind,
128}
129
130#[derive(Debug, Clone, Deserialize)]
132#[serde(deny_unknown_fields)]
133pub struct Caches {
134 pub mirror: Mirror,
136 pub pnpm: CacheLimit,
138 pub maven: CacheLimit,
140 #[serde(default = "default_reseed_at")]
143 pub reseed_at: ByteSize,
144 #[serde(default = "default_snapshot_max")]
147 pub snapshot_max: ByteSize,
148 #[serde(default)]
150 pub build: BuildCache,
151}
152
153#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
156#[serde(deny_unknown_fields, default)]
157pub struct BuildCache {
158 pub enabled: bool,
160 pub max_size: ByteSize,
162 pub branches: Vec<String>,
165 pub directory: Option<PathBuf>,
167 pub actions_cache: bool,
170}
171
172impl BuildCache {
173 pub fn actions_cache_check(&self) -> api::Check {
175 api::Check {
176 name: api::ACTIONS_CACHE.to_owned(),
177 status: api::CheckStatus::Info,
178 detail: if self.enabled {
179 api::actions_cache_place(self.actions_cache)
180 } else {
181 "GitHub's, as the build cache is off"
182 }
183 .to_owned(),
184 fix: None,
185 }
186 }
187
188 fn validate(&self) -> Result<(), ConfigError> {
189 if self.enabled && self.max_size.0 == 0 {
190 return Err(ConfigError::ZeroLimit {
191 field: "caches.build.max_size".to_owned(),
192 });
193 }
194 Ok(())
195 }
196}
197
198impl Default for BuildCache {
199 fn default() -> BuildCache {
200 BuildCache {
201 enabled: true,
202 max_size: ByteSize(10 << 30),
203 branches: Vec::new(),
204 directory: None,
205 actions_cache: true,
206 }
207 }
208}
209
210fn default_reseed_at() -> ByteSize {
211 ByteSize(2 << 30)
212}
213
214fn default_snapshot_max() -> ByteSize {
216 ByteSize(10 << 30)
217}
218
219fn default_mirror_max_size() -> ByteSize {
220 ByteSize(10 << 30)
221}
222
223#[derive(Debug, Clone, Deserialize)]
225#[serde(deny_unknown_fields)]
226pub struct Mirror {
227 pub enabled: bool,
229 pub memory: ByteSize,
231 pub cpus: u32,
233 #[serde(deserialize_with = "duration")]
235 pub ttl: Duration,
236 #[serde(default = "default_mirror_max_size")]
238 pub max_size: ByteSize,
239}
240
241#[derive(Debug, Clone, Deserialize)]
243#[serde(deny_unknown_fields)]
244pub struct CacheLimit {
245 pub max_size: ByteSize,
247}
248
249pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
252
253#[derive(Debug, Clone, Deserialize)]
255#[serde(try_from = "ProfileToml")]
256pub struct Profile {
257 pub image: Image,
259 pub cpus: u32,
261 pub memory: ByteSize,
263 pub slots: u32,
265 pub record_failures: bool,
267 pub min_memory: Option<ByteSize>,
269 pub max_memory: Option<ByteSize>,
271 pub max_slots: Option<u32>,
273 pub priced_as: Option<api::Sku>,
276}
277
278#[derive(Debug, Clone, PartialEq, Eq)]
280pub enum Image {
281 Stage(String),
283 Pulled(String),
286}
287
288impl Image {
289 pub fn reference(&self) -> String {
291 match self {
292 Image::Stage(stage) => format!("runnerd-runner:{stage}"),
293 Image::Pulled(reference) => reference.clone(),
294 }
295 }
296}
297
298#[derive(Deserialize)]
300#[serde(deny_unknown_fields)]
301struct ProfileToml {
302 image_stage: Option<String>,
303 image: Option<String>,
304 cpus: u32,
305 memory: ByteSize,
306 slots: u32,
307 #[serde(default)]
308 record_failures: bool,
309 min_memory: Option<ByteSize>,
310 max_memory: Option<ByteSize>,
311 max_slots: Option<u32>,
312 priced_as: Option<api::Sku>,
313}
314
315impl TryFrom<ProfileToml> for Profile {
316 type Error = String;
317
318 fn try_from(profile: ProfileToml) -> Result<Profile, String> {
319 let image = match (profile.image_stage, profile.image) {
320 (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
321 (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
322 (Some(_), Some(_)) => {
323 return Err(
324 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
325 not both"
326 .to_owned(),
327 );
328 }
329 _ => {
330 return Err(
331 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
332 .to_owned(),
333 );
334 }
335 };
336 Ok(Profile {
337 image,
338 cpus: profile.cpus,
339 memory: profile.memory,
340 slots: profile.slots,
341 record_failures: profile.record_failures,
342 min_memory: profile.min_memory,
343 max_memory: profile.max_memory,
344 max_slots: profile.max_slots,
345 priced_as: profile.priced_as,
346 })
347 }
348}
349
350pub fn labels_match(wanted: &[String], labels: &[String]) -> bool {
354 !wanted.is_empty()
355 && wanted.iter().all(|wanted| {
356 labels
357 .iter()
358 .any(|label| label.eq_ignore_ascii_case(wanted))
359 })
360}
361
362#[derive(Debug, Deserialize)]
365#[serde(deny_unknown_fields)]
366pub struct Repository {
367 pub name: String,
369 pub profile: String,
371 pub labels: Vec<String>,
373 #[serde(default = "default_warm")]
376 pub warm: u32,
377 #[serde(default = "default_true")]
379 pub build_cache: bool,
380 #[serde(default)]
383 pub build_cache_writers: BuildCacheWriters,
384}
385
386#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
388#[serde(rename_all = "kebab-case")]
389pub enum BuildCacheWriters {
390 #[default]
392 DefaultBranch,
393 PullRequests,
395}
396
397fn default_true() -> bool {
398 true
399}
400
401fn default_warm() -> u32 {
402 1
403}
404
405#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
407#[serde(deny_unknown_fields, default)]
408pub struct History {
409 pub database: Option<PathBuf>,
411 #[serde(deserialize_with = "duration")]
413 pub samples: Duration,
414 #[serde(deserialize_with = "duration")]
416 pub logs: Duration,
417 #[serde(deserialize_with = "duration")]
419 pub events: Duration,
420 #[serde(deserialize_with = "duration")]
422 pub jobs: Duration,
423 pub log_max: ByteSize,
425}
426
427impl Default for History {
428 fn default() -> History {
429 History {
430 database: None,
431 samples: Duration::from_hours(7 * 24),
432 logs: Duration::from_hours(30 * 24),
433 events: Duration::from_hours(30 * 24),
434 jobs: Duration::from_hours(90 * 24),
435 log_max: ByteSize(50 << 20),
436 }
437 }
438}
439
440#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
443#[serde(deny_unknown_fields, default)]
444pub struct Records {
445 pub max_total: ByteSize,
447 pub max_count: u32,
449 #[serde(deserialize_with = "duration")]
451 pub max_age: Duration,
452 pub min_free: ByteSize,
454}
455
456impl Default for Records {
457 fn default() -> Records {
458 Records {
459 max_total: ByteSize(20 << 30),
460 max_count: 10,
461 max_age: Duration::from_hours(7 * 24),
462 min_free: ByteSize(10 << 30),
463 }
464 }
465}
466
467#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
470#[serde(deny_unknown_fields, default)]
471pub struct Sizing {
472 pub margin: u32,
475 pub min_jobs: u32,
478 pub auto: bool,
481 #[serde(deserialize_with = "duration")]
484 pub interval: Duration,
485 #[serde(deserialize_with = "duration")]
488 pub raise_interval: Duration,
489 pub max_step: u32,
492}
493
494impl Sizing {
495 pub fn raise_after(&self) -> Duration {
498 self.raise_interval.min(self.interval)
499 }
500}
501
502impl Default for Sizing {
503 fn default() -> Sizing {
504 Sizing {
505 margin: 15,
506 min_jobs: 10,
507 auto: false,
508 interval: Duration::from_hours(1),
509 raise_interval: Duration::from_hours(1),
510 max_step: 50,
511 }
512 }
513}
514
515#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
517#[serde(deny_unknown_fields, default)]
518pub struct Web {
519 pub origins: Vec<Origin>,
522 pub open: Option<Origin>,
525}
526
527impl Web {
528 fn validate(&self) -> Result<(), ConfigError> {
530 match &self.open {
531 Some(open) if !self.origins.contains(open) => {
532 Err(ConfigError::OpenNotListed { open: open.clone() })
533 }
534 _ => Ok(()),
535 }
536 }
537}
538
539#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
541#[serde(deny_unknown_fields, default)]
542pub struct Notify {
543 pub digest_at: DayTime,
545 pub targets: Vec<NotifyTarget>,
547}
548
549impl Default for Notify {
550 fn default() -> Notify {
551 Notify {
552 digest_at: DayTime { minutes: 9 * 60 },
553 targets: Vec::new(),
554 }
555 }
556}
557
558impl Notify {
559 fn validate(&self) -> Result<(), ConfigError> {
562 let refuse = |target: &str, why: &'static str| {
563 Err(ConfigError::Notify {
564 target: target.to_owned(),
565 why,
566 })
567 };
568 for (index, target) in self.targets.iter().enumerate() {
569 let name = target.name.as_str();
570 if name.trim().is_empty() || name.chars().any(char::is_control) {
571 return refuse(name, "name is empty");
572 }
573 if self.targets[..index].iter().any(|other| other.name == name) {
574 return refuse(name, "another target has this name");
575 }
576 let url = target.url.expose();
577 if url == SECRET_SHOWN {
578 return refuse(
579 name,
580 "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
581 );
582 }
583 let Some(rest) = url
584 .strip_prefix("https://")
585 .or_else(|| url.strip_prefix("http://"))
586 else {
587 return refuse(name, "url does not start with https:// or http://");
588 };
589 let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
590 if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
591 return refuse(name, "url is not a URL");
592 }
593 if target.kind == NotifyKind::Ntfy
594 && (path.is_empty() || path.contains(['/', '?', '#']))
595 {
596 return refuse(
597 name,
598 "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
599 );
600 }
601 for (header, value) in &target.headers {
602 if value.expose() == SECRET_SHOWN {
603 return refuse(
604 name,
605 "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
606 );
607 }
608 let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
609 if header.is_empty() || !header.chars().all(token) {
610 return refuse(name, "a header's name is not a header name");
611 }
612 if value.expose().chars().any(char::is_control) {
613 return refuse(name, "a header's value has a control character");
614 }
615 }
616 if target.events.contains(&api::NotifyEvent::Test) {
617 return refuse(
618 name,
619 "events lists test, which only runnerctl notify test sends",
620 );
621 }
622 }
623 Ok(())
624 }
625}
626
627#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
629#[serde(deny_unknown_fields)]
630pub struct NotifyTarget {
631 pub name: String,
633 pub kind: NotifyKind,
635 pub url: Secret,
638 #[serde(default)]
640 pub headers: BTreeMap<String, Secret>,
641 #[serde(default = "default_notify_events")]
643 pub events: Vec<api::NotifyEvent>,
644}
645
646fn default_notify_events() -> Vec<api::NotifyEvent> {
647 vec![
648 api::NotifyEvent::Failed,
649 api::NotifyEvent::Lost,
650 api::NotifyEvent::Runner,
651 api::NotifyEvent::Insights,
652 ]
653}
654
655#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
657#[serde(rename_all = "kebab-case")]
658pub enum NotifyKind {
659 Webhook,
661 Ntfy,
663 Discord,
665}
666
667#[derive(Clone, PartialEq, Eq, Deserialize)]
669#[serde(transparent)]
670pub struct Secret(String);
671
672impl Secret {
673 pub fn expose(&self) -> &str {
675 &self.0
676 }
677}
678
679impl From<&str> for Secret {
680 fn from(value: &str) -> Secret {
681 Secret(value.to_owned())
682 }
683}
684
685impl fmt::Debug for Secret {
686 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
687 f.write_str("<secret>")
688 }
689}
690
691#[derive(Debug, Clone, Copy, PartialEq, Eq)]
693pub struct DayTime {
694 pub minutes: u32,
696}
697
698impl FromStr for DayTime {
699 type Err = String;
700
701 fn from_str(text: &str) -> Result<DayTime, String> {
702 let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
703 let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
704 let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
705 if !digits(hour) || !digits(minute) {
706 return Err(invalid());
707 }
708 let (hour, minute): (u32, u32) = (
709 hour.parse().map_err(|_| invalid())?,
710 minute.parse().map_err(|_| invalid())?,
711 );
712 if hour > 23 || minute > 59 {
713 return Err(invalid());
714 }
715 Ok(DayTime {
716 minutes: hour * 60 + minute,
717 })
718 }
719}
720
721impl fmt::Display for DayTime {
722 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
723 write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
724 }
725}
726
727impl<'de> Deserialize<'de> for DayTime {
728 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
729 String::deserialize(deserializer)?
730 .parse()
731 .map_err(de::Error::custom)
732 }
733}
734
735#[derive(Debug, Clone, PartialEq, Eq)]
739pub struct Origin(String);
740
741impl Origin {
742 pub fn as_str(&self) -> &str {
744 &self.0
745 }
746}
747
748impl FromStr for Origin {
749 type Err = String;
750
751 fn from_str(s: &str) -> Result<Origin, String> {
752 let invalid = |why: &str| {
753 format!(
754 "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
755 \"https://host.example.ts.net:8443\""
756 )
757 };
758 let (scheme, rest) = s
759 .split_once("://")
760 .ok_or_else(|| invalid("it has no scheme"))?;
761 let scheme = scheme.to_ascii_lowercase();
762 if rest.contains(['/', '?', '#']) {
763 return Err(invalid("it has a path"));
764 }
765 if rest.contains('@') {
766 return Err(invalid("it names a user"));
767 }
768 let (host, port) = match rest.strip_prefix('[') {
769 Some(v6) => {
770 let (address, after) = v6
771 .split_once(']')
772 .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
773 let port = match after {
774 "" => None,
775 _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
776 };
777 (format!("[{address}]"), port)
778 }
779 None => match rest.split_once(':') {
780 Some((host, port)) => (host.to_owned(), Some(port)),
781 None => (rest.to_owned(), None),
782 },
783 };
784 let host = host.to_ascii_lowercase();
785 let not_a_host = || invalid("its host is not a name or an address");
786 let (host, loopback) =
788 if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
789 let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
790 (format!("[{}]", ipv6_origin(address)), address.is_loopback())
791 } else {
792 if host.is_empty()
793 || !host
794 .chars()
795 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
796 {
797 return Err(not_a_host());
798 }
799 let name = host.strip_suffix('.').unwrap_or(&host);
801 let last = name.rsplit('.').next().unwrap_or_default();
802 let numeric = !last.is_empty()
803 && (last.bytes().all(|byte| byte.is_ascii_digit())
804 || last
805 .strip_prefix("0x")
806 .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
807 if numeric {
808 let address: Ipv4Addr = name.parse().map_err(|_| {
809 invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
810 })?;
811 (address.to_string(), address.is_loopback())
812 } else {
813 let loopback = host == "localhost";
814 (host, loopback)
815 }
816 };
817 let port = port
818 .map(|port| {
819 Some(port)
820 .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
821 .and_then(|port| port.parse::<u16>().ok())
822 .filter(|port| *port != 0)
823 .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
824 })
825 .transpose()?;
826 let default_port = match scheme.as_str() {
827 "https" => 443,
828 "http" if loopback => 80,
829 "http" => {
830 return Err(invalid(
831 "http is for a loopback host only, since the session cookie would cross the network in \
832 the clear; use https",
833 ));
834 }
835 _ => return Err(invalid("its scheme is not https")),
836 };
837 Ok(Origin(match port {
838 Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
839 _ => format!("{scheme}://{host}"),
840 }))
841 }
842}
843
844fn ipv6_origin(address: Ipv6Addr) -> String {
847 let pieces = address.segments();
848 let (mut zeros_at, mut zeros) = (0, 0);
849 let mut at = 0;
850 while at < pieces.len() {
851 let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
852 if run > zeros {
853 (zeros_at, zeros) = (at, run);
854 }
855 at += run.max(1);
856 }
857 let hex = |pieces: &[u16]| {
858 pieces
859 .iter()
860 .map(|piece| format!("{piece:x}"))
861 .collect::<Vec<_>>()
862 .join(":")
863 };
864 if zeros < 2 {
865 return hex(&pieces);
866 }
867 format!(
868 "{}::{}",
869 hex(&pieces[..zeros_at]),
870 hex(&pieces[zeros_at + zeros..])
871 )
872}
873
874impl fmt::Display for Origin {
875 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
876 f.write_str(&self.0)
877 }
878}
879
880impl<'de> Deserialize<'de> for Origin {
881 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
882 String::deserialize(deserializer)?
883 .parse()
884 .map_err(de::Error::custom)
885 }
886}
887
888#[derive(Debug, thiserror::Error)]
890pub enum ConfigError {
891 #[error("cannot read {}: {source}", path.display())]
893 Read {
894 path: PathBuf,
896 source: std::io::Error,
898 },
899 #[error(transparent)]
901 Parse(#[from] toml::de::Error),
902 #[error(
904 "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
905 defined_profiles(.defined)
906 )]
907 UnknownProfile {
908 repository: String,
910 profile: String,
912 defined: Vec<String>,
914 },
915 #[error("repository {repository} needs at least one non-empty label")]
917 MissingLabels {
918 repository: String,
920 },
921 #[error(
923 "repository {repository} has two entries with profile {profile:?}; give each of its pools its own profile"
924 )]
925 PoolProfileTwice {
926 repository: String,
928 profile: String,
930 },
931 #[error(
934 "repository {repository}'s pools in {first} and {second} both have the label {label:?}, so a job asking for \
935 it could land in either; give each pool labels of its own, such as only {second:?}'s own label for the \
936 lighter one"
937 )]
938 PoolLabelsOverlap {
939 repository: String,
941 first: String,
943 second: String,
945 label: String,
947 },
948 #[error(
950 "repository {repository}'s entries set build_cache_writers differently; whose runs may write to the build \
951 cache is the repository's, so set the same in each"
952 )]
953 PoolWritersDiffer {
954 repository: String,
956 },
957 #[error(
959 "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
960 set allow_unsafe = true under [host] to use it anyway"
961 )]
962 UnsafeNotAllowed,
963 #[error("{field} is 0; set it above 0")]
965 ZeroLimit {
966 field: String,
968 },
969 #[error(
971 "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
972 )]
973 CpuOvercommit {
974 value: f64,
976 },
977 #[error(
979 "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
980 )]
981 OpenNotListed {
982 open: Origin,
984 },
985 #[error("profiles.{profile}: {why}")]
988 SizingBounds {
989 profile: String,
991 why: &'static str,
993 },
994 #[error("notify target {target:?}: {why}")]
996 Notify {
997 target: String,
999 why: &'static str,
1001 },
1002 #[error(
1004 "host.memory_ceiling is {ceiling}, under the {needs} one runner of {profile} and the mirror take; set it \
1005 to the most memory the host can grow to, such as the VM's maximum"
1006 )]
1007 CeilingUnderProfile {
1008 profile: String,
1010 needs: ByteSize,
1012 ceiling: ByteSize,
1014 },
1015 #[error("slots × memory over all profiles plus the mirror is too large to add up")]
1017 BudgetOverflow,
1018 #[error(
1020 "admission = \"measured\" admits runners against the slice {slice}'s MemoryMax, and it has none; install \
1021 a unit file for it (`runnerd doctor` prints one)"
1022 )]
1023 MeasuredWithoutMemoryMax {
1024 slice: String,
1026 },
1027 #[error(
1029 "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
1030 its `memory` or raise the slice's MemoryMax"
1031 )]
1032 ProfileOverBudget {
1033 profile: String,
1035 needs: ByteSize,
1037 slice: String,
1039 memory_max: ByteSize,
1041 },
1042 #[error(
1044 "slots × memory over all profiles plus the mirror is {total}, \
1045 the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
1046 MemoryMax (`runnerd doctor` prints its unit)"
1047 )]
1048 OverBudget {
1049 total: ByteSize,
1051 slice: String,
1053 memory_max: ByteSize,
1055 },
1056}
1057
1058fn defined_profiles(defined: &[String]) -> String {
1059 if defined.is_empty() {
1060 "it defines none".to_owned()
1061 } else {
1062 format!("defined: {}", defined.join(", "))
1063 }
1064}
1065
1066#[derive(Debug, Clone, PartialEq, Eq)]
1068pub enum Warning {
1069 UnlimitedSlice {
1071 slice: String,
1073 total: ByteSize,
1075 },
1076}
1077
1078impl fmt::Display for Warning {
1079 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1080 match self {
1081 Warning::UnlimitedSlice { slice, total } => write!(
1082 f,
1083 "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
1084 install a unit file for it"
1085 ),
1086 }
1087 }
1088}
1089
1090impl Config {
1091 pub fn load(path: &Path) -> Result<Config, ConfigError> {
1093 let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
1094 path: path.to_owned(),
1095 source,
1096 })?;
1097 Config::parse(&text)
1098 }
1099
1100 pub fn parse(text: &str) -> Result<Config, ConfigError> {
1102 Ok(toml::from_str(text)?)
1103 }
1104
1105 pub fn check(
1108 text: &str,
1109 memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
1110 ) -> (Option<Config>, api::ConfigCheck) {
1111 let config = match Config::parse(text) {
1112 Ok(config) => config,
1113 Err(error) => return (None, budget::invalid(error.to_string(), None, None)),
1114 };
1115 let max = match memory_max(&config.host.slice) {
1116 Ok(max) => max,
1117 Err(error) => {
1118 return (
1119 None,
1120 budget::invalid(error.to_string(), Some(&config), None),
1121 );
1122 }
1123 };
1124 let check = config.check_against(max);
1125 if check.valid {
1126 (Some(config), check)
1127 } else {
1128 (None, check)
1129 }
1130 }
1131
1132 pub fn mirror_memory(&self) -> u64 {
1134 if self.caches.mirror.enabled {
1135 self.caches.mirror.memory.0
1136 } else {
1137 0
1138 }
1139 }
1140
1141 pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1143 let mirror = self.mirror_memory();
1144 self.profiles
1145 .values()
1146 .try_fold(mirror, |total, p| {
1147 p.memory
1148 .0
1149 .checked_mul(u64::from(p.slots))?
1150 .checked_add(total)
1151 })
1152 .map(ByteSize)
1153 .ok_or(ConfigError::BudgetOverflow)
1154 }
1155
1156 pub fn memory_needed(&self) -> Result<ByteSize, ConfigError> {
1159 match self.host.admission {
1160 Admission::Caps => self.memory_caps(),
1161 Admission::Measured => {
1162 let largest = self
1163 .profiles
1164 .values()
1165 .map(|p| p.memory.0)
1166 .max()
1167 .unwrap_or(0);
1168 largest
1169 .checked_add(self.mirror_memory())
1170 .map(ByteSize)
1171 .ok_or(ConfigError::BudgetOverflow)
1172 }
1173 }
1174 }
1175
1176 fn check_memory_ceiling(&self) -> Result<(), ConfigError> {
1180 let Some(ceiling) = self.host.memory_ceiling else {
1181 return Ok(());
1182 };
1183 let mirror = self.mirror_memory();
1184 let largest = self
1185 .profiles
1186 .iter()
1187 .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1188 .max_by_key(|(_, needs)| *needs);
1189 match largest {
1190 Some((profile, needs)) if needs > ceiling.0 => Err(ConfigError::CeilingUnderProfile {
1191 profile: profile.clone(),
1192 needs: ByteSize(needs),
1193 ceiling,
1194 }),
1195 _ => Ok(()),
1196 }
1197 }
1198
1199 fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1201 for (name, p) in &self.profiles {
1202 let memory = p.memory.0;
1203 let field = if p
1204 .min_memory
1205 .zip(p.max_memory)
1206 .is_some_and(|(min, max)| min.0 > max.0)
1207 {
1208 "min_memory is above its max_memory"
1209 } else if p.min_memory.is_some_and(|min| memory < min.0) {
1210 "memory is below its min_memory"
1211 } else if p.max_memory.is_some_and(|max| memory > max.0) {
1212 "memory is above its max_memory"
1213 } else if p.max_slots.is_some_and(|max| p.slots > max) {
1214 "slots are above its max_slots"
1215 } else {
1216 continue;
1217 };
1218 return Err(ConfigError::SizingBounds {
1219 profile: name.clone(),
1220 why: field,
1221 });
1222 }
1223 Ok(())
1224 }
1225
1226 fn check_pools(&self) -> Result<(), ConfigError> {
1229 for (at, first) in self.repositories.iter().enumerate() {
1230 for second in self.repositories[at + 1..]
1231 .iter()
1232 .filter(|second| second.name == first.name)
1233 {
1234 if second.profile == first.profile {
1235 return Err(ConfigError::PoolProfileTwice {
1236 repository: first.name.clone(),
1237 profile: first.profile.clone(),
1238 });
1239 }
1240 if let Some(label) = first.labels.iter().find(|label| {
1241 second
1242 .labels
1243 .iter()
1244 .any(|other| other.eq_ignore_ascii_case(label))
1245 }) {
1246 return Err(ConfigError::PoolLabelsOverlap {
1247 repository: first.name.clone(),
1248 first: first.profile.clone(),
1249 second: second.profile.clone(),
1250 label: label.clone(),
1251 });
1252 }
1253 if second.build_cache_writers != first.build_cache_writers {
1254 return Err(ConfigError::PoolWritersDiffer {
1255 repository: first.name.clone(),
1256 });
1257 }
1258 }
1259 }
1260 Ok(())
1261 }
1262
1263 pub fn pool(&self, repository: &str, labels: &[String]) -> Option<&Repository> {
1266 self.repositories
1267 .iter()
1268 .find(|pool| pool.name == repository && labels_match(labels, &pool.labels))
1269 }
1270
1271 pub fn pool_in(&self, repository: &str, profile: &str) -> Option<&Repository> {
1273 self.repositories
1274 .iter()
1275 .find(|pool| pool.name == repository && pool.profile == profile)
1276 }
1277
1278 pub fn pricing(&self) -> api::Pricing {
1280 api::Pricing {
1281 profiles: self
1282 .profiles
1283 .iter()
1284 .filter_map(|(name, profile)| Some((name.clone(), profile.priced_as?)))
1285 .collect(),
1286 default_sku: api::Sku::standard_for(std::env::consts::ARCH),
1287 }
1288 }
1289
1290 pub fn repository_names(&self) -> Vec<String> {
1292 let mut names: Vec<String> = Vec::new();
1293 for repository in &self.repositories {
1294 if !names.contains(&repository.name) {
1295 names.push(repository.name.clone());
1296 }
1297 }
1298 names
1299 }
1300
1301 pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1303 if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1304 return Err(ConfigError::UnsafeNotAllowed);
1305 }
1306 let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1307 [
1308 (p.memory.0 == 0, format!("profiles.{name}.memory")),
1309 (p.cpus == 0, format!("profiles.{name}.cpus")),
1310 (p.slots == 0, format!("profiles.{name}.slots")),
1311 (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1312 ]
1313 });
1314 let mirror = &self.caches.mirror;
1315 let mirror_zero_limits = [
1316 (
1317 mirror.enabled && mirror.memory.0 == 0,
1318 "caches.mirror.memory".to_owned(),
1319 ),
1320 (
1321 mirror.enabled && mirror.cpus == 0,
1322 "caches.mirror.cpus".to_owned(),
1323 ),
1324 (
1325 self.host.poll_interval.is_zero(),
1326 "host.poll_interval".to_owned(),
1327 ),
1328 (
1329 self.host.memory_ceiling == Some(ByteSize(0)),
1330 "host.memory_ceiling".to_owned(),
1331 ),
1332 (self.records.max_count == 0, "records.max_count".to_owned()),
1333 (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1334 (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1335 (
1336 self.sizing.raise_interval.is_zero(),
1337 "sizing.raise_interval".to_owned(),
1338 ),
1339 (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1340 (
1341 self.records.max_total.0 == 0,
1342 "records.max_total".to_owned(),
1343 ),
1344 ];
1345 if let Some((_, field)) = zero_limits
1346 .chain(mirror_zero_limits)
1347 .find(|(zero, _)| *zero)
1348 {
1349 return Err(ConfigError::ZeroLimit { field });
1350 }
1351 self.check_sizing_bounds()?;
1352 self.caches.build.validate()?;
1353 self.host.check_cpu_overcommit()?;
1354 self.check_memory_ceiling()?;
1355 self.web.validate()?;
1356 self.notify.validate()?;
1357 for repository in &self.repositories {
1358 if !self.profiles.contains_key(&repository.profile) {
1359 return Err(ConfigError::UnknownProfile {
1360 repository: repository.name.clone(),
1361 profile: repository.profile.clone(),
1362 defined: self.profiles.keys().cloned().collect(),
1363 });
1364 }
1365 if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1366 {
1367 return Err(ConfigError::MissingLabels {
1368 repository: repository.name.clone(),
1369 });
1370 }
1371 }
1372 self.check_pools()?;
1373 self.check_budget(memory_max)
1374 }
1375
1376 fn check_budget(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1378 let total = self.memory_caps()?;
1379 if self.host.admission == Admission::Measured {
1380 let Some(memory_max) = memory_max else {
1381 return Err(ConfigError::MeasuredWithoutMemoryMax {
1382 slice: self.host.slice.clone(),
1383 });
1384 };
1385 let mirror = self.mirror_memory();
1386 if let Some((profile, needs)) = self
1387 .profiles
1388 .iter()
1389 .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1390 .find(|(_, needs)| *needs > memory_max.0)
1391 {
1392 return Err(ConfigError::ProfileOverBudget {
1393 profile: profile.clone(),
1394 needs: ByteSize(needs),
1395 slice: self.host.slice.clone(),
1396 memory_max,
1397 });
1398 }
1399 let needed = self.memory_needed()?;
1401 if needed > memory_max {
1402 return Err(ConfigError::OverBudget {
1403 total: needed,
1404 slice: self.host.slice.clone(),
1405 memory_max,
1406 });
1407 }
1408 return Ok(Vec::new());
1409 }
1410 match memory_max {
1411 Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1412 total,
1413 slice: self.host.slice.clone(),
1414 memory_max,
1415 }),
1416 Some(_) => Ok(Vec::new()),
1417 None => Ok(vec![Warning::UnlimitedSlice {
1418 slice: self.host.slice.clone(),
1419 total,
1420 }]),
1421 }
1422 }
1423}
1424
1425#[derive(Debug, thiserror::Error)]
1427pub enum ResizeError {
1428 #[error(transparent)]
1430 Parse(#[from] toml_edit::TomlError),
1431 #[error("the config has no profile {0:?} under [profiles]")]
1433 NoProfile(String),
1434}
1435
1436pub fn resize_profile(
1439 text: &str,
1440 profile: &str,
1441 resize: api::Resize,
1442) -> Result<String, ResizeError> {
1443 let mut document: toml_edit::DocumentMut = text.parse()?;
1444 let table = document
1445 .get_mut("profiles")
1446 .and_then(|profiles| profiles.get_mut(profile))
1447 .and_then(toml_edit::Item::as_table_like_mut)
1448 .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1449 let values = [
1450 (
1451 "memory",
1452 toml_edit::Value::from(ByteSize(resize.memory).to_string()),
1453 ),
1454 ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1455 ];
1456 let cpus = resize
1457 .cpus
1458 .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1459 for (key, mut value) in values.into_iter().chain(cpus) {
1460 if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1462 *value.decor_mut() = old.decor().clone();
1463 }
1464 table.insert(key, toml_edit::Item::Value(value));
1465 }
1466 Ok(document.to_string())
1467}
1468
1469pub const SECRET_SHOWN: &str = "<secret>";
1471
1472pub fn mask_secrets(text: &str) -> Option<String> {
1476 let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1477 let mut masked = false;
1478 for target in notify_targets(&mut document) {
1479 for (_, value) in secrets_of(target) {
1480 replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1481 masked = true;
1482 }
1483 }
1484 Some(if masked {
1485 document.to_string()
1486 } else {
1487 text.to_owned()
1488 })
1489}
1490
1491pub fn keep_secrets(text: &str, stored: &str) -> String {
1498 if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1499 return text.to_owned();
1500 }
1501 let (Ok(mut document), Ok(mut old)) = (
1502 text.parse::<toml_edit::DocumentMut>(),
1503 stored.parse::<toml_edit::DocumentMut>(),
1504 ) else {
1505 return text.to_owned();
1506 };
1507 let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1508 for target in notify_targets(&mut old) {
1509 let Some(name) = target_name(target) else {
1510 continue;
1511 };
1512 let secrets = secrets_of(target)
1513 .into_iter()
1514 .map(|(field, value)| (field, value.clone()))
1515 .collect();
1516 kept.insert(name, secrets);
1517 }
1518 for target in notify_targets(&mut document) {
1519 let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1520 continue;
1521 };
1522 for (field, value) in secrets_of(target) {
1523 if value.as_str() != Some(SECRET_SHOWN) {
1524 continue;
1525 }
1526 if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1527 replace_value(value, old.clone());
1528 }
1529 }
1530 }
1531 document.to_string()
1532}
1533
1534#[derive(Debug, PartialEq, Eq)]
1536enum SecretField {
1537 Url,
1538 Header(String),
1539}
1540
1541fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1543 match document
1544 .get_mut("notify")
1545 .and_then(|notify| notify.get_mut("targets"))
1546 {
1547 Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1548 .iter_mut()
1549 .map(|table| table as &mut dyn toml_edit::TableLike)
1550 .collect(),
1551 Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1552 .iter_mut()
1553 .filter_map(toml_edit::Value::as_inline_table_mut)
1554 .map(|table| table as &mut dyn toml_edit::TableLike)
1555 .collect(),
1556 _ => Vec::new(),
1557 }
1558}
1559
1560fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1561 target
1562 .get("name")
1563 .and_then(toml_edit::Item::as_str)
1564 .map(str::to_owned)
1565}
1566
1567fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1569 let mut found = Vec::new();
1570 for (key, item) in target.iter_mut() {
1571 match key.get() {
1572 "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1573 "headers" => {
1574 let Some(headers) = item.as_table_like_mut() else {
1575 continue;
1576 };
1577 for (name, item) in headers.iter_mut() {
1578 let field = SecretField::Header(name.get().to_owned());
1579 found.extend(item.as_value_mut().map(|value| (field, value)));
1580 }
1581 }
1582 _ => {}
1583 }
1584 }
1585 found
1586}
1587
1588fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1590 *new.decor_mut() = value.decor().clone();
1591 *value = new;
1592}
1593
1594pub fn revision(text: &str) -> String {
1597 let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1598 (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1599 });
1600 format!("{hash:016x}")
1601}
1602
1603pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1605 let output = Command::new("systemctl")
1606 .args(["show", slice, "--property=MemoryMax", "--value"])
1607 .output()?;
1608 if !output.status.success() {
1609 return Err(std::io::Error::other(format!(
1610 "systemctl show {slice} failed: {}",
1611 String::from_utf8_lossy(&output.stderr).trim()
1612 )));
1613 }
1614 let value = String::from_utf8_lossy(&output.stdout);
1615 parse_memory_max(value.trim()).ok_or_else(|| {
1616 std::io::Error::other(format!(
1617 "systemctl show {slice}: unexpected MemoryMax {value:?}"
1618 ))
1619 })
1620}
1621
1622pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1624 #[expect(
1625 clippy::cast_precision_loss,
1626 clippy::cast_possible_truncation,
1627 clippy::cast_sign_loss,
1628 reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1629 most 16"
1630 )]
1631 let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1632 budget
1633}
1634
1635pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1638 let output = Command::new("systemctl")
1639 .args([
1640 "show",
1641 slice,
1642 "--property=EffectiveCPUs",
1643 "--property=AllowedCPUs",
1644 "--property=CPUQuotaPerSecUSec",
1645 ])
1646 .output()?;
1647 if !output.status.success() {
1648 return Err(std::io::Error::other(format!(
1649 "systemctl show {slice} failed: {}",
1650 String::from_utf8_lossy(&output.stderr).trim()
1651 )));
1652 }
1653 let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1654 parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1655 std::io::Error::other(format!(
1656 "systemctl show {slice}: unexpected CPU properties {:?}",
1657 String::from_utf8_lossy(&output.stdout).trim()
1658 ))
1659 })
1660}
1661
1662pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1665 let value = |name: &str| {
1666 properties.lines().find_map(|line| {
1667 line.strip_prefix(name)
1668 .and_then(|rest| rest.strip_prefix('='))
1669 .map(str::trim)
1670 })
1671 };
1672 let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
1673 .into_iter()
1674 .flatten()
1675 .find(|cpus| !cpus.is_empty());
1676 let cpus = match set {
1677 Some(set) => cpu_count(set)?,
1678 None => host,
1679 };
1680 let quota = match value("CPUQuotaPerSecUSec") {
1681 None | Some("" | "infinity") => u64::MAX,
1682 Some(quota) => quota_millicores(quota)?,
1683 };
1684 Some(cpus.saturating_mul(1000).min(quota))
1685}
1686
1687fn cpu_count(set: &str) -> Option<u64> {
1689 set.split([',', ' '])
1690 .filter(|range| !range.is_empty())
1691 .try_fold(0u64, |count, range| {
1692 let (first, last) = range.split_once('-').unwrap_or((range, range));
1693 let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
1694 Some(count + last.checked_sub(first)? + 1)
1695 })
1696}
1697
1698fn quota_millicores(quota: &str) -> Option<u64> {
1701 const UNITS: [(&str, f64); 10] = [
1703 ("y", 31_557_600_000.0),
1704 ("M", 2_629_800_000.0),
1705 ("w", 604_800_000.0),
1706 ("d", 86_400_000.0),
1707 ("h", 3_600_000.0),
1708 ("min", 60_000.0),
1709 ("s", 1000.0),
1710 ("ms", 1.0),
1711 ("us", 0.001),
1712 ("µs", 0.001),
1713 ];
1714 let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
1715 let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
1716 let (number, unit) = part.split_at(at);
1717 let per_unit = UNITS
1718 .iter()
1719 .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
1720 Some(total + number.parse::<f64>().ok()? * per_unit)
1721 })?;
1722 #[allow(
1723 clippy::cast_possible_truncation,
1724 clippy::cast_sign_loss,
1725 reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
1726 )]
1727 (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
1728 .then(|| millicores.round() as u64)
1729}
1730
1731pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
1733 if value == "infinity" {
1734 Some(None)
1735 } else {
1736 value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
1737 }
1738}
1739
1740#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1742pub struct ByteSize(pub u64);
1743
1744const UNITS: [(char, u64); 4] = [
1745 ('T', 1 << 40),
1746 ('G', 1 << 30),
1747 ('M', 1 << 20),
1748 ('K', 1 << 10),
1749];
1750
1751impl FromStr for ByteSize {
1752 type Err = String;
1753
1754 #[allow(
1755 clippy::cast_precision_loss,
1756 clippy::cast_possible_truncation,
1757 clippy::cast_sign_loss,
1758 reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
1759 )]
1760 fn from_str(s: &str) -> Result<Self, Self::Err> {
1761 let invalid = || {
1762 format!(
1763 "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
1764 )
1765 };
1766 let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
1767 let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
1768 let number = &s[..s.len() - 1];
1769 if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
1770 return Err(invalid());
1771 }
1772 let number: f64 = number.parse().map_err(|_| invalid())?;
1773 let bytes = (number * *factor as f64).round();
1774 if bytes >= u64::MAX as f64 {
1776 return Err(format!("size {s:?} is too large"));
1777 }
1778 Ok(ByteSize(bytes as u64))
1779 }
1780}
1781
1782impl fmt::Display for ByteSize {
1783 #[allow(
1784 clippy::cast_precision_loss,
1785 reason = "shown with two decimals, far coarser than f64's precision"
1786 )]
1787 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1788 let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
1789 return write!(f, "{}B", self.0);
1790 };
1791 let number = format!("{:.2}", self.0 as f64 / *factor as f64);
1792 let number = number.trim_end_matches('0').trim_end_matches('.');
1793 write!(f, "{number}{unit}")
1794 }
1795}
1796
1797impl ByteSize {
1798 #[allow(
1802 clippy::cast_precision_loss,
1803 reason = "shown with one decimal at most, far coarser than f64's precision"
1804 )]
1805 pub fn short(self) -> String {
1806 let Some(index) = UNITS.iter().position(|(_, factor)| self.0 >= *factor) else {
1807 return format!("{}B", self.0);
1808 };
1809 let (mut unit, factor) = UNITS[index];
1810 let mut value = self.0 as f64 / factor as f64;
1811 if value.round() >= 1024.0
1812 && let Some(larger) = index.checked_sub(1)
1813 {
1814 unit = UNITS[larger].0;
1815 value /= 1024.0;
1816 }
1817 let tenths = (value * 10.0).round() / 10.0;
1819 if tenths < 10.0 {
1820 let number = format!("{tenths:.1}");
1821 format!("{}{unit}", number.trim_end_matches(".0"))
1822 } else {
1823 format!("{:.0}{unit}", value.round())
1824 }
1825 }
1826}
1827
1828impl<'de> Deserialize<'de> for ByteSize {
1829 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1830 String::deserialize(deserializer)?
1831 .parse()
1832 .map_err(de::Error::custom)
1833 }
1834}
1835
1836pub fn parse_duration(s: &str) -> Result<Duration, String> {
1838 let invalid = || {
1839 format!(
1840 "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
1841 )
1842 };
1843 let (number, factor) = match s.chars().last() {
1844 Some('s') => (&s[..s.len() - 1], 1),
1845 Some('m') => (&s[..s.len() - 1], 60),
1846 Some('h') => (&s[..s.len() - 1], 3600),
1847 Some('d') => (&s[..s.len() - 1], 86400),
1848 _ => return Err(invalid()),
1849 };
1850 let number: u64 = number.parse().map_err(|_| invalid())?;
1851 number
1852 .checked_mul(factor)
1853 .map(Duration::from_secs)
1854 .ok_or_else(invalid)
1855}
1856
1857pub fn parse_age(text: &str) -> Result<Duration, String> {
1859 let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
1860 let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
1861 let (number, unit) = text.split_at(split);
1862 let number: u64 = number.parse().map_err(|_| error())?;
1863 let unit = match unit {
1864 "s" => 1,
1865 "m" => 60,
1866 "h" => 3600,
1867 "d" => 86_400,
1868 "w" => 7 * 86_400,
1869 _ => return Err(error()),
1870 };
1871 Ok(Duration::from_secs(number.saturating_mul(unit)))
1872}
1873
1874fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
1875 parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
1876}
1877
1878#[cfg(test)]
1879mod tests;