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