aboutsummaryrefslogtreecommitdiffstats
path: root/src/plan.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/plan.rs')
-rw-r--r--src/plan.rs104
1 files changed, 34 insertions, 70 deletions
diff --git a/src/plan.rs b/src/plan.rs
index 952f3aa..80e2bad 100644
--- a/src/plan.rs
+++ b/src/plan.rs
@@ -16,11 +16,12 @@ use serde::Serialize;
use crate::content::{self, Content};
use crate::media;
+use crate::migrate;
use crate::name::Address;
use crate::program::{self, Given, ProgramFailed, Role};
use crate::schema::{Reactions, Schema};
use crate::scratch::Scratch;
-use crate::store::Store;
+use crate::store::{Kept, Store};
use crate::submission::{Change, Item, Submission};
use crate::view::{self, FileChange};
@@ -72,6 +73,7 @@ pub struct ViewOutcome {
pub struct Planned {
pub plan: Plan,
content: Content,
+ kept: Kept,
content_changes: bool,
rendered: Vec<(String, Option<Scratch>, bool)>,
}
@@ -81,6 +83,7 @@ fn refused(plan: Plan) -> Result<Planned> {
Ok(Planned {
plan,
content: Content::new(),
+ kept: Kept::new(),
content_changes: false,
rendered: Vec::new(),
})
@@ -134,8 +137,11 @@ pub fn plan(schema: &Schema, store: &Store, submission: &Submission) -> Result<P
}
}
let empty = Content::new();
+ // What this generation sees of the snapshot, which may have been accepted under
+ // another schema.
+ let seen = migrate::seen(schema, channel, current.as_ref())?;
let base_content = match (&current, submission.complete) {
- (Some(snapshot), false) => &snapshot.content,
+ (Some(_), false) => &seen.content,
_ => &empty,
};
// Other channels, for validators and views given their slots.
@@ -156,10 +162,7 @@ pub fn plan(schema: &Schema, store: &Store, submission: &Submission) -> Result<P
}
}
for other in other_channels {
- let content = store
- .current(other)?
- .map(|snapshot| snapshot.content)
- .unwrap_or_default();
+ let content = migrate::seen(schema, other, store.current(other)?.as_ref())?.content;
others.insert(other.to_owned(), content);
}
@@ -310,13 +313,31 @@ pub fn plan(schema: &Schema, store: &Store, submission: &Submission) -> Result<P
}
}
};
- for slot in candidate.keys() {
- if !channel_spec.slots.contains_key(slot) {
+ // What no slot wants: kept, unless the submission is complete.
+ let kept = if submission.complete {
+ for slot in seen.kept.keys() {
plan.warnings.push(format!(
- "kept, but in no slot of the schema: {channel}/{slot}"
+ "goes, as the submission is complete: {channel}/{slot}, which was kept"
));
}
- }
+ Kept::new()
+ } else {
+ let mut kept = seen.kept.clone();
+ kept.retain(|slot, (media_type, _)| {
+ let superseded = candidate.contains_key(slot);
+ if superseded {
+ plan.warnings.push(format!(
+ "goes, as {channel}/{slot} is filled anew: the entry of type {media_type} kept from before"
+ ));
+ }
+ !superseded
+ });
+ for slot in kept.keys() {
+ plan.warnings
+ .push(format!("kept, in no slot of the schema: {channel}/{slot}"));
+ }
+ kept
+ };
// Views.
let mut rendered_views = Vec::new();
@@ -370,7 +391,7 @@ pub fn plan(schema: &Schema, store: &Store, submission: &Submission) -> Result<P
restart: restart.into_iter().collect(),
reload: reload.into_iter().collect(),
};
- let content_changes = current.as_ref().map(|snapshot| &snapshot.content) != Some(&candidate);
+ let content_changes = current.is_none() || candidate != seen.content || kept != seen.kept;
plan.changes = content_changes
|| plan
.views
@@ -380,6 +401,7 @@ pub fn plan(schema: &Schema, store: &Store, submission: &Submission) -> Result<P
Ok(Planned {
plan,
content: candidate,
+ kept,
content_changes,
rendered: rendered_views,
})
@@ -457,6 +479,7 @@ impl Planned {
plan.snapshot = Some(store.commit(
&plan.channel,
&self.content,
+ &self.kept,
plan.current.as_deref(),
schema_bytes,
)?);
@@ -505,62 +528,3 @@ pub fn react(reactions: &Reactions, systemctl: &Path) -> Vec<String> {
}
failed
}
-
-/// Renders every view of `schema` from the current snapshots, puts those that differ
-/// from what is in place there, and takes away views the schema doesn't have: at boot,
-/// and after a generation change. Runs no reactions. Returns how each view changed.
-pub fn materialize(schema: &Schema, store: &Store) -> Result<Vec<ViewOutcome>> {
- let mut contents: BTreeMap<String, Content> = BTreeMap::new();
- for channel in schema.channels.keys() {
- if let Some(snapshot) = store.current(channel)? {
- contents.insert(channel.clone(), snapshot.content);
- }
- }
- let entries: view::Entries = contents
- .iter()
- .map(|(name, content)| (name.as_str(), content))
- .collect();
- let mut outcomes = Vec::new();
- for (name, view_spec) in schema.views_of(None) {
- let secret = schema.view_is_secret(view_spec);
- let rendered = view::render(schema, name, view_spec, &entries, &store.scratch())
- .with_context(|| format!("view {name}"))?;
- let installed = store.installed_view(name);
- let (change, files) = view::compare(
- installed.as_deref(),
- rendered.as_ref().map(Scratch::path),
- secret,
- )?;
- if change != view::Change::Unchanged {
- store.install_view(name, rendered, secret)?;
- }
- outcomes.push(ViewOutcome {
- name: name.clone(),
- secret,
- change,
- files,
- });
- }
- let views = store.run.join("views");
- if let Ok(entries) = std::fs::read_dir(&views) {
- for entry in entries {
- let name = entry?.file_name().to_string_lossy().into_owned();
- if name == ".generations"
- || name.ends_with(".new-link")
- || schema.views.contains_key(&name)
- {
- continue;
- }
- if store.installed_view(&name).is_some() {
- store.install_view(&name, None, false)?;
- outcomes.push(ViewOutcome {
- name,
- secret: false,
- change: view::Change::Removed,
- files: Vec::new(),
- });
- }
- }
- }
- Ok(outcomes)
-}