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}"),