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