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