//! Release/ISO automation foundations. //! //! A biweekly release should not require a small VPS to rebuild the Gentoo //! world. The model is: take a known-good installer-base ISO, overlay the //! Phoebus installer + configs + pinned OpenCode, verify checksums, smoke test, //! then publish atomically. This module produces and verifies the manifest that //! makes that reproducible. //! //! Actual ISO assembly needs `xorriso`; this module emits the intended command //! and refuses to publish without a passing verification. use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Artifact { pub name: String, pub sha256: String, pub size_bytes: u64, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct ReleaseManifest { pub version: String, pub created_unix: u64, pub installer_base: String, pub stage3: String, pub opencode_version: String, pub artifacts: Vec, } impl Default for ReleaseManifest { fn default() -> Self { Self { version: env!("CARGO_PKG_VERSION").to_string(), created_unix: 0, installer_base: String::new(), stage3: String::new(), opencode_version: String::new(), artifacts: Vec::new(), } } } pub fn sha256_file(path: &Path) -> std::io::Result { let mut file = std::fs::File::open(path)?; let mut hasher = Sha256::new(); std::io::copy(&mut file, &mut hasher)?; Ok(format!("{:x}", hasher.finalize())) } pub fn artifact_for(path: &Path) -> std::io::Result { let meta = std::fs::metadata(path)?; Ok(Artifact { name: path .file_name() .map(|s| s.to_string_lossy().to_string()) .unwrap_or_default(), sha256: sha256_file(path)?, size_bytes: meta.len(), }) } impl ReleaseManifest { pub fn save(&self, path: &Path) -> std::io::Result<()> { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; } std::fs::write(path, serde_json::to_string_pretty(self).unwrap()) } pub fn load(path: &Path) -> std::io::Result { let text = std::fs::read_to_string(path)?; serde_json::from_str(&text) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } /// Verify a candidate artifact against the manifest. pub fn verify(&self, name: &str, path: &Path) -> bool { match self.artifacts.iter().find(|a| a.name == name) { Some(expected) => sha256_file(path) .map(|got| got == expected.sha256) .unwrap_or(false), None => false, } } /// The xorriso command that would assemble the candidate ISO. /// /// The base ISO is remastered with the Phoebus payload; nothing in the base /// system is rebuilt. pub fn assemble_command(base_iso: &Path, payload_dir: &Path, output: &Path) -> Vec { vec![ "xorriso".into(), "-indev".into(), base_iso.to_string_lossy().to_string(), "-outdev".into(), output.to_string_lossy().to_string(), "-map".into(), payload_dir.to_string_lossy().to_string(), "/phoebus".into(), "-boot_image".into(), "any".into(), "keep".into(), ] } } pub fn default_manifest_path(dir: &Path) -> PathBuf { dir.join("phoebus-release.json") } #[cfg(test)] mod tests { use super::*; #[test] fn sha256_of_known_bytes() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("f"); std::fs::write(&path, b"abc").unwrap(); // SHA-256("abc") assert_eq!( sha256_file(&path).unwrap(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" ); } #[test] fn manifest_roundtrip_and_verify() { let dir = tempfile::tempdir().unwrap(); let iso = dir.path().join("phoebus.iso"); std::fs::write(&iso, b"iso-bytes").unwrap(); let mut manifest = ReleaseManifest::default(); manifest.artifacts.push(artifact_for(&iso).unwrap()); manifest.installer_base = "installer-base-2026.iso".into(); let path = default_manifest_path(dir.path()); manifest.save(&path).unwrap(); let loaded = ReleaseManifest::load(&path).unwrap(); assert_eq!(loaded, manifest); assert!(loaded.verify("phoebus.iso", &iso)); assert!(!loaded.verify("missing.iso", &iso)); } #[test] fn assemble_command_targets_payload() { let cmd = ReleaseManifest::assemble_command( Path::new("/base.iso"), Path::new("/payload"), Path::new("/out.iso"), ); assert_eq!(cmd[0], "xorriso"); assert!(cmd.iter().any(|a| a == "/phoebus")); assert!(cmd.iter().any(|a| a == "/base.iso")); } }