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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
# 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 http://mozilla.org/MPL/2.0/.
import logging
import os
import shutil
import sys
import time
from collections import defaultdict
from pathlib import Path
import yaml
from redo import retry
from taskgraph import create
from taskgraph.create import create_tasks
from taskgraph.generator import TaskGraphGenerator
from taskgraph.main import format_kind_graph_mermaid
from taskgraph.parameters import Parameters
from taskgraph.taskgraph import TaskGraph
from taskgraph.util import json
from taskgraph.util.python_path import find_object
from taskgraph.util.taskcluster import get_artifact
from taskgraph.util.vcs import get_repository
from taskgraph.util.yaml import load_yaml
from . import GECKO
from .actions import render_actions_json
from .parameters import get_app_version, get_version
from .util.backstop import ANDROID_PERFTEST_BACKSTOP_INDEX, BACKSTOP_INDEX, is_backstop
from .util.bugbug import push_schedules
from .util.hg import get_hg_revision_branch, get_hg_revision_info
from .util.partials import populate_release_history
from .util.taskcluster import insert_index
from .util.taskgraph import find_decision_task, find_existing_tasks_from_previous_kinds
logger = logging.getLogger(__name__)
ARTIFACTS_DIR = os.environ.get("MOZ_UPLOAD_DIR", "artifacts")
# For each project, this gives a set of parameters specific to the project.
# See `taskcluster/docs/parameters.rst` for information on parameters.
PER_PROJECT_PARAMETERS = {
"try": {
"enable_always_target": True,
"target_tasks_method": "try_tasks",
"release_type": "nightly",
},
"kaios-try": {
"target_tasks_method": "try_tasks",
},
"ash": {
"target_tasks_method": "default",
},
"cedar": {
"target_tasks_method": "default",
},
"holly": {
"enable_always_target": True,
"target_tasks_method": "holly_tasks",
},
"oak": {
"target_tasks_method": "default",
"release_type": "nightly-oak",
},
"graphics": {
"target_tasks_method": "graphics_tasks",
},
"autoland": {
"optimize_strategies": "gecko_taskgraph.optimize:project.autoland",
"target_tasks_method": "autoland_tasks",
"test_manifest_loader": "bugbug", # Remove this line to disable "manifest scheduling".
},
"mozilla-central": {
"target_tasks_method": "mozilla_central_tasks",
"release_type": "nightly",
},
"mozilla-beta": {
"target_tasks_method": "mozilla_beta_tasks",
"release_type": "beta",
},
"mozilla-release": {
"target_tasks_method": "mozilla_release_tasks",
"release_type": "release",
},
"mozilla-esr140": {
"target_tasks_method": "mozilla_esr140_tasks",
"release_type": "esr140",
},
"pine": {
"target_tasks_method": "pine_tasks",
"release_type": "nightly-pine",
},
"maple": {
# Prevent it from running everything - For now just use "Add New Jobs"
"target_tasks_method": "nothing",
"release_type": "release",
},
"cypress": {
"target_tasks_method": "cypress_tasks",
"release_type": "nightly-cypress",
},
"larch": {
"target_tasks_method": "larch_tasks",
"release_type": "nightly-larch",
},
"kaios": {
"target_tasks_method": "kaios_tasks",
},
"toolchains": {
"target_tasks_method": "mozilla_central_tasks",
},
# git projects
"staging-firefox": {
"target_tasks_method": "default",
},
# the default parameters are used for projects that do not match above.
"default": {
"target_tasks_method": "default",
},
}
def full_task_graph_to_runnable_jobs(full_task_json):
runnable_jobs = {}
for label, node in full_task_json.items():
if not ("extra" in node["task"] and "treeherder" in node["task"]["extra"]):
continue
th = node["task"]["extra"]["treeherder"]
runnable_jobs[label] = {"symbol": th["symbol"]}
for i in ("groupName", "groupSymbol", "collection"):
if i in th:
runnable_jobs[label][i] = th[i]
if th.get("machine", {}).get("platform"):
runnable_jobs[label]["platform"] = th["machine"]["platform"]
return runnable_jobs
def full_task_graph_to_manifests_by_task(full_task_json):
manifests_by_task = defaultdict(list)
for label, node in full_task_json.items():
manifests = node["attributes"].get("test_manifests")
if not manifests:
continue
manifests_by_task[label].extend(manifests)
return manifests_by_task
def try_syntax_from_message(message):
"""
Parse the try syntax out of a commit message, returning '' if none is
found.
"""
try_idx = message.find("try:")
if try_idx == -1:
return ""
return message[try_idx:].split("\n", 1)[0]
def taskgraph_decision(options, parameters=None):
"""
Run the decision task. This function implements `mach taskgraph decision`,
and is responsible for
* processing decision task command-line options into parameters
* running task-graph generation exactly the same way the other `mach
taskgraph` commands do
* generating a set of artifacts to memorialize the graph
* calling TaskCluster APIs to create the graph
"""
parameters = parameters or (
lambda graph_config: get_decision_parameters(graph_config, options)
)
decision_task_id = os.environ["TASK_ID"]
# create a TaskGraphGenerator instance
tgg = TaskGraphGenerator(
root_dir=options.get("root"),
parameters=parameters,
decision_task_id=decision_task_id,
write_artifacts=True,
enable_verifications=options.get("verify", True),
)
if not create.testing:
# set additional index paths for the decision task
set_decision_indexes(decision_task_id, tgg.parameters, tgg.graph_config)
# write out the parameters used to generate this graph
write_artifact("parameters.yml", dict(**tgg.parameters))
# write out the public/actions.json file
write_artifact(
"actions.json",
render_actions_json(tgg.parameters, tgg.graph_config, decision_task_id),
)
# write out the full graph for reference
full_task_json = tgg.full_task_graph.to_json()
write_artifact("full-task-graph.json", full_task_json)
# write out kind graph
write_artifact("kind-graph.mm", format_kind_graph_mermaid(tgg.kind_graph))
# write out the public/runnable-jobs.json file
write_artifact(
"runnable-jobs.json", full_task_graph_to_runnable_jobs(full_task_json)
)
# write out the public/manifests-by-task.json file
write_artifact(
"manifests-by-task.json.gz",
full_task_graph_to_manifests_by_task(full_task_json),
)
# `tests-by-manifest.json.gz` was previously written out here
# it was moved to `loader/test.py` because its contents now depend on
# data generated in a subprocess which we do not have access to here
# see https://bugzilla.mozilla.org/show_bug.cgi?id=1989038 for additional
# details
# this is just a test to check whether the from_json() function is working
_, _ = TaskGraph.from_json(full_task_json)
# write out the target task set to allow reproducing this as input
write_artifact("target-tasks.json", list(tgg.target_task_set.tasks.keys()))
# write out the optimized task graph to describe what will actually happen,
# and the map of labels to taskids
write_artifact("task-graph.json", tgg.morphed_task_graph.to_json())
write_artifact("label-to-taskid.json", tgg.label_to_taskid)
# write bugbug scheduling information if it was invoked
if len(push_schedules) > 0:
write_artifact("bugbug-push-schedules.json", push_schedules.popitem()[1])
# upload run-task, fetch-content, robustcheckout.py and more as artifacts
mozharness_dir = Path(GECKO, "testing", "mozharness")
scripts_dir = Path(GECKO, "taskcluster", "scripts")
taskgraph_dir = Path(
GECKO, "third_party", "python", "taskcluster_taskgraph", "taskgraph"
)
to_copy = {
scripts_dir / "run-task": f"{ARTIFACTS_DIR}/run-task-hg",
scripts_dir / "tester" / "test-linux.sh": ARTIFACTS_DIR,
taskgraph_dir / "run-task" / "fetch-content": ARTIFACTS_DIR,
taskgraph_dir / "run-task" / "run-task": f"{ARTIFACTS_DIR}/run-task-git",
mozharness_dir / "external_tools" / "robustcheckout.py": ARTIFACTS_DIR,
}
for target, dest in to_copy.items():
shutil.copy2(target, dest)
# actually create the graph
create_tasks(
tgg.graph_config,
tgg.morphed_task_graph,
tgg.label_to_taskid,
tgg.parameters,
decision_task_id=decision_task_id,
)
def get_decision_parameters(graph_config, options):
"""
Load parameters from the command-line options for 'taskgraph decision'.
This also applies per-project parameters, based on the given project.
"""
product_dir = graph_config["product-dir"]
parameters = {
n: options[n]
for n in [
"base_repository",
"base_ref",
"base_rev",
"head_repository",
"head_rev",
"head_ref",
"head_tag",
"project",
"pushlog_id",
"pushdate",
"owner",
"level",
"repository_type",
"target_tasks_method",
"tasks_for",
]
if n in options
}
repo_path = os.getcwd()
repo = get_repository(repo_path)
try:
commit_message = repo.get_commit_message()
except UnicodeDecodeError:
commit_message = ""
# Set some vcs specific parameters
if parameters["repository_type"] == "hg":
if head_git_rev := get_hg_revision_info(
GECKO, revision=parameters["head_rev"], info="extras.git_commit"
):
parameters["head_git_rev"] = head_git_rev
parameters["hg_branch"] = get_hg_revision_branch(
GECKO, revision=parameters["head_rev"]
)
changed_files_since_base = set(
repo.get_changed_files(
rev=parameters["head_rev"], base=parameters["base_rev"]
)
)
if "try" in parameters["project"] and options["tasks_for"] == "hg-push":
parameters["files_changed"] = sorted(
set(repo.get_outgoing_files()) | changed_files_since_base
)
else:
parameters["files_changed"] = sorted(changed_files_since_base)
elif parameters["repository_type"] == "git":
parameters["hg_branch"] = None
parameters["files_changed"] = repo.get_changed_files(
rev=parameters["head_rev"], base=parameters["base_rev"]
)
# Define default filter list, as most configurations shouldn't need
# custom filters.
parameters["filters"] = [
"target_tasks_method",
]
parameters["enable_always_target"] = ["docker-image"]
parameters["existing_tasks"] = {}
parameters["do_not_optimize"] = []
parameters["build_number"] = 1
parameters["version"] = get_version(product_dir)
parameters["app_version"] = get_app_version(product_dir)
parameters["message"] = try_syntax_from_message(commit_message)
parameters["next_version"] = None
parameters["optimize_strategies"] = None
parameters["optimize_target_tasks"] = True
parameters["phabricator_diff"] = None
parameters["release_type"] = ""
parameters["release_eta"] = ""
parameters["release_enable_partner_repack"] = False
parameters["release_enable_partner_attribution"] = False
parameters["release_partners"] = []
parameters["release_partner_config"] = {}
parameters["release_partner_build_number"] = 1
parameters["release_enable_emefree"] = False
parameters["release_product"] = None
parameters["required_signoffs"] = []
parameters["signoff_urls"] = {}
parameters["test_manifest_loader"] = "default"
parameters["try_mode"] = None
parameters["try_task_config"] = {}
# owner must be an email, but sometimes (e.g., for ffxbld) it is not, in which
# case, fake it
if "@" not in parameters["owner"]:
parameters["owner"] += "@noreply.mozilla.org"
# use the pushdate as build_date if given, else use current time
parameters["build_date"] = parameters["pushdate"] or int(time.time())
# moz_build_date is the build identifier based on build_date
parameters["moz_build_date"] = time.strftime(
"%Y%m%d%H%M%S", time.gmtime(parameters["build_date"])
)
project = parameters["project"]
try:
parameters.update(PER_PROJECT_PARAMETERS[project])
except KeyError:
logger.warning(
f"using default project parameters; add {project} to "
f"PER_PROJECT_PARAMETERS in {__file__} to customize behavior "
"for this project"
)
parameters.update(PER_PROJECT_PARAMETERS["default"])
# `target_tasks_method` has higher precedence than `project` parameters
if options.get("target_tasks_method"):
parameters["target_tasks_method"] = options["target_tasks_method"]
# ..but can be overridden by the commit message: if it contains the special
# string "DONTBUILD" and this is an on-push decision task, then use the
# special 'nothing' target task method.
if "DONTBUILD" in commit_message and options["tasks_for"] == "hg-push":
parameters["target_tasks_method"] = "nothing"
if options.get("include_push_tasks"):
get_existing_tasks(options.get("rebuild_kinds", []), parameters, graph_config)
# If the target method is nightly, we should build partials. This means
# knowing what has been released previously.
# An empty release_history is fine, it just means no partials will be built
parameters.setdefault("release_history", dict())
if "nightly" in parameters.get("target_tasks_method", ""):
parameters["release_history"] = populate_release_history("Firefox", project)
if options.get("try_task_config_file"):
task_config_file = os.path.abspath(options.get("try_task_config_file"))
else:
# if try_task_config.json is present, load it
task_config_file = os.path.join(os.getcwd(), "try_task_config.json")
# load try settings
if "try" in project and options["tasks_for"] == "hg-push":
set_try_config(parameters, task_config_file)
if options.get("optimize_target_tasks") is not None:
parameters["optimize_target_tasks"] = options["optimize_target_tasks"]
# Determine if this should be a backstop push.
parameters["backstop"] = is_backstop(parameters)
# For the android perf tasks, run them 50% less often
parameters["android_perftest_backstop"] = is_backstop(
parameters,
push_interval=30,
time_interval=60 * 6,
backstop_strategy="android_perftest_backstop",
)
if "decision-parameters" in graph_config["taskgraph"]:
find_object(graph_config["taskgraph"]["decision-parameters"])(
graph_config, parameters
)
result = Parameters(**parameters)
result.check()
return result
def get_existing_tasks(rebuild_kinds, parameters, graph_config):
"""
Find the decision task corresponding to the on-push graph, and return
a mapping of labels to task-ids from it. This will skip the kinds specificed
by `rebuild_kinds`.
"""
try:
decision_task = retry(
find_decision_task,
args=(parameters, graph_config),
attempts=4,
sleeptime=5 * 60,
)
except Exception:
logger.exception("Didn't find existing push task.")
sys.exit(1)
_, task_graph = TaskGraph.from_json(
get_artifact(decision_task, "public/full-task-graph.json")
)
parameters["existing_tasks"] = find_existing_tasks_from_previous_kinds(
task_graph, [decision_task], rebuild_kinds
)
def set_try_config(parameters, task_config_file):
if os.path.isfile(task_config_file):
logger.info(f"using try tasks from {task_config_file}")
with open(task_config_file) as fh:
task_config = json.load(fh)
task_config_version = task_config.pop("version", 1)
if task_config_version == 1:
parameters["try_mode"] = "try_task_config"
parameters["try_task_config"] = task_config
elif task_config_version == 2:
parameters.update(task_config["parameters"])
parameters["try_mode"] = "try_task_config"
else:
raise Exception(
f"Unknown `try_task_config.json` version: {task_config_version}"
)
def set_decision_indexes(decision_task_id, params, graph_config):
index_paths = []
if params["android_perftest_backstop"]:
index_paths.insert(0, ANDROID_PERFTEST_BACKSTOP_INDEX)
if params["backstop"]:
# When two Decision tasks run at nearly the same time, it's possible
# they both end up being backstops if the second checks the backstop
# index before the first inserts it. Insert this index first to reduce
# the chances of that happening.
index_paths.insert(0, BACKSTOP_INDEX)
subs = params.copy()
subs["trust-domain"] = graph_config["trust-domain"]
for index_path in index_paths:
insert_index(index_path.format(**subs), decision_task_id)
def write_artifact(filename, data):
logger.info(f"writing artifact file `{filename}`")
if not os.path.isdir(ARTIFACTS_DIR):
os.mkdir(ARTIFACTS_DIR)
path = os.path.join(ARTIFACTS_DIR, filename)
if filename.endswith(".yml"):
with open(path, "w") as f:
yaml.safe_dump(data, f, allow_unicode=True, default_flow_style=False)
elif filename.endswith(".json"):
with open(path, "w") as f:
json.dump(data, f)
elif filename.endswith(".json.gz"):
import gzip
with gzip.open(path, "wb") as f:
f.write(json.dumps(data).encode("utf-8"))
else:
with open(path, "w") as f:
f.write(data)
def read_artifact(filename):
path = os.path.join(ARTIFACTS_DIR, filename)
if filename.endswith(".yml"):
return load_yaml(path, filename)
if filename.endswith(".json"):
with open(path) as f:
return json.load(f)
if filename.endswith(".json.gz"):
import gzip
with gzip.open(path, "rb") as f:
return json.load(f)
else:
raise TypeError(f"Don't know how to read {filename}")
def rename_artifact(src, dest):
os.rename(os.path.join(ARTIFACTS_DIR, src), os.path.join(ARTIFACTS_DIR, dest))
|