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