| 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 |
# Support for running tasks that download remote content and re-export |
| 6 |
# it as task artifacts. |
| 7 |
|
| 8 |
|
| 9 |
import os |
| 10 |
import re |
| 11 |
from dataclasses import dataclass |
| 12 |
from typing import Callable |
| 13 |
|
| 14 |
from voluptuous import Extra, Optional, Required |
| 15 |
|
| 16 |
import taskgraph |
| 17 |
|
| 18 |
from ..util import path |
| 19 |
from ..util.cached_tasks import add_optimization |
| 20 |
from ..util.schema import Schema, validate_schema |
| 21 |
from ..util.treeherder import join_symbol |
| 22 |
from .base import TransformSequence |
| 23 |
|
| 24 |
CACHE_TYPE = "content.v1" |
| 25 |
|
| 26 |
FETCH_SCHEMA = Schema( |
| 27 |
{ |
| 28 |
# Name of the task. |
| 29 |
Required("name"): str, |
| 30 |
# Relative path (from config.path) to the file the task was defined |
| 31 |
# in. |
| 32 |
Optional("task-from"): str, |
| 33 |
# Description of the task. |
| 34 |
Required("description"): str, |
| 35 |
Optional("expires-after"): str, |
| 36 |
Optional("docker-image"): object, |
| 37 |
Optional( |
| 38 |
"fetch-alias", |
| 39 |
description="An alias that can be used instead of the real fetch task name in " |
| 40 |
"fetch stanzas for tasks.", |
| 41 |
): str, |
| 42 |
Optional( |
| 43 |
"artifact-prefix", |
| 44 |
description="The prefix of the taskcluster artifact being uploaded. " |
| 45 |
"Defaults to `public/`; if it starts with something other than " |
| 46 |
"`public/` the artifact will require scopes to access.", |
| 47 |
): str, |
| 48 |
Optional("attributes"): {str: object}, |
| 49 |
Required("fetch"): { |
| 50 |
Required("type"): str, |
| 51 |
Extra: object, |
| 52 |
}, |
| 53 |
} |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
# define a collection of payload builders, depending on the worker implementation |
| 58 |
fetch_builders = {} |
| 59 |
|
| 60 |
|
| 61 |
@dataclass(frozen=True) |
| 62 |
class FetchBuilder: |
| 63 |
schema: Schema |
| 64 |
builder: Callable |
| 65 |
|
| 66 |
|
| 67 |
def fetch_builder(name, schema): |
| 68 |
schema = Schema({Required("type"): name}).extend(schema) |
| 69 |
|
| 70 |
def wrap(func): |
| 71 |
fetch_builders[name] = FetchBuilder(schema, func) # type: ignore |
| 72 |
return func |
| 73 |
|
| 74 |
return wrap |
| 75 |
|
| 76 |
|
| 77 |
transforms = TransformSequence() |
| 78 |
transforms.add_validate(FETCH_SCHEMA) |
| 79 |
|
| 80 |
|
| 81 |
@transforms.add |
| 82 |
def process_fetch_task(config, tasks): |
| 83 |
# Converts fetch-url entries to the run schema. |
| 84 |
for task in tasks: |
| 85 |
typ = task["fetch"]["type"] |
| 86 |
name = task["name"] |
| 87 |
fetch = task.pop("fetch") |
| 88 |
|
| 89 |
if typ not in fetch_builders: |
| 90 |
raise Exception(f"Unknown fetch type {typ} in fetch {name}") |
| 91 |
validate_schema(fetch_builders[typ].schema, fetch, f"In task.fetch {name!r}:") |
| 92 |
|
| 93 |
task.update(configure_fetch(config, typ, name, fetch)) |
| 94 |
|
| 95 |
yield task |
| 96 |
|
| 97 |
|
| 98 |
def configure_fetch(config, typ, name, fetch): |
| 99 |
if typ not in fetch_builders: |
| 100 |
raise Exception(f"No fetch type {typ} in fetch {name}") |
| 101 |
validate_schema(fetch_builders[typ].schema, fetch, f"In task.fetch {name!r}:") |
| 102 |
|
| 103 |
return fetch_builders[typ].builder(config, name, fetch) |
| 104 |
|
| 105 |
|
| 106 |
@transforms.add |
| 107 |
def make_task(config, tasks): |
| 108 |
# Fetch tasks are idempotent and immutable. Have them live for |
| 109 |
# essentially forever. |
| 110 |
if config.params["level"] == "3": |
| 111 |
expires = "1000 years" |
| 112 |
else: |
| 113 |
expires = config.graph_config._config.get("task-expires-after", "28 days") |
| 114 |
|
| 115 |
for task in tasks: |
| 116 |
name = task["name"] |
| 117 |
artifact_prefix = task.get("artifact-prefix", "public") |
| 118 |
env = task.get("env", {}) |
| 119 |
env.update({"UPLOAD_DIR": "/builds/worker/artifacts"}) |
| 120 |
attributes = task.get("attributes", {}) |
| 121 |
attributes["fetch-artifact"] = path.join(artifact_prefix, task["artifact_name"]) |
| 122 |
alias = task.get("fetch-alias") |
| 123 |
if alias: |
| 124 |
attributes["fetch-alias"] = alias |
| 125 |
|
| 126 |
task_desc = { |
| 127 |
"attributes": attributes, |
| 128 |
"name": name, |
| 129 |
"description": task["description"], |
| 130 |
"expires-after": task.get("expires-after", expires), |
| 131 |
"label": f"fetch-{name}", |
| 132 |
"run-on-projects": [], |
| 133 |
"run": { |
| 134 |
"using": "run-task", |
| 135 |
"checkout": False, |
| 136 |
"command": task["command"], |
| 137 |
}, |
| 138 |
"worker-type": "images", |
| 139 |
"worker": { |
| 140 |
"chain-of-trust": True, |
| 141 |
"docker-image": task.get("docker-image", {"in-tree": "fetch"}), |
| 142 |
"env": env, |
| 143 |
"max-run-time": 900, |
| 144 |
"artifacts": [ |
| 145 |
{ |
| 146 |
"type": "directory", |
| 147 |
"name": artifact_prefix, |
| 148 |
"path": "/builds/worker/artifacts", |
| 149 |
} |
| 150 |
], |
| 151 |
}, |
| 152 |
} |
| 153 |
|
| 154 |
if "treeherder" in config.graph_config: |
| 155 |
task_desc["treeherder"] = { |
| 156 |
"symbol": join_symbol("Fetch", name), |
| 157 |
"kind": "build", |
| 158 |
"platform": "fetch/opt", |
| 159 |
"tier": 1, |
| 160 |
} |
| 161 |
|
| 162 |
if task.get("secret", None): |
| 163 |
task_desc["scopes"] = ["secrets:get:" + task.get("secret")] |
| 164 |
task_desc["worker"]["taskcluster-proxy"] = True |
| 165 |
|
| 166 |
if not taskgraph.fast: |
| 167 |
cache_name = task_desc["label"].replace(f"{config.kind}-", "", 1) |
| 168 |
|
| 169 |
# This adds the level to the index path automatically. |
| 170 |
add_optimization( |
| 171 |
config, |
| 172 |
task_desc, |
| 173 |
cache_type=CACHE_TYPE, |
| 174 |
cache_name=cache_name, |
| 175 |
digest_data=task["digest_data"], |
| 176 |
) |
| 177 |
yield task_desc |
| 178 |
|
| 179 |
|
| 180 |
@fetch_builder( |
| 181 |
"static-url", |
| 182 |
schema={ |
| 183 |
# The URL to download. |
| 184 |
Required("url"): str, |
| 185 |
# The SHA-256 of the downloaded content. |
| 186 |
Required("sha256"): str, |
| 187 |
# Size of the downloaded entity, in bytes. |
| 188 |
Required("size"): int, |
| 189 |
# GPG signature verification. |
| 190 |
Optional("gpg-signature"): { |
| 191 |
# URL where GPG signature document can be obtained. Can contain the |
| 192 |
# value ``{url}``, which will be substituted with the value from |
| 193 |
# ``url``. |
| 194 |
Required("sig-url"): str, |
| 195 |
# Path to file containing GPG public key(s) used to validate |
| 196 |
# download. |
| 197 |
Required("key-path"): str, |
| 198 |
}, |
| 199 |
# The name to give to the generated artifact. Defaults to the file |
| 200 |
# portion of the URL. Using a different extension converts the |
| 201 |
# archive to the given type. Only conversion to .tar.zst is |
| 202 |
# supported. |
| 203 |
Optional("artifact-name"): str, |
| 204 |
# Strip the given number of path components at the beginning of |
| 205 |
# each file entry in the archive. |
| 206 |
# Requires an artifact-name ending with .tar.zst. |
| 207 |
Optional("strip-components"): int, |
| 208 |
# Add the given prefix to each file entry in the archive. |
| 209 |
# Requires an artifact-name ending with .tar.zst. |
| 210 |
Optional("add-prefix"): str, |
| 211 |
# Headers to pass alongside the request. |
| 212 |
Optional("headers"): { |
| 213 |
str: str, |
| 214 |
}, |
| 215 |
# IMPORTANT: when adding anything that changes the behavior of the task, |
| 216 |
# it is important to update the digest data used to compute cache hits. |
| 217 |
}, |
| 218 |
) |
| 219 |
def create_fetch_url_task(config, name, fetch): |
| 220 |
artifact_name = fetch.get("artifact-name") |
| 221 |
if not artifact_name: |
| 222 |
artifact_name = fetch["url"].split("/")[-1] |
| 223 |
|
| 224 |
command = [ |
| 225 |
"fetch-content", |
| 226 |
"static-url", |
| 227 |
] |
| 228 |
|
| 229 |
# Arguments that matter to the cache digest |
| 230 |
args = [ |
| 231 |
"--sha256", |
| 232 |
fetch["sha256"], |
| 233 |
"--size", |
| 234 |
f"{fetch['size']}", |
| 235 |
] |
| 236 |
|
| 237 |
if fetch.get("strip-components"): |
| 238 |
args.extend(["--strip-components", f"{fetch['strip-components']}"]) |
| 239 |
|
| 240 |
if fetch.get("add-prefix"): |
| 241 |
args.extend(["--add-prefix", fetch["add-prefix"]]) |
| 242 |
|
| 243 |
command.extend(args) |
| 244 |
|
| 245 |
env = {} |
| 246 |
|
| 247 |
if "gpg-signature" in fetch: |
| 248 |
sig_url = fetch["gpg-signature"]["sig-url"].format(url=fetch["url"]) |
| 249 |
key_path = os.path.join(taskgraph.GECKO, fetch["gpg-signature"]["key-path"]) # type: ignore |
| 250 |
|
| 251 |
with open(key_path) as fh: |
| 252 |
gpg_key = fh.read() |
| 253 |
|
| 254 |
env["FETCH_GPG_KEY"] = gpg_key |
| 255 |
command.extend( |
| 256 |
[ |
| 257 |
"--gpg-sig-url", |
| 258 |
sig_url, |
| 259 |
"--gpg-key-env", |
| 260 |
"FETCH_GPG_KEY", |
| 261 |
] |
| 262 |
) |
| 263 |
|
| 264 |
if "headers" in fetch: |
| 265 |
for k, v in fetch["headers"].items(): |
| 266 |
command.extend(["-H", f"{k}:{v}"]) |
| 267 |
|
| 268 |
command.extend([fetch["url"], f"/builds/worker/artifacts/{artifact_name}"]) |
| 269 |
|
| 270 |
return { |
| 271 |
"command": command, |
| 272 |
"artifact_name": artifact_name, |
| 273 |
"env": env, |
| 274 |
# We don't include the GPG signature in the digest because it isn't |
| 275 |
# materially important for caching: GPG signatures are supplemental |
| 276 |
# trust checking beyond what the shasum already provides. |
| 277 |
"digest_data": args + [artifact_name], |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
@fetch_builder( |
| 282 |
"git", |
| 283 |
schema={ |
| 284 |
Required("repo"): str, |
| 285 |
Required("revision"): str, |
| 286 |
Optional("include-dot-git"): bool, |
| 287 |
Optional("artifact-name"): str, |
| 288 |
Optional("path-prefix"): str, |
| 289 |
# ssh-key is a taskcluster secret path (e.g. project/civet/github-deploy-key) |
| 290 |
# In the secret dictionary, the key should be specified as |
| 291 |
# "ssh_privkey": "-----BEGIN OPENSSH PRIVATE KEY-----\nkfksnb3jc..." |
| 292 |
# n.b. The OpenSSH private key file format requires a newline at the end of the file. |
| 293 |
Optional("ssh-key"): str, |
| 294 |
}, |
| 295 |
) |
| 296 |
def create_git_fetch_task(config, name, fetch): |
| 297 |
path_prefix = fetch.get("path-prefix") |
| 298 |
if not path_prefix: |
| 299 |
path_prefix = fetch["repo"].rstrip("/").rsplit("/", 1)[-1] |
| 300 |
artifact_name = fetch.get("artifact-name") |
| 301 |
if not artifact_name: |
| 302 |
artifact_name = f"{path_prefix}.tar.zst" |
| 303 |
|
| 304 |
if not re.match(r"[0-9a-fA-F]{40}", fetch["revision"]): |
| 305 |
raise Exception(f'Revision is not a sha1 in fetch task "{name}"') |
| 306 |
|
| 307 |
args = [ |
| 308 |
"fetch-content", |
| 309 |
"git-checkout-archive", |
| 310 |
"--path-prefix", |
| 311 |
path_prefix, |
| 312 |
fetch["repo"], |
| 313 |
fetch["revision"], |
| 314 |
f"/builds/worker/artifacts/{artifact_name}", |
| 315 |
] |
| 316 |
|
| 317 |
ssh_key = fetch.get("ssh-key") |
| 318 |
if ssh_key: |
| 319 |
args.append("--ssh-key-secret") |
| 320 |
args.append(ssh_key) |
| 321 |
|
| 322 |
digest_data = [fetch["revision"], path_prefix, artifact_name] |
| 323 |
if fetch.get("include-dot-git", False): |
| 324 |
args.append("--include-dot-git") |
| 325 |
digest_data.append(".git") |
| 326 |
|
| 327 |
return { |
| 328 |
"command": args, |
| 329 |
"artifact_name": artifact_name, |
| 330 |
"digest_data": digest_data, |
| 331 |
"secret": ssh_key, |
| 332 |
} |