Something went wrong. Try again.
POC : Observability infrastructure managment
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268#![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<Vec<crate::status::ComposeService>> { 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<Vec<crate::status::ComposeService>> { 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<impl View + use<'_>> { Ok(view! { <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>"sope"</title> <style>(STYLESHEET)</style> </head> <body> <header class="topbar"> <div class="topbar-inner"> <a class="brand" href=(topcoat::router::href!(home))> <img class="brand-logo" src=(LOGO_WHITE) alt="Nicomatic" width="148" height="30"> <span class="brand-product">"sope"</span> </a> <nav class="top-nav" aria-label="Principal"> <a href=(topcoat::router::href!(home))>"Overview"</a> <a href=(topcoat::router::href!(hub::page))>"Hub"</a> <a href=(topcoat::router::href!(build::page))>"Build"</a> <a href=(topcoat::router::href!(settings::page))>"Settings"</a> </nav> <div class="top-actions"> <a class="btn-ghost" href=(topcoat::router::href!(settings::page))>"Config"</a> <a class="btn-primary" href="#deploy-all">"Deploy"</a> </div> </div> </header> <main class="wrap"> (slot) <p class="footer-note">"sope · hub & satellites · localhost only"</p> </main> </body> </html> })}
#[page]async fn home(cx: &Cx) -> Result<impl View + use<'_>> { 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<String>)> = 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! { <section class="page-hero"> <p class="crumb"> <span class="badge badge-new">"Local"</span> </p> <h1>"Build, ship, and run your observability stack"</h1> <p class="lede"> "Local admin: status, compose start/stop, live Nix build, sope.toml editing." </p> </section>
<div class="panel"> <div class="panel-head"> <div> <h2> <a class="node-link" href=(topcoat::router::href!(hub::page))>"Hub"</a> </h2> <p class="meta">(cfg.hub.user)"@"(cfg.hub.host)" · "(cfg.hub.path)</p> </div> <a class="btn-secondary btn" href=(grafana_url.clone()) target="_blank" rel="noopener">"Open Grafana"</a> </div> if let Some(err) = hub_err.as_ref() { <div class="err">(err.clone())</div> } else { <div class="resource"> <div class="resource-main"> <div class="resource-title"> <span class="node-link">"Compose services"</span> <span class=(if hub_running == hub_total && hub_total > 0 { "badge badge-ok" } else if hub_running == 0 { "badge badge-bad" } else { "badge badge-warn" })> (hub_running)"/"(hub_total)" running" </span> </div> <div class="resource-host">"Prometheus · Loki · Tempo · Grafana · Caddy"</div> </div> <a class="inline-link" href=(topcoat::router::href!(hub::page))>"View details →"</a> </div> } </div>
<div class="panel"> <div class="panel-head"> <div> <h2>"Satellites"</h2> <p class="meta">"Push Alloy + cAdvisor vers le hub"</p> </div> <a class="btn-secondary btn" href=(topcoat::router::href!(settings::page))>"Edit config"</a> </div> if sat_rows.is_empty() { <p class="meta">"No satellites — add some in Settings."</p> } else { <div class="stack"> for (sat_name, host, running, total, err) in sat_rows.iter() { <div class="resource"> <div class="resource-main"> <div class="resource-title"> <a class="node-link" href=(topcoat::router::href!(satellites::name::page, name: sat_name.clone()))> (sat_name.clone()) </a> if err.is_none() { <span class=(if *running == *total && *total > 0 { "badge badge-ok" } else if *running == 0 { "badge badge-bad" } else { "badge badge-warn" })> (*running)"/"(*total)" running" </span> } </div> <div class="resource-host">(host.clone())</div> if let Some(e) = err { <div class="err">(e.clone())</div> } </div> <a class="inline-link" href=(topcoat::router::href!(satellites::name::page, name: sat_name.clone()))> "Manage →" </a> </div> } </div> } </div>
<div class="panel" id="deploy-all"> <div class="action-band"> <div> <h2>"Deploy everything"</h2> <p>"Hub then every satellite. Skip-build by default."</p> </div> <form method="post" action=(topcoat::router::href!(deploy::action))> <input type="hidden" name="target" value="all"> <div class="actions"> <label class="check"> <input type="checkbox" name="rebuild" value="1"> "Rebuild images" </label> <button type="submit">"Deploy all"</button> </div> </form> </div> </div>
<div class="panel danger-zone"> <h2>"Danger zone"</h2> <p class="meta"> "Destroy all: compose down -v, sope images, rm -rf remote paths. Irreversible." </p> <form method="post" action=(topcoat::router::href!(destroy::action))> <input type="hidden" name="target" value="all"> <div class="actions"> <label class="check"> <input type="checkbox" name="confirm" value="destroy" required=(true)> "I confirm total destruction" </label> <button class="btn-danger" type="submit">"Destroy all"</button> </div> </form> </div> })}