//! Async client for the `hive-priv` privileged-helper socket. //! //! Exposes a standalone async function per operation. Each call opens a //! fresh connection to `/run/hive/priv.sock`, sends one JSON line, reads //! the response, and closes. Connection-per-call is intentional: priv //! calls are infrequent (once per rebuild step), so simplicity wins over //! a persistent connection. use anyhow::{Context as _, Result, bail}; use hive_priv_sock::{ BindMount, CredentialMount, InfraAction, InfraContainer, JournalQuery, 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 { let mut stream = UnixStream::connect(PRIV_SOCK) .await .context("connect to hive-priv socket")?; 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 resp_line = String::new(); BufReader::new(stream) .read_line(&mut resp_line) .await .context("read response from hive-priv")?; // 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::(&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 { 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::(&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<()> { ok(call(&PrivRequest::StartContainer { name: name.to_owned(), }) .await?) } pub async fn stop_container(name: &str) -> Result<()> { ok(call(&PrivRequest::StopContainer { name: name.to_owned(), }) .await?) } pub async fn kill_container(name: &str) -> Result<()> { ok(call(&PrivRequest::KillContainer { name: name.to_owned(), }) .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?) } /// 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(), }) .await?) } pub async fn list_containers() -> Result { let (stdout, _) = check(call(&PrivRequest::ListContainers).await?)?; Ok(stdout) } /// Read a container's journal via the root helper (`journalctl -M`). /// Returns `(stdout, stderr)`; a non-zero journalctl exit is reported in /// `stderr` rather than as an `Err`, so callers can surface either. pub async fn read_container_journal( container: &str, query: JournalQuery, ) -> Result<(String, String)> { check( call(&PrivRequest::ReadContainerJournal { container: container.to_owned(), query, }) .await?, ) } pub async fn write_nspawn_flags( container: &str, binds: &[BindMount], isolation: Option, load_credentials: &[CredentialMount], ) -> Result<()> { ok(call(&PrivRequest::WriteNspawnFlags { container: container.to_owned(), binds: binds.to_vec(), isolation, load_credentials: load_credentials.to_vec(), }) .await?) } pub async fn write_resource_limits( container: &str, memory_max: &str, cpu_quota: &str, ) -> Result<()> { ok(call(&PrivRequest::WriteResourceLimits { container: container.to_owned(), memory_max: memory_max.to_owned(), cpu_quota: cpu_quota.to_owned(), }) .await?) } pub async fn remove_service_dropin(container: &str) -> Result<()> { ok(call(&PrivRequest::RemoveServiceDropin { container: container.to_owned(), }) .await?) } pub async fn daemon_reload() -> Result<()> { ok(call(&PrivRequest::DaemonReload).await?) } pub async fn reload_gateway_nginx() -> Result<()> { ok(call(&PrivRequest::ReloadGatewayNginx).await?) } pub async fn chown_socket_dir(agent_name: &str, uid: u32, gid: u32) -> Result<()> { ok(call(&PrivRequest::ChownSocketDir { agent_name: agent_name.to_owned(), uid, gid, }) .await?) } pub async fn chmod_socket_dir(agent_name: &str, mode: u32) -> Result<()> { ok(call(&PrivRequest::ChmodSocketDir { agent_name: agent_name.to_owned(), mode, }) .await?) } /// Run `forgejo admin ` inside the `hive-forge` container via /// hive-priv (which runs as root and can nsenter into the container). /// Returns `(stdout, stderr)` on success. pub async fn run_forge_admin(args: &[&str]) -> Result<(String, String)> { let owned: Vec = args.iter().map(|s| (*s).to_owned()).collect(); check(call(&PrivRequest::RunForgeAdmin { args: owned }).await?) } /// Write the Forgejo access token for `agent_name` to /// `//state/forge-token` via hive-priv /// (running as root). The file is written 0600 and chowned to the agent /// user so it is readable from inside the agent container. pub async fn write_agent_forge_token(agent_name: &str, token: &str) -> Result<()> { ok(call(&PrivRequest::WriteAgentForgeToken { agent_name: agent_name.to_owned(), token: token.to_owned(), }) .await?) } /// Write a Matrix access token for `agent_name` via hive-priv (running as /// root). `account: None` writes the hive-internal /// `/matrix-token`; `account: Some(name)` writes /// `/matrix-token-` for an extra (external) account. The file /// is written 0600 and chowned to the agent user so it is readable from /// inside the agent container. hive-priv validates the account suffix. /// /// `homeserver: Some(url)` (only meaningful with `account: Some`) also /// writes the sidecar `/matrix-account-.json` so the daemon /// can auto-discover the extra account without a `matrixAccounts` config /// declaration (see issue tracker "external matrix account auto-discovery"). pub async fn write_agent_matrix_token( agent_name: &str, token: &str, account: Option<&str>, homeserver: Option<&str>, ) -> Result<()> { ok(call(&PrivRequest::WriteAgentMatrixToken { agent_name: agent_name.to_owned(), token: token.to_owned(), account: account.map(ToOwned::to_owned), homeserver: homeserver.map(ToOwned::to_owned), }) .await?) } /// Write a GitHub personal access token (PAT) for `agent_name` via hive-priv /// (running as root). Writes `/github-token` 0600, chowned to the agent /// user so the `gh` wrapper / git credential helper can read it from inside the /// container. Single account per agent — no account suffix. The token value is /// operator-supplied (for the agent's GitHub integration, `hyperhive.github.enable`). /// /// # Errors /// /// Returns an error if the hive-priv call fails — the socket is unreachable, /// `agent_name` is rejected by the root-side validation, or the file /// write/chown fails. pub async fn write_agent_github_token(agent_name: &str, token: &str) -> Result<()> { ok(call(&PrivRequest::WriteAgentGithubToken { agent_name: agent_name.to_owned(), token: token.to_owned(), }) .await?) } /// Write a per-agent account for a dashboard-declared external forge — /// label + base URL + token — to `/forge-