-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathdocker.py
201 lines (145 loc) · 5.64 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
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import contextlib
import io
import operator
import os
import pathlib
import tarfile
import time
import docker # type: ignore
import jinja2
from .logging import log, log_raw
from .utils import write_if_different
def write_dockerfiles(source_dir: pathlib.Path, dest_dir: pathlib.Path):
env = jinja2.Environment(loader=jinja2.FileSystemLoader(str(source_dir)))
for f in os.listdir(source_dir):
if not f.endswith(".Dockerfile"):
continue
tmpl = env.get_template(f)
data = tmpl.render()
write_if_different(dest_dir / f, data.encode("utf-8"))
def build_docker_image(client, image_data: bytes, image_dir: pathlib.Path, name):
image_path = image_dir / ("image-%s" % name)
return ensure_docker_image(client, io.BytesIO(image_data), image_path=image_path)
def ensure_docker_image(client, fh, image_path=None):
res = client.api.build(fileobj=fh, decode=True)
image = None
for s in res:
if "stream" in s:
for l in s["stream"].strip().splitlines():
log(l)
if "aux" in s and "ID" in s["aux"]:
image = s["aux"]["ID"]
if "error" in s:
log(s["error"])
if not image:
raise Exception("unable to determine built Docker image")
if image_path:
tar_path = pathlib.Path(str(image_path) + ".tar")
with tar_path.open("wb") as fh:
for chunk in client.images.get(image).save():
fh.write(chunk)
with image_path.open("w") as fh:
fh.write(image + "\n")
return image
def get_image(client, source_dir: pathlib.Path, image_dir: pathlib.Path, name):
if client is None:
return None
image_path = image_dir / ("image-%s" % name)
tar_path = image_path.with_suffix(".tar")
with image_path.open("r") as fh:
image_id = fh.read().strip()
try:
client.images.get(image_id)
return image_id
except docker.errors.ImageNotFound:
if tar_path.exists():
with tar_path.open("rb") as fh:
data = fh.read()
client.images.load(data)
return image_id
else:
return build_docker_image(client, str(source_dir).encode(), image_dir, name)
def copy_file_to_container(path, container, container_path, archive_path=None):
"""Copy a path on the local filesystem to a running container."""
buf = io.BytesIO()
tf = tarfile.open("irrelevant", "w", buf)
dest_path = archive_path or path.name
tf.add(str(path), dest_path)
tf.close()
log("copying %s to container:%s/%s" % (path, container_path, dest_path))
container.put_archive(container_path, buf.getvalue())
@contextlib.contextmanager
def run_container(client, image):
container = client.containers.run(
image, command=["/bin/sleep", "86400"], detach=True
)
# Check if container is actually running
for _ in range(10):
container.reload()
if container.status in ("created", "starting"):
time.sleep(1)
continue
if container.status == "running":
break
state = container.attrs.get("State", {})
exit_code = state.get("ExitCode")
error = state.get("Error", "")
raise RuntimeError(
f"Container failed to start (status {container.status}) with exit code {exit_code}: {error}"
)
try:
yield container
finally:
container.stop(timeout=0)
container.remove()
def container_exec(container, command, user="build", environment=None):
# docker-py's exec_run() won't return the exit code. So we reinvent the
# wheel.
try:
create_res = container.client.api.exec_create(
container.id, command, user=user, environment=environment
)
except Exception as exc:
if container.status != "running":
state = container.attrs.get("State", {})
exit_code = state.get("ExitCode")
error = state.get("Error", "")
raise RuntimeError(
f"Container is not running (status {container.status}) with exit code {exit_code}: {error}"
) from exc
else:
raise
exec_output = container.client.api.exec_start(create_res["Id"], stream=True)
for chunk in exec_output:
for l in chunk.strip().splitlines():
log(l)
log_raw(chunk)
inspect_res = container.client.api.exec_inspect(create_res["Id"])
if inspect_res["ExitCode"] != 0:
if "PYBUILD_BREAK_ON_FAILURE" in os.environ:
print("to enter container: docker exec -it %s /bin/bash" % container.id)
import pdb
pdb.set_trace()
raise Exception("exit code %d from %s" % (inspect_res["ExitCode"], command))
# 2019-01-01T00:00:00
DEFAULT_MTIME = 1546329600
def container_get_archive(container, path):
"""Get a deterministic tar archive from a container."""
data, stat = container.get_archive(path)
old_data = io.BytesIO()
for chunk in data:
old_data.write(chunk)
old_data.seek(0)
new_data = io.BytesIO()
with (
tarfile.open(fileobj=old_data) as itf,
tarfile.open(fileobj=new_data, mode="w") as otf,
):
for member in sorted(itf.getmembers(), key=operator.attrgetter("name")):
file_data = itf.extractfile(member) if not member.linkname else None
member.mtime = DEFAULT_MTIME
otf.addfile(member, file_data)
return new_data.getvalue()