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 #[serde(default)]
51 pub github: Github,
52}
53
54#[derive(Debug, Deserialize)]
56#[serde(deny_unknown_fields)]
57pub struct Host {
58 pub slice: String,
60 pub isolation: Isolation,
62 #[serde(default)]
64 pub allow_unsafe: bool,
65 #[serde(default = "default_poll_interval", deserialize_with = "duration")]
67 pub poll_interval: Duration,
68 #[serde(default)]
70 pub admission: Admission,
71 #[serde(default)]
74 pub cpu_overcommit: Option<f64>,
75 #[serde(default)]
79 pub memory_ceiling: Option<ByteSize>,
80 #[serde(default = "default_on")]
84 pub pooled_count: bool,
85 #[serde(default = "default_on")]
90 pub skip_syncs: bool,
91 #[serde(default)]
93 pub sysbox_probe_profile: Option<String>,
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub enum ImageSyncs {
99 Skips,
102 SkipsCalls,
105 Syncs,
107 Unread,
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum PastBoots {
114 Read,
116 NotKept,
118 Unreadable,
120}
121
122impl Host {
123 pub fn syncs_check(
127 &self,
128 images: &BTreeMap<String, ImageSyncs>,
129 boots: PastBoots,
130 ) -> api::Check {
131 let check = |status, detail: String, fix: Option<&str>| api::Check {
132 name: "runner syncs".to_owned(),
133 status,
134 detail,
135 fix: fix.map(str::to_owned),
136 };
137 if !self.skip_syncs {
138 return check(
139 api::CheckStatus::Info,
140 "kept, as [host] skip_syncs = false sets".to_owned(),
141 None,
142 );
143 }
144 let of = |wanted: ImageSyncs| -> Vec<&str> {
145 images
146 .iter()
147 .filter(|(_, syncs)| **syncs == wanted)
148 .map(|(image, _)| image.as_str())
149 .collect()
150 };
151 let words = [
153 (
154 ImageSyncs::Skips,
155 "skipped by the runners of ",
156 ", which carry runnerd-skip-syncs and mount their layers volatile",
157 ),
158 (
159 ImageSyncs::SkipsCalls,
160 "skipped in sync calls only by the runners of ",
161 ": they carry runnerd-skip-syncs, and their layers sync when unmounted",
162 ),
163 (
164 ImageSyncs::Syncs,
165 "kept by the runners of ",
166 ": they lack runnerd-skip-syncs, so runnerd never asks them",
167 ),
168 (
169 ImageSyncs::Unread,
170 "decided at each runner's start for ",
171 ", which Docker does not have yet",
172 ),
173 ];
174 let mut parts: Vec<String> = words
175 .iter()
176 .filter_map(|(kind, before, after)| {
177 let list = of(*kind);
178 (!list.is_empty()).then(|| format!("{before}{}{after}", list.join(", ")))
179 })
180 .collect();
181 if parts.is_empty() {
182 parts.push(
183 "decided at each runner's start, from its image's runnerd.skip-syncs label"
184 .to_owned(),
185 );
186 }
187 let runners = parts.join("; ");
188 let arms = |kind: &ImageSyncs| *kind != ImageSyncs::Syncs;
189 if !images.is_empty() && !images.values().any(arms) {
190 return check(
191 api::CheckStatus::Info,
192 format!("{runners}. Nothing is emptied after a crash"),
193 None,
194 );
195 }
196 let crash = "a crash or power loss while such a runner ran empties the slots' volumes and the pnpm and Maven \
197 stores before any runner starts";
198 match boots {
199 PastBoots::Read => check(
200 api::CheckStatus::Info,
201 format!(
202 "{runners}. After {crash}; an orderly shutdown or reboot keeps them, as the journal shows its \
203 final sync"
204 ),
205 None,
206 ),
207 PastBoots::NotKept => check(
208 api::CheckStatus::Warn,
209 format!(
210 "{runners}. This host's journal keeps no past boot, so runnerd cannot tell an orderly shutdown \
211 from a crash: every boot after such a runner ran empties the slots' volumes and the pnpm and \
212 Maven stores before any runner starts"
213 ),
214 Some(
215 "keep the journal across boots: set Storage=persistent in /etc/systemd/journald.conf, then \
216 sudo systemctl restart systemd-journald; or set [host] skip_syncs = false",
217 ),
218 ),
219 PastBoots::Unreadable => check(
220 api::CheckStatus::Warn,
221 format!(
222 "{runners}. runnerd's user cannot read the system journal, so runnerd cannot tell an orderly \
223 shutdown from a crash: every boot after such a runner ran empties the slots' volumes and the \
224 pnpm and Maven stores before any runner starts"
225 ),
226 Some(
227 "let runnerd's user read the journal: sudo usermod -aG systemd-journal \"$USER\", then log in \
228 again; or set [host] skip_syncs = false",
229 ),
230 ),
231 }
232 }
233
234 fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
236 match self.cpu_overcommit {
237 Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
238 Err(ConfigError::CpuOvercommit { value })
239 }
240 _ => Ok(()),
241 }
242 }
243}
244
245pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
247
248#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
250#[serde(rename_all = "kebab-case")]
251pub enum Admission {
252 Caps,
255 #[default]
259 Measured,
260}
261
262impl Admission {
263 pub fn policy(self) -> api::AdmissionPolicy {
265 match self {
266 Admission::Caps => api::AdmissionPolicy::Caps,
267 Admission::Measured => api::AdmissionPolicy::Measured,
268 }
269 }
270}
271
272fn default_on() -> bool {
273 true
274}
275
276fn default_poll_interval() -> Duration {
277 Duration::from_secs(15)
278}
279
280#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
282#[serde(rename_all = "kebab-case")]
283pub enum Isolation {
284 Sysbox,
286 UnsafeDind,
288}
289
290#[derive(Debug, Clone, Deserialize)]
292#[serde(deny_unknown_fields)]
293pub struct Caches {
294 pub mirror: Mirror,
296 pub pnpm: CacheLimit,
298 pub maven: CacheLimit,
300 #[serde(default = "default_reseed_at")]
303 pub reseed_at: ByteSize,
304 #[serde(default = "default_snapshot_max")]
307 pub snapshot_max: ByteSize,
308 #[serde(default)]
310 pub build: BuildCache,
311 #[serde(default)]
313 pub actions: ActionArchives,
314}
315
316#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
319#[serde(deny_unknown_fields, default)]
320pub struct ActionArchives {
321 pub enabled: bool,
323 pub directory: Option<PathBuf>,
325 #[serde(deserialize_with = "duration")]
327 pub keep: Duration,
328}
329
330impl ActionArchives {
331 fn validate(&self) -> Result<(), ConfigError> {
332 if self.enabled && self.keep.is_zero() {
333 return Err(ConfigError::ZeroLimit {
334 field: "caches.actions.keep".to_owned(),
335 });
336 }
337 Ok(())
338 }
339}
340
341impl Default for ActionArchives {
342 fn default() -> ActionArchives {
343 ActionArchives {
344 enabled: true,
345 directory: None,
346 keep: Duration::from_hours(14 * 24),
347 }
348 }
349}
350
351#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
354#[serde(deny_unknown_fields, default)]
355pub struct BuildCache {
356 pub enabled: bool,
358 pub max_size: ByteSize,
360 pub branches: Vec<String>,
363 pub directory: Option<PathBuf>,
365 pub actions_cache: bool,
368}
369
370impl BuildCache {
371 pub fn actions_cache_check(&self) -> api::Check {
373 api::Check {
374 name: api::ACTIONS_CACHE.to_owned(),
375 status: api::CheckStatus::Info,
376 detail: if self.enabled {
377 api::actions_cache_place(self.actions_cache)
378 } else {
379 "GitHub's, as the build cache is off"
380 }
381 .to_owned(),
382 fix: None,
383 }
384 }
385
386 fn validate(&self) -> Result<(), ConfigError> {
387 if self.enabled && self.max_size.0 == 0 {
388 return Err(ConfigError::ZeroLimit {
389 field: "caches.build.max_size".to_owned(),
390 });
391 }
392 Ok(())
393 }
394}
395
396impl Default for BuildCache {
397 fn default() -> BuildCache {
398 BuildCache {
399 enabled: true,
400 max_size: ByteSize(10 << 30),
401 branches: Vec::new(),
402 directory: None,
403 actions_cache: true,
404 }
405 }
406}
407
408fn default_reseed_at() -> ByteSize {
409 ByteSize(2 << 30)
410}
411
412fn default_snapshot_max() -> ByteSize {
414 ByteSize(10 << 30)
415}
416
417fn default_mirror_max_size() -> ByteSize {
418 ByteSize(10 << 30)
419}
420
421#[derive(Debug, Clone, Deserialize)]
423#[serde(deny_unknown_fields)]
424pub struct Mirror {
425 pub enabled: bool,
427 pub memory: ByteSize,
429 pub cpus: u32,
431 #[serde(deserialize_with = "duration")]
433 pub ttl: Duration,
434 #[serde(default = "default_mirror_max_size")]
436 pub max_size: ByteSize,
437}
438
439#[derive(Debug, Clone, Deserialize)]
441#[serde(deny_unknown_fields)]
442pub struct CacheLimit {
443 pub max_size: ByteSize,
445}
446
447pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
450
451#[derive(Debug, Clone, Deserialize)]
453#[serde(try_from = "ProfileToml")]
454pub struct Profile {
455 pub image: Image,
457 pub cpus: u32,
459 pub memory: ByteSize,
461 pub slots: u32,
463 pub record_failures: bool,
465 pub min_memory: Option<ByteSize>,
467 pub max_memory: Option<ByteSize>,
469 pub max_slots: Option<u32>,
471 pub priced_as: Option<api::Sku>,
474}
475
476#[derive(Debug, Clone, PartialEq, Eq)]
478pub enum Image {
479 Stage(String),
481 Pulled(String),
484}
485
486impl Image {
487 pub fn reference(&self) -> String {
489 match self {
490 Image::Stage(stage) => format!("runnerd-runner:{stage}"),
491 Image::Pulled(reference) => reference.clone(),
492 }
493 }
494}
495
496#[derive(Deserialize)]
498#[serde(deny_unknown_fields)]
499struct ProfileToml {
500 image_stage: Option<String>,
501 image: Option<String>,
502 cpus: u32,
503 memory: ByteSize,
504 slots: u32,
505 #[serde(default)]
506 record_failures: bool,
507 min_memory: Option<ByteSize>,
508 max_memory: Option<ByteSize>,
509 max_slots: Option<u32>,
510 priced_as: Option<api::Sku>,
511}
512
513impl TryFrom<ProfileToml> for Profile {
514 type Error = String;
515
516 fn try_from(profile: ProfileToml) -> Result<Profile, String> {
517 let image = match (profile.image_stage, profile.image) {
518 (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
519 (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
520 (Some(_), Some(_)) => {
521 return Err(
522 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
523 not both"
524 .to_owned(),
525 );
526 }
527 _ => {
528 return Err(
529 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
530 .to_owned(),
531 );
532 }
533 };
534 Ok(Profile {
535 image,
536 cpus: profile.cpus,
537 memory: profile.memory,
538 slots: profile.slots,
539 record_failures: profile.record_failures,
540 min_memory: profile.min_memory,
541 max_memory: profile.max_memory,
542 max_slots: profile.max_slots,
543 priced_as: profile.priced_as,
544 })
545 }
546}
547
548pub fn labels_match(wanted: &[String], labels: &[String]) -> bool {
552 !wanted.is_empty()
553 && wanted.iter().all(|wanted| {
554 labels
555 .iter()
556 .any(|label| label.eq_ignore_ascii_case(wanted))
557 })
558}
559
560#[derive(Debug, Deserialize)]
563#[serde(deny_unknown_fields)]
564pub struct Repository {
565 pub name: String,
567 pub profile: String,
569 pub labels: Vec<String>,
571 #[serde(default = "default_warm")]
574 pub warm: u32,
575 #[serde(default = "default_true")]
577 pub build_cache: bool,
578 #[serde(default)]
581 pub build_cache_writers: BuildCacheWriters,
582 #[serde(default)]
586 pub rerun_runner_faults: bool,
587}
588
589#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
591#[serde(rename_all = "kebab-case")]
592pub enum BuildCacheWriters {
593 #[default]
595 DefaultBranch,
596 PullRequests,
598}
599
600fn default_true() -> bool {
601 true
602}
603
604fn default_warm() -> u32 {
605 1
606}
607
608#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
610#[serde(deny_unknown_fields, default)]
611pub struct History {
612 pub database: Option<PathBuf>,
614 #[serde(deserialize_with = "duration")]
616 pub samples: Duration,
617 #[serde(deserialize_with = "duration")]
619 pub logs: Duration,
620 #[serde(deserialize_with = "duration")]
622 pub events: Duration,
623 #[serde(deserialize_with = "duration")]
625 pub jobs: Duration,
626 pub log_max: ByteSize,
628}
629
630impl Default for History {
631 fn default() -> History {
632 History {
633 database: None,
634 samples: Duration::from_hours(7 * 24),
635 logs: Duration::from_hours(30 * 24),
636 events: Duration::from_hours(30 * 24),
637 jobs: Duration::from_hours(90 * 24),
638 log_max: ByteSize(50 << 20),
639 }
640 }
641}
642
643#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
646#[serde(deny_unknown_fields, default)]
647pub struct Records {
648 pub max_total: ByteSize,
650 pub max_count: u32,
652 #[serde(deserialize_with = "duration")]
654 pub max_age: Duration,
655 pub min_free: ByteSize,
657}
658
659impl Default for Records {
660 fn default() -> Records {
661 Records {
662 max_total: ByteSize(20 << 30),
663 max_count: 10,
664 max_age: Duration::from_hours(7 * 24),
665 min_free: ByteSize(10 << 30),
666 }
667 }
668}
669
670#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
673#[serde(deny_unknown_fields, default)]
674pub struct Sizing {
675 pub margin: u32,
678 pub min_jobs: u32,
681 pub auto: bool,
684 #[serde(deserialize_with = "duration")]
687 pub interval: Duration,
688 #[serde(deserialize_with = "duration")]
691 pub raise_interval: Duration,
692 pub max_step: u32,
695}
696
697impl Sizing {
698 pub fn raise_after(&self) -> Duration {
701 self.raise_interval.min(self.interval)
702 }
703}
704
705impl Default for Sizing {
706 fn default() -> Sizing {
707 Sizing {
708 margin: 15,
709 min_jobs: 10,
710 auto: false,
711 interval: Duration::from_hours(1),
712 raise_interval: Duration::from_hours(1),
713 max_step: 50,
714 }
715 }
716}
717
718#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
720#[serde(deny_unknown_fields, default)]
721pub struct Web {
722 pub origins: Vec<Origin>,
725 pub open: Option<Origin>,
728}
729
730impl Web {
731 fn validate(&self) -> Result<(), ConfigError> {
733 match &self.open {
734 Some(open) if !self.origins.contains(open) => {
735 Err(ConfigError::OpenNotListed { open: open.clone() })
736 }
737 _ => Ok(()),
738 }
739 }
740}
741
742#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
745#[serde(deny_unknown_fields, default)]
746pub struct Github {
747 pub job_actions: bool,
750}
751
752impl Default for Github {
753 fn default() -> Github {
754 Github { job_actions: true }
755 }
756}
757
758#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
760#[serde(deny_unknown_fields, default)]
761pub struct Notify {
762 pub digest_at: DayTime,
764 pub targets: Vec<NotifyTarget>,
766}
767
768impl Default for Notify {
769 fn default() -> Notify {
770 Notify {
771 digest_at: DayTime { minutes: 9 * 60 },
772 targets: Vec::new(),
773 }
774 }
775}
776
777impl Notify {
778 fn validate(&self) -> Result<(), ConfigError> {
781 let refuse = |target: &str, why: &'static str| {
782 Err(ConfigError::Notify {
783 target: target.to_owned(),
784 why,
785 })
786 };
787 for (index, target) in self.targets.iter().enumerate() {
788 let name = target.name.as_str();
789 if name.trim().is_empty() || name.chars().any(char::is_control) {
790 return refuse(name, "name is empty");
791 }
792 if self.targets[..index].iter().any(|other| other.name == name) {
793 return refuse(name, "another target has this name");
794 }
795 let url = target.url.expose();
796 if url == SECRET_SHOWN {
797 return refuse(
798 name,
799 "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
800 );
801 }
802 let Some(rest) = url
803 .strip_prefix("https://")
804 .or_else(|| url.strip_prefix("http://"))
805 else {
806 return refuse(name, "url does not start with https:// or http://");
807 };
808 let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
809 if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
810 return refuse(name, "url is not a URL");
811 }
812 if target.kind == NotifyKind::Ntfy
813 && (path.is_empty() || path.contains(['/', '?', '#']))
814 {
815 return refuse(
816 name,
817 "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
818 );
819 }
820 for (header, value) in &target.headers {
821 if value.expose() == SECRET_SHOWN {
822 return refuse(
823 name,
824 "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
825 );
826 }
827 let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
828 if header.is_empty() || !header.chars().all(token) {
829 return refuse(name, "a header's name is not a header name");
830 }
831 if value.expose().chars().any(char::is_control) {
832 return refuse(name, "a header's value has a control character");
833 }
834 }
835 if target.events.contains(&api::NotifyEvent::Test) {
836 return refuse(
837 name,
838 "events lists test, which only runnerctl notify test sends",
839 );
840 }
841 }
842 Ok(())
843 }
844}
845
846#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
848#[serde(deny_unknown_fields)]
849pub struct NotifyTarget {
850 pub name: String,
852 pub kind: NotifyKind,
854 pub url: Secret,
857 #[serde(default)]
859 pub headers: BTreeMap<String, Secret>,
860 #[serde(default = "default_notify_events")]
862 pub events: Vec<api::NotifyEvent>,
863}
864
865fn default_notify_events() -> Vec<api::NotifyEvent> {
866 vec![
867 api::NotifyEvent::Failed,
868 api::NotifyEvent::Lost,
869 api::NotifyEvent::Runner,
870 api::NotifyEvent::Insights,
871 ]
872}
873
874#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
876#[serde(rename_all = "kebab-case")]
877pub enum NotifyKind {
878 Webhook,
880 Ntfy,
882 Discord,
884}
885
886#[derive(Clone, PartialEq, Eq, Deserialize)]
888#[serde(transparent)]
889pub struct Secret(String);
890
891impl Secret {
892 pub fn expose(&self) -> &str {
894 &self.0
895 }
896}
897
898impl From<&str> for Secret {
899 fn from(value: &str) -> Secret {
900 Secret(value.to_owned())
901 }
902}
903
904impl fmt::Debug for Secret {
905 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
906 f.write_str("<secret>")
907 }
908}
909
910#[derive(Debug, Clone, Copy, PartialEq, Eq)]
912pub struct DayTime {
913 pub minutes: u32,
915}
916
917impl FromStr for DayTime {
918 type Err = String;
919
920 fn from_str(text: &str) -> Result<DayTime, String> {
921 let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
922 let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
923 let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
924 if !digits(hour) || !digits(minute) {
925 return Err(invalid());
926 }
927 let (hour, minute): (u32, u32) = (
928 hour.parse().map_err(|_| invalid())?,
929 minute.parse().map_err(|_| invalid())?,
930 );
931 if hour > 23 || minute > 59 {
932 return Err(invalid());
933 }
934 Ok(DayTime {
935 minutes: hour * 60 + minute,
936 })
937 }
938}
939
940impl fmt::Display for DayTime {
941 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
942 write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
943 }
944}
945
946impl<'de> Deserialize<'de> for DayTime {
947 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
948 String::deserialize(deserializer)?
949 .parse()
950 .map_err(de::Error::custom)
951 }
952}
953
954#[derive(Debug, Clone, PartialEq, Eq)]
958pub struct Origin(String);
959
960impl Origin {
961 pub fn as_str(&self) -> &str {
963 &self.0
964 }
965}
966
967impl FromStr for Origin {
968 type Err = String;
969
970 fn from_str(s: &str) -> Result<Origin, String> {
971 let invalid = |why: &str| {
972 format!(
973 "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
974 \"https://host.example.ts.net:8443\""
975 )
976 };
977 let (scheme, rest) = s
978 .split_once("://")
979 .ok_or_else(|| invalid("it has no scheme"))?;
980 let scheme = scheme.to_ascii_lowercase();
981 if rest.contains(['/', '?', '#']) {
982 return Err(invalid("it has a path"));
983 }
984 if rest.contains('@') {
985 return Err(invalid("it names a user"));
986 }
987 let (host, port) = match rest.strip_prefix('[') {
988 Some(v6) => {
989 let (address, after) = v6
990 .split_once(']')
991 .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
992 let port = match after {
993 "" => None,
994 _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
995 };
996 (format!("[{address}]"), port)
997 }
998 None => match rest.split_once(':') {
999 Some((host, port)) => (host.to_owned(), Some(port)),
1000 None => (rest.to_owned(), None),
1001 },
1002 };
1003 let host = host.to_ascii_lowercase();
1004 let not_a_host = || invalid("its host is not a name or an address");
1005 let (host, loopback) =
1007 if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
1008 let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
1009 (format!("[{}]", ipv6_origin(address)), address.is_loopback())
1010 } else {
1011 if host.is_empty()
1012 || !host
1013 .chars()
1014 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
1015 {
1016 return Err(not_a_host());
1017 }
1018 let name = host.strip_suffix('.').unwrap_or(&host);
1020 let last = name.rsplit('.').next().unwrap_or_default();
1021 let numeric = !last.is_empty()
1022 && (last.bytes().all(|byte| byte.is_ascii_digit())
1023 || last
1024 .strip_prefix("0x")
1025 .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
1026 if numeric {
1027 let address: Ipv4Addr = name.parse().map_err(|_| {
1028 invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
1029 })?;
1030 (address.to_string(), address.is_loopback())
1031 } else {
1032 let loopback = host == "localhost";
1033 (host, loopback)
1034 }
1035 };
1036 let port = port
1037 .map(|port| {
1038 Some(port)
1039 .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
1040 .and_then(|port| port.parse::<u16>().ok())
1041 .filter(|port| *port != 0)
1042 .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
1043 })
1044 .transpose()?;
1045 let default_port = match scheme.as_str() {
1046 "https" => 443,
1047 "http" if loopback => 80,
1048 "http" => {
1049 return Err(invalid(
1050 "http is for a loopback host only, since the session cookie would cross the network in \
1051 the clear; use https",
1052 ));
1053 }
1054 _ => return Err(invalid("its scheme is not https")),
1055 };
1056 Ok(Origin(match port {
1057 Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
1058 _ => format!("{scheme}://{host}"),
1059 }))
1060 }
1061}
1062
1063fn ipv6_origin(address: Ipv6Addr) -> String {
1066 let pieces = address.segments();
1067 let (mut zeros_at, mut zeros) = (0, 0);
1068 let mut at = 0;
1069 while at < pieces.len() {
1070 let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
1071 if run > zeros {
1072 (zeros_at, zeros) = (at, run);
1073 }
1074 at += run.max(1);
1075 }
1076 let hex = |pieces: &[u16]| {
1077 pieces
1078 .iter()
1079 .map(|piece| format!("{piece:x}"))
1080 .collect::<Vec<_>>()
1081 .join(":")
1082 };
1083 if zeros < 2 {
1084 return hex(&pieces);
1085 }
1086 format!(
1087 "{}::{}",
1088 hex(&pieces[..zeros_at]),
1089 hex(&pieces[zeros_at + zeros..])
1090 )
1091}
1092
1093impl fmt::Display for Origin {
1094 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1095 f.write_str(&self.0)
1096 }
1097}
1098
1099impl<'de> Deserialize<'de> for Origin {
1100 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1101 String::deserialize(deserializer)?
1102 .parse()
1103 .map_err(de::Error::custom)
1104 }
1105}
1106
1107#[derive(Debug, Clone, PartialEq, Eq)]
1109pub struct ParseError {
1110 pub line: usize,
1112 pub message: String,
1114}
1115
1116#[derive(Debug, thiserror::Error)]
1118pub enum ConfigError {
1119 #[error("cannot read {}: {source}", path.display())]
1121 Read {
1122 path: PathBuf,
1124 source: std::io::Error,
1126 },
1127 #[error(transparent)]
1129 Parse(#[from] toml::de::Error),
1130 #[error(
1132 "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
1133 defined_profiles(.defined)
1134 )]
1135 UnknownProfile {
1136 repository: String,
1138 profile: String,
1140 defined: Vec<String>,
1142 },
1143 #[error("repository {repository} needs at least one non-empty label")]
1145 MissingLabels {
1146 repository: String,
1148 },
1149 #[error(
1151 "repository {repository} has two entries with profile {profile:?}; give each of its pools its own profile"
1152 )]
1153 PoolProfileTwice {
1154 repository: String,
1156 profile: String,
1158 },
1159 #[error(
1162 "repository {repository}'s pools in {first} and {second} both have the label {label:?}, so a job asking for \
1163 it could land in either; give each pool labels of its own, such as only {second:?}'s own label for the \
1164 lighter one"
1165 )]
1166 PoolLabelsOverlap {
1167 repository: String,
1169 first: String,
1171 second: String,
1173 label: String,
1175 },
1176 #[error(
1178 "repository {repository}'s entries set build_cache_writers differently; whose runs may write to the build \
1179 cache is the repository's, so set the same in each"
1180 )]
1181 PoolWritersDiffer {
1182 repository: String,
1184 },
1185 #[error(
1187 "repositories {first} and {second} are one repository on GitHub, which ignores case; spell it the same in \
1188 each entry"
1189 )]
1190 RepositorySpelledTwice {
1191 first: String,
1193 second: String,
1195 },
1196 #[error(
1198 "repository {repository}'s entries set rerun_runner_faults differently; whether runnerd re-runs a run its \
1199 runner failed is the repository's, so set the same in each"
1200 )]
1201 PoolRerunsDiffer {
1202 repository: String,
1204 },
1205 #[error(
1207 "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
1208 set allow_unsafe = true under [host] to use it anyway"
1209 )]
1210 UnsafeNotAllowed,
1211 #[error("{field} is 0; set it above 0")]
1213 ZeroLimit {
1214 field: String,
1216 },
1217 #[error(
1219 "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
1220 )]
1221 CpuOvercommit {
1222 value: f64,
1224 },
1225 #[error(
1227 "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
1228 )]
1229 OpenNotListed {
1230 open: Origin,
1232 },
1233 #[error("profiles.{profile}: {why}")]
1236 SizingBounds {
1237 profile: String,
1239 why: &'static str,
1241 },
1242 #[error("notify target {target:?}: {why}")]
1244 Notify {
1245 target: String,
1247 why: &'static str,
1249 },
1250 #[error(
1252 "host.memory_ceiling is {ceiling}, under the {needs} one runner of {profile} and the mirror take; set it \
1253 to the most memory the host can grow to, such as the VM's maximum"
1254 )]
1255 CeilingUnderProfile {
1256 profile: String,
1258 needs: ByteSize,
1260 ceiling: ByteSize,
1262 },
1263 #[error("slots × memory over all profiles plus the mirror is too large to add up")]
1265 BudgetOverflow,
1266 #[error(
1268 "measured admission, the default, admits runners against the slice {slice}'s MemoryMax, and it has none; \
1269 install a unit file for it (`runnerd doctor` prints one), or set [host] admission = \"caps\""
1270 )]
1271 MeasuredWithoutMemoryMax {
1272 slice: String,
1274 },
1275 #[error(
1277 "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
1278 its `memory` or raise the slice's MemoryMax"
1279 )]
1280 ProfileOverBudget {
1281 profile: String,
1283 needs: ByteSize,
1285 slice: String,
1287 memory_max: ByteSize,
1289 },
1290 #[error(
1292 "slots × memory over all profiles plus the mirror is {total}, \
1293 the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
1294 MemoryMax (`runnerd doctor` prints its unit)"
1295 )]
1296 OverBudget {
1297 total: ByteSize,
1299 slice: String,
1301 memory_max: ByteSize,
1303 },
1304}
1305
1306fn defined_profiles(defined: &[String]) -> String {
1307 if defined.is_empty() {
1308 "it defines none".to_owned()
1309 } else {
1310 format!("defined: {}", defined.join(", "))
1311 }
1312}
1313
1314#[derive(Debug, Clone, PartialEq, Eq)]
1316pub enum Warning {
1317 UnlimitedSlice {
1319 slice: String,
1321 total: ByteSize,
1323 },
1324}
1325
1326impl fmt::Display for Warning {
1327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1328 match self {
1329 Warning::UnlimitedSlice { slice, total } => write!(
1330 f,
1331 "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
1332 install a unit file for it"
1333 ),
1334 }
1335 }
1336}
1337
1338impl Config {
1339 pub fn load(path: &Path) -> Result<Config, ConfigError> {
1341 let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
1342 path: path.to_owned(),
1343 source,
1344 })?;
1345 Config::parse(&text)
1346 }
1347
1348 pub fn parse(text: &str) -> Result<Config, ConfigError> {
1350 Ok(toml::from_str(text)?)
1351 }
1352
1353 pub fn unreviewed() -> &'static str {
1356 "not saved: the text changed after its changes were shown; review them again"
1357 }
1358
1359 pub fn parse_error(text: &str) -> Option<ParseError> {
1363 let Err(error) = toml::from_str::<Config>(text) else {
1364 return None;
1365 };
1366 let span = error.span()?;
1367 let before = text.get(..span.start)?;
1368 Some(ParseError {
1369 line: before.matches('\n').count() + 1,
1370 message: error.message().trim().to_owned(),
1371 })
1372 }
1373
1374 pub fn check(
1377 text: &str,
1378 memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
1379 ) -> (Option<Config>, api::ConfigCheck) {
1380 let config = match Config::parse(text) {
1381 Ok(config) => config,
1382 Err(error) => return (None, budget::invalid(error.to_string(), None, None)),
1383 };
1384 let max = match memory_max(&config.host.slice) {
1385 Ok(max) => max,
1386 Err(error) => {
1387 return (
1388 None,
1389 budget::invalid(error.to_string(), Some(&config), None),
1390 );
1391 }
1392 };
1393 let check = config.check_against(max);
1394 if check.valid {
1395 (Some(config), check)
1396 } else {
1397 (None, check)
1398 }
1399 }
1400
1401 pub fn mirror_memory(&self) -> u64 {
1403 if self.caches.mirror.enabled {
1404 self.caches.mirror.memory.0
1405 } else {
1406 0
1407 }
1408 }
1409
1410 pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1412 let mirror = self.mirror_memory();
1413 self.profiles
1414 .values()
1415 .try_fold(mirror, |total, p| {
1416 p.memory
1417 .0
1418 .checked_mul(u64::from(p.slots))?
1419 .checked_add(total)
1420 })
1421 .map(ByteSize)
1422 .ok_or(ConfigError::BudgetOverflow)
1423 }
1424
1425 pub fn memory_needed(&self) -> Result<ByteSize, ConfigError> {
1428 match self.host.admission {
1429 Admission::Caps => self.memory_caps(),
1430 Admission::Measured => {
1431 let largest = self
1432 .profiles
1433 .values()
1434 .map(|p| p.memory.0)
1435 .max()
1436 .unwrap_or(0);
1437 largest
1438 .checked_add(self.mirror_memory())
1439 .map(ByteSize)
1440 .ok_or(ConfigError::BudgetOverflow)
1441 }
1442 }
1443 }
1444
1445 pub fn runner_ceiling(&self) -> Option<u64> {
1449 self.host
1450 .memory_ceiling
1451 .map(|ceiling| ceiling.0.saturating_sub(self.mirror_memory()))
1452 }
1453
1454 fn check_memory_ceiling(&self) -> Result<(), ConfigError> {
1459 let (Some(ceiling), Some(most)) = (self.host.memory_ceiling, self.runner_ceiling()) else {
1460 return Ok(());
1461 };
1462 let largest = self
1463 .profiles
1464 .iter()
1465 .max_by_key(|(_, profile)| profile.memory.0);
1466 match largest {
1467 Some((profile, largest)) if largest.memory.0 > most => {
1468 Err(ConfigError::CeilingUnderProfile {
1469 profile: profile.clone(),
1470 needs: ByteSize(largest.memory.0.saturating_add(self.mirror_memory())),
1471 ceiling,
1472 })
1473 }
1474 _ => Ok(()),
1475 }
1476 }
1477
1478 fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1480 for (name, p) in &self.profiles {
1481 let memory = p.memory.0;
1482 let field = if p
1483 .min_memory
1484 .zip(p.max_memory)
1485 .is_some_and(|(min, max)| min.0 > max.0)
1486 {
1487 "min_memory is above its max_memory"
1488 } else if p.min_memory.is_some_and(|min| memory < min.0) {
1489 "memory is below its min_memory"
1490 } else if p.max_memory.is_some_and(|max| memory > max.0) {
1491 "memory is above its max_memory"
1492 } else if p.max_slots.is_some_and(|max| p.slots > max) {
1493 "slots are above its max_slots"
1494 } else {
1495 continue;
1496 };
1497 return Err(ConfigError::SizingBounds {
1498 profile: name.clone(),
1499 why: field,
1500 });
1501 }
1502 Ok(())
1503 }
1504
1505 fn check_pools(&self) -> Result<(), ConfigError> {
1509 for (at, first) in self.repositories.iter().enumerate() {
1510 if let Some(other) = self.repositories[at + 1..].iter().find(|other| {
1511 other.name != first.name && other.name.eq_ignore_ascii_case(&first.name)
1512 }) {
1513 return Err(ConfigError::RepositorySpelledTwice {
1514 first: first.name.clone(),
1515 second: other.name.clone(),
1516 });
1517 }
1518 for second in self.repositories[at + 1..]
1519 .iter()
1520 .filter(|second| second.name == first.name)
1521 {
1522 if second.profile == first.profile {
1523 return Err(ConfigError::PoolProfileTwice {
1524 repository: first.name.clone(),
1525 profile: first.profile.clone(),
1526 });
1527 }
1528 if let Some(label) = first.labels.iter().find(|label| {
1529 second
1530 .labels
1531 .iter()
1532 .any(|other| other.eq_ignore_ascii_case(label))
1533 }) {
1534 return Err(ConfigError::PoolLabelsOverlap {
1535 repository: first.name.clone(),
1536 first: first.profile.clone(),
1537 second: second.profile.clone(),
1538 label: label.clone(),
1539 });
1540 }
1541 if second.build_cache_writers != first.build_cache_writers {
1542 return Err(ConfigError::PoolWritersDiffer {
1543 repository: first.name.clone(),
1544 });
1545 }
1546 if second.rerun_runner_faults != first.rerun_runner_faults {
1547 return Err(ConfigError::PoolRerunsDiffer {
1548 repository: first.name.clone(),
1549 });
1550 }
1551 }
1552 }
1553 Ok(())
1554 }
1555
1556 pub fn pool(&self, repository: &str, labels: &[String]) -> Option<&Repository> {
1559 self.repositories
1560 .iter()
1561 .find(|pool| pool.name == repository && labels_match(labels, &pool.labels))
1562 }
1563
1564 pub fn pool_in(&self, repository: &str, profile: &str) -> Option<&Repository> {
1566 self.repositories
1567 .iter()
1568 .find(|pool| pool.name == repository && pool.profile == profile)
1569 }
1570
1571 pub fn pricing(&self) -> api::Pricing {
1573 api::Pricing {
1574 profiles: self
1575 .profiles
1576 .iter()
1577 .filter_map(|(name, profile)| Some((name.clone(), profile.priced_as?)))
1578 .collect(),
1579 default_sku: api::Sku::standard_for(std::env::consts::ARCH),
1580 }
1581 }
1582
1583 pub fn repository_names(&self) -> Vec<String> {
1585 let mut names: Vec<String> = Vec::new();
1586 for repository in &self.repositories {
1587 if !names.contains(&repository.name) {
1588 names.push(repository.name.clone());
1589 }
1590 }
1591 names
1592 }
1593
1594 pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1596 if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1597 return Err(ConfigError::UnsafeNotAllowed);
1598 }
1599 let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1600 [
1601 (p.memory.0 == 0, format!("profiles.{name}.memory")),
1602 (p.cpus == 0, format!("profiles.{name}.cpus")),
1603 (p.slots == 0, format!("profiles.{name}.slots")),
1604 (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1605 ]
1606 });
1607 let mirror = &self.caches.mirror;
1608 let mirror_zero_limits = [
1609 (
1610 mirror.enabled && mirror.memory.0 == 0,
1611 "caches.mirror.memory".to_owned(),
1612 ),
1613 (
1614 mirror.enabled && mirror.cpus == 0,
1615 "caches.mirror.cpus".to_owned(),
1616 ),
1617 (
1618 self.host.poll_interval.is_zero(),
1619 "host.poll_interval".to_owned(),
1620 ),
1621 (
1622 self.host.memory_ceiling == Some(ByteSize(0)),
1623 "host.memory_ceiling".to_owned(),
1624 ),
1625 (self.records.max_count == 0, "records.max_count".to_owned()),
1626 (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1627 (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1628 (
1629 self.sizing.raise_interval.is_zero(),
1630 "sizing.raise_interval".to_owned(),
1631 ),
1632 (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1633 (
1634 self.records.max_total.0 == 0,
1635 "records.max_total".to_owned(),
1636 ),
1637 ];
1638 if let Some((_, field)) = zero_limits
1639 .chain(mirror_zero_limits)
1640 .find(|(zero, _)| *zero)
1641 {
1642 return Err(ConfigError::ZeroLimit { field });
1643 }
1644 self.check_sizing_bounds()?;
1645 self.caches.build.validate()?;
1646 self.caches.actions.validate()?;
1647 self.host.check_cpu_overcommit()?;
1648 self.check_memory_ceiling()?;
1649 self.web.validate()?;
1650 self.notify.validate()?;
1651 for repository in &self.repositories {
1652 if !self.profiles.contains_key(&repository.profile) {
1653 return Err(ConfigError::UnknownProfile {
1654 repository: repository.name.clone(),
1655 profile: repository.profile.clone(),
1656 defined: self.profiles.keys().cloned().collect(),
1657 });
1658 }
1659 if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1660 {
1661 return Err(ConfigError::MissingLabels {
1662 repository: repository.name.clone(),
1663 });
1664 }
1665 }
1666 self.check_pools()?;
1667 self.check_budget(memory_max)
1668 }
1669
1670 fn check_budget(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1672 let total = self.memory_caps()?;
1673 if self.host.admission == Admission::Measured {
1674 let Some(memory_max) = memory_max else {
1675 return Err(ConfigError::MeasuredWithoutMemoryMax {
1676 slice: self.host.slice.clone(),
1677 });
1678 };
1679 let mirror = self.mirror_memory();
1680 if let Some((profile, needs)) = self
1681 .profiles
1682 .iter()
1683 .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1684 .find(|(_, needs)| *needs > memory_max.0)
1685 {
1686 return Err(ConfigError::ProfileOverBudget {
1687 profile: profile.clone(),
1688 needs: ByteSize(needs),
1689 slice: self.host.slice.clone(),
1690 memory_max,
1691 });
1692 }
1693 let needed = self.memory_needed()?;
1695 if needed > memory_max {
1696 return Err(ConfigError::OverBudget {
1697 total: needed,
1698 slice: self.host.slice.clone(),
1699 memory_max,
1700 });
1701 }
1702 return Ok(Vec::new());
1703 }
1704 match memory_max {
1705 Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1706 total,
1707 slice: self.host.slice.clone(),
1708 memory_max,
1709 }),
1710 Some(_) => Ok(Vec::new()),
1711 None => Ok(vec![Warning::UnlimitedSlice {
1712 slice: self.host.slice.clone(),
1713 total,
1714 }]),
1715 }
1716 }
1717}
1718
1719#[derive(Debug, thiserror::Error)]
1721pub enum ResizeError {
1722 #[error(transparent)]
1724 Parse(#[from] toml_edit::TomlError),
1725 #[error("the config has no profile {0:?} under [profiles]")]
1727 NoProfile(String),
1728}
1729
1730pub fn resize_profile(
1733 text: &str,
1734 profile: &str,
1735 resize: api::Resize,
1736) -> Result<String, ResizeError> {
1737 let mut document: toml_edit::DocumentMut = text.parse()?;
1738 let table = document
1739 .get_mut("profiles")
1740 .and_then(|profiles| profiles.get_mut(profile))
1741 .and_then(toml_edit::Item::as_table_like_mut)
1742 .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1743 let values = [
1744 (
1745 "memory",
1746 toml_edit::Value::from(ByteSize(resize.memory).exact()),
1747 ),
1748 ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1749 ];
1750 let cpus = resize
1751 .cpus
1752 .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1753 for (key, mut value) in values.into_iter().chain(cpus) {
1754 if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1756 *value.decor_mut() = old.decor().clone();
1757 }
1758 table.insert(key, toml_edit::Item::Value(value));
1759 }
1760 Ok(document.to_string())
1761}
1762
1763pub fn profile_sizes(text: &str, profile: &str) -> Option<api::Resize> {
1766 let config = Config::parse(text).ok()?;
1767 let sizes = config.profiles.get(profile)?;
1768 Some(api::Resize {
1769 memory: sizes.memory.0,
1770 slots: sizes.slots,
1771 cpus: Some(sizes.cpus),
1772 })
1773}
1774
1775pub fn resized_at(
1779 text: &str,
1780 profile: &str,
1781 resize: api::Resize,
1782) -> Option<std::ops::Range<usize>> {
1783 let document = toml_edit::Document::parse(text).ok()?;
1784 let table = document
1785 .as_table()
1786 .get("profiles")?
1787 .get(profile)?
1788 .as_table_like()?;
1789 let cpus = resize.cpus.map(|_| "cpus");
1790 ["memory", "slots"]
1791 .into_iter()
1792 .chain(cpus)
1793 .filter_map(|key| {
1794 let (key, item) = table.get_key_value(key)?;
1795 Some(key.span()?.start..item.span()?.end)
1796 })
1797 .reduce(|first, next| first.start.min(next.start)..first.end.max(next.end))
1798}
1799
1800pub const SECRET_SHOWN: &str = "<secret>";
1802
1803pub fn mask_secrets(text: &str) -> Option<String> {
1807 let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1808 let mut masked = false;
1809 for target in notify_targets(&mut document) {
1810 for (_, value) in secrets_of(target) {
1811 replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1812 masked = true;
1813 }
1814 }
1815 Some(if masked {
1816 document.to_string()
1817 } else {
1818 text.to_owned()
1819 })
1820}
1821
1822pub fn keep_secrets(text: &str, stored: &str) -> String {
1829 if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1830 return text.to_owned();
1831 }
1832 let (Ok(mut document), Ok(mut old)) = (
1833 text.parse::<toml_edit::DocumentMut>(),
1834 stored.parse::<toml_edit::DocumentMut>(),
1835 ) else {
1836 return text.to_owned();
1837 };
1838 let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1839 for target in notify_targets(&mut old) {
1840 let Some(name) = target_name(target) else {
1841 continue;
1842 };
1843 let secrets = secrets_of(target)
1844 .into_iter()
1845 .map(|(field, value)| (field, value.clone()))
1846 .collect();
1847 kept.insert(name, secrets);
1848 }
1849 for target in notify_targets(&mut document) {
1850 let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1851 continue;
1852 };
1853 for (field, value) in secrets_of(target) {
1854 if value.as_str() != Some(SECRET_SHOWN) {
1855 continue;
1856 }
1857 if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1858 replace_value(value, old.clone());
1859 }
1860 }
1861 }
1862 document.to_string()
1863}
1864
1865#[derive(Debug, PartialEq, Eq)]
1867enum SecretField {
1868 Url,
1869 Header(String),
1870}
1871
1872fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1874 match document
1875 .get_mut("notify")
1876 .and_then(|notify| notify.get_mut("targets"))
1877 {
1878 Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1879 .iter_mut()
1880 .map(|table| table as &mut dyn toml_edit::TableLike)
1881 .collect(),
1882 Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1883 .iter_mut()
1884 .filter_map(toml_edit::Value::as_inline_table_mut)
1885 .map(|table| table as &mut dyn toml_edit::TableLike)
1886 .collect(),
1887 _ => Vec::new(),
1888 }
1889}
1890
1891fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1892 target
1893 .get("name")
1894 .and_then(toml_edit::Item::as_str)
1895 .map(str::to_owned)
1896}
1897
1898fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1900 let mut found = Vec::new();
1901 for (key, item) in target.iter_mut() {
1902 match key.get() {
1903 "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1904 "headers" => {
1905 let Some(headers) = item.as_table_like_mut() else {
1906 continue;
1907 };
1908 for (name, item) in headers.iter_mut() {
1909 let field = SecretField::Header(name.get().to_owned());
1910 found.extend(item.as_value_mut().map(|value| (field, value)));
1911 }
1912 }
1913 _ => {}
1914 }
1915 }
1916 found
1917}
1918
1919fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1921 *new.decor_mut() = value.decor().clone();
1922 *value = new;
1923}
1924
1925pub fn revision(text: &str) -> String {
1928 let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1929 (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1930 });
1931 format!("{hash:016x}")
1932}
1933
1934pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1936 let output = Command::new("systemctl")
1937 .args(["show", slice, "--property=MemoryMax", "--value"])
1938 .output()?;
1939 if !output.status.success() {
1940 return Err(std::io::Error::other(format!(
1941 "systemctl show {slice} failed: {}",
1942 String::from_utf8_lossy(&output.stderr).trim()
1943 )));
1944 }
1945 let value = String::from_utf8_lossy(&output.stdout);
1946 parse_memory_max(value.trim()).ok_or_else(|| {
1947 std::io::Error::other(format!(
1948 "systemctl show {slice}: unexpected MemoryMax {value:?}"
1949 ))
1950 })
1951}
1952
1953pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1955 #[expect(
1956 clippy::cast_precision_loss,
1957 clippy::cast_possible_truncation,
1958 clippy::cast_sign_loss,
1959 reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1960 most 16"
1961 )]
1962 let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1963 budget
1964}
1965
1966pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1969 let output = Command::new("systemctl")
1970 .args([
1971 "show",
1972 slice,
1973 "--property=EffectiveCPUs",
1974 "--property=AllowedCPUs",
1975 "--property=CPUQuotaPerSecUSec",
1976 ])
1977 .output()?;
1978 if !output.status.success() {
1979 return Err(std::io::Error::other(format!(
1980 "systemctl show {slice} failed: {}",
1981 String::from_utf8_lossy(&output.stderr).trim()
1982 )));
1983 }
1984 let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1985 parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1986 std::io::Error::other(format!(
1987 "systemctl show {slice}: unexpected CPU properties {:?}",
1988 String::from_utf8_lossy(&output.stdout).trim()
1989 ))
1990 })
1991}
1992
1993pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1996 let value = |name: &str| {
1997 properties.lines().find_map(|line| {
1998 line.strip_prefix(name)
1999 .and_then(|rest| rest.strip_prefix('='))
2000 .map(str::trim)
2001 })
2002 };
2003 let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
2004 .into_iter()
2005 .flatten()
2006 .find(|cpus| !cpus.is_empty());
2007 let cpus = match set {
2008 Some(set) => cpu_count(set)?,
2009 None => host,
2010 };
2011 let quota = match value("CPUQuotaPerSecUSec") {
2012 None | Some("" | "infinity") => u64::MAX,
2013 Some(quota) => quota_millicores(quota)?,
2014 };
2015 Some(cpus.saturating_mul(1000).min(quota))
2016}
2017
2018fn cpu_count(set: &str) -> Option<u64> {
2020 set.split([',', ' '])
2021 .filter(|range| !range.is_empty())
2022 .try_fold(0u64, |count, range| {
2023 let (first, last) = range.split_once('-').unwrap_or((range, range));
2024 let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
2025 Some(count + last.checked_sub(first)? + 1)
2026 })
2027}
2028
2029fn quota_millicores(quota: &str) -> Option<u64> {
2032 const UNITS: [(&str, f64); 10] = [
2034 ("y", 31_557_600_000.0),
2035 ("M", 2_629_800_000.0),
2036 ("w", 604_800_000.0),
2037 ("d", 86_400_000.0),
2038 ("h", 3_600_000.0),
2039 ("min", 60_000.0),
2040 ("s", 1000.0),
2041 ("ms", 1.0),
2042 ("us", 0.001),
2043 ("µs", 0.001),
2044 ];
2045 let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
2046 let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
2047 let (number, unit) = part.split_at(at);
2048 let per_unit = UNITS
2049 .iter()
2050 .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
2051 Some(total + number.parse::<f64>().ok()? * per_unit)
2052 })?;
2053 #[allow(
2054 clippy::cast_possible_truncation,
2055 clippy::cast_sign_loss,
2056 reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
2057 )]
2058 (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
2059 .then(|| millicores.round() as u64)
2060}
2061
2062pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
2064 if value == "infinity" {
2065 Some(None)
2066 } else {
2067 value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
2068 }
2069}
2070
2071#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
2073pub struct ByteSize(pub u64);
2074
2075const UNITS: [(char, u64); 4] = [
2076 ('T', 1 << 40),
2077 ('G', 1 << 30),
2078 ('M', 1 << 20),
2079 ('K', 1 << 10),
2080];
2081
2082impl FromStr for ByteSize {
2083 type Err = String;
2084
2085 #[allow(
2086 clippy::cast_precision_loss,
2087 clippy::cast_possible_truncation,
2088 clippy::cast_sign_loss,
2089 reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
2090 )]
2091 fn from_str(s: &str) -> Result<Self, Self::Err> {
2092 let invalid = || {
2093 format!(
2094 "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
2095 )
2096 };
2097 let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
2098 let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
2099 let number = &s[..s.len() - 1];
2100 if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
2101 return Err(invalid());
2102 }
2103 let number: f64 = number.parse().map_err(|_| invalid())?;
2104 let bytes = (number * *factor as f64).round();
2105 if bytes >= u64::MAX as f64 {
2107 return Err(format!("size {s:?} is too large"));
2108 }
2109 Ok(ByteSize(bytes as u64))
2110 }
2111}
2112
2113impl fmt::Display for ByteSize {
2114 #[allow(
2115 clippy::cast_precision_loss,
2116 reason = "shown with two decimals, far coarser than f64's precision"
2117 )]
2118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2119 let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
2120 return write!(f, "{}B", self.0);
2121 };
2122 let number = format!("{:.2}", self.0 as f64 / *factor as f64);
2123 let number = number.trim_end_matches('0').trim_end_matches('.');
2124 write!(f, "{number}{unit}")
2125 }
2126}
2127
2128impl ByteSize {
2129 #[allow(
2133 clippy::cast_precision_loss,
2134 reason = "a unit is a power of two, so the quotient is exact below 2^53 bytes; the parse checks it"
2135 )]
2136 pub fn exact(self) -> String {
2137 let reads_back = |text: &String| text.parse::<ByteSize>() == Ok(self);
2138 let shown = self.to_string();
2139 if reads_back(&shown) {
2140 return shown;
2141 }
2142 let mut shortest: Option<String> = None;
2143 for (unit, factor) in UNITS {
2144 let text = format!("{}{unit}", self.0 as f64 / factor as f64);
2145 if reads_back(&text) && shortest.as_ref().is_none_or(|best| text.len() < best.len()) {
2146 shortest = Some(text);
2147 }
2148 }
2149 shortest.unwrap_or(shown)
2150 }
2151
2152 #[allow(
2156 clippy::cast_precision_loss,
2157 reason = "shown with one decimal at most, far coarser than f64's precision"
2158 )]
2159 pub fn short(self) -> String {
2160 let Some(index) = UNITS.iter().position(|(_, factor)| self.0 >= *factor) else {
2161 return format!("{}B", self.0);
2162 };
2163 let (mut unit, factor) = UNITS[index];
2164 let mut value = self.0 as f64 / factor as f64;
2165 if value.round() >= 1024.0
2166 && let Some(larger) = index.checked_sub(1)
2167 {
2168 unit = UNITS[larger].0;
2169 value /= 1024.0;
2170 }
2171 let tenths = (value * 10.0).round() / 10.0;
2173 if tenths < 10.0 {
2174 let number = format!("{tenths:.1}");
2175 format!("{}{unit}", number.trim_end_matches(".0"))
2176 } else {
2177 format!("{:.0}{unit}", value.round())
2178 }
2179 }
2180}
2181
2182impl<'de> Deserialize<'de> for ByteSize {
2183 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
2184 String::deserialize(deserializer)?
2185 .parse()
2186 .map_err(de::Error::custom)
2187 }
2188}
2189
2190pub fn parse_duration(s: &str) -> Result<Duration, String> {
2192 let invalid = || {
2193 format!(
2194 "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
2195 )
2196 };
2197 let (number, factor) = match s.chars().last() {
2198 Some('s') => (&s[..s.len() - 1], 1),
2199 Some('m') => (&s[..s.len() - 1], 60),
2200 Some('h') => (&s[..s.len() - 1], 3600),
2201 Some('d') => (&s[..s.len() - 1], 86400),
2202 _ => return Err(invalid()),
2203 };
2204 let number: u64 = number.parse().map_err(|_| invalid())?;
2205 number
2206 .checked_mul(factor)
2207 .map(Duration::from_secs)
2208 .ok_or_else(invalid)
2209}
2210
2211pub fn parse_age(text: &str) -> Result<Duration, String> {
2213 let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
2214 let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
2215 let (number, unit) = text.split_at(split);
2216 let number: u64 = number.parse().map_err(|_| error())?;
2217 let unit = match unit {
2218 "s" => 1,
2219 "m" => 60,
2220 "h" => 3600,
2221 "d" => 86_400,
2222 "w" => 7 * 86_400,
2223 _ => return Err(error()),
2224 };
2225 Ok(Duration::from_secs(number.saturating_mul(unit)))
2226}
2227
2228fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
2229 parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
2230}
2231
2232#[cfg(test)]
2233mod tests;