use std::fs; use std::path::{Path, PathBuf}; use anyhow::{bail, Context, Result}; use tera::{Context as TeraContext, Tera}; use crate::config::Config; const TEMPLATE_FILES: &[&str] = &[ "hub.compose.yml.tera", "satellite.compose.yml.tera", "hub/prometheus/prometheus.yml.tera", "hub/loki/loki.yml.tera", "hub/tempo/tempo.yml.tera", "hub/grafana/grafana.ini.tera", "hub/grafana/provisioning/datasources/datasources.yml.tera", "hub/caddy/Caddyfile.tera", "satellite/config.alloy.tera", ]; fn templates_dir() -> Result { if let Ok(dir) = std::env::var("SOPE_TEMPLATES") { let p = PathBuf::from(dir); if p.is_dir() { return Ok(p); } bail!("SOPE_TEMPLATES is not a directory: {}", p.display()); } let candidates = [ PathBuf::from("templates"), PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("templates"), ]; for c in candidates { if c.is_dir() { return Ok(c); } } bail!("templates not found — set SOPE_TEMPLATES or run from the repo root") } fn tera() -> Result { let dir = templates_dir()?; let mut tera = Tera::default(); tera.autoescape_on(vec![]); for rel in TEMPLATE_FILES { let path = dir.join(rel); let content = fs::read_to_string(&path) .with_context(|| format!("read template {}", path.display()))?; tera.add_raw_template(rel, &content) .with_context(|| format!("parse template {rel}"))?; } Ok(tera) } fn write_rendered(path: &Path, content: &str) -> Result<()> { if let Some(parent) = path.parent() { fs::create_dir_all(parent)?; } fs::write(path, content).with_context(|| format!("write {}", path.display()))?; Ok(()) } fn base_ctx(cfg: &Config) -> Result { let mut ctx = TeraContext::new(); ctx.insert("image_tag", "latest"); ctx.insert("auth_token", &cfg.auth.token); ctx.insert("grafana_admin_user", &cfg.grafana.admin_user); ctx.insert("grafana_admin_password", &cfg.grafana.admin_password); ctx.insert("grafana_port", &cfg.hub.grafana_port); ctx.insert("grafana_bind", &cfg.hub.grafana_bind); ctx.insert("ingest_bind", &cfg.hub.ingest_bind); ctx.insert("hub_host", &cfg.hub.host); Ok(ctx) } pub fn render_hub(cfg: &Config, out: &Path) -> Result<()> { let tera = tera()?; let ctx = base_ctx(cfg)?; let files = [ ("hub.compose.yml.tera", "docker-compose.yml"), ("hub/prometheus/prometheus.yml.tera", "prometheus/prometheus.yml"), ("hub/loki/loki.yml.tera", "loki/loki.yml"), ("hub/tempo/tempo.yml.tera", "tempo/tempo.yml"), ("hub/grafana/grafana.ini.tera", "grafana/grafana.ini"), ( "hub/grafana/provisioning/datasources/datasources.yml.tera", "grafana/provisioning/datasources/datasources.yml", ), ("hub/caddy/Caddyfile.tera", "caddy/Caddyfile"), ]; for (tmpl, dest) in files { let rendered = tera .render(tmpl, &ctx) .with_context(|| format!("render {tmpl}"))?; write_rendered(&out.join(dest), &rendered)?; } Ok(()) } pub fn render_satellite(cfg: &Config, name: &str, out: &Path) -> Result<()> { let sat = cfg.satellite(name)?; let colocated = cfg.same_host(sat); let tera = tera()?; let mut ctx = base_ctx(cfg)?; ctx.insert("satellite_name", &sat.name); // Same host: Alloy reaches local Caddy via loopback (avoids hairpin DNS). ctx.insert( "hub_host", if colocated { "127.0.0.1" } else { cfg.hub.host.as_str() }, ); // Same host: hub Caddy already owns :4317/:4318 — shift satellite OTLP host ports. ctx.insert("otlp_grpc_port", if colocated { "14317" } else { "4317" }); ctx.insert("otlp_http_port", if colocated { "14318" } else { "4318" }); let compose = tera.render("satellite.compose.yml.tera", &ctx)?; write_rendered(&out.join("docker-compose.yml"), &compose)?; let alloy = tera.render("satellite/config.alloy.tera", &ctx)?; write_rendered(&out.join("config.alloy"), &alloy)?; Ok(()) } pub fn render_all(cfg: &Config, out: &Path) -> Result<()> { render_hub(cfg, &out.join("hub"))?; for sat in &cfg.satellites { render_satellite(cfg, &sat.name, &out.join("satellites").join(&sat.name))?; } Ok(()) }