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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! The host a library consumer drives: where releases, containers and
//! their state live, and which ZFS dataset backs them.
use crate::error::Result;
use crate::manifest::{self, BlackshipConfig};
use crate::zfs::ZfsManager;
use std::path::{Path, PathBuf};
use std::sync::Arc;
/// A FreeBSD host as Blackship sees it: a cheap, `Send + Sync` description
/// that touches nothing on disk until used.
///
/// ```no_run
/// use blackship::Host;
///
/// let host = Host::new("/var/db/kim/blackship").with_zfs("zroot", "kim/blackship");
/// assert!(host.uses_zfs());
/// ```
#[derive(Debug, Clone)]
pub struct Host {
layout: Arc<Layout>,
}
impl Host {
/// A host keeping roots, releases, downloads and state under `data_dir`.
/// Without [`with_zfs`](Self::with_zfs) every container is a full copy.
pub fn new(data_dir: impl Into<PathBuf>) -> Self {
let data_dir = data_dir.into();
let mut config = BlackshipConfig::default();
config.config.releases_dir = data_dir.join("releases");
config.config.cache_dir = data_dir.join("cache");
config.config.data_dir = data_dir;
Self::from_layout(Layout {
config,
scoped_state: true,
})
}
/// The host described by a `blackship.toml`, or defaults when it is missing.
///
/// # Errors
///
/// The manifest exists but cannot be read, parsed or validated.
pub fn from_manifest(path: &Path) -> Result<Self> {
Ok(Self::from_config(Some(manifest::load_or_default(path)?)))
}
/// Back releases and containers with datasets under `pool/dataset`;
/// containers become clones of a `@pristine` release snapshot.
#[must_use]
pub fn with_zfs(mut self, pool: impl Into<String>, dataset: impl Into<String>) -> Self {
let config = &mut self.layout_mut().config.config;
config.zfs_enabled = true;
config.zpool = Some(pool.into());
config.dataset = dataset.into();
self
}
/// Keep releases somewhere other than `data_dir/releases`.
#[must_use]
pub fn with_releases_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.layout_mut().config.config.releases_dir = dir.into();
self
}
/// Keep downloads somewhere other than `data_dir/cache`.
#[must_use]
pub fn with_cache_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.layout_mut().config.config.cache_dir = dir.into();
self
}
/// Fetch distribution sets from another FreeBSD mirror.
#[must_use]
pub fn with_mirror(mut self, url: impl Into<String>) -> Self {
self.layout_mut().config.config.mirror_url = url.into();
self
}
/// Where container roots and state live.
#[must_use]
pub fn data_dir(&self) -> &Path {
self.layout.data_dir()
}
/// Where releases live.
#[must_use]
pub fn releases_dir(&self) -> &Path {
self.layout.releases_dir()
}
/// Whether releases and containers are ZFS datasets.
#[must_use]
pub fn uses_zfs(&self) -> bool {
self.layout.zfs().is_some()
}
/// The CLI's view: a loaded manifest, or none at all.
pub(crate) fn from_config(config: Option<BlackshipConfig>) -> Self {
let scoped_state = config.is_some();
Self::from_layout(Layout {
config: config.unwrap_or_default(),
scoped_state,
})
}
pub(crate) const fn layout(&self) -> &Arc<Layout> {
&self.layout
}
fn from_layout(layout: Layout) -> Self {
Self {
layout: Arc::new(layout),
}
}
fn layout_mut(&mut self) -> &mut Layout {
Arc::make_mut(&mut self.layout)
}
}
/// Paths and datasets, resolved once per host.
#[derive(Debug, Clone)]
pub struct Layout {
config: BlackshipConfig,
/// Whether the network stores read this config or the CLI's default.
scoped_state: bool,
}
impl Layout {
pub(crate) const fn config(&self) -> &BlackshipConfig {
&self.config
}
/// The config handed to state stores (`NetworkLeaseStore::from_config` and friends).
pub(crate) fn state_config(&self) -> Option<&BlackshipConfig> {
self.scoped_state.then_some(&self.config)
}
pub(crate) fn data_dir(&self) -> &Path {
&self.config.config.data_dir
}
pub(crate) fn releases_dir(&self) -> &Path {
&self.config.config.releases_dir
}
/// Parent of every ephemeral container root.
pub(crate) fn containers_dir(&self) -> PathBuf {
self.data_dir().join("containers")
}
/// Where a container's root lives when it has no record saying otherwise.
pub(crate) fn container_root(&self, name: &str) -> PathBuf {
self.containers_dir().join(name)
}
/// Per-container ownership records (see `container::registry`).
pub(crate) fn registry_dir(&self) -> PathBuf {
self.data_dir().join("container-state")
}
/// The dataset manager, when ZFS is enabled and has a pool.
pub(crate) fn zfs(&self) -> Option<ZfsManager> {
let config = &self.config.config;
if !config.zfs_enabled {
return None;
}
config
.zpool
.as_ref()
.map(|pool| ZfsManager::new(pool, &config.dataset, config.jails_dir()))
}
}
#[cfg(test)]
mod tests;