aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs117
1 files changed, 109 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index ca7d0df..8d960f1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -99,7 +99,14 @@ const COMMANDS: &[(&str, &str)] = &[
"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"),
+ (
+ "status",
+ "Show the channels' snapshots and the views, what waits and what was refused",
+ ),
+ (
+ "forget",
+ "Remove the waiting or refused submission named PATH, from the incoming or refused directory",
+ ),
(
"materialize",
"Put every view in place from the snapshots (at boot, after a switch)",
@@ -220,7 +227,7 @@ fn run() -> Result<ExitCode> {
let path = matches.get_one::<String>("path").map(PathBuf::from);
let out = matches.get_one::<String>("out").map(PathBuf::from);
// An option that isn't for the command is an error.
- let takes_path = matches!(command, "plan" | "apply" | "stage" | "pack");
+ let takes_path = matches!(command, "plan" | "apply" | "stage" | "pack" | "forget");
match (&path, takes_path) {
(None, true) => bail!("--{command} needs a PATH"),
(Some(path), false) => bail!("--{command} takes no PATH ({})", path.display()),
@@ -285,6 +292,15 @@ fn run() -> Result<ExitCode> {
Some(bwrap) => Some(PathBuf::from(bwrap)),
None => Some(PathBuf::from("bwrap")),
});
+ if command == "forget" {
+ let _lock = store.lock()?;
+ let name = path.as_deref().unwrap().to_string_lossy().into_owned();
+ let removed = store.forget(&name)?;
+ for path in &removed {
+ println!("removed {}", path.display());
+ }
+ return Ok(ExitCode::SUCCESS);
+ }
let schema_path = settings
.schema
.unwrap_or_else(|| PathBuf::from("/etc/enstate/schema.json"));
@@ -355,13 +371,18 @@ fn run() -> Result<ExitCode> {
})
}
"status" => {
- let status = status(&schema, &store)?;
+ let status = status(&schema, &schema_bytes, &store)?;
if json {
println!("{}", serde_json::to_string_pretty(&status)?);
} else {
print_status(&status)?;
}
- Ok(ExitCode::SUCCESS)
+ // A deployment that runs it notices what needs attention.
+ Ok(if status.attention {
+ ExitCode::from(1)
+ } else {
+ ExitCode::SUCCESS
+ })
}
_ => unreachable!(),
}
@@ -504,6 +525,16 @@ fn print_views(out: &mut impl Write, views: &[ViewOutcome], only_changes: bool)
struct Status {
channels: Vec<ChannelStatus>,
views: Vec<ViewStatus>,
+ /// Submissions waiting in the incoming directory, each with whether the running
+ /// generation would refuse it, and why.
+ incoming: Vec<IncomingStatus>,
+ /// Submissions the switch refused, with why.
+ refused: Vec<enstate::store::Refusal>,
+ /// Where they are, to look at or remove.
+ refused_dir: PathBuf,
+ /// Whether something needs attention: a required slot unfilled, a refused
+ /// submission.
+ attention: bool,
}
#[derive(serde::Serialize)]
@@ -563,10 +594,48 @@ fn print_status(status: &Status) -> Result<()> {
};
writeln!(out, "view {}: {state}", view.view)?;
}
+ for incoming in &status.incoming {
+ if !incoming.refused {
+ writeln!(
+ out,
+ "incoming {}: waiting for the next switch or --migrate",
+ incoming.name
+ )?;
+ continue;
+ }
+ writeln!(
+ out,
+ "{} {}: the running generation would refuse it; while it waits, a switch \
+ to a generation that refuses it too is refused (enstate --forget {} removes it):",
+ style.paint("31", "incoming"),
+ incoming.name,
+ incoming.name
+ )?;
+ for reason in &incoming.reasons {
+ writeln!(out, " {reason}")?;
+ }
+ }
+ for refusal in &status.refused {
+ let at = if refusal.at.is_empty() {
+ String::new()
+ } else {
+ format!(" at {}", refusal.at)
+ };
+ writeln!(
+ out,
+ "{} {}{at}, in {}:",
+ style.paint("31", "refused"),
+ refusal.name,
+ status.refused_dir.join(&refusal.name).display()
+ )?;
+ for reason in &refusal.reasons {
+ writeln!(out, " {reason}")?;
+ }
+ }
Ok(())
}
-fn status(schema: &Schema, store: &Store) -> Result<Status> {
+fn status(schema: &Schema, schema_bytes: &[u8], store: &Store) -> Result<Status> {
let mut channels = Vec::new();
for (channel, seen) in enstate::migrate::seen_all(schema, store)? {
let slots = schema
@@ -604,7 +673,29 @@ fn status(schema: &Schema, store: &Store) -> Result<Status> {
installed: store.installed_view(view).is_some(),
})
.collect();
- Ok(Status { channels, views })
+ let refused = store.refused()?;
+ // What waits, as the running generation would take it: one it would refuse
+ // refuses every switch (the check plans what waits) until it goes.
+ let incoming: Vec<IncomingStatus> =
+ enstate::migrate::plan_incoming(schema, schema_bytes, store)?
+ .into_iter()
+ .map(|outcome| IncomingStatus {
+ refused: !outcome.accepted(),
+ reasons: outcome.reasons(),
+ name: outcome.name,
+ })
+ .collect();
+ let attention = !refused.is_empty()
+ || incoming.iter().any(|incoming| incoming.refused)
+ || channels.iter().any(|channel| !channel.missing.is_empty());
+ Ok(Status {
+ channels,
+ views,
+ incoming,
+ refused_dir: store.refused_dir(),
+ refused,
+ attention,
+ })
}
fn print_migration(migration: &enstate::migrate::Migration) -> Result<()> {
@@ -661,9 +752,10 @@ fn print_migration(migration: &enstate::migrate::Migration) -> Result<()> {
fn uses(command: &str, setting: &str) -> bool {
match setting {
// Where the snapshots and views are: everything but making a submission.
- "state-dir" | "run-dir" => command != "pack",
+ "state-dir" => command != "pack",
+ "run-dir" => !matches!(command, "pack" | "forget"),
// The schema to plan against, or, for --pack, to check against.
- "schema" => true,
+ "schema" => command != "forget",
// Reactions run.
"systemctl" => matches!(command, "apply" | "migrate"),
"unit" => command == "apply",
@@ -697,3 +789,12 @@ fn print_report(report: &enstate::pack::Report) -> Result<()> {
)?;
Ok(())
}
+
+#[derive(serde::Serialize)]
+struct IncomingStatus {
+ name: String,
+ /// Whether the running generation would refuse it.
+ refused: bool,
+ #[serde(skip_serializing_if = "Vec::is_empty")]
+ reasons: Vec<String>,
+}