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
|
|
@ -247,6 +247,40 @@ impl Builder {
|
|||
}
|
||||
}
|
||||
|
||||
/// The agent's icon as the PNG the forge takes as an avatar, base64: the
|
||||
/// image nix itself renders from the icon (`services.hyperhive.agent.iconPng`),
|
||||
/// built here so the builder runs no renderer of its own. `None` when it
|
||||
/// did not build or read back: the reply then carries the SVG alone, which
|
||||
/// leaves the avatar as it is.
|
||||
async fn icon_png(&self, job: &BuildFromMeta, token: &str) -> Option<String> {
|
||||
use base64::Engine as _;
|
||||
let mut cmd = self.nix_command(token);
|
||||
cmd.args(BUILD_FLAGS)
|
||||
.arg(format!("{}.{ICON_PNG_OPTION}", self.meta_config(job)));
|
||||
let path = match run_raw(cmd, self.deadline).await {
|
||||
Ok((status, out, _)) if status.success() => String::from_utf8_lossy(&out)
|
||||
.lines()
|
||||
.next()?
|
||||
.trim()
|
||||
.to_owned(),
|
||||
Ok((status, _, err)) => {
|
||||
tracing::warn!(agent = %job.agent, error = %tail(&String::from_utf8_lossy(&err), status), "the agent's icon PNG did not build; the reply carries the SVG alone");
|
||||
return None;
|
||||
}
|
||||
Err(why) => {
|
||||
tracing::warn!(agent = %job.agent, %why, "the agent's icon PNG did not build; the reply carries the SVG alone");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
match read_png(std::path::Path::new(&path)).await {
|
||||
Ok(png) => Some(base64::engine::general_purpose::STANDARD.encode(png)),
|
||||
Err(why) => {
|
||||
tracing::warn!(agent = %job.agent, %why, "the agent's icon PNG was not read; the reply carries the SVG alone");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The agent's `services.hyperhive.agent.icon`: the text of the SVG file the
|
||||
/// option names, read by the eval itself so the builder needs no store
|
||||
/// path. An option that is null, or that the agent's module does not declare,
|
||||
|
|
@ -257,7 +291,13 @@ impl Builder {
|
|||
.eval_option_applied(job, token, ICON_OPTION, Some(ICON_APPLY))
|
||||
.await
|
||||
{
|
||||
Eval::Value(out) => parse_icon(&out),
|
||||
Eval::Value(out) => match parse_icon(&out)? {
|
||||
IconRead::Set { svg, .. } => {
|
||||
let png = self.icon_png(job, token).await;
|
||||
Some(IconRead::Set { svg, png })
|
||||
}
|
||||
unset @ IconRead::Unset => Some(unset),
|
||||
},
|
||||
Eval::Missing => Some(IconRead::Unset),
|
||||
Eval::Unknown => None,
|
||||
}
|
||||
|
|
@ -314,6 +354,14 @@ const ICON_APPLY: &str = "p: if p == null then null else builtins.readFile p";
|
|||
/// and an icon is a small drawing. A larger file is left out as unread.
|
||||
const ICON_MAX_BYTES: usize = 256 * 1024;
|
||||
|
||||
/// The agent option holding the PNG nix renders from its icon: a derivation, or
|
||||
/// null when there is no icon.
|
||||
const ICON_PNG_OPTION: &str = "services.hyperhive.agent.iconPng";
|
||||
|
||||
/// The largest rendering the reply carries, in bytes. An icon at that size is
|
||||
/// far below it; a drawing that is not is left out.
|
||||
const ICON_PNG_MAX_BYTES: usize = 256 * 1024;
|
||||
|
||||
/// What `nix eval --json` of an agent option came to.
|
||||
enum Eval {
|
||||
/// It printed a value.
|
||||
|
|
@ -353,10 +401,29 @@ fn parse_icon(out: &[u8]) -> Option<IconRead> {
|
|||
);
|
||||
None
|
||||
}
|
||||
Some(svg) => Some(IconRead::Set { svg }),
|
||||
Some(svg) => Some(IconRead::Set { svg, png: None }),
|
||||
}
|
||||
}
|
||||
|
||||
/// The PNG file at `path`, which `nix build` printed: not empty, and at most
|
||||
/// [`ICON_PNG_MAX_BYTES`].
|
||||
async fn read_png(path: &std::path::Path) -> Result<Vec<u8>, String> {
|
||||
let png = tokio::fs::read(path)
|
||||
.await
|
||||
.map_err(|e| format!("reading {}: {e}", path.display()))?;
|
||||
if png.is_empty() {
|
||||
return Err(format!("{} is empty", path.display()));
|
||||
}
|
||||
if png.len() > ICON_PNG_MAX_BYTES {
|
||||
return Err(format!(
|
||||
"{} is {} bytes, over the {ICON_PNG_MAX_BYTES} the reply carries",
|
||||
path.display(),
|
||||
png.len()
|
||||
));
|
||||
}
|
||||
Ok(png)
|
||||
}
|
||||
|
||||
/// `nix eval --json` of a [`NixBuilds`]: the spelling, or `None` when the
|
||||
/// output is not a string.
|
||||
fn parse_nix_builds(out: &[u8]) -> Option<NixBuilds> {
|
||||
|
|
@ -730,6 +797,7 @@ mod tests {
|
|||
"echo '\"<svg/>\"'",
|
||||
Some(IconRead::Set {
|
||||
svg: "<svg/>".to_owned(),
|
||||
png: None,
|
||||
}),
|
||||
),
|
||||
("echo null", Some(IconRead::Unset)),
|
||||
|
|
@ -756,6 +824,90 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
/// A `nix` that answers the icon option with an SVG and the PNG option with
|
||||
/// what `png` makes of a file path, a build for anything else with a store path.
|
||||
fn icon_nix(tag: &str, png: &str) -> PathBuf {
|
||||
fake_nix(
|
||||
tag,
|
||||
&format!(
|
||||
"for a in \"$@\"; do last=$a; done; case \"$last\" in *agent.iconPng) {png};; *agent.icon) echo '\"<svg/>\"';; *) if [ \"$1\" = eval ]; then echo false; else echo /nix/store/x; fi;; esac\n"
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// The PNG nix renders goes in the reply, base64, beside the SVG; one that
|
||||
/// did not build, is missing, empty or too large leaves the PNG out and still
|
||||
/// sends the SVG.
|
||||
#[tokio::test]
|
||||
async fn an_icon_png_nix_built_goes_in_the_reply() {
|
||||
use base64::Engine as _;
|
||||
let dir = std::env::temp_dir().join(format!("icon-png-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).expect("scratch dir");
|
||||
let ok = dir.join("ok.png");
|
||||
std::fs::write(&ok, "PNGDATA").expect("write png");
|
||||
let empty = dir.join("empty.png");
|
||||
std::fs::write(&empty, "").expect("write png");
|
||||
let big = dir.join("big.png");
|
||||
std::fs::write(&big, vec![0u8; ICON_PNG_MAX_BYTES + 1]).expect("write png");
|
||||
for (i, (answer, want)) in [
|
||||
(
|
||||
format!("echo {}", ok.display()),
|
||||
Some(base64::engine::general_purpose::STANDARD.encode("PNGDATA")),
|
||||
),
|
||||
("echo error: boom >&2; exit 1".to_owned(), None),
|
||||
(format!("echo {}", dir.join("missing.png").display()), None),
|
||||
(format!("echo {}", empty.display()), None),
|
||||
(format!("echo {}", big.display()), None),
|
||||
("true".to_owned(), None),
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let b = Builder {
|
||||
nix: icon_nix(&format!("png-{i}"), &answer),
|
||||
..builder()
|
||||
};
|
||||
let reply = b.build_from_meta(&meta_job(), "t").await;
|
||||
let FromMetaReply::Built {
|
||||
icon: Some(IconRead::Set { svg, png }),
|
||||
..
|
||||
} = reply
|
||||
else {
|
||||
panic!("{answer}: {reply:?}");
|
||||
};
|
||||
assert_eq!(svg, "<svg/>");
|
||||
assert_eq!(png, want, "{answer}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// An agent with no icon has no PNG to build: the PNG option is not read.
|
||||
#[tokio::test]
|
||||
async fn an_agent_with_no_icon_builds_no_png() {
|
||||
let nix = fake_nix(
|
||||
"no-icon-png",
|
||||
"for a in \"$@\"; do last=$a; done; case \"$last\" in *agent.iconPng) echo called > \"$0.called\"; echo /nix/store/x;; *agent.icon) echo null;; *) if [ \"$1\" = eval ]; then echo false; else echo /nix/store/x; fi;; esac\n",
|
||||
);
|
||||
let marker = PathBuf::from(format!("{}.called", nix.display()));
|
||||
let _ = std::fs::remove_file(&marker);
|
||||
let b = Builder { nix, ..builder() };
|
||||
let reply = b.build_from_meta(&meta_job(), "t").await;
|
||||
assert!(
|
||||
matches!(
|
||||
&reply,
|
||||
FromMetaReply::Built {
|
||||
icon: Some(IconRead::Unset),
|
||||
..
|
||||
}
|
||||
),
|
||||
"{reply:?}"
|
||||
);
|
||||
assert!(
|
||||
!marker.exists(),
|
||||
"the PNG option was read for an agent with no icon"
|
||||
);
|
||||
}
|
||||
|
||||
/// An icon file over the cap is left out of the reply rather than sent: the
|
||||
/// reply is one queue message.
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue