| 1 |
use crate::config::Config; |
| 2 |
use crate::domain::{AppId, Target}; |
| 3 |
use crate::events::EventTx; |
| 4 |
use crate::ota::OtaRegistry; |
| 5 |
use crate::topology::{Host, HostTransport, Topology}; |
| 6 |
use metrics_exporter_prometheus::PrometheusHandle; |
| 7 |
use ops_exec::{AgentRpc, CapabilitySet, Executor, LocalExec, SshExec}; |
| 8 |
use sqlx::SqlitePool; |
| 9 |
use std::collections::HashMap; |
| 10 |
use std::sync::Arc; |
| 11 |
use std::sync::atomic::AtomicBool; |
| 12 |
use tokio::sync::Mutex; |
| 13 |
use tokio::task::AbortHandle; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
pub type ExecutorMap = HashMap<String, Arc<dyn Executor>>; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#[derive(Clone)] |
| 27 |
pub struct ActiveSlot { |
| 28 |
pub build_id: i64, |
| 29 |
pub abort: AbortHandle, |
| 30 |
pub cancel: Arc<AtomicBool>, |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub type ActiveBuilds = Arc<Mutex<HashMap<(AppId, Target), ActiveSlot>>>; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub type HostLocks = Arc<HashMap<String, Arc<Mutex<()>>>>; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
pub fn build_host_locks(topo: &Topology) -> HostLocks { |
| 50 |
Arc::new( |
| 51 |
topo.hosts |
| 52 |
.iter() |
| 53 |
.map(|h| (h.name.clone(), Arc::new(Mutex::new(())))) |
| 54 |
.collect(), |
| 55 |
) |
| 56 |
} |
| 57 |
|
| 58 |
#[derive(Clone)] |
| 59 |
pub struct AppState { |
| 60 |
pub pool: SqlitePool, |
| 61 |
pub topo: Arc<Topology>, |
| 62 |
pub cfg: Arc<Config>, |
| 63 |
pub prom: PrometheusHandle, |
| 64 |
pub events: EventTx, |
| 65 |
pub ota: Arc<OtaRegistry>, |
| 66 |
|
| 67 |
|
| 68 |
pub executors: Arc<ExecutorMap>, |
| 69 |
|
| 70 |
|
| 71 |
pub syncs: Arc<ExecutorMap>, |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
pub api_token: Option<Arc<str>>, |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
pub active: ActiveBuilds, |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
pub host_locks: HostLocks, |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
pub distribution: DistributionCache, |
| 94 |
|
| 95 |
|
| 96 |
pub http: reqwest::Client, |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
pub mnw_base_url: Arc<str>, |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
pub type DistributionCache = |
| 105 |
Arc<Mutex<HashMap<(String, String), (std::time::Instant, crate::ota::Distribution)>>>; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
pub const DISTRIBUTION_TTL: std::time::Duration = std::time::Duration::from_mins(1); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
pub fn build_executor(host: &Host) -> Arc<dyn Executor> { |
| 121 |
let caps = CapabilitySet::from_tokens(&host.actuate, &host.observe); |
| 122 |
match host.transport { |
| 123 |
HostTransport::Agent => { |
| 124 |
|
| 125 |
let url = host.agent_url.clone().unwrap_or_default(); |
| 126 |
Arc::new(AgentRpc::new(url, host.name.clone(), caps)) |
| 127 |
} |
| 128 |
HostTransport::Ssh if host.ssh == "local" || host.ssh.is_empty() => { |
| 129 |
Arc::new(LocalExec::new(caps)) |
| 130 |
} |
| 131 |
HostTransport::Ssh => Arc::new(SshExec::new(host.ssh.clone(), caps)), |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
pub fn build_sync(host: &Host) -> Arc<dyn Executor> { |
| 157 |
let caps = CapabilitySet::from_tokens(&host.actuate, &host.observe); |
| 158 |
if host.ssh == "local" || host.ssh.is_empty() { |
| 159 |
let exec = LocalExec::new(caps); |
| 160 |
let exec = match &host.pull_root { |
| 161 |
Some(root) => exec.with_pull_root(root), |
| 162 |
None => exec, |
| 163 |
}; |
| 164 |
Arc::new(exec) |
| 165 |
} else { |
| 166 |
let exec = SshExec::new(host.ssh.clone(), caps); |
| 167 |
let exec = match &host.pull_root { |
| 168 |
Some(root) => exec.with_pull_root(root), |
| 169 |
None => exec, |
| 170 |
}; |
| 171 |
Arc::new(exec) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
pub fn build_deploy_executor(d: &crate::topology::DeployTarget) -> Arc<dyn Executor> { |
| 188 |
let caps = CapabilitySet::from_tokens(["deploy", "restart"], ["build-log"]); |
| 189 |
if d.host == "local" || d.host.is_empty() { |
| 190 |
return Arc::new(LocalExec::new(caps)); |
| 191 |
} |
| 192 |
Arc::new(SshExec::new(d.host.clone(), caps).with_port(d.port)) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
pub fn build_executors(topo: &Topology) -> ExecutorMap { |
| 197 |
topo.hosts |
| 198 |
.iter() |
| 199 |
.map(|h| (h.name.clone(), build_executor(h))) |
| 200 |
.collect() |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
pub fn build_syncs(topo: &Topology) -> ExecutorMap { |
| 205 |
topo.hosts |
| 206 |
.iter() |
| 207 |
.map(|h| (h.name.clone(), build_sync(h))) |
| 208 |
.collect() |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(test)] |
| 212 |
mod tests { |
| 213 |
use super::*; |
| 214 |
use ops_exec::{Action, SyncOpts}; |
| 215 |
|
| 216 |
fn host(toml_host: &str) -> Host { |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#[derive(serde::Deserialize)] |
| 221 |
struct Hosts { |
| 222 |
#[serde(rename = "host")] |
| 223 |
hosts: Vec<Host>, |
| 224 |
} |
| 225 |
let parsed: Hosts = toml::from_str(toml_host).unwrap(); |
| 226 |
parsed.hosts.into_iter().next().unwrap() |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn build_host_executor_permits_build_not_sign() { |
| 231 |
let h = host("[[host]]\nname = \"fw13\"\nssh = \"local\"\ntargets = [\"linux/x86_64\"]"); |
| 232 |
let exec = build_executor(&h); |
| 233 |
assert!(exec.capabilities().permits(&Action::Build)); |
| 234 |
assert!(exec.capabilities().permits(&Action::Package)); |
| 235 |
assert!(!exec.capabilities().permits(&Action::Sign)); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
#[tokio::test] |
| 243 |
async fn agent_host_syncs_over_ssh_never_the_agent() { |
| 244 |
let h = host( |
| 245 |
"[[host]]\nname = \"mbp\"\nssh = \"mbp\"\ntargets = [\"macos/aarch64\"]\n\ |
| 246 |
transport = \"agent\"\nagent_url = \"http://mbp:8765\"\n\ |
| 247 |
actuate = [\"build\", \"sign\", \"notarize\", \"staple\"]\n\ |
| 248 |
observe = [\"build-log\", \"gatekeeper\", \"artifact\"]\n\ |
| 249 |
pull_root = \"/nonexistent\"", |
| 250 |
); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
let sync = build_sync(&h); |
| 257 |
let err = sync |
| 258 |
.pull_glob( |
| 259 |
"/nonexistent/*.dmg", |
| 260 |
std::path::Path::new("/tmp"), |
| 261 |
&SyncOpts::default(), |
| 262 |
) |
| 263 |
.await |
| 264 |
.expect_err("nothing matches, so this must error either way"); |
| 265 |
assert!( |
| 266 |
!err.to_string().contains("unsupported by design"), |
| 267 |
"sync transport for an agent host must NOT be AgentRpc: {err}" |
| 268 |
); |
| 269 |
assert!( |
| 270 |
!err.to_string().contains("no artifact root declared"), |
| 271 |
"the declared pull_root must let the pull reach the ssh transport: {err}" |
| 272 |
); |
| 273 |
|
| 274 |
let exec = build_executor(&h); |
| 275 |
let err = exec |
| 276 |
.pull_glob( |
| 277 |
"/x/*.dmg", |
| 278 |
std::path::Path::new("/tmp"), |
| 279 |
&SyncOpts::default(), |
| 280 |
) |
| 281 |
.await |
| 282 |
.expect_err("AgentRpc has no glob form"); |
| 283 |
assert!( |
| 284 |
err.to_string().contains("unsupported by design"), |
| 285 |
"exec plane is the agent: {err}" |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn local_host_syncs_locally() { |
| 291 |
let h = host("[[host]]\nname = \"fw13\"\nssh = \"local\"\ntargets = [\"linux/x86_64\"]"); |
| 292 |
|
| 293 |
assert!(build_sync(&h).capabilities().permits(&Action::Build)); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
#[tokio::test] |
| 301 |
async fn sync_pull_is_confined_to_declared_root() { |
| 302 |
let h = host( |
| 303 |
"[[host]]\nname = \"fw13\"\nssh = \"local\"\ntargets = [\"linux/x86_64\"]\n\ |
| 304 |
pull_root = \"/home/max/Code/Apps\"", |
| 305 |
); |
| 306 |
let sync = build_sync(&h); |
| 307 |
|
| 308 |
let err = sync |
| 309 |
.pull_file( |
| 310 |
std::path::Path::new("/home/max/.tauri/passwords.env"), |
| 311 |
std::path::Path::new("/tmp/out"), |
| 312 |
&SyncOpts::default(), |
| 313 |
) |
| 314 |
.await |
| 315 |
.expect_err("a path outside pull_root must be denied"); |
| 316 |
assert!( |
| 317 |
err.to_string() |
| 318 |
.contains("escapes the declared artifact root"), |
| 319 |
"{err}" |
| 320 |
); |
| 321 |
|
| 322 |
|
| 323 |
let bare = host("[[host]]\nname = \"fw13\"\nssh = \"local\"\ntargets = [\"linux/x86_64\"]"); |
| 324 |
let err = build_sync(&bare) |
| 325 |
.pull_file( |
| 326 |
std::path::Path::new("/home/max/Code/Apps/goingson/x.dmg"), |
| 327 |
std::path::Path::new("/tmp/out"), |
| 328 |
&SyncOpts::default(), |
| 329 |
) |
| 330 |
.await |
| 331 |
.expect_err("no pull_root ⇒ no pulls"); |
| 332 |
assert!( |
| 333 |
err.to_string().contains("no artifact root declared"), |
| 334 |
"{err}" |
| 335 |
); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
#[tokio::test] |
| 343 |
async fn host_locks_are_one_per_host_and_independent() { |
| 344 |
use crate::topology::Topology; |
| 345 |
let dir = tempfile::tempdir().unwrap(); |
| 346 |
let repo = dir.path().join("goingson"); |
| 347 |
std::fs::create_dir_all(&repo).unwrap(); |
| 348 |
std::fs::write(repo.join("bento.toml"), "targets = [\"macos/aarch64\"]\n").unwrap(); |
| 349 |
let topo = Topology::from_str_for_tests(&format!( |
| 350 |
"[[host]]\nname = \"fw13\"\nssh = \"local\"\ntargets = [\"linux/x86_64\"]\n\ |
| 351 |
[[host]]\nname = \"mbp\"\nssh = \"mbp\"\ntargets = [\"macos/aarch64\"]\n\ |
| 352 |
[app.goingson]\nrepo = \"{}\"\n", |
| 353 |
repo.display() |
| 354 |
)) |
| 355 |
.unwrap(); |
| 356 |
let locks = build_host_locks(&topo); |
| 357 |
assert_eq!(locks.len(), 2); |
| 358 |
let mbp = locks.get("mbp").expect("mbp lock").clone(); |
| 359 |
let _held = mbp.clone().lock_owned().await; |
| 360 |
|
| 361 |
assert!(mbp.try_lock().is_err(), "same-host builds must serialize"); |
| 362 |
|
| 363 |
assert!( |
| 364 |
locks.get("fw13").expect("fw13 lock").try_lock().is_ok(), |
| 365 |
"a different host must not be blocked" |
| 366 |
); |
| 367 |
} |
| 368 |
|
| 369 |
#[test] |
| 370 |
fn agent_host_executor_permits_sign() { |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
let h = host( |
| 375 |
"[[host]]\nname = \"mbp\"\nssh = \"mbp\"\ntargets = [\"macos/aarch64\"]\n\ |
| 376 |
transport = \"agent\"\nagent_url = \"http://mbp:8765\"\n\ |
| 377 |
actuate = [\"build\", \"sign\", \"notarize\", \"staple\"]", |
| 378 |
); |
| 379 |
let exec = build_executor(&h); |
| 380 |
assert!(exec.capabilities().permits(&Action::Sign)); |
| 381 |
assert!(exec.capabilities().permits(&Action::Notarize)); |
| 382 |
assert!(exec.capabilities().permits(&Action::Build)); |
| 383 |
} |
| 384 |
} |
| 385 |
|