datadouble 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datadouble/__init__.py +10 -0
- datadouble/announce.py +380 -0
- datadouble/anonymize.py +165 -0
- datadouble/api.py +1159 -0
- datadouble/boundary.py +47 -0
- datadouble/catalog.py +677 -0
- datadouble/channels.py +62 -0
- datadouble/ci.py +1357 -0
- datadouble/cli.py +940 -0
- datadouble/closure.py +712 -0
- datadouble/engine.py +435 -0
- datadouble/files.py +78 -0
- datadouble/images.py +265 -0
- datadouble/jailer.py +403 -0
- datadouble/names.py +114 -0
- datadouble/notify.py +262 -0
- datadouble/objects.py +195 -0
- datadouble/outcome.py +27 -0
- datadouble/pii.py +109 -0
- datadouble/planner.py +1492 -0
- datadouble/programs.py +68 -0
- datadouble/promote.py +190 -0
- datadouble/prune.py +258 -0
- datadouble/py.typed +0 -0
- datadouble/redaction.py +131 -0
- datadouble/registry.py +128 -0
- datadouble/replicate.py +562 -0
- datadouble/resolve.py +201 -0
- datadouble/run.py +1075 -0
- datadouble/runtime.py +154 -0
- datadouble/secrets.py +57 -0
- datadouble/settings.py +81 -0
- datadouble/signing.py +98 -0
- datadouble/spec.py +1590 -0
- datadouble/staging.py +346 -0
- datadouble/storage.py +222 -0
- datadouble/strict.py +498 -0
- datadouble/subset.py +406 -0
- datadouble/verify.py +171 -0
- datadouble/versions.py +25 -0
- datadouble-0.0.3.dist-info/METADATA +36 -0
- datadouble-0.0.3.dist-info/RECORD +45 -0
- datadouble-0.0.3.dist-info/WHEEL +4 -0
- datadouble-0.0.3.dist-info/entry_points.txt +12 -0
- datadouble-0.0.3.dist-info/licenses/LICENSE +21 -0
datadouble/api.py
ADDED
|
@@ -0,0 +1,1159 @@
|
|
|
1
|
+
# Copyright (c) 2026- Tight Line LLC
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""The JSON interface.
|
|
5
|
+
|
|
6
|
+
Every Datadouble operation is a JSON request answered by a JSON response. The
|
|
7
|
+
command-line interface, the CI/CD adapters and the agent skills only build
|
|
8
|
+
requests for this module, so behavior cannot differ between them.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import platform
|
|
12
|
+
from contextlib import ExitStack
|
|
13
|
+
from enum import StrEnum
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Annotated, Literal, assert_never
|
|
16
|
+
|
|
17
|
+
import yaml
|
|
18
|
+
from pydantic import (
|
|
19
|
+
BaseModel,
|
|
20
|
+
ConfigDict,
|
|
21
|
+
Field,
|
|
22
|
+
JsonValue,
|
|
23
|
+
TypeAdapter,
|
|
24
|
+
ValidationError,
|
|
25
|
+
model_validator,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
from datadouble import __version__, jailer
|
|
29
|
+
from datadouble.announce import AnnounceError, announce, withdraw
|
|
30
|
+
from datadouble.anonymize import AnonymizeError
|
|
31
|
+
from datadouble.boundary import BoundaryError, require_inside
|
|
32
|
+
from datadouble.catalog import (
|
|
33
|
+
Catalog,
|
|
34
|
+
CatalogError,
|
|
35
|
+
CatalogLock,
|
|
36
|
+
LockChange,
|
|
37
|
+
changes,
|
|
38
|
+
describe_changes,
|
|
39
|
+
read_lock,
|
|
40
|
+
write_lock,
|
|
41
|
+
)
|
|
42
|
+
from datadouble.channels import Channel
|
|
43
|
+
from datadouble.ci import (
|
|
44
|
+
IMAGE,
|
|
45
|
+
Agent,
|
|
46
|
+
CiBridge,
|
|
47
|
+
CiError,
|
|
48
|
+
JobKind,
|
|
49
|
+
PipelineOptions,
|
|
50
|
+
Report,
|
|
51
|
+
Side,
|
|
52
|
+
SpecSource,
|
|
53
|
+
SpecSourceKind,
|
|
54
|
+
pipeline,
|
|
55
|
+
spec_path,
|
|
56
|
+
)
|
|
57
|
+
from datadouble.engine import EngineError, RequirementError
|
|
58
|
+
from datadouble.images import ImageError
|
|
59
|
+
from datadouble.jailer import JailerError
|
|
60
|
+
from datadouble.names import Generation, GroupName
|
|
61
|
+
from datadouble.notify import Notification, Post, post, recall, recovered, remember, send
|
|
62
|
+
from datadouble.outcome import ExitCode, Outcome
|
|
63
|
+
from datadouble.planner import (
|
|
64
|
+
Plan,
|
|
65
|
+
PlanError,
|
|
66
|
+
PlanOptions,
|
|
67
|
+
SourceCatalog,
|
|
68
|
+
collisions,
|
|
69
|
+
merge,
|
|
70
|
+
plan,
|
|
71
|
+
)
|
|
72
|
+
from datadouble.promote import Promoted, PromoteError, promote
|
|
73
|
+
from datadouble.prune import PruneError, PruneResult, PruneTarget, prune
|
|
74
|
+
from datadouble.registry import Registry, RegistryError, installed
|
|
75
|
+
from datadouble.replicate import (
|
|
76
|
+
Copy,
|
|
77
|
+
Options,
|
|
78
|
+
ReplicateError,
|
|
79
|
+
ReplicateResult,
|
|
80
|
+
claim,
|
|
81
|
+
copies_of,
|
|
82
|
+
release,
|
|
83
|
+
replicate,
|
|
84
|
+
)
|
|
85
|
+
from datadouble.run import Run, RunError, RunOptions, RunResult, Stage, catalogs
|
|
86
|
+
from datadouble.runtime import ContainerError
|
|
87
|
+
from datadouble.secrets import SecretError
|
|
88
|
+
from datadouble.settings import SettingsError
|
|
89
|
+
from datadouble.signing import SigningError
|
|
90
|
+
from datadouble.spec import (
|
|
91
|
+
ImageFormat,
|
|
92
|
+
Spec,
|
|
93
|
+
SpecError,
|
|
94
|
+
SpecLayer,
|
|
95
|
+
Strategy,
|
|
96
|
+
StrictOverrides,
|
|
97
|
+
load_spec,
|
|
98
|
+
read_spec,
|
|
99
|
+
spec_schema,
|
|
100
|
+
)
|
|
101
|
+
from datadouble.staging import Fetched, StagingError, activate, fetch
|
|
102
|
+
from datadouble.storage import StoreError
|
|
103
|
+
from datadouble.strict import StrictReport
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class Command(StrEnum):
|
|
107
|
+
"""Every command the JSON interface accepts."""
|
|
108
|
+
|
|
109
|
+
VERSION = "version"
|
|
110
|
+
SCHEMA = "schema"
|
|
111
|
+
SPEC = "spec"
|
|
112
|
+
LOCK = "lock"
|
|
113
|
+
IMPORT_JAILER = "import-jailer"
|
|
114
|
+
PLAN = "plan"
|
|
115
|
+
RUN = "run"
|
|
116
|
+
PROMOTE = "promote"
|
|
117
|
+
FETCH = "fetch"
|
|
118
|
+
ACTIVATE = "activate"
|
|
119
|
+
REPLICATE = "replicate"
|
|
120
|
+
CLAIM = "claim"
|
|
121
|
+
RELEASE = "release"
|
|
122
|
+
ANNOUNCE = "announce"
|
|
123
|
+
COPIES = "copies"
|
|
124
|
+
PRUNE = "prune"
|
|
125
|
+
CI_INIT = "ci.init"
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class ErrorCode(StrEnum):
|
|
129
|
+
"""Machine-readable reasons a request failed."""
|
|
130
|
+
|
|
131
|
+
INVALID_REQUEST = "invalid_request"
|
|
132
|
+
FILE_FAILED = "file_failed"
|
|
133
|
+
INVALID_SPEC = "invalid_spec"
|
|
134
|
+
INVALID_SETTINGS = "invalid_settings"
|
|
135
|
+
CATALOG_LOCK = "catalog_lock"
|
|
136
|
+
PLAN_FAILED = "plan_failed"
|
|
137
|
+
ADAPTER_MISSING = "adapter_missing"
|
|
138
|
+
UNRESOLVED_REFERENCE = "unresolved_reference"
|
|
139
|
+
ENGINE_FAILED = "engine_failed"
|
|
140
|
+
INVALID_SUBSET = "invalid_subset"
|
|
141
|
+
CONTAINER_FAILED = "container_failed"
|
|
142
|
+
ANONYMIZATION_FAILED = "anonymization_failed"
|
|
143
|
+
STORE_FAILED = "store_failed"
|
|
144
|
+
RUN_FAILED = "run_failed"
|
|
145
|
+
PROMOTE_REFUSED = "promote_refused"
|
|
146
|
+
SIGNATURE_FAILED = "signature_failed"
|
|
147
|
+
STAGING_FAILED = "staging_failed"
|
|
148
|
+
REPLICATE_FAILED = "replicate_failed"
|
|
149
|
+
ANNOUNCE_FAILED = "announce_failed"
|
|
150
|
+
PRUNE_FAILED = "prune_failed"
|
|
151
|
+
OUTSIDE_BOUNDARY = "outside_boundary"
|
|
152
|
+
CI_FAILED = "ci_failed"
|
|
153
|
+
REQUIREMENT_UNMET = "requirement_unmet"
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class _Message(BaseModel):
|
|
157
|
+
"""Base for every request and response: strict, immutable, no extra fields."""
|
|
158
|
+
|
|
159
|
+
model_config = ConfigDict(strict=True, frozen=True, extra="forbid")
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
# Requests
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
class _Request(_Message):
|
|
166
|
+
ci: CiBridge | None = None
|
|
167
|
+
"""The CI system running the request: its outputs, summary, outcome and log masking."""
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class VersionRequest(_Request):
|
|
171
|
+
"""Ask for the installed Datadouble version."""
|
|
172
|
+
|
|
173
|
+
command: Literal[Command.VERSION]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class SchemaRequest(_Request):
|
|
177
|
+
"""Ask for the JSON Schema that specs are validated against."""
|
|
178
|
+
|
|
179
|
+
command: Literal[Command.SCHEMA]
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
_OLD_LOCK = "catalog.lock"
|
|
183
|
+
"""The lock's name beside every spec before locks were named after their specs."""
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def default_lock(spec: Path) -> Path:
|
|
187
|
+
"""The spec's own lock: its path with the suffix ``.lock``, such as ``shop.lock``.
|
|
188
|
+
|
|
189
|
+
Fails when that is missing and a ``catalog.lock`` sits beside the spec,
|
|
190
|
+
rather than reading a lock that may belong to another spec.
|
|
191
|
+
"""
|
|
192
|
+
lock = spec.with_suffix(".lock")
|
|
193
|
+
old = spec.parent / _OLD_LOCK
|
|
194
|
+
if not lock.exists() and old.exists():
|
|
195
|
+
message = (
|
|
196
|
+
f"{spec} has no lock {lock.name}, but {old} exists. Each spec's lock is named after"
|
|
197
|
+
f" the spec: if {old.name} is this spec's lock, rename it to {lock.name}"
|
|
198
|
+
)
|
|
199
|
+
raise CatalogError(message)
|
|
200
|
+
return lock
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class SpecRequest(_Request):
|
|
204
|
+
"""Read a spec, merged with the specs it extends, and show the result and its files."""
|
|
205
|
+
|
|
206
|
+
command: Literal[Command.SPEC]
|
|
207
|
+
spec: Path
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class _SpecRequest(_Request):
|
|
211
|
+
spec: Path
|
|
212
|
+
lock: Path | None = None
|
|
213
|
+
"""The catalog lock; the spec's path with the suffix ``.lock`` when omitted, such as
|
|
214
|
+
``shop.lock`` for ``shop.yml``."""
|
|
215
|
+
|
|
216
|
+
def lock_path(self) -> Path:
|
|
217
|
+
return self.lock or default_lock(self.spec)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
class LockRequest(_SpecRequest):
|
|
221
|
+
"""Introspect the source and write its catalog lock, listing what changed."""
|
|
222
|
+
|
|
223
|
+
command: Literal[Command.LOCK]
|
|
224
|
+
output: Path | None = None
|
|
225
|
+
"""A directory to write the new lock (named after the spec, such as ``shop.lock``) and its
|
|
226
|
+
changes (``changes.txt``) into, leaving the current lock as it is, for someone to
|
|
227
|
+
review before committing; the CI ``lock`` job uses it. The current lock is
|
|
228
|
+
replaced when omitted."""
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class ImportJailerRequest(_SpecRequest):
|
|
232
|
+
"""Translate a Jailer subset into native spec settings, and lock the catalog."""
|
|
233
|
+
|
|
234
|
+
command: Literal[Command.IMPORT_JAILER]
|
|
235
|
+
model: Path
|
|
236
|
+
"""The extraction model (``.jm``)."""
|
|
237
|
+
datamodel: Path
|
|
238
|
+
"""The data model folder."""
|
|
239
|
+
output: Path
|
|
240
|
+
"""Where the native ``subset`` and ``exclude`` settings are written, as YAML."""
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class _Overrides(_SpecRequest):
|
|
244
|
+
strict: StrictOverrides | None = None
|
|
245
|
+
"""Strict-mode levels over the spec's, for the kinds named, such as ``{"data": "tables"}``."""
|
|
246
|
+
strategy: Strategy | None = None
|
|
247
|
+
formats: tuple[ImageFormat, ...] | None = Field(default=None, min_length=1)
|
|
248
|
+
|
|
249
|
+
def plan_options(self, layers: tuple[SpecLayer, ...]) -> PlanOptions:
|
|
250
|
+
return PlanOptions(
|
|
251
|
+
strict=self.strict, strategy=self.strategy, formats=self.formats, layers=layers
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class PlanRequest(_Overrides):
|
|
256
|
+
"""Plan a build against the live catalog without extracting anything."""
|
|
257
|
+
|
|
258
|
+
command: Literal[Command.PLAN]
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
class RunRequest(_Overrides):
|
|
262
|
+
"""Run build stages, or resume a generation."""
|
|
263
|
+
|
|
264
|
+
command: Literal[Command.RUN]
|
|
265
|
+
stages: tuple[Stage, ...] = Field(min_length=1)
|
|
266
|
+
work: Path = Path(".datadouble")
|
|
267
|
+
resume: Generation | None = None
|
|
268
|
+
retry_key: str | None = Field(default=None, min_length=1)
|
|
269
|
+
"""The CI system's ID for this run: a retry resumes the generation it started."""
|
|
270
|
+
|
|
271
|
+
@model_validator(mode="after")
|
|
272
|
+
def _one_way_to_resume(self) -> "RunRequest":
|
|
273
|
+
if self.resume is not None and self.retry_key is not None:
|
|
274
|
+
message = "give resume or retry_key, not both"
|
|
275
|
+
raise ValueError(message)
|
|
276
|
+
return self
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class _Groups(_Request):
|
|
280
|
+
spec: Path
|
|
281
|
+
groups: tuple[GroupName, ...] = ()
|
|
282
|
+
"""Image groups to work on; every group in the spec when empty."""
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
class PromoteRequest(_Groups):
|
|
286
|
+
"""Copy verified, signed images to the export stores (production)."""
|
|
287
|
+
|
|
288
|
+
command: Literal[Command.PROMOTE]
|
|
289
|
+
generation: Generation | None = None
|
|
290
|
+
"""The generation to promote; the newest in the build's store when omitted."""
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
class FetchRequest(_Groups):
|
|
294
|
+
"""Bring the export channel's images into staging, if they are new."""
|
|
295
|
+
|
|
296
|
+
command: Literal[Command.FETCH]
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
class ActivateRequest(_Groups):
|
|
300
|
+
"""Point the staging channel at a staged generation (rollback)."""
|
|
301
|
+
|
|
302
|
+
command: Literal[Command.ACTIVATE]
|
|
303
|
+
generation: Generation
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class ReplicateRequest(_Groups):
|
|
307
|
+
"""Fill the pool, or make on-demand copies for an owner; then announce them."""
|
|
308
|
+
|
|
309
|
+
command: Literal[Command.REPLICATE]
|
|
310
|
+
owner: str | None = None
|
|
311
|
+
all_at_once: bool = False
|
|
312
|
+
announce: bool = True
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
class ClaimRequest(_Groups):
|
|
316
|
+
"""Take an unclaimed pool copy for an owner; then announce it."""
|
|
317
|
+
|
|
318
|
+
command: Literal[Command.CLAIM]
|
|
319
|
+
owner: str = Field(min_length=1)
|
|
320
|
+
announce: bool = True
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
class ReleaseRequest(_Groups):
|
|
324
|
+
"""Destroy copies by ID, owner or expiry, and withdraw their announcements."""
|
|
325
|
+
|
|
326
|
+
command: Literal[Command.RELEASE]
|
|
327
|
+
copy_ids: tuple[str, ...] = ()
|
|
328
|
+
owner: str | None = None
|
|
329
|
+
expired: bool = False
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
class AnnounceRequest(_Groups):
|
|
333
|
+
"""Announce existing copies again: an owner's, or every unclaimed pool copy."""
|
|
334
|
+
|
|
335
|
+
command: Literal[Command.ANNOUNCE]
|
|
336
|
+
owner: str | None = None
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
class CopiesRequest(_Groups):
|
|
340
|
+
"""List copies, without their credentials."""
|
|
341
|
+
|
|
342
|
+
command: Literal[Command.COPIES]
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
class PruneRequest(_Request):
|
|
346
|
+
"""Apply retention to the build's stores, the export stores, or staging."""
|
|
347
|
+
|
|
348
|
+
command: Literal[Command.PRUNE]
|
|
349
|
+
spec: Path
|
|
350
|
+
target: PruneTarget
|
|
351
|
+
dry_run: bool = False
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
class CiInitRequest(_Request):
|
|
355
|
+
"""Write one side's CI/CD configuration for a spec's jobs."""
|
|
356
|
+
|
|
357
|
+
command: Literal[Command.CI_INIT]
|
|
358
|
+
system: str
|
|
359
|
+
jobs: tuple[JobKind, ...] = Field(min_length=1)
|
|
360
|
+
spec: Path
|
|
361
|
+
runner: str | None = None
|
|
362
|
+
"""The runner jobs run on; required for every job on the production side."""
|
|
363
|
+
work: Path | None = None
|
|
364
|
+
"""The build's work directory in production scratch space; required for build, and for
|
|
365
|
+
every production job when ``notify`` sends recoveries to a webhook (decision 142)."""
|
|
366
|
+
side: Side | None = None
|
|
367
|
+
"""Which side ``prune`` alone runs on."""
|
|
368
|
+
output: Path = Path()
|
|
369
|
+
"""The repository root the configuration is written into."""
|
|
370
|
+
image: str | None = None
|
|
371
|
+
"""The Datadouble image jobs run; this version's published image by default."""
|
|
372
|
+
environment: tuple[str, ...] = ()
|
|
373
|
+
"""More environment variables every job passes on, for credentials adapters read
|
|
374
|
+
from the environment, such as ``AWS_ACCESS_KEY_ID``."""
|
|
375
|
+
docker: str | None = None
|
|
376
|
+
"""The Docker command steps run the image with, a name or a path; otherwise the
|
|
377
|
+
runner's ``DATADOUBLE_DOCKER``, or ``docker``."""
|
|
378
|
+
docker_socket: Path | None = None
|
|
379
|
+
"""The Docker daemon's socket on the runner, an absolute path; otherwise the
|
|
380
|
+
runner's ``DATADOUBLE_DOCKER_SOCKET``, or ``/var/run/docker.sock``."""
|
|
381
|
+
agent: Agent | None = None
|
|
382
|
+
"""Where jobs run Datadouble: ``docker`` (a runner with Docker) or ``kubernetes`` (a pod
|
|
383
|
+
beside the ``kubernetes`` runtime's); the system's default when unset."""
|
|
384
|
+
cloud: str | None = None
|
|
385
|
+
"""The Kubernetes cloud a system's Kubernetes agents start in; its default when unset."""
|
|
386
|
+
spec_source: SpecSource = SpecSourceKind.CHECKOUT
|
|
387
|
+
"""Where jobs find the spec's directory (the spec, the specs it extends, its lock):
|
|
388
|
+
``checkout``, the CI system's checkout of this repository; ``{"mounted": {"path": ...,
|
|
389
|
+
"config_map": ...}}``, a directory already in the job; or ``{"git": {"url": ...,
|
|
390
|
+
"ref": ..., "path": ..., "credential": ...}}``, a repository the job clones. ``spec``
|
|
391
|
+
is still read here, to check it; jobs pass ``<their directory>/<its file name>``."""
|
|
392
|
+
manual: bool = False
|
|
393
|
+
"""Leave out every schedule and follow-on trigger, such as ``promote`` after ``build``,
|
|
394
|
+
so only a person starts the jobs, or a request for on-demand copies: for trying a
|
|
395
|
+
branch's jobs beside the real ones. Each generated file says so."""
|
|
396
|
+
library_remote: str | None = None
|
|
397
|
+
"""The Git URL of a mirror of this repository, holding the release's tag, which the
|
|
398
|
+
jobs load Datadouble's own CI code from at run time: the Jenkins shared library, or
|
|
399
|
+
the Argo CD chart. The public repository when unset; other systems refuse it."""
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
type Request = (
|
|
403
|
+
VersionRequest
|
|
404
|
+
| SchemaRequest
|
|
405
|
+
| SpecRequest
|
|
406
|
+
| LockRequest
|
|
407
|
+
| ImportJailerRequest
|
|
408
|
+
| PlanRequest
|
|
409
|
+
| RunRequest
|
|
410
|
+
| PromoteRequest
|
|
411
|
+
| FetchRequest
|
|
412
|
+
| ActivateRequest
|
|
413
|
+
| ReplicateRequest
|
|
414
|
+
| ClaimRequest
|
|
415
|
+
| ReleaseRequest
|
|
416
|
+
| AnnounceRequest
|
|
417
|
+
| CopiesRequest
|
|
418
|
+
| PruneRequest
|
|
419
|
+
| CiInitRequest
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
_REQUEST: TypeAdapter[Request] = TypeAdapter(
|
|
423
|
+
Annotated[
|
|
424
|
+
VersionRequest
|
|
425
|
+
| SchemaRequest
|
|
426
|
+
| SpecRequest
|
|
427
|
+
| LockRequest
|
|
428
|
+
| ImportJailerRequest
|
|
429
|
+
| PlanRequest
|
|
430
|
+
| RunRequest
|
|
431
|
+
| PromoteRequest
|
|
432
|
+
| FetchRequest
|
|
433
|
+
| ActivateRequest
|
|
434
|
+
| ReplicateRequest
|
|
435
|
+
| ClaimRequest
|
|
436
|
+
| ReleaseRequest
|
|
437
|
+
| AnnounceRequest
|
|
438
|
+
| CopiesRequest
|
|
439
|
+
| PruneRequest
|
|
440
|
+
| CiInitRequest,
|
|
441
|
+
Field(discriminator="command"),
|
|
442
|
+
]
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
# Responses
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
class VersionResponse(_Message):
|
|
450
|
+
"""The installed Datadouble version and the Python running it."""
|
|
451
|
+
|
|
452
|
+
command: Literal[Command.VERSION] = Command.VERSION
|
|
453
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
454
|
+
version: str
|
|
455
|
+
python: str
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
class SchemaResponse(_Message):
|
|
459
|
+
"""The spec JSON Schema."""
|
|
460
|
+
|
|
461
|
+
command: Literal[Command.SCHEMA] = Command.SCHEMA
|
|
462
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
463
|
+
schema_: dict[str, JsonValue] = Field(alias="schema")
|
|
464
|
+
|
|
465
|
+
model_config = ConfigDict(strict=True, frozen=True, extra="forbid", serialize_by_alias=True)
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
class SpecResponse(_Message):
|
|
469
|
+
"""A spec merged with the specs it extends, and the files it was read from."""
|
|
470
|
+
|
|
471
|
+
command: Literal[Command.SPEC] = Command.SPEC
|
|
472
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
473
|
+
spec: dict[str, JsonValue]
|
|
474
|
+
"""The merged spec as written, before defaults, without ``extends``."""
|
|
475
|
+
layers: tuple[SpecLayer, ...]
|
|
476
|
+
"""Every file the spec was read from, in the order they merged: the spec's own file last."""
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
class LockResponse(_Message):
|
|
480
|
+
"""The catalog lock that was written."""
|
|
481
|
+
|
|
482
|
+
command: Literal[Command.LOCK] = Command.LOCK
|
|
483
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
484
|
+
lock: str
|
|
485
|
+
fingerprint: str
|
|
486
|
+
tables: int
|
|
487
|
+
changes: tuple[LockChange, ...] | None
|
|
488
|
+
"""What changed from the earlier lock; None when there was no lock to compare with."""
|
|
489
|
+
changes_file: str | None = None
|
|
490
|
+
"""Where the changes were written for people, with ``output``."""
|
|
491
|
+
unreadable: str | None = None
|
|
492
|
+
"""Why the earlier lock could not be read, when one exists; ``changes`` is None then."""
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
class ImportJailerResponse(_Message):
|
|
496
|
+
"""Where the translated settings and the lock were written, and what was ignored."""
|
|
497
|
+
|
|
498
|
+
command: Literal[Command.IMPORT_JAILER] = Command.IMPORT_JAILER
|
|
499
|
+
outcome: Outcome
|
|
500
|
+
output: str
|
|
501
|
+
lock: str
|
|
502
|
+
report: tuple[str, ...]
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
class PlanResponse(_Message):
|
|
506
|
+
"""A plan, with any warnings."""
|
|
507
|
+
|
|
508
|
+
command: Literal[Command.PLAN] = Command.PLAN
|
|
509
|
+
outcome: Literal[Outcome.SUCCESS, Outcome.WARNING]
|
|
510
|
+
plan: Plan
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
class RunResponse(_Message):
|
|
514
|
+
"""What a run did; ``failure`` when an image failed verification."""
|
|
515
|
+
|
|
516
|
+
command: Literal[Command.RUN] = Command.RUN
|
|
517
|
+
outcome: Outcome
|
|
518
|
+
result: RunResult
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
class PromoteResponse(_Message):
|
|
522
|
+
"""The images promoted."""
|
|
523
|
+
|
|
524
|
+
command: Literal[Command.PROMOTE] = Command.PROMOTE
|
|
525
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
526
|
+
promoted: tuple[Promoted, ...]
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
class FetchResponse(_Message):
|
|
530
|
+
"""What fetch did per image line."""
|
|
531
|
+
|
|
532
|
+
command: Literal[Command.FETCH] = Command.FETCH
|
|
533
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
534
|
+
fetched: tuple[Fetched, ...]
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
class ActivateResponse(_Message):
|
|
538
|
+
"""The staging channels as they now point."""
|
|
539
|
+
|
|
540
|
+
command: Literal[Command.ACTIVATE] = Command.ACTIVATE
|
|
541
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
542
|
+
channels: tuple[Channel, ...]
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
class CopiesResponse(_Message):
|
|
546
|
+
"""Copies made, claimed, released or listed, and where they were announced."""
|
|
547
|
+
|
|
548
|
+
command: Literal[
|
|
549
|
+
Command.REPLICATE, Command.CLAIM, Command.RELEASE, Command.ANNOUNCE, Command.COPIES
|
|
550
|
+
]
|
|
551
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
552
|
+
result: ReplicateResult
|
|
553
|
+
announced: tuple[str, ...] = ()
|
|
554
|
+
withdrawn: tuple[str, ...] = ()
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
class PruneResponse(_Message):
|
|
558
|
+
"""What prune removed, or would remove."""
|
|
559
|
+
|
|
560
|
+
command: Literal[Command.PRUNE] = Command.PRUNE
|
|
561
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
562
|
+
result: PruneResult
|
|
563
|
+
bytes: int
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
class CiInitResponse(_Message):
|
|
567
|
+
"""The configuration files written, and the secrets the jobs read by name."""
|
|
568
|
+
|
|
569
|
+
command: Literal[Command.CI_INIT] = Command.CI_INIT
|
|
570
|
+
outcome: Literal[Outcome.SUCCESS] = Outcome.SUCCESS
|
|
571
|
+
system: str
|
|
572
|
+
side: Side
|
|
573
|
+
files: tuple[str, ...]
|
|
574
|
+
environment: tuple[str, ...]
|
|
575
|
+
"""Environment variables the jobs read secrets from: define each where the jobs run."""
|
|
576
|
+
instructions: tuple[str, ...] = ()
|
|
577
|
+
"""What to set up in the CI system that its configuration cannot hold."""
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
class ErrorDetail(_Message):
|
|
581
|
+
"""One problem found in a request."""
|
|
582
|
+
|
|
583
|
+
location: str
|
|
584
|
+
problem: str
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
class ErrorResponse(_Message):
|
|
588
|
+
"""A request that could not be carried out."""
|
|
589
|
+
|
|
590
|
+
outcome: Literal[Outcome.FAILURE] = Outcome.FAILURE
|
|
591
|
+
error: ErrorCode
|
|
592
|
+
message: str
|
|
593
|
+
details: tuple[ErrorDetail, ...] = ()
|
|
594
|
+
strict: StrictReport | None = None
|
|
595
|
+
"""For strict-mode failures: each new object and how to resolve it."""
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
type Response = (
|
|
599
|
+
VersionResponse
|
|
600
|
+
| SchemaResponse
|
|
601
|
+
| SpecResponse
|
|
602
|
+
| LockResponse
|
|
603
|
+
| ImportJailerResponse
|
|
604
|
+
| PlanResponse
|
|
605
|
+
| RunResponse
|
|
606
|
+
| PromoteResponse
|
|
607
|
+
| FetchResponse
|
|
608
|
+
| ActivateResponse
|
|
609
|
+
| CopiesResponse
|
|
610
|
+
| PruneResponse
|
|
611
|
+
| CiInitResponse
|
|
612
|
+
| ErrorResponse
|
|
613
|
+
)
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
# Handling
|
|
617
|
+
|
|
618
|
+
_ERRORS: tuple[tuple[type[Exception], ErrorCode], ...] = (
|
|
619
|
+
(SettingsError, ErrorCode.INVALID_SETTINGS),
|
|
620
|
+
(CatalogError, ErrorCode.CATALOG_LOCK),
|
|
621
|
+
(RegistryError, ErrorCode.ADAPTER_MISSING),
|
|
622
|
+
(SecretError, ErrorCode.UNRESOLVED_REFERENCE),
|
|
623
|
+
(EngineError, ErrorCode.ENGINE_FAILED),
|
|
624
|
+
(JailerError, ErrorCode.INVALID_SUBSET),
|
|
625
|
+
(ImageError, ErrorCode.ENGINE_FAILED),
|
|
626
|
+
(ContainerError, ErrorCode.CONTAINER_FAILED),
|
|
627
|
+
(AnonymizeError, ErrorCode.ANONYMIZATION_FAILED),
|
|
628
|
+
(StoreError, ErrorCode.STORE_FAILED),
|
|
629
|
+
(RunError, ErrorCode.RUN_FAILED),
|
|
630
|
+
(SigningError, ErrorCode.SIGNATURE_FAILED),
|
|
631
|
+
(StagingError, ErrorCode.STAGING_FAILED),
|
|
632
|
+
(ReplicateError, ErrorCode.REPLICATE_FAILED),
|
|
633
|
+
(AnnounceError, ErrorCode.ANNOUNCE_FAILED),
|
|
634
|
+
(PruneError, ErrorCode.PRUNE_FAILED),
|
|
635
|
+
(BoundaryError, ErrorCode.OUTSIDE_BOUNDARY),
|
|
636
|
+
(CiError, ErrorCode.CI_FAILED),
|
|
637
|
+
)
|
|
638
|
+
_HANDLED: tuple[type[Exception], ...] = (
|
|
639
|
+
SpecError,
|
|
640
|
+
RequirementError,
|
|
641
|
+
PlanError,
|
|
642
|
+
PromoteError,
|
|
643
|
+
*(kind for kind, _ in _ERRORS),
|
|
644
|
+
)
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
def _error(error: Exception) -> ErrorResponse:
|
|
648
|
+
match error:
|
|
649
|
+
case SpecError():
|
|
650
|
+
return ErrorResponse(
|
|
651
|
+
error=ErrorCode.INVALID_SPEC,
|
|
652
|
+
message=str(error),
|
|
653
|
+
details=tuple(ErrorDetail(location=loc, problem=msg) for loc, msg in error.details),
|
|
654
|
+
)
|
|
655
|
+
case PlanError():
|
|
656
|
+
return ErrorResponse(
|
|
657
|
+
error=ErrorCode.PLAN_FAILED,
|
|
658
|
+
message=str(error),
|
|
659
|
+
details=tuple(ErrorDetail(location="plan", problem=p) for p in error.problems),
|
|
660
|
+
strict=error.strict,
|
|
661
|
+
)
|
|
662
|
+
case RequirementError():
|
|
663
|
+
return ErrorResponse(
|
|
664
|
+
error=ErrorCode.REQUIREMENT_UNMET,
|
|
665
|
+
message=str(error),
|
|
666
|
+
details=tuple(
|
|
667
|
+
ErrorDetail(location=f"sources.{source}.image", problem=p)
|
|
668
|
+
for source, p in error.problems
|
|
669
|
+
),
|
|
670
|
+
)
|
|
671
|
+
case PromoteError():
|
|
672
|
+
return ErrorResponse(
|
|
673
|
+
error=ErrorCode.PROMOTE_REFUSED,
|
|
674
|
+
message=str(error),
|
|
675
|
+
details=tuple(ErrorDetail(location="promote", problem=p) for p in error.problems),
|
|
676
|
+
)
|
|
677
|
+
case _:
|
|
678
|
+
code = next(code for kind, code in _ERRORS if isinstance(error, kind))
|
|
679
|
+
return ErrorResponse(error=code, message=str(error))
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
def _catalogs(spec: Spec, registry: Registry) -> tuple[SourceCatalog, ...]:
|
|
683
|
+
"""Every source's live catalog and version, read inside the production boundary."""
|
|
684
|
+
engine = registry.engine(spec.engine)
|
|
685
|
+
require_inside("reading the source", registry)
|
|
686
|
+
with ExitStack() as stack:
|
|
687
|
+
sessions = {
|
|
688
|
+
name: stack.enter_context(engine.connect(source.connection))
|
|
689
|
+
for name, source in spec.sources.items()
|
|
690
|
+
}
|
|
691
|
+
return catalogs(sessions)
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def _merged(spec: Spec, registry: Registry) -> Catalog:
|
|
695
|
+
"""Every source's catalog as one; fails naming databases two sources read."""
|
|
696
|
+
sources = _catalogs(spec, registry)
|
|
697
|
+
problems = collisions(sources)
|
|
698
|
+
if problems:
|
|
699
|
+
raise PlanError(tuple(problems))
|
|
700
|
+
return merge(sources)
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
def _lock(request: _SpecRequest) -> CatalogLock | None:
|
|
704
|
+
path = request.lock_path()
|
|
705
|
+
return read_lock(path) if path.exists() else None
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
def _write_lock(request: LockRequest, registry: Registry) -> LockResponse:
|
|
709
|
+
current = request.lock_path()
|
|
710
|
+
if request.output is not None and request.output.resolve() == current.resolve().parent:
|
|
711
|
+
message = (
|
|
712
|
+
f"output {request.output} holds the current lock {current}; name another"
|
|
713
|
+
" directory, so the current lock stays as it is"
|
|
714
|
+
)
|
|
715
|
+
raise CatalogError(message)
|
|
716
|
+
spec = load_spec(request.spec)
|
|
717
|
+
lock = CatalogLock.of(_merged(spec, registry))
|
|
718
|
+
earlier, unreadable = _earlier_lock(current)
|
|
719
|
+
found = None if earlier is None else changes(earlier.catalog, lock.catalog)
|
|
720
|
+
path = current
|
|
721
|
+
changes_file: Path | None = None
|
|
722
|
+
if request.output is not None:
|
|
723
|
+
request.output.mkdir(parents=True, exist_ok=True)
|
|
724
|
+
path = request.output / current.name
|
|
725
|
+
changes_file = request.output / "changes.txt"
|
|
726
|
+
text = "".join(f"{line}\n" for line in describe_changes(found, unreadable))
|
|
727
|
+
changes_file.write_text(text, encoding="utf-8")
|
|
728
|
+
write_lock(lock, path)
|
|
729
|
+
return LockResponse(
|
|
730
|
+
lock=str(path),
|
|
731
|
+
fingerprint=lock.fingerprint,
|
|
732
|
+
tables=len(lock.catalog.tables),
|
|
733
|
+
changes=found,
|
|
734
|
+
changes_file=None if changes_file is None else str(changes_file),
|
|
735
|
+
unreadable=unreadable,
|
|
736
|
+
)
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
def _earlier_lock(path: Path) -> tuple[CatalogLock | None, str | None]:
|
|
740
|
+
"""The lock a new one replaces, or why it cannot be read.
|
|
741
|
+
|
|
742
|
+
A lock that cannot be read is replaced without a comparison, since writing
|
|
743
|
+
a new one is how to fix it.
|
|
744
|
+
"""
|
|
745
|
+
if not path.is_file():
|
|
746
|
+
return None, None
|
|
747
|
+
try:
|
|
748
|
+
return read_lock(path), None
|
|
749
|
+
except CatalogError as error:
|
|
750
|
+
return None, str(error)
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
def _import_jailer(request: ImportJailerRequest, registry: Registry) -> ImportJailerResponse:
|
|
754
|
+
spec = load_spec(request.spec)
|
|
755
|
+
catalog = _merged(spec, registry)
|
|
756
|
+
translated = jailer.load(
|
|
757
|
+
request.model, request.datamodel, catalog, case_sensitive=spec.case_sensitive
|
|
758
|
+
)
|
|
759
|
+
settings = {
|
|
760
|
+
"exclude": list(translated.exclude),
|
|
761
|
+
"subset": translated.subset.model_dump(mode="json", exclude_defaults=True),
|
|
762
|
+
}
|
|
763
|
+
request.output.write_text(yaml.safe_dump(settings, sort_keys=False), "utf-8")
|
|
764
|
+
lock = CatalogLock.of(catalog)
|
|
765
|
+
write_lock(lock, request.lock_path())
|
|
766
|
+
return ImportJailerResponse(
|
|
767
|
+
outcome=Outcome.WARNING if translated.report else Outcome.SUCCESS,
|
|
768
|
+
output=str(request.output),
|
|
769
|
+
lock=str(request.lock_path()),
|
|
770
|
+
report=translated.report,
|
|
771
|
+
)
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
def _plan(request: PlanRequest, registry: Registry) -> PlanResponse:
|
|
775
|
+
loaded = read_spec(request.spec)
|
|
776
|
+
spec = loaded.spec
|
|
777
|
+
lock = _lock(request)
|
|
778
|
+
engine = registry.engine(spec.engine)
|
|
779
|
+
sources = _catalogs(spec, registry)
|
|
780
|
+
planned = plan(spec, sources, lock, engine.capabilities(), request.plan_options(loaded.layers))
|
|
781
|
+
if planned.warnings:
|
|
782
|
+
return PlanResponse(outcome=Outcome.WARNING, plan=planned)
|
|
783
|
+
return PlanResponse(outcome=Outcome.SUCCESS, plan=planned)
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
def _run(request: RunRequest, registry: Registry) -> RunResponse:
|
|
787
|
+
loaded = read_spec(request.spec)
|
|
788
|
+
options = RunOptions(
|
|
789
|
+
stages=request.stages,
|
|
790
|
+
work=request.work,
|
|
791
|
+
resume=request.resume,
|
|
792
|
+
retry_key=request.retry_key,
|
|
793
|
+
plan=request.plan_options(loaded.layers),
|
|
794
|
+
)
|
|
795
|
+
result = Run(loaded.spec, _lock(request), registry, options).execute()
|
|
796
|
+
if result.failed():
|
|
797
|
+
outcome = Outcome.FAILURE
|
|
798
|
+
elif result.warnings:
|
|
799
|
+
outcome = Outcome.WARNING
|
|
800
|
+
else:
|
|
801
|
+
outcome = Outcome.SUCCESS
|
|
802
|
+
return RunResponse(outcome=outcome, result=result)
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
def _copies(request: _Groups, registry: Registry) -> CopiesResponse:
|
|
806
|
+
spec = load_spec(request.spec)
|
|
807
|
+
match request:
|
|
808
|
+
case ReplicateRequest():
|
|
809
|
+
options = Options(
|
|
810
|
+
groups=request.groups, owner=request.owner, all_at_once=request.all_at_once
|
|
811
|
+
)
|
|
812
|
+
result = replicate(spec, registry, options)
|
|
813
|
+
fresh = _with_secrets(spec, request.groups, result.created)
|
|
814
|
+
announced = announce(spec, registry, fresh) if request.announce else ()
|
|
815
|
+
return CopiesResponse(command=request.command, result=result, announced=announced)
|
|
816
|
+
case ClaimRequest():
|
|
817
|
+
result = claim(spec, registry, Options(groups=request.groups, owner=request.owner))
|
|
818
|
+
taken = _with_secrets(spec, request.groups, result.created)
|
|
819
|
+
announced = announce(spec, registry, taken) if request.announce else ()
|
|
820
|
+
public = result.model_copy(update={"created": tuple(c.public() for c in taken)})
|
|
821
|
+
return CopiesResponse(command=request.command, result=public, announced=announced)
|
|
822
|
+
case ReleaseRequest():
|
|
823
|
+
options = Options(
|
|
824
|
+
groups=request.groups,
|
|
825
|
+
owner=request.owner,
|
|
826
|
+
copy_ids=request.copy_ids,
|
|
827
|
+
expired=request.expired,
|
|
828
|
+
)
|
|
829
|
+
result = release(spec, registry, options)
|
|
830
|
+
withdrawn = withdraw(spec, registry, result.removed)
|
|
831
|
+
return CopiesResponse(command=request.command, result=result, withdrawn=withdrawn)
|
|
832
|
+
case AnnounceRequest():
|
|
833
|
+
everything = copies_of(spec, request.groups, with_secrets=True)
|
|
834
|
+
chosen = tuple(
|
|
835
|
+
c
|
|
836
|
+
for c in everything
|
|
837
|
+
if (c.owner == request.owner if request.owner else not c.claimed)
|
|
838
|
+
)
|
|
839
|
+
announced = announce(spec, registry, chosen)
|
|
840
|
+
result = ReplicateResult(copies=tuple(c.public() for c in everything))
|
|
841
|
+
return CopiesResponse(command=request.command, result=result, announced=announced)
|
|
842
|
+
case _:
|
|
843
|
+
result = ReplicateResult(copies=copies_of(spec, request.groups))
|
|
844
|
+
return CopiesResponse(command=Command.COPIES, result=result)
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
def _with_secrets(
|
|
848
|
+
spec: Spec, groups: tuple[GroupName, ...], chosen: tuple[Copy, ...]
|
|
849
|
+
) -> tuple[Copy, ...]:
|
|
850
|
+
ids = {c.id for c in chosen}
|
|
851
|
+
return tuple(c for c in copies_of(spec, groups, with_secrets=True) if c.id in ids)
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
def _ci_init(request: CiInitRequest, registry: Registry) -> CiInitResponse:
|
|
855
|
+
loaded = read_spec(request.spec)
|
|
856
|
+
spec = loaded.spec
|
|
857
|
+
adapter = registry.ci_system(request.system)
|
|
858
|
+
engine = registry.engine(spec.engine)
|
|
859
|
+
jobs_spec = spec_path(request.spec_source, request.spec, request.output)
|
|
860
|
+
agent = request.agent or adapter.agents[0]
|
|
861
|
+
if agent not in adapter.agents:
|
|
862
|
+
message = (
|
|
863
|
+
f"{adapter.name} jobs cannot run on {agent} agents; they run on"
|
|
864
|
+
f" {', '.join(adapter.agents)}"
|
|
865
|
+
)
|
|
866
|
+
raise CiError(message)
|
|
867
|
+
if request.cloud and agent is not Agent.KUBERNETES:
|
|
868
|
+
message = "a cloud applies only to kubernetes agents"
|
|
869
|
+
raise CiError(message)
|
|
870
|
+
lock = default_lock(request.spec)
|
|
871
|
+
options = PipelineOptions(
|
|
872
|
+
kinds=request.jobs,
|
|
873
|
+
spec_path=jobs_spec,
|
|
874
|
+
image=request.image or f"{IMAGE}:v{__version__}",
|
|
875
|
+
version=__version__,
|
|
876
|
+
owner=engine.owner,
|
|
877
|
+
runner=request.runner,
|
|
878
|
+
work=request.work,
|
|
879
|
+
side=request.side,
|
|
880
|
+
environment=request.environment,
|
|
881
|
+
docker=request.docker,
|
|
882
|
+
docker_socket=request.docker_socket,
|
|
883
|
+
agent=agent,
|
|
884
|
+
cloud=request.cloud,
|
|
885
|
+
remote=registry.remote_settings(),
|
|
886
|
+
spec_files=tuple(
|
|
887
|
+
(layer.path.as_posix(), (loaded.directory / layer.path).read_text(encoding="utf-8"))
|
|
888
|
+
for layer in loaded.layers
|
|
889
|
+
),
|
|
890
|
+
lock_text=lock.read_text(encoding="utf-8") if lock.is_file() else "",
|
|
891
|
+
spec_source=request.spec_source,
|
|
892
|
+
manual=request.manual,
|
|
893
|
+
library_remote=request.library_remote,
|
|
894
|
+
)
|
|
895
|
+
planned = pipeline(spec, options)
|
|
896
|
+
if planned.identity_token and not adapter.identity_token:
|
|
897
|
+
message = (
|
|
898
|
+
f"keyless signing needs the CI system's identity token, which {adapter.name} jobs"
|
|
899
|
+
" don't get; sign with a key pair instead (`signing.cosign.key`)"
|
|
900
|
+
)
|
|
901
|
+
raise CiError(message)
|
|
902
|
+
written: list[str] = []
|
|
903
|
+
for generated in adapter.generate(planned):
|
|
904
|
+
path = request.output / generated.path
|
|
905
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
906
|
+
path.write_text(generated.content, encoding="utf-8")
|
|
907
|
+
written.append(str(path))
|
|
908
|
+
return CiInitResponse(
|
|
909
|
+
system=adapter.name,
|
|
910
|
+
side=planned.side,
|
|
911
|
+
files=tuple(written),
|
|
912
|
+
environment=planned.environment(),
|
|
913
|
+
instructions=adapter.instructions(planned),
|
|
914
|
+
)
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
def _operate(request: Request, registry: Registry) -> Response:
|
|
918
|
+
match request:
|
|
919
|
+
case PromoteRequest():
|
|
920
|
+
spec = load_spec(request.spec)
|
|
921
|
+
promoted = promote(spec, registry, groups=request.groups, generation=request.generation)
|
|
922
|
+
return PromoteResponse(promoted=promoted)
|
|
923
|
+
case FetchRequest():
|
|
924
|
+
spec = load_spec(request.spec)
|
|
925
|
+
return FetchResponse(fetched=fetch(spec, registry, groups=request.groups))
|
|
926
|
+
case ActivateRequest():
|
|
927
|
+
spec = load_spec(request.spec)
|
|
928
|
+
return ActivateResponse(
|
|
929
|
+
channels=activate(spec, request.generation, groups=request.groups)
|
|
930
|
+
)
|
|
931
|
+
case PruneRequest():
|
|
932
|
+
spec = load_spec(request.spec)
|
|
933
|
+
result = prune(spec, registry, request.target, dry_run=request.dry_run)
|
|
934
|
+
return PruneResponse(result=result, bytes=result.bytes)
|
|
935
|
+
case CiInitRequest():
|
|
936
|
+
return _ci_init(request, registry)
|
|
937
|
+
case (
|
|
938
|
+
ReplicateRequest()
|
|
939
|
+
| ClaimRequest()
|
|
940
|
+
| ReleaseRequest()
|
|
941
|
+
| AnnounceRequest()
|
|
942
|
+
| CopiesRequest()
|
|
943
|
+
):
|
|
944
|
+
return _copies(request, registry)
|
|
945
|
+
case _: # pragma: no cover - handle() routes the build commands itself
|
|
946
|
+
message = f"{request.command} is not an operation"
|
|
947
|
+
raise RunError(message)
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
def _build(
|
|
951
|
+
request: LockRequest | ImportJailerRequest | PlanRequest | RunRequest, registry: Registry
|
|
952
|
+
) -> Response:
|
|
953
|
+
match request:
|
|
954
|
+
case LockRequest():
|
|
955
|
+
return _write_lock(request, registry)
|
|
956
|
+
case ImportJailerRequest():
|
|
957
|
+
return _import_jailer(request, registry)
|
|
958
|
+
case PlanRequest():
|
|
959
|
+
return _plan(request, registry)
|
|
960
|
+
case RunRequest():
|
|
961
|
+
return _run(request, registry)
|
|
962
|
+
case _:
|
|
963
|
+
assert_never(request)
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
def _describe(
|
|
967
|
+
request: VersionRequest | SchemaRequest | SpecRequest,
|
|
968
|
+
) -> VersionResponse | SchemaResponse | SpecResponse:
|
|
969
|
+
"""Answer a request that reads no source and needs no adapters."""
|
|
970
|
+
match request:
|
|
971
|
+
case VersionRequest():
|
|
972
|
+
return VersionResponse(version=__version__, python=platform.python_version())
|
|
973
|
+
case SchemaRequest():
|
|
974
|
+
return SchemaResponse(schema=spec_schema())
|
|
975
|
+
case SpecRequest():
|
|
976
|
+
loaded = read_spec(request.spec)
|
|
977
|
+
return SpecResponse(spec=loaded.document, layers=loaded.layers)
|
|
978
|
+
case _:
|
|
979
|
+
assert_never(request)
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
def handle(request: Request, registry: Registry | None = None) -> Response:
|
|
983
|
+
"""Carry out a parsed request with the installed adapters (or ``registry``)."""
|
|
984
|
+
try:
|
|
985
|
+
match request:
|
|
986
|
+
case VersionRequest() | SchemaRequest() | SpecRequest():
|
|
987
|
+
return _describe(request)
|
|
988
|
+
case LockRequest() | ImportJailerRequest() | PlanRequest() | RunRequest():
|
|
989
|
+
return _build(request, registry or installed())
|
|
990
|
+
case (
|
|
991
|
+
PromoteRequest()
|
|
992
|
+
| FetchRequest()
|
|
993
|
+
| ActivateRequest()
|
|
994
|
+
| ReplicateRequest()
|
|
995
|
+
| ClaimRequest()
|
|
996
|
+
| ReleaseRequest()
|
|
997
|
+
| AnnounceRequest()
|
|
998
|
+
| CopiesRequest()
|
|
999
|
+
| PruneRequest()
|
|
1000
|
+
| CiInitRequest()
|
|
1001
|
+
):
|
|
1002
|
+
return _operate(request, registry or installed())
|
|
1003
|
+
case _:
|
|
1004
|
+
assert_never(request)
|
|
1005
|
+
except _HANDLED as error:
|
|
1006
|
+
return _error(error)
|
|
1007
|
+
except OSError as error:
|
|
1008
|
+
# A file the adapters did not explain, such as a directory nobody can
|
|
1009
|
+
# write; its name could come from a source's file tree, so it is left out.
|
|
1010
|
+
message = f"a file operation failed: {error.strerror}"
|
|
1011
|
+
return ErrorResponse(error=ErrorCode.FILE_FAILED, message=message)
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
_TAG_ERRORS = frozenset({"union_tag_invalid", "union_tag_not_found"})
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
def _location(kind: str, loc: tuple[int | str, ...]) -> str:
|
|
1018
|
+
"""Where a problem is, without the union branch the request was matched to."""
|
|
1019
|
+
if kind in _TAG_ERRORS:
|
|
1020
|
+
return "command"
|
|
1021
|
+
parts = list(loc[1:]) if loc and loc[0] in set(Command) else list(loc)
|
|
1022
|
+
return ".".join(str(part) for part in parts) or "(request)"
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
def parse_request(raw: str | bytes) -> Request | ErrorResponse:
|
|
1026
|
+
"""Parse a JSON request, or explain why it is not a valid one."""
|
|
1027
|
+
try:
|
|
1028
|
+
return _REQUEST.validate_json(raw)
|
|
1029
|
+
except ValidationError as error:
|
|
1030
|
+
details = tuple(
|
|
1031
|
+
ErrorDetail(location=_location(item["type"], item["loc"]), problem=item["msg"])
|
|
1032
|
+
for item in error.errors(include_url=False)
|
|
1033
|
+
)
|
|
1034
|
+
return ErrorResponse(
|
|
1035
|
+
error=ErrorCode.INVALID_REQUEST,
|
|
1036
|
+
message="The request is not a valid Datadouble request.",
|
|
1037
|
+
details=details,
|
|
1038
|
+
)
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
def handle_json(raw: str | bytes, registry: Registry | None = None) -> Response:
|
|
1042
|
+
"""Parse and carry out a JSON request."""
|
|
1043
|
+
parsed = parse_request(raw)
|
|
1044
|
+
if isinstance(parsed, ErrorResponse):
|
|
1045
|
+
return parsed
|
|
1046
|
+
return handle(parsed, registry)
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
def exit_code(response: Response) -> ExitCode:
|
|
1050
|
+
"""The process exit code for a response."""
|
|
1051
|
+
match response.outcome:
|
|
1052
|
+
case Outcome.SUCCESS:
|
|
1053
|
+
return ExitCode.SUCCESS
|
|
1054
|
+
case Outcome.WARNING:
|
|
1055
|
+
return ExitCode.WARNING
|
|
1056
|
+
case Outcome.FAILURE:
|
|
1057
|
+
return ExitCode.FAILURE
|
|
1058
|
+
case _:
|
|
1059
|
+
assert_never(response.outcome)
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
def ci_report(response: Response, summary: str) -> Report:
|
|
1063
|
+
"""What the CI system is told about a response: no data values, only identifiers."""
|
|
1064
|
+
outputs = {"outcome": str(response.outcome)}
|
|
1065
|
+
files: tuple[Path, ...] = ()
|
|
1066
|
+
match response:
|
|
1067
|
+
case ErrorResponse():
|
|
1068
|
+
outputs["error"] = str(response.error)
|
|
1069
|
+
return Report(
|
|
1070
|
+
command="error",
|
|
1071
|
+
outcome=response.outcome,
|
|
1072
|
+
outputs=outputs,
|
|
1073
|
+
summary=summary,
|
|
1074
|
+
message=response.message,
|
|
1075
|
+
)
|
|
1076
|
+
case RunResponse():
|
|
1077
|
+
outputs["generation"] = response.result.generation
|
|
1078
|
+
warnings = response.result.warnings
|
|
1079
|
+
case PlanResponse():
|
|
1080
|
+
warnings = response.plan.warnings
|
|
1081
|
+
case LockResponse():
|
|
1082
|
+
outputs["fingerprint"] = response.fingerprint
|
|
1083
|
+
if response.changes is not None:
|
|
1084
|
+
outputs["changes"] = str(len(response.changes))
|
|
1085
|
+
if response.changes_file is not None:
|
|
1086
|
+
files = (Path(response.lock), Path(response.changes_file))
|
|
1087
|
+
warnings = ()
|
|
1088
|
+
case _:
|
|
1089
|
+
warnings = ()
|
|
1090
|
+
return Report(
|
|
1091
|
+
command=str(response.command),
|
|
1092
|
+
outcome=response.outcome,
|
|
1093
|
+
outputs=outputs,
|
|
1094
|
+
summary=summary,
|
|
1095
|
+
warnings=tuple(warnings),
|
|
1096
|
+
files=files,
|
|
1097
|
+
)
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
def _record(request: Request) -> Path | None:
|
|
1101
|
+
"""The file a generated job's request records its outcome in, if ``ci init`` named one."""
|
|
1102
|
+
if request.ci is None or request.ci.job is None:
|
|
1103
|
+
return None
|
|
1104
|
+
return request.ci.record
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
def previous_outcome(request: Request) -> tuple[str | None, tuple[str, ...]]:
|
|
1108
|
+
"""The outcome a generated job's previous run recorded, before this one starts.
|
|
1109
|
+
|
|
1110
|
+
The record is marked as running, so a run that never ends leaves a
|
|
1111
|
+
record that is not a success (decision 142). None when the job keeps no
|
|
1112
|
+
record or has none yet. Returns problems for the caller to log.
|
|
1113
|
+
"""
|
|
1114
|
+
path = _record(request)
|
|
1115
|
+
if path is None:
|
|
1116
|
+
return None, ()
|
|
1117
|
+
return recall(path)
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
def notify(
|
|
1121
|
+
request: Request,
|
|
1122
|
+
report: Report,
|
|
1123
|
+
run_url: str | None,
|
|
1124
|
+
*,
|
|
1125
|
+
previous: str | None = None,
|
|
1126
|
+
poster: Post = post,
|
|
1127
|
+
) -> tuple[str, ...]:
|
|
1128
|
+
"""Post a generated job's outcome to its spec's webhooks (decisions 125 and 142).
|
|
1129
|
+
|
|
1130
|
+
Only a request that names its job (``ci.job``) notifies, and only for the
|
|
1131
|
+
outcomes the spec's ``notify`` section lists. A success after a
|
|
1132
|
+
``previous`` outcome that was not one is a recovery. The job's record, if
|
|
1133
|
+
it keeps one, is updated first. Returns a problem per notification that
|
|
1134
|
+
failed, for the caller to log: a failed notification never changes a
|
|
1135
|
+
job's outcome.
|
|
1136
|
+
"""
|
|
1137
|
+
if request.ci is None or request.ci.job is None:
|
|
1138
|
+
return ()
|
|
1139
|
+
match request:
|
|
1140
|
+
case VersionRequest() | SchemaRequest() | SpecRequest():
|
|
1141
|
+
return ()
|
|
1142
|
+
case _:
|
|
1143
|
+
path = request.spec
|
|
1144
|
+
record = _record(request)
|
|
1145
|
+
problems = () if record is None else remember(record, report.outcome)
|
|
1146
|
+
try:
|
|
1147
|
+
spec = load_spec(path)
|
|
1148
|
+
except SpecError:
|
|
1149
|
+
return (*problems, "the spec cannot be read, so its webhooks were not posted to")
|
|
1150
|
+
notification = Notification(
|
|
1151
|
+
spec=spec.name,
|
|
1152
|
+
job=request.ci.job,
|
|
1153
|
+
outcome=report.outcome,
|
|
1154
|
+
recovered=recovered(previous, report.outcome),
|
|
1155
|
+
generation=report.outputs.get("generation"),
|
|
1156
|
+
error=report.outputs.get("error"),
|
|
1157
|
+
run_url=run_url,
|
|
1158
|
+
)
|
|
1159
|
+
return (*problems, *send(spec.notify, notification, poster=poster))
|