Watch
0
0
Fork
You've already forked hyperhive
0

remove what no longer has a reader: build-log store, dashboard sent/delivered events, graph resource dep

This commit is contained in:
atlas 2026-11-13 10:08:19 +01:00 • committed by mara
commit 49266e4410

View file

@ -222,7 +222,7 @@ pub async fn provision_container(name: &str, paths: &AgentPaths) -> Result<()> {
/// Assumes `provision_container` already registered the agent.
/// `toplevel` is the store path of the system closure to create it from.
pub async fn create_only(name: &str, toplevel: &str) -> Result<()> {
priv_run_inner("create", name, Some(toplevel), None).await
priv_run_inner("create", name, Some(toplevel)).await
}
/// Rebuild-path preamble, run by the job queue's `Prebuild` node: fail fast on
@ -252,10 +252,9 @@ pub async fn swap_update(
hive: &HiveEnv,
paths: &AgentPaths,
toplevel: &str,
node_id: Option<u64>,
) -> Result<()> {
write_dropins(name, hive, paths).await?;
priv_run_inner("update", name, Some(toplevel), node_id).await
priv_run_inner("update", name, Some(toplevel)).await
}
/// Build the `AgentSpec` list for [`crate::agent_set::refresh`] from
@ -728,22 +727,19 @@ fn is_actionable_nix_line(line: &str) -> bool {
/// What every log line of one container operation carries, so the swarm log
/// store can group a build's lines and the builds board can list the builds:
/// `build_id` (the build-log row's id, empty when the store is not installed),
/// `agent` and `kind`. A `start` line opens the build and a `finish` line
/// closes it with its `status` and `runtime_secs`; the output lines between
/// them have no `event`.
#[derive(Clone)]
struct BuildTag {
build_id: String,
agent: String,
kind: String,
cmdline: String,
}
impl BuildTag {
fn new(log_id: Option<i64>, agent: &str, kind: &str, cmdline: &str) -> Self {
fn new(agent: &str, kind: &str, cmdline: &str) -> Self {
Self {
build_id: log_id.map(|id| id.to_string()).unwrap_or_default(),
agent: agent.to_owned(),
kind: kind.to_owned(),
cmdline: cmdline.to_owned(),
@ -754,7 +750,6 @@ impl BuildTag {
tracing::info!(
target: "nixos-container",
event = "start",
build_id = %self.build_id,
agent = %self.agent,
kind = %self.kind,
cmdline = %self.cmdline,
@ -771,7 +766,6 @@ impl BuildTag {
if actionable {
tracing::warn!(
target: "nixos-container",
build_id = %self.build_id,
agent = %self.agent,
kind = %self.kind,
cmdline = %self.cmdline,
@ -780,7 +774,6 @@ impl BuildTag {
} else {
tracing::info!(
target: "nixos-container",
build_id = %self.build_id,
agent = %self.agent,
kind = %self.kind,
cmdline = %self.cmdline,
@ -795,7 +788,6 @@ impl BuildTag {
tracing::info!(
target: "nixos-container",
event = "finish",
build_id = %self.build_id,
agent = %self.agent,
kind = %self.kind,
status,
@ -808,7 +800,6 @@ impl BuildTag {
tracing::warn!(
target: "nixos-container",
event = "finish",
build_id = %self.build_id,
agent = %self.agent,
kind = %self.kind,
status,
@ -822,46 +813,22 @@ impl BuildTag {
}
/// Build the per-line callback for `create_container_streaming` /
/// `update_container_streaming`. Both ops share identical dispatch logic
/// (log the line through its `BuildTag`, then `append_stdout` or
/// `append_stderr`); this helper avoids duplicating that match body across the
/// two call sites.
fn make_log_callback(
logs: Option<std::sync::Arc<crate::build_logs::BuildLogs>>,
log_id: Option<i64>,
tag: BuildTag,
) -> impl FnMut(hive_priv_sock::PrivStream, &str) {
use hive_priv_sock::PrivStream;
move |stream, line| {
tag.line(stream, line);
if let (Some(h), Some(id)) = (&logs, log_id) {
match stream {
PrivStream::Stdout => h.append_stdout(id, line),
PrivStream::Stderr => h.append_stderr(id, line),
}
}
}
/// `update_container_streaming`: log each line through its `BuildTag`.
fn make_log_callback(tag: BuildTag) -> impl FnMut(hive_priv_sock::PrivStream, &str) {
move |stream, line| tag.line(stream, line)
}
/// Put a failed non-streaming op's error into its build-log row, which has no
/// other output: `start` runs through `call`, not the streaming protocol. The
/// error carries `nixos-container`'s stderr; `journal` is the container
/// unit's own log from hive-priv. An `Err` there (an older hive-priv does not
/// know the op) leaves the stderr alone.
fn record_failure(
logs: Option<&crate::build_logs::BuildLogs>,
log_id: Option<i64>,
err: &anyhow::Error,
journal: Result<String>,
) {
if let (Some(h), Some(id)) = (logs, log_id) {
for line in format!("{err:#}").lines() {
h.append_stderr(id, line);
}
match journal {
Ok(text) => text.lines().for_each(|l| h.append_stderr(id, l)),
Err(e) => tracing::debug!(error = ?e, "start journal unavailable"),
}
/// Log a failed `start`'s error and the container unit's own journal (from
/// hive-priv) line by line. An `Err` journal (an older hive-priv does not know
/// the op) leaves just the error.
fn record_failure(tag: &BuildTag, err: &anyhow::Error, journal: Result<String>) {
use hive_priv_sock::PrivStream;
for line in format!("{err:#}").lines() {
tag.line(PrivStream::Stderr, line);
}
match journal {
Ok(text) => text.lines().for_each(|l| tag.line(PrivStream::Stderr, l)),
Err(e) => tracing::debug!(error = ?e, "start journal unavailable"),
}
}
@ -872,42 +839,21 @@ fn missing_toplevel(kind: &str, name: &str) -> anyhow::Error {
anyhow::anyhow!("{name}: {kind} needs a closure to apply")
}
/// Execute a container operation via hive-priv and integrate with
/// `build_logs.sqlite`. hive-priv runs as root and forwards output lines
/// to hive-c0re in real time via the streaming priv protocol. Each line
/// is appended to the build-log row as it arrives, so the dashboard
/// shows live progress during long `nixos-container create` / `update` runs.
/// Execute a container operation via hive-priv. hive-priv runs as root and
/// forwards output lines to hive-c0re in real time via the streaming priv
/// protocol; each line is logged through the op's `BuildTag`.
async fn priv_run(kind: &str, name: &str) -> Result<()> {
priv_run_inner(kind, name, None, None).await
priv_run_inner(kind, name, None).await
}
/// Like `priv_run` but stamps `node_id` onto the build-log row it opens, so
/// the log can be found from the queue node.
///
/// This used to be a `Fn(i64)` callback that handed the row id *back* to the
/// queue, which then held it in a side map. The row carries the link itself
/// now, so the id only ever travels one way.
async fn priv_run_inner(
kind: &str,
name: &str,
toplevel: Option<&str>,
node_id: Option<u64>,
) -> Result<()> {
/// [`priv_run`] with the closure a `create` or `update` applies.
async fn priv_run_inner(kind: &str, name: &str, toplevel: Option<&str>) -> Result<()> {
let container = container_name(name);
let cmdline = format!("nixos-container {kind} {container}");
let logs = crate::build_logs::global();
let log_id = logs.as_ref().and_then(|h| {
h.start(name, kind, &cmdline, node_id)
.map_err(|e| {
tracing::warn!(error = ?e, "build_logs: start failed (priv_run log dropped)");
})
.ok()
});
// For long-running ops use the streaming protocol so build_logs
// receives lines in real time rather than as a batch at completion.
let tag = BuildTag::new(log_id, name, kind, &cmdline);
// Long-running ops use the streaming protocol so lines are logged in real
// time rather than as a batch at completion.
let tag = BuildTag::new(name, kind, &cmdline);
tag.started();
let began = std::time::Instant::now();
let result: Result<()> = match kind {
@ -916,7 +862,7 @@ async fn priv_run_inner(
crate::priv_client::create_container_streaming(
name,
toplevel,
make_log_callback(logs.clone(), log_id, tag.clone()),
make_log_callback(tag.clone()),
)
.await
}
@ -927,7 +873,7 @@ async fn priv_run_inner(
crate::priv_client::update_container_streaming(
name,
toplevel,
make_log_callback(logs.clone(), log_id, tag.clone()),
make_log_callback(tag.clone()),
)
.await
}
@ -940,7 +886,7 @@ async fn priv_run_inner(
let started = crate::priv_client::start_container(name).await;
if let Err(e) = &started {
let journal = crate::priv_client::start_failure_log(name, attempt).await;
record_failure(logs.as_deref(), log_id, e, journal);
record_failure(&tag, e, journal);
}
started
}
@ -952,22 +898,6 @@ async fn priv_run_inner(
let succeeded = result.is_ok();
tag.finished(succeeded, began.elapsed());
if let (Some(h), Some(id)) = (&logs, log_id) {
h.finish(
id,
if succeeded {
crate::build_logs::BuildStatus::Ok
} else {
crate::build_logs::BuildStatus::Fail
},
);
}
match result {
Ok(()) => Ok(()),
Err(e) => match log_id {
Some(id) => bail!("{e:#}; see build log #{id}"),
None => bail!("{e:#}"),
},
}
result.map_err(|e| anyhow::anyhow!("{e:#}"))
}