| 1 |
use crate::config::Config; |
| 2 |
use crate::domain::NodeId; |
| 3 |
use crate::events::EventTx; |
| 4 |
use crate::topology::{Node, Topology}; |
| 5 |
use metrics_exporter_prometheus::PrometheusHandle; |
| 6 |
use ops_exec::{CapabilitySet, Executor, LocalExec, SshExec}; |
| 7 |
use sqlx::SqlitePool; |
| 8 |
use std::collections::HashMap; |
| 9 |
use std::sync::Arc; |
| 10 |
use tokio::sync::Mutex; |
| 11 |
use tokio::task::AbortHandle; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
pub type ExecutorMap = HashMap<NodeId, Arc<dyn Executor>>; |
| 17 |
|
| 18 |
#[derive(Clone)] |
| 19 |
pub struct AppState { |
| 20 |
pub pool: SqlitePool, |
| 21 |
pub topo: Arc<Topology>, |
| 22 |
pub cfg: Arc<Config>, |
| 23 |
pub prom: PrometheusHandle, |
| 24 |
|
| 25 |
|
| 26 |
pub active_build: Arc<Mutex<Option<AbortHandle>>>, |
| 27 |
|
| 28 |
|
| 29 |
pub events: EventTx, |
| 30 |
|
| 31 |
pub executors: Arc<ExecutorMap>, |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
pub fn build_executor(node: &Node) -> Arc<dyn Executor> { |
| 38 |
let caps = CapabilitySet::from_tokens(&node.actuate, &node.observe); |
| 39 |
if node.ssh_target == "local" || node.ssh_target.is_empty() { |
| 40 |
Arc::new(LocalExec::new(caps)) |
| 41 |
} else { |
| 42 |
Arc::new(SshExec::new(node.ssh_target.clone(), caps)) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
pub fn build_executors(topo: &Topology) -> ExecutorMap { |
| 48 |
topo.tiers |
| 49 |
.iter() |
| 50 |
.flat_map(|t| t.nodes.iter()) |
| 51 |
.map(|node| (node.name.clone(), build_executor(node))) |
| 52 |
.collect() |
| 53 |
} |
| 54 |
|