mind-control: swarm brake hold stops every Mind's units, through the one stopper
stop_all becomes SystemdLauncher::stop(StopScope): a Mind's two exact units, or the swarm's mind-self@*.service and mind-persona@*.service globs. Both holds write first, then stop whatever the write's outcome, with the same four outcomes. Soul s1 swarm-hold-leaves-live-turn-running: a held swarm brake left a live Persona turn running with its shell until the turn ended. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
14e7838813
commit
edfbf9ce25
5 changed files with 184 additions and 68 deletions
|
|
@ -8,18 +8,19 @@
|
|||
//! mind-control --instance <slug> brake release
|
||||
//! mind-control [--root <dir>] --instance <slug> check
|
||||
//! mind-control [--root <dir>] swarm show
|
||||
//! mind-control swarm brake hold | release
|
||||
//! mind-control swarm brake hold | release (hold also stops every Mind's units)
|
||||
//! ```
|
||||
//!
|
||||
//! The `swarm` verbs write `<root>/swarm.cc` and stop no unit: the swarm's
|
||||
//! brake is read by the organs that act on the swarm's behalf.
|
||||
//! The `swarm` verbs write `<root>/swarm.cc`; `swarm brake hold` also stops
|
||||
//! every Mind's units, and `swarm brake release` stops nothing.
|
||||
//!
|
||||
//! `check` exits 0 only when the instance brake and the swarm brake are released; a
|
||||
//! systemd unit runs it as its `ExecCondition`. It writes nothing.
|
||||
//!
|
||||
//! `brake hold` writes the brake held and then, whatever the write's outcome,
|
||||
//! stops the instance's running units through the `stop_units` parameter. It
|
||||
//! fails if either step failed. No other command stops anything.
|
||||
//! Either `brake hold` writes its brake held and then, whatever the write's
|
||||
//! outcome, stops the units it covers through the one `stop_units` parameter
|
||||
//! (a Mind's two units, or every Mind's). It fails if either step failed. No
|
||||
//! other command stops anything.
|
||||
|
||||
use std::io::Write;
|
||||
use std::net::SocketAddr;
|
||||
|
|
@ -32,7 +33,7 @@ use eureka_pipeline::{RunTurn, Slug};
|
|||
use crate::control::{
|
||||
ControlWriter, Effective, FileSource, SwarmWriter, control_path, derive, derive_swarm, load_state, load_swarm_state, swarm_path,
|
||||
};
|
||||
use crate::launch::unit;
|
||||
use crate::launch::{StopScope, unit};
|
||||
|
||||
const USAGE: &str =
|
||||
"usage: mind-control [--root <dir>] --instance <slug> show | check | brake hold | brake release | mind-control [--root <dir>] swarm show | brake hold | brake release";
|
||||
|
|
@ -77,14 +78,14 @@ pub fn operator_name(sudo_user: Option<String>, user: Option<String>) -> String
|
|||
/// default; a leading `--root <dir>` argument replaces it (argv, not environment,
|
||||
/// because `sudo -E` carries the environment). The operator's name, the clock, the output and the stopper are parameters so a test drives
|
||||
/// the whole command without touching `/etc` or systemd. `stop_units` is
|
||||
/// called only by `brake hold`, after the write, whatever the write's outcome.
|
||||
/// called only by a `brake hold`, after the write, whatever the write's outcome.
|
||||
pub fn run(
|
||||
args: &[String],
|
||||
root: &Path,
|
||||
set_by: &str,
|
||||
now: DateTime<Utc>,
|
||||
out: &mut impl Write,
|
||||
stop_units: &mut dyn FnMut(&Slug) -> Result<()>,
|
||||
stop_units: &mut dyn FnMut(StopScope<'_>) -> Result<()>,
|
||||
) -> Result<()> {
|
||||
let (root, args) = match args {
|
||||
[flag, dir, rest @ ..] if flag == "--root" => (Path::new(dir), rest),
|
||||
|
|
@ -93,7 +94,7 @@ pub fn run(
|
|||
if let [word, rest @ ..] = args
|
||||
&& word == "swarm"
|
||||
{
|
||||
return swarm(root, rest, set_by, now, out);
|
||||
return swarm(root, rest, set_by, now, out, stop_units);
|
||||
}
|
||||
let (instance, command) = match args {
|
||||
[flag, instance, command @ ..] if flag == "--instance" => (Slug(instance.clone()), command),
|
||||
|
|
@ -106,15 +107,13 @@ pub fn run(
|
|||
["check"] => check(root, &path, &instance, out),
|
||||
["brake", "hold"] => {
|
||||
let written = ControlWriter::open(&path).and_then(|mut writer| writer.set_brake(false, now, set_by));
|
||||
let stopped = stop_units(&instance);
|
||||
let stopped = stop_units(StopScope::Mind(&instance));
|
||||
match (written, stopped) {
|
||||
(Ok(()), Ok(())) => {
|
||||
writeln!(out, "units: stopped {} {}", unit(&instance, RunTurn::SelfRun)?, unit(&instance, RunTurn::PersonaTurn)?)?;
|
||||
Ok(())
|
||||
}
|
||||
(Err(write), Ok(())) => Err(write.context("the brake was not written; the units were stopped")),
|
||||
(Ok(()), Err(stop)) => Err(stop.context("the brake is held; the stop failed")),
|
||||
(Err(write), Err(stop)) => Err(anyhow!("the brake was not written ({write:#}) and the stop failed ({stop:#})")),
|
||||
outcome => held(outcome),
|
||||
}
|
||||
}
|
||||
["brake", "release"] => ControlWriter::open(&path)?.set_brake(true, now, set_by),
|
||||
|
|
@ -122,14 +121,40 @@ pub fn run(
|
|||
}
|
||||
}
|
||||
|
||||
/// A hold whose write or stop failed: what was written, what was stopped, and
|
||||
/// which step failed, the same words for a Mind's hold and the swarm's.
|
||||
fn held(outcome: (Result<()>, Result<()>)) -> Result<()> {
|
||||
match outcome {
|
||||
(Ok(()), Ok(())) => Ok(()),
|
||||
(Err(write), Ok(())) => Err(write.context("the brake was not written; the units were stopped")),
|
||||
(Ok(()), Err(stop)) => Err(stop.context("the brake is held; the stop failed")),
|
||||
(Err(write), Err(stop)) => Err(anyhow!("the brake was not written ({write:#}) and the stop failed ({stop:#})")),
|
||||
}
|
||||
}
|
||||
|
||||
/// The `swarm` verbs over `<root>/swarm.cc`. Every refusal happens before the
|
||||
/// writer is opened, so a refused command creates nothing.
|
||||
fn swarm(root: &Path, args: &[String], set_by: &str, now: DateTime<Utc>, out: &mut impl Write) -> Result<()> {
|
||||
/// writer is opened, so a refused command creates nothing. `brake hold` then
|
||||
/// stops every Mind's units, whatever the write's outcome.
|
||||
fn swarm(
|
||||
root: &Path,
|
||||
args: &[String],
|
||||
set_by: &str,
|
||||
now: DateTime<Utc>,
|
||||
out: &mut impl Write,
|
||||
stop_units: &mut dyn FnMut(StopScope<'_>) -> Result<()>,
|
||||
) -> Result<()> {
|
||||
let path = swarm_path(root);
|
||||
let words: Vec<&str> = args.iter().map(String::as_str).collect();
|
||||
match words.as_slice() {
|
||||
["show"] => show_swarm(&path, out),
|
||||
["brake", "hold"] => SwarmWriter::open(&path)?.set_brake(false, now, set_by),
|
||||
["brake", "hold"] => {
|
||||
let written = SwarmWriter::open(&path).and_then(|mut writer| writer.set_brake(false, now, set_by));
|
||||
let stopped = stop_units(StopScope::Swarm);
|
||||
if written.is_ok() && stopped.is_ok() {
|
||||
writeln!(out, "units: stopped every mind-self@ and mind-persona@ unit")?;
|
||||
}
|
||||
held((written, stopped))
|
||||
}
|
||||
["brake", "release"] => SwarmWriter::open(&path)?.set_brake(true, now, set_by),
|
||||
_ => bail!("{USAGE}"),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@
|
|||
//! Persona turn's run opens with no claims, so a request that carries any is
|
||||
//! refused. The primitive
|
||||
//! owns no file and stops no unit through the `Launcher` port: stopping her
|
||||
//! units is the operator's brake hold, through `SystemdLauncher::stop_all`.
|
||||
//! units is either brake's hold (one Mind's, or the swarm's for every Mind),
|
||||
//! through `SystemdLauncher::stop`.
|
||||
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
|
|
@ -125,45 +126,63 @@ pub(crate) fn unit(instance: &Slug, turn: RunTurn) -> Result<String> {
|
|||
Ok(format!("{}@{}.service", template(turn), instance.0))
|
||||
}
|
||||
|
||||
/// The arguments of the one synchronous stop: the exact unit `unit()` names
|
||||
/// for each turn of this instance, the same names start and alive use. No
|
||||
/// glob: a prefix glob also matches another instance (`eureka*` matches
|
||||
/// `eureka-two`) and a glob that matches no loaded unit exits 0. No
|
||||
/// What one stop reaches: the two units of one Mind, or every Mind's.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum StopScope<'a> {
|
||||
Mind(&'a Slug),
|
||||
Swarm,
|
||||
}
|
||||
|
||||
/// The arguments of the one synchronous stop. A Mind's: the exact unit `unit()`
|
||||
/// names for each turn of this instance, the same names start and alive use,
|
||||
/// with no glob (a prefix glob also matches another instance, `eureka*` matches
|
||||
/// `eureka-two`). The swarm's: each template's glob over the loaded units,
|
||||
/// which is exactly every running Mind and exits 0 when none is loaded. No
|
||||
/// `--no-block`: `systemctl stop` returns when the units are down. The
|
||||
/// instance is validated by `unit()`.
|
||||
pub(crate) fn stop_args(instance: &Slug) -> Result<Vec<String>> {
|
||||
pub(crate) fn stop_args(scope: StopScope<'_>) -> Result<Vec<String>> {
|
||||
let mut args = vec!["stop".to_string(), "--".to_string()];
|
||||
for turn in [RunTurn::SelfRun, RunTurn::PersonaTurn] {
|
||||
args.push(unit(instance, turn)?);
|
||||
args.push(match scope {
|
||||
StopScope::Mind(instance) => unit(instance, turn)?,
|
||||
StopScope::Swarm => format!("{}@*.service", template(turn)),
|
||||
});
|
||||
}
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
impl SystemdLauncher {
|
||||
/// Stops every unit of both turns of one instance and waits for them. An
|
||||
/// inherent method, not on `Launcher`: only the operator's brake hold
|
||||
/// calls it. A unit that is not installed is systemctl's exit 5, and the
|
||||
/// others still stop; when the stop fails the error names each unit's
|
||||
/// state, because systemd may have stopped one and not the other.
|
||||
pub fn stop_all(&self, instance: &Slug) -> Result<()> {
|
||||
let args = stop_args(instance)?;
|
||||
/// Stops the units of one Mind, or of every Mind, and waits for them. An
|
||||
/// inherent method, not on `Launcher`: only a brake hold calls it, through
|
||||
/// `mind-control`. A unit that is not installed is systemctl's exit 5, and
|
||||
/// the others still stop; when a Mind's stop fails the error names each
|
||||
/// unit's state, because systemd may have stopped one and not the other,
|
||||
/// and a failed swarm stop names systemctl's exit status only.
|
||||
pub fn stop(&self, scope: StopScope<'_>) -> Result<()> {
|
||||
let args = stop_args(scope)?;
|
||||
let args: Vec<&str> = args.iter().map(String::as_str).collect();
|
||||
let reply = self.systemctl.run(&args)?;
|
||||
if reply.success {
|
||||
return Ok(());
|
||||
}
|
||||
let states = [RunTurn::SelfRun, RunTurn::PersonaTurn]
|
||||
.into_iter()
|
||||
.map(|turn| {
|
||||
let unit = unit(instance, turn)?;
|
||||
let state = self.unit_state(&unit);
|
||||
Ok(format!("{unit} is {state}"))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?
|
||||
.join(", ");
|
||||
let states = match scope {
|
||||
StopScope::Mind(instance) => {
|
||||
let states = [RunTurn::SelfRun, RunTurn::PersonaTurn]
|
||||
.into_iter()
|
||||
.map(|turn| {
|
||||
let unit = unit(instance, turn)?;
|
||||
let state = self.unit_state(&unit);
|
||||
Ok(format!("{unit} is {state}"))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?
|
||||
.join(", ");
|
||||
format!("; {states}")
|
||||
}
|
||||
StopScope::Swarm => String::new(),
|
||||
};
|
||||
match reply.code {
|
||||
Some(code) => Err(anyhow!("systemctl stop failed with exit status {code}; {states}")),
|
||||
None => Err(anyhow!("systemctl stop was ended by a signal; {states}")),
|
||||
Some(code) => Err(anyhow!("systemctl stop failed with exit status {code}{states}")),
|
||||
None => Err(anyhow!("systemctl stop was ended by a signal{states}")),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1223,7 +1242,7 @@ mod tests {
|
|||
fn stop_args_is_one_synchronous_stop_of_exactly_the_units_start_names() {
|
||||
for name in ["eureka", "eureka.test", "eureka-two"] {
|
||||
let instance = Slug(name.into());
|
||||
let args = stop_args(&instance).unwrap();
|
||||
let args = stop_args(StopScope::Mind(&instance)).unwrap();
|
||||
assert_eq!(args[..2], ["stop", "--"], "one synchronous stop, no --no-block");
|
||||
let (launcher, spy) = spied();
|
||||
let mut started = Vec::new();
|
||||
|
|
@ -1234,21 +1253,42 @@ mod tests {
|
|||
assert_eq!(args[2..], started[..], "stop names exactly the units start names, for both turns");
|
||||
assert!(args[2..].iter().all(|unit| !unit.contains(['*', '?', '['])), "a glob can match another instance or none: {args:?}");
|
||||
}
|
||||
assert_eq!(stop_args(&eureka()).unwrap(), ["stop", "--", "mind-self@eureka.service", "mind-persona@eureka.service"]);
|
||||
assert_eq!(stop_args(StopScope::Mind(&eureka())).unwrap(), ["stop", "--", "mind-self@eureka.service", "mind-persona@eureka.service"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_all_runs_the_one_stop_and_waits() {
|
||||
fn stop_runs_the_one_stop_and_waits() {
|
||||
let (launcher, spy) = spied();
|
||||
launcher.stop_all(&eureka()).unwrap();
|
||||
assert_eq!(*spy.calls.borrow(), vec![stop_args(&eureka()).unwrap()]);
|
||||
launcher.stop(StopScope::Mind(&eureka())).unwrap();
|
||||
assert_eq!(*spy.calls.borrow(), vec![stop_args(StopScope::Mind(&eureka())).unwrap()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_all_with_an_invalid_instance_is_an_error_and_runs_no_systemctl() {
|
||||
fn the_swarm_stop_is_one_stop_of_both_templates_globs() {
|
||||
let (launcher, spy) = spied();
|
||||
launcher.stop(StopScope::Swarm).unwrap();
|
||||
assert_eq!(*spy.calls.borrow(), vec![vec!["stop", "--", "mind-self@*.service", "mind-persona@*.service"]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_swarm_stop_names_the_exit_status_and_asks_no_state() {
|
||||
let (launcher, spy) = spied();
|
||||
spy.success.set(false);
|
||||
spy.code.set(Some(5));
|
||||
*spy.stdout.borrow_mut() = "canary-output-7f3a91
|
||||
".to_string();
|
||||
let shown = format!("{:#}", launcher.stop(StopScope::Swarm).unwrap_err());
|
||||
assert!(shown.contains("exit status 5") && !shown.contains("canary"), "{shown}");
|
||||
assert_eq!(spy.calls.borrow().len(), 1, "no per-unit query for the swarm");
|
||||
spy.code.set(None);
|
||||
assert!(format!("{:#}", launcher.stop(StopScope::Swarm).unwrap_err()).contains("signal"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_with_an_invalid_instance_is_an_error_and_runs_no_systemctl() {
|
||||
let (launcher, spy) = spied();
|
||||
for bad in ["canary-../evil-7f3a91", "canary instance", "", "canary*"] {
|
||||
let error = launcher.stop_all(&Slug(bad.into())).unwrap_err();
|
||||
let error = launcher.stop(StopScope::Mind(&Slug(bad.into()))).unwrap_err();
|
||||
assert!(!format!("{error:#}").contains("canary"), "{error:#}");
|
||||
}
|
||||
assert!(spy.calls.borrow().is_empty());
|
||||
|
|
@ -1260,7 +1300,7 @@ mod tests {
|
|||
spy.success.set(false);
|
||||
spy.code.set(Some(5));
|
||||
spy.states.borrow_mut().extend([("mind-self@eureka.service", "inactive\n"), ("mind-persona@eureka.service", "active\n")]);
|
||||
let shown = format!("{:#}", launcher.stop_all(&eureka()).unwrap_err());
|
||||
let shown = format!("{:#}", launcher.stop(StopScope::Mind(&eureka())).unwrap_err());
|
||||
assert!(shown.contains("exit status 5"), "{shown}");
|
||||
assert!(shown.contains("mind-self@eureka.service is inactive") && shown.contains("mind-persona@eureka.service is active"), "{shown}");
|
||||
let asked: Vec<Vec<String>> = spy.calls.borrow().iter().skip(1).cloned().collect();
|
||||
|
|
@ -1269,10 +1309,10 @@ mod tests {
|
|||
spy.states.borrow_mut().clear();
|
||||
*spy.stdout.borrow_mut() = "canary-output-7f3a91\n".to_string();
|
||||
spy.code.set(None);
|
||||
let shown = format!("{:#}", launcher.stop_all(&eureka()).unwrap_err());
|
||||
let shown = format!("{:#}", launcher.stop(StopScope::Mind(&eureka())).unwrap_err());
|
||||
assert!(shown.contains("signal") && shown.contains("is unknown") && !shown.contains("canary"), "{shown}");
|
||||
spy.states.borrow_mut().push(("mind-self@eureka.service", "failed\n"));
|
||||
let shown = format!("{:#}", launcher.stop_all(&eureka()).unwrap_err());
|
||||
let shown = format!("{:#}", launcher.stop(StopScope::Mind(&eureka())).unwrap_err());
|
||||
assert!(shown.contains("mind-self@eureka.service is failed"), "{shown}");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ fn main() {
|
|||
&set_by,
|
||||
chrono::Utc::now(),
|
||||
&mut std::io::stdout(),
|
||||
&mut |instance| mind_body::launch::SystemdLauncher::new().stop_all(instance),
|
||||
&mut |scope| mind_body::launch::SystemdLauncher::new().stop(scope),
|
||||
);
|
||||
if let Err(error) = result {
|
||||
eprintln!("mind-control: {error:#}");
|
||||
|
|
|
|||
Loading…
Reference in a new issue