#![allow(refining_impl_trait)] mod build; mod compose; mod deploy; mod destroy; mod hub; mod satellites; mod settings; mod state; use std::future::Future; use std::net::SocketAddr; use std::path::PathBuf; use anyhow::{Context, Result as AnyResult}; use topcoat::context::{context, Cx}; use topcoat::router::{layout, module_router, page, Slot}; use topcoat::view::{View, view}; use topcoat::Result; use crate::config::Config; use satellites::name::name; pub use state::AppState; const STYLESHEET: &str = include_str!("styles.css"); const LOGO_WHITE: &str = include_str!("assets/logo_white.datauri"); pub fn app(cx: &Cx) -> &AppState { context(cx) } pub(crate) fn to_topcoat(err: anyhow::Error) -> topcoat::Error { topcoat::Error::msg(format!("{err:#}")) } pub fn run(config_path: PathBuf, config: Config, bind: &str, port: u16) -> AnyResult<()> { let addr: SocketAddr = format!("{bind}:{port}") .parse() .with_context(|| format!("invalid address: {bind}:{port}"))?; let rt = tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .context("tokio runtime")?; let state = AppState::new(config_path, config); rt.block_on(async move { let listener = tokio::net::TcpListener::bind(addr) .await .with_context(|| format!("bind {addr}"))?; let display = listener .local_addr() .map(|a| a.to_string()) .unwrap_or_else(|_| addr.to_string()); println!("sope ui → http://{display}"); println!("Ctrl+C to stop"); let router = module_router!().with(state).build(); topcoat::serve(listener, router) .await .context("serveur Topcoat")?; Ok(()) }) } pub(crate) async fn load_hub_status(cfg: Config) -> Result> { tokio::task::spawn_blocking(move || crate::status::status_hub_data(&cfg)) .await .map_err(|e| topcoat::Error::msg(e.to_string()))? .map_err(to_topcoat) } pub(crate) async fn load_sat_status( cfg: Config, name: String, ) -> Result> { tokio::task::spawn_blocking(move || crate::status::status_satellite_data(&cfg, &name)) .await .map_err(|e| topcoat::Error::msg(e.to_string()))? .map_err(to_topcoat) } #[layout] async fn root_layout(cx: &Cx, slot: Slot<'_>) -> Result> { Ok(view! { "sope"
(slot)
}) } #[page] async fn home(cx: &Cx) -> Result> { let cfg = app(cx).config_snapshot(); let (hub_services, hub_err) = match load_hub_status(cfg.clone()).await { Ok(s) => (s, None), Err(e) => (Vec::new(), Some(format!("{e}"))), }; let hub_running = hub_services.iter().filter(|s| s.is_running()).count(); let hub_total = hub_services.len(); let grafana_url = format!("http://{}:{}", cfg.hub.host, cfg.hub.grafana_port); let mut sat_rows: Vec<(String, String, usize, usize, Option)> = Vec::new(); for sat in &cfg.satellites { let name = sat.name.clone(); let host = sat.host.clone(); match load_sat_status(cfg.clone(), name.clone()).await { Ok(svcs) => { let running = svcs.iter().filter(|s| s.is_running()).count(); sat_rows.push((name, host, running, svcs.len(), None)); } Err(e) => { sat_rows.push((name, host, 0, 0, Some(format!("{e}")))); } } } Ok(view! {

"Local"

"Build, ship, and run your observability stack"

"Local admin: status, compose start/stop, live Nix build, sope.toml editing."

"Hub"

(cfg.hub.user)"@"(cfg.hub.host)" · "(cfg.hub.path)

"Open Grafana"
if let Some(err) = hub_err.as_ref() {
(err.clone())
} else {
"Compose services" 0 { "badge badge-ok" } else if hub_running == 0 { "badge badge-bad" } else { "badge badge-warn" })> (hub_running)"/"(hub_total)" running"
"Prometheus · Loki · Tempo · Grafana · Caddy"
"View details →"
}

"Satellites"

"Push Alloy + cAdvisor vers le hub"

"Edit config"
if sat_rows.is_empty() {

"No satellites — add some in Settings."

} else {
for (sat_name, host, running, total, err) in sat_rows.iter() {
(sat_name.clone()) if err.is_none() { 0 { "badge badge-ok" } else if *running == 0 { "badge badge-bad" } else { "badge badge-warn" })> (*running)"/"(*total)" running" }
(host.clone())
if let Some(e) = err {
(e.clone())
}
"Manage →"
}
}

"Deploy everything"

"Hub then every satellite. Skip-build by default."

"Danger zone"

"Destroy all: compose down -v, sope images, rm -rf remote paths. Irreversible."

}) }