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
17#[derive(Debug, Deserialize)]
19#[serde(deny_unknown_fields)]
20pub struct Config {
21 pub host: Host,
23 pub caches: Caches,
25 pub profiles: BTreeMap<String, Profile>,
27 pub repositories: Vec<Repository>,
29 #[serde(default)]
31 pub history: History,
32 #[serde(default)]
34 pub records: Records,
35 #[serde(default)]
37 pub sizing: Sizing,
38 #[serde(default)]
40 pub web: Web,
41 #[serde(default)]
43 pub notify: Notify,
44}
45
46#[derive(Debug, Deserialize)]
48#[serde(deny_unknown_fields)]
49pub struct Host {
50 pub slice: String,
52 pub isolation: Isolation,
54 #[serde(default)]
56 pub allow_unsafe: bool,
57 #[serde(default = "default_poll_interval", deserialize_with = "duration")]
59 pub poll_interval: Duration,
60 #[serde(default)]
62 pub admission: Admission,
63 #[serde(default)]
66 pub cpu_overcommit: Option<f64>,
67}
68
69impl Host {
70 fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
72 match self.cpu_overcommit {
73 Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
74 Err(ConfigError::CpuOvercommit { value })
75 }
76 _ => Ok(()),
77 }
78 }
79}
80
81pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
83
84#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
86#[serde(rename_all = "kebab-case")]
87pub enum Admission {
88 #[default]
90 Caps,
91 Measured,
94}
95
96fn default_poll_interval() -> Duration {
97 Duration::from_secs(15)
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
102#[serde(rename_all = "kebab-case")]
103pub enum Isolation {
104 Sysbox,
106 UnsafeDind,
108}
109
110#[derive(Debug, Clone, Deserialize)]
112#[serde(deny_unknown_fields)]
113pub struct Caches {
114 pub mirror: Mirror,
116 pub pnpm: CacheLimit,
118 pub maven: CacheLimit,
120 #[serde(default = "default_reseed_at")]
123 pub reseed_at: ByteSize,
124 #[serde(default = "default_snapshot_max")]
127 pub snapshot_max: ByteSize,
128 #[serde(default)]
130 pub build: BuildCache,
131}
132
133#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
136#[serde(deny_unknown_fields, default)]
137pub struct BuildCache {
138 pub enabled: bool,
140 pub max_size: ByteSize,
142 pub branches: Vec<String>,
145 pub directory: Option<PathBuf>,
147}
148
149impl BuildCache {
150 fn validate(&self) -> Result<(), ConfigError> {
151 if self.enabled && self.max_size.0 == 0 {
152 return Err(ConfigError::ZeroLimit {
153 field: "caches.build.max_size".to_owned(),
154 });
155 }
156 Ok(())
157 }
158}
159
160impl Default for BuildCache {
161 fn default() -> BuildCache {
162 BuildCache {
163 enabled: true,
164 max_size: ByteSize(10 << 30),
165 branches: Vec::new(),
166 directory: None,
167 }
168 }
169}
170
171fn default_reseed_at() -> ByteSize {
172 ByteSize(2 << 30)
173}
174
175fn default_snapshot_max() -> ByteSize {
177 ByteSize(10 << 30)
178}
179
180fn default_mirror_max_size() -> ByteSize {
181 ByteSize(10 << 30)
182}
183
184#[derive(Debug, Clone, Deserialize)]
186#[serde(deny_unknown_fields)]
187pub struct Mirror {
188 pub enabled: bool,
190 pub memory: ByteSize,
192 pub cpus: u32,
194 #[serde(deserialize_with = "duration")]
196 pub ttl: Duration,
197 #[serde(default = "default_mirror_max_size")]
199 pub max_size: ByteSize,
200}
201
202#[derive(Debug, Clone, Deserialize)]
204#[serde(deny_unknown_fields)]
205pub struct CacheLimit {
206 pub max_size: ByteSize,
208}
209
210pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
213
214#[derive(Debug, Clone, Deserialize)]
216#[serde(try_from = "ProfileToml")]
217pub struct Profile {
218 pub image: Image,
220 pub cpus: u32,
222 pub memory: ByteSize,
224 pub slots: u32,
226 pub record_failures: bool,
228 pub min_memory: Option<ByteSize>,
230 pub max_memory: Option<ByteSize>,
232 pub max_slots: Option<u32>,
234}
235
236#[derive(Debug, Clone, PartialEq, Eq)]
238pub enum Image {
239 Stage(String),
241 Pulled(String),
244}
245
246impl Image {
247 pub fn reference(&self) -> String {
249 match self {
250 Image::Stage(stage) => format!("runnerd-runner:{stage}"),
251 Image::Pulled(reference) => reference.clone(),
252 }
253 }
254}
255
256#[derive(Deserialize)]
258#[serde(deny_unknown_fields)]
259struct ProfileToml {
260 image_stage: Option<String>,
261 image: Option<String>,
262 cpus: u32,
263 memory: ByteSize,
264 slots: u32,
265 #[serde(default)]
266 record_failures: bool,
267 min_memory: Option<ByteSize>,
268 max_memory: Option<ByteSize>,
269 max_slots: Option<u32>,
270}
271
272impl TryFrom<ProfileToml> for Profile {
273 type Error = String;
274
275 fn try_from(profile: ProfileToml) -> Result<Profile, String> {
276 let image = match (profile.image_stage, profile.image) {
277 (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
278 (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
279 (Some(_), Some(_)) => {
280 return Err(
281 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
282 not both"
283 .to_owned(),
284 );
285 }
286 _ => {
287 return Err(
288 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
289 .to_owned(),
290 );
291 }
292 };
293 Ok(Profile {
294 image,
295 cpus: profile.cpus,
296 memory: profile.memory,
297 slots: profile.slots,
298 record_failures: profile.record_failures,
299 min_memory: profile.min_memory,
300 max_memory: profile.max_memory,
301 max_slots: profile.max_slots,
302 })
303 }
304}
305
306#[derive(Debug, Deserialize)]
308#[serde(deny_unknown_fields)]
309pub struct Repository {
310 pub name: String,
312 pub profile: String,
314 pub labels: Vec<String>,
316 #[serde(default = "default_warm")]
319 pub warm: u32,
320 #[serde(default = "default_true")]
322 pub build_cache: bool,
323}
324
325fn default_true() -> bool {
326 true
327}
328
329fn default_warm() -> u32 {
330 1
331}
332
333#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
335#[serde(deny_unknown_fields, default)]
336pub struct History {
337 pub database: Option<PathBuf>,
339 #[serde(deserialize_with = "duration")]
341 pub samples: Duration,
342 #[serde(deserialize_with = "duration")]
344 pub logs: Duration,
345 #[serde(deserialize_with = "duration")]
347 pub events: Duration,
348 #[serde(deserialize_with = "duration")]
350 pub jobs: Duration,
351 pub log_max: ByteSize,
353}
354
355impl Default for History {
356 fn default() -> History {
357 History {
358 database: None,
359 samples: Duration::from_hours(7 * 24),
360 logs: Duration::from_hours(30 * 24),
361 events: Duration::from_hours(30 * 24),
362 jobs: Duration::from_hours(90 * 24),
363 log_max: ByteSize(50 << 20),
364 }
365 }
366}
367
368#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
371#[serde(deny_unknown_fields, default)]
372pub struct Records {
373 pub max_total: ByteSize,
375 pub max_count: u32,
377 #[serde(deserialize_with = "duration")]
379 pub max_age: Duration,
380 pub min_free: ByteSize,
382}
383
384impl Default for Records {
385 fn default() -> Records {
386 Records {
387 max_total: ByteSize(20 << 30),
388 max_count: 10,
389 max_age: Duration::from_hours(7 * 24),
390 min_free: ByteSize(10 << 30),
391 }
392 }
393}
394
395#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
398#[serde(deny_unknown_fields, default)]
399pub struct Sizing {
400 pub margin: u32,
403 pub min_jobs: u32,
406 pub auto: bool,
409 #[serde(deserialize_with = "duration")]
412 pub interval: Duration,
413 #[serde(deserialize_with = "duration")]
416 pub raise_interval: Duration,
417 pub max_step: u32,
419}
420
421impl Sizing {
422 pub fn raise_after(&self) -> Duration {
425 self.raise_interval.min(self.interval)
426 }
427}
428
429impl Default for Sizing {
430 fn default() -> Sizing {
431 Sizing {
432 margin: 15,
433 min_jobs: 10,
434 auto: false,
435 interval: Duration::from_hours(24),
436 raise_interval: Duration::from_hours(1),
437 max_step: 25,
438 }
439 }
440}
441
442#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
444#[serde(deny_unknown_fields, default)]
445pub struct Web {
446 pub origins: Vec<Origin>,
449 pub open: Option<Origin>,
452}
453
454impl Web {
455 fn validate(&self) -> Result<(), ConfigError> {
457 match &self.open {
458 Some(open) if !self.origins.contains(open) => {
459 Err(ConfigError::OpenNotListed { open: open.clone() })
460 }
461 _ => Ok(()),
462 }
463 }
464}
465
466#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
468#[serde(deny_unknown_fields, default)]
469pub struct Notify {
470 pub digest_at: DayTime,
472 pub targets: Vec<NotifyTarget>,
474}
475
476impl Default for Notify {
477 fn default() -> Notify {
478 Notify {
479 digest_at: DayTime { minutes: 9 * 60 },
480 targets: Vec::new(),
481 }
482 }
483}
484
485impl Notify {
486 fn validate(&self) -> Result<(), ConfigError> {
489 let refuse = |target: &str, why: &'static str| {
490 Err(ConfigError::Notify {
491 target: target.to_owned(),
492 why,
493 })
494 };
495 for (index, target) in self.targets.iter().enumerate() {
496 let name = target.name.as_str();
497 if name.trim().is_empty() || name.chars().any(char::is_control) {
498 return refuse(name, "name is empty");
499 }
500 if self.targets[..index].iter().any(|other| other.name == name) {
501 return refuse(name, "another target has this name");
502 }
503 let url = target.url.expose();
504 if url == SECRET_SHOWN {
505 return refuse(
506 name,
507 "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
508 );
509 }
510 let Some(rest) = url
511 .strip_prefix("https://")
512 .or_else(|| url.strip_prefix("http://"))
513 else {
514 return refuse(name, "url does not start with https:// or http://");
515 };
516 let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
517 if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
518 return refuse(name, "url is not a URL");
519 }
520 if target.kind == NotifyKind::Ntfy
521 && (path.is_empty() || path.contains(['/', '?', '#']))
522 {
523 return refuse(
524 name,
525 "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
526 );
527 }
528 for (header, value) in &target.headers {
529 if value.expose() == SECRET_SHOWN {
530 return refuse(
531 name,
532 "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
533 );
534 }
535 let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
536 if header.is_empty() || !header.chars().all(token) {
537 return refuse(name, "a header's name is not a header name");
538 }
539 if value.expose().chars().any(char::is_control) {
540 return refuse(name, "a header's value has a control character");
541 }
542 }
543 if target.events.contains(&api::NotifyEvent::Test) {
544 return refuse(
545 name,
546 "events lists test, which only runnerctl notify test sends",
547 );
548 }
549 }
550 Ok(())
551 }
552}
553
554#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
556#[serde(deny_unknown_fields)]
557pub struct NotifyTarget {
558 pub name: String,
560 pub kind: NotifyKind,
562 pub url: Secret,
564 #[serde(default)]
566 pub headers: BTreeMap<String, Secret>,
567 #[serde(default = "default_notify_events")]
569 pub events: Vec<api::NotifyEvent>,
570}
571
572fn default_notify_events() -> Vec<api::NotifyEvent> {
573 vec![
574 api::NotifyEvent::Failed,
575 api::NotifyEvent::Lost,
576 api::NotifyEvent::Runner,
577 api::NotifyEvent::Insights,
578 ]
579}
580
581#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
583#[serde(rename_all = "kebab-case")]
584pub enum NotifyKind {
585 Webhook,
587 Ntfy,
589}
590
591#[derive(Clone, PartialEq, Eq, Deserialize)]
593#[serde(transparent)]
594pub struct Secret(String);
595
596impl Secret {
597 pub fn expose(&self) -> &str {
599 &self.0
600 }
601}
602
603impl From<&str> for Secret {
604 fn from(value: &str) -> Secret {
605 Secret(value.to_owned())
606 }
607}
608
609impl fmt::Debug for Secret {
610 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
611 f.write_str("<secret>")
612 }
613}
614
615#[derive(Debug, Clone, Copy, PartialEq, Eq)]
617pub struct DayTime {
618 pub minutes: u32,
620}
621
622impl FromStr for DayTime {
623 type Err = String;
624
625 fn from_str(text: &str) -> Result<DayTime, String> {
626 let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
627 let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
628 let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
629 if !digits(hour) || !digits(minute) {
630 return Err(invalid());
631 }
632 let (hour, minute): (u32, u32) = (
633 hour.parse().map_err(|_| invalid())?,
634 minute.parse().map_err(|_| invalid())?,
635 );
636 if hour > 23 || minute > 59 {
637 return Err(invalid());
638 }
639 Ok(DayTime {
640 minutes: hour * 60 + minute,
641 })
642 }
643}
644
645impl fmt::Display for DayTime {
646 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
647 write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
648 }
649}
650
651impl<'de> Deserialize<'de> for DayTime {
652 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
653 String::deserialize(deserializer)?
654 .parse()
655 .map_err(de::Error::custom)
656 }
657}
658
659#[derive(Debug, Clone, PartialEq, Eq)]
663pub struct Origin(String);
664
665impl Origin {
666 pub fn as_str(&self) -> &str {
668 &self.0
669 }
670}
671
672impl FromStr for Origin {
673 type Err = String;
674
675 fn from_str(s: &str) -> Result<Origin, String> {
676 let invalid = |why: &str| {
677 format!(
678 "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
679 \"https://host.example.ts.net:8443\""
680 )
681 };
682 let (scheme, rest) = s
683 .split_once("://")
684 .ok_or_else(|| invalid("it has no scheme"))?;
685 let scheme = scheme.to_ascii_lowercase();
686 if rest.contains(['/', '?', '#']) {
687 return Err(invalid("it has a path"));
688 }
689 if rest.contains('@') {
690 return Err(invalid("it names a user"));
691 }
692 let (host, port) = match rest.strip_prefix('[') {
693 Some(v6) => {
694 let (address, after) = v6
695 .split_once(']')
696 .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
697 let port = match after {
698 "" => None,
699 _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
700 };
701 (format!("[{address}]"), port)
702 }
703 None => match rest.split_once(':') {
704 Some((host, port)) => (host.to_owned(), Some(port)),
705 None => (rest.to_owned(), None),
706 },
707 };
708 let host = host.to_ascii_lowercase();
709 let not_a_host = || invalid("its host is not a name or an address");
710 let (host, loopback) =
712 if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
713 let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
714 (format!("[{}]", ipv6_origin(address)), address.is_loopback())
715 } else {
716 if host.is_empty()
717 || !host
718 .chars()
719 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
720 {
721 return Err(not_a_host());
722 }
723 let name = host.strip_suffix('.').unwrap_or(&host);
725 let last = name.rsplit('.').next().unwrap_or_default();
726 let numeric = !last.is_empty()
727 && (last.bytes().all(|byte| byte.is_ascii_digit())
728 || last
729 .strip_prefix("0x")
730 .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
731 if numeric {
732 let address: Ipv4Addr = name.parse().map_err(|_| {
733 invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
734 })?;
735 (address.to_string(), address.is_loopback())
736 } else {
737 let loopback = host == "localhost";
738 (host, loopback)
739 }
740 };
741 let port = port
742 .map(|port| {
743 Some(port)
744 .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
745 .and_then(|port| port.parse::<u16>().ok())
746 .filter(|port| *port != 0)
747 .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
748 })
749 .transpose()?;
750 let default_port = match scheme.as_str() {
751 "https" => 443,
752 "http" if loopback => 80,
753 "http" => {
754 return Err(invalid(
755 "http is for a loopback host only, since the session cookie would cross the network in \
756 the clear; use https",
757 ));
758 }
759 _ => return Err(invalid("its scheme is not https")),
760 };
761 Ok(Origin(match port {
762 Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
763 _ => format!("{scheme}://{host}"),
764 }))
765 }
766}
767
768fn ipv6_origin(address: Ipv6Addr) -> String {
771 let pieces = address.segments();
772 let (mut zeros_at, mut zeros) = (0, 0);
773 let mut at = 0;
774 while at < pieces.len() {
775 let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
776 if run > zeros {
777 (zeros_at, zeros) = (at, run);
778 }
779 at += run.max(1);
780 }
781 let hex = |pieces: &[u16]| {
782 pieces
783 .iter()
784 .map(|piece| format!("{piece:x}"))
785 .collect::<Vec<_>>()
786 .join(":")
787 };
788 if zeros < 2 {
789 return hex(&pieces);
790 }
791 format!(
792 "{}::{}",
793 hex(&pieces[..zeros_at]),
794 hex(&pieces[zeros_at + zeros..])
795 )
796}
797
798impl fmt::Display for Origin {
799 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
800 f.write_str(&self.0)
801 }
802}
803
804impl<'de> Deserialize<'de> for Origin {
805 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
806 String::deserialize(deserializer)?
807 .parse()
808 .map_err(de::Error::custom)
809 }
810}
811
812#[derive(Debug, thiserror::Error)]
814pub enum ConfigError {
815 #[error("cannot read {}: {source}", path.display())]
817 Read {
818 path: PathBuf,
820 source: std::io::Error,
822 },
823 #[error(transparent)]
825 Parse(#[from] toml::de::Error),
826 #[error(
828 "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
829 defined_profiles(.defined)
830 )]
831 UnknownProfile {
832 repository: String,
834 profile: String,
836 defined: Vec<String>,
838 },
839 #[error("repository {repository} needs at least one non-empty label")]
841 MissingLabels {
842 repository: String,
844 },
845 #[error(
847 "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
848 set allow_unsafe = true under [host] to use it anyway"
849 )]
850 UnsafeNotAllowed,
851 #[error("{field} is 0; set it above 0")]
853 ZeroLimit {
854 field: String,
856 },
857 #[error(
859 "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
860 )]
861 CpuOvercommit {
862 value: f64,
864 },
865 #[error(
867 "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
868 )]
869 OpenNotListed {
870 open: Origin,
872 },
873 #[error("profiles.{profile}: {why}")]
876 SizingBounds {
877 profile: String,
879 why: &'static str,
881 },
882 #[error("notify target {target:?}: {why}")]
884 Notify {
885 target: String,
887 why: &'static str,
889 },
890 #[error("slots × memory over all profiles plus the mirror is too large to add up")]
892 BudgetOverflow,
893 #[error(
895 "admission = \"measured\" admits runners against the slice {slice}'s MemoryMax, and it has none; install \
896 a unit file for it (`runnerd doctor` prints one)"
897 )]
898 MeasuredWithoutMemoryMax {
899 slice: String,
901 },
902 #[error(
904 "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
905 its `memory` or raise the slice's MemoryMax"
906 )]
907 ProfileOverBudget {
908 profile: String,
910 needs: ByteSize,
912 slice: String,
914 memory_max: ByteSize,
916 },
917 #[error(
919 "slots × memory over all profiles plus the mirror is {total}, \
920 the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
921 MemoryMax (`runnerd doctor` prints its unit)"
922 )]
923 OverBudget {
924 total: ByteSize,
926 slice: String,
928 memory_max: ByteSize,
930 },
931}
932
933fn defined_profiles(defined: &[String]) -> String {
934 if defined.is_empty() {
935 "it defines none".to_owned()
936 } else {
937 format!("defined: {}", defined.join(", "))
938 }
939}
940
941#[derive(Debug, Clone, PartialEq, Eq)]
943pub enum Warning {
944 UnlimitedSlice {
946 slice: String,
948 total: ByteSize,
950 },
951}
952
953impl fmt::Display for Warning {
954 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
955 match self {
956 Warning::UnlimitedSlice { slice, total } => write!(
957 f,
958 "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
959 install a unit file for it"
960 ),
961 }
962 }
963}
964
965impl Config {
966 pub fn load(path: &Path) -> Result<Config, ConfigError> {
968 let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
969 path: path.to_owned(),
970 source,
971 })?;
972 Config::parse(&text)
973 }
974
975 pub fn parse(text: &str) -> Result<Config, ConfigError> {
977 Ok(toml::from_str(text)?)
978 }
979
980 pub fn check(
983 text: &str,
984 memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
985 ) -> (Option<Config>, api::ConfigCheck) {
986 let invalid = |message: String, memory_caps: Option<ByteSize>, max: Option<ByteSize>| {
987 api::ConfigCheck {
988 valid: false,
989 message,
990 warnings: Vec::new(),
991 memory_caps: memory_caps.map(|caps| caps.0),
992 memory_max: max.map(|max| max.0),
993 revision: None,
994 }
995 };
996 let config = match Config::parse(text) {
997 Ok(config) => config,
998 Err(error) => return (None, invalid(error.to_string(), None, None)),
999 };
1000 let caps = config.memory_caps().ok();
1001 let max = match memory_max(&config.host.slice) {
1002 Ok(max) => max,
1003 Err(error) => return (None, invalid(error.to_string(), caps, None)),
1004 };
1005 match config
1006 .validate(max)
1007 .and_then(|warnings| Ok((warnings, config.memory_caps()?)))
1008 {
1009 Ok((warnings, total)) => {
1010 let message = match (config.host.admission, max) {
1011 (Admission::Measured, Some(max)) if total > max => format!(
1012 "valid, memory caps sum to {total}, over the {max} MemoryMax: admission = \"measured\" \
1013 starts what the predicted peaks fit"
1014 ),
1015 _ => format!("valid, memory caps sum to {total}"),
1016 };
1017 let check = api::ConfigCheck {
1018 valid: true,
1019 message,
1020 warnings: warnings.iter().map(ToString::to_string).collect(),
1021 memory_caps: Some(total.0),
1022 memory_max: max.map(|max| max.0),
1023 revision: None,
1024 };
1025 (Some(config), check)
1026 }
1027 Err(error) => (None, invalid(error.to_string(), caps, max)),
1028 }
1029 }
1030
1031 pub fn mirror_memory(&self) -> u64 {
1033 if self.caches.mirror.enabled {
1034 self.caches.mirror.memory.0
1035 } else {
1036 0
1037 }
1038 }
1039
1040 pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1042 let mirror = self.mirror_memory();
1043 self.profiles
1044 .values()
1045 .try_fold(mirror, |total, p| {
1046 p.memory
1047 .0
1048 .checked_mul(u64::from(p.slots))?
1049 .checked_add(total)
1050 })
1051 .map(ByteSize)
1052 .ok_or(ConfigError::BudgetOverflow)
1053 }
1054
1055 fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1057 for (name, p) in &self.profiles {
1058 let memory = p.memory.0;
1059 let field = if p
1060 .min_memory
1061 .zip(p.max_memory)
1062 .is_some_and(|(min, max)| min.0 > max.0)
1063 {
1064 "min_memory is above its max_memory"
1065 } else if p.min_memory.is_some_and(|min| memory < min.0) {
1066 "memory is below its min_memory"
1067 } else if p.max_memory.is_some_and(|max| memory > max.0) {
1068 "memory is above its max_memory"
1069 } else if p.max_slots.is_some_and(|max| p.slots > max) {
1070 "slots are above its max_slots"
1071 } else {
1072 continue;
1073 };
1074 return Err(ConfigError::SizingBounds {
1075 profile: name.clone(),
1076 why: field,
1077 });
1078 }
1079 Ok(())
1080 }
1081
1082 pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1084 if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1085 return Err(ConfigError::UnsafeNotAllowed);
1086 }
1087 let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1088 [
1089 (p.memory.0 == 0, format!("profiles.{name}.memory")),
1090 (p.cpus == 0, format!("profiles.{name}.cpus")),
1091 (p.slots == 0, format!("profiles.{name}.slots")),
1092 (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1093 ]
1094 });
1095 let mirror = &self.caches.mirror;
1096 let mirror_zero_limits = [
1097 (
1098 mirror.enabled && mirror.memory.0 == 0,
1099 "caches.mirror.memory".to_owned(),
1100 ),
1101 (
1102 mirror.enabled && mirror.cpus == 0,
1103 "caches.mirror.cpus".to_owned(),
1104 ),
1105 (
1106 self.host.poll_interval.is_zero(),
1107 "host.poll_interval".to_owned(),
1108 ),
1109 (self.records.max_count == 0, "records.max_count".to_owned()),
1110 (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1111 (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1112 (
1113 self.sizing.raise_interval.is_zero(),
1114 "sizing.raise_interval".to_owned(),
1115 ),
1116 (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1117 (
1118 self.records.max_total.0 == 0,
1119 "records.max_total".to_owned(),
1120 ),
1121 ];
1122 if let Some((_, field)) = zero_limits
1123 .chain(mirror_zero_limits)
1124 .find(|(zero, _)| *zero)
1125 {
1126 return Err(ConfigError::ZeroLimit { field });
1127 }
1128 self.check_sizing_bounds()?;
1129 self.caches.build.validate()?;
1130 self.host.check_cpu_overcommit()?;
1131 self.web.validate()?;
1132 self.notify.validate()?;
1133 for repository in &self.repositories {
1134 if !self.profiles.contains_key(&repository.profile) {
1135 return Err(ConfigError::UnknownProfile {
1136 repository: repository.name.clone(),
1137 profile: repository.profile.clone(),
1138 defined: self.profiles.keys().cloned().collect(),
1139 });
1140 }
1141 if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1142 {
1143 return Err(ConfigError::MissingLabels {
1144 repository: repository.name.clone(),
1145 });
1146 }
1147 }
1148 let total = self.memory_caps()?;
1149 if self.host.admission == Admission::Measured {
1150 let Some(memory_max) = memory_max else {
1151 return Err(ConfigError::MeasuredWithoutMemoryMax {
1152 slice: self.host.slice.clone(),
1153 });
1154 };
1155 let mirror = self.mirror_memory();
1156 if let Some((profile, needs)) = self
1157 .profiles
1158 .iter()
1159 .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1160 .find(|(_, needs)| *needs > memory_max.0)
1161 {
1162 return Err(ConfigError::ProfileOverBudget {
1163 profile: profile.clone(),
1164 needs: ByteSize(needs),
1165 slice: self.host.slice.clone(),
1166 memory_max,
1167 });
1168 }
1169 return Ok(Vec::new());
1170 }
1171 match memory_max {
1172 Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1173 total,
1174 slice: self.host.slice.clone(),
1175 memory_max,
1176 }),
1177 Some(_) => Ok(Vec::new()),
1178 None => Ok(vec![Warning::UnlimitedSlice {
1179 slice: self.host.slice.clone(),
1180 total,
1181 }]),
1182 }
1183 }
1184}
1185
1186#[derive(Debug, thiserror::Error)]
1188pub enum ResizeError {
1189 #[error(transparent)]
1191 Parse(#[from] toml_edit::TomlError),
1192 #[error("the config has no profile {0:?} under [profiles]")]
1194 NoProfile(String),
1195}
1196
1197pub fn resize_profile(
1200 text: &str,
1201 profile: &str,
1202 resize: api::Resize,
1203) -> Result<String, ResizeError> {
1204 let mut document: toml_edit::DocumentMut = text.parse()?;
1205 let table = document
1206 .get_mut("profiles")
1207 .and_then(|profiles| profiles.get_mut(profile))
1208 .and_then(toml_edit::Item::as_table_like_mut)
1209 .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1210 let values = [
1211 (
1212 "memory",
1213 toml_edit::Value::from(ByteSize(resize.memory).to_string()),
1214 ),
1215 ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1216 ];
1217 let cpus = resize
1218 .cpus
1219 .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1220 for (key, mut value) in values.into_iter().chain(cpus) {
1221 if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1223 *value.decor_mut() = old.decor().clone();
1224 }
1225 table.insert(key, toml_edit::Item::Value(value));
1226 }
1227 Ok(document.to_string())
1228}
1229
1230pub const SECRET_SHOWN: &str = "<secret>";
1232
1233pub fn mask_secrets(text: &str) -> Option<String> {
1237 let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1238 let mut masked = false;
1239 for target in notify_targets(&mut document) {
1240 for (_, value) in secrets_of(target) {
1241 replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1242 masked = true;
1243 }
1244 }
1245 Some(if masked {
1246 document.to_string()
1247 } else {
1248 text.to_owned()
1249 })
1250}
1251
1252pub fn keep_secrets(text: &str, stored: &str) -> String {
1259 if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1260 return text.to_owned();
1261 }
1262 let (Ok(mut document), Ok(mut old)) = (
1263 text.parse::<toml_edit::DocumentMut>(),
1264 stored.parse::<toml_edit::DocumentMut>(),
1265 ) else {
1266 return text.to_owned();
1267 };
1268 let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1269 for target in notify_targets(&mut old) {
1270 let Some(name) = target_name(target) else {
1271 continue;
1272 };
1273 let secrets = secrets_of(target)
1274 .into_iter()
1275 .map(|(field, value)| (field, value.clone()))
1276 .collect();
1277 kept.insert(name, secrets);
1278 }
1279 for target in notify_targets(&mut document) {
1280 let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1281 continue;
1282 };
1283 for (field, value) in secrets_of(target) {
1284 if value.as_str() != Some(SECRET_SHOWN) {
1285 continue;
1286 }
1287 if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1288 replace_value(value, old.clone());
1289 }
1290 }
1291 }
1292 document.to_string()
1293}
1294
1295#[derive(Debug, PartialEq, Eq)]
1297enum SecretField {
1298 Url,
1299 Header(String),
1300}
1301
1302fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1304 match document
1305 .get_mut("notify")
1306 .and_then(|notify| notify.get_mut("targets"))
1307 {
1308 Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1309 .iter_mut()
1310 .map(|table| table as &mut dyn toml_edit::TableLike)
1311 .collect(),
1312 Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1313 .iter_mut()
1314 .filter_map(toml_edit::Value::as_inline_table_mut)
1315 .map(|table| table as &mut dyn toml_edit::TableLike)
1316 .collect(),
1317 _ => Vec::new(),
1318 }
1319}
1320
1321fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1322 target
1323 .get("name")
1324 .and_then(toml_edit::Item::as_str)
1325 .map(str::to_owned)
1326}
1327
1328fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1330 let mut found = Vec::new();
1331 for (key, item) in target.iter_mut() {
1332 match key.get() {
1333 "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1334 "headers" => {
1335 let Some(headers) = item.as_table_like_mut() else {
1336 continue;
1337 };
1338 for (name, item) in headers.iter_mut() {
1339 let field = SecretField::Header(name.get().to_owned());
1340 found.extend(item.as_value_mut().map(|value| (field, value)));
1341 }
1342 }
1343 _ => {}
1344 }
1345 }
1346 found
1347}
1348
1349fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1351 *new.decor_mut() = value.decor().clone();
1352 *value = new;
1353}
1354
1355pub fn revision(text: &str) -> String {
1358 let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1359 (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1360 });
1361 format!("{hash:016x}")
1362}
1363
1364pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1366 let output = Command::new("systemctl")
1367 .args(["show", slice, "--property=MemoryMax", "--value"])
1368 .output()?;
1369 if !output.status.success() {
1370 return Err(std::io::Error::other(format!(
1371 "systemctl show {slice} failed: {}",
1372 String::from_utf8_lossy(&output.stderr).trim()
1373 )));
1374 }
1375 let value = String::from_utf8_lossy(&output.stdout);
1376 parse_memory_max(value.trim()).ok_or_else(|| {
1377 std::io::Error::other(format!(
1378 "systemctl show {slice}: unexpected MemoryMax {value:?}"
1379 ))
1380 })
1381}
1382
1383pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1385 #[expect(
1386 clippy::cast_precision_loss,
1387 clippy::cast_possible_truncation,
1388 clippy::cast_sign_loss,
1389 reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1390 most 16"
1391 )]
1392 let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1393 budget
1394}
1395
1396pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1399 let output = Command::new("systemctl")
1400 .args([
1401 "show",
1402 slice,
1403 "--property=EffectiveCPUs",
1404 "--property=AllowedCPUs",
1405 "--property=CPUQuotaPerSecUSec",
1406 ])
1407 .output()?;
1408 if !output.status.success() {
1409 return Err(std::io::Error::other(format!(
1410 "systemctl show {slice} failed: {}",
1411 String::from_utf8_lossy(&output.stderr).trim()
1412 )));
1413 }
1414 let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1415 parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1416 std::io::Error::other(format!(
1417 "systemctl show {slice}: unexpected CPU properties {:?}",
1418 String::from_utf8_lossy(&output.stdout).trim()
1419 ))
1420 })
1421}
1422
1423pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1426 let value = |name: &str| {
1427 properties.lines().find_map(|line| {
1428 line.strip_prefix(name)
1429 .and_then(|rest| rest.strip_prefix('='))
1430 .map(str::trim)
1431 })
1432 };
1433 let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
1434 .into_iter()
1435 .flatten()
1436 .find(|cpus| !cpus.is_empty());
1437 let cpus = match set {
1438 Some(set) => cpu_count(set)?,
1439 None => host,
1440 };
1441 let quota = match value("CPUQuotaPerSecUSec") {
1442 None | Some("" | "infinity") => u64::MAX,
1443 Some(quota) => quota_millicores(quota)?,
1444 };
1445 Some(cpus.saturating_mul(1000).min(quota))
1446}
1447
1448fn cpu_count(set: &str) -> Option<u64> {
1450 set.split([',', ' '])
1451 .filter(|range| !range.is_empty())
1452 .try_fold(0u64, |count, range| {
1453 let (first, last) = range.split_once('-').unwrap_or((range, range));
1454 let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
1455 Some(count + last.checked_sub(first)? + 1)
1456 })
1457}
1458
1459fn quota_millicores(quota: &str) -> Option<u64> {
1462 const UNITS: [(&str, f64); 10] = [
1464 ("y", 31_557_600_000.0),
1465 ("M", 2_629_800_000.0),
1466 ("w", 604_800_000.0),
1467 ("d", 86_400_000.0),
1468 ("h", 3_600_000.0),
1469 ("min", 60_000.0),
1470 ("s", 1000.0),
1471 ("ms", 1.0),
1472 ("us", 0.001),
1473 ("µs", 0.001),
1474 ];
1475 let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
1476 let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
1477 let (number, unit) = part.split_at(at);
1478 let per_unit = UNITS
1479 .iter()
1480 .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
1481 Some(total + number.parse::<f64>().ok()? * per_unit)
1482 })?;
1483 #[allow(
1484 clippy::cast_possible_truncation,
1485 clippy::cast_sign_loss,
1486 reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
1487 )]
1488 (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
1489 .then(|| millicores.round() as u64)
1490}
1491
1492pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
1494 if value == "infinity" {
1495 Some(None)
1496 } else {
1497 value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
1498 }
1499}
1500
1501#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1503pub struct ByteSize(pub u64);
1504
1505const UNITS: [(char, u64); 4] = [
1506 ('T', 1 << 40),
1507 ('G', 1 << 30),
1508 ('M', 1 << 20),
1509 ('K', 1 << 10),
1510];
1511
1512impl FromStr for ByteSize {
1513 type Err = String;
1514
1515 #[allow(
1516 clippy::cast_precision_loss,
1517 clippy::cast_possible_truncation,
1518 clippy::cast_sign_loss,
1519 reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
1520 )]
1521 fn from_str(s: &str) -> Result<Self, Self::Err> {
1522 let invalid = || {
1523 format!(
1524 "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
1525 )
1526 };
1527 let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
1528 let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
1529 let number = &s[..s.len() - 1];
1530 if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
1531 return Err(invalid());
1532 }
1533 let number: f64 = number.parse().map_err(|_| invalid())?;
1534 let bytes = (number * *factor as f64).round();
1535 if bytes >= u64::MAX as f64 {
1537 return Err(format!("size {s:?} is too large"));
1538 }
1539 Ok(ByteSize(bytes as u64))
1540 }
1541}
1542
1543impl fmt::Display for ByteSize {
1544 #[allow(
1545 clippy::cast_precision_loss,
1546 reason = "shown with two decimals, far coarser than f64's precision"
1547 )]
1548 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1549 let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
1550 return write!(f, "{}B", self.0);
1551 };
1552 let number = format!("{:.2}", self.0 as f64 / *factor as f64);
1553 let number = number.trim_end_matches('0').trim_end_matches('.');
1554 write!(f, "{number}{unit}")
1555 }
1556}
1557
1558impl<'de> Deserialize<'de> for ByteSize {
1559 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1560 String::deserialize(deserializer)?
1561 .parse()
1562 .map_err(de::Error::custom)
1563 }
1564}
1565
1566pub fn parse_duration(s: &str) -> Result<Duration, String> {
1568 let invalid = || {
1569 format!(
1570 "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
1571 )
1572 };
1573 let (number, factor) = match s.chars().last() {
1574 Some('s') => (&s[..s.len() - 1], 1),
1575 Some('m') => (&s[..s.len() - 1], 60),
1576 Some('h') => (&s[..s.len() - 1], 3600),
1577 Some('d') => (&s[..s.len() - 1], 86400),
1578 _ => return Err(invalid()),
1579 };
1580 let number: u64 = number.parse().map_err(|_| invalid())?;
1581 number
1582 .checked_mul(factor)
1583 .map(Duration::from_secs)
1584 .ok_or_else(invalid)
1585}
1586
1587pub fn parse_age(text: &str) -> Result<Duration, String> {
1589 let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
1590 let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
1591 let (number, unit) = text.split_at(split);
1592 let number: u64 = number.parse().map_err(|_| error())?;
1593 let unit = match unit {
1594 "s" => 1,
1595 "m" => 60,
1596 "h" => 3600,
1597 "d" => 86_400,
1598 "w" => 7 * 86_400,
1599 _ => return Err(error()),
1600 };
1601 Ok(Duration::from_secs(number.saturating_mul(unit)))
1602}
1603
1604fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
1605 parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
1606}
1607
1608#[cfg(test)]
1609mod tests;