Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /als-apg_osprey /src /osprey /cli /validate_cmd.py
| """The ``osprey validate`` verb — check a deployment's source without building. | |
| Validation is the cheap half of a build: resolve the ``extends:`` chain, check | |
| the convention directories, the ``data:`` tree, service templates, lifecycle | |
| steps and env vars, then lint the web stack against the config a build would | |
| render. Nothing is written and nothing is started, which is what makes this the | |
| verb a CI pipeline runs first and an operator runs after every profile edit. | |
| Zero-argument is the normal spelling: the repo enclosing the working directory | |
| is the thing being validated, found by the same walk every repo-scoped command | |
| uses. The optional path argument stays for the one profile that is not a repo | |
| root — a persona delta under ``personas/``, which is a profile in its own right | |
| and has its own way to be wrong. | |
| ``osprey profile validate`` is the same verb spelled as part of the ``profile`` | |
| noun group, and both spellings are documented surface. Only the INTERFACE | |
| differs — a required TARGET there, an optional one plus ``--repo`` here — so the | |
| check itself lives once, in :func:`check_profile_file`, and both commands | |
| resolve a profile file and hand it over. The two used to be copy-pasted bodies | |
| and had already drifted into two failure headers, two next-step lines and two | |
| wordings of the same refusal. | |
| """ | |
| from __future__ import annotations | |
| from pathlib import Path | |
| import click | |
| from osprey.errors import BuildProfileError | |
| from .output import note, report, section | |
| from .repo_resolver import PROFILE_FILENAME, find_repo_root, repo_option | |
| from .styles import Styles | |
| def profile_file_at(target: Path) -> Path: | |
| """Return the profile file *target* names, directly or as its directory. | |
| Both spellings are accepted because both are things an operator has on | |
| hand: a directory (the repo root, or any directory holding a | |
| ``profile.yml``) and an explicit path to a profile file (how persona deltas | |
| are named, since a delta is a file beside its siblings, not a directory). | |
| Raises: | |
| click.UsageError: When *target* is a directory with no profile.yml. | |
| """ | |
| if target.is_dir(): | |
| candidate = target / PROFILE_FILENAME | |
| if not candidate.is_file(): | |
| raise click.UsageError( | |
| f"No {PROFILE_FILENAME} in {target}. Pass the profile file directly, or " | |
| f"create a deployment repo with `osprey init {target} --preset <NAME>`." | |
| ) | |
| return candidate.resolve() | |
| return target.resolve() | |
| def check_profile_file(profile_file: Path, *, drift: str = "error") -> None: | |
| """Validate the profile at *profile_file* and print the verdict. | |
| The whole of what both spellings of the verb do once a profile file has been | |
| named: resolve the ``extends:`` chain, run the profile's own consistency | |
| check, lint the declared web stack against the config a build would render, | |
| compare a materialized profile with the preset it came from, and report. | |
| The host-variant overlay is applied first, exactly as ``osprey build`` | |
| applies it. Without that, this verb would judge a document no host builds: | |
| a repo whose ``.env.variant`` selects ``profiles/teststand.yml`` would be | |
| called valid on the strength of the tracked profile while the render the | |
| build produces is the merged one — and the operator would find out at build | |
| time, from the verb they ran validate to stay ahead of. | |
| Args: | |
| profile_file: An existing profile file — a repo's ``profile.yml`` or a | |
| persona delta. | |
| drift: ``error`` refuses a profile that differs STRUCTURALLY from its | |
| preset in a place no marker comment claims — a key, block or list | |
| member one document has and the other has not; ``warn`` prints | |
| those places and passes. A value difference on a key both carry is | |
| printed either way and refuses under neither. | |
| Raises: | |
| click.UsageError: With every accumulated problem, so the caller exits 2. | |
| """ | |
| from .build_profile import resolve_build_profile | |
| from .build_profile_deploy import ( | |
| deploy_aware_config_errors, | |
| deploy_aware_config_warnings, | |
| limits_block_errors, | |
| ) | |
| from .build_profile_drift import MARKER_REACH, preset_drift_report | |
| from .build_profile_panels import bar_items_selection_warnings | |
| from .variant_selection import VARIANT_DIRNAME, VariantSelection, resolve_variant_selection | |
| variant = VariantSelection(name=None, path=None) | |
| try: | |
| # Only a repo root has a host variant. A persona delta named directly is | |
| # a file among its siblings, not a deployment, and the directory holding | |
| # it carries no setting to read. | |
| if profile_file.name == PROFILE_FILENAME: | |
| variant = resolve_variant_selection(profile_file.parent) | |
| overlays = (variant.path,) if variant.path is not None else () | |
| build_profile, profile_dir = resolve_build_profile(profile_file, None, overlays) | |
| # Named explicitly rather than left to resolution's internals: this | |
| # command exists to run exactly this check, so it must not become a | |
| # no-op if resolution ever stops validating on its own. | |
| build_profile.validate(profile_dir) | |
| except BuildProfileError as e: | |
| raise click.UsageError(str(e)) from e | |
| # The multi-user web stack the profile declares, judged on the config the | |
| # build would render — the `config:` block with the `deploy:` block's | |
| # contributions applied. Checked from the command rather than inside | |
| # `validate()`, which also runs during profile resolution — see | |
| # `lint_profile_config`. | |
| # The profile's own directory, not the working directory: a catalog entry's | |
| # `build_profile: personas/<name>.yml` is named relative to the profile it | |
| # sits beside. Passing it is what makes `osprey validate --repo <path>`, and | |
| # a run from any subdirectory, read the same deltas a run from the repo root | |
| # reads — without it the persona half of the lint silently sees nothing and | |
| # reports the profile valid. | |
| profile_root = profile_file.parent | |
| web_errors = deploy_aware_config_errors( | |
| build_profile.deploy, build_profile.config, profile_root=profile_root | |
| ) | |
| # A sibling call, not a line inside `deploy_aware_config_errors`: that | |
| # function judges the multi-user web stack against the deploy block, and | |
| # `osprey build` runs it over the RENDERED config as well. A per-type | |
| # `limits_checking` block is neither web-stack business nor a render fact — | |
| # it is a claim the `config:` block makes about a posture, checked once, | |
| # here and in the build's own profile-side pass. Folded into the web lint it | |
| # would be asked twice during a build and refuse the same profile twice. | |
| web_errors = [*web_errors, *limits_block_errors(build_profile.config)] | |
| if web_errors: | |
| # "Profile validation failed", not "Build profile ...": the success line | |
| # below says "Profile is valid", and `BuildProfile.validate` already owns | |
| # the "Build profile validation failed" header for its own errors. | |
| raise click.UsageError("Profile validation failed:\n - " + "\n - ".join(web_errors)) | |
| # Advisory findings are printed rather than raised: they name real exposures | |
| # (a privileged terminal with no login wall — `auth.method: token` or `none`) that are not | |
| # mistakes every deployment has made, so they must not fail a CI gate — but | |
| # a finding nobody prints is a finding nobody has. | |
| for warning in ( | |
| *deploy_aware_config_warnings( | |
| build_profile.deploy, build_profile.config, profile_root=profile_root | |
| ), | |
| # The same line `osprey build` prints for a `web.bar_items` entry the | |
| # served default drops because its panel is not selected. | |
| *bar_items_selection_warnings(build_profile.config, build_profile.web_panels), | |
| ): | |
| note(f"⚠ {warning}") | |
| # A materialized profile against the preset it was written from. Only a | |
| # profile `osprey init` stamped has a preset to be compared with; a | |
| # hand-written one has nothing to drift from. | |
| # | |
| # Only the STRUCTURAL findings refuse, because only they name the silent | |
| # kind of failure — a key, a block or a member the preset gained and this | |
| # copy never did builds green forever, and nothing says so. A value | |
| # difference on a key both documents carry is the operator doing what the | |
| # emitted profile invites, so it is printed and never refused; the cost of | |
| # that ruling is that a preset changing a default in a newer OSPREY arrives | |
| # as a note. Either way a facility fact is claimed once, with a marker | |
| # comment, and never asked about again. | |
| if build_profile.provenance is not None: | |
| try: | |
| report_card = preset_drift_report(profile_file, build_profile.provenance) | |
| except BuildProfileError as e: | |
| raise click.UsageError(str(e)) from e | |
| if report_card.note: | |
| note(report_card.note) | |
| for stale in report_card.stale_markers: | |
| note(f"⚠ {stale}") | |
| for finding in report_card.notes: | |
| note(f"⚠ preset drift: {finding.render()}") | |
| refusals = report_card.refusals | |
| if refusals and drift == "error": | |
| tag = build_profile.provenance.deviation_marker | |
| raise click.UsageError( | |
| f"Profile differs from preset {report_card.preset} in {len(refusals)} " | |
| f"place(s) no marker claims:\n - " | |
| + "\n - ".join(finding.render() for finding in refusals) | |
| + f"\nA deliberate difference is marked with a `# {tag}: <why>` comment " | |
| f"within {MARKER_REACH} lines above its line, or one naming the key or " | |
| f"member when the profile has no line for it. `--drift=warn` reports " | |
| f"without refusing." | |
| ) | |
| for finding in refusals: | |
| note(f"⚠ preset drift: {finding.render()}") | |
| # Before the verdict, and worded as `osprey build` words it: the reader has | |
| # to know WHICH document was judged before being told it is fine. | |
| if variant.selected: | |
| note( | |
| f"host variant {variant.name} " | |
| f"({VARIANT_DIRNAME}/{variant.name}.yml over {PROFILE_FILENAME})" | |
| ) | |
| report(f"✓ Profile is valid: {profile_file}", style=Styles.SUCCESS) | |
| rows: list[tuple[str, object]] = [("Name", build_profile.name)] | |
| # Named separately because "valid" says nothing about whether the deploy | |
| # coordinates were read at all: a profile that omits the block and one whose | |
| # block checked out otherwise print the same line. | |
| if build_profile.deploy is not None: | |
| deploy = build_profile.deploy | |
| rows.append(("Deploy", f"{deploy.ci} CI → {deploy.host.user}@{deploy.host.name}")) | |
| section("", rows) | |
| report("") | |
| report("Next steps:") | |
| note("1. Render the deployment: osprey build") | |
| note("2. Re-run this command after editing the profile") | |
| def validate(target: Path | None, drift: str, repo: Path | None) -> None: | |
| """Check the deployment profile without building. | |
| With no argument, validates the deployment repo enclosing the working | |
| directory. TARGET names a different profile to check instead — a persona | |
| delta file under personas/, or a directory holding a profile.yml. | |
| Resolves extends: chains and runs the full consistency check — convention | |
| directories, the data: tree, service templates, lifecycle steps, env vars — | |
| then lints the declared web stack against the config a build would render. | |
| Every problem found is reported, not just the first. | |
| A profile `osprey init` wrote is also compared with the preset it came from, | |
| persona deltas included. A key, block or list member one of them has and the | |
| other has not is refused unless a marker comment (`# DEVIATION: <why>`, tag | |
| set by provenance.deviation_marker) claims it; --drift=warn reports those | |
| and passes. A key both carry with different values is always just reported. | |
| Judges what this host builds: when .env.variant selects an overlay under | |
| profiles/, that overlay is merged in first, and named in the output. | |
| Exits 0 when the profile is valid, 2 with the accumulated errors when it is | |
| not, so a CI job can gate on it. | |
| Examples: | |
| \b | |
| $ osprey validate | |
| $ osprey validate personas/reader.yml | |
| $ osprey validate --repo ~/als-assistant | |
| """ | |
| if target is not None and repo is not None: | |
| raise click.UsageError( | |
| "--repo and TARGET both name what to validate. Drop one: --repo picks " | |
| "the deployment repo, TARGET picks a specific profile file." | |
| ) | |
| if target is not None: | |
| profile_file = profile_file_at(target) | |
| else: | |
| # The resolver guarantees the marker is a file at the root it returns, | |
| # so there is no second existence check to make here. | |
| profile_file = find_repo_root(repo) / PROFILE_FILENAME | |
| check_profile_file(profile_file, drift=drift) | |
Xet Storage Details
- Size:
- 13.5 kB
- Xet hash:
- ac6708b14191748a81af0245f887fb176efe7dcf52021822648f34364326016a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.