diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 19 | ||||
| -rw-r--r-- | src/plan.rs | 20 |
2 files changed, 38 insertions, 1 deletions
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())), + } +} |
