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