| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import logging |
| 6 |
import os |
| 7 |
import re |
| 8 |
|
| 9 |
from voluptuous import Optional, Required |
| 10 |
|
| 11 |
import taskgraph |
| 12 |
from taskgraph.transforms.base import TransformSequence |
| 13 |
from taskgraph.util import json |
| 14 |
from taskgraph.util.docker import create_context_tar, generate_context_hash |
| 15 |
from taskgraph.util.schema import Schema |
| 16 |
|
| 17 |
from .task import task_description_schema |
| 18 |
|
| 19 |
logger = logging.getLogger(__name__) |
| 20 |
|
| 21 |
CONTEXTS_DIR = "docker-contexts" |
| 22 |
|
| 23 |
DIGEST_RE = re.compile("^[0-9a-f]{64}$") |
| 24 |
|
| 25 |
IMAGE_BUILDER_IMAGE = ( |
| 26 |
"mozillareleases/image_builder:5.1.0" |
| 27 |
"@sha256:" |
| 28 |
"7fe70dcedefffffa03237ba5d456d42e0d7461de066db3f7a7c280a104869cd5" |
| 29 |
) |
| 30 |
|
| 31 |
transforms = TransformSequence() |
| 32 |
|
| 33 |
docker_image_schema = Schema( |
| 34 |
{ |
| 35 |
# Name of the docker image. |
| 36 |
Required("name"): str, |
| 37 |
# Name of the parent docker image. |
| 38 |
Optional("parent"): str, |
| 39 |
# Treeherder symbol. |
| 40 |
Optional("symbol"): str, |
| 41 |
# relative path (from config.path) to the file the docker image was defined |
| 42 |
# in. |
| 43 |
Optional("task-from"): str, |
| 44 |
# Arguments to use for the Dockerfile. |
| 45 |
Optional("args"): {str: str}, |
| 46 |
# Name of the docker image definition under taskcluster/docker, when |
| 47 |
# different from the docker image name. |
| 48 |
Optional("definition"): str, |
| 49 |
# List of package tasks this docker image depends on. |
| 50 |
Optional("packages"): [str], |
| 51 |
Optional( |
| 52 |
"index", |
| 53 |
description="information for indexing this build so its artifacts can be discovered", |
| 54 |
): task_description_schema["index"], |
| 55 |
Optional( |
| 56 |
"cache", |
| 57 |
description="Whether this image should be cached based on inputs.", |
| 58 |
): bool, |
| 59 |
} |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
transforms.add_validate(docker_image_schema) |
| 64 |
|
| 65 |
|
| 66 |
@transforms.add |
| 67 |
def fill_template(config, tasks): |
| 68 |
available_packages = set() |
| 69 |
for task in config.kind_dependencies_tasks.values(): |
| 70 |
if task.kind != "packages": |
| 71 |
continue |
| 72 |
name = task.label.replace("packages-", "") |
| 73 |
available_packages.add(name) |
| 74 |
|
| 75 |
context_hashes = {} |
| 76 |
|
| 77 |
if not taskgraph.fast and config.write_artifacts: |
| 78 |
if not os.path.isdir(CONTEXTS_DIR): |
| 79 |
os.makedirs(CONTEXTS_DIR) |
| 80 |
|
| 81 |
for task in tasks: |
| 82 |
image_name = task.pop("name") |
| 83 |
job_symbol = task.pop("symbol", None) |
| 84 |
args = task.pop("args", {}) |
| 85 |
definition = task.pop("definition", image_name) |
| 86 |
packages = task.pop("packages", []) |
| 87 |
parent = task.pop("parent", None) |
| 88 |
|
| 89 |
for p in packages: |
| 90 |
if p not in available_packages: |
| 91 |
raise Exception( |
| 92 |
f"Missing package job for {config.kind}-{image_name}: {p}" |
| 93 |
) |
| 94 |
|
| 95 |
if not taskgraph.fast: |
| 96 |
context_path = os.path.join("taskcluster", "docker", definition) |
| 97 |
topsrcdir = os.path.dirname(config.graph_config.taskcluster_yml) |
| 98 |
if config.write_artifacts: |
| 99 |
context_file = os.path.join(CONTEXTS_DIR, f"{image_name}.tar.gz") |
| 100 |
logger.info(f"Writing {context_file} for docker image {image_name}") |
| 101 |
context_hash = create_context_tar( |
| 102 |
topsrcdir, |
| 103 |
context_path, |
| 104 |
context_file, |
| 105 |
args, |
| 106 |
) |
| 107 |
else: |
| 108 |
context_hash = generate_context_hash(topsrcdir, context_path, args) |
| 109 |
else: |
| 110 |
if config.write_artifacts: |
| 111 |
raise Exception("Can't write artifacts if `taskgraph.fast` is set.") |
| 112 |
context_hash = "0" * 40 |
| 113 |
digest_data = [context_hash] |
| 114 |
digest_data += [json.dumps(args, sort_keys=True)] |
| 115 |
context_hashes[image_name] = context_hash |
| 116 |
|
| 117 |
description = f"Build the docker image {image_name} for use by dependent tasks" |
| 118 |
|
| 119 |
args["DOCKER_IMAGE_PACKAGES"] = " ".join(f"<{p}>" for p in packages) |
| 120 |
|
| 121 |
# Adjust the zstandard compression level based on the execution level. |
| 122 |
# We use faster compression for level 1 because we care more about |
| 123 |
# end-to-end times. We use slower/better compression for other levels |
| 124 |
# because images are read more often and it is worth the trade-off to |
| 125 |
# burn more CPU once to reduce image size. |
| 126 |
zstd_level = "3" if int(config.params["level"]) == 1 else "10" |
| 127 |
|
| 128 |
expires = config.graph_config._config.get("task-expires-after", "28 days") |
| 129 |
|
| 130 |
# include some information that is useful in reconstructing this task |
| 131 |
# from JSON |
| 132 |
taskdesc = { |
| 133 |
"label": "docker-image-" + image_name, |
| 134 |
"description": description, |
| 135 |
"attributes": { |
| 136 |
"image_name": image_name, |
| 137 |
"artifact_prefix": "public", |
| 138 |
}, |
| 139 |
"always-target": True, |
| 140 |
"expires-after": expires if config.params.is_try() else "1 year", |
| 141 |
"scopes": [], |
| 142 |
"run-on-projects": [], |
| 143 |
"worker-type": "images", |
| 144 |
"worker": { |
| 145 |
"implementation": "docker-worker", |
| 146 |
"os": "linux", |
| 147 |
"artifacts": [ |
| 148 |
{ |
| 149 |
"type": "file", |
| 150 |
"path": "/workspace/image.tar.zst", |
| 151 |
"name": "public/image.tar.zst", |
| 152 |
} |
| 153 |
], |
| 154 |
"env": { |
| 155 |
"CONTEXT_TASK_ID": {"task-reference": "<decision>"}, |
| 156 |
"CONTEXT_PATH": f"public/docker-contexts/{image_name}.tar.gz", |
| 157 |
"HASH": context_hash, |
| 158 |
"PROJECT": config.params["project"], |
| 159 |
"IMAGE_NAME": image_name, |
| 160 |
"DOCKER_IMAGE_ZSTD_LEVEL": zstd_level, |
| 161 |
"DOCKER_BUILD_ARGS": { |
| 162 |
"task-reference": json.dumps(args), |
| 163 |
}, |
| 164 |
"VCS_BASE_REPOSITORY": config.params["base_repository"], |
| 165 |
"VCS_HEAD_REPOSITORY": config.params["head_repository"], |
| 166 |
"VCS_HEAD_REV": config.params["head_rev"], |
| 167 |
"VCS_REPOSITORY_TYPE": config.params["repository_type"], |
| 168 |
}, |
| 169 |
"chain-of-trust": True, |
| 170 |
"max-run-time": 7200, |
| 171 |
}, |
| 172 |
} |
| 173 |
if "index" in task: |
| 174 |
taskdesc["index"] = task["index"] |
| 175 |
if job_symbol: |
| 176 |
taskdesc["treeherder"] = { |
| 177 |
"symbol": job_symbol, |
| 178 |
"platform": "taskcluster-images/opt", |
| 179 |
"kind": "other", |
| 180 |
"tier": 1, |
| 181 |
} |
| 182 |
|
| 183 |
worker = taskdesc["worker"] |
| 184 |
|
| 185 |
worker["docker-image"] = IMAGE_BUILDER_IMAGE |
| 186 |
digest_data.append(f"image-builder-image:{IMAGE_BUILDER_IMAGE}") |
| 187 |
|
| 188 |
if packages: |
| 189 |
deps = taskdesc.setdefault("dependencies", {}) |
| 190 |
for p in sorted(packages): |
| 191 |
deps[p] = f"packages-{p}" |
| 192 |
|
| 193 |
if parent: |
| 194 |
deps = taskdesc.setdefault("dependencies", {}) |
| 195 |
deps["parent"] = f"docker-image-{parent}" |
| 196 |
worker["env"]["PARENT_TASK_ID"] = { |
| 197 |
"task-reference": "<parent>", |
| 198 |
} |
| 199 |
|
| 200 |
if task.get("cache", True) and not taskgraph.fast: |
| 201 |
taskdesc["cache"] = { |
| 202 |
"type": "docker-images.v2", |
| 203 |
"name": image_name, |
| 204 |
"digest-data": digest_data, |
| 205 |
} |
| 206 |
|
| 207 |
yield taskdesc |