From 54f070ef0f905f867a24dece72f667be23f9f843 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 00:51:48 +0000 Subject: 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. --- nix/module.nix | 2 ++ src/main.rs | 19 ++++++++++++++++++- src/plan.rs | 20 ++++++++++++++++++++ tests/receptor.rs | 11 +++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) 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, schema: Option, systemctl: Option, + unit: Option, } const COMMANDS: &[(&str, &str)] = &[ @@ -249,13 +257,22 @@ fn run() -> Result { "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 Option { + 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!( -- cgit v1.3.1