Skip to main content

max / makenotwork

3.5 KB · 85 lines History Blame Raw
1 use anyhow::{Context, Result};
2 use serde::Deserialize;
3 use std::path::PathBuf;
4
5 #[derive(Debug, Clone, Deserialize)]
6 pub struct Config {
7 pub listen: String,
8 pub db_path: PathBuf,
9 pub topology_path: PathBuf,
10 /// MM-local checkout scratch dir (per-sha worktrees live here).
11 pub workdir: PathBuf,
12 /// MM-local releases dir (`releases/<version>/` and `current` live here).
13 pub release_root: PathBuf,
14 /// Scratch postgres DB url used by `migration_dry_run`. Sando drops and
15 /// recreates the schema on every run, so do not point this at anything
16 /// you care about.
17 #[serde(default)]
18 pub scratch_db_url: Option<String>,
19 /// Names of cargo bin targets the server crate produces (files under
20 /// `target/release/`). First entry is the primary unit (referenced from
21 /// the systemd unit's ExecStart). Defaults to `["server"]`; MNW ships
22 /// `["makenotwork", "mnw-admin"]`.
23 #[serde(default = "default_bin_names")]
24 pub bin_names: Vec<String>,
25 /// Root for per-gate run logs (`<logs_root>/<version>/<gate>.log`).
26 /// Served via `GET /logs/{version}/{gate}`. Defaults to `/srv/sando/logs`.
27 #[serde(default = "default_logs_root")]
28 pub logs_root: PathBuf,
29 /// Non-binary contents to stage into each release dir alongside
30 /// `bin_names`. Each entry copies `worktree/<src>` into
31 /// `<release>/<dst>`. `required=false` makes a missing source a warn
32 /// (older shas missing one of these don't break sando mid-bisect);
33 /// `required=true` errors. Default is empty — projects opt-in via
34 /// daemon config so the sando code stays project-agnostic.
35 #[serde(default)]
36 pub release_contents: Vec<ReleaseEntry>,
37 }
38
39 /// A directory or file copied from the worktree into the staged release dir.
40 /// Multiple entries with the same `dst` are allowed and merged (used by MNW
41 /// to build `docs/` from three different worktree sources).
42 #[derive(Debug, Clone, Deserialize)]
43 pub struct ReleaseEntry {
44 /// Path relative to the worktree root (e.g. `server/static`).
45 pub src: PathBuf,
46 /// Path relative to the release dir (e.g. `static`). Parent dirs are
47 /// created as needed.
48 pub dst: PathBuf,
49 /// If true, a missing source aborts the build. If false, log warn + skip.
50 #[serde(default)]
51 pub required: bool,
52 }
53
54 fn default_bin_names() -> Vec<String> { vec!["server".into()] }
55 fn default_logs_root() -> PathBuf { PathBuf::from("/srv/sando/logs") }
56
57 impl Config {
58 /// Primary binary — the one the systemd unit's ExecStart points at.
59 pub fn primary_bin(&self) -> &str {
60 self.bin_names.first().map(|s| s.as_str()).unwrap_or("server")
61 }
62
63 pub fn load() -> Result<Self> {
64 let path = std::env::var("SANDO_CONFIG").unwrap_or_else(|_| "sando-daemon.toml".into());
65 let raw = std::fs::read_to_string(&path)
66 .with_context(|| format!("reading daemon config at {path}"))?;
67 Ok(toml::from_str(&raw)?)
68 }
69
70 #[cfg(test)]
71 pub fn for_tests() -> Self {
72 Self {
73 listen: "127.0.0.1:0".into(),
74 db_path: PathBuf::from(":memory:"),
75 topology_path: PathBuf::from("/tmp/sando-test-topology.toml"),
76 workdir: PathBuf::from("/tmp/sando-test-workdir"),
77 release_root: PathBuf::from("/tmp/sando-test-release-root"),
78 scratch_db_url: None,
79 bin_names: vec!["server".into()],
80 logs_root: PathBuf::from("/tmp/sando-test-logs"),
81 release_contents: Vec::new(),
82 }
83 }
84 }
85