diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69810715..c854fce4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -192,12 +192,12 @@ jobs: include: - os: windows-latest # The Compose variants need Linux containers: not run, not "passed". - skip: --skip lifecycle_with_compose --skip a_lane_has_its_own_containers --skip an_announced_port_moves_inside_a_lanes_container + skip: --skip lifecycle_with_compose --skip a_lane_has_its_own_containers --skip an_announced_port_moves_inside_a_lanes_container --skip a_profile_leaves_out_containers - os: ubuntu-latest skip: '' # No Docker on the macOS runners (ADR-0054). - os: macos-14 - skip: --skip lifecycle_with_compose --skip a_lane_has_its_own_containers --skip an_announced_port_moves_inside_a_lanes_container + skip: --skip lifecycle_with_compose --skip a_lane_has_its_own_containers --skip an_announced_port_moves_inside_a_lanes_container --skip a_profile_leaves_out_containers runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index f131cbac..3bff1415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,18 @@ and listed with their assets on the ## [Unreleased] +### Added + +- **A profile can leave out containers, whole Compose groups and services** + (ADR-0058, #244). `[profiles.] skip = ["compose.search", + "compose.app.scanner", "indexer"]` makes a light profile for daily work: + what it skips does not start with the tree, a skipped container an earlier + start left running is stopped, and each still starts on demand (`devstack + start compose.app.scanner`). A service that starts with the profile and + uses a skipped container's address is an error in the profile's check. A + lane on a node does not create what its profile skips. A file with `skip` + wants `requires = "0.21.0"`. + ## [0.20.0] - 2026-10-23 macOS as a developer platform in preview, installed with Homebrew on macOS diff --git a/crates/devstack-compose/src/lib.rs b/crates/devstack-compose/src/lib.rs index 9e4b7639..30944d23 100644 --- a/crates/devstack-compose/src/lib.rs +++ b/crates/devstack-compose/src/lib.rs @@ -761,6 +761,18 @@ impl Docker { self.run_group(g, &a, budget::STOP).map(|_| ()) } + /// Stop the group's selected services (`g.services`) where they run; + /// none selected: nothing (Compose would stop every one). + pub fn stop_selected(&self, g: &Group) -> Result<()> { + if g.services.is_empty() { + return Ok(()); + } + let mut a = g.base_args(); + a.extend(["stop".into(), "--timeout".into(), "10".into()]); + a.extend(g.services.iter().map(OsString::from)); + self.run_group(g, &a, budget::STOP).map(|_| ()) + } + /// Stop a container by its name (`docker stop`), whether or not the /// loaded Compose model knows its service. pub fn stop_container(&self, dir: &Path, name: &str) -> Result<()> { diff --git a/crates/devstack-config/src/lib.rs b/crates/devstack-config/src/lib.rs index 342d1c35..1f7536bd 100644 --- a/crates/devstack-config/src/lib.rs +++ b/crates/devstack-config/src/lib.rs @@ -47,6 +47,10 @@ pub const REVIEWED_SINCE: &str = "0.20.0"; /// (ADR-0057): a file that sets it wants `requires` at least this. pub const ANNOUNCED_SINCE: &str = "0.20.0"; +/// The first devstack that knows `[profiles.] skip` (ADR-0058): a +/// file that sets it wants `requires` at least this. +pub const SKIP_SINCE: &str = "0.21.0"; + /// `"1.2.3"`, `"v1.2"`, `"1"` as numbers; `None` when malformed. A /// pre-release or build suffix (`1.2.3-rc1`, `1.2.3+abc`) is ignored here; /// [`version_order`] takes it into account. diff --git a/crates/devstack-config/src/model.rs b/crates/devstack-config/src/model.rs index 5e35a631..231ce15c 100644 --- a/crates/devstack-config/src/model.rs +++ b/crates/devstack-config/src/model.rs @@ -6,7 +6,7 @@ use schemars::JsonSchema; use serde::Deserialize; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::path::PathBuf; use std::time::Duration; @@ -96,6 +96,11 @@ pub struct Profile { /// The services that start, instead of each service's own `autostart`. #[serde(default)] pub autostart: Option>, + /// What does not start with this profile (ADR-0058): a container + /// (`compose..`), every container of a group + /// (`compose.`), or a service. Still started on demand. + #[serde(default)] + pub skip: Vec, /// How single services run with this profile. #[serde(default)] pub services: BTreeMap, @@ -142,9 +147,48 @@ pub struct Active { pub data: DataMode, pub env: BTreeMap, pub command: Option, + /// The containers it leaves out, `(group, service)`, a skipped group's + /// every one (ADR-0058). + pub skip: BTreeSet<(String, String)>, } impl Stack { + /// The tree's start leaves container `service` of `group` out: the + /// profile it runs with skips it (ADR-0058). + pub fn skips(&self, group: &str, service: &str) -> bool { + self.profile + .as_ref() + .is_some_and(|p| p.skip.contains(&(group.to_string(), service.to_string()))) + } + + /// The containers profile `name` leaves out, `(group, service)`: what + /// [`Active::skip`] holds once it is applied. Entries that name nothing + /// are left out of it ([`Self::with_profile`] reports them). + pub fn skipped_containers(&self, name: &str) -> BTreeSet<(String, String)> { + let mut out = BTreeSet::new(); + let Some(p) = self.profiles.get(name) else { + return out; + }; + for e in &p.skip { + let Some(rest) = e.strip_prefix("compose.") else { + continue; + }; + let (group, one) = match rest.split_once('.') { + Some((g, s)) => (g, Some(s)), + None => (rest, None), + }; + let Some(g) = self.compose.get(group) else { + continue; + }; + for s in &g.services { + if one.is_none_or(|o| o == s) { + out.insert((group.to_string(), s.clone())); + } + } + } + out + } + /// The stack as profile `name` runs it (ADR-0041), checked like a stack /// file; the issues name the profile's keys. /// What a lane needs at least with `profile` (ADR-0049 point 5): @@ -270,6 +314,41 @@ impl Stack { svc.autostart = list.contains(n); } } + for (i, e) in p.skip.iter().enumerate() { + let at = format!("profiles.{name}.skip[{i}]"); + if let Some(rest) = e.strip_prefix("compose.") { + let (group, one) = match rest.split_once('.') { + Some((g, s)) => (g, Some(s)), + None => (rest, None), + }; + match (self.compose.get(group), one) { + (None, _) => issues.push(issue(at, format!("no compose group `{group}`"))), + (Some(g), Some(one)) if !g.services.iter().any(|s| s == one) => { + issues.push(issue( + at, + format!("`{one}` is not started by compose group `{group}` (not in its `services`)"), + )) + } + _ => {} + } + continue; + } + match s.services.get_mut(e) { + None => issues.push(issue( + at, + format!( + "no service `{e}` in `[services]` (a container is `compose..`, a group `compose.`)" + ), + )), + Some(_) if p.autostart.as_ref().is_some_and(|l| l.contains(e)) => { + issues.push(issue( + at, + format!("`{e}` is in the profile's `autostart` too: it either starts or not"), + )) + } + Some(svc) => svc.autostart = false, + } + } if let Some(c) = &p.command && !s.commands.contains_key(c) { @@ -287,6 +366,7 @@ impl Stack { data: p.data.unwrap_or_default(), env: p.env.clone(), command: p.command.clone(), + skip: self.skipped_containers(name), }); Ok(s) } diff --git a/crates/devstack-config/src/validate.rs b/crates/devstack-config/src/validate.rs index eb5cac66..030d7aa9 100644 --- a/crates/devstack-config/src/validate.rs +++ b/crates/devstack-config/src/validate.rs @@ -221,6 +221,70 @@ impl Check<'_> { } } +/// What a service that starts with the profile needs of a container the +/// profile leaves out (ADR-0058 point 4): an address in its `run`, `env`, +/// worker switches or `ensure_dirs`, or a host it rewrites. Its start would +/// fail on it. `after` is not: the service starts without waiting. +fn skipped_needs(c: &mut Check, stack: &Stack) { + let Some(skip) = stack.profile.as_ref().map(|p| &p.skip) else { + return; + }; + if skip.is_empty() { + return; + } + let skipped = |g: &str, s: &str| skip.contains(&(g.to_string(), s.to_string())); + for (s, svc) in stack.services.iter().filter(|(_, svc)| svc.autostart) { + let p = format!("services.{s}"); + let why = |g: &str, svc: &str| { + format!( + "the profile leaves `compose.{g}.{svc}` out, so it has no address here (skip `{s}` too, or keep the container)" + ) + }; + let mut texts: Vec<(String, &str)> = vec![(format!("{p}.run"), svc.run.as_str())]; + texts.extend( + svc.env + .iter() + .map(|(k, v)| (format!("{p}.env.{k}"), v.as_str())), + ); + if let Some(w) = &svc.workers { + texts.extend( + w.env + .iter() + .map(|(k, v)| (format!("{p}.workers.env.{k}"), v.as_str())), + ); + } + texts.extend( + svc.ensure_dirs + .iter() + .enumerate() + .map(|(i, d)| (format!("{p}.ensure_dirs[{i}]"), d.as_str())), + ); + for (at, t) in texts { + let Ok(segs) = interpolate::parse(t) else { + continue; // reported where the text is checked + }; + for ph in interpolate::placeholders(&segs) { + if let Placeholder::ComposePort { group, service, .. } = ph + && skipped(group, service) + { + c.err(&at, format!("`{ph}`: {}", why(group, service))); + } + } + } + if let Some(r) = &svc.compose { + for (i, h) in svc.rewrite_hosts.iter().enumerate() { + let (group, service) = h.split_once('.').unwrap_or((r.group.as_str(), h.as_str())); + if skipped(group, service) { + c.err( + format!("{p}.rewrite_hosts[{i}]"), + format!("`{h}`: {}", why(group, service)), + ); + } + } + } + } +} + pub fn validate(stack: &Stack) -> Vec { let mut c = Check { stack, @@ -802,6 +866,7 @@ pub fn validate(stack: &Stack) -> Vec { format!("`after` forms a cycle: {}", cycle.join(" -> ")), ); } + skipped_needs(&mut c, stack); // ---------------------------------------------------------------- commands for (n, cmd) in &stack.commands { @@ -971,6 +1036,28 @@ pub fn warnings(stack: &Stack) -> Vec { }); } } + let skip = crate::version_order(crate::SKIP_SINCE); + if stack + .stack + .requires + .as_deref() + .is_none_or(|r| crate::version_order(r) < skip) + { + for name in stack + .profiles + .iter() + .filter(|(_, p)| !p.skip.is_empty()) + .map(|(n, _)| n) + { + out.push(Issue { + path: format!("profiles.{name}.skip"), + message: format!( + "a devstack before {v} does not know this key and fails on it with \"unknown field\": `requires = \"{v}\"` makes it ask to be updated instead", + v = crate::SKIP_SINCE + ), + }); + } + } if step == 0 { return out; } diff --git a/crates/devstack-config/tests/profiles.rs b/crates/devstack-config/tests/profiles.rs index 8df4e95a..1b7a4258 100644 --- a/crates/devstack-config/tests/profiles.rs +++ b/crates/devstack-config/tests/profiles.rs @@ -170,3 +170,127 @@ fn a_profiles_env_takes_the_placeholders_of_a_command() { ); load("[profiles.x]\nenv = { U = \"{service.api.url}\", L = \"{lane}\" }\n").unwrap(); } + +/// Two Compose groups for `skip` (ADR-0058): one with a heavy search engine, +/// one with the database and a scanner. +const WITH_COMPOSE: &str = r#" +[compose.search] +project = "shop-search" +files = ["deploy/search.yml"] +services = ["engine"] + +[compose.app] +project = "shop-dev" +files = ["deploy/compose.yml"] +services = ["db", "scanner"] +"#; + +#[test] +fn a_profile_leaves_out_containers_groups_and_services() { + let s = load(&format!( + r#"{WITH_COMPOSE} +[profiles.lite] +skip = ["compose.search", "compose.app.scanner", "mailer"] +"# + )) + .unwrap(); + assert!( + !s.skips("app", "scanner"), + "without the profile nothing is left out" + ); + let p = s.with_profile("lite").unwrap(); + for (g, svc) in [("search", "engine"), ("app", "scanner")] { + assert!(p.skips(g, svc), "{g}.{svc}"); + } + assert!(!p.skips("app", "db")); + assert!(!p.services["mailer"].autostart); + assert!(p.services["api"].autostart, "the rest starts as written"); + // Still part of the stack: started on demand. + assert_eq!(p.compose["app"].services, ["db", "scanner"]); +} + +#[test] +fn what_a_profile_skips_must_be_there() { + let e = load(&format!( + "{WITH_COMPOSE}\n[profiles.lite]\nskip = [\"compose.nope\", \"compose.app.cache\", \"ghost\"]\n" + )) + .unwrap_err() + .to_string(); + for want in [ + "profiles.lite.skip[0]: no compose group `nope`", + "profiles.lite.skip[1]: `cache` is not started by compose group `app`", + "profiles.lite.skip[2]: no service `ghost`", + ] { + assert!(e.contains(want), "{want}\n{e}"); + } + let e = load(&format!( + "{WITH_COMPOSE}\n[profiles.lite]\nautostart = [\"api\"]\nskip = [\"api\"]\n" + )) + .unwrap_err() + .to_string(); + assert!( + e.contains("profiles.lite.skip[0]") && e.contains("`autostart` too"), + "{e}" + ); +} + +#[test] +fn a_starting_service_cannot_need_a_skipped_containers_address() { + let svc = r#" +[services.indexer] +run = "indexer --search {compose.search.engine.9200}" +after = ["compose.search.engine"] +"#; + let e = load(&format!( + "{WITH_COMPOSE}{svc}\n[profiles.lite]\nskip = [\"compose.search\"]\n" + )) + .unwrap_err() + .to_string(); + assert!( + e.contains("profiles.lite (services.indexer.run)") + && e.contains("leaves `compose.search.engine` out"), + "{e}" + ); + // Left out too, or only waited for: fine. + load(&format!( + "{WITH_COMPOSE}{svc}\n[profiles.lite]\nskip = [\"compose.search\", \"indexer\"]\n" + )) + .unwrap(); + let only_after = r#" +[services.indexer] +run = "indexer" +after = ["compose.search.engine"] +"#; + load(&format!( + "{WITH_COMPOSE}{only_after}\n[profiles.lite]\nskip = [\"compose.search\"]\n" + )) + .unwrap(); + // The profile's own env reaches every service. + let e = load(&format!( + "{WITH_COMPOSE}\n[profiles.lite]\nskip = [\"compose.app.db\"]\nenv = {{ DB = \"{{compose.app.db.5432}}\" }}\n" + )) + .unwrap_err() + .to_string(); + assert!( + e.contains("services.api.env.DB") && e.contains("compose.app.db"), + "{e}" + ); +} + +#[test] +fn skip_wants_requires_at_its_first_devstack() { + use devstack_config::{SKIP_SINCE, validate::warnings}; + let mut s = load(&format!( + "{WITH_COMPOSE}\n[profiles.lite]\nskip = [\"compose.search\"]\n" + )) + .unwrap(); + assert!( + warnings(&s) + .iter() + .any(|i| i.path == "profiles.lite.skip" && i.message.contains(SKIP_SINCE)), + "{:?}", + warnings(&s) + ); + s.stack.requires = Some(SKIP_SINCE.into()); + assert!(warnings(&s).iter().all(|i| i.path != "profiles.lite.skip")); +} diff --git a/crates/devstack-daemon/src/supervisor/infra.rs b/crates/devstack-daemon/src/supervisor/infra.rs index 3139ddff..87f0d56e 100644 --- a/crates/devstack-daemon/src/supervisor/infra.rs +++ b/crates/devstack-daemon/src/supervisor/infra.rs @@ -363,6 +363,13 @@ impl Supervisor { .collect() } + /// The tree's profile leaves every container of `group` out (ADR-0058). + fn left_out(&self, group: &str) -> bool { + self.stack.compose.get(group).is_some_and(|g| { + !g.services.is_empty() && g.services.iter().all(|s| self.stack.skips(group, s)) + }) + } + /// Compose groups in `after` order. pub(super) fn group_order(&self) -> Vec { topo( @@ -407,7 +414,37 @@ impl Supervisor { } else { Vec::new() }; - let group = self.compose_group(&g); + let mut group = self.compose_group(&g); + // What the tree's profile leaves out (ADR-0058) does not start, + // and one an earlier start left running is stopped. + let skipped: Vec = group + .services + .iter() + .filter(|s| self.stack.skips(&g, s)) + .cloned() + .collect(); + if !skipped.is_empty() { + let mut off = group.clone(); + off.services = skipped.clone(); + let message = match self.docker.stop_selected(&off) { + Ok(()) => format!("{}: left out by the profile", skipped.join(", ")), + Err(e) => format!( + "{}: left out by the profile, but could not be stopped: {e}", + skipped.join(", ") + ), + }; + self.emit(EventBody::Infra { + group: g.clone(), + message, + }); + group.services.retain(|s| !skipped.contains(s)); + } + if !skipped.is_empty() && group.services.is_empty() { + if self.lane.is_empty() { + self.hold_ports(&g, &held); + } + continue; + } if let Err(e) = self.docker.up(&group, Duration::from_secs(300)) { let msg = e.to_string(); self.emit(EventBody::Infra { @@ -436,7 +473,13 @@ impl Supervisor { /// [`NODE_GROUP_WAIT`] counts as failed. A file of an older agent, /// written once everything was up, says so at once. fn node_infra_up(&self, file: &Path) -> Result<(), String> { - let order = self.group_order(); + // A group the profile leaves out completely is not made there + // (ADR-0058 point 5): nothing to wait for. + let order: Vec = self + .group_order() + .into_iter() + .filter(|g| !self.left_out(g)) + .collect(); let mut follow = NodeBringUp::default(); let mut moved = Instant::now(); loop { diff --git a/crates/devstack-node/src/lanerun.rs b/crates/devstack-node/src/lanerun.rs index 6a13c83e..f9d0fc09 100644 --- a/crates/devstack-node/src/lanerun.rs +++ b/crates/devstack-node/src/lanerun.rs @@ -490,9 +490,27 @@ impl LaneRun { g.project = p.project; g.networks = p.networks; g.env = self.port_variables(stack, name); + g.services = self.started(stack, name); g } + /// The services of group `name` the lane starts: its `services` but + /// those its profile leaves out (ADR-0058 point 5), which are neither + /// checked nor created here. + fn started(&self, stack: &devstack_config::Stack, name: &str) -> Vec { + let skip = self + .profile + .as_deref() + .map(|p| stack.skipped_containers(p)) + .unwrap_or_default(); + stack.compose[name] + .services + .iter() + .filter(|s| !skip.contains(&(name.to_string(), (*s).clone()))) + .cloned() + .collect() + } + /// The `DEVSTACK_PORT_*` of a group's announced ports (ADR-0057): the /// files' ports moved by the lane's offset, the numbers its requester /// forwards and relays. A `shared` service is the lane's own here. @@ -518,7 +536,7 @@ impl LaneRun { ], env_files: Vec::new(), networks: p.networks, - services: stack.compose[name].services.clone(), + services: self.started(stack, name), dir, env: self.port_variables(stack, name), } @@ -685,6 +703,11 @@ impl LaneRun { // rules go in first. let mut groups: Vec<(String, Group, Model)> = Vec::new(); for name in order(&stack) { + if self.started(&stack, &name).is_empty() { + // Its profile leaves every container out: nothing is made. + phase(&format!("infrastructure: {name}: left out by the profile")); + continue; + } phase(&format!("infrastructure: {name}: checking")); let mirror = self.mirror_group(&stack, &name); for f in [format!("{name}.json"), format!("{name}.yml")] { diff --git a/crates/devstack/tests/daemon_lifecycle.rs b/crates/devstack/tests/daemon_lifecycle.rs index aa759ba8..772b81e2 100644 --- a/crates/devstack/tests/daemon_lifecycle.rs +++ b/crates/devstack/tests/daemon_lifecycle.rs @@ -2975,3 +2975,158 @@ steps = [{{ exec = "{echo}" }}] assert!(ok, "{stop}"); assert!(compose_ps(&lane_project).is_empty(), "after lane stop"); } + +/// A profile's `skip` (ADR-0058): the containers it leaves out do not start +/// with the tree, one an earlier start left running is stopped, and each +/// still starts on demand. Docker only (no .NET). +#[test] +#[ignore = "needs Docker: DEVSTACK_INTEGRATION_COMPOSE=1, --include-ignored"] +fn a_profile_leaves_out_containers() { + if !enabled("DEVSTACK_INTEGRATION_COMPOSE") { + eprintln!("skipped: set DEVSTACK_INTEGRATION_COMPOSE=1 (needs Docker)"); + return; + } + let dir = tempfile::tempdir().unwrap(); + let home = tempfile::tempdir().unwrap(); + let id = format!("{}-{}", std::process::id(), free_port()); + let (app, heavy) = ( + format!("devstack-it-skip-app-{id}"), + format!("devstack-it-skip-heavy-{id}"), + ); + let sleeper = " image: busybox:1.36\n command: [\"sleep\", \"600\"]\n init: true\n"; + std::fs::write( + dir.path().join("app.yml"), + format!("services:\n db:\n{sleeper} scanner:\n{sleeper}"), + ) + .unwrap(); + std::fs::write( + dir.path().join("heavy.yml"), + format!("services:\n engine:\n{sleeper}"), + ) + .unwrap(); + std::fs::write( + dir.path().join("devstack.toml"), + format!( + r#"version = 1 +[stack] +name = "skip" +[compose.app] +project = "{app}" +files = ["app.yml"] +services = ["db", "scanner"] +[compose.heavy] +project = "{heavy}" +files = ["heavy.yml"] +services = ["engine"] +[profiles.lite] +skip = ["compose.heavy", "compose.app.scanner"] +"# + ), + ) + .unwrap(); + std::fs::write(dir.path().join(".gitignore"), ".devstack/\n").unwrap(); + let file = dir.path().join("devstack.toml"); + let cli = |args: &[&str]| { + let out = Command::new(BIN) + .args(args) + .arg("-f") + .arg(&file) + .env("LOCALAPPDATA", home.path()) + .env("XDG_STATE_HOME", home.path()) + .env("DEVSTACK_NO_HUB", "1") + .output() + .unwrap(); + let text = format!( + "{}{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + (out.status.success(), text) + }; + // The services of `project` that run now. + let running = |project: &str| -> Vec { + let out = Command::new("docker") + .args([ + "compose", + "-p", + project, + "ps", + "--services", + "--status", + "running", + ]) + .output() + .unwrap(); + let mut s: Vec = String::from_utf8_lossy(&out.stdout) + .lines() + .map(|l| l.trim().to_string()) + .filter(|l| !l.is_empty()) + .collect(); + s.sort(); + s + }; + struct Cleanup<'a>(&'a dyn Fn()); + impl Drop for Cleanup<'_> { + fn drop(&mut self) { + (self.0)(); + } + } + let clean = || { + let _ = cli(&["down", "--infra"]); + for p in [&app, &heavy] { + let _ = Command::new("docker") + .args(["compose", "-p", p, "down", "--volumes", "--timeout", "1"]) + .output(); + } + }; + let _cleanup = Cleanup(&clean); + let ok = Command::new("git") + .args(["init", "-q"]) + .current_dir(dir.path()) + .status() + .unwrap() + .success(); + assert!(ok, "git init"); + + // Without the profile everything runs; `down` leaves the containers. + let (ok, up) = cli(&["up"]); + assert!(ok, "{up}"); + // `up` answers before the containers run when no service waits for them. + assert!( + wait_for( + || running(&app) == ["db", "scanner"] && running(&heavy) == ["engine"], + Duration::from_secs(60) + ), + "{:?} {:?}", + running(&app), + running(&heavy) + ); + let (ok, down) = cli(&["down"]); + assert!(ok, "{down}"); + assert_eq!(running(&heavy), ["engine"], "`down` keeps the containers"); + + // With it, what it leaves out goes, and the rest runs. + let (ok, up) = cli(&["up", "--profile", "lite"]); + assert!(ok, "{up}"); + assert!( + wait_for( + || running(&app) == ["db"] && running(&heavy).is_empty(), + Duration::from_secs(60) + ), + "{:?} {:?}", + running(&app), + running(&heavy) + ); + + // On demand it starts all the same. + let (ok, start) = cli(&["start", "compose.app.scanner"]); + assert!(ok, "{start}"); + assert!( + wait_for( + || running(&app) == ["db", "scanner"], + Duration::from_secs(30) + ), + "{:?}", + running(&app) + ); +} diff --git a/docs/adr/0041-a-profile-says-how-a-lane-runs-for-a-purpose.md b/docs/adr/0041-a-profile-says-how-a-lane-runs-for-a-purpose.md index dc300e14..e769c2d2 100644 --- a/docs/adr/0041-a-profile-says-how-a-lane-runs-for-a-purpose.md +++ b/docs/adr/0041-a-profile-says-how-a-lane-runs-for-a-purpose.md @@ -1,6 +1,6 @@ # ADR-0041: A profile says how a lane (or the main tree) runs for a purpose, chosen when it starts -- **Status:** Accepted (2026-10-19) +- **Status:** Accepted (2026-10-19); point 1 extended by [ADR-0058](0058-a-profile-can-leave-out-containers-and-services.md) (`skip`) - **Date:** 2026-10-19 - **Deciders:** project owner - **Related:** [#115](https://github.com/nuetzliches/devstack/issues/115), [#103](https://github.com/nuetzliches/devstack/issues/103), [ADR-0017](0017-stack-file-is-toml.md) (its "no profiles in version 1" meant environments; amended), [ADR-0008](0008-background-workers-run-only-in-the-active-slot.md), [ADR-0023](0023-stack-file-compatibility.md), [ADR-0024](0024-a-lane-is-a-supervisor-in-the-main-trees-daemon.md), [ADR-0030](0030-each-lane-has-its-own-infrastructure.md) (point 6: workers in lanes), [ADR-0038](0038-a-lanes-data-outlives-its-lease.md) diff --git a/docs/adr/0058-a-profile-can-leave-out-containers-and-services.md b/docs/adr/0058-a-profile-can-leave-out-containers-and-services.md new file mode 100644 index 00000000..36546a9c --- /dev/null +++ b/docs/adr/0058-a-profile-can-leave-out-containers-and-services.md @@ -0,0 +1,113 @@ +# ADR-0058: A profile can leave out containers, whole Compose groups and services + +- **Status:** Accepted (2026-10-23) +- **Date:** 2026-10-23 +- **Deciders:** project owner +- **Related:** [#244](https://github.com/nuetzliches/devstack/issues/244), [ADR-0004](0004-infrastructure-is-delegated-to-compose.md), [ADR-0023](0023-stack-file-compatibility.md), [ADR-0025](0025-a-lane-can-run-on-another-node.md), [ADR-0030](0030-each-lane-has-its-own-infrastructure.md), [ADR-0041](0041-a-profile-says-how-a-lane-runs-for-a-purpose.md), [ADR-0048](0048-a-containers-main-port-can-move-with-the-main-slot.md) + +## Context + +A downstream stack has two Compose groups: one with a single search +engine (about 2 GB of memory), one with about a dozen containers, among +them a virus scanner, a document converter, an address geocoder and a mock +AI service. Daily work on most branches needs none of these. The team wants +a light profile that leaves them out. + +A profile's `autostart` (ADR-0041 point 1) takes `[services]` entries only. +A Compose group always starts every container in its `services` list, and +nothing in a profile or a group leaves one out. A Compose `profiles:` entry +in the dev overlay does not help either: devstack starts containers by name, +and Compose starts a container named on its command line even if it belongs +to a Compose profile. ADR-0041 meant a profile to choose which of a group's +services start ("What this ADR does not say"), but it never gave it a key +for that. + +Three facts of the daemon shape the decision: + +- A service that waits `after` a container that does not run starts at once + (the container has no published port to check). +- A `{compose...}` placeholder, or a `rewrite_hosts` + entry, naming a container that does not run fails the service's start + ("port … is not published"). +- Containers outlive `devstack down` (ADR-0004). A container the last start + ran keeps running, and keeps its memory, unless something stops it. + +## Decision + +1. **`[profiles.] skip`** lists what does not start with the profile: + - `compose..`: a container of a group's `services`; + - `compose.`: every container of the group; + - ``: a `[services]` entry, as if its `autostart` were `false`. + + The list adds to `autostart`: `autostart` says which services start, + `skip` takes things away. A name in both is an error. +2. **The tree's start leaves skipped containers out.** `docker compose up` + gets the group's other services. A group with nothing left is not + started at all, and the event says the profile left it out. A skipped + container of this tree's project that still runs from an earlier start + is stopped (`docker compose stop`), so the profile frees what it leaves + out. Its port held for the main slot (ADR-0048) stays held and refuses + connections while it is down. +3. **A skipped container stays part of the stack.** It is still in the + group's `services`, so `devstack start compose..` (the + dashboard, MCP) starts it on demand, and `down --infra` removes it. + `skip` only changes what starts with the tree. +4. **Checked with the profile (ADR-0041 point 3).** Errors: an entry that + names no group, no container of a group's `services` or no service; a + name in both `autostart` and `skip`. A service that starts with the + profile and needs a skipped container's address is also an error, since + its start would fail: a `{compose..…}` placeholder in its + `run`, `env`, `workers.env` or `ensure_dirs` (the profile's `env` + included), or a `rewrite_hosts` entry. The fix is to skip that service + too, or to keep the container. `after` on a skipped container is not an + error: the service starts without waiting for it. +5. **A lane on a node** (ADR-0025): the node's agent applies the profile's + `skip` to the groups it creates and starts. A skipped container is not + created there, and a group with nothing left is not made at all. The + lane's daemon does not wait for a group its profile leaves out. +6. **Format.** `skip` is new and optional, so existing files are unchanged + (ADR-0023). A file that uses it raises `[stack] requires` to the release + that brings it, and `devstack check` warns otherwise. + +## Alternatives considered + +- **`autostart` takes containers too.** It reads naturally, but today a + profile's `autostart = ["api", "web"]` starts every container. Making + it start only the containers it names would change existing profiles. + Limiting that rule to the groups the list mentions would keep them, but + then whether a container starts depends on whether another container of + its group is named. +- **A per-container `autostart = false` in the group** (on demand, like a + service). It is useful, but it is a property of the stack, not of a + purpose: it leaves the container out of every start. It can come later as + its own key. `skip` is the profile's side of the same idea. +- **Compose `profiles:` in the overlay.** Compose ignores them for services + named on its command line, and devstack names every service it starts. +- **Drop a skipped container's placeholders, or start the service anyway.** + A service would then run without an address it was written to need. The + check says so instead. + +## Consequences + +- A light profile frees the memory of what it leaves out, also for + containers an earlier start left running. +- A service that needs a skipped container's address cannot start with the + profile: skip it too, or keep the container. +- On a node a skipped container is not created, so it cannot be started on + demand during that run. Here it can. +- A `shared` container (ADR-0030) that the main tree's profile skips is not + there for the lanes either: they use the main tree's. + +## Enforced by + +- `crates/devstack-config`: `Profile::skip`, `Stack::apply_profile` (the + references, `autostart = false`), `Active::skip` and `Stack::skips`; + `validate.rs` (what a starting service needs of a skipped container; the + `requires` warning, `SKIP_SINCE`); `tests/profiles.rs`. +- `crates/devstack-compose`: `Docker::stop_selected`. +- `crates/devstack-daemon/src/supervisor/infra.rs`: `infra_up` leaves the + skipped containers out and stops the ones that run; `node_infra_up` does + not wait for a group that is left out completely. +- `crates/devstack-node/src/lanerun.rs`: the groups a lane on a node + creates and starts. +- `docs/stack-file.md`, `skills/devstack-config/SKILL.md`. diff --git a/docs/adr/README.md b/docs/adr/README.md index 5dd6b044..d9b6c22d 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -89,7 +89,7 @@ stay. | [0038](0038-a-lanes-data-outlives-its-lease.md) | A lane's mirror and data outlive its lease on a node, for a while, and can be pruned | Accepted | | [0039](0039-an-admin-can-update-a-server-node.md) | An admin can have a server node update its devstack, from the dashboard | Accepted, amended 2026-10-20 (a checksum from the release, not a signature; the running binary is replaced; a drain ends, and can be withdrawn) | | [0040](0040-a-shared-lane-can-be-watched-like-ones-own.md) | A shared lane can be shared for viewing, and is then shown as one's own lane is | Accepted; amended 2026-10-20 (sharing a lane includes viewing it) | -| [0041](0041-a-profile-says-how-a-lane-runs-for-a-purpose.md) | A profile says how a lane (or the main tree) runs for a purpose, chosen when it starts | Accepted | +| [0041](0041-a-profile-says-how-a-lane-runs-for-a-purpose.md) | A profile says how a lane (or the main tree) runs for a purpose, chosen when it starts | Accepted; point 1 extended by ADR-0058 (`skip`) | | [0042](0042-a-dev-servers-rebuilds-show-like-builds.md) | A dev server's rebuilds show like builds, read from its output | Accepted | | [0043](0043-a-dev-servers-fixed-port-is-devstacks.md) | A dev server's fixed port is devstack's, forwarded to its instance | Accepted | | [0044](0044-a-lanes-prepared-directories-can-be-linked-into-its-worktree.md) | A lane's prepared directories can be linked into its worktree | Accepted | @@ -106,3 +106,4 @@ stay. | [0055](0055-a-lane-without-a-stack-file-says-so-and-can-be-given-one.md) | A lane whose branch has no stack file runs with the main tree's, says so, and can be given one | Accepted | | [0056](0056-a-lane-on-a-node-relays-its-udp-ports-through-a-tunnel-per-session.md) | A lane on a node relays its UDP ports, a tunnel per client, a datagram per frame | Accepted | | [0057](0057-a-port-a-container-announces-moves-inside-it-too.md) | A port a container announces moves inside the container too, and Compose is told its number | Accepted | +| [0058](0058-a-profile-can-leave-out-containers-and-services.md) | A profile can leave out containers, whole Compose groups and services | Accepted | diff --git a/docs/stack-file.md b/docs/stack-file.md index 53de1219..495beae2 100644 --- a/docs/stack-file.md +++ b/docs/stack-file.md @@ -699,6 +699,10 @@ need = { memory = "8G" } # a lane with it needs at least this, o kind = "static" # a build served as files instead of a dev server build = "npm run build -- --configuration e2e" output = "dist/web/browser" + +[profiles.lite] +description = "daily work without the heavy containers" +skip = ["compose.search", "compose.app.cache", "worker"] # a group, a container, a service ``` ```sh @@ -709,13 +713,29 @@ devstack lane start --profile acceptance --run # from the main tree - **What it may set:** `workers`, `data` (`keep`, the default, or `fresh`), `env` (for every service and every command's `exec` steps, with the placeholders a command has: no `{port}`, `{artifact}` or - `{is_main}`), `autostart`, `command` (one of `[commands]`), `need` (as + `{is_main}`), `autostart`, `skip` (below), `command` (one of `[commands]`), `need` (as `[lanes.need]`, each part the larger of the two; a lane with the profile is measured on its own, its runs are the profile's), and per service `kind`, `build`, `output`, `run`, `spa`, `cwd`, `health` and `env`. A service it does not name runs as the file says. Its `env` is added to every service's, a service's own `env` in the profile on top. `watch`, `port` and the other keys stay as the file says. +- **`skip`** ([ADR-0058](adr/0058-a-profile-can-leave-out-containers-and-services.md)) + leaves out what the profile does not need: a container + (`compose..`, one of the group's `services`), every + container of a group (`compose.`), or a service (as `autostart = + false`; a name in `autostart` too is an error). A skipped container does + not start with the tree, and one an earlier start left running is + stopped. It stays part of the stack: `devstack start + compose.app.cache` starts it on demand, and a port `main_ports` holds + for it stays held and refuses connections while it is down. A service + that starts and needs a skipped container's address (a `{compose.…}` + placeholder in its `run`, `env`, `workers.env` or `ensure_dirs`, the + profile's `env` included, or a `rewrite_hosts` entry) is an error, since + its start would fail: skip it too, or keep the container. One that only + waits for it (`after`) starts without waiting. On a node a skipped + container is not created, so it cannot be started there later in that + run. A file with `skip` should say `requires = "0.21.0"` in `[stack]`. - **Another `kind` drops what only the old one takes:** a dev server made `static` loses its `run` (unless the profile gives one), `rebuild` and `restart_on`; a `devserver` or `tool` loses `build` and `output`; `spa` diff --git a/skills/devstack-config/SKILL.md b/skills/devstack-config/SKILL.md index 05ede9cc..a16a18a3 100644 --- a/skills/devstack-config/SKILL.md +++ b/skills/devstack-config/SKILL.md @@ -82,7 +82,13 @@ without a stack file starts from `devstack init` (a draft with TODOs). light review: `workers`, `data = "fresh"`, `env` with the tree's addresses, `autostart`, per service `kind = "static"` with `build` and `output`, and the suite as `command`. Each profile is checked as the stack - it makes; add `requires = "0.11.0"` to `[stack]`. + it makes; add `requires = "0.11.0"` to `[stack]`. A light profile for + daily work leaves heavy containers out with `skip` (ADR-0058): + `skip = ["compose.search", "compose.app.scanner", "indexer"]` (a whole + group, a container, a service; `requires = "0.21.0"`). What it skips is + stopped at the start and still starts on demand (`devstack start + compose.app.scanner`); a service that starts and uses a skipped + container's address (`{compose.…}`, `rewrite_hosts`) must be skipped too. - **Hot reload is opt-in per session.** `[services..hot_reload]` with the watcher's `run` (`dotnet watch run --non-interactive`) only makes `devstack hot-reload on` possible; the service still builds by