-
Notifications
You must be signed in to change notification settings - Fork 360
/
base.py
423 lines (348 loc) · 12.5 KB
/
base.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
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
import textwrap
import jinja2
import tarfile
import io
import os
import re
import logging
import docker
TEMPLATE = r"""
FROM buildpack-deps:artful
# Set up locales properly
RUN apt-get update && \
apt-get install --yes --no-install-recommends locales && \
apt-get purge && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
locale-gen
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8
# Use bash as default shell, rather than sh
ENV SHELL /bin/bash
# Set up user
ARG NB_USER
ARG NB_UID
ENV USER ${NB_USER}
ENV HOME /home/${NB_USER}
RUN adduser --disabled-password \
--gecos "Default user" \
--uid ${NB_UID} \
${NB_USER}
WORKDIR ${HOME}
RUN apt-get update && \
apt-get install --yes --no-install-recommends \
{% for package in base_packages -%}
{{ package }} \
{% endfor -%}
&& apt-get purge && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
{% if packages -%}
RUN apt-get update && \
apt-get install --yes \
{% for package in packages -%}
{{ package }} \
{% endfor -%}
&& apt-get purge && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
{% endif -%}
EXPOSE 8888
{% if env -%}
# Almost all environment variables
{% for item in env -%}
ENV {{item[0]}} {{item[1]}}
{% endfor -%}
{% endif -%}
{% if path -%}
# Special case PATH
ENV PATH {{ ':'.join(path) }}:${PATH}
{% endif -%}
{% if build_script_files -%}
# If scripts required during build are present, copy them
{% for src, dst in build_script_files.items() %}
COPY {{ src }} {{ dst }}
{% endfor -%}
{% endif -%}
{% for sd in build_script_directives -%}
{{sd}}
{% endfor %}
# Copy and chown stuff. This doubles the size of the repo, because
# you can't actually copy as USER, only as root! Thanks, Docker!
USER root
COPY src/ ${HOME}
RUN chown -R ${NB_USER}:${NB_USER} ${HOME}
# Run assemble scripts! These will actually build the specification
# in the repository into the image.
{% for sd in assemble_script_directives -%}
{{ sd }}
{% endfor %}
# Container image Labels!
# Put these at the end, since we don't want to rebuild everything
# when these change! Did I mention I hate Dockerfile cache semantics?
{% for k, v in labels.items() -%}
LABEL {{k}}={{v}}
{%- endfor %}
# We always want containers to run as non-root
USER ${NB_USER}
# Make sure that postBuild scripts are marked executable before executing them
{% if post_build_scripts -%}
{% for s in post_build_scripts -%}
RUN chmod +x {{ s }} && ./{{ s }}
{% endfor %}
{% endif -%}
# Specify the default command to run
CMD ["jupyter", "notebook", "--ip", "0.0.0.0"]
{% if appendix -%}
# Appendix:
{{ appendix }}
{% endif %}
"""
class BuildPack:
"""
A composable BuildPack.
Specifically used for creating Dockerfiles for use with repo2docker only.
Things that are kept constant:
- base image
- some environment variables (such as locale)
- user creation & ownership of home directory
- working directory
Everything that is configurable is additive & deduplicative,
and there are *some* general guarantees of ordering.
"""
def __init__(self):
self.log = logging.getLogger('repo2docker')
self.appendix = ''
def get_packages(self):
"""
List of packages that are installed in this BuildPack.
Versions are not specified, and ordering is not guaranteed. These
are usually installed as apt packages.
"""
return set()
def get_base_packages(self):
"""
Base set of apt packages that are installed for all images.
These contain useful images that are commonly used by a lot of images,
where it would be useful to share a base docker image layer that
contains them.
These would be installed with a --no-install-recommends option.
"""
return {
# Utils!
"less",
# FIXME: Use npm from nodesource!
# Everything seems to depend on npm these days, unfortunately.
"npm",
"unzip",
}
def get_env(self):
"""
Ordered list of environment variables to be set for this image.
Ordered so that environment variables can use other environment
variables in their values.
Expects tuples, with the first item being the environment variable
name and the second item being the value.
"""
return []
def get_path(self):
"""
Ordered list of file system paths to look for executables in.
Just sets the PATH environment variable. Separated out since
it is very commonly set by various buildpacks.
"""
return []
def get_labels(self):
"""
Docker labels to set on the built image.
"""
return {}
def get_build_script_files(self):
"""
Dict of files to be copied to the container image for use in building.
This is copied before the `build_scripts` & `assemble_scripts` are
run, so can be executed from either of them.
It's a dictionary where the key is the source file path in the host
system, and the value is the destination file path inside the
container image.
"""
return {}
def get_build_scripts(self):
"""
Ordered list of shell script snippets to build the base image.
A list of tuples, where the first item is a username & the
second is a single logical line of a bash script that should
be RUN as that user.
These are run before the source of the repository is copied
into the container image, and hence can not reference stuff
from the repository. When the build scripts are done, the
container image should be in a state where it is generically
re-useable for building various other repositories with
similar environments.
You can use environment variable substitutions in both the
username and the execution script.
"""
return []
def get_assemble_scripts(self):
"""
Ordered list of shell script snippets to build the repo into the image.
A list of tuples, where the first item is a username & the
second is a single logical line of a bash script that should
be RUN as that user.
These are run after the source of the repository is copied into
the container image (into the current directory). These should be
the scripts that actually build the repository into the container
image.
If this needs to be dynamically determined (based on the presence
or absence of certain files, for example), you can create any
method and decorate it with `traitlets.default('assemble_scripts)`
and the return value of this method is used as the value of
assemble_scripts. You can expect that the script is running in
the current directory of the repository being built when doing
dynamic detection.
You can use environment variable substitutions in both the
username and the execution script.
"""
return []
def get_post_build_scripts(self):
"""
An ordered list of executable scripts to execute after build.
Is run as a non-root user, and must be executable. Used for doing
things that are currently not supported by other means!
The scripts should be as deterministic as possible - running it twice
should not produce different results!
"""
return []
def binder_path(self, path):
"""Locate a file"""
if os.path.exists('binder'):
return os.path.join('binder', path)
else:
return path
def detect(self):
return True
def render(self):
"""
Render BuildPack into Dockerfile
"""
t = jinja2.Template(TEMPLATE)
build_script_directives = []
last_user = 'root'
for user, script in self.get_build_scripts():
if last_user != user:
build_script_directives.append("USER {}".format(user))
last_user = user
build_script_directives.append("RUN {}".format(
textwrap.dedent(script.strip('\n'))
))
assemble_script_directives = []
last_user = 'root'
for user, script in self.get_assemble_scripts():
if last_user != user:
assemble_script_directives.append("USER {}".format(user))
last_user = user
assemble_script_directives.append("RUN {}".format(
textwrap.dedent(script.strip('\n'))
))
return t.render(
packages=sorted(self.get_packages()),
path=self.get_path(),
env=self.get_env(),
labels=self.get_labels(),
build_script_directives=build_script_directives,
assemble_script_directives=assemble_script_directives,
build_script_files=self.get_build_script_files(),
base_packages=sorted(self.get_base_packages()),
post_build_scripts=self.get_post_build_scripts(),
appendix=self.appendix,
)
def build(self, image_spec, memory_limit, build_args):
tarf = io.BytesIO()
tar = tarfile.open(fileobj=tarf, mode='w')
dockerfile_tarinfo = tarfile.TarInfo("Dockerfile")
dockerfile = self.render().encode('utf-8')
dockerfile_tarinfo.size = len(dockerfile)
tar.addfile(
dockerfile_tarinfo,
io.BytesIO(dockerfile)
)
def _filter_tar(tar):
# We need to unset these for build_script_files we copy into tar
# Otherwise they seem to vary each time, preventing effective use
# of the cache!
# https://github.com/docker/docker-py/pull/1582 is related
tar.uname = ''
tar.gname = ''
tar.uid = 1000
tar.gid = 1000
return tar
for src in sorted(self.get_build_script_files()):
src_parts = src.split('/')
src_path = os.path.join(os.path.dirname(__file__), *src_parts)
tar.add(src_path, src, filter=_filter_tar)
tar.add('.', 'src/', filter=_filter_tar)
tar.close()
tarf.seek(0)
limits = {
# Always disable memory swap for building, since mostly
# nothing good can come of that.
'memswap': -1
}
if memory_limit:
limits['memory'] = memory_limit
client = docker.APIClient(version='auto',
**docker.utils.kwargs_from_env())
for line in client.build(
fileobj=tarf,
tag=image_spec,
custom_context=True,
buildargs=build_args,
decode=True,
forcerm=True,
rm=True,
container_limits=limits
):
yield line
class BaseImage(BuildPack):
def get_env(self):
return [
("APP_BASE", "/srv")
]
def detect(self):
return True
def get_assemble_scripts(self):
assemble_scripts = []
try:
with open(self.binder_path('apt.txt')) as f:
extra_apt_packages = []
for l in f:
package = l.partition('#')[0].strip()
if not package:
continue
# Validate that this is, indeed, just a list of packages
# We're doing shell injection around here, gotta be careful.
# FIXME: Add support for specifying version numbers
if not re.match(r"^[a-z0-9.+-]+", package):
raise ValueError("Found invalid package name {} in "
"apt.txt".format(package))
extra_apt_packages.append(package)
assemble_scripts.append((
'root',
r"""
apt-get update && \
apt-get install --yes --no-install-recommends {} && \
apt-get purge && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
""".format(' '.join(extra_apt_packages))
))
except FileNotFoundError:
pass
return assemble_scripts
def get_post_build_scripts(self):
post_build = self.binder_path('postBuild')
if os.path.exists(post_build):
return [post_build]
return []