orch-cli 0.112.0

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
use std::iter;

use crate::store::cluster;
use crate::store::cluster::{discovery, local};

/// The cluster's members as this machine's service sees them
pub async fn run() -> anyhow::Result<()> {
    if !cluster::enabled() {
        println!("orch runs on this machine only: the config has no cluster section");
        return Ok(());
    }

    let Some(endpoint) = local::daemon_endpoint().await else {
        anyhow::bail!("the orch service is not running");
    };
    let members = local::fetch_nodes(&endpoint).await?;
    let tailnet = discovery::status().await.ok();
    let name_of = |addr: &str| {
        let ip = addr.split(':').next().unwrap_or_default();
        tailnet
            .as_ref()
            .and_then(|t| {
                iter::once(&t.me)
                    .chain(&t.peers)
                    .find(|h| h.ip == ip)
                    .map(|h| (h.name.clone(), h.online))
            })
            .unwrap_or_else(|| (ip.to_string(), true))
    };
    println!(
        "{:<20} {:<22} {:<8} {:<10} STATE",
        "NODE", "ADDRESS", "ROLE", "VERSION"
    );
    for m in members {
        let (name, online) = name_of(&m.addr);
        let role = if m.voter { "voter" } else { "learner" };
        let version = m.version.map_or_else(|| "-".to_string(), |v| v.to_string());
        let mut state = Vec::new();
        let needs = m.needs_upgrade.map(|v| format!("needs upgrade to {v}"));
        if let Some(needs) = &needs {
            state.push(needs.as_str());
        }
        if m.leader {
            state.push("leader");
        }
        if m.me {
            state.push("this machine");
        }
        if !online {
            state.push("offline");
        }
        let failed = m
            .answered
            .and_then(|a| a.upgrade_error)
            .map(|e| format!("upgrade failed: {e}"));
        if let Some(failed) = &failed {
            state.push(failed.as_str());
        }
        println!(
            "{name:<20} {:<22} {role:<8} {version:<10} {}",
            m.addr,
            state.join(", ")
        );
    }

    Ok(())
}