Bug 1258497: actually create tasks, using the taskcluster proxy draft
authorDustin J. Mitchell <dustin@mozilla.com>
Mon, 02 May 2016 15:54:58 +0000
changeset 358386 69cd138719851ce8f06befbe13574479a3555290
parent 358385 b7a281510f328a68cdb65e2f7dd52d3a9caf762c
child 519842 926d689f77f6722b26d80cb54097020433ee7542
push id16995
push userdmitchell@mozilla.com
push dateMon, 02 May 2016 18:47:33 +0000
bugs1258497
milestone49.0a1
Bug 1258497: actually create tasks, using the taskcluster proxy This calls the TaskCluster API directly, rather than relying on the worker implementation to do so. MozReview-Commit-ID: H6ABkLS6zjR
taskcluster/mach_commands.py
taskcluster/taskgraph/create.py
--- a/taskcluster/mach_commands.py
+++ b/taskcluster/mach_commands.py
@@ -148,16 +148,17 @@ class MachCommands(MachCommandBase):
         default="1",
         help='SCM level of this repository')
     def taskgraph_decision(self, **options):
         # load parameters from env vars, command line, etc.
         parameters = self.get_decision_parameters(options)
 
         # create a TaskGraphGenerator instance
         import taskgraph.generator
+        import taskgraph.create
         tgg = taskgraph.generator.TaskGraphGenerator(
             root=options['root'],
             log=self.log,
             parameters=parameters,
             optimization_finder=None)  # XXX
 
         # produce some artifacts
         def write_artifact(filename, data):
@@ -188,17 +189,18 @@ class MachCommands(MachCommandBase):
         # write out the full graph for reference
         write_artifact('full-task-graph.json',
                        self.taskgraph_to_json(tgg.full_task_graph))
 
         # write out the optimized task graph to describe what will happen
         write_artifact('task-graph.json',
                        self.taskgraph_to_json(tgg.optimized_task_graph))
 
-        # TODO: call the taskcluster API to create the tasks in the optimized graph
+        # actually create the graph
+        taskgraph.create.create_tasks(tgg.optimized_task_graph)
 
     ##
     # Parameter handling
 
     def load_parameters_file(self, options):
         filename = options['parameters']
         if not filename:
             return {}
new file mode 100644
--- /dev/null
+++ b/taskcluster/taskgraph/create.py
@@ -0,0 +1,35 @@
+# 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/.
+from __future__ import unicode_literals
+
+import requests
+import json
+import collections
+
+from slugid import nice as slugid
+
+def create_tasks(taskgraph):
+    task_group_id = slugid()
+    label_to_taskid = collections.defaultdict(slugid)
+
+    for label in taskgraph.graph.visit_postorder():
+        task = taskgraph.tasks[label]
+        deps = [r for (l, r, n) in taskgraph.graph.edges if l == label]
+        task_def = task.task
+        task_def['taskGroupId'] = task_group_id
+        task_def['dependencies'] = [label_to_taskid[l] for l in deps]
+        task_def['requires'] = 'all-completed'
+
+        _create_task(label_to_taskid[label], task_def)
+
+def _create_task(task_id, task_def):
+    # create the task using 'http://taskcluster/queue', which is proxied to the queue service
+    # with credentials appropriate to this job.
+    res = requests.put('http://taskcluster/queue/v1/task/{}'.format(task_id), data=json.dumps(task_def))
+    if res.status_code != 200:
+        try:
+            print res.json()['message']
+        except:
+            print res.text
+        res.raise_for_status()