|
| 1 | +//! Child process for scripts/mesh-cross-node.py; never opens the desktop or Keychain. |
| 2 | +use anyhow::{bail, Context, Result}; |
| 3 | +use buzz_mesh_compute::{ |
| 4 | + config::{ClientConfig, ServeConfig}, |
| 5 | + identity::ensure_owner_at, |
| 6 | + lifecycle::{Lifecycle, Phase}, |
| 7 | +}; |
| 8 | +use serde_json::{json, Value}; |
| 9 | +use std::{ |
| 10 | + io::{BufRead, Write}, |
| 11 | + path::PathBuf, |
| 12 | + time::Duration, |
| 13 | +}; |
| 14 | + |
| 15 | +fn main() -> Result<()> { |
| 16 | + let root = PathBuf::from(std::env::var("BUZZ_MESH_TEST_HOME")?); |
| 17 | + anyhow::ensure!( |
| 18 | + root.is_absolute() && root.join("SMOKE_ONLY").is_file(), |
| 19 | + "isolated test home required" |
| 20 | + ); |
| 21 | + anyhow::ensure!( |
| 22 | + std::env::var("HOME")? == root.to_string_lossy(), |
| 23 | + "HOME must be isolated" |
| 24 | + ); |
| 25 | + anyhow::ensure!( |
| 26 | + std::env::var("BUZZ_MESH_IROH_RELAYS")? == "0", |
| 27 | + "public relays must be disabled" |
| 28 | + ); |
| 29 | + let runtime = tokio::runtime::Builder::new_multi_thread() |
| 30 | + .enable_all() |
| 31 | + .build()?; |
| 32 | + let lifecycle = Lifecycle::default(); |
| 33 | + // stdin is the bounded parent/child control channel. EOF always requests shutdown. |
| 34 | + let result = (|| -> Result<()> { |
| 35 | + for line in std::io::stdin().lock().lines() { |
| 36 | + let command: Value = serde_json::from_str(&line?)?; |
| 37 | + let response = runtime.block_on(command_reply(&lifecycle, &root, command)); |
| 38 | + let response = match response { |
| 39 | + Ok(value) => json!({"ok": value}), |
| 40 | + Err(error) => json!({"error": format!("{error:#}")}), |
| 41 | + }; |
| 42 | + println!("BUZZ_SMOKE {}", response); |
| 43 | + std::io::stdout().flush()?; |
| 44 | + } |
| 45 | + Ok(()) |
| 46 | + })(); |
| 47 | + let stopped = runtime.block_on(lifecycle.stop_and_wait()); |
| 48 | + result.and(stopped) |
| 49 | +} |
| 50 | + |
| 51 | +async fn command_reply( |
| 52 | + lifecycle: &Lifecycle, |
| 53 | + root: &std::path::Path, |
| 54 | + command: Value, |
| 55 | +) -> Result<Value> { |
| 56 | + match command["op"].as_str().context("op required")? { |
| 57 | + "identity" => Ok(json!(ensure_owner_at(&root.join("owner.json"))?)), |
| 58 | + "start" => { |
| 59 | + let port = |name: &str| -> Result<u16> { |
| 60 | + Ok(u16::try_from( |
| 61 | + command[name].as_u64().context("port required")?, |
| 62 | + )?) |
| 63 | + }; |
| 64 | + let node = ClientConfig { |
| 65 | + api_port: port("api_port")?, |
| 66 | + console_port: port("console_port")?, |
| 67 | + owner_key: root.join("owner.json"), |
| 68 | + owner_id: ensure_owner_at(&root.join("owner.json"))?, |
| 69 | + trusted_owners: serde_json::from_value(command["owners"].clone())?, |
| 70 | + mesh_name: Some(command["mesh"].as_str().context("mesh required")?.into()), |
| 71 | + join_token: None, |
| 72 | + }; |
| 73 | + if let Some(model) = command["model"].as_str() { |
| 74 | + lifecycle.serve(ServeConfig { |
| 75 | + node, |
| 76 | + model: model.into(), |
| 77 | + max_vram_gb: None, |
| 78 | + })?; |
| 79 | + } else { |
| 80 | + lifecycle.start(node)?; |
| 81 | + } |
| 82 | + tokio::time::timeout(Duration::from_secs(240), async { |
| 83 | + while lifecycle.phase() == Phase::Starting { |
| 84 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 85 | + } |
| 86 | + }) |
| 87 | + .await?; |
| 88 | + anyhow::ensure!( |
| 89 | + lifecycle.phase() == Phase::Ready, |
| 90 | + "startup failed: {:?}", |
| 91 | + lifecycle.phase() |
| 92 | + ); |
| 93 | + Ok(json!({"serving": lifecycle.is_serving()})) |
| 94 | + } |
| 95 | + "status" => { |
| 96 | + let status = lifecycle.status().await?; |
| 97 | + Ok( |
| 98 | + json!({"payload": status.payload, "invite": status.invite_token, "api": status.api_base_url}), |
| 99 | + ) |
| 100 | + } |
| 101 | + "join" => { |
| 102 | + lifecycle.dial(command["token"].as_str().context("token required")?)?; |
| 103 | + Ok(json!(true)) |
| 104 | + } |
| 105 | + "stop" => { |
| 106 | + lifecycle.stop_and_wait().await?; |
| 107 | + Ok(json!(true)) |
| 108 | + } |
| 109 | + other => bail!("unknown operation {other}"), |
| 110 | + } |
| 111 | +} |
0 commit comments