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