| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
use crate::domain::{AppId, Target}; |
| 20 |
use anyhow::{Context, Result}; |
| 21 |
use serde::Deserialize; |
| 22 |
use std::collections::HashMap; |
| 23 |
use std::path::{Path, PathBuf}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#[derive(Debug, Clone)] |
| 34 |
pub struct Topology { |
| 35 |
pub hosts: Vec<Host>, |
| 36 |
|
| 37 |
pub app: HashMap<String, AppConfig>, |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
#[derive(Debug, Clone, Deserialize)] |
| 42 |
struct RawTopology { |
| 43 |
#[serde(default, rename = "host")] |
| 44 |
hosts: Vec<Host>, |
| 45 |
|
| 46 |
#[serde(default)] |
| 47 |
app: HashMap<String, AppPointer>, |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#[derive(Debug, Clone, Deserialize)] |
| 56 |
struct AppPointer { |
| 57 |
|
| 58 |
repo: String, |
| 59 |
|
| 60 |
#[serde(default)] |
| 61 |
repo_by_host: HashMap<String, String>, |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)] |
| 66 |
#[serde(rename_all = "snake_case")] |
| 67 |
pub enum Kind { |
| 68 |
|
| 69 |
#[default] |
| 70 |
App, |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
Library, |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
Service, |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
#[derive(Debug, Clone, Deserialize)] |
| 115 |
pub struct DeployTarget { |
| 116 |
|
| 117 |
pub target: Target, |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
pub host: String, |
| 122 |
|
| 123 |
#[serde(default)] |
| 124 |
pub port: Option<u16>, |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
pub install_path: String, |
| 129 |
|
| 130 |
|
| 131 |
pub service: String, |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#[serde(default)] |
| 136 |
pub health_url: Option<String>, |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
#[derive(Debug, Clone, Deserialize)] |
| 143 |
struct AppManifest { |
| 144 |
#[serde(default)] |
| 145 |
kind: Kind, |
| 146 |
#[serde(default = "default_branch")] |
| 147 |
branch: String, |
| 148 |
#[serde(default = "default_recipe_dir")] |
| 149 |
recipe_dir: String, |
| 150 |
#[serde(default)] |
| 151 |
version_path: Option<String>, |
| 152 |
#[serde(default)] |
| 153 |
features: Vec<String>, |
| 154 |
#[serde(default)] |
| 155 |
require_all_targets: bool, |
| 156 |
targets: Vec<Target>, |
| 157 |
|
| 158 |
#[serde(default, rename = "deploy")] |
| 159 |
deploy: Vec<DeployTarget>, |
| 160 |
#[serde(default = "default_tag_format")] |
| 161 |
tag_format: String, |
| 162 |
} |
| 163 |
|
| 164 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)] |
| 165 |
#[serde(rename_all = "snake_case")] |
| 166 |
pub enum HostTransport { |
| 167 |
|
| 168 |
|
| 169 |
#[default] |
| 170 |
Ssh, |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
Agent, |
| 176 |
} |
| 177 |
|
| 178 |
#[derive(Debug, Clone, Deserialize)] |
| 179 |
pub struct Host { |
| 180 |
pub name: String, |
| 181 |
|
| 182 |
pub ssh: String, |
| 183 |
|
| 184 |
|
| 185 |
#[serde(default)] |
| 186 |
pub targets: Vec<Target>, |
| 187 |
|
| 188 |
#[serde(default)] |
| 189 |
pub transport: HostTransport, |
| 190 |
|
| 191 |
|
| 192 |
#[serde(default)] |
| 193 |
pub agent_url: Option<String>, |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
#[serde(default = "default_actuate")] |
| 198 |
pub actuate: Vec<String>, |
| 199 |
|
| 200 |
#[serde(default = "default_observe")] |
| 201 |
pub observe: Vec<String>, |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
#[serde(default)] |
| 217 |
pub pull_root: Option<PathBuf>, |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#[serde(default)] |
| 228 |
pub pull_roots: Vec<PathBuf>, |
| 229 |
} |
| 230 |
|
| 231 |
impl Host { |
| 232 |
|
| 233 |
|
| 234 |
pub fn artifact_roots(&self) -> Vec<PathBuf> { |
| 235 |
self.pull_root |
| 236 |
.iter() |
| 237 |
.cloned() |
| 238 |
.chain(self.pull_roots.iter().cloned()) |
| 239 |
.collect() |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
fn default_actuate() -> Vec<String> { |
| 247 |
vec!["build".into(), "package".into()] |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
fn default_observe() -> Vec<String> { |
| 254 |
vec!["build-log".into(), "artifact".into()] |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
#[derive(Debug, Clone)] |
| 259 |
pub struct AppConfig { |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
pub repo: String, |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
pub repo_by_host: HashMap<String, String>, |
| 279 |
|
| 280 |
pub kind: Kind, |
| 281 |
pub branch: String, |
| 282 |
|
| 283 |
pub recipe_dir: String, |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
pub version_path: Option<String>, |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
pub features: Vec<String>, |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
pub require_all_targets: bool, |
| 307 |
|
| 308 |
pub targets: Vec<Target>, |
| 309 |
|
| 310 |
|
| 311 |
pub deploy: Vec<DeployTarget>, |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
pub tag_format: String, |
| 322 |
} |
| 323 |
|
| 324 |
fn default_tag_format() -> String { |
| 325 |
"v{version}".into() |
| 326 |
} |
| 327 |
|
| 328 |
impl AppConfig { |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
pub fn repo_for(&self, host: &str) -> &str { |
| 336 |
self.repo_by_host |
| 337 |
.get(host) |
| 338 |
.map_or(self.repo.as_str(), String::as_str) |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
pub fn deploy_for(&self, target: Target) -> Option<&DeployTarget> { |
| 343 |
self.deploy.iter().find(|d| d.target == target) |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
pub fn tag_for(&self, version: &crate::domain::Version) -> String { |
| 348 |
self.tag_format.replace("{version}", &version.to_string()) |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
fn default_branch() -> String { |
| 353 |
"main".into() |
| 354 |
} |
| 355 |
fn default_recipe_dir() -> String { |
| 356 |
"dist/recipes".into() |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
pub const APP_MANIFEST: &str = "bento.toml"; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
fn validate_deploy(name: &str, app: &AppConfig) -> Result<()> { |
| 371 |
if app.deploy.is_empty() { |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
return Ok(()); |
| 384 |
} |
| 385 |
anyhow::ensure!( |
| 386 |
app.kind == Kind::Service, |
| 387 |
"app `{name}` declares [[deploy]] entries but is not `kind = \"service\"`; \ |
| 388 |
only a service is installed onto a host" |
| 389 |
); |
| 390 |
let mut seen = Vec::new(); |
| 391 |
for d in &app.deploy { |
| 392 |
anyhow::ensure!( |
| 393 |
app.targets.contains(&d.target), |
| 394 |
"app `{name}`: [[deploy]] names target {} which the app does not ship", |
| 395 |
d.target |
| 396 |
); |
| 397 |
anyhow::ensure!( |
| 398 |
!seen.contains(&d.target), |
| 399 |
"app `{name}`: two [[deploy]] entries for target {} — \ |
| 400 |
one target installs to one place", |
| 401 |
d.target |
| 402 |
); |
| 403 |
seen.push(d.target); |
| 404 |
anyhow::ensure!( |
| 405 |
!d.host.trim().is_empty(), |
| 406 |
"app `{name}`: [[deploy]] for {} has an empty host", |
| 407 |
d.target |
| 408 |
); |
| 409 |
|
| 410 |
anyhow::ensure!( |
| 411 |
d.install_path.starts_with('/') |
| 412 |
&& !Path::new(&d.install_path) |
| 413 |
.components() |
| 414 |
.any(|c| c == std::path::Component::ParentDir), |
| 415 |
"app `{name}`: install_path `{}` must be an absolute path with no `..`", |
| 416 |
d.install_path |
| 417 |
); |
| 418 |
|
| 419 |
|
| 420 |
anyhow::ensure!( |
| 421 |
d.service.ends_with(".service") |
| 422 |
&& !d.service.contains('/') |
| 423 |
&& !d.service.chars().any(char::is_whitespace), |
| 424 |
"app `{name}`: service `{}` must be a bare unit name ending in `.service`", |
| 425 |
d.service |
| 426 |
); |
| 427 |
} |
| 428 |
|
| 429 |
for t in &app.targets { |
| 430 |
anyhow::ensure!( |
| 431 |
seen.contains(t), |
| 432 |
"app `{name}`: target {t} has no [[deploy]] entry — \ |
| 433 |
every target a service ships must say where it lands" |
| 434 |
); |
| 435 |
} |
| 436 |
Ok(()) |
| 437 |
} |
| 438 |
|
| 439 |
impl Topology { |
| 440 |
pub fn load(path: &Path) -> Result<Self> { |
| 441 |
let raw = std::fs::read_to_string(path) |
| 442 |
.with_context(|| format!("reading topology at {}", path.display()))?; |
| 443 |
let raw: RawTopology = toml::from_str(&raw) |
| 444 |
.with_context(|| format!("parsing topology at {}", path.display()))?; |
| 445 |
Self::resolve(raw) |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
fn resolve(raw: RawTopology) -> Result<Self> { |
| 455 |
let mut app = HashMap::with_capacity(raw.app.len()); |
| 456 |
for (name, ptr) in raw.app { |
| 457 |
let manifest_path = crate::engine::expand_tilde(&ptr.repo).join(APP_MANIFEST); |
| 458 |
let text = std::fs::read_to_string(&manifest_path).with_context(|| { |
| 459 |
format!( |
| 460 |
"app `{name}`: reading {}. Per-app build config lives in the app's repo; \ |
| 461 |
create it there with `targets = [...]`", |
| 462 |
manifest_path.display() |
| 463 |
) |
| 464 |
})?; |
| 465 |
let m: AppManifest = toml::from_str(&text) |
| 466 |
.with_context(|| format!("app `{name}`: parsing {}", manifest_path.display()))?; |
| 467 |
app.insert( |
| 468 |
name, |
| 469 |
AppConfig { |
| 470 |
repo: ptr.repo, |
| 471 |
repo_by_host: ptr.repo_by_host, |
| 472 |
kind: m.kind, |
| 473 |
branch: m.branch, |
| 474 |
recipe_dir: m.recipe_dir, |
| 475 |
version_path: m.version_path, |
| 476 |
features: m.features, |
| 477 |
require_all_targets: m.require_all_targets, |
| 478 |
targets: m.targets, |
| 479 |
deploy: m.deploy, |
| 480 |
tag_format: m.tag_format, |
| 481 |
}, |
| 482 |
); |
| 483 |
} |
| 484 |
let topo = Topology { |
| 485 |
hosts: raw.hosts, |
| 486 |
app, |
| 487 |
}; |
| 488 |
topo.validate()?; |
| 489 |
Ok(topo) |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
#[cfg(test)] |
| 497 |
pub fn from_str_for_tests(s: &str) -> Result<Self> { |
| 498 |
Self::resolve(toml::from_str(s)?) |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
pub fn validate_delivery(&self, cfg: &crate::config::Config) -> Result<()> { |
| 519 |
for (name, app) in &self.app { |
| 520 |
if app.kind != Kind::Service { |
| 521 |
continue; |
| 522 |
} |
| 523 |
let hands_off = cfg.handoff.contains_key(name.as_str()); |
| 524 |
match (app.deploy.is_empty(), hands_off) { |
| 525 |
(true, false) => anyhow::bail!( |
| 526 |
"app `{name}` is a service but neither declares [[deploy]] entries nor has \ |
| 527 |
a [handoff.{name}] table in the daemon config — it would build and archive \ |
| 528 |
and never reach the host that runs it" |
| 529 |
), |
| 530 |
(false, true) => anyhow::bail!( |
| 531 |
"app `{name}` is a service that both declares [[deploy]] entries and has a \ |
| 532 |
[handoff.{name}] table — it would be installed by Bento AND handed to Sando \ |
| 533 |
to install. Pick one: Bento deploys it, or Sando does" |
| 534 |
), |
| 535 |
_ => {} |
| 536 |
} |
| 537 |
} |
| 538 |
Ok(()) |
| 539 |
} |
| 540 |
|
| 541 |
fn validate(&self) -> Result<()> { |
| 542 |
anyhow::ensure!( |
| 543 |
!self.hosts.is_empty(), |
| 544 |
"topology must declare at least one host" |
| 545 |
); |
| 546 |
anyhow::ensure!( |
| 547 |
!self.app.is_empty(), |
| 548 |
"topology must declare at least one app" |
| 549 |
); |
| 550 |
|
| 551 |
for (name, app) in &self.app { |
| 552 |
for t in &app.targets { |
| 553 |
if self.host_for(*t).is_none() { |
| 554 |
anyhow::bail!("app `{name}` ships target {t} but no host declares it"); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
anyhow::ensure!( |
| 560 |
app.tag_format.contains("{version}"), |
| 561 |
"app `{name}`: tag_format `{}` must contain `{{version}}`, or every \ |
| 562 |
release would resolve to the same tag", |
| 563 |
app.tag_format |
| 564 |
); |
| 565 |
anyhow::ensure!( |
| 566 |
!app.tag_format |
| 567 |
.chars() |
| 568 |
.any(|c| matches!(c, '"' | '`' | '$' | ';' | '&' | '|' | '\\' | ' ')), |
| 569 |
"app `{name}`: tag_format `{}` contains shell metacharacters", |
| 570 |
app.tag_format |
| 571 |
); |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
for host in app.repo_by_host.keys() { |
| 576 |
anyhow::ensure!( |
| 577 |
self.hosts.iter().any(|h| &h.name == host), |
| 578 |
"app `{name}`: repo_by_host names host `{host}`, which no [[host]] declares" |
| 579 |
); |
| 580 |
} |
| 581 |
validate_deploy(name, app)?; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
for h in &self.hosts { |
| 587 |
if !h.targets.is_empty() && !h.actuate.iter().any(|a| a == "build") { |
| 588 |
anyhow::bail!( |
| 589 |
"host `{}` declares buildable targets but is not granted the `build` capability", |
| 590 |
h.name |
| 591 |
); |
| 592 |
} |
| 593 |
if h.transport == HostTransport::Agent && h.agent_url.is_none() { |
| 594 |
anyhow::bail!( |
| 595 |
"host `{}` uses transport = \"agent\" but sets no agent_url", |
| 596 |
h.name |
| 597 |
); |
| 598 |
} |
| 599 |
} |
| 600 |
Ok(()) |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
pub fn host_for(&self, target: Target) -> Option<&Host> { |
| 605 |
self.hosts.iter().find(|h| h.targets.contains(&target)) |
| 606 |
} |
| 607 |
|
| 608 |
pub fn app(&self, app: &AppId) -> Option<&AppConfig> { |
| 609 |
self.app.get(app.as_str()) |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
#[cfg(test)] |
| 614 |
mod tests { |
| 615 |
use super::*; |
| 616 |
|
| 617 |
const HOSTS: &str = r#" |
| 618 |
[[host]] |
| 619 |
name = "fw13" |
| 620 |
ssh = "local" |
| 621 |
targets = ["linux/x86_64"] |
| 622 |
|
| 623 |
[[host]] |
| 624 |
name = "mbp" |
| 625 |
ssh = "mbp" |
| 626 |
targets = ["macos/aarch64", "ios/universal"] |
| 627 |
"#; |
| 628 |
|
| 629 |
const MANIFEST: &str = r#"targets = ["macos/aarch64", "linux/x86_64"] |
| 630 |
"#; |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
fn load_with(hosts: &str, manifest: &str) -> Result<(Topology, tempfile::TempDir)> { |
| 636 |
let dir = tempfile::tempdir().unwrap(); |
| 637 |
let repo = dir.path().join("goingson"); |
| 638 |
std::fs::create_dir_all(&repo).unwrap(); |
| 639 |
std::fs::write(repo.join(APP_MANIFEST), manifest).unwrap(); |
| 640 |
let daemon = format!("{hosts}\n[app.goingson]\nrepo = \"{}\"\n", repo.display()); |
| 641 |
Topology::from_str_for_tests(&daemon).map(|t| (t, dir)) |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
fn load_with_pointer( |
| 647 |
hosts: &str, |
| 648 |
manifest: &str, |
| 649 |
pointer_extra: &str, |
| 650 |
) -> Result<(Topology, tempfile::TempDir)> { |
| 651 |
let dir = tempfile::tempdir().unwrap(); |
| 652 |
let repo = dir.path().join("goingson"); |
| 653 |
std::fs::create_dir_all(&repo).unwrap(); |
| 654 |
std::fs::write(repo.join(APP_MANIFEST), manifest).unwrap(); |
| 655 |
let daemon = format!( |
| 656 |
"{hosts}\n[app.goingson]\nrepo = \"{}\"\n{pointer_extra}", |
| 657 |
repo.display() |
| 658 |
); |
| 659 |
Topology::from_str_for_tests(&daemon).map(|t| (t, dir)) |
| 660 |
} |
| 661 |
|
| 662 |
fn load(hosts: &str) -> Result<Topology> { |
| 663 |
load_with(hosts, MANIFEST).map(|(t, dir)| { |
| 664 |
std::mem::forget(dir); |
| 665 |
t |
| 666 |
}) |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn parses_and_resolves_hosts() { |
| 671 |
let t = load(HOSTS).unwrap(); |
| 672 |
assert_eq!(t.hosts.len(), 2); |
| 673 |
let target: Target = "macos/aarch64".parse().unwrap(); |
| 674 |
assert_eq!(t.host_for(target).unwrap().name, "mbp"); |
| 675 |
assert_eq!(t.app(&"goingson".into()).unwrap().branch, "main"); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
#[test] |
| 682 |
fn features_defaults_empty_and_parses_when_present() { |
| 683 |
let t = load(HOSTS).unwrap(); |
| 684 |
assert!(t.app(&"goingson".into()).unwrap().features.is_empty()); |
| 685 |
|
| 686 |
let (t, _dir) = load_with( |
| 687 |
HOSTS, |
| 688 |
"targets = [\"linux/x86_64\"]\nfeatures = [\"supernote\", \"extra\"]\n", |
| 689 |
) |
| 690 |
.unwrap(); |
| 691 |
assert_eq!( |
| 692 |
t.app(&"goingson".into()).unwrap().features, |
| 693 |
vec!["supernote".to_string(), "extra".to_string()] |
| 694 |
); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
#[test] |
| 701 |
fn repo_for_defaults_to_the_single_path_for_every_host() { |
| 702 |
let t = load(HOSTS).unwrap(); |
| 703 |
let app = t.app(&"goingson".into()).unwrap(); |
| 704 |
assert!(app.repo_by_host.is_empty()); |
| 705 |
for host in ["fw13", "mbp", "nobody"] { |
| 706 |
assert_eq!(app.repo_for(host), app.repo); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
#[test] |
| 713 |
fn repo_by_host_overrides_one_host_only() { |
| 714 |
let (t, _dir) = load_with_pointer( |
| 715 |
HOSTS, |
| 716 |
MANIFEST, |
| 717 |
"[app.goingson.repo_by_host]\nmbp = \"/Users/max/Code/Apps/goingson\"\n", |
| 718 |
) |
| 719 |
.unwrap(); |
| 720 |
let app = t.app(&"goingson".into()).unwrap(); |
| 721 |
assert_eq!(app.repo_for("mbp"), "/Users/max/Code/Apps/goingson"); |
| 722 |
assert_eq!(app.repo_for("fw13"), app.repo); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
#[test] |
| 728 |
fn repo_by_host_naming_an_unknown_host_is_rejected() { |
| 729 |
let err = load_with_pointer( |
| 730 |
HOSTS, |
| 731 |
MANIFEST, |
| 732 |
"[app.goingson.repo_by_host]\nwindows-x86 = \"C:/Users/me/Code/Apps/goingson\"\n", |
| 733 |
) |
| 734 |
.unwrap_err(); |
| 735 |
assert!(format!("{err:#}").contains("windows-x86"), "{err:#}"); |
| 736 |
} |
| 737 |
|
| 738 |
#[test] |
| 739 |
fn rejects_target_without_a_host() { |
| 740 |
let only_linux = r#" |
| 741 |
[[host]] |
| 742 |
name = "fw13" |
| 743 |
ssh = "local" |
| 744 |
targets = ["linux/x86_64"] |
| 745 |
"#; |
| 746 |
assert!(load_with(only_linux, "targets = [\"windows/x86_64\"]\n").is_err()); |
| 747 |
} |
| 748 |
|
| 749 |
#[test] |
| 750 |
fn capability_defaults_make_a_build_host() { |
| 751 |
let t = load(HOSTS).unwrap(); |
| 752 |
let fw13 = t.hosts.iter().find(|h| h.name == "fw13").unwrap(); |
| 753 |
assert_eq!(fw13.transport, HostTransport::Ssh); |
| 754 |
assert!(fw13.actuate.contains(&"build".to_string())); |
| 755 |
assert!(fw13.actuate.contains(&"package".to_string())); |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn agent_transport_parses_with_url_and_caps() { |
| 760 |
let (t, _dir) = load_with( |
| 761 |
r#" |
| 762 |
[[host]] |
| 763 |
name = "mbp" |
| 764 |
ssh = "mbp" |
| 765 |
targets = ["macos/aarch64"] |
| 766 |
transport = "agent" |
| 767 |
agent_url = "http://mbp:8765" |
| 768 |
actuate = ["build", "sign", "notarize", "staple"] |
| 769 |
"#, |
| 770 |
"targets = [\"macos/aarch64\"]\n", |
| 771 |
) |
| 772 |
.unwrap(); |
| 773 |
let mbp = &t.hosts[0]; |
| 774 |
assert_eq!(mbp.transport, HostTransport::Agent); |
| 775 |
assert_eq!(mbp.agent_url.as_deref(), Some("http://mbp:8765")); |
| 776 |
assert!(mbp.actuate.contains(&"sign".to_string())); |
| 777 |
} |
| 778 |
|
| 779 |
#[test] |
| 780 |
fn agent_host_without_url_is_rejected() { |
| 781 |
let bad = r#" |
| 782 |
[[host]] |
| 783 |
name = "mbp" |
| 784 |
ssh = "mbp" |
| 785 |
targets = ["macos/aarch64"] |
| 786 |
transport = "agent" |
| 787 |
|
| 788 |
"#; |
| 789 |
assert!(load(bad).is_err()); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
#[test] |
| 797 |
fn tag_format_defaults_to_v_and_can_name_the_product() { |
| 798 |
let v = |s: &str| crate::domain::Version::parse(s).unwrap(); |
| 799 |
|
| 800 |
let t = load(HOSTS).unwrap(); |
| 801 |
let app = t.app(&"goingson".into()).unwrap(); |
| 802 |
assert_eq!(app.tag_format, "v{version}"); |
| 803 |
assert_eq!(app.tag_for(&v("0.4.1")), "v0.4.1"); |
| 804 |
|
| 805 |
let (t, _dir) = load_with( |
| 806 |
HOSTS, |
| 807 |
"targets = [\"linux/x86_64\"]\ntag_format = \"pom-v{version}\"\n", |
| 808 |
) |
| 809 |
.unwrap(); |
| 810 |
assert_eq!( |
| 811 |
t.app(&"goingson".into()).unwrap().tag_for(&v("0.4.1")), |
| 812 |
"pom-v0.4.1" |
| 813 |
); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
#[test] |
| 820 |
fn tag_format_must_vary_and_stay_shell_safe() { |
| 821 |
let bad = |f: &str| { |
| 822 |
load_with( |
| 823 |
HOSTS, |
| 824 |
&format!("targets = [\"linux/x86_64\"]\ntag_format = \"{f}\"\n"), |
| 825 |
) |
| 826 |
.is_err() |
| 827 |
}; |
| 828 |
assert!(bad("release"), "a constant tag pins every release together"); |
| 829 |
assert!(bad("v{version}; rm -rf /")); |
| 830 |
assert!(bad("v{version}$(id)")); |
| 831 |
assert!(bad("v{version} extra")); |
| 832 |
assert!(!bad("pom-v{version}")); |
| 833 |
assert!(!bad("release/{version}")); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
#[test] |
| 840 |
fn service_deploy_entries_resolve_per_target() { |
| 841 |
let (t, _dir) = load_with( |
| 842 |
HOSTS, |
| 843 |
r#"kind = "service" |
| 844 |
targets = ["linux/x86_64", "macos/aarch64"] |
| 845 |
|
| 846 |
[[deploy]] |
| 847 |
target = "linux/x86_64" |
| 848 |
host = "root@prod" |
| 849 |
port = 2200 |
| 850 |
install_path = "/usr/local/bin/demo" |
| 851 |
service = "demo.service" |
| 852 |
health_url = "http://prod:9100/api/health" |
| 853 |
|
| 854 |
[[deploy]] |
| 855 |
target = "macos/aarch64" |
| 856 |
host = "mbp" |
| 857 |
install_path = "/usr/local/bin/demo" |
| 858 |
service = "demo.service" |
| 859 |
"#, |
| 860 |
) |
| 861 |
.unwrap(); |
| 862 |
let app = t.app(&"goingson".into()).unwrap(); |
| 863 |
assert_eq!(app.kind, Kind::Service); |
| 864 |
let x86 = app.deploy_for("linux/x86_64".parse().unwrap()).unwrap(); |
| 865 |
assert_eq!(x86.host, "root@prod"); |
| 866 |
assert_eq!(x86.port, Some(2200)); |
| 867 |
assert_eq!( |
| 868 |
x86.health_url.as_deref(), |
| 869 |
Some("http://prod:9100/api/health") |
| 870 |
); |
| 871 |
let mac = app.deploy_for("macos/aarch64".parse().unwrap()).unwrap(); |
| 872 |
assert_eq!(mac.host, "mbp"); |
| 873 |
assert_eq!(mac.port, None); |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
#[test] |
| 884 |
fn deploy_entries_belong_only_to_a_service() { |
| 885 |
let deploy = "\n[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 886 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n"; |
| 887 |
assert!( |
| 888 |
load_with(HOSTS, &format!("targets = [\"linux/x86_64\"]\n{deploy}")).is_err(), |
| 889 |
"only a service installs onto a host" |
| 890 |
); |
| 891 |
assert!( |
| 892 |
load_with(HOSTS, "kind = \"service\"\ntargets = [\"linux/x86_64\"]\n").is_ok(), |
| 893 |
"a service with no [[deploy]] parses; whether it hands off is validate_delivery's call" |
| 894 |
); |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
#[test] |
| 901 |
fn a_service_must_deploy_itself_or_hand_off_but_not_both() { |
| 902 |
let tmp = tempfile::tempdir().unwrap(); |
| 903 |
let mut cfg = crate::config::Config::for_tests(tmp.path()); |
| 904 |
|
| 905 |
let (handing_off, _keep) = |
| 906 |
load_with(HOSTS, "kind = \"service\"\ntargets = [\"linux/x86_64\"]\n").unwrap(); |
| 907 |
let name = handing_off.app.keys().next().unwrap().clone(); |
| 908 |
|
| 909 |
|
| 910 |
let err = handing_off.validate_delivery(&cfg).unwrap_err(); |
| 911 |
assert!( |
| 912 |
format!("{err:#}").contains("never reach the host"), |
| 913 |
"{err:#}" |
| 914 |
); |
| 915 |
|
| 916 |
|
| 917 |
cfg.handoff.insert( |
| 918 |
name.clone(), |
| 919 |
crate::config::Handoff { |
| 920 |
host: "local".into(), |
| 921 |
staging_root: "/srv/sando/staging".into(), |
| 922 |
url: "http://127.0.0.1:7766".into(), |
| 923 |
sando_app: None, |
| 924 |
token_env: None, |
| 925 |
}, |
| 926 |
); |
| 927 |
handing_off.validate_delivery(&cfg).unwrap(); |
| 928 |
|
| 929 |
|
| 930 |
let (self_deploying, _keep2) = load_with( |
| 931 |
HOSTS, |
| 932 |
"kind = \"service\"\ntargets = [\"linux/x86_64\"]\n\ |
| 933 |
[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 934 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n", |
| 935 |
) |
| 936 |
.unwrap(); |
| 937 |
let err = self_deploying.validate_delivery(&cfg).unwrap_err(); |
| 938 |
assert!(format!("{err:#}").contains("Pick one"), "{err:#}"); |
| 939 |
|
| 940 |
|
| 941 |
let (plain, _keep3) = load_with(HOSTS, "targets = [\"linux/x86_64\"]\n").unwrap(); |
| 942 |
plain.validate_delivery(&cfg).unwrap(); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
#[test] |
| 948 |
fn service_target_without_a_deploy_entry_is_rejected() { |
| 949 |
let err = load_with( |
| 950 |
HOSTS, |
| 951 |
"kind = \"service\"\ntargets = [\"linux/x86_64\", \"macos/aarch64\"]\n\ |
| 952 |
[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 953 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n", |
| 954 |
) |
| 955 |
.unwrap_err(); |
| 956 |
assert!( |
| 957 |
format!("{err:#}").contains("macos/aarch64"), |
| 958 |
"must name the target with no destination: {err:#}" |
| 959 |
); |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
#[test] |
| 967 |
fn deploy_rejects_paths_and_units_the_installer_would_refuse() { |
| 968 |
let entry = |install: &str, service: &str| { |
| 969 |
format!( |
| 970 |
"kind = \"service\"\ntargets = [\"linux/x86_64\"]\n\ |
| 971 |
[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 972 |
install_path = \"{install}\"\nservice = \"{service}\"\n" |
| 973 |
) |
| 974 |
}; |
| 975 |
|
| 976 |
|
| 977 |
assert!(load_with(HOSTS, &entry("usr/local/bin/d", "d.service")).is_err()); |
| 978 |
assert!(load_with(HOSTS, &entry("/opt/../etc/systemd/system/x", "d.service")).is_err()); |
| 979 |
|
| 980 |
|
| 981 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "/etc/x.service")).is_err()); |
| 982 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "d.service x")).is_err()); |
| 983 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "d")).is_err()); |
| 984 |
|
| 985 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "d.service")).is_ok()); |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
#[test] |
| 991 |
fn duplicate_deploy_entries_for_one_target_are_rejected() { |
| 992 |
let e = "[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 993 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n"; |
| 994 |
assert!( |
| 995 |
load_with( |
| 996 |
HOSTS, |
| 997 |
&format!("kind = \"service\"\ntargets = [\"linux/x86_64\"]\n{e}{e}") |
| 998 |
) |
| 999 |
.is_err() |
| 1000 |
); |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
#[test] |
| 1006 |
fn deploy_entry_for_an_unshipped_target_is_rejected() { |
| 1007 |
assert!( |
| 1008 |
load_with( |
| 1009 |
HOSTS, |
| 1010 |
"kind = \"service\"\ntargets = [\"linux/x86_64\"]\n\ |
| 1011 |
[[deploy]]\ntarget = \"macos/aarch64\"\nhost = \"h\"\n\ |
| 1012 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n", |
| 1013 |
) |
| 1014 |
.is_err() |
| 1015 |
); |
| 1016 |
} |
| 1017 |
|
| 1018 |
#[test] |
| 1019 |
fn build_host_without_build_capability_is_rejected() { |
| 1020 |
let bad = r#" |
| 1021 |
[[host]] |
| 1022 |
name = "fw13" |
| 1023 |
ssh = "local" |
| 1024 |
targets = ["linux/x86_64"] |
| 1025 |
actuate = ["package"] |
| 1026 |
|
| 1027 |
"#; |
| 1028 |
assert!(load(bad).is_err()); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
#[cfg(test)] |
| 1033 |
mod live_config_smoke { |
| 1034 |
use super::*; |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
#[test] |
| 1041 |
fn live_topology_loads_if_present() { |
| 1042 |
let Some(home) = std::env::var_os("HOME") else { |
| 1043 |
return; |
| 1044 |
}; |
| 1045 |
let path = Path::new(&home).join(".config/bento/bento.toml"); |
| 1046 |
if !path.exists() { |
| 1047 |
return; |
| 1048 |
} |
| 1049 |
let topo = Topology::load(&path).expect("live bento.toml must load"); |
| 1050 |
topo.app(&"balanced_breakfast".into()) |
| 1051 |
.expect("bb configured"); |
| 1052 |
let af = topo |
| 1053 |
.app(&"audiofiles".into()) |
| 1054 |
.expect("audiofiles configured"); |
| 1055 |
assert_eq!( |
| 1056 |
af.version_path.as_deref(), |
| 1057 |
Some("crates/audiofiles-app/Cargo.toml") |
| 1058 |
); |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
for name in ["makeover", "makeover-touch", "pter", "alloy_tui"] { |
| 1065 |
let c = topo |
| 1066 |
.app(&name.into()) |
| 1067 |
.unwrap_or_else(|| panic!("{name} configured")); |
| 1068 |
assert_eq!(c.kind, Kind::Library, "{name} must be a library"); |
| 1069 |
} |
| 1070 |
assert_eq!(topo.app(&"goingson".into()).unwrap().kind, Kind::App); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|