action.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import json
  7. import logging
  8. import requests
  9. import yaml
  10. from .create import create_tasks
  11. from .decision import write_artifact
  12. from .optimize import optimize_task_graph
  13. from .taskgraph import TaskGraph
  14. logger = logging.getLogger(__name__)
  15. TASKCLUSTER_QUEUE_URL = "https://queue.taskcluster.net/v1/task/"
  16. def taskgraph_action(options):
  17. """
  18. Run the action task. This function implements `mach taskgraph action-task`,
  19. and is responsible for
  20. * creating taskgraph of tasks asked for in parameters with respect to
  21. a given gecko decision task and schedule these jobs.
  22. """
  23. decision_task_id = options['decision_id']
  24. # read in the full graph for reference
  25. full_task_json = get_artifact(decision_task_id, "public/full-task-graph.json")
  26. decision_params = get_artifact(decision_task_id, "public/parameters.yml")
  27. all_tasks, full_task_graph = TaskGraph.from_json(full_task_json)
  28. target_tasks = set(options['task_labels'].split(','))
  29. target_graph = full_task_graph.graph.transitive_closure(target_tasks)
  30. target_task_graph = TaskGraph(
  31. {l: all_tasks[l] for l in target_graph.nodes},
  32. target_graph)
  33. existing_tasks = get_artifact(decision_task_id, "public/label-to-taskid.json")
  34. # We don't want to optimize target tasks since they have been requested by user
  35. # Hence we put `target_tasks under` `do_not_optimize`
  36. optimized_graph, label_to_taskid = optimize_task_graph(target_task_graph=target_task_graph,
  37. params=decision_params,
  38. do_not_optimize=target_tasks,
  39. existing_tasks=existing_tasks)
  40. # write out the optimized task graph to describe what will actually happen,
  41. # and the map of labels to taskids
  42. write_artifact('task-graph.json', optimized_graph.to_json())
  43. write_artifact('label-to-taskid.json', label_to_taskid)
  44. # actually create the graph
  45. create_tasks(optimized_graph, label_to_taskid, decision_params)
  46. def get_artifact(task_id, path):
  47. url = TASKCLUSTER_QUEUE_URL + task_id + "/artifacts/" + path
  48. resp = requests.get(url=url)
  49. if path.endswith('.json'):
  50. artifact = json.loads(resp.text)
  51. elif path.endswith('.yml'):
  52. artifact = yaml.load(resp.text)
  53. return artifact