aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 15:02:23 +0000
committertv <tv@krebsco.de>2026-11-30 14:59:15 +0000
commita0f22c88d54d351b060c2d0d9f0a8cbf5f412ba2 (patch)
treed65495f9ed222fb978bc09afcb3bd644a99aa7c5 /src/main.rs
parent83f1d36bbaeb193ae1c663b5b1b7956661ecac8c (diff)
enstate: --migrate, renames both ways, kept entries
What a generation sees of a snapshot accepted under another schema: same name and type, renamedFrom, a rename in the stored schema read backwards, and entries kept, with their types, in the snapshot's kept/. Plans start from what is seen, and store it under the running schema; a generation change rewrites no snapshot. --migrate (and --dry-run) says what each channel sees, refuses for required slots nothing fills, puts the views in place and runs their reactions; --materialize and --status go by what is seen, too. A view made from an unfilled required slot isn't there. The rollback cases of migrations.md that need no converters or steps are tests.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs154
1 files changed, 109 insertions, 45 deletions
diff --git a/src/main.rs b/src/main.rs
index 16c553d..3539d76 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -81,6 +81,10 @@ const COMMANDS: &[(&str, &str)] = &[
"Put every view in place from the snapshots (at boot, after a switch)",
),
(
+ "migrate",
+ "Serve the generation of --schema from the snapshots: what it sees, its views, their reactions",
+ ),
+ (
"pack",
"Make a submission in --out from the source list in PATH",
),
@@ -119,7 +123,13 @@ fn command() -> Command {
Arg::new("json")
.long("json")
.action(ArgAction::SetTrue)
- .help("Print JSON (--plan, --apply, --status, --materialize)"),
+ .help("Print JSON (--plan, --apply, --status, --materialize, --migrate)"),
+ )
+ .arg(
+ Arg::new("dry-run")
+ .long("dry-run")
+ .action(ArgAction::SetTrue)
+ .help("With --migrate: say what it would do, changing nothing"),
)
.arg(
Arg::new("out")
@@ -188,6 +198,9 @@ fn run() -> Result<ExitCode> {
if out.is_some() && command != "pack" {
bail!("--out is for --pack only");
}
+ if matches.get_flag("dry-run") && command != "migrate" {
+ bail!("--dry-run is for --migrate only");
+ }
if json && command == "pack" {
bail!("--json is not for --pack");
}
@@ -241,7 +254,7 @@ fn run() -> Result<ExitCode> {
})
}
"materialize" => {
- let views = plan::materialize(&schema, &store)?;
+ let views = enstate::migrate::materialize(&schema, &store)?;
if json {
println!("{}", serde_json::to_string_pretty(&views)?);
} else {
@@ -249,6 +262,26 @@ fn run() -> Result<ExitCode> {
}
Ok(ExitCode::SUCCESS)
}
+ "migrate" => {
+ let migration = enstate::migrate::migrate(
+ &schema,
+ &store,
+ matches.get_flag("dry-run"),
+ &systemctl,
+ )?;
+ if json {
+ println!("{}", serde_json::to_string_pretty(&migration)?);
+ } else {
+ print_migration(&migration)?;
+ }
+ Ok(if !migration.accepted {
+ ExitCode::from(1)
+ } else if !migration.failed.is_empty() {
+ ExitCode::from(3)
+ } else {
+ ExitCode::SUCCESS
+ })
+ }
"status" => {
let status = status(&schema, &store)?;
if json {
@@ -405,6 +438,8 @@ struct ChannelStatus {
missing: Vec<String>,
/// Kept, but in no slot of the schema.
kept: Vec<String>,
+ /// Where entries came from, what can't be taken.
+ notes: Vec<String>,
}
#[derive(serde::Serialize)]
@@ -413,27 +448,57 @@ struct ViewStatus {
installed: bool,
}
-fn status(schema: &Schema, store: &Store) -> Result<Status> {
- let mut channels = Vec::new();
- let mut names: Vec<String> = schema.channels.keys().cloned().collect();
- for channel in store.channels()? {
- if !names.contains(&channel) {
- names.push(channel);
+fn print_status(status: &Status) -> Result<()> {
+ let style = Style::new();
+ let mut out = std::io::stdout().lock();
+ for channel in &status.channels {
+ writeln!(
+ out,
+ "channel {}: snapshot {}",
+ channel.channel,
+ channel.snapshot.as_deref().unwrap_or("none")
+ )?;
+ for slot in &channel.filled {
+ writeln!(out, " {} {slot}", style.paint("32", "filled"))?;
+ }
+ for slot in &channel.unfilled {
+ let missing = channel.missing.contains(slot);
+ let label = if missing {
+ style.paint("31", "missing")
+ } else {
+ "unfilled".to_owned()
+ };
+ writeln!(out, " {label} {slot}")?;
+ }
+ for slot in &channel.kept {
+ writeln!(out, " {} {slot}", style.paint("33", "kept"))?;
+ }
+ for note in &channel.notes {
+ writeln!(out, " {note}")?;
}
}
- for channel in names {
- let snapshot = store.current(&channel)?;
- let content = snapshot
- .as_ref()
- .map(|snapshot| snapshot.content.clone())
- .unwrap_or_default();
+ for view in &status.views {
+ let state = if view.installed {
+ style.paint("32", "in place")
+ } else {
+ style.paint("33", "not in place")
+ };
+ writeln!(out, "view {}: {state}", view.view)?;
+ }
+ Ok(())
+}
+
+fn status(schema: &Schema, store: &Store) -> Result<Status> {
+ let mut channels = Vec::new();
+ for (channel, seen) in enstate::migrate::seen_all(schema, store)? {
let slots = schema
.channel(&channel)
.map(|spec| spec.slots.clone())
.unwrap_or_default();
+ let content = &seen.content;
channels.push(ChannelStatus {
channel: channel.clone(),
- snapshot: snapshot.map(|snapshot| snapshot.id),
+ snapshot: store.current(&channel)?.map(|snapshot| snapshot.id),
filled: slots
.keys()
.filter(|slot| content.contains_key(*slot))
@@ -449,11 +514,8 @@ fn status(schema: &Schema, store: &Store) -> Result<Status> {
.filter(|(slot, spec)| spec.required && !content.contains_key(*slot))
.map(|(slot, _)| slot.clone())
.collect(),
- kept: content
- .keys()
- .filter(|slot| !slots.contains_key(*slot))
- .cloned()
- .collect(),
+ kept: seen.kept_names(),
+ notes: seen.notes.clone(),
});
}
let views = schema
@@ -467,39 +529,41 @@ fn status(schema: &Schema, store: &Store) -> Result<Status> {
Ok(Status { channels, views })
}
-fn print_status(status: &Status) -> Result<()> {
+fn print_migration(migration: &enstate::migrate::Migration) -> Result<()> {
let style = Style::new();
let mut out = std::io::stdout().lock();
- for channel in &status.channels {
+ let verdict = if migration.accepted {
+ style.paint("32", "every required slot is filled")
+ } else {
+ style.paint("31", "refused")
+ };
+ let dry_run = if migration.dry_run { " (dry run)" } else { "" };
+ writeln!(out, "migration{dry_run}: {verdict}")?;
+ for reason in &migration.refused {
+ writeln!(out, " {} {reason}", style.paint("31", "refused:"))?;
+ }
+ for (channel, channel_migration) in &migration.channels {
writeln!(
out,
- "channel {}: snapshot {}",
- channel.channel,
- channel.snapshot.as_deref().unwrap_or("none")
+ "channel {channel}: snapshot {}",
+ channel_migration.snapshot.as_deref().unwrap_or("none")
)?;
- for slot in &channel.filled {
- writeln!(out, " {} {slot}", style.paint("32", "filled"))?;
- }
- for slot in &channel.unfilled {
- let missing = channel.missing.contains(slot);
- let label = if missing {
- style.paint("31", "missing")
- } else {
- "unfilled".to_owned()
- };
- writeln!(out, " {label} {slot}")?;
+ for note in &channel_migration.seen.notes {
+ writeln!(out, " {note}")?;
}
- for slot in &channel.kept {
- writeln!(out, " {} {slot}", style.paint("33", "kept"))?;
+ for slot in &channel_migration.kept {
+ writeln!(out, " {} {channel}/{slot}", style.paint("33", "kept"))?;
}
}
- for view in &status.views {
- let state = if view.installed {
- style.paint("32", "in place")
- } else {
- style.paint("33", "not in place")
- };
- writeln!(out, "view {}: {state}", view.view)?;
+ print_views(&mut out, &migration.views, true)?;
+ if !migration.reactions.restart.is_empty() {
+ writeln!(out, "restart: {}", migration.reactions.restart.join(" "))?;
+ }
+ if !migration.reactions.reload.is_empty() {
+ writeln!(out, "reload: {}", migration.reactions.reload.join(" "))?;
+ }
+ for failed in &migration.failed {
+ writeln!(out, "{} {failed}", style.paint("31", "failed:"))?;
}
Ok(())
}