deploy the closure the swarm declares, read no forge head, drop the update badge
This commit is contained in:
parent
279428a66f
commit
47c2acb640
1 changed files with 647 additions and 2001 deletions
|
|
@ -1,7 +1,10 @@
|
|||
//! The closure the swarm built for an agent, as a deploy reads its record
|
||||
//! before downloading it.
|
||||
//! The closure the swarm declares for an agent: its record in `agent-closures`
|
||||
//! read before a deploy downloads it, and the marker of the store path the
|
||||
//! hive last applied, which a changed declaration is compared with.
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use swarm_wire::agent_closures::AgentClosure;
|
||||
|
||||
use super::probe;
|
||||
|
|
@ -17,3 +20,162 @@ pub async fn record(agent: &str) -> Result<Option<AgentClosure>> {
|
|||
.ok_or_else(|| anyhow!("this hive has no connection to the swarm queue"))?;
|
||||
probe::read_record(&client, agent).await
|
||||
}
|
||||
|
||||
/// Record that `agent` runs the closure at `store_path`.
|
||||
///
|
||||
/// # Errors
|
||||
/// The marker could not be written.
|
||||
pub fn mark_deployed(agent: &str, store_path: &str) -> Result<()> {
|
||||
mark_deployed_in(&crate::paths::applied_closure_marker(agent), store_path)
|
||||
}
|
||||
|
||||
/// The store path `agent` was last swapped to; `None` when the hive has no
|
||||
/// record of one: it never applied a closure, or last did before the marker
|
||||
/// existed.
|
||||
#[must_use]
|
||||
pub fn deployed(agent: &str) -> Option<String> {
|
||||
deployed_in(&crate::paths::applied_closure_marker(agent))
|
||||
}
|
||||
|
||||
/// Written whole to a temp file beside the marker and renamed over it, so a
|
||||
/// reader sees the old marker or the new one.
|
||||
fn mark_deployed_in(marker: &Path, store_path: &str) -> Result<()> {
|
||||
let tmp = marker.with_extension("tmp");
|
||||
if let Some(dir) = marker.parent() {
|
||||
std::fs::create_dir_all(dir).with_context(|| format!("create {}", dir.display()))?;
|
||||
}
|
||||
std::fs::write(&tmp, store_path).with_context(|| format!("write {}", tmp.display()))?;
|
||||
std::fs::rename(&tmp, marker)
|
||||
.with_context(|| format!("rename {} to {}", tmp.display(), marker.display()))
|
||||
}
|
||||
|
||||
fn deployed_in(marker: &Path) -> Option<String> {
|
||||
let stored = std::fs::read_to_string(marker).ok()?;
|
||||
let stored = stored.trim();
|
||||
(!stored.is_empty()).then(|| stored.to_owned())
|
||||
}
|
||||
|
||||
/// Whether the swarm's declared closure is one the hive should now deploy: the
|
||||
/// builder pushed it and it differs from what `deployed` says runs.
|
||||
///
|
||||
/// An agent with no `deployed` marker is not pending, since the hive cannot
|
||||
/// tell what it runs: every agent starts that way when the hive first reads
|
||||
/// this marker, and deploying all of them at once is worse than waiting for the
|
||||
/// swarm's next deploy request, which writes the marker.
|
||||
#[must_use]
|
||||
pub fn pending(record: &AgentClosure, deployed: Option<&str>) -> bool {
|
||||
record.cached && deployed.is_some_and(|deployed| deployed != record.store_path)
|
||||
}
|
||||
|
||||
/// Whether `deployed` is the closure `record` declares, so deploying it again
|
||||
/// would change nothing.
|
||||
#[must_use]
|
||||
pub fn up_to_date(record: &AgentClosure, deployed: Option<&str>) -> bool {
|
||||
deployed == Some(record.store_path.as_str())
|
||||
}
|
||||
|
||||
/// The store path the swarm declares for `agent` when it is [`pending`]. A record
|
||||
/// that cannot be read, or none, is logged and counts as nothing pending: the
|
||||
/// hive then deploys nothing it was not told to.
|
||||
pub async fn pending_path(agent: &str) -> Option<String> {
|
||||
match record(agent).await {
|
||||
Ok(Some(record)) => {
|
||||
pending(&record, deployed(agent).as_deref()).then_some(record.store_path)
|
||||
}
|
||||
Ok(None) => None,
|
||||
Err(e) => {
|
||||
tracing::warn!(%agent, error = %format!("{e:#}"), "cannot read the agent's declared closure");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the closure the swarm declares for `agent` is the one it runs; false
|
||||
/// when that cannot be told, so a deploy request goes ahead.
|
||||
pub async fn is_up_to_date(agent: &str) -> bool {
|
||||
match record(agent).await {
|
||||
Ok(Some(record)) => up_to_date(&record, deployed(agent).as_deref()),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const DECLARED: &str = "/nix/store/0123456789abcdfghijklmnpqrsvwxyz-nixos-system-alice";
|
||||
const RUNNING: &str = "/nix/store/zyxwvsrqpnmlkjihgfdcbadfhijklmn-nixos-system-alice";
|
||||
|
||||
fn declared(cached: bool) -> AgentClosure {
|
||||
AgentClosure {
|
||||
meta_rev: "a".repeat(40),
|
||||
store_path: DECLARED.to_owned(),
|
||||
system: "x86_64-linux".to_owned(),
|
||||
built_unix: 1,
|
||||
cached,
|
||||
private_users: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_declaration_other_than_the_running_closure_is_pending() {
|
||||
assert!(pending(&declared(true), Some(RUNNING)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_running_closure_is_not_pending() {
|
||||
assert!(!pending(&declared(true), Some(DECLARED)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_closure_the_builder_did_not_push_is_not_pending() {
|
||||
assert!(!pending(&declared(false), Some(RUNNING)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_declared_closure_is_up_to_date() {
|
||||
assert!(up_to_date(&declared(true), Some(DECLARED)));
|
||||
assert!(!up_to_date(&declared(true), Some(RUNNING)));
|
||||
assert!(!up_to_date(&declared(true), None), "unknown is deployed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_agent_with_no_marker_is_not_pending() {
|
||||
assert!(!pending(&declared(true), None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_marker_holds_the_store_path_and_is_replaced_whole() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let marker = dir.path().join("applied").join(".alice.closure");
|
||||
assert_eq!(deployed_in(&marker), None, "no marker yet");
|
||||
mark_deployed_in(&marker, RUNNING).unwrap();
|
||||
assert_eq!(deployed_in(&marker).as_deref(), Some(RUNNING));
|
||||
mark_deployed_in(&marker, DECLARED).unwrap();
|
||||
assert_eq!(deployed_in(&marker).as_deref(), Some(DECLARED));
|
||||
}
|
||||
|
||||
/// A hive that ran the previous marker format has a config commit in its
|
||||
/// old files and none at the new path: the agent is not pending, and the
|
||||
/// old files are not read or touched.
|
||||
#[test]
|
||||
fn markers_in_the_old_format_are_neither_read_nor_removed() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let old = dir.path().join(".alice.config-rev");
|
||||
std::fs::write(&old, "1111111111111111111111111111111111111111").unwrap();
|
||||
let marker = dir.path().join(".alice.closure");
|
||||
assert_eq!(deployed_in(&marker), None);
|
||||
assert!(!pending(&declared(true), deployed_in(&marker).as_deref()));
|
||||
assert!(old.exists());
|
||||
mark_deployed_in(&marker, DECLARED).unwrap();
|
||||
assert!(old.exists(), "a successful deploy leaves old markers alone");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_marker_is_unknown() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let marker = dir.path().join(".alice.closure");
|
||||
std::fs::write(&marker, "\n").unwrap();
|
||||
assert_eq!(deployed_in(&marker), None);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue