Skip to main content

cloud/
compose.rs

1//! Podman Compose file generation for yah-cloud service deployments (R040-F7).
2//!
3//! Translates `MachineConfig` + `LegacyServiceConfig[]` into a ready-to-deploy
4//! `ComposeBundle` containing:
5//!
6//! - `compose.yml`: Podman Compose (Docker Compose v3-compatible) stack
7//! - `Caddyfile`: Caddy reverse-proxy config for `mesh_only: false` services
8//!   (omitted when all services are mesh-only)
9//!
10//! ## Tier isolation
11//! Services share a Compose network named after the machine's first `tier:*`
12//! tag (`tier:t2` → network `tier-t2`). Tier isolation at the software layer
13//! (Postgres roles, Headscale ACL tags) is the camp owner's responsibility;
14//! this just scopes the container network so different-tier stacks on the
15//! same host are not bridged together.
16//!
17//! ## Tenant isolation (W206 / R558-T2)
18//! When a machine hosts services from a single tenant (the common, degenerate
19//! case — every [`LegacyServiceConfig::tenant`] is the singleton), the scheme
20//! above is unchanged. When it hosts services from **two or more distinct
21//! tenants**, the network splits per tenant: each service joins
22//! `<tenant>-<tier>` (e.g. `ss-tier-t2`, `noisetable-tier-t2`) so cross-tenant
23//! stacks on the same host are not bridged together. The shared Caddy ingress
24//! joins every tenant network so a single reverse proxy still reaches any
25//! public service. See [`NetworkPlan`].
26//!
27//! ## Caddy reverse proxy
28//! `mesh_only: false` services are exposed through a Caddy container that
29//! Cloudflare orange-cloud (or a Cloudflare Tunnel via R040-F15) terminates
30//! in front of. Caddy is chosen over nginx because it handles TLS from
31//! Cloudflare origin certs without extra config, and its reverse_proxy
32//! directive handles service discovery by container name.
33//!
34//! ## mesh_only services
35//! Services with `mesh_only: true` use `expose:` only (no host-port
36//! binding). They are reachable within the Compose network and, once
37//! R040-F16 lands its `bind_interface` plumbing, directly via Tailscale.
38
39use std::collections::BTreeSet;
40
41use crate::config::{LegacyServiceConfig, MachineConfig};
42use crate::mesh_service;
43
44/// A rendered compose bundle ready to push to the yubaba's `POST /compose`.
45#[derive(Debug, Clone)]
46pub struct ComposeBundle {
47    /// Podman compose YAML (Docker Compose v3-compatible).
48    pub compose_yaml: String,
49    /// Caddy reverse-proxy config — `None` when all services are `mesh_only`.
50    pub caddyfile: Option<String>,
51    /// Shell commands the yubaba must execute after writing compose files —
52    /// typically ufw rules for services that have `bind_interface` set.
53    /// Commands are executed in order via `sh -c`; failures are logged but
54    /// do not abort the deploy (ufw may not be installed on dev machines).
55    pub firewall_cmds: Vec<String>,
56}
57
58/// Generate a `ComposeBundle` for a machine's full service set.
59///
60/// `services` is the union of all declared services across the mirrors the
61/// machine hosts. Pass an optional `public_hostname` (e.g.
62/// `"pdx.cloud.noisetable.example"`) to get a domain-named Caddyfile; omit
63/// it and Caddy falls back to port-based listeners (useful for staging / when
64/// `cloud_domain` isn't set yet in `mirrors/<camp>.toml`).
65pub fn generate_compose_bundle(
66    machine: &MachineConfig,
67    services: &[LegacyServiceConfig],
68    public_hostname: Option<&str>,
69) -> ComposeBundle {
70    let compose_yaml = build_compose_yaml(machine, services);
71    let caddyfile = build_caddyfile(services, public_hostname);
72    let firewall_cmds = collect_firewall_cmds(services);
73    ComposeBundle {
74        compose_yaml,
75        caddyfile,
76        firewall_cmds,
77    }
78}
79
80/// Derive the primary Compose network name from the machine's tier tag.
81/// Falls back to `"yah-cloud"` when no `tier:*` tag is present.
82fn machine_network(machine: &MachineConfig) -> String {
83    machine
84        .mesh_tags
85        .iter()
86        .find_map(|t| t.strip_prefix("tier:"))
87        .map(|tier| format!("tier-{tier}"))
88        .unwrap_or_else(|| "yah-cloud".to_string())
89}
90
91/// How a machine's services map onto Compose bridge networks (W206 / R558-T2).
92///
93/// Single-tenant (degenerate) machines keep the historical
94/// one-network-per-machine scheme: every service joins the tier-derived
95/// [`machine_network`] (`tier-t2`, or `yah-cloud` when the machine carries no
96/// `tier:*` tag). When a machine hosts services from **two or more distinct
97/// tenants**, the network is split per tenant — each service joins
98/// `<tenant>-<base>` (e.g. `ss-tier-t2`, `noisetable-tier-t2`) so cross-tenant
99/// stacks on the same host are not bridged together. The shared Caddy ingress
100/// joins every declared network so it can still reach any public service.
101///
102/// The tenant prefix uses the base network (`tier-t2`) as a suffix rather than
103/// replacing it, keeping the tier legible in the network name and the
104/// single-tenant output byte-identical to the pre-T2 renderer.
105struct NetworkPlan {
106    base: String,
107    multi_tenant: bool,
108}
109
110impl NetworkPlan {
111    fn derive(machine: &MachineConfig, services: &[LegacyServiceConfig]) -> Self {
112        let base = machine_network(machine);
113        let distinct_tenants: BTreeSet<&str> =
114            services.iter().map(|s| s.tenant.0.as_str()).collect();
115        NetworkPlan {
116            base,
117            multi_tenant: distinct_tenants.len() > 1,
118        }
119    }
120
121    /// Network the given service joins.
122    fn network_for(&self, svc: &LegacyServiceConfig) -> String {
123        if self.multi_tenant {
124            format!("{}-{}", svc.tenant.0, self.base)
125        } else {
126            self.base.clone()
127        }
128    }
129
130    /// All distinct networks to declare in the compose `networks:` block and
131    /// attach the shared Caddy ingress to. Sorted for deterministic output.
132    fn declared(&self, services: &[LegacyServiceConfig]) -> Vec<String> {
133        if !self.multi_tenant {
134            return vec![self.base.clone()];
135        }
136        services
137            .iter()
138            .map(|s| self.network_for(s))
139            .collect::<BTreeSet<_>>()
140            .into_iter()
141            .collect()
142    }
143}
144
145fn build_compose_yaml(machine: &MachineConfig, services: &[LegacyServiceConfig]) -> String {
146    let plan = NetworkPlan::derive(machine, services);
147    let declared = plan.declared(services);
148
149    let mut out = String::new();
150    out.push_str("# Generated by `yah cloud service deploy` — do not edit manually.\n");
151    out.push_str(&format!(
152        "# Machine: {} | Location: {}\n",
153        machine.name,
154        machine.location()
155    ));
156    out.push_str("version: \"3.8\"\n\n");
157
158    let has_public = services.iter().any(|s| !s.mesh_only);
159
160    if !services.is_empty() || has_public {
161        out.push_str("services:\n");
162    }
163
164    for svc in services {
165        append_service(&mut out, svc, &plan.network_for(svc));
166    }
167
168    if has_public {
169        append_caddy_service(&mut out, &declared);
170    }
171
172    // Networks block — one entry per declared network (a single tier network
173    // when single-tenant, one `<tenant>-<tier>` network per tenant otherwise).
174    out.push_str("networks:\n");
175    for net in &declared {
176        out.push_str(&format!("  {net}:\n"));
177        out.push_str("    driver: bridge\n");
178    }
179
180    if has_public {
181        out.push_str("\nvolumes:\n");
182        out.push_str("  caddy_data:\n");
183    }
184
185    out
186}
187
188fn append_service(out: &mut String, svc: &LegacyServiceConfig, network: &str) {
189    out.push_str(&format!("  {}:\n", svc.name));
190    out.push_str(&format!("    image: {}:{}\n", svc.image, svc.version));
191    out.push_str("    restart: unless-stopped\n");
192
193    // Services with bind_interface use the host network stack directly so they
194    // can bind to a specific interface (e.g. tailscale0). In host mode the
195    // container is not joined to the compose bridge network and expose: is a
196    // no-op, so both are omitted.
197    if let Some(iface) = &svc.bind_interface {
198        out.push_str("    network_mode: \"host\"\n");
199        // Emit env_file so the process picks up POSTGRES_LISTEN_ADDRESSES (or
200        // equivalent) that cloud-init writes at first boot from `tailscale ip`.
201        out.push_str(&format!(
202            "    env_file:\n      - {}\n",
203            mesh_service::MESH_IP_ENV_FILE,
204        ));
205        // Annotate with the pg_hba hint so operators know what to configure.
206        out.push_str(&format!(
207            "    # bind_interface={iface}: use host network + tailscale0 IP. \
208             Apply pg_hba snippet from `yah cloud service recipe postgres`.\n",
209        ));
210
211        if !svc.env.is_empty() {
212            out.push_str("    environment:\n");
213            let mut pairs: Vec<(&String, &String)> = svc.env.iter().collect();
214            pairs.sort_by_key(|(k, _)| k.as_str());
215            for (k, v) in pairs {
216                let escaped = v.replace('"', "\\\"");
217                out.push_str(&format!("      {k}: \"{escaped}\"\n"));
218            }
219        }
220        out.push('\n');
221        return;
222    }
223
224    if !svc.env.is_empty() {
225        out.push_str("    environment:\n");
226        let mut pairs: Vec<(&String, &String)> = svc.env.iter().collect();
227        pairs.sort_by_key(|(k, _)| k.as_str());
228        for (k, v) in pairs {
229            let escaped = v.replace('"', "\\\"");
230            out.push_str(&format!("      {k}: \"{escaped}\"\n"));
231        }
232    }
233
234    if !svc.ports.is_empty() {
235        out.push_str("    expose:\n");
236        for p in &svc.ports {
237            out.push_str(&format!("      - \"{}\"\n", p.container));
238        }
239    }
240
241    out.push_str(&format!("    networks:\n      - {network}\n\n"));
242}
243
244/// Collect ufw firewall commands for all services that declare a `bind_interface`.
245/// Each such service gets an `allow in on <iface> port <port>` + `deny <port>`
246/// pair, mirroring the yubaba-7443 pattern established in `mirror.yml`.
247fn collect_firewall_cmds(services: &[LegacyServiceConfig]) -> Vec<String> {
248    let mut cmds = Vec::new();
249    for svc in services {
250        if let Some(iface) = &svc.bind_interface {
251            for port in &svc.ports {
252                cmds.extend(mesh_service::ufw_rules_for_mesh_port(iface, port.container));
253            }
254        }
255    }
256    cmds
257}
258
259fn append_caddy_service(out: &mut String, networks: &[String]) {
260    out.push_str("  caddy:\n");
261    out.push_str("    image: caddy:2-alpine\n");
262    out.push_str("    restart: unless-stopped\n");
263    out.push_str("    ports:\n");
264    out.push_str("      - \"80:80\"\n");
265    out.push_str("      - \"443:443\"\n");
266    out.push_str("    volumes:\n");
267    out.push_str("      - /etc/yah-cloud/Caddyfile:/etc/caddy/Caddyfile:ro\n");
268    out.push_str("      - caddy_data:/data\n");
269    // Caddy joins every tenant network so a single ingress can reverse-proxy
270    // public services regardless of which tenant they belong to.
271    out.push_str("    networks:\n");
272    for net in networks {
273        out.push_str(&format!("      - {net}\n"));
274    }
275    out.push('\n');
276}
277
278/// Build a Caddyfile for non-mesh_only services. Returns `None` when all
279/// services are mesh-only.
280///
281/// When `hostname` is supplied each service gets a named virtual host
282/// (`<hostname>` for the first service, `<service>.<hostname>` for the
283/// rest). Without a hostname Caddy uses `:port` placeholders so the stack
284/// is immediately testable — operators swap in the real domain once
285/// Cloudflare DNS is wired (SECRETS.md).
286fn build_caddyfile(services: &[LegacyServiceConfig], hostname: Option<&str>) -> Option<String> {
287    let public: Vec<&LegacyServiceConfig> = services.iter().filter(|s| !s.mesh_only).collect();
288    if public.is_empty() {
289        return None;
290    }
291
292    let mut out = String::new();
293    out.push_str("# Generated by `yah cloud service deploy` — do not edit manually.\n");
294
295    if hostname.is_none() {
296        out.push_str("# Set cloud_domain in mirrors/<camp>.toml for named virtual hosts.\n");
297        out.push_str("# Example: pdx.cloud.noisetable.example { reverse_proxy service:8080 }\n");
298    }
299    out.push('\n');
300
301    for (i, svc) in public.iter().enumerate() {
302        let first_port = first_port(svc);
303        let site = match hostname {
304            Some(h) if i == 0 => h.to_string(),
305            Some(h) => format!("{}.{h}", svc.name),
306            None => format!(":{first_port}"),
307        };
308        out.push_str(&format!("{site} {{\n"));
309        out.push_str(&format!("    reverse_proxy {}:{first_port}\n", svc.name));
310        out.push_str("}\n\n");
311    }
312
313    Some(out)
314}
315
316fn first_port(svc: &LegacyServiceConfig) -> u16 {
317    svc.ports.first().map(|p| p.container).unwrap_or(80)
318}
319
320// ── Tests ───────────────────────────────────────────────────────────────────
321
322#[cfg(test)]
323mod tests {
324    use super::*;
325    use crate::config::{BucketSpec, LegacyServiceConfig, MachineConfig, PortMapping}; // PortMapping used in test constructors
326    use std::collections::HashMap;
327
328    fn machine(tags: &[&str]) -> MachineConfig {
329        MachineConfig {
330            name: "test-pdx-1".into(),
331            provider: "hetzner".into(),
332            location: Some("pdx".into()),
333            server_type: Some("cpx22".into()),
334            hosts_mirrors: vec!["noisetable".into()],
335            mesh_tags: tags.iter().map(|t| t.to_string()).collect(),
336            region: None,
337            zone: None,
338            arch: None,
339            bucket: Some(BucketSpec {
340                name: "test-assets-pdx-1".into(),
341                public_read: false,
342            }),
343            vendor: None,
344            nickname: None,
345            legacy_hostkey_fingerprint: None,
346            registration: Default::default(),
347            ssh_keys: vec![],
348            cloudflared: None,
349            hosts_operator_bridge: false,
350            connect: None,
351            allocatable: None,
352            taints: vec![],
353        }
354    }
355
356    fn service(name: &str, mesh_only: bool) -> LegacyServiceConfig {
357        LegacyServiceConfig {
358            name: name.into(),
359            image: format!("ghcr.io/test/{name}"),
360            version: "v1.0.0".into(),
361            env: HashMap::new(),
362            ports: vec![PortMapping {
363                host: 8080,
364                container: 8080,
365            }],
366            mesh_only,
367            bind_interface: None,
368            tenant: workload_spec::TenantId::singleton(),
369        }
370    }
371
372    fn mesh_bound_service(name: &str, port: u16) -> LegacyServiceConfig {
373        LegacyServiceConfig {
374            name: name.into(),
375            image: format!("ghcr.io/test/{name}"),
376            version: "v1.0.0".into(),
377            env: HashMap::new(),
378            ports: vec![PortMapping {
379                host: port,
380                container: port,
381            }],
382            mesh_only: true,
383            bind_interface: Some("tailscale0".into()),
384            tenant: workload_spec::TenantId::singleton(),
385        }
386    }
387
388    #[test]
389    fn mesh_only_service_has_no_caddy() {
390        let m = machine(&["tier:t2"]);
391        let svcs = [service("asset-registry", true)];
392        let bundle = generate_compose_bundle(&m, &svcs, None);
393        assert!(
394            !bundle.compose_yaml.contains("caddy:"),
395            "caddy should not appear"
396        );
397        assert!(bundle.caddyfile.is_none(), "no Caddyfile for mesh-only");
398    }
399
400    #[test]
401    fn public_service_includes_caddy_and_caddyfile() {
402        let m = machine(&["tier:t2"]);
403        let svcs = [service("asset-registry", false)];
404        let bundle = generate_compose_bundle(&m, &svcs, None);
405        assert!(
406            bundle.compose_yaml.contains("caddy:"),
407            "caddy missing from compose"
408        );
409        assert!(
410            bundle.compose_yaml.contains("caddy_data:"),
411            "volume missing"
412        );
413        assert!(
414            bundle.caddyfile.is_some(),
415            "Caddyfile expected for public service"
416        );
417    }
418
419    #[test]
420    fn tier_tag_becomes_network_name() {
421        let m = machine(&["region:pdx", "tier:t2"]);
422        let svcs: [LegacyServiceConfig; 0] = [];
423        let bundle = generate_compose_bundle(&m, &svcs, None);
424        assert!(
425            bundle.compose_yaml.contains("tier-t2:"),
426            "network name mismatch"
427        );
428        assert!(
429            !bundle.compose_yaml.contains("yah-cloud:"),
430            "fallback network present"
431        );
432    }
433
434    #[test]
435    fn no_tier_tag_falls_back_to_yah_cloud_network() {
436        let m = machine(&["region:pdx"]);
437        let svcs: [LegacyServiceConfig; 0] = [];
438        let bundle = generate_compose_bundle(&m, &svcs, None);
439        assert!(
440            bundle.compose_yaml.contains("yah-cloud:"),
441            "fallback network missing"
442        );
443    }
444
445    fn tenant_service(name: &str, tenant: &str, mesh_only: bool) -> LegacyServiceConfig {
446        LegacyServiceConfig {
447            name: name.into(),
448            image: format!("ghcr.io/test/{name}"),
449            version: "v1.0.0".into(),
450            env: HashMap::new(),
451            ports: vec![PortMapping {
452                host: 8080,
453                container: 8080,
454            }],
455            mesh_only,
456            bind_interface: None,
457            tenant: workload_spec::TenantId(tenant.into()),
458        }
459    }
460
461    #[test]
462    fn single_tenant_keeps_one_shared_network() {
463        // Two services, both the singleton tenant → one shared tier network,
464        // no per-tenant split (R558-T2 degenerate case is byte-identical).
465        let m = machine(&["tier:t2"]);
466        let svcs = [service("a", true), service("b", true)];
467        let yaml = generate_compose_bundle(&m, &svcs, None).compose_yaml;
468        assert!(
469            yaml.contains("tier-t2:"),
470            "shared tier network expected:\n{yaml}"
471        );
472        assert!(
473            !yaml.contains("-tier-t2:"),
474            "no tenant-prefixed network when single-tenant:\n{yaml}"
475        );
476    }
477
478    #[test]
479    fn multi_tenant_splits_into_per_tenant_networks() {
480        let m = machine(&["tier:t2"]);
481        let svcs = [
482            tenant_service("yah-api", "ss", true),
483            tenant_service("nt-api", "noisetable", true),
484        ];
485        let yaml = generate_compose_bundle(&m, &svcs, None).compose_yaml;
486        assert!(yaml.contains("ss-tier-t2:"), "ss network missing:\n{yaml}");
487        assert!(
488            yaml.contains("noisetable-tier-t2:"),
489            "noisetable network missing:\n{yaml}"
490        );
491        assert!(
492            yaml.contains("      - ss-tier-t2\n"),
493            "yah-api should join the ss network:\n{yaml}"
494        );
495        assert!(
496            yaml.contains("      - noisetable-tier-t2\n"),
497            "nt-api should join the noisetable network:\n{yaml}"
498        );
499        assert!(
500            !yaml.contains("      - tier-t2\n"),
501            "no service joins the bare tier network when multi-tenant:\n{yaml}"
502        );
503    }
504
505    #[test]
506    fn multi_tenant_caddy_joins_every_tenant_network() {
507        let m = machine(&["tier:t2"]);
508        // Public (mesh_only=false) services across two tenants → the shared
509        // Caddy ingress must join both tenant networks to reach them.
510        let svcs = [
511            tenant_service("yah-web", "ss", false),
512            tenant_service("nt-web", "noisetable", false),
513        ];
514        let yaml = generate_compose_bundle(&m, &svcs, None).compose_yaml;
515        assert!(yaml.contains("caddy:"), "caddy present for public services");
516        // Isolate Caddy's own `networks:` list: it sits between its
517        // `caddy_data:/data` volume line and the top-level `networks:` block.
518        let after_caddy_vol = yaml
519            .split("- caddy_data:/data")
520            .nth(1)
521            .expect("caddy volumes");
522        let caddy_nets = after_caddy_vol.split("\nnetworks:").next().unwrap();
523        assert!(
524            caddy_nets.contains("- ss-tier-t2"),
525            "caddy joins ss network:\n{yaml}"
526        );
527        assert!(
528            caddy_nets.contains("- noisetable-tier-t2"),
529            "caddy joins noisetable network:\n{yaml}"
530        );
531    }
532
533    #[test]
534    fn image_includes_version() {
535        let m = machine(&[]);
536        let svcs = [service("asset-registry", true)];
537        let bundle = generate_compose_bundle(&m, &svcs, None);
538        assert!(bundle
539            .compose_yaml
540            .contains("ghcr.io/test/asset-registry:v1.0.0"));
541    }
542
543    #[test]
544    fn env_vars_are_rendered_sorted() {
545        let m = machine(&[]);
546        let mut env = HashMap::new();
547        env.insert("ZEBRA".into(), "last".into());
548        env.insert("ALPHA".into(), "first".into());
549        let svc = LegacyServiceConfig {
550            name: "myservice".into(),
551            image: "img".into(),
552            version: "v1".into(),
553            env,
554            ports: vec![],
555            mesh_only: true,
556            bind_interface: None,
557            tenant: workload_spec::TenantId::singleton(),
558        };
559        let bundle = generate_compose_bundle(&m, &[svc], None);
560        let yaml = &bundle.compose_yaml;
561        assert!(yaml.contains("ALPHA: \"first\""), "ALPHA missing");
562        assert!(yaml.contains("ZEBRA: \"last\""), "ZEBRA missing");
563        // Sorted: ALPHA before ZEBRA
564        let alpha_pos = yaml.find("ALPHA").unwrap();
565        let zebra_pos = yaml.find("ZEBRA").unwrap();
566        assert!(alpha_pos < zebra_pos, "env vars not sorted");
567    }
568
569    #[test]
570    fn ports_become_expose() {
571        let m = machine(&[]);
572        let svcs = [service("svc", true)];
573        let bundle = generate_compose_bundle(&m, &svcs, None);
574        assert!(bundle.compose_yaml.contains("expose:\n      - \"8080\""));
575    }
576
577    #[test]
578    fn caddyfile_uses_hostname_when_provided() {
579        let m = machine(&[]);
580        let svcs = [service("api", false)];
581        let bundle = generate_compose_bundle(&m, &svcs, Some("pdx.cloud.example.com"));
582        let cf = bundle.caddyfile.unwrap();
583        assert!(
584            cf.contains("pdx.cloud.example.com {"),
585            "hostname missing from Caddyfile"
586        );
587        assert!(
588            cf.contains("reverse_proxy api:8080"),
589            "reverse_proxy missing"
590        );
591    }
592
593    #[test]
594    fn caddyfile_uses_port_placeholder_when_no_hostname() {
595        let m = machine(&[]);
596        let svcs = [service("api", false)];
597        let bundle = generate_compose_bundle(&m, &svcs, None);
598        let cf = bundle.caddyfile.unwrap();
599        assert!(cf.contains(":8080 {"), "port placeholder missing");
600    }
601
602    #[test]
603    fn multiple_public_services_get_subdomains() {
604        let m = machine(&[]);
605        let svcs = [service("api", false), service("admin", false)];
606        let bundle = generate_compose_bundle(&m, &svcs, Some("pdx.cloud.example.com"));
607        let cf = bundle.caddyfile.unwrap();
608        // First gets the bare hostname, rest get subdomain prefix
609        assert!(cf.contains("pdx.cloud.example.com {"));
610        assert!(cf.contains("admin.pdx.cloud.example.com {"));
611    }
612
613    #[test]
614    fn mixed_services_only_routes_public_in_caddyfile() {
615        let m = machine(&[]);
616        let svcs = [service("public-svc", false), service("mesh-svc", true)];
617        let bundle = generate_compose_bundle(&m, &svcs, None);
618        let cf = bundle.caddyfile.as_deref().unwrap();
619        assert!(
620            cf.contains("public-svc"),
621            "public service missing from Caddyfile"
622        );
623        assert!(
624            !cf.contains("mesh-svc"),
625            "mesh-only service leaked into Caddyfile"
626        );
627    }
628
629    // ── bind_interface (R040-F16) ────────────────────────────────────────────
630
631    #[test]
632    fn bind_interface_emits_network_mode_host() {
633        let m = machine(&["tier:t2"]);
634        let svcs = [mesh_bound_service("postgres", 5432)];
635        let bundle = generate_compose_bundle(&m, &svcs, None);
636        assert!(
637            bundle.compose_yaml.contains("network_mode: \"host\""),
638            "host mode missing:\n{}",
639            bundle.compose_yaml,
640        );
641    }
642
643    #[test]
644    fn bind_interface_emits_env_file_for_mesh_ip() {
645        let m = machine(&[]);
646        let svcs = [mesh_bound_service("postgres", 5432)];
647        let bundle = generate_compose_bundle(&m, &svcs, None);
648        assert!(
649            bundle.compose_yaml.contains(mesh_service::MESH_IP_ENV_FILE),
650            "env_file missing from compose yaml:\n{}",
651            bundle.compose_yaml,
652        );
653    }
654
655    #[test]
656    fn bind_interface_service_not_in_bridge_network() {
657        let m = machine(&["tier:t2"]);
658        let svcs = [mesh_bound_service("postgres", 5432)];
659        let bundle = generate_compose_bundle(&m, &svcs, None);
660        // The "networks:" block at the top level is still present (for caddy etc.),
661        // but the postgres service entry must NOT have a `networks:` directive.
662        let svc_block_end = bundle
663            .compose_yaml
664            .find("network_mode: \"host\"")
665            .expect("network_mode:host missing");
666        let after = &bundle.compose_yaml[svc_block_end..];
667        // The postgres block ends with a blank line; there must not be a "networks:" line
668        // before that blank line.
669        let blank = after.find("\n\n").unwrap_or(after.len());
670        let postgres_block = &after[..blank];
671        assert!(
672            !postgres_block.contains("networks:\n      -"),
673            "bind_interface service should not be added to the bridge network:\n{}",
674            postgres_block,
675        );
676    }
677
678    #[test]
679    fn bind_interface_service_has_no_expose_block() {
680        let m = machine(&[]);
681        let svcs = [mesh_bound_service("postgres", 5432)];
682        let bundle = generate_compose_bundle(&m, &svcs, None);
683        // In network_mode:host, expose: is meaningless — ensure it's omitted.
684        let svc_start = bundle.compose_yaml.find("  postgres:").unwrap();
685        let svc_end = bundle.compose_yaml[svc_start..]
686            .find("\n\n")
687            .map(|i| svc_start + i)
688            .unwrap_or(bundle.compose_yaml.len());
689        let block = &bundle.compose_yaml[svc_start..svc_end];
690        assert!(
691            !block.contains("expose:"),
692            "expose: must be omitted in host mode:\n{block}"
693        );
694    }
695
696    #[test]
697    fn bind_interface_populates_firewall_cmds() {
698        let m = machine(&[]);
699        let svcs = [mesh_bound_service("postgres", 5432)];
700        let bundle = generate_compose_bundle(&m, &svcs, None);
701        assert_eq!(
702            bundle.firewall_cmds.len(),
703            2,
704            "expected 2 ufw rules: {:?}",
705            bundle.firewall_cmds
706        );
707        assert!(bundle.firewall_cmds[0].contains("allow in on tailscale0 to any port 5432"));
708        assert!(bundle.firewall_cmds[1].contains("deny 5432"));
709    }
710
711    #[test]
712    fn no_bind_interface_produces_empty_firewall_cmds() {
713        let m = machine(&[]);
714        let svcs = [service("api", false), service("worker", true)];
715        let bundle = generate_compose_bundle(&m, &svcs, None);
716        assert!(
717            bundle.firewall_cmds.is_empty(),
718            "no bind_interface → no firewall cmds"
719        );
720    }
721
722    #[test]
723    fn multiple_bound_services_accumulate_firewall_cmds() {
724        let m = machine(&[]);
725        let svcs = [
726            mesh_bound_service("postgres", 5432),
727            mesh_bound_service("nats", 4222),
728        ];
729        let bundle = generate_compose_bundle(&m, &svcs, None);
730        // 2 rules per service × 2 services = 4
731        assert_eq!(
732            bundle.firewall_cmds.len(),
733            4,
734            "unexpected rules: {:?}",
735            bundle.firewall_cmds
736        );
737        let all = bundle.firewall_cmds.join("\n");
738        assert!(all.contains("5432"), "postgres rules missing");
739        assert!(all.contains("4222"), "nats rules missing");
740    }
741
742    #[test]
743    fn bind_interface_service_excluded_from_caddyfile() {
744        let m = machine(&[]);
745        // A pg service has bind_interface + mesh_only:true — must NOT appear in Caddyfile.
746        let svcs = [mesh_bound_service("postgres", 5432), service("api", false)];
747        let bundle = generate_compose_bundle(&m, &svcs, None);
748        let cf = bundle.caddyfile.as_deref().unwrap();
749        assert!(
750            !cf.contains("postgres"),
751            "bound service must not be in Caddyfile"
752        );
753        assert!(cf.contains("api"), "public service missing from Caddyfile");
754    }
755}