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}
92
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub enum ImageSyncs {
96 Skips,
98 Syncs,
100 Unread,
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum PastBoots {
107 Read,
109 NotKept,
111 Unreadable,
113}
114
115impl Host {
116 pub fn syncs_check(
120 &self,
121 images: &BTreeMap<String, ImageSyncs>,
122 boots: PastBoots,
123 ) -> api::Check {
124 let check = |status, detail: String, fix: Option<&str>| api::Check {
125 name: "runner syncs".to_owned(),
126 status,
127 detail,
128 fix: fix.map(str::to_owned),
129 };
130 if !self.skip_syncs {
131 return check(
132 api::CheckStatus::Info,
133 "kept, as [host] skip_syncs = false sets".to_owned(),
134 None,
135 );
136 }
137 let of = |wanted: ImageSyncs| -> Vec<&str> {
138 images
139 .iter()
140 .filter(|(_, syncs)| **syncs == wanted)
141 .map(|(image, _)| image.as_str())
142 .collect()
143 };
144 let (skips, syncs, unread) = (
145 of(ImageSyncs::Skips),
146 of(ImageSyncs::Syncs),
147 of(ImageSyncs::Unread),
148 );
149 let mut parts = Vec::new();
150 if !skips.is_empty() {
151 parts.push(format!(
152 "skipped by the runners of {}, which carry runnerd-skip-syncs",
153 skips.join(", ")
154 ));
155 }
156 if !syncs.is_empty() {
157 parts.push(format!(
158 "kept by the runners of {}: they lack runnerd-skip-syncs, so runnerd never asks them",
159 syncs.join(", ")
160 ));
161 }
162 if !unread.is_empty() {
163 parts.push(format!(
164 "decided at each runner's start for {}, which Docker does not have yet",
165 unread.join(", ")
166 ));
167 }
168 if parts.is_empty() {
169 parts.push(
170 "decided at each runner's start, from its image's runnerd.skip-syncs label"
171 .to_owned(),
172 );
173 }
174 let runners = parts.join("; ");
175 if skips.is_empty() && unread.is_empty() && !syncs.is_empty() {
176 return check(
177 api::CheckStatus::Info,
178 format!("{runners}. Nothing is emptied after a crash"),
179 None,
180 );
181 }
182 let crash = "a crash or power loss while such a runner ran empties the slots' volumes and the pnpm and Maven \
183 stores before any runner starts";
184 match boots {
185 PastBoots::Read => check(
186 api::CheckStatus::Info,
187 format!(
188 "{runners}. After {crash}; an orderly shutdown or reboot keeps them, as the journal shows its \
189 final sync"
190 ),
191 None,
192 ),
193 PastBoots::NotKept => check(
194 api::CheckStatus::Warn,
195 format!(
196 "{runners}. This host's journal keeps no past boot, so runnerd cannot tell an orderly shutdown \
197 from a crash: every boot after such a runner ran empties the slots' volumes and the pnpm and \
198 Maven stores before any runner starts"
199 ),
200 Some(
201 "keep the journal across boots: set Storage=persistent in /etc/systemd/journald.conf, then \
202 sudo systemctl restart systemd-journald; or set [host] skip_syncs = false",
203 ),
204 ),
205 PastBoots::Unreadable => check(
206 api::CheckStatus::Warn,
207 format!(
208 "{runners}. runnerd's user cannot read the system journal, so runnerd cannot tell an orderly \
209 shutdown from a crash: every boot after such a runner ran empties the slots' volumes and the \
210 pnpm and Maven stores before any runner starts"
211 ),
212 Some(
213 "let runnerd's user read the journal: sudo usermod -aG systemd-journal \"$USER\", then log in \
214 again; or set [host] skip_syncs = false",
215 ),
216 ),
217 }
218 }
219
220 fn check_cpu_overcommit(&self) -> Result<(), ConfigError> {
222 match self.cpu_overcommit {
223 Some(value) if !(value > 0.0 && value <= MAX_CPU_OVERCOMMIT) => {
224 Err(ConfigError::CpuOvercommit { value })
225 }
226 _ => Ok(()),
227 }
228 }
229}
230
231pub const MAX_CPU_OVERCOMMIT: f64 = 16.0;
233
234#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
236#[serde(rename_all = "kebab-case")]
237pub enum Admission {
238 Caps,
241 #[default]
245 Measured,
246}
247
248impl Admission {
249 pub fn policy(self) -> api::AdmissionPolicy {
251 match self {
252 Admission::Caps => api::AdmissionPolicy::Caps,
253 Admission::Measured => api::AdmissionPolicy::Measured,
254 }
255 }
256}
257
258fn default_on() -> bool {
259 true
260}
261
262fn default_poll_interval() -> Duration {
263 Duration::from_secs(15)
264}
265
266#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
268#[serde(rename_all = "kebab-case")]
269pub enum Isolation {
270 Sysbox,
272 UnsafeDind,
274}
275
276#[derive(Debug, Clone, Deserialize)]
278#[serde(deny_unknown_fields)]
279pub struct Caches {
280 pub mirror: Mirror,
282 pub pnpm: CacheLimit,
284 pub maven: CacheLimit,
286 #[serde(default = "default_reseed_at")]
289 pub reseed_at: ByteSize,
290 #[serde(default = "default_snapshot_max")]
293 pub snapshot_max: ByteSize,
294 #[serde(default)]
296 pub build: BuildCache,
297}
298
299#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
302#[serde(deny_unknown_fields, default)]
303pub struct BuildCache {
304 pub enabled: bool,
306 pub max_size: ByteSize,
308 pub branches: Vec<String>,
311 pub directory: Option<PathBuf>,
313 pub actions_cache: bool,
316}
317
318impl BuildCache {
319 pub fn actions_cache_check(&self) -> api::Check {
321 api::Check {
322 name: api::ACTIONS_CACHE.to_owned(),
323 status: api::CheckStatus::Info,
324 detail: if self.enabled {
325 api::actions_cache_place(self.actions_cache)
326 } else {
327 "GitHub's, as the build cache is off"
328 }
329 .to_owned(),
330 fix: None,
331 }
332 }
333
334 fn validate(&self) -> Result<(), ConfigError> {
335 if self.enabled && self.max_size.0 == 0 {
336 return Err(ConfigError::ZeroLimit {
337 field: "caches.build.max_size".to_owned(),
338 });
339 }
340 Ok(())
341 }
342}
343
344impl Default for BuildCache {
345 fn default() -> BuildCache {
346 BuildCache {
347 enabled: true,
348 max_size: ByteSize(10 << 30),
349 branches: Vec::new(),
350 directory: None,
351 actions_cache: true,
352 }
353 }
354}
355
356fn default_reseed_at() -> ByteSize {
357 ByteSize(2 << 30)
358}
359
360fn default_snapshot_max() -> ByteSize {
362 ByteSize(10 << 30)
363}
364
365fn default_mirror_max_size() -> ByteSize {
366 ByteSize(10 << 30)
367}
368
369#[derive(Debug, Clone, Deserialize)]
371#[serde(deny_unknown_fields)]
372pub struct Mirror {
373 pub enabled: bool,
375 pub memory: ByteSize,
377 pub cpus: u32,
379 #[serde(deserialize_with = "duration")]
381 pub ttl: Duration,
382 #[serde(default = "default_mirror_max_size")]
384 pub max_size: ByteSize,
385}
386
387#[derive(Debug, Clone, Deserialize)]
389#[serde(deny_unknown_fields)]
390pub struct CacheLimit {
391 pub max_size: ByteSize,
393}
394
395pub const PUBLISHED_RUNNER: &str = "ghcr.io/ayagmar/runnerd-runner";
398
399#[derive(Debug, Clone, Deserialize)]
401#[serde(try_from = "ProfileToml")]
402pub struct Profile {
403 pub image: Image,
405 pub cpus: u32,
407 pub memory: ByteSize,
409 pub slots: u32,
411 pub record_failures: bool,
413 pub min_memory: Option<ByteSize>,
415 pub max_memory: Option<ByteSize>,
417 pub max_slots: Option<u32>,
419 pub priced_as: Option<api::Sku>,
422}
423
424#[derive(Debug, Clone, PartialEq, Eq)]
426pub enum Image {
427 Stage(String),
429 Pulled(String),
432}
433
434impl Image {
435 pub fn reference(&self) -> String {
437 match self {
438 Image::Stage(stage) => format!("runnerd-runner:{stage}"),
439 Image::Pulled(reference) => reference.clone(),
440 }
441 }
442}
443
444#[derive(Deserialize)]
446#[serde(deny_unknown_fields)]
447struct ProfileToml {
448 image_stage: Option<String>,
449 image: Option<String>,
450 cpus: u32,
451 memory: ByteSize,
452 slots: u32,
453 #[serde(default)]
454 record_failures: bool,
455 min_memory: Option<ByteSize>,
456 max_memory: Option<ByteSize>,
457 max_slots: Option<u32>,
458 priced_as: Option<api::Sku>,
459}
460
461impl TryFrom<ProfileToml> for Profile {
462 type Error = String;
463
464 fn try_from(profile: ProfileToml) -> Result<Profile, String> {
465 let image = match (profile.image_stage, profile.image) {
466 (Some(stage), None) if !stage.is_empty() => Image::Stage(stage),
467 (None, Some(reference)) if !reference.is_empty() => Image::Pulled(reference),
468 (Some(_), Some(_)) => {
469 return Err(
470 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull), \
471 not both"
472 .to_owned(),
473 );
474 }
475 _ => {
476 return Err(
477 "set image_stage (a stage built from image/Dockerfile) or image (a reference to pull)"
478 .to_owned(),
479 );
480 }
481 };
482 Ok(Profile {
483 image,
484 cpus: profile.cpus,
485 memory: profile.memory,
486 slots: profile.slots,
487 record_failures: profile.record_failures,
488 min_memory: profile.min_memory,
489 max_memory: profile.max_memory,
490 max_slots: profile.max_slots,
491 priced_as: profile.priced_as,
492 })
493 }
494}
495
496pub fn labels_match(wanted: &[String], labels: &[String]) -> bool {
500 !wanted.is_empty()
501 && wanted.iter().all(|wanted| {
502 labels
503 .iter()
504 .any(|label| label.eq_ignore_ascii_case(wanted))
505 })
506}
507
508#[derive(Debug, Deserialize)]
511#[serde(deny_unknown_fields)]
512pub struct Repository {
513 pub name: String,
515 pub profile: String,
517 pub labels: Vec<String>,
519 #[serde(default = "default_warm")]
522 pub warm: u32,
523 #[serde(default = "default_true")]
525 pub build_cache: bool,
526 #[serde(default)]
529 pub build_cache_writers: BuildCacheWriters,
530}
531
532#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
534#[serde(rename_all = "kebab-case")]
535pub enum BuildCacheWriters {
536 #[default]
538 DefaultBranch,
539 PullRequests,
541}
542
543fn default_true() -> bool {
544 true
545}
546
547fn default_warm() -> u32 {
548 1
549}
550
551#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
553#[serde(deny_unknown_fields, default)]
554pub struct History {
555 pub database: Option<PathBuf>,
557 #[serde(deserialize_with = "duration")]
559 pub samples: Duration,
560 #[serde(deserialize_with = "duration")]
562 pub logs: Duration,
563 #[serde(deserialize_with = "duration")]
565 pub events: Duration,
566 #[serde(deserialize_with = "duration")]
568 pub jobs: Duration,
569 pub log_max: ByteSize,
571}
572
573impl Default for History {
574 fn default() -> History {
575 History {
576 database: None,
577 samples: Duration::from_hours(7 * 24),
578 logs: Duration::from_hours(30 * 24),
579 events: Duration::from_hours(30 * 24),
580 jobs: Duration::from_hours(90 * 24),
581 log_max: ByteSize(50 << 20),
582 }
583 }
584}
585
586#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
589#[serde(deny_unknown_fields, default)]
590pub struct Records {
591 pub max_total: ByteSize,
593 pub max_count: u32,
595 #[serde(deserialize_with = "duration")]
597 pub max_age: Duration,
598 pub min_free: ByteSize,
600}
601
602impl Default for Records {
603 fn default() -> Records {
604 Records {
605 max_total: ByteSize(20 << 30),
606 max_count: 10,
607 max_age: Duration::from_hours(7 * 24),
608 min_free: ByteSize(10 << 30),
609 }
610 }
611}
612
613#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
616#[serde(deny_unknown_fields, default)]
617pub struct Sizing {
618 pub margin: u32,
621 pub min_jobs: u32,
624 pub auto: bool,
627 #[serde(deserialize_with = "duration")]
630 pub interval: Duration,
631 #[serde(deserialize_with = "duration")]
634 pub raise_interval: Duration,
635 pub max_step: u32,
638}
639
640impl Sizing {
641 pub fn raise_after(&self) -> Duration {
644 self.raise_interval.min(self.interval)
645 }
646}
647
648impl Default for Sizing {
649 fn default() -> Sizing {
650 Sizing {
651 margin: 15,
652 min_jobs: 10,
653 auto: false,
654 interval: Duration::from_hours(1),
655 raise_interval: Duration::from_hours(1),
656 max_step: 50,
657 }
658 }
659}
660
661#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
663#[serde(deny_unknown_fields, default)]
664pub struct Web {
665 pub origins: Vec<Origin>,
668 pub open: Option<Origin>,
671}
672
673impl Web {
674 fn validate(&self) -> Result<(), ConfigError> {
676 match &self.open {
677 Some(open) if !self.origins.contains(open) => {
678 Err(ConfigError::OpenNotListed { open: open.clone() })
679 }
680 _ => Ok(()),
681 }
682 }
683}
684
685#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
688#[serde(deny_unknown_fields, default)]
689pub struct Github {
690 pub job_actions: bool,
693}
694
695impl Default for Github {
696 fn default() -> Github {
697 Github { job_actions: true }
698 }
699}
700
701#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
703#[serde(deny_unknown_fields, default)]
704pub struct Notify {
705 pub digest_at: DayTime,
707 pub targets: Vec<NotifyTarget>,
709}
710
711impl Default for Notify {
712 fn default() -> Notify {
713 Notify {
714 digest_at: DayTime { minutes: 9 * 60 },
715 targets: Vec::new(),
716 }
717 }
718}
719
720impl Notify {
721 fn validate(&self) -> Result<(), ConfigError> {
724 let refuse = |target: &str, why: &'static str| {
725 Err(ConfigError::Notify {
726 target: target.to_owned(),
727 why,
728 })
729 };
730 for (index, target) in self.targets.iter().enumerate() {
731 let name = target.name.as_str();
732 if name.trim().is_empty() || name.chars().any(char::is_control) {
733 return refuse(name, "name is empty");
734 }
735 if self.targets[..index].iter().any(|other| other.name == name) {
736 return refuse(name, "another target has this name");
737 }
738 let url = target.url.expose();
739 if url == SECRET_SHOWN {
740 return refuse(
741 name,
742 "url is <secret>, which runnerd fills in only for a target of the same name that had one; write the url",
743 );
744 }
745 let Some(rest) = url
746 .strip_prefix("https://")
747 .or_else(|| url.strip_prefix("http://"))
748 else {
749 return refuse(name, "url does not start with https:// or http://");
750 };
751 let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
752 if host.is_empty() || url.chars().any(|c| c.is_whitespace() || c.is_control()) {
753 return refuse(name, "url is not a URL");
754 }
755 if target.kind == NotifyKind::Ntfy
756 && (path.is_empty() || path.contains(['/', '?', '#']))
757 {
758 return refuse(
759 name,
760 "url is not an ntfy topic's, such as https://ntfy.sh/<topic>",
761 );
762 }
763 for (header, value) in &target.headers {
764 if value.expose() == SECRET_SHOWN {
765 return refuse(
766 name,
767 "a header is <secret>, which runnerd fills in only for a target of the same name that had it; write its value",
768 );
769 }
770 let token = |c: char| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c);
771 if header.is_empty() || !header.chars().all(token) {
772 return refuse(name, "a header's name is not a header name");
773 }
774 if value.expose().chars().any(char::is_control) {
775 return refuse(name, "a header's value has a control character");
776 }
777 }
778 if target.events.contains(&api::NotifyEvent::Test) {
779 return refuse(
780 name,
781 "events lists test, which only runnerctl notify test sends",
782 );
783 }
784 }
785 Ok(())
786 }
787}
788
789#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
791#[serde(deny_unknown_fields)]
792pub struct NotifyTarget {
793 pub name: String,
795 pub kind: NotifyKind,
797 pub url: Secret,
800 #[serde(default)]
802 pub headers: BTreeMap<String, Secret>,
803 #[serde(default = "default_notify_events")]
805 pub events: Vec<api::NotifyEvent>,
806}
807
808fn default_notify_events() -> Vec<api::NotifyEvent> {
809 vec![
810 api::NotifyEvent::Failed,
811 api::NotifyEvent::Lost,
812 api::NotifyEvent::Runner,
813 api::NotifyEvent::Insights,
814 ]
815}
816
817#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
819#[serde(rename_all = "kebab-case")]
820pub enum NotifyKind {
821 Webhook,
823 Ntfy,
825 Discord,
827}
828
829#[derive(Clone, PartialEq, Eq, Deserialize)]
831#[serde(transparent)]
832pub struct Secret(String);
833
834impl Secret {
835 pub fn expose(&self) -> &str {
837 &self.0
838 }
839}
840
841impl From<&str> for Secret {
842 fn from(value: &str) -> Secret {
843 Secret(value.to_owned())
844 }
845}
846
847impl fmt::Debug for Secret {
848 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
849 f.write_str("<secret>")
850 }
851}
852
853#[derive(Debug, Clone, Copy, PartialEq, Eq)]
855pub struct DayTime {
856 pub minutes: u32,
858}
859
860impl FromStr for DayTime {
861 type Err = String;
862
863 fn from_str(text: &str) -> Result<DayTime, String> {
864 let invalid = || format!("invalid time of day {text:?}, expected HH:MM such as \"09:00\"");
865 let (hour, minute) = text.split_once(':').ok_or_else(invalid)?;
866 let digits = |part: &str| part.len() == 2 && part.chars().all(|c| c.is_ascii_digit());
867 if !digits(hour) || !digits(minute) {
868 return Err(invalid());
869 }
870 let (hour, minute): (u32, u32) = (
871 hour.parse().map_err(|_| invalid())?,
872 minute.parse().map_err(|_| invalid())?,
873 );
874 if hour > 23 || minute > 59 {
875 return Err(invalid());
876 }
877 Ok(DayTime {
878 minutes: hour * 60 + minute,
879 })
880 }
881}
882
883impl fmt::Display for DayTime {
884 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
885 write!(f, "{:02}:{:02}", self.minutes / 60, self.minutes % 60)
886 }
887}
888
889impl<'de> Deserialize<'de> for DayTime {
890 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
891 String::deserialize(deserializer)?
892 .parse()
893 .map_err(de::Error::custom)
894 }
895}
896
897#[derive(Debug, Clone, PartialEq, Eq)]
901pub struct Origin(String);
902
903impl Origin {
904 pub fn as_str(&self) -> &str {
906 &self.0
907 }
908}
909
910impl FromStr for Origin {
911 type Err = String;
912
913 fn from_str(s: &str) -> Result<Origin, String> {
914 let invalid = |why: &str| {
915 format!(
916 "invalid origin {s:?}: {why}; write the scheme, host and port only, such as \
917 \"https://host.example.ts.net:8443\""
918 )
919 };
920 let (scheme, rest) = s
921 .split_once("://")
922 .ok_or_else(|| invalid("it has no scheme"))?;
923 let scheme = scheme.to_ascii_lowercase();
924 if rest.contains(['/', '?', '#']) {
925 return Err(invalid("it has a path"));
926 }
927 if rest.contains('@') {
928 return Err(invalid("it names a user"));
929 }
930 let (host, port) = match rest.strip_prefix('[') {
931 Some(v6) => {
932 let (address, after) = v6
933 .split_once(']')
934 .ok_or_else(|| invalid("its IPv6 address has no closing ]"))?;
935 let port = match after {
936 "" => None,
937 _ => Some(after.strip_prefix(':').ok_or_else(|| invalid("its port"))?),
938 };
939 (format!("[{address}]"), port)
940 }
941 None => match rest.split_once(':') {
942 Some((host, port)) => (host.to_owned(), Some(port)),
943 None => (rest.to_owned(), None),
944 },
945 };
946 let host = host.to_ascii_lowercase();
947 let not_a_host = || invalid("its host is not a name or an address");
948 let (host, loopback) =
950 if let Some(v6) = host.strip_prefix('[').and_then(|v6| v6.strip_suffix(']')) {
951 let address: Ipv6Addr = v6.parse().map_err(|_| not_a_host())?;
952 (format!("[{}]", ipv6_origin(address)), address.is_loopback())
953 } else {
954 if host.is_empty()
955 || !host
956 .chars()
957 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
958 {
959 return Err(not_a_host());
960 }
961 let name = host.strip_suffix('.').unwrap_or(&host);
963 let last = name.rsplit('.').next().unwrap_or_default();
964 let numeric = !last.is_empty()
965 && (last.bytes().all(|byte| byte.is_ascii_digit())
966 || last
967 .strip_prefix("0x")
968 .is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit())));
969 if numeric {
970 let address: Ipv4Addr = name.parse().map_err(|_| {
971 invalid("its IPv4 address is not four decimal numbers, such as 192.0.2.1")
972 })?;
973 (address.to_string(), address.is_loopback())
974 } else {
975 let loopback = host == "localhost";
976 (host, loopback)
977 }
978 };
979 let port = port
980 .map(|port| {
981 Some(port)
982 .filter(|port| port.bytes().all(|byte| byte.is_ascii_digit()))
983 .and_then(|port| port.parse::<u16>().ok())
984 .filter(|port| *port != 0)
985 .ok_or_else(|| invalid("its port is not a number from 1 to 65535"))
986 })
987 .transpose()?;
988 let default_port = match scheme.as_str() {
989 "https" => 443,
990 "http" if loopback => 80,
991 "http" => {
992 return Err(invalid(
993 "http is for a loopback host only, since the session cookie would cross the network in \
994 the clear; use https",
995 ));
996 }
997 _ => return Err(invalid("its scheme is not https")),
998 };
999 Ok(Origin(match port {
1000 Some(port) if port != default_port => format!("{scheme}://{host}:{port}"),
1001 _ => format!("{scheme}://{host}"),
1002 }))
1003 }
1004}
1005
1006fn ipv6_origin(address: Ipv6Addr) -> String {
1009 let pieces = address.segments();
1010 let (mut zeros_at, mut zeros) = (0, 0);
1011 let mut at = 0;
1012 while at < pieces.len() {
1013 let run = pieces[at..].iter().take_while(|piece| **piece == 0).count();
1014 if run > zeros {
1015 (zeros_at, zeros) = (at, run);
1016 }
1017 at += run.max(1);
1018 }
1019 let hex = |pieces: &[u16]| {
1020 pieces
1021 .iter()
1022 .map(|piece| format!("{piece:x}"))
1023 .collect::<Vec<_>>()
1024 .join(":")
1025 };
1026 if zeros < 2 {
1027 return hex(&pieces);
1028 }
1029 format!(
1030 "{}::{}",
1031 hex(&pieces[..zeros_at]),
1032 hex(&pieces[zeros_at + zeros..])
1033 )
1034}
1035
1036impl fmt::Display for Origin {
1037 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1038 f.write_str(&self.0)
1039 }
1040}
1041
1042impl<'de> Deserialize<'de> for Origin {
1043 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1044 String::deserialize(deserializer)?
1045 .parse()
1046 .map_err(de::Error::custom)
1047 }
1048}
1049
1050#[derive(Debug, Clone, PartialEq, Eq)]
1052pub struct ParseError {
1053 pub line: usize,
1055 pub message: String,
1057}
1058
1059#[derive(Debug, thiserror::Error)]
1061pub enum ConfigError {
1062 #[error("cannot read {}: {source}", path.display())]
1064 Read {
1065 path: PathBuf,
1067 source: std::io::Error,
1069 },
1070 #[error(transparent)]
1072 Parse(#[from] toml::de::Error),
1073 #[error(
1075 "repository {repository} uses profile {profile:?}, which is not defined under [profiles]; {}",
1076 defined_profiles(.defined)
1077 )]
1078 UnknownProfile {
1079 repository: String,
1081 profile: String,
1083 defined: Vec<String>,
1085 },
1086 #[error("repository {repository} needs at least one non-empty label")]
1088 MissingLabels {
1089 repository: String,
1091 },
1092 #[error(
1094 "repository {repository} has two entries with profile {profile:?}; give each of its pools its own profile"
1095 )]
1096 PoolProfileTwice {
1097 repository: String,
1099 profile: String,
1101 },
1102 #[error(
1105 "repository {repository}'s pools in {first} and {second} both have the label {label:?}, so a job asking for \
1106 it could land in either; give each pool labels of its own, such as only {second:?}'s own label for the \
1107 lighter one"
1108 )]
1109 PoolLabelsOverlap {
1110 repository: String,
1112 first: String,
1114 second: String,
1116 label: String,
1118 },
1119 #[error(
1121 "repository {repository}'s entries set build_cache_writers differently; whose runs may write to the build \
1122 cache is the repository's, so set the same in each"
1123 )]
1124 PoolWritersDiffer {
1125 repository: String,
1127 },
1128 #[error(
1130 "isolation \"unsafe-dind\" runs runner containers privileged, so a job can take over the host; \
1131 set allow_unsafe = true under [host] to use it anyway"
1132 )]
1133 UnsafeNotAllowed,
1134 #[error("{field} is 0; set it above 0")]
1136 ZeroLimit {
1137 field: String,
1139 },
1140 #[error(
1142 "host.cpu_overcommit is {value}; set it above 0 and at most {MAX_CPU_OVERCOMMIT}, such as 1.0"
1143 )]
1144 CpuOvercommit {
1145 value: f64,
1147 },
1148 #[error(
1150 "web.open is {open}, which is not one of [web] origins; add it to origins, or remove open"
1151 )]
1152 OpenNotListed {
1153 open: Origin,
1155 },
1156 #[error("profiles.{profile}: {why}")]
1159 SizingBounds {
1160 profile: String,
1162 why: &'static str,
1164 },
1165 #[error("notify target {target:?}: {why}")]
1167 Notify {
1168 target: String,
1170 why: &'static str,
1172 },
1173 #[error(
1175 "host.memory_ceiling is {ceiling}, under the {needs} one runner of {profile} and the mirror take; set it \
1176 to the most memory the host can grow to, such as the VM's maximum"
1177 )]
1178 CeilingUnderProfile {
1179 profile: String,
1181 needs: ByteSize,
1183 ceiling: ByteSize,
1185 },
1186 #[error("slots × memory over all profiles plus the mirror is too large to add up")]
1188 BudgetOverflow,
1189 #[error(
1191 "measured admission, the default, admits runners against the slice {slice}'s MemoryMax, and it has none; \
1192 install a unit file for it (`runnerd doctor` prints one), or set [host] admission = \"caps\""
1193 )]
1194 MeasuredWithoutMemoryMax {
1195 slice: String,
1197 },
1198 #[error(
1200 "one runner of {profile} and the mirror take {needs}, the slice {slice}'s MemoryMax is {memory_max}; lower \
1201 its `memory` or raise the slice's MemoryMax"
1202 )]
1203 ProfileOverBudget {
1204 profile: String,
1206 needs: ByteSize,
1208 slice: String,
1210 memory_max: ByteSize,
1212 },
1213 #[error(
1215 "slots × memory over all profiles plus the mirror is {total}, \
1216 the slice {slice}'s MemoryMax is {memory_max}; lower `slots` or `memory`, or raise the slice's \
1217 MemoryMax (`runnerd doctor` prints its unit)"
1218 )]
1219 OverBudget {
1220 total: ByteSize,
1222 slice: String,
1224 memory_max: ByteSize,
1226 },
1227}
1228
1229fn defined_profiles(defined: &[String]) -> String {
1230 if defined.is_empty() {
1231 "it defines none".to_owned()
1232 } else {
1233 format!("defined: {}", defined.join(", "))
1234 }
1235}
1236
1237#[derive(Debug, Clone, PartialEq, Eq)]
1239pub enum Warning {
1240 UnlimitedSlice {
1242 slice: String,
1244 total: ByteSize,
1246 },
1247}
1248
1249impl fmt::Display for Warning {
1250 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1251 match self {
1252 Warning::UnlimitedSlice { slice, total } => write!(
1253 f,
1254 "the slice {slice} has no MemoryMax, so nothing holds runners to their {total} of caps; \
1255 install a unit file for it"
1256 ),
1257 }
1258 }
1259}
1260
1261impl Config {
1262 pub fn load(path: &Path) -> Result<Config, ConfigError> {
1264 let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
1265 path: path.to_owned(),
1266 source,
1267 })?;
1268 Config::parse(&text)
1269 }
1270
1271 pub fn parse(text: &str) -> Result<Config, ConfigError> {
1273 Ok(toml::from_str(text)?)
1274 }
1275
1276 pub fn unreviewed() -> &'static str {
1279 "not saved: the text changed after its changes were shown; review them again"
1280 }
1281
1282 pub fn parse_error(text: &str) -> Option<ParseError> {
1286 let Err(error) = toml::from_str::<Config>(text) else {
1287 return None;
1288 };
1289 let span = error.span()?;
1290 let before = text.get(..span.start)?;
1291 Some(ParseError {
1292 line: before.matches('\n').count() + 1,
1293 message: error.message().trim().to_owned(),
1294 })
1295 }
1296
1297 pub fn check(
1300 text: &str,
1301 memory_max: impl FnOnce(&str) -> std::io::Result<Option<ByteSize>>,
1302 ) -> (Option<Config>, api::ConfigCheck) {
1303 let config = match Config::parse(text) {
1304 Ok(config) => config,
1305 Err(error) => return (None, budget::invalid(error.to_string(), None, None)),
1306 };
1307 let max = match memory_max(&config.host.slice) {
1308 Ok(max) => max,
1309 Err(error) => {
1310 return (
1311 None,
1312 budget::invalid(error.to_string(), Some(&config), None),
1313 );
1314 }
1315 };
1316 let check = config.check_against(max);
1317 if check.valid {
1318 (Some(config), check)
1319 } else {
1320 (None, check)
1321 }
1322 }
1323
1324 pub fn mirror_memory(&self) -> u64 {
1326 if self.caches.mirror.enabled {
1327 self.caches.mirror.memory.0
1328 } else {
1329 0
1330 }
1331 }
1332
1333 pub fn memory_caps(&self) -> Result<ByteSize, ConfigError> {
1335 let mirror = self.mirror_memory();
1336 self.profiles
1337 .values()
1338 .try_fold(mirror, |total, p| {
1339 p.memory
1340 .0
1341 .checked_mul(u64::from(p.slots))?
1342 .checked_add(total)
1343 })
1344 .map(ByteSize)
1345 .ok_or(ConfigError::BudgetOverflow)
1346 }
1347
1348 pub fn memory_needed(&self) -> Result<ByteSize, ConfigError> {
1351 match self.host.admission {
1352 Admission::Caps => self.memory_caps(),
1353 Admission::Measured => {
1354 let largest = self
1355 .profiles
1356 .values()
1357 .map(|p| p.memory.0)
1358 .max()
1359 .unwrap_or(0);
1360 largest
1361 .checked_add(self.mirror_memory())
1362 .map(ByteSize)
1363 .ok_or(ConfigError::BudgetOverflow)
1364 }
1365 }
1366 }
1367
1368 pub fn runner_ceiling(&self) -> Option<u64> {
1372 self.host
1373 .memory_ceiling
1374 .map(|ceiling| ceiling.0.saturating_sub(self.mirror_memory()))
1375 }
1376
1377 fn check_memory_ceiling(&self) -> Result<(), ConfigError> {
1382 let (Some(ceiling), Some(most)) = (self.host.memory_ceiling, self.runner_ceiling()) else {
1383 return Ok(());
1384 };
1385 let largest = self
1386 .profiles
1387 .iter()
1388 .max_by_key(|(_, profile)| profile.memory.0);
1389 match largest {
1390 Some((profile, largest)) if largest.memory.0 > most => {
1391 Err(ConfigError::CeilingUnderProfile {
1392 profile: profile.clone(),
1393 needs: ByteSize(largest.memory.0.saturating_add(self.mirror_memory())),
1394 ceiling,
1395 })
1396 }
1397 _ => Ok(()),
1398 }
1399 }
1400
1401 fn check_sizing_bounds(&self) -> Result<(), ConfigError> {
1403 for (name, p) in &self.profiles {
1404 let memory = p.memory.0;
1405 let field = if p
1406 .min_memory
1407 .zip(p.max_memory)
1408 .is_some_and(|(min, max)| min.0 > max.0)
1409 {
1410 "min_memory is above its max_memory"
1411 } else if p.min_memory.is_some_and(|min| memory < min.0) {
1412 "memory is below its min_memory"
1413 } else if p.max_memory.is_some_and(|max| memory > max.0) {
1414 "memory is above its max_memory"
1415 } else if p.max_slots.is_some_and(|max| p.slots > max) {
1416 "slots are above its max_slots"
1417 } else {
1418 continue;
1419 };
1420 return Err(ConfigError::SizingBounds {
1421 profile: name.clone(),
1422 why: field,
1423 });
1424 }
1425 Ok(())
1426 }
1427
1428 fn check_pools(&self) -> Result<(), ConfigError> {
1431 for (at, first) in self.repositories.iter().enumerate() {
1432 for second in self.repositories[at + 1..]
1433 .iter()
1434 .filter(|second| second.name == first.name)
1435 {
1436 if second.profile == first.profile {
1437 return Err(ConfigError::PoolProfileTwice {
1438 repository: first.name.clone(),
1439 profile: first.profile.clone(),
1440 });
1441 }
1442 if let Some(label) = first.labels.iter().find(|label| {
1443 second
1444 .labels
1445 .iter()
1446 .any(|other| other.eq_ignore_ascii_case(label))
1447 }) {
1448 return Err(ConfigError::PoolLabelsOverlap {
1449 repository: first.name.clone(),
1450 first: first.profile.clone(),
1451 second: second.profile.clone(),
1452 label: label.clone(),
1453 });
1454 }
1455 if second.build_cache_writers != first.build_cache_writers {
1456 return Err(ConfigError::PoolWritersDiffer {
1457 repository: first.name.clone(),
1458 });
1459 }
1460 }
1461 }
1462 Ok(())
1463 }
1464
1465 pub fn pool(&self, repository: &str, labels: &[String]) -> Option<&Repository> {
1468 self.repositories
1469 .iter()
1470 .find(|pool| pool.name == repository && labels_match(labels, &pool.labels))
1471 }
1472
1473 pub fn pool_in(&self, repository: &str, profile: &str) -> Option<&Repository> {
1475 self.repositories
1476 .iter()
1477 .find(|pool| pool.name == repository && pool.profile == profile)
1478 }
1479
1480 pub fn pricing(&self) -> api::Pricing {
1482 api::Pricing {
1483 profiles: self
1484 .profiles
1485 .iter()
1486 .filter_map(|(name, profile)| Some((name.clone(), profile.priced_as?)))
1487 .collect(),
1488 default_sku: api::Sku::standard_for(std::env::consts::ARCH),
1489 }
1490 }
1491
1492 pub fn repository_names(&self) -> Vec<String> {
1494 let mut names: Vec<String> = Vec::new();
1495 for repository in &self.repositories {
1496 if !names.contains(&repository.name) {
1497 names.push(repository.name.clone());
1498 }
1499 }
1500 names
1501 }
1502
1503 pub fn validate(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1505 if self.host.isolation == Isolation::UnsafeDind && !self.host.allow_unsafe {
1506 return Err(ConfigError::UnsafeNotAllowed);
1507 }
1508 let zero_limits = self.profiles.iter().flat_map(|(name, p)| {
1509 [
1510 (p.memory.0 == 0, format!("profiles.{name}.memory")),
1511 (p.cpus == 0, format!("profiles.{name}.cpus")),
1512 (p.slots == 0, format!("profiles.{name}.slots")),
1513 (p.max_slots == Some(0), format!("profiles.{name}.max_slots")),
1514 ]
1515 });
1516 let mirror = &self.caches.mirror;
1517 let mirror_zero_limits = [
1518 (
1519 mirror.enabled && mirror.memory.0 == 0,
1520 "caches.mirror.memory".to_owned(),
1521 ),
1522 (
1523 mirror.enabled && mirror.cpus == 0,
1524 "caches.mirror.cpus".to_owned(),
1525 ),
1526 (
1527 self.host.poll_interval.is_zero(),
1528 "host.poll_interval".to_owned(),
1529 ),
1530 (
1531 self.host.memory_ceiling == Some(ByteSize(0)),
1532 "host.memory_ceiling".to_owned(),
1533 ),
1534 (self.records.max_count == 0, "records.max_count".to_owned()),
1535 (self.sizing.min_jobs == 0, "sizing.min_jobs".to_owned()),
1536 (self.sizing.interval.is_zero(), "sizing.interval".to_owned()),
1537 (
1538 self.sizing.raise_interval.is_zero(),
1539 "sizing.raise_interval".to_owned(),
1540 ),
1541 (self.sizing.max_step == 0, "sizing.max_step".to_owned()),
1542 (
1543 self.records.max_total.0 == 0,
1544 "records.max_total".to_owned(),
1545 ),
1546 ];
1547 if let Some((_, field)) = zero_limits
1548 .chain(mirror_zero_limits)
1549 .find(|(zero, _)| *zero)
1550 {
1551 return Err(ConfigError::ZeroLimit { field });
1552 }
1553 self.check_sizing_bounds()?;
1554 self.caches.build.validate()?;
1555 self.host.check_cpu_overcommit()?;
1556 self.check_memory_ceiling()?;
1557 self.web.validate()?;
1558 self.notify.validate()?;
1559 for repository in &self.repositories {
1560 if !self.profiles.contains_key(&repository.profile) {
1561 return Err(ConfigError::UnknownProfile {
1562 repository: repository.name.clone(),
1563 profile: repository.profile.clone(),
1564 defined: self.profiles.keys().cloned().collect(),
1565 });
1566 }
1567 if repository.labels.is_empty() || repository.labels.iter().any(|l| l.trim().is_empty())
1568 {
1569 return Err(ConfigError::MissingLabels {
1570 repository: repository.name.clone(),
1571 });
1572 }
1573 }
1574 self.check_pools()?;
1575 self.check_budget(memory_max)
1576 }
1577
1578 fn check_budget(&self, memory_max: Option<ByteSize>) -> Result<Vec<Warning>, ConfigError> {
1580 let total = self.memory_caps()?;
1581 if self.host.admission == Admission::Measured {
1582 let Some(memory_max) = memory_max else {
1583 return Err(ConfigError::MeasuredWithoutMemoryMax {
1584 slice: self.host.slice.clone(),
1585 });
1586 };
1587 let mirror = self.mirror_memory();
1588 if let Some((profile, needs)) = self
1589 .profiles
1590 .iter()
1591 .map(|(name, profile)| (name, profile.memory.0.saturating_add(mirror)))
1592 .find(|(_, needs)| *needs > memory_max.0)
1593 {
1594 return Err(ConfigError::ProfileOverBudget {
1595 profile: profile.clone(),
1596 needs: ByteSize(needs),
1597 slice: self.host.slice.clone(),
1598 memory_max,
1599 });
1600 }
1601 let needed = self.memory_needed()?;
1603 if needed > memory_max {
1604 return Err(ConfigError::OverBudget {
1605 total: needed,
1606 slice: self.host.slice.clone(),
1607 memory_max,
1608 });
1609 }
1610 return Ok(Vec::new());
1611 }
1612 match memory_max {
1613 Some(memory_max) if total > memory_max => Err(ConfigError::OverBudget {
1614 total,
1615 slice: self.host.slice.clone(),
1616 memory_max,
1617 }),
1618 Some(_) => Ok(Vec::new()),
1619 None => Ok(vec![Warning::UnlimitedSlice {
1620 slice: self.host.slice.clone(),
1621 total,
1622 }]),
1623 }
1624 }
1625}
1626
1627#[derive(Debug, thiserror::Error)]
1629pub enum ResizeError {
1630 #[error(transparent)]
1632 Parse(#[from] toml_edit::TomlError),
1633 #[error("the config has no profile {0:?} under [profiles]")]
1635 NoProfile(String),
1636}
1637
1638pub fn resize_profile(
1641 text: &str,
1642 profile: &str,
1643 resize: api::Resize,
1644) -> Result<String, ResizeError> {
1645 let mut document: toml_edit::DocumentMut = text.parse()?;
1646 let table = document
1647 .get_mut("profiles")
1648 .and_then(|profiles| profiles.get_mut(profile))
1649 .and_then(toml_edit::Item::as_table_like_mut)
1650 .ok_or_else(|| ResizeError::NoProfile(profile.to_owned()))?;
1651 let values = [
1652 (
1653 "memory",
1654 toml_edit::Value::from(ByteSize(resize.memory).exact()),
1655 ),
1656 ("slots", toml_edit::Value::from(i64::from(resize.slots))),
1657 ];
1658 let cpus = resize
1659 .cpus
1660 .map(|cpus| ("cpus", toml_edit::Value::from(i64::from(cpus))));
1661 for (key, mut value) in values.into_iter().chain(cpus) {
1662 if let Some(old) = table.get(key).and_then(toml_edit::Item::as_value) {
1664 *value.decor_mut() = old.decor().clone();
1665 }
1666 table.insert(key, toml_edit::Item::Value(value));
1667 }
1668 Ok(document.to_string())
1669}
1670
1671pub fn profile_sizes(text: &str, profile: &str) -> Option<api::Resize> {
1674 let config = Config::parse(text).ok()?;
1675 let sizes = config.profiles.get(profile)?;
1676 Some(api::Resize {
1677 memory: sizes.memory.0,
1678 slots: sizes.slots,
1679 cpus: Some(sizes.cpus),
1680 })
1681}
1682
1683pub fn resized_at(
1687 text: &str,
1688 profile: &str,
1689 resize: api::Resize,
1690) -> Option<std::ops::Range<usize>> {
1691 let document = toml_edit::Document::parse(text).ok()?;
1692 let table = document
1693 .as_table()
1694 .get("profiles")?
1695 .get(profile)?
1696 .as_table_like()?;
1697 let cpus = resize.cpus.map(|_| "cpus");
1698 ["memory", "slots"]
1699 .into_iter()
1700 .chain(cpus)
1701 .filter_map(|key| {
1702 let (key, item) = table.get_key_value(key)?;
1703 Some(key.span()?.start..item.span()?.end)
1704 })
1705 .reduce(|first, next| first.start.min(next.start)..first.end.max(next.end))
1706}
1707
1708pub const SECRET_SHOWN: &str = "<secret>";
1710
1711pub fn mask_secrets(text: &str) -> Option<String> {
1715 let mut document: toml_edit::DocumentMut = text.parse().ok()?;
1716 let mut masked = false;
1717 for target in notify_targets(&mut document) {
1718 for (_, value) in secrets_of(target) {
1719 replace_value(value, toml_edit::Value::from(SECRET_SHOWN));
1720 masked = true;
1721 }
1722 }
1723 Some(if masked {
1724 document.to_string()
1725 } else {
1726 text.to_owned()
1727 })
1728}
1729
1730pub fn keep_secrets(text: &str, stored: &str) -> String {
1737 if !text.contains(SECRET_SHOWN) || Config::parse(text).is_err() {
1738 return text.to_owned();
1739 }
1740 let (Ok(mut document), Ok(mut old)) = (
1741 text.parse::<toml_edit::DocumentMut>(),
1742 stored.parse::<toml_edit::DocumentMut>(),
1743 ) else {
1744 return text.to_owned();
1745 };
1746 let mut kept: BTreeMap<String, Vec<(SecretField, toml_edit::Value)>> = BTreeMap::new();
1747 for target in notify_targets(&mut old) {
1748 let Some(name) = target_name(target) else {
1749 continue;
1750 };
1751 let secrets = secrets_of(target)
1752 .into_iter()
1753 .map(|(field, value)| (field, value.clone()))
1754 .collect();
1755 kept.insert(name, secrets);
1756 }
1757 for target in notify_targets(&mut document) {
1758 let Some(secrets) = target_name(target).and_then(|name| kept.get(&name)) else {
1759 continue;
1760 };
1761 for (field, value) in secrets_of(target) {
1762 if value.as_str() != Some(SECRET_SHOWN) {
1763 continue;
1764 }
1765 if let Some((_, old)) = secrets.iter().find(|(kept, _)| *kept == field) {
1766 replace_value(value, old.clone());
1767 }
1768 }
1769 }
1770 document.to_string()
1771}
1772
1773#[derive(Debug, PartialEq, Eq)]
1775enum SecretField {
1776 Url,
1777 Header(String),
1778}
1779
1780fn notify_targets(document: &mut toml_edit::DocumentMut) -> Vec<&mut dyn toml_edit::TableLike> {
1782 match document
1783 .get_mut("notify")
1784 .and_then(|notify| notify.get_mut("targets"))
1785 {
1786 Some(toml_edit::Item::ArrayOfTables(tables)) => tables
1787 .iter_mut()
1788 .map(|table| table as &mut dyn toml_edit::TableLike)
1789 .collect(),
1790 Some(toml_edit::Item::Value(toml_edit::Value::Array(array))) => array
1791 .iter_mut()
1792 .filter_map(toml_edit::Value::as_inline_table_mut)
1793 .map(|table| table as &mut dyn toml_edit::TableLike)
1794 .collect(),
1795 _ => Vec::new(),
1796 }
1797}
1798
1799fn target_name(target: &dyn toml_edit::TableLike) -> Option<String> {
1800 target
1801 .get("name")
1802 .and_then(toml_edit::Item::as_str)
1803 .map(str::to_owned)
1804}
1805
1806fn secrets_of(target: &mut dyn toml_edit::TableLike) -> Vec<(SecretField, &mut toml_edit::Value)> {
1808 let mut found = Vec::new();
1809 for (key, item) in target.iter_mut() {
1810 match key.get() {
1811 "url" => found.extend(item.as_value_mut().map(|value| (SecretField::Url, value))),
1812 "headers" => {
1813 let Some(headers) = item.as_table_like_mut() else {
1814 continue;
1815 };
1816 for (name, item) in headers.iter_mut() {
1817 let field = SecretField::Header(name.get().to_owned());
1818 found.extend(item.as_value_mut().map(|value| (field, value)));
1819 }
1820 }
1821 _ => {}
1822 }
1823 }
1824 found
1825}
1826
1827fn replace_value(value: &mut toml_edit::Value, mut new: toml_edit::Value) {
1829 *new.decor_mut() = value.decor().clone();
1830 *value = new;
1831}
1832
1833pub fn revision(text: &str) -> String {
1836 let hash = text.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
1837 (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3)
1838 });
1839 format!("{hash:016x}")
1840}
1841
1842pub fn slice_memory_max(slice: &str) -> std::io::Result<Option<ByteSize>> {
1844 let output = Command::new("systemctl")
1845 .args(["show", slice, "--property=MemoryMax", "--value"])
1846 .output()?;
1847 if !output.status.success() {
1848 return Err(std::io::Error::other(format!(
1849 "systemctl show {slice} failed: {}",
1850 String::from_utf8_lossy(&output.stderr).trim()
1851 )));
1852 }
1853 let value = String::from_utf8_lossy(&output.stdout);
1854 parse_memory_max(value.trim()).ok_or_else(|| {
1855 std::io::Error::other(format!(
1856 "systemctl show {slice}: unexpected MemoryMax {value:?}"
1857 ))
1858 })
1859}
1860
1861pub fn cpu_budget(millicores: u64, overcommit: f64) -> u64 {
1863 #[expect(
1864 clippy::cast_precision_loss,
1865 clippy::cast_possible_truncation,
1866 clippy::cast_sign_loss,
1867 reason = "a host's millicores are far below 2^52, and the config check holds the factor above 0 and at \
1868 most 16"
1869 )]
1870 let budget = (millicores as f64 * overcommit.max(0.0)).round() as u64;
1871 budget
1872}
1873
1874pub fn slice_cpus(slice: &str) -> std::io::Result<u64> {
1877 let output = Command::new("systemctl")
1878 .args([
1879 "show",
1880 slice,
1881 "--property=EffectiveCPUs",
1882 "--property=AllowedCPUs",
1883 "--property=CPUQuotaPerSecUSec",
1884 ])
1885 .output()?;
1886 if !output.status.success() {
1887 return Err(std::io::Error::other(format!(
1888 "systemctl show {slice} failed: {}",
1889 String::from_utf8_lossy(&output.stderr).trim()
1890 )));
1891 }
1892 let host = std::thread::available_parallelism().map_or(1, std::num::NonZero::get) as u64;
1893 parse_slice_cpus(&String::from_utf8_lossy(&output.stdout), host).ok_or_else(|| {
1894 std::io::Error::other(format!(
1895 "systemctl show {slice}: unexpected CPU properties {:?}",
1896 String::from_utf8_lossy(&output.stdout).trim()
1897 ))
1898 })
1899}
1900
1901pub fn parse_slice_cpus(properties: &str, host: u64) -> Option<u64> {
1904 let value = |name: &str| {
1905 properties.lines().find_map(|line| {
1906 line.strip_prefix(name)
1907 .and_then(|rest| rest.strip_prefix('='))
1908 .map(str::trim)
1909 })
1910 };
1911 let set = [value("EffectiveCPUs"), value("AllowedCPUs")]
1912 .into_iter()
1913 .flatten()
1914 .find(|cpus| !cpus.is_empty());
1915 let cpus = match set {
1916 Some(set) => cpu_count(set)?,
1917 None => host,
1918 };
1919 let quota = match value("CPUQuotaPerSecUSec") {
1920 None | Some("" | "infinity") => u64::MAX,
1921 Some(quota) => quota_millicores(quota)?,
1922 };
1923 Some(cpus.saturating_mul(1000).min(quota))
1924}
1925
1926fn cpu_count(set: &str) -> Option<u64> {
1928 set.split([',', ' '])
1929 .filter(|range| !range.is_empty())
1930 .try_fold(0u64, |count, range| {
1931 let (first, last) = range.split_once('-').unwrap_or((range, range));
1932 let (first, last): (u64, u64) = (first.parse().ok()?, last.parse().ok()?);
1933 Some(count + last.checked_sub(first)? + 1)
1934 })
1935}
1936
1937fn quota_millicores(quota: &str) -> Option<u64> {
1940 const UNITS: [(&str, f64); 10] = [
1942 ("y", 31_557_600_000.0),
1943 ("M", 2_629_800_000.0),
1944 ("w", 604_800_000.0),
1945 ("d", 86_400_000.0),
1946 ("h", 3_600_000.0),
1947 ("min", 60_000.0),
1948 ("s", 1000.0),
1949 ("ms", 1.0),
1950 ("us", 0.001),
1951 ("µs", 0.001),
1952 ];
1953 let millicores = quota.split_whitespace().try_fold(0.0, |total, part| {
1954 let at = part.find(|c: char| !(c.is_ascii_digit() || c == '.'))?;
1955 let (number, unit) = part.split_at(at);
1956 let per_unit = UNITS
1957 .iter()
1958 .find_map(|(name, per_unit)| (*name == unit).then_some(*per_unit))?;
1959 Some(total + number.parse::<f64>().ok()? * per_unit)
1960 })?;
1961 #[allow(
1962 clippy::cast_possible_truncation,
1963 clippy::cast_sign_loss,
1964 reason = "a quota is a few thousand millicores per CPU, checked finite and not negative first"
1965 )]
1966 (!quota.trim().is_empty() && millicores.is_finite() && millicores >= 0.0)
1967 .then(|| millicores.round() as u64)
1968}
1969
1970pub fn parse_memory_max(value: &str) -> Option<Option<ByteSize>> {
1972 if value == "infinity" {
1973 Some(None)
1974 } else {
1975 value.parse().ok().map(|bytes| Some(ByteSize(bytes)))
1976 }
1977}
1978
1979#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1981pub struct ByteSize(pub u64);
1982
1983const UNITS: [(char, u64); 4] = [
1984 ('T', 1 << 40),
1985 ('G', 1 << 30),
1986 ('M', 1 << 20),
1987 ('K', 1 << 10),
1988];
1989
1990impl FromStr for ByteSize {
1991 type Err = String;
1992
1993 #[allow(
1994 clippy::cast_precision_loss,
1995 clippy::cast_possible_truncation,
1996 clippy::cast_sign_loss,
1997 reason = "sizes in a config are far below 2^52 bytes, and the result is checked against u64's range first"
1998 )]
1999 fn from_str(s: &str) -> Result<Self, Self::Err> {
2000 let invalid = || {
2001 format!(
2002 "invalid size {s:?}, expected a number and a unit (K, M, G or T), such as \"9G\""
2003 )
2004 };
2005 let unit = s.chars().last().ok_or_else(invalid)?.to_ascii_uppercase();
2006 let (_, factor) = UNITS.iter().find(|(u, _)| *u == unit).ok_or_else(invalid)?;
2007 let number = &s[..s.len() - 1];
2008 if number.is_empty() || !number.chars().all(|c| c.is_ascii_digit() || c == '.') {
2009 return Err(invalid());
2010 }
2011 let number: f64 = number.parse().map_err(|_| invalid())?;
2012 let bytes = (number * *factor as f64).round();
2013 if bytes >= u64::MAX as f64 {
2015 return Err(format!("size {s:?} is too large"));
2016 }
2017 Ok(ByteSize(bytes as u64))
2018 }
2019}
2020
2021impl fmt::Display for ByteSize {
2022 #[allow(
2023 clippy::cast_precision_loss,
2024 reason = "shown with two decimals, far coarser than f64's precision"
2025 )]
2026 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2027 let Some((unit, factor)) = UNITS.iter().find(|(_, factor)| self.0 >= *factor) else {
2028 return write!(f, "{}B", self.0);
2029 };
2030 let number = format!("{:.2}", self.0 as f64 / *factor as f64);
2031 let number = number.trim_end_matches('0').trim_end_matches('.');
2032 write!(f, "{number}{unit}")
2033 }
2034}
2035
2036impl ByteSize {
2037 #[allow(
2041 clippy::cast_precision_loss,
2042 reason = "a unit is a power of two, so the quotient is exact below 2^53 bytes; the parse checks it"
2043 )]
2044 pub fn exact(self) -> String {
2045 let reads_back = |text: &String| text.parse::<ByteSize>() == Ok(self);
2046 let shown = self.to_string();
2047 if reads_back(&shown) {
2048 return shown;
2049 }
2050 let mut shortest: Option<String> = None;
2051 for (unit, factor) in UNITS {
2052 let text = format!("{}{unit}", self.0 as f64 / factor as f64);
2053 if reads_back(&text) && shortest.as_ref().is_none_or(|best| text.len() < best.len()) {
2054 shortest = Some(text);
2055 }
2056 }
2057 shortest.unwrap_or(shown)
2058 }
2059
2060 #[allow(
2064 clippy::cast_precision_loss,
2065 reason = "shown with one decimal at most, far coarser than f64's precision"
2066 )]
2067 pub fn short(self) -> String {
2068 let Some(index) = UNITS.iter().position(|(_, factor)| self.0 >= *factor) else {
2069 return format!("{}B", self.0);
2070 };
2071 let (mut unit, factor) = UNITS[index];
2072 let mut value = self.0 as f64 / factor as f64;
2073 if value.round() >= 1024.0
2074 && let Some(larger) = index.checked_sub(1)
2075 {
2076 unit = UNITS[larger].0;
2077 value /= 1024.0;
2078 }
2079 let tenths = (value * 10.0).round() / 10.0;
2081 if tenths < 10.0 {
2082 let number = format!("{tenths:.1}");
2083 format!("{}{unit}", number.trim_end_matches(".0"))
2084 } else {
2085 format!("{:.0}{unit}", value.round())
2086 }
2087 }
2088}
2089
2090impl<'de> Deserialize<'de> for ByteSize {
2091 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
2092 String::deserialize(deserializer)?
2093 .parse()
2094 .map_err(de::Error::custom)
2095 }
2096}
2097
2098pub fn parse_duration(s: &str) -> Result<Duration, String> {
2100 let invalid = || {
2101 format!(
2102 "invalid duration {s:?}, expected a number and s, m, h or d, such as \"168h\" or \"7d\""
2103 )
2104 };
2105 let (number, factor) = match s.chars().last() {
2106 Some('s') => (&s[..s.len() - 1], 1),
2107 Some('m') => (&s[..s.len() - 1], 60),
2108 Some('h') => (&s[..s.len() - 1], 3600),
2109 Some('d') => (&s[..s.len() - 1], 86400),
2110 _ => return Err(invalid()),
2111 };
2112 let number: u64 = number.parse().map_err(|_| invalid())?;
2113 number
2114 .checked_mul(factor)
2115 .map(Duration::from_secs)
2116 .ok_or_else(invalid)
2117}
2118
2119pub fn parse_age(text: &str) -> Result<Duration, String> {
2121 let error = || "expected a number and s, m, h, d or w, such as 12h".to_owned();
2122 let split = text.find(|c: char| !c.is_ascii_digit()).ok_or_else(error)?;
2123 let (number, unit) = text.split_at(split);
2124 let number: u64 = number.parse().map_err(|_| error())?;
2125 let unit = match unit {
2126 "s" => 1,
2127 "m" => 60,
2128 "h" => 3600,
2129 "d" => 86_400,
2130 "w" => 7 * 86_400,
2131 _ => return Err(error()),
2132 };
2133 Ok(Duration::from_secs(number.saturating_mul(unit)))
2134}
2135
2136fn duration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
2137 parse_duration(&String::deserialize(deserializer)?).map_err(de::Error::custom)
2138}
2139
2140#[cfg(test)]
2141mod tests;