Skip to main content

ignition_core/actions/
deploy.rs

1//! Deploy actions: `ign deploy render` (Phase 21) and the `deploy plan`
2//! engine entry point (Phase 22).
3//!
4//! An OFFLINE verb: it reads a declared repo from disk and returns a model.
5//! There is no `Session`, no profile resolution, no network connection and
6//! no credential read; the envelope's `profile` is null. Render reads the
7//! repo and writes nothing, except into the optional `--out` directory, and
8//! only after the whole render succeeded: a refused render leaves `--out`
9//! untouched. The write goes through [`write_out`]'s clobber ladder, which
10//! never deletes and only replaces its own unmodified prior output.
11//!
12//! The `data` keys, in order: `env, gateway, mode, out, files, bytes,
13//! render_sha256, resources, tag_providers, uuids_minted, opaque_files,
14//! warnings`. Later plans add keys additively (the `workspace.rs`
15//! discipline); `out` is always present and is null without `--out`.
16//!
17//! Errors never echo file contents: findings name repo-relative paths and
18//! key names only.
19//!
20//! ## `deploy plan` (22-01)
21//!
22//! [`load_declared`] is the shared front half of every verb that starts from
23//! a declared repo (render, plan, later pull and verify): it reads
24//! `targets/<env>.yaml`, the gateway manifest and the source tree and
25//! renders them. [`deploy_plan`] then compares that render with a node's
26//! live files, read through a [`TreeReader`]. It opens no gateway session,
27//! sends no REST request, takes no scan-lock and writes nothing: the reader
28//! has no write half to call.
29//!
30//! The [`DeployPlanResult`] `data` keys, in order: `env, gateway, mode,
31//! node, transport, render_sha256, plan_sha256, summary, entries,
32//! delete_set, base, warnings`. Later plans add keys additively.
33//! Entries carry paths and sha256 only, never file bytes.
34//!
35//! ## `deploy pull` (22-08, DPLN-06)
36//!
37//! [`deploy_pull`] bootstraps `gateways/<name>/` from the live master through
38//! the same read-only [`TreeReader`]: it reads, builds a tree that renders
39//! back to the live effective view ([`crate::deploy::pull`]), and only then
40//! writes the local repo. It never writes to the gateway host or the gateway
41//! and resolves no session. Replacing a non-empty `resources/`, `tags/` or
42//! `modes/<env>/` needs `yes` ([`deploy_pull_preview`] names what would be
43//! replaced); `manifest.yaml` and other environments' overlays are never
44//! touched. On unix every removal and write goes through descriptor-relative
45//! calls rooted at the repo (`tree::dirfd`): a directory swapped for a
46//! symlink mid-run is refused, never followed.
47//!
48//! The [`DeployPullResult`] `data` keys, in order: `env, gateway, mode, node,
49//! transport, out, files_written, manifest, render_sha256, skipped,
50//! warnings`.
51//!
52//! ## `deploy verify` (22-10, DPLN-07, DPLN-08, DPLN-09)
53//!
54//! [`deploy_verify`] gathers read-only evidence from the master gateway and
55//! judges it with the pure [`verify::evaluate`] (six checks, fixed order; see
56//! [`crate::deploy::verify`] for the rules). It takes a [`DeployApi`] for the
57//! REST resource, scan and mode reads, a [`GatewayApi`] for the log and the
58//! WebDev tag probes, and a [`TreeReader`] for the live files. It takes no
59//! scan-lock, triggers no scan and writes nothing.
60//!
61//! A red verdict is a normal result: `ok: false`, `slug:
62//! deploy_verify_failed`, and [`DeployVerifyResult::failure_exit_code`]
63//! returns `Some(6)` for the binary to apply after rendering (Decision A).
64//! An unreachable gateway, a refused resource, scan or mode read, and a
65//! transport failure are [`CoreError`]s instead, because there is nothing to
66//! judge. No log text, tag value or file byte reaches the result.
67//!
68//! The [`DeployVerifyResult`] `data` keys, in order: `env, gateway, mode,
69//! node, project, ok, slug, checks, resources_checked, warnings`.
70//!
71//! ## `deploy verify --node` (24-09, DPAIR-01, DPAIR-03, D-12)
72//!
73//! [`deploy_verify_node`] adds the backup of a redundant pair. `master` is
74//! [`deploy_verify`] unchanged; `backup` runs the backup checks only and
75//! `both` runs the master's six and then the backup's. The result then also
76//! carries `backup` (the [`crate::deploy::pair::BackupReport`] shape, with
77//! `budget_s` 0, `waited_s` 0 and `resync` null: verify never waits, never
78//! resyncs and never writes). Both keys are absent for `master`, so its
79//! output is byte-identical to the verb before Phase 24.
80
81use std::collections::{BTreeMap, BTreeSet};
82use std::path::{Path, PathBuf};
83use std::time::Duration;
84
85use serde::Serialize;
86
87use crate::client::logs::{DEFAULT_LOG_LIMIT, LogQuery};
88use crate::client::mode::normalize_deployment_mode;
89use crate::client::query::ListQuery;
90use crate::client::scan::{ScanKind, ScanStatus};
91use crate::client::{DeployApi, GatewayApi};
92use crate::deploy::apply::journal::{journal_on_box, journal_present};
93use crate::deploy::component_problem;
94use crate::deploy::exclude;
95use crate::deploy::layout::{RelPath, collection_path};
96use crate::deploy::ledger::{Ledger, LedgerRoot, ledger_location};
97use crate::deploy::manifest::Manifest;
98use crate::deploy::pair::{
99    BackupCheck, BackupCheckId, BackupNode, BackupReport, BackupSignals, SecretCheckMode,
100    SecretNamesReport, TickOutcome, check_secret_names, collect_referenced_secrets, compare_files,
101    declared_secret_providers, effective_files, exempt_unreadable, files_check,
102    files_skipped_check, mode_backup_check, read_back_backup_check, unreachable_check,
103    unreadable_units, unreadable_warnings, verify_report,
104};
105use crate::deploy::plan::{
106    BaseTree, PlanEntry, PlanSummary, UnreadableUnit, apply_base, live_tag_folder_findings, plan,
107    render_diffs,
108};
109use crate::deploy::pull::{PullInput, REASON_PATH_TOO_LONG, Skipped, generate_manifest, pull};
110use crate::deploy::render::{RenderInput, RenderOutcome, RenderedResource, render};
111use crate::deploy::target::{NodeTarget, Target};
112use crate::deploy::transport::{CORE_COLLECTION, DeleteSet, READ_MAX_ENTRIES, Scope, TreeReader};
113use crate::deploy::tree::{
114    RenderMarker, SourceTree, read_repo_file, require_repo_dir, walk_capped, write_out,
115};
116use crate::deploy::verify::{self, CheckStatus};
117use crate::error::CoreError;
118
119/// The `ign deploy render` result (the envelope's `data`).
120#[derive(Debug, Clone, PartialEq, Serialize)]
121pub struct DeployRenderResult {
122    /// The `--env` value.
123    pub env: String,
124    /// Gateway directory name from the target.
125    pub gateway: String,
126    /// Deployment mode from the target.
127    pub mode: String,
128    /// The `--out` directory as given; null without `--out`.
129    pub out: Option<String>,
130    /// Number of rendered files.
131    pub files: usize,
132    /// Total rendered bytes.
133    pub bytes: usize,
134    /// Lowercase hex SHA-256 of the rendered listing (README documents it).
135    pub render_sha256: String,
136    /// Rendered resources, byte-ordered, singletons first.
137    pub resources: Vec<RenderedResource>,
138    /// Tag providers rendered, byte-ordered.
139    pub tag_providers: Vec<String>,
140    /// Collection paths that received a minted uuid, byte-ordered.
141    pub uuids_minted: Vec<String>,
142    /// Collection paths passed through as opaque bytes, byte-ordered.
143    pub opaque_files: Vec<String>,
144    /// Warnings in pipeline order.
145    pub warnings: Vec<String>,
146}
147
148/// Read and parse `targets/<env>.yaml`: the target file's repo-relative
149/// name and the parsed target.
150pub fn load_target(repo: &Path, env: &str) -> Result<(String, Target), CoreError> {
151    if let Some(problem) = component_problem(env) {
152        return Err(CoreError::InvalidInput {
153            reason: format!("--env {env}: {problem}"),
154        });
155    }
156    let target_rel = format!("targets/{env}.yaml");
157    let target_bytes = read_repo_file(
158        repo,
159        &target_rel,
160        Some(&format!("--env {env} selects {target_rel}")),
161    )?;
162    let target = Target::parse(&target_rel, &target_bytes)?;
163    Ok((target_rel, target))
164}
165
166/// A declared repo, loaded and rendered for one environment.
167#[derive(Debug, Clone)]
168pub struct Declared {
169    /// The `--env` value.
170    pub env: String,
171    /// Repo-relative path of the target file (`targets/<env>.yaml`).
172    pub target_file: String,
173    /// The parsed target.
174    pub target: Target,
175    /// The parsed gateway manifest.
176    pub manifest: Manifest,
177    /// The render of the declared tree.
178    pub outcome: RenderOutcome,
179}
180
181impl Declared {
182    /// The effective tag provider exclusion set of this target and manifest
183    /// ([`exclude::effective`]): byte-sorted, always holding `System`. Every
184    /// host read scope and every plan of this tree uses it.
185    pub fn excluded_tag_providers(&self) -> Vec<String> {
186        exclude::effective(&self.target, Some(&self.manifest))
187    }
188}
189
190/// Load the target, the manifest and the source tree of `env` under `repo`
191/// and render them. Reads the repo and nothing else.
192pub fn load_declared(repo: &Path, env: &str) -> Result<Declared, CoreError> {
193    load_declared_in(repo, repo, env)
194}
195
196/// [`load_declared`] with the target read from `targets` instead of `repo`
197/// (`--targets DIR`, D-14): `targets/<env>.yaml` comes from `targets`, the
198/// `gateways/<gateway>/` tree from `repo`. A caller that builds the node's
199/// host passes the same `targets` root to it, so a relative `known_hosts` or
200/// `local.data_dir` resolves against the targets checkout.
201pub fn load_declared_in(repo: &Path, targets: &Path, env: &str) -> Result<Declared, CoreError> {
202    let (target_file, target) = load_target(targets, env)?;
203
204    let gateway_rel = format!("gateways/{}", target.gateway);
205    let gateway_dir = repo.join("gateways").join(&target.gateway);
206    require_repo_dir(repo, "gateways")?;
207    require_repo_dir(repo, &gateway_rel)?;
208
209    let manifest_rel = format!("{gateway_rel}/manifest.yaml");
210    let manifest_bytes = read_repo_file(repo, &manifest_rel, None)?;
211    let manifest = Manifest::parse(&manifest_rel, &manifest_bytes)?;
212
213    let source = SourceTree::read_gateway(&gateway_dir, env)?;
214    let outcome = render(&RenderInput {
215        gateway: &target.gateway,
216        env,
217        manifest: &manifest,
218        target: &target,
219        source: &source,
220    })?;
221    Ok(Declared {
222        env: env.to_string(),
223        target_file,
224        target,
225        manifest,
226        outcome,
227    })
228}
229
230/// Render the declared tree of `env` under `repo`, optionally writing it
231/// (and `render.json`) to `out`.
232pub fn deploy_render(
233    repo: &Path,
234    env: &str,
235    out: Option<&Path>,
236) -> Result<DeployRenderResult, CoreError> {
237    deploy_render_in(repo, repo, env, out)
238}
239
240/// [`deploy_render`] with the target read from `targets` (`--targets DIR`,
241/// D-14). `out` is written wherever the caller names it, never under
242/// `targets`.
243pub fn deploy_render_in(
244    repo: &Path,
245    targets: &Path,
246    env: &str,
247    out: Option<&Path>,
248) -> Result<DeployRenderResult, CoreError> {
249    let Declared {
250        target, outcome, ..
251    } = load_declared_in(repo, targets, env)?;
252
253    // Only a successful render reaches the filesystem.
254    if let Some(out) = out {
255        let marker = RenderMarker::for_tree(&target.gateway, env, &target.mode, &outcome.tree);
256        write_out(&outcome.tree, out, &marker)?;
257    }
258
259    Ok(DeployRenderResult {
260        env: env.to_string(),
261        gateway: target.gateway.clone(),
262        mode: target.mode.clone(),
263        out: out.map(|p| p.to_string_lossy().into_owned()),
264        files: outcome.tree.files.len(),
265        bytes: outcome.tree.bytes(),
266        render_sha256: outcome.tree.render_sha256(),
267        resources: outcome.resources,
268        tag_providers: outcome.tag_providers,
269        uuids_minted: outcome.uuids_minted,
270        opaque_files: outcome.opaque_files,
271        warnings: outcome.warnings,
272    })
273}
274
275/// The `ign deploy plan` result (the envelope's `data`).
276#[derive(Debug, Clone, PartialEq, Serialize)]
277pub struct DeployPlanResult {
278    /// The `--env` value.
279    pub env: String,
280    /// Gateway directory name from the target.
281    pub gateway: String,
282    /// Deployment mode from the target.
283    pub mode: String,
284    /// The node that was read: always `master` in this phase.
285    pub node: String,
286    /// How the node's host was read: `local` or `ssh`.
287    pub transport: String,
288    /// Lowercase hex SHA-256 of the rendered listing.
289    pub render_sha256: String,
290    /// Lowercase hex SHA-256 of the canonical plan text (D-08).
291    pub plan_sha256: String,
292    /// Counts by status; `has_changes` ignores `unmanaged`.
293    pub summary: PlanSummary,
294    /// One entry per unit, in unit-key byte order.
295    pub entries: Vec<PlanEntry>,
296    /// The files plan proposes to delete: the declared-names ledger's files
297    /// minus the rendered files (22-03). Empty without a ledger.
298    pub delete_set: DeleteSet,
299    /// The `--base` render the drift was computed against; null without it.
300    pub base: Option<BaseSummary>,
301    /// Render warnings, then plan warnings.
302    pub warnings: Vec<String>,
303    /// The effective tag provider exclusion set: byte-sorted, always holds
304    /// `System`. Those providers were not read and are never written or
305    /// deleted. Not part of `plan_sha256`.
306    pub excluded_tag_providers: Vec<String>,
307    /// Units the host holds but could not serve whole (a Windows sftp host
308    /// path of 240 characters or more), in key byte order: never an entry,
309    /// never in the delete set, one `U` line each in `plan_sha256`. Always
310    /// empty on Linux and local hosts.
311    pub unreadable: Vec<UnreadableUnit>,
312}
313
314/// Options of `deploy plan`.
315#[derive(Debug, Clone, Default)]
316pub struct PlanOptions {
317    /// A directory written by `deploy render --out`, re-hashed and used as
318    /// the base of the three-way drift check.
319    pub base: Option<PathBuf>,
320    /// Add redacted unified text diffs to the entries.
321    pub diff: bool,
322}
323
324/// What `--base` was: the directory and its recomputed digest.
325#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
326pub struct BaseSummary {
327    /// The `--base` directory as given.
328    pub dir: String,
329    /// The base's recomputed `render_sha256`.
330    pub render_sha256: String,
331    /// Number of files in the base (`render.json` excluded).
332    pub files: usize,
333}
334
335/// Plan `declared` against the live files `reader` returns for the master
336/// node. Reads once; never writes, never opens a gateway session, never
337/// prints.
338pub async fn deploy_plan(
339    declared: &Declared,
340    reader: &dyn TreeReader,
341    opts: &PlanOptions,
342) -> Result<DeployPlanResult, CoreError> {
343    let node = &declared.target.nodes.master;
344    let transport = match (&node.ssh, &node.local) {
345        (Some(_), _) => "ssh",
346        (None, Some(_)) => "local",
347        (None, None) => "none",
348    };
349    // A base that is not exactly a clean render is refused before any
350    // transport work (22-04).
351    let base = match &opts.base {
352        Some(dir) => {
353            let dir = dir.clone();
354            let (gateway, env, mode) = (
355                declared.target.gateway.clone(),
356                declared.env.clone(),
357                declared.target.mode.clone(),
358            );
359            let read =
360                tokio::task::spawn_blocking(move || BaseTree::read(&dir, &gateway, &env, &mode))
361                    .await
362                    .map_err(|e| CoreError::Internal(format!("--base read task failed: {e}")))?;
363            Some(read?)
364        }
365        None => None,
366    };
367    // An interrupted apply left its journal: the tree may be torn, so nothing
368    // is read or planned until `rollback --run` recovers it (DAPL-09, D-09).
369    // An unreadable journal blocks the same way. Checked after the base (a
370    // bad one costs no transport work) and before the collections are read.
371    if let Some(journal) = journal_on_box(
372        reader,
373        node,
374        &declared.target.gateway,
375        &declared.target.mode,
376    )
377    .await?
378    {
379        return Err(journal_present(
380            &journal,
381            &declared.env,
382            node,
383            &declared.target.gateway,
384            &declared.target.mode,
385        ));
386    }
387    let excluded = declared.excluded_tag_providers();
388    let scope = Scope::from_manifest(&declared.manifest, &declared.target.mode, &excluded);
389    let observed = reader.read(&scope).await?;
390    // A live tag folder named like a tag file (CD5d) is refused before any
391    // planning: an apply would only meet it when it renames onto it
392    // (261030-8m8 review WR-04).
393    let clashes = live_tag_folder_findings(&observed, &declared.target.mode);
394    if !clashes.is_empty() {
395        return Err(CoreError::deploy_findings(clashes));
396    }
397    let gateway = &declared.target.gateway;
398    let mode = &declared.target.mode;
399    // An absent ledger is a first deploy (empty delete set); one that exists
400    // but cannot be trusted is a refusal, never an empty delete set (D-07).
401    let ledger = match reader.read_ledger(gateway, mode).await? {
402        Some(bytes) => Some(Ledger::parse(
403            &ledger_location(LedgerRoot::for_node(node), gateway, mode),
404            &bytes,
405            gateway,
406            mode,
407        )?),
408        None => None,
409    };
410    let mut planned = plan(
411        &declared.outcome.tree,
412        &observed,
413        ledger.as_ref(),
414        &scope,
415        &excluded,
416    );
417    if let Some(base) = &base {
418        apply_base(&mut planned, base);
419    }
420    if opts.diff {
421        render_diffs(&mut planned, &declared.outcome.tree, &observed);
422    }
423
424    let mut warnings = declared.outcome.warnings.clone();
425    warnings.extend(planned.warnings);
426    Ok(DeployPlanResult {
427        env: declared.env.clone(),
428        gateway: declared.target.gateway.clone(),
429        mode: declared.target.mode.clone(),
430        node: "master".to_string(),
431        transport: transport.to_string(),
432        render_sha256: declared.outcome.tree.render_sha256(),
433        plan_sha256: planned.plan_sha256,
434        summary: planned.summary,
435        entries: planned.entries,
436        delete_set: planned.delete_set,
437        base: base.map(|b| BaseSummary {
438            dir: b.dir,
439            render_sha256: b.render_sha256,
440            files: b.files.len(),
441        }),
442        warnings,
443        excluded_tag_providers: excluded,
444        unreadable: planned.unreadable,
445    })
446}
447
448/// The `ign deploy pull` result (the envelope's `data`).
449#[derive(Debug, Clone, PartialEq, Serialize)]
450pub struct DeployPullResult {
451    /// The `--env` value.
452    pub env: String,
453    /// Gateway directory name from the target.
454    pub gateway: String,
455    /// Deployment mode from the target; its collection is the overlay.
456    pub mode: String,
457    /// The node that was read: always `master` in this phase.
458    pub node: String,
459    /// How the node's host was read: `local` or `ssh`.
460    pub transport: String,
461    /// The directory written, repo-relative: `gateways/<name>`.
462    pub out: String,
463    /// Source files written under `resources/`, `tags/` and `modes/<env>/`
464    /// (the manifest is not counted).
465    pub files_written: usize,
466    /// `kept` when `manifest.yaml` already existed, `generated` otherwise.
467    pub manifest: String,
468    /// Lowercase hex SHA-256 of what `deploy render` produces from the
469    /// pulled tree.
470    pub render_sha256: String,
471    /// Every live unit (or type) not written, byte-sorted by path, each with
472    /// a reason. Never a value.
473    pub skipped: Vec<Skipped>,
474    /// Render warnings of the pulled tree, then pull warnings.
475    pub warnings: Vec<String>,
476    /// The effective tag provider exclusion set: byte-sorted, always holds
477    /// `System`. Those providers were not listed or read, and are left out
478    /// of a generated manifest.
479    pub excluded_tag_providers: Vec<String>,
480}
481
482/// What an existing `gateways/<name>/` holds in the three subtrees a pull
483/// replaces. Gateway-relative paths, files in `resources`, `tags`,
484/// `modes/<env>` order and byte order within each.
485struct Existing {
486    files: Vec<String>,
487    /// Every directory below and including each subtree root.
488    dirs: BTreeSet<String>,
489}
490
491/// Whether `repo/rel` exists, without following a symlink.
492fn repo_entry_exists(repo: &Path, rel: &str) -> Result<bool, CoreError> {
493    match std::fs::symlink_metadata(repo.join(rel)) {
494        Ok(_) => Ok(true),
495        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
496        Err(e) => Err(CoreError::deploy_tree_invalid(
497            rel,
498            None,
499            None,
500            format!("cannot inspect ({})", e.kind()),
501        )),
502    }
503}
504
505/// Inspect the subtrees `deploy pull` would replace. A symlink at any level
506/// of them, or anywhere under them, is `deploy_tree_invalid` and nothing is
507/// changed. `None` when `gateways/<gateway>/` does not exist.
508fn scan_existing(repo: &Path, gateway: &str, env: &str) -> Result<Option<Existing>, CoreError> {
509    let gateway_rel = format!("gateways/{gateway}");
510    for rel in ["gateways", gateway_rel.as_str()] {
511        if !repo_entry_exists(repo, rel)? {
512            return Ok(None);
513        }
514        require_repo_dir(repo, rel)?;
515    }
516    let mut existing = Existing {
517        files: Vec::new(),
518        dirs: BTreeSet::new(),
519    };
520    for sub in [
521        "resources".to_string(),
522        "tags".to_string(),
523        format!("modes/{env}"),
524    ] {
525        if sub.starts_with("modes/") {
526            let modes = format!("{gateway_rel}/modes");
527            if !repo_entry_exists(repo, &modes)? {
528                continue;
529            }
530            require_repo_dir(repo, &modes)?;
531        }
532        let rel = format!("{gateway_rel}/{sub}");
533        if !repo_entry_exists(repo, &rel)? {
534            continue;
535        }
536        require_repo_dir(repo, &rel)?;
537        let mut files: BTreeMap<String, PathBuf> = BTreeMap::new();
538        let mut dirs: BTreeSet<String> = BTreeSet::new();
539        walk_capped(
540            &repo.join(&rel),
541            &sub,
542            &mut files,
543            &mut dirs,
544            READ_MAX_ENTRIES,
545        )
546        .map_err(|why| CoreError::deploy_tree_invalid(&rel, None, None, why))?;
547        existing.dirs.insert(sub.clone());
548        existing.dirs.extend(dirs);
549        existing.files.extend(files.into_keys());
550    }
551    Ok(Some(existing))
552}
553
554/// The refusal prose of a pull that would replace `existing`.
555fn pull_prose(gateway: &str, env: &str, existing: &Existing) -> String {
556    let shown: Vec<&str> = existing.files.iter().take(5).map(String::as_str).collect();
557    let more = if existing.files.len() > shown.len() {
558        ", ..."
559    } else {
560        ""
561    };
562    format!(
563        "deploy pull would replace gateways/{gateway}/resources, tags and modes/{env} with the live tree of nodes.master ({} existing files, e.g. {}{more})",
564        existing.files.len(),
565        shown.join(", ")
566    )
567}
568
569/// What `deploy pull` would replace: `Some(prose)` when the gateway
570/// directory holds files in `resources/`, `tags/` or `modes/<env>/` (the
571/// text the `--yes` guard refuses with), `None` when it is absent or holds
572/// nothing there. Reads the local repo only: no gateway is contacted.
573pub fn deploy_pull_preview(repo: &Path, env: &str) -> Result<Option<String>, CoreError> {
574    deploy_pull_preview_in(repo, repo, env)
575}
576
577/// [`deploy_pull_preview`] with the target read from `targets` (`--targets
578/// DIR`, D-14); the existing tree is still scanned under `repo`.
579pub fn deploy_pull_preview_in(
580    repo: &Path,
581    targets: &Path,
582    env: &str,
583) -> Result<Option<String>, CoreError> {
584    let (_, target) = load_target(targets, env)?;
585    Ok(scan_existing(repo, &target.gateway, env)?
586        .filter(|existing| !existing.files.is_empty())
587        .map(|existing| pull_prose(&target.gateway, env, &existing)))
588}
589
590fn transport_label(node: &NodeTarget) -> &'static str {
591    match (&node.ssh, &node.local) {
592        (Some(_), _) => "ssh",
593        (None, Some(_)) => "local",
594        (None, None) => "none",
595    }
596}
597
598/// Bootstrap `gateways/<gateway>/` from the live master. See the module doc.
599///
600/// `yes` confirms replacing existing `resources/`, `tags/` and `modes/<env>/`
601/// files; without it a non-empty tree is refused as `confirmation_required`
602/// before the gateway is read and before any write.
603pub async fn deploy_pull(
604    repo: &Path,
605    env: &str,
606    reader: &dyn TreeReader,
607    yes: bool,
608) -> Result<DeployPullResult, CoreError> {
609    deploy_pull_in(repo, repo, env, reader, yes).await
610}
611
612/// [`deploy_pull`] with the target read from `targets` (`--targets DIR`,
613/// D-14). The pulled tree and the manifest still live under `repo`.
614pub async fn deploy_pull_in(
615    repo: &Path,
616    targets: &Path,
617    env: &str,
618    reader: &dyn TreeReader,
619    yes: bool,
620) -> Result<DeployPullResult, CoreError> {
621    let (_, target) = load_target(targets, env)?;
622    let gateway = target.gateway.clone();
623    let existing = scan_existing(repo, &gateway, env)?;
624    if let Some(existing) = existing.as_ref().filter(|e| !e.files.is_empty())
625        && !yes
626    {
627        return Err(CoreError::ConfirmationRequired {
628            operation: pull_prose(&gateway, env, existing),
629        });
630    }
631
632    // The manifest: an existing one is used and kept (D-10); otherwise one is
633    // generated from the live types and tag providers.
634    let manifest_rel = format!("gateways/{gateway}/manifest.yaml");
635    let mut left_out: Vec<Skipped> = Vec::new();
636    let (manifest, generated, excluded) = if repo_entry_exists(repo, &manifest_rel)? {
637        let bytes = read_repo_file(repo, &manifest_rel, None)?;
638        let manifest = Manifest::parse(&manifest_rel, &bytes)?;
639        // A kept manifest that declares an excluded provider is refused
640        // before any host read: render would refuse the pulled tree anyway.
641        let excluded = exclude::effective(&target, Some(&manifest));
642        let contradictions = exclude::contradictions(&excluded, &manifest, &manifest_rel);
643        if !contradictions.is_empty() {
644            return Err(CoreError::deploy_findings(contradictions));
645        }
646        (manifest, None, excluded)
647    } else {
648        let excluded = exclude::effective(&target, None);
649        let mut live_types = reader.list_types(CORE_COLLECTION).await?;
650        live_types.extend(reader.list_types(&target.mode).await?);
651        // Provider discovery lists directory names only: no provider is read,
652        // so an excluded one is never walked (and an empty provider directory
653        // is named too).
654        let mut names = reader.list_tag_providers(CORE_COLLECTION).await?;
655        names.extend(reader.list_tag_providers(&target.mode).await?);
656        let providers: Vec<String> = names
657            .into_iter()
658            .filter(|p| !exclude::is_excluded(p, &excluded))
659            .collect::<BTreeSet<String>>()
660            .into_iter()
661            .collect();
662        let (yaml, skipped) = generate_manifest(&live_types, &providers);
663        left_out = skipped;
664        let manifest = Manifest::parse(&manifest_rel, yaml.as_bytes())?;
665        (manifest, Some(yaml), excluded)
666    };
667
668    let scope = Scope::from_manifest(&manifest, &target.mode, &excluded);
669    let observed = reader.read(&scope).await?;
670    let outcome = pull(&PullInput {
671        gateway: &gateway,
672        env,
673        target: &target,
674        manifest: &manifest,
675        observed: &observed,
676    })?;
677
678    write_pulled(
679        repo,
680        &gateway,
681        existing.as_ref(),
682        &outcome.source,
683        generated.as_deref(),
684    )?;
685
686    let removed = removed_unreadable_warnings(&gateway, env, existing.as_ref(), &outcome.skipped);
687    left_out.extend(outcome.skipped);
688    left_out.sort_by(|a, b| a.path.as_bytes().cmp(b.path.as_bytes()));
689    let mut warnings = outcome.rendered.warnings.clone();
690    warnings.extend(removed);
691    warnings.extend(observed.local_units.iter().map(|unit| {
692        format!(
693            "{unit} also exists in the `local` collection, which the gateway loads over the mode collection; pull did not read the `local` copy"
694        )
695    }));
696    Ok(DeployPullResult {
697        env: env.to_string(),
698        gateway: gateway.clone(),
699        mode: target.mode.clone(),
700        node: "master".to_string(),
701        transport: transport_label(&target.nodes.master).to_string(),
702        out: format!("gateways/{gateway}"),
703        files_written: outcome.source.files.len(),
704        manifest: if generated.is_some() {
705            "generated"
706        } else {
707            "kept"
708        }
709        .to_string(),
710        render_sha256: outcome.rendered.tree.render_sha256(),
711        skipped: left_out,
712        warnings,
713        excluded_tag_providers: excluded,
714    })
715}
716
717/// The most removed repo files one [`removed_unreadable_warnings`] line
718/// names before it counts the rest.
719const REMOVED_FILES_SHOWN: usize = 10;
720
721/// One warning per `path_too_long` unit whose repo copy this pull removed
722/// (quick-261029-ms3, WR-05). `--yes` replaces `resources/`, `tags/` and
723/// `modes/<env>/`, and an unreadable unit is never written in part, so the
724/// files the repo held for it are gone after the pull. They are not kept: a
725/// kept copy is not what the host serves, so the pulled tree would no longer
726/// render to the `render_sha256` pull reports and render then plan would not
727/// be empty. The removal is named instead of silent, with every removed
728/// repo file (gateway-relative under `gateways/<gateway>/`); git still holds
729/// them. Nothing for a unit the repo did not hold.
730fn removed_unreadable_warnings(
731    gateway: &str,
732    env: &str,
733    existing: Option<&Existing>,
734    skipped: &[Skipped],
735) -> Vec<String> {
736    let Some(existing) = existing else {
737        return Vec::new();
738    };
739    let mode_prefix = format!("modes/{env}/");
740    let mut out = Vec::new();
741    for unit in skipped.iter().filter(|s| s.reason == REASON_PATH_TOO_LONG) {
742        let under = format!("{}/", unit.path);
743        let removed: Vec<String> = existing
744            .files
745            .iter()
746            .filter(|file| {
747                let base = file.strip_prefix(&mode_prefix).unwrap_or(file);
748                RelPath::new(base)
749                    .ok()
750                    .and_then(|p| collection_path(&p).ok())
751                    .is_some_and(|c| c.as_str() == unit.path || c.as_str().starts_with(&under))
752            })
753            .map(|file| format!("gateways/{gateway}/{file}"))
754            .collect();
755        if removed.is_empty() {
756            continue;
757        }
758        let take = removed.len().min(REMOVED_FILES_SHOWN);
759        let mut named = removed[..take].join(", ");
760        if removed.len() > take {
761            named.push_str(&format!(" and {} more", removed.len() - take));
762        }
763        out.push(format!(
764            "{} has a live path the host's sftp server cannot read ({REASON_PATH_TOO_LONG}), so pull did not write it and removed its {} repo file(s): {named}; the live unit is unchanged and git still holds the removed files",
765            unit.path,
766            removed.len()
767        ));
768    }
769    out
770}
771
772/// A name that reaches the repo from the gateway is checked once more here:
773/// render already refused unsafe names, so this only fails closed.
774fn safe_component(name: &str) -> bool {
775    !name.is_empty()
776        && name != "."
777        && name != ".."
778        && !name.contains(['/', '\\', '\0'])
779        && !name.chars().any(char::is_control)
780}
781
782/// Refuse a pulled tree that cannot be written whole. This runs before the
783/// first delete, so a pull that would fail part-way fails with the existing
784/// tree untouched (CR-01): an unsafe path component, two paths that collide
785/// when case is folded (a case-insensitive filesystem keeps only one), and a
786/// path that is both a file and a directory.
787fn validate_pulled(gateway: &str, source: &SourceTree) -> Result<(), CoreError> {
788    let refuse = |path: &str, why: String| {
789        CoreError::deploy_tree_invalid(format!("gateways/{gateway}/{path}"), None, None, why)
790    };
791    let mut folded: BTreeMap<String, &str> = BTreeMap::new();
792    let mut dir_folds: BTreeSet<String> = BTreeSet::new();
793    for path in source.files.keys() {
794        let parts: Vec<&str> = path.components().collect();
795        if parts.iter().any(|c| !safe_component(c)) {
796            return Err(refuse(
797                path.as_str(),
798                "has an unsafe path component; refused before anything was changed".to_string(),
799            ));
800        }
801        let fold = path.as_str().to_lowercase();
802        if let Some(other) = folded.insert(fold.clone(), path.as_str()) {
803            return Err(refuse(
804                path.as_str(),
805                format!(
806                    "collides with {other} when case is folded; a case-insensitive filesystem keeps only one. Refused before anything was changed"
807                ),
808            ));
809        }
810        for end in fold.match_indices('/').map(|(i, _)| i) {
811            dir_folds.insert(fold[..end].to_string());
812        }
813    }
814    for (fold, path) in &folded {
815        if dir_folds.contains(fold) {
816            return Err(refuse(
817                path,
818                "is both a file and a directory in the pulled tree; refused before anything was changed"
819                    .to_string(),
820            ));
821        }
822    }
823    Ok(())
824}
825
826/// Validate the pulled tree, then replace what a pull replaces and write it
827/// (see [`write_pulled_fs`]). The validation comes first so no destructive
828/// step runs for a tree that is known to be unwritable.
829fn write_pulled(
830    repo: &Path,
831    gateway: &str,
832    existing: Option<&Existing>,
833    source: &SourceTree,
834    manifest: Option<&str>,
835) -> Result<(), CoreError> {
836    validate_pulled(gateway, source)?;
837    write_pulled_fs(repo, gateway, existing, source, manifest)
838}
839
840/// Remove what a pull replaces, then write the pulled tree and, when one was
841/// generated, the manifest. Unix: every step is descriptor-relative and
842/// rooted at the repo, so no component can be redirected by a symlink.
843#[cfg(unix)]
844fn write_pulled_fs(
845    repo: &Path,
846    gateway: &str,
847    existing: Option<&Existing>,
848    source: &SourceTree,
849    manifest: Option<&str>,
850) -> Result<(), CoreError> {
851    use crate::deploy::tree::dirfd::{self, FdError, Kind};
852    use std::io::Write;
853
854    let fail = |what: &str, e: FdError| match e {
855        FdError::Changed => CoreError::deploy_tree_invalid(
856            what,
857            None,
858            None,
859            "changed while it was written (a symlink or a non-directory appeared); refused, nothing outside the gateway directory was touched",
860        ),
861        FdError::Io(io) => CoreError::Internal(format!("cannot write {what}: {io}")),
862        other => CoreError::Internal(format!("cannot write {what}: {other:?}")),
863    };
864
865    // The repo root is the operator's own and may legitimately be reached
866    // through a symlink (`--repo link`), so it is resolved once here; every
867    // component below it is then opened without following symlinks.
868    let repo_real = std::fs::canonicalize(repo).map_err(|e| {
869        CoreError::Internal(format!("cannot resolve the repo {}: {e}", repo.display()))
870    })?;
871    let root = dirfd::open_root(&repo_real).map_err(|e| fail("the repo", e))?;
872    let gateways = dirfd::ensure_dir(&root, "gateways").map_err(|e| fail("gateways", e))?;
873    let gw_label = format!("gateways/{gateway}");
874    let gw = dirfd::ensure_dir(&gateways, gateway).map_err(|e| fail(&gw_label, e))?;
875
876    if let Some(existing) = existing {
877        for file in &existing.files {
878            let parts: Vec<&str> = file.split('/').collect();
879            let (name, parents) = parts.split_last().ok_or_else(|| {
880                CoreError::Internal("an existing file has an empty path".to_string())
881            })?;
882            let label = format!("{gw_label}/{file}");
883            let dir = dirfd::walk(&gw, parents).map_err(|e| fail(&label, e))?;
884            match dirfd::kind_at(&dir, name).map_err(|e| fail(&label, e))? {
885                Kind::File => dirfd::unlink_at(&dir, name).map_err(|e| fail(&label, e))?,
886                Kind::Missing => {}
887                _ => return Err(fail(&label, FdError::Changed)),
888            }
889        }
890        // children before parents: a child path sorts after its parent
891        for dir_path in existing.dirs.iter().rev() {
892            let parts: Vec<&str> = dir_path.split('/').collect();
893            let Some((name, parents)) = parts.split_last() else {
894                continue;
895            };
896            let label = format!("{gw_label}/{dir_path}");
897            let parent = dirfd::walk(&gw, parents).map_err(|e| fail(&label, e))?;
898            match dirfd::rmdir_at(&parent, name) {
899                Ok(()) | Err(FdError::Missing) => {}
900                Err(FdError::NotEmpty) => {
901                    return Err(CoreError::deploy_tree_invalid(
902                        label,
903                        None,
904                        None,
905                        "is not empty after the pull removed its files (something wrote into it); nothing more was changed",
906                    ));
907                }
908                Err(e) => return Err(fail(&label, e)),
909            }
910        }
911    }
912
913    if let Some(text) = manifest {
914        let label = format!("{gw_label}/manifest.yaml");
915        let mut file = dirfd::create_new(&gw, "manifest.yaml").map_err(|e| fail(&label, e))?;
916        file.write_all(text.as_bytes())
917            .map_err(|e| CoreError::Internal(format!("cannot write {label}: {e}")))?;
918    }
919    for (path, bytes) in &source.files {
920        let label = format!("{gw_label}/{path}");
921        let parts: Vec<&str> = path.components().collect();
922        if parts.iter().any(|c| !safe_component(c)) {
923            return Err(CoreError::deploy_tree_invalid(
924                label,
925                None,
926                None,
927                "has an unsafe path component; refused",
928            ));
929        }
930        let (name, parents) = parts
931            .split_last()
932            .ok_or_else(|| CoreError::Internal("a pulled file has an empty path".to_string()))?;
933        let dir = dirfd::ensure_walk(&gw, parents).map_err(|e| fail(&label, e))?;
934        let mut file = dirfd::create_new(&dir, name).map_err(|e| fail(&label, e))?;
935        file.write_all(bytes)
936            .map_err(|e| CoreError::Internal(format!("cannot write {label}: {e}")))?;
937    }
938    Ok(())
939}
940
941/// Non-unix: std has no descriptor-relative primitives, so the replaced
942/// subtrees were checked for symlinks by [`scan_existing`] just before and
943/// the operations are path-based (the 22-01 residual for Windows).
944#[cfg(not(unix))]
945fn write_pulled_fs(
946    repo: &Path,
947    gateway: &str,
948    existing: Option<&Existing>,
949    source: &SourceTree,
950    manifest: Option<&str>,
951) -> Result<(), CoreError> {
952    use std::io::Write;
953
954    let io = |what: &Path, e: std::io::Error| {
955        CoreError::Internal(format!("cannot write {}: {e}", what.display()))
956    };
957    let gw = repo.join("gateways").join(gateway);
958    std::fs::create_dir_all(&gw).map_err(|e| io(&gw, e))?;
959    if let Some(existing) = existing {
960        for file in &existing.files {
961            let path = file.split('/').fold(gw.clone(), |acc, c| acc.join(c));
962            std::fs::remove_file(&path).map_err(|e| io(&path, e))?;
963        }
964        for dir_path in existing.dirs.iter().rev() {
965            let path = dir_path.split('/').fold(gw.clone(), |acc, c| acc.join(c));
966            std::fs::remove_dir(&path).map_err(|e| io(&path, e))?;
967        }
968    }
969    let create = |path: &Path, bytes: &[u8]| -> Result<(), CoreError> {
970        if let Some(parent) = path.parent() {
971            std::fs::create_dir_all(parent).map_err(|e| io(parent, e))?;
972        }
973        let mut file = std::fs::OpenOptions::new()
974            .write(true)
975            .create_new(true)
976            .open(path)
977            .map_err(|e| io(path, e))?;
978        file.write_all(bytes).map_err(|e| io(path, e))
979    };
980    if let Some(text) = manifest {
981        create(&gw.join("manifest.yaml"), text.as_bytes())?;
982    }
983    for (path, bytes) in &source.files {
984        let parts: Vec<&str> = path.components().collect();
985        if parts.iter().any(|c| !safe_component(c)) {
986            return Err(CoreError::deploy_tree_invalid(
987                format!("gateways/{gateway}/{path}"),
988                None,
989                None,
990                "has an unsafe path component; refused",
991            ));
992        }
993        let dest = parts.iter().fold(gw.clone(), |acc, c| acc.join(c));
994        create(&dest, bytes)?;
995    }
996    Ok(())
997}
998
999/// Options of `deploy verify`.
1000#[derive(Debug, Clone)]
1001pub struct VerifyOptions {
1002    /// How long the probes are polled before the verdict.
1003    pub probe_deadline: Duration,
1004    /// Time between two probe polls.
1005    pub probe_interval: Duration,
1006    /// How long after a scan completed the gateway log is trusted to hold its
1007    /// lines (CV1). Skipped when the gateway has logged since.
1008    pub log_settle: Duration,
1009}
1010
1011impl Default for VerifyOptions {
1012    fn default() -> Self {
1013        Self {
1014            probe_deadline: verify::PROBE_DEADLINE,
1015            probe_interval: verify::PROBE_INTERVAL,
1016            log_settle: Duration::from_millis(verify::LOG_SETTLE_MS.unsigned_abs()),
1017        }
1018    }
1019}
1020
1021/// The `ign deploy verify` result (the envelope's `data`).
1022#[derive(Debug, Clone, PartialEq, Serialize)]
1023pub struct DeployVerifyResult {
1024    /// The `--env` value.
1025    pub env: String,
1026    /// Gateway directory name from the target.
1027    pub gateway: String,
1028    /// Deployment mode from the target.
1029    pub mode: String,
1030    /// The node that was verified: `master` (the default), `backup` or
1031    /// `both`.
1032    pub node: String,
1033    /// The WebDev project the tag probes were read through.
1034    pub project: String,
1035    /// True when no check failed.
1036    pub ok: bool,
1037    /// `deploy_verify_failed` when `ok` is false, else null.
1038    pub slug: Option<&'static str>,
1039    /// The six checks in fixed order.
1040    pub checks: Vec<verify::Check>,
1041    /// Declared resources `read_back` compared.
1042    pub resources_checked: usize,
1043    /// Non-failing notes.
1044    pub warnings: Vec<String>,
1045    /// The effective tag provider exclusion set: byte-sorted, always holds
1046    /// `System`. Those providers were not read or checked.
1047    pub excluded_tag_providers: Vec<String>,
1048    /// The backup node's checks (`--node backup|both`, V2 shape with
1049    /// `budget_s` 0, `waited_s` 0 and `resync` null); absent for `master`.
1050    #[serde(skip_serializing_if = "Option::is_none")]
1051    pub backup: Option<BackupReport>,
1052    /// The secret-name check of each node `--node` ran (V7); absent for
1053    /// `master`.
1054    #[serde(skip_serializing_if = "Option::is_none")]
1055    pub secrets: Option<SecretNamesReport>,
1056}
1057
1058impl DeployVerifyResult {
1059    /// The red-verdict exit passthrough (Decision A), mirroring
1060    /// [`crate::actions::testing::TestingRunResult::failure_exit_code`]:
1061    /// `Some(6)` (target state) when a check failed, `None` otherwise. The
1062    /// envelope renders first; the binary decides the exit after.
1063    pub fn failure_exit_code(&self) -> Option<u8> {
1064        (!self.ok).then_some(6)
1065    }
1066}
1067
1068/// Most pages of log lines read before the answer is called truncated.
1069const LOG_PAGES_MAX: usize = 5;
1070
1071/// Verify the master gateway against `declared` (DPLN-07, DPLN-08, DPLN-09).
1072///
1073/// Reads only: `scan_status`, `deployment_mode`, `resource_list` or
1074/// `resource_singleton` per declared non-tag type, one read of the live files
1075/// through `reader`, the gateway log, and the tag probes through `project`'s
1076/// WebDev route. It acquires no scan-lock, triggers no scan and writes no
1077/// file. Evidence is gathered, then judged by [`verify::evaluate`].
1078///
1079/// A gateway that cannot be reached ([`CoreError::Network`]), any refusal of
1080/// the resource, scan or mode reads, and a transport failure return a
1081/// [`CoreError`]: there is nothing to judge. A refusal of the log or tag
1082/// source is evidence, not an error: that check fails with the refusal's slug
1083/// (never its text). A red verdict is a normal `Ok` result; the binary maps
1084/// it to exit 6 through [`DeployVerifyResult::failure_exit_code`].
1085pub async fn deploy_verify(
1086    declared: &Declared,
1087    project: &str,
1088    deploy: &dyn DeployApi,
1089    gateway: &dyn GatewayApi,
1090    reader: &dyn TreeReader,
1091    opts: &VerifyOptions,
1092) -> Result<DeployVerifyResult, CoreError> {
1093    let expected =
1094        verify::Expected::from_render(&declared.target.mode, &declared.outcome, &declared.manifest);
1095
1096    let scan = deploy.scan_status(ScanKind::Config).await?;
1097    let scan_read_at = std::time::Instant::now();
1098    let mode = normalize_deployment_mode(deploy.deployment_mode().await?);
1099    let read_back = gather_read_back(deploy, &expected).await?;
1100
1101    // The live files, once, over the read-only transport. Tag units are the
1102    // ones judged; reading is also what proves the host is reachable.
1103    let excluded = declared.excluded_tag_providers();
1104    let scope = Scope::from_manifest(&declared.manifest, &declared.target.mode, &excluded);
1105    let live = reader.read(&scope).await?;
1106    let planned = plan(&declared.outcome.tree, &live, None, &scope, &excluded);
1107    let files = planned
1108        .entries
1109        .iter()
1110        .filter(|entry| expected.tag_units.contains(&entry.key))
1111        .map(|entry| (entry.key.clone(), entry.status))
1112        .collect();
1113
1114    let logs = if scan.scan_active {
1115        // Its lines are not readable yet; the check reports "not observed".
1116        Ok(Vec::new())
1117    } else {
1118        settle_logs(gateway, &scan, scan_read_at, opts.log_settle).await?;
1119        read_logs(gateway, verify::log_window_start(&scan)).await?
1120    };
1121    let probes = if expected.probes.is_empty() {
1122        None
1123    } else {
1124        Some(poll_probes(gateway, project, &expected.probes, opts).await?)
1125    };
1126
1127    let observed = verify::ObservedState {
1128        scan,
1129        mode,
1130        read_back,
1131        files,
1132        logs,
1133        probes,
1134        unreadable: planned
1135            .unreadable
1136            .iter()
1137            .map(|unit| (unit.key.clone(), unit.declared))
1138            .collect(),
1139    };
1140    let report = verify::evaluate(&expected, &observed);
1141    Ok(DeployVerifyResult {
1142        env: declared.env.clone(),
1143        gateway: declared.target.gateway.clone(),
1144        mode: declared.target.mode.clone(),
1145        node: "master".to_string(),
1146        project: project.to_string(),
1147        ok: report.ok,
1148        slug: report.slug,
1149        checks: report.checks,
1150        resources_checked: report.resources_checked,
1151        warnings: report.warnings,
1152        excluded_tag_providers: excluded,
1153        backup: None,
1154        secrets: None,
1155    })
1156}
1157
1158/// Which node(s) `deploy verify --node` looks at (D-12, V6).
1159#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1160pub enum VerifyNode {
1161    /// The six master checks; the default and byte-identical to the verb
1162    /// before Phase 24.
1163    Master,
1164    /// Only the backup checks.
1165    Backup,
1166    /// The six master checks, then the backup checks.
1167    Both,
1168}
1169
1170impl VerifyNode {
1171    /// The word the flag and `data.node` use.
1172    pub fn as_str(self) -> &'static str {
1173        match self {
1174            VerifyNode::Master => "master",
1175            VerifyNode::Backup => "backup",
1176            VerifyNode::Both => "both",
1177        }
1178    }
1179}
1180
1181/// The master's surfaces `deploy verify --node master|both` reads through.
1182pub type VerifyMasterSurfaces<'a> = (&'a dyn DeployApi, &'a dyn GatewayApi);
1183
1184/// `deploy verify` over one node or both (D-12, V6).
1185///
1186/// `Master` is [`deploy_verify`] unchanged. `Backup` runs the backup checks
1187/// only: the redundancy check (`peerConnected` and not `syncPending`), the
1188/// mode, the REST read-back of the render, and the backup's files against the
1189/// master's live copy, read through `master_reader` (a `warn` when `backup`
1190/// has no file transport). `Both` runs the master's six checks, then the
1191/// backup's. Like `deploy_verify` it never waits, never resyncs and never
1192/// writes: the backup is reached through [`BackupNode`], which has no write
1193/// to express, and the master only through reads.
1194///
1195/// `master` is required for `Master` and `Both`, `backup` for `Backup` and
1196/// `Both`; a missing one is [`CoreError::InvalidInput`].
1197pub async fn deploy_verify_node(
1198    declared: &Declared,
1199    project: &str,
1200    node: VerifyNode,
1201    master: Option<VerifyMasterSurfaces<'_>>,
1202    master_reader: &dyn TreeReader,
1203    backup: Option<&BackupNode<'_>>,
1204    opts: &VerifyOptions,
1205) -> Result<DeployVerifyResult, CoreError> {
1206    let missing = |what: &str| CoreError::InvalidInput {
1207        reason: format!("deploy verify --node {} needs the {what}", node.as_str()),
1208    };
1209    let master_result = if node == VerifyNode::Backup {
1210        None
1211    } else {
1212        let (deploy, gateway) = master.ok_or_else(|| missing("master surfaces"))?;
1213        Some(deploy_verify(declared, project, deploy, gateway, master_reader, opts).await?)
1214    };
1215    if node == VerifyNode::Master {
1216        return master_result.ok_or_else(|| missing("master surfaces"));
1217    }
1218    let backup = backup.ok_or_else(|| missing("backup node"))?;
1219    let expected =
1220        verify::Expected::from_render(&declared.target.mode, &declared.outcome, &declared.manifest);
1221    let mut backup_warnings = Vec::new();
1222    let (tick, compared) = verify_backup_tick(
1223        declared,
1224        &expected,
1225        master_reader,
1226        backup,
1227        &mut backup_warnings,
1228    )
1229    .await;
1230    let report = verify_report(&tick);
1231
1232    let mut result = match master_result {
1233        // The master verify has not read the backup, so the backup's
1234        // read-back warnings are carried here exactly as `--node backup`
1235        // reports them (WR-03).
1236        Some(mut result) => {
1237            result.warnings.extend(backup_warnings);
1238            result
1239        }
1240        None => DeployVerifyResult {
1241            env: declared.env.clone(),
1242            gateway: declared.target.gateway.clone(),
1243            mode: declared.target.mode.clone(),
1244            node: String::new(),
1245            project: project.to_string(),
1246            ok: true,
1247            slug: None,
1248            checks: Vec::new(),
1249            resources_checked: compared,
1250            warnings: backup_warnings,
1251            excluded_tag_providers: declared.excluded_tag_providers(),
1252            backup: None,
1253            secrets: None,
1254        },
1255    };
1256    // The secret names each checked node holds, master first (V7). Names
1257    // only: `secret_names` never reads a value.
1258    let secrets = verify_secret_names(
1259        declared,
1260        master
1261            .map(|(deploy, _)| deploy)
1262            .filter(|_| node != VerifyNode::Backup),
1263        Some(backup),
1264    )
1265    .await?;
1266    let secrets_ok = secrets.nodes.iter().all(|n| n.status != CheckStatus::Fail);
1267    result.node = node.as_str().to_string();
1268    result.ok = result.ok && report.converged && secrets_ok;
1269    result.slug = (!result.ok).then_some(verify::VERIFY_FAILED_SLUG);
1270    result.backup = Some(report);
1271    result.secrets = Some(secrets);
1272    Ok(result)
1273}
1274
1275/// The secret-name check of `deploy verify --node` (D-13, V7): for each node
1276/// handed in (master first), one `secret_names` listing per provider the
1277/// render references, judged in [`SecretCheckMode::Verify`] so a provider the
1278/// render declares but a node lacks counts as `missing` (K8, verify side).
1279/// A listing that is refused for any reason other than "no provider" or
1280/// "cannot list" is an error: there is nothing to judge.
1281async fn verify_secret_names(
1282    declared: &Declared,
1283    master: Option<&dyn DeployApi>,
1284    backup: Option<&BackupNode<'_>>,
1285) -> Result<SecretNamesReport, CoreError> {
1286    let referenced = collect_referenced_secrets(&declared.outcome.tree);
1287    let declared_providers = declared_secret_providers(&declared.outcome.tree);
1288    let providers: BTreeSet<&str> = referenced.iter().map(|(p, _)| p.as_str()).collect();
1289    let mut nodes = Vec::with_capacity(2);
1290    if let Some(master) = master {
1291        let mut listings = BTreeMap::new();
1292        for provider in &providers {
1293            listings.insert(
1294                (*provider).to_string(),
1295                master.secret_names(provider).await?,
1296            );
1297        }
1298        nodes.push(check_secret_names(
1299            "master",
1300            &referenced,
1301            &listings,
1302            &declared_providers,
1303            SecretCheckMode::Verify,
1304        ));
1305    }
1306    if let Some(backup) = backup {
1307        let mut listings = BTreeMap::new();
1308        for provider in &providers {
1309            listings.insert(
1310                (*provider).to_string(),
1311                backup.secret_names(provider).await?,
1312            );
1313        }
1314        nodes.push(check_secret_names(
1315            "backup",
1316            &referenced,
1317            &listings,
1318            &declared_providers,
1319            SecretCheckMode::Verify,
1320        ));
1321    }
1322    Ok(SecretNamesReport::ordered(nodes))
1323}
1324
1325/// One look at the backup for `deploy verify`: its redundancy status, mode,
1326/// REST read-back and files. Every read that cannot be answered becomes an
1327/// `unreachable` check carrying the error code (never the gateway's words);
1328/// verify has no pre-apply sample, so the sync timestamp is recorded, not
1329/// compared. Returns the tick and the number of resources the read-back
1330/// compared.
1331async fn verify_backup_tick(
1332    declared: &Declared,
1333    expected: &verify::Expected,
1334    master_reader: &dyn TreeReader,
1335    backup: &BackupNode<'_>,
1336    warnings: &mut Vec<String>,
1337) -> (TickOutcome, usize) {
1338    let signals = match backup.status().await {
1339        Ok(status) => Ok(BackupSignals::from_status(None, &status)),
1340        Err(error) => Err(error.code().to_string()),
1341    };
1342    let mode = match backup.deployment_mode().await {
1343        Ok(mode) => mode_backup_check(expected, mode.as_deref()),
1344        Err(error) => unreachable_check(BackupCheckId::Mode, error.code()),
1345    };
1346    let mut compared = 0;
1347    let read_back = match backup.read_back(expected).await {
1348        Ok(read_back) => {
1349            let (check, count) = read_back_backup_check(expected, &read_back, warnings);
1350            compared = count;
1351            check
1352        }
1353        Err(error) => unreachable_check(BackupCheckId::ReadBack, error.code()),
1354    };
1355    let (files, files_checked) =
1356        verify_backup_files(declared, master_reader, backup, warnings).await;
1357    (
1358        TickOutcome {
1359            signals,
1360            exhausted: false,
1361            content: Some(vec![mode, read_back, files]),
1362            files_checked,
1363            stable_ticks: 0,
1364        },
1365        compared,
1366    )
1367}
1368
1369/// The backup's files against the master's live copy of every rendered path.
1370/// Nothing was deleted by verify, so no path is expected absent. A rendered
1371/// unit either node cannot read fails the check as `unreadable`; any other
1372/// unreadable unit is a warning (quick-261029-ms3).
1373async fn verify_backup_files(
1374    declared: &Declared,
1375    master_reader: &dyn TreeReader,
1376    backup: &BackupNode<'_>,
1377    warnings: &mut Vec<String>,
1378) -> (BackupCheck, usize) {
1379    let scope = Scope::from_manifest(
1380        &declared.manifest,
1381        &declared.target.mode,
1382        &declared.excluded_tag_providers(),
1383    );
1384    let backup_files = match backup.read_files(&scope).await {
1385        // A backup with no file transport cannot be asked.
1386        None => return (files_skipped_check(), 0),
1387        Some(Ok(observed)) => observed,
1388        Some(Err(error)) => {
1389            return (
1390                unreachable_check(BackupCheckId::Files, &format!("backup {}", error.code())),
1391                0,
1392            );
1393        }
1394    };
1395    let backup_unreadable = unreadable_units(&backup_files);
1396    let backup_files = effective_files(backup_files);
1397    let master_files = match master_reader.read(&scope).await {
1398        Ok(observed) => observed,
1399        Err(error) => {
1400            return (
1401                unreachable_check(BackupCheckId::Files, &format!("master {}", error.code())),
1402                0,
1403            );
1404        }
1405    };
1406    let master_unreadable = unreadable_units(&master_files);
1407    let master_files = effective_files(master_files);
1408    let present: BTreeSet<_> = declared.outcome.tree.files.keys().cloned().collect();
1409    let absent = BTreeSet::new();
1410    let mut comparison = compare_files(&master_files, &backup_files, &present, &absent);
1411    exempt_unreadable(
1412        &mut comparison,
1413        &present,
1414        &absent,
1415        &[
1416            ("master", &master_unreadable),
1417            ("backup", &backup_unreadable),
1418        ],
1419    );
1420    warnings.extend(unreadable_warnings(&comparison));
1421    (files_check(&comparison), comparison.checked)
1422}
1423
1424/// Read each declared, comparable resource from the REST effective view:
1425/// one list per named type, one singleton read per singleton. An absent
1426/// resource (or a type whose route answers 404) is `Missing`; any other
1427/// refusal is an error.
1428pub(crate) async fn gather_read_back(
1429    deploy: &dyn DeployApi,
1430    expected: &verify::Expected,
1431) -> Result<BTreeMap<String, verify::Remote>, CoreError> {
1432    let classify = |record: crate::client::config_resources::ResourceRecord| {
1433        if record.collection.as_deref() == Some(verify::SYSTEM_COLLECTION) {
1434            verify::Remote::System
1435        } else {
1436            verify::Remote::Config(record.config)
1437        }
1438    };
1439    let mut lists: BTreeMap<(String, String), Option<Vec<_>>> = BTreeMap::new();
1440    let mut out = BTreeMap::new();
1441    for res in &expected.resources {
1442        let remote = match &res.name {
1443            None => match deploy.resource_singleton(&res.module, &res.ty, None).await {
1444                Ok(record) => classify(record),
1445                Err(CoreError::NotFound { .. }) => verify::Remote::Missing,
1446                Err(e) => return Err(e),
1447            },
1448            Some(name) => {
1449                let type_key = (res.module.clone(), res.ty.clone());
1450                if !lists.contains_key(&type_key) {
1451                    let items = match deploy
1452                        .resource_list(&res.module, &res.ty, &ListQuery::default())
1453                        .await
1454                    {
1455                        Ok(envelope) => Some(envelope.items),
1456                        Err(CoreError::NotFound { .. }) => None,
1457                        Err(e) => return Err(e),
1458                    };
1459                    lists.insert(type_key.clone(), items);
1460                }
1461                lists
1462                    .get(&type_key)
1463                    .and_then(Option::as_ref)
1464                    .and_then(|items| items.iter().find(|r| r.name.as_deref() == Some(name)))
1465                    .map_or(verify::Remote::Missing, |r| classify(r.clone()))
1466            }
1467        };
1468        out.insert(res.key.clone(), remote);
1469    }
1470    Ok(out)
1471}
1472
1473/// Wait until an empty answer from the log means something (CV1: a line is
1474/// readable up to about 1.5 s after the scan completed). The wait is skipped
1475/// when the gateway has logged anything stamped after the settle point.
1476async fn settle_logs(
1477    gateway: &dyn GatewayApi,
1478    scan: &ScanStatus,
1479    scan_read_at: std::time::Instant,
1480    settle: Duration,
1481) -> Result<(), CoreError> {
1482    if settle.is_zero() {
1483        return Ok(());
1484    }
1485    let settle_ms = i64::try_from(settle.as_millis()).unwrap_or(i64::MAX);
1486    let probe = LogQuery {
1487        start_time: Some(scan.last_scan_timestamp.saturating_add(settle_ms)),
1488        limit: 1,
1489        ..LogQuery::default()
1490    };
1491    match gateway.logs(&probe).await {
1492        Ok(envelope) if !envelope.items.is_empty() => return Ok(()),
1493        Err(e @ CoreError::Network { .. }) => return Err(e),
1494        // An unreadable log is reported by the real read that follows.
1495        Ok(_) | Err(_) => {}
1496    }
1497    tokio::time::sleep(settle.saturating_sub(scan_read_at.elapsed())).await;
1498    Ok(())
1499}
1500
1501/// The WARN-or-higher log lines since `start_time` (CV1: no `logger` filter,
1502/// no `endTime`; matched client-side). The inner `Err` is the slug of why
1503/// they could not be read.
1504async fn read_logs(
1505    gateway: &dyn GatewayApi,
1506    start_time: i64,
1507) -> Result<Result<Vec<verify::LogLine>, String>, CoreError> {
1508    let mut lines = Vec::new();
1509    let mut offset = 0i64;
1510    for _ in 0..LOG_PAGES_MAX {
1511        let query = LogQuery {
1512            start_time: Some(start_time),
1513            min_level: Some("WARN".to_string()),
1514            limit: DEFAULT_LOG_LIMIT,
1515            offset,
1516            ..LogQuery::default()
1517        };
1518        match gateway.logs(&query).await {
1519            Ok(envelope) => {
1520                let count = i64::try_from(envelope.items.len()).unwrap_or(i64::MAX);
1521                lines.extend(envelope.items.into_iter().map(|e| verify::LogLine {
1522                    timestamp: e.timestamp,
1523                    logger: e.logger_name,
1524                    message: e.message,
1525                }));
1526                if count < DEFAULT_LOG_LIMIT {
1527                    return Ok(Ok(lines));
1528                }
1529                offset += count;
1530            }
1531            Err(e @ CoreError::Network { .. }) => return Err(e),
1532            Err(e) => return Ok(Err(e.code().to_string())),
1533        }
1534    }
1535    Ok(Err("logs_truncated".to_string()))
1536}
1537
1538/// Read `paths` through the WebDev `tags` route every `probe_interval` until
1539/// every one is Good or `probe_deadline` passed; the last reading is judged.
1540/// Values are dropped at once. The inner `Err` is the slug of why the route
1541/// could not be read.
1542async fn poll_probes(
1543    gateway: &dyn GatewayApi,
1544    project: &str,
1545    paths: &[String],
1546    opts: &VerifyOptions,
1547) -> Result<Result<Vec<verify::ProbeReading>, String>, CoreError> {
1548    let interval = opts.probe_interval.max(Duration::from_millis(1));
1549    let started = tokio::time::Instant::now();
1550    loop {
1551        let readings: Vec<verify::ProbeReading> =
1552            match crate::actions::tags::tags_read(gateway, project, paths).await {
1553                Ok(result) => result
1554                    .results
1555                    .into_iter()
1556                    .enumerate()
1557                    .map(|(i, row)| verify::ProbeReading {
1558                        // A row that does not echo its path is the one asked for.
1559                        path: if row.path.is_empty() {
1560                            paths.get(i).cloned().unwrap_or_default()
1561                        } else {
1562                            row.path
1563                        },
1564                        quality: row.quality,
1565                    })
1566                    .collect(),
1567                Err(e @ CoreError::Network { .. }) => return Err(e),
1568                Err(e) => return Ok(Err(e.code().to_string())),
1569            };
1570        let all_good = paths.iter().all(|path| {
1571            readings
1572                .iter()
1573                .any(|r| &r.path == path && r.quality.starts_with("Good"))
1574        });
1575        if all_good || started.elapsed() + interval > opts.probe_deadline {
1576            return Ok(Ok(readings));
1577        }
1578        tokio::time::sleep(interval).await;
1579    }
1580}
1581
1582#[cfg(test)]
1583mod tests {
1584    use super::*;
1585
1586    /// A repo whose `gateways/g/resources/` holds one file the pull replaces.
1587    fn repo_with_existing() -> (tempfile::TempDir, Existing) {
1588        let repo = tempfile::TempDir::new().expect("repo");
1589        let dir = repo.path().join("gateways/g/resources/ignition/x");
1590        std::fs::create_dir_all(&dir).expect("dirs");
1591        std::fs::write(dir.join("keep.json"), b"{\"keep\": true}").expect("file");
1592        let existing = Existing {
1593            files: vec!["resources/ignition/x/keep.json".to_string()],
1594            dirs: ["resources", "resources/ignition", "resources/ignition/x"]
1595                .iter()
1596                .map(|d| d.to_string())
1597                .collect(),
1598        };
1599        (repo, existing)
1600    }
1601
1602    fn snapshot(root: &Path) -> BTreeMap<PathBuf, Vec<u8>> {
1603        fn walk(dir: &Path, out: &mut BTreeMap<PathBuf, Vec<u8>>) {
1604            for entry in std::fs::read_dir(dir).expect("read_dir") {
1605                let path = entry.expect("entry").path();
1606                if path.is_dir() {
1607                    out.insert(path.clone(), Vec::new());
1608                    walk(&path, out);
1609                } else {
1610                    out.insert(path.clone(), std::fs::read(&path).expect("read"));
1611                }
1612            }
1613        }
1614        let mut out = BTreeMap::new();
1615        walk(root, &mut out);
1616        out
1617    }
1618
1619    /// CR-01: a pulled tree that cannot be written is refused before the first
1620    /// delete, so the existing tree is left exactly as it was.
1621    fn assert_refused_untouched(files: &[(&str, &str)], needle: &str) {
1622        let (repo, existing) = repo_with_existing();
1623        let before = snapshot(repo.path());
1624        let source = SourceTree::from_files(files.iter().copied()).expect("source");
1625        let err =
1626            write_pulled(repo.path(), "g", Some(&existing), &source, None).expect_err("refused");
1627        assert_eq!(err.code(), "deploy_tree_invalid", "{err}");
1628        assert!(err.to_string().contains(needle), "{needle}: {err}");
1629        assert_eq!(
1630            snapshot(repo.path()),
1631            before,
1632            "a refused pull must not delete the existing tree"
1633        );
1634    }
1635
1636    #[test]
1637    fn unsafe_component_is_refused_before_any_delete() {
1638        assert_refused_untouched(
1639            &[
1640                ("resources/ignition/x/fine.json", "{}"),
1641                ("resources/ignition/x/bad\nname.json", "{}"),
1642            ],
1643            "unsafe path component",
1644        );
1645    }
1646
1647    #[test]
1648    fn case_fold_collision_is_refused_before_any_delete() {
1649        assert_refused_untouched(
1650            &[
1651                ("resources/ignition/x/Foo.json", "{}"),
1652                ("resources/ignition/x/foo.json", "{}"),
1653            ],
1654            "case is folded",
1655        );
1656    }
1657
1658    #[test]
1659    fn file_and_directory_clash_is_refused_before_any_delete() {
1660        assert_refused_untouched(
1661            &[
1662                ("resources/ignition/x", "{}"),
1663                ("resources/ignition/x/a.json", "{}"),
1664            ],
1665            "both a file and a directory",
1666        );
1667    }
1668
1669    #[test]
1670    fn a_valid_tree_replaces_the_existing_one() {
1671        let (repo, existing) = repo_with_existing();
1672        let source =
1673            SourceTree::from_files([("resources/ignition/x/new.json", "{}")]).expect("source");
1674        write_pulled(repo.path(), "g", Some(&existing), &source, None).expect("written");
1675        let dir = repo.path().join("gateways/g/resources/ignition/x");
1676        assert!(!dir.join("keep.json").exists());
1677        assert!(dir.join("new.json").is_file());
1678    }
1679}