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 #[serde(default = "default_on")]
81 pub pooled_count: bool,
82}
83
84impl Host {
85 fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
87 match self.cpu_overcommit {
88 Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
89 Err(ConfigError::CpuOvercommit { value })
90 }
91 _ => Ok(()),
92 }
93 }
94}
95
96pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
98
99#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
101#[serde(rename_all = "kebab-case")]
102pub enum Admission {
103 #[default]
105 Caps,
106 Measured,
109}
110
111impl Admission {
112 pub fn policy(self) -> api::AdmissionPolicy {
114 match self {
115 Admission::Caps => api::AdmissionPolicy::Caps,
116 Admission::Measured => api::AdmissionPolicy::Measured,
117 }
118 }
119}
120
121fn default_on() -> bool {
122 true
123}
124
125fn default_poll_interval() -> Duration {
126 Duration::from_secs(15)
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
131#[serde(rename_all = "kebab-case")]
132pub enum Isolation {
133 Sysbox,
135 UnsafeDind,
137}
138
139#[derive(Debug, Clone, Deserialize)]
141#[serde(deny_unknown_fields)]
142pub struct Caches {
143 pub mirror: Mirror,
145 pub pnpm: CacheLimit,
147 pub maven: CacheLimit,
149 #[serde(default = "default_reseed_at")]
152 pub reseed_at: ByteSize,
153 #[serde(default = "default_snapshot_max")]
156 pub snapshot_max: ByteSize,
157 #[serde(default)]
159 pub build: BuildCache,
160}
161
162#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
165#[serde(deny_unknown_fields, default)]
166pub struct BuildCache {
167 pub enabled: bool,
169 pub max_size: ByteSize,
171 pub branches: Vec<String>,
174 pub directory: Option<PathBuf>,
176 pub actions_cache: bool,
179}
180
181impl BuildCache {
182 pub fn actions_cache_check(&self) -> api::Check {
184 api::Check {
185 name: api::ACTIONS_CACHE.to_owned(),
186 status: api::CheckStatus::Info,
187 detail: if self.enabled {
188 api::actions_cache_place(self.actions_cache)
189 } else {
190 "GitHub's, as the build cache is off"
191 }
192 .to_owned(),
193 fix: None,
194 }
195 }
196
197 fn validate(&self) -> Result<(), ConfigError> {
198 if self.enabled && self.max_size.0 == 0 {
199 return Err(ConfigError::ZeroLimit {
200 field: "caches.build.max_size".to_owned(),
201 });
202 }
203 Ok(())
204 }
205}
206
207impl Default for BuildCache {
208 fn default() -> BuildCache {
209 BuildCache {
210 enabled: true,
211 max_size: ByteSize(10 << 30),
212 branches: Vec::new(),
213 directory: None,
214 actions_cache: true,
215 }
216 }
217}
218
219fn default_reseed_at() -> ByteSize {
220 ByteSize(2 << 30)
221}
222
223fn default_snapshot_max() -> ByteSize {
225 ByteSize(10 << 30)
226}
227
228fn default_mirror_max_size() -> ByteSize {
229 ByteSize(10 << 30)
230}
231
232#[derive(Debug, Clone, Deserialize)]
234#[serde(deny_unknown_fields)]
235pub struct Mirror {
236 pub enabled: bool,
238 pub memory: ByteSize,
240 pub cpus: u32,
242 #[serde(deserialize_with = "duration")]
244 pub ttl: Duration,
245 #[serde(default = "default_mirror_max_size")]
247 pub max_size: ByteSize,
248}
249
250#[derive(Debug, Clone, Deserialize)]
252#[serde(deny_unknown_fields)]
253pub struct CacheLimit {
254 pub max_size: ByteSize,
256}
257
258pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
261
262#[derive(Debug, Clone, Deserialize)]
264#[serde(try_from = "ProfileToml")]
265pub struct Profile {
266 pub image: Image,
268 pub cpus: u32,
270 pub memory: ByteSize,
272 pub slots: u32,
274 pub record_failures: bool,
276 pub min_memory: Option<ByteSize>,
278 pub max_memory: Option<ByteSize>,
280 pub max_slots: Option<u32>,
282 pub priced_as: Option<api::Sku>,
285}
286
287#[derive(Debug, Clone, PartialEq, Eq)]
289pub enum Image {
290 Stage(String),
292 Pulled(String),
295}
296
297impl Image {
298 pub fn reference(&self) -> String {
300 match self {
301 Image::Stage(stage) => format!("runnerd-runner:{stage}"),
302 Image::Pulled(reference) => reference.clone(),
303 }
304 }
305}
306
307#[derive(Deserialize)]
309#[serde(deny_unknown_fields)]
310struct ProfileToml {
311 image_stage: Option<String>,
312 image: Option<String>,
313 cpus: u32,
314 memory: ByteSize,
315 slots: u32,
316 #[serde(default)]
317 record_failures: bool,
318 min_memory: Option<ByteSize>,
319 max_memory: Option<ByteSize>,
320 max_slots: Option<u32>,
321 priced_as: Option<api::Sku>,
322}
323
324impl TryFrom<ProfileToml> for Profile {
325 type Error = String;
326
327 fn try_from(profile: ProfileToml) -> Result<Profile, String> {
328 let image = match (profile.image_stage, profile.image) {
329 (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
330 (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
331 (Some(_), Some(_)) => {
332 return Err(
333 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
334 not both"
335 .to_owned(),
336 );
337 }
338 _ => {
339 return Err(
340 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
341 .to_owned(),
342 );
343 }
344 };
345 Ok(Profile {
346 image,
347 cpus: profile.cpus,
348 memory: profile.memory,
349 slots: profile.slots,
350 record_failures: profile.record_failures,
351 min_memory: profile.min_memory,
352 max_memory: profile.max_memory,
353 max_slots: profile.max_slots,
354 priced_as: profile.priced_as,
355 })
356 }
357}
358
359pub fn labels_match(wanted: &[String], labels: &[String]) -> bool {
363 !wanted.is_empty()
364 && wanted.iter().all(|wanted| {
365 labels
366 .iter()
367 .any(|label| label.eq_ignore_ascii_case(wanted))
368 })
369}
370
371#[derive(Debug, Deserialize)]
374#[serde(deny_unknown_fields)]
375pub struct Repository {
376 pub name: String,
378 pub profile: String,
380 pub labels: Vec<String>,
382 #[serde(default = "default_warm")]
385 pub warm: u32,
386 #[serde(default = "default_true")]
388 pub build_cache: bool,
389 #[serde(default)]
392 pub build_cache_writers: BuildCacheWriters,
393}
394
395#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
397#[serde(rename_all = "kebab-case")]
398pub enum BuildCacheWriters {
399 #[default]
401 DefaultBranch,
402 PullRequests,
404}
405
406fn default_true() -> bool {
407 true
408}
409
410fn default_warm() -> u32 {
411 1
412}
413
414#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
416#[serde(deny_unknown_fields, default)]
417pub struct History {
418 pub database: Option<PathBuf>,
420 #[serde(deserialize_with = "duration")]
422 pub samples: Duration,
423 #[serde(deserialize_with = "duration")]
425 pub logs: Duration,
426 #[serde(deserialize_with = "duration")]
428 pub events: Duration,
429 #[serde(deserialize_with = "duration")]
431 pub jobs: Duration,
432 pub log_max: ByteSize,
434}
435
436impl Default for History {
437 fn default() -> History {
438 History {
439 database: None,
440 samples: Duration::from_hours(7 * 24),
441 logs: Duration::from_hours(30 * 24),
442 events: Duration::from_hours(30 * 24),
443 jobs: Duration::from_hours(90 * 24),
444 log_max: ByteSize(50 << 20),
445 }
446 }
447}
448
449#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
452#[serde(deny_unknown_fields, default)]
453pub struct Records {
454 pub max_total: ByteSize,
456 pub max_count: u32,
458 #[serde(deserialize_with = "duration")]
460 pub max_age: Duration,
461 pub min_free: ByteSize,
463}
464
465impl Default for Records {
466 fn default() -> Records {
467 Records {
468 max_total: ByteSize(20 << 30),
469 max_count: 10,
470 max_age: Duration::from_hours(7 * 24),
471 min_free: ByteSize(10 << 30),
472 }
473 }
474}
475
476#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
479#[serde(deny_unknown_fields, default)]
480pub struct Sizing {
481 pub margin: u32,
484 pub min_jobs: u32,
487 pub auto: bool,
490 #[serde(deserialize_with = "duration")]
493 pub interval: Duration,
494 #[serde(deserialize_with = "duration")]
497 pub raise_interval: Duration,
498 pub max_step: u32,
501}
502
503impl Sizing {
504 pub fn raise_after(&self) -> Duration {
507 self.raise_interval.min(self.interval)
508 }
509}
510
511impl Default for Sizing {
512 fn default() -> Sizing {
513 Sizing {
514 margin: 15,
515 min_jobs: 10,
516 auto: false,
517 interval: Duration::from_hours(1),
518 raise_interval: Duration::from_hours(1),
519 max_step: 50,
520 }
521 }
522}
523
524#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
526#[serde(deny_unknown_fields, default)]
527pub struct Web {
528 pub origins: Vec<Origin>,
531 pub open: Option<Origin>,
534}
535
536impl Web {
537 fn validate(&self) -> Result<(), ConfigError> {
539 match &self.open {
540 Some(open) if !self.origins.contains(open) => {
541 Err(ConfigError::OpenNotListed { open: open.clone() })
542 }
543 _ => Ok(()),
544 }
545 }
546}
547
548#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
550#[serde(deny_unknown_fields, default)]
551pub struct Notify {
552 pub digest_at: DayTime,
554 pub targets: Vec<NotifyTarget>,
556}
557
558impl Default for Notify {
559 fn default() -> Notify {
560 Notify {
561 digest_at: DayTime { minutes: 9 * 60 },
562 targets: Vec::new(),
563 }
564 }
565}
566
567impl Notify {
568 fn validate(&self) -> Result<(), ConfigError> {
571 let refuse = |target: &str, why: &'static str| {
572 Err(ConfigError::Notify {
573 target: target.to_owned(),
574 why,
575 })
576 };
577 for (index, target) in self.targets.iter().enumerate() {
578 let name = target.name.as_str();
579 if name.trim().is_empty() || name.chars().any(char::is_control) {
580 return refuse(name, "name is empty");
581 }
582 if self.targets[..index].iter().any(|other| other.name == name) {
583 return refuse(name, "another target has this name");
584 }
585 let url = target.url.expose();
586 if url == SECRET_SHOWN {
587 return refuse(
588 name,
589 "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
590 );
591 }
592 let Some(rest) = url
593 .strip_prefix("https://")
594 .or_else(|| url.strip_prefix("http://"))
595 else {
596 return refuse(name, "url does not start with https:// or http://");
597 };
598 let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
599 if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
600 return refuse(name, "url is not a URL");
601 }
602 if target.kind == NotifyKind::Ntfy
603 && (path.is_empty() || path.contains(['/', '?', '#']))
604 {
605 return refuse(
606 name,
607 "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
608 );
609 }
610 for (header, value) in &target.headers {
611 if value.expose() == SECRET_SHOWN {
612 return refuse(
613 name,
614 "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
615 );
616 }
617 let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
618 if header.is_empty() || !header.chars().all(token) {
619 return refuse(name, "a header's name is not a header name");
620 }
621 if value.expose().chars().any(char::is_control) {
622 return refuse(name, "a header's value has a control character");
623 }
624 }
625 if target.events.contains(&api::NotifyEvent::Test) {
626 return refuse(
627 name,
628 "events lists test, which only runnerctl notify test sends",
629 );
630 }
631 }
632 Ok(())
633 }
634}
635
636#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
638#[serde(deny_unknown_fields)]
639pub struct NotifyTarget {
640 pub name: String,
642 pub kind: NotifyKind,
644 pub url: Secret,
647 #[serde(default)]
649 pub headers: BTreeMap<String, Secret>,
650 #[serde(default = "default_notify_events")]
652 pub events: Vec<api::NotifyEvent>,
653}
654
655fn default_notify_events() -> Vec<api::NotifyEvent> {
656 vec![
657 api::NotifyEvent::Failed,
658 api::NotifyEvent::Lost,
659 api::NotifyEvent::Runner,
660 api::NotifyEvent::Insights,
661 ]
662}
663
664#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
666#[serde(rename_all = "kebab-case")]
667pub enum NotifyKind {
668 Webhook,
670 Ntfy,
672 Discord,
674}
675
676#[derive(Clone, PartialEq, Eq, Deserialize)]
678#[serde(transparent)]
679pub struct Secret(String);
680
681impl Secret {
682 pub fn expose(&self) -> &str {
684 &self.0
685 }
686}
687
688impl From<&str> for Secret {
689 fn from(value: &str) -> Secret {
690 Secret(value.to_owned())
691 }
692}
693
694impl fmt::Debug for Secret {
695 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
696 f.write_str("<secret>")
697 }
698}
699
700#[derive(Debug, Clone, Copy, PartialEq, Eq)]
702pub struct DayTime {
703 pub minutes: u32,
705}
706
707impl FromStr for DayTime {
708 type Err = String;
709
710 fn from_str(text: &str) -> Result<DayTime, String> {
711 let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
712 let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
713 let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
714 if !digits(hour) || !digits(minute) {
715 return Err(invalid());
716 }
717 let (hour, minute): (u32, u32) = (
718 hour.parse().map_err(|_| invalid())?,
719 minute.parse().map_err(|_| invalid())?,
720 );
721 if hour > 23 || minute > 59 {
722 return Err(invalid());
723 }
724 Ok(DayTime {
725 minutes: hour * 60 + minute,
726 })
727 }
728}
729
730impl fmt::Display for DayTime {
731 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
732 write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
733 }
734}
735
736impl<'de> Deserialize<'de> for DayTime {
737 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
738 String::deserialize(deserializer)?
739 .parse()
740 .map_err(de::Error::custom)
741 }
742}
743
744#[derive(Debug, Clone, PartialEq, Eq)]
748pub struct Origin(String);
749
750impl Origin {
751 pub fn as_str(&self) -> &str {
753 &self.0
754 }
755}
756
757impl FromStr for Origin {
758 type Err = String;
759
760 fn from_str(s: &str) -> Result<Origin, String> {
761 let invalid = |why: &str| {
762 format!(
763 "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
764 \"https://host.example.ts.net:8443\""
765 )
766 };
767 let (scheme, rest) = s
768 .split_once("://")
769 .ok_or_else(|| invalid("it has no scheme"))?;
770 let scheme = scheme.to_ascii_lowercase();
771 if rest.contains(['/', '?', '#']) {
772 return Err(invalid("it has a path"));
773 }
774 if rest.contains('@') {
775 return Err(invalid("it names a user"));
776 }
777 let (host, port) = match rest.strip_prefix('[') {
778 Some(v6) => {
779 let (address, after) = v6
780 .split_once(']')
781 .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
782 let port = match after {
783 "" => None,
784 _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
785 };
786 (format!("[{address}]"), port)
787 }
788 None => match rest.split_once(':') {
789 Some((host, port)) => (host.to_owned(), Some(port)),
790 None => (rest.to_owned(), None),
791 },
792 };
793 let host = host.to_ascii_lowercase();
794 let not_a_host = || invalid("its host is not a name or an address");
795 let (host, loopback) =
797 if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
798 let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
799 (format!("[{}]", ipv6_origin(address)), address.is_loopback())
800 } else {
801 if host.is_empty()
802 || !host
803 .chars()
804 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
805 {
806 return Err(not_a_host());
807 }
808 let name = host.strip_suffix('.').unwrap_or(&host);
810 let last = name.rsplit('.').next().unwrap_or_default();
811 let numeric = !last.is_empty()
812 && (last.bytes().all(|byte| byte.is_ascii_digit())
813 || last
814 .strip_prefix("0x")
815 .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
816 if numeric {
817 let address: Ipv4Addr = name.parse().map_err(|_| {
818 invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
819 })?;
820 (address.to_string(), address.is_loopback())
821 } else {
822 let loopback = host == "localhost";
823 (host, loopback)
824 }
825 };
826 let port = port
827 .map(|port| {
828 Some(port)
829 .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
830 .and_then(|port| port.parse::<u16>().ok())
831 .filter(|port| *port != 0)
832 .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
833 })
834 .transpose()?;
835 let default_port = match scheme.as_str() {
836 "https" => 443,
837 "http" if loopback => 80,
838 "http" => {
839 return Err(invalid(
840 "http is for a loopback host only, since the session cookie would cross the network in \
841 the clear; use https",
842 ));
843 }
844 _ => return Err(invalid("its scheme is not https")),
845 };
846 Ok(Origin(match port {
847 Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
848 _ => format!("{scheme}://{host}"),
849 }))
850 }
851}
852
853fn ipv6_origin(address: Ipv6Addr) -> String {
856 let pieces = address.segments();
857 let (mut zeros_at, mut zeros) = (0, 0);
858 let mut at = 0;
859 while at < pieces.len() {
860 let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
861 if run > zeros {
862 (zeros_at, zeros) = (at, run);
863 }
864 at += run.max(1);
865 }
866 let hex = |pieces: &[u16]| {
867 pieces
868 .iter()
869 .map(|piece| format!("{piece:x}"))
870 .collect::<Vec<_>>()
871 .join(":")
872 };
873 if zeros < 2 {
874 return hex(&pieces);
875 }
876 format!(
877 "{}::{}",
878 hex(&pieces[..zeros_at]),
879 hex(&pieces[zeros_at + zeros..])
880 )
881}
882
883impl fmt::Display for Origin {
884 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
885 f.write_str(&self.0)
886 }
887}
888
889impl<'de> Deserialize<'de> for Origin {
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#[derive(Debug, thiserror::Error)]
899pub enum ConfigError {
900 #[error("cannot read {}: {source}", path.display())]
902 Read {
903 path: PathBuf,
905 source: std::io::Error,
907 },
908 #[error(transparent)]
910 Parse(#[from] toml::de::Error),
911 #[error(
913 "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
914 defined_profiles(.defined)
915 )]
916 UnknownProfile {
917 repository: String,
919 profile: String,
921 defined: Vec<String>,
923 },
924 #[error("repository {repository} needs at least one non-empty label")]
926 MissingLabels {
927 repository: String,
929 },
930 #[error(
932 "repository {repository} has two entries with profile {profile:?}; give each of its pools its own profile"
933 )]
934 PoolProfileTwice {
935 repository: String,
937 profile: String,
939 },
940 #[error(
943 "repository {repository}'s pools in {first} and {second} both have the label {label:?}, so a job asking for \
944 it could land in either; give each pool labels of its own, such as only {second:?}'s own label for the \
945 lighter one"
946 )]
947 PoolLabelsOverlap {
948 repository: String,
950 first: String,
952 second: String,
954 label: String,
956 },
957 #[error(
959 "repository {repository}'s entries set build_cache_writers differently; whose runs may write to the build \
960 cache is the repository's, so set the same in each"
961 )]
962 PoolWritersDiffer {
963 repository: String,
965 },
966 #[error(
968 "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
969 set allow_unsafe = true under [host] to use it anyway"
970 )]
971 UnsafeNotAllowed,
972 #[error("{field} is 0; set it above 0")]
974 ZeroLimit {
975 field: String,
977 },
978 #[error(
980 "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
981 )]
982 CpuOvercommit {
983 value: f64,
985 },
986 #[error(
988 "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
989 )]
990 OpenNotListed {
991 open: Origin,
993 },
994 #[error("profiles.{profile}: {why}")]
997 SizingBounds {
998 profile: String,
1000 why: &'static str,
1002 },
1003 #[error("notify target {target:?}: {why}")]
1005 Notify {
1006 target: String,
1008 why: &'static str,
1010 },
1011 #[error(
1013 "host.memory_ceiling is {ceiling}, under the {needs} one runner of {profile} and the mirror take; set it \
1014 to the most memory the host can grow to, such as the VM's maximum"
1015 )]
1016 CeilingUnderProfile {
1017 profile: String,
1019 needs: ByteSize,
1021 ceiling: ByteSize,
1023 },
1024 #[error("slots × memory over all profiles plus the mirror is too large to add up")]
1026 BudgetOverflow,
1027 #[error(
1029 "admission = \"measured\" admits runners against the slice {slice}'s MemoryMax, and it has none; install \
1030 a unit file for it (`runnerd doctor` prints one)"
1031 )]
1032 MeasuredWithoutMemoryMax {
1033 slice: String,
1035 },
1036 #[error(
1038 "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
1039 its `memory` or raise the slice's MemoryMax"
1040 )]
1041 ProfileOverBudget {
1042 profile: String,
1044 needs: ByteSize,
1046 slice: String,
1048 memory_max: ByteSize,
1050 },
1051 #[error(
1053 "slots × memory over all profiles plus the mirror is {total}, \
1054 the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
1055 MemoryMax (`runnerd doctor` prints its unit)"
1056 )]
1057 OverBudget {
1058 total: ByteSize,
1060 slice: String,
1062 memory_max: ByteSize,
1064 },
1065}
1066
1067fn defined_profiles(defined: &[String]) -> String {
1068 if defined.is_empty() {
1069 "it defines none".to_owned()
1070 } else {
1071 format!("defined: {}", defined.join(", "))
1072 }
1073}
1074
1075#[derive(Debug, Clone, PartialEq, Eq)]
1077pub enum Warning {
1078 UnlimitedSlice {
1080 slice: String,
1082 total: ByteSize,
1084 },
1085}
1086
1087impl fmt::Display for Warning {
1088 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1089 match self {
1090 Warning::UnlimitedSlice { slice, total } => write!(
1091 f,
1092 "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
1093 install a unit file for it"
1094 ),
1095 }
1096 }
1097}
1098
1099impl Config {
1100 pub fn load(path: &Path) -> Result<Config, ConfigError> {
1102 let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
1103 path: path.to_owned(),
1104 source,
1105 })?;
1106 Config::parse(&text)
1107 }
1108
1109 pub fn parse(text: &str) -> Result<Config, ConfigError> {
1111 Ok(toml::from_str(text)?)
1112 }
1113
1114 pub fn check(
1117 text: &str,
1118 memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
1119 ) -> (Option<Config>, api::ConfigCheck) {
1120 let config = match Config::parse(text) {
1121 Ok(config) => config,
1122 Err(error) => return (None, budget::invalid(error.to_string(), None, None)),
1123 };
1124 let max = match memory_max(&config.host.slice) {
1125 Ok(max) => max,
1126 Err(error) => {
1127 return (
1128 None,
1129 budget::invalid(error.to_string(), Some(&config), None),
1130 );
1131 }
1132 };
1133 let check = config.check_against(max);
1134 if check.valid {
1135 (Some(config), check)
1136 } else {
1137 (None, check)
1138 }
1139 }
1140
1141 pub fn mirror_memory(&self) -> u64 {
1143 if self.caches.mirror.enabled {
1144 self.caches.mirror.memory.0
1145 } else {
1146 0
1147 }
1148 }
1149
1150 pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1152 let mirror = self.mirror_memory();
1153 self.profiles
1154 .values()
1155 .try_fold(mirror, |total, p| {
1156 p.memory
1157 .0
1158 .checked_mul(u64::from(p.slots))?
1159 .checked_add(total)
1160 })
1161 .map(ByteSize)
1162 .ok_or(ConfigError::BudgetOverflow)
1163 }
1164
1165 pub fn memory_needed(&self) -> Result<ByteSize, ConfigError> {
1168 match self.host.admission {
1169 Admission::Caps => self.memory_caps(),
1170 Admission::Measured => {
1171 let largest = self
1172 .profiles
1173 .values()
1174 .map(|p| p.memory.0)
1175 .max()
1176 .unwrap_or(0);
1177 largest
1178 .checked_add(self.mirror_memory())
1179 .map(ByteSize)
1180 .ok_or(ConfigError::BudgetOverflow)
1181 }
1182 }
1183 }
1184
1185 pub fn runner_ceiling(&self) -> Option<u64> {
1189 self.host
1190 .memory_ceiling
1191 .map(|ceiling| ceiling.0.saturating_sub(self.mirror_memory()))
1192 }
1193
1194 fn check_memory_ceiling(&self) -> Result<(), ConfigError> {
1199 let (Some(ceiling), Some(most)) = (self.host.memory_ceiling, self.runner_ceiling()) else {
1200 return Ok(());
1201 };
1202 let largest = self
1203 .profiles
1204 .iter()
1205 .max_by_key(|(_, profile)| profile.memory.0);
1206 match largest {
1207 Some((profile, largest)) if largest.memory.0 > most => {
1208 Err(ConfigError::CeilingUnderProfile {
1209 profile: profile.clone(),
1210 needs: ByteSize(largest.memory.0.saturating_add(self.mirror_memory())),
1211 ceiling,
1212 })
1213 }
1214 _ => Ok(()),
1215 }
1216 }
1217
1218 fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1220 for (name, p) in &self.profiles {
1221 let memory = p.memory.0;
1222 let field = if p
1223 .min_memory
1224 .zip(p.max_memory)
1225 .is_some_and(|(min, max)| min.0 > max.0)
1226 {
1227 "min_memory is above its max_memory"
1228 } else if p.min_memory.is_some_and(|min| memory < min.0) {
1229 "memory is below its min_memory"
1230 } else if p.max_memory.is_some_and(|max| memory > max.0) {
1231 "memory is above its max_memory"
1232 } else if p.max_slots.is_some_and(|max| p.slots > max) {
1233 "slots are above its max_slots"
1234 } else {
1235 continue;
1236 };
1237 return Err(ConfigError::SizingBounds {
1238 profile: name.clone(),
1239 why: field,
1240 });
1241 }
1242 Ok(())
1243 }
1244
1245 fn check_pools(&self) -> Result<(), ConfigError> {
1248 for (at, first) in self.repositories.iter().enumerate() {
1249 for second in self.repositories[at + 1..]
1250 .iter()
1251 .filter(|second| second.name == first.name)
1252 {
1253 if second.profile == first.profile {
1254 return Err(ConfigError::PoolProfileTwice {
1255 repository: first.name.clone(),
1256 profile: first.profile.clone(),
1257 });
1258 }
1259 if let Some(label) = first.labels.iter().find(|label| {
1260 second
1261 .labels
1262 .iter()
1263 .any(|other| other.eq_ignore_ascii_case(label))
1264 }) {
1265 return Err(ConfigError::PoolLabelsOverlap {
1266 repository: first.name.clone(),
1267 first: first.profile.clone(),
1268 second: second.profile.clone(),
1269 label: label.clone(),
1270 });
1271 }
1272 if second.build_cache_writers != first.build_cache_writers {
1273 return Err(ConfigError::PoolWritersDiffer {
1274 repository: first.name.clone(),
1275 });
1276 }
1277 }
1278 }
1279 Ok(())
1280 }
1281
1282 pub fn pool(&self, repository: &str, labels: &[String]) -> Option<&Repository> {
1285 self.repositories
1286 .iter()
1287 .find(|pool| pool.name == repository && labels_match(labels, &pool.labels))
1288 }
1289
1290 pub fn pool_in(&self, repository: &str, profile: &str) -> Option<&Repository> {
1292 self.repositories
1293 .iter()
1294 .find(|pool| pool.name == repository && pool.profile == profile)
1295 }
1296
1297 pub fn pricing(&self) -> api::Pricing {
1299 api::Pricing {
1300 profiles: self
1301 .profiles
1302 .iter()
1303 .filter_map(|(name, profile)| Some((name.clone(), profile.priced_as?)))
1304 .collect(),
1305 default_sku: api::Sku::standard_for(std::env::consts::ARCH),
1306 }
1307 }
1308
1309 pub fn repository_names(&self) -> Vec<String> {
1311 let mut names: Vec<String> = Vec::new();
1312 for repository in &self.repositories {
1313 if !names.contains(&repository.name) {
1314 names.push(repository.name.clone());
1315 }
1316 }
1317 names
1318 }
1319
1320 pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1322 if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1323 return Err(ConfigError::UnsafeNotAllowed);
1324 }
1325 let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1326 [
1327 (p.memory.0 == 0, format!("profiles.{name}.memory")),
1328 (p.cpus == 0, format!("profiles.{name}.cpus")),
1329 (p.slots == 0, format!("profiles.{name}.slots")),
1330 (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1331 ]
1332 });
1333 let mirror = &self.caches.mirror;
1334 let mirror_zero_limits = [
1335 (
1336 mirror.enabled && mirror.memory.0 == 0,
1337 "caches.mirror.memory".to_owned(),
1338 ),
1339 (
1340 mirror.enabled && mirror.cpus == 0,
1341 "caches.mirror.cpus".to_owned(),
1342 ),
1343 (
1344 self.host.poll_interval.is_zero(),
1345 "host.poll_interval".to_owned(),
1346 ),
1347 (
1348 self.host.memory_ceiling == Some(ByteSize(0)),
1349 "host.memory_ceiling".to_owned(),
1350 ),
1351 (self.records.max_count == 0, "records.max_count".to_owned()),
1352 (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1353 (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1354 (
1355 self.sizing.raise_interval.is_zero(),
1356 "sizing.raise_interval".to_owned(),
1357 ),
1358 (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1359 (
1360 self.records.max_total.0 == 0,
1361 "records.max_total".to_owned(),
1362 ),
1363 ];
1364 if let Some((_, field)) = zero_limits
1365 .chain(mirror_zero_limits)
1366 .find(|(zero, _)| *zero)
1367 {
1368 return Err(ConfigError::ZeroLimit { field });
1369 }
1370 self.check_sizing_bounds()?;
1371 self.caches.build.validate()?;
1372 self.host.check_cpu_overcommit()?;
1373 self.check_memory_ceiling()?;
1374 self.web.validate()?;
1375 self.notify.validate()?;
1376 for repository in &self.repositories {
1377 if !self.profiles.contains_key(&repository.profile) {
1378 return Err(ConfigError::UnknownProfile {
1379 repository: repository.name.clone(),
1380 profile: repository.profile.clone(),
1381 defined: self.profiles.keys().cloned().collect(),
1382 });
1383 }
1384 if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1385 {
1386 return Err(ConfigError::MissingLabels {
1387 repository: repository.name.clone(),
1388 });
1389 }
1390 }
1391 self.check_pools()?;
1392 self.check_budget(memory_max)
1393 }
1394
1395 fn check_budget(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1397 let total = self.memory_caps()?;
1398 if self.host.admission == Admission::Measured {
1399 let Some(memory_max) = memory_max else {
1400 return Err(ConfigError::MeasuredWithoutMemoryMax {
1401 slice: self.host.slice.clone(),
1402 });
1403 };
1404 let mirror = self.mirror_memory();
1405 if let Some((profile, needs)) = self
1406 .profiles
1407 .iter()
1408 .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1409 .find(|(_, needs)| *needs > memory_max.0)
1410 {
1411 return Err(ConfigError::ProfileOverBudget {
1412 profile: profile.clone(),
1413 needs: ByteSize(needs),
1414 slice: self.host.slice.clone(),
1415 memory_max,
1416 });
1417 }
1418 let needed = self.memory_needed()?;
1420 if needed > memory_max {
1421 return Err(ConfigError::OverBudget {
1422 total: needed,
1423 slice: self.host.slice.clone(),
1424 memory_max,
1425 });
1426 }
1427 return Ok(Vec::new());
1428 }
1429 match memory_max {
1430 Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1431 total,
1432 slice: self.host.slice.clone(),
1433 memory_max,
1434 }),
1435 Some(_) => Ok(Vec::new()),
1436 None => Ok(vec![Warning::UnlimitedSlice {
1437 slice: self.host.slice.clone(),
1438 total,
1439 }]),
1440 }
1441 }
1442}
1443
1444#[derive(Debug, thiserror::Error)]
1446pub enum ResizeError {
1447 #[error(transparent)]
1449 Parse(#[from] toml_edit::TomlError),
1450 #[error("the config has no profile {0:?} under [profiles]")]
1452 NoProfile(String),
1453}
1454
1455pub fn resize_profile(
1458 text: &str,
1459 profile: &str,
1460 resize: api::Resize,
1461) -> Result<String, ResizeError> {
1462 let mut document: toml_edit::DocumentMut = text.parse()?;
1463 let table = document
1464 .get_mut("profiles")
1465 .and_then(|profiles| profiles.get_mut(profile))
1466 .and_then(toml_edit::Item::as_table_like_mut)
1467 .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1468 let values = [
1469 (
1470 "memory",
1471 toml_edit::Value::from(ByteSize(resize.memory).exact()),
1472 ),
1473 ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1474 ];
1475 let cpus = resize
1476 .cpus
1477 .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1478 for (key, mut value) in values.into_iter().chain(cpus) {
1479 if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1481 *value.decor_mut() = old.decor().clone();
1482 }
1483 table.insert(key, toml_edit::Item::Value(value));
1484 }
1485 Ok(document.to_string())
1486}
1487
1488pub fn profile_sizes(text: &str, profile: &str) -> Option<api::Resize> {
1491 let config = Config::parse(text).ok()?;
1492 let sizes = config.profiles.get(profile)?;
1493 Some(api::Resize {
1494 memory: sizes.memory.0,
1495 slots: sizes.slots,
1496 cpus: Some(sizes.cpus),
1497 })
1498}
1499
1500pub fn resized_at(
1504 text: &str,
1505 profile: &str,
1506 resize: api::Resize,
1507) -> Option<std::ops::Range<usize>> {
1508 let document = toml_edit::Document::parse(text).ok()?;
1509 let table = document
1510 .as_table()
1511 .get("profiles")?
1512 .get(profile)?
1513 .as_table_like()?;
1514 let cpus = resize.cpus.map(|_| "cpus");
1515 ["memory", "slots"]
1516 .into_iter()
1517 .chain(cpus)
1518 .filter_map(|key| {
1519 let (key, item) = table.get_key_value(key)?;
1520 Some(key.span()?.start..item.span()?.end)
1521 })
1522 .reduce(|first, next| first.start.min(next.start)..first.end.max(next.end))
1523}
1524
1525pub const SECRET_SHOWN: &str = "<secret>";
1527
1528pub fn mask_secrets(text: &str) -> Option<String> {
1532 let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1533 let mut masked = false;
1534 for target in notify_targets(&mut document) {
1535 for (_, value) in secrets_of(target) {
1536 replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1537 masked = true;
1538 }
1539 }
1540 Some(if masked {
1541 document.to_string()
1542 } else {
1543 text.to_owned()
1544 })
1545}
1546
1547pub fn keep_secrets(text: &str, stored: &str) -> String {
1554 if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1555 return text.to_owned();
1556 }
1557 let (Ok(mut document), Ok(mut old)) = (
1558 text.parse::<toml_edit::DocumentMut>(),
1559 stored.parse::<toml_edit::DocumentMut>(),
1560 ) else {
1561 return text.to_owned();
1562 };
1563 let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1564 for target in notify_targets(&mut old) {
1565 let Some(name) = target_name(target) else {
1566 continue;
1567 };
1568 let secrets = secrets_of(target)
1569 .into_iter()
1570 .map(|(field, value)| (field, value.clone()))
1571 .collect();
1572 kept.insert(name, secrets);
1573 }
1574 for target in notify_targets(&mut document) {
1575 let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1576 continue;
1577 };
1578 for (field, value) in secrets_of(target) {
1579 if value.as_str() != Some(SECRET_SHOWN) {
1580 continue;
1581 }
1582 if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1583 replace_value(value, old.clone());
1584 }
1585 }
1586 }
1587 document.to_string()
1588}
1589
1590#[derive(Debug, PartialEq, Eq)]
1592enum SecretField {
1593 Url,
1594 Header(String),
1595}
1596
1597fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1599 match document
1600 .get_mut("notify")
1601 .and_then(|notify| notify.get_mut("targets"))
1602 {
1603 Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1604 .iter_mut()
1605 .map(|table| table as &mut dyn toml_edit::TableLike)
1606 .collect(),
1607 Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1608 .iter_mut()
1609 .filter_map(toml_edit::Value::as_inline_table_mut)
1610 .map(|table| table as &mut dyn toml_edit::TableLike)
1611 .collect(),
1612 _ => Vec::new(),
1613 }
1614}
1615
1616fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1617 target
1618 .get("name")
1619 .and_then(toml_edit::Item::as_str)
1620 .map(str::to_owned)
1621}
1622
1623fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1625 let mut found = Vec::new();
1626 for (key, item) in target.iter_mut() {
1627 match key.get() {
1628 "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1629 "headers" => {
1630 let Some(headers) = item.as_table_like_mut() else {
1631 continue;
1632 };
1633 for (name, item) in headers.iter_mut() {
1634 let field = SecretField::Header(name.get().to_owned());
1635 found.extend(item.as_value_mut().map(|value| (field, value)));
1636 }
1637 }
1638 _ => {}
1639 }
1640 }
1641 found
1642}
1643
1644fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1646 *new.decor_mut() = value.decor().clone();
1647 *value = new;
1648}
1649
1650pub fn revision(text: &str) -> String {
1653 let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1654 (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1655 });
1656 format!("{hash:016x}")
1657}
1658
1659pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1661 let output = Command::new("systemctl")
1662 .args(["show", slice, "--property=MemoryMax", "--value"])
1663 .output()?;
1664 if !output.status.success() {
1665 return Err(std::io::Error::other(format!(
1666 "systemctl show {slice} failed: {}",
1667 String::from_utf8_lossy(&output.stderr).trim()
1668 )));
1669 }
1670 let value = String::from_utf8_lossy(&output.stdout);
1671 parse_memory_max(value.trim()).ok_or_else(|| {
1672 std::io::Error::other(format!(
1673 "systemctl show {slice}: unexpected MemoryMax {value:?}"
1674 ))
1675 })
1676}
1677
1678pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1680 #[expect(
1681 clippy::cast_precision_loss,
1682 clippy::cast_possible_truncation,
1683 clippy::cast_sign_loss,
1684 reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1685 most 16"
1686 )]
1687 let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1688 budget
1689}
1690
1691pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1694 let output = Command::new("systemctl")
1695 .args([
1696 "show",
1697 slice,
1698 "--property=EffectiveCPUs",
1699 "--property=AllowedCPUs",
1700 "--property=CPUQuotaPerSecUSec",
1701 ])
1702 .output()?;
1703 if !output.status.success() {
1704 return Err(std::io::Error::other(format!(
1705 "systemctl show {slice} failed: {}",
1706 String::from_utf8_lossy(&output.stderr).trim()
1707 )));
1708 }
1709 let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1710 parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1711 std::io::Error::other(format!(
1712 "systemctl show {slice}: unexpected CPU properties {:?}",
1713 String::from_utf8_lossy(&output.stdout).trim()
1714 ))
1715 })
1716}
1717
1718pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1721 let value = |name: &str| {
1722 properties.lines().find_map(|line| {
1723 line.strip_prefix(name)
1724 .and_then(|rest| rest.strip_prefix('='))
1725 .map(str::trim)
1726 })
1727 };
1728 let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
1729 .into_iter()
1730 .flatten()
1731 .find(|cpus| !cpus.is_empty());
1732 let cpus = match set {
1733 Some(set) => cpu_count(set)?,
1734 None => host,
1735 };
1736 let quota = match value("CPUQuotaPerSecUSec") {
1737 None | Some("" | "infinity") => u64::MAX,
1738 Some(quota) => quota_millicores(quota)?,
1739 };
1740 Some(cpus.saturating_mul(1000).min(quota))
1741}
1742
1743fn cpu_count(set: &str) -> Option<u64> {
1745 set.split([',', ' '])
1746 .filter(|range| !range.is_empty())
1747 .try_fold(0u64, |count, range| {
1748 let (first, last) = range.split_once('-').unwrap_or((range, range));
1749 let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
1750 Some(count + last.checked_sub(first)? + 1)
1751 })
1752}
1753
1754fn quota_millicores(quota: &str) -> Option<u64> {
1757 const UNITS: [(&str, f64); 10] = [
1759 ("y", 31_557_600_000.0),
1760 ("M", 2_629_800_000.0),
1761 ("w", 604_800_000.0),
1762 ("d", 86_400_000.0),
1763 ("h", 3_600_000.0),
1764 ("min", 60_000.0),
1765 ("s", 1000.0),
1766 ("ms", 1.0),
1767 ("us", 0.001),
1768 ("µs", 0.001),
1769 ];
1770 let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
1771 let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
1772 let (number, unit) = part.split_at(at);
1773 let per_unit = UNITS
1774 .iter()
1775 .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
1776 Some(total + number.parse::<f64>().ok()? * per_unit)
1777 })?;
1778 #[allow(
1779 clippy::cast_possible_truncation,
1780 clippy::cast_sign_loss,
1781 reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
1782 )]
1783 (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
1784 .then(|| millicores.round() as u64)
1785}
1786
1787pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
1789 if value == "infinity" {
1790 Some(None)
1791 } else {
1792 value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
1793 }
1794}
1795
1796#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1798pub struct ByteSize(pub u64);
1799
1800const UNITS: [(char, u64); 4] = [
1801 ('T', 1 << 40),
1802 ('G', 1 << 30),
1803 ('M', 1 << 20),
1804 ('K', 1 << 10),
1805];
1806
1807impl FromStr for ByteSize {
1808 type Err = String;
1809
1810 #[allow(
1811 clippy::cast_precision_loss,
1812 clippy::cast_possible_truncation,
1813 clippy::cast_sign_loss,
1814 reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
1815 )]
1816 fn from_str(s: &str) -> Result<Self, Self::Err> {
1817 let invalid = || {
1818 format!(
1819 "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
1820 )
1821 };
1822 let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
1823 let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
1824 let number = &s[..s.len() - 1];
1825 if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
1826 return Err(invalid());
1827 }
1828 let number: f64 = number.parse().map_err(|_| invalid())?;
1829 let bytes = (number * *factor as f64).round();
1830 if bytes >= u64::MAX as f64 {
1832 return Err(format!("size {s:?} is too large"));
1833 }
1834 Ok(ByteSize(bytes as u64))
1835 }
1836}
1837
1838impl fmt::Display for ByteSize {
1839 #[allow(
1840 clippy::cast_precision_loss,
1841 reason = "shown with two decimals, far coarser than f64's precision"
1842 )]
1843 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1844 let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
1845 return write!(f, "{}B", self.0);
1846 };
1847 let number = format!("{:.2}", self.0 as f64 / *factor as f64);
1848 let number = number.trim_end_matches('0').trim_end_matches('.');
1849 write!(f, "{number}{unit}")
1850 }
1851}
1852
1853impl ByteSize {
1854 #[allow(
1858 clippy::cast_precision_loss,
1859 reason = "a unit is a power of two, so the quotient is exact below 2^53 bytes; the parse checks it"
1860 )]
1861 pub fn exact(self) -> String {
1862 let reads_back = |text: &String| text.parse::<ByteSize>() == Ok(self);
1863 let shown = self.to_string();
1864 if reads_back(&shown) {
1865 return shown;
1866 }
1867 let mut shortest: Option<String> = None;
1868 for (unit, factor) in UNITS {
1869 let text = format!("{}{unit}", self.0 as f64 / factor as f64);
1870 if reads_back(&text) && shortest.as_ref().is_none_or(|best| text.len() < best.len()) {
1871 shortest = Some(text);
1872 }
1873 }
1874 shortest.unwrap_or(shown)
1875 }
1876
1877 #[allow(
1881 clippy::cast_precision_loss,
1882 reason = "shown with one decimal at most, far coarser than f64's precision"
1883 )]
1884 pub fn short(self) -> String {
1885 let Some(index) = UNITS.iter().position(|(_, factor)| self.0 >= *factor) else {
1886 return format!("{}B", self.0);
1887 };
1888 let (mut unit, factor) = UNITS[index];
1889 let mut value = self.0 as f64 / factor as f64;
1890 if value.round() >= 1024.0
1891 && let Some(larger) = index.checked_sub(1)
1892 {
1893 unit = UNITS[larger].0;
1894 value /= 1024.0;
1895 }
1896 let tenths = (value * 10.0).round() / 10.0;
1898 if tenths < 10.0 {
1899 let number = format!("{tenths:.1}");
1900 format!("{}{unit}", number.trim_end_matches(".0"))
1901 } else {
1902 format!("{:.0}{unit}", value.round())
1903 }
1904 }
1905}
1906
1907impl<'de> Deserialize<'de> for ByteSize {
1908 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1909 String::deserialize(deserializer)?
1910 .parse()
1911 .map_err(de::Error::custom)
1912 }
1913}
1914
1915pub fn parse_duration(s: &str) -> Result<Duration, String> {
1917 let invalid = || {
1918 format!(
1919 "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
1920 )
1921 };
1922 let (number, factor) = match s.chars().last() {
1923 Some('s') => (&s[..s.len() - 1], 1),
1924 Some('m') => (&s[..s.len() - 1], 60),
1925 Some('h') => (&s[..s.len() - 1], 3600),
1926 Some('d') => (&s[..s.len() - 1], 86400),
1927 _ => return Err(invalid()),
1928 };
1929 let number: u64 = number.parse().map_err(|_| invalid())?;
1930 number
1931 .checked_mul(factor)
1932 .map(Duration::from_secs)
1933 .ok_or_else(invalid)
1934}
1935
1936pub fn parse_age(text: &str) -> Result<Duration, String> {
1938 let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
1939 let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
1940 let (number, unit) = text.split_at(split);
1941 let number: u64 = number.parse().map_err(|_| error())?;
1942 let unit = match unit {
1943 "s" => 1,
1944 "m" => 60,
1945 "h" => 3600,
1946 "d" => 86_400,
1947 "w" => 7 * 86_400,
1948 _ => return Err(error()),
1949 };
1950 Ok(Duration::from_secs(number.saturating_mul(unit)))
1951}
1952
1953fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
1954 parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
1955}
1956
1957#[cfg(test)]
1958mod tests;