aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.rs4
-rw-r--r--src/migrate.rs4
-rw-r--r--src/plan.rs44
-rw-r--r--tests/common/mod.rs24
-rw-r--r--tests/migrations.rs5
-rw-r--r--tests/receptor.rs4
6 files changed, 65 insertions, 20 deletions
diff --git a/src/main.rs b/src/main.rs
index 3539d76..49b8558 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -230,13 +230,13 @@ fn run() -> Result<ExitCode> {
let systemctl = settings
.systemctl
.unwrap_or_else(|| PathBuf::from("systemctl"));
- let _lock = store.lock()?;
+ let lock = store.lock()?;
match command {
"plan" | "apply" => {
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)?
+ planned.apply(&store, &schema_bytes, &systemctl, lock)?
} else {
planned.plan
};
diff --git a/src/migrate.rs b/src/migrate.rs
index 340180f..dbbf38d 100644
--- a/src/migrate.rs
+++ b/src/migrate.rs
@@ -23,7 +23,7 @@ use serde::Serialize;
use crate::content::Content;
use crate::name::Address;
-use crate::plan::{ViewOutcome, react};
+use crate::plan::{Reacting, ViewOutcome, react};
use crate::schema::{Reactions, Schema};
use crate::scratch::Scratch;
use crate::store::{Kept, Snapshot, Store};
@@ -311,7 +311,7 @@ pub fn migrate(
let failed = if dry_run {
Vec::new()
} else {
- react(&reactions, systemctl)
+ react(&reactions, systemctl, Reacting::Migrate)
};
Ok(Migration {
accepted: refused.is_empty(),
diff --git a/src/plan.rs b/src/plan.rs
index 80e2bad..8627dd8 100644
--- a/src/plan.rs
+++ b/src/plan.rs
@@ -21,7 +21,7 @@ use crate::name::Address;
use crate::program::{self, Given, ProgramFailed, Role};
use crate::schema::{Reactions, Schema};
use crate::scratch::Scratch;
-use crate::store::{Kept, Store};
+use crate::store::{Kept, Lock, Store};
use crate::submission::{Change, Item, Submission};
use crate::view::{self, FileChange};
@@ -469,8 +469,16 @@ fn apply_items(base: &Content, items: &[Item], excluded: &BTreeSet<usize>) -> Co
impl Planned {
/// Applies the plan, if it was accepted: the new snapshot, the views that change,
- /// and their reactions, by `systemctl`.
- pub fn apply(self, store: &Store, schema_bytes: &[u8], systemctl: &Path) -> Result<Plan> {
+ /// and, with `lock` released, their reactions. Units restarted may want the unit
+ /// that runs `--migrate`, which waits for the lock: holding it while they restart
+ /// would deadlock.
+ pub fn apply(
+ self,
+ store: &Store,
+ schema_bytes: &[u8],
+ systemctl: &Path,
+ lock: Lock,
+ ) -> Result<Plan> {
let mut plan = self.plan;
if !plan.accepted {
return Ok(plan);
@@ -491,22 +499,38 @@ impl Planned {
.with_context(|| format!("view {name}"))?;
}
}
- plan.failed = react(&plan.reactions, systemctl);
+ drop(lock);
+ plan.failed = react(&plan.reactions, systemctl, Reacting::Apply);
Ok(plan)
}
}
-/// Runs `reactions`; what failed.
-pub fn react(reactions: &Reactions, systemctl: &Path) -> Vec<String> {
+/// How reactions are run.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Reacting {
+ /// After applying a submission: units are restarted or reloaded, and started if
+ /// they aren't running (they may have failed for want of their views).
+ Apply,
+ /// After a generation change, from the unit that runs `--migrate`, which units
+ /// using views are ordered after: only units that are running, without waiting
+ /// for them (`--no-block`). A unit that isn't running starts with the views in
+ /// place.
+ Migrate,
+}
+
+/// Runs `reactions` by `systemctl`, each unit once; returns what failed.
+pub fn react(reactions: &Reactions, systemctl: &Path, how: Reacting) -> Vec<String> {
let mut failed = Vec::new();
- for (verb, units) in [
- ("restart", &reactions.restart),
- ("reload", &reactions.reload),
- ] {
+ let (options, restart, reload): (&[&str], _, _) = match how {
+ Reacting::Apply => (&[], "restart", "reload-or-restart"),
+ Reacting::Migrate => (&["--no-block"], "try-restart", "try-reload-or-restart"),
+ };
+ for (verb, units) in [(restart, &reactions.restart), (reload, &reactions.reload)] {
if units.is_empty() {
continue;
}
let result = Command::new(systemctl)
+ .args(options)
.arg(verb)
.args(units)
.status()
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 751d809..f1f3f74 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -73,17 +73,33 @@ impl Host {
pub fn apply(&self, schema: &(Schema, Vec<u8>), dir: &Path) -> Plan {
let submission = Submission::load(dir).unwrap();
let store = self.store();
- let _lock = store.lock().unwrap();
+ let lock = store.lock().unwrap();
plan::plan(&schema.0, &store, &submission)
.unwrap()
- .apply(&store, &schema.1, &self.systemctl())
+ .apply(&store, &schema.1, &self.systemctl(), lock)
.unwrap()
}
- /// A systemctl that writes down what it was asked.
+ /// A systemctl that writes down what it was asked, and whether the store's lock
+ /// was free then.
pub fn systemctl(&self) -> PathBuf {
let log = self.root.join("systemctl.log");
- PathBuf::from(self.program("systemctl", &format!("echo \"$*\" >> {}", log.display())))
+ let lock = self.root.join("state/lock");
+ let locks = self.root.join("locks.log");
+ PathBuf::from(self.program(
+ "systemctl",
+ &format!(
+ "echo \"$*\" >> {log}\nif flock -n {lock} true; then echo free; else echo held; fi >> {locks}",
+ log = log.display(),
+ lock = lock.display(),
+ locks = locks.display(),
+ ),
+ ))
+ }
+
+ /// Whether the lock was free each time systemctl ran.
+ pub fn locks(&self) -> String {
+ std::fs::read_to_string(self.root.join("locks.log")).unwrap_or_default()
}
pub fn reactions(&self) -> String {
diff --git a/tests/migrations.rs b/tests/migrations.rs
index 7b78b8e..b91972c 100644
--- a/tests/migrations.rs
+++ b/tests/migrations.rs
@@ -61,7 +61,10 @@ fn a_renamed_slot_and_a_rollback() {
);
assert_eq!(host.view("retiolum.key", "retiolum.key").unwrap(), b"K");
assert!(host.view("tinc.key", "tinc.key").is_none());
- assert!(host.reactions().contains("restart tincr.service"));
+ assert!(
+ host.reactions()
+ .contains("--no-block try-restart tincr.service")
+ );
let stored = host.store().current("secrets").unwrap().unwrap();
assert_eq!(stored.id, first);
assert_eq!(stored.schema, s1.1);
diff --git a/tests/receptor.rs b/tests/receptor.rs
index 034e2a8..843eaa6 100644
--- a/tests/receptor.rs
+++ b/tests/receptor.rs
@@ -70,8 +70,10 @@ fn apply_rotate_and_nothing_to_change() {
// A unit both restarted and reloaded is restarted.
assert_eq!(
host.reactions(),
- "restart svc.service\nreload other.service\n"
+ "restart svc.service\nreload-or-restart other.service\n"
);
+ // With the lock released: a unit restarted may want the one that runs --migrate.
+ assert_eq!(host.locks(), "free\nfree\n");
// The same again changes nothing: no new snapshot, no reactions.
let again = host.submission(