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