diff options
| author | tv <tv@krebsco.de> | 2026-12-01 00:51:48 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-01 00:51:48 +0000 |
| commit | 54f070ef0f905f867a24dece72f667be23f9f843 (patch) | |
| tree | 6f6258ebfa3bb0532ad3abbbeefc5a550dc4d2cf | |
| parent | 683e2128a5d00bcadea7cc2f71a48253032632a5 (diff) | |
enstate: --apply starts the unit that runs --migrate again if it failed
The setting unit names it (the module: enstate.service). After a boot
without state it failed; an accepted --apply starts it (--no-block,
with the lock released), so it serves the views and is no longer
failed.
| -rw-r--r-- | nix/module.nix | 2 | ||||
| -rw-r--r-- | src/main.rs | 19 | ||||
| -rw-r--r-- | src/plan.rs | 20 | ||||
| -rw-r--r-- | tests/receptor.rs | 11 |
4 files changed, 51 insertions, 1 deletions
diff --git a/nix/module.nix b/nix/module.nix index 62f4222..a273d45 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -142,6 +142,8 @@ let settings = pkgs.writers.writeJSON "enstate-config.json" { state-dir = cfg.stateDir; run-dir = cfg.runDir; + # Started again by --apply if it failed (for want of the state, at boot). + unit = "enstate.service"; }; enstate = lib.getExe cfg.package; diff --git a/src/main.rs b/src/main.rs index 4c1e24b..8d3830b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,13 @@ const OPTIONS: &[OptionSpec] = &[ Some("/etc/enstate/schema.json"), "The schema of the running generation; for --pack, one to check the source list against", ), + option( + "unit", + Kind::Text, + "UNIT", + None, + "The systemd unit that runs --migrate at boot and after switches: --apply starts it again if it failed", + ), OptionSpec { hidden: true, ..option( @@ -67,6 +74,7 @@ struct Settings { run_dir: Option<PathBuf>, schema: Option<PathBuf>, systemctl: Option<PathBuf>, + unit: Option<String>, } const COMMANDS: &[(&str, &str)] = &[ @@ -249,13 +257,22 @@ fn run() -> Result<ExitCode> { "plan" | "apply" | "stage" => { let submission = Submission::load(path.as_deref().unwrap())?; let planned = plan::plan(&schema, &store, &submission)?; - let plan = if command == "apply" { + let mut plan = if command == "apply" { planned.apply(&store, &schema_bytes, &systemctl, lock)? } else if command == "stage" { planned.stage(&store, &schema_bytes, lock)? } else { planned.plan }; + // A unit that failed for want of the state applied now (at boot, say) + // gets to serve the views, and is no longer failed. + if command == "apply" + && plan.accepted + && let Some(unit) = &settings.unit + && let Some(failed) = plan::start_if_failed(unit, &systemctl) + { + plan.failed.push(failed); + } if json { println!("{}", serde_json::to_string_pretty(&plan)?); } else { diff --git a/src/plan.rs b/src/plan.rs index b37510d..12c91b6 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -592,3 +592,23 @@ pub fn react(reactions: &Reactions, systemctl: &Path, how: Reacting) -> Vec<Stri } failed } + +/// Starts `unit` (the one that runs `--migrate`), without waiting for it, if it has +/// failed; what failed in doing so. +pub fn start_if_failed(unit: &str, systemctl: &Path) -> Option<String> { + let failed = Command::new(systemctl) + .args(["is-failed", "--quiet", unit]) + .status() + .is_ok_and(|status| status.success()); + if !failed { + return None; + } + match Command::new(systemctl) + .args(["start", "--no-block", unit]) + .status() + { + Ok(status) if status.success() => None, + Ok(status) => Some(format!("{} start {unit}: {status}", systemctl.display())), + Err(error) => Some(format!("{} start {unit}: {error}", systemctl.display())), + } +} diff --git a/tests/receptor.rs b/tests/receptor.rs index 656ed1f..8bbadf4 100644 --- a/tests/receptor.rs +++ b/tests/receptor.rs @@ -536,6 +536,17 @@ fn command_line() { assert_eq!(code, Some(0), "{stderr}"); let plan: Value = serde_json::from_str(&stdout).unwrap(); assert_eq!(plan["accepted"], json!(true)); + // With the unit that runs --migrate named: started again if it failed (the fake + // systemctl says it did). + assert!(!host.reactions().contains("is-failed")); + let (code, _, stderr) = enstate(&["--apply", good.to_str().unwrap(), "--unit=enstate.service"]); + assert_eq!(code, Some(0), "{stderr}"); + assert!( + host.reactions() + .contains("is-failed --quiet enstate.service\nstart --no-block enstate.service\n"), + "{}", + host.reactions() + ); let (code, stdout, _) = enstate(&["--status"]); assert_eq!(code, Some(0)); assert!( |
