blob: 8c8076e24d5db17f547d58fe59c33b9c05bbf140 [file] [log] [blame]
maruel@chromium.org0437a732013-08-27 16:05:52 +00001#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Client tool to trigger tasks or retrieve results from a Swarming server."""
7
8__version__ = '0.1'
9
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000010import binascii
maruel@chromium.org0437a732013-08-27 16:05:52 +000011import hashlib
12import json
13import logging
14import os
maruel@chromium.org0437a732013-08-27 16:05:52 +000015import shutil
maruel@chromium.org0437a732013-08-27 16:05:52 +000016import subprocess
17import sys
18import time
19import urllib
maruel@chromium.org0437a732013-08-27 16:05:52 +000020
21from third_party import colorama
22from third_party.depot_tools import fix_encoding
23from third_party.depot_tools import subcommand
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000024
25from utils import net
maruel@chromium.org0437a732013-08-27 16:05:52 +000026from utils import threading_utils
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000027from utils import tools
28from utils import zip_package
maruel@chromium.org0437a732013-08-27 16:05:52 +000029
maruel@chromium.org7b844a62013-09-17 13:04:59 +000030import isolateserver
maruel@chromium.org0437a732013-08-27 16:05:52 +000031import run_isolated
32
33
34ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
35TOOLS_PATH = os.path.join(ROOT_DIR, 'tools')
36
37
maruel@chromium.org0437a732013-08-27 16:05:52 +000038# The default time to wait for a shard to finish running.
csharp@chromium.org24758492013-08-28 19:10:54 +000039DEFAULT_SHARD_WAIT_TIME = 80 * 60.
maruel@chromium.org0437a732013-08-27 16:05:52 +000040
41
42NO_OUTPUT_FOUND = (
43 'No output produced by the test, it may have failed to run.\n'
44 '\n')
45
46
maruel@chromium.orge9403ab2013-09-20 18:03:49 +000047# TODO(maruel): cygwin != Windows. If a swarm_bot is running in cygwin, it's
48# different from running in native python.
49PLATFORM_MAPPING_SWARMING = {
maruel@chromium.org0437a732013-08-27 16:05:52 +000050 'cygwin': 'Windows',
51 'darwin': 'Mac',
52 'linux2': 'Linux',
53 'win32': 'Windows',
54}
55
maruel@chromium.orge9403ab2013-09-20 18:03:49 +000056PLATFORM_MAPPING_ISOLATE = {
57 'linux2': 'linux',
58 'darwin': 'mac',
59 'win32': 'win',
60}
61
maruel@chromium.org0437a732013-08-27 16:05:52 +000062
63class Failure(Exception):
64 """Generic failure."""
65 pass
66
67
68class Manifest(object):
69 """Represents a Swarming task manifest.
70
71 Also includes code to zip code and upload itself.
72 """
73 def __init__(
maruel@chromium.org814d23f2013-10-01 19:08:00 +000074 self, isolated_hash, test_name, shards, test_filter, slave_os,
maruel@chromium.org7b844a62013-09-17 13:04:59 +000075 working_dir, isolate_server, verbose, profile, priority, algo):
maruel@chromium.org0437a732013-08-27 16:05:52 +000076 """Populates a manifest object.
77 Args:
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.
81 test_filter - The gtest filter to apply when running the test.
82 slave_os - OS to run on.
83 working_dir - Relative working directory to start the script.
84 isolate_server - isolate server url.
85 verbose - if True, have the slave print more details.
86 profile - if True, have the slave print more timing data.
maruel@chromium.org7b844a62013-09-17 13:04:59 +000087 priority - int between 0 and 1000, lower the higher priority.
88 algo - hashing algorithm used.
maruel@chromium.org0437a732013-08-27 16:05:52 +000089 """
maruel@chromium.org814d23f2013-10-01 19:08:00 +000090 self.isolated_hash = isolated_hash
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000091 self.bundle = zip_package.ZipPackage(ROOT_DIR)
92
maruel@chromium.org0437a732013-08-27 16:05:52 +000093 self._test_name = test_name
94 self._shards = shards
95 self._test_filter = test_filter
maruel@chromium.org814d23f2013-10-01 19:08:00 +000096 self._target_platform = slave_os
maruel@chromium.org0437a732013-08-27 16:05:52 +000097 self._working_dir = working_dir
98
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +000099 self.isolate_server = isolate_server
100 self._data_server_retrieval = isolate_server + '/content/retrieve/default/'
maruel@chromium.org0437a732013-08-27 16:05:52 +0000101 self._data_server_storage = isolate_server + '/content/store/default/'
102 self._data_server_has = isolate_server + '/content/contains/default'
103 self._data_server_get_token = isolate_server + '/content/get_token'
104
105 self.verbose = bool(verbose)
106 self.profile = bool(profile)
107 self.priority = priority
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000108 self._algo = algo
maruel@chromium.org0437a732013-08-27 16:05:52 +0000109
110 self._zip_file_hash = ''
111 self._tasks = []
maruel@chromium.org0437a732013-08-27 16:05:52 +0000112 self._token_cache = None
113
114 def _token(self):
115 if not self._token_cache:
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000116 result = net.url_open(self._data_server_get_token)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000117 if not result:
118 # TODO(maruel): Implement authentication.
119 raise Failure('Failed to get token, need authentication')
120 # Quote it right away, so creating the urls is simpler.
121 self._token_cache = urllib.quote(result.read())
122 return self._token_cache
123
124 def add_task(self, task_name, actions, time_out=600):
125 """Appends a new task to the swarm manifest file."""
126 # See swarming/src/common/test_request_message.py TestObject constructor for
127 # the valid flags.
128 self._tasks.append(
129 {
130 'action': actions,
131 'decorate_output': self.verbose,
132 'test_name': task_name,
133 'time_out': time_out,
134 })
135
maruel@chromium.org0437a732013-08-27 16:05:52 +0000136 def zip_and_upload(self):
137 """Zips up all the files necessary to run a shard and uploads to Swarming
138 master.
139 """
140 assert not self._zip_file_hash
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000141
maruel@chromium.org0437a732013-08-27 16:05:52 +0000142 start_time = time.time()
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000143 zip_contents = self.bundle.zip_into_buffer()
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000144 self._zip_file_hash = self._algo(zip_contents).hexdigest()
maruel@chromium.org0437a732013-08-27 16:05:52 +0000145 print 'Zipping completed, time elapsed: %f' % (time.time() - start_time)
146
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000147 response = net.url_open(
maruel@chromium.org0437a732013-08-27 16:05:52 +0000148 self._data_server_has + '?token=%s' % self._token(),
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000149 data=binascii.unhexlify(self._zip_file_hash),
maruel@chromium.org0437a732013-08-27 16:05:52 +0000150 content_type='application/octet-stream')
151 if response is None:
152 print >> sys.stderr, (
153 'Unable to query server for zip file presence, aborting.')
154 return False
155
156 if response.read(1) == chr(1):
157 print 'Zip file already on server, no need to reupload.'
158 return True
159
160 print 'Zip file not on server, starting uploading.'
161
162 url = '%s%s?priority=0&token=%s' % (
163 self._data_server_storage, self._zip_file_hash, self._token())
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000164 response = net.url_open(
maruel@chromium.org0437a732013-08-27 16:05:52 +0000165 url, data=zip_contents, content_type='application/octet-stream')
166 if response is None:
167 print >> sys.stderr, 'Failed to upload the zip file: %s' % url
168 return False
169
170 return True
171
172 def to_json(self):
173 """Exports the current configuration into a swarm-readable manifest file.
174
175 This function doesn't mutate the object.
176 """
177 test_case = {
178 'test_case_name': self._test_name,
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000179 'data': [],
maruel@chromium.org0437a732013-08-27 16:05:52 +0000180 'tests': self._tasks,
181 'env_vars': {},
182 'configurations': [
183 {
184 'min_instances': self._shards,
185 'config_name': self._target_platform,
186 'dimensions': {
187 'os': self._target_platform,
188 },
189 },
190 ],
191 'working_dir': self._working_dir,
192 'restart_on_failure': True,
193 'cleanup': 'root',
194 'priority': self.priority,
195 }
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000196 if self._zip_file_hash:
197 test_case['data'].append(
198 [
199 self._data_server_retrieval + urllib.quote(self._zip_file_hash),
200 'swarm_data.zip',
201 ])
maruel@chromium.org0437a732013-08-27 16:05:52 +0000202 # These flags are googletest specific.
203 if self._test_filter and self._test_filter != '*':
204 test_case['env_vars']['GTEST_FILTER'] = self._test_filter
205 if self._shards > 1:
206 test_case['env_vars']['GTEST_SHARD_INDEX'] = '%(instance_index)s'
207 test_case['env_vars']['GTEST_TOTAL_SHARDS'] = '%(num_instances)s'
208
209 return json.dumps(test_case, separators=(',',':'))
210
211
212def now():
213 """Exists so it can be mocked easily."""
214 return time.time()
215
216
217def get_test_keys(swarm_base_url, test_name):
218 """Returns the Swarm test key for each shards of test_name."""
219 key_data = urllib.urlencode([('name', test_name)])
220 url = '%s/get_matching_test_cases?%s' % (swarm_base_url, key_data)
221
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000222 for _ in net.retry_loop(max_attempts=net.URL_OPEN_MAX_ATTEMPTS):
223 result = net.url_read(url, retry_404=True)
224 if result is None:
maruel@chromium.org0437a732013-08-27 16:05:52 +0000225 raise Failure(
226 'Error: Unable to find any tests with the name, %s, on swarm server'
227 % test_name)
228
maruel@chromium.org0437a732013-08-27 16:05:52 +0000229 # TODO(maruel): Compare exact string.
230 if 'No matching' in result:
231 logging.warning('Unable to find any tests with the name, %s, on swarm '
232 'server' % test_name)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000233 continue
234 return json.loads(result)
235
236 raise Failure(
237 'Error: Unable to find any tests with the name, %s, on swarm server'
238 % test_name)
239
240
241def retrieve_results(base_url, test_key, timeout, should_stop):
242 """Retrieves results for a single test_key."""
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000243 assert isinstance(timeout, float), timeout
maruel@chromium.org0437a732013-08-27 16:05:52 +0000244 params = [('r', test_key)]
245 result_url = '%s/get_result?%s' % (base_url, urllib.urlencode(params))
246 start = now()
247 while True:
248 if timeout and (now() - start) >= timeout:
249 logging.error('retrieve_results(%s) timed out', base_url)
250 return {}
251 # Do retries ourselves.
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000252 response = net.url_read(result_url, retry_404=False, retry_50x=False)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000253 if response is None:
254 # Aggressively poll for results. Do not use retry_404 so
255 # should_stop is polled more often.
256 remaining = min(5, timeout - (now() - start)) if timeout else 5
257 if remaining > 0:
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000258 if should_stop.get():
259 return {}
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000260 net.sleep_before_retry(1, remaining)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000261 else:
262 try:
vadimsh@chromium.org043b76d2013-09-12 16:15:13 +0000263 data = json.loads(response) or {}
maruel@chromium.org0437a732013-08-27 16:05:52 +0000264 except (ValueError, TypeError):
265 logging.warning(
266 'Received corrupted data for test_key %s. Retrying.', test_key)
267 else:
268 if data['output']:
269 return data
270 if should_stop.get():
271 return {}
272
273
274def yield_results(swarm_base_url, test_keys, timeout, max_threads):
275 """Yields swarm test results from the swarm server as (index, result).
276
277 Duplicate shards are ignored, the first one to complete is returned.
278
279 max_threads is optional and is used to limit the number of parallel fetches
280 done. Since in general the number of test_keys is in the range <=10, it's not
281 worth normally to limit the number threads. Mostly used for testing purposes.
282 """
283 shards_remaining = range(len(test_keys))
284 number_threads = (
285 min(max_threads, len(test_keys)) if max_threads else len(test_keys))
286 should_stop = threading_utils.Bit()
287 results_remaining = len(test_keys)
288 with threading_utils.ThreadPool(number_threads, number_threads, 0) as pool:
289 try:
290 for test_key in test_keys:
291 pool.add_task(
292 0, retrieve_results, swarm_base_url, test_key, timeout, should_stop)
293 while shards_remaining and results_remaining:
294 result = pool.get_one_result()
295 results_remaining -= 1
296 if not result:
297 # Failed to retrieve one key.
298 logging.error('Failed to retrieve the results for a swarm key')
299 continue
300 shard_index = result['config_instance_index']
301 if shard_index in shards_remaining:
302 shards_remaining.remove(shard_index)
303 yield shard_index, result
304 else:
305 logging.warning('Ignoring duplicate shard index %d', shard_index)
306 # Pop the last entry, there's no such shard.
307 shards_remaining.pop()
308 finally:
309 # Done, kill the remaining threads.
310 should_stop.set()
311
312
313def chromium_setup(manifest):
314 """Sets up the commands to run.
315
316 Highly chromium specific.
317 """
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000318 # Add uncompressed zip here. It'll be compressed as part of the package sent
319 # to Swarming server.
320 run_test_name = 'run_isolated.zip'
321 manifest.bundle.add_buffer(run_test_name,
322 run_isolated.get_as_zip_package().zip_into_buffer(compress=False))
maruel@chromium.org0437a732013-08-27 16:05:52 +0000323
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000324 cleanup_script_name = 'swarm_cleanup.py'
325 manifest.bundle.add_file(os.path.join(TOOLS_PATH, cleanup_script_name),
326 cleanup_script_name)
327
maruel@chromium.org0437a732013-08-27 16:05:52 +0000328 run_cmd = [
329 'python', run_test_name,
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000330 '--hash', manifest.isolated_hash,
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000331 '--isolate-server', manifest.isolate_server,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000332 ]
333 if manifest.verbose or manifest.profile:
334 # Have it print the profiling section.
335 run_cmd.append('--verbose')
336 manifest.add_task('Run Test', run_cmd)
337
338 # Clean up
339 manifest.add_task('Clean Up', ['python', cleanup_script_name])
340
341
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000342def archive(isolated, isolate_server, os_slave, algo, verbose):
maruel@chromium.org0437a732013-08-27 16:05:52 +0000343 """Archives a .isolated and all the dependencies on the CAC."""
344 tempdir = None
345 try:
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000346 logging.info('archive(%s, %s)', isolated, isolate_server)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000347 cmd = [
348 sys.executable,
349 os.path.join(ROOT_DIR, 'isolate.py'),
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000350 'archive',
maruel@chromium.org0437a732013-08-27 16:05:52 +0000351 '--outdir', isolate_server,
352 '--isolated', isolated,
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000353 '-V', 'OS', PLATFORM_MAPPING_ISOLATE[os_slave],
maruel@chromium.org0437a732013-08-27 16:05:52 +0000354 ]
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000355 cmd.extend(['--verbose'] * verbose)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000356 logging.info(' '.join(cmd))
357 if subprocess.call(cmd, verbose):
358 return
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000359 return isolateserver.hash_file(isolated, algo)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000360 finally:
361 if tempdir:
362 shutil.rmtree(tempdir)
363
364
365def process_manifest(
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000366 file_hash_or_isolated, test_name, shards, test_filter, slave_os,
367 working_dir, isolate_server, swarming, verbose, profile, priority, algo):
maruel@chromium.org0437a732013-08-27 16:05:52 +0000368 """Process the manifest file and send off the swarm test request.
369
370 Optionally archives an .isolated file.
371 """
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000372 if file_hash_or_isolated.endswith('.isolated'):
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000373 file_hash = archive(
374 file_hash_or_isolated, isolate_server, slave_os, algo, verbose)
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000375 if not file_hash:
376 print >> sys.stderr, 'Archival failure %s' % file_hash_or_isolated
maruel@chromium.org0437a732013-08-27 16:05:52 +0000377 return 1
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000378 elif isolateserver.is_valid_hash(file_hash_or_isolated, algo):
379 file_hash = file_hash_or_isolated
maruel@chromium.org0437a732013-08-27 16:05:52 +0000380 else:
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000381 print >> sys.stderr, 'Invalid hash %s' % file_hash_or_isolated
maruel@chromium.org0437a732013-08-27 16:05:52 +0000382 return 1
383
384 try:
385 manifest = Manifest(
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000386 file_hash,
387 test_name,
388 shards,
389 test_filter,
390 PLATFORM_MAPPING_SWARMING[slave_os],
391 working_dir,
392 isolate_server,
393 verbose,
394 profile,
395 priority,
396 algo)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000397 except ValueError as e:
398 print >> sys.stderr, 'Unable to process %s: %s' % (test_name, e)
399 return 1
400
401 chromium_setup(manifest)
402
403 # Zip up relevant files.
404 print('Zipping up files...')
405 if not manifest.zip_and_upload():
406 return 1
407
408 # Send test requests off to swarm.
409 print('Sending test requests to swarm.')
410 print('Server: %s' % swarming)
411 print('Job name: %s' % test_name)
412 test_url = swarming + '/test'
413 manifest_text = manifest.to_json()
vadimsh@chromium.org6b706212013-08-28 15:03:46 +0000414 result = net.url_open(test_url, data={'request': manifest_text})
maruel@chromium.org0437a732013-08-27 16:05:52 +0000415 if not result:
416 print >> sys.stderr, 'Failed to send test for %s\n%s' % (
417 test_name, test_url)
418 return 1
419 try:
420 json.load(result)
421 except (ValueError, TypeError) as e:
422 print >> sys.stderr, 'Failed to send test for %s' % test_name
423 print >> sys.stderr, 'Manifest: %s' % manifest_text
424 print >> sys.stderr, str(e)
425 return 1
426 return 0
427
428
429def trigger(
430 slave_os,
431 tasks,
432 task_prefix,
433 working_dir,
434 isolate_server,
435 swarming,
436 verbose,
437 profile,
438 priority):
439 """Sends off the hash swarming test requests."""
440 highest_exit_code = 0
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000441 for (file_hash, test_name, shards, testfilter) in tasks:
maruel@chromium.org0437a732013-08-27 16:05:52 +0000442 # TODO(maruel): It should first create a request manifest object, then pass
443 # it to a function to zip, archive and trigger.
444 exit_code = process_manifest(
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000445 file_hash,
maruel@chromium.org0437a732013-08-27 16:05:52 +0000446 task_prefix + test_name,
447 int(shards),
448 testfilter,
449 slave_os,
450 working_dir,
451 isolate_server,
452 swarming,
453 verbose,
454 profile,
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000455 priority,
456 hashlib.sha1)
maruel@chromium.org0437a732013-08-27 16:05:52 +0000457 highest_exit_code = max(highest_exit_code, exit_code)
458 return highest_exit_code
459
460
461def decorate_shard_output(result, shard_exit_code):
462 """Returns wrapped output for swarming task shard."""
463 tag = 'index %s (machine tag: %s, id: %s)' % (
464 result['config_instance_index'],
465 result['machine_id'],
466 result.get('machine_tag', 'unknown'))
467 return (
468 '\n'
469 '================================================================\n'
470 'Begin output from shard %s\n'
471 '================================================================\n'
472 '\n'
473 '%s'
474 '================================================================\n'
475 'End output from shard %s. Return %d\n'
476 '================================================================\n'
477 ) % (tag, result['output'] or NO_OUTPUT_FOUND, tag, shard_exit_code)
478
479
480def collect(url, test_name, timeout, decorate):
481 """Retrieves results of a Swarming job."""
482 test_keys = get_test_keys(url, test_name)
483 if not test_keys:
484 raise Failure('No test keys to get results with.')
485
maruel@chromium.org9c1c7b52013-08-28 19:04:36 +0000486 exit_code = None
maruel@chromium.org0437a732013-08-27 16:05:52 +0000487 for _index, output in yield_results(url, test_keys, timeout, None):
488 shard_exit_codes = (output['exit_codes'] or '1').split(',')
489 shard_exit_code = max(int(i) for i in shard_exit_codes)
490 if decorate:
491 print decorate_shard_output(output, shard_exit_code)
492 else:
493 print(
494 '%s/%s: %s' % (
495 output['machine_id'],
496 output['machine_tag'],
497 output['exit_codes']))
498 print(''.join(' %s\n' % l for l in output['output'].splitlines()))
maruel@chromium.org9c1c7b52013-08-28 19:04:36 +0000499 exit_code = exit_code or shard_exit_code
500 return exit_code if exit_code is not None else 1
maruel@chromium.org0437a732013-08-27 16:05:52 +0000501
502
503def add_trigger_options(parser):
504 """Adds all options to trigger a task on Swarming."""
505 parser.add_option(
506 '-I', '--isolate-server',
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000507 metavar='URL', default='',
508 help='Isolate server to use')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000509 parser.add_option(
510 '-w', '--working_dir', default='swarm_tests',
511 help='Working directory on the swarm slave side. default: %default.')
512 parser.add_option(
513 '-o', '--os', default=sys.platform,
514 help='Swarm OS image to request. Should be one of the valid sys.platform '
515 'values like darwin, linux2 or win32 default: %default.')
516 parser.add_option(
517 '-T', '--task-prefix', default='',
518 help='Prefix to give the swarm test request. default: %default')
519 parser.add_option(
520 '--profile', action='store_true',
521 default=bool(os.environ.get('ISOLATE_DEBUG')),
522 help='Have run_isolated.py print profiling info')
523 parser.add_option(
524 '--priority', type='int', default=100,
525 help='The lower value, the more important the task is')
526
527
528def process_trigger_options(parser, options):
529 options.isolate_server = options.isolate_server.rstrip('/')
530 if not options.isolate_server:
531 parser.error('--isolate-server is required.')
532 if options.os in ('', 'None'):
533 # Use the current OS.
534 options.os = sys.platform
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000535 if not options.os in PLATFORM_MAPPING_SWARMING:
maruel@chromium.org0437a732013-08-27 16:05:52 +0000536 parser.error('Invalid --os option.')
537
538
539def add_collect_options(parser):
540 parser.add_option(
541 '-t', '--timeout',
542 type='float',
543 default=DEFAULT_SHARD_WAIT_TIME,
544 help='Timeout to wait for result, set to 0 for no timeout; default: '
545 '%default s')
546 parser.add_option('--decorate', action='store_true', help='Decorate output')
547
548
549@subcommand.usage('test_name')
550def CMDcollect(parser, args):
551 """Retrieves results of a Swarming job.
552
553 The result can be in multiple part if the execution was sharded. It can
554 potentially have retries.
555 """
556 add_collect_options(parser)
557 (options, args) = parser.parse_args(args)
558 if not args:
559 parser.error('Must specify one test name.')
560 elif len(args) > 1:
561 parser.error('Must specify only one test name.')
562
563 try:
564 return collect(options.swarming, args[0], options.timeout, options.decorate)
565 except Failure as e:
566 parser.error(e.args[0])
567
568
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000569@subcommand.usage('[hash|isolated ...]')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000570def CMDrun(parser, args):
571 """Triggers a job and wait for the results.
572
573 Basically, does everything to run command(s) remotely.
574 """
575 add_trigger_options(parser)
576 add_collect_options(parser)
577 options, args = parser.parse_args(args)
578
579 if not args:
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000580 parser.error('Must pass at least one .isolated file or its hash (sha1).')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000581 process_trigger_options(parser, options)
582
583 success = []
584 for arg in args:
585 logging.info('Triggering %s', arg)
586 try:
587 result = trigger(
588 options.os,
589 [(arg, os.path.basename(arg), '1', '')],
590 options.task_prefix,
591 options.working_dir,
592 options.isolate_server,
593 options.swarming,
594 options.verbose,
595 options.profile,
596 options.priority)
597 except Failure as e:
598 result = e.args[0]
599 if result:
600 print >> sys.stderr, 'Failed to trigger %s: %s' % (arg, result)
601 else:
602 success.append(os.path.basename(arg))
603
604 if not success:
605 print >> sys.stderr, 'Failed to trigger any job.'
606 return result
607
608 code = 0
609 for arg in success:
610 logging.info('Collecting %s', arg)
611 try:
612 new_code = collect(
613 options.swarming,
614 options.task_prefix + arg,
615 options.timeout,
616 options.decorate)
617 code = max(code, new_code)
618 except Failure as e:
619 code = max(code, 1)
620 print >> sys.stderr, e.args[0]
621 return code
622
623
624def CMDtrigger(parser, args):
625 """Triggers Swarm request(s).
626
maruel@chromium.org7b844a62013-09-17 13:04:59 +0000627 Accepts one or multiple --task requests, with either the hash (sha1) of a
628 .isolated file already uploaded or the path to an .isolated file to archive,
629 packages it if needed and sends a Swarm manifest file to the Swarm server.
maruel@chromium.org0437a732013-08-27 16:05:52 +0000630 """
631 add_trigger_options(parser)
632 parser.add_option(
633 '--task', nargs=4, action='append', default=[], dest='tasks',
634 help='Task to trigger. The format is '
635 '(hash|isolated, test_name, shards, test_filter). This may be '
636 'used multiple times to send multiple hashes jobs. If an isolated '
637 'file is specified instead of an hash, it is first archived.')
638 (options, args) = parser.parse_args(args)
639
640 if args:
641 parser.error('Unknown args: %s' % args)
642 process_trigger_options(parser, options)
643 if not options.tasks:
644 parser.error('At least one --task is required.')
645
646 try:
647 return trigger(
648 options.os,
649 options.tasks,
650 options.task_prefix,
651 options.working_dir,
652 options.isolate_server,
653 options.swarming,
654 options.verbose,
655 options.profile,
656 options.priority)
657 except Failure as e:
658 parser.error(e.args[0])
659
660
661class OptionParserSwarming(tools.OptionParserWithLogging):
662 def __init__(self, **kwargs):
663 tools.OptionParserWithLogging.__init__(
664 self, prog='swarming.py', **kwargs)
665 self.add_option(
maruel@chromium.orge9403ab2013-09-20 18:03:49 +0000666 '-S', '--swarming',
667 metavar='URL', default='',
668 help='Swarming server to use')
maruel@chromium.org0437a732013-08-27 16:05:52 +0000669
670 def parse_args(self, *args, **kwargs):
671 options, args = tools.OptionParserWithLogging.parse_args(
672 self, *args, **kwargs)
673 options.swarming = options.swarming.rstrip('/')
674 if not options.swarming:
675 self.error('--swarming is required.')
676 return options, args
677
678
679def main(args):
680 dispatcher = subcommand.CommandDispatcher(__name__)
681 try:
682 return dispatcher.execute(OptionParserSwarming(version=__version__), args)
maruel@chromium.org9958e4a2013-09-17 00:01:48 +0000683 except Failure as e:
maruel@chromium.org0437a732013-08-27 16:05:52 +0000684 sys.stderr.write('\nError: ')
685 sys.stderr.write(str(e))
686 sys.stderr.write('\n')
687 return 1
688
689
690if __name__ == '__main__':
691 fix_encoding.fix_encoding()
692 tools.disable_buffering()
693 colorama.init()
694 sys.exit(main(sys.argv[1:]))