-
Notifications
You must be signed in to change notification settings - Fork 618
/
build.py
501 lines (420 loc) 路 17.6 KB
/
build.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
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
import json
import logging
import os
import shlex
import shutil
import sys
import tempfile
from typing import Any, Dict, List, Optional, Tuple
import pkg_resources
from dockerpycreds.utils import find_executable # type: ignore
from six.moves import shlex_quote
import wandb
import wandb.docker as docker
from wandb.apis.internal import Api
from wandb.errors import DockerError, ExecutionError, LaunchError
from ...lib.git import GitRepo
from .._project_spec import EntryPoint, LaunchProject, compute_command_args
_logger = logging.getLogger(__name__)
_GENERATED_DOCKERFILE_NAME = "Dockerfile.wandb-autogenerated"
DEFAULT_ENTRYPOINT = "_wandb_default_entrypoint"
DEFAULT_CUDA_VERSION = "10.0"
def validate_docker_installation() -> None:
"""Verify if Docker is installed on host machine."""
if not find_executable("docker"):
raise ExecutionError(
"Could not find Docker executable. "
"Ensure Docker is installed as per the instructions "
"at https://docs.docker.com/install/overview/."
)
def get_docker_user(launch_project: LaunchProject, runner_type: str) -> Tuple[str, int]:
import getpass
username = getpass.getuser()
if runner_type == "sagemaker" and not launch_project.docker_image:
# unless user has provided their own image, sagemaker must run as root but keep the name for workdir etc
return username, 0
userid = launch_project.docker_user_id or os.geteuid()
return username, userid
DOCKERFILE_TEMPLATE = """
# ----- stage 1: build -----
FROM {py_build_image} as build
# requirements section depends on pip vs conda, and presence of buildx
{requirements_section}
# ----- stage 2: base -----
{base_setup}
COPY --from=build /env /env
ENV PATH="/env/bin:$PATH"
ENV SHELL /bin/bash
# some resources (eg sagemaker) must run on root
{user_setup}
WORKDIR {workdir}
RUN chown -R {uid} {workdir}
# make artifacts cache dir unrelated to build
RUN mkdir -p {workdir}/.cache && chown -R {uid} {workdir}/.cache
# copy code/etc
COPY --chown={uid} src/ {workdir}
ENV PYTHONUNBUFFERED=1
# some resources (eg sagemaker) have unique entrypoint requirements
{entrypoint_setup}
"""
# this goes into base_setup in TEMPLATE
PYTHON_SETUP_TEMPLATE = """
FROM {py_base_image} as base
"""
# this goes into base_setup in TEMPLATE
CUDA_SETUP_TEMPLATE = """
FROM {cuda_base_image} as base
# TODO: once NVIDIA their linux repository keys for all docker images
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$(cat /etc/os-release | grep ^ID= | cut -d "=" -f2 )$(cat /etc/os-release | grep ^VERSION_ID= | cut -d "=" -f2 | sed -e 's/[\".]//g' )/$(uname -i)/3bf863cc.pub
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/$(cat /etc/os-release | grep ^ID= | cut -d "=" -f2 )$(cat /etc/os-release | grep ^VERSION_ID= | cut -d "=" -f2 | sed -e 's/[\".]//g' )/$(uname -i)/7fa2af80.pub
RUN apt-get update -qq && apt-get install -y software-properties-common && add-apt-repository -y ppa:deadsnakes/ppa
# install python
RUN apt-get update -qq && apt-get install --no-install-recommends -y \
{python_packages} \
&& apt-get -qq purge && apt-get -qq clean \
&& rm -rf /var/lib/apt/lists/*
# make sure `python` points at the right version
RUN update-alternatives --install /usr/bin/python python /usr/bin/python{py_version} 1 \
&& update-alternatives --install /usr/local/bin/python python /usr/bin/python{py_version} 1
"""
# this goes into requirements_section in TEMPLATE
PIP_TEMPLATE = """
RUN python -m venv /env
# make sure we install into the env
ENV PATH="/env/bin:$PATH"
COPY {requirements_files} ./
{buildx_optional_prefix} {pip_install}
"""
# this goes into requirements_section in TEMPLATE
CONDA_TEMPLATE = """
COPY src/environment.yml .
{buildx_optional_prefix} conda env create -f environment.yml -n env
# pack the environment so that we can transfer to the base image
RUN conda install -c conda-forge conda-pack
RUN conda pack -n env -o /tmp/env.tar && \
mkdir /env && cd /env && tar xf /tmp/env.tar && \
rm /tmp/env.tar
RUN /env/bin/conda-unpack
"""
USER_CREATE_TEMPLATE = """
RUN useradd \
--create-home \
--no-log-init \
--shell /bin/bash \
--gid 0 \
--uid {uid} \
{user} || echo ""
"""
ENTRYPOINT_TEMPLATE = """
COPY ./src/{default_entrypoint} {workdir}
ENTRYPOINT ["sh", "./{default_entrypoint}", "{entrypoint}"]
"""
BASH_ENTRYPOINT = """
#!/bin/sh
if [ -z "${WANDB_ENTRYPOINT_COMMAND}" ]; then
CMD="$1"
else
CMD="${WANDB_ENTRYPOINT_COMMAND}"
fi
CMD="${CMD} ${WANDB_ARGS}"
echo "Running command: ${CMD}"
eval $CMD
"""
def get_current_python_version() -> Tuple[str, str]:
full_version = sys.version.split()[0].split(".")
major = full_version[0]
version = ".".join(full_version[:2]) if len(full_version) >= 2 else major + ".0"
return version, major
def get_base_setup(
launch_project: LaunchProject, py_version: str, py_major: str
) -> str:
"""Fill in the Dockerfile templates for stage 2 of build. CPU version is built on python, GPU
version is built on nvidia:cuda"""
python_base_image = f"python:{py_version}-buster"
if launch_project.cuda:
cuda_version = launch_project.cuda_version or DEFAULT_CUDA_VERSION
# cuda image doesn't come with python tooling
if py_major == "2":
python_packages = [
f"python{py_version}",
f"libpython{py_version}",
"python-pip",
"python-setuptools",
]
else:
python_packages = [
f"python{py_version}",
f"libpython{py_version}",
"python3-pip",
"python3-setuptools",
]
base_setup = CUDA_SETUP_TEMPLATE.format(
cuda_base_image=f"nvidia/cuda:{cuda_version}-runtime",
python_packages=" \\\n".join(python_packages),
py_version=py_version,
)
else:
python_packages = [
"python3-dev" if py_major == "3" else "python-dev",
"gcc",
] # gcc required for python < 3.7 for some reason
base_setup = PYTHON_SETUP_TEMPLATE.format(py_base_image=python_base_image)
return base_setup
def get_env_vars_dict(launch_project: LaunchProject, api: Api) -> Dict[str, str]:
"""Generates environment variables for the project.
Arguments:
launch_project: LaunchProject to generate environment variables for.
Returns:
Dictionary of environment variables.
"""
env_vars = {}
env_vars["WANDB_BASE_URL"] = api.settings("base_url")
env_vars["WANDB_API_KEY"] = api.api_key
env_vars["WANDB_PROJECT"] = launch_project.target_project
env_vars["WANDB_ENTITY"] = launch_project.target_entity
env_vars["WANDB_LAUNCH"] = "True"
env_vars["WANDB_RUN_ID"] = launch_project.run_id
if launch_project.docker_image:
env_vars["WANDB_DOCKER"] = launch_project.docker_image
if launch_project.name is not None:
env_vars["WANDB_NAME"] = launch_project.name
# TODO: handle env vars > 32760 characters
env_vars["WANDB_CONFIG"] = json.dumps(launch_project.override_config)
artifacts = {}
# if we're spinning up a launch process from a job
# we should tell the run to use that artifact
if launch_project.job:
artifacts = {wandb.util.LAUNCH_JOB_ARTIFACT_SLOT_NAME: launch_project.job}
env_vars["WANDB_ARTIFACTS"] = json.dumps(
{**artifacts, **launch_project.override_artifacts}
)
# check if the user provided an override entrypoint, otherwise use the default
if launch_project.override_entrypoint is not None:
env_vars["WANDB_ENTRYPOINT_COMMAND"] = join(
launch_project.override_entrypoint.command
)
if launch_project.override_args:
env_vars["WANDB_ARGS"] = " ".join(
compute_command_args(launch_project.override_args)
)
return env_vars
def get_requirements_section(launch_project: LaunchProject, builder_type: str) -> str:
if builder_type == "docker":
buildx_installed = docker.is_buildx_installed()
if not buildx_installed:
wandb.termwarn(
"Docker BuildX is not installed, for faster builds upgrade docker: https://github.com/docker/buildx#installing"
)
prefix = "RUN WANDB_DISABLE_CACHE=true"
elif builder_type == "kaniko":
prefix = "RUN WANDB_DISABLE_CACHE=true"
buildx_installed = False
if launch_project.deps_type == "pip":
requirements_files = []
if launch_project.project_dir is not None and os.path.exists(
os.path.join(launch_project.project_dir, "requirements.txt")
):
requirements_files += ["src/requirements.txt"]
pip_install_line = "pip install -r requirements.txt"
if launch_project.project_dir is not None and os.path.exists(
os.path.join(launch_project.project_dir, "requirements.frozen.txt")
):
# if we have frozen requirements stored, copy those over and have them take precedence
requirements_files += ["src/requirements.frozen.txt", "_wandb_bootstrap.py"]
pip_install_line = (
_parse_existing_requirements(launch_project)
+ "python _wandb_bootstrap.py"
)
if buildx_installed:
prefix = "RUN --mount=type=cache,mode=0777,target=/root/.cache/pip"
requirements_line = PIP_TEMPLATE.format(
buildx_optional_prefix=prefix,
requirements_files=" ".join(requirements_files),
pip_install=pip_install_line,
)
elif launch_project.deps_type == "conda":
if buildx_installed:
prefix = "RUN --mount=type=cache,mode=0777,target=/opt/conda/pkgs"
requirements_line = CONDA_TEMPLATE.format(buildx_optional_prefix=prefix)
else:
# this means no deps file was found
requirements_line = "RUN mkdir -p env/" # Docker fails otherwise
wandb.termwarn("No requirements file found. No packages will be installed.")
return requirements_line
def get_user_setup(username: str, userid: int, runner_type: str) -> str:
if runner_type == "sagemaker":
# sagemaker must run as root
return "USER root"
user_create = USER_CREATE_TEMPLATE.format(uid=userid, user=username)
user_create += f"\nUSER {username}"
return user_create
def get_entrypoint_setup(
launch_project: LaunchProject,
entry_point: EntryPoint,
workdir: str,
) -> str:
# this check will always pass, since this is only called in the build case where
# the project_dir is set
assert launch_project.project_dir is not None
with open(os.path.join(launch_project.project_dir, DEFAULT_ENTRYPOINT), "w") as fp:
fp.write(BASH_ENTRYPOINT)
return ENTRYPOINT_TEMPLATE.format(
workdir=workdir,
entrypoint=join(entry_point.command),
default_entrypoint=DEFAULT_ENTRYPOINT,
)
def generate_dockerfile(
launch_project: LaunchProject,
entry_point: EntryPoint,
runner_type: str,
builder_type: str,
) -> str:
# get python versions truncated to major.minor to ensure image availability
if launch_project.python_version:
spl = launch_project.python_version.split(".")[:2]
py_version, py_major = (".".join(spl), spl[0])
else:
py_version, py_major = get_current_python_version()
# ----- stage 1: build -----
if launch_project.deps_type == "pip" or launch_project.deps_type is None:
python_build_image = "python:{}".format(
py_version
) # use full python image for package installation
elif launch_project.deps_type == "conda":
# neither of these images are receiving regular updates, latest should be pretty stable
python_build_image = (
"continuumio/miniconda3:latest"
if py_major == "3"
else "continuumio/miniconda:latest"
)
requirements_section = get_requirements_section(launch_project, builder_type)
# ----- stage 2: base -----
python_base_setup = get_base_setup(launch_project, py_version, py_major)
# set up user info
username, userid = get_docker_user(launch_project, runner_type)
user_setup = get_user_setup(username, userid, runner_type)
workdir = f"/home/{username}"
entrypoint_section = get_entrypoint_setup(launch_project, entry_point, workdir)
dockerfile_contents = DOCKERFILE_TEMPLATE.format(
py_build_image=python_build_image,
requirements_section=requirements_section,
base_setup=python_base_setup,
uid=userid,
user_setup=user_setup,
workdir=workdir,
entrypoint_setup=entrypoint_section,
)
return dockerfile_contents
_inspected_images = {}
def docker_image_exists(docker_image: str, should_raise: bool = False) -> bool:
"""Checks if a specific image is already available,
optionally raising an exception"""
_logger.info("Checking if base image exists...")
try:
data = docker.run(["docker", "image", "inspect", docker_image])
# always true, since return stderr defaults to false
assert isinstance(data, str)
parsed = json.loads(data)[0]
_inspected_images[docker_image] = parsed
return True
except (DockerError, ValueError) as e:
if should_raise:
raise e
_logger.info("Base image not found. Generating new base image")
return False
def docker_image_inspect(docker_image: str) -> Dict[str, Any]:
"""Get the parsed json result of docker inspect image_name"""
if _inspected_images.get(docker_image) is None:
docker_image_exists(docker_image, True)
return _inspected_images.get(docker_image, {})
def pull_docker_image(docker_image: str) -> None:
"""Pulls the requested docker image"""
if docker_image_exists(docker_image):
# don't pull images if they exist already, eg if they are local images
return
try:
docker.run(["docker", "pull", docker_image])
except DockerError as e:
raise LaunchError(f"Docker server returned error: {e}")
def construct_gcp_image_uri(
launch_project: LaunchProject,
gcp_repo: str,
gcp_project: str,
gcp_registry: str,
) -> str:
base_uri = launch_project.image_uri
return "/".join([gcp_registry, gcp_project, gcp_repo, base_uri])
def construct_gcp_registry_uri(
gcp_repo: str, gcp_project: str, gcp_registry: str
) -> str:
return "/".join([gcp_registry, gcp_project, gcp_repo])
def _parse_existing_requirements(launch_project: LaunchProject) -> str:
requirements_line = ""
assert launch_project.project_dir is not None
base_requirements = os.path.join(launch_project.project_dir, "requirements.txt")
if os.path.exists(base_requirements):
include_only = set()
with open(base_requirements) as f:
iter = pkg_resources.parse_requirements(f)
while True:
try:
pkg = next(iter)
if hasattr(pkg, "name"):
name = pkg.name.lower() # type: ignore
else:
name = str(pkg)
include_only.add(shlex_quote(name))
except StopIteration:
break
# Different versions of pkg_resources throw different errors
# just catch them all and ignore packages we can't parse
except Exception as e:
_logger.warn(f"Unable to parse requirements.txt: {e}")
continue
requirements_line += "WANDB_ONLY_INCLUDE={} ".format(",".join(include_only))
return requirements_line
def _get_docker_image_uri(name: Optional[str], work_dir: str, image_id: str) -> str:
"""
Returns an appropriate Docker image URI for a project based on the git hash of the specified
working directory.
:param name: The URI of the Docker repository with which to tag the image. The
repository URI is used as the prefix of the image URI.
:param work_dir: Path to the working directory in which to search for a git commit hash
"""
name = name.replace(" ", "-") if name else "wandb-launch"
# Optionally include first 7 digits of git SHA in tag name, if available.
git_commit = GitRepo(work_dir).last_commit
version_string = (
":" + str(git_commit[:7]) + image_id if git_commit else ":" + image_id
)
return name + version_string
def _create_docker_build_ctx(
launch_project: LaunchProject,
dockerfile_contents: str,
) -> str:
"""Creates build context temp dir containing Dockerfile and project code, returning path to temp dir."""
directory = tempfile.mkdtemp()
dst_path = os.path.join(directory, "src")
assert launch_project.project_dir is not None
shutil.copytree(
src=launch_project.project_dir,
dst=dst_path,
symlinks=True,
)
shutil.copy(
os.path.join(os.path.dirname(__file__), "templates", "_wandb_bootstrap.py"),
os.path.join(directory),
)
if launch_project.python_version:
runtime_path = os.path.join(dst_path, "runtime.txt")
with open(runtime_path, "w") as fp:
fp.write(f"python-{launch_project.python_version}")
# TODO: we likely don't need to pass the whole git repo into the container
# with open(os.path.join(directory, ".dockerignore"), "w") as f:
# f.write("**/.git")
with open(os.path.join(directory, _GENERATED_DOCKERFILE_NAME), "w") as handle:
handle.write(dockerfile_contents)
return directory
def join(split_command: List[str]) -> str:
"""Return a shell-escaped string from *split_command*."""
return " ".join(shlex.quote(arg) for arg in split_command)