maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Marc-Antoine Ruel | 8add124 | 2013-11-05 17:28:27 -0500 | [diff] [blame] | 2 | # Copyright 2013 The Swarming Authors. All rights reserved. |
Marc-Antoine Ruel | e98b112 | 2013-11-05 20:27:57 -0500 | [diff] [blame] | 3 | # Use of this source code is governed under the Apache License, Version 2.0 that |
| 4 | # can be found in the LICENSE file. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 5 | |
| 6 | """Client tool to trigger tasks or retrieve results from a Swarming server.""" |
| 7 | |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 8 | __version__ = '0.4.11' |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 9 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 10 | import datetime |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 11 | import getpass |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 12 | import hashlib |
| 13 | import json |
| 14 | import logging |
| 15 | import os |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 16 | import re |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 17 | import shutil |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 18 | import subprocess |
| 19 | import sys |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 20 | import threading |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 21 | import time |
| 22 | import urllib |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 23 | |
| 24 | from third_party import colorama |
| 25 | from third_party.depot_tools import fix_encoding |
| 26 | from third_party.depot_tools import subcommand |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 27 | |
Marc-Antoine Ruel | 8806e62 | 2014-02-12 14:15:53 -0500 | [diff] [blame] | 28 | from utils import file_path |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 29 | from third_party.chromium import natsort |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 30 | from utils import net |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 31 | from utils import on_error |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 32 | from utils import threading_utils |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 33 | from utils import tools |
| 34 | from utils import zip_package |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 35 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 36 | import auth |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame] | 37 | import isolateserver |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 38 | import run_isolated |
| 39 | |
| 40 | |
| 41 | ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 42 | TOOLS_PATH = os.path.join(ROOT_DIR, 'tools') |
| 43 | |
| 44 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 45 | # The default time to wait for a shard to finish running. |
csharp@chromium.org | 2475849 | 2013-08-28 19:10:54 +0000 | [diff] [blame] | 46 | DEFAULT_SHARD_WAIT_TIME = 80 * 60. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 47 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 48 | # How often to print status updates to stdout in 'collect'. |
| 49 | STATUS_UPDATE_INTERVAL = 15 * 60. |
| 50 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 51 | |
| 52 | NO_OUTPUT_FOUND = ( |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 53 | 'No output produced by the task, it may have failed to run.\n' |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 54 | '\n') |
| 55 | |
| 56 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 57 | class Failure(Exception): |
| 58 | """Generic failure.""" |
| 59 | pass |
| 60 | |
| 61 | |
| 62 | class Manifest(object): |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 63 | """Represents a Swarming task manifest.""" |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 64 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 65 | def __init__( |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 66 | self, isolate_server, namespace, isolated_hash, task_name, extra_args, |
Marc-Antoine Ruel | aea5065 | 2014-06-12 14:23:48 -0400 | [diff] [blame] | 67 | env, dimensions, deadline, verbose, profile, |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 68 | priority): |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 69 | """Populates a manifest object. |
| 70 | Args: |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 71 | isolate_server - isolate server url. |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 72 | namespace - isolate server namespace to use. |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 73 | isolated_hash - the manifest's sha-1 that the slave is going to fetch. |
| 74 | task_name - the name to give the task request. |
| 75 | extra_args - additional arguments to pass to isolated command. |
Marc-Antoine Ruel | 05dab5e | 2013-11-06 15:06:47 -0500 | [diff] [blame] | 76 | env - environment variables to set. |
Marc-Antoine Ruel | 92f3242 | 2013-11-06 18:12:13 -0500 | [diff] [blame] | 77 | dimensions - dimensions to filter the task on. |
Marc-Antoine Ruel | 13b7b78 | 2014-03-14 11:14:57 -0400 | [diff] [blame] | 78 | deadline - maximum pending time before this task expires. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 79 | verbose - if True, have the slave print more details. |
| 80 | profile - if True, have the slave print more timing data. |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame] | 81 | priority - int between 0 and 1000, lower the higher priority. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 82 | """ |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 83 | self.isolate_server = isolate_server |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 84 | self.namespace = namespace |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 85 | self.isolated_hash = isolated_hash |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 86 | self.task_name = task_name |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 87 | self.extra_args = tuple(extra_args or []) |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 88 | self.env = env.copy() |
| 89 | self.dimensions = dimensions.copy() |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 90 | self.deadline = deadline |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 91 | self.verbose = bool(verbose) |
| 92 | self.profile = bool(profile) |
| 93 | self.priority = priority |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 94 | self._tasks = [] |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 95 | self._files = [] |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 96 | |
Marc-Antoine Ruel | af78a90 | 2014-03-20 10:42:49 -0400 | [diff] [blame] | 97 | def add_task(self, task_name, actions, time_out=2*60*60): |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 98 | """Appends a new task as a TestObject to the swarming manifest file. |
Marc-Antoine Ruel | cd62973 | 2013-12-20 15:00:42 -0500 | [diff] [blame] | 99 | |
| 100 | Tasks cannot be added once the manifest was uploaded. |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 101 | |
Marc-Antoine Ruel | af78a90 | 2014-03-20 10:42:49 -0400 | [diff] [blame] | 102 | By default, command will be killed after 2 hours of execution. |
| 103 | |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 104 | See TestObject in services/swarming/src/common/test_request_message.py for |
| 105 | the valid format. |
Marc-Antoine Ruel | cd62973 | 2013-12-20 15:00:42 -0500 | [diff] [blame] | 106 | """ |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 107 | self._tasks.append( |
| 108 | { |
| 109 | 'action': actions, |
| 110 | 'decorate_output': self.verbose, |
| 111 | 'test_name': task_name, |
Marc-Antoine Ruel | af78a90 | 2014-03-20 10:42:49 -0400 | [diff] [blame] | 112 | 'hard_time_out': time_out, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 113 | }) |
| 114 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 115 | def add_bundled_file(self, file_name, file_url): |
| 116 | """Appends a file to the manifest. |
| 117 | |
| 118 | File will be downloaded and extracted by the swarm bot before launching the |
| 119 | task. |
| 120 | """ |
| 121 | self._files.append([file_url, file_name]) |
| 122 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 123 | def to_json(self): |
| 124 | """Exports the current configuration into a swarm-readable manifest file. |
| 125 | |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 126 | The actual serialization format is defined as a TestCase object as described |
| 127 | in services/swarming/src/common/test_request_message.py |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 128 | """ |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 129 | request = { |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 130 | 'cleanup': 'root', |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 131 | 'configurations': [ |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 132 | # Is a TestConfiguration. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 133 | { |
Marc-Antoine Ruel | 5d79919 | 2013-11-06 15:20:39 -0500 | [diff] [blame] | 134 | 'config_name': 'isolated', |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 135 | 'deadline_to_run': self.deadline, |
| 136 | 'dimensions': self.dimensions, |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 137 | 'priority': self.priority, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 138 | }, |
| 139 | ], |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 140 | 'data': self._files, |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 141 | 'env_vars': self.env, |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 142 | 'test_case_name': self.task_name, |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 143 | 'tests': self._tasks, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 144 | } |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 145 | return json.dumps(request, sort_keys=True, separators=(',',':')) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 146 | |
Marc-Antoine Ruel | cd62973 | 2013-12-20 15:00:42 -0500 | [diff] [blame] | 147 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 148 | class TaskOutputCollector(object): |
| 149 | """Fetches task output from isolate server to local disk. |
| 150 | |
| 151 | This object is shared among multiple threads running 'retrieve_results' |
| 152 | function, in particular they call 'process_shard_result' method in parallel. |
| 153 | """ |
| 154 | |
| 155 | def __init__(self, task_output_dir, task_name, shard_count): |
| 156 | """Initializes TaskOutputCollector, ensures |task_output_dir| exists. |
| 157 | |
| 158 | Args: |
| 159 | task_output_dir: local directory to put fetched files to. |
| 160 | task_name: name of the swarming task results belong to. |
| 161 | shard_count: expected number of task shards. |
| 162 | """ |
| 163 | self.task_output_dir = task_output_dir |
| 164 | self.task_name = task_name |
| 165 | self.shard_count = shard_count |
| 166 | |
| 167 | self._lock = threading.Lock() |
| 168 | self._per_shard_results = {} |
| 169 | self._storage = None |
| 170 | |
| 171 | if not os.path.isdir(self.task_output_dir): |
| 172 | os.makedirs(self.task_output_dir) |
| 173 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 174 | def process_shard_result(self, shard_index, result): |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 175 | """Stores results of a single task shard, fetches output files if necessary. |
| 176 | |
| 177 | Called concurrently from multiple threads. |
| 178 | """ |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 179 | # Sanity check index is in expected range. |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 180 | assert isinstance(shard_index, int) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 181 | if shard_index < 0 or shard_index >= self.shard_count: |
| 182 | logging.warning( |
| 183 | 'Shard index %d is outside of expected range: [0; %d]', |
| 184 | shard_index, self.shard_count - 1) |
| 185 | return |
| 186 | |
| 187 | # Store result dict of that shard, ignore results we've already seen. |
| 188 | with self._lock: |
| 189 | if shard_index in self._per_shard_results: |
| 190 | logging.warning('Ignoring duplicate shard index %d', shard_index) |
| 191 | return |
| 192 | self._per_shard_results[shard_index] = result |
| 193 | |
| 194 | # Fetch output files if necessary. |
| 195 | isolated_files_location = extract_output_files_location(result['output']) |
| 196 | if isolated_files_location: |
| 197 | isolate_server, namespace, isolated_hash = isolated_files_location |
| 198 | storage = self._get_storage(isolate_server, namespace) |
| 199 | if storage: |
| 200 | # Output files are supposed to be small and they are not reused across |
| 201 | # tasks. So use MemoryCache for them instead of on-disk cache. Make |
| 202 | # files writable, so that calling script can delete them. |
| 203 | isolateserver.fetch_isolated( |
| 204 | isolated_hash, |
| 205 | storage, |
| 206 | isolateserver.MemoryCache(file_mode_mask=0700), |
| 207 | os.path.join(self.task_output_dir, str(shard_index)), |
| 208 | False) |
| 209 | |
| 210 | def finalize(self): |
| 211 | """Writes summary.json, shutdowns underlying Storage.""" |
| 212 | with self._lock: |
| 213 | # Write an array of shard results with None for missing shards. |
| 214 | summary = { |
| 215 | 'task_name': self.task_name, |
| 216 | 'shards': [ |
| 217 | self._per_shard_results.get(i) for i in xrange(self.shard_count) |
| 218 | ], |
| 219 | } |
| 220 | tools.write_json( |
| 221 | os.path.join(self.task_output_dir, 'summary.json'), |
| 222 | summary, |
| 223 | False) |
| 224 | if self._storage: |
| 225 | self._storage.close() |
| 226 | self._storage = None |
| 227 | |
| 228 | def _get_storage(self, isolate_server, namespace): |
| 229 | """Returns isolateserver.Storage to use to fetch files.""" |
| 230 | with self._lock: |
| 231 | if not self._storage: |
| 232 | self._storage = isolateserver.get_storage(isolate_server, namespace) |
| 233 | else: |
| 234 | # Shards must all use exact same isolate server and namespace. |
| 235 | if self._storage.location != isolate_server: |
| 236 | logging.error( |
| 237 | 'Task shards are using multiple isolate servers: %s and %s', |
| 238 | self._storage.location, isolate_server) |
| 239 | return None |
| 240 | if self._storage.namespace != namespace: |
| 241 | logging.error( |
| 242 | 'Task shards are using multiple namespaces: %s and %s', |
| 243 | self._storage.namespace, namespace) |
| 244 | return None |
| 245 | return self._storage |
| 246 | |
| 247 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 248 | def now(): |
| 249 | """Exists so it can be mocked easily.""" |
| 250 | return time.time() |
| 251 | |
| 252 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 253 | def get_task_keys(swarm_base_url, task_name): |
| 254 | """Returns the Swarming task key for each shards of task_name.""" |
| 255 | key_data = urllib.urlencode([('name', task_name)]) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 256 | url = '%s/get_matching_test_cases?%s' % (swarm_base_url, key_data) |
| 257 | |
vadimsh@chromium.org | 043b76d | 2013-09-12 16:15:13 +0000 | [diff] [blame] | 258 | for _ in net.retry_loop(max_attempts=net.URL_OPEN_MAX_ATTEMPTS): |
| 259 | result = net.url_read(url, retry_404=True) |
| 260 | if result is None: |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 261 | raise Failure( |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 262 | 'Error: Unable to find any task with the name, %s, on swarming server' |
| 263 | % task_name) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 264 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 265 | # TODO(maruel): Compare exact string. |
| 266 | if 'No matching' in result: |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 267 | logging.warning('Unable to find any task with the name, %s, on swarming ' |
| 268 | 'server' % task_name) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 269 | continue |
| 270 | return json.loads(result) |
| 271 | |
| 272 | raise Failure( |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 273 | 'Error: Unable to find any task with the name, %s, on swarming server' |
| 274 | % task_name) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 275 | |
| 276 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 277 | def extract_output_files_location(task_log): |
| 278 | """Task log -> location of task output files to fetch. |
| 279 | |
| 280 | TODO(vadimsh,maruel): Use side-channel to get this information. |
| 281 | See 'run_tha_test' in run_isolated.py for where the data is generated. |
| 282 | |
| 283 | Returns: |
| 284 | Tuple (isolate server URL, namespace, isolated hash) on success. |
| 285 | None if information is missing or can not be parsed. |
| 286 | """ |
| 287 | match = re.search( |
| 288 | r'\[run_isolated_out_hack\](.*)\[/run_isolated_out_hack\]', |
| 289 | task_log, |
| 290 | re.DOTALL) |
| 291 | if not match: |
| 292 | return None |
| 293 | |
| 294 | def to_ascii(val): |
| 295 | if not isinstance(val, basestring): |
| 296 | raise ValueError() |
| 297 | return val.encode('ascii') |
| 298 | |
| 299 | try: |
| 300 | data = json.loads(match.group(1)) |
| 301 | if not isinstance(data, dict): |
| 302 | raise ValueError() |
| 303 | isolated_hash = to_ascii(data['hash']) |
| 304 | namespace = to_ascii(data['namespace']) |
| 305 | isolate_server = to_ascii(data['storage']) |
| 306 | if not file_path.is_url(isolate_server): |
| 307 | raise ValueError() |
| 308 | return (isolate_server, namespace, isolated_hash) |
| 309 | except (KeyError, ValueError): |
| 310 | logging.warning( |
| 311 | 'Unexpected value of run_isolated_out_hack: %s', match.group(1)) |
| 312 | return None |
| 313 | |
| 314 | |
| 315 | def retrieve_results( |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 316 | base_url, shard_index, task_key, timeout, should_stop, output_collector): |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 317 | """Retrieves results for a single task_key. |
| 318 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 319 | Returns: |
| 320 | <result dict> on success. |
| 321 | None on failure. |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 322 | """ |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 323 | assert isinstance(timeout, float), timeout |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 324 | params = [('r', task_key)] |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 325 | result_url = '%s/get_result?%s' % (base_url, urllib.urlencode(params)) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 326 | started = now() |
| 327 | deadline = started + timeout if timeout else None |
| 328 | attempt = 0 |
| 329 | |
| 330 | while not should_stop.is_set(): |
| 331 | attempt += 1 |
| 332 | |
| 333 | # Waiting for too long -> give up. |
| 334 | current_time = now() |
| 335 | if deadline and current_time >= deadline: |
| 336 | logging.error('retrieve_results(%s) timed out on attempt %d', |
| 337 | base_url, attempt) |
| 338 | return None |
| 339 | |
| 340 | # Do not spin too fast. Spin faster at the beginning though. |
| 341 | # Start with 1 sec delay and for each 30 sec of waiting add another second |
| 342 | # of delay, until hitting 15 sec ceiling. |
| 343 | if attempt > 1: |
| 344 | max_delay = min(15, 1 + (current_time - started) / 30.0) |
| 345 | delay = min(max_delay, deadline - current_time) if deadline else max_delay |
| 346 | if delay > 0: |
| 347 | logging.debug('Waiting %.1f sec before retrying', delay) |
| 348 | should_stop.wait(delay) |
| 349 | if should_stop.is_set(): |
| 350 | return None |
| 351 | |
| 352 | # Disable internal retries in net.url_read, since we are doing retries |
| 353 | # ourselves. Do not use retry_404 so should_stop is polled more often. |
vadimsh@chromium.org | 043b76d | 2013-09-12 16:15:13 +0000 | [diff] [blame] | 354 | response = net.url_read(result_url, retry_404=False, retry_50x=False) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 355 | |
| 356 | # Request failed. Try again. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 357 | if response is None: |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 358 | continue |
| 359 | |
| 360 | # Got some response, ensure it is JSON dict, retry if not. |
| 361 | try: |
| 362 | result = json.loads(response) or {} |
| 363 | if not isinstance(result, dict): |
| 364 | raise ValueError() |
| 365 | except (ValueError, TypeError): |
| 366 | logging.warning( |
| 367 | 'Received corrupted or invalid data for task_key %s, retrying: %r', |
| 368 | task_key, response) |
| 369 | continue |
| 370 | |
| 371 | # Swarming server uses non-empty 'output' value as a flag that task has |
| 372 | # finished. How to wait for tasks that produce no output is a mystery. |
| 373 | if result.get('output'): |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 374 | # Record the result, try to fetch attached output files (if any). |
| 375 | if output_collector: |
| 376 | # TODO(vadimsh): Respect |should_stop| and |deadline| when fetching. |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 377 | output_collector.process_shard_result(shard_index, result) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 378 | return result |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 379 | |
| 380 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 381 | def yield_results( |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 382 | swarm_base_url, task_keys, timeout, max_threads, |
| 383 | print_status_updates, output_collector): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 384 | """Yields swarming task results from the swarming server as (index, result). |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 385 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 386 | Duplicate shards are ignored. Shards are yielded in order of completion. |
| 387 | Timed out shards are NOT yielded at all. Caller can compare number of yielded |
| 388 | shards with len(task_keys) to verify all shards completed. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 389 | |
| 390 | max_threads is optional and is used to limit the number of parallel fetches |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 391 | done. Since in general the number of task_keys is in the range <=10, it's not |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 392 | worth normally to limit the number threads. Mostly used for testing purposes. |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 393 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 394 | output_collector is an optional instance of TaskOutputCollector that will be |
| 395 | used to fetch files produced by a task from isolate server to the local disk. |
| 396 | |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 397 | Yields: |
| 398 | (index, result). In particular, 'result' is defined as the |
| 399 | GetRunnerResults() function in services/swarming/server/test_runner.py. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 400 | """ |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 401 | number_threads = ( |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 402 | min(max_threads, len(task_keys)) if max_threads else len(task_keys)) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 403 | should_stop = threading.Event() |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 404 | results_channel = threading_utils.TaskChannel() |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 405 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 406 | with threading_utils.ThreadPool(number_threads, number_threads, 0) as pool: |
| 407 | try: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 408 | # Adds a task to the thread pool to call 'retrieve_results' and return |
| 409 | # the results together with shard_index that produced them (as a tuple). |
| 410 | def enqueue_retrieve_results(shard_index, task_key): |
| 411 | task_fn = lambda *args: (shard_index, retrieve_results(*args)) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 412 | pool.add_task( |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 413 | 0, results_channel.wrap_task(task_fn), |
| 414 | swarm_base_url, shard_index, task_key, timeout, |
| 415 | should_stop, output_collector) |
| 416 | |
| 417 | # Enqueue 'retrieve_results' calls for each shard key to run in parallel. |
| 418 | for shard_index, task_key in enumerate(task_keys): |
| 419 | enqueue_retrieve_results(shard_index, task_key) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 420 | |
| 421 | # Wait for all of them to finish. |
| 422 | shards_remaining = range(len(task_keys)) |
| 423 | active_task_count = len(task_keys) |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 424 | while active_task_count: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 425 | shard_index, result = None, None |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 426 | try: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 427 | shard_index, result = results_channel.pull( |
| 428 | timeout=STATUS_UPDATE_INTERVAL) |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 429 | except threading_utils.TaskChannel.Timeout: |
| 430 | if print_status_updates: |
| 431 | print( |
| 432 | 'Waiting for results from the following shards: %s' % |
| 433 | ', '.join(map(str, shards_remaining))) |
| 434 | sys.stdout.flush() |
| 435 | continue |
| 436 | except Exception: |
| 437 | logging.exception('Unexpected exception in retrieve_results') |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 438 | |
| 439 | # A call to 'retrieve_results' finished (successfully or not). |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 440 | active_task_count -= 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 441 | if not result: |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 442 | logging.error('Failed to retrieve the results for a swarming key') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 443 | continue |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 444 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 445 | # Yield back results to the caller. |
| 446 | assert shard_index in shards_remaining |
| 447 | shards_remaining.remove(shard_index) |
| 448 | yield shard_index, result |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 449 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 450 | finally: |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 451 | # Done or aborted with Ctrl+C, kill the remaining threads. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 452 | should_stop.set() |
| 453 | |
| 454 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 455 | def setup_run_isolated(manifest, bundle): |
| 456 | """Sets up the manifest to run an isolated task via run_isolated.py. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 457 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 458 | Modifies |bundle| (by adding files) and |manifest| (by adding commands) in |
| 459 | place. |
| 460 | |
| 461 | Args: |
| 462 | manifest: Manifest with swarm task definition. |
| 463 | bundle: ZipPackage with files that would be transfered to swarm bot. |
| 464 | If None, only |manifest| is modified (useful in tests). |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 465 | """ |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 466 | # Add uncompressed zip here. It'll be compressed as part of the package sent |
| 467 | # to Swarming server. |
| 468 | run_test_name = 'run_isolated.zip' |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 469 | if bundle and run_test_name not in bundle.files: |
| 470 | bundle.add_buffer( |
| 471 | run_test_name, |
| 472 | run_isolated.get_as_zip_package().zip_into_buffer(compress=False)) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 473 | |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 474 | cleanup_script_name = 'swarm_cleanup.py' |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 475 | if bundle and cleanup_script_name not in bundle.files: |
| 476 | bundle.add_file( |
| 477 | os.path.join(TOOLS_PATH, cleanup_script_name), cleanup_script_name) |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 478 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 479 | run_cmd = [ |
| 480 | 'python', run_test_name, |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 481 | '--hash', manifest.isolated_hash, |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 482 | '--namespace', manifest.namespace, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 483 | ] |
Marc-Antoine Ruel | 8806e62 | 2014-02-12 14:15:53 -0500 | [diff] [blame] | 484 | if file_path.is_url(manifest.isolate_server): |
| 485 | run_cmd.extend(('--isolate-server', manifest.isolate_server)) |
| 486 | else: |
| 487 | run_cmd.extend(('--indir', manifest.isolate_server)) |
| 488 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 489 | if manifest.verbose or manifest.profile: |
| 490 | # Have it print the profiling section. |
| 491 | run_cmd.append('--verbose') |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 492 | |
| 493 | # Pass all extra args for run_isolated.py, it will pass them to the command. |
| 494 | if manifest.extra_args: |
| 495 | run_cmd.append('--') |
| 496 | run_cmd.extend(manifest.extra_args) |
| 497 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 498 | manifest.add_task('Run Test', run_cmd) |
| 499 | |
| 500 | # Clean up |
| 501 | manifest.add_task('Clean Up', ['python', cleanup_script_name]) |
| 502 | |
| 503 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 504 | def setup_googletest(env, shards, index): |
Marc-Antoine Ruel | cd62973 | 2013-12-20 15:00:42 -0500 | [diff] [blame] | 505 | """Sets googletest specific environment variables.""" |
| 506 | if shards > 1: |
| 507 | env = env.copy() |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 508 | env['GTEST_SHARD_INDEX'] = str(index) |
| 509 | env['GTEST_TOTAL_SHARDS'] = str(shards) |
Marc-Antoine Ruel | cd62973 | 2013-12-20 15:00:42 -0500 | [diff] [blame] | 510 | return env |
| 511 | |
| 512 | |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 513 | def archive(isolate_server, namespace, isolated, algo, verbose): |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 514 | """Archives a .isolated and all the dependencies on the CAC.""" |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 515 | logging.info('archive(%s, %s, %s)', isolate_server, namespace, isolated) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 516 | tempdir = None |
Marc-Antoine Ruel | 8806e62 | 2014-02-12 14:15:53 -0500 | [diff] [blame] | 517 | if file_path.is_url(isolate_server): |
| 518 | command = 'archive' |
| 519 | flag = '--isolate-server' |
| 520 | else: |
| 521 | command = 'hashtable' |
| 522 | flag = '--outdir' |
| 523 | |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 524 | print('Archiving: %s' % isolated) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 525 | try: |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 526 | cmd = [ |
| 527 | sys.executable, |
| 528 | os.path.join(ROOT_DIR, 'isolate.py'), |
Marc-Antoine Ruel | 8806e62 | 2014-02-12 14:15:53 -0500 | [diff] [blame] | 529 | command, |
| 530 | flag, isolate_server, |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 531 | '--namespace', namespace, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 532 | '--isolated', isolated, |
| 533 | ] |
maruel@chromium.org | e9403ab | 2013-09-20 18:03:49 +0000 | [diff] [blame] | 534 | cmd.extend(['--verbose'] * verbose) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 535 | logging.info(' '.join(cmd)) |
| 536 | if subprocess.call(cmd, verbose): |
| 537 | return |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame] | 538 | return isolateserver.hash_file(isolated, algo) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 539 | finally: |
| 540 | if tempdir: |
| 541 | shutil.rmtree(tempdir) |
| 542 | |
| 543 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 544 | def get_shard_task_name(task_name, shards, index): |
| 545 | """Returns a task name to use for a single shard of a task.""" |
| 546 | if shards == 1: |
| 547 | return task_name |
| 548 | return '%s:%s:%s' % (task_name, shards, index) |
| 549 | |
| 550 | |
| 551 | def upload_zip_bundle(isolate_server, bundle): |
| 552 | """Uploads a zip package to isolate storage and returns raw fetch URL. |
| 553 | |
| 554 | Args: |
| 555 | isolate_server: URL of an isolate server. |
| 556 | bundle: instance of ZipPackage to upload. |
| 557 | |
| 558 | Returns: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 559 | URL to get the file from. |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 560 | """ |
| 561 | # Swarming bot would need to be able to grab the file from the storage |
| 562 | # using raw HTTP GET. Use 'default' namespace so that the raw data returned |
| 563 | # to a bot is not zipped, since swarm_bot doesn't understand compressed |
| 564 | # data yet. This namespace have nothing to do with |namespace| passed to |
| 565 | # run_isolated.py that is used to store files for isolated task. |
| 566 | logging.info('Zipping up and uploading files...') |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 567 | start_time = now() |
| 568 | isolate_item = isolateserver.BufferItem( |
| 569 | bundle.zip_into_buffer(), high_priority=True) |
| 570 | with isolateserver.get_storage(isolate_server, 'default') as storage: |
| 571 | uploaded = storage.upload_items([isolate_item]) |
| 572 | bundle_url = storage.get_fetch_url(isolate_item) |
| 573 | elapsed = now() - start_time |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 574 | if isolate_item in uploaded: |
| 575 | logging.info('Upload complete, time elapsed: %f', elapsed) |
| 576 | else: |
| 577 | logging.info('Zip file already on server, time elapsed: %f', elapsed) |
| 578 | return bundle_url |
| 579 | |
| 580 | |
| 581 | def trigger_by_manifest(swarming, manifest): |
| 582 | """Given a task manifest, triggers it for execution on swarming. |
| 583 | |
| 584 | Args: |
| 585 | swarming: URL of a swarming service. |
| 586 | manifest: instance of Manifest. |
| 587 | |
| 588 | Returns: |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 589 | tuple(Task id, priority) on success. tuple(None, None) on failure. |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 590 | """ |
| 591 | logging.info('Triggering: %s', manifest.task_name) |
| 592 | manifest_text = manifest.to_json() |
| 593 | result = net.url_read(swarming + '/test', data={'request': manifest_text}) |
| 594 | if not result: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 595 | on_error.report('Failed to trigger task %s' % manifest.task_name) |
Vadim Shtayura | 1c024f7 | 2014-07-09 19:00:10 -0700 | [diff] [blame^] | 596 | return None, None |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 597 | try: |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 598 | data = json.loads(result) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 599 | except (ValueError, TypeError): |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 600 | msg = '\n'.join(( |
| 601 | 'Failed to trigger task %s' % manifest.task_name, |
| 602 | 'Manifest: %s' % manifest_text, |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 603 | 'Bad response: %s' % result)) |
| 604 | on_error.report(msg) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 605 | return None, None |
| 606 | if not data: |
| 607 | return None, None |
| 608 | return data['test_keys'][0]['test_key'], data['priority'] |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 609 | |
| 610 | |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 611 | def abort_task(_swarming, _manifest): |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 612 | """Given a task manifest that was triggered, aborts its execution.""" |
| 613 | # TODO(vadimsh): No supported by the server yet. |
| 614 | |
| 615 | |
| 616 | def trigger_task_shards( |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 617 | swarming, isolate_server, namespace, isolated_hash, task_name, extra_args, |
Marc-Antoine Ruel | aea5065 | 2014-06-12 14:23:48 -0400 | [diff] [blame] | 618 | shards, dimensions, env, deadline, verbose, profile, priority): |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 619 | """Triggers multiple subtasks of a sharded task. |
| 620 | |
| 621 | Returns: |
Vadim Shtayura | f27448e | 2014-06-26 11:35:05 -0700 | [diff] [blame] | 622 | Dict with task details, returned to caller as part of --dump-json output. |
| 623 | None in case of failure. |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 624 | """ |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 625 | # Collects all files that are necessary to bootstrap a task execution |
| 626 | # on the bot. Usually it includes self contained run_isolated.zip and |
| 627 | # a bunch of small other scripts. All heavy files are pulled |
| 628 | # by run_isolated.zip. Updated in 'setup_run_isolated'. |
| 629 | bundle = zip_package.ZipPackage(ROOT_DIR) |
| 630 | |
| 631 | # Make a separate Manifest for each shard, put shard index and number of |
| 632 | # shards into env and subtask name. |
| 633 | manifests = [] |
| 634 | for index in xrange(shards): |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 635 | manifest = Manifest( |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 636 | isolate_server=isolate_server, |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 637 | namespace=namespace, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 638 | isolated_hash=isolated_hash, |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 639 | task_name=get_shard_task_name(task_name, shards, index), |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 640 | extra_args=extra_args, |
Marc-Antoine Ruel | 92f3242 | 2013-11-06 18:12:13 -0500 | [diff] [blame] | 641 | dimensions=dimensions, |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 642 | env=setup_googletest(env, shards, index), |
Marc-Antoine Ruel | 13b7b78 | 2014-03-14 11:14:57 -0400 | [diff] [blame] | 643 | deadline=deadline, |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 644 | verbose=verbose, |
| 645 | profile=profile, |
Vadim Shtayura | bcff74f | 2014-02-27 16:19:34 -0800 | [diff] [blame] | 646 | priority=priority) |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 647 | setup_run_isolated(manifest, bundle) |
| 648 | manifests.append(manifest) |
| 649 | |
| 650 | # Upload zip bundle file to get its URL. |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 651 | try: |
| 652 | bundle_url = upload_zip_bundle(isolate_server, bundle) |
| 653 | except (IOError, OSError): |
| 654 | on_error.report('Failed to upload the zip file for task %s' % task_name) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 655 | return None, None |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 656 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 657 | # Attach that file to all manifests. |
| 658 | for manifest in manifests: |
| 659 | manifest.add_bundled_file('swarm_data.zip', bundle_url) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 660 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 661 | # Trigger all the subtasks. |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 662 | tasks = {} |
| 663 | priority_warning = False |
Vadim Shtayura | f27448e | 2014-06-26 11:35:05 -0700 | [diff] [blame] | 664 | for index, manifest in enumerate(manifests): |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 665 | task_id, priority = trigger_by_manifest(swarming, manifest) |
| 666 | if not task_id: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 667 | break |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 668 | if not priority_warning and priority != manifest.priority: |
| 669 | priority_warning = True |
| 670 | print >> sys.stderr, 'Priority was reset to %s' % priority |
Vadim Shtayura | f27448e | 2014-06-26 11:35:05 -0700 | [diff] [blame] | 671 | tasks[manifest.task_name] = { |
| 672 | 'shard_index': index, |
| 673 | 'task_id': task_id, |
| 674 | 'view_url': '%s/user/task/%s' % (swarming, task_id), |
| 675 | } |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 676 | |
| 677 | # Some shards weren't triggered. Abort everything. |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 678 | if len(tasks) != len(manifests): |
| 679 | if tasks: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 680 | print >> sys.stderr, 'Not all shards were triggered' |
Vadim Shtayura | f27448e | 2014-06-26 11:35:05 -0700 | [diff] [blame] | 681 | for task_dict in tasks.itervalues(): |
| 682 | abort_task(swarming, task_dict['task_id']) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 683 | return None |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 684 | |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 685 | return tasks |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 686 | |
| 687 | |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 688 | def isolated_to_hash(isolate_server, namespace, arg, algo, verbose): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 689 | """Archives a .isolated file if needed. |
| 690 | |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 691 | Returns the file hash to trigger and a bool specifying if it was a file (True) |
| 692 | or a hash (False). |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 693 | """ |
| 694 | if arg.endswith('.isolated'): |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 695 | file_hash = archive(isolate_server, namespace, arg, algo, verbose) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 696 | if not file_hash: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 697 | on_error.report('Archival failure %s' % arg) |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 698 | return None, True |
| 699 | return file_hash, True |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 700 | elif isolateserver.is_valid_hash(arg, algo): |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 701 | return arg, False |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 702 | else: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 703 | on_error.report('Invalid hash %s' % arg) |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 704 | return None, False |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 705 | |
| 706 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 707 | def trigger( |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 708 | swarming, |
| 709 | isolate_server, |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 710 | namespace, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 711 | file_hash_or_isolated, |
| 712 | task_name, |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 713 | extra_args, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 714 | shards, |
Marc-Antoine Ruel | 92f3242 | 2013-11-06 18:12:13 -0500 | [diff] [blame] | 715 | dimensions, |
| 716 | env, |
Marc-Antoine Ruel | 13b7b78 | 2014-03-14 11:14:57 -0400 | [diff] [blame] | 717 | deadline, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 718 | verbose, |
| 719 | profile, |
| 720 | priority): |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 721 | """Sends off the hash swarming task requests. |
| 722 | |
| 723 | Returns: |
| 724 | tuple(dict(task_name: task_id), base task name). The dict of tasks is None |
| 725 | in case of failure. |
| 726 | """ |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 727 | file_hash, is_file = isolated_to_hash( |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 728 | isolate_server, namespace, file_hash_or_isolated, hashlib.sha1, verbose) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 729 | if not file_hash: |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 730 | return 1, '' |
| 731 | if not task_name: |
| 732 | # If a file name was passed, use its base name of the isolated hash. |
| 733 | # Otherwise, use user name as an approximation of a task name. |
| 734 | if is_file: |
| 735 | key = os.path.splitext(os.path.basename(file_hash_or_isolated))[0] |
| 736 | else: |
| 737 | key = getpass.getuser() |
Vadim Shtayura | c3d97b0 | 2014-04-26 19:16:05 -0700 | [diff] [blame] | 738 | task_name = '%s/%s/%s/%d' % ( |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 739 | key, |
| 740 | '_'.join('%s=%s' % (k, v) for k, v in sorted(dimensions.iteritems())), |
Vadim Shtayura | c3d97b0 | 2014-04-26 19:16:05 -0700 | [diff] [blame] | 741 | file_hash, |
| 742 | now() * 1000) |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 743 | |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 744 | tasks = trigger_task_shards( |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 745 | swarming=swarming, |
| 746 | isolate_server=isolate_server, |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 747 | namespace=namespace, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 748 | isolated_hash=file_hash, |
| 749 | task_name=task_name, |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 750 | extra_args=extra_args, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 751 | shards=shards, |
| 752 | dimensions=dimensions, |
Marc-Antoine Ruel | 13b7b78 | 2014-03-14 11:14:57 -0400 | [diff] [blame] | 753 | deadline=deadline, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 754 | env=env, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 755 | verbose=verbose, |
| 756 | profile=profile, |
Vadim Shtayura | bcff74f | 2014-02-27 16:19:34 -0800 | [diff] [blame] | 757 | priority=priority) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 758 | return tasks, task_name |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 759 | |
| 760 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 761 | def decorate_shard_output(shard_index, result, shard_exit_code): |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 762 | """Returns wrapped output for swarming task shard.""" |
| 763 | tag = 'index %s (machine tag: %s, id: %s)' % ( |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 764 | shard_index, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 765 | result['machine_id'], |
| 766 | result.get('machine_tag', 'unknown')) |
| 767 | return ( |
| 768 | '\n' |
| 769 | '================================================================\n' |
| 770 | 'Begin output from shard %s\n' |
| 771 | '================================================================\n' |
| 772 | '\n' |
| 773 | '%s' |
| 774 | '================================================================\n' |
Vadim Shtayura | 473455a | 2014-05-14 15:22:35 -0700 | [diff] [blame] | 775 | 'End output from shard %s.\nExit code %d (%s).\n' |
| 776 | '================================================================\n') % ( |
| 777 | tag, result['output'] or NO_OUTPUT_FOUND, tag, |
| 778 | shard_exit_code, hex(0xffffffff & shard_exit_code)) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 779 | |
| 780 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 781 | def collect( |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 782 | url, task_name, shards, timeout, decorate, |
| 783 | print_status_updates, task_output_dir): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 784 | """Retrieves results of a Swarming task.""" |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 785 | # Grab task keys for each shard. Order is important, used to figure out |
| 786 | # shard index based on the key. |
| 787 | # TODO(vadimsh): Simplify this once server support is added. |
| 788 | task_keys = [] |
| 789 | for index in xrange(shards): |
| 790 | shard_task_name = get_shard_task_name(task_name, shards, index) |
| 791 | logging.info('Collecting %s', shard_task_name) |
| 792 | shard_task_keys = get_task_keys(url, shard_task_name) |
| 793 | if not shard_task_keys: |
| 794 | raise Failure('No task keys to get results with: %s' % shard_task_name) |
| 795 | if len(shard_task_keys) != 1: |
| 796 | raise Failure('Expecting only one shard for a task: %s' % shard_task_name) |
| 797 | task_keys.append(shard_task_keys[0]) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 798 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 799 | # Collect output files only if explicitly asked with --task-output-dir option. |
| 800 | if task_output_dir: |
| 801 | output_collector = TaskOutputCollector( |
| 802 | task_output_dir, task_name, len(task_keys)) |
| 803 | else: |
| 804 | output_collector = None |
| 805 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 806 | seen_shards = set() |
Vadim Shtayura | c524f51 | 2014-05-15 09:54:56 -0700 | [diff] [blame] | 807 | exit_codes = [] |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 808 | |
| 809 | try: |
| 810 | for index, output in yield_results( |
| 811 | url, task_keys, timeout, None, print_status_updates, output_collector): |
| 812 | seen_shards.add(index) |
Vadim Shtayura | 473455a | 2014-05-14 15:22:35 -0700 | [diff] [blame] | 813 | |
| 814 | # Grab first non-zero exit code as an overall shard exit code. |
| 815 | shard_exit_code = 0 |
| 816 | for code in map(int, (output['exit_codes'] or '1').split(',')): |
| 817 | if code: |
| 818 | shard_exit_code = code |
| 819 | break |
Vadim Shtayura | c524f51 | 2014-05-15 09:54:56 -0700 | [diff] [blame] | 820 | exit_codes.append(shard_exit_code) |
Vadim Shtayura | 473455a | 2014-05-14 15:22:35 -0700 | [diff] [blame] | 821 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 822 | if decorate: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 823 | print decorate_shard_output(index, output, shard_exit_code) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 824 | else: |
| 825 | print( |
| 826 | '%s/%s: %s' % ( |
| 827 | output['machine_id'], |
| 828 | output['machine_tag'], |
| 829 | output['exit_codes'])) |
| 830 | print(''.join(' %s\n' % l for l in output['output'].splitlines())) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 831 | finally: |
| 832 | if output_collector: |
| 833 | output_collector.finalize() |
| 834 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 835 | if len(seen_shards) != len(task_keys): |
| 836 | missing_shards = [x for x in range(len(task_keys)) if x not in seen_shards] |
| 837 | print >> sys.stderr, ('Results from some shards are missing: %s' % |
| 838 | ', '.join(map(str, missing_shards))) |
Vadim Shtayura | c524f51 | 2014-05-15 09:54:56 -0700 | [diff] [blame] | 839 | return 1 |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 840 | |
Vadim Shtayura | c524f51 | 2014-05-15 09:54:56 -0700 | [diff] [blame] | 841 | return int(bool(any(exit_codes))) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 842 | |
| 843 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 844 | def add_filter_options(parser): |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 845 | parser.filter_group = tools.optparse.OptionGroup(parser, 'Filtering slaves') |
| 846 | parser.filter_group.add_option( |
Marc-Antoine Ruel | b39e8cf | 2014-01-20 10:39:31 -0500 | [diff] [blame] | 847 | '-d', '--dimension', default=[], action='append', nargs=2, |
Marc-Antoine Ruel | 92f3242 | 2013-11-06 18:12:13 -0500 | [diff] [blame] | 848 | dest='dimensions', metavar='FOO bar', |
| 849 | help='dimension to filter on') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 850 | parser.add_option_group(parser.filter_group) |
| 851 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 852 | |
Marc-Antoine Ruel | 025e782 | 2014-05-01 11:50:24 -0400 | [diff] [blame] | 853 | def process_filter_options(parser, options): |
| 854 | options.dimensions = dict(options.dimensions) |
| 855 | if not options.dimensions: |
| 856 | parser.error('Please at least specify one --dimension') |
| 857 | |
| 858 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 859 | def add_sharding_options(parser): |
| 860 | parser.sharding_group = tools.optparse.OptionGroup(parser, 'Sharding options') |
| 861 | parser.sharding_group.add_option( |
| 862 | '--shards', type='int', default=1, |
| 863 | help='Number of shards to trigger and collect.') |
| 864 | parser.add_option_group(parser.sharding_group) |
| 865 | |
| 866 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 867 | def add_trigger_options(parser): |
| 868 | """Adds all options to trigger a task on Swarming.""" |
| 869 | isolateserver.add_isolate_server_options(parser, True) |
| 870 | add_filter_options(parser) |
| 871 | |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 872 | parser.task_group = tools.optparse.OptionGroup(parser, 'Task properties') |
| 873 | parser.task_group.add_option( |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 874 | '-e', '--env', default=[], action='append', nargs=2, metavar='FOO bar', |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 875 | help='Environment variables to set') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 876 | parser.task_group.add_option( |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 877 | '--priority', type='int', default=100, |
| 878 | help='The lower value, the more important the task is') |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 879 | parser.task_group.add_option( |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 880 | '-T', '--task-name', |
| 881 | help='Display name of the task. It uniquely identifies the task. ' |
Vadim Shtayura | c3d97b0 | 2014-04-26 19:16:05 -0700 | [diff] [blame] | 882 | 'Defaults to <base_name>/<dimensions>/<isolated hash>/<timestamp> ' |
| 883 | 'if an isolated file is provided, if a hash is provided, it ' |
| 884 | 'defaults to <user>/<dimensions>/<isolated hash>/<timestamp>') |
Marc-Antoine Ruel | 13b7b78 | 2014-03-14 11:14:57 -0400 | [diff] [blame] | 885 | parser.task_group.add_option( |
| 886 | '--deadline', type='int', default=6*60*60, |
| 887 | help='Seconds to allow the task to be pending for a bot to run before ' |
| 888 | 'this task request expires.') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 889 | parser.add_option_group(parser.task_group) |
Marc-Antoine Ruel | cd62973 | 2013-12-20 15:00:42 -0500 | [diff] [blame] | 890 | # TODO(maruel): This is currently written in a chromium-specific way. |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 891 | parser.group_logging.add_option( |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 892 | '--profile', action='store_true', |
| 893 | default=bool(os.environ.get('ISOLATE_DEBUG')), |
| 894 | help='Have run_isolated.py print profiling info') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 895 | |
| 896 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 897 | def process_trigger_options(parser, options, args): |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 898 | isolateserver.process_isolate_server_options(parser, options) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 899 | if len(args) != 1: |
| 900 | parser.error('Must pass one .isolated file or its hash (sha1).') |
Marc-Antoine Ruel | 025e782 | 2014-05-01 11:50:24 -0400 | [diff] [blame] | 901 | process_filter_options(parser, options) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 902 | |
| 903 | |
| 904 | def add_collect_options(parser): |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 905 | parser.server_group.add_option( |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 906 | '-t', '--timeout', |
| 907 | type='float', |
| 908 | default=DEFAULT_SHARD_WAIT_TIME, |
| 909 | help='Timeout to wait for result, set to 0 for no timeout; default: ' |
| 910 | '%default s') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 911 | parser.group_logging.add_option( |
| 912 | '--decorate', action='store_true', help='Decorate output') |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 913 | parser.group_logging.add_option( |
| 914 | '--print-status-updates', action='store_true', |
| 915 | help='Print periodic status updates') |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 916 | parser.task_output_group = tools.optparse.OptionGroup(parser, 'Task output') |
| 917 | parser.task_output_group.add_option( |
| 918 | '--task-output-dir', |
| 919 | help='Directory to put task results into. When the task finishes, this ' |
| 920 | 'directory contains <task-output-dir>/summary.json file with ' |
| 921 | 'a summary of task results across all shards, and per-shard ' |
| 922 | 'directory with output files produced by a shard: ' |
| 923 | '<task-output-dir>/<zero-based-shard-index>/') |
| 924 | parser.add_option_group(parser.task_output_group) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 925 | |
| 926 | |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 927 | def extract_isolated_command_extra_args(args): |
| 928 | try: |
| 929 | index = args.index('--') |
| 930 | except ValueError: |
| 931 | return (args, []) |
| 932 | return (args[:index], args[index+1:]) |
| 933 | |
| 934 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 935 | @subcommand.usage('task_name') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 936 | def CMDcollect(parser, args): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 937 | """Retrieves results of a Swarming task. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 938 | |
| 939 | The result can be in multiple part if the execution was sharded. It can |
| 940 | potentially have retries. |
| 941 | """ |
| 942 | add_collect_options(parser) |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 943 | add_sharding_options(parser) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 944 | (options, args) = parser.parse_args(args) |
| 945 | if not args: |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 946 | parser.error('Must specify one task name.') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 947 | elif len(args) > 1: |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 948 | parser.error('Must specify only one task name.') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 949 | |
| 950 | try: |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 951 | return collect( |
| 952 | options.swarming, |
| 953 | args[0], |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 954 | options.shards, |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 955 | options.timeout, |
| 956 | options.decorate, |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 957 | options.print_status_updates, |
| 958 | options.task_output_dir) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 959 | except Failure: |
| 960 | on_error.report(None) |
vadimsh@chromium.org | d908a54 | 2013-10-30 01:36:17 +0000 | [diff] [blame] | 961 | return 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 962 | |
| 963 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 964 | def CMDquery(parser, args): |
| 965 | """Returns information about the bots connected to the Swarming server.""" |
| 966 | add_filter_options(parser) |
| 967 | parser.filter_group.add_option( |
Marc-Antoine Ruel | 2808311 | 2014-03-13 16:34:04 -0400 | [diff] [blame] | 968 | '--dead-only', action='store_true', |
| 969 | help='Only print dead bots, useful to reap them and reimage broken bots') |
| 970 | parser.filter_group.add_option( |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 971 | '-k', '--keep-dead', action='store_true', |
| 972 | help='Do not filter out dead bots') |
| 973 | parser.filter_group.add_option( |
| 974 | '-b', '--bare', action='store_true', |
Marc-Antoine Ruel | e7b0016 | 2014-03-12 16:59:01 -0400 | [diff] [blame] | 975 | help='Do not print out dimensions') |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 976 | options, args = parser.parse_args(args) |
Marc-Antoine Ruel | 2808311 | 2014-03-13 16:34:04 -0400 | [diff] [blame] | 977 | |
| 978 | if options.keep_dead and options.dead_only: |
| 979 | parser.error('Use only one of --keep-dead and --dead-only') |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 980 | service = net.get_http_service(options.swarming) |
| 981 | data = service.json_request('GET', '/swarming/api/v1/bots') |
| 982 | if data is None: |
| 983 | print >> sys.stderr, 'Failed to access %s' % options.swarming |
| 984 | return 1 |
| 985 | timeout = datetime.timedelta(seconds=data['machine_death_timeout']) |
| 986 | utcnow = datetime.datetime.utcnow() |
Marc-Antoine Ruel | e4bebbc | 2014-06-04 09:36:14 -0400 | [diff] [blame] | 987 | for machine in natsort.natsorted(data['machines'], key=lambda x: x['id']): |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 988 | last_seen = datetime.datetime.strptime( |
| 989 | machine['last_seen'], '%Y-%m-%d %H:%M:%S') |
Marc-Antoine Ruel | 2808311 | 2014-03-13 16:34:04 -0400 | [diff] [blame] | 990 | is_dead = utcnow - last_seen > timeout |
| 991 | if options.dead_only: |
| 992 | if not is_dead: |
| 993 | continue |
| 994 | elif not options.keep_dead and is_dead: |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 995 | continue |
| 996 | |
Marc-Antoine Ruel | e7b0016 | 2014-03-12 16:59:01 -0400 | [diff] [blame] | 997 | # If the user requested to filter on dimensions, ensure the bot has all the |
| 998 | # dimensions requested. |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 999 | dimensions = machine['dimensions'] |
| 1000 | for key, value in options.dimensions: |
| 1001 | if key not in dimensions: |
| 1002 | break |
Marc-Antoine Ruel | e7b0016 | 2014-03-12 16:59:01 -0400 | [diff] [blame] | 1003 | # A bot can have multiple value for a key, for example, |
| 1004 | # {'os': ['Windows', 'Windows-6.1']}, so that --dimension os=Windows will |
| 1005 | # be accepted. |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 1006 | if isinstance(dimensions[key], list): |
| 1007 | if value not in dimensions[key]: |
| 1008 | break |
| 1009 | else: |
| 1010 | if value != dimensions[key]: |
| 1011 | break |
| 1012 | else: |
Marc-Antoine Ruel | e4bebbc | 2014-06-04 09:36:14 -0400 | [diff] [blame] | 1013 | print machine['id'] |
Marc-Antoine Ruel | e7b0016 | 2014-03-12 16:59:01 -0400 | [diff] [blame] | 1014 | if not options.bare: |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 1015 | print ' %s' % dimensions |
| 1016 | return 0 |
| 1017 | |
| 1018 | |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1019 | @subcommand.usage('(hash|isolated) [-- extra_args]') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1020 | def CMDrun(parser, args): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1021 | """Triggers a task and wait for the results. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1022 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1023 | Basically, does everything to run a command remotely. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1024 | """ |
| 1025 | add_trigger_options(parser) |
| 1026 | add_collect_options(parser) |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 1027 | add_sharding_options(parser) |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1028 | args, isolated_cmd_args = extract_isolated_command_extra_args(args) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1029 | options, args = parser.parse_args(args) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1030 | process_trigger_options(parser, options, args) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1031 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1032 | try: |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1033 | tasks, task_name = trigger( |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1034 | swarming=options.swarming, |
Marc-Antoine Ruel | 8806e62 | 2014-02-12 14:15:53 -0500 | [diff] [blame] | 1035 | isolate_server=options.isolate_server or options.indir, |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 1036 | namespace=options.namespace, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1037 | file_hash_or_isolated=args[0], |
| 1038 | task_name=options.task_name, |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1039 | extra_args=isolated_cmd_args, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1040 | shards=options.shards, |
| 1041 | dimensions=options.dimensions, |
| 1042 | env=dict(options.env), |
Marc-Antoine Ruel | 13b7b78 | 2014-03-14 11:14:57 -0400 | [diff] [blame] | 1043 | deadline=options.deadline, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1044 | verbose=options.verbose, |
| 1045 | profile=options.profile, |
| 1046 | priority=options.priority) |
| 1047 | except Failure as e: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1048 | on_error.report( |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1049 | 'Failed to trigger %s(%s): %s' % |
| 1050 | (options.task_name, args[0], e.args[0])) |
| 1051 | return 1 |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1052 | if not tasks: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1053 | on_error.report('Failed to trigger the task.') |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1054 | return 1 |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 1055 | if task_name != options.task_name: |
| 1056 | print('Triggered task: %s' % task_name) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1057 | try: |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1058 | # TODO(maruel): Use task_ids, it's much more efficient! |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1059 | return collect( |
| 1060 | options.swarming, |
Marc-Antoine Ruel | 5b47578 | 2014-02-14 20:57:59 -0500 | [diff] [blame] | 1061 | task_name, |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 1062 | options.shards, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1063 | options.timeout, |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 1064 | options.decorate, |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 1065 | options.print_status_updates, |
| 1066 | options.task_output_dir) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1067 | except Failure: |
| 1068 | on_error.report(None) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1069 | return 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1070 | |
| 1071 | |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1072 | @subcommand.usage("(hash|isolated) [-- extra_args]") |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1073 | def CMDtrigger(parser, args): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1074 | """Triggers a Swarming task. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1075 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1076 | Accepts either the hash (sha1) of a .isolated file already uploaded or the |
| 1077 | path to an .isolated file to archive, packages it if needed and sends a |
| 1078 | Swarming manifest file to the Swarming server. |
| 1079 | |
| 1080 | If an .isolated file is specified instead of an hash, it is first archived. |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1081 | |
| 1082 | Passes all extra arguments provided after '--' as additional command line |
| 1083 | arguments for an isolated command specified in *.isolate file. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1084 | """ |
| 1085 | add_trigger_options(parser) |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 1086 | add_sharding_options(parser) |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1087 | args, isolated_cmd_args = extract_isolated_command_extra_args(args) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1088 | parser.add_option( |
| 1089 | '--dump-json', |
| 1090 | metavar='FILE', |
| 1091 | help='Dump details about the triggered task(s) to this file as json') |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1092 | options, args = parser.parse_args(args) |
| 1093 | process_trigger_options(parser, options, args) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1094 | |
| 1095 | try: |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1096 | tasks, task_name = trigger( |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 1097 | swarming=options.swarming, |
Marc-Antoine Ruel | 8806e62 | 2014-02-12 14:15:53 -0500 | [diff] [blame] | 1098 | isolate_server=options.isolate_server or options.indir, |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 1099 | namespace=options.namespace, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1100 | file_hash_or_isolated=args[0], |
| 1101 | task_name=options.task_name, |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1102 | extra_args=isolated_cmd_args, |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1103 | shards=options.shards, |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1104 | dimensions=options.dimensions, |
Marc-Antoine Ruel | 92f3242 | 2013-11-06 18:12:13 -0500 | [diff] [blame] | 1105 | env=dict(options.env), |
Marc-Antoine Ruel | 13b7b78 | 2014-03-14 11:14:57 -0400 | [diff] [blame] | 1106 | deadline=options.deadline, |
Marc-Antoine Ruel | a704987 | 2013-11-05 19:28:35 -0500 | [diff] [blame] | 1107 | verbose=options.verbose, |
| 1108 | profile=options.profile, |
| 1109 | priority=options.priority) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1110 | if tasks: |
| 1111 | if task_name != options.task_name: |
| 1112 | print('Triggered task: %s' % task_name) |
| 1113 | if options.dump_json: |
| 1114 | data = { |
| 1115 | 'base_task_name': task_name, |
| 1116 | 'tasks': tasks, |
| 1117 | } |
| 1118 | tools.write_json(options.dump_json, data, True) |
| 1119 | return int(not tasks) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1120 | except Failure: |
| 1121 | on_error.report(None) |
vadimsh@chromium.org | d908a54 | 2013-10-30 01:36:17 +0000 | [diff] [blame] | 1122 | return 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1123 | |
| 1124 | |
| 1125 | class OptionParserSwarming(tools.OptionParserWithLogging): |
| 1126 | def __init__(self, **kwargs): |
| 1127 | tools.OptionParserWithLogging.__init__( |
| 1128 | self, prog='swarming.py', **kwargs) |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 1129 | self.server_group = tools.optparse.OptionGroup(self, 'Server') |
| 1130 | self.server_group.add_option( |
maruel@chromium.org | e9403ab | 2013-09-20 18:03:49 +0000 | [diff] [blame] | 1131 | '-S', '--swarming', |
Kevin Graney | 5346c16 | 2014-01-24 12:20:01 -0500 | [diff] [blame] | 1132 | metavar='URL', default=os.environ.get('SWARMING_SERVER', ''), |
maruel@chromium.org | e9403ab | 2013-09-20 18:03:49 +0000 | [diff] [blame] | 1133 | help='Swarming server to use') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 1134 | self.add_option_group(self.server_group) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 1135 | auth.add_auth_options(self) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1136 | |
| 1137 | def parse_args(self, *args, **kwargs): |
| 1138 | options, args = tools.OptionParserWithLogging.parse_args( |
| 1139 | self, *args, **kwargs) |
| 1140 | options.swarming = options.swarming.rstrip('/') |
| 1141 | if not options.swarming: |
| 1142 | self.error('--swarming is required.') |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 1143 | auth.process_auth_options(self, options) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1144 | return options, args |
| 1145 | |
| 1146 | |
| 1147 | def main(args): |
| 1148 | dispatcher = subcommand.CommandDispatcher(__name__) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1149 | return dispatcher.execute(OptionParserSwarming(version=__version__), args) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1150 | |
| 1151 | |
| 1152 | if __name__ == '__main__': |
| 1153 | fix_encoding.fix_encoding() |
| 1154 | tools.disable_buffering() |
| 1155 | colorama.init() |
| 1156 | sys.exit(main(sys.argv[1:])) |