1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
//! 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<u8>) {
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<u8>), 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())
.unwrap()
}
/// A systemctl that writes down what it was asked.
pub fn systemctl(&self) -> PathBuf {
let log = self.root.join("systemctl.log");
PathBuf::from(self.program("systemctl", &format!("echo \"$*\" >> {}", log.display())))
}
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<Vec<u8>> {
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<u8>), dry_run: bool) -> Migration {
let store = self.store();
let _lock = store.lock().unwrap();
enstate::migrate::migrate(&schema.0, &store, dry_run, &self.systemctl()).unwrap()
}
/// The current snapshot's id of `channel`.
pub fn snapshot(&self, channel: &str) -> Option<String> {
self.store()
.current(channel)
.unwrap()
.map(|snapshot| snapshot.id)
}
}
|