-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
313 lines (238 loc) · 10.8 KB
/
build.py
File metadata and controls
313 lines (238 loc) · 10.8 KB
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
"""Copyright (C) Alpine Intuition Sàrl - All Rights Reserved.
This source code is protected under international copyright law. All rights
reserved and protected by the copyright holders.
This file is confidential and only available to authorized individuals with the
permission of the copyright holders. If you encounter this file and do not have
permission, please contact the copyright holders and delete this file.
"""
import logging
import re
import tempfile
from pathlib import Path
from typing import Optional, Union
import docker
from rich.progress import Progress
log = logging.getLogger("i2-build")
class BuildManager:
"""i2 build manager."""
def __init__(self, debug: bool = False):
"""Initialize build manager.
Args:
debug: Optional; Show extensive logs.
Returns:
None.
Raises:
None.
"""
self.client = docker.from_env()
if debug:
log.setLevel(logging.DEBUG)
def _get_worker_class_name(self, content):
"""Get the worker class name.
Verify script & get class name. We cannot import the file since it can
contains unknow packages for the host system so we have to parse the file.
"""
field = "__task_class_name__"
worker_class_names = [li for li in content if field in li]
if len(worker_class_names) == 0:
raise AttributeError(f"Missing field in given script: {field}")
if len(worker_class_names) > 1:
raise AttributeError(f"Multiple field definitions given script: {field}")
worker_class_name = worker_class_names[0].split("=")[-1].strip()
worker_class_name = worker_class_name.replace('"', "").replace("'", "")
sanatized = re.sub("[^A-Za-z0-9_]+", "", worker_class_name)
if worker_class_name != sanatized:
raise ValueError(
f"'{field}' must not contain any special characters: {sanatized}"
)
return worker_class_name
def _build_docker_img(
self,
tag: str,
additional_args: dict = {},
):
"""Build the docker image for a worker in current dir.
Args:
tag: The tag for the image to build.
additional_args: Optional; Additional arguments for the build.
Returns:
The tag of the built docker image.
Raises:
docker.errors.BuildError: There was an error during the build (the specific error is printed).
"""
client = docker.APIClient(base_url="unix://var/run/docker.sock")
log.info(f"Building '{tag}'...")
try:
generator = client.build(
path=str(Path.cwd()), tag=tag, decode=True, **additional_args
)
output = generator.__next__()
except docker.errors.APIError as error:
raise docker.errors.BuildError(reason=error.explanation, build_log=error)
# Setup progress bar
num_steps = int(output["stream"].split()[1].split("/")[-1])
with Progress(transient=True) as progress:
task = progress.add_task("Building...", total=num_steps)
stream = re.sub(r" +", " ", output["stream"].strip())
log.info(f"[bold][DOCKER SDK LOG][/bold] {stream}")
progress.update(task, advance=1)
while True:
try:
output = generator.__next__()
if "stream" in output:
stream = re.sub(r" +", " ", output["stream"]).replace("\n", "")
if stream != "":
# remove colors so no issues when printing in console
colorless_stream = stream.replace("[91m", "").replace(
"[0m", ""
)
log.info(
f"[bold][DOCKER SDK LOG][/bold] {colorless_stream}"
)
if "Step" in stream:
progress.update(task, advance=1)
elif "errorDetail" in output: # pragma: no cover
msg = output["errorDetail"]["message"]
reason = stream if stream != "" else msg
raise docker.errors.BuildError(reason=reason, build_log=msg)
except StopIteration:
break
log.info("Building ended successfully!")
return self.client.images.get(tag).id.split(":")[-1]
def _test_task(self, docker_img, worker_class):
"""Test the forward pass of a built worker.
Args:
docker_img: Name of the docker image to test.
worker_class: Name of the worker python class.
Returns:
None.
Raises:
RuntimeError: There was a problem with docker during the tests.
"""
log.info("Testing...")
try:
cmd = (
f"python -c 'from worker_script import {worker_class}; "
+ f"{worker_class}().unit_testing()'"
)
logs = self.client.containers.run(docker_img, cmd, stderr=True)
except docker.errors.APIError as error:
raise RuntimeError(f"Error while task unit testing: \n{error}")
except docker.errors.ContainerError as error:
raise RuntimeError(f"There was a problem during the tests: \n{error}")
if logs != b"":
log.info(f"Starting logs:\n\n{logs.decode()}")
log.info("Testing ended successfully!")
def build_task(
self,
script: Union[Path, str],
dockerfile: Optional[Union[str, Path]] = None,
build_args: str = None,
docker_tag: str = None,
cpu: bool = False,
no_cache: bool = False,
):
"""Build an archipel task.
Args:
script: The worker script.
dockerfile: Optional; Name of the Dockerfile. If none provided, base
image is used.
build_args: Optional; Set build-time variables, like in docker.
docker_tag: Optional; Name and optionally a tag in the 'name:tag' format.
cpu: Optional; Force the use of CPU base image when no dockerfile available.
no_cache: Optional; Do not use previous cache when building the image.
Returns:
None.
Raises:
ValueError: Invalid Dockerfile contents or build arguments.
FileNotFoundError: Invalid locaiton specified for script or Dockerfile.
"""
cwd = Path.cwd()
script = Path(script)
if not script.is_file():
raise FileNotFoundError(f"File not found: {script} (build context: {cwd})")
# Setup task name, if none provided just take the script name
log.info(f"Building task from '{script}'...")
task_name = script.stem
with open(script, "r") as f:
worker_class_name = self._get_worker_class_name(f.readlines())
log.debug(f"Worker class: {worker_class_name}")
# Build docker img
log.debug(f"Build context: {cwd}")
# Check if a dockerfile is given or available (base name 'Dockerfile'). If not
# use the archipe base one (depending on device detected before). In both case,
# we use a temporary file to store dockerfile content in order to add our needed
# commands.
if dockerfile is None:
dockerfile = script.parent / "Dockerfile"
if dockerfile.is_file():
with open(dockerfile, "r") as f:
content = f.read()
else:
device = "cpu" if cpu else "gpu"
log.info(f"No Dockerfile found, use {device.upper()} base image")
img = f"alpineintuition/archipel-base-{device}"
content = f"FROM {img}\n"
else:
dockerfile = Path(dockerfile)
if str(cwd) not in str(dockerfile.resolve()):
raise ValueError(
f"Provided Dockerfile ({dockerfile}) does not exist "
+ f"in build context ({cwd})"
)
if not dockerfile.is_file():
raise FileNotFoundError(
f"Provided dockerfile does not exist: {dockerfile}"
)
with open(dockerfile, "r") as f:
content = f.read()
if cpu and dockerfile.is_file():
log.warning(
"Dockerfile available but cpu mode argument given. "
+ "Dockerfile has priority, cpu mode argument ignored."
)
tmp_dockerfile = tempfile.NamedTemporaryFile()
tmp_dockerfile_path = tmp_dockerfile.name
tmp_dockerfile.write(content.encode())
tmp_dockerfile.write(f"\nCOPY {script} /opt/archipel/worker_script.py".encode())
tmp_dockerfile.seek(0)
docker_tag = f"i2-task-{task_name}:latest" if docker_tag is None else docker_tag
additional_args = {"dockerfile": tmp_dockerfile_path, "nocache": no_cache}
if build_args is not None:
buildargs = {}
for index, build_arg in enumerate(build_args):
splitted_build_arg = build_arg.split("=")
if len(splitted_build_arg) != 2:
raise ValueError(
f"Invalid format for build args {index}. "
+ f"Need to look like 'ARGUMENT=VALUE'. Got: {build_arg}"
)
buildargs[splitted_build_arg[0]] = splitted_build_arg[1]
additional_args["buildargs"] = buildargs
try:
self._build_docker_img(docker_tag, additional_args)
finally:
# if build failed, be sure temp file is close and removed
tmp_dockerfile.close()
self._test_task(docker_tag, worker_class_name)
log.info(f"Building and testing done! (docker tag: '{docker_tag}')")
def verify_task(self, docker_tag):
"""Verify that a built docker image is valid.
Args:
docker_tag: Name of the docker image to test.
Returns:
None.
Raises:
RuntimeError: There was a problem with docker during the tests.
"""
try:
cmd = "cat /opt/archipel/worker_script.py".split()
out = self.client.containers.run(docker_tag, cmd, stderr=True)
worker_class_name = self._get_worker_class_name(out.decode().split("\n"))
log.debug(f"Worker class name: {worker_class_name}")
except docker.errors.ImageNotFound:
raise RuntimeError(f"Image not found: {docker_tag}")
except docker.errors.ContainerError as error:
msg = error.stderr.decode()
raise RuntimeError(f"Error when trying to detect class name: \n {msg}")
self._test_task(docker_tag, worker_class_name)