-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.py
executable file
·379 lines (317 loc) · 12.8 KB
/
build.py
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
from __future__ import annotations
from argparse import ArgumentParser, Namespace
from datetime import datetime
try:
from packaging.version import LegacyVersion as LooseVersion
except Exception:
from distutils.version import LooseVersion
from pathlib import Path
from typing import Any, Generator, Optional
import dataclasses
import grp
import itertools
import json
import os
import re
import subprocess
QEMU_IMAGE = 'docker.io/multiarch/qemu-user-static:latest'
BUILDER_NAME = 'multiarch'
FALLBACK_PLATFORM = 'linux/amd64'
DOCKERFILE_DEPENDENCY_PATTERNS = (
re.compile(r'from\s+(?:--platform=[^\s]+\s+)?(?P<image>[^\s:]+)(?::(?P<tag>[^\s]+))?(?:\s+as\s+[^\s]+)?'),
re.compile(r'copy\s+--from=(?P<image>[^\s:]+)(?::(?P<tag>[^\s]+))?\s+.*'),
)
WORKFLOW_PATH = '.github/workflows/ci.yaml'
@dataclasses.dataclass
class Configuration:
workdir: Path
cache: Path
options: dict[str, Any]
@classmethod
def load(cls, project: Path) -> Configuration:
options = read_json(project/'default.json')
options['registry']['password'] = os.environ.get('GITHUB_TOKEN')
return cls(
project,
project/'.cache',
options,
)
def setup_cache(self) -> None:
self.cache.mkdir(exist_ok=True)
@staticmethod
def setup_docker_buildx() -> None:
docker('run', '--rm', '--privileged', QEMU_IMAGE, '--reset', '--persistent', 'yes')
try:
docker('buildx', 'inspect', BUILDER_NAME)
except RuntimeError:
docker('buildx', 'create', '--name', BUILDER_NAME, '--use')
docker('buildx', 'inspect', '--bootstrap', BUILDER_NAME)
@property
def authenticated(self) -> bool:
return self.options['registry']['name'] and self.options['registry']['user'] and self.options['registry']['password']
def registry_login(self) -> None:
if self.authenticated:
docker('login', self.options['registry']['name'], '--username', self.options['registry']['user'], '--password-stdin', stdin=self.options['registry']['password'])
def registry_logout(self) -> None:
if self.authenticated:
docker('logout', self.options['registry']['name'], check=False)
@dataclasses.dataclass
class BuildInfo:
source: str
version: re.Pattern
correction: dict[str, str]
platforms: list[str]
labels: dict[str, str]
registry: dict[str, str]
@classmethod
def from_file(cls, path: Path) -> BuildInfo:
attrs = CONFIG.options.copy()
if path.exists():
override = read_json(path)
labels = attrs['labels'].copy()
labels.update(override.get('labels', dict()))
attrs.update(override, labels=labels)
attrs['version'] = re.compile(attrs['version'])
return BuildInfo(**attrs)
@dataclasses.dataclass
class Repository:
path: Path
def clone(self, build_info: BuildInfo) -> VersionInfo:
git_dir = self.path/'.git'
if git_dir.is_dir():
git('-C', self.path, 'reset', '--hard', 'origin/HEAD')
git('-C', self.path, 'pull', 'origin', 'HEAD', '--rebase')
else:
git('clone', build_info.source, self.path)
latest_ref, latest_version = self._find_lastest_ref(build_info.version)
latest_version = self._correct_version(latest_version, build_info.correction)
return VersionInfo(
latest_ref,
latest_version,
git('-C', self.path, 'rev-list', '--max-count=1', latest_ref, capture=True),
git('-C', self.path, 'rev-parse', 'HEAD', capture=True),
)
def checkout(self, version_info: VersionInfo) -> None:
git('-C', self.path, 'checkout', '--quiet', version_info.latest_ref)
def _find_lastest_ref(self, version_regex: re.Pattern) -> tuple[str, str]:
lines = git('-C', self.path, 'show-ref', capture=True).splitlines()
refs = []
for line in lines:
words = line.split(' ', maxsplit=1)
if len(words) != 2:
raise RuntimeError('could not parse output from git show-ref')
_, ref = words
refs.append(ref)
matches = list()
for ref in refs:
match = version_regex.fullmatch(ref)
if match:
matches.append((ref, match))
versions = [(ref, match.group(1)) for ref, match in matches]
sorted_versions = sorted(versions, key=lambda pair: LooseVersion(pair[1]))
latest_ref, latest_version = sorted_versions[-1]
return latest_ref, latest_version
@staticmethod
def _correct_version(version: str, correction: dict[str, str]) -> str:
return version.replace(correction['search'], correction['replace'])
@dataclasses.dataclass
class VersionInfo:
latest_ref: str
latest_version: str
latest_commit: str
current_commit: str
@classmethod
def empty(cls):
return cls('', '', '', '')
@dataclasses.dataclass
class ContainerImage:
context: Path
dockerfile: Path
tag: str
build_info: BuildInfo
repo: Repository
@classmethod
def from_file(cls, dockerfile: Path) -> ContainerImage:
return cls(dockerfile.parent, dockerfile, cls._get_tag_from_dockerfile(dockerfile), BuildInfo.from_file(dockerfile.parent/'build.json'), Repository(dockerfile.parent/'src'))
@staticmethod
def _get_tag_from_dockerfile(path: Path) -> str:
return 'latest' if path.name == 'Dockerfile' else removesuffix(path.name, '.Dockerfile')
@property
def name(self) -> str:
return '/'.join(item for item in (CONFIG.options['registry']['name'], CONFIG.options['registry']['user'], self.context.name) if item)
def build(self) -> None:
if self.build_info.source:
version_info = self.repo.clone(self.build_info)
self.repo.checkout(version_info)
else:
version_info = VersionInfo.empty()
self._build_image(version_info)
def _build_image(self, version_info: VersionInfo) -> None:
tags = []
for tag in self._generate_image_tags(version_info):
tags.extend(('--tag', f'{self.name}:{tag}'))
labels = []
for key, value in self._generate_image_labels(version_info).items():
labels.extend(('--label', f'{key}={value}'))
docker(
'buildx', 'build',
'--cache-from', f'type=local,src={CONFIG.cache}',
'--cache-to', f'type=local,dest={CONFIG.cache}',
'--platform', ','.join(self.build_info.platforms) if CONFIG.authenticated else FALLBACK_PLATFORM,
*tags,
*labels,
'--push' if CONFIG.authenticated else '--load',
'--file', self.dockerfile,
self.context,
)
def _generate_image_tags(self, version_info: VersionInfo) -> Generator[str, None, None]:
yield self.tag
if version_info.latest_version:
yield from itertools.accumulate(
version_info.latest_version.split('.'),
lambda a, b: f'{self.tag}-{a}.{b}' if self.tag != 'latest' else f'{a}.{b}' if a else b,
)
def _generate_image_labels(self, version_info: VersionInfo):
timestamp = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
labels = {
key: value.format(
image=self,
build=self.build_info,
version=version_info,
timestamp=timestamp,
)
for key, value in self.build_info.labels.items()
}
labels = {key: value for key, value in labels.items() if value}
return labels
def main():
entrypoint = ArgumentParser()
entrypoint.add_argument('-w', '--workdir', type=Path, default=Path.cwd(), metavar='DIR')
subparsers = entrypoint.add_subparsers(dest='command', required=True)
parser = subparsers.add_parser('image')
parser.add_argument('image', nargs='+', type=Path, metavar='DOCKERFILE')
parser = subparsers.add_parser('workflow')
opts = entrypoint.parse_args()
global CONFIG
CONFIG = Configuration.load(opts.workdir)
commands = dict(
image=build_images,
workflow=write_workflow,
)
commands[opts.command](opts)
def build_images(opts: Namespace) -> None:
CONFIG.setup_cache()
CONFIG.setup_docker_buildx()
CONFIG.registry_login()
try:
for path in opts.image:
image = ContainerImage.from_file(path)
image.build()
finally:
CONFIG.registry_logout()
@dataclasses.dataclass
class Job:
dockerfile: Path
build_info: BuildInfo
@classmethod
def from_file(cls, dockerfile: Path) -> Job:
return cls(dockerfile, BuildInfo.from_file(dockerfile.parent/'build.json'))
@property
def dependencies(self) -> list[str]:
prefix = f'{CONFIG.options["registry"]["name"]}/{CONFIG.options["registry"]["user"]}/'
return list(sorted(set(self._extract_dependencies(prefix))))
@property
def name(self) -> str:
value = removesuffix(self.dockerfile.as_posix(), '/Dockerfile')
value = removesuffix(value, '.Dockerfile')
return value.replace('/', '-')
def _extract_dependencies(self, prefix: str) -> Generator[str, None, None]:
with open(self.dockerfile) as file:
for line in file:
image, tag = self._match_dependency_pattern(line)
if image and image.startswith(prefix):
image_name = image.removeprefix(prefix)
path = Path(f'{image_name}/{tag}.Dockerfile')
yield f'{image_name}-{tag}' if path.is_file() else image_name
@staticmethod
def _match_dependency_pattern(line: str) -> tuple[Optional[str], Optional[str]]:
for pattern in DOCKERFILE_DEPENDENCY_PATTERNS:
match = pattern.fullmatch(line.strip().lower())
if not match:
continue
return match.group('image'), match.group('tag')
return None, None
def generate(self) -> dict[str, Any]:
return {
'runs-on': 'ubuntu-22.04',
'needs': self.dependencies,
'steps': [
{
'name': 'Checkout',
'uses': 'actions/checkout@v4',
'with': {
'fetch-depth': 0,
}
},
{
'name': 'Build, Test & Push',
'run': f'./build.py image {self.dockerfile}',
'env': {
'GITHUB_TOKEN': '${{ secrets.GITHUB_TOKEN }}'
},
},
],
}
def write_workflow(opts: Namespace) -> None:
import yaml
jobs = [
Job.from_file(path.relative_to(opts.workdir))
for path in sorted(
itertools.chain(
opts.workdir.glob('*/Dockerfile'),
opts.workdir.glob('*/*.Dockerfile'),
)
)
]
data = {
'name': 'CI',
'on': {
'push': {
'branches': [
'main',
],
},
'schedule': [
{'cron': '0 3 * * *'},
],
},
'jobs': {job.name: job.generate() for job in jobs},
}
text = yaml.safe_dump(data, indent=2, sort_keys=False)
# remove quotes around `on` key
text = text.replace("\n'on':\n", '\non:\n')
CONFIG.workdir.joinpath(WORKFLOW_PATH).write_text(text)
def docker(*args: Any, **kwargs: Any) -> str:
groups = {grp.getgrgid(gid).gr_name for gid in os.getgroups()}
sudo = () if 'docker' in groups else ('sudo', '--preserve-env=DOCKER_CLI_EXPERIMENTAL,BUILDX_NO_DEFAULT_LOAD,DOCKER_HOST')
envvars = dict(DOCKER_CLI_EXPERIMENTAL='enabled', BUILDX_NO_DEFAULT_LOAD='false', DOCKER_HOST=os.environ.get('DOCKER_HOST', ''))
return run(*sudo, 'docker', *args, env=envvars, **kwargs)
def git(*args: Any, **kwargs: Any) -> str:
return run('git', *args, **kwargs)
def run(*args: Any, stdin: str = None, capture: bool = False, check: bool = True, env: dict[str, Any] = None) -> str:
args = tuple(str(item) for item in args if item is not None)
cmdline = ' '.join(args)
print('>', cmdline, flush=True)
environment = dict(os.environ, **env) if env else None
process = subprocess.run(args, input=stdin, text=True, check=False, capture_output=capture, env=environment)
if check and process.returncode != 0:
message = f'{cmdline}: {process.stderr.strip()}' if process.stderr else cmdline
raise RuntimeError(f'subprocess failed with exit code {process.returncode}: {message}')
return process.stdout.strip() if capture else ''
def removesuffix(value: str, suffix: str) -> str:
return value[:-len(suffix)] if value.endswith(suffix) else value
def read_json(path: Path) -> Any:
return json.loads(path.read_text())
if __name__ == '__main__':
main()