maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel | ea586f3 | 2016-04-05 11:11:33 -0700 | [diff] [blame] | 2 | # Copyright 2013 The LUCI Authors. All rights reserved. |
maruel | f1f5e2a | 2016-05-25 17:10:39 -0700 | [diff] [blame] | 3 | # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 | # that can be found in the LICENSE file. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 5 | """Client tool to trigger tasks or retrieve results from a Swarming server.""" |
| 6 | |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
| 9 | __version__ = '1.0' |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 10 | |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 11 | import collections |
Marc-Antoine Ruel | 5e6ccdb | 2015-04-02 15:55:13 -0400 | [diff] [blame] | 12 | import datetime |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 13 | import json |
| 14 | import logging |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 15 | import optparse |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 16 | import os |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 17 | import re |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 18 | import sys |
maruel | 11e31af | 2017-02-15 07:30:50 -0800 | [diff] [blame] | 19 | import textwrap |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 20 | import threading |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 21 | import time |
Takuto Ikuta | 3525017 | 2020-01-31 09:33:46 +0000 | [diff] [blame] | 22 | import uuid |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 23 | |
vadimsh@chromium.org | 6b70621 | 2013-08-28 15:03:46 +0000 | [diff] [blame] | 24 | from utils import tools |
Marc-Antoine Ruel | 016c760 | 2019-04-02 18:31:13 +0000 | [diff] [blame] | 25 | tools.force_local_third_party() |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 26 | |
Marc-Antoine Ruel | 016c760 | 2019-04-02 18:31:13 +0000 | [diff] [blame] | 27 | # third_party/ |
| 28 | import colorama |
| 29 | from chromium import natsort |
| 30 | from depot_tools import fix_encoding |
| 31 | from depot_tools import subcommand |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 32 | import six |
Marc-Antoine Ruel | ad8cabe | 2019-10-10 23:24:26 +0000 | [diff] [blame] | 33 | from six.moves import urllib |
Marc-Antoine Ruel | 016c760 | 2019-04-02 18:31:13 +0000 | [diff] [blame] | 34 | |
| 35 | # pylint: disable=ungrouped-imports |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 36 | import auth |
iannucci | 31ab919 | 2017-05-02 19:11:56 -0700 | [diff] [blame] | 37 | import cipd |
maruel@chromium.org | 7b844a6 | 2013-09-17 13:04:59 +0000 | [diff] [blame] | 38 | import isolateserver |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 39 | import isolate_storage |
Marc-Antoine Ruel | 2666d9c | 2018-05-18 13:52:02 -0400 | [diff] [blame] | 40 | import local_caching |
Marc-Antoine Ruel | 016c760 | 2019-04-02 18:31:13 +0000 | [diff] [blame] | 41 | from utils import file_path |
| 42 | from utils import fs |
| 43 | from utils import logging_utils |
| 44 | from utils import net |
| 45 | from utils import on_error |
| 46 | from utils import subprocess42 |
| 47 | from utils import threading_utils |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class Failure(Exception): |
| 51 | """Generic failure.""" |
| 52 | pass |
| 53 | |
| 54 | |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 55 | def default_task_name(options): |
| 56 | """Returns a default task name if not specified.""" |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 57 | if not options.task_name: |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 58 | task_name = u'%s/%s' % (options.user, '_'.join( |
| 59 | '%s=%s' % (k, v) for k, v in options.dimensions)) |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 60 | if options.isolated: |
| 61 | task_name += u'/' + options.isolated |
| 62 | return task_name |
| 63 | return options.task_name |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 64 | |
| 65 | |
| 66 | ### Triggering. |
| 67 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 68 | # See ../appengine/swarming/swarming_rpcs.py. |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 69 | CipdPackage = collections.namedtuple('CipdPackage', [ |
| 70 | 'package_name', |
| 71 | 'path', |
| 72 | 'version', |
| 73 | ]) |
borenet | 02f772b | 2016-06-22 12:42:19 -0700 | [diff] [blame] | 74 | |
borenet | 02f772b | 2016-06-22 12:42:19 -0700 | [diff] [blame] | 75 | # See ../appengine/swarming/swarming_rpcs.py. |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 76 | CipdInput = collections.namedtuple('CipdInput', [ |
| 77 | 'client_package', |
| 78 | 'packages', |
| 79 | 'server', |
| 80 | ]) |
borenet | 02f772b | 2016-06-22 12:42:19 -0700 | [diff] [blame] | 81 | |
| 82 | # See ../appengine/swarming/swarming_rpcs.py. |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 83 | FilesRef = collections.namedtuple('FilesRef', [ |
| 84 | 'isolated', |
| 85 | 'isolatedserver', |
| 86 | 'namespace', |
| 87 | ]) |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 88 | |
| 89 | # See ../appengine/swarming/swarming_rpcs.py. |
Robert Iannucci | bf5f84c | 2017-11-22 12:56:50 -0800 | [diff] [blame] | 90 | StringListPair = collections.namedtuple( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 91 | 'StringListPair', |
Marc-Antoine Ruel | 89669dc | 2019-05-01 14:01:08 +0000 | [diff] [blame] | 92 | [ |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 93 | 'key', |
| 94 | 'value', # repeated string |
Marc-Antoine Ruel | 89669dc | 2019-05-01 14:01:08 +0000 | [diff] [blame] | 95 | ]) |
| 96 | |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 97 | # See ../appengine/swarming/swarming_rpcs.py. |
| 98 | Containment = collections.namedtuple('Containment', [ |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 99 | 'containment_type', |
| 100 | ]) |
Robert Iannucci | bf5f84c | 2017-11-22 12:56:50 -0800 | [diff] [blame] | 101 | |
| 102 | # See ../appengine/swarming/swarming_rpcs.py. |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 103 | TaskProperties = collections.namedtuple('TaskProperties', [ |
| 104 | 'caches', |
| 105 | 'cipd_input', |
| 106 | 'command', |
| 107 | 'containment', |
| 108 | 'relative_cwd', |
| 109 | 'dimensions', |
| 110 | 'env', |
| 111 | 'env_prefixes', |
| 112 | 'execution_timeout_secs', |
| 113 | 'extra_args', |
| 114 | 'grace_period_secs', |
| 115 | 'idempotent', |
| 116 | 'inputs_ref', |
| 117 | 'io_timeout_secs', |
| 118 | 'outputs', |
| 119 | 'secret_bytes', |
| 120 | ]) |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 121 | |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 122 | # See ../appengine/swarming/swarming_rpcs.py. |
| 123 | TaskSlice = collections.namedtuple('TaskSlice', [ |
| 124 | 'expiration_secs', |
| 125 | 'properties', |
| 126 | 'wait_for_capacity', |
| 127 | ]) |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 128 | |
| 129 | # See ../appengine/swarming/swarming_rpcs.py. |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 130 | NewTaskRequest = collections.namedtuple('NewTaskRequest', [ |
| 131 | 'name', |
| 132 | 'parent_task_id', |
| 133 | 'priority', |
Junji Watanabe | 71bbaef | 2020-07-21 08:55:37 +0000 | [diff] [blame] | 134 | 'realm', |
Scott Lee | 44c13d7 | 2020-09-14 06:09:50 +0000 | [diff] [blame] | 135 | 'resultdb', |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 136 | 'task_slices', |
| 137 | 'service_account', |
| 138 | 'tags', |
| 139 | 'user', |
| 140 | 'pool_task_template', |
| 141 | ]) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 142 | |
| 143 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 144 | def namedtuple_to_dict(value): |
| 145 | """Recursively converts a namedtuple to a dict.""" |
Marc-Antoine Ruel | b73066b | 2018-04-19 20:16:55 -0400 | [diff] [blame] | 146 | if hasattr(value, '_asdict'): |
| 147 | return namedtuple_to_dict(value._asdict()) |
| 148 | if isinstance(value, (list, tuple)): |
| 149 | return [namedtuple_to_dict(v) for v in value] |
| 150 | if isinstance(value, dict): |
Marc-Antoine Ruel | 04903a3 | 2019-10-09 21:09:25 +0000 | [diff] [blame] | 151 | return {k: namedtuple_to_dict(v) for k, v in value.items()} |
Lei Lei | 73a5f73 | 2020-03-23 20:36:14 +0000 | [diff] [blame] | 152 | # json.dumps in Python3 doesn't support bytes. |
| 153 | if isinstance(value, bytes): |
| 154 | return six.ensure_str(value) |
Marc-Antoine Ruel | b73066b | 2018-04-19 20:16:55 -0400 | [diff] [blame] | 155 | return value |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 156 | |
| 157 | |
Vadim Shtayura | 2d83a94 | 2017-08-14 17:41:24 -0700 | [diff] [blame] | 158 | def task_request_to_raw_request(task_request): |
maruel | 71c61c8 | 2016-02-22 06:52:05 -0800 | [diff] [blame] | 159 | """Returns the json-compatible dict expected by the server for new request. |
maruel | af6269b | 2015-09-10 14:37:51 -0700 | [diff] [blame] | 160 | |
| 161 | This is for the v1 client Swarming API. |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 162 | """ |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 163 | out = namedtuple_to_dict(task_request) |
Vadim Shtayura | 2d83a94 | 2017-08-14 17:41:24 -0700 | [diff] [blame] | 164 | # Don't send 'service_account' if it is None to avoid confusing older |
| 165 | # version of the server that doesn't know about 'service_account' and don't |
| 166 | # use it at all. |
| 167 | if not out['service_account']: |
| 168 | out.pop('service_account') |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 169 | for task_slice in out['task_slices']: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 170 | task_slice['properties']['env'] = [{ |
| 171 | 'key': k, |
| 172 | 'value': v |
| 173 | } for k, v in task_slice['properties']['env'].items()] |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 174 | task_slice['properties']['env'].sort(key=lambda x: x['key']) |
Takuto Ikuta | 3525017 | 2020-01-31 09:33:46 +0000 | [diff] [blame] | 175 | out['request_uuid'] = str(uuid.uuid4()) |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 176 | return out |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 177 | |
| 178 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 179 | def swarming_trigger(swarming, raw_request): |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 180 | """Triggers a request on the Swarming server and returns the json data. |
| 181 | |
| 182 | It's the low-level function. |
| 183 | |
| 184 | Returns: |
| 185 | { |
| 186 | 'request': { |
| 187 | 'created_ts': u'2010-01-02 03:04:05', |
| 188 | 'name': .. |
| 189 | }, |
| 190 | 'task_id': '12300', |
| 191 | } |
| 192 | """ |
| 193 | logging.info('Triggering: %s', raw_request['name']) |
| 194 | |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 195 | result = net.url_read_json( |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 196 | swarming + '/_ah/api/swarming/v1/tasks/new', data=raw_request) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 197 | if not result: |
| 198 | on_error.report('Failed to trigger task %s' % raw_request['name']) |
| 199 | return None |
maruel | e557bce | 2015-11-17 09:01:27 -0800 | [diff] [blame] | 200 | if result.get('error'): |
maruel | d4d1531 | 2015-11-16 17:22:59 -0800 | [diff] [blame] | 201 | # The reply is an error. |
maruel | e557bce | 2015-11-17 09:01:27 -0800 | [diff] [blame] | 202 | msg = 'Failed to trigger task %s' % raw_request['name'] |
| 203 | if result['error'].get('errors'): |
| 204 | for err in result['error']['errors']: |
| 205 | if err.get('message'): |
| 206 | msg += '\nMessage: %s' % err['message'] |
| 207 | if err.get('debugInfo'): |
| 208 | msg += '\nDebug info:\n%s' % err['debugInfo'] |
| 209 | elif result['error'].get('message'): |
maruel | bf53e04 | 2015-12-01 15:00:51 -0800 | [diff] [blame] | 210 | msg += '\nMessage: %s' % result['error']['message'] |
maruel | e557bce | 2015-11-17 09:01:27 -0800 | [diff] [blame] | 211 | |
| 212 | on_error.report(msg) |
maruel | d4d1531 | 2015-11-16 17:22:59 -0800 | [diff] [blame] | 213 | return None |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 214 | return result |
| 215 | |
| 216 | |
| 217 | def setup_googletest(env, shards, index): |
| 218 | """Sets googletest specific environment variables.""" |
| 219 | if shards > 1: |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 220 | assert not any(i['key'] == 'GTEST_SHARD_INDEX' for i in env), env |
| 221 | assert not any(i['key'] == 'GTEST_TOTAL_SHARDS' for i in env), env |
| 222 | env = env[:] |
| 223 | env.append({'key': 'GTEST_SHARD_INDEX', 'value': str(index)}) |
| 224 | env.append({'key': 'GTEST_TOTAL_SHARDS', 'value': str(shards)}) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 225 | return env |
| 226 | |
| 227 | |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 228 | def trigger_task_shards(swarming, task_request): |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 229 | """Triggers one or many subtasks of a sharded task. |
| 230 | |
| 231 | Returns: |
| 232 | Dict with task details, returned to caller as part of --dump-json output. |
| 233 | None in case of failure. |
| 234 | """ |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 235 | |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 236 | def convert_request(): |
Erik Chen | d50a88f | 2019-02-16 01:22:07 +0000 | [diff] [blame] | 237 | """ |
| 238 | Args: |
| 239 | index: The index of the task request. |
| 240 | |
| 241 | Returns: |
| 242 | raw_request: A swarming compatible JSON dictionary of the request. |
| 243 | shard_index: The index of the shard, which may be different than the index |
| 244 | of the task request. |
| 245 | """ |
Vadim Shtayura | 2d83a94 | 2017-08-14 17:41:24 -0700 | [diff] [blame] | 246 | req = task_request_to_raw_request(task_request) |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 247 | shard_index = 0 |
Erik Chen | d50a88f | 2019-02-16 01:22:07 +0000 | [diff] [blame] | 248 | |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 249 | task_slices = req['task_slices'] |
| 250 | total_shards = 1 |
| 251 | # Multiple tasks slices might exist if there are optional "slices", e.g. |
| 252 | # multiple ways of dispatching the task that should be equivalent. These |
| 253 | # should be functionally equivalent but we have cannot guarantee that. If |
| 254 | # we see the GTEST_SHARD_INDEX env var, we assume that it applies to all |
| 255 | # slices. |
| 256 | for task_slice in task_slices: |
| 257 | for env_var in task_slice['properties']['env']: |
| 258 | if env_var['key'] == 'GTEST_SHARD_INDEX': |
| 259 | shard_index = int(env_var['value']) |
| 260 | if env_var['key'] == 'GTEST_TOTAL_SHARDS': |
| 261 | total_shards = int(env_var['value']) |
| 262 | if total_shards > 1: |
| 263 | req['name'] += ':%s:%s' % (shard_index, total_shards) |
| 264 | if shard_index and total_shards: |
| 265 | req['tags'] += [ |
| 266 | 'shard_index:%d' % shard_index, |
| 267 | 'total_shards:%d' % total_shards, |
| 268 | ] |
Erik Chen | d50a88f | 2019-02-16 01:22:07 +0000 | [diff] [blame] | 269 | |
| 270 | return req, shard_index |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 271 | |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 272 | request, shard_index = convert_request() |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 273 | tasks = {} |
| 274 | priority_warning = False |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 275 | task = swarming_trigger(swarming, request) |
| 276 | if task is not None: |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 277 | logging.info('Request result: %s', task) |
| 278 | if (not priority_warning and |
Marc-Antoine Ruel | b121676 | 2017-08-17 10:07:49 -0400 | [diff] [blame] | 279 | int(task['request']['priority']) != task_request.priority): |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 280 | priority_warning = True |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 281 | print( |
| 282 | 'Priority was reset to %s' % task['request']['priority'], |
| 283 | file=sys.stderr) |
Ted Pudlik | adc55e9 | 2020-09-28 23:25:49 +0000 | [diff] [blame] | 284 | view_url = '%s/user/task/%s' % (swarming, task['task_id']) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 285 | tasks[request['name']] = { |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 286 | 'shard_index': shard_index, |
| 287 | 'task_id': task['task_id'], |
Ted Pudlik | adc55e9 | 2020-09-28 23:25:49 +0000 | [diff] [blame] | 288 | 'view_url': view_url, |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 289 | } |
Ted Pudlik | adc55e9 | 2020-09-28 23:25:49 +0000 | [diff] [blame] | 290 | logging.info('Task UI: %s', view_url) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 291 | |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 292 | if not tasks: |
| 293 | print('Task did not trigger successfully', file=sys.stderr) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 294 | return None |
| 295 | |
| 296 | return tasks |
| 297 | |
| 298 | |
| 299 | ### Collection. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 300 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 301 | # How often to print status updates to stdout in 'collect'. |
Jao-ke Chin-Lee | ba184e6 | 2018-11-19 17:04:41 +0000 | [diff] [blame] | 302 | STATUS_UPDATE_INTERVAL = 5 * 60. |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 303 | |
Marc-Antoine Ruel | 2f6581a | 2014-10-03 11:09:53 -0400 | [diff] [blame] | 304 | |
Marc-Antoine Ruel | 20b764d | 2018-06-22 18:08:37 +0000 | [diff] [blame] | 305 | class TaskState(object): |
| 306 | """Represents the current task state. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 307 | |
Marc-Antoine Ruel | 20b764d | 2018-06-22 18:08:37 +0000 | [diff] [blame] | 308 | For documentation, see the comments in the swarming_rpcs.TaskState enum, which |
| 309 | is the source of truth for these values: |
| 310 | https://cs.chromium.org/chromium/infra/luci/appengine/swarming/swarming_rpcs.py?q=TaskState\( |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 311 | |
Marc-Antoine Ruel | 20b764d | 2018-06-22 18:08:37 +0000 | [diff] [blame] | 312 | It's in fact an enum. |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 313 | """ |
| 314 | RUNNING = 0x10 |
| 315 | PENDING = 0x20 |
| 316 | EXPIRED = 0x30 |
| 317 | TIMED_OUT = 0x40 |
| 318 | BOT_DIED = 0x50 |
| 319 | CANCELED = 0x60 |
| 320 | COMPLETED = 0x70 |
Marc-Antoine Ruel | 2e52c55 | 2018-03-26 19:27:36 -0400 | [diff] [blame] | 321 | KILLED = 0x80 |
Marc-Antoine Ruel | fc70835 | 2018-05-04 20:25:43 -0400 | [diff] [blame] | 322 | NO_RESOURCE = 0x100 |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 323 | |
Marc-Antoine Ruel | 20b764d | 2018-06-22 18:08:37 +0000 | [diff] [blame] | 324 | STATES_RUNNING = ('PENDING', 'RUNNING') |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 325 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 326 | _ENUMS = { |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 327 | 'RUNNING': RUNNING, |
| 328 | 'PENDING': PENDING, |
| 329 | 'EXPIRED': EXPIRED, |
| 330 | 'TIMED_OUT': TIMED_OUT, |
| 331 | 'BOT_DIED': BOT_DIED, |
| 332 | 'CANCELED': CANCELED, |
| 333 | 'COMPLETED': COMPLETED, |
| 334 | 'KILLED': KILLED, |
| 335 | 'NO_RESOURCE': NO_RESOURCE, |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 338 | @classmethod |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 339 | def from_enum(cls, state): |
| 340 | """Returns int value based on the string.""" |
| 341 | if state not in cls._ENUMS: |
| 342 | raise ValueError('Invalid state %s' % state) |
| 343 | return cls._ENUMS[state] |
| 344 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 345 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 346 | class TaskOutputCollector(object): |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 347 | """Assembles task execution summary (for --task-summary-json output). |
| 348 | |
| 349 | Optionally fetches task outputs from isolate server to local disk (used when |
| 350 | --task-output-dir is passed). |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 351 | |
| 352 | This object is shared among multiple threads running 'retrieve_results' |
| 353 | function, in particular they call 'process_shard_result' method in parallel. |
| 354 | """ |
| 355 | |
Takuto Ikuta | 1e6072c | 2018-11-06 20:42:43 +0000 | [diff] [blame] | 356 | def __init__(self, task_output_dir, task_output_stdout, shard_count, |
| 357 | filter_cb): |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 358 | """Initializes TaskOutputCollector, ensures |task_output_dir| exists. |
| 359 | |
| 360 | Args: |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 361 | task_output_dir: (optional) local directory to put fetched files to. |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 362 | shard_count: expected number of task shards. |
| 363 | """ |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 364 | self.task_output_dir = ( |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 365 | six.text_type(os.path.abspath(task_output_dir)) |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 366 | if task_output_dir else task_output_dir) |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 367 | self.task_output_stdout = task_output_stdout |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 368 | self.shard_count = shard_count |
Takuto Ikuta | 1e6072c | 2018-11-06 20:42:43 +0000 | [diff] [blame] | 369 | self.filter_cb = filter_cb |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 370 | |
| 371 | self._lock = threading.Lock() |
| 372 | self._per_shard_results = {} |
| 373 | self._storage = None |
| 374 | |
nodir | e5028a9 | 2016-04-29 14:38:21 -0700 | [diff] [blame] | 375 | if self.task_output_dir: |
| 376 | file_path.ensure_tree(self.task_output_dir) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 377 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 378 | def process_shard_result(self, shard_index, result): |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 379 | """Stores results of a single task shard, fetches output files if necessary. |
| 380 | |
Marc-Antoine Ruel | e4dcbb8 | 2014-10-01 09:30:56 -0400 | [diff] [blame] | 381 | Modifies |result| in place. |
| 382 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 383 | shard_index is 0-based. |
| 384 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 385 | Called concurrently from multiple threads. |
| 386 | """ |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 387 | # Sanity check index is in expected range. |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 388 | assert isinstance(shard_index, int) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 389 | if shard_index < 0 or shard_index >= self.shard_count: |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 390 | logging.warning('Shard index %d is outside of expected range: [0; %d]', |
| 391 | shard_index, self.shard_count - 1) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 392 | return |
| 393 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 394 | if result.get('outputs_ref'): |
| 395 | ref = result['outputs_ref'] |
| 396 | result['outputs_ref']['view_url'] = '%s/browse?%s' % ( |
| 397 | ref['isolatedserver'], |
Marc-Antoine Ruel | ad8cabe | 2019-10-10 23:24:26 +0000 | [diff] [blame] | 398 | urllib.parse.urlencode([('namespace', ref['namespace']), |
| 399 | ('hash', ref['isolated'])])) |
Kevin Graney | c2c3b9e | 2014-08-26 09:04:17 -0400 | [diff] [blame] | 400 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 401 | # Store result dict of that shard, ignore results we've already seen. |
| 402 | with self._lock: |
| 403 | if shard_index in self._per_shard_results: |
| 404 | logging.warning('Ignoring duplicate shard index %d', shard_index) |
| 405 | return |
| 406 | self._per_shard_results[shard_index] = result |
| 407 | |
| 408 | # Fetch output files if necessary. |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 409 | if self.task_output_dir and result.get('outputs_ref'): |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 410 | server_ref = isolate_storage.ServerRef( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 411 | result['outputs_ref']['isolatedserver'], |
| 412 | result['outputs_ref']['namespace']) |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 413 | storage = self._get_storage(server_ref) |
Marc-Antoine Ruel | e4dcbb8 | 2014-10-01 09:30:56 -0400 | [diff] [blame] | 414 | if storage: |
| 415 | # Output files are supposed to be small and they are not reused across |
Marc-Antoine Ruel | 2666d9c | 2018-05-18 13:52:02 -0400 | [diff] [blame] | 416 | # tasks. So use MemoryContentAddressedCache for them instead of on-disk |
| 417 | # cache. Make files writable, so that calling script can delete them. |
Marc-Antoine Ruel | e4dcbb8 | 2014-10-01 09:30:56 -0400 | [diff] [blame] | 418 | isolateserver.fetch_isolated( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 419 | result['outputs_ref']['isolated'], storage, |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 420 | local_caching.MemoryContentAddressedCache(file_mode_mask=0o700), |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 421 | os.path.join(self.task_output_dir, str(shard_index)), False, |
| 422 | self.filter_cb) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 423 | |
| 424 | def finalize(self): |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 425 | """Assembles and returns task summary JSON, shutdowns underlying Storage.""" |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 426 | with self._lock: |
| 427 | # Write an array of shard results with None for missing shards. |
| 428 | summary = { |
Marc-Antoine Ruel | 0fdee22 | 2019-10-10 14:42:40 +0000 | [diff] [blame] | 429 | 'shards': [ |
| 430 | self._per_shard_results.get(i) for i in range(self.shard_count) |
| 431 | ], |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 432 | } |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 433 | |
| 434 | # Don't store stdout in the summary if not requested too. |
| 435 | if "json" not in self.task_output_stdout: |
| 436 | for shard_json in summary['shards']: |
| 437 | if not shard_json: |
| 438 | continue |
| 439 | if "output" in shard_json: |
| 440 | del shard_json["output"] |
| 441 | if "outputs" in shard_json: |
| 442 | del shard_json["outputs"] |
| 443 | |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 444 | # Write summary.json to task_output_dir as well. |
| 445 | if self.task_output_dir: |
| 446 | tools.write_json( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 447 | os.path.join(self.task_output_dir, u'summary.json'), summary, False) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 448 | if self._storage: |
| 449 | self._storage.close() |
| 450 | self._storage = None |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 451 | return summary |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 452 | |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 453 | def _get_storage(self, server_ref): |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 454 | """Returns isolateserver.Storage to use to fetch files.""" |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 455 | assert self.task_output_dir |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 456 | with self._lock: |
| 457 | if not self._storage: |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 458 | self._storage = isolateserver.get_storage(server_ref) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 459 | else: |
| 460 | # Shards must all use exact same isolate server and namespace. |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 461 | if self._storage.server_ref.url != server_ref.url: |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 462 | logging.error( |
| 463 | 'Task shards are using multiple isolate servers: %s and %s', |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 464 | self._storage.server_ref.url, server_ref.url) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 465 | return None |
Marc-Antoine Ruel | b851313 | 2018-11-20 19:48:53 +0000 | [diff] [blame] | 466 | if self._storage.server_ref.namespace != server_ref.namespace: |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 467 | logging.error('Task shards are using multiple namespaces: %s and %s', |
| 468 | self._storage.server_ref.namespace, |
| 469 | server_ref.namespace) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 470 | return None |
| 471 | return self._storage |
| 472 | |
| 473 | |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 474 | def now(): |
| 475 | """Exists so it can be mocked easily.""" |
| 476 | return time.time() |
| 477 | |
| 478 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 479 | def parse_time(value): |
| 480 | """Converts serialized time from the API to datetime.datetime.""" |
| 481 | # When microseconds are 0, the '.123456' suffix is elided. This means the |
| 482 | # serialized format is not consistent, which confuses the hell out of python. |
| 483 | for fmt in ('%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S'): |
| 484 | try: |
| 485 | return datetime.datetime.strptime(value, fmt) |
| 486 | except ValueError: |
| 487 | pass |
| 488 | raise ValueError('Failed to parse %s' % value) |
| 489 | |
| 490 | |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 491 | def retrieve_results(base_url, shard_index, task_id, timeout, should_stop, |
| 492 | output_collector, include_perf, fetch_stdout): |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 493 | """Retrieves results for a single task ID. |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 494 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 495 | Returns: |
| 496 | <result dict> on success. |
| 497 | None on failure. |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 498 | """ |
maruel | 71c61c8 | 2016-02-22 06:52:05 -0800 | [diff] [blame] | 499 | assert timeout is None or isinstance(timeout, float), timeout |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 500 | result_url = '%s/_ah/api/swarming/v1/task/%s/result' % (base_url, task_id) |
maruel | 9531ce0 | 2016-04-13 06:11:23 -0700 | [diff] [blame] | 501 | if include_perf: |
| 502 | result_url += '?include_performance_stats=true' |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 503 | output_url = '%s/_ah/api/swarming/v1/task/%s/stdout' % (base_url, task_id) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 504 | started = now() |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 505 | deadline = started + timeout if timeout > 0 else None |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 506 | attempt = 0 |
| 507 | |
| 508 | while not should_stop.is_set(): |
| 509 | attempt += 1 |
| 510 | |
| 511 | # Waiting for too long -> give up. |
| 512 | current_time = now() |
| 513 | if deadline and current_time >= deadline: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 514 | logging.error('retrieve_results(%s) timed out on attempt %d', base_url, |
| 515 | attempt) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 516 | return None |
| 517 | |
| 518 | # Do not spin too fast. Spin faster at the beginning though. |
| 519 | # Start with 1 sec delay and for each 30 sec of waiting add another second |
| 520 | # of delay, until hitting 15 sec ceiling. |
| 521 | if attempt > 1: |
| 522 | max_delay = min(15, 1 + (current_time - started) / 30.0) |
| 523 | delay = min(max_delay, deadline - current_time) if deadline else max_delay |
| 524 | if delay > 0: |
| 525 | logging.debug('Waiting %.1f sec before retrying', delay) |
| 526 | should_stop.wait(delay) |
| 527 | if should_stop.is_set(): |
| 528 | return None |
| 529 | |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 530 | # Disable internal retries in net.url_read_json, since we are doing retries |
| 531 | # ourselves. |
| 532 | # TODO(maruel): We'd need to know if it's a 404 and not retry at all. |
maruel | 0eb1d1b | 2015-10-02 14:48:21 -0700 | [diff] [blame] | 533 | # TODO(maruel): Sadly, we currently have to poll here. Use hanging HTTP |
| 534 | # request on GAE v2. |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 535 | # Retry on 500s only if no timeout is specified. |
| 536 | result = net.url_read_json(result_url, retry_50x=bool(timeout == -1)) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 537 | if not result: |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 538 | if timeout == -1: |
| 539 | return None |
Marc-Antoine Ruel | 200b395 | 2014-08-14 11:07:44 -0400 | [diff] [blame] | 540 | continue |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 541 | |
maruel | bf53e04 | 2015-12-01 15:00:51 -0800 | [diff] [blame] | 542 | if result.get('error'): |
| 543 | # An error occurred. |
| 544 | if result['error'].get('errors'): |
| 545 | for err in result['error']['errors']: |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 546 | logging.warning('Error while reading task: %s; %s', |
| 547 | err.get('message'), err.get('debugInfo')) |
maruel | bf53e04 | 2015-12-01 15:00:51 -0800 | [diff] [blame] | 548 | elif result['error'].get('message'): |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 549 | logging.warning('Error while reading task: %s', |
| 550 | result['error']['message']) |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 551 | if timeout == -1: |
| 552 | return result |
maruel | bf53e04 | 2015-12-01 15:00:51 -0800 | [diff] [blame] | 553 | continue |
| 554 | |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 555 | # When timeout == -1, always return on first attempt. 500s are already |
| 556 | # retried in this case. |
Marc-Antoine Ruel | 20b764d | 2018-06-22 18:08:37 +0000 | [diff] [blame] | 557 | if result['state'] not in TaskState.STATES_RUNNING or timeout == -1: |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 558 | if fetch_stdout: |
| 559 | out = net.url_read_json(output_url) |
Vadim Shtayura | 6fd3c7b | 2017-11-03 15:32:51 -0700 | [diff] [blame] | 560 | result['output'] = out.get('output', '') if out else '' |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 561 | # Record the result, try to fetch attached output files (if any). |
| 562 | if output_collector: |
| 563 | # TODO(vadimsh): Respect |should_stop| and |deadline| when fetching. |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 564 | output_collector.process_shard_result(shard_index, result) |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 565 | if result.get('internal_failure'): |
| 566 | logging.error('Internal error!') |
| 567 | elif result['state'] == 'BOT_DIED': |
| 568 | logging.error('Bot died!') |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 569 | return result |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 570 | |
| 571 | |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 572 | def yield_results(swarm_base_url, task_ids, timeout, max_threads, |
| 573 | print_status_updates, output_collector, include_perf, |
| 574 | fetch_stdout): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 575 | """Yields swarming task results from the swarming server as (index, result). |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 576 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 577 | Duplicate shards are ignored. Shards are yielded in order of completion. |
| 578 | Timed out shards are NOT yielded at all. Caller can compare number of yielded |
| 579 | shards with len(task_keys) to verify all shards completed. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 580 | |
| 581 | 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] | 582 | 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] | 583 | 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] | 584 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 585 | output_collector is an optional instance of TaskOutputCollector that will be |
| 586 | used to fetch files produced by a task from isolate server to the local disk. |
| 587 | |
Marc-Antoine Ruel | 5c72034 | 2014-02-21 14:46:14 -0500 | [diff] [blame] | 588 | Yields: |
| 589 | (index, result). In particular, 'result' is defined as the |
| 590 | GetRunnerResults() function in services/swarming/server/test_runner.py. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 591 | """ |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 592 | number_threads = ( |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 593 | min(max_threads, len(task_ids)) if max_threads else len(task_ids)) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 594 | should_stop = threading.Event() |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 595 | results_channel = threading_utils.TaskChannel() |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 596 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 597 | with threading_utils.ThreadPool(number_threads, number_threads, 0) as pool: |
| 598 | try: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 599 | # Adds a task to the thread pool to call 'retrieve_results' and return |
| 600 | # the results together with shard_index that produced them (as a tuple). |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 601 | def enqueue_retrieve_results(shard_index, task_id): |
Marc-Antoine Ruel | 486c9b5 | 2018-07-23 19:30:47 +0000 | [diff] [blame] | 602 | # pylint: disable=no-value-for-parameter |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 603 | task_fn = lambda *args: (shard_index, retrieve_results(*args)) |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 604 | pool.add_task(0, results_channel.wrap_task(task_fn), swarm_base_url, |
| 605 | shard_index, task_id, timeout, should_stop, |
| 606 | output_collector, include_perf, fetch_stdout) |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 607 | |
| 608 | # Enqueue 'retrieve_results' calls for each shard key to run in parallel. |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 609 | for shard_index, task_id in enumerate(task_ids): |
| 610 | enqueue_retrieve_results(shard_index, task_id) |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 611 | |
| 612 | # Wait for all of them to finish. |
Lei Lei | 73a5f73 | 2020-03-23 20:36:14 +0000 | [diff] [blame] | 613 | # Convert to list, since range in Python3 doesn't have remove. |
| 614 | shards_remaining = list(range(len(task_ids))) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 615 | active_task_count = len(task_ids) |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 616 | while active_task_count: |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 617 | shard_index, result = None, None |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 618 | try: |
Marc-Antoine Ruel | 4494b6c | 2018-11-28 21:00:41 +0000 | [diff] [blame] | 619 | shard_index, result = results_channel.next( |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 620 | timeout=STATUS_UPDATE_INTERVAL) |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 621 | except threading_utils.TaskChannel.Timeout: |
| 622 | if print_status_updates: |
Jao-ke Chin-Lee | ba184e6 | 2018-11-19 17:04:41 +0000 | [diff] [blame] | 623 | time_now = str(datetime.datetime.now()) |
| 624 | _, time_now = time_now.split(' ') |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 625 | print('%s ' |
| 626 | 'Waiting for results from the following shards: %s' % |
| 627 | (time_now, ', '.join(map(str, shards_remaining)))) |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 628 | sys.stdout.flush() |
| 629 | continue |
| 630 | except Exception: |
| 631 | logging.exception('Unexpected exception in retrieve_results') |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 632 | |
| 633 | # A call to 'retrieve_results' finished (successfully or not). |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 634 | active_task_count -= 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 635 | if not result: |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 636 | logging.error('Failed to retrieve the results for a swarming key') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 637 | continue |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 638 | |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 639 | # Yield back results to the caller. |
| 640 | assert shard_index in shards_remaining |
| 641 | shards_remaining.remove(shard_index) |
| 642 | yield shard_index, result |
Vadim Shtayura | b19319e | 2014-04-27 08:50:06 -0700 | [diff] [blame] | 643 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 644 | finally: |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 645 | # Done or aborted with Ctrl+C, kill the remaining threads. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 646 | should_stop.set() |
| 647 | |
| 648 | |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 649 | def decorate_shard_output(swarming, shard_index, metadata, include_stdout): |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 650 | """Returns wrapped output for swarming task shard.""" |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 651 | if metadata.get('started_ts') and not metadata.get('deduped_from'): |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 652 | pending = '%.1fs' % (parse_time(metadata['started_ts']) - |
| 653 | parse_time(metadata['created_ts'])).total_seconds() |
Marc-Antoine Ruel | 3f9931a | 2017-11-03 14:34:49 -0400 | [diff] [blame] | 654 | elif (metadata.get('state') in ('BOT_DIED', 'CANCELED', 'EXPIRED') and |
| 655 | metadata.get('abandoned_ts')): |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 656 | pending = '%.1fs' % (parse_time(metadata['abandoned_ts']) - |
| 657 | parse_time(metadata['created_ts'])).total_seconds() |
Marc-Antoine Ruel | 5e6ccdb | 2015-04-02 15:55:13 -0400 | [diff] [blame] | 658 | else: |
| 659 | pending = 'N/A' |
| 660 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 661 | if metadata.get('duration') is not None: |
| 662 | duration = '%.1fs' % metadata['duration'] |
Marc-Antoine Ruel | 5e6ccdb | 2015-04-02 15:55:13 -0400 | [diff] [blame] | 663 | else: |
| 664 | duration = 'N/A' |
| 665 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 666 | if metadata.get('exit_code') is not None: |
| 667 | # Integers are encoded as string to not loose precision. |
| 668 | exit_code = '%s' % metadata['exit_code'] |
Marc-Antoine Ruel | 5e6ccdb | 2015-04-02 15:55:13 -0400 | [diff] [blame] | 669 | else: |
| 670 | exit_code = 'N/A' |
| 671 | |
| 672 | bot_id = metadata.get('bot_id') or 'N/A' |
| 673 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 674 | url = '%s/user/task/%s' % (swarming, metadata['task_id']) |
Marc-Antoine Ruel | 4e6b73d | 2014-10-03 18:00:05 -0400 | [diff] [blame] | 675 | tag_header = 'Shard %d %s' % (shard_index, url) |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 676 | tag_footer1 = 'End of shard %d' % (shard_index) |
Marc-Antoine Ruel | 3f9931a | 2017-11-03 14:34:49 -0400 | [diff] [blame] | 677 | if metadata.get('state') == 'CANCELED': |
| 678 | tag_footer2 = ' Pending: %s CANCELED' % pending |
| 679 | elif metadata.get('state') == 'EXPIRED': |
| 680 | tag_footer2 = ' Pending: %s EXPIRED (lack of capacity)' % pending |
Marc-Antoine Ruel | 2e52c55 | 2018-03-26 19:27:36 -0400 | [diff] [blame] | 681 | elif metadata.get('state') in ('BOT_DIED', 'TIMED_OUT', 'KILLED'): |
Marc-Antoine Ruel | 3f9931a | 2017-11-03 14:34:49 -0400 | [diff] [blame] | 682 | tag_footer2 = ' Pending: %s Duration: %s Bot: %s Exit: %s %s' % ( |
| 683 | pending, duration, bot_id, exit_code, metadata['state']) |
| 684 | else: |
| 685 | tag_footer2 = ' Pending: %s Duration: %s Bot: %s Exit: %s' % ( |
| 686 | pending, duration, bot_id, exit_code) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 687 | |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 688 | tag_len = max(len(x) for x in [tag_header, tag_footer1, tag_footer2]) |
| 689 | dash_pad = '+-%s-+' % ('-' * tag_len) |
| 690 | tag_header = '| %s |' % tag_header.ljust(tag_len) |
| 691 | tag_footer1 = '| %s |' % tag_footer1.ljust(tag_len) |
| 692 | tag_footer2 = '| %s |' % tag_footer2.ljust(tag_len) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 693 | |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 694 | if include_stdout: |
| 695 | return '\n'.join([ |
| 696 | dash_pad, |
| 697 | tag_header, |
| 698 | dash_pad, |
Marc-Antoine Ruel | 3f9931a | 2017-11-03 14:34:49 -0400 | [diff] [blame] | 699 | (metadata.get('output') or '').rstrip(), |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 700 | dash_pad, |
| 701 | tag_footer1, |
| 702 | tag_footer2, |
| 703 | dash_pad, |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 704 | ]) |
Marc-Antoine Ruel | 793bff3 | 2019-04-18 17:50:48 +0000 | [diff] [blame] | 705 | return '\n'.join([ |
| 706 | dash_pad, |
| 707 | tag_header, |
| 708 | tag_footer2, |
| 709 | dash_pad, |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 710 | ]) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 711 | |
| 712 | |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 713 | def collect(swarming, task_ids, timeout, decorate, print_status_updates, |
| 714 | task_summary_json, task_output_dir, task_output_stdout, |
| 715 | include_perf, filepath_filter): |
maruel | a549078 | 2015-09-30 10:56:59 -0700 | [diff] [blame] | 716 | """Retrieves results of a Swarming task. |
| 717 | |
| 718 | Returns: |
| 719 | process exit code that should be returned to the user. |
| 720 | """ |
Takuto Ikuta | 1e6072c | 2018-11-06 20:42:43 +0000 | [diff] [blame] | 721 | |
| 722 | filter_cb = None |
| 723 | if filepath_filter: |
| 724 | filter_cb = re.compile(filepath_filter).match |
| 725 | |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 726 | # Collect summary JSON and output files (if task_output_dir is not None). |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 727 | output_collector = TaskOutputCollector(task_output_dir, task_output_stdout, |
| 728 | len(task_ids), filter_cb) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 729 | |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 730 | seen_shards = set() |
maruel | a549078 | 2015-09-30 10:56:59 -0700 | [diff] [blame] | 731 | exit_code = None |
Marc-Antoine Ruel | d59e807 | 2014-10-21 18:54:45 -0400 | [diff] [blame] | 732 | total_duration = 0 |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 733 | try: |
Marc-Antoine Ruel | 5e6ccdb | 2015-04-02 15:55:13 -0400 | [diff] [blame] | 734 | for index, metadata in yield_results( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 735 | swarming, |
| 736 | task_ids, |
| 737 | timeout, |
| 738 | None, |
| 739 | print_status_updates, |
| 740 | output_collector, |
| 741 | include_perf, |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 742 | (len(task_output_stdout) > 0), |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 743 | ): |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 744 | seen_shards.add(index) |
Vadim Shtayura | 473455a | 2014-05-14 15:22:35 -0700 | [diff] [blame] | 745 | |
Marc-Antoine Ruel | 5e6ccdb | 2015-04-02 15:55:13 -0400 | [diff] [blame] | 746 | # Default to failure if there was no process that even started. |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 747 | shard_exit_code = metadata.get('exit_code') |
| 748 | if shard_exit_code: |
maruel | a549078 | 2015-09-30 10:56:59 -0700 | [diff] [blame] | 749 | # It's encoded as a string, so bool('0') is True. |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 750 | shard_exit_code = int(shard_exit_code) |
maruel | a549078 | 2015-09-30 10:56:59 -0700 | [diff] [blame] | 751 | if shard_exit_code or exit_code is None: |
Marc-Antoine Ruel | 4e6b73d | 2014-10-03 18:00:05 -0400 | [diff] [blame] | 752 | exit_code = shard_exit_code |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 753 | total_duration += metadata.get('duration', 0) |
Vadim Shtayura | 473455a | 2014-05-14 15:22:35 -0700 | [diff] [blame] | 754 | |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 755 | if decorate: |
Lei Lei | 805a75d | 2020-10-08 16:31:55 +0000 | [diff] [blame] | 756 | s = decorate_shard_output(swarming, index, metadata, |
Lei Lei | 73a5f73 | 2020-03-23 20:36:14 +0000 | [diff] [blame] | 757 | "console" in task_output_stdout).encode( |
Lei Lei | 805a75d | 2020-10-08 16:31:55 +0000 | [diff] [blame] | 758 | 'utf-8', 'replace') |
| 759 | |
| 760 | # The default system encoding is ascii, which can not handle non-ascii |
| 761 | # characters, switch to use sys.stdout.buffer.write in Python3 to |
| 762 | # send utf-8 to stdout regardless of the console's encoding. |
| 763 | if six.PY3: |
| 764 | sys.stdout.buffer.write(s) |
| 765 | else: |
| 766 | print(s) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 767 | if len(seen_shards) < len(task_ids): |
| 768 | print('') |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 769 | else: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 770 | print('%s: %s %s' % (metadata.get( |
| 771 | 'bot_id', 'N/A'), metadata['task_id'], shard_exit_code)) |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 772 | if "console" in task_output_stdout and metadata['output']: |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 773 | output = metadata['output'].rstrip() |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 774 | if output: |
| 775 | print(''.join(' %s\n' % l for l in output.splitlines())) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 776 | finally: |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 777 | summary = output_collector.finalize() |
| 778 | if task_summary_json: |
| 779 | tools.write_json(task_summary_json, summary, False) |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 780 | |
Marc-Antoine Ruel | d59e807 | 2014-10-21 18:54:45 -0400 | [diff] [blame] | 781 | if decorate and total_duration: |
| 782 | print('Total duration: %.1fs' % total_duration) |
| 783 | |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 784 | if len(seen_shards) != len(task_ids): |
| 785 | missing_shards = [x for x in range(len(task_ids)) if x not in seen_shards] |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 786 | print( |
| 787 | 'Results from some shards are missing: %s' % |
| 788 | ', '.join(map(str, missing_shards)), |
| 789 | file=sys.stderr) |
Vadim Shtayura | c524f51 | 2014-05-15 09:54:56 -0700 | [diff] [blame] | 790 | return 1 |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 791 | |
maruel | a549078 | 2015-09-30 10:56:59 -0700 | [diff] [blame] | 792 | return exit_code if exit_code is not None else 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 793 | |
| 794 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 795 | ### API management. |
| 796 | |
| 797 | |
| 798 | class APIError(Exception): |
| 799 | pass |
| 800 | |
| 801 | |
| 802 | def endpoints_api_discovery_apis(host): |
| 803 | """Uses Cloud Endpoints' API Discovery Service to returns metadata about all |
| 804 | the APIs exposed by a host. |
| 805 | |
| 806 | https://developers.google.com/discovery/v1/reference/apis/list |
| 807 | """ |
maruel | 380e326 | 2016-08-31 16:10:06 -0700 | [diff] [blame] | 808 | # Uses the real Cloud Endpoints. This needs to be fixed once the Cloud |
| 809 | # Endpoints version is turned down. |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 810 | data = net.url_read_json(host + '/_ah/api/discovery/v1/apis') |
| 811 | if data is None: |
| 812 | raise APIError('Failed to discover APIs on %s' % host) |
| 813 | out = {} |
| 814 | for api in data['items']: |
| 815 | if api['id'] == 'discovery:v1': |
| 816 | continue |
| 817 | # URL is of the following form: |
| 818 | # url = host + ( |
| 819 | # '/_ah/api/discovery/v1/apis/%s/%s/rest' % (api['id'], api['version']) |
| 820 | api_data = net.url_read_json(api['discoveryRestUrl']) |
| 821 | if api_data is None: |
| 822 | raise APIError('Failed to discover %s on %s' % (api['id'], host)) |
| 823 | out[api['id']] = api_data |
| 824 | return out |
| 825 | |
| 826 | |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 827 | def get_yielder(base_url, limit): |
| 828 | """Returns the first query and a function that yields following items.""" |
| 829 | CHUNK_SIZE = 250 |
| 830 | |
| 831 | url = base_url |
| 832 | if limit: |
| 833 | url += '%slimit=%d' % ('&' if '?' in url else '?', min(CHUNK_SIZE, limit)) |
| 834 | data = net.url_read_json(url) |
| 835 | if data is None: |
| 836 | # TODO(maruel): Do basic diagnostic. |
| 837 | raise Failure('Failed to access %s' % url) |
| 838 | org_cursor = data.pop('cursor', None) |
| 839 | org_total = len(data.get('items') or []) |
| 840 | logging.info('get_yielder(%s) returning %d items', base_url, org_total) |
| 841 | if not org_cursor or not org_total: |
| 842 | # This is not an iterable resource. |
| 843 | return data, lambda: [] |
| 844 | |
| 845 | def yielder(): |
| 846 | cursor = org_cursor |
| 847 | total = org_total |
| 848 | # Some items support cursors. Try to get automatically if cursors are needed |
| 849 | # by looking at the 'cursor' items. |
| 850 | while cursor and (not limit or total < limit): |
| 851 | merge_char = '&' if '?' in base_url else '?' |
Marc-Antoine Ruel | ad8cabe | 2019-10-10 23:24:26 +0000 | [diff] [blame] | 852 | url = base_url + '%scursor=%s' % (merge_char, urllib.parse.quote(cursor)) |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 853 | if limit: |
| 854 | url += '&limit=%d' % min(CHUNK_SIZE, limit - total) |
| 855 | new = net.url_read_json(url) |
| 856 | if new is None: |
| 857 | raise Failure('Failed to access %s' % url) |
| 858 | cursor = new.get('cursor') |
| 859 | new_items = new.get('items') |
| 860 | nb_items = len(new_items or []) |
| 861 | total += nb_items |
| 862 | logging.info('get_yielder(%s) yielding %d items', base_url, nb_items) |
| 863 | yield new_items |
| 864 | |
| 865 | return data, yielder |
| 866 | |
| 867 | |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 868 | ### Commands. |
| 869 | |
| 870 | |
| 871 | def abort_task(_swarming, _manifest): |
| 872 | """Given a task manifest that was triggered, aborts its execution.""" |
| 873 | # TODO(vadimsh): No supported by the server yet. |
| 874 | |
| 875 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 876 | def add_filter_options(parser): |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 877 | parser.filter_group = optparse.OptionGroup(parser, 'Bot selection') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 878 | parser.filter_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 879 | '-d', |
| 880 | '--dimension', |
| 881 | default=[], |
| 882 | action='append', |
| 883 | nargs=2, |
| 884 | dest='dimensions', |
| 885 | metavar='FOO bar', |
Marc-Antoine Ruel | 92f3242 | 2013-11-06 18:12:13 -0500 | [diff] [blame] | 886 | help='dimension to filter on') |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 887 | parser.filter_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 888 | '--optional-dimension', |
| 889 | default=[], |
| 890 | action='append', |
| 891 | nargs=3, |
| 892 | dest='optional_dimensions', |
| 893 | metavar='key value expiration', |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 894 | help='optional dimensions which will result in additional task slices ') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 895 | parser.add_option_group(parser.filter_group) |
| 896 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 897 | |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 898 | def _validate_filter_option(parser, key, value, expiration, argname): |
| 899 | if ':' in key: |
| 900 | parser.error('%s key cannot contain ":"' % argname) |
| 901 | if key.strip() != key: |
| 902 | parser.error('%s key has whitespace' % argname) |
| 903 | if not key: |
| 904 | parser.error('%s key is empty' % argname) |
| 905 | |
| 906 | if value.strip() != value: |
| 907 | parser.error('%s value has whitespace' % argname) |
| 908 | if not value: |
| 909 | parser.error('%s value is empty' % argname) |
| 910 | |
| 911 | if expiration is not None: |
| 912 | try: |
| 913 | expiration = int(expiration) |
| 914 | except ValueError: |
| 915 | parser.error('%s expiration is not an integer' % argname) |
| 916 | if expiration <= 0: |
| 917 | parser.error('%s expiration should be positive' % argname) |
| 918 | if expiration % 60 != 0: |
| 919 | parser.error('%s expiration is not divisible by 60' % argname) |
| 920 | |
| 921 | |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 922 | def process_filter_options(parser, options): |
| 923 | for key, value in options.dimensions: |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 924 | _validate_filter_option(parser, key, value, None, 'dimension') |
| 925 | for key, value, exp in options.optional_dimensions: |
| 926 | _validate_filter_option(parser, key, value, exp, 'optional-dimension') |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 927 | options.dimensions.sort() |
| 928 | |
| 929 | |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 930 | def add_trigger_options(parser): |
| 931 | """Adds all options to trigger a task on Swarming.""" |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 932 | isolateserver.add_isolate_server_options(parser) |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 933 | add_filter_options(parser) |
| 934 | |
Marc-Antoine Ruel | 1f835c7 | 2018-05-25 12:29:42 -0400 | [diff] [blame] | 935 | group = optparse.OptionGroup(parser, 'TaskSlice properties') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 936 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 937 | '-s', |
| 938 | '--isolated', |
| 939 | metavar='HASH', |
Marc-Antoine Ruel | 185ded4 | 2015-01-28 20:49:18 -0500 | [diff] [blame] | 940 | help='Hash of the .isolated to grab from the isolate server') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 941 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 942 | '-e', |
| 943 | '--env', |
| 944 | default=[], |
| 945 | action='append', |
| 946 | nargs=2, |
| 947 | metavar='FOO bar', |
Vadim Shtayura | b450c60 | 2014-05-12 19:23:25 -0700 | [diff] [blame] | 948 | help='Environment variables to set') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 949 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 950 | '--env-prefix', |
| 951 | default=[], |
| 952 | action='append', |
| 953 | nargs=2, |
Robert Iannucci | bf5f84c | 2017-11-22 12:56:50 -0800 | [diff] [blame] | 954 | metavar='VAR local/path', |
| 955 | help='Prepend task-relative `local/path` to the task\'s VAR environment ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 956 | 'variable using os-appropriate pathsep character. Can be specified ' |
| 957 | 'multiple times for the same VAR to add multiple paths.') |
Robert Iannucci | bf5f84c | 2017-11-22 12:56:50 -0800 | [diff] [blame] | 958 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 959 | '--idempotent', |
| 960 | action='store_true', |
| 961 | default=False, |
Marc-Antoine Ruel | 0219639 | 2014-10-17 16:29:43 -0400 | [diff] [blame] | 962 | help='When set, the server will actively try to find a previous task ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 963 | 'with the same parameter and return this result instead if possible') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 964 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 965 | '--secret-bytes-path', |
| 966 | metavar='FILE', |
Stephen Martinis | f391c77 | 2019-02-01 01:22:12 +0000 | [diff] [blame] | 967 | help='The optional path to a file containing the secret_bytes to use ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 968 | 'with this task.') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 969 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 970 | '--hard-timeout', |
| 971 | type='int', |
| 972 | default=60 * 60, |
| 973 | metavar='SECS', |
Marc-Antoine Ruel | 2f6581a | 2014-10-03 11:09:53 -0400 | [diff] [blame] | 974 | help='Seconds to allow the task to complete.') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 975 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 976 | '--io-timeout', |
| 977 | type='int', |
| 978 | default=20 * 60, |
| 979 | metavar='SECS', |
Marc-Antoine Ruel | 2f6581a | 2014-10-03 11:09:53 -0400 | [diff] [blame] | 980 | help='Seconds to allow the task to be silent.') |
Marc-Antoine Ruel | 7f61a4d | 2019-05-22 20:10:07 +0000 | [diff] [blame] | 981 | containment_choices = ('NONE', 'AUTO', 'JOB_OBJECT') |
| 982 | parser.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 983 | '--containment-type', |
| 984 | default='NONE', |
| 985 | metavar='NONE', |
Marc-Antoine Ruel | 7f61a4d | 2019-05-22 20:10:07 +0000 | [diff] [blame] | 986 | choices=containment_choices, |
| 987 | help='Containment to use; one of: %s' % ', '.join(containment_choices)) |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 988 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 989 | '--raw-cmd', |
| 990 | action='store_true', |
| 991 | default=False, |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 992 | help='When set, the command after -- is used as-is without run_isolated. ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 993 | 'In this case, the .isolated file is expected to not have a command') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 994 | group.add_option( |
Marc-Antoine Ruel | ba1bf22 | 2017-12-21 21:41:01 -0500 | [diff] [blame] | 995 | '--relative-cwd', |
| 996 | help='Ignore the isolated \'relative_cwd\' and use this one instead; ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 997 | 'requires --raw-cmd') |
Marc-Antoine Ruel | ba1bf22 | 2017-12-21 21:41:01 -0500 | [diff] [blame] | 998 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 999 | '--cipd-package', |
| 1000 | action='append', |
| 1001 | default=[], |
| 1002 | metavar='PKG', |
maruel | 5475ba6 | 2017-05-31 15:35:47 -0700 | [diff] [blame] | 1003 | help='CIPD packages to install on the Swarming bot. Uses the format: ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1004 | 'path:package_name:version') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1005 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1006 | '--named-cache', |
| 1007 | action='append', |
| 1008 | nargs=2, |
| 1009 | default=[], |
maruel | 5475ba6 | 2017-05-31 15:35:47 -0700 | [diff] [blame] | 1010 | metavar='NAME RELPATH', |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1011 | help='"<name> <relpath>" items to keep a persistent bot managed cache') |
| 1012 | group.add_option( |
vadimsh | 93d167c | 2016-09-13 11:31:51 -0700 | [diff] [blame] | 1013 | '--service-account', |
Vadim Shtayura | 2d83a94 | 2017-08-14 17:41:24 -0700 | [diff] [blame] | 1014 | help='Email of a service account to run the task as, or literal "bot" ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1015 | 'string to indicate that the task should use the same account the ' |
| 1016 | 'bot itself is using to authenticate to Swarming. Don\'t use task ' |
| 1017 | 'service accounts if not given (default).') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1018 | group.add_option( |
Robert Iannucci | fafa735 | 2018-06-13 17:08:17 +0000 | [diff] [blame] | 1019 | '--pool-task-template', |
| 1020 | choices=('AUTO', 'CANARY_PREFER', 'CANARY_NEVER', 'SKIP'), |
| 1021 | default='AUTO', |
| 1022 | help='Set how you want swarming to apply the pool\'s TaskTemplate. ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1023 | 'By default, the pool\'s TaskTemplate is automatically selected, ' |
| 1024 | 'according the pool configuration on the server. Choices are: ' |
| 1025 | 'AUTO, CANARY_PREFER, CANARY_NEVER, and SKIP (default: AUTO).') |
Robert Iannucci | fafa735 | 2018-06-13 17:08:17 +0000 | [diff] [blame] | 1026 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1027 | '-o', |
| 1028 | '--output', |
| 1029 | action='append', |
| 1030 | default=[], |
| 1031 | metavar='PATH', |
maruel | 5475ba6 | 2017-05-31 15:35:47 -0700 | [diff] [blame] | 1032 | help='A list of files to return in addition to those written to ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1033 | '${ISOLATED_OUTDIR}. An error will occur if a file specified by' |
| 1034 | 'this option is also written directly to ${ISOLATED_OUTDIR}.') |
Marc-Antoine Ruel | 1f835c7 | 2018-05-25 12:29:42 -0400 | [diff] [blame] | 1035 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1036 | '--wait-for-capacity', |
| 1037 | action='store_true', |
| 1038 | default=False, |
Marc-Antoine Ruel | 1f835c7 | 2018-05-25 12:29:42 -0400 | [diff] [blame] | 1039 | help='Instructs to leave the task PENDING even if there\'s no known bot ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1040 | 'that could run this task, otherwise the task will be denied with ' |
| 1041 | 'NO_RESOURCE') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1042 | parser.add_option_group(group) |
| 1043 | |
Marc-Antoine Ruel | 1f835c7 | 2018-05-25 12:29:42 -0400 | [diff] [blame] | 1044 | group = optparse.OptionGroup(parser, 'TaskRequest details') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1045 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1046 | '--priority', |
| 1047 | type='int', |
| 1048 | default=200, |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1049 | help='The lower value, the more important the task is') |
| 1050 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1051 | '-T', |
| 1052 | '--task-name', |
| 1053 | metavar='NAME', |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1054 | help='Display name of the task. Defaults to ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1055 | '<base_name>/<dimensions>/<isolated hash>/<timestamp> if an ' |
| 1056 | 'isolated file is provided, if a hash is provided, it defaults to ' |
| 1057 | '<user>/<dimensions>/<isolated hash>/<timestamp>') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1058 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1059 | '--tags', |
| 1060 | action='append', |
| 1061 | default=[], |
| 1062 | metavar='FOO:BAR', |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1063 | help='Tags to assign to the task.') |
| 1064 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1065 | '--user', |
| 1066 | default='', |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1067 | help='User associated with the task. Defaults to authenticated user on ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1068 | 'the server.') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1069 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1070 | '--expiration', |
| 1071 | type='int', |
| 1072 | default=6 * 60 * 60, |
| 1073 | metavar='SECS', |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1074 | help='Seconds to allow the task to be pending for a bot to run before ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1075 | 'this task request expires.') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1076 | group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1077 | '--deadline', type='int', dest='expiration', help=optparse.SUPPRESS_HELP) |
Junji Watanabe | 71bbaef | 2020-07-21 08:55:37 +0000 | [diff] [blame] | 1078 | group.add_option( |
| 1079 | '--realm', |
| 1080 | dest='realm', |
| 1081 | metavar='REALM', |
| 1082 | help='Realm associated with the task.') |
Scott Lee | 44c13d7 | 2020-09-14 06:09:50 +0000 | [diff] [blame] | 1083 | group.add_option( |
| 1084 | '--resultdb', |
| 1085 | action='store_true', |
| 1086 | default=False, |
| 1087 | help='When set, the task is created with ResultDB enabled.') |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1088 | parser.add_option_group(group) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1089 | |
| 1090 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1091 | def process_trigger_options(parser, options, args): |
Marc-Antoine Ruel | 3a030bc | 2018-04-23 10:31:25 -0400 | [diff] [blame] | 1092 | """Processes trigger options and does preparatory steps. |
| 1093 | |
| 1094 | Returns: |
| 1095 | NewTaskRequest instance. |
| 1096 | """ |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1097 | process_filter_options(parser, options) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1098 | options.env = dict(options.env) |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1099 | if args and args[0] == '--': |
| 1100 | args = args[1:] |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1101 | |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1102 | if not options.dimensions: |
| 1103 | parser.error('Please at least specify one --dimension') |
Marc-Antoine Ruel | 33d198c | 2018-11-27 21:12:16 +0000 | [diff] [blame] | 1104 | if not any(k == 'pool' for k, _v in options.dimensions): |
| 1105 | parser.error('You must specify --dimension pool <value>') |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1106 | if not all(len(t.split(':', 1)) == 2 for t in options.tags): |
| 1107 | parser.error('--tags must be in the format key:value') |
| 1108 | if options.raw_cmd and not args: |
| 1109 | parser.error( |
| 1110 | 'Arguments with --raw-cmd should be passed after -- as command ' |
| 1111 | 'delimiter.') |
| 1112 | if options.isolate_server and not options.namespace: |
| 1113 | parser.error( |
| 1114 | '--namespace must be a valid value when --isolate-server is used') |
| 1115 | if not options.isolated and not options.raw_cmd: |
| 1116 | parser.error('Specify at least one of --raw-cmd or --isolated or both') |
| 1117 | |
| 1118 | # Isolated |
| 1119 | # --isolated is required only if --raw-cmd wasn't provided. |
| 1120 | # TODO(maruel): --isolate-server may be optional as Swarming may have its own |
| 1121 | # preferred server. |
Takuto Ikuta | ae767b3 | 2020-05-11 01:22:19 +0000 | [diff] [blame] | 1122 | isolateserver.process_isolate_server_options(parser, options, |
| 1123 | not options.raw_cmd) |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1124 | inputs_ref = None |
| 1125 | if options.isolate_server: |
| 1126 | inputs_ref = FilesRef( |
| 1127 | isolated=options.isolated, |
| 1128 | isolatedserver=options.isolate_server, |
| 1129 | namespace=options.namespace) |
| 1130 | |
| 1131 | # Command |
| 1132 | command = None |
| 1133 | extra_args = None |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1134 | if options.raw_cmd: |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1135 | command = args |
Marc-Antoine Ruel | ba1bf22 | 2017-12-21 21:41:01 -0500 | [diff] [blame] | 1136 | if options.relative_cwd: |
| 1137 | a = os.path.normpath(os.path.abspath(options.relative_cwd)) |
| 1138 | if not a.startswith(os.getcwd()): |
| 1139 | parser.error( |
| 1140 | '--relative-cwd must not try to escape the working directory') |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1141 | else: |
Marc-Antoine Ruel | ba1bf22 | 2017-12-21 21:41:01 -0500 | [diff] [blame] | 1142 | if options.relative_cwd: |
| 1143 | parser.error('--relative-cwd requires --raw-cmd') |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1144 | extra_args = args |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1145 | |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1146 | # CIPD |
borenet | 02f772b | 2016-06-22 12:42:19 -0700 | [diff] [blame] | 1147 | cipd_packages = [] |
| 1148 | for p in options.cipd_package: |
| 1149 | split = p.split(':', 2) |
| 1150 | if len(split) != 3: |
| 1151 | parser.error('CIPD packages must take the form: path:package:version') |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1152 | cipd_packages.append( |
| 1153 | CipdPackage(package_name=split[1], path=split[0], version=split[2])) |
borenet | 02f772b | 2016-06-22 12:42:19 -0700 | [diff] [blame] | 1154 | cipd_input = None |
| 1155 | if cipd_packages: |
| 1156 | cipd_input = CipdInput( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1157 | client_package=None, packages=cipd_packages, server=None) |
borenet | 02f772b | 2016-06-22 12:42:19 -0700 | [diff] [blame] | 1158 | |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1159 | # Secrets |
iannucci | dc80dfb | 2016-10-28 12:50:20 -0700 | [diff] [blame] | 1160 | secret_bytes = None |
| 1161 | if options.secret_bytes_path: |
Marc-Antoine Ruel | 5c98fa7 | 2018-05-18 12:19:59 -0400 | [diff] [blame] | 1162 | with open(options.secret_bytes_path, 'rb') as f: |
iannucci | dc80dfb | 2016-10-28 12:50:20 -0700 | [diff] [blame] | 1163 | secret_bytes = f.read().encode('base64') |
| 1164 | |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1165 | # Named caches |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 1166 | caches = [{ |
| 1167 | u'name': six.text_type(i[0]), |
| 1168 | u'path': six.text_type(i[1]) |
| 1169 | } for i in options.named_cache] |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1170 | |
Robert Iannucci | bf5f84c | 2017-11-22 12:56:50 -0800 | [diff] [blame] | 1171 | env_prefixes = {} |
| 1172 | for k, v in options.env_prefix: |
| 1173 | env_prefixes.setdefault(k, []).append(v) |
| 1174 | |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1175 | # Get dimensions into the key/value format we can manipulate later. |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1176 | orig_dims = [{ |
| 1177 | 'key': key, |
| 1178 | 'value': value |
| 1179 | } for key, value in options.dimensions] |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1180 | orig_dims.sort(key=lambda x: (x['key'], x['value'])) |
| 1181 | |
| 1182 | # Construct base properties that we will use for all the slices, adding in |
| 1183 | # optional dimensions for the fallback slices. |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1184 | properties = TaskProperties( |
maruel | 681d680 | 2017-01-17 16:56:03 -0800 | [diff] [blame] | 1185 | caches=caches, |
borenet | 02f772b | 2016-06-22 12:42:19 -0700 | [diff] [blame] | 1186 | cipd_input=cipd_input, |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1187 | command=command, |
Marc-Antoine Ruel | 89669dc | 2019-05-01 14:01:08 +0000 | [diff] [blame] | 1188 | containment=Containment( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1189 | containment_type=options.containment_type, |
Marc-Antoine Ruel | 89669dc | 2019-05-01 14:01:08 +0000 | [diff] [blame] | 1190 | ), |
Marc-Antoine Ruel | ba1bf22 | 2017-12-21 21:41:01 -0500 | [diff] [blame] | 1191 | relative_cwd=options.relative_cwd, |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1192 | dimensions=orig_dims, |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1193 | env=options.env, |
Marc-Antoine Ruel | 04903a3 | 2019-10-09 21:09:25 +0000 | [diff] [blame] | 1194 | env_prefixes=[StringListPair(k, v) for k, v in env_prefixes.items()], |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1195 | execution_timeout_secs=options.hard_timeout, |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1196 | extra_args=extra_args, |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1197 | grace_period_secs=30, |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1198 | idempotent=options.idempotent, |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1199 | inputs_ref=inputs_ref, |
aludwin | cc5524e | 2016-10-28 10:25:24 -0700 | [diff] [blame] | 1200 | io_timeout_secs=options.io_timeout, |
iannucci | dc80dfb | 2016-10-28 12:50:20 -0700 | [diff] [blame] | 1201 | outputs=options.output, |
| 1202 | secret_bytes=secret_bytes) |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1203 | |
| 1204 | slices = [] |
| 1205 | |
| 1206 | # Group the optional dimensions by expiration. |
| 1207 | dims_by_exp = {} |
| 1208 | for key, value, exp_secs in options.optional_dimensions: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1209 | dims_by_exp.setdefault(int(exp_secs), []).append({ |
| 1210 | 'key': key, |
| 1211 | 'value': value |
| 1212 | }) |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1213 | |
| 1214 | # Create the optional slices with expiration deltas, we fix up the properties |
| 1215 | # below. |
| 1216 | last_exp = 0 |
| 1217 | for expiration_secs in sorted(dims_by_exp): |
| 1218 | t = TaskSlice( |
| 1219 | expiration_secs=expiration_secs - last_exp, |
| 1220 | properties=properties, |
| 1221 | wait_for_capacity=False) |
| 1222 | slices.append(t) |
| 1223 | last_exp = expiration_secs |
| 1224 | |
| 1225 | # Add back in the default slice (the last one). |
| 1226 | exp = max(int(options.expiration) - last_exp, 60) |
| 1227 | base_task_slice = TaskSlice( |
| 1228 | expiration_secs=exp, |
Marc-Antoine Ruel | 1f835c7 | 2018-05-25 12:29:42 -0400 | [diff] [blame] | 1229 | properties=properties, |
| 1230 | wait_for_capacity=options.wait_for_capacity) |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1231 | slices.append(base_task_slice) |
| 1232 | |
Brad Hall | 7f463e6 | 2018-11-16 16:13:30 +0000 | [diff] [blame] | 1233 | # Add optional dimensions to the task slices, replacing a dimension that |
| 1234 | # has the same key if it is a dimension where repeating isn't valid (otherwise |
| 1235 | # we append it). Currently the only dimension we can repeat is "caches"; the |
| 1236 | # rest (os, cpu, etc) shouldn't be repeated. |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1237 | extra_dims = [] |
Marc-Antoine Ruel | 04903a3 | 2019-10-09 21:09:25 +0000 | [diff] [blame] | 1238 | for i, (_, kvs) in enumerate(sorted(dims_by_exp.items(), reverse=True)): |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1239 | dims = list(orig_dims) |
Brad Hall | 7f463e6 | 2018-11-16 16:13:30 +0000 | [diff] [blame] | 1240 | # Replace or append the key/value pairs for this expiration in extra_dims; |
| 1241 | # we keep extra_dims around because we are iterating backwards and filling |
| 1242 | # in slices with shorter expirations. Dimensions expire as time goes on so |
| 1243 | # the slices that expire earlier will generally have more dimensions. |
| 1244 | for kv in kvs: |
| 1245 | if kv['key'] == 'caches': |
| 1246 | extra_dims.append(kv) |
| 1247 | else: |
| 1248 | extra_dims = [x for x in extra_dims if x['key'] != kv['key']] + [kv] |
| 1249 | # Then, add all the optional dimensions to the original dimension set, again |
| 1250 | # replacing if needed. |
| 1251 | for kv in extra_dims: |
| 1252 | if kv['key'] == 'caches': |
| 1253 | dims.append(kv) |
| 1254 | else: |
| 1255 | dims = [x for x in dims if x['key'] != kv['key']] + [kv] |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1256 | dims.sort(key=lambda x: (x['key'], x['value'])) |
| 1257 | slice_properties = properties._replace(dimensions=dims) |
| 1258 | slices[-2 - i] = slices[-2 - i]._replace(properties=slice_properties) |
| 1259 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1260 | return NewTaskRequest( |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1261 | name=default_task_name(options), |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1262 | parent_task_id=os.environ.get('SWARMING_TASK_ID', ''), |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1263 | priority=options.priority, |
Brad Hall | f78187a | 2018-10-19 17:08:55 +0000 | [diff] [blame] | 1264 | task_slices=slices, |
Vadim Shtayura | 2d83a94 | 2017-08-14 17:41:24 -0700 | [diff] [blame] | 1265 | service_account=options.service_account, |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1266 | tags=options.tags, |
Robert Iannucci | fafa735 | 2018-06-13 17:08:17 +0000 | [diff] [blame] | 1267 | user=options.user, |
Junji Watanabe | 71bbaef | 2020-07-21 08:55:37 +0000 | [diff] [blame] | 1268 | pool_task_template=options.pool_task_template, |
Scott Lee | 44c13d7 | 2020-09-14 06:09:50 +0000 | [diff] [blame] | 1269 | realm=options.realm, |
| 1270 | resultdb={'enable': options.resultdb}) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1271 | |
| 1272 | |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 1273 | class TaskOutputStdoutOption(optparse.Option): |
| 1274 | """Where to output the each task's console output (stderr/stdout). |
| 1275 | |
| 1276 | The output will be; |
| 1277 | none - not be downloaded. |
| 1278 | json - stored in summary.json file *only*. |
| 1279 | console - shown on stdout *only*. |
| 1280 | all - stored in summary.json and shown on stdout. |
| 1281 | """ |
| 1282 | |
| 1283 | choices = ['all', 'json', 'console', 'none'] |
| 1284 | |
| 1285 | def __init__(self, *args, **kw): |
| 1286 | optparse.Option.__init__( |
| 1287 | self, |
| 1288 | *args, |
| 1289 | choices=self.choices, |
Marc-Antoine Ruel | 2848884 | 2017-09-12 18:09:17 -0400 | [diff] [blame] | 1290 | default=['console', 'json'], |
Marc-Antoine Ruel | 793bff3 | 2019-04-18 17:50:48 +0000 | [diff] [blame] | 1291 | help=re.sub(r'\s\s*', ' ', self.__doc__), |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 1292 | **kw) |
| 1293 | |
| 1294 | def convert_value(self, opt, value): |
| 1295 | if value not in self.choices: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1296 | raise optparse.OptionValueError( |
| 1297 | "%s must be one of %s not %r" % |
| 1298 | (self.get_opt_string(), self.choices, value)) |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 1299 | stdout_to = [] |
| 1300 | if value == 'all': |
Marc-Antoine Ruel | 2848884 | 2017-09-12 18:09:17 -0400 | [diff] [blame] | 1301 | stdout_to = ['console', 'json'] |
Tim 'mithro' Ansell | 5e8001d | 2017-09-08 09:32:52 +1000 | [diff] [blame] | 1302 | elif value != 'none': |
| 1303 | stdout_to = [value] |
| 1304 | return stdout_to |
| 1305 | |
| 1306 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1307 | def add_collect_options(parser): |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 1308 | parser.server_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1309 | '-t', |
| 1310 | '--timeout', |
| 1311 | type='float', |
| 1312 | default=0., |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1313 | help='Timeout to wait for result, set to -1 for no timeout and get ' |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1314 | 'current state; defaults to waiting until the task completes') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 1315 | parser.group_logging.add_option( |
| 1316 | '--decorate', action='store_true', help='Decorate output') |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 1317 | parser.group_logging.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1318 | '--print-status-updates', |
| 1319 | action='store_true', |
Vadim Shtayura | 86a2cef | 2014-04-18 11:13:39 -0700 | [diff] [blame] | 1320 | help='Print periodic status updates') |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 1321 | parser.task_output_group = optparse.OptionGroup(parser, 'Task output') |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 1322 | parser.task_output_group.add_option( |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 1323 | '--task-summary-json', |
| 1324 | metavar='FILE', |
| 1325 | help='Dump a summary of task results to this file as json. It contains ' |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1326 | 'only shards statuses as know to server directly. Any output files ' |
| 1327 | 'emitted by the task can be collected by using --task-output-dir') |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 1328 | parser.task_output_group.add_option( |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 1329 | '--task-output-dir', |
Vadim Shtayura | c8437bf | 2014-07-09 19:45:36 -0700 | [diff] [blame] | 1330 | metavar='DIR', |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 1331 | help='Directory to put task results into. When the task finishes, this ' |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1332 | 'directory contains per-shard directory with output files produced ' |
| 1333 | 'by shards: <task-output-dir>/<zero-based-shard-index>/.') |
maruel | 9531ce0 | 2016-04-13 06:11:23 -0700 | [diff] [blame] | 1334 | parser.task_output_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1335 | TaskOutputStdoutOption('--task-output-stdout')) |
Takuto Ikuta | 1e6072c | 2018-11-06 20:42:43 +0000 | [diff] [blame] | 1336 | parser.task_output_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1337 | '--filepath-filter', |
| 1338 | help='This is regexp filter used to specify downloaded filepath when ' |
| 1339 | 'collecting isolated output.') |
| 1340 | parser.task_output_group.add_option( |
| 1341 | '--perf', |
| 1342 | action='store_true', |
| 1343 | default=False, |
maruel | 9531ce0 | 2016-04-13 06:11:23 -0700 | [diff] [blame] | 1344 | help='Includes performance statistics') |
Vadim Shtayura | e3fbd10 | 2014-04-29 17:05:21 -0700 | [diff] [blame] | 1345 | parser.add_option_group(parser.task_output_group) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1346 | |
| 1347 | |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1348 | def process_collect_options(parser, options): |
| 1349 | # Only negative -1 is allowed, disallow other negative values. |
| 1350 | if options.timeout != -1 and options.timeout < 0: |
| 1351 | parser.error('Invalid --timeout value') |
| 1352 | |
| 1353 | |
Marc-Antoine Ruel | 13e7c88 | 2015-03-26 18:19:10 -0400 | [diff] [blame] | 1354 | @subcommand.usage('bots...') |
| 1355 | def CMDbot_delete(parser, args): |
| 1356 | """Forcibly deletes bots from the Swarming server.""" |
| 1357 | parser.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1358 | '-f', |
| 1359 | '--force', |
| 1360 | action='store_true', |
Marc-Antoine Ruel | 13e7c88 | 2015-03-26 18:19:10 -0400 | [diff] [blame] | 1361 | help='Do not prompt for confirmation') |
| 1362 | options, args = parser.parse_args(args) |
| 1363 | if not args: |
maruel | fd0a90c | 2016-06-10 11:51:10 -0700 | [diff] [blame] | 1364 | parser.error('Please specify bots to delete') |
Marc-Antoine Ruel | 13e7c88 | 2015-03-26 18:19:10 -0400 | [diff] [blame] | 1365 | |
| 1366 | bots = sorted(args) |
| 1367 | if not options.force: |
| 1368 | print('Delete the following bots?') |
| 1369 | for bot in bots: |
| 1370 | print(' %s' % bot) |
| 1371 | if raw_input('Continue? [y/N] ') not in ('y', 'Y'): |
| 1372 | print('Goodbye.') |
| 1373 | return 1 |
| 1374 | |
| 1375 | result = 0 |
| 1376 | for bot in bots: |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 1377 | url = '%s/_ah/api/swarming/v1/bot/%s/delete' % (options.swarming, bot) |
vadimsh | e4c0e24 | 2015-09-30 11:53:54 -0700 | [diff] [blame] | 1378 | if net.url_read_json(url, data={}, method='POST') is None: |
| 1379 | print('Deleting %s failed. Probably already gone' % bot) |
Marc-Antoine Ruel | 13e7c88 | 2015-03-26 18:19:10 -0400 | [diff] [blame] | 1380 | result = 1 |
| 1381 | return result |
| 1382 | |
| 1383 | |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1384 | def CMDbots(parser, args): |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 1385 | """Returns information about the bots connected to the Swarming server.""" |
| 1386 | add_filter_options(parser) |
| 1387 | parser.filter_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1388 | '--dead-only', |
| 1389 | action='store_true', |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1390 | help='Filter out bots alive, useful to reap them and reimage broken bots') |
Marc-Antoine Ruel | 2808311 | 2014-03-13 16:34:04 -0400 | [diff] [blame] | 1391 | parser.filter_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1392 | '-k', |
| 1393 | '--keep-dead', |
| 1394 | action='store_true', |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1395 | help='Keep both dead and alive bots') |
| 1396 | parser.filter_group.add_option( |
| 1397 | '--busy', action='store_true', help='Keep only busy bots') |
| 1398 | parser.filter_group.add_option( |
| 1399 | '--idle', action='store_true', help='Keep only idle bots') |
| 1400 | parser.filter_group.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1401 | '-b', '--bare', action='store_true', help='Do not print out dimensions') |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 1402 | options, args = parser.parse_args(args) |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1403 | process_filter_options(parser, options) |
Marc-Antoine Ruel | 2808311 | 2014-03-13 16:34:04 -0400 | [diff] [blame] | 1404 | |
| 1405 | if options.keep_dead and options.dead_only: |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1406 | parser.error('Use only one of --keep-dead or --dead-only') |
| 1407 | if options.busy and options.idle: |
| 1408 | parser.error('Use only one of --busy or --idle') |
Vadim Shtayura | 6b555c1 | 2014-07-23 16:22:18 -0700 | [diff] [blame] | 1409 | |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 1410 | url = options.swarming + '/_ah/api/swarming/v1/bots/list?' |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1411 | values = [] |
| 1412 | if options.dead_only: |
| 1413 | values.append(('is_dead', 'TRUE')) |
| 1414 | elif options.keep_dead: |
| 1415 | values.append(('is_dead', 'NONE')) |
| 1416 | else: |
| 1417 | values.append(('is_dead', 'FALSE')) |
Marc-Antoine Ruel | c6c579e | 2014-09-08 18:43:45 -0400 | [diff] [blame] | 1418 | |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1419 | if options.busy: |
| 1420 | values.append(('is_busy', 'TRUE')) |
| 1421 | elif options.idle: |
| 1422 | values.append(('is_busy', 'FALSE')) |
| 1423 | else: |
| 1424 | values.append(('is_busy', 'NONE')) |
| 1425 | |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1426 | for key, value in options.dimensions: |
| 1427 | values.append(('dimensions', '%s:%s' % (key, value))) |
Marc-Antoine Ruel | ad8cabe | 2019-10-10 23:24:26 +0000 | [diff] [blame] | 1428 | url += urllib.parse.urlencode(values) |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1429 | try: |
| 1430 | data, yielder = get_yielder(url, 0) |
| 1431 | bots = data.get('items') or [] |
| 1432 | for items in yielder(): |
| 1433 | if items: |
| 1434 | bots.extend(items) |
| 1435 | except Failure as e: |
| 1436 | sys.stderr.write('\n%s\n' % e) |
| 1437 | return 1 |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1438 | for bot in natsort.natsorted(bots, key=lambda x: x['bot_id']): |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1439 | print(bot['bot_id']) |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1440 | if not options.bare: |
| 1441 | dimensions = {i['key']: i.get('value') for i in bot.get('dimensions', {})} |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1442 | print(' %s' % json.dumps(dimensions, sort_keys=True)) |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1443 | if bot.get('task_id'): |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1444 | print(' task: %s' % bot['task_id']) |
Marc-Antoine Ruel | 819fb16 | 2014-03-12 16:38:26 -0400 | [diff] [blame] | 1445 | return 0 |
| 1446 | |
| 1447 | |
maruel | fd0a90c | 2016-06-10 11:51:10 -0700 | [diff] [blame] | 1448 | @subcommand.usage('task_id') |
| 1449 | def CMDcancel(parser, args): |
| 1450 | """Cancels a task.""" |
Marc-Antoine Ruel | 2e52c55 | 2018-03-26 19:27:36 -0400 | [diff] [blame] | 1451 | parser.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1452 | '-k', |
| 1453 | '--kill-running', |
| 1454 | action='store_true', |
| 1455 | default=False, |
Marc-Antoine Ruel | 2e52c55 | 2018-03-26 19:27:36 -0400 | [diff] [blame] | 1456 | help='Kill the task even if it was running') |
maruel | fd0a90c | 2016-06-10 11:51:10 -0700 | [diff] [blame] | 1457 | options, args = parser.parse_args(args) |
| 1458 | if not args: |
| 1459 | parser.error('Please specify the task to cancel') |
Marc-Antoine Ruel | 2e52c55 | 2018-03-26 19:27:36 -0400 | [diff] [blame] | 1460 | data = {'kill_running': options.kill_running} |
maruel | fd0a90c | 2016-06-10 11:51:10 -0700 | [diff] [blame] | 1461 | for task_id in args: |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 1462 | url = '%s/_ah/api/swarming/v1/task/%s/cancel' % (options.swarming, task_id) |
Marc-Antoine Ruel | 2e52c55 | 2018-03-26 19:27:36 -0400 | [diff] [blame] | 1463 | resp = net.url_read_json(url, data=data, method='POST') |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1464 | if resp is None: |
maruel | fd0a90c | 2016-06-10 11:51:10 -0700 | [diff] [blame] | 1465 | print('Deleting %s failed. Probably already gone' % task_id) |
| 1466 | return 1 |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1467 | logging.info('%s', resp) |
maruel | fd0a90c | 2016-06-10 11:51:10 -0700 | [diff] [blame] | 1468 | return 0 |
| 1469 | |
| 1470 | |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1471 | @subcommand.usage('--json file | task_id...') |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1472 | def CMDcollect(parser, args): |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1473 | """Retrieves results of one or multiple Swarming task by its ID. |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1474 | |
| 1475 | The result can be in multiple part if the execution was sharded. It can |
| 1476 | potentially have retries. |
| 1477 | """ |
| 1478 | add_collect_options(parser) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1479 | parser.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1480 | '-j', |
| 1481 | '--json', |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1482 | help='Load the task ids from .json as saved by trigger --dump-json') |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1483 | options, args = parser.parse_args(args) |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1484 | process_collect_options(parser, options) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1485 | if not args and not options.json: |
| 1486 | parser.error('Must specify at least one task id or --json.') |
| 1487 | if args and options.json: |
| 1488 | parser.error('Only use one of task id or --json.') |
| 1489 | |
| 1490 | if options.json: |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 1491 | options.json = six.text_type(os.path.abspath(options.json)) |
Marc-Antoine Ruel | 9025a78 | 2015-03-17 16:42:59 -0400 | [diff] [blame] | 1492 | try: |
maruel | 1ceb387 | 2015-10-14 06:10:44 -0700 | [diff] [blame] | 1493 | with fs.open(options.json, 'rb') as f: |
maruel | 71c61c8 | 2016-02-22 06:52:05 -0800 | [diff] [blame] | 1494 | data = json.load(f) |
| 1495 | except (IOError, ValueError): |
| 1496 | parser.error('Failed to open %s' % options.json) |
| 1497 | try: |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1498 | tasks = sorted(data['tasks'].values(), key=lambda x: x['shard_index']) |
maruel | 71c61c8 | 2016-02-22 06:52:05 -0800 | [diff] [blame] | 1499 | args = [t['task_id'] for t in tasks] |
| 1500 | except (KeyError, TypeError): |
| 1501 | parser.error('Failed to process %s' % options.json) |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1502 | if not options.timeout: |
Marc-Antoine Ruel | b73066b | 2018-04-19 20:16:55 -0400 | [diff] [blame] | 1503 | # Take in account all the task slices. |
| 1504 | offset = 0 |
| 1505 | for s in data['request']['task_slices']: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1506 | m = ( |
| 1507 | offset + s['properties']['execution_timeout_secs'] + |
| 1508 | s['expiration_secs']) |
Marc-Antoine Ruel | b73066b | 2018-04-19 20:16:55 -0400 | [diff] [blame] | 1509 | if m > options.timeout: |
| 1510 | options.timeout = m |
| 1511 | offset += s['expiration_secs'] |
Marc-Antoine Ruel | 9fc4261 | 2018-04-20 08:34:22 -0400 | [diff] [blame] | 1512 | options.timeout += 10. |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1513 | else: |
| 1514 | valid = frozenset('0123456789abcdef') |
| 1515 | if any(not valid.issuperset(task_id) for task_id in args): |
| 1516 | parser.error('Task ids are 0-9a-f.') |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1517 | |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1518 | try: |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1519 | return collect(options.swarming, args, options.timeout, options.decorate, |
| 1520 | options.print_status_updates, options.task_summary_json, |
| 1521 | options.task_output_dir, options.task_output_stdout, |
| 1522 | options.perf, options.filepath_filter) |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1523 | except Failure: |
| 1524 | on_error.report(None) |
| 1525 | return 1 |
| 1526 | |
| 1527 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1528 | @subcommand.usage('[method name]') |
Marc-Antoine Ruel | 833f5eb | 2018-04-25 16:49:40 -0400 | [diff] [blame] | 1529 | def CMDpost(parser, args): |
| 1530 | """Sends a JSON RPC POST to one API endpoint and prints out the raw result. |
| 1531 | |
| 1532 | Input data must be sent to stdin, result is printed to stdout. |
| 1533 | |
| 1534 | If HTTP response code >= 400, returns non-zero. |
| 1535 | """ |
| 1536 | options, args = parser.parse_args(args) |
| 1537 | if len(args) != 1: |
| 1538 | parser.error('Must specify only API name') |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 1539 | url = options.swarming + '/_ah/api/swarming/v1/' + args[0] |
Marc-Antoine Ruel | 833f5eb | 2018-04-25 16:49:40 -0400 | [diff] [blame] | 1540 | data = sys.stdin.read() |
| 1541 | try: |
| 1542 | resp = net.url_read(url, data=data, method='POST') |
| 1543 | except net.TimeoutError: |
| 1544 | sys.stderr.write('Timeout!\n') |
| 1545 | return 1 |
| 1546 | if not resp: |
| 1547 | sys.stderr.write('No response!\n') |
| 1548 | return 1 |
| 1549 | sys.stdout.write(resp) |
| 1550 | return 0 |
| 1551 | |
| 1552 | |
| 1553 | @subcommand.usage('[method name]') |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1554 | def CMDquery(parser, args): |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1555 | """Returns raw JSON information via an URL endpoint. Use 'query-list' to |
| 1556 | gather the list of API methods from the server. |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1557 | |
| 1558 | Examples: |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1559 | Raw task request and results: |
| 1560 | swarming.py query -S server-url.com task/123456/request |
| 1561 | swarming.py query -S server-url.com task/123456/result |
| 1562 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1563 | Listing all bots: |
maruel | 84e77aa | 2015-10-21 06:37:24 -0700 | [diff] [blame] | 1564 | swarming.py query -S server-url.com bots/list |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1565 | |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1566 | Listing last 10 tasks on a specific bot named 'bot1': |
| 1567 | swarming.py query -S server-url.com --limit 10 bot/bot1/tasks |
maruel | 84e77aa | 2015-10-21 06:37:24 -0700 | [diff] [blame] | 1568 | |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1569 | Listing last 10 tasks with tags os:Ubuntu-14.04 and pool:Chrome. Note that |
maruel | 84e77aa | 2015-10-21 06:37:24 -0700 | [diff] [blame] | 1570 | quoting is important!: |
| 1571 | swarming.py query -S server-url.com --limit 10 \\ |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1572 | 'tasks/list?tags=os:Ubuntu-14.04&tags=pool:Chrome' |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1573 | """ |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1574 | parser.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1575 | '-L', |
| 1576 | '--limit', |
| 1577 | type='int', |
| 1578 | default=200, |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1579 | help='Limit to enforce on limitless items (like number of tasks); ' |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1580 | 'default=%default') |
Paweł Hajdan, Jr | 53ef013 | 2015-03-20 17:49:18 +0100 | [diff] [blame] | 1581 | parser.add_option( |
| 1582 | '--json', help='Path to JSON output file (otherwise prints to stdout)') |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1583 | parser.add_option( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1584 | '--progress', |
| 1585 | action='store_true', |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1586 | help='Prints a dot at each request to show progress') |
| 1587 | options, args = parser.parse_args(args) |
maruel | d8aba22 | 2015-09-03 12:21:19 -0700 | [diff] [blame] | 1588 | if len(args) != 1: |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1589 | parser.error( |
| 1590 | 'Must specify only method name and optionally query args properly ' |
| 1591 | 'escaped.') |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 1592 | base_url = options.swarming + '/_ah/api/swarming/v1/' + args[0] |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1593 | try: |
| 1594 | data, yielder = get_yielder(base_url, options.limit) |
| 1595 | for items in yielder(): |
| 1596 | if items: |
| 1597 | data['items'].extend(items) |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1598 | if options.progress: |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1599 | sys.stderr.write('.') |
| 1600 | sys.stderr.flush() |
| 1601 | except Failure as e: |
| 1602 | sys.stderr.write('\n%s\n' % e) |
| 1603 | return 1 |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1604 | if options.progress: |
maruel | af6b06c | 2017-06-08 06:26:53 -0700 | [diff] [blame] | 1605 | sys.stderr.write('\n') |
| 1606 | sys.stderr.flush() |
Paweł Hajdan, Jr | 53ef013 | 2015-03-20 17:49:18 +0100 | [diff] [blame] | 1607 | if options.json: |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 1608 | options.json = six.text_type(os.path.abspath(options.json)) |
maruel | 1ceb387 | 2015-10-14 06:10:44 -0700 | [diff] [blame] | 1609 | tools.write_json(options.json, data, True) |
Paweł Hajdan, Jr | 53ef013 | 2015-03-20 17:49:18 +0100 | [diff] [blame] | 1610 | else: |
Marc-Antoine Ruel | cda90ee | 2015-03-23 15:13:20 -0400 | [diff] [blame] | 1611 | try: |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1612 | tools.write_json(sys.stdout, data, False) |
Marc-Antoine Ruel | cda90ee | 2015-03-23 15:13:20 -0400 | [diff] [blame] | 1613 | sys.stdout.write('\n') |
| 1614 | except IOError: |
| 1615 | pass |
Marc-Antoine Ruel | 79940ae | 2014-09-23 17:55:41 -0400 | [diff] [blame] | 1616 | return 0 |
| 1617 | |
| 1618 | |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1619 | def CMDquery_list(parser, args): |
| 1620 | """Returns list of all the Swarming APIs that can be used with command |
| 1621 | 'query'. |
| 1622 | """ |
| 1623 | parser.add_option( |
| 1624 | '--json', help='Path to JSON output file (otherwise prints to stdout)') |
| 1625 | options, args = parser.parse_args(args) |
| 1626 | if args: |
| 1627 | parser.error('No argument allowed.') |
| 1628 | |
| 1629 | try: |
| 1630 | apis = endpoints_api_discovery_apis(options.swarming) |
| 1631 | except APIError as e: |
| 1632 | parser.error(str(e)) |
| 1633 | if options.json: |
Takuto Ikuta | 6e2ff96 | 2019-10-29 12:35:27 +0000 | [diff] [blame] | 1634 | options.json = six.text_type(os.path.abspath(options.json)) |
maruel | 1ceb387 | 2015-10-14 06:10:44 -0700 | [diff] [blame] | 1635 | with fs.open(options.json, 'wb') as f: |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1636 | json.dump(apis, f) |
| 1637 | else: |
| 1638 | help_url = ( |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1639 | 'https://apis-explorer.appspot.com/apis-explorer/?base=%s/_ah/api#p/' % |
| 1640 | options.swarming) |
Marc-Antoine Ruel | 04903a3 | 2019-10-09 21:09:25 +0000 | [diff] [blame] | 1641 | for i, (api_id, api) in enumerate(sorted(apis.items())): |
maruel | 11e31af | 2017-02-15 07:30:50 -0800 | [diff] [blame] | 1642 | if i: |
| 1643 | print('') |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1644 | print(api_id) |
| 1645 | print(' ' + api['description'].strip()) |
maruel | 11e31af | 2017-02-15 07:30:50 -0800 | [diff] [blame] | 1646 | if 'resources' in api: |
| 1647 | # Old. |
Marc-Antoine Ruel | 793bff3 | 2019-04-18 17:50:48 +0000 | [diff] [blame] | 1648 | # TODO(maruel): Remove. |
| 1649 | # pylint: disable=too-many-nested-blocks |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1650 | for j, (resource_name, |
| 1651 | resource) in enumerate(sorted(api['resources'].items())): |
maruel | 11e31af | 2017-02-15 07:30:50 -0800 | [diff] [blame] | 1652 | if j: |
| 1653 | print('') |
Marc-Antoine Ruel | 04903a3 | 2019-10-09 21:09:25 +0000 | [diff] [blame] | 1654 | for method_name, method in sorted(resource['methods'].items()): |
maruel | 11e31af | 2017-02-15 07:30:50 -0800 | [diff] [blame] | 1655 | # Only list the GET ones. |
| 1656 | if method['httpMethod'] != 'GET': |
| 1657 | continue |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1658 | print('- %s.%s: %s' % (resource_name, method_name, method['path'])) |
| 1659 | print('\n'.join(' ' + l for l in textwrap.wrap( |
| 1660 | method.get('description', 'No description'), 78))) |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1661 | print(' %s%s%s' % (help_url, api['servicePath'], method['id'])) |
maruel | 11e31af | 2017-02-15 07:30:50 -0800 | [diff] [blame] | 1662 | else: |
| 1663 | # New. |
Marc-Antoine Ruel | 04903a3 | 2019-10-09 21:09:25 +0000 | [diff] [blame] | 1664 | for method_name, method in sorted(api['methods'].items()): |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1665 | # Only list the GET ones. |
| 1666 | if method['httpMethod'] != 'GET': |
| 1667 | continue |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1668 | print('- %s: %s' % (method['id'], method['path'])) |
maruel | 11e31af | 2017-02-15 07:30:50 -0800 | [diff] [blame] | 1669 | print('\n'.join( |
| 1670 | ' ' + l for l in textwrap.wrap(method['description'], 78))) |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1671 | print(' %s%s%s' % (help_url, api['servicePath'], method['id'])) |
maruel | 77f720b | 2015-09-15 12:35:22 -0700 | [diff] [blame] | 1672 | return 0 |
| 1673 | |
| 1674 | |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1675 | @subcommand.usage('(hash|isolated) [-- extra_args]') |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1676 | def CMDrun(parser, args): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1677 | """Triggers a task and wait for the results. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1678 | |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1679 | Basically, does everything to run a command remotely. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1680 | """ |
| 1681 | add_trigger_options(parser) |
| 1682 | add_collect_options(parser) |
| 1683 | options, args = parser.parse_args(args) |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1684 | process_collect_options(parser, options) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1685 | task_request = process_trigger_options(parser, options, args) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1686 | try: |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 1687 | tasks = trigger_task_shards(options.swarming, task_request) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1688 | except Failure as e: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1689 | on_error.report('Failed to trigger %s(%s): %s' % |
| 1690 | (task_request.name, args[0], e.args[0])) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1691 | return 1 |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1692 | if not tasks: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1693 | on_error.report('Failed to trigger the task.') |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1694 | return 1 |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1695 | print('Triggered task: %s' % task_request.name) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1696 | task_ids = [ |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1697 | t['task_id'] |
| 1698 | for t in sorted(tasks.values(), key=lambda x: x['shard_index']) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1699 | ] |
Caleb Rouleau | 779c4f0 | 2019-05-22 21:18:49 +0000 | [diff] [blame] | 1700 | for task_id in task_ids: |
| 1701 | print('Task: {server}/task?id={task}'.format( |
| 1702 | server=options.swarming, task=task_id)) |
Marc-Antoine Ruel | f24f09c | 2018-03-23 16:06:18 -0400 | [diff] [blame] | 1703 | if not options.timeout: |
Marc-Antoine Ruel | 3a030bc | 2018-04-23 10:31:25 -0400 | [diff] [blame] | 1704 | offset = 0 |
| 1705 | for s in task_request.task_slices: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1706 | m = (offset + s.properties.execution_timeout_secs + s.expiration_secs) |
Marc-Antoine Ruel | 3a030bc | 2018-04-23 10:31:25 -0400 | [diff] [blame] | 1707 | if m > options.timeout: |
| 1708 | options.timeout = m |
Marc-Antoine Ruel | 1f835c7 | 2018-05-25 12:29:42 -0400 | [diff] [blame] | 1709 | offset += s.expiration_secs |
Marc-Antoine Ruel | 3a030bc | 2018-04-23 10:31:25 -0400 | [diff] [blame] | 1710 | options.timeout += 10. |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1711 | try: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1712 | return collect(options.swarming, task_ids, options.timeout, |
| 1713 | options.decorate, options.print_status_updates, |
| 1714 | options.task_summary_json, options.task_output_dir, |
| 1715 | options.task_output_stdout, options.perf, |
| 1716 | options.filepath_filter) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1717 | except Failure: |
| 1718 | on_error.report(None) |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1719 | return 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1720 | |
| 1721 | |
maruel | 0eb1d1b | 2015-10-02 14:48:21 -0700 | [diff] [blame] | 1722 | @subcommand.usage('bot_id') |
| 1723 | def CMDterminate(parser, args): |
| 1724 | """Tells a bot to gracefully shut itself down as soon as it can. |
| 1725 | |
| 1726 | This is done by completing whatever current task there is then exiting the bot |
| 1727 | process. |
| 1728 | """ |
| 1729 | parser.add_option( |
| 1730 | '--wait', action='store_true', help='Wait for the bot to terminate') |
| 1731 | options, args = parser.parse_args(args) |
| 1732 | if len(args) != 1: |
| 1733 | parser.error('Please provide the bot id') |
smut | 281c390 | 2018-05-30 17:50:05 -0700 | [diff] [blame] | 1734 | url = options.swarming + '/_ah/api/swarming/v1/bot/%s/terminate' % args[0] |
maruel | 0eb1d1b | 2015-10-02 14:48:21 -0700 | [diff] [blame] | 1735 | request = net.url_read_json(url, data={}) |
| 1736 | if not request: |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1737 | print('Failed to ask for termination', file=sys.stderr) |
maruel | 0eb1d1b | 2015-10-02 14:48:21 -0700 | [diff] [blame] | 1738 | return 1 |
| 1739 | if options.wait: |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1740 | return collect(options.swarming, [request['task_id']], 0., False, False, |
| 1741 | None, None, [], False, None) |
maruel | bfc5f87 | 2017-06-10 16:43:17 -0700 | [diff] [blame] | 1742 | else: |
Lei Lei | fe202df | 2019-06-11 17:33:34 +0000 | [diff] [blame] | 1743 | print(request['task_id']) |
maruel | 0eb1d1b | 2015-10-02 14:48:21 -0700 | [diff] [blame] | 1744 | return 0 |
| 1745 | |
| 1746 | |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1747 | @subcommand.usage("(hash|isolated) [-- extra_args|raw command]") |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1748 | def CMDtrigger(parser, args): |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1749 | """Triggers a Swarming task. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1750 | |
Vadim Shtayura | ae8085b | 2014-05-02 17:13:10 -0700 | [diff] [blame] | 1751 | Passes all extra arguments provided after '--' as additional command line |
| 1752 | arguments for an isolated command specified in *.isolate file. |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1753 | """ |
| 1754 | add_trigger_options(parser) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1755 | parser.add_option( |
| 1756 | '--dump-json', |
| 1757 | metavar='FILE', |
| 1758 | help='Dump details about the triggered task(s) to this file as json') |
Marc-Antoine Ruel | 7c54327 | 2013-11-26 13:26:15 -0500 | [diff] [blame] | 1759 | options, args = parser.parse_args(args) |
Marc-Antoine Ruel | efdc528 | 2014-12-12 19:31:00 -0500 | [diff] [blame] | 1760 | task_request = process_trigger_options(parser, options, args) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1761 | try: |
Ye Kuang | 3f3f2f7 | 2020-10-21 10:14:59 +0000 | [diff] [blame] | 1762 | tasks = trigger_task_shards(options.swarming, task_request) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1763 | if tasks: |
maruel | 0a25f6c | 2017-05-10 10:43:23 -0700 | [diff] [blame] | 1764 | print('Triggered task: %s' % task_request.name) |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1765 | tasks_sorted = sorted(tasks.values(), key=lambda x: x['shard_index']) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1766 | if options.dump_json: |
| 1767 | data = { |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1768 | 'base_task_name': task_request.name, |
| 1769 | 'tasks': tasks, |
| 1770 | 'request': task_request_to_raw_request(task_request), |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1771 | } |
maruel | 46b015f | 2015-10-13 18:40:35 -0700 | [diff] [blame] | 1772 | tools.write_json(unicode(options.dump_json), data, True) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1773 | print('To collect results, use:') |
Bruce Dawson | f0a5ae4 | 2018-09-04 20:06:46 +0000 | [diff] [blame] | 1774 | print(' tools/swarming_client/swarming.py collect -S %s --json %s' % |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1775 | (options.swarming, options.dump_json)) |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1776 | else: |
Marc-Antoine Ruel | 12a7da4 | 2014-10-01 08:29:47 -0400 | [diff] [blame] | 1777 | print('To collect results, use:') |
Bruce Dawson | f0a5ae4 | 2018-09-04 20:06:46 +0000 | [diff] [blame] | 1778 | print(' tools/swarming_client/swarming.py collect -S %s %s' % |
Junji Watanabe | cb05404 | 2020-07-21 08:43:26 +0000 | [diff] [blame] | 1779 | (options.swarming, ' '.join(t['task_id'] for t in tasks_sorted))) |
Marc-Antoine Ruel | 2f6581a | 2014-10-03 11:09:53 -0400 | [diff] [blame] | 1780 | print('Or visit:') |
| 1781 | for t in tasks_sorted: |
| 1782 | print(' ' + t['view_url']) |
Marc-Antoine Ruel | d6dbe76 | 2014-06-18 13:49:42 -0400 | [diff] [blame] | 1783 | return int(not tasks) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1784 | except Failure: |
| 1785 | on_error.report(None) |
vadimsh@chromium.org | d908a54 | 2013-10-30 01:36:17 +0000 | [diff] [blame] | 1786 | return 1 |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1787 | |
| 1788 | |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 1789 | class OptionParserSwarming(logging_utils.OptionParserWithLogging): |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1790 | |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1791 | def __init__(self, **kwargs): |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 1792 | logging_utils.OptionParserWithLogging.__init__( |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1793 | self, prog='swarming.py', **kwargs) |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 1794 | self.server_group = optparse.OptionGroup(self, 'Server') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 1795 | self.server_group.add_option( |
Junji Watanabe | 38b28b0 | 2020-04-23 10:23:30 +0000 | [diff] [blame] | 1796 | '-S', |
| 1797 | '--swarming', |
| 1798 | metavar='URL', |
| 1799 | default=os.environ.get('SWARMING_SERVER', ''), |
maruel@chromium.org | e9403ab | 2013-09-20 18:03:49 +0000 | [diff] [blame] | 1800 | help='Swarming server to use') |
Marc-Antoine Ruel | 5471e3d | 2013-11-11 19:10:32 -0500 | [diff] [blame] | 1801 | self.add_option_group(self.server_group) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 1802 | auth.add_auth_options(self) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1803 | |
| 1804 | def parse_args(self, *args, **kwargs): |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 1805 | options, args = logging_utils.OptionParserWithLogging.parse_args( |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1806 | self, *args, **kwargs) |
Marc-Antoine Ruel | 012067b | 2014-12-10 15:45:42 -0500 | [diff] [blame] | 1807 | auth.process_auth_options(self, options) |
| 1808 | user = self._process_swarming(options) |
| 1809 | if hasattr(options, 'user') and not options.user: |
| 1810 | options.user = user |
| 1811 | return options, args |
| 1812 | |
| 1813 | def _process_swarming(self, options): |
| 1814 | """Processes the --swarming option and aborts if not specified. |
| 1815 | |
| 1816 | Returns the identity as determined by the server. |
| 1817 | """ |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1818 | if not options.swarming: |
| 1819 | self.error('--swarming is required.') |
Marc-Antoine Ruel | 012067b | 2014-12-10 15:45:42 -0500 | [diff] [blame] | 1820 | try: |
| 1821 | options.swarming = net.fix_url(options.swarming) |
| 1822 | except ValueError as e: |
| 1823 | self.error('--swarming %s' % e) |
Takuto Ikuta | ae767b3 | 2020-05-11 01:22:19 +0000 | [diff] [blame] | 1824 | |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 1825 | try: |
| 1826 | user = auth.ensure_logged_in(options.swarming) |
| 1827 | except ValueError as e: |
| 1828 | self.error(str(e)) |
Marc-Antoine Ruel | 012067b | 2014-12-10 15:45:42 -0500 | [diff] [blame] | 1829 | return user |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1830 | |
| 1831 | |
| 1832 | def main(args): |
| 1833 | dispatcher = subcommand.CommandDispatcher(__name__) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 1834 | return dispatcher.execute(OptionParserSwarming(version=__version__), args) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1835 | |
| 1836 | |
| 1837 | if __name__ == '__main__': |
maruel | 8e4e40c | 2016-05-30 06:21:07 -0700 | [diff] [blame] | 1838 | subprocess42.inhibit_os_error_reporting() |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1839 | fix_encoding.fix_encoding() |
| 1840 | tools.disable_buffering() |
| 1841 | colorama.init() |
Takuto Ikuta | 7c843c8 | 2020-04-15 05:42:54 +0000 | [diff] [blame] | 1842 | net.set_user_agent('swarming.py/' + __version__) |
maruel@chromium.org | 0437a73 | 2013-08-27 16:05:52 +0000 | [diff] [blame] | 1843 | sys.exit(main(sys.argv[1:])) |