feat(#1116): stream nixos-container create/update output live into build_logs

This commit is contained in:
damocles 2026-06-03 10:17:44 +02:00
commit 68451eb205
4 changed files with 335 additions and 46 deletions

View file

@ -1207,14 +1207,13 @@ async fn set_nspawn_flags(
}
/// Execute a container operation via hive-priv and integrate with
/// build_logs.sqlite. hive-priv runs as root and logs output to
/// journald as it arrives; this function captures the final stdout +
/// stderr into build_logs for the dashboard after the operation
/// completes. For `create` and `update` (the long-running ops)
/// hive-priv already logs each line to its own journald stream —
/// streaming into build_logs is deferred to a follow-up that adds a
/// streaming mode to the priv protocol.
/// 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.
async fn priv_run(kind: &str, name: &str) -> Result<()> {
use hive_sh4re::priv_proto::PrivStream;
let container = container_name(name);
let cmdline = format!("nixos-container {kind} {container}");
@ -1227,35 +1226,78 @@ async fn priv_run(kind: &str, name: &str) -> Result<()> {
.ok()
});
let result: Result<(String, String)> = match kind {
"create" => crate::priv_client::create_container(name).await,
"update" => crate::priv_client::update_container(name).await,
"start" => crate::priv_client::start_container(name).await.map(|()| (String::new(), String::new())),
"stop" => crate::priv_client::stop_container(name).await.map(|()| (String::new(), String::new())),
"kill" => crate::priv_client::kill_container(name).await.map(|()| (String::new(), String::new())),
"destroy" => crate::priv_client::destroy_container(name).await.map(|()| (String::new(), String::new())),
// For long-running ops use the streaming protocol so build_logs
// receives lines in real time rather than as a batch at completion.
let result: Result<()> = match kind {
"create" => {
let h = logs.clone();
let id = log_id;
crate::priv_client::create_container_streaming(name, move |stream, line| {
match stream {
PrivStream::Stdout => {
tracing::info!(target: "nixos-container", cmdline = %cmdline, "{line}");
if let (Some(h), Some(id)) = (&h, id) {
h.append_stdout(id, line);
}
}
PrivStream::Stderr => {
tracing::warn!(target: "nixos-container", cmdline = %cmdline, "{line}");
if let (Some(h), Some(id)) = (&h, id) {
h.append_stderr(id, line);
}
}
}
})
.await
}
"update" => {
let h = logs.clone();
let id = log_id;
crate::priv_client::update_container_streaming(name, move |stream, line| {
match stream {
PrivStream::Stdout => {
tracing::info!(target: "nixos-container", cmdline = %cmdline, "{line}");
if let (Some(h), Some(id)) = (&h, id) {
h.append_stdout(id, line);
}
}
PrivStream::Stderr => {
tracing::warn!(target: "nixos-container", cmdline = %cmdline, "{line}");
if let (Some(h), Some(id)) = (&h, id) {
h.append_stderr(id, line);
}
}
}
})
.await
}
"start" => crate::priv_client::start_container(name).await,
"stop" => crate::priv_client::stop_container(name).await,
"kill" => crate::priv_client::kill_container(name).await,
"destroy" => crate::priv_client::destroy_container(name).await,
other => Err(anyhow::anyhow!("unknown container op: {other}")),
};
let ok = result.is_ok();
let succeeded = result.is_ok();
if let (Some(h), Some(id)) = (&logs, log_id) {
if let Ok((ref stdout, ref stderr)) = result {
for line in stdout.lines() {
tracing::info!(target: "nixos-container", cmdline = %cmdline, "{line}");
h.append_stdout(id, line);
}
for line in stderr.lines() {
tracing::warn!(target: "nixos-container", cmdline = %cmdline, "{line}");
h.append_stderr(id, line);
}
}
h.finish(id, if ok { crate::build_logs::BuildStatus::Ok } else { crate::build_logs::BuildStatus::Fail });
h.finish(
id,
if succeeded {
crate::build_logs::BuildStatus::Ok
} else {
crate::build_logs::BuildStatus::Fail
},
);
}
match result {
Ok(_) => Ok(()),
Ok(()) => Ok(()),
Err(e) => {
let journal = if kind == "update" { container_journal_tail(&container).await } else { String::new() };
let journal = if kind == "update" {
container_journal_tail(&container).await
} else {
String::new()
};
match log_id {
Some(id) => bail!("{e:#}; see build log #{id}{journal}"),
None => bail!("{e:#}{journal}"),

View file

@ -8,12 +8,14 @@
use anyhow::{Context as _, Result, bail};
use hive_sh4re::priv_proto::{
BindMount, JournalOutput, NetworkIsolation, PRIV_SOCK, PrivRequest, PrivResponse,
BindMount, JournalOutput, NetworkIsolation, PRIV_SOCK, PrivEvent, PrivRequest, PrivResponse,
PrivStream,
};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::UnixStream;
/// Send a single request to `hive-priv` and return the response.
/// For streaming ops use `call_streaming` instead.
pub async fn call(req: &PrivRequest) -> Result<PrivResponse> {
let mut stream = UnixStream::connect(PRIV_SOCK)
.await
@ -29,7 +31,46 @@ pub async fn call(req: &PrivRequest) -> Result<PrivResponse> {
.read_line(&mut resp_line)
.await
.context("read response from hive-priv")?;
serde_json::from_str(&resp_line).context("parse PrivResponse")
// New hive-priv sends `PrivEvent::Done(PrivResponse)` (untagged, wire-
// identical to bare `PrivResponse`) — deserialise as `PrivEvent` to
// handle both the old bare format and the new tagged format.
match serde_json::from_str::<PrivEvent>(&resp_line).context("parse PrivResponse")? {
PrivEvent::Done(resp) => Ok(resp),
PrivEvent::Line(_) => bail!("unexpected stream line from non-streaming priv op"),
}
}
/// Send a streaming request to `hive-priv`, calling `on_line` for each
/// `PrivEvent::Line` as it arrives, then returning the terminal
/// `PrivResponse`. Used for long-running ops (`create` / `update`).
pub async fn call_streaming(
req: &PrivRequest,
mut on_line: impl FnMut(PrivStream, &str),
) -> Result<PrivResponse> {
let mut stream = UnixStream::connect(PRIV_SOCK)
.await
.context("connect to hive-priv socket (streaming)")?;
let line = serde_json::to_string(req).context("serialise PrivRequest")? + "\n";
stream
.write_all(line.as_bytes())
.await
.context("send request to hive-priv")?;
stream.shutdown().await.context("shutdown write half")?;
let mut reader = BufReader::new(stream);
loop {
let mut event_line = String::new();
reader
.read_line(&mut event_line)
.await
.context("read event from hive-priv")?;
if event_line.is_empty() {
bail!("hive-priv closed connection before sending Done event");
}
match serde_json::from_str::<PrivEvent>(&event_line).context("parse PrivEvent")? {
PrivEvent::Line(l) => on_line(l.stream, &l.data),
PrivEvent::Done(resp) => return Ok(resp),
}
}
}
pub async fn start_container(name: &str) -> Result<()> {
@ -57,20 +98,55 @@ pub async fn update_container(name: &str) -> Result<(String, String)> {
check(
call(&PrivRequest::UpdateContainer {
name: name.to_owned(),
stream: false,
})
.await?,
)
}
/// Streaming variant: forward stdout/stderr lines to `on_line` as they
/// arrive. Returns `Ok(())` on success; the callback is responsible for
/// appending lines to build_logs or otherwise capturing the output.
pub async fn update_container_streaming(
name: &str,
on_line: impl FnMut(PrivStream, &str),
) -> Result<()> {
ok(call_streaming(
&PrivRequest::UpdateContainer {
name: name.to_owned(),
stream: true,
},
on_line,
)
.await?)
}
pub async fn create_container(name: &str) -> Result<(String, String)> {
check(
call(&PrivRequest::CreateContainer {
name: name.to_owned(),
stream: false,
})
.await?,
)
}
/// Streaming variant: forward stdout/stderr lines to `on_line` as they
/// arrive. Returns `Ok(())` on success.
pub async fn create_container_streaming(
name: &str,
on_line: impl FnMut(PrivStream, &str),
) -> Result<()> {
ok(call_streaming(
&PrivRequest::CreateContainer {
name: name.to_owned(),
stream: true,
},
on_line,
)
.await?)
}
pub async fn destroy_container(name: &str) -> Result<()> {
ok(call(&PrivRequest::DestroyContainer {
name: name.to_owned(),