//! A host on directories, for the tests: its store, programs as shell scripts, //! schemas, submissions, and what its views and reactions are. #![allow(dead_code)] use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use enstate::migrate::Migration; use enstate::plan::{self, Plan}; use enstate::schema::Schema; use enstate::store::Store; use enstate::submission::Submission; use serde_json::{Value, json}; pub struct Host { pub root: PathBuf, } impl Host { pub fn new(name: &str) -> Self { let root = Path::new(env!("CARGO_TARGET_TMPDIR")) .join("receptor") .join(name); let _ = std::fs::remove_dir_all(&root); std::fs::create_dir_all(root.join("programs")).unwrap(); Self { root } } pub fn store(&self) -> Store { Store::new(&self.root.join("state"), &self.root.join("run")) } /// A shell script, with the tests' PATH (programs get an empty environment). pub fn program(&self, name: &str, body: &str) -> String { let path = self.root.join("programs").join(name); let script = format!( "#!/bin/sh\nPATH={}\nset -eu\n{body}\n", std::env::var("PATH").unwrap() ); std::fs::write(&path, script).unwrap(); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); path.display().to_string() } pub fn schema(&self, schema: Value) -> (Schema, Vec) { let bytes = serde_json::to_vec_pretty(&schema).unwrap(); std::fs::write(self.root.join("schema.json"), &bytes).unwrap(); (Schema::from_slice(&bytes).unwrap(), bytes) } /// A submission named `name`: its manifest, and files below `channels/`. pub fn submission(&self, name: &str, manifest: Value, files: &[(&str, &[u8])]) -> PathBuf { let dir = self.root.join("submissions").join(name); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); let mut manifest = manifest; manifest["submission"] = json!(1); std::fs::write(dir.join("manifest.json"), manifest.to_string()).unwrap(); for (path, bytes) in files { let path = dir.join("channels").join(path); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::write(path, bytes).unwrap(); } dir } pub fn plan(&self, schema: &Schema, dir: &Path) -> Plan { let submission = Submission::load(dir).unwrap(); plan::plan(schema, &self.store(), &submission).unwrap().plan } pub fn apply(&self, schema: &(Schema, Vec), dir: &Path) -> Plan { let submission = Submission::load(dir).unwrap(); let store = self.store(); let lock = store.lock().unwrap(); plan::plan(&schema.0, &store, &submission) .unwrap() .apply(&store, &schema.1, &self.systemctl(), lock) .unwrap() } /// A systemctl that writes down what it was asked, and whether the store's lock /// was free then. pub fn systemctl(&self) -> PathBuf { let log = self.root.join("systemctl.log"); let lock = self.root.join("state/lock"); let locks = self.root.join("locks.log"); PathBuf::from(self.program( "systemctl", &format!( "echo \"$*\" >> {log}\nif flock -n {lock} true; then echo free; else echo held; fi >> {locks}", log = log.display(), lock = lock.display(), locks = locks.display(), ), )) } /// Whether the lock was free each time systemctl ran. pub fn locks(&self) -> String { std::fs::read_to_string(self.root.join("locks.log")).unwrap_or_default() } pub fn reactions(&self) -> String { std::fs::read_to_string(self.root.join("systemctl.log")).unwrap_or_default() } pub fn view(&self, view: &str, file: &str) -> Option> { std::fs::read(self.root.join("run/views").join(view).join(file)).ok() } pub fn mode(&self, view: &str, file: &str) -> u32 { std::fs::metadata(self.root.join("run/views").join(view).join(file)) .unwrap() .permissions() .mode() & 0o777 } } impl Host { /// Serves the generation of `schema` from the snapshots, or says what that would do. pub fn migrate(&self, schema: &(Schema, Vec), dry_run: bool) -> Migration { let store = self.store(); let _lock = store.lock().unwrap(); enstate::migrate::migrate(&schema.0, &schema.1, &store, dry_run, &self.systemctl()).unwrap() } /// Stages a submission for the generation of `schema`, before switching to it. pub fn stage(&self, schema: &(Schema, Vec), dir: &Path) -> Plan { let submission = Submission::load(dir).unwrap(); let store = self.store(); let lock = store.lock().unwrap(); plan::plan(&schema.0, &store, &submission) .unwrap() .stage(&store, &schema.1, lock) .unwrap() } /// The current snapshot's id of `channel`. pub fn snapshot(&self, channel: &str) -> Option { self.store() .current(channel) .unwrap() .map(|snapshot| snapshot.id) } }