Skip to main content

ryra_core/
lib.rs

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