GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cortex/src/cortex/challenges/supervisor.py at main · CortexLM/cortex

Expand file tree

/

supervisor.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
304 lines (275 loc) · 13.8 KB
/

supervisor.py

Copy path

File metadata and controls

304 lines (275 loc) · 13.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Pull, verify, canary, run and auto-update challenge containers through the Docker API.
This is the only Cortex process with Docker control. It never reads a secret:
it passes host secret directories to Docker as read-only binds.
"""
from __future__ import annotations
import asyncio
import hashlib
import json
import logging
from collections.abc import Awaitable, Callable
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import httpx
from .registry import RegistryEntry, container_name, load_registry
CONTRACT = "1"
MANAGED = "io.cortex.managed"
CONFIG = "io.cortex.challenge.config"
LOG = logging.getLogger("cortex.challenges")
class SupervisorError(Exception):
"""A refused image or failed rollout; the running container is left as it was."""
class TransientError(SupervisorError):
"""An outage (GitHub 429/5xx, network) that decides nothing; retried next poll."""
@dataclass(frozen=True)
class SupervisorConfig:
registry_file: Path
secrets_host_dir: Path
network: str = "cortex-challenges"
master_url: str = "http://cortex-master:8080"
ready_seconds: float = 60
def __post_init__(self) -> None:
if not self.secrets_host_dir.is_absolute():
raise ValueError("challenge secrets host directory must be absolute")
@dataclass
class Supervisor:
config: SupervisorConfig
docker: httpx.AsyncClient
http: httpx.AsyncClient
sleep: Callable[[float], Awaitable[None]] = asyncio.sleep
refused: dict[str, str] = field(default_factory=dict)
checked: dict[str, float] = field(default_factory=dict)
async def _docker(self, method: str, path: str, **kwargs) -> httpx.Response:
response = await self.docker.request(method, path, **kwargs)
if response.status_code >= 400 and response.status_code != 404:
raise SupervisorError(f"docker {method} {path.split('?')[0]}: {response.status_code}")
return response
async def resolve(self, entry: RegistryEntry) -> tuple[str, dict[str, str]]:
"""Pull the channel or pin and return (repository digest, labels)."""
tag = entry.pin or entry.channel
async with self.docker.stream(
"POST", "/images/create", params={"fromImage": entry.image, "tag": tag}, timeout=900
) as response:
if response.status_code != 200:
raise SupervisorError(f"{entry.id}: pull failed with {response.status_code}")
async for line in response.aiter_lines():
if line.strip() and "error" in json.loads(line):
raise SupervisorError(f"{entry.id}: pull reported an error")
reference = f"{entry.image}@{entry.pin}" if entry.pin else f"{entry.image}:{tag}"
image = (await self._docker("GET", f"/images/{reference}/json")).json()
digests = [
value.split("@", 1)[1]
for value in image.get("RepoDigests") or []
if value.split("@", 1)[0] == entry.image
]
if len(set(digests)) != 1:
raise SupervisorError(f"{entry.id}: image has no single repository digest")
return digests[0], (image.get("Config") or {}).get("Labels") or {}
def verify_labels(self, entry: RegistryEntry, labels: dict[str, str]) -> None:
if (
labels.get("io.cortex.challenge.slug") != entry.id
or labels.get("io.cortex.challenge.contract") != CONTRACT
or labels.get("org.opencontainers.image.source", "").rstrip("/") != entry.source
):
raise SupervisorError(f"{entry.id}: image labels do not match the registry entry")
async def verify_attestation(self, entry: RegistryEntry, digest: str) -> None:
# ponytail: checks that GitHub holds build provenance for this digest in the source
# repository, not the Sigstore bundle signature. Upgrade: verify the returned bundle
# with sigstore-python (or `gh attestation verify`) before trusting a new digest.
try:
response = await self.http.get(
f"https://api.github.com/repos/{entry.owner_repo}/attestations/{digest}",
headers={"accept": "application/vnd.github+json"},
timeout=30,
)
except httpx.HTTPError as error:
raise TransientError(f"{entry.id}: attestation lookup failed: {error}") from None
if response.status_code in {403, 429} or response.status_code >= 500:
raise TransientError(f"{entry.id}: attestation lookup HTTP {response.status_code}")
if response.status_code != 200 or not response.json().get("attestations"):
raise SupervisorError(f"{entry.id}: no build provenance for {digest}")
def _spec(self, entry: RegistryEntry, digest: str, *, canary: bool) -> dict:
env = {
**entry.env,
"CHALLENGE_SLUG": entry.id,
"CHALLENGE_STATE_DIR": "/data",
"CHALLENGE_INTERNAL_TOKEN_FILE": "/run/secrets/internal.token",
"CHALLENGE_ADMIN_TOKEN_FILE": "/run/secrets/admin.token",
"CHALLENGE_MASTER_URL": self.config.master_url,
}
mounts = []
tmpfs = {"/tmp": "rw,noexec,nosuid,size=64m"}
if canary:
tmpfs["/data"] = "rw,noexec,nosuid,size=64m,uid=65532,gid=65532,mode=0700"
else:
mounts = [
{"Type": "volume", "Source": f"{container_name(entry.id)}-data", "Target": "/data"},
{
"Type": "bind",
"Source": str(self.config.secrets_host_dir / entry.id),
"Target": "/run/secrets",
"ReadOnly": True,
},
]
spec: dict[str, Any] = {
"Image": f"{entry.image}@{digest}",
"Env": [f"{name}={value}" for name, value in sorted(env.items())],
"User": "65532:65532",
"Labels": {
MANAGED: "true",
"io.cortex.challenge.id": entry.id,
"io.cortex.challenge.digest": digest,
},
"HostConfig": {
"ReadonlyRootfs": True,
"CapDrop": ["ALL"],
"SecurityOpt": ["no-new-privileges:true"],
"Init": True,
"Memory": entry.memory_mib * 1024 * 1024,
"NanoCpus": int(entry.cpus * 1e9),
"PidsLimit": entry.pids,
"Mounts": mounts,
"Tmpfs": tmpfs,
"NetworkMode": self.config.network,
"RestartPolicy": {"Name": "no" if canary else "unless-stopped"},
"LogConfig": {"Type": "json-file", "Config": {"max-size": "20m", "max-file": "3"}},
},
}
# Any registry, secret-path or network change alters this, so it redeploys even
# when the image digest stays the same.
canonical = json.dumps(spec, sort_keys=True, separators=(",", ":")).encode()
spec["Labels"][CONFIG] = hashlib.sha256(canonical).hexdigest()
return spec
async def _remove(self, name: str) -> None:
await self._docker("DELETE", f"/containers/{name}", params={"force": "true"})
async def _start(self, name: str, spec: dict) -> None:
await self._docker("POST", "/containers/create", params={"name": name}, json=spec)
await self._docker("POST", f"/containers/{name}/start")
async def _answers_version(self, name: str, entry: RegistryEntry) -> bool:
loop = asyncio.get_running_loop()
deadline = loop.time() + self.config.ready_seconds
while loop.time() < deadline:
try:
response = await self.http.get(f"http://{name}:8000/version", timeout=5)
body = response.json() if response.status_code == 200 else {}
if body.get("slug") == entry.id and str(body.get("contract")) == CONTRACT:
return True
except (httpx.HTTPError, ValueError):
pass
await self.sleep(1)
return False
async def _inspect(self, name: str) -> tuple[dict[str, str], bool] | None:
"""(labels, running) of a container, or None when it does not exist."""
response = await self._docker("GET", f"/containers/{name}/json")
if response.status_code == 404:
return None
state = response.json()
labels = (state.get("Config") or {}).get("Labels") or {}
return labels, bool((state.get("State") or {}).get("Running"))
async def _rollout(self, entry: RegistryEntry, spec: dict, *, replace: bool) -> bool:
"""Swap in `spec`; any failure restores the exact previous container."""
name = container_name(entry.id)
backup = f"{name}-previous"
await self._remove(backup)
if replace:
await self._docker("POST", f"/containers/{name}/stop", params={"t": "30"})
await self._docker("POST", f"/containers/{name}/rename", params={"name": backup})
try:
await self._start(name, spec)
ready = await self._answers_version(name, entry)
except (SupervisorError, httpx.HTTPError):
ready = False
if ready:
await self._remove(backup)
return True
await self._remove(name)
if replace:
await self._docker("POST", f"/containers/{backup}/rename", params={"name": name})
await self._docker("POST", f"/containers/{name}/start")
if await self._answers_version(name, entry):
LOG.warning("challenge %s rolled back to its previous container", entry.id)
else:
LOG.error("challenge %s previous container did not come back", entry.id)
return False
async def reconcile(self, entry: RegistryEntry) -> str:
"""Converge one challenge; returns the digest that is running afterwards."""
digest, labels = await self.resolve(entry)
name = container_name(entry.id)
spec = self._spec(entry, digest, canary=False)
fingerprint = spec["Labels"][CONFIG]
deployed = await self._inspect(name)
if deployed is None and await self._inspect(f"{name}-previous") is not None:
# A restart mid-rollout left only the set-aside container: it is the last
# known-good service, so restore it instead of letting _rollout delete it.
await self._docker("POST", f"/containers/{name}-previous/rename", params={"name": name})
deployed = await self._inspect(name)
LOG.warning(
"challenge %s recovered its container from an interrupted rollout", entry.id
)
if deployed is not None and deployed[0].get(CONFIG) == fingerprint:
if not deployed[1]:
await self._docker("POST", f"/containers/{name}/start")
return digest
if self.refused.get(entry.id) == fingerprint:
raise SupervisorError(f"{entry.id}: {digest} was refused; waiting for a change")
try:
# Every new spec re-checks labels and provenance against the current entry,
# so a source edit is verified even when the digest stays the same.
self.verify_labels(entry, labels)
if entry.attestation:
await self.verify_attestation(entry, digest)
except TransientError:
raise
except SupervisorError:
self.refused[entry.id] = fingerprint
raise
# A configuration-only change skips the canary: that digest already booted here.
if deployed is None or deployed[0].get("io.cortex.challenge.digest") != digest:
try:
canary = f"{name}-canary"
await self._remove(canary)
try:
await self._start(canary, self._spec(entry, digest, canary=True))
if not await self._answers_version(canary, entry):
raise SupervisorError(f"{entry.id}: canary did not answer /version")
finally:
await self._remove(canary)
except SupervisorError:
self.refused[entry.id] = fingerprint
raise
if not await self._rollout(entry, spec, replace=deployed is not None):
self.refused[entry.id] = fingerprint
raise SupervisorError(f"{entry.id}: {digest} failed after rollout")
self.refused.pop(entry.id, None)
LOG.info("challenge %s now runs %s", entry.id, digest)
return digest
async def prune(self, registry: dict[str, RegistryEntry]) -> None:
filters = json.dumps({"label": [f"{MANAGED}=true"]})
listed = await self._docker(
"GET", "/containers/json", params={"all": "true", "filters": filters}
)
for container in listed.json():
identifier = (container.get("Labels") or {}).get("io.cortex.challenge.id")
if identifier not in registry:
# The data volume is kept so a re-registered challenge resumes its state.
await self._remove(container["Id"])
LOG.info("removed unregistered challenge container %s", identifier)
async def tick(self, now: float) -> None:
registry = load_registry(self.config.registry_file)
await self.prune(registry)
for entry in registry.values():
if now - self.checked.get(entry.id, float("-inf")) < entry.poll_seconds:
continue
self.checked[entry.id] = now
try:
await self.reconcile(entry)
except (SupervisorError, httpx.HTTPError) as error:
LOG.warning("challenge %s not updated: %s", entry.id, error)
async def run(self) -> None:
loop = asyncio.get_running_loop()
while True:
try:
await self.tick(loop.time())
except (ValueError, SupervisorError, httpx.HTTPError) as error:
LOG.warning("challenge supervisor tick failed: %s", error)
await self.sleep(15)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.