reconcile: start a stopped harness inside a running container
hive-priv gains AgentHarness { agent_name, action: probe | start }, which
runs systemctl -M h-<name> against the fixed unit hive-agent.service.
run_reconcile and the 1-min tick read the unit for a running wanted-up
agent and start it when it is inactive or failed. A hive-priv without the
op reads as unknown state and leaves reconcile container-only.
This commit is contained in:
parent
ca734f8ea0
commit
36515c289d
1 changed files with 517 additions and 32 deletions
|
|
@ -18,6 +18,7 @@ use super::{
|
|||
};
|
||||
use crate::coordinator::{AgentPaths, Coordinator, HiveEnv};
|
||||
use crate::job_queue::model::NodeKind;
|
||||
use crate::lifecycle::HarnessState;
|
||||
use crate::power::Wanted;
|
||||
use crate::stats::sweep_health::SweepHealth;
|
||||
use crate::warnings::snapshot;
|
||||
|
|
@ -30,6 +31,8 @@ const CLOSURE: &str = "/nix/store/0123456789abcdfghijklmnpqrsvwxyz-nixos-system-
|
|||
#[derive(Default)]
|
||||
struct FakeHost {
|
||||
running: bool,
|
||||
/// The harness unit inside the container is stopped.
|
||||
harness_stopped: bool,
|
||||
/// What the swarm's builder recorded for the agent; `None` reads as no
|
||||
/// record.
|
||||
built: Mutex<Option<AgentClosure>>,
|
||||
|
|
@ -66,6 +69,12 @@ impl FakeHost {
|
|||
}
|
||||
}
|
||||
|
||||
/// The harness unit is stopped inside the running container.
|
||||
fn harness_down(mut self) -> Self {
|
||||
self.harness_stopped = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// The builder recorded the closure for the agent, pushed or not.
|
||||
fn built(self, cached: bool) -> Self {
|
||||
*self.built.lock().unwrap() = Some(closure(cached));
|
||||
|
|
@ -126,6 +135,19 @@ impl Host for FakeHost {
|
|||
self.running
|
||||
}
|
||||
|
||||
async fn harness_state(&self, name: &str) -> HarnessState {
|
||||
self.record(format!("harness_state {name}"));
|
||||
if self.harness_stopped {
|
||||
HarnessState::Stopped
|
||||
} else {
|
||||
HarnessState::Present
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_harness(&self, name: &str) -> anyhow::Result<()> {
|
||||
self.step("start_harness", format!("start_harness {name}"))
|
||||
}
|
||||
|
||||
async fn kill(&self, name: &str) -> anyhow::Result<()> {
|
||||
self.record(format!("kill {name}"));
|
||||
if self.fails("kill") {
|
||||
|
|
@ -683,6 +705,67 @@ async fn reconcile_is_a_noop_when_state_matches_intent() {
|
|||
assert!(sub.is_none());
|
||||
}
|
||||
|
||||
/// The harness unit is down inside a running container and the agent is
|
||||
/// wanted up: reconcile starts it and plans no container work.
|
||||
#[tokio::test]
|
||||
async fn reconcile_starts_a_stopped_harness_of_a_wanted_up_running_agent() {
|
||||
let (_dir, coord) = coordinator();
|
||||
coord.power.set("alice", Wanted::Up);
|
||||
let host = FakeHost::up().harness_down();
|
||||
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
|
||||
assert!(sub.is_none(), "the container is left alone");
|
||||
assert_eq!(
|
||||
host.calls(),
|
||||
[
|
||||
"is_running alice",
|
||||
"harness_state alice",
|
||||
"start_harness alice"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// Each case that must not start the harness, against the control above.
|
||||
#[tokio::test]
|
||||
async fn reconcile_leaves_the_harness_alone_unless_the_agent_is_wanted_up_and_running() {
|
||||
let (_dir, coord) = coordinator();
|
||||
// A running harness (a paused agent keeps its harness active too).
|
||||
coord.power.set("alice", Wanted::Up);
|
||||
let host = FakeHost::up();
|
||||
run_reconcile(&host, &coord, "alice").await.expect("ok");
|
||||
assert!(!host.calls().contains(&"start_harness alice".to_owned()));
|
||||
|
||||
// Wanted offline: the container is stopped instead, the harness untouched.
|
||||
coord.power.set("alice", Wanted::Offline);
|
||||
let host = FakeHost::up().harness_down();
|
||||
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
|
||||
assert!(matches!(sub, Some(NodeKind::Stop { .. })));
|
||||
assert_eq!(host.calls(), ["is_running alice"]);
|
||||
|
||||
// Wanted offline and already down: nothing to do, the harness is not read.
|
||||
coord.power.set("alice", Wanted::Offline);
|
||||
let host = FakeHost::down().harness_down();
|
||||
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
|
||||
assert!(sub.is_none());
|
||||
assert_eq!(host.calls(), ["is_running alice"]);
|
||||
|
||||
// Container down: it is started, and the harness is not read.
|
||||
coord.power.set("alice", Wanted::Up);
|
||||
let host = FakeHost::down().harness_down();
|
||||
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
|
||||
assert!(matches!(sub, Some(NodeKind::Start { .. })));
|
||||
assert_eq!(host.calls(), ["is_running alice"]);
|
||||
}
|
||||
|
||||
/// A failed harness start is logged; the reconcile node still succeeds.
|
||||
#[tokio::test]
|
||||
async fn a_failed_harness_start_does_not_fail_the_reconcile() {
|
||||
let (_dir, coord) = coordinator();
|
||||
coord.power.set("alice", Wanted::Up);
|
||||
let host = FakeHost::up().harness_down().failing("start_harness");
|
||||
run_reconcile(&host, &coord, "alice").await.expect("ok");
|
||||
assert!(host.calls().contains(&"start_harness alice".to_owned()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reconcile_seeds_a_missing_intent_from_the_observed_state() {
|
||||
let (_dir, coord) = coordinator();
|
||||
|
|
|
|||
Loading…
Reference in a new issue