| 1 |
use crate::config::AppConfig; |
| 2 |
use crate::domain::{AppId, NodeId}; |
| 3 |
use crate::events::EventTx; |
| 4 |
use crate::topology::{Node, Topology}; |
| 5 |
use ops_exec::{CapabilitySet, Executor, LocalExec, SshExec}; |
| 6 |
use sqlx::SqlitePool; |
| 7 |
use std::collections::HashMap; |
| 8 |
use std::sync::Arc; |
| 9 |
use tokio::sync::Mutex; |
| 10 |
use tokio::task::AbortHandle; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
pub type ExecutorMap = HashMap<NodeId, Arc<dyn Executor>>; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
pub struct ActiveBuild { |
| 20 |
pub handle: AbortHandle, |
| 21 |
pub run_id: crate::domain::RunId, |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
pub struct App { |
| 32 |
pub cfg: Arc<AppConfig>, |
| 33 |
pub topo: Arc<Topology>, |
| 34 |
pub executors: Arc<ExecutorMap>, |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
pub type AppMap = std::collections::BTreeMap<AppId, Arc<App>>; |
| 39 |
|
| 40 |
#[derive(Clone)] |
| 41 |
pub struct AppState { |
| 42 |
pub pool: SqlitePool, |
| 43 |
|
| 44 |
|
| 45 |
pub apps: Arc<AppMap>, |
| 46 |
|
| 47 |
|
| 48 |
pub default_app: AppId, |
| 49 |
pub topo: Arc<Topology>, |
| 50 |
pub cfg: Arc<AppConfig>, |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub active_build: Arc<Mutex<Option<ActiveBuild>>>, |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
pub deploy_lock: Arc<Mutex<()>>, |
| 62 |
|
| 63 |
|
| 64 |
pub events: EventTx, |
| 65 |
|
| 66 |
pub executors: Arc<ExecutorMap>, |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub api_token: Option<Arc<str>>, |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
pub fn one_app( |
| 80 |
cfg: Arc<AppConfig>, |
| 81 |
topo: Arc<Topology>, |
| 82 |
executors: Arc<ExecutorMap>, |
| 83 |
) -> (Arc<AppMap>, AppId) { |
| 84 |
let id = cfg.id.clone(); |
| 85 |
let mut apps = AppMap::new(); |
| 86 |
apps.insert( |
| 87 |
id.clone(), |
| 88 |
Arc::new(App { |
| 89 |
cfg, |
| 90 |
topo, |
| 91 |
executors, |
| 92 |
}), |
| 93 |
); |
| 94 |
(Arc::new(apps), id) |
| 95 |
} |
| 96 |
|
| 97 |
impl AppState { |
| 98 |
|
| 99 |
pub fn app(&self, id: &AppId) -> Option<&Arc<App>> { |
| 100 |
self.apps.get(id) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn app_ids(&self) -> Vec<AppId> { |
| 106 |
self.apps.keys().cloned().collect() |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
pub fn build_executor(node: &Node) -> Arc<dyn Executor> { |
| 114 |
let caps = CapabilitySet::from_tokens(&node.actuate, &node.observe); |
| 115 |
if node.ssh_target == "local" || node.ssh_target.is_empty() { |
| 116 |
Arc::new(LocalExec::new(caps)) |
| 117 |
} else { |
| 118 |
Arc::new(SshExec::new(node.ssh_target.clone(), caps)) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
pub fn build_executors(topo: &Topology) -> ExecutorMap { |
| 124 |
topo.tiers |
| 125 |
.iter() |
| 126 |
.flat_map(|t| t.nodes.iter()) |
| 127 |
.map(|node| (node.name.clone(), build_executor(node))) |
| 128 |
.collect() |
| 129 |
} |
| 130 |
|