diff options
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..44b110c --- /dev/null +++ b/src/config.rs @@ -0,0 +1,201 @@ +//! Filesystem layout and runtime configuration. +//! +//! Everything the server owns lives under one root directory so a test can point +//! the whole application at a `tempdir` and get an isolated instance. The heavy +//! bytes (packages, logs, mirrors) live on the filesystem and are referenced by +//! path from the DB (DESIGN.md §11a). + +use std::path::{Path, PathBuf}; + +use serde::{Deserialize, Serialize}; + +/// Runtime configuration. Persisted as `<root>/config.json`. +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(default)] +pub struct Config { + /// Root directory holding db, mirrors, caches, store, repo and logs. + pub root: PathBuf, + /// Name of the single pacman repo we publish (DESIGN.md §9, decision 4). + pub repo_name: String, + /// Architecture, fixed to x86_64 by decision 17. + pub arch: String, + /// GPG key id used to sign packages *and* the database (decision 22). + pub signing_key_id: String, + /// Address the HTTP UI binds to. Default is loopback only (§14). + pub bind: String, + /// Shared credential for the UI. Single user, one credential, no user table. + pub password: String, + /// Public base URL used in consumer-facing snippets. + pub base_url: String, + /// devtools build profile (§8.3). + pub build_profile: String, + /// Path to the git executable. + pub git_bin: String, + /// Path to the makepkg executable. + pub makepkg_bin: String, + /// Path to makechrootpkg. + pub makechrootpkg_bin: String, + /// Path to mkarchroot. + pub mkarchroot_bin: String, + /// Path to repo-add. + pub repo_add_bin: String, + /// Path to gpg. + pub gpg_bin: String, + /// Unprivileged user that runs PKGBUILD code (§14). Empty = current user. + pub build_user: String, + /// Directory prepended to PATH for child processes. The test harness puts + /// the toolchain shims here (§16a). + pub path_prepend: Option<PathBuf>, +} + +impl Default for Config { + fn default() -> Self { + Self { + root: PathBuf::from("/var/lib/aur-build"), + repo_name: "aur-build".into(), + arch: "x86_64".into(), + signing_key_id: String::new(), + bind: "127.0.0.1:8484".into(), + password: String::new(), + base_url: "http://127.0.0.1:8484".into(), + build_profile: "extra-x86_64".into(), + git_bin: "git".into(), + makepkg_bin: "makepkg".into(), + makechrootpkg_bin: "makechrootpkg".into(), + mkarchroot_bin: "mkarchroot".into(), + repo_add_bin: "repo-add".into(), + gpg_bin: "gpg".into(), + build_user: String::new(), + path_prepend: None, + } + } +} + +impl Config { + pub fn load(root: &Path) -> anyhow::Result<Self> { + let p = root.join("config.json"); + if !p.exists() { + anyhow::bail!("no config at {}; run `aur-build init`", p.display()); + } + let s = std::fs::read_to_string(&p)?; + let mut cfg: Config = serde_json::from_str(&s)?; + // root inside the config file is relative to the config's directory + // unless it is absolute; that keeps a moved tree usable. + if !cfg.root.is_absolute() { + cfg.root = root.join(&cfg.root); + } + Ok(cfg) + } + + pub fn save(&self) -> anyhow::Result<()> { + std::fs::create_dir_all(&self.root)?; + std::fs::write( + self.root.join("config.json"), + serde_json::to_string_pretty(self)? + "\n", + )?; + Ok(()) + } + + /// Create the directory tree the server needs. Idempotent. + pub fn ensure_dirs(&self) -> anyhow::Result<()> { + for d in [ + self.root.clone(), + self.mirrors_dir(), + self.vcs_cache_dir(), + self.source_cache_dir(), + self.staging_dir(), + self.store_dir(), + self.log_dir(), + self.repo_dir(), + self.chroot_dir(), + ] { + std::fs::create_dir_all(d)?; + } + std::fs::create_dir_all(self.repo_arch_dir())?; + Ok(()) + } + + pub fn db_path(&self) -> PathBuf { + self.root.join("aur-build.db") + } + /// Local AUR git mirrors, one clone per pkgbase (§5.1). Load-bearing, not a + /// cache: it is what makes historical releases rebuildable (§5.1a). + pub fn mirrors_dir(&self) -> PathBuf { + self.root.join("mirrors") + } + pub fn mirror_path(&self, pkgbase: &str) -> PathBuf { + self.mirrors_dir().join(format!("{pkgbase}.git")) + } + /// Persistent upstream VCS clones (§5.2 cost control). + pub fn vcs_cache_dir(&self) -> PathBuf { + self.root.join("vcs_cache") + } + pub fn vcs_cache_path(&self, url: &str) -> PathBuf { + let h = crate::hash::short(url.as_bytes(), 16); + self.vcs_cache_dir().join(format!("{}-{}.git", slug(url), h)) + } + /// Content-addressed download cache reused by the build (§5.3). + pub fn source_cache_dir(&self) -> PathBuf { + self.root.join("source_cache") + } + pub fn source_cache_path(&self, digest: &str) -> PathBuf { + self.source_cache_dir().join(&digest[..2.min(digest.len())]).join(digest) + } + /// Disposable per-build workspace (§8.2). + pub fn staging_dir(&self) -> PathBuf { + self.root.join("staging") + } + pub fn staging_build_dir(&self, build_id: &str) -> PathBuf { + self.staging_dir().join(build_id) + } + /// Content-addressed artifact store. + pub fn store_dir(&self) -> PathBuf { + self.root.join("store") + } + pub fn store_path(&self, digest: &str) -> PathBuf { + self.store_dir().join(&digest[..2.min(digest.len())]).join(digest) + } + pub fn log_dir(&self) -> PathBuf { + self.root.join("logs") + } + pub fn build_log_path(&self, build_id: &str) -> PathBuf { + self.log_dir().join(format!("{build_id}.log")) + } + /// The served repo tree: `<repo>/<name>/os/<arch>/` (§9). + pub fn repo_dir(&self) -> PathBuf { + self.root.join("repo") + } + pub fn repo_arch_dir(&self) -> PathBuf { + self.repo_dir() + .join(&self.repo_name) + .join("os") + .join(&self.arch) + } + pub fn repo_db_name(&self) -> String { + format!("{}.db", self.repo_name) + } + /// Warm chroot seeds, one per profile (§8.3). + pub fn chroot_dir(&self) -> PathBuf { + self.root.join("chroot") + } + pub fn chroot_seed(&self, profile: &str) -> PathBuf { + self.chroot_dir().join(profile) + } +} + +/// Filesystem-safe slug for a URL, used in cache filenames. +pub fn slug(url: &str) -> String { + url.chars() + .map(|c| { + if c.is_ascii_alphanumeric() { + c.to_ascii_lowercase() + } else { + '-' + } + }) + .collect::<String>() + .trim_matches('-') + .chars() + .take(32) + .collect() +} |
