swarm: set an agent's config repo avatar from its icon
The agent module exposes the PNG nix renders from the agent's icon as the read-only option services.hyperhive.agent.iconPng, which the forge and matrix avatar syncs now share. When the builder reads an icon it builds that one attribute with nix build and sends the file, base64, next to the SVG in the meta build reply, so the builder runs no renderer of its own. After recording the icon, the controller sets the avatar of agent-configs/<agent> from the PNG as the forge admin, removes the avatar when the icon is removed, and leaves it alone when the builder sent no PNG. A marker per repo holds the image's fingerprint, so a build whose icon did not change makes no forge call; a forge that refuses the avatar does not fail the build. An old builder sends no PNG, so it changes nothing.
This commit is contained in:
parent
8a0615ccfc
commit
1a31a4cd5a
1 changed files with 499 additions and 25 deletions
|
|
@ -80,6 +80,13 @@ pub(crate) trait MetaRepo {
|
|||
cleared: &str,
|
||||
resolved: &str,
|
||||
) -> impl Future<Output = Result<usize>> + Send;
|
||||
/// Make the avatar of `agent`'s config repo the PNG `png`, or remove it
|
||||
/// for `None`.
|
||||
fn set_repo_avatar(
|
||||
&self,
|
||||
agent: &str,
|
||||
png: Option<&[u8]>,
|
||||
) -> impl Future<Output = Result<()>> + Send;
|
||||
}
|
||||
|
||||
impl MetaRepo for forge::Client {
|
||||
|
|
@ -127,6 +134,14 @@ impl MetaRepo for forge::Client {
|
|||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn set_repo_avatar(&self, agent: &str, png: Option<&[u8]>) -> Result<()> {
|
||||
crate::jobq::forge_node(
|
||||
"build_meta",
|
||||
self.sync_repo_avatar(forge::CONFIG_ORG, agent, png),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a build is sent and its closure recorded: the queue in production, a
|
||||
|
|
@ -185,7 +200,7 @@ impl MetaBuilds for async_nats::Client {
|
|||
.await
|
||||
.context("opening the agent-icons bucket")?;
|
||||
match icon {
|
||||
IconRead::Set { svg } => store
|
||||
IconRead::Set { svg, .. } => store
|
||||
.put(agent, svg.clone().into_bytes().into())
|
||||
.await
|
||||
.map(|_| ())
|
||||
|
|
@ -198,6 +213,30 @@ impl MetaBuilds for async_nats::Client {
|
|||
}
|
||||
}
|
||||
|
||||
/// Follow the agent's icon in the avatar of its config repo: a rendered icon
|
||||
/// sets it, an icon that was removed removes it, and an icon the builder could
|
||||
/// not render leaves it as it is. Like the icon above, a failure is logged and
|
||||
/// leaves the deploy alone: the next build tries again.
|
||||
async fn set_repo_avatar(head: &impl MetaRepo, agent: &str, icon: &IconRead) {
|
||||
use base64::Engine as _;
|
||||
let png = match icon {
|
||||
IconRead::Set { png: Some(png), .. } => {
|
||||
match base64::engine::general_purpose::STANDARD.decode(png) {
|
||||
Ok(bytes) => Some(bytes),
|
||||
Err(e) => {
|
||||
tracing::warn!(%agent, error = %e, "build_meta: the builder's icon rendering is not base64; the repo avatar is left as it is");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
IconRead::Set { png: None, .. } => return,
|
||||
IconRead::Unset => None,
|
||||
};
|
||||
if let Err(e) = head.set_repo_avatar(agent, png.as_deref()).await {
|
||||
tracing::warn!(%agent, error = %format!("{e:#}"), "build_meta: the agent's repo avatar was not updated");
|
||||
}
|
||||
}
|
||||
|
||||
/// The most of `nix`'s tail a PR comment carries, in bytes: the builder's own cap
|
||||
/// on the tail it sends.
|
||||
const COMMENT_TAIL_BYTES: usize = 4000;
|
||||
|
|
@ -398,10 +437,11 @@ pub(crate) async fn run(
|
|||
// 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");
|
||||
if let Some(icon) = &icon {
|
||||
if let Err(e) = queue.record_icon(&agent, icon).await {
|
||||
tracing::warn!(%agent, error = %format!("{e:#}"), "build_meta: the agent's icon was not recorded");
|
||||
}
|
||||
set_repo_avatar(head, &agent, icon).await;
|
||||
}
|
||||
let built_rev = meta_rev.clone();
|
||||
let closure = AgentClosure {
|
||||
|
|
@ -448,6 +488,10 @@ mod tests {
|
|||
grant_fails: bool,
|
||||
/// The forge a PR comment goes to; `None` panics on a comment.
|
||||
comments: Option<std::sync::Arc<forge::Client>>,
|
||||
/// The repo avatars asked for, `None` being a removal; the forge
|
||||
/// refusing them is `avatar_fails`.
|
||||
avatars: Mutex<Vec<(String, Option<Vec<u8>>)>>,
|
||||
avatar_fails: bool,
|
||||
}
|
||||
|
||||
impl FakeHead {
|
||||
|
|
@ -456,6 +500,8 @@ mod tests {
|
|||
head,
|
||||
grant_fails: false,
|
||||
comments: None,
|
||||
avatars: Mutex::new(Vec::new()),
|
||||
avatar_fails: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -499,6 +545,15 @@ mod tests {
|
|||
.clear_marker(forge::CONFIG_ORG, agent, pr, marker, cleared, resolved)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn set_repo_avatar(&self, agent: &str, png: Option<&[u8]>) -> Result<()> {
|
||||
anyhow::ensure!(!self.avatar_fails, "the forge refused the avatar");
|
||||
self.avatars
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((agent.to_owned(), png.map(<[u8]>::to_vec)));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A queue that answers every build with `reply`, and keeps what it was
|
||||
|
|
@ -728,6 +783,7 @@ mod tests {
|
|||
async fn the_icon_the_builder_read_is_set_removed_or_left() {
|
||||
let svg = IconRead::Set {
|
||||
svg: "<svg/>".to_owned(),
|
||||
png: None,
|
||||
};
|
||||
for (read, written) in [
|
||||
(Some(svg.clone()), vec![("atlas".to_owned(), svg)]),
|
||||
|
|
@ -746,6 +802,57 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
/// The repo avatar follows the icon: a rendered icon sets it, a removed one
|
||||
/// removes it, and one the builder could not render leaves it alone.
|
||||
#[tokio::test]
|
||||
async fn the_repo_avatar_follows_the_icon() {
|
||||
use base64::Engine as _;
|
||||
let png = base64::engine::general_purpose::STANDARD.encode("PNGDATA");
|
||||
for (read, asked) in [
|
||||
(
|
||||
Some(IconRead::Set {
|
||||
svg: "<svg/>".to_owned(),
|
||||
png: Some(png),
|
||||
}),
|
||||
vec![("atlas".to_owned(), Some(b"PNGDATA".to_vec()))],
|
||||
),
|
||||
(Some(IconRead::Unset), vec![("atlas".to_owned(), None)]),
|
||||
(
|
||||
Some(IconRead::Set {
|
||||
svg: "<svg/>".to_owned(),
|
||||
png: None,
|
||||
}),
|
||||
vec![],
|
||||
),
|
||||
(
|
||||
Some(IconRead::Set {
|
||||
svg: "<svg/>".to_owned(),
|
||||
png: Some("not base64!".to_owned()),
|
||||
}),
|
||||
vec![],
|
||||
),
|
||||
(None, vec![]),
|
||||
] {
|
||||
let head = FakeHead::reading(Ok(Some(REV)));
|
||||
let queue = FakeQueue::built_with_icon(read);
|
||||
assert_eq!(run_with(&head, &queue).await, Outcome::Done);
|
||||
assert_eq!(*head.avatars.lock().unwrap(), asked);
|
||||
}
|
||||
}
|
||||
|
||||
/// The avatar is not what the deploy waits on either: a forge that refuses
|
||||
/// it leaves the build green and the closure recorded.
|
||||
#[tokio::test]
|
||||
async fn a_repo_avatar_the_forge_refuses_does_not_fail_the_build() {
|
||||
let head = FakeHead {
|
||||
avatar_fails: true,
|
||||
..FakeHead::reading(Ok(Some(REV)))
|
||||
};
|
||||
let queue = FakeQueue::built_with_icon(Some(IconRead::Unset));
|
||||
assert_eq!(run_with(&head, &queue).await, Outcome::Done);
|
||||
assert_eq!(queue.recorded.lock().unwrap().len(), 1, "still recorded");
|
||||
}
|
||||
|
||||
/// The icon is not what the deploy waits on: a write the queue refuses
|
||||
/// leaves the build green.
|
||||
#[tokio::test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue