diff options
| author | tv <tv@krebsco.de> | 2026-12-01 12:51:55 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-01 12:58:25 +0000 |
| commit | 63311f0741d543093e083b9c6004aef6bf2a8421 (patch) | |
| tree | d4258c933fcccd735b669f27839364f593d58e13 /src/migrate.rs | |
| parent | 8dd22f4eb40ce10913daeeca15113379eb863282 (diff) | |
enstate: refused submissions, visible
A submission the switch refuses is moved to STATE/refused/NAME with
NAME.json next to it: when, and why ("refused" if nothing more). The
check's message names the waiting submission's path. --status lists
what waits in the incoming directory, planned against the running
generation (one the check refused stays waiting, and refuses switches
while it does), and what was refused, with the reasons (and --json as
data); it exits with 1 if something needs attention: a refused
submission, a waiting one the running generation would refuse, an
unfilled required slot, so that a deployment running it notices.
--forget NAME removes a waiting or refused submission. After hrm's
review.
Diffstat (limited to 'src/migrate.rs')
| -rw-r--r-- | src/migrate.rs | 163 |
1 files changed, 111 insertions, 52 deletions
diff --git a/src/migrate.rs b/src/migrate.rs index 5b97d27..15dd2f7 100644 --- a/src/migrate.rs +++ b/src/migrate.rs @@ -296,7 +296,6 @@ pub fn migrate( dry_run: bool, systemctl: &Path, ) -> Result<Migration> { - let mut refused = Vec::new(); let pending = store.incoming()?; let scratch = match dry_run && !pending.is_empty() { true => Some(Scratch::new(&store.scratch(), "state-")?), @@ -307,57 +306,8 @@ pub fn migrate( .map(|scratch| store.copy_to(scratch.path())) .transpose()?; let work = copy.as_ref().unwrap_or(store); - let mut incoming = Vec::new(); - for dir in &pending { - let name = dir - .file_name() - .map(|name| name.to_string_lossy().into_owned()) - .unwrap_or_default(); - let planned = - Submission::load(dir).and_then(|submission| plan::plan(schema, work, &submission)); - let (outcome, accepted) = match planned { - Err(error) => { - refused.push(format!("incoming {name}: {error:#}")); - let error = Some(format!("{error:#}")); - ( - IncomingOutcome { - name: name.clone(), - plan: None, - error, - }, - false, - ) - } - Ok(planned) => { - let plan = planned.store(work, schema_bytes)?; - if !plan.accepted { - refused.push(format!("incoming {name}: {}", plan.refused.join("; "))); - } - let accepted = plan.accepted; - ( - IncomingOutcome { - name: name.clone(), - plan: Some(plan), - error: None, - }, - accepted, - ) - } - }; - incoming.push(outcome); - if !dry_run { - if accepted { - std::fs::remove_dir_all(dir) - .with_context(|| format!("removing {}", dir.display()))?; - } else { - let target = store.refused_dir().join(&name); - std::fs::create_dir_all(store.refused_dir())?; - let _ = std::fs::remove_dir_all(&target); - std::fs::rename(dir, &target) - .with_context(|| format!("moving {} to {}", dir.display(), target.display()))?; - } - } - } + let (incoming, mut refused) = + take_incoming(schema, schema_bytes, store, work, &pending, dry_run)?; let store = work; let seen = seen_all(schema, store)?; let mut channels = BTreeMap::new(); @@ -413,3 +363,112 @@ pub fn migrate( failed, }) } + +/// Plans the submissions `pending` (in the incoming directory) against `schema`, in +/// order, storing those accepted in `work`, the store or a copy of it; unless `dry_run`, +/// they are removed, and those refused moved to the refused directory. Returns what +/// became of each, and why the refused ones were refused. +fn take_incoming( + schema: &Schema, + schema_bytes: &[u8], + store: &Store, + work: &Store, + pending: &[std::path::PathBuf], + dry_run: bool, +) -> Result<(Vec<IncomingOutcome>, Vec<String>)> { + let mut refused = Vec::new(); + let mut incoming = Vec::new(); + for dir in pending { + let name = dir + .file_name() + .map(|name| name.to_string_lossy().into_owned()) + .unwrap_or_default(); + let planned = + Submission::load(dir).and_then(|submission| plan::plan(schema, work, &submission)); + let (outcome, accepted) = match planned { + Err(error) => { + refused.push(format!("incoming {name} ({}): {error:#}", dir.display())); + let error = Some(format!("{error:#}")); + ( + IncomingOutcome { + name: name.clone(), + plan: None, + error, + }, + false, + ) + } + Ok(planned) => { + let plan = planned.store(work, schema_bytes)?; + if !plan.accepted { + refused.push(format!( + "incoming {name} ({}): {}", + dir.display(), + plan.refused.join("; ") + )); + } + let accepted = plan.accepted; + ( + IncomingOutcome { + name: name.clone(), + plan: Some(plan), + error: None, + }, + accepted, + ) + } + }; + let reasons = outcome.reasons(); + incoming.push(outcome); + if !dry_run { + if accepted { + std::fs::remove_dir_all(dir) + .with_context(|| format!("removing {}", dir.display()))?; + } else { + store.refuse_incoming(dir, &name, &reasons)?; + } + } + } + Ok((incoming, refused)) +} + +/// What the submissions waiting in the incoming directory would do, planned against +/// `schema`, in order, on a copy of the snapshots, changing nothing: for `--status`, +/// which shows what the running generation would refuse (a submission the check +/// before a switch refused stays waiting, and refuses every later switch). +pub fn plan_incoming( + schema: &Schema, + schema_bytes: &[u8], + store: &Store, +) -> Result<Vec<IncomingOutcome>> { + let pending = store.incoming()?; + if pending.is_empty() { + return Ok(Vec::new()); + } + let scratch = Scratch::new(&store.scratch(), "state-")?; + let copy = store.copy_to(scratch.path())?; + Ok(take_incoming(schema, schema_bytes, store, ©, &pending, true)?.0) +} + +impl IncomingOutcome { + pub fn accepted(&self) -> bool { + self.error.is_none() && self.plan.as_ref().is_some_and(|plan| plan.accepted) + } + + /// Why it was refused, if it was. + pub fn reasons(&self) -> Vec<String> { + if self.accepted() { + return Vec::new(); + } + let reasons = match (&self.error, &self.plan) { + (Some(error), _) => vec![error.clone()], + (None, Some(plan)) => plan.refused.clone(), + (None, None) => Vec::new(), + }; + if reasons.is_empty() { + vec!["refused".to_owned()] + } else { + reasons + } + } +} |
