maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Client tool to trigger tasks or retrieve results from a Swarming server.""" |
| 7 | |
| 8 | __version__ = '0.1' |
| 9 | |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 10 | import binascii |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 11 | import hashlib |
| 12 | import json |
| 13 | import logging |
| 14 | import os |
| 15 | import re |
| 16 | import shutil |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 17 | import subprocess |
| 18 | import sys |
| 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 | |
| 26 | from utils import net |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 27 | from utils import threading_utils |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 28 | from utils import tools |
| 29 | from utils import zip_package |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 30 | |
| 31 | import run_isolated |
| 32 | |
| 33 | |
| 34 | ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 35 | TOOLS_PATH = os.path.join(ROOT_DIR, 'tools') |
| 36 | |
| 37 | |
| 38 | # Default servers. |
| 39 | # TODO(maruel): Chromium-specific. |
| 40 | ISOLATE_SERVER = 'https://isolateserver-dev.appspot.com/' |
| 41 | SWARM_SERVER = 'https://chromium-swarm-dev.appspot.com' |
| 42 | |
| 43 | |
| 44 | # The default time to wait for a shard to finish running. |
csharp@chromium.org | 2475849 | 2013-08-28 19:10:54 +0000 | [diff] [blame] | 45 | DEFAULT_SHARD_WAIT_TIME = 80 * 60. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | NO_OUTPUT_FOUND = ( |
| 49 | 'No output produced by the test, it may have failed to run.\n' |
| 50 | '\n') |
| 51 | |
| 52 | |
| 53 | PLATFORM_MAPPING = { |
| 54 | 'cygwin': 'Windows', |
| 55 | 'darwin': 'Mac', |
| 56 | 'linux2': 'Linux', |
| 57 | 'win32': 'Windows', |
| 58 | } |
| 59 | |
| 60 | |
| 61 | class Failure(Exception): |
| 62 | """Generic failure.""" |
| 63 | pass |
| 64 | |
| 65 | |
| 66 | class Manifest(object): |
| 67 | """Represents a Swarming task manifest. |
| 68 | |
| 69 | Also includes code to zip code and upload itself. |
| 70 | """ |
| 71 | def __init__( |
| 72 | self, manifest_hash, test_name, shards, test_filter, slave_os, |
| 73 | working_dir, isolate_server, verbose, profile, priority): |
| 74 | """Populates a manifest object. |
| 75 | Args: |
| 76 | manifest_hash - The manifest's sha-1 that the slave is going to fetch. |
| 77 | test_name - The name to give the test request. |
| 78 | shards - The number of swarm shards to request. |
| 79 | test_filter - The gtest filter to apply when running the test. |
| 80 | slave_os - OS to run on. |
| 81 | working_dir - Relative working directory to start the script. |
| 82 | isolate_server - isolate server url. |
| 83 | verbose - if True, have the slave print more details. |
| 84 | profile - if True, have the slave print more timing data. |
| 85 | priority - int between 0 and 1000, lower the higher priority |
| 86 | """ |
| 87 | self.manifest_hash = manifest_hash |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 88 | self.bundle = zip_package.ZipPackage(ROOT_DIR) |
| 89 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 90 | self._test_name = test_name |
| 91 | self._shards = shards |
| 92 | self._test_filter = test_filter |
| 93 | self._target_platform = PLATFORM_MAPPING[slave_os] |
| 94 | self._working_dir = working_dir |
| 95 | |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame^] | 96 | self.isolate_server = isolate_server |
| 97 | self._data_server_retrieval = isolate_server + '/content/retrieve/default/' |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 98 | self._data_server_storage = isolate_server + '/content/store/default/' |
| 99 | self._data_server_has = isolate_server + '/content/contains/default' |
| 100 | self._data_server_get_token = isolate_server + '/content/get_token' |
| 101 | |
| 102 | self.verbose = bool(verbose) |
| 103 | self.profile = bool(profile) |
| 104 | self.priority = priority |
| 105 | |
| 106 | self._zip_file_hash = '' |
| 107 | self._tasks = [] |
| 108 | self._files = {} |
| 109 | self._token_cache = None |
| 110 | |
| 111 | def _token(self): |
| 112 | if not self._token_cache: |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 113 | result = net.url_open(self._data_server_get_token) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 114 | if not result: |
| 115 | # TODO(maruel): Implement authentication. |
| 116 | raise Failure('Failed to get token, need authentication') |
| 117 | # Quote it right away, so creating the urls is simpler. |
| 118 | self._token_cache = urllib.quote(result.read()) |
| 119 | return self._token_cache |
| 120 | |
| 121 | def add_task(self, task_name, actions, time_out=600): |
| 122 | """Appends a new task to the swarm manifest file.""" |
| 123 | # See swarming/src/common/test_request_message.py TestObject constructor for |
| 124 | # the valid flags. |
| 125 | self._tasks.append( |
| 126 | { |
| 127 | 'action': actions, |
| 128 | 'decorate_output': self.verbose, |
| 129 | 'test_name': task_name, |
| 130 | 'time_out': time_out, |
| 131 | }) |
| 132 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 133 | def zip_and_upload(self): |
| 134 | """Zips up all the files necessary to run a shard and uploads to Swarming |
| 135 | master. |
| 136 | """ |
| 137 | assert not self._zip_file_hash |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 138 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 139 | start_time = time.time() |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 140 | zip_contents = self.bundle.zip_into_buffer() |
| 141 | self._zip_file_hash = hashlib.sha1(zip_contents).hexdigest() |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 142 | print 'Zipping completed, time elapsed: %f' % (time.time() - start_time) |
| 143 | |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 144 | response = net.url_open( |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 145 | self._data_server_has + '?token=%s' % self._token(), |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 146 | data=binascii.unhexlify(self._zip_file_hash), |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 147 | content_type='application/octet-stream') |
| 148 | if response is None: |
| 149 | print >> sys.stderr, ( |
| 150 | 'Unable to query server for zip file presence, aborting.') |
| 151 | return False |
| 152 | |
| 153 | if response.read(1) == chr(1): |
| 154 | print 'Zip file already on server, no need to reupload.' |
| 155 | return True |
| 156 | |
| 157 | print 'Zip file not on server, starting uploading.' |
| 158 | |
| 159 | url = '%s%s?priority=0&token=%s' % ( |
| 160 | self._data_server_storage, self._zip_file_hash, self._token()) |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 161 | response = net.url_open( |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 162 | url, data=zip_contents, content_type='application/octet-stream') |
| 163 | if response is None: |
| 164 | print >> sys.stderr, 'Failed to upload the zip file: %s' % url |
| 165 | return False |
| 166 | |
| 167 | return True |
| 168 | |
| 169 | def to_json(self): |
| 170 | """Exports the current configuration into a swarm-readable manifest file. |
| 171 | |
| 172 | This function doesn't mutate the object. |
| 173 | """ |
| 174 | test_case = { |
| 175 | 'test_case_name': self._test_name, |
| 176 | 'data': [ |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame^] | 177 | [self._data_server_retrieval + urllib.quote(self._zip_file_hash), |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 178 | 'swarm_data.zip'], |
| 179 | ], |
| 180 | 'tests': self._tasks, |
| 181 | 'env_vars': {}, |
| 182 | 'configurations': [ |
| 183 | { |
| 184 | 'min_instances': self._shards, |
| 185 | 'config_name': self._target_platform, |
| 186 | 'dimensions': { |
| 187 | 'os': self._target_platform, |
| 188 | }, |
| 189 | }, |
| 190 | ], |
| 191 | 'working_dir': self._working_dir, |
| 192 | 'restart_on_failure': True, |
| 193 | 'cleanup': 'root', |
| 194 | 'priority': self.priority, |
| 195 | } |
| 196 | |
| 197 | # These flags are googletest specific. |
| 198 | if self._test_filter and self._test_filter != '*': |
| 199 | test_case['env_vars']['GTEST_FILTER'] = self._test_filter |
| 200 | if self._shards > 1: |
| 201 | test_case['env_vars']['GTEST_SHARD_INDEX'] = '%(instance_index)s' |
| 202 | test_case['env_vars']['GTEST_TOTAL_SHARDS'] = '%(num_instances)s' |
| 203 | |
| 204 | return json.dumps(test_case, separators=(',',':')) |
| 205 | |
| 206 | |
| 207 | def now(): |
| 208 | """Exists so it can be mocked easily.""" |
| 209 | return time.time() |
| 210 | |
| 211 | |
| 212 | def get_test_keys(swarm_base_url, test_name): |
| 213 | """Returns the Swarm test key for each shards of test_name.""" |
| 214 | key_data = urllib.urlencode([('name', test_name)]) |
| 215 | url = '%s/get_matching_test_cases?%s' % (swarm_base_url, key_data) |
| 216 | |
vadimsh@chromium.org | 043b76d | 2013-09-12 16:15:13 +0000 | [diff] [blame] | 217 | for _ in net.retry_loop(max_attempts=net.URL_OPEN_MAX_ATTEMPTS): |
| 218 | result = net.url_read(url, retry_404=True) |
| 219 | if result is None: |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 220 | raise Failure( |
| 221 | 'Error: Unable to find any tests with the name, %s, on swarm server' |
| 222 | % test_name) |
| 223 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 224 | # TODO(maruel): Compare exact string. |
| 225 | if 'No matching' in result: |
| 226 | logging.warning('Unable to find any tests with the name, %s, on swarm ' |
| 227 | 'server' % test_name) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 228 | continue |
| 229 | return json.loads(result) |
| 230 | |
| 231 | raise Failure( |
| 232 | 'Error: Unable to find any tests with the name, %s, on swarm server' |
| 233 | % test_name) |
| 234 | |
| 235 | |
| 236 | def retrieve_results(base_url, test_key, timeout, should_stop): |
| 237 | """Retrieves results for a single test_key.""" |
| 238 | assert isinstance(timeout, float) |
| 239 | params = [('r', test_key)] |
| 240 | result_url = '%s/get_result?%s' % (base_url, urllib.urlencode(params)) |
| 241 | start = now() |
| 242 | while True: |
| 243 | if timeout and (now() - start) >= timeout: |
| 244 | logging.error('retrieve_results(%s) timed out', base_url) |
| 245 | return {} |
| 246 | # Do retries ourselves. |
vadimsh@chromium.org | 043b76d | 2013-09-12 16:15:13 +0000 | [diff] [blame] | 247 | response = net.url_read(result_url, retry_404=False, retry_50x=False) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 248 | if response is None: |
| 249 | # Aggressively poll for results. Do not use retry_404 so |
| 250 | # should_stop is polled more often. |
| 251 | remaining = min(5, timeout - (now() - start)) if timeout else 5 |
| 252 | if remaining > 0: |
vadimsh@chromium.org | 043b76d | 2013-09-12 16:15:13 +0000 | [diff] [blame] | 253 | net.sleep_before_retry(1, remaining) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 254 | else: |
| 255 | try: |
vadimsh@chromium.org | 043b76d | 2013-09-12 16:15:13 +0000 | [diff] [blame] | 256 | data = json.loads(response) or {} |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 257 | except (ValueError, TypeError): |
| 258 | logging.warning( |
| 259 | 'Received corrupted data for test_key %s. Retrying.', test_key) |
| 260 | else: |
| 261 | if data['output']: |
| 262 | return data |
| 263 | if should_stop.get(): |
| 264 | return {} |
| 265 | |
| 266 | |
| 267 | def yield_results(swarm_base_url, test_keys, timeout, max_threads): |
| 268 | """Yields swarm test results from the swarm server as (index, result). |
| 269 | |
| 270 | Duplicate shards are ignored, the first one to complete is returned. |
| 271 | |
| 272 | max_threads is optional and is used to limit the number of parallel fetches |
| 273 | done. Since in general the number of test_keys is in the range <=10, it's not |
| 274 | worth normally to limit the number threads. Mostly used for testing purposes. |
| 275 | """ |
| 276 | shards_remaining = range(len(test_keys)) |
| 277 | number_threads = ( |
| 278 | min(max_threads, len(test_keys)) if max_threads else len(test_keys)) |
| 279 | should_stop = threading_utils.Bit() |
| 280 | results_remaining = len(test_keys) |
| 281 | with threading_utils.ThreadPool(number_threads, number_threads, 0) as pool: |
| 282 | try: |
| 283 | for test_key in test_keys: |
| 284 | pool.add_task( |
| 285 | 0, retrieve_results, swarm_base_url, test_key, timeout, should_stop) |
| 286 | while shards_remaining and results_remaining: |
| 287 | result = pool.get_one_result() |
| 288 | results_remaining -= 1 |
| 289 | if not result: |
| 290 | # Failed to retrieve one key. |
| 291 | logging.error('Failed to retrieve the results for a swarm key') |
| 292 | continue |
| 293 | shard_index = result['config_instance_index'] |
| 294 | if shard_index in shards_remaining: |
| 295 | shards_remaining.remove(shard_index) |
| 296 | yield shard_index, result |
| 297 | else: |
| 298 | logging.warning('Ignoring duplicate shard index %d', shard_index) |
| 299 | # Pop the last entry, there's no such shard. |
| 300 | shards_remaining.pop() |
| 301 | finally: |
| 302 | # Done, kill the remaining threads. |
| 303 | should_stop.set() |
| 304 | |
| 305 | |
| 306 | def chromium_setup(manifest): |
| 307 | """Sets up the commands to run. |
| 308 | |
| 309 | Highly chromium specific. |
| 310 | """ |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 311 | # Add uncompressed zip here. It'll be compressed as part of the package sent |
| 312 | # to Swarming server. |
| 313 | run_test_name = 'run_isolated.zip' |
| 314 | manifest.bundle.add_buffer(run_test_name, |
| 315 | run_isolated.get_as_zip_package().zip_into_buffer(compress=False)) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 316 | |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 317 | cleanup_script_name = 'swarm_cleanup.py' |
| 318 | manifest.bundle.add_file(os.path.join(TOOLS_PATH, cleanup_script_name), |
| 319 | cleanup_script_name) |
| 320 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 321 | run_cmd = [ |
| 322 | 'python', run_test_name, |
| 323 | '--hash', manifest.manifest_hash, |
maruel@chromium.org | b7e79a2 | 2013-09-13 01:24:56 +0000 | [diff] [blame^] | 324 | '--isolate-server', manifest.isolate_server, |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 325 | ] |
| 326 | if manifest.verbose or manifest.profile: |
| 327 | # Have it print the profiling section. |
| 328 | run_cmd.append('--verbose') |
| 329 | manifest.add_task('Run Test', run_cmd) |
| 330 | |
| 331 | # Clean up |
| 332 | manifest.add_task('Clean Up', ['python', cleanup_script_name]) |
| 333 | |
| 334 | |
| 335 | def archive(isolated, isolate_server, verbose): |
| 336 | """Archives a .isolated and all the dependencies on the CAC.""" |
| 337 | tempdir = None |
| 338 | try: |
| 339 | logging.info('Archiving') |
| 340 | cmd = [ |
| 341 | sys.executable, |
| 342 | os.path.join(ROOT_DIR, 'isolate.py'), |
| 343 | 'hashtable', |
| 344 | '--outdir', isolate_server, |
| 345 | '--isolated', isolated, |
| 346 | ] |
| 347 | if verbose: |
| 348 | cmd.append('--verbose') |
| 349 | logging.info(' '.join(cmd)) |
| 350 | if subprocess.call(cmd, verbose): |
| 351 | return |
| 352 | return hashlib.sha1(open(isolated, 'rb').read()).hexdigest() |
| 353 | finally: |
| 354 | if tempdir: |
| 355 | shutil.rmtree(tempdir) |
| 356 | |
| 357 | |
| 358 | def process_manifest( |
| 359 | file_sha1_or_isolated, test_name, shards, test_filter, slave_os, |
| 360 | working_dir, isolate_server, swarming, verbose, profile, priority): |
| 361 | """Process the manifest file and send off the swarm test request. |
| 362 | |
| 363 | Optionally archives an .isolated file. |
| 364 | """ |
| 365 | if file_sha1_or_isolated.endswith('.isolated'): |
| 366 | file_sha1 = archive(file_sha1_or_isolated, isolate_server, verbose) |
| 367 | if not file_sha1: |
| 368 | print >> sys.stderr, 'Archival failure %s' % file_sha1_or_isolated |
| 369 | return 1 |
| 370 | elif re.match(r'^[a-f0-9]{40}$', file_sha1_or_isolated): |
| 371 | file_sha1 = file_sha1_or_isolated |
| 372 | else: |
| 373 | print >> sys.stderr, 'Invalid hash %s' % file_sha1_or_isolated |
| 374 | return 1 |
| 375 | |
| 376 | try: |
| 377 | manifest = Manifest( |
| 378 | file_sha1, test_name, shards, test_filter, slave_os, |
| 379 | working_dir, isolate_server, verbose, profile, priority) |
| 380 | except ValueError as e: |
| 381 | print >> sys.stderr, 'Unable to process %s: %s' % (test_name, e) |
| 382 | return 1 |
| 383 | |
| 384 | chromium_setup(manifest) |
| 385 | |
| 386 | # Zip up relevant files. |
| 387 | print('Zipping up files...') |
| 388 | if not manifest.zip_and_upload(): |
| 389 | return 1 |
| 390 | |
| 391 | # Send test requests off to swarm. |
| 392 | print('Sending test requests to swarm.') |
| 393 | print('Server: %s' % swarming) |
| 394 | print('Job name: %s' % test_name) |
| 395 | test_url = swarming + '/test' |
| 396 | manifest_text = manifest.to_json() |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 397 | result = net.url_open(test_url, data={'request': manifest_text}) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 398 | if not result: |
| 399 | print >> sys.stderr, 'Failed to send test for %s\n%s' % ( |
| 400 | test_name, test_url) |
| 401 | return 1 |
| 402 | try: |
| 403 | json.load(result) |
| 404 | except (ValueError, TypeError) as e: |
| 405 | print >> sys.stderr, 'Failed to send test for %s' % test_name |
| 406 | print >> sys.stderr, 'Manifest: %s' % manifest_text |
| 407 | print >> sys.stderr, str(e) |
| 408 | return 1 |
| 409 | return 0 |
| 410 | |
| 411 | |
| 412 | def trigger( |
| 413 | slave_os, |
| 414 | tasks, |
| 415 | task_prefix, |
| 416 | working_dir, |
| 417 | isolate_server, |
| 418 | swarming, |
| 419 | verbose, |
| 420 | profile, |
| 421 | priority): |
| 422 | """Sends off the hash swarming test requests.""" |
| 423 | highest_exit_code = 0 |
| 424 | for (file_sha1, test_name, shards, testfilter) in tasks: |
| 425 | # TODO(maruel): It should first create a request manifest object, then pass |
| 426 | # it to a function to zip, archive and trigger. |
| 427 | exit_code = process_manifest( |
| 428 | file_sha1, |
| 429 | task_prefix + test_name, |
| 430 | int(shards), |
| 431 | testfilter, |
| 432 | slave_os, |
| 433 | working_dir, |
| 434 | isolate_server, |
| 435 | swarming, |
| 436 | verbose, |
| 437 | profile, |
| 438 | priority) |
| 439 | highest_exit_code = max(highest_exit_code, exit_code) |
| 440 | return highest_exit_code |
| 441 | |
| 442 | |
| 443 | def decorate_shard_output(result, shard_exit_code): |
| 444 | """Returns wrapped output for swarming task shard.""" |
| 445 | tag = 'index %s (machine tag: %s, id: %s)' % ( |
| 446 | result['config_instance_index'], |
| 447 | result['machine_id'], |
| 448 | result.get('machine_tag', 'unknown')) |
| 449 | return ( |
| 450 | '\n' |
| 451 | '================================================================\n' |
| 452 | 'Begin output from shard %s\n' |
| 453 | '================================================================\n' |
| 454 | '\n' |
| 455 | '%s' |
| 456 | '================================================================\n' |
| 457 | 'End output from shard %s. Return %d\n' |
| 458 | '================================================================\n' |
| 459 | ) % (tag, result['output'] or NO_OUTPUT_FOUND, tag, shard_exit_code) |
| 460 | |
| 461 | |
| 462 | def collect(url, test_name, timeout, decorate): |
| 463 | """Retrieves results of a Swarming job.""" |
| 464 | test_keys = get_test_keys(url, test_name) |
| 465 | if not test_keys: |
| 466 | raise Failure('No test keys to get results with.') |
| 467 | |
maruel@chromium.org | 9c1c7b5 | 2013-08-28 19:04:36 +0000 | [diff] [blame] | 468 | exit_code = None |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 469 | for _index, output in yield_results(url, test_keys, timeout, None): |
| 470 | shard_exit_codes = (output['exit_codes'] or '1').split(',') |
| 471 | shard_exit_code = max(int(i) for i in shard_exit_codes) |
| 472 | if decorate: |
| 473 | print decorate_shard_output(output, shard_exit_code) |
| 474 | else: |
| 475 | print( |
| 476 | '%s/%s: %s' % ( |
| 477 | output['machine_id'], |
| 478 | output['machine_tag'], |
| 479 | output['exit_codes'])) |
| 480 | print(''.join(' %s\n' % l for l in output['output'].splitlines())) |
maruel@chromium.org | 9c1c7b5 | 2013-08-28 19:04:36 +0000 | [diff] [blame] | 481 | exit_code = exit_code or shard_exit_code |
| 482 | return exit_code if exit_code is not None else 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 483 | |
| 484 | |
| 485 | def add_trigger_options(parser): |
| 486 | """Adds all options to trigger a task on Swarming.""" |
| 487 | parser.add_option( |
| 488 | '-I', '--isolate-server', |
| 489 | default=ISOLATE_SERVER, |
| 490 | metavar='URL', |
| 491 | help='Isolate server where data is stored. default: %default') |
| 492 | parser.add_option( |
| 493 | '-w', '--working_dir', default='swarm_tests', |
| 494 | help='Working directory on the swarm slave side. default: %default.') |
| 495 | parser.add_option( |
| 496 | '-o', '--os', default=sys.platform, |
| 497 | help='Swarm OS image to request. Should be one of the valid sys.platform ' |
| 498 | 'values like darwin, linux2 or win32 default: %default.') |
| 499 | parser.add_option( |
| 500 | '-T', '--task-prefix', default='', |
| 501 | help='Prefix to give the swarm test request. default: %default') |
| 502 | parser.add_option( |
| 503 | '--profile', action='store_true', |
| 504 | default=bool(os.environ.get('ISOLATE_DEBUG')), |
| 505 | help='Have run_isolated.py print profiling info') |
| 506 | parser.add_option( |
| 507 | '--priority', type='int', default=100, |
| 508 | help='The lower value, the more important the task is') |
| 509 | |
| 510 | |
| 511 | def process_trigger_options(parser, options): |
| 512 | options.isolate_server = options.isolate_server.rstrip('/') |
| 513 | if not options.isolate_server: |
| 514 | parser.error('--isolate-server is required.') |
| 515 | if options.os in ('', 'None'): |
| 516 | # Use the current OS. |
| 517 | options.os = sys.platform |
| 518 | if not options.os in PLATFORM_MAPPING: |
| 519 | parser.error('Invalid --os option.') |
| 520 | |
| 521 | |
| 522 | def add_collect_options(parser): |
| 523 | parser.add_option( |
| 524 | '-t', '--timeout', |
| 525 | type='float', |
| 526 | default=DEFAULT_SHARD_WAIT_TIME, |
| 527 | help='Timeout to wait for result, set to 0 for no timeout; default: ' |
| 528 | '%default s') |
| 529 | parser.add_option('--decorate', action='store_true', help='Decorate output') |
| 530 | |
| 531 | |
| 532 | @subcommand.usage('test_name') |
| 533 | def CMDcollect(parser, args): |
| 534 | """Retrieves results of a Swarming job. |
| 535 | |
| 536 | The result can be in multiple part if the execution was sharded. It can |
| 537 | potentially have retries. |
| 538 | """ |
| 539 | add_collect_options(parser) |
| 540 | (options, args) = parser.parse_args(args) |
| 541 | if not args: |
| 542 | parser.error('Must specify one test name.') |
| 543 | elif len(args) > 1: |
| 544 | parser.error('Must specify only one test name.') |
| 545 | |
| 546 | try: |
| 547 | return collect(options.swarming, args[0], options.timeout, options.decorate) |
| 548 | except Failure as e: |
| 549 | parser.error(e.args[0]) |
| 550 | |
| 551 | |
| 552 | @subcommand.usage('[sha1|isolated ...]') |
| 553 | def CMDrun(parser, args): |
| 554 | """Triggers a job and wait for the results. |
| 555 | |
| 556 | Basically, does everything to run command(s) remotely. |
| 557 | """ |
| 558 | add_trigger_options(parser) |
| 559 | add_collect_options(parser) |
| 560 | options, args = parser.parse_args(args) |
| 561 | |
| 562 | if not args: |
| 563 | parser.error('Must pass at least one .isolated file or its sha1.') |
| 564 | process_trigger_options(parser, options) |
| 565 | |
| 566 | success = [] |
| 567 | for arg in args: |
| 568 | logging.info('Triggering %s', arg) |
| 569 | try: |
| 570 | result = trigger( |
| 571 | options.os, |
| 572 | [(arg, os.path.basename(arg), '1', '')], |
| 573 | options.task_prefix, |
| 574 | options.working_dir, |
| 575 | options.isolate_server, |
| 576 | options.swarming, |
| 577 | options.verbose, |
| 578 | options.profile, |
| 579 | options.priority) |
| 580 | except Failure as e: |
| 581 | result = e.args[0] |
| 582 | if result: |
| 583 | print >> sys.stderr, 'Failed to trigger %s: %s' % (arg, result) |
| 584 | else: |
| 585 | success.append(os.path.basename(arg)) |
| 586 | |
| 587 | if not success: |
| 588 | print >> sys.stderr, 'Failed to trigger any job.' |
| 589 | return result |
| 590 | |
| 591 | code = 0 |
| 592 | for arg in success: |
| 593 | logging.info('Collecting %s', arg) |
| 594 | try: |
| 595 | new_code = collect( |
| 596 | options.swarming, |
| 597 | options.task_prefix + arg, |
| 598 | options.timeout, |
| 599 | options.decorate) |
| 600 | code = max(code, new_code) |
| 601 | except Failure as e: |
| 602 | code = max(code, 1) |
| 603 | print >> sys.stderr, e.args[0] |
| 604 | return code |
| 605 | |
| 606 | |
| 607 | def CMDtrigger(parser, args): |
| 608 | """Triggers Swarm request(s). |
| 609 | |
| 610 | Accepts one or multiple --task requests, with either the sha1 of a .isolated |
| 611 | file already uploaded or the path to an .isolated file to archive, packages it |
| 612 | if needed and sends a Swarm manifest file to the Swarm server. |
| 613 | """ |
| 614 | add_trigger_options(parser) |
| 615 | parser.add_option( |
| 616 | '--task', nargs=4, action='append', default=[], dest='tasks', |
| 617 | help='Task to trigger. The format is ' |
| 618 | '(hash|isolated, test_name, shards, test_filter). This may be ' |
| 619 | 'used multiple times to send multiple hashes jobs. If an isolated ' |
| 620 | 'file is specified instead of an hash, it is first archived.') |
| 621 | (options, args) = parser.parse_args(args) |
| 622 | |
| 623 | if args: |
| 624 | parser.error('Unknown args: %s' % args) |
| 625 | process_trigger_options(parser, options) |
| 626 | if not options.tasks: |
| 627 | parser.error('At least one --task is required.') |
| 628 | |
| 629 | try: |
| 630 | return trigger( |
| 631 | options.os, |
| 632 | options.tasks, |
| 633 | options.task_prefix, |
| 634 | options.working_dir, |
| 635 | options.isolate_server, |
| 636 | options.swarming, |
| 637 | options.verbose, |
| 638 | options.profile, |
| 639 | options.priority) |
| 640 | except Failure as e: |
| 641 | parser.error(e.args[0]) |
| 642 | |
| 643 | |
| 644 | class OptionParserSwarming(tools.OptionParserWithLogging): |
| 645 | def __init__(self, **kwargs): |
| 646 | tools.OptionParserWithLogging.__init__( |
| 647 | self, prog='swarming.py', **kwargs) |
| 648 | self.add_option( |
| 649 | '-S', '--swarming', default=SWARM_SERVER, |
| 650 | help='Specify the url of the Swarming server, default: %default') |
| 651 | |
| 652 | def parse_args(self, *args, **kwargs): |
| 653 | options, args = tools.OptionParserWithLogging.parse_args( |
| 654 | self, *args, **kwargs) |
| 655 | options.swarming = options.swarming.rstrip('/') |
| 656 | if not options.swarming: |
| 657 | self.error('--swarming is required.') |
| 658 | return options, args |
| 659 | |
| 660 | |
| 661 | def main(args): |
| 662 | dispatcher = subcommand.CommandDispatcher(__name__) |
| 663 | try: |
| 664 | return dispatcher.execute(OptionParserSwarming(version=__version__), args) |
| 665 | except ( |
| 666 | Failure, |
| 667 | run_isolated.MappingError, |
| 668 | run_isolated.ConfigError) as e: |
| 669 | sys.stderr.write('\nError: ') |
| 670 | sys.stderr.write(str(e)) |
| 671 | sys.stderr.write('\n') |
| 672 | return 1 |
| 673 | |
| 674 | |
| 675 | if __name__ == '__main__': |
| 676 | fix_encoding.fix_encoding() |
| 677 | tools.disable_buffering() |
| 678 | colorama.init() |
| 679 | sys.exit(main(sys.argv[1:])) |