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