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