pubdeploy 0.8.0__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.
- pubdeploy/__init__.py +532 -0
- pubdeploy/channels.py +188 -0
- pubdeploy/cli.py +120 -0
- pubdeploy/config.py +140 -0
- pubdeploy/contract.py +87 -0
- pubdeploy/github.py +61 -0
- pubdeploy/hooks.py +82 -0
- pubdeploy/implied.py +179 -0
- pubdeploy/journal.json +207 -0
- pubdeploy/k8s.py +472 -0
- pubdeploy/machine.py +363 -0
- pubdeploy/model.py +229 -0
- pubdeploy/names.py +25 -0
- pubdeploy/order.py +101 -0
- pubdeploy/providers.py +75 -0
- pubdeploy/registry.py +96 -0
- pubdeploy/release-commit +1 -0
- pubdeploy/schema.json +127 -0
- pubdeploy/schema.py +238 -0
- pubdeploy/store.py +127 -0
- pubdeploy/testing/__init__.py +94 -0
- pubdeploy/testing/fixtures/deploy-app/.github/workflows/deploy.yml +11 -0
- pubdeploy/testing/fixtures/deploy-app/k8s/admin/network-policy.yaml +11 -0
- pubdeploy/testing/fixtures/deploy-app/k8s/app.yaml +63 -0
- pubdeploy/testing/fixtures/deploy-app/k8s/kustomization.yaml +6 -0
- pubdeploy/testing/fixtures/deploy-app/pubrules.json +27 -0
- pubdeploy/testing/fixtures/deploy-app/rendered.yaml +86 -0
- pubdeploy/testing/fixtures/deploy-app/zones.json +8 -0
- pubdeploy/testing/plugin.py +26 -0
- pubdeploy/testing/steps.py +1124 -0
- pubdeploy/testing/world.py +569 -0
- pubdeploy/types/__init__.py +181 -0
- pubdeploy/types/auth.py +199 -0
- pubdeploy/types/github.py +110 -0
- pubdeploy/types/k8s.py +609 -0
- pubdeploy/types/net.py +237 -0
- pubdeploy/types/rollout.py +186 -0
- pubdeploy/units.py +177 -0
- pubdeploy/walk.py +325 -0
- pubdeploy-0.8.0.dist-info/METADATA +520 -0
- pubdeploy-0.8.0.dist-info/RECORD +45 -0
- pubdeploy-0.8.0.dist-info/WHEEL +5 -0
- pubdeploy-0.8.0.dist-info/entry_points.txt +11 -0
- pubdeploy-0.8.0.dist-info/licenses/LICENSE +202 -0
- pubdeploy-0.8.0.dist-info/top_level.txt +1 -0
pubdeploy/types/net.py
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"""upstream, dns.site, service.health and storage.tenant: what answers on the network.
|
|
2
|
+
|
|
3
|
+
dns.site's offline part reads the hosting block, of this repository's
|
|
4
|
+
pubrules.json or of the one --zones names (the repository that keeps the
|
|
5
|
+
zone): the host must be a site of one of its zones. Online, its A or AAAA
|
|
6
|
+
records must resolve. Its fulfil proposes the site to add.
|
|
7
|
+
|
|
8
|
+
service.health's fulfil runs a Job from its CronJob, when that is what is
|
|
9
|
+
missing; nothing is done for a host that does not answer.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import time
|
|
15
|
+
import base64
|
|
16
|
+
import json
|
|
17
|
+
from datetime import datetime, timedelta
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from ..github import ApiError
|
|
21
|
+
from ..model import Context, Need, Result
|
|
22
|
+
from . import Base, github_refused, kubectl_json, kubectl_refused, last_success, run_job, schema
|
|
23
|
+
|
|
24
|
+
HTTPS = {"type": "string", "pattern": r"^https://[^\s/?#]+(/[^\s]*)?$", "x-phrase": "must be an https:// address"}
|
|
25
|
+
HOST = {"type": "string", "pattern": r"^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z0-9-]{0,61}[a-z0-9]$",
|
|
26
|
+
"x-phrase": "must be a host name in lower case"}
|
|
27
|
+
TIMEOUT = 5.0
|
|
28
|
+
SINCE = timedelta(hours=24) # how recently a CronJob's last success must be
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def ask(ctx: Context, url: str, timeout: float) -> tuple[int | None, float, str]:
|
|
32
|
+
"""The status `url` answers with, how long it took, and why there is none."""
|
|
33
|
+
started = time.monotonic()
|
|
34
|
+
try:
|
|
35
|
+
status, _body = ctx.http_get(url, timeout)
|
|
36
|
+
except TimeoutError:
|
|
37
|
+
return None, time.monotonic() - started, "timeout"
|
|
38
|
+
except OSError as e:
|
|
39
|
+
return None, time.monotonic() - started, type(e).__name__
|
|
40
|
+
return status, time.monotonic() - started, ""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def path_of(url: str) -> str:
|
|
44
|
+
rest = url.split("://", 1)[-1]
|
|
45
|
+
return "/" + rest.split("/", 1)[1] if "/" in rest else "/"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Upstream(Base):
|
|
49
|
+
"""A service this one calls, which must answer its health address."""
|
|
50
|
+
type = "upstream"
|
|
51
|
+
budget = 6.0
|
|
52
|
+
schema = schema({"health": HTTPS}, ("health",))
|
|
53
|
+
|
|
54
|
+
def check(self, need: Need, ctx: Context) -> Result:
|
|
55
|
+
url = need.params["health"]
|
|
56
|
+
status, seconds, why = ask(ctx, url, TIMEOUT)
|
|
57
|
+
if why == "timeout":
|
|
58
|
+
return Result("unknown", f"{need.name} did not answer at {path_of(url)} in {TIMEOUT:g} s",
|
|
59
|
+
fix=f"see whether {need.name} is up: curl -sS {url}")
|
|
60
|
+
if status is None:
|
|
61
|
+
return Result("missing", f"{need.name} cannot be reached at {path_of(url)} ({why})",
|
|
62
|
+
fix=f"{need.name} must be up before this deploys: curl -sS {url}")
|
|
63
|
+
if not 200 <= status < 400:
|
|
64
|
+
return Result("missing", f"{need.name} answers {status} at {path_of(url)}",
|
|
65
|
+
fix=f"{need.name} must answer 2xx or 3xx before this deploys: curl -sS {url}")
|
|
66
|
+
return Result("ready", f"{need.name} answers {status} at {path_of(url)} in {seconds:.1f} s")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def site_of(host: str, zones: dict[str, Any]) -> str | None:
|
|
70
|
+
"""The zone `host` is a site of, or None."""
|
|
71
|
+
for zone, given in zones.items():
|
|
72
|
+
sites = (given or {}).get("sites", {}) if isinstance(given, dict) else {}
|
|
73
|
+
if host == zone and "@" in sites:
|
|
74
|
+
return zone
|
|
75
|
+
if host.endswith(f".{zone}") and host[:-len(zone) - 1] in sites:
|
|
76
|
+
return zone
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class Site(Base):
|
|
81
|
+
"""The public host: a site in the hosting block, and in the DNS."""
|
|
82
|
+
type = "dns.site"
|
|
83
|
+
grade = "propose"
|
|
84
|
+
budget = 5.0
|
|
85
|
+
offline = True
|
|
86
|
+
schema = schema({"host": HOST})
|
|
87
|
+
|
|
88
|
+
def default_name(self, params: dict[str, Any]) -> str | None:
|
|
89
|
+
return params.get("host")
|
|
90
|
+
|
|
91
|
+
def fulfil(self, need: Need, ctx: Context) -> Result:
|
|
92
|
+
host = need.params.get("host", need.name)
|
|
93
|
+
zone = next((z for z in sorted(ctx.zones or {}, key=len, reverse=True) if host.endswith(f".{z}")), None)
|
|
94
|
+
if host in (ctx.zones or {}):
|
|
95
|
+
zone, label = host, "@"
|
|
96
|
+
else:
|
|
97
|
+
label = host[:-len(zone) - 1] if zone else host
|
|
98
|
+
within = f"hosting.zones[\"{zone}\"].sites" if zone else "the sites of its zone in hosting.zones"
|
|
99
|
+
entry = json.dumps({label: {"provider": "<provider>", "kind": "server", "server": "<server>"}})
|
|
100
|
+
return Result("missing", f"{host} is a site to add", fix=f"in the pubrules.json that keeps the zone, add to "
|
|
101
|
+
f"{within}:\n {entry[1:-1]}\nthen apply its hosting stack")
|
|
102
|
+
|
|
103
|
+
def check(self, need: Need, ctx: Context) -> Result:
|
|
104
|
+
host = need.params.get("host", need.name)
|
|
105
|
+
if ctx.zones is None:
|
|
106
|
+
return Result("unknown", f"no hosting block says where {host} is",
|
|
107
|
+
fix="give --zones PATH, the pubrules.json whose hosting block keeps the zone")
|
|
108
|
+
zone = site_of(host, ctx.zones)
|
|
109
|
+
if zone is None and not any(host == z or host.endswith(f".{z}") for z in ctx.zones):
|
|
110
|
+
# No zone given holds it: the zones of another owner, not a site missing.
|
|
111
|
+
return Result("unknown", f"no hosting block given has a zone for {host}",
|
|
112
|
+
fix="give --zones PATH again, the pubrules.json whose hosting block keeps its zone")
|
|
113
|
+
if zone is None:
|
|
114
|
+
return Result("missing", f"{host} is no site of a zone in the hosting block",
|
|
115
|
+
fix="add it to the sites of its zone in the hosting block")
|
|
116
|
+
if ctx.offline:
|
|
117
|
+
return Result("ready", f"{host} is a site of {zone}")
|
|
118
|
+
found = [ctx.resolve(host, rtype) for rtype in ("A", "AAAA")]
|
|
119
|
+
if any(f is None for f in found):
|
|
120
|
+
return Result("unknown", f"{host} could not be resolved", fix="install dig (bind9-dnsutils)")
|
|
121
|
+
records = [r for f in found for r in f or []]
|
|
122
|
+
if not records:
|
|
123
|
+
return Result("missing", f"{host} has no A or AAAA record",
|
|
124
|
+
fix=f"apply the hosting stack that keeps {zone}")
|
|
125
|
+
return Result("ready", f"{host} is a site of {zone}, with {len(records)} address"
|
|
126
|
+
f"{'es' if len(records) != 1 else ''}")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class Health(Base):
|
|
130
|
+
"""The deployed service answers, and its CronJob ran."""
|
|
131
|
+
type = "service.health"
|
|
132
|
+
stop = "shorten"
|
|
133
|
+
grade = "auto"
|
|
134
|
+
budget = 6.0
|
|
135
|
+
fulfil_budget = 300.0
|
|
136
|
+
singleton = True
|
|
137
|
+
needs_of_context = ("kubectl-write",)
|
|
138
|
+
|
|
139
|
+
def action(self, need: Need, ctx: Context) -> str:
|
|
140
|
+
return f"run a Job from {need.params.get('job')}, and wait for it"
|
|
141
|
+
|
|
142
|
+
def fulfil(self, need: Need, ctx: Context) -> Result:
|
|
143
|
+
return run_job(ctx, str(need.params.get("job", "/")).split("/", 1)[1], self.fulfil_budget)
|
|
144
|
+
schema = schema({"path": {"type": "string", "pattern": r"^/[^\s]*$", "x-phrase": "must be a path, starting /"},
|
|
145
|
+
"job": {"type": "string", "pattern": r"^cronjob/[a-z0-9]([-a-z0-9.]{0,251}[a-z0-9])?$",
|
|
146
|
+
"x-phrase": "must be cronjob/NAME"}})
|
|
147
|
+
|
|
148
|
+
def check(self, need: Need, ctx: Context) -> Result:
|
|
149
|
+
host = ctx.block.get("host")
|
|
150
|
+
if not host:
|
|
151
|
+
return Result("unknown", "the deploy block has no host to ask", fix="give deploy.host")
|
|
152
|
+
url = f"https://{host}{need.params.get('path', '/')}"
|
|
153
|
+
status, seconds, why = ask(ctx, url, TIMEOUT)
|
|
154
|
+
if why == "timeout":
|
|
155
|
+
return Result("unknown", f"{host} did not answer in {TIMEOUT:g} s", fix=f"curl -sS {url}")
|
|
156
|
+
if status != 200:
|
|
157
|
+
said = f"answers {status}" if status is not None else f"cannot be reached ({why})"
|
|
158
|
+
return Result("missing", f"{host} {said} at {path_of(url)}", fixable=False,
|
|
159
|
+
fix=f"pubdeploy k8s diagnose --namespace {ctx.namespace}")
|
|
160
|
+
summary = f"{host} answers 200 at {path_of(url)} in {seconds:.1f} s"
|
|
161
|
+
job = need.params.get("job")
|
|
162
|
+
if not job:
|
|
163
|
+
return Result("ready", summary)
|
|
164
|
+
name = job.split("/", 1)[1]
|
|
165
|
+
found, ran = kubectl_json(ctx, "-n", ctx.namespace, "get", "cronjob", name)
|
|
166
|
+
if found is None:
|
|
167
|
+
if ran.returncode != 0 and ("(NotFound)" in ran.stderr or " not found" in ran.stderr):
|
|
168
|
+
return Result("missing", f"CronJob {name} is not in {ctx.namespace}", fixable=False,
|
|
169
|
+
fix=f"see that the manifests in {ctx.block.get('overlay', 'k8s')} hold it")
|
|
170
|
+
return kubectl_refused(ran, ctx.namespace, "cronjobs")
|
|
171
|
+
last = last_success(ctx, name, found.get("status") or {})
|
|
172
|
+
if not last:
|
|
173
|
+
return Result("missing", f"CronJob {name} has not succeeded yet", step="job",
|
|
174
|
+
fix=f"kubectl -n {ctx.namespace} create job --from=cronjob/{name} {name}-now")
|
|
175
|
+
when = datetime.fromisoformat(str(last).replace("Z", "+00:00"))
|
|
176
|
+
if ctx.now() - when > SINCE:
|
|
177
|
+
return Result("drift", f"CronJob {name} last succeeded at {last}, over 24 h ago", step="job",
|
|
178
|
+
fix=f"pubdeploy k8s diagnose --namespace {ctx.namespace}")
|
|
179
|
+
return Result("ready", f"{summary}; CronJob {name} succeeded at {last}")
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
PRINCIPALS = {"type": "array", "items": {"type": "string", "minLength": 1}, "x-phrase": "must list principals"}
|
|
183
|
+
TENANTS = "tenants.json"
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class Tenant(Base):
|
|
187
|
+
"""A tenant of the storage service: declared in its repository's
|
|
188
|
+
tenants.json with the principals given, and deployed."""
|
|
189
|
+
type = "storage.tenant"
|
|
190
|
+
grade = "propose"
|
|
191
|
+
budget = 4.0
|
|
192
|
+
schema = schema({"storage": {"type": "string", "pattern": r"^[A-Za-z0-9][A-Za-z0-9-]*/[A-Za-z0-9_.-]+$",
|
|
193
|
+
"x-phrase": "must be the storage repository, OWNER/NAME"},
|
|
194
|
+
"url": HTTPS, "write": PRINCIPALS, "read": PRINCIPALS, "delete": PRINCIPALS,
|
|
195
|
+
"private": {"type": "boolean"}, "heads": {"type": "boolean"}, "inbox": {"type": "boolean"}},
|
|
196
|
+
("storage", "url"))
|
|
197
|
+
|
|
198
|
+
def check(self, need: Need, ctx: Context) -> Result:
|
|
199
|
+
name, repo = need.name, need.params["storage"]
|
|
200
|
+
try:
|
|
201
|
+
found: Any = ctx.gh("GET", f"repos/{repo}/contents/{TENANTS}", None) or {}
|
|
202
|
+
tenants = json.loads(base64.b64decode(found.get("content", "")))
|
|
203
|
+
except ApiError as e:
|
|
204
|
+
if e.status == 404:
|
|
205
|
+
return Result("missing", f"{repo} has no {TENANTS}", fix=f"see that {repo} is the storage repository")
|
|
206
|
+
return github_refused(e, f"{TENANTS} of {repo}")
|
|
207
|
+
except (ValueError, TypeError, AttributeError):
|
|
208
|
+
return Result("unknown", f"{TENANTS} of {repo} is not JSON", fix=f"see {TENANTS} in {repo}")
|
|
209
|
+
given = tenants.get(name) if isinstance(tenants, dict) else None
|
|
210
|
+
if not isinstance(given, dict):
|
|
211
|
+
return Result("missing", f"tenant {name} is not in {repo}'s {TENANTS}", fix=self.proposal(need))
|
|
212
|
+
for key in ("write", "read", "delete"):
|
|
213
|
+
lacking = [p for p in need.params.get(key, []) if p not in (given.get(key) or [])]
|
|
214
|
+
if lacking:
|
|
215
|
+
return Result("drift", f"tenant {name} in {repo} does not let {lacking[0]} {key}",
|
|
216
|
+
fix=self.proposal(need), details=[f"{key}: {p} is not listed" for p in lacking])
|
|
217
|
+
url = f"{need.params['url'].rstrip('/')}/{name}/list?prefix=f/"
|
|
218
|
+
status, _seconds, why = ask(ctx, url, TIMEOUT)
|
|
219
|
+
if status is None:
|
|
220
|
+
return Result("unknown", f"storage did not answer for {name} ({why})", fix=f"curl -sS {url}")
|
|
221
|
+
if status == 404:
|
|
222
|
+
return Result("drift", f"tenant {name} is declared but not deployed yet",
|
|
223
|
+
fix=f"apply {repo}'s deploy, which deploys its {TENANTS}")
|
|
224
|
+
private = need.params.get("private", given.get("private") is True)
|
|
225
|
+
if (private and status in (401, 403)) or (not private and 200 <= status < 300):
|
|
226
|
+
return Result("ready", f"tenant {name} is in {repo}'s {TENANTS}, and storage answers {status} for it")
|
|
227
|
+
return Result("drift", f"storage answers {status} for {'private ' * bool(private)}tenant {name}",
|
|
228
|
+
fix=f"curl -sS {url}")
|
|
229
|
+
|
|
230
|
+
def proposal(self, need: Need) -> str:
|
|
231
|
+
entry = {k: need.params[k] for k in ("private", "heads", "inbox", "read", "write", "delete")
|
|
232
|
+
if k in need.params}
|
|
233
|
+
return (f"in {need.params['storage']}'s {TENANTS}, add or extend:\n "
|
|
234
|
+
f"{json.dumps({need.name: entry})[1:-1]}")
|
|
235
|
+
|
|
236
|
+
def fulfil(self, need: Need, ctx: Context) -> Result:
|
|
237
|
+
return Result("missing", f"tenant {need.name} is {need.params['storage']}'s to add", fix=self.proposal(need))
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""github.gate and deploy.rollout: whether the deploy may run, and whether it did.
|
|
2
|
+
|
|
3
|
+
The gate is the repository variable that must be "on" for the deploy
|
|
4
|
+
workflow to deploy. It is walked after every need but the rollout and the
|
|
5
|
+
service's health, and judged by them: open while all are ready, closed
|
|
6
|
+
while one is not; open too early when one is missing; closed with nothing
|
|
7
|
+
left to wait for is the one step left to take.
|
|
8
|
+
|
|
9
|
+
Fulfilled, the gate is set on, only while every need before it is ready,
|
|
10
|
+
and the deploy workflow is run on the default branch; the rollout runs it
|
|
11
|
+
(unless the gate did, in this walk) and waits for that run to end.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from ..github import ApiError
|
|
19
|
+
from ..model import Context, Need, Result
|
|
20
|
+
from . import Base, at_once, github_refused, kubectl_json, kubectl_refused, schema
|
|
21
|
+
from .github import GITHUB_NAME, NO_ORIGIN, gate_value
|
|
22
|
+
|
|
23
|
+
LAST = ("deploy.rollout", "service.health")
|
|
24
|
+
POLL = 15.0 # seconds between looks at a run a fulfil waits for
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def default_branch(ctx: Context) -> str:
|
|
28
|
+
return (ctx.gh("GET", f"repos/{ctx.slug}", None) or {}).get("default_branch", "main")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def dispatch(ctx: Context, workflow: str, branch: str) -> str:
|
|
32
|
+
"""Run the workflow on the branch; when, as runs' created_at say it."""
|
|
33
|
+
at = ctx.now().strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
34
|
+
ctx.gh("POST", f"repos/{ctx.slug}/actions/workflows/{workflow}/dispatches", {"ref": branch})
|
|
35
|
+
ctx.dispatched[workflow] = ctx.now()
|
|
36
|
+
return at
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def set_variable(ctx: Context, name: str, value: str) -> None:
|
|
40
|
+
try:
|
|
41
|
+
ctx.gh("PATCH", f"repos/{ctx.slug}/actions/variables/{name}", {"name": name, "value": value})
|
|
42
|
+
except ApiError as e:
|
|
43
|
+
if e.status != 404:
|
|
44
|
+
raise
|
|
45
|
+
ctx.gh("POST", f"repos/{ctx.slug}/actions/variables", {"name": name, "value": value})
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Gate(Base):
|
|
49
|
+
type = "github.gate"
|
|
50
|
+
grade = "auto"
|
|
51
|
+
budget = 2.0
|
|
52
|
+
fulfil_budget = 30.0
|
|
53
|
+
singleton = True
|
|
54
|
+
schema = schema({"variable": GITHUB_NAME}, ("variable",))
|
|
55
|
+
|
|
56
|
+
def action(self, need: Need, ctx: Context) -> str:
|
|
57
|
+
workflow = ctx.block.get("workflow", "deploy.yml")
|
|
58
|
+
return f"set {need.params['variable']} on, once every need before it is ready, and run {workflow}"
|
|
59
|
+
|
|
60
|
+
def fulfil(self, need: Need, ctx: Context) -> Result:
|
|
61
|
+
variable, workflow = need.params["variable"], ctx.block.get("workflow", "deploy.yml")
|
|
62
|
+
late = sorted(i for i, r in ctx.results.items()
|
|
63
|
+
if i != need.id and ctx.needs.get(i, need).type not in LAST and r.state != "ready")
|
|
64
|
+
if late:
|
|
65
|
+
return Result("missing", f"{variable} stays off: {late[0]} is not ready", fix="see the needs above")
|
|
66
|
+
try:
|
|
67
|
+
set_variable(ctx, variable, "on")
|
|
68
|
+
branch = default_branch(ctx)
|
|
69
|
+
dispatch(ctx, workflow, branch)
|
|
70
|
+
except ApiError as e:
|
|
71
|
+
refused = github_refused(e, f"variable {variable}")
|
|
72
|
+
return Result("missing", f"GitHub refused to set {variable} or run {workflow}", fix=refused.fix)
|
|
73
|
+
return Result("ready", f"set {variable} on, and ran {workflow} on {branch}")
|
|
74
|
+
|
|
75
|
+
def check(self, need: Need, ctx: Context) -> Result:
|
|
76
|
+
if not ctx.slug:
|
|
77
|
+
return NO_ORIGIN
|
|
78
|
+
variable = need.params["variable"]
|
|
79
|
+
value, refused = gate_value(ctx, variable)
|
|
80
|
+
if refused:
|
|
81
|
+
return refused
|
|
82
|
+
earlier = {i: r for i, r in ctx.results.items()
|
|
83
|
+
if i != need.id and ctx.needs.get(i, need).type not in LAST}
|
|
84
|
+
late = sorted(i for i, r in earlier.items() if r.state != "ready")
|
|
85
|
+
waiting = sorted(i for i, r in earlier.items() if r.state in ("missing", "blocked"))
|
|
86
|
+
set_on = f"gh variable set {variable} -R {ctx.slug} --body on"
|
|
87
|
+
if value == "on":
|
|
88
|
+
if waiting:
|
|
89
|
+
return Result("drift", f"{variable} is on while {len(waiting)} need"
|
|
90
|
+
f"{'s are' if len(waiting) != 1 else ' is'} missing", fixable=False,
|
|
91
|
+
fix=f"gh variable set {variable} -R {ctx.slug} --body off, until they are ready",
|
|
92
|
+
details=[f"missing: {i}" for i in waiting])
|
|
93
|
+
if late:
|
|
94
|
+
return Result("unknown", f"{variable} is on, and {len(late)} need"
|
|
95
|
+
f"{'s' if len(late) != 1 else ''} could not be shown ready",
|
|
96
|
+
fix="see the needs above", details=[f"not ready: {i}" for i in late])
|
|
97
|
+
return Result("ready", f"{variable} is on, and the {len(earlier)} needs before it are ready")
|
|
98
|
+
shown = "unset" if value is None else "not on"
|
|
99
|
+
if late:
|
|
100
|
+
return Result("blocked", f"{variable} is {shown}, and waits for {len(late)} need"
|
|
101
|
+
f"{'s' if len(late) != 1 else ''}",
|
|
102
|
+
fix=f"once they are ready: {set_on}", details=[f"not ready: {i}" for i in late])
|
|
103
|
+
return Result("missing", f"{variable} is {shown}, and every need before it is ready", fix=set_on)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class Rollout(Base):
|
|
107
|
+
"""The latest deploy run on the default branch passed on its head, and
|
|
108
|
+
every Deployment in the namespace has its replicas ready."""
|
|
109
|
+
type = "deploy.rollout"
|
|
110
|
+
stop = "continue"
|
|
111
|
+
stop_warning = "the deploy workflow keeps running on GitHub; cancel the run there to end it"
|
|
112
|
+
grade = "auto"
|
|
113
|
+
# Two GitHub calls, then three at once, at about 0.6 s each, and a kubectl call.
|
|
114
|
+
budget = 8.0
|
|
115
|
+
fulfil_budget = 900.0
|
|
116
|
+
singleton = True
|
|
117
|
+
|
|
118
|
+
def action(self, need: Need, ctx: Context) -> str:
|
|
119
|
+
return f"run {ctx.block.get('workflow', 'deploy.yml')} on the default branch, and wait for it"
|
|
120
|
+
|
|
121
|
+
def fulfil(self, need: Need, ctx: Context) -> Result:
|
|
122
|
+
workflow = ctx.block.get("workflow", "deploy.yml")
|
|
123
|
+
try:
|
|
124
|
+
branch = default_branch(ctx)
|
|
125
|
+
given = ctx.dispatched.get(workflow)
|
|
126
|
+
at = given.strftime("%Y-%m-%dT%H:%M:%SZ") if given else dispatch(ctx, workflow, branch)
|
|
127
|
+
for _ in range(int((self.fulfil_budget - 2 * POLL) / POLL)):
|
|
128
|
+
ctx.sleep(POLL)
|
|
129
|
+
found: Any = ctx.gh("GET", f"repos/{ctx.slug}/actions/workflows/{workflow}/runs"
|
|
130
|
+
f"?branch={branch}&event=workflow_dispatch&per_page=5", None) or {}
|
|
131
|
+
runs = [r for r in found.get("workflow_runs") or [] if str(r.get("created_at", "")) >= at]
|
|
132
|
+
if not runs or runs[0].get("status") != "completed":
|
|
133
|
+
continue
|
|
134
|
+
run = runs[0]
|
|
135
|
+
if run.get("conclusion") != "success":
|
|
136
|
+
return Result("drift", f"{workflow} run {run.get('id')} ended {run.get('conclusion')}",
|
|
137
|
+
fix=f"gh run view {run.get('id')} -R {ctx.slug} --log-failed")
|
|
138
|
+
return Result("ready", f"ran {workflow} on {branch}: run {run.get('id')} passed")
|
|
139
|
+
except ApiError as e:
|
|
140
|
+
return Result("missing", f"GitHub refused to run {workflow}", fix=github_refused(e, workflow).fix)
|
|
141
|
+
return Result("drift", f"{workflow} on {branch} did not end in time",
|
|
142
|
+
fix=f"gh run list -R {ctx.slug} --workflow {workflow}")
|
|
143
|
+
|
|
144
|
+
def check(self, need: Need, ctx: Context) -> Result:
|
|
145
|
+
if not ctx.slug:
|
|
146
|
+
return NO_ORIGIN
|
|
147
|
+
workflow = ctx.block.get("workflow", "deploy.yml")
|
|
148
|
+
try:
|
|
149
|
+
branch = (ctx.gh("GET", f"repos/{ctx.slug}", None) or {}).get("default_branch", "main")
|
|
150
|
+
head = ((ctx.gh("GET", f"repos/{ctx.slug}/branches/{branch}", None) or {}).get("commit") or {}).get("sha", "")
|
|
151
|
+
listings = at_once(ctx.gh, [("GET", f"repos/{ctx.slug}/actions/workflows/{workflow}/runs"
|
|
152
|
+
f"?branch={branch}&event={event}&per_page=1", None)
|
|
153
|
+
for event in ("push", "workflow_run", "workflow_dispatch")])
|
|
154
|
+
runs = [r for found in listings for r in (found or {}).get("workflow_runs") or []]
|
|
155
|
+
except ApiError as e:
|
|
156
|
+
if e.status == 404:
|
|
157
|
+
return Result("missing", f"{workflow} is not a workflow of {ctx.slug}", fixable=False,
|
|
158
|
+
fix=f"push .github/workflows/{workflow} to {branch}")
|
|
159
|
+
return github_refused(e, f"the runs of {workflow}")
|
|
160
|
+
if not runs:
|
|
161
|
+
return Result("missing", f"{workflow} has not run on {branch}",
|
|
162
|
+
fix=f"gh workflow run {workflow} -R {ctx.slug} --ref {branch}")
|
|
163
|
+
run = max(runs, key=lambda r: str(r.get("created_at", "")))
|
|
164
|
+
sha = str(run.get("head_sha", ""))
|
|
165
|
+
if run.get("conclusion") != "success":
|
|
166
|
+
how = run.get("conclusion") or run.get("status") or "unknown"
|
|
167
|
+
return Result("drift", f"the last deploy, of {sha[:7]}, is {how}",
|
|
168
|
+
fix=f"see why in gh run view {run.get('id')} -R {ctx.slug}, then rerun it")
|
|
169
|
+
if head and sha != head:
|
|
170
|
+
return Result("drift", f"{branch} is at {head[:7]}, the last passed deploy is of {sha[:7]}",
|
|
171
|
+
fix=f"wait for {workflow} on {head[:7]}, or gh workflow run {workflow} -R {ctx.slug}")
|
|
172
|
+
found, ran = kubectl_json(ctx, "-n", ctx.namespace, "get", "deployment")
|
|
173
|
+
if found is None:
|
|
174
|
+
return kubectl_refused(ran, ctx.namespace, "deployments")
|
|
175
|
+
late = []
|
|
176
|
+
deployments = [d for d in found.get("items") or [] if isinstance(d, dict)]
|
|
177
|
+
for d in deployments:
|
|
178
|
+
want = (d.get("spec") or {}).get("replicas", 1)
|
|
179
|
+
have = (d.get("status") or {}).get("readyReplicas", 0)
|
|
180
|
+
if have != want:
|
|
181
|
+
late.append(f"Deployment/{(d.get('metadata') or {}).get('name')} has {have} of {want} ready")
|
|
182
|
+
if late:
|
|
183
|
+
return Result("drift", late[0], details=late,
|
|
184
|
+
fix=f"pubdeploy k8s diagnose --namespace {ctx.namespace}")
|
|
185
|
+
return Result("ready", f"{branch}'s {sha[:7]} is deployed, and {len(deployments)} Deployment"
|
|
186
|
+
f"{'s are' if len(deployments) != 1 else ' is'} ready")
|
pubdeploy/units.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""A deploy block's units, and what `pubrules sync --check` asks of its workflow.
|
|
2
|
+
|
|
3
|
+
The top level of the deploy block is the unit its `namespace` names; `units`
|
|
4
|
+
adds more, each with its own namespace, overlay, gate and host:
|
|
5
|
+
|
|
6
|
+
"deploy": {"namespace": "example", "overlay": "k8s", "gate": "DEPLOY_EXAMPLE",
|
|
7
|
+
"units": {"other": {"namespace": "example-other", "overlay": "k8s-other",
|
|
8
|
+
"gate": "DEPLOY_OTHER", "host": "other.example.eu"}},
|
|
9
|
+
"needs": [...]}
|
|
10
|
+
|
|
11
|
+
They share the workflow, and every need with no `unit`; `"unit": "other"`
|
|
12
|
+
scopes a need to one. A unit takes nothing of the top level's namespace,
|
|
13
|
+
overlay, gate or host.
|
|
14
|
+
|
|
15
|
+
When the workflow calls pubdeploy's k8s-deploy.yml, its `deploys` lines and
|
|
16
|
+
the units must agree: each unit's overlay and namespace (the unit the
|
|
17
|
+
server's program deploys, and the namespace it deploys into) a line, and
|
|
18
|
+
each line a unit. k8s-deploy.yml deploys what a run of the CI workflow
|
|
19
|
+
tested, so the workflow must start on `workflow_run` of it, with `actions:
|
|
20
|
+
read`, and on `workflow_dispatch`, by which the gate's and the rollout's
|
|
21
|
+
fulfils run it; `pubdeploy bump` rewrites a caller started by `push`
|
|
22
|
+
(`retrigger`).
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import re
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
from typing import Any
|
|
30
|
+
|
|
31
|
+
import yaml
|
|
32
|
+
|
|
33
|
+
from .schema import DEFAULT_OVERLAY, DEFAULT_WORKFLOW, WORKFLOWS
|
|
34
|
+
|
|
35
|
+
OWN = ("namespace", "overlay", "gate", "host")
|
|
36
|
+
K8S_DEPLOY = re.compile(r"(?P<owner>[A-Za-z0-9][A-Za-z0-9-]*)/pubdeploy/\.github/workflows/k8s-deploy\.ya?ml@"
|
|
37
|
+
r"(?:v|[0-9a-f]{40}[ \t]+#[ \t]*v)(?P<version>[0-9][^\s'\"]*)")
|
|
38
|
+
CI = "main" # the CI workflow pubrules sync writes, by name
|
|
39
|
+
TRIGGER = " workflow_run: { workflows: [main], types: [completed], branches: [main] }"
|
|
40
|
+
DISPATCH = " workflow_dispatch: {}"
|
|
41
|
+
PERMISSIONS = "permissions: { actions: read, contents: read, packages: write }"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def names(block: dict[str, Any]) -> list[str]:
|
|
45
|
+
"""The units, by name: the top level's first."""
|
|
46
|
+
return [block["namespace"], *sorted(block.get("units") or {})]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def unit_block(block: dict[str, Any], unit: str) -> dict[str, Any]:
|
|
50
|
+
"""The block as one unit sees it: its own namespace, overlay, gate and host."""
|
|
51
|
+
shared = {k: v for k, v in block.items() if k not in ("units", *OWN)}
|
|
52
|
+
if unit == block["namespace"]:
|
|
53
|
+
own = {k: block[k] for k in OWN if k in block}
|
|
54
|
+
else:
|
|
55
|
+
own = dict((block.get("units") or {})[unit])
|
|
56
|
+
return {**shared, **own, "unit": unit}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def deploys(text: str) -> list[tuple[str, str]] | None:
|
|
60
|
+
"""The (overlay, unit) of each deploys line of a workflow calling
|
|
61
|
+
k8s-deploy.yml, or None when it calls none."""
|
|
62
|
+
try:
|
|
63
|
+
found = yaml.safe_load(text)
|
|
64
|
+
except yaml.YAMLError:
|
|
65
|
+
return None
|
|
66
|
+
jobs = found.get("jobs") if isinstance(found, dict) else None
|
|
67
|
+
out = None
|
|
68
|
+
for job in (jobs or {}).values():
|
|
69
|
+
if isinstance(job, dict) and K8S_DEPLOY.search(str(job.get("uses", ""))):
|
|
70
|
+
given = str((job.get("with") or {}).get("deploys", ""))
|
|
71
|
+
out = (out or []) + [tuple(line.split()[:2]) for line in given.splitlines() if len(line.split()) >= 2]
|
|
72
|
+
return out
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def problems(root: Path, block: dict[str, Any]) -> list[str]:
|
|
76
|
+
"""What is wrong with the workflow for the units: none when it calls no k8s-deploy.yml."""
|
|
77
|
+
workflow = block.get("workflow", DEFAULT_WORKFLOW)
|
|
78
|
+
path = root / WORKFLOWS / workflow
|
|
79
|
+
try:
|
|
80
|
+
text = path.read_text(encoding="utf-8")
|
|
81
|
+
except OSError:
|
|
82
|
+
return []
|
|
83
|
+
out = trigger_problems(text, workflow)
|
|
84
|
+
lines = deploys(text)
|
|
85
|
+
if lines is None:
|
|
86
|
+
return out
|
|
87
|
+
units = {(unit_block(block, u).get("overlay", DEFAULT_OVERLAY), unit_block(block, u)["namespace"]): u
|
|
88
|
+
for u in names(block)}
|
|
89
|
+
for (overlay, ns), unit in units.items():
|
|
90
|
+
if (overlay, ns) not in lines:
|
|
91
|
+
out.append(f"deploy: unit {unit} ({overlay}, namespace {ns}) is no deploys line of {workflow}: "
|
|
92
|
+
f"add \"{overlay} {ns}\" to its deploys")
|
|
93
|
+
for overlay, ns in lines:
|
|
94
|
+
if (overlay, ns) not in units:
|
|
95
|
+
out.append(f"deploy: {workflow}'s deploys line \"{overlay} {ns}\" is no unit of the deploy block: "
|
|
96
|
+
f"add it to deploy.units, or drop the line")
|
|
97
|
+
return out
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def trigger_problems(text: str, workflow: str) -> list[str]:
|
|
101
|
+
"""A caller of k8s-deploy.yml starts on workflow_run of CI, with actions: read, and on workflow_dispatch."""
|
|
102
|
+
m = K8S_DEPLOY.search(text)
|
|
103
|
+
if not m:
|
|
104
|
+
return []
|
|
105
|
+
try:
|
|
106
|
+
found = yaml.safe_load(text) or {}
|
|
107
|
+
except yaml.YAMLError:
|
|
108
|
+
return []
|
|
109
|
+
on = found.get("on", found.get(True)) if isinstance(found, dict) else None # YAML 1.1 reads `on` as true
|
|
110
|
+
run = on.get("workflow_run") if isinstance(on, dict) else None
|
|
111
|
+
runs = isinstance(run, dict) and CI in (run.get("workflows") or [])
|
|
112
|
+
dispatched = isinstance(on, dict) and "workflow_dispatch" in on
|
|
113
|
+
permissions = found.get("permissions") if isinstance(found, dict) else None
|
|
114
|
+
reads = isinstance(permissions, dict) and permissions.get("actions") in ("read", "write")
|
|
115
|
+
if runs and reads and dispatched:
|
|
116
|
+
return []
|
|
117
|
+
return [f"deploy: {workflow} calls k8s-deploy.yml, which deploys what a run of {CI} "
|
|
118
|
+
f"tested, and a walk runs it by hand: under on, have\n{TRIGGER}\n{DISPATCH}\nand\n{PERMISSIONS}"]
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def retrigger(text: str) -> str:
|
|
122
|
+
"""A caller of k8s-deploy.yml started by `push`, started by `workflow_run`
|
|
123
|
+
of CI instead, with `actions: read`: the two lines trigger_problems names.
|
|
124
|
+
Text it cannot read as one of those shapes is left as it is."""
|
|
125
|
+
lines = text.splitlines(keepends=True)
|
|
126
|
+
out, i = [], 0
|
|
127
|
+
while i < len(lines):
|
|
128
|
+
line = lines[i]
|
|
129
|
+
if re.match(r"^on:\s*(#.*)?$", line):
|
|
130
|
+
out.append(line)
|
|
131
|
+
i += 1
|
|
132
|
+
body = []
|
|
133
|
+
while i < len(lines) and (lines[i].startswith((" ", "\t")) or not lines[i].strip()):
|
|
134
|
+
body.append(lines[i])
|
|
135
|
+
i += 1
|
|
136
|
+
kept, in_push = [], False
|
|
137
|
+
for b in body:
|
|
138
|
+
if re.match(r"^\s{1,3}\S", b):
|
|
139
|
+
in_push = bool(re.match(r"^\s+push:", b))
|
|
140
|
+
if not in_push:
|
|
141
|
+
kept.append(b)
|
|
142
|
+
if not any(re.match(r"^\s+workflow_dispatch:", b) for b in kept):
|
|
143
|
+
at = next((k for k in range(len(kept), 0, -1) if kept[k - 1].strip()), 0)
|
|
144
|
+
kept.insert(at, DISPATCH + "\n")
|
|
145
|
+
if not any(re.match(r"^\s+workflow_run:", b) for b in body):
|
|
146
|
+
kept.insert(0, TRIGGER + "\n")
|
|
147
|
+
out += kept
|
|
148
|
+
continue
|
|
149
|
+
m = re.match(r"^on:\s*(\[[^\]]*\]|[A-Za-z_]+|\{.*\})\s*$", line)
|
|
150
|
+
if m:
|
|
151
|
+
words = [w.strip() for w in re.split(r"[\[\],{}:]", m[1]) if w.strip() and w.strip() not in ("push", "{}")]
|
|
152
|
+
words = [w for w in words if w not in ("workflow_run", "workflow_dispatch")] + ["workflow_dispatch"]
|
|
153
|
+
out += ["on:\n", TRIGGER + "\n", *[f" {w}: {{}}\n" for w in words]]
|
|
154
|
+
i += 1
|
|
155
|
+
continue
|
|
156
|
+
flow = re.match(r"^permissions:\s*\{(.*)\}\s*$", line)
|
|
157
|
+
if flow and "actions:" not in flow[1]:
|
|
158
|
+
out.append(f"permissions: {{ actions: read, {flow[1].strip()} }}\n")
|
|
159
|
+
i += 1
|
|
160
|
+
continue
|
|
161
|
+
if re.match(r"^permissions:\s*$", line):
|
|
162
|
+
out.append(line)
|
|
163
|
+
i += 1
|
|
164
|
+
block = []
|
|
165
|
+
while i < len(lines) and lines[i].startswith((" ", "\t")):
|
|
166
|
+
block.append(lines[i])
|
|
167
|
+
i += 1
|
|
168
|
+
if not any(re.match(r"^\s+actions:", b) for b in block):
|
|
169
|
+
block.insert(0, " actions: read\n")
|
|
170
|
+
out += block
|
|
171
|
+
continue
|
|
172
|
+
out.append(line)
|
|
173
|
+
i += 1
|
|
174
|
+
text = "".join(out)
|
|
175
|
+
if not re.search(r"^permissions:", text, re.M):
|
|
176
|
+
text = re.sub(r"^(jobs:)", PERMISSIONS + "\n\\1", text, count=1, flags=re.M)
|
|
177
|
+
return text
|