From 62f857e35f94e98b3205502e410f0d727fa5ecc5 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 30 Nov 2026 17:56:39 +0000 Subject: enstate: --stage, a submission for the next generation Plans against --schema, stores the snapshot under it, and puts no views in place and runs no reactions: the running generation goes on as it is, keeping what it has no slot for, and the switch (--migrate) brings the new views. The way to fill a required slot a generation adds before switching to it, and to bring a host's first state: with the new system's enstate, schema and configuration. Tests, and a subtest of the VM test that does so. --- README.md | 3 ++ nix/test.nix | 25 ++++++++++++++++ src/main.rs | 24 ++++++++++++---- src/plan.rs | 27 ++++++++++++++++++ tests/common/mod.rs | 11 +++++++ tests/migrations.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 166 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8a5f624..96282c5 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ The design is in [docs/design.md](docs/design.md); doing it: complete and partial submissions, items required and optional, groups, removal, the chain as order, validators (a type's and across slots), views and their reactions; +- `--stage PATH`: a submission for the generation of `--schema`, before + switching to it (a host's first one, too): the snapshot only, no views, + no reactions; - `--migrate [--dry-run]`: serving a generation from snapshots accepted under another schema (same names, `renamedFrom` both ways, kept entries), refusing where a required slot can't be filled; diff --git a/nix/test.nix b/nix/test.nix index f2739eb..d2f69d9 100644 --- a/nix/test.nix +++ b/nix/test.nix @@ -52,6 +52,11 @@ pkgs.testers.runNixOSTest { systemd.services.consumer = credential 50 config "retiolum.key"; }; + # Adds a required slot: a submission for it is staged before switching. + specialisation.added.configuration = { + enstate.channels.secrets.slots."extra.key" = key; + }; + specialisation.retyped.configuration = { enstate.channels.secrets.slots = lib.mkForce { "tinc.key" = key // { @@ -110,6 +115,26 @@ pkgs.testers.runNixOSTest { machine.fail("/run/booted-system/specialisation/retyped/bin/switch-to-configuration test") machine.succeed("enstate --status | grep -q 'filled tinc.key'") + with subtest("a generation that adds a required slot: staged with its own enstate, then switched to"): + new = "/run/booted-system/specialisation/added" + machine.fail(f"{new}/bin/switch-to-configuration test") + machine.succeed( + "mkdir -p /root/added", + "printf X > /root/added/key", + """echo '{"channel": "secrets", "items": {"extra.key": {"source": {"file": "key"}}}}' """ + "> /root/added/sources.json", + f"{new}/sw/bin/enstate --pack /root/added/sources.json --out /root/added/submission", + f"{new}/sw/bin/enstate --plan /root/added/submission --schema {new}/etc/enstate/schema.json --config {new}/etc/enstate/config.json", + f"{new}/sw/bin/enstate --stage /root/added/submission --schema {new}/etc/enstate/schema.json --config {new}/etc/enstate/config.json", + ) + machine.fail("test -e /run/enstate/views/secrets.extra.key") + seen("tinc.key L") + machine.succeed(f"{new}/bin/switch-to-configuration test") + machine.wait_until_succeeds("grep -qx X /run/enstate/views/secrets.extra.key/extra.key") + seen("tinc.key L") + machine.succeed("/run/booted-system/bin/switch-to-configuration test") + machine.succeed("enstate --status | grep -q 'kept extra.key'") + with subtest("after a reboot, the views are there again"): machine.shutdown() machine.start() diff --git a/src/main.rs b/src/main.rs index 12db84b..df8b5c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,10 @@ const COMMANDS: &[(&str, &str)] = &[ "apply", "Apply the submission in PATH, if its plan is accepted", ), + ( + "stage", + "Like --apply, for the generation of --schema before switching to it: the snapshot only, no views, no reactions", + ), ("status", "Show the channels' snapshots and the views"), ( "materialize", @@ -126,7 +130,7 @@ fn command() -> Command { Arg::new("json") .long("json") .action(ArgAction::SetTrue) - .help("Print JSON (--plan, --apply, --status, --materialize, --migrate)"), + .help("Print JSON (--plan, --apply, --stage, --status, --materialize, --migrate)"), ) .arg( Arg::new("dry-run") @@ -143,7 +147,7 @@ fn command() -> Command { .arg( Arg::new("path") .value_name("PATH") - .help("The submission (--plan, --apply) or source list (--pack)"), + .help("The submission (--plan, --apply, --stage) or source list (--pack)"), ) .next_help_heading("Settings"); config::arguments::with_print_config(config::arguments::with_options(command, OPTIONS)) @@ -199,7 +203,7 @@ fn run() -> Result { let path = matches.get_one::("path").map(PathBuf::from); let out = matches.get_one::("out").map(PathBuf::from); // An option that isn't for the command is an error. - let takes_path = matches!(command, "plan" | "apply" | "pack"); + let takes_path = matches!(command, "plan" | "apply" | "stage" | "pack"); match (&path, takes_path) { (None, true) => bail!("--{command} needs a PATH"), (Some(path), false) => bail!("--{command} takes no PATH ({})", path.display()), @@ -242,18 +246,20 @@ fn run() -> Result { .unwrap_or_else(|| PathBuf::from("systemctl")); let lock = store.lock()?; match command { - "plan" | "apply" => { + "plan" | "apply" | "stage" => { let submission = Submission::load(path.as_deref().unwrap())?; let planned = plan::plan(&schema, &store, &submission)?; let plan = if command == "apply" { planned.apply(&store, &schema_bytes, &systemctl, lock)? + } else if command == "stage" { + planned.stage(&store, &schema_bytes, lock)? } else { planned.plan }; if json { println!("{}", serde_json::to_string_pretty(&plan)?); } else { - print_plan(&plan, command == "apply")?; + print_plan(&plan, command != "plan")?; } Ok(if !plan.accepted { ExitCode::from(1) @@ -388,8 +394,14 @@ fn print_plan(plan: &Plan, applying: bool) -> Result<()> { } if applying && plan.accepted { match &plan.snapshot { + Some(id) if plan.staged => writeln!( + out, + "staged: snapshot {id}; views and reactions come with the switch" + )?, Some(id) => writeln!(out, "applied: snapshot {id}")?, - None if plan.changes => writeln!(out, "applied: the snapshot is unchanged")?, + None if plan.changes && !plan.staged => { + writeln!(out, "applied: the snapshot is unchanged")? + } None => {} } for failed in &plan.failed { diff --git a/src/plan.rs b/src/plan.rs index 8627dd8..9a56505 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -44,6 +44,10 @@ pub struct Plan { /// The new snapshot's id, once applied. #[serde(skip_serializing_if = "Option::is_none")] pub snapshot: Option, + /// Whether it was staged (`--stage`): its views and reactions come with the + /// switch to the generation it was planned for. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub staged: bool, /// Reactions that failed, once applied. #[serde(skip_serializing_if = "Vec::is_empty")] pub failed: Vec, @@ -119,6 +123,7 @@ pub fn plan(schema: &Schema, store: &Store, submission: &Submission) -> Result

Result { + let mut plan = self.plan; + plan.staged = true; + if plan.accepted && self.content_changes { + plan.snapshot = Some(store.commit( + &plan.channel, + &self.content, + &self.kept, + plan.current.as_deref(), + schema_bytes, + )?); + } + drop(lock); + Ok(plan) + } } /// How reactions are run. diff --git a/tests/common/mod.rs b/tests/common/mod.rs index f1f3f74..6f80d65 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -127,6 +127,17 @@ impl Host { enstate::migrate::migrate(&schema.0, &store, dry_run, &self.systemctl()).unwrap() } + /// Stages a submission for the generation of `schema`, before switching to it. + pub fn stage(&self, schema: &(Schema, Vec), dir: &Path) -> Plan { + let submission = Submission::load(dir).unwrap(); + let store = self.store(); + let lock = store.lock().unwrap(); + plan::plan(&schema.0, &store, &submission) + .unwrap() + .stage(&store, &schema.1, lock) + .unwrap() + } + /// The current snapshot's id of `channel`. pub fn snapshot(&self, channel: &str) -> Option { self.store() diff --git a/tests/migrations.rs b/tests/migrations.rs index b91972c..1856b38 100644 --- a/tests/migrations.rs +++ b/tests/migrations.rs @@ -248,3 +248,85 @@ fn what_is_kept() { .is_empty() ); } + +/// A generation that adds a required slot: the switch is refused until a submission +/// for it is staged, which changes nothing the running generation serves; a submission +/// under the running generation keeps the staged entry; the switch brings it. And a +/// first deployment, without any state. +#[test] +fn staging_for_the_next_generation() { + let host = Host::new("stage"); + let s1_value = secrets( + json!({ "a": { "type": "application/octet-stream" } }), + json!({ "a": { "from": ["secrets/a"] } }), + ); + let s1 = host.schema(s1_value.clone()); + assert!(host.apply(&s1, &rotate(&host, "a", "a", b"A")).accepted); + let reactions_before = host.reactions(); + let s2_value = secrets( + json!({ + "a": { "type": "application/octet-stream" }, + "b.key": { "type": "application/octet-stream", "secret": true } + }), + json!({ + "a": { "from": ["secrets/a"] }, + "b.key": { "from": ["secrets/b.key"], "reactions": { "restart": ["b.service"] } } + }), + ); + let s2 = host.schema(s2_value.clone()); + assert_eq!( + host.migrate(&s2, true).refused, + ["required slot secrets/b.key can't be filled"] + ); + + // Staged: the snapshot, under S2; no views, no reactions. + let plan = host.stage(&s2, &rotate(&host, "b", "b.key", b"B")); + assert!( + plan.accepted && plan.staged && plan.snapshot.is_some(), + "{plan:?}" + ); + assert!(host.view("b.key", "b.key").is_none()); + assert_eq!(host.view("a", "a").unwrap(), b"A"); + assert_eq!(host.reactions(), reactions_before); + assert_eq!( + host.store().current("secrets").unwrap().unwrap().schema, + s2.1 + ); + assert!(host.migrate(&s2, true).accepted); + + // The running generation goes on: what it has no slot for is kept. + let s1 = host.schema(s1_value); + let plan = host.apply(&s1, &rotate(&host, "a2", "a", b"A2")); + assert_eq!( + plan.warnings, + ["kept, in no slot of the schema: secrets/b.key"] + ); + + // The switch: the staged entry is there, with its view and reaction. + let s2 = host.schema(s2_value.clone()); + let migration = host.migrate(&s2, false); + assert!(migration.accepted, "{migration:?}"); + assert_eq!(host.view("b.key", "b.key").unwrap(), b"B"); + assert_eq!(host.view("a", "a").unwrap(), b"A2"); + assert!( + host.reactions() + .contains("--no-block try-restart b.service") + ); + + // A first deployment: no state, no views; staged, then switched to. + let fresh = Host::new("stage-first"); + let s2 = fresh.schema(s2_value); + let first = fresh.submission( + "first", + json!({ "channel": "secrets", "complete": true, "items": [ + { "address": "secrets/a", "op": "set" }, + { "address": "secrets/b.key", "op": "set" } + ] }), + &[("secrets/a", b"A"), ("secrets/b.key", b"B")], + ); + let plan = fresh.stage(&s2, &first); + assert!(plan.accepted && plan.snapshot.is_some(), "{plan:?}"); + assert!(fresh.view("a", "a").is_none()); + assert!(fresh.migrate(&s2, false).accepted); + assert_eq!(fresh.view("b.key", "b.key").unwrap(), b"B"); +} -- cgit v1.3.1