the builder reads an agent's icon from its config and the controller writes the swarm's copy
This commit is contained in:
parent
07ca0b6100
commit
181ef5f421
2 changed files with 299 additions and 13 deletions
|
|
@ -317,6 +317,7 @@ async fn rebuild(world: &impl World, agent: &str, stored: Stored) -> Outcome {
|
|||
cached,
|
||||
private_users,
|
||||
nix_builds,
|
||||
..
|
||||
} => (out_path, system, cached, private_users, nix_builds),
|
||||
FromMetaReply::Failed { error_tail } => {
|
||||
tracing::warn!(%agent, %meta_rev, %error_tail, "cache miss: the rebuild failed");
|
||||
|
|
@ -684,6 +685,7 @@ mod tests {
|
|||
cached,
|
||||
private_users: Some(false),
|
||||
nix_builds: Some(NixBuilds::Host),
|
||||
icon: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1028,6 +1030,7 @@ mod tests {
|
|||
cached,
|
||||
private_users: Some(true),
|
||||
nix_builds: Some(NixBuilds::Disabled),
|
||||
icon: None,
|
||||
};
|
||||
let world = Fake::missing(&["iris"]).answering("iris", answer);
|
||||
pass_at(&world, &mut Backoff::default(), Instant::now()).await;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ use std::future::Future;
|
|||
use anyhow::{Context as _, Result};
|
||||
use hive_jobq::scheduler::Outcome;
|
||||
use swarm_wire::agent_closures::AgentClosure;
|
||||
use swarm_wire::builder::{BuildFromMeta, FromMetaReply, META_SUBJECT, REPLY_DEADLINE};
|
||||
use swarm_wire::builder::{BuildFromMeta, FromMetaReply, IconRead, META_SUBJECT, REPLY_DEADLINE};
|
||||
|
||||
use crate::agent_renewal::unix_now;
|
||||
use crate::builder::runs::{BuildOutcome, BuildRecord, Builds};
|
||||
|
|
@ -140,6 +140,8 @@ pub(crate) trait MetaBuilds {
|
|||
agent: &str,
|
||||
closure: &AgentClosure,
|
||||
) -> impl Future<Output = Result<()>> + Send;
|
||||
/// Sets `agent`'s icon in the swarm, or removes it.
|
||||
fn record_icon(&self, agent: &str, icon: &IconRead) -> impl Future<Output = Result<()>> + Send;
|
||||
}
|
||||
|
||||
impl MetaBuilds for async_nats::Client {
|
||||
|
|
@ -177,6 +179,23 @@ impl MetaBuilds for async_nats::Client {
|
|||
.map(|_| ())
|
||||
.with_context(|| format!("writing agent-closures/{agent}"))
|
||||
}
|
||||
|
||||
async fn record_icon(&self, agent: &str, icon: &IconRead) -> Result<()> {
|
||||
let store = swarm_queue_client::agent_icon::open_or_create(self)
|
||||
.await
|
||||
.context("opening the agent-icons bucket")?;
|
||||
match icon {
|
||||
IconRead::Set { svg } => store
|
||||
.put(agent, svg.clone().into_bytes().into())
|
||||
.await
|
||||
.map(|_| ())
|
||||
.with_context(|| format!("writing agent-icons/{agent}")),
|
||||
IconRead::Unset => store
|
||||
.delete(agent)
|
||||
.await
|
||||
.with_context(|| format!("deleting agent-icons/{agent}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The most of `nix`'s tail a PR comment carries, in bytes: the builder's own cap
|
||||
|
|
@ -374,7 +393,16 @@ pub(crate) async fn run(
|
|||
cached,
|
||||
private_users,
|
||||
nix_builds,
|
||||
icon,
|
||||
}) => {
|
||||
// The icon is the agent's config, so it follows the build, not the
|
||||
// cache push or the record below. A write that fails is logged and
|
||||
// leaves the deploy alone: the next build writes it again.
|
||||
if let Some(icon) = &icon
|
||||
&& let Err(e) = queue.record_icon(&agent, icon).await
|
||||
{
|
||||
tracing::warn!(%agent, error = %format!("{e:#}"), "build_meta: the agent's icon was not recorded");
|
||||
}
|
||||
let built_rev = meta_rev.clone();
|
||||
let closure = AgentClosure {
|
||||
meta_rev,
|
||||
|
|
@ -480,6 +508,8 @@ mod tests {
|
|||
record_fails: bool,
|
||||
sent: Mutex<Vec<BuildFromMeta>>,
|
||||
recorded: Mutex<Vec<(String, AgentClosure)>>,
|
||||
icon_fails: bool,
|
||||
icons: Mutex<Vec<(String, IconRead)>>,
|
||||
}
|
||||
|
||||
impl FakeQueue {
|
||||
|
|
@ -489,6 +519,8 @@ mod tests {
|
|||
record_fails: false,
|
||||
sent: Mutex::new(Vec::new()),
|
||||
recorded: Mutex::new(Vec::new()),
|
||||
icon_fails: false,
|
||||
icons: Mutex::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -511,6 +543,18 @@ mod tests {
|
|||
cached,
|
||||
private_users,
|
||||
nix_builds,
|
||||
icon: None,
|
||||
}))
|
||||
}
|
||||
|
||||
fn built_with_icon(icon: Option<IconRead>) -> Self {
|
||||
Self::answering(Ok(FromMetaReply::Built {
|
||||
out_path: "/nix/store/x-toplevel".to_owned(),
|
||||
system: "x86_64-linux".to_owned(),
|
||||
cached: true,
|
||||
private_users: Some(false),
|
||||
nix_builds: Some(NixBuilds::Host),
|
||||
icon,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
@ -532,6 +576,15 @@ mod tests {
|
|||
.push((agent.to_owned(), closure.clone()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn record_icon(&self, agent: &str, icon: &IconRead) -> Result<()> {
|
||||
anyhow::ensure!(!self.icon_fails, "the bucket refused the icon");
|
||||
self.icons
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((agent.to_owned(), icon.clone()));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_with(head: &FakeHead, queue: &FakeQueue) -> Outcome {
|
||||
|
|
@ -668,6 +721,55 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
/// The agent's icon, as the builder read it from the config, is written to the
|
||||
/// swarm: a file sets it, an unset option removes it, and an option the builder
|
||||
/// could not read leaves it as it is.
|
||||
#[tokio::test]
|
||||
async fn the_icon_the_builder_read_is_set_removed_or_left() {
|
||||
let svg = IconRead::Set {
|
||||
svg: "<svg/>".to_owned(),
|
||||
};
|
||||
for (read, written) in [
|
||||
(Some(svg.clone()), vec![("atlas".to_owned(), svg)]),
|
||||
(
|
||||
Some(IconRead::Unset),
|
||||
vec![("atlas".to_owned(), IconRead::Unset)],
|
||||
),
|
||||
(None, vec![]),
|
||||
] {
|
||||
let queue = FakeQueue::built_with_icon(read);
|
||||
assert_eq!(
|
||||
run_with(&FakeHead::reading(Ok(Some(REV))), &queue).await,
|
||||
Outcome::Done
|
||||
);
|
||||
assert_eq!(*queue.icons.lock().unwrap(), written);
|
||||
}
|
||||
}
|
||||
|
||||
/// The icon is not what the deploy waits on: a write the queue refuses
|
||||
/// leaves the build green.
|
||||
#[tokio::test]
|
||||
async fn an_icon_that_cannot_be_written_does_not_fail_the_build() {
|
||||
let queue = FakeQueue {
|
||||
icon_fails: true,
|
||||
..FakeQueue::built_with_icon(Some(IconRead::Unset))
|
||||
};
|
||||
assert_eq!(
|
||||
run_with(&FakeHead::reading(Ok(Some(REV))), &queue).await,
|
||||
Outcome::Done
|
||||
);
|
||||
assert!(queue.icons.lock().unwrap().is_empty());
|
||||
assert_eq!(queue.recorded.lock().unwrap().len(), 1, "still recorded");
|
||||
}
|
||||
|
||||
/// A build that fails writes no icon.
|
||||
#[tokio::test]
|
||||
async fn a_failed_build_writes_no_icon() {
|
||||
let queue = nix_failed("error: x");
|
||||
let _ = run_with(&FakeHead::reading(Ok(Some(REV))), &queue).await;
|
||||
assert!(queue.icons.lock().unwrap().is_empty());
|
||||
}
|
||||
|
||||
/// Any `nix` failure, a timeout included, fails the node at once with what
|
||||
/// the builder said: not a no-verdict failure, and nothing is recorded. The
|
||||
/// green test above is the control that a record is written when the build
|
||||
|
|
|
|||
Loading…
Reference in a new issue