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