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

@ -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(),