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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Starting the actor task.
//!
//! [`spawn_supervisor`] is the ordinary entry point and starts with no dog
//! defaults: `boot` reads them.
//! `SupervisorBuilder` is the long form the daemon's boot path uses when it
//! has to override the clock, the runner or the paths, as the tests do.
use super::*;
/// Builds a supervisor actor.
#[derive(Debug)]
pub(crate) struct SupervisorBuilder<R: ProcessRunner> {
pub(super) runner: R,
pub(super) paths: ShepPaths,
pub(super) events: Bus,
pub(super) extras: Option<Extras>,
pub(super) environment: String,
pub(super) provider_secrets: Option<Arc<ProviderSecrets>>,
pub(super) dog_defaults: DogDefaults,
/// The `listen` sockets a predecessor carried, one set per sheep.
#[cfg(unix)]
pub(super) adopted_listeners: Vec<AdoptedListeners>,
pub(super) dog_binaries: BTreeMap<String, String>,
}
impl<R: ProcessRunner> SupervisorBuilder<R> {
/// A builder with no lifecycle extras: the engine spawns, restarts and
/// kills, and nothing watches, schedules or probes. `events` receives
/// [`BusEvent::Process`] plus logs forwarded from each sheep.
pub(crate) fn new(runner: R, paths: ShepPaths, events: Bus) -> Self {
Self {
runner,
paths,
events,
extras: None,
environment: DEFAULT_ENVIRONMENT.to_string(),
provider_secrets: None,
dog_defaults: DogDefaults::default(),
#[cfg(unix)]
adopted_listeners: Vec::new(),
dog_binaries: BTreeMap::new(),
}
}
/// Wires in the lifecycle extras.
#[must_use]
pub(crate) fn extras(mut self, extras: Extras) -> Self {
self.extras = Some(extras);
self
}
/// The environment a sheep that names none of its own resolves its
/// secrets in, from `[daemon] environment`.
///
/// Left at [`DEFAULT_ENVIRONMENT`] when unset, which is what
/// `DaemonSection` itself defaults to, so a builder nobody told and a
/// file that says nothing agree.
#[must_use]
pub(crate) fn environment(mut self, environment: String) -> Self {
self.environment = environment;
self
}
/// The registry a provider dog pushes into, shared with the connection
/// tasks that serve `Request::PutSecrets`.
///
/// Left unset, the actor loads one of its own from
/// [`ShepPaths::secrets_cache`], which is what a cold boot with no dog
/// yet running resolves against anyway. `boot` passes the shared one
/// so a push reaches the next spawn.
#[must_use]
pub(crate) fn provider_secrets(mut self, secrets: Arc<ProviderSecrets>) -> Self {
self.provider_secrets = Some(secrets);
self
}
/// The operator's defaults for each dog, layered under every sheep's table.
///
/// Left unset, the actor starts with none. `boot` reads
/// [`ShepPaths::dog_defaults`] itself and refuses to start on a store it
/// cannot read, so no actor serves tables without defaults that are on
/// disk.
#[must_use]
pub(crate) fn dog_defaults(mut self, defaults: DogDefaults) -> Self {
self.dog_defaults = defaults;
self
}
/// The `listen` sockets a predecessor handed over, which the next spawn or
/// reload of each sheep passes on instead of binding.
///
/// Left empty, every sheep binds on its next start.
#[cfg(unix)]
#[must_use]
pub(crate) fn adopted_listeners(mut self, listeners: Vec<AdoptedListeners>) -> Self {
self.adopted_listeners = listeners;
self
}
/// Where each adopted dog's binary lives, by dog name.
///
/// A sheep with a `dogs.sandbox` table spawns through the `sandbox` entry
/// and is refused without one. Left empty, the actor knows no dog until a
/// `StartDog` hands it an adopted source. `boot` fills it from the dogs
/// `shep.toml` records, since the daemon never reads that file itself.
#[must_use]
pub(crate) fn dog_binaries(mut self, binaries: BTreeMap<String, String>) -> Self {
self.dog_binaries = binaries;
self
}
/// Spawns the actor.
///
/// Must be called from within a Tokio runtime context.
pub(crate) fn spawn(self) -> SupervisorHandle {
let (tx, rx) = mpsc::channel(MAILBOX_CAPACITY);
let actor = self.build(tx.clone());
tokio::spawn(actor.run(rx));
SupervisorHandle { tx }
}
/// Spawns the actor around a flock this image inherited, restoring
/// `counters` before any slot is installed. Every sheep in `flock` goes in
/// under the id, epoch, status and history the blob carried, around the
/// descriptors that crossed the `execve`. Nothing here spawns, signals or
/// reopens anything. `reloads` is the apps that were mid-swap, restored
/// once the flock is in. Must be called from within a Tokio runtime
/// context.
///
/// # Errors
///
/// - [`AdoptError::Spec`]: a carried config does not normalize.
/// - [`AdoptError::Runner`]: the runner refused the inherited handles.
#[cfg(unix)]
pub(crate) fn spawn_adopted(
self,
flock: Vec<AdoptedSheep>,
counters: Counters,
reloads: Vec<CarriedReload>,
) -> Result<SupervisorHandle, AdoptError> {
let (tx, rx) = mpsc::channel(MAILBOX_CAPACITY);
let mut actor = self.build(tx.clone());
// Counters before slots: a fresh sheep must not be handed an id a
// caller is still holding.
actor.next_id = counters.next_id;
actor.next_deadline = counters.next_deadline;
actor.next_action_stamp = counters.next_action_stamp;
// One reaper for the whole adopted flock: a status can be collected
// once, so two reapers racing on one pid would have one take the exit
// and the other meet `ECHILD`.
let reaper = Arc::new(AdoptedReaper::new());
for sheep in flock {
actor.install_adopted(sheep, &reaper)?;
}
// After the loop and before the actor runs: a job names two entries,
// so it has nowhere to go until every sheep is in, and the readiness
// waits `install_adopted` armed report into a mailbox nothing drains.
actor.install_carried_reloads(reloads);
tokio::spawn(actor.run(rx));
Ok(SupervisorHandle { tx })
}
/// The actor both spawn paths start from: no sheep, counters at zero.
fn build(self, tx: mpsc::Sender<Msg>) -> Actor<R> {
let provider_secrets = self
.provider_secrets
.unwrap_or_else(|| Arc::new(ProviderSecrets::load(&self.paths.secrets_cache)));
#[cfg(unix)]
let listeners = self
.adopted_listeners
.into_iter()
.map(|adopted| (adopted.sheep, Arc::new(adopted.sockets)))
.collect();
#[cfg(not(unix))]
let listeners = BTreeMap::new();
Actor {
runner: self.runner,
paths: self.paths,
events: self.events,
host_environment: self.environment,
dog_defaults: self.dog_defaults,
dog_binaries: self.dog_binaries,
provider_secrets,
tx,
sheep: HashMap::new(),
next_id: 0,
next_deadline: 0,
next_action_stamp: 0,
pending: Vec::new(),
shutting_down: false,
extras: self.extras,
registry: ExtrasRegistry::default(),
reloads: HashMap::new(),
smits: Smits::new(),
disabled: BTreeSet::new(),
listeners,
listeners_before: BTreeMap::new(),
}
}
}
/// Spawns the actor with no lifecycle extras and no dog defaults, which `boot`
/// reads: shorthand for `SupervisorBuilder::new(runner, paths, events).spawn()`.
///
/// Must be called from within a Tokio runtime context: it spawns the actor
/// task immediately.
pub fn spawn_supervisor<R: ProcessRunner>(
runner: R,
paths: ShepPaths,
events: Bus,
) -> SupervisorHandle {
SupervisorBuilder::new(runner, paths, events).spawn()
}