forked from thuasta/saiblo-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththuai_builder.py
56 lines (42 loc) · 1.67 KB
/
thuai_builder.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
"""Contains docker image build for THUAI."""
from typing import Dict
from pathlib import Path
import string
import random
import docker
import docker.errors
from base_docker_image_builder import BaseDockerImageBuilder
BUILDER_NAME_LENGTH = 10
IMAGE_NAME_PREFIX = "THUAI-image"
class ThuaiBuilder(BaseDockerImageBuilder):
"""Docker image builder for THUAI matches."""
def __init__(self):
self.client = docker.from_env()
self.built_images = {}
# Generate a random string for the builder, to avoid name conflict
chars = string.ascii_letters + string.digits
self.name = "".join(random.choice(chars) for _ in range(BUILDER_NAME_LENGTH))
# ID for image, to avoid name conflict
self.image_id = 0
async def build(self, file_path: Path) -> str:
# if not built yet...
if file_path not in self.built_images:
img_tag = self.get_image_name()
self.client.images.build(path=file_path, tag=img_tag, rm=True)
self.built_images[file_path] = img_tag
return self.built_images[file_path]
async def clean(self) -> None:
for image_tag in self.built_images.values():
self.client.images.remove(image=image_tag)
self.built_images.clear()
async def list(self) -> Dict[Path, str]:
return self.built_images.copy()
async def get_image_name(self) -> str:
"""Get a no-duplicate name for images
Name format: image-{builder name}-{id}
Returns:
The generated image name.
"""
name = IMAGE_NAME_PREFIX + self.name + "-" + str(self.image_id)
self.image_id = self.image_id + 1
return name