-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocker_build.py
More file actions
574 lines (513 loc) · 21.5 KB
/
docker_build.py
File metadata and controls
574 lines (513 loc) · 21.5 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import logging
import re
import traceback
import docker
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from constants import (
BASE_IMAGE_BUILD_DIR,
ENV_IMAGE_BUILD_DIR,
INSTANCE_IMAGE_BUILD_DIR,
MAP_VERSION_TO_INSTALL,
)
from test_spec import (
get_test_specs_from_dataset,
make_test_spec,
TestSpec
)
from docker_utils import (
cleanup_container,
remove_image,
find_dependent_images
)
ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
class BuildImageError(Exception):
def __init__(self, image_name, message, logger):
super().__init__(message)
self.image_name = image_name
self.log_path = logger.log_file
self.logger = logger
def __str__(self):
log_msg = traceback.format_exc()
self.logger.info(log_msg)
return (
f"{self.image_name}: {super().__str__()}\n"
f"Check ({self.log_path}) for more information."
)
def setup_logger(instance_id: str, log_file: Path, mode="w"):
"""
This logger is used for logging the build process of images and containers.
It writes logs to the log file.
"""
log_file.parent.mkdir(parents=True, exist_ok=True)
logger = logging.getLogger(f"{instance_id}.{log_file.name}")
handler = logging.FileHandler(log_file, mode=mode)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.propagate = False
setattr(logger, "log_file", log_file)
return logger
def close_logger(logger):
# To avoid too many open files
for handler in logger.handlers:
handler.close()
logger.removeHandler(handler)
def build_image(
image_name: str,
setup_scripts: dict,
dockerfile: str,
platform: str,
client: docker.DockerClient,
build_dir: Path,
from_hub: bool ,
nocache: bool = False,
):
"""
Builds a docker image with the given name, setup scripts, dockerfile, and platform.
Args:
image_name (str): Name of the image to build
setup_scripts (dict): Dictionary of setup script names to setup script contents
dockerfile (str): Contents of the Dockerfile
platform (str): Platform to build the image for
client (docker.DockerClient): Docker client to use for building the image
build_dir (Path): Directory for the build context (will also contain logs, scripts, and artifacts)
nocache (bool): Whether to use the cache when building
"""
# Create a logger for the build process
logger = setup_logger(image_name, build_dir / "build_image.log")
if from_hub:
try:
logger.info(f"[PULL] Trying to pull {image_name} from remote registry…")
remote_tag = image_name.replace('.', '_').replace(':', '_')
remote_image_name = f"guolianghong/omnigirl:{remote_tag}"
# 拉取远程镜像
pulled_image = client.images.pull(remote_image_name, platform=platform)
# 给它打上本地需要的 tag(恢复为 image_name)
client.images.get(pulled_image.id).tag(image_name)
logger.info(f"[TAG] Retagged pulled image as: {image_name}")
# 删除原 remote tag
client.images.remove(remote_image_name, force=True)
logger.info(f"[CLEANUP] Removed temporary tag: {remote_image_name}")
close_logger(logger)
return # 成功拉取并转换,跳过构建
except Exception as e:
logger.warning(f"[WARN] Pull failed or retag failed: {e} – fallback to local build.")
logger.info("Fail to pull image from hub, try build to build the image locally")
logger.info(
f"Building image {image_name}\n"
f"Using dockerfile:\n{dockerfile}\n"
f"Adding ({len(setup_scripts)}) setup scripts to image build repo"
)
for setup_script_name, setup_script in setup_scripts.items():
logger.info(f"[SETUP SCRIPT] {setup_script_name}:\n{setup_script}")
try:
# Write the setup scripts to the build directory
for setup_script_name, setup_script in setup_scripts.items():
setup_script_path = build_dir / setup_script_name
with open(setup_script_path, "w") as f:
f.write(setup_script)
if setup_script_name not in dockerfile:
logger.warning(
f"Setup script {setup_script_name} may not be used in Dockerfile"
)
# Write the dockerfile to the build directory
dockerfile_path = build_dir / "Dockerfile"
with open(dockerfile_path, "w") as f:
f.write(dockerfile)
# Build the image
logger.info(
f"Building docker image {image_name} in {build_dir} with platform {platform}"
)
response = client.api.build(
path=str(build_dir),
tag=image_name,
rm=True,
forcerm=True,
decode=True,
platform=platform,
nocache=nocache,
)
# Log the build process continuously
buildlog = ""
for chunk in response:
if "stream" in chunk:
# Remove ANSI escape sequences from the log
chunk_stream = ansi_escape.sub("", chunk["stream"])
logger.info(chunk_stream.strip())
buildlog += chunk_stream
elif "errorDetail" in chunk:
# Decode error message, raise BuildError
logger.error(
f"Error: {ansi_escape.sub('', chunk['errorDetail']['message'])}"
)
raise docker.errors.BuildError(
chunk["errorDetail"]["message"], buildlog
)
logger.info("Image built successfully!")
except docker.errors.BuildError as e:
logger.error(f"docker.errors.BuildError during {image_name}: {e}")
raise BuildImageError(image_name, str(e), logger) from e
except Exception as e:
logger.error(f"Error building image {image_name}: {e}")
raise BuildImageError(image_name, str(e), logger) from e
finally:
close_logger(logger) # functions that create loggers should close them
def build_base_images(
client: docker.DockerClient,
dataset: list,
from_hub: bool,
force_rebuild: bool = False
):
"""
Builds the base images required for the dataset if they do not already exist.
Args:
client (docker.DockerClient): Docker client to use for building the images
dataset (list): List of test specs or dataset to build images for
force_rebuild (bool): Whether to force rebuild the images even if they already exist
"""
# Get the base images to build from the dataset
test_specs = get_test_specs_from_dataset(dataset)
base_images = {
x.base_image_key: (x.base_dockerfile, x.platform) for x in test_specs
}
if force_rebuild:
for key in base_images:
remove_image(client, key, "quiet")
# Build the base images
for image_name, (dockerfile, platform) in base_images.items():
try:
# Check if the base image already exists
client.images.get(image_name)
if force_rebuild:
# Remove the base image if it exists and force rebuild is enabled
remove_image(client, image_name, "quiet")
else:
print(f"Base image {image_name} already exists, skipping build.")
continue
except docker.errors.ImageNotFound:
pass
# Build the base image (if it does not exist or force rebuild is enabled)
print(f"Building base image ({image_name})")
build_image(
image_name=image_name,
setup_scripts={},
dockerfile=dockerfile,
platform=platform,
client=client,
build_dir=BASE_IMAGE_BUILD_DIR / image_name.replace(":", "__"),
from_hub=from_hub,
)
print("Base images built successfully.")
def get_env_configs_to_build(
client: docker.DockerClient,
dataset: list,
):
"""
Returns a dictionary of image names to build scripts and dockerfiles for environment images.
Returns only the environment images that need to be built.
Args:
client (docker.DockerClient): Docker client to use for building the images
dataset (list): List of test specs or dataset to build images for
"""
image_scripts = dict()
base_images = dict()
test_specs = get_test_specs_from_dataset(dataset)
print(len(test_specs))
for test_spec in test_specs:
# Check if the base image exists
try:
if test_spec.base_image_key not in base_images:
base_images[test_spec.base_image_key] = client.images.get(
test_spec.base_image_key
)
base_image = base_images[test_spec.base_image_key]
except docker.errors.ImageNotFound:
raise Exception(
f"Base image {test_spec.base_image_key} not found for {test_spec.env_image_key}\n."
"Please build the base images first."
)
# Check if the environment image exists
image_exists = False
try:
env_image = client.images.get(test_spec.env_image_key)
image_exists = True
if env_image.attrs["Created"] < base_image.attrs["Created"]:
# Remove the environment image if it was built after the base_image
for dep in find_dependent_images(env_image):
# Remove instance images that depend on this environment image
remove_image(client, dep.image_id, "quiet")
remove_image(client, test_spec.env_image_key, "quiet")
image_exists = False
except docker.errors.ImageNotFound:
pass
if not image_exists:
# Add the environment image to the list of images to build
image_scripts[test_spec.env_image_key] = {
"setup_script": test_spec.setup_env_script,
"dockerfile": test_spec.env_dockerfile,
"platform": test_spec.platform,
}
# print(image_scripts[list(image_scripts.keys())[0]])
# print(image_scripts[list(image_scripts.keys())[1]])
# print(image_scripts[list(image_scripts.keys())[2]])
# input()
return image_scripts
def build_env_images(
client: docker.DockerClient,
dataset: list,
from_hub: bool,
force_rebuild: bool = False,
max_workers: int = 4
):
"""
Builds the environment images required for the dataset if they do not already exist.
Args:
client (docker.DockerClient): Docker client to use for building the images
dataset (list): List of test specs or dataset to build images for
force_rebuild (bool): Whether to force rebuild the images even if they already exist
max_workers (int): Maximum number of workers to use for building images
"""
# Get the environment images to build from the dataset
if force_rebuild:
env_image_keys = {x.env_image_key for x in get_test_specs_from_dataset(dataset)}
for key in env_image_keys:
remove_image(client, key, "quiet")
build_base_images(client, dataset, from_hub, force_rebuild)
configs_to_build = get_env_configs_to_build(client, dataset)
if len(configs_to_build) == 0:
print("No environment images need to be built.")
return [], []
print(f"Total environment images to build: {len(configs_to_build)}")
# Build the environment images
successful, failed = list(), list()
with tqdm(
total=len(configs_to_build), smoothing=0, desc="Building environment images"
) as pbar:
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Create a future for each image to build
futures = {
executor.submit(
build_image,
image_name,
{"setup_env.sh": config["setup_script"]},
config["dockerfile"],
config["platform"],
client,
ENV_IMAGE_BUILD_DIR / image_name.replace(":", "__"),
from_hub,
): image_name
for image_name, config in configs_to_build.items()
}
# Wait for each future to complete
for future in as_completed(futures):
pbar.update(1)
try:
# Update progress bar, check if image built successfully
future.result()
successful.append(futures[future])
except BuildImageError as e:
print(f"BuildImageError {e.image_name}")
traceback.print_exc()
failed.append(futures[future])
continue
except Exception as e:
print(f"Error building image")
traceback.print_exc()
failed.append(futures[future])
continue
# Show how many images failed to build
if len(failed) == 0:
print("All environment images built successfully.")
else:
print(f"{len(failed)} environment images failed to build.")
# Return the list of (un)successfuly built images
return successful, failed
def build_instance_images(
client: docker.DockerClient,
dataset: list,
from_hub: bool,
force_rebuild: bool = False,
max_workers: int = 4
):
"""
Builds the instance images required for the dataset if they do not already exist.
Args:
dataset (list): List of test specs or dataset to build images for
client (docker.DockerClient): Docker client to use for building the images
force_rebuild (bool): Whether to force rebuild the images even if they already exist
max_workers (int): Maximum number of workers to use for building images
"""
# Build environment images (and base images as needed) first
test_specs = list(map(make_test_spec, dataset))
test_specs = [test_spec for test_spec in test_specs if test_spec!=None]
if force_rebuild:
for spec in test_specs:
remove_image(client, spec.instance_image_key, "quiet")
_, env_failed = build_env_images(client, test_specs, from_hub,force_rebuild, max_workers)
if len(env_failed) > 0:
# Don't build images for instances that depend on failed-to-build env images
dont_run_specs = [spec for spec in test_specs if spec.env_image_key in env_failed]
test_specs = [spec for spec in test_specs if spec.env_image_key not in env_failed]
print(f"Skipping {len(dont_run_specs)} instances - due to failed env image builds")
print(f"Building instance images for {len(test_specs)} instances")
successful, failed = list(), list()
# Build the instance images
with tqdm(
total=len(test_specs), smoothing=0, desc="Building instance images"
) as pbar:
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Create a future for each image to build
futures = {
executor.submit(
build_instance_image,
test_spec,
client,
None, # logger is created in build_instance_image, don't make loggers before you need them
from_hub,
False,
): test_spec
for test_spec in test_specs
}
# Wait for each future to complete
for future in as_completed(futures):
pbar.update(1)
try:
# Update progress bar, check if image built successfully
future.result()
successful.append(futures[future])
except BuildImageError as e:
print(f"BuildImageError {e.image_name}")
traceback.print_exc()
failed.append(futures[future])
continue
except Exception as e:
print(f"Error building image")
traceback.print_exc()
failed.append(futures[future])
continue
# Show how many images failed to build
if len(failed) == 0:
print("All instance images built successfully.")
else:
print(f"{len(failed)} instance images failed to build.")
# Return the list of (un)successfuly built images
return successful, failed
def build_instance_image(
test_spec: TestSpec,
client: docker.DockerClient,
logger: logging.Logger,
from_hub: bool,
nocache: bool,
):
"""
Builds the instance image for the given test spec if it does not already exist.
Args:
test_spec (TestSpec): Test spec to build the instance image for
client (docker.DockerClient): Docker client to use for building the image
logger (logging.Logger): Logger to use for logging the build process
nocache (bool): Whether to use the cache when building
"""
# Set up logging for the build process
build_dir = INSTANCE_IMAGE_BUILD_DIR / test_spec.instance_image_key.replace(":", "__")
new_logger = False
if logger is None:
new_logger = True
logger = setup_logger(test_spec.instance_id, build_dir / "prepare_image.log")
# Get the image names and dockerfile for the instance image
image_name = test_spec.instance_image_key
env_image_name = test_spec.env_image_key
dockerfile = test_spec.instance_dockerfile
# Check that the env. image the instance image is based on exists
try:
env_image = client.images.get(env_image_name)
except docker.errors.ImageNotFound as e:
raise BuildImageError(
test_spec.instance_id,
f"Environment image {env_image_name} not found for {test_spec.instance_id}",
logger,
) from e
logger.info(
f"Environment image {env_image_name} found for {test_spec.instance_id}\n"
f"Building instance image {image_name} for {test_spec.instance_id}"
)
# Check if the instance image already exists
image_exists = False
try:
instance_image = client.images.get(image_name)
if instance_image.attrs["Created"] < env_image.attrs["Created"]:
# the environment image is newer than the instance image, meaning the instance image may be outdated
remove_image(client, image_name, "quiet")
image_exists = False
else:
image_exists = True
except docker.errors.ImageNotFound:
pass
# Build the instance image
if not image_exists:
build_image(
image_name=image_name,
setup_scripts={
"setup_repo.sh": test_spec.install_repo_script,
},
dockerfile=dockerfile,
platform=test_spec.platform,
client=client,
build_dir=build_dir,
from_hub=from_hub,
nocache=nocache,
)
else:
logger.info(f"Image {image_name} already exists, skipping build.")
if new_logger:
close_logger(logger)
def build_container(
test_spec: TestSpec,
client: docker.DockerClient,
run_id: str,
logger: logging.Logger,
from_hub: bool,
nocache: bool,
force_rebuild: bool = False
):
"""
Builds the instance image for the given test spec and creates a container from the image.
Args:
test_spec (TestSpec): Test spec to build the instance image and container for
client (docker.DockerClient): Docker client for building image + creating the container
run_id (str): Run ID identifying process, used for the container name
logger (logging.Logger): Logger to use for logging the build process
nocache (bool): Whether to use the cache when building
force_rebuild (bool): Whether to force rebuild the image even if it already exists
"""
# Build corresponding instance image
if force_rebuild:
remove_image(client, test_spec.instance_image_key, "quiet")
build_instance_image(test_spec, client, logger, from_hub, nocache)
container = None
try:
# Get configurations for how container should be created
config = MAP_VERSION_TO_INSTALL[test_spec.repo][test_spec.version]
user = "root" if not config.get("execute_test_as_nonroot", False) else "nonroot"
nano_cpus = config.get("nano_cpus")
# Create the container
logger.info(f"Creating container for {test_spec.instance_id}...")
container = client.containers.create(
image=test_spec.instance_image_key,
name=test_spec.get_instance_container_name(run_id),
user=user,
detach=True,
command="tail -f /dev/null",
nano_cpus=nano_cpus,
platform=test_spec.platform,
)
logger.info(f"Container for {test_spec.instance_id} created: {container.id}")
return container
except Exception as e:
# If an error occurs, clean up the container and raise an exception
logger.error(f"Error creating container for {test_spec.instance_id}: {e}")
logger.info(traceback.format_exc())
cleanup_container(client, container, logger)
raise BuildImageError(test_spec.instance_id, str(e), logger) from e