Phase 4: manager socket + manager_server with privileged tool surface
This commit is contained in:
parent
4f191b2e43
commit
aa67e5a481
6 changed files with 188 additions and 15 deletions
|
|
@ -9,7 +9,7 @@ use tokio::net::{UnixListener, UnixStream};
|
|||
use crate::coordinator::Coordinator;
|
||||
use crate::lifecycle;
|
||||
|
||||
pub async fn serve(socket: &Path, agent_flake: &str, coord: Arc<Coordinator>) -> Result<()> {
|
||||
pub async fn serve(socket: &Path, coord: Arc<Coordinator>) -> Result<()> {
|
||||
if let Some(parent) = socket.parent() {
|
||||
std::fs::create_dir_all(parent)
|
||||
.with_context(|| format!("create socket parent {}", parent.display()))?;
|
||||
|
|
@ -20,21 +20,20 @@ pub async fn serve(socket: &Path, agent_flake: &str, coord: Arc<Coordinator>) ->
|
|||
|
||||
let listener = UnixListener::bind(socket)
|
||||
.with_context(|| format!("bind admin socket {}", socket.display()))?;
|
||||
tracing::info!(socket = %socket.display(), %agent_flake, "hive-c0re listening");
|
||||
tracing::info!(socket = %socket.display(), agent_flake = %coord.agent_flake, "hive-c0re admin listening");
|
||||
|
||||
loop {
|
||||
let (stream, _) = listener.accept().await.context("accept connection")?;
|
||||
let agent_flake = agent_flake.to_owned();
|
||||
let coord = coord.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = handle(stream, &agent_flake, coord).await {
|
||||
if let Err(e) = handle(stream, coord).await {
|
||||
tracing::warn!(error = ?e, "connection failed");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle(stream: UnixStream, agent_flake: &str, coord: Arc<Coordinator>) -> Result<()> {
|
||||
async fn handle(stream: UnixStream, coord: Arc<Coordinator>) -> Result<()> {
|
||||
let (read, mut write) = stream.into_split();
|
||||
let mut reader = BufReader::new(read);
|
||||
let mut line = String::new();
|
||||
|
|
@ -46,7 +45,7 @@ async fn handle(stream: UnixStream, agent_flake: &str, coord: Arc<Coordinator>)
|
|||
return Ok(());
|
||||
}
|
||||
let resp = match serde_json::from_str::<HostRequest>(line.trim()) {
|
||||
Ok(req) => dispatch(&req, agent_flake, &coord).await,
|
||||
Ok(req) => dispatch(&req, &coord).await,
|
||||
Err(e) => HostResponse::error(format!("parse error: {e}")),
|
||||
};
|
||||
let mut payload = serde_json::to_string(&resp)?;
|
||||
|
|
@ -56,13 +55,13 @@ async fn handle(stream: UnixStream, agent_flake: &str, coord: Arc<Coordinator>)
|
|||
}
|
||||
}
|
||||
|
||||
async fn dispatch(req: &HostRequest, agent_flake: &str, coord: &Coordinator) -> HostResponse {
|
||||
async fn dispatch(req: &HostRequest, coord: &Coordinator) -> HostResponse {
|
||||
let result: anyhow::Result<HostResponse> = async {
|
||||
Ok(match req {
|
||||
HostRequest::Spawn { name } => {
|
||||
tracing::info!(%name, "spawn");
|
||||
let agent_dir = coord.register_agent(name).await?;
|
||||
if let Err(e) = lifecycle::spawn(name, agent_flake, &agent_dir).await {
|
||||
if let Err(e) = lifecycle::spawn(name, &coord.agent_flake, &agent_dir).await {
|
||||
// Roll back socket registration if container creation failed.
|
||||
coord.unregister_agent(name);
|
||||
return Err(e);
|
||||
|
|
@ -78,7 +77,7 @@ async fn dispatch(req: &HostRequest, agent_flake: &str, coord: &Coordinator) ->
|
|||
HostRequest::Rebuild { name } => {
|
||||
tracing::info!(%name, "rebuild");
|
||||
let agent_dir = coord.register_agent(name).await?;
|
||||
lifecycle::rebuild(name, agent_flake, &agent_dir).await?;
|
||||
lifecycle::rebuild(name, &coord.agent_flake, &agent_dir).await?;
|
||||
HostResponse::success()
|
||||
}
|
||||
HostRequest::List => HostResponse::list(lifecycle::list().await?),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue