diff options
| author | tv <tv@krebsco.de> | 2026-11-30 18:21:16 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 19:51:09 +0000 |
| commit | b74359e97643be0f43034c3aeab533a2389a939f (patch) | |
| tree | 4f13dc922acb8f7bef53ca3e787d0a296692bb52 /src/migrate.rs | |
| parent | 62f857e35f94e98b3205502e410f0d727fa5ecc5 (diff) | |
enstate: submissions waiting for the next switch
--migrate first takes the submissions in STATE/incoming, in the order of
their names: planned against the schema, stored if accepted (their views
follow with the rest), removed, and moved to STATE/refused if refused,
which refuses the migration. A dry run (the check before a switch) does
so on a copy of the snapshots, changing nothing. --stage leaves a
marker, STATE/migrate-due, for the next switch to migrate even if the
schema doesn't change; --migrate removes it. Scratch directories are
cleaned up without following the symbolic links in them.
Diffstat (limited to 'src/migrate.rs')
| -rw-r--r-- | src/migrate.rs | 104 |
1 files changed, 97 insertions, 7 deletions
diff --git a/src/migrate.rs b/src/migrate.rs index dbbf38d..ca1e6b5 100644 --- a/src/migrate.rs +++ b/src/migrate.rs @@ -23,10 +23,11 @@ use serde::Serialize; use crate::content::Content; use crate::name::Address; -use crate::plan::{Reacting, ViewOutcome, react}; +use crate::plan::{self, Plan, Reacting, ViewOutcome, react}; use crate::schema::{Reactions, Schema}; use crate::scratch::Scratch; use crate::store::{Kept, Snapshot, Store}; +use crate::submission::Submission; use crate::view; /// What the running generation sees of one channel. @@ -253,6 +254,9 @@ pub struct Migration { pub views: Vec<ViewOutcome>, pub reactions: Reactions, pub dry_run: bool, + /// The submissions taken from the incoming directory. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub incoming: Vec<IncomingOutcome>, #[serde(skip_serializing_if = "Vec::is_empty")] pub failed: Vec<String>, } @@ -265,20 +269,98 @@ pub struct ChannelMigration { pub kept: Vec<String>, } -/// Serves the generation of `schema` from the snapshots, after a generation change, or -/// before it (`dry_run`): what each channel sees, which required slots nothing fills -/// (which refuses the switch), and how the views change. Unless `dry_run`, the views -/// are put in place, those of unfilled required slots taken away, and the reactions of -/// those that changed run by `systemctl`. +/// An incoming submission, and what became of it. +#[derive(Debug, Clone, Serialize)] +pub struct IncomingOutcome { + pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub plan: Option<Plan>, + /// Why it couldn't even be planned. + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option<String>, +} + +/// Serves the generation of `schema` (as `schema_bytes`) from the snapshots, after a +/// generation change, or before it (`dry_run`). First the submissions waiting in the +/// incoming directory are planned against `schema`, in order, and stored if accepted +/// (no views, no reactions yet); they are removed, and those refused moved to the +/// refused directory. Then: what each channel sees, which required slots nothing +/// fills, and how the views change. A refused submission, or an unfilled required +/// slot, refuses the switch. Unless `dry_run`, the views are put in place, those of +/// unfilled required slots taken away, and the reactions of those that changed run by +/// `systemctl`. A dry run does all of it on a copy of the snapshots. pub fn migrate( schema: &Schema, + schema_bytes: &[u8], store: &Store, 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-")?), + false => None, + }; + let copy = scratch + .as_ref() + .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 store = work; let seen = seen_all(schema, store)?; let mut channels = BTreeMap::new(); - let mut refused = Vec::new(); for (channel, channel_seen) in &seen { for slot in &channel_seen.unfilled { refused.push(format!("required slot {slot} can't be filled")); @@ -311,6 +393,13 @@ pub fn migrate( let failed = if dry_run { Vec::new() } else { + match std::fs::remove_file(store.migrate_due()) { + Err(error) if error.kind() != std::io::ErrorKind::NotFound => { + return Err(error) + .with_context(|| format!("removing {}", store.migrate_due().display())); + } + _ => {} + } react(&reactions, systemctl, Reacting::Migrate) }; Ok(Migration { @@ -320,6 +409,7 @@ pub fn migrate( views, reactions, dry_run, + incoming, failed, }) } |
