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