-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.py
144 lines (129 loc) · 4.26 KB
/
docker.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
import json
import sys
from urllib.parse import urlparse
from aiodocker import Docker
async def list_images():
"""
Retrieve local images built by repo2docker
"""
async with Docker() as docker:
r2d_images = await docker.images.list(
filters=json.dumps({"dangling": ["false"], "label": ["repo2docker.ref"]})
)
images = [
{
"repo": image["Labels"]["repo2docker.repo"],
"ref": image["Labels"]["repo2docker.ref"],
"image_name": image["Labels"]["tljh_repo2docker.image_name"],
"display_name": image["Labels"]["tljh_repo2docker.display_name"],
"mem_limit": image["Labels"]["tljh_repo2docker.mem_limit"],
"cpu_limit": image["Labels"]["tljh_repo2docker.cpu_limit"],
"status": "built",
}
for image in r2d_images
if "tljh_repo2docker.image_name" in image["Labels"]
]
return images
async def list_containers():
"""
Retrieve the list of local images being built by repo2docker.
Images are built in a Docker container.
"""
async with Docker() as docker:
r2d_containers = await docker.containers.list(
filters=json.dumps({"label": ["repo2docker.ref"]})
)
containers = [
{
"repo": container["Labels"]["repo2docker.repo"],
"ref": container["Labels"]["repo2docker.ref"],
"image_name": container["Labels"]["repo2docker.build"],
"display_name": container["Labels"]["tljh_repo2docker.display_name"],
"mem_limit": container["Labels"]["tljh_repo2docker.mem_limit"],
"cpu_limit": container["Labels"]["tljh_repo2docker.cpu_limit"],
"status": "building",
}
for container in r2d_containers
if "repo2docker.build" in container["Labels"]
]
return containers
def get_repo2docker_cmd(repo, ref, name="", memory=None, cpu=None):
"""
Create the command to build an image with repo2docker given a repo, ref and limits
"""
ref = ref or "master"
if len(ref) >= 40:
ref = ref[:7]
# default to the repo name if no name specified
# and sanitize the name of the docker image
name = name or urlparse(repo).path.strip("/")
name = name.lower().replace("/", "-")
image_name = f"{name}:{ref}"
# memory is specified in GB
memory = f"{memory}G" if memory else ""
cpu = cpu or ""
# add extra labels to set additional image properties
labels = [
f"LABEL tljh_repo2docker.display_name={name}",
f"LABEL tljh_repo2docker.image_name={image_name}",
f"LABEL tljh_repo2docker.mem_limit={memory}",
f"LABEL tljh_repo2docker.cpu_limit={cpu}",
]
cmd = [
sys.executable,
"-m",
"repo2docker",
"--ref",
ref,
"--user-name",
"jovyan",
"--user-id",
"1100",
"--no-run",
"--image-name",
image_name,
"--appendix",
"\n".join(labels),
repo,
]
return cmd, image_name
async def build_image(
repo, ref, name="", memory=None, cpu=None, username=None, password=None
):
"""
Build an image given a repo, ref and limits
"""
cmd, image_name = get_repo2docker_cmd(repo, ref, name, memory, cpu)
config = {
"Cmd": cmd,
"Image": "jupyter/repo2docker:master",
"Labels": {
"repo2docker.repo": repo,
"repo2docker.ref": ref,
"repo2docker.build": image_name,
"tljh_repo2docker.display_name": name,
"tljh_repo2docker.mem_limit": memory,
"tljh_repo2docker.cpu_limit": cpu,
},
"Volumes": {
"/var/run/docker.sock": {
"bind": "/var/run/docker.sock",
"mode": "rw",
}
},
"HostConfig": {
"Binds": ["/var/run/docker.sock:/var/run/docker.sock"],
},
"Tty": False,
"AttachStdout": False,
"AttachStderr": False,
"OpenStdin": False,
}
if username and password:
config.update(
{
"Env": [f"GIT_CREDENTIAL_ENV=username={username}\npassword={password}"],
}
)
async with Docker() as docker:
await docker.containers.run(config=config)