Skip to main content

ryra_core/
lib.rs

1pub mod auth_bridge;
2pub mod authelia;
3pub mod backup;
4pub mod caddy;
5pub mod capability;
6pub mod config;
7pub mod configure;
8pub mod data;
9pub mod deploy;
10pub mod error;
11pub mod exposure;
12pub mod from_protocol;
13pub mod generate;
14pub mod manifest;
15pub mod metadata;
16pub mod metrics_bridge;
17pub mod ops;
18pub mod paths;
19pub mod plan;
20pub mod registry;
21pub mod system;
22pub mod to_protocol;
23pub mod upgrade;
24pub mod well_known;
25
26use std::collections::BTreeMap;
27use std::path::{Path, PathBuf};
28
29use config::ConfigPaths;
30use config::schema::InstalledService;
31use error::{Error, Result};
32
33pub use capability::{
34    Capability, any_installed_provider, find_installed_provider, installed_provides,
35    service_provides,
36};
37pub use configure::{
38    ConfigureChange, ConfigureResult, EnvKeyChange, ExposureChange,
39    Overrides as ConfigureOverrides, ServiceReconcile, configure_service, reconcile_service,
40};
41pub use exposure::{
42    Exposure, check_auth_exposure_compat, is_caddy_local_url, is_public_url, is_tailscale_url,
43};
44pub use generate::GeneratedFile;
45pub use manifest::{ManifestEntry, manifest_path};
46pub use metadata::{Metadata, load_metadata};
47pub use paths::{
48    CONFIG_DIR_ENV, DATA_DIR_ENV, DEFAULT_REGISTRY_URL, REGISTRY_DEFAULT, REGISTRY_DIR_ENV,
49    metadata_path, quadlet_dir, service_data_root, service_home, systemd_user_dir,
50};
51pub use plan::{AddResult, RemoveResult, ResetResult, Step, TailscalePort, TrackedEnv, Warning};
52pub use upgrade::{
53    BackupSnapshot, DEFAULT_BACKUP_KEEP, DiffEntry, DiffKind, DiffResult, EnvAddition,
54    RevertResult, UpgradeResult, diff_service, list_backups, prune_backups, revert_service,
55    upgrade_service,
56};
57pub use well_known::WellKnownService;
58
59pub(crate) use paths::home_dir;
60pub(crate) use well_known::caddy_https_port;
61
62/// Resolve the registry directory for a service reference.
63pub async fn resolve_registry_dir(service_ref: &registry::resolve::ServiceRef) -> Result<PathBuf> {
64    let paths = ConfigPaths::resolve()?;
65    paths.ensure_cache_dir()?;
66    let config = config::load_or_default(&paths.config_file)?;
67    registry::resolve::resolve_registry_dir(service_ref, &config, &paths.cache_dir).await
68}
69
70/// Build a ServiceRef from an installed service's stored registry name.
71pub fn service_ref_from_installed(installed: &InstalledService) -> registry::resolve::ServiceRef {
72    if installed.repo.is_empty() || installed.repo == REGISTRY_DEFAULT {
73        registry::resolve::ServiceRef::Default(installed.name.clone())
74    } else {
75        registry::resolve::ServiceRef::Custom {
76            registry: installed.repo.clone(),
77            service: installed.name.clone(),
78        }
79    }
80}
81
82/// When a shared-network provider (caddy or inbucket) is installed, patch
83/// already-installed services' primary quadlets to include `Network=<svc>.network`
84/// if they should reach the new provider. Emits `WriteFile` + `DaemonReload`
85/// + `RestartService` per patched service.
86///
87/// Scope is intentionally narrow: it only adds the network that the newly
88/// installed provider owns. The install-time networking policy for each
89/// patched service's OTHER networks is unchanged.
90fn retroactive_network_joins(
91    new_service: &str,
92    quadlet_path: &std::path::Path,
93    _repo_dir: Option<&std::path::Path>,
94) -> Vec<Step> {
95    let mut steps = Vec::new();
96    // Which join-relevant capability did the new service just become a
97    // provider of? `OidcProvider` doesn't trigger this — auth-aware
98    // services join its network at install time via the auth bridge,
99    // not retroactively.
100    let new_cap = if service_provides(new_service, Capability::ReverseProxy) {
101        Capability::ReverseProxy
102    } else if service_provides(new_service, Capability::SmtpRelay) {
103        Capability::SmtpRelay
104    } else {
105        return steps;
106    };
107
108    let installed = list_installed().unwrap_or_default();
109    for svc in &installed {
110        // Providers (of any capability) don't join themselves to other
111        // providers' networks via this path.
112        if !svc.provides.is_empty() {
113            continue;
114        }
115        let (network_name, should_join) = match new_cap {
116            Capability::ReverseProxy => {
117                // Services with a routed URL want the proxy network.
118                // Tailscale-exposed services route via `tailscale serve`,
119                // not the reverse proxy, so they skip.
120                let wants_proxy = matches!(
121                    svc.exposure,
122                    Exposure::Internal { .. } | Exposure::Public { .. }
123                );
124                (new_service.to_string(), wants_proxy)
125            }
126            Capability::SmtpRelay => {
127                // Any already-installed service whose .env points SMTP at
128                // the relay's hostname needs to reach it.
129                (
130                    new_service.to_string(),
131                    service_uses_smtp_relay(&svc.name, new_service),
132                )
133            }
134            // Unreachable: `new_cap` was selected from the two cases
135            // above. Match exhaustively so a new join-relevant capability
136            // forces a compile error here. Metrics wiring has its own
137            // retroactive pass (`retroactive_metrics_wiring`) because it
138            // writes targets/datasources, not just network joins.
139            Capability::OidcProvider
140            | Capability::ForwardAuthProvider
141            | Capability::MetricsStore
142            | Capability::MetricsDashboard => {
143                continue;
144            }
145        };
146        if !should_join {
147            continue;
148        }
149        let installed_names_owned: Vec<String> = installed.iter().map(|s| s.name.clone()).collect();
150        let all_service_names: Vec<&str> =
151            installed_names_owned.iter().map(|s| s.as_str()).collect();
152        steps.extend(network_join_steps(
153            &svc.name,
154            &network_name,
155            quadlet_path,
156            &all_service_names,
157        ));
158    }
159    steps
160}
161
162/// Steps joining every quadlet belonging to `svc_name` to
163/// `<network_name>.network`, followed by a daemon-reload and restarts so
164/// podman recreates the containers on the new network. Empty when every
165/// file already carries the network line.
166///
167/// Multi-container services (e.g. zammad with a separate railsserver
168/// that actually sends mail) need the network on every component
169/// container — restarting only the primary unit doesn't cascade to
170/// subunits, whose containers would keep running on the old network.
171fn network_join_steps(
172    svc_name: &str,
173    network_name: &str,
174    quadlet_path: &std::path::Path,
175    all_service_names: &[&str],
176) -> Vec<Step> {
177    let mut steps = Vec::new();
178    let marker = format!("Network={network_name}.network");
179    let mut units_to_restart: Vec<String> = Vec::new();
180    let Ok(entries) = std::fs::read_dir(quadlet_path) else {
181        return steps;
182    };
183    for entry in entries.flatten() {
184        let path = entry.path();
185        let name = match path.file_name().and_then(|n| n.to_str()) {
186            Some(n) if n.ends_with(".container") => n.to_string(),
187            _ => continue,
188        };
189        if !quadlet_belongs_to(&name, svc_name, all_service_names) {
190            continue;
191        }
192        let content = match std::fs::read_to_string(&path) {
193            Ok(c) => c,
194            Err(_) => continue,
195        };
196        if content.contains(&marker) {
197            continue;
198        }
199        // The quadlet dir holds symlinks into service homes — patch the
200        // real file, not the link: atomic_write (correctly) refuses to
201        // overwrite a symlink with a regular file.
202        let real_path = match std::fs::canonicalize(&path) {
203            Ok(p) => p,
204            Err(_) => continue,
205        };
206        let updated = generate::bundle::inject_networks(
207            &content,
208            std::slice::from_ref(&network_name.to_string()),
209        );
210        steps.push(Step::WriteFile(GeneratedFile {
211            path: real_path,
212            content: updated,
213        }));
214        // Unit name is the .container filename minus extension; systemd's
215        // generator turns `foo-bar.container` into `foo-bar.service`.
216        let unit = name.trim_end_matches(".container").to_string();
217        units_to_restart.push(unit);
218    }
219    if !units_to_restart.is_empty() {
220        steps.push(Step::DaemonReload);
221        for unit in units_to_restart {
222            steps.push(Step::RestartService { unit });
223        }
224    }
225    steps
226}
227
228/// The container port a metrics store's primary endpoint listens on,
229/// from its registry definition ("http" port, else the first declared).
230fn store_container_port(store_name: &str) -> Option<u16> {
231    let def = capability::lookup_registry_def(store_name)?;
232    def.ports
233        .iter()
234        .find(|p| p.name.eq_ignore_ascii_case("http"))
235        .or_else(|| def.ports.first())
236        .map(|p| p.container_port)
237}
238
239/// When a metrics store is being installed, wire up everything already
240/// on the machine: scrape targets for `[metrics]`-declaring services,
241/// datasources for dashboard providers, and the network joins both need
242/// to reach (or be reached by) the store. Consumers installed *after*
243/// the store are handled at their own add time instead.
244fn retroactive_metrics_wiring(
245    store_name: &str,
246    store_def: &registry::service_def::ServiceDef,
247    quadlet_path: &std::path::Path,
248) -> Vec<Step> {
249    let mut steps = Vec::new();
250    let installed = list_installed().unwrap_or_default();
251    let store_port = store_def
252        .ports
253        .iter()
254        .find(|p| p.name.eq_ignore_ascii_case("http"))
255        .or_else(|| store_def.ports.first())
256        .map(|p| p.container_port);
257    let installed_names_owned: Vec<String> = installed.iter().map(|s| s.name.clone()).collect();
258    let all_service_names: Vec<&str> = installed_names_owned.iter().map(|s| s.as_str()).collect();
259
260    for svc in &installed {
261        if svc.name == store_name {
262            continue;
263        }
264        // Needs the registry def for [metrics]/provides — services from
265        // other registries or missing from the cache are skipped (their
266        // next `ryra upgrade`/reinstall wires them).
267        let Some(def) = capability::lookup_registry_def(&svc.name) else {
268            continue;
269        };
270        let mut dashboard_wired = false;
271        let mut needs_join = false;
272        // Host-network consumers (node-exporter) are scraped via the host
273        // gateway at their *resolved* host port, parsed back out of the
274        // installed .env — and they never join the store's bridge network
275        // (Network=host conflicts with bridge networks).
276        let metrics_host_port = def.metrics.as_ref().and_then(|m| {
277            upgrade::read_existing_ports(&svc.name)
278                .ok()
279                .and_then(|ports| ports.get(&m.port.to_ascii_lowercase()).copied())
280        });
281        if let Ok(Some(step)) =
282            metrics_bridge::scrape_target_step(store_name, &def, metrics_host_port)
283        {
284            steps.push(step);
285            needs_join = def.metrics.as_ref().is_some_and(|m| !m.host_network);
286        }
287        if def
288            .capabilities
289            .provides
290            .contains(&Capability::MetricsDashboard)
291            && let Some(port) = store_port
292            && let Ok(step) = metrics_bridge::datasource_step(&svc.name, store_name, port)
293        {
294            steps.push(step);
295            dashboard_wired = true;
296            needs_join = true;
297        }
298        if !needs_join {
299            continue;
300        }
301        let join_steps =
302            network_join_steps(&svc.name, store_name, quadlet_path, &all_service_names);
303        // Dashboards read provisioning at boot — restart even when the
304        // network join was a no-op (e.g. the store was re-added and the
305        // quadlet still carries the network line).
306        let restarts_main = join_steps
307            .iter()
308            .any(|s| matches!(s, Step::RestartService { unit } if unit == &svc.name));
309        steps.extend(join_steps);
310        if dashboard_wired && !restarts_main {
311            steps.push(Step::RestartService {
312                unit: svc.name.clone(),
313            });
314        }
315    }
316    steps
317}
318
319/// Heuristic: does this service's `.env` point SMTP at the given relay's
320/// container hostname? Matches any line whose value is `<relay>` or
321/// `<relay>:<port>` — covers the common shape
322/// `SOMETHING_SMTP_HOST=<relay>` and variants like
323/// `FORGEJO__mailer__SMTP_ADDR=<relay>`.
324fn service_uses_smtp_relay(service_name: &str, relay_host: &str) -> bool {
325    let env_path = match service_home(service_name) {
326        Ok(h) => h.join(".env"),
327        Err(_) => return false,
328    };
329    let content = match std::fs::read_to_string(&env_path) {
330        Ok(c) => c,
331        Err(_) => return false,
332    };
333    let with_port = format!("{relay_host}:");
334    content.lines().any(|line| {
335        let Some((_, value)) = line.split_once('=') else {
336            return false;
337        };
338        let v = value.trim();
339        v == relay_host || v.starts_with(&with_port)
340    })
341}
342
343/// Determine which extra podman networks a service should join.
344///
345/// Four providers own a shared network:
346/// - `authelia.network` — services with `--auth` join so they can reach the
347///   OIDC provider by container DNS.
348/// - `inbucket.network` — services with SMTP configured join so they can
349///   reach `inbucket:2500` without requiring caddy to be installed.
350/// - `caddy.network` — URL-having services join for reverse-proxy routing;
351///   auth-enabled services join so OIDC discovery goes through caddy's TLS;
352///   inbucket itself joins so its web UI can be reverse-proxied when a URL
353///   is supplied.
354/// - the metrics store's network — `[metrics]`-declaring services join so
355///   the store can scrape them by container DNS; dashboard providers join
356///   so they can query the store.
357#[allow(clippy::too_many_arguments)]
358fn resolve_extra_networks(
359    service_name: &str,
360    enable_auth: bool,
361    authelia_installed: bool,
362    caddy_installed: bool,
363    inbucket_installed: bool,
364    has_url: bool,
365    has_smtp: bool,
366    metrics_store: Option<&str>,
367    wants_metrics: bool,
368) -> Vec<String> {
369    let mut networks = Vec::new();
370    if enable_auth && authelia_installed && !WellKnownService::Authelia.matches(service_name) {
371        networks.push(WellKnownService::Authelia.to_string());
372    }
373    // SMTP-using services reach inbucket via its own network — no caddy
374    // dependency. This is symmetric with how auth services reach authelia.
375    let joins_inbucket =
376        has_smtp && inbucket_installed && !WellKnownService::Inbucket.matches(service_name);
377    if joins_inbucket {
378        networks.push(WellKnownService::Inbucket.to_string());
379    }
380    let joins_caddy = (has_url || enable_auth || WellKnownService::Inbucket.matches(service_name))
381        && caddy_installed
382        && !WellKnownService::Caddy.matches(service_name);
383    if joins_caddy && !networks.contains(&WellKnownService::Caddy.to_string()) {
384        networks.push(WellKnownService::Caddy.to_string());
385    }
386    // Scraped services and dashboards reach the metrics store the same
387    // way SMTP users reach inbucket — over the store's own network.
388    if let Some(store) = metrics_store
389        && wants_metrics
390        && store != service_name
391    {
392        networks.push(store.to_string());
393    }
394    networks
395}
396
397/// Why the planner is running. The render path is shared between fresh
398/// installs and re-renders (`ryra upgrade`); the side-effect steps are
399/// not — re-registering an OIDC client on upgrade would mint a new
400/// `client_id`/`client_secret` against authelia's existing entry, and
401/// patching every other installed service's quadlet (retroactive network
402/// joins) is install-time work. The mode gates those.
403#[derive(Debug, Clone, Copy, PartialEq, Eq)]
404pub enum PlanMode {
405    /// Fresh install. Validate that the service isn't already on disk,
406    /// register OIDC clients, retroactively patch other services to
407    /// join shared networks, set up Tailscale, and start the unit.
408    Add,
409    /// Re-render an installed service to pick up registry changes.
410    /// Skips the validation rejects and the install-time side effects;
411    /// the upgrade caller handles diff/backup/restart.
412    Upgrade,
413}
414
415/// The user's auth decision for one install.
416///
417/// Replaces the `(Option<AuthKind>, bool)` pair that previously travelled
418/// through the planner, where `enable_auth = false` with a `Some` kind was
419/// representable but meaningless. Frontends resolve "--auth on a service
420/// with no native kinds" before constructing this (error, or a no-op note
421/// for the OIDC provider itself), so a choice always round-trips through
422/// `metadata.toml` as `Option<AuthKind>`.
423#[derive(Debug, Clone, PartialEq, Eq)]
424pub enum AuthChoice {
425    /// No auth integration.
426    None,
427    /// Auth with the service's native OIDC integration: OIDC env vars are
428    /// templated and a client is registered with the provider.
429    Native(registry::service_def::AuthKind),
430}
431
432impl AuthChoice {
433    /// True when the user asked for auth. Drives the auth fabric:
434    /// network joins, the auth bridge, the no-native-OIDC validation.
435    pub fn enabled(&self) -> bool {
436        !matches!(self, AuthChoice::None)
437    }
438
439    /// The native OIDC kind, when the service has one. Drives OIDC env
440    /// templating and client registration.
441    pub fn native_kind(&self) -> Option<&registry::service_def::AuthKind> {
442        match self {
443            AuthChoice::Native(kind) => Some(kind),
444            AuthChoice::None => None,
445        }
446    }
447}
448
449/// Inputs to [`add_service`]. One typed request instead of a positional
450/// argument list, so call sites (CLI today, other frontends later) name
451/// what they're asking for and the retry path can't drift out of sync
452/// with the original call.
453pub struct AddServiceParams<'a> {
454    pub service_name: &'a str,
455    pub exposure: &'a Exposure,
456    pub auth: AuthChoice,
457    pub enable_smtp: bool,
458    pub enable_backup: bool,
459    pub env_overrides: &'a BTreeMap<String, String>,
460    pub enabled_groups: &'a std::collections::BTreeSet<String>,
461    /// `[[choice]]` selections (`choice name -> option name`). Choices absent
462    /// from the map fall back to their declared `default` at render time.
463    pub selected_choices: &'a BTreeMap<String, String>,
464    pub registry_name: &'a str,
465    pub repo_dir: &'a Path,
466    /// When provided, its secrets and auth credentials are reused instead
467    /// of generating fresh ones. Pass the context from the interactive
468    /// prompt phase so the values the user saw match what gets written.
469    pub pre_built_ctx: Option<BTreeMap<String, String>>,
470    pub port_in_use: &'a dyn Fn(u16) -> bool,
471    pub acme_mode: Option<&'a caddy::AcmeMode>,
472    pub mode: PlanMode,
473    /// Pin specific port assignments by name (e.g. `{"http": 10005}`)
474    /// instead of running the allocator. Used by upgrade so a re-render
475    /// preserves the install's existing host ports — port_in_use would
476    /// say they're taken (the running service holds them) and the
477    /// allocator would skip to the next free one.
478    pub port_overrides: &'a BTreeMap<String, u16>,
479}
480
481/// Build the Caddy site-block + reload steps for a service exposed at `url`,
482/// or a `UrlWithoutReverseProxy` warning when no Caddy is installed. Shared by
483/// the podman and native add paths so both expose services identically; the
484/// caller supplies the reverse-proxy upstream, which is the only thing that
485/// differs by runtime: `<container>:<container_port>` (podman, on Caddy's
486/// shared network) vs `host.containers.internal:<host_port>` (native, a host
487/// process reached over the bridge — the same convention the metrics bridge
488/// uses for native scrape targets).
489fn caddy_route_steps(
490    service_name: &str,
491    url: &str,
492    target_host: String,
493    upstream_port: u16,
494    host_port: Option<u16>,
495    caddy_installed: bool,
496    https_port: u16,
497) -> Result<(Vec<Step>, Vec<Warning>)> {
498    let mut steps = Vec::new();
499    let mut warnings = Vec::new();
500    if caddy_installed {
501        let parsed = url::Url::parse(url)
502            .map_err(|e| Error::Template(format!("invalid service URL '{url}': {e}")))?;
503        let domain = parsed.host_str().ok_or_else(|| {
504            Error::Template(format!(
505                "service URL '{url}' has no host — Caddy needs a hostname to route to"
506            ))
507        })?;
508        let block = caddy::render_site_block(&caddy::CaddySiteParams {
509            service_name: service_name.to_string(),
510            target_host,
511            domain: domain.to_string(),
512            container_port: upstream_port,
513            https_port,
514            force_internal_tls: false,
515        });
516        let caddyfile_path = caddy::caddyfile_path()?;
517        let existing =
518            std::fs::read_to_string(&caddyfile_path).map_err(|source| Error::FileRead {
519                path: caddyfile_path.clone(),
520                source,
521            })?;
522        let updated = caddy::add_route(&existing, service_name, &block);
523        steps.push(Step::WriteFile(GeneratedFile {
524            path: caddyfile_path,
525            content: updated,
526        }));
527        steps.push(Step::ReloadCaddy);
528    } else if let Some(primary) = host_port {
529        // --url but no ryra-managed reverse proxy: routing is the user's job
530        // (their own nginx / Cloudflare Tunnel / etc.).
531        warnings.push(Warning::UrlWithoutReverseProxy {
532            service_name: service_name.to_string(),
533            url: url.to_string(),
534            host_port: primary,
535        });
536    }
537    Ok((steps, warnings))
538}
539
540/// Add a service: generate config, return steps to execute.
541pub fn add_service(params: AddServiceParams<'_>) -> Result<AddResult> {
542    let AddServiceParams {
543        service_name,
544        exposure,
545        auth,
546        enable_smtp,
547        enable_backup,
548        env_overrides,
549        enabled_groups,
550        selected_choices,
551        registry_name,
552        repo_dir,
553        pre_built_ctx,
554        port_in_use,
555        acme_mode,
556        mode,
557        port_overrides,
558    } = params;
559    // Derived views of the typed inputs, bound once for the many body
560    // sites that only need one facet. Helpers that need the full picture
561    // (env templating, exposure-variant dispatch) take `&Exposure` /
562    // `&AuthChoice` facets directly.
563    let auth_kind: Option<&registry::service_def::AuthKind> = auth.native_kind();
564    let enable_auth: bool = auth.enabled();
565    let url: Option<&str> = exposure.url();
566    let paths = ConfigPaths::resolve()?;
567    let config = config::load_or_default(&paths.config_file)?;
568
569    // Quadlet directory is the source of truth: a marker'd `.container`
570    // means the service is already installed.
571    //
572    // Upgrade explicitly *re-renders* an installed service — those rejects
573    // would block the legitimate path.
574    if mode == PlanMode::Add {
575        if is_service_installed(service_name) {
576            return Err(Error::ServiceAlreadyInstalled(service_name.to_string()));
577        }
578
579        // No config entry, but preserved volumes or a lingering home dir from
580        // `ryra remove <svc>` (default Preserve mode) would make the fresh .env's
581        // generated secrets disagree with what's already baked into the volume —
582        // postgres writes POSTGRES_PASSWORD into pgdata on first init and then
583        // skips reinit, so a new password in .env just restart-loops on auth
584        // failures. Surface the same way as an incomplete install; the CLI's
585        // existing purge-and-retry recovery handles it.
586        if data::enumerate_service(service_name)?.is_some() {
587            return Err(Error::ServiceIncomplete(service_name.to_string()));
588        }
589    }
590
591    let reg_service = registry::find_service(repo_dir, service_name)?;
592
593    // Validate: architecture compatibility
594    if let Some(msg) = reg_service.def.check_architecture() {
595        return Err(Error::UnsupportedArchitecture(msg));
596    }
597
598    // Validate: all required services must be installed. `requires` is a
599    // top-level cross-service dependency on a *shared* service (e.g. the auth
600    // provider). A service's own containers live in its `quadlets/` (gated by
601    // choice), so per-option infra is owned, never required, no overlap.
602    let missing_requires: Vec<&str> = reg_service
603        .def
604        .requires
605        .iter()
606        .filter(|r| !is_service_installed(&r.service))
607        .map(|r| r.service.as_str())
608        .collect();
609    if !missing_requires.is_empty() {
610        return Err(Error::MissingRequiredServices {
611            service: service_name.to_string(),
612            missing: missing_requires.iter().map(|s| s.to_string()).collect(),
613        });
614    }
615
616    // If the user chose to enable auth, an auth provider must be configured
617    if auth_kind.is_some() && config.auth.is_none() {
618        return Err(Error::AuthNotConfigured);
619    }
620
621    // --auth requires native OIDC support; forward auth is no longer supported.
622    // The exception is the OIDC provider itself, which doesn't need to act as
623    // a client of itself.
624    if enable_auth
625        && reg_service.def.integrations.auth.is_empty()
626        && !capability::def_provides(&reg_service.def, Capability::OidcProvider)
627    {
628        return Err(Error::NoOidcSupport(service_name.to_string()));
629    }
630
631    // --backup requires the service author to have certified backup
632    // safety. Refusing here means a user typo can't silently produce
633    // an install whose backups would never restore cleanly.
634    if enable_backup && !reg_service.def.integrations.backup {
635        return Err(Error::BackupNotSupported(service_name.to_string()));
636    }
637
638    // Every `--enable <group>` must match a group defined on this service.
639    // Surfacing unknown group names here (vs. silently ignoring them) means
640    // a typo fails fast instead of producing a half-configured service.
641    for g in enabled_groups {
642        if !reg_service.def.env_groups.iter().any(|eg| &eg.name == g) {
643            let known: Vec<String> = reg_service
644                .def
645                .env_groups
646                .iter()
647                .map(|eg| eg.name.clone())
648                .collect();
649            let hint = if known.is_empty() {
650                " (service defines no env_groups)".to_string()
651            } else {
652                format!(" (known: {})", known.join(", "))
653            };
654            return Err(Error::UnknownEnvGroup {
655                service: service_name.to_string(),
656                group: g.clone(),
657                hint,
658            });
659        }
660    }
661
662    // Resolve a host port for every entry in [[ports]]. Each port gets its
663    // own distinct host port — a prior bug allocated one port and gave it
664    // to every entry, so services with multiple [[ports]] (ente-web:
665    // 3000/3002/3003, inbucket: http+smtp) hit `bind: address already in
666    // use` on all but the first.
667    let mut port_warnings: Vec<Warning> = Vec::new();
668    // Ports to allocate: top-level [[ports]] plus the [[ports]] of each choice's
669    // selected option (gated infra, e.g. a bundled DB's published loopback
670    // port). ryra picks a free host port for each, so there's no hardcoded port
671    // to collide. Unselected options contribute none.
672    let mut effective_ports: Vec<&registry::service_def::PortDef> =
673        reg_service.def.ports.iter().collect();
674    for choice in &reg_service.def.choices {
675        let sel = selected_choices
676            .get(&choice.name)
677            .unwrap_or(&choice.default);
678        if let Some(opt) = choice.options.iter().find(|o| &o.name == sel) {
679            effective_ports.extend(opt.ports.iter());
680        }
681    }
682    let mut claimed: std::collections::HashSet<u16> =
683        effective_ports.iter().filter_map(|p| p.host_port).collect();
684    let mut resolved_ports: Vec<(String, u16)> = Vec::with_capacity(effective_ports.len());
685    for p in effective_ports.iter().copied() {
686        let host = if let Some(pinned) = port_overrides.get(&p.name) {
687            // Upgrade passes the install's existing port here so re-renders
688            // are stable. Trust the caller — port_in_use would say it's
689            // taken (the running service holds it) and the allocator would
690            // pick a different one.
691            *pinned
692        } else if let Some(hp) = p.host_port {
693            hp
694        } else {
695            let privileged = p.container_port < 1024;
696            let claimed_in_service = claimed.contains(&p.container_port);
697            let in_use = port_in_use(p.container_port);
698            if privileged || claimed_in_service || in_use {
699                let allocated = system::port::allocate_port_excluding(&claimed, port_in_use)?;
700                let reason = if privileged {
701                    "port is privileged (requires root)".to_string()
702                } else if claimed_in_service {
703                    format!(
704                        "port {} is already claimed by another port in this service",
705                        p.container_port
706                    )
707                } else {
708                    format!("port {} is already in use", p.container_port)
709                };
710                port_warnings.push(Warning::PortReassigned {
711                    service_name: service_name.to_string(),
712                    port_name: p.name.clone(),
713                    original_port: p.container_port,
714                    assigned_port: allocated,
715                    reason,
716                });
717                allocated
718            } else {
719                p.container_port
720            }
721        };
722        claimed.insert(host);
723        resolved_ports.push((p.name.clone(), host));
724    }
725
726    // Caddy host-port binding is driven by the `binding` choice, not by ambient
727    // kernel state: `proxied` (default) keeps the always-works high ports
728    // 8080/8443 (rootless-safe, what tests/LAN/behind-a-proxy use); `direct`
729    // is an explicit opt-in to 80/443 for a public origin. `direct` *needs*
730    // the unprivileged low-port sysctl — if it isn't in place we keep the high
731    // ports rather than emit a quadlet that fails to bind at runtime (the CLI
732    // offers to set the sysctl when you choose `direct`, so the common path is
733    // already covered). Default-absent selection reads as `proxied`.
734    let caddy_direct = selected_choices
735        .get("binding")
736        .map(|s| s == "direct")
737        .unwrap_or(false);
738    if WellKnownService::Caddy.matches(service_name)
739        && caddy_direct
740        && system::sysctl::rootless_can_bind_low_ports()
741    {
742        for (name, port) in resolved_ports.iter_mut() {
743            match name.as_str() {
744                "http" if *port == 8080 => *port = 80,
745                "https" if *port == 8443 => *port = 443,
746                _ => {}
747            }
748        }
749    }
750
751    // Primary host port drives service.url / service.port templating.
752    // Prefer "http", fall back to the first defined port.
753    let host_port = resolved_ports
754        .iter()
755        .find(|(name, _)| name.eq_ignore_ascii_case("http"))
756        .or_else(|| resolved_ports.first())
757        .map(|(_, p)| *p);
758
759    // Check for port conflicts by probing whether the port is already bound.
760    // For explicitly-set host_port entries, allocate_port_excluding didn't run
761    // so we re-check here.
762    for (_, port) in &resolved_ports {
763        if port_in_use(*port) {
764            return Err(Error::PortConflict { port: *port });
765        }
766    }
767
768    // Blue/green: the routable port needs a *second* host port so both color
769    // slots can bind at once. The base resolved port is the blue slot; allocate
770    // a green sibling and expose both as SERVICE_PORT_<NAME>_BLUE / _GREEN. The
771    // base SERVICE_PORT_<NAME> stays pointed at the active (blue) slot so
772    // templates and OIDC redirects keep working. On upgrade, `port_overrides`
773    // pins the green port from the install's `.env` so it stays stable.
774    let blue_green =
775        reg_service.def.service.deploy == registry::service_def::DeployStrategy::BlueGreen;
776    if blue_green {
777        let primary = resolved_ports
778            .iter()
779            .find(|(name, _)| name.eq_ignore_ascii_case("http"))
780            .or_else(|| resolved_ports.first())
781            .map(|(name, port)| (name.clone(), *port));
782        if let Some((name, blue_port)) = primary {
783            let green_key = format!("{}_green", name.to_ascii_lowercase());
784            let green_port = match port_overrides.get(&green_key) {
785                Some(pinned) => *pinned,
786                None => {
787                    let p = system::port::allocate_port_excluding(&claimed, port_in_use)?;
788                    claimed.insert(p);
789                    p
790                }
791            };
792            resolved_ports.push((format!("{name}_blue"), blue_port));
793            resolved_ports.push((format!("{name}_green"), green_port));
794        }
795    }
796
797    let home_dir = service_home(service_name)?;
798    let quadlet_path = quadlet_dir()?;
799
800    // Authoritative: capability presence in `list_installed` answers
801    // "is there a reverse-proxy / OIDC IdP / SMTP relay / metrics
802    // scraper installed?" without naming any specific provider.
803    let installed_now = list_installed().unwrap_or_default();
804    let authelia_installed =
805        find_installed_provider(&installed_now, Capability::OidcProvider).is_some();
806    let caddy_installed =
807        find_installed_provider(&installed_now, Capability::ReverseProxy).is_some();
808    let inbucket_installed =
809        find_installed_provider(&installed_now, Capability::SmtpRelay).is_some();
810    let metrics_store =
811        find_installed_provider(&installed_now, Capability::MetricsStore).map(|s| s.name.clone());
812
813    // --auth routes the OIDC back-channel through Caddy as the internal TLS
814    // terminator (see auth_bridge). If authelia is installed and reachable
815    // but Caddy isn't, the bridge would silently no-op and OIDC would break
816    // only at login time. Fail loudly at add time instead. Skip the auth
817    // infra itself (the IdP and the proxy don't consume the bridge).
818    let provides_auth_infra = reg_service
819        .def
820        .capabilities
821        .provides
822        .iter()
823        .any(|c| matches!(c, Capability::OidcProvider | Capability::ReverseProxy));
824    if enable_auth
825        && !provides_auth_infra
826        && !caddy_installed
827        && let Some(authelia) = find_installed_provider(&installed_now, Capability::OidcProvider)
828        && let Some(auth_url) = authelia.exposure.url()
829    {
830        return Err(Error::AuthRequiresReverseProxy {
831            service: service_name.to_string(),
832            auth_url: auth_url.to_string(),
833        });
834    }
835
836    // Build auth-bridge artifacts (CA trust + dynamic /etc/hosts for the
837    // auth provider's domain). Pure — all filesystem writes are
838    // emitted as Step::WriteFile below, not performed here.
839    let auth_bridge = auth_bridge::build(&auth_bridge::AuthBridgeParams {
840        service_name,
841        service_provides: &reg_service.def.capabilities.provides,
842        enable_auth,
843        config: &config,
844        installed: &installed_now,
845        service_data: &home_dir,
846    })?;
847
848    let (extra_volumes, extra_env, extra_exec_start_pre, auth_bridge_steps) = match auth_bridge {
849        Some(b) => (b.volumes, b.env, b.exec_start_pre, b.steps),
850        None => (Vec::new(), BTreeMap::new(), Vec::new(), Vec::new()),
851    };
852
853    let has_smtp = enable_smtp
854        && reg_service.def.integrations.smtp
855        && !reg_service.def.mappings.smtp.is_empty()
856        && config.smtp.is_some();
857    // Host-network metrics services (node-exporter) can't join a bridge
858    // network — their scrape target goes via the host gateway instead.
859    let wants_metrics = reg_service
860        .def
861        .metrics
862        .as_ref()
863        .is_some_and(|m| !m.host_network)
864        || capability::def_provides(&reg_service.def, Capability::MetricsDashboard);
865    let extra_networks = resolve_extra_networks(
866        service_name,
867        enable_auth,
868        authelia_installed,
869        caddy_installed,
870        inbucket_installed,
871        url.is_some(),
872        has_smtp,
873        metrics_store.as_deref(),
874        wants_metrics,
875    );
876
877    let output = generate::generate_env(generate::GenerateEnvParams {
878        config: &config,
879        service_def: &reg_service.def,
880        auth_kind,
881        host_port,
882        resolved_ports: &resolved_ports,
883        env_overrides,
884        exposure,
885        extra_env,
886        pre_built_ctx,
887        enable_smtp: has_smtp,
888        enabled_groups,
889        selected_choices,
890    })?;
891
892    let podman_args: Vec<String> = Vec::new();
893
894    // Declared port names, for validating ${SERVICE_PORT_*} quadlet refs.
895    let port_names: Vec<String> = resolved_ports.iter().map(|(n, _)| n.clone()).collect();
896
897    // Build install metadata persisted to `metadata.toml` in service_home.
898    // This is what makes service state authoritative for "what's installed
899    // and how is it wired" — every field a future `ryra list` needs to
900    // reconstruct the install reads back from this file.
901    // `smtp_enabled` captures *user intent* (the `enable_smtp` flag the
902    // caller passed) rather than the gated render flag (`has_smtp`).
903    // Otherwise an install on a host without globally-configured SMTP
904    // records `false`, and a later `ryra configure` that doesn't touch
905    // SMTP would still show as "modified" because the legacy default
906    // (`true`) disagrees with what gets serialized. Storing intent keeps
907    // metadata stable across re-renders and lets `ryra configure --smtp`
908    // remember the choice even before global SMTP is configured.
909    // A blue/green install comes up on the `blue` slot; restart-strategy
910    // installs carry no color. The deploy flow flips this on each `ryra
911    // upgrade`.
912    let active_color = match reg_service.def.service.deploy {
913        registry::service_def::DeployStrategy::BlueGreen => {
914            Some(registry::service_def::Color::Blue)
915        }
916        registry::service_def::DeployStrategy::Restart => None,
917    };
918    let install_metadata = Metadata {
919        registry: registry_name.to_string(),
920        url: url.map(str::to_string),
921        auth: auth_kind.cloned(),
922        provides: reg_service.def.capabilities.provides.clone(),
923        backup_enabled: enable_backup,
924        smtp_enabled: enable_smtp,
925        enabled_groups: enabled_groups.iter().cloned().collect(),
926        selected_choices: selected_choices.clone(),
927        runtime: reg_service.def.service.runtime.clone(),
928        active_color,
929    };
930
931    // Native services have no quadlet bundle / image: build the binary, install
932    // it, write a plain systemd --user unit, and start it. Returns here so the
933    // entire podman path below stays untouched. Reuses everything already
934    // computed: home_dir, the generated .env (`output`), ports, and metadata.
935    if reg_service.def.service.runtime == registry::service_def::Runtime::Native {
936        let tracked_envs = collect_static_envs(
937            &reg_service.def,
938            &output.ctx,
939            enabled_groups,
940            selected_choices,
941        )?;
942        let allocated_ports = resolved_ports.clone();
943        let generated_secrets = collect_generated_secrets(&reg_service.def, env_overrides);
944
945        // Caddy route for native services. Unlike podman slots (containers on
946        // Caddy's shared network, reached by name), a native process is reached
947        // over the host bridge at `host.containers.internal:<host_port>`. For
948        // blue/green the active (blue) slot's port is the upstream; the swap
949        // repoints it to green on `ryra upgrade`.
950        let native_blue_green =
951            reg_service.def.service.deploy == registry::service_def::DeployStrategy::BlueGreen;
952        let mut caddy_steps: Vec<Step> = Vec::new();
953        let mut native_warnings: Vec<Warning> = Vec::new();
954        if let Some(u) = url
955            && !exposure.is_tailscale()
956        {
957            let upstream_port = if native_blue_green {
958                resolved_ports
959                    .iter()
960                    .find(|(n, _)| n.eq_ignore_ascii_case("http_blue"))
961                    .map(|(_, p)| *p)
962            } else {
963                host_port
964            };
965            if let Some(p) = upstream_port {
966                let (route_steps, route_warnings) = caddy_route_steps(
967                    service_name,
968                    u,
969                    "host.containers.internal".to_string(),
970                    p,
971                    host_port,
972                    caddy_installed,
973                    caddy_https_port(&config),
974                )?;
975                caddy_steps = route_steps;
976                native_warnings.extend(route_warnings);
977            }
978        }
979
980        return build_native_add(NativeAddParams {
981            service_name,
982            reg_service: &reg_service,
983            home_dir: &home_dir,
984            output,
985            install_metadata: &install_metadata,
986            registry_name,
987            url,
988            tracked_envs,
989            allocated_ports,
990            generated_secrets,
991            excluded_quadlets: excluded_quadlets(&reg_service.def, selected_choices),
992            caddy_steps,
993            warnings: native_warnings,
994        });
995    }
996
997    let excluded_quadlets = excluded_quadlets(&reg_service.def, selected_choices);
998
999    // Process quadlet bundle from registry
1000    let bundle =
1001        generate::bundle::process_quadlet_bundle(&generate::bundle::ProcessBundleParams {
1002            service_dir: &reg_service.service_dir,
1003            service_name,
1004            extra_networks: &extra_networks,
1005            extra_volumes: &extra_volumes,
1006            podman_args: &podman_args,
1007            extra_exec_start_pre: &extra_exec_start_pre,
1008            port_names: &port_names,
1009            excluded_quadlets: &excluded_quadlets,
1010        })?;
1011
1012    // Generate warnings
1013    let mut warnings = Vec::new();
1014
1015    if let Some(ref reqs) = reg_service.def.requirements
1016        && let Some(total) = system::memory::total_ram_mb()
1017    {
1018        if total < reqs.ram.min {
1019            warnings.push(Warning::RamBelowMinimum {
1020                service_name: service_name.to_string(),
1021                min_mb: reqs.ram.min,
1022                available_mb: total,
1023            });
1024        } else if let Some(rec) = reqs.ram.recommended
1025            && total < rec
1026        {
1027            warnings.push(Warning::RamBelowRecommended {
1028                service_name: service_name.to_string(),
1029                recommended_mb: rec,
1030                available_mb: total,
1031            });
1032        }
1033    }
1034    warnings.extend(port_warnings);
1035
1036    // Build ordered steps
1037    let mut steps = Vec::new();
1038
1039    // 1. Create service data directory
1040    steps.push(Step::CreateDir(home_dir.clone()));
1041
1042    // Capture env content before it is moved into steps
1043    let env_content = output.env_file.content.clone();
1044
1045    // 2. Pull all images (from quadlet bundle)
1046    for image in &bundle.images {
1047        steps.push(Step::PullImage {
1048            image: image.clone(),
1049        });
1050    }
1051
1052    // 3. Write quadlet files from bundle (real files live in service_home)
1053    //    and symlink each one into the systemd-mandated quadlet path so
1054    //    quadlet's generator finds them on daemon-reload.
1055    //    Blue/green: split the main app container into blue + green slots
1056    //    first (aux quadlets pass through untouched).
1057    let quadlet_files = if blue_green {
1058        deploy::expand_color_quadlets(bundle.quadlet_files, service_name)
1059    } else {
1060        bundle.quadlet_files
1061    };
1062    for file in quadlet_files {
1063        let link = file
1064            .path
1065            .file_name()
1066            .map(|n| quadlet_path.join(n))
1067            .ok_or_else(|| {
1068                Error::Bundle(format!("invalid quadlet path: {}", file.path.display()))
1069            })?;
1070        let target = file.path.clone();
1071        steps.push(Step::WriteFile(file));
1072        steps.push(Step::Symlink { link, target });
1073    }
1074
1075    // 3b. Write metadata.toml — install record (registry, exposure, url,
1076    // auth) used by `ryra list` / `remove` / `status` to reconstruct the
1077    // install. Emitted *before* any step that can fail remotely (Tailscale
1078    // API calls, image pulls) so a partial install can still be torn down
1079    // by `remove_service` — which relies on metadata.toml to identify the
1080    // install. World-readable mode (atomic_write picks 0o644 by name).
1081    let metadata_content = toml::to_string_pretty(&install_metadata)?;
1082    steps.push(Step::WriteFile(GeneratedFile {
1083        path: metadata_path(service_name)?,
1084        content: metadata_content,
1085    }));
1086
1087    // 3c. Tailscale Services — when `--tailscale` was used, the host's
1088    // existing tailscaled advertises the service at
1089    // `https://<name>.<tailnet>.ts.net` (TailVIP-routed). One-time
1090    // `TailscaleSetup` ensures ACL tags + auto-approval are in place;
1091    // `TailscaleEnable` defines the service via admin API and runs
1092    // `tailscale serve --service=...` from the host. No sidecar
1093    // containers, no per-service tailscaled.
1094    if mode == PlanMode::Add && exposure.is_tailscale() {
1095        // Scope the Tailscale Service name by host (`<service>-<host>`)
1096        // — Tailscale Services are global per tailnet, so without the
1097        // suffix two ryra machines that both `ryra add vikunja --tailscale`
1098        // would silently stomp each other's registration. The svc_name
1099        // falls out of the exposure URL (built by
1100        // `system::tailscale::derive_service_url` with the host suffix)
1101        // — keeping URL as the single source of
1102        // truth means `metadata.toml` round-trips and remove paths
1103        // recover the same name without re-shelling tailscale.
1104        let svc_name = exposure.tailscale_svc_name().ok_or_else(|| {
1105            Error::InvalidServiceRef(format!(
1106                "tailscale exposure for '{service_name}' has a malformed URL — \
1107                 expected `https://<service>-<host>.<tailnet>.ts.net/`"
1108            ))
1109        })?;
1110        // A multi-port service (e.g. ente: web UI + API) serves each
1111        // tailscale_https port; single-port services serve their primary
1112        // port at the web root. Empty only when there are no ports at all.
1113        let ts_ports = plan::tailscale_ports(&reg_service.def.ports, &resolved_ports, host_port);
1114        if !ts_ports.is_empty() {
1115            steps.push(Step::TailscaleSetup);
1116            steps.push(Step::TailscaleEnable {
1117                svc_name,
1118                ports: ts_ports,
1119            });
1120        }
1121    }
1122
1123    // 4. Write config files from bundle
1124    for file in bundle.config_files {
1125        steps.push(Step::WriteFile(file));
1126    }
1127
1128    // 4b. Copy vendored files (plugin DLLs, archives etc.) from the
1129    // registry into service_home. The config pipeline is UTF-8 /
1130    // template-only; binary payloads flow through CopyFile instead.
1131    for (src, dst) in bundle.files {
1132        steps.push(Step::CopyFile { src, dst });
1133    }
1134
1135    // 5. Write .env file
1136    steps.push(Step::WriteFile(output.env_file));
1137
1138    // 6. Create bind mount directories (must exist before container starts)
1139    for dir in &bundle.bind_mount_dirs {
1140        steps.push(Step::CreateDir(dir.clone()));
1141    }
1142
1143    // 7. Auth-bridge artifacts (CA bundle, refresh script, host-resolve script,
1144    // placeholder /etc/hosts) — needed before container starts for TLS trust
1145    // and auth-domain resolution.
1146    steps.extend(auth_bridge_steps);
1147
1148    // 8. Register OIDC client with the auth provider BEFORE starting the service.
1149    // This must happen first because the service's ExecStartPost (e.g., register-oidc.sh)
1150    // needs the auth provider configured and caddy's network alias in place so OIDC
1151    // discovery URLs resolve correctly from within the service container.
1152    //
1153    // Skipped on upgrade: build_context generates a fresh client_id/secret
1154    // every call, so re-registering would append a second entry to authelia's
1155    // configuration.yml and break the existing OIDC integration.
1156    if mode == PlanMode::Add
1157        && let (
1158            Some(registry::service_def::AuthKind::Oidc),
1159            Some(config::schema::AuthCredentials::Authelia { .. }),
1160        ) = (auth_kind, config.auth.as_ref())
1161    {
1162        steps.extend(authelia::register_oidc_client(
1163            service_name,
1164            &reg_service.def,
1165            url,
1166            &output.ctx,
1167            &quadlet_path,
1168        )?);
1169    }
1170
1171    // 9. Add Caddy route for services with a URL when Caddy is installed.
1172    // This creates a reverse proxy from the service's domain to its container port.
1173    //
1174    // Tailscale exposures skip this: the service is already reachable on
1175    // its host port via the tailnet, and MagicDNS handles the hostname.
1176    if let Some(url) = url
1177        && !WellKnownService::Caddy.matches(service_name)
1178        && !exposure.is_tailscale()
1179    {
1180        let container_port = reg_service
1181            .def
1182            .ports
1183            .first()
1184            .map(|p| p.container_port)
1185            .unwrap_or(80);
1186        let primary_quadlet = reg_service
1187            .service_dir
1188            .join("quadlets")
1189            .join(format!("{service_name}.container"));
1190        // Blue/green routes at the active color's container; the swap
1191        // (`ryra upgrade`) repoints this to the other color and reloads.
1192        let target_host = if blue_green {
1193            deploy::color_unit(service_name, registry::service_def::Color::Blue)
1194        } else {
1195            caddy::primary_container_name(&primary_quadlet, service_name)
1196        };
1197        let (route_steps, route_warnings) = caddy_route_steps(
1198            service_name,
1199            url,
1200            target_host,
1201            container_port,
1202            host_port,
1203            caddy_installed,
1204            caddy_https_port(&config),
1205        )?;
1206        steps.extend(route_steps);
1207        warnings.extend(route_warnings);
1208    }
1209
1210    // 9b. When a shared-network provider (caddy, inbucket) is being
1211    // installed, retroactively patch services that were installed before
1212    // it so they can reach the new provider by container DNS.
1213    // resolve_extra_networks only decides at install time; without this
1214    // step, services installed earlier remain isolated.
1215    //
1216    // Skipped on upgrade: re-running on the same shared-network provider
1217    // would re-patch services unnecessarily, and a re-render of a regular
1218    // service shouldn't touch its peers' quadlets.
1219    if mode == PlanMode::Add {
1220        steps.extend(retroactive_network_joins(
1221            service_name,
1222            &quadlet_path,
1223            Some(repo_dir),
1224        ));
1225    }
1226
1227    // 9c. Metrics wiring. Consumer side at install time: a service that
1228    // declares [metrics] gets a file_sd scrape target dropped into the
1229    // installed store (live — file_sd needs no reload); a dashboard
1230    // provider gets a datasource provisioned before its first start.
1231    // Provider side retroactively: a store being installed wires up
1232    // everything already on the machine.
1233    if mode == PlanMode::Add {
1234        if let Some(store) = &metrics_store {
1235            let metrics_host_port = reg_service.def.metrics.as_ref().and_then(|m| {
1236                resolved_ports
1237                    .iter()
1238                    .find(|(n, _)| n == &m.port)
1239                    .map(|(_, p)| *p)
1240            });
1241            if let Some(step) =
1242                metrics_bridge::scrape_target_step(store, &reg_service.def, metrics_host_port)?
1243            {
1244                steps.push(step);
1245            }
1246            if capability::def_provides(&reg_service.def, Capability::MetricsDashboard)
1247                && let Some(port) = store_container_port(store)
1248            {
1249                steps.push(metrics_bridge::datasource_step(service_name, store, port)?);
1250            }
1251        }
1252        if capability::def_provides(&reg_service.def, Capability::MetricsStore) {
1253            steps.extend(retroactive_metrics_wiring(
1254                service_name,
1255                &reg_service.def,
1256                &quadlet_path,
1257            ));
1258        }
1259    }
1260
1261    // 9d. Caddy: seed the user-owned `tls.caddy` snippet on first install.
1262    // Site blocks emit `import services_tls`; this file defines that snippet.
1263    // After first write ryra never touches it — users edit it directly
1264    // for Cloudflare DNS-01, wildcards, BYO certs, plain HTTP for Tunnel,
1265    // anything Caddy supports. seed-caddyfile.sh defensively recreates
1266    // the file as `tls internal` on container start if it goes missing.
1267    if WellKnownService::Caddy.matches(service_name) {
1268        let snippet_path = caddy::tls_snippet_path()?;
1269        if !snippet_path.exists() {
1270            let mode = acme_mode.cloned().unwrap_or(caddy::AcmeMode::Internal);
1271            steps.push(Step::WriteFile(GeneratedFile {
1272                path: snippet_path,
1273                content: mode.snippet(),
1274            }));
1275        }
1276    }
1277
1278    // 9z. Manifest — sha256 list of every file we just emitted, written to
1279    // `~/.local/share/services/<svc>/service.manifest` so `ryra diff` and
1280    // `ryra upgrade` can detect drift between the registry and what's
1281    // actually on disk. `.env` is excluded because it carries generated
1282    // secrets that legitimately rotate at runtime; the manifest itself is
1283    // excluded to avoid the chicken-and-egg of hashing itself. CopyFile
1284    // sources (binary plugin payloads) are not yet covered — drift on
1285    // those is rare and adds I/O at plan time. Revisit if it bites.
1286    let manifest_path_for_svc = manifest::manifest_path(service_name)?;
1287    let env_filename = std::ffi::OsStr::new(".env");
1288    let mut manifest_entries: Vec<manifest::ManifestEntry> = Vec::new();
1289    for step in &steps {
1290        if let Step::WriteFile(file) = step {
1291            if file.path == manifest_path_for_svc {
1292                continue;
1293            }
1294            if file.path.file_name() == Some(env_filename) {
1295                continue;
1296            }
1297            manifest_entries.push(manifest::ManifestEntry {
1298                path: file.path.clone(),
1299                sha256: manifest::hash_bytes(file.content.as_bytes()),
1300            });
1301        }
1302    }
1303    // Static env vars — every registry-defined env whose template carries
1304    // no `{{secret.*}}` or `{{auth.*}}` reference. Tracked so `ryra
1305    // upgrade` can append registry-added env vars to the user's existing
1306    // `.env` without re-rendering it (which would clobber rotated
1307    // secrets). Append-only by design. The richer `tracked_envs` is what
1308    // upgrade uses to decide whether to prompt the user; the on-disk
1309    // manifest only records key+value (the `# env: KEY=VAL` lines).
1310    let tracked_envs = collect_static_envs(
1311        &reg_service.def,
1312        &output.ctx,
1313        enabled_groups,
1314        selected_choices,
1315    )?;
1316    let manifest_envs: Vec<manifest::EnvEntry> = tracked_envs
1317        .iter()
1318        .map(|t| manifest::EnvEntry {
1319            key: t.key.clone(),
1320            value: t.value.clone(),
1321        })
1322        .collect();
1323    steps.push(Step::WriteFile(GeneratedFile {
1324        path: manifest_path_for_svc,
1325        content: manifest::format(&manifest_entries, &manifest_envs),
1326    }));
1327
1328    // 10. Reload and start via systemd
1329    steps.push(Step::DaemonReload);
1330    // Start — dependencies start automatically via Requires=/After= in the quadlet.
1331    // Blue/green brings up only the active (blue) slot; the green slot's quadlet
1332    // is installed but stays idle until the first `ryra upgrade` rolls onto it.
1333    let start_unit = if blue_green {
1334        deploy::color_unit(service_name, registry::service_def::Color::Blue)
1335    } else {
1336        service_name.to_string()
1337    };
1338    steps.push(Step::StartService { unit: start_unit });
1339
1340    // Collect post-install info
1341    let allocated_ports: Vec<(String, u16)> = resolved_ports.clone();
1342
1343    // Secret names from env var templates (not stored in state)
1344    let mut generated_secrets: Vec<String> = reg_service
1345        .def
1346        .env
1347        .iter()
1348        .filter(|e| !env_overrides.contains_key(&e.name))
1349        .flat_map(|e| generate::extract_secret_refs(&e.value))
1350        .collect();
1351    // Deduplicate — the same secret may be referenced by multiple env vars
1352    generated_secrets.sort();
1353    generated_secrets.dedup();
1354
1355    Ok(AddResult {
1356        steps,
1357        warnings,
1358        repo_url: registry_name.to_string(),
1359        allocated_ports,
1360        generated_secrets,
1361        env_content,
1362        url: url.map(|u| u.to_string()),
1363        tracked_envs,
1364    })
1365}
1366
1367/// Secret names referenced by a service's env templates (for the install
1368/// summary; values live in `.env`, not state). Shared by add paths.
1369fn collect_generated_secrets(
1370    def: &registry::service_def::ServiceDef,
1371    env_overrides: &BTreeMap<String, String>,
1372) -> Vec<String> {
1373    let mut out: Vec<String> = def
1374        .env
1375        .iter()
1376        .filter(|e| !env_overrides.contains_key(&e.name))
1377        .flat_map(|e| generate::extract_secret_refs(&e.value))
1378        .collect();
1379    out.sort();
1380    out.dedup();
1381    out
1382}
1383
1384/// Inputs for [`build_native_add`] — grouped to keep the signature sane.
1385struct NativeAddParams<'a> {
1386    service_name: &'a str,
1387    reg_service: &'a registry::RegistryService,
1388    home_dir: &'a Path,
1389    output: generate::EnvOutput,
1390    install_metadata: &'a Metadata,
1391    registry_name: &'a str,
1392    url: Option<&'a str>,
1393    tracked_envs: Vec<TrackedEnv>,
1394    allocated_ports: Vec<(String, u16)>,
1395    generated_secrets: Vec<String>,
1396    /// Quadlet filenames excluded by the choice selection (see
1397    /// [`excluded_quadlets`]). A native service may ship a `quadlets/` dir of
1398    /// auxiliary containers (e.g. a bundled postgres) alongside its binary.
1399    excluded_quadlets: Vec<String>,
1400    /// Caddy route steps (site-block write + reload) for a native service with
1401    /// a routed URL, pre-built by [`caddy_route_steps`] in `add_service`.
1402    /// Appended after the unit starts. Empty for loopback/tailscale exposures.
1403    caddy_steps: Vec<Step>,
1404    /// Warnings surfaced for this native install (e.g. `UrlWithoutReverseProxy`
1405    /// when a URL was given but no Caddy is installed).
1406    warnings: Vec<Warning>,
1407}
1408
1409/// Quadlet filenames to skip for an install: those claimed by a
1410/// `[[choice.option]]` whose option is not selected (falling back to the
1411/// choice's `default`). A quadlet claimed by no option always installs.
1412fn excluded_quadlets(
1413    def: &registry::service_def::ServiceDef,
1414    selected_choices: &BTreeMap<String, String>,
1415) -> Vec<String> {
1416    let mut all_claimed: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
1417    let mut selected: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
1418    for choice in &def.choices {
1419        let picked = selected_choices
1420            .get(&choice.name)
1421            .unwrap_or(&choice.default);
1422        for option in &choice.options {
1423            for q in &option.quadlets {
1424                all_claimed.insert(q.clone());
1425                if &option.name == picked {
1426                    selected.insert(q.clone());
1427                }
1428            }
1429        }
1430    }
1431    all_claimed.difference(&selected).cloned().collect()
1432}
1433
1434/// Plan a `runtime = "native"` install: build the binary (unless prebuilt),
1435/// install it, write the service `.env`, render a plain `systemd --user` unit
1436/// and link it, then start. No image, no quadlet — but the same `.env`
1437/// contract (`SERVICE_PORT_HTTP`, etc.) and the same `service_home` the rest of
1438/// ryra (Caddy routing, whole-folder backups) already understands.
1439fn build_native_add(p: NativeAddParams<'_>) -> Result<AddResult> {
1440    let NativeAddParams {
1441        service_name,
1442        reg_service,
1443        home_dir,
1444        output,
1445        install_metadata,
1446        registry_name,
1447        url,
1448        tracked_envs,
1449        allocated_ports,
1450        generated_secrets,
1451        excluded_quadlets,
1452        caddy_steps,
1453        warnings,
1454    } = p;
1455
1456    let run = reg_service.def.service.run.as_ref().ok_or_else(|| {
1457        Error::Bundle(format!(
1458            "native service '{service_name}' is missing its `run` command"
1459        ))
1460    })?;
1461    let build = reg_service.def.service.build.as_ref();
1462
1463    let env_content = output.env_file.content.clone();
1464    let source_dir = reg_service.service_dir.clone();
1465    let mut steps = Vec::new();
1466
1467    // The service home holds STATE only: data/, .env, the unit, the install
1468    // record. The service itself runs from its source dir (no binary copy), so
1469    // a plain `target/release/app`, `bun run src/index.ts`, or `cargo watch`
1470    // all work the same and a rebuild lands where the unit already looks.
1471    steps.push(Step::CreateDir(home_dir.to_path_buf()));
1472    steps.push(Step::CreateDir(home_dir.join("data")));
1473
1474    let blue_green =
1475        reg_service.def.service.deploy == registry::service_def::DeployStrategy::BlueGreen;
1476
1477    // Install record + the generated .env (carries the SERVICE_PORT_* vars,
1478    // including the blue/green color pair when present).
1479    steps.push(Step::WriteFile(GeneratedFile {
1480        path: metadata_path(service_name)?,
1481        content: toml::to_string_pretty(install_metadata)?,
1482    }));
1483    steps.push(Step::WriteFile(output.env_file));
1484
1485    let description = reg_service.def.service.description.as_str();
1486    if blue_green {
1487        // Each color slot is its own working copy of the source, built
1488        // independently, so the live slot's files are never mutated by an
1489        // idle-slot rebuild — the isolation that makes blue/green safe for
1490        // interpreted runtimes (Python/Node), not only compiled ones. Both
1491        // slots are created at install; only blue is started (below).
1492        let primary = allocated_ports
1493            .iter()
1494            .find(|(n, _)| n.eq_ignore_ascii_case("http"))
1495            .or_else(|| allocated_ports.first())
1496            .map(|(n, _)| n.clone())
1497            .ok_or_else(|| {
1498                Error::Bundle(format!(
1499                    "blue/green native '{service_name}' has no port to route"
1500                ))
1501            })?;
1502        let port_var = format!("SERVICE_PORT_{}", primary.to_uppercase());
1503        let home_str = home_dir.to_string_lossy().into_owned();
1504        for color in [
1505            registry::service_def::Color::Blue,
1506            registry::service_def::Color::Green,
1507        ] {
1508            let slot = home_dir.join("colors").join(color.as_str());
1509            let slot_str = slot.to_string_lossy().into_owned();
1510            let port = allocated_ports
1511                .iter()
1512                .find(|(n, _)| *n == format!("{}_{}", primary.to_ascii_lowercase(), color))
1513                .map(|(_, p)| *p)
1514                .ok_or_else(|| {
1515                    Error::Bundle(format!(
1516                        "blue/green native '{service_name}' missing the {color} port"
1517                    ))
1518                })?;
1519            // Populate + build the slot from the source tree.
1520            steps.push(Step::SyncDir {
1521                src: source_dir.clone(),
1522                dst: slot.clone(),
1523            });
1524            if let Some(command) = build {
1525                steps.push(Step::Build {
1526                    dir: slot.clone(),
1527                    command: command.clone(),
1528                });
1529            }
1530            let unit_name = format!("{}.service", deploy::color_unit(service_name, color));
1531            let unit_path = home_dir.join(&unit_name);
1532            steps.push(Step::WriteFile(GeneratedFile {
1533                path: unit_path.clone(),
1534                content: deploy::native_color_unit(&deploy::NativeColorUnit {
1535                    description,
1536                    color,
1537                    workdir: &slot_str,
1538                    home: &home_str,
1539                    port_var: &port_var,
1540                    port,
1541                    run,
1542                }),
1543            }));
1544            steps.push(Step::Symlink {
1545                link: systemd_user_dir()?.join(&unit_name),
1546                target: unit_path,
1547            });
1548        }
1549    } else {
1550        // Optional build/prepare step (cargo build, bun install) in the source dir.
1551        if let Some(command) = build {
1552            steps.push(Step::Build {
1553                dir: source_dir.clone(),
1554                command: command.clone(),
1555            });
1556        }
1557        // The unit: real file in the service home, symlinked into the systemd
1558        // --user dir so the unit is found on daemon-reload (mirrors quadlets).
1559        let unit_name = format!("{service_name}.service");
1560        let unit_path = home_dir.join(&unit_name);
1561        steps.push(Step::WriteFile(GeneratedFile {
1562            path: unit_path.clone(),
1563            content: native_unit(home_dir, &source_dir, run, description),
1564        }));
1565        steps.push(Step::Symlink {
1566            link: systemd_user_dir()?.join(&unit_name),
1567            target: unit_path,
1568        });
1569    }
1570
1571    // A native service may ship auxiliary container quadlets (e.g. a bundled
1572    // postgres) in its `quadlets/` dir, reached over a published loopback port.
1573    // Process them exactly like the podman path (pull images, write + symlink
1574    // each unit, create bind-mount dirs), gated by the choice selection, then
1575    // start each one before the native app (which Restart=always retries until
1576    // its DB is up). Skipped when there's no `quadlets/` dir.
1577    let mut quadlet_units: Vec<String> = Vec::new();
1578    if source_dir.join("quadlets").is_dir() {
1579        // Includes choice-gated ports (e.g. the bundled DB's allocated port),
1580        // so a quadlet's `${SERVICE_PORT_DB}` validates.
1581        let port_names: Vec<String> = allocated_ports.iter().map(|(n, _)| n.clone()).collect();
1582        let bundle =
1583            generate::bundle::process_quadlet_bundle(&generate::bundle::ProcessBundleParams {
1584                service_dir: &source_dir,
1585                service_name,
1586                extra_networks: &[],
1587                extra_volumes: &[],
1588                podman_args: &[],
1589                extra_exec_start_pre: &[],
1590                port_names: &port_names,
1591                excluded_quadlets: &excluded_quadlets,
1592            })?;
1593        for image in &bundle.images {
1594            steps.push(Step::PullImage {
1595                image: image.clone(),
1596            });
1597        }
1598        for dir in &bundle.bind_mount_dirs {
1599            steps.push(Step::CreateDir(dir.clone()));
1600        }
1601        let quadlet_path = quadlet_dir()?;
1602        for file in bundle.quadlet_files {
1603            let fname = file
1604                .path
1605                .file_name()
1606                .ok_or_else(|| {
1607                    Error::Bundle(format!("invalid quadlet path: {}", file.path.display()))
1608                })?
1609                .to_os_string();
1610            if let Some(stem) = fname.to_string_lossy().strip_suffix(".container") {
1611                quadlet_units.push(stem.to_string());
1612            }
1613            let link = quadlet_path.join(&fname);
1614            let target = file.path.clone();
1615            steps.push(Step::WriteFile(file));
1616            steps.push(Step::Symlink { link, target });
1617        }
1618    }
1619
1620    steps.push(Step::DaemonReload);
1621    // Auxiliary containers first, so the DB is up (or coming up) before the app.
1622    for unit in &quadlet_units {
1623        steps.push(Step::StartService { unit: unit.clone() });
1624    }
1625    // Blue/green brings up only the active (blue) slot; green is installed but
1626    // idle until the first `ryra upgrade` rolls onto it.
1627    let app_unit = if blue_green {
1628        deploy::color_unit(service_name, registry::service_def::Color::Blue)
1629    } else {
1630        service_name.to_string()
1631    };
1632    steps.push(Step::StartService { unit: app_unit });
1633
1634    // Caddy route (if the service is URL-exposed and Caddy is installed),
1635    // pre-built in add_service. After the slot is up so the upstream exists.
1636    steps.extend(caddy_steps);
1637
1638    Ok(AddResult {
1639        steps,
1640        warnings,
1641        repo_url: registry_name.to_string(),
1642        allocated_ports,
1643        generated_secrets,
1644        env_content,
1645        url: url.map(|u| u.to_string()),
1646        tracked_envs,
1647    })
1648}
1649
1650/// Render a plain `systemd --user` unit for a native service. `EnvironmentFile`
1651/// supplies the service `.env` (so `SERVICE_PORT_HTTP` and friends are present);
1652/// `SERVICE_HOME` points the process at its data dir, matching the contract a
1653/// container service gets via the quadlet.
1654fn native_unit(home_dir: &Path, source_dir: &Path, run: &str, description: &str) -> String {
1655    let home = home_dir.display();
1656    let source = source_dir.display();
1657    // ExecStart via `sh -c 'exec <run>'` so a binary path (`target/release/app`),
1658    // an interpreter command (`bun run src/index.ts`), and a watcher
1659    // (`cargo watch -x run`) all work the same. `exec` replaces the shell so
1660    // systemd tracks the real PID and stop/restart reach the process.
1661    //
1662    // A user-level unit's PATH is minimal, so toolchains installed under $HOME
1663    // (bun, cargo, deno, go, pipx) wouldn't be found. Prepend the common ones
1664    // (%h = the user's home) so `run = "bun ..."` / `"cargo ..."` just work.
1665    format!(
1666        "[Unit]\n\
1667         Description={description}\n\
1668         After=network.target\n\
1669         \n\
1670         [Service]\n\
1671         Type=simple\n\
1672         WorkingDirectory={source}\n\
1673         EnvironmentFile={home}/.env\n\
1674         Environment=SERVICE_HOME={home}\n\
1675         Environment=PATH=%h/.local/bin:%h/.cargo/bin:%h/.bun/bin:%h/.deno/bin:%h/go/bin:/usr/local/bin:/usr/bin:/bin\n\
1676         ExecStart=/bin/sh -c 'exec {run}'\n\
1677         Restart=always\n\
1678         RestartSec=5\n\
1679         \n\
1680         [Install]\n\
1681         WantedBy=default.target\n",
1682    )
1683}
1684
1685/// Check if a quadlet filename belongs to a service.
1686///
1687/// Matches `{service_name}.container`, `{service_name}-db.volume`, etc.
1688/// but NOT `{service_name_prefix}-other.container` (e.g., "foo" must not
1689/// match "foo-bar.container" when "foo-bar" is a known service).
1690///
1691/// `all_service_names` contains every installed service name — used to detect
1692/// when a longer service name owns the file instead.
1693pub fn quadlet_belongs_to(filename: &str, service_name: &str, all_service_names: &[&str]) -> bool {
1694    if !filename.starts_with(service_name) {
1695        return false;
1696    }
1697    let rest = &filename[service_name.len()..];
1698    if rest.starts_with('.') {
1699        return true;
1700    }
1701    if !rest.starts_with('-') {
1702        return false;
1703    }
1704    // Check that no other installed service is a longer prefix match.
1705    // e.g., "foo-bar.container" with service "foo" — if "foo-bar"
1706    // is also installed, it owns this file.
1707    !all_service_names.iter().any(|&other| {
1708        other.len() > service_name.len()
1709            && other.starts_with(service_name)
1710            && filename.starts_with(other)
1711            && filename[other.len()..].starts_with(['.', '-'])
1712    })
1713}
1714
1715/// How destructive `remove_service` should be.
1716#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
1717#[serde(rename_all = "snake_case")]
1718pub enum RemoveMode {
1719    #[default]
1720    /// Stop + remove quadlets + delete ephemeral config files, but keep
1721    /// the data subdirs under the service home dir and keep all podman
1722    /// named volumes. After this, `ryra data ls` reports the service as
1723    /// `Orphan`.
1724    Preserve,
1725    /// Stop + remove everything: quadlets, entire home dir, named volumes.
1726    Purge,
1727}
1728
1729/// Remove a service: update state, return cleanup steps.
1730pub fn remove_service(service_name: &str, mode: RemoveMode) -> Result<RemoveResult> {
1731    // Reconstruct the InstalledService view from the quadlet's
1732    // `# Service-*` headers — that's the source of truth now.
1733    let installed_owned = build_installed_from_metadata(service_name)
1734        .ok_or_else(|| Error::ServiceNotInstalled(service_name.to_string()))?;
1735    let installed = &installed_owned;
1736
1737    // Native services have no quadlets / podman objects: tear down the
1738    // systemd --user unit and (on purge) the home dir. Runtime comes from the
1739    // install record, so this works without the registry. Mirrors the native
1740    // add path's early return.
1741    if let Ok(Some(meta)) = metadata::load_metadata(service_name)
1742        && meta.runtime == registry::service_def::Runtime::Native
1743    {
1744        let url = installed.exposure.url().map(|s| s.to_string());
1745        return remove_native_service(service_name, mode, url);
1746    }
1747
1748    // Stop all units belonging to this service (main + sidecars).
1749    // Quadlet files named {service_name}.ext or {service_name}-sidecar.ext.
1750    let quadlet_path = quadlet_dir()?;
1751    let mut steps = Vec::new();
1752    let mut volume_names = Vec::new();
1753    let mut networks: Vec<String> = Vec::new();
1754    let mut has_named_volumes = false;
1755    // Quadlet directory scan is authoritative — captures every
1756    // ryra-managed service so the "is foo-bar a sibling service?"
1757    // prefix check (used to scope file removal) sees every install.
1758    let name_pool = scan_managed_services().unwrap_or_default();
1759    let all_names: Vec<&str> = name_pool.iter().map(|s| s.as_str()).collect();
1760
1761    // Disable the Tailscale Service before tearing the host port down.
1762    // Always emit when the service was tailscale-enabled — the API
1763    // delete is idempotent and `tailscale serve --service=svc:X off`
1764    // is fine to run on a service that's already cleared.
1765    //
1766    // svc_name comes from the stored exposure URL (the `<service>-<host>`
1767    // first label) — pulling it from the URL captured at install time
1768    // means a hostname change post-install doesn't break teardown. If
1769    // the URL is malformed, skip the step rather than blocking the
1770    // whole removal — a stale tailnet entry is a smaller harm than a
1771    // service that won't uninstall.
1772    if let Some(svc_name) = installed.exposure.tailscale_svc_name() {
1773        steps.push(Step::TailscaleDisable { svc_name });
1774    }
1775
1776    if quadlet_path.is_dir()
1777        && let Ok(entries) = std::fs::read_dir(&quadlet_path)
1778    {
1779        for entry in entries.flatten() {
1780            let file_name = entry.file_name();
1781            let name = file_name.to_string_lossy();
1782            // Catches both the service's own quadlets (foo.container,
1783            // foo-db.container, …) and its `ts-foo*` tailscale sidecar.
1784            if !quadlet_belongs_to(&name, service_name, &all_names) {
1785                continue;
1786            }
1787            // Stop each .container unit before removing files
1788            if name.ends_with(".container") {
1789                let unit = name.trim_end_matches(".container").to_string();
1790                steps.push(Step::StopService { unit });
1791            }
1792            if name.ends_with(".network") {
1793                // Stop the generated `<net>-network` oneshot, and remember the
1794                // network so it can be dropped once every container is down.
1795                let net = name.trim_end_matches(".network").to_string();
1796                steps.push(Step::StopService {
1797                    unit: format!("{net}-network"),
1798                });
1799                networks.push(net);
1800            }
1801            if name.ends_with(".volume") {
1802                has_named_volumes = true;
1803                if matches!(mode, RemoveMode::Purge) {
1804                    let vol = name.trim_end_matches(".volume").to_string();
1805                    // Quadlet prefixes volume names with "systemd-"
1806                    volume_names.push(format!("systemd-{vol}"));
1807                }
1808            }
1809            steps.push(Step::RemoveFile(entry.path()));
1810        }
1811    }
1812
1813    // Clean up ryra-managed Caddy site block + OIDC client registration
1814    // BEFORE the daemon reload, so the routing layers drop their stale
1815    // pointers while the doomed containers are already stopped.
1816    // Caddy-routed exposures (Internal / Public) had a `# Service-Source: registry/<svc>`
1817    // block written into the Caddyfile on add; remove it now. Loopback
1818    // and Tailscale never had one (no Caddy involvement), so skip.
1819    let had_caddy_route = matches!(
1820        installed.exposure,
1821        Exposure::Internal { .. } | Exposure::Public { .. }
1822    );
1823    if !WellKnownService::Caddy.matches(service_name) && had_caddy_route {
1824        let caddyfile_path = caddy::caddyfile_path()?;
1825        if caddyfile_path.exists() {
1826            let existing =
1827                std::fs::read_to_string(&caddyfile_path).map_err(|source| Error::FileRead {
1828                    path: caddyfile_path.clone(),
1829                    source,
1830                })?;
1831            let updated = caddy::remove_route(&existing, service_name);
1832            if updated != existing {
1833                steps.push(Step::WriteFile(GeneratedFile {
1834                    path: caddyfile_path,
1835                    content: updated.clone(),
1836                }));
1837                // Skip reload if the Caddyfile is now empty — Caddy rejects
1838                // empty configs and will fail the reload.
1839                if !updated.trim().is_empty() {
1840                    steps.push(Step::ReloadCaddy);
1841                }
1842            }
1843        }
1844    }
1845
1846    if !WellKnownService::Authelia.matches(service_name)
1847        && matches!(
1848            installed.auth_kind,
1849            Some(registry::service_def::AuthKind::Oidc)
1850        )
1851    {
1852        steps.extend(authelia::unregister_oidc_client(service_name)?);
1853    }
1854
1855    // Metrics cleanup. Drop this service's scrape target from every
1856    // installed store (file_sd notices the removal, no reload). If the
1857    // service being removed IS a store, also drop the datasources it
1858    // provisioned on dashboards and restart them so the dead datasource
1859    // disappears from their UI.
1860    let installed_all = list_installed().unwrap_or_default();
1861    for store in installed_all
1862        .iter()
1863        .filter(|s| installed_provides(s, Capability::MetricsStore))
1864    {
1865        if store.name != service_name
1866            && let Ok(target) = metrics_bridge::target_file_path(&store.name, service_name)
1867            && target.exists()
1868        {
1869            steps.push(Step::RemoveFile(target));
1870        }
1871    }
1872    if installed.provides.contains(&Capability::MetricsStore) {
1873        for dash in installed_all
1874            .iter()
1875            .filter(|s| installed_provides(s, Capability::MetricsDashboard))
1876        {
1877            if dash.name == service_name {
1878                continue;
1879            }
1880            if let Ok(ds) = metrics_bridge::datasource_file_path(&dash.name, service_name)
1881                && ds.exists()
1882            {
1883                steps.push(Step::RemoveFile(ds));
1884                steps.push(Step::RestartService {
1885                    unit: dash.name.clone(),
1886                });
1887            }
1888        }
1889    }
1890
1891    // Reload systemd after removing quadlet files
1892    steps.push(Step::DaemonReload);
1893
1894    // Drop the service's podman networks now that all its containers are
1895    // stopped and the network units unloaded. `ryra remove` previously left
1896    // these behind — it deleted the `.network` file but never the network
1897    // itself — and the leak broke the next install: the regenerated network
1898    // unit's `podman network create` hit the still-present network and failed.
1899    // Best-effort — a network still used by another service is correctly
1900    // skipped (the rm fails and is ignored by the executor).
1901    for net in networks {
1902        steps.push(Step::RemoveNetwork { name: net });
1903    }
1904
1905    match mode {
1906        RemoveMode::Purge => {
1907            // Remove podman volumes after containers and units are gone
1908            for vol_name in volume_names {
1909                steps.push(Step::RemoveVolume { name: vol_name });
1910            }
1911            // Wipe entire service data directory
1912            steps.push(Step::RemoveDir(service_home(service_name)?));
1913        }
1914        RemoveMode::Preserve => {
1915            // Keep volumes intact — volume_names is guaranteed empty here
1916            // because accumulation is gated on Purge mode above.
1917            // Remove only ephemeral children of the home dir; keep data.
1918            let home = service_home(service_name)?;
1919            let (data, ephemeral) = crate::data::classify::classify_home_dir(&home)?;
1920            for path in ephemeral {
1921                match std::fs::metadata(&path) {
1922                    Ok(m) if m.is_dir() => steps.push(Step::RemoveDir(path)),
1923                    Ok(_) => steps.push(Step::RemoveFile(path)),
1924                    // Path vanished between scan and step emission.
1925                    // `rm -f` is a no-op on a missing path; keeping the step ensures a
1926                    // retry of the same plan is idempotent.
1927                    Err(_) => steps.push(Step::RemoveFile(path)),
1928                }
1929            }
1930            // If the service has no bind-mounted data *and* no podman
1931            // named volumes, preserve-mode has literally nothing to
1932            // preserve — the home dir would just be an empty ghost.
1933            // Drop it in that case. When volumes exist (twenty,
1934            // postgres, …) we keep the home dir so owner inference in
1935            // enumerate_all can still attribute the volumes back to
1936            // this service; `ryra list` then reports a real orphan.
1937            if data.is_empty() && !has_named_volumes && home.exists() {
1938                steps.push(Step::RemoveDir(home));
1939            }
1940        }
1941    }
1942
1943    let url = installed.exposure.url().map(|s| s.to_string());
1944
1945    Ok(RemoveResult {
1946        steps,
1947        service_name: service_name.to_string(),
1948        url,
1949    })
1950}
1951
1952/// Tear down a `runtime = "native"` install: stop its `systemd --user` unit,
1953/// drop the unit symlink, reload, and remove either the whole home (purge) or
1954/// just the rebuildable/ephemeral bits (preserve keeps `data/` + the install
1955/// record). The dual of [`build_native_add`].
1956fn remove_native_service(
1957    service_name: &str,
1958    mode: RemoveMode,
1959    url: Option<String>,
1960) -> Result<RemoveResult> {
1961    let home = service_home(service_name)?;
1962    // A restart-strategy install has one `<svc>.service`; a blue/green install
1963    // has `<svc>-blue.service` + `<svc>-green.service` and no bare unit. Tear
1964    // down whichever actually exist so neither shape leaks a unit.
1965    let unit_dir = systemd_user_dir()?;
1966    let unit_names: Vec<String> = [
1967        format!("{service_name}.service"),
1968        format!("{service_name}-blue.service"),
1969        format!("{service_name}-green.service"),
1970    ]
1971    .into_iter()
1972    .filter(|u| unit_dir.join(u).exists())
1973    .collect();
1974    let mut steps = Vec::new();
1975
1976    // Tear down any auxiliary container quadlets the native service shipped
1977    // (e.g. a bundled postgres): stop each unit and drop its systemd symlink.
1978    // The real `.container` files live under the service home and go with it
1979    // (purge) or are dropped below (preserve). Their data (bind-mounted under
1980    // the home, e.g. db-data/) is kept on preserve, removed on purge with home.
1981    let mut aux_container_files: Vec<String> = Vec::new();
1982    if let Ok(qdir) = quadlet_dir() {
1983        let names = scan_managed_services().unwrap_or_default();
1984        let all: Vec<&str> = names.iter().map(|s| s.as_str()).collect();
1985        if let Ok(entries) = std::fs::read_dir(&qdir) {
1986            for entry in entries.flatten() {
1987                let fname = entry.file_name().to_string_lossy().into_owned();
1988                if let Some(stem) = fname.strip_suffix(".container")
1989                    && quadlet_belongs_to(&fname, service_name, &all)
1990                {
1991                    steps.push(Step::StopService {
1992                        unit: stem.to_string(),
1993                    });
1994                    steps.push(Step::RemoveFile(qdir.join(&fname)));
1995                    aux_container_files.push(fname);
1996                }
1997            }
1998        }
1999    }
2000
2001    for unit_name in &unit_names {
2002        steps.push(Step::StopService {
2003            unit: unit_name.trim_end_matches(".service").to_string(),
2004        });
2005        steps.push(Step::RemoveFile(unit_dir.join(unit_name)));
2006    }
2007    steps.push(Step::DaemonReload);
2008
2009    match mode {
2010        RemoveMode::Purge => steps.push(Step::RemoveDir(home)),
2011        RemoveMode::Preserve => {
2012            // Keep data/ and metadata.toml; drop the rebuildable binary, the
2013            // generated .env, the unit file(s), the blue/green color slots, and
2014            // the aux quadlet files (all re-created on re-add). Container data
2015            // dirs (db-data/) stay.
2016            let mut ephemeral: Vec<String> = vec!["bin".into(), ".env".into(), "colors".into()];
2017            ephemeral.extend(unit_names.iter().cloned());
2018            for child in &ephemeral {
2019                let p = home.join(child);
2020                match std::fs::metadata(&p) {
2021                    Ok(m) if m.is_dir() => steps.push(Step::RemoveDir(p)),
2022                    Ok(_) => steps.push(Step::RemoveFile(p)),
2023                    Err(_) => {} // not present (e.g. no colors/ for a restart install)
2024                }
2025            }
2026            for f in &aux_container_files {
2027                steps.push(Step::RemoveFile(home.join(f)));
2028            }
2029        }
2030    }
2031
2032    Ok(RemoveResult {
2033        steps,
2034        service_name: service_name.to_string(),
2035        url,
2036    })
2037}
2038
2039/// A lifecycle transition applied to an installed service's unit family.
2040#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
2041#[serde(rename_all = "snake_case")]
2042pub enum Lifecycle {
2043    Start,
2044    Stop,
2045}
2046
2047/// Plan a start/stop of an installed service's full unit family (main
2048/// container + sidecars). Errors with [`Error::ServiceNotInstalled`] if
2049/// the service isn't installed.
2050///
2051/// systemd cascades *start* through `Requires=`, but never cascades
2052/// *stop* — so every `.container` unit is named explicitly and the steps
2053/// are ordered to respect dependencies: the main app unit stops first
2054/// (before its db/cache sidecars) and starts last (after them).
2055pub fn lifecycle_steps(service_name: &str, action: Lifecycle) -> Result<Vec<Step>> {
2056    // Same validation + error surface as `remove_service`.
2057    build_installed_from_metadata(service_name)
2058        .ok_or_else(|| Error::ServiceNotInstalled(service_name.to_string()))?;
2059
2060    // Native services are a single systemd --user unit named after the service
2061    // (no sidecars / quadlets).
2062    if matches!(
2063        metadata::load_metadata(service_name),
2064        Ok(Some(m)) if m.runtime == registry::service_def::Runtime::Native
2065    ) {
2066        let unit = service_name.to_string();
2067        return Ok(vec![match action {
2068            Lifecycle::Start => Step::StartService { unit },
2069            Lifecycle::Stop => Step::StopService { unit },
2070        }]);
2071    }
2072
2073    let mut units = service_container_units(service_name)?;
2074    match action {
2075        // Main unit first → stops before the sidecars it depends on.
2076        Lifecycle::Stop => units.sort_by_key(|u| u != service_name),
2077        // Main unit last → starts after the sidecars it depends on.
2078        Lifecycle::Start => units.sort_by_key(|u| u == service_name),
2079    }
2080
2081    Ok(units
2082        .into_iter()
2083        .map(|unit| match action {
2084            Lifecycle::Start => Step::StartService { unit },
2085            Lifecycle::Stop => Step::StopService { unit },
2086        })
2087        .collect())
2088}
2089
2090/// systemd unit base names of every `.container` quadlet belonging to a
2091/// service (main container, sidecars, and the `ts-<svc>` tailscale
2092/// sidecar). Mirrors the family scan in [`remove_service`].
2093fn service_container_units(service_name: &str) -> Result<Vec<String>> {
2094    let quadlet_path = quadlet_dir()?;
2095    let name_pool = scan_managed_services().unwrap_or_default();
2096    let all_names: Vec<&str> = name_pool.iter().map(|s| s.as_str()).collect();
2097
2098    let mut units = Vec::new();
2099    if quadlet_path.is_dir()
2100        && let Ok(entries) = std::fs::read_dir(&quadlet_path)
2101    {
2102        for entry in entries.flatten() {
2103            let file_name = entry.file_name();
2104            let name = file_name.to_string_lossy();
2105            if !quadlet_belongs_to(&name, service_name, &all_names) {
2106                continue;
2107            }
2108            if name.ends_with(".container") {
2109                units.push(name.trim_end_matches(".container").to_string());
2110            }
2111        }
2112    }
2113    Ok(units)
2114}
2115
2116/// Parameters for [`record_pending`].
2117pub struct RecordPendingParams<'a> {
2118    pub service_name: &'a str,
2119    pub auth_kind: Option<registry::service_def::AuthKind>,
2120    pub registry_name: &'a str,
2121    pub allocated_ports: &'a [(String, u16)],
2122    pub repo_dir: &'a Path,
2123    /// How the service is exposed to clients. Replaces the previous
2124    /// `(url: Option<&str>, tailscale_enabled: bool)` pair so callers
2125    /// can't construct invalid combinations like a `*.ts.net` URL with
2126    /// `tailscale_enabled = false`. Decomposed into the legacy storage
2127    /// fields inside `record_pending` until the schema migrates to
2128    /// hold the typed enum directly.
2129    pub exposure: &'a Exposure,
2130}
2131
2132/// Record a service as pending installation (installed: false).
2133/// Called BEFORE executing steps so that partial failures are recoverable.
2134/// Persist install-time scaffolding to `preferences.toml`. This is now
2135/// the only side-effect — quadlet headers track the install itself,
2136/// preferences just remembers cross-cutting defaults so the next
2137/// `ryra add --auth` doesn't have to re-prompt for the OIDC issuer.
2138pub fn record_pending(params: RecordPendingParams<'_>) -> Result<()> {
2139    let paths = ConfigPaths::resolve()?;
2140    paths.ensure_dirs()?;
2141    let mut config = config::load_or_default(&paths.config_file)?;
2142
2143    // Auto-configure [auth] when an auth provider is installed so
2144    // future `ryra add <svc> --auth` calls know where to wire the
2145    // OIDC client. The `services` array is not touched — quadlet
2146    // headers are the source of truth for what's installed.
2147    if WellKnownService::Authelia.matches(params.service_name) {
2148        config.auth = Some(authelia::auth_config(
2149            params.allocated_ports,
2150            params.exposure.url(),
2151        )?);
2152        config::save_config(&paths.config_file, &config)?;
2153    }
2154
2155    Ok(())
2156}
2157
2158/// Drop the cached `[auth]` block when the auth provider is removed —
2159/// otherwise a later `ryra add <svc> --auth` thinks auth is still
2160/// configured and skips the auto-install path, then bombs out trying
2161/// to register an OIDC client against a non-existent authelia config.
2162/// The function name is preserved for caller compatibility; quadlet
2163/// removal is what actually finalises the install state.
2164pub fn finalize_remove(service_name: &str) -> Result<()> {
2165    let paths = ConfigPaths::resolve()?;
2166    let mut config = config::load_or_default(&paths.config_file)?;
2167
2168    if WellKnownService::Authelia.matches(service_name)
2169        && let Some(auth) = &config.auth
2170        && auth.provider_name() == "authelia"
2171    {
2172        config.auth = None;
2173        config::save_config(&paths.config_file, &config)?;
2174    }
2175
2176    Ok(())
2177}
2178
2179/// Steps to purge leftover data/volumes for an orphan service — one
2180/// with data on disk but no live install (e.g., after `ryra remove
2181/// <svc>` in default Preserve mode, or after a partial install where
2182/// the quadlets landed but `metadata.toml` never did). Unlike
2183/// `remove_service`, this doesn't require an install record to exist.
2184/// Templates whose rendered value is "sensitive" — either because the
2185/// value itself is a secret/credential, or because it rotates with each
2186/// install (so tracking it as static produces false drift positives).
2187/// Anything referencing one of these is excluded from the manifest.
2188///
2189/// Crucially this is *narrower* than "every {{auth.*}} reference":
2190/// `{{auth.url}}`, `{{auth.issuer}}`, `{{auth.provider}}`, `{{auth.internal_url}}`
2191/// are all stable per-install URLs/strings that the user benefits from
2192/// having tracked (so a global authelia URL change is caught by diff).
2193/// Only the credential pair `auth.client_id` + `auth.client_secret`
2194/// rotate per install. Same for SMTP: `smtp.host`/`smtp.port`/`smtp.from`/
2195/// `smtp.security` are tracked, only `smtp.username` and `smtp.password`
2196/// are excluded.
2197const SENSITIVE_TEMPLATE_REFS: &[&str] = &[
2198    "{{secret.",
2199    "{{auth.client_id",
2200    "{{auth.client_secret",
2201    "{{smtp.username",
2202    "{{smtp.password",
2203];
2204
2205fn is_static_template(value: &str) -> bool {
2206    !SENSITIVE_TEMPLATE_REFS.iter().any(|s| value.contains(s))
2207}
2208
2209/// Render every static env var the registry expects in `.env` for the
2210/// service. "Static" means the template carries no reference to any
2211/// rotating per-install value (see `SENSITIVE_TEMPLATE_REFS`).
2212///
2213/// Walks four sources, in the same order they're rendered into `.env` by
2214/// `generate::generate_env`:
2215///   1. `service_def.env` — top-level static entries.
2216///   2. Each enabled `[[env_group]]` — opt-in bundles.
2217///   3. `service_def.mappings.smtp` — only when SMTP is configured globally
2218///      and the service opts in (`integrations.smtp`).
2219///   4. `service_def.mappings.auth` — only when `--auth` was used.
2220///
2221/// Capturing 3 and 4 is what makes global-config drift visible: when the
2222/// user reconfigures global SMTP / re-installs authelia, the per-service
2223/// mapping values change, and tracking them lets `ryra diff` notice.
2224fn collect_static_envs(
2225    service_def: &registry::service_def::ServiceDef,
2226    ctx: &BTreeMap<String, String>,
2227    enabled_groups: &std::collections::BTreeSet<String>,
2228    selected_choices: &BTreeMap<String, String>,
2229) -> Result<Vec<plan::TrackedEnv>> {
2230    use registry::service_def::EnvKind;
2231    let mut out: Vec<plan::TrackedEnv> = Vec::new();
2232    let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
2233    let push = |name: &str,
2234                value_template: &str,
2235                kind: EnvKind,
2236                prompt: Option<String>,
2237                out: &mut Vec<plan::TrackedEnv>,
2238                seen: &mut std::collections::HashSet<String>|
2239     -> Result<()> {
2240        if !is_static_template(value_template) {
2241            return Ok(());
2242        }
2243        if !seen.insert(name.to_string()) {
2244            return Ok(());
2245        }
2246        let value = generate::template::render(value_template, ctx)?;
2247        out.push(plan::TrackedEnv {
2248            key: name.to_string(),
2249            value,
2250            kind,
2251            prompt,
2252        });
2253        Ok(())
2254    };
2255    for env in &service_def.env {
2256        push(
2257            &env.name,
2258            &env.value,
2259            env.kind.clone(),
2260            env.prompt.clone(),
2261            &mut out,
2262            &mut seen,
2263        )?;
2264    }
2265    for group in &service_def.env_groups {
2266        if !enabled_groups.contains(&group.name) {
2267            continue;
2268        }
2269        for env in &group.env {
2270            push(
2271                &env.name,
2272                &env.value,
2273                env.kind.clone(),
2274                env.prompt.clone(),
2275                &mut out,
2276                &mut seen,
2277            )?;
2278        }
2279    }
2280    // Selected option of each `[[choice]]`, falling back to the choice's
2281    // `default` when no selection is recorded, mirroring `render_env_vars`.
2282    for choice in &service_def.choices {
2283        let selected = selected_choices
2284            .get(&choice.name)
2285            .unwrap_or(&choice.default);
2286        let Some(option) = choice.options.iter().find(|o| &o.name == selected) else {
2287            continue;
2288        };
2289        for env in &option.env {
2290            push(
2291                &env.name,
2292                &env.value,
2293                env.kind.clone(),
2294                env.prompt.clone(),
2295                &mut out,
2296                &mut seen,
2297            )?;
2298        }
2299    }
2300    // Mirror the gating from `generate::render_env_vars`: SMTP mappings
2301    // only fire when smtp is configured globally; auth mappings only when
2302    // --auth was used. ctx-key presence is a faithful proxy for both.
2303    // Mapping-emitted env vars are always treated as Default (silent
2304    // append on upgrade) — there's no user-facing prompt label for them.
2305    if service_def.integrations.smtp && ctx.contains_key("smtp.host") {
2306        for (env_name, value_template) in &service_def.mappings.smtp {
2307            push(
2308                env_name,
2309                value_template,
2310                EnvKind::Default,
2311                None,
2312                &mut out,
2313                &mut seen,
2314            )?;
2315        }
2316    }
2317    if ctx.contains_key("auth.client_id") {
2318        for (env_name, value_template) in &service_def.mappings.auth {
2319            push(
2320                env_name,
2321                value_template,
2322                EnvKind::Default,
2323                None,
2324                &mut out,
2325                &mut seen,
2326            )?;
2327        }
2328    }
2329    Ok(out)
2330}
2331
2332pub fn orphan_purge_steps(svc: &data::ServiceData) -> Vec<Step> {
2333    let mut steps = Vec::new();
2334
2335    // Quadlet files in `~/.config/containers/systemd/` belonging to
2336    // this service. Mirrors `remove_service`'s sweep — filename match
2337    // via `quadlet_belongs_to` catches both regular files and
2338    // symlinks, so a re-`ryra add` after purge starts clean instead
2339    // of seeing a leftover `.volume` and re-prompting about orphan data.
2340    let mut had_quadlet = false;
2341    let mut networks: Vec<String> = Vec::new();
2342    if let Ok(qdir) = quadlet_dir()
2343        && qdir.is_dir()
2344        && let Ok(entries) = std::fs::read_dir(&qdir)
2345    {
2346        let name_pool = scan_managed_services().unwrap_or_default();
2347        let all_names: Vec<&str> = name_pool.iter().map(|s| s.as_str()).collect();
2348        for entry in entries.flatten() {
2349            let file_name = entry.file_name();
2350            let name = file_name.to_string_lossy();
2351            if !quadlet_belongs_to(&name, &svc.service, &all_names) {
2352                continue;
2353            }
2354            // Stop generated units before removing files so the
2355            // upcoming daemon-reload unloads them cleanly instead of
2356            // leaving "loaded: not-found, active (exited)" entries.
2357            if name.ends_with(".container") {
2358                let unit = name.trim_end_matches(".container").to_string();
2359                steps.push(Step::StopService { unit });
2360            } else if name.ends_with(".network") {
2361                let net = name.trim_end_matches(".network").to_string();
2362                steps.push(Step::StopService {
2363                    unit: format!("{net}-network"),
2364                });
2365                networks.push(net);
2366            } else if name.ends_with(".volume") {
2367                let unit = format!("{}-volume", name.trim_end_matches(".volume"));
2368                steps.push(Step::StopService { unit });
2369            }
2370            steps.push(Step::RemoveFile(entry.path()));
2371            had_quadlet = true;
2372        }
2373    }
2374    if had_quadlet {
2375        steps.push(Step::DaemonReload);
2376    }
2377    // Drop the podman networks once the containers are down (see remove_service).
2378    for net in networks {
2379        steps.push(Step::RemoveNetwork { name: net });
2380    }
2381
2382    for path in &svc.data_paths {
2383        if path.is_dir() {
2384            steps.push(Step::RemoveDir(path.clone()));
2385        } else {
2386            steps.push(Step::RemoveFile(path.clone()));
2387        }
2388    }
2389    if svc.home_dir.exists() {
2390        steps.push(Step::RemoveDir(svc.home_dir.clone()));
2391    }
2392    for v in &svc.volumes {
2393        steps.push(Step::RemoveVolume {
2394            name: v.name.clone(),
2395        });
2396    }
2397    steps
2398}
2399
2400/// Reset ryra: tear down all services, infrastructure, and config.
2401pub fn reset() -> Result<ResetResult> {
2402    let mut steps = Vec::new();
2403
2404    // Quadlet directory scan is the source of truth for ryra-managed
2405    // services — every install stamps a marker comment on the main
2406    // `.container`, so this catches every install regardless of the
2407    // state of `preferences.toml`.
2408    let managed_names = scan_managed_services().unwrap_or_default();
2409
2410    // 0. Disable every Tailscale Service before tearing services down.
2411    // `TailscaleDisable` stops `tailscale serve --service=svc:<X>` and
2412    // deletes the admin-side service definition via the API, so the
2413    // tailnet is clean after reset and the next install gets bare
2414    // hostnames. Read exposure from the quadlet headers so this still
2415    // works after the services array goes away.
2416    for svc in list_installed().unwrap_or_default() {
2417        if let Some(svc_name) = svc.exposure.tailscale_svc_name() {
2418            steps.push(Step::TailscaleDisable { svc_name });
2419        }
2420    }
2421
2422    // 1. Stop and remove only ryra-managed quadlet files (scoped by installed service names)
2423    let quadlet_path = quadlet_dir()?;
2424    let all_names: Vec<&str> = managed_names.iter().map(|s| s.as_str()).collect();
2425    let mut networks: Vec<String> = Vec::new();
2426    if quadlet_path.is_dir()
2427        && let Ok(entries) = std::fs::read_dir(&quadlet_path)
2428    {
2429        for entry in entries.flatten() {
2430            let file_name = entry.file_name();
2431            let name = file_name.to_string_lossy();
2432            // Only touch files belonging to a ryra-managed service —
2433            // including the `ts-<service>` tailscale sidecars when the
2434            // service was installed with --tailscale.
2435            let is_ryra_file = managed_names
2436                .iter()
2437                .any(|svc| quadlet_belongs_to(&name, svc, &all_names));
2438            if !is_ryra_file {
2439                continue;
2440            }
2441            if name.ends_with(".container") {
2442                let unit = name.trim_end_matches(".container").to_string();
2443                steps.push(Step::StopService { unit });
2444            }
2445            if name.ends_with(".network") {
2446                let net = name.trim_end_matches(".network").to_string();
2447                steps.push(Step::StopService {
2448                    unit: format!("{net}-network"),
2449                });
2450                networks.push(net);
2451            }
2452            if name.ends_with(".volume") {
2453                let vol = name.trim_end_matches(".volume").to_string();
2454                // Quadlet auto-generates `<vol>-volume.service` for each
2455                // `.volume` file. Stopping it before we remove the file
2456                // makes systemd unload the unit on the upcoming
2457                // daemon-reload — without this, leftover oneshot units
2458                // sit in "loaded: not-found, active (exited)" forever
2459                // until logout.
2460                steps.push(Step::StopService {
2461                    unit: format!("{vol}-volume"),
2462                });
2463            }
2464            steps.push(Step::RemoveFile(entry.path()));
2465        }
2466    }
2467
2468    // 1b. Native services keep their unit in the systemd --user dir, not the
2469    // quadlet dir, so the scan above misses them. Sweep ryra's native installs:
2470    // each home dir whose install record says `runtime = native` gets its unit
2471    // stopped and its `systemd/user` symlink removed (before the reload below
2472    // unloads it, and before step 4 wipes the data root).
2473    let user_unit_dir = systemd_user_dir()?;
2474    if let Ok(root) = service_data_root()
2475        && let Ok(entries) = std::fs::read_dir(&root)
2476    {
2477        for entry in entries.flatten() {
2478            let Some(name) = entry.file_name().to_str().map(str::to_string) else {
2479                continue;
2480            };
2481            if matches!(
2482                metadata::load_metadata(&name),
2483                Ok(Some(m)) if m.runtime == registry::service_def::Runtime::Native
2484            ) {
2485                steps.push(Step::StopService { unit: name.clone() });
2486                steps.push(Step::RemoveFile(
2487                    user_unit_dir.join(format!("{name}.service")),
2488                ));
2489            }
2490        }
2491    }
2492
2493    // 2. Reload user systemd after removing quadlets
2494    steps.push(Step::DaemonReload);
2495
2496    // 2b. Drop the podman networks now that every container is stopped and the
2497    // network units unloaded (see remove_service for why the leak matters).
2498    for net in networks {
2499        steps.push(Step::RemoveNetwork { name: net });
2500    }
2501
2502    // 3. Remove podman volumes for every ryra-visible service — installed
2503    // and orphaned. `enumerate_all` walks both the quadlet markers and the
2504    // data root, so volumes left behind by a `ryra remove --preserve`
2505    // (which drops the quadlet but keeps the named volume) get swept up
2506    // here too.
2507    let mut seen_volumes = std::collections::BTreeSet::new();
2508    for svc in data::enumerate_all().unwrap_or_default() {
2509        for vol in svc.volumes {
2510            if seen_volumes.insert(vol.name.clone()) {
2511                steps.push(Step::RemoveVolume { name: vol.name });
2512            }
2513        }
2514    }
2515
2516    // 4. Nuke the entire service data root in one shot. The user-facing
2517    // reset prompt promises "Delete ~/.local/share/services/", so the
2518    // implementation must match — sweeping managed dirs, orphan dirs
2519    // (left by `--preserve` removes), the top-level caddy-root-ca.crt,
2520    // and any other ryra-written tooling state living under that root.
2521    let data_root = service_data_root()?;
2522    if data_root.exists() {
2523        steps.push(Step::RemoveDir(data_root));
2524    }
2525
2526    Ok(ResetResult { steps })
2527}
2528
2529/// Called after reset steps succeed — removes ryra's config directory.
2530pub fn finalize_reset() -> Result<()> {
2531    let paths = ConfigPaths::resolve()?;
2532    if paths.config_dir.exists() {
2533        std::fs::remove_dir_all(&paths.config_dir).map_err(|source| Error::FileWrite {
2534            path: paths.config_dir,
2535            source,
2536        })?;
2537    }
2538    Ok(())
2539}
2540
2541/// Get the current status of the ryra installation. Considers ryra
2542/// "initialized" when EITHER a marker'd quadlet is on disk OR a
2543/// `preferences.toml` exists — quadlets are the source of truth for
2544/// installed services, but a preferences-only state (e.g. an SMTP relay
2545/// configured before any service install) still counts.
2546pub fn status() -> config::status::RyraStatus {
2547    let paths = match ConfigPaths::resolve() {
2548        Ok(p) => p,
2549        Err(_) => return config::status::RyraStatus::NotInitialized,
2550    };
2551
2552    let has_quadlets = scan_managed_services()
2553        .map(|n| !n.is_empty())
2554        .unwrap_or(false);
2555
2556    let config = match config::load_config(&paths.config_file) {
2557        Ok(c) => c,
2558        Err(Error::ConfigNotFound(_)) if has_quadlets => config::schema::Config::default(),
2559        Err(Error::ConfigNotFound(_)) => return config::status::RyraStatus::NotInitialized,
2560        Err(e) => return config::status::RyraStatus::Error(e.to_string()),
2561    };
2562
2563    config::status::RyraStatus::Initialized(config::status::StatusInfo::from_config(
2564        paths.config_file,
2565        &config,
2566    ))
2567}
2568
2569/// True if the named service is ryra-managed and *fully* installed —
2570/// the marker'd `.container` is present AND `metadata.toml` exists.
2571/// A partial install (quadlets written but the install plan errored
2572/// before metadata.toml landed) is treated as not-installed so that
2573/// `ryra remove <svc> --purge` routes through the orphan-cleanup path
2574/// instead of failing with "service is not installed". Same source of
2575/// truth as [`list_installed`].
2576pub fn is_service_installed(name: &str) -> bool {
2577    // The install record says whether (and how) a service is installed. Native
2578    // services have no quadlet — their presence is the systemd --user unit;
2579    // podman services are the marker'd quadlet. No metadata → not installed.
2580    let Ok(Some(meta)) = metadata::load_metadata(name) else {
2581        return false;
2582    };
2583    match meta.runtime {
2584        // Blue/green native installs have no bare `<name>.service` — they ship
2585        // two color units instead — so accept either form.
2586        registry::service_def::Runtime::Native => systemd_user_dir()
2587            .map(|d| {
2588                d.join(format!("{name}.service")).exists()
2589                    || d.join(format!("{name}-blue.service")).exists()
2590                    || d.join(format!("{name}-green.service")).exists()
2591            })
2592            .unwrap_or(false),
2593        registry::service_def::Runtime::Podman => scan_managed_services()
2594            .map(|names| names.iter().any(|n| n == name))
2595            .unwrap_or(false),
2596    }
2597}
2598
2599/// Scan the user's quadlet directory for ryra-managed services. A
2600/// `.container` file is considered ryra-managed iff it carries a
2601/// `# Service-Source: registry/<name>` comment within its first 16
2602/// lines (added at install time). Returns the deduplicated set of
2603/// service names found.
2604///
2605/// This makes the on-disk quadlet directory the source of truth for
2606/// "which services are installed" — `preferences.toml` was historically
2607/// authoritative, but could drift (e.g. if config was wiped while
2608/// services kept running). Callers that want richer metadata (URL,
2609/// exposure) still need `preferences.toml`; for "is X installed" the
2610/// quadlet scan is reliable.
2611pub fn scan_managed_services() -> Result<Vec<String>> {
2612    let dir = match quadlet_dir() {
2613        Ok(d) => d,
2614        Err(_) => return Ok(Vec::new()),
2615    };
2616    let entries = match std::fs::read_dir(&dir) {
2617        Ok(e) => e,
2618        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
2619        Err(source) => return Err(Error::FileRead { path: dir, source }),
2620    };
2621    let mut names: Vec<String> = Vec::new();
2622    for entry in entries.flatten() {
2623        let path = entry.path();
2624        if path.extension().and_then(|e| e.to_str()) != Some("container") {
2625            continue;
2626        }
2627        let Ok(content) = std::fs::read_to_string(&path) else {
2628            continue;
2629        };
2630        for line in content.lines().take(16) {
2631            if let Some(rest) = line.trim().strip_prefix("# Service-Source: registry/")
2632                && !rest.is_empty()
2633                && !names.iter().any(|n| n == rest)
2634            {
2635                names.push(rest.to_string());
2636                break;
2637            }
2638        }
2639    }
2640    names.sort();
2641    Ok(names)
2642}
2643
2644/// Build a full [`InstalledService`] from `metadata.toml` + `.env`.
2645/// Returns `None` if metadata.toml is missing — that's the signal that
2646/// either the service was never installed or it was installed by a
2647/// pre-metadata.toml ryra (in which case the caller should treat it as
2648/// not-installed and let the user reinstall).
2649fn build_installed_from_metadata(service_name: &str) -> Option<InstalledService> {
2650    let meta = load_metadata(service_name).ok().flatten()?;
2651
2652    // Loopback when no URL; otherwise classify by hostname suffix.
2653    let exposure = match meta.url.as_deref() {
2654        None => Exposure::Loopback,
2655        Some(u) => Exposure::from_url(u),
2656    };
2657
2658    let auth_kind = meta.auth.clone();
2659
2660    // Ports come from the `.env` file ryra writes alongside the quadlet
2661    // — `SERVICE_PORT_<NAME>=<value>` lines map back to the BTreeMap
2662    // keyed by lowercase name. Missing `.env` is treated as empty (still
2663    // a valid install — services without published ports legitimately
2664    // omit it).
2665    let ports = service_home(service_name)
2666        .ok()
2667        .and_then(|home| std::fs::read_to_string(home.join(".env")).ok())
2668        .map(|env| {
2669            env.lines()
2670                .filter_map(|l| {
2671                    let l = l.trim();
2672                    if l.is_empty() || l.starts_with('#') {
2673                        return None;
2674                    }
2675                    let (key, val) = l.split_once('=')?;
2676                    let name = key.strip_prefix("SERVICE_PORT_")?.to_lowercase();
2677                    let port = val
2678                        .trim_matches(|c: char| c == '"' || c == '\'')
2679                        .parse::<u16>()
2680                        .ok()?;
2681                    Some((name, port))
2682                })
2683                .collect::<std::collections::BTreeMap<String, u16>>()
2684        })
2685        .unwrap_or_default();
2686
2687    Some(InstalledService {
2688        name: service_name.to_string(),
2689        version: "0.1.0".to_string(),
2690        repo: meta.registry,
2691        ports,
2692        auth_kind,
2693        exposure,
2694        provides: meta.provides,
2695        installed: true,
2696    })
2697}
2698
2699/// List installed services. **Quadlet directory is the source of
2700/// truth** — every service whose main `.container` file carries our
2701/// marker is reconstructed from its on-disk headers + `.env`. The
2702/// preferences file is only consulted as a fallback for entries the
2703/// scan can't see (e.g. partially-rolled-out installs from older
2704/// ryra versions before metadata headers landed).
2705pub fn list_installed() -> Result<Vec<InstalledService>> {
2706    let mut names: std::collections::BTreeSet<String> = scan_managed_services()
2707        .unwrap_or_default()
2708        .into_iter()
2709        .collect();
2710    // Native services carry no quadlet marker. Pick them up from the data root:
2711    // any home dir that `is_service_installed` confirms (runtime-aware) and that
2712    // the quadlet scan didn't already catch.
2713    if let Ok(root) = service_data_root()
2714        && let Ok(entries) = std::fs::read_dir(&root)
2715    {
2716        for entry in entries.flatten() {
2717            if let Some(name) = entry.file_name().to_str()
2718                && !names.contains(name)
2719                && is_service_installed(name)
2720            {
2721                names.insert(name.to_string());
2722            }
2723        }
2724    }
2725    let out: Vec<InstalledService> = names
2726        .iter()
2727        .filter_map(|n| build_installed_from_metadata(n))
2728        .collect();
2729    Ok(out)
2730}
2731
2732/// Search available services in a repo, optionally filtered by query.
2733pub fn search_services(repo_dir: &Path, query: Option<&str>) -> Result<Vec<SearchResult>> {
2734    let available = registry::list_available(repo_dir)?;
2735
2736    let results = available
2737        .into_iter()
2738        .filter(|reg_svc| match query {
2739            None => true,
2740            Some(q) => {
2741                let q = q.to_lowercase();
2742                reg_svc.def.service.name.to_lowercase().contains(&q)
2743                    || reg_svc.def.service.description.to_lowercase().contains(&q)
2744            }
2745        })
2746        .map(|reg_svc| {
2747            let name = &reg_svc.def.service.name;
2748            let installed = is_service_installed(name);
2749            let mut supports = Vec::new();
2750            for kind in &reg_svc.def.integrations.auth {
2751                supports.push(kind.to_string());
2752            }
2753            if reg_svc.def.integrations.smtp {
2754                supports.push("smtp".to_string());
2755            }
2756            SearchResult {
2757                name: name.clone(),
2758                description: reg_svc.def.service.description,
2759                installed,
2760                supports,
2761            }
2762        })
2763        .collect();
2764
2765    Ok(results)
2766}
2767
2768pub struct SearchResult {
2769    pub name: String,
2770    pub description: String,
2771    pub installed: bool,
2772    /// Integrations this service supports (e.g., "oidc", "smtp").
2773    pub supports: Vec<String>,
2774}
2775
2776/// Get test definitions for an installed service by reading its `test.toml`.
2777pub async fn service_tests(service_name: &str) -> Result<ServiceTestInfo> {
2778    let installed = build_installed_from_metadata(service_name)
2779        .ok_or_else(|| Error::ServiceNotInstalled(service_name.to_string()))?;
2780
2781    let service_ref = service_ref_from_installed(&installed);
2782    let repo_dir = resolve_registry_dir(&service_ref).await?;
2783
2784    let test_toml_path = repo_dir.join(service_name).join("test.toml");
2785    let env_file = service_home(service_name)?.join(".env");
2786
2787    if !test_toml_path.exists() {
2788        return Ok(ServiceTestInfo {
2789            service_name: service_name.to_string(),
2790            registry_name: service_ref.registry_name().to_string(),
2791            tests: vec![],
2792            env_file,
2793        });
2794    }
2795
2796    let content = std::fs::read_to_string(&test_toml_path).map_err(|source| Error::FileRead {
2797        path: test_toml_path.clone(),
2798        source,
2799    })?;
2800
2801    #[derive(serde::Deserialize)]
2802    struct TestFile {
2803        #[serde(default)]
2804        tests: Vec<registry::test_def::TestDef>,
2805    }
2806
2807    let parsed: TestFile = toml::from_str(&content).map_err(|source| Error::TomlParse {
2808        path: test_toml_path,
2809        source,
2810    })?;
2811
2812    Ok(ServiceTestInfo {
2813        service_name: service_name.to_string(),
2814        registry_name: service_ref.registry_name().to_string(),
2815        tests: parsed.tests,
2816        env_file,
2817    })
2818}
2819
2820pub struct ServiceTestInfo {
2821    pub service_name: String,
2822    pub registry_name: String,
2823    pub tests: Vec<registry::test_def::TestDef>,
2824    pub env_file: PathBuf,
2825}
2826
2827/// Get detailed info about a service from a repo.
2828pub fn service_info(repo_dir: &Path, service_name: &str) -> Result<ServiceDetail> {
2829    let reg_service = registry::find_service(repo_dir, service_name)?;
2830    let def = &reg_service.def;
2831
2832    Ok(ServiceDetail {
2833        name: def.service.name.clone(),
2834        description: def.service.description.clone(),
2835        url: def.service.url.clone(),
2836        ports: def
2837            .ports
2838            .iter()
2839            .map(|p| (p.container_port, p.protocol.clone(), p.name.clone()))
2840            .collect(),
2841        env_vars: def
2842            .env
2843            .iter()
2844            .map(|e| (e.name.clone(), e.prompt.clone()))
2845            .collect(),
2846    })
2847}
2848
2849pub struct ServiceDetail {
2850    pub name: String,
2851    pub description: String,
2852    pub url: Option<String>,
2853    pub ports: Vec<(u16, registry::service_def::PortProtocol, String)>,
2854    pub env_vars: Vec<(String, Option<String>)>,
2855}
2856
2857#[cfg(test)]
2858mod tests {
2859    use super::*;
2860
2861    /// Lay out a minimal path-registry with one podman service and return its
2862    /// dir. `deploy_line` is spliced into `[service]` (e.g. `deploy =
2863    /// "blue-green"\nhealth_check = "/healthz"`), so one helper covers both the
2864    /// blue/green and the plain case.
2865    fn write_demo_registry(tmp: &std::path::Path, deploy_line: &str) {
2866        let svc_dir = tmp.join("demo");
2867        std::fs::create_dir_all(svc_dir.join("quadlets")).unwrap();
2868        std::fs::write(
2869            svc_dir.join("service.toml"),
2870            format!(
2871                "[service]\n\
2872                 name = \"demo\"\n\
2873                 description = \"demo\"\n\
2874                 runtime = \"podman\"\n\
2875                 {deploy_line}\n\
2876                 \n\
2877                 [[ports]]\n\
2878                 name = \"http\"\n\
2879                 container_port = 8080\n"
2880            ),
2881        )
2882        .unwrap();
2883        std::fs::write(
2884            svc_dir.join("quadlets").join("demo.container"),
2885            "[Container]\n\
2886             Image=docker.io/traefik/whoami:latest\n\
2887             ContainerName=demo\n\
2888             PublishPort=${SERVICE_PORT_HTTP}:8080\n\
2889             EnvironmentFile=%h/.local/share/services/demo/.env\n\
2890             \n\
2891             [Service]\n\
2892             EnvironmentFile=%h/.local/share/services/demo/.env\n\
2893             \n\
2894             [Install]\n\
2895             WantedBy=default.target\n",
2896        )
2897        .unwrap();
2898    }
2899
2900    /// Native blue/green registry: runtime = native with a build + run command,
2901    /// no quadlets — the SyncDir/Build path operates on the source dir directly.
2902    fn write_native_registry(tmp: &std::path::Path) {
2903        let svc_dir = tmp.join("napp");
2904        std::fs::create_dir_all(&svc_dir).unwrap();
2905        std::fs::write(
2906            svc_dir.join("service.toml"),
2907            "[service]\n\
2908             name = \"napp\"\n\
2909             description = \"native demo\"\n\
2910             runtime = \"native\"\n\
2911             run = \"python -m app\"\n\
2912             build = \"pip install -r requirements.txt\"\n\
2913             deploy = \"blue-green\"\n\
2914             health_check = \"/healthz\"\n\
2915             \n\
2916             [[ports]]\n\
2917             name = \"http\"\n\
2918             container_port = 8080\n",
2919        )
2920        .unwrap();
2921        // A source file to prove SyncDir has something to copy.
2922        std::fs::write(svc_dir.join("app.py"), "print('hi')\n").unwrap();
2923    }
2924
2925    fn plan_demo(tmp: &std::path::Path) -> AddResult {
2926        plan_service(tmp, "demo")
2927    }
2928
2929    fn plan_service(tmp: &std::path::Path, name: &'static str) -> AddResult {
2930        plan_service_exposed(tmp, name, exposure::Exposure::Loopback)
2931    }
2932
2933    fn plan_service_exposed(
2934        tmp: &std::path::Path,
2935        name: &'static str,
2936        exposure: exposure::Exposure,
2937    ) -> AddResult {
2938        let empty_map = std::collections::BTreeMap::new();
2939        let empty_ports: std::collections::BTreeMap<String, u16> =
2940            std::collections::BTreeMap::new();
2941        let empty_set = std::collections::BTreeSet::new();
2942        let port_in_use = |_p: u16| false;
2943        add_service(AddServiceParams {
2944            service_name: name,
2945            exposure: &exposure,
2946            auth: AuthChoice::None,
2947            enable_smtp: false,
2948            enable_backup: false,
2949            env_overrides: &empty_map,
2950            enabled_groups: &empty_set,
2951            selected_choices: &empty_map,
2952            registry_name: "test",
2953            repo_dir: tmp,
2954            pre_built_ctx: None,
2955            port_in_use: &port_in_use,
2956            acme_mode: None,
2957            mode: PlanMode::Add,
2958            port_overrides: &empty_ports,
2959        })
2960        .expect("plan add")
2961    }
2962
2963    /// The whole point: `ryra add` on a `deploy = "blue-green"` podman service
2964    /// must emit two color slots, allocate a second host port, and start only
2965    /// the active (blue) slot — never the bare `<svc>` unit.
2966    #[test]
2967    fn blue_green_podman_add_emits_two_slots_and_starts_blue() {
2968        let tmp = tempfile::tempdir().unwrap();
2969        write_demo_registry(
2970            tmp.path(),
2971            "deploy = \"blue-green\"\nhealth_check = \"/healthz\"",
2972        );
2973        let result = plan_demo(tmp.path());
2974
2975        // Quadlet files written: demo-blue.container + demo-green.container,
2976        // and crucially NOT the bare demo.container.
2977        let written: Vec<String> = result
2978            .steps
2979            .iter()
2980            .filter_map(|s| match s {
2981                Step::WriteFile(f) => f
2982                    .path
2983                    .file_name()
2984                    .and_then(|n| n.to_str())
2985                    .map(String::from),
2986                _ => None,
2987            })
2988            .collect();
2989        assert!(
2990            written.iter().any(|n| n == "demo-blue.container"),
2991            "got {written:?}"
2992        );
2993        assert!(
2994            written.iter().any(|n| n == "demo-green.container"),
2995            "got {written:?}"
2996        );
2997        assert!(
2998            !written.iter().any(|n| n == "demo.container"),
2999            "bare slot leaked: {written:?}"
3000        );
3001
3002        // The color quadlets carry their color-specific port var + container name.
3003        let blue = result
3004            .steps
3005            .iter()
3006            .find_map(|s| match s {
3007                Step::WriteFile(f) if f.path.ends_with("demo-blue.container") => Some(&f.content),
3008                _ => None,
3009            })
3010            .unwrap();
3011        assert!(blue.contains("ContainerName=demo-blue"));
3012        assert!(blue.contains("${SERVICE_PORT_HTTP_BLUE}"));
3013
3014        // Start targets the active (blue) slot, not the bare unit.
3015        let started: Vec<&str> = result
3016            .steps
3017            .iter()
3018            .filter_map(|s| match s {
3019                Step::StartService { unit } => Some(unit.as_str()),
3020                _ => None,
3021            })
3022            .collect();
3023        assert!(started.contains(&"demo-blue"), "started: {started:?}");
3024        assert!(!started.contains(&"demo"), "bare unit started: {started:?}");
3025
3026        // A second host port was allocated and exposed as the two color vars.
3027        let env = result
3028            .steps
3029            .iter()
3030            .find_map(|s| match s {
3031                Step::WriteFile(f) if f.path.file_name() == Some(std::ffi::OsStr::new(".env")) => {
3032                    Some(&f.content)
3033                }
3034                _ => None,
3035            })
3036            .unwrap();
3037        assert!(env.contains("SERVICE_PORT_HTTP_BLUE="), "env: {env}");
3038        assert!(env.contains("SERVICE_PORT_HTTP_GREEN="), "env: {env}");
3039    }
3040
3041    /// `ryra add` on a blue/green NATIVE service (any language) must give each
3042    /// color its own synced+built working dir, write two units, and start only
3043    /// blue — the language-agnostic isolation path.
3044    #[test]
3045    fn blue_green_native_add_syncs_builds_and_starts_blue() {
3046        let tmp = tempfile::tempdir().unwrap();
3047        write_native_registry(tmp.path());
3048        let result = plan_service(tmp.path(), "napp");
3049
3050        // Each color slot gets a SyncDir + a Build in its own dir.
3051        let syncs: Vec<String> = result
3052            .steps
3053            .iter()
3054            .filter_map(|s| match s {
3055                Step::SyncDir { dst, .. } => Some(dst.to_string_lossy().into_owned()),
3056                _ => None,
3057            })
3058            .collect();
3059        assert!(
3060            syncs.iter().any(|d| d.ends_with("colors/blue")),
3061            "syncs: {syncs:?}"
3062        );
3063        assert!(
3064            syncs.iter().any(|d| d.ends_with("colors/green")),
3065            "syncs: {syncs:?}"
3066        );
3067        let builds: Vec<String> = result
3068            .steps
3069            .iter()
3070            .filter_map(|s| match s {
3071                Step::Build { dir, .. } => Some(dir.to_string_lossy().into_owned()),
3072                _ => None,
3073            })
3074            .collect();
3075        assert!(
3076            builds.iter().any(|d| d.ends_with("colors/blue")),
3077            "builds: {builds:?}"
3078        );
3079        assert!(
3080            builds.iter().any(|d| d.ends_with("colors/green")),
3081            "builds: {builds:?}"
3082        );
3083
3084        // Two color units written; the green unit runs from its own slot and
3085        // binds its own port via an explicit override.
3086        let green_unit = result
3087            .steps
3088            .iter()
3089            .find_map(|s| match s {
3090                Step::WriteFile(f) if f.path.ends_with("napp-green.service") => Some(&f.content),
3091                _ => None,
3092            })
3093            .expect("green unit");
3094        assert!(green_unit.contains("WorkingDirectory="));
3095        assert!(green_unit.contains("colors/green"));
3096        assert!(green_unit.contains("Environment=SERVICE_PORT_HTTP="));
3097        assert!(green_unit.contains("ExecStart=/bin/sh -c 'exec python -m app'"));
3098
3099        // Only blue is started.
3100        let started: Vec<&str> = result
3101            .steps
3102            .iter()
3103            .filter_map(|s| match s {
3104                Step::StartService { unit } => Some(unit.as_str()),
3105                _ => None,
3106            })
3107            .collect();
3108        assert_eq!(started, vec!["napp-blue"], "started: {started:?}");
3109
3110        // Port pair in .env.
3111        let env = result
3112            .steps
3113            .iter()
3114            .find_map(|s| match s {
3115                Step::WriteFile(f) if f.path.file_name() == Some(std::ffi::OsStr::new(".env")) => {
3116                    Some(&f.content)
3117                }
3118                _ => None,
3119            })
3120            .unwrap();
3121        assert!(env.contains("SERVICE_PORT_HTTP_BLUE="));
3122        assert!(env.contains("SERVICE_PORT_HTTP_GREEN="));
3123    }
3124
3125    /// A native service with a URL now flows through the Caddy-route logic
3126    /// (it used to return before that code ran). With no Caddy installed it
3127    /// surfaces the UrlWithoutReverseProxy warning rather than silently
3128    /// ignoring the exposure — proving the native path reaches the route logic
3129    /// (and, for blue/green, the active-slot port selection).
3130    #[test]
3131    fn blue_green_native_add_with_url_warns_when_no_caddy() {
3132        let tmp = tempfile::tempdir().unwrap();
3133        write_native_registry(tmp.path());
3134        let result = plan_service_exposed(
3135            tmp.path(),
3136            "napp",
3137            exposure::Exposure::Public {
3138                url: "https://napp.example.com".into(),
3139            },
3140        );
3141        assert!(
3142            result
3143                .warnings
3144                .iter()
3145                .any(|w| matches!(w, Warning::UrlWithoutReverseProxy { .. })),
3146            "native + url + no caddy should warn UrlWithoutReverseProxy"
3147        );
3148    }
3149
3150    /// A plain (restart) podman service is untouched by the blue/green path:
3151    /// one quadlet, started by its bare name.
3152    #[test]
3153    fn restart_podman_add_is_unchanged() {
3154        let tmp = tempfile::tempdir().unwrap();
3155        write_demo_registry(tmp.path(), "");
3156        let result = plan_demo(tmp.path());
3157        let written: Vec<String> = result
3158            .steps
3159            .iter()
3160            .filter_map(|s| match s {
3161                Step::WriteFile(f) => f
3162                    .path
3163                    .file_name()
3164                    .and_then(|n| n.to_str())
3165                    .map(String::from),
3166                _ => None,
3167            })
3168            .collect();
3169        assert!(
3170            written.iter().any(|n| n == "demo.container"),
3171            "got {written:?}"
3172        );
3173        assert!(
3174            !written.iter().any(|n| n.contains("-blue")),
3175            "got {written:?}"
3176        );
3177        let started: Vec<&str> = result
3178            .steps
3179            .iter()
3180            .filter_map(|s| match s {
3181                Step::StartService { unit } => Some(unit.as_str()),
3182                _ => None,
3183            })
3184            .collect();
3185        assert!(started.contains(&"demo"));
3186    }
3187
3188    #[test]
3189    fn static_template_filter_excludes_secrets_and_credentials() {
3190        // Plain literal — tracked.
3191        assert!(is_static_template("3306"));
3192        assert!(is_static_template("mariadb"));
3193        // Stable template references — tracked.
3194        assert!(is_static_template("{{service.port}}"));
3195        assert!(is_static_template("{{service.url}}"));
3196        assert!(is_static_template("{{auth.url}}"));
3197        assert!(is_static_template("{{auth.issuer}}"));
3198        assert!(is_static_template("{{auth.provider}}"));
3199        assert!(is_static_template("{{auth.internal_url}}"));
3200        assert!(is_static_template("{{smtp.host}}"));
3201        assert!(is_static_template("{{smtp.port}}"));
3202        assert!(is_static_template("{{smtp.from}}"));
3203        // Composite template: stable + stable — tracked.
3204        assert!(is_static_template("{{service.url}}/oauth/callback"));
3205
3206        // Secrets — never tracked.
3207        assert!(!is_static_template("{{secret.admin_password}}"));
3208        assert!(!is_static_template("{{secret.jwt_key}}"));
3209        // Per-install OIDC credentials — never tracked (rotates on auth provider reinstall).
3210        assert!(!is_static_template("{{auth.client_id}}"));
3211        assert!(!is_static_template("{{auth.client_secret}}"));
3212        // SMTP credentials — never tracked.
3213        assert!(!is_static_template("{{smtp.username}}"));
3214        assert!(!is_static_template("{{smtp.password}}"));
3215        // Composite templates carrying a sensitive ref must also be excluded.
3216        assert!(!is_static_template(
3217            "redis://:{{secret.redis_pw}}@host:6379"
3218        ));
3219    }
3220
3221    #[test]
3222    fn tailscale_url_matches() {
3223        assert!(is_tailscale_url("http://debian.cobbler-tuna.ts.net"));
3224        assert!(is_tailscale_url("http://debian.cobbler-tuna.ts.net:10001/"));
3225        assert!(is_tailscale_url("https://foo.example-net.ts.net"));
3226        assert!(is_tailscale_url("http://HOST.COBBLER-TUNA.TS.NET"));
3227    }
3228
3229    #[test]
3230    fn tailscale_url_rejects() {
3231        assert!(!is_tailscale_url("https://nextcloud.internal:8443"));
3232        assert!(!is_tailscale_url("https://example.com"));
3233        assert!(!is_tailscale_url("http://127.0.0.1:10001"));
3234        // lookalike — must be exact `.ts.net` suffix
3235        assert!(!is_tailscale_url("https://ts.net"));
3236        assert!(!is_tailscale_url("https://evil-ts.net.example.com"));
3237        assert!(!is_tailscale_url("not a url"));
3238    }
3239
3240    #[test]
3241    fn public_url_accepts_public_domains() {
3242        assert!(is_public_url("https://seafile.ryra.no"));
3243        assert!(is_public_url("https://example.com"));
3244        assert!(is_public_url("https://docs.ryra.no:8443"));
3245    }
3246
3247    #[test]
3248    fn public_url_rejects_lan_and_tailnet() {
3249        assert!(!is_public_url("https://nextcloud.internal:8443"));
3250        assert!(!is_public_url("https://service.localhost"));
3251        assert!(!is_public_url("https://something.local"));
3252        assert!(!is_public_url("https://localhost:8080"));
3253        assert!(!is_public_url("https://debian.cobbler-tuna.ts.net"));
3254        assert!(!is_public_url("http://127.0.0.1:10001"));
3255        assert!(!is_public_url("http://192.168.1.10"));
3256        assert!(!is_public_url("http://[::1]"));
3257        assert!(!is_public_url("not a url"));
3258    }
3259
3260    // resolve_extra_networks positional args:
3261    // (name, enable_auth, authelia_installed, caddy_installed,
3262    //  inbucket_installed, has_url, has_smtp)
3263
3264    #[test]
3265    fn networks_empty_when_no_auth() {
3266        let nets = resolve_extra_networks(
3267            "whoami", false, false, false, false, false, false, None, false,
3268        );
3269        assert!(nets.is_empty());
3270    }
3271
3272    #[test]
3273    fn networks_empty_when_auth_but_no_authelia() {
3274        let nets = resolve_extra_networks(
3275            "forgejo", true, false, false, false, false, false, None, false,
3276        );
3277        assert!(nets.is_empty());
3278    }
3279
3280    #[test]
3281    fn networks_authelia_when_auth_enabled() {
3282        let nets = resolve_extra_networks(
3283            "forgejo", true, true, false, false, false, false, None, false,
3284        );
3285        assert_eq!(nets, vec!["authelia"]);
3286    }
3287
3288    #[test]
3289    fn networks_auth_with_caddy_includes_both() {
3290        let nets = resolve_extra_networks(
3291            "forgejo", true, true, true, false, false, false, None, false,
3292        );
3293        assert!(nets.contains(&"authelia".to_string()));
3294        assert!(nets.contains(&"caddy".to_string()));
3295    }
3296
3297    #[test]
3298    fn networks_authelia_excluded_for_authelia_itself() {
3299        let nets = resolve_extra_networks(
3300            "authelia", true, true, false, false, false, false, None, false,
3301        );
3302        assert!(nets.is_empty());
3303    }
3304
3305    #[test]
3306    fn networks_smtp_joins_inbucket_without_caddy() {
3307        // Reaching inbucket for SMTP must NOT require caddy.
3308        let nets = resolve_extra_networks(
3309            "forgejo", false, false, false, true, false, true, None, false,
3310        );
3311        assert_eq!(nets, vec!["inbucket"]);
3312    }
3313
3314    #[test]
3315    fn networks_smtp_skips_inbucket_when_it_is_self() {
3316        let nets = resolve_extra_networks(
3317            "inbucket", false, false, false, true, false, true, None, false,
3318        );
3319        assert!(!nets.contains(&"inbucket".to_string()));
3320    }
3321
3322    #[test]
3323    fn networks_smtp_skips_inbucket_when_not_installed() {
3324        let nets = resolve_extra_networks(
3325            "forgejo", false, false, false, false, false, true, None, false,
3326        );
3327        assert!(!nets.contains(&"inbucket".to_string()));
3328    }
3329
3330    #[test]
3331    fn networks_metrics_consumer_joins_store() {
3332        let nets = resolve_extra_networks(
3333            "grafana",
3334            false,
3335            false,
3336            false,
3337            false,
3338            false,
3339            false,
3340            Some("prometheus"),
3341            true,
3342        );
3343        assert_eq!(nets, vec!["prometheus".to_string()]);
3344    }
3345
3346    #[test]
3347    fn networks_metrics_store_skips_itself() {
3348        let nets = resolve_extra_networks(
3349            "prometheus",
3350            false,
3351            false,
3352            false,
3353            false,
3354            false,
3355            false,
3356            Some("prometheus"),
3357            true,
3358        );
3359        assert!(nets.is_empty());
3360    }
3361
3362    #[test]
3363    fn networks_metrics_indifferent_service_skips_store() {
3364        let nets = resolve_extra_networks(
3365            "vaultwarden",
3366            false,
3367            false,
3368            false,
3369            false,
3370            false,
3371            false,
3372            Some("prometheus"),
3373            false,
3374        );
3375        assert!(nets.is_empty());
3376    }
3377
3378    #[test]
3379    fn quadlet_belongs_to_exact_match() {
3380        let all = &["foo", "foo-bar"];
3381        assert!(quadlet_belongs_to("foo.container", "foo", all));
3382        assert!(quadlet_belongs_to("foo.network", "foo", all));
3383    }
3384
3385    #[test]
3386    fn quadlet_belongs_to_sidecar() {
3387        // foo-db is a sidecar, not a separate service
3388        let all = &["foo"];
3389        assert!(quadlet_belongs_to("foo-db.volume", "foo", all));
3390    }
3391
3392    #[test]
3393    fn quadlet_belongs_to_rejects_prefix_collision() {
3394        let all = &["foo", "foo-bar"];
3395        assert!(!quadlet_belongs_to("foo-bar.container", "foo", all));
3396        assert!(!quadlet_belongs_to("foo-bar-db.volume", "foo", all));
3397    }
3398
3399    #[test]
3400    fn quadlet_belongs_to_hyphenated_service() {
3401        let all = &["foo", "foo-bar"];
3402        assert!(quadlet_belongs_to("foo-bar.container", "foo-bar", all));
3403        assert!(quadlet_belongs_to("foo-bar-db.volume", "foo-bar", all));
3404        assert!(!quadlet_belongs_to("foo.container", "foo-bar", all));
3405    }
3406}