Watch
0
0
Fork
You've already forked hyperhive
0

the builder reads an agent's icon from its config and the controller writes the swarm's copy

This commit is contained in:
iris 2026-11-16 22:21:21 +01:00 • committed by mara
commit 181ef5f421
3 changed files with 299 additions and 13 deletions

View file

@ -623,6 +623,7 @@ async fn handle_meta(
cached,
private_users,
nix_builds,
..
} => tracing::info!(
agent = %job.agent,
meta_rev = %job.meta_rev,
@ -864,6 +865,7 @@ mod tests {
cached: false,
private_users: Some(false),
nix_builds: Some(hive_types::NixBuilds::Host),
icon: None,
}
},
)
@ -887,6 +889,7 @@ mod tests {
cached: false,
private_users: Some(false),
nix_builds: Some(hive_types::NixBuilds::Host),
icon: None,
}
},
)
@ -1473,9 +1476,9 @@ mod tests {
"nix_builds": "host",
})
);
assert_eq!(fake.lines("tokens"), ["forge-token"; 3]);
assert_eq!(fake.lines("tokens"), ["forge-token"; 4]);
let calls = fake.calls();
assert_eq!(calls.len(), 3, "{calls:?}");
assert_eq!(calls.len(), 4, "{calls:?}");
assert!(
calls[0].contains(&format!(
"git+https://forge.example/internal/meta.git?rev={REV}#nixosConfigurations.atlas.config.system.build.toplevel"
@ -1490,6 +1493,11 @@ mod tests {
calls[2].contains("services.hyperhive.agent.nixBuilds"),
"{calls:?}"
);
// The icon is read as the text of the file the option names.
assert!(
calls[3].contains("services.hyperhive.agent.icon") && calls[3].contains("--apply"),
"{calls:?}"
);
}
/// An option the module does not declare is false; one whose eval failed, or
@ -1605,7 +1613,7 @@ mod tests {
];
serve_meta(fake.builder(), env, msgs).await;
assert_eq!(next(&mut sent).await.0, "_INBOX.2");
assert_eq!(fake.calls().len(), 3, "one build and its two option reads");
assert_eq!(fake.calls().len(), 4, "one build and its three option reads");
}
#[tokio::test(start_paused = true)]

View file

@ -21,7 +21,7 @@ use std::time::Duration;
use crate::hyperhive_flake::HyperhiveFlake;
use hive_types::NixBuilds;
use swarm_wire::builder::{
BuildAgent, BuildFromMeta, FromMetaReply, LockMeta, LockReply, META_REPO_PATH, Reply,
BuildAgent, BuildFromMeta, FromMetaReply, IconRead, LockMeta, LockReply, META_REPO_PATH, Reply,
};
use tokio::io::AsyncReadExt as _;
@ -96,6 +96,7 @@ impl Builder {
cached: false,
private_users: self.private_users(job, token).await,
nix_builds: self.nix_builds(job, token).await,
icon: self.icon(job, token).await,
},
Reply::Failed { error_tail } => FromMetaReply::Failed { error_tail },
},
@ -189,9 +190,23 @@ impl Builder {
/// sandbox against the flake the build just evaluated. Logs why when the
/// answer is not a value.
async fn eval_option(&self, job: &BuildFromMeta, token: &str, option: &str) -> Eval {
self.eval_option_applied(job, token, option, None).await
}
/// [`Self::eval_option`] with `apply`, a nix function, applied to the value.
async fn eval_option_applied(
&self,
job: &BuildFromMeta,
token: &str,
option: &str,
apply: Option<&str>,
) -> Eval {
let mut cmd = self.nix_command(token);
cmd.args(EVAL_FLAGS)
.arg(format!("{}.{option}", self.meta_config(job)));
cmd.args(EVAL_FLAGS);
if let Some(apply) = apply {
cmd.args(["--apply", apply]);
}
cmd.arg(format!("{}.{option}", self.meta_config(job)));
match run_raw(cmd, self.deadline).await {
Ok((status, out, _)) if status.success() => Eval::Value(out),
Ok((_, _, err)) if attribute_missing(&String::from_utf8_lossy(&err)) => {
@ -232,6 +247,22 @@ impl Builder {
}
}
/// 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,
/// is [`IconRead::Unset`]; a failed eval, a timeout, an output that is no
/// string and a file over [`ICON_MAX_BYTES`] are `None`.
async fn icon(&self, job: &BuildFromMeta, token: &str) -> Option<IconRead> {
match self
.eval_option_applied(job, token, ICON_OPTION, Some(ICON_APPLY))
.await
{
Eval::Value(out) => parse_icon(&out),
Eval::Missing => Some(IconRead::Unset),
Eval::Unknown => None,
}
}
/// The config commit as a flake reference, fetched through the PR's head
/// ref.
fn agent_url(&self, job: &BuildAgent) -> String {
@ -273,6 +304,16 @@ const PRIVATE_USERS_OPTION: &str = "services.hyperhive.agent.user.privateUsers";
/// The agent option holding where its nix builds run.
const NIX_BUILDS_OPTION: &str = "services.hyperhive.agent.nixBuilds";
/// The agent option naming its icon, an SVG file.
const ICON_OPTION: &str = "services.hyperhive.agent.icon";
/// Applied to the icon option: the file's text, or null when none is set.
const ICON_APPLY: &str = "p: if p == null then null else builtins.readFile p";
/// The largest icon the reply carries, in bytes: a reply is one queue message,
/// and an icon is a small drawing. A larger file is left out as unread.
const ICON_MAX_BYTES: usize = 256 * 1024;
/// What `nix eval --json` of an agent option came to.
enum Eval {
/// It printed a value.
@ -298,6 +339,24 @@ fn parse_flag(out: &[u8]) -> Option<bool> {
}
}
/// `nix eval --json` of the icon option after [`ICON_APPLY`]: null is unset, a
/// string is the SVG's text, and anything else, an empty or an oversize file
/// included, is `None`.
fn parse_icon(out: &[u8]) -> Option<IconRead> {
match serde_json::from_slice::<Option<String>>(out).ok()? {
None => Some(IconRead::Unset),
Some(svg) if svg.is_empty() => None,
Some(svg) if svg.len() > ICON_MAX_BYTES => {
tracing::warn!(
bytes = svg.len(),
"the agent's icon is too large; not sending it"
);
None
}
Some(svg) => Some(IconRead::Set { svg }),
}
}
/// `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> {
@ -660,6 +719,56 @@ mod tests {
}
}
/// The reply carries the agent's icon: the file's text for a set option, `Unset`
/// for a null one or a module that predates it, and nothing for an eval that
/// failed or printed what no icon is. The eval is asked to read the file, which
/// is what the `--apply` check pins.
#[tokio::test]
async fn a_meta_build_reports_the_agents_icon() {
for (i, (answer, want)) in [
(
"echo '\"<svg/>\"'",
Some(IconRead::Set {
svg: "<svg/>".to_owned(),
}),
),
("echo null", Some(IconRead::Unset)),
(ATTRIBUTE_MISSING, Some(IconRead::Unset)),
("echo '\"\"'", None),
("echo 5", None),
("echo error: boom >&2; exit 1", None),
]
.into_iter()
.enumerate()
{
let nix = fake_nix(
&format!("icon-{i}"),
&format!(
"for a in \"$@\"; do last=$a; done; case \"$last\" in *agent.icon) case \"$*\" in *--apply*) {answer};; *) echo 'no apply' >&2; exit 1;; esac;; *) if [ \"$1\" = eval ]; then echo false; else echo /nix/store/x; fi;; esac\n"
),
);
let b = Builder { nix, ..builder() };
let reply = b.build_from_meta(&meta_job(), "t").await;
assert!(
matches!(&reply, FromMetaReply::Built { icon, .. } if *icon == want),
"{answer}: {reply:?}"
);
}
}
/// An icon file over the cap is left out of the reply rather than sent: the
/// reply is one queue message.
#[test]
fn an_icon_over_the_cap_is_not_sent() {
let big = format!("\"{}\"", "a".repeat(ICON_MAX_BYTES + 1));
assert_eq!(parse_icon(big.as_bytes()), None);
let fits = format!("\"{}\"", "a".repeat(ICON_MAX_BYTES));
assert!(matches!(
parse_icon(fits.as_bytes()),
Some(IconRead::Set { .. })
));
}
/// An eval that does not finish in time is unknown for both options, not
/// their defaults. The build itself finishes, which is the control.
#[tokio::test]
@ -741,6 +850,7 @@ mod tests {
cached: false,
private_users: None,
nix_builds: None,
icon: None,
}
);
}

View file

@ -212,6 +212,7 @@ pub async fn cache_built(
system,
private_users,
nix_builds,
icon,
..
},
) = (cache, &reply)
@ -240,6 +241,7 @@ pub async fn cache_built(
cached: true,
private_users: *private_users,
nix_builds: *nix_builds,
icon: icon.clone(),
}
}
Err(reason) => {
@ -268,6 +270,7 @@ mod tests {
cached: false,
private_users: None,
nix_builds: Some(NixBuilds::Disabled),
icon: Some(swarm_wire::builder::IconRead::Unset),
}
}
@ -306,6 +309,7 @@ mod tests {
FromMetaReply::Built {
private_users: None,
nix_builds: Some(NixBuilds::Disabled),
icon: Some(swarm_wire::builder::IconRead::Unset),
..
}
),