blob: 4ec0303b80cf2d180b9afc07d11645105075d3c9 [file] [log] [blame]
maruel@chromium.org0437a732013-08-27 16:05:52 +00001#!/usr/bin/env python
Marc-Antoine Ruel8add1242013-11-05 17:28:27 -05002# Copyright 2013 The Swarming Authors. All rights reserved.
Marc-Antoine Ruele98b1122013-11-05 20:27:57 -05003# 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.org0437a732013-08-27 16:05:52 +00005
6"""Client tool to trigger tasks or retrieve results from a Swarming server."""
7
8__version__ = '0.1'
9
10import hashlib
11import json
12import logging
13import os
maruel@chromium.org0437a732013-08-27 16:05:52 +000014import shutil
maruel@chromium.org0437a732013-08-27 16:05:52 +000015import subprocess
16import sys
17import time
18import urllib
maruel@chromium.org0437a732013-08-27 16:05:52 +000019
20from third_party import colorama
21from third_party.depot_tools import fix_encoding
22from third_party.depot_tools import subcommand
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000023
24from utils import net
maruel@chromium.org0437a732013-08-27 16:05:52 +000025from utils import threading_utils
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000026from utils import tools
27from utils import zip_package
maruel@chromium.org0437a732013-08-27 16:05:52 +000028
maruel@chromium.org7b844a62013-09-17 13:04:59 +000029import isolateserver
maruel@chromium.org0437a732013-08-27 16:05:52 +000030import run_isolated
31
32
33ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
34TOOLS_PATH = os.path.join(ROOT_DIR, 'tools')
35
36
maruel@chromium.org0437a732013-08-27 16:05:52 +000037# The default time to wait for a shard to finish running.
csharp@chromium.org24758492013-08-28 19:10:54 +000038DEFAULT_SHARD_WAIT_TIME = 80 * 60.
maruel@chromium.org0437a732013-08-27 16:05:52 +000039
40
41NO_OUTPUT_FOUND = (
42 'No output produced by the test, it may have failed to run.\n'
43 '\n')
44
45
maruel@chromium.orge9403ab2013-09-20 18:03:49 +000046# TODO(maruel): cygwin != Windows. If a swarm_bot is running in cygwin, it's
47# different from running in native python.
48PLATFORM_MAPPING_SWARMING = {
maruel@chromium.org0437a732013-08-27 16:05:52 +000049 'cygwin': 'Windows',
50 'darwin': 'Mac',
51 'linux2': 'Linux',
52 'win32': 'Windows',
53}
54
maruel@chromium.orge9403ab2013-09-20 18:03:49 +000055PLATFORM_MAPPING_ISOLATE = {
56 'linux2': 'linux',
57 'darwin': 'mac',
58 'win32': 'win',
59}
60
maruel@chromium.org0437a732013-08-27 16:05:52 +000061
62class Failure(Exception):
63 """Generic failure."""
64 pass
65
66
67class Manifest(object):
68 """Represents a Swarming task manifest.
69
70 Also includes code to zip code and upload itself.
71 """
72 def __init__(
Marc-Antoine Ruel05dab5e2013-11-06 15:06:47 -050073 self, isolate_server, isolated_hash, test_name, shards, env,
Marc-Antoine Ruela7049872013-11-05 19:28:35 -050074 slave_os, working_dir, verbose, profile, priority, algo):
maruel@chromium.org0437a732013-08-27 16:05:52 +000075 """Populates a manifest object.
76 Args:
Marc-Antoine Ruela7049872013-11-05 19:28:35 -050077 isolate_server - isolate server url.
maruel@chromium.org814d23f2013-10-01 19:08:00 +000078 isolated_hash - The manifest's sha-1 that the slave is going to fetch.
maruel@chromium.org0437a732013-08-27 16:05:52 +000079 test_name - The name to give the test request.
80 shards - The number of swarm shards to request.
Marc-Antoine Ruel05dab5e2013-11-06 15:06:47 -050081 env - environment variables to set.
maruel@chromium.org0437a732013-08-27 16:05:52 +000082 slave_os - OS to run on.
83 working_dir - Relative working directory to start the script.
maruel@chromium.org0437a732013-08-27 16:05:52 +000084 verbose - if True, have the slave print more details.
85 profile - if True, have the slave print more timing data.
maruel@chromium.org7b844a62013-09-17 13:04:59 +000086 priority - int between 0 and 1000, lower the higher priority.
87 algo - hashing algorithm used.
maruel@chromium.org0437a732013-08-27 16:05:52 +000088 """
Marc-Antoine Ruela7049872013-11-05 19:28:35 -050089 self.isolate_server = isolate_server
90 self.storage = isolateserver.get_storage(isolate_server, 'default')
91
maruel@chromium.org814d23f2013-10-01 19:08:00 +000092 self.isolated_hash = isolated_hash
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000093 self.bundle = zip_package.ZipPackage(ROOT_DIR)
94
maruel@chromium.org0437a732013-08-27 16:05:52 +000095 self._test_name = test_name
96 self._shards = shards
Marc-Antoine Ruel05dab5e2013-11-06 15:06:47 -050097 self._env = env
maruel@chromium.org814d23f2013-10-01 19:08:00 +000098 self._target_platform = slave_os
maruel@chromium.org0437a732013-08-27 16:05:52 +000099 self._working_dir = working_dir
100
maruel@chromium.org0437a732013-08-27 16:05:52 +0000101 self.verbose = bool(verbose)
102 self.profile = bool(profile)
103 self.priority = priority
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000104 self._algo = algo
maruel@chromium.org0437a732013-08-27 16:05:52 +0000105
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000106 self._isolate_item = None
maruel@chromium.org0437a732013-08-27 16:05:52 +0000107 self._tasks = []
maruel@chromium.org0437a732013-08-27 16:05:52 +0000108
109 def add_task(self, task_name, actions, time_out=600):
110 """Appends a new task to the swarm manifest file."""
111 # See swarming/src/common/test_request_message.py TestObject constructor for
112 # the valid flags.
113 self._tasks.append(
114 {
115 'action': actions,
116 'decorate_output': self.verbose,
117 'test_name': task_name,
118 'time_out': time_out,
119 })
120
maruel@chromium.org0437a732013-08-27 16:05:52 +0000121 def zip_and_upload(self):
122 """Zips up all the files necessary to run a shard and uploads to Swarming
123 master.
124 """
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000125 assert not self._isolate_item
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000126
maruel@chromium.org0437a732013-08-27 16:05:52 +0000127 start_time = time.time()
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000128 self._isolate_item = isolateserver.BufferItem(
129 self.bundle.zip_into_buffer(), self._algo, is_isolated=True)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000130 print 'Zipping completed, time elapsed: %f' % (time.time() - start_time)
131
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000132 try:
133 start_time = time.time()
134 uploaded = self.storage.upload_items([self._isolate_item])
135 elapsed = time.time() - start_time
136 except (IOError, OSError) as exc:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000137 tools.report_error('Failed to upload the zip file: %s' % exc)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000138 return False
139
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000140 if self._isolate_item in uploaded:
141 print 'Upload complete, time elapsed: %f' % elapsed
142 else:
143 print 'Zip file already on server, time elapsed: %f' % elapsed
maruel@chromium.org0437a732013-08-27 16:05:52 +0000144
145 return True
146
147 def to_json(self):
148 """Exports the current configuration into a swarm-readable manifest file.
149
150 This function doesn't mutate the object.
151 """
152 test_case = {
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500153 'cleanup': 'root',
maruel@chromium.org0437a732013-08-27 16:05:52 +0000154 'configurations': [
155 {
maruel@chromium.org0437a732013-08-27 16:05:52 +0000156 'config_name': self._target_platform,
157 'dimensions': {
158 'os': self._target_platform,
159 },
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500160 'min_instances': self._shards,
161 'priority': self.priority,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000162 },
163 ],
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500164 'data': [],
165 # TODO: Let the encoding get set from the command line.
166 'encoding': 'UTF-8',
Marc-Antoine Ruel05dab5e2013-11-06 15:06:47 -0500167 'env_vars': self._env,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000168 'restart_on_failure': True,
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500169 'test_case_name': self._test_name,
170 'tests': self._tasks,
171 'working_dir': self._working_dir,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000172 }
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000173 if self._isolate_item:
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000174 test_case['data'].append(
175 [
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000176 self.storage.get_fetch_url(self._isolate_item.digest),
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000177 'swarm_data.zip',
178 ])
maruel@chromium.org0437a732013-08-27 16:05:52 +0000179 return json.dumps(test_case, separators=(',',':'))
180
181
182def now():
183 """Exists so it can be mocked easily."""
184 return time.time()
185
186
187def get_test_keys(swarm_base_url, test_name):
188 """Returns the Swarm test key for each shards of test_name."""
189 key_data = urllib.urlencode([('name', test_name)])
190 url = '%s/get_matching_test_cases?%s' % (swarm_base_url, key_data)
191
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000192 for _ in net.retry_loop(max_attempts=net.URL_OPEN_MAX_ATTEMPTS):
193 result = net.url_read(url, retry_404=True)
194 if result is None:
maruel@chromium.org0437a732013-08-27 16:05:52 +0000195 raise Failure(
196 'Error: Unable to find any tests with the name, %s, on swarm server'
197 % test_name)
198
maruel@chromium.org0437a732013-08-27 16:05:52 +0000199 # TODO(maruel): Compare exact string.
200 if 'No matching' in result:
201 logging.warning('Unable to find any tests with the name, %s, on swarm '
202 'server' % test_name)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000203 continue
204 return json.loads(result)
205
206 raise Failure(
207 'Error: Unable to find any tests with the name, %s, on swarm server'
208 % test_name)
209
210
211def retrieve_results(base_url, test_key, timeout, should_stop):
212 """Retrieves results for a single test_key."""
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000213 assert isinstance(timeout, float), timeout
maruel@chromium.org0437a732013-08-27 16:05:52 +0000214 params = [('r', test_key)]
215 result_url = '%s/get_result?%s' % (base_url, urllib.urlencode(params))
216 start = now()
217 while True:
218 if timeout and (now() - start) >= timeout:
219 logging.error('retrieve_results(%s) timed out', base_url)
220 return {}
221 # Do retries ourselves.
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000222 response = net.url_read(result_url, retry_404=False, retry_50x=False)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000223 if response is None:
224 # Aggressively poll for results. Do not use retry_404 so
225 # should_stop is polled more often.
226 remaining = min(5, timeout - (now() - start)) if timeout else 5
227 if remaining > 0:
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000228 if should_stop.get():
229 return {}
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000230 net.sleep_before_retry(1, remaining)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000231 else:
232 try:
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000233 data = json.loads(response) or {}
maruel@chromium.org0437a732013-08-27 16:05:52 +0000234 except (ValueError, TypeError):
235 logging.warning(
236 'Received corrupted data for test_key %s. Retrying.', test_key)
237 else:
238 if data['output']:
239 return data
240 if should_stop.get():
241 return {}
242
243
244def yield_results(swarm_base_url, test_keys, timeout, max_threads):
245 """Yields swarm test results from the swarm server as (index, result).
246
247 Duplicate shards are ignored, the first one to complete is returned.
248
249 max_threads is optional and is used to limit the number of parallel fetches
250 done. Since in general the number of test_keys is in the range <=10, it's not
251 worth normally to limit the number threads. Mostly used for testing purposes.
252 """
253 shards_remaining = range(len(test_keys))
254 number_threads = (
255 min(max_threads, len(test_keys)) if max_threads else len(test_keys))
256 should_stop = threading_utils.Bit()
257 results_remaining = len(test_keys)
258 with threading_utils.ThreadPool(number_threads, number_threads, 0) as pool:
259 try:
260 for test_key in test_keys:
261 pool.add_task(
262 0, retrieve_results, swarm_base_url, test_key, timeout, should_stop)
263 while shards_remaining and results_remaining:
264 result = pool.get_one_result()
265 results_remaining -= 1
266 if not result:
267 # Failed to retrieve one key.
268 logging.error('Failed to retrieve the results for a swarm key')
269 continue
270 shard_index = result['config_instance_index']
271 if shard_index in shards_remaining:
272 shards_remaining.remove(shard_index)
273 yield shard_index, result
274 else:
275 logging.warning('Ignoring duplicate shard index %d', shard_index)
276 # Pop the last entry, there's no such shard.
277 shards_remaining.pop()
278 finally:
279 # Done, kill the remaining threads.
280 should_stop.set()
281
282
283def chromium_setup(manifest):
284 """Sets up the commands to run.
285
286 Highly chromium specific.
287 """
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000288 # Add uncompressed zip here. It'll be compressed as part of the package sent
289 # to Swarming server.
290 run_test_name = 'run_isolated.zip'
291 manifest.bundle.add_buffer(run_test_name,
292 run_isolated.get_as_zip_package().zip_into_buffer(compress=False))
maruel@chromium.org0437a732013-08-27 16:05:52 +0000293
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000294 cleanup_script_name = 'swarm_cleanup.py'
295 manifest.bundle.add_file(os.path.join(TOOLS_PATH, cleanup_script_name),
296 cleanup_script_name)
297
maruel@chromium.org0437a732013-08-27 16:05:52 +0000298 run_cmd = [
299 'python', run_test_name,
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000300 '--hash', manifest.isolated_hash,
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000301 '--isolate-server', manifest.isolate_server,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000302 ]
303 if manifest.verbose or manifest.profile:
304 # Have it print the profiling section.
305 run_cmd.append('--verbose')
306 manifest.add_task('Run Test', run_cmd)
307
308 # Clean up
309 manifest.add_task('Clean Up', ['python', cleanup_script_name])
310
311
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000312def archive(isolated, isolate_server, os_slave, algo, verbose):
maruel@chromium.org0437a732013-08-27 16:05:52 +0000313 """Archives a .isolated and all the dependencies on the CAC."""
314 tempdir = None
315 try:
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000316 logging.info('archive(%s, %s)', isolated, isolate_server)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000317 cmd = [
318 sys.executable,
319 os.path.join(ROOT_DIR, 'isolate.py'),
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000320 'archive',
maruel@chromium.org0437a732013-08-27 16:05:52 +0000321 '--outdir', isolate_server,
322 '--isolated', isolated,
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000323 '-V', 'OS', PLATFORM_MAPPING_ISOLATE[os_slave],
maruel@chromium.org0437a732013-08-27 16:05:52 +0000324 ]
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000325 cmd.extend(['--verbose'] * verbose)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000326 logging.info(' '.join(cmd))
327 if subprocess.call(cmd, verbose):
328 return
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000329 return isolateserver.hash_file(isolated, algo)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000330 finally:
331 if tempdir:
332 shutil.rmtree(tempdir)
333
334
335def process_manifest(
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500336 swarming, isolate_server, file_hash_or_isolated, test_name, shards,
337 test_filter, slave_os, working_dir, verbose, profile, priority, algo):
maruel@chromium.org0437a732013-08-27 16:05:52 +0000338 """Process the manifest file and send off the swarm test request.
339
340 Optionally archives an .isolated file.
341 """
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000342 if file_hash_or_isolated.endswith('.isolated'):
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000343 file_hash = archive(
344 file_hash_or_isolated, isolate_server, slave_os, algo, verbose)
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000345 if not file_hash:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000346 tools.report_error('Archival failure %s' % file_hash_or_isolated)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000347 return 1
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000348 elif isolateserver.is_valid_hash(file_hash_or_isolated, algo):
349 file_hash = file_hash_or_isolated
maruel@chromium.org0437a732013-08-27 16:05:52 +0000350 else:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000351 tools.report_error('Invalid hash %s' % file_hash_or_isolated)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000352 return 1
353
Marc-Antoine Ruel05dab5e2013-11-06 15:06:47 -0500354 env = {}
355 # These flags are googletest specific.
356 if test_filter and test_filter != '*':
357 env['GTEST_FILTER'] = test_filter
358 if shards > 1:
359 env['GTEST_SHARD_INDEX'] = '%(instance_index)s'
360 env['GTEST_TOTAL_SHARDS'] = '%(num_instances)s'
361
maruel@chromium.org0437a732013-08-27 16:05:52 +0000362 try:
363 manifest = Manifest(
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500364 isolate_server=isolate_server,
365 isolated_hash=file_hash,
366 test_name=test_name,
367 shards=shards,
Marc-Antoine Ruel05dab5e2013-11-06 15:06:47 -0500368 env=env,
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500369 slave_os=PLATFORM_MAPPING_SWARMING[slave_os],
370 working_dir=working_dir,
371 verbose=verbose,
372 profile=profile,
373 priority=priority,
374 algo=algo)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000375 except ValueError as e:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000376 tools.report_error('Unable to process %s: %s' % (test_name, e))
maruel@chromium.org0437a732013-08-27 16:05:52 +0000377 return 1
378
379 chromium_setup(manifest)
380
381 # Zip up relevant files.
382 print('Zipping up files...')
383 if not manifest.zip_and_upload():
384 return 1
385
386 # Send test requests off to swarm.
387 print('Sending test requests to swarm.')
388 print('Server: %s' % swarming)
389 print('Job name: %s' % test_name)
390 test_url = swarming + '/test'
391 manifest_text = manifest.to_json()
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000392 result = net.url_read(test_url, data={'request': manifest_text})
maruel@chromium.org0437a732013-08-27 16:05:52 +0000393 if not result:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000394 tools.report_error(
395 'Failed to send test for %s\n%s' % (test_name, test_url))
maruel@chromium.org0437a732013-08-27 16:05:52 +0000396 return 1
397 try:
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000398 json.loads(result)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000399 except (ValueError, TypeError) as e:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000400 msg = '\n'.join((
401 'Failed to send test for %s' % test_name,
402 'Manifest: %s' % manifest_text,
403 'Bad response: %s' % result,
404 str(e)))
405 tools.report_error(msg)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000406 return 1
407 return 0
408
409
410def trigger(
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500411 swarming,
412 isolate_server,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000413 slave_os,
414 tasks,
415 task_prefix,
416 working_dir,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000417 verbose,
418 profile,
419 priority):
420 """Sends off the hash swarming test requests."""
421 highest_exit_code = 0
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000422 for (file_hash, test_name, shards, testfilter) in tasks:
maruel@chromium.org0437a732013-08-27 16:05:52 +0000423 # TODO(maruel): It should first create a request manifest object, then pass
424 # it to a function to zip, archive and trigger.
425 exit_code = process_manifest(
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500426 swarming=swarming,
427 isolate_server=isolate_server,
428 file_hash_or_isolated=file_hash,
429 test_name=task_prefix + test_name,
430 shards=int(shards),
431 test_filter=testfilter,
432 slave_os=slave_os,
433 working_dir=working_dir,
434 verbose=verbose,
435 profile=profile,
436 priority=priority,
437 algo=hashlib.sha1)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000438 highest_exit_code = max(highest_exit_code, exit_code)
439 return highest_exit_code
440
441
442def decorate_shard_output(result, shard_exit_code):
443 """Returns wrapped output for swarming task shard."""
444 tag = 'index %s (machine tag: %s, id: %s)' % (
445 result['config_instance_index'],
446 result['machine_id'],
447 result.get('machine_tag', 'unknown'))
448 return (
449 '\n'
450 '================================================================\n'
451 'Begin output from shard %s\n'
452 '================================================================\n'
453 '\n'
454 '%s'
455 '================================================================\n'
456 'End output from shard %s. Return %d\n'
457 '================================================================\n'
458 ) % (tag, result['output'] or NO_OUTPUT_FOUND, tag, shard_exit_code)
459
460
461def collect(url, test_name, timeout, decorate):
462 """Retrieves results of a Swarming job."""
463 test_keys = get_test_keys(url, test_name)
464 if not test_keys:
465 raise Failure('No test keys to get results with.')
466
maruel@chromium.org9c1c7b52013-08-28 19:04:36 +0000467 exit_code = None
maruel@chromium.org0437a732013-08-27 16:05:52 +0000468 for _index, output in yield_results(url, test_keys, timeout, None):
469 shard_exit_codes = (output['exit_codes'] or '1').split(',')
470 shard_exit_code = max(int(i) for i in shard_exit_codes)
471 if decorate:
472 print decorate_shard_output(output, shard_exit_code)
473 else:
474 print(
475 '%s/%s: %s' % (
476 output['machine_id'],
477 output['machine_tag'],
478 output['exit_codes']))
479 print(''.join(' %s\n' % l for l in output['output'].splitlines()))
maruel@chromium.org9c1c7b52013-08-28 19:04:36 +0000480 exit_code = exit_code or shard_exit_code
481 return exit_code if exit_code is not None else 1
maruel@chromium.org0437a732013-08-27 16:05:52 +0000482
483
484def add_trigger_options(parser):
485 """Adds all options to trigger a task on Swarming."""
486 parser.add_option(
487 '-I', '--isolate-server',
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000488 metavar='URL', default='',
489 help='Isolate server to use')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000490 parser.add_option(
491 '-w', '--working_dir', default='swarm_tests',
492 help='Working directory on the swarm slave side. default: %default.')
493 parser.add_option(
494 '-o', '--os', default=sys.platform,
495 help='Swarm OS image to request. Should be one of the valid sys.platform '
496 'values like darwin, linux2 or win32 default: %default.')
497 parser.add_option(
498 '-T', '--task-prefix', default='',
499 help='Prefix to give the swarm test request. default: %default')
500 parser.add_option(
501 '--profile', action='store_true',
502 default=bool(os.environ.get('ISOLATE_DEBUG')),
503 help='Have run_isolated.py print profiling info')
504 parser.add_option(
505 '--priority', type='int', default=100,
506 help='The lower value, the more important the task is')
507
508
509def process_trigger_options(parser, options):
510 options.isolate_server = options.isolate_server.rstrip('/')
511 if not options.isolate_server:
512 parser.error('--isolate-server is required.')
513 if options.os in ('', 'None'):
514 # Use the current OS.
515 options.os = sys.platform
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000516 if not options.os in PLATFORM_MAPPING_SWARMING:
maruel@chromium.org0437a732013-08-27 16:05:52 +0000517 parser.error('Invalid --os option.')
518
519
520def add_collect_options(parser):
521 parser.add_option(
522 '-t', '--timeout',
523 type='float',
524 default=DEFAULT_SHARD_WAIT_TIME,
525 help='Timeout to wait for result, set to 0 for no timeout; default: '
526 '%default s')
527 parser.add_option('--decorate', action='store_true', help='Decorate output')
528
529
530@subcommand.usage('test_name')
531def CMDcollect(parser, args):
532 """Retrieves results of a Swarming job.
533
534 The result can be in multiple part if the execution was sharded. It can
535 potentially have retries.
536 """
537 add_collect_options(parser)
538 (options, args) = parser.parse_args(args)
539 if not args:
540 parser.error('Must specify one test name.')
541 elif len(args) > 1:
542 parser.error('Must specify only one test name.')
543
544 try:
545 return collect(options.swarming, args[0], options.timeout, options.decorate)
546 except Failure as e:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000547 tools.report_error(e)
548 return 1
maruel@chromium.org0437a732013-08-27 16:05:52 +0000549
550
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000551@subcommand.usage('[hash|isolated ...]')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000552def CMDrun(parser, args):
553 """Triggers a job and wait for the results.
554
555 Basically, does everything to run command(s) remotely.
556 """
557 add_trigger_options(parser)
558 add_collect_options(parser)
559 options, args = parser.parse_args(args)
560
561 if not args:
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000562 parser.error('Must pass at least one .isolated file or its hash (sha1).')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000563 process_trigger_options(parser, options)
564
565 success = []
566 for arg in args:
567 logging.info('Triggering %s', arg)
568 try:
569 result = trigger(
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500570 swarming=options.swarming,
571 isolate_server=options.isolate_server,
572 slave_os=options.os,
573 tasks=[(arg, os.path.basename(arg), '1', '')],
574 task_prefix=options.task_prefix,
575 working_dir=options.working_dir,
576 verbose=options.verbose,
577 profile=options.profile,
578 priority=options.priority)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000579 except Failure as e:
580 result = e.args[0]
581 if result:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000582 tools.report_error('Failed to trigger %s: %s' % (arg, result))
maruel@chromium.org0437a732013-08-27 16:05:52 +0000583 else:
584 success.append(os.path.basename(arg))
585
586 if not success:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000587 tools.report_error('Failed to trigger any job.')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000588 return result
589
590 code = 0
591 for arg in success:
592 logging.info('Collecting %s', arg)
593 try:
594 new_code = collect(
595 options.swarming,
596 options.task_prefix + arg,
597 options.timeout,
598 options.decorate)
599 code = max(code, new_code)
600 except Failure as e:
601 code = max(code, 1)
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000602 tools.report_error(e)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000603 return code
604
605
606def CMDtrigger(parser, args):
607 """Triggers Swarm request(s).
608
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000609 Accepts one or multiple --task requests, with either the hash (sha1) of a
610 .isolated file already uploaded or the path to an .isolated file to archive,
611 packages it if needed and sends a Swarm manifest file to the Swarm server.
maruel@chromium.org0437a732013-08-27 16:05:52 +0000612 """
613 add_trigger_options(parser)
614 parser.add_option(
615 '--task', nargs=4, action='append', default=[], dest='tasks',
616 help='Task to trigger. The format is '
617 '(hash|isolated, test_name, shards, test_filter). This may be '
618 'used multiple times to send multiple hashes jobs. If an isolated '
619 'file is specified instead of an hash, it is first archived.')
620 (options, args) = parser.parse_args(args)
621
622 if args:
623 parser.error('Unknown args: %s' % args)
624 process_trigger_options(parser, options)
625 if not options.tasks:
626 parser.error('At least one --task is required.')
627
628 try:
629 return trigger(
Marc-Antoine Ruela7049872013-11-05 19:28:35 -0500630 swarming=options.swarming,
631 isolate_server=options.isolate_server,
632 slave_os=options.os,
633 tasks=options.tasks,
634 task_prefix=options.task_prefix,
635 working_dir=options.working_dir,
636 verbose=options.verbose,
637 profile=options.profile,
638 priority=options.priority)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000639 except Failure as e:
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000640 tools.report_error(e)
641 return 1
maruel@chromium.org0437a732013-08-27 16:05:52 +0000642
643
644class OptionParserSwarming(tools.OptionParserWithLogging):
645 def __init__(self, **kwargs):
646 tools.OptionParserWithLogging.__init__(
647 self, prog='swarming.py', **kwargs)
648 self.add_option(
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000649 '-S', '--swarming',
650 metavar='URL', default='',
651 help='Swarming server to use')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000652
653 def parse_args(self, *args, **kwargs):
654 options, args = tools.OptionParserWithLogging.parse_args(
655 self, *args, **kwargs)
656 options.swarming = options.swarming.rstrip('/')
657 if not options.swarming:
658 self.error('--swarming is required.')
659 return options, args
660
661
662def main(args):
663 dispatcher = subcommand.CommandDispatcher(__name__)
664 try:
665 return dispatcher.execute(OptionParserSwarming(version=__version__), args)
vadimsh@chromium.orgd908a542013-10-30 01:36:17 +0000666 except Exception as e:
667 tools.report_error(e)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000668 return 1
669
670
671if __name__ == '__main__':
672 fix_encoding.fix_encoding()
673 tools.disable_buffering()
674 colorama.init()
675 sys.exit(main(sys.argv[1:]))