Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 1 | # Lint as: python2, python3 |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 2 | # Copyright Martin J. Bligh, Google Inc 2008 |
| 3 | # Released under the GPL v2 |
| 4 | |
| 5 | """ |
| 6 | This class allows you to communicate with the frontend to submit jobs etc |
| 7 | It is designed for writing more sophisiticated server-side control files that |
| 8 | can recursively add and manage other jobs. |
| 9 | |
| 10 | We turn the JSON dictionaries into real objects that are more idiomatic |
| 11 | |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 12 | For docs, see: |
Aviv Keshet | 2c709f6 | 2013-05-07 12:52:15 -0700 | [diff] [blame] | 13 | http://www.chromium.org/chromium-os/testing/afe-rpc-infrastructure |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 14 | http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-api |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 15 | """ |
| 16 | |
Richard Barnette | b9b3798 | 2016-05-20 19:23:39 -0700 | [diff] [blame] | 17 | #pylint: disable=missing-docstring |
| 18 | |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 19 | from __future__ import absolute_import |
| 20 | from __future__ import division |
| 21 | from __future__ import print_function |
| 22 | |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 23 | import getpass |
| 24 | import os |
| 25 | import re |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 26 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 27 | import common |
Allen Li | 327e6fd | 2016-11-22 13:45:41 -0800 | [diff] [blame] | 28 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 29 | from autotest_lib.frontend.afe import rpc_client_lib |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 30 | from autotest_lib.client.common_lib import control_data |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 31 | from autotest_lib.client.common_lib import global_config |
Allen Li | f74957d | 2017-11-20 17:46:48 -0800 | [diff] [blame] | 32 | from autotest_lib.client.common_lib import host_states |
Allen Li | 352b86a | 2016-12-14 12:11:27 -0800 | [diff] [blame] | 33 | from autotest_lib.client.common_lib import priorities |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 34 | from autotest_lib.client.common_lib import utils |
Scott Zawalski | 63470dd | 2012-09-05 00:49:43 -0400 | [diff] [blame] | 35 | from autotest_lib.tko import db |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 36 | from six.moves import zip |
Scott Zawalski | 63470dd | 2012-09-05 00:49:43 -0400 | [diff] [blame] | 37 | |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 38 | try: |
| 39 | from chromite.lib import metrics |
| 40 | except ImportError: |
| 41 | metrics = utils.metrics_mock |
Scott Zawalski | 63470dd | 2012-09-05 00:49:43 -0400 | [diff] [blame] | 42 | |
mbligh | 4e57661 | 2008-12-22 14:56:36 +0000 | [diff] [blame] | 43 | try: |
| 44 | from autotest_lib.server.site_common import site_utils as server_utils |
| 45 | except: |
| 46 | from autotest_lib.server import utils as server_utils |
| 47 | form_ntuples_from_machines = server_utils.form_ntuples_from_machines |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 48 | |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 49 | GLOBAL_CONFIG = global_config.global_config |
| 50 | DEFAULT_SERVER = 'autotest' |
| 51 | |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 52 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 53 | def dump_object(header, obj): |
| 54 | """ |
| 55 | Standard way to print out the frontend objects (eg job, host, acl, label) |
| 56 | in a human-readable fashion for debugging |
| 57 | """ |
| 58 | result = header + '\n' |
| 59 | for key in obj.hash: |
| 60 | if key == 'afe' or key == 'hash': |
| 61 | continue |
| 62 | result += '%20s: %s\n' % (key, obj.hash[key]) |
| 63 | return result |
| 64 | |
| 65 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 66 | class RpcClient(object): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 67 | """ |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 68 | Abstract RPC class for communicating with the autotest frontend |
| 69 | Inherited for both TKO and AFE uses. |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 70 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 71 | All the constructors go in the afe / tko class. |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 72 | Manipulating methods go in the object classes themselves |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 73 | """ |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 74 | def __init__(self, path, user, server, print_log, debug, reply_debug): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 75 | """ |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 76 | Create a cached instance of a connection to the frontend |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 77 | |
| 78 | user: username to connect as |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 79 | server: frontend server to connect to |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 80 | print_log: pring a logging message to stdout on every operation |
| 81 | debug: print out all RPC traffic |
| 82 | """ |
Dan Shi | ff78f11 | 2015-06-12 13:34:02 -0700 | [diff] [blame] | 83 | if not user and utils.is_in_container(): |
| 84 | user = GLOBAL_CONFIG.get_config_value('SSP', 'user', default=None) |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 85 | if not user: |
mbligh | db59e3c | 2009-11-21 01:45:18 +0000 | [diff] [blame] | 86 | user = getpass.getuser() |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 87 | if not server: |
mbligh | 475f776 | 2009-01-30 00:34:04 +0000 | [diff] [blame] | 88 | if 'AUTOTEST_WEB' in os.environ: |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 89 | server = os.environ['AUTOTEST_WEB'] |
mbligh | 475f776 | 2009-01-30 00:34:04 +0000 | [diff] [blame] | 90 | else: |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 91 | server = GLOBAL_CONFIG.get_config_value('SERVER', 'hostname', |
| 92 | default=DEFAULT_SERVER) |
| 93 | self.server = server |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 94 | self.user = user |
| 95 | self.print_log = print_log |
| 96 | self.debug = debug |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 97 | self.reply_debug = reply_debug |
Scott Zawalski | 347aaf4 | 2012-04-03 16:33:00 -0400 | [diff] [blame] | 98 | headers = {'AUTHORIZATION': self.user} |
Prathmesh Prabhu | 2892ff6 | 2017-12-19 10:21:31 -0800 | [diff] [blame] | 99 | rpc_server = rpc_client_lib.add_protocol(server) + path |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 100 | if debug: |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 101 | print('SERVER: %s' % rpc_server) |
| 102 | print('HEADERS: %s' % headers) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 103 | self.proxy = rpc_client_lib.get_proxy(rpc_server, headers=headers) |
| 104 | |
| 105 | |
| 106 | def run(self, call, **dargs): |
| 107 | """ |
| 108 | Make a RPC call to the AFE server |
| 109 | """ |
| 110 | rpc_call = getattr(self.proxy, call) |
| 111 | if self.debug: |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 112 | print('DEBUG: %s %s' % (call, dargs)) |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 113 | try: |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 114 | result = utils.strip_unicode(rpc_call(**dargs)) |
| 115 | if self.reply_debug: |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 116 | print(result) |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 117 | return result |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 118 | except Exception: |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 119 | raise |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 120 | |
| 121 | |
| 122 | def log(self, message): |
| 123 | if self.print_log: |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 124 | print(message) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 125 | |
| 126 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 127 | class TKO(RpcClient): |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 128 | def __init__(self, user=None, server=None, print_log=True, debug=False, |
| 129 | reply_debug=False): |
Scott Zawalski | 347aaf4 | 2012-04-03 16:33:00 -0400 | [diff] [blame] | 130 | super(TKO, self).__init__(path='/new_tko/server/noauth/rpc/', |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 131 | user=user, |
| 132 | server=server, |
| 133 | print_log=print_log, |
| 134 | debug=debug, |
| 135 | reply_debug=reply_debug) |
Scott Zawalski | 63470dd | 2012-09-05 00:49:43 -0400 | [diff] [blame] | 136 | self._db = None |
| 137 | |
| 138 | |
Allen Li | 327e6fd | 2016-11-22 13:45:41 -0800 | [diff] [blame] | 139 | @metrics.SecondsTimerDecorator( |
Prathmesh Prabhu | a7556a9 | 2017-02-01 14:18:41 -0800 | [diff] [blame] | 140 | 'chromeos/autotest/tko/get_job_status_duration') |
Scott Zawalski | 63470dd | 2012-09-05 00:49:43 -0400 | [diff] [blame] | 141 | def get_job_test_statuses_from_db(self, job_id): |
| 142 | """Get job test statuses from the database. |
| 143 | |
| 144 | Retrieve a set of fields from a job that reflect the status of each test |
| 145 | run within a job. |
| 146 | fields retrieved: status, test_name, reason, test_started_time, |
| 147 | test_finished_time, afe_job_id, job_owner, hostname. |
| 148 | |
| 149 | @param job_id: The afe job id to look up. |
| 150 | @returns a TestStatus object of the resulting information. |
| 151 | """ |
| 152 | if self._db is None: |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 153 | self._db = db.db() |
Fang Deng | 5c50833 | 2014-03-19 10:26:00 -0700 | [diff] [blame] | 154 | fields = ['status', 'test_name', 'subdir', 'reason', |
| 155 | 'test_started_time', 'test_finished_time', 'afe_job_id', |
| 156 | 'job_owner', 'hostname', 'job_tag'] |
Scott Zawalski | 63470dd | 2012-09-05 00:49:43 -0400 | [diff] [blame] | 157 | table = 'tko_test_view_2' |
| 158 | where = 'job_tag like "%s-%%"' % job_id |
| 159 | test_status = [] |
| 160 | # Run commit before we query to ensure that we are pulling the latest |
| 161 | # results. |
| 162 | self._db.commit() |
| 163 | for entry in self._db.select(','.join(fields), table, (where, None)): |
| 164 | status_dict = {} |
| 165 | for key,value in zip(fields, entry): |
| 166 | # All callers expect values to be a str object. |
| 167 | status_dict[key] = str(value) |
| 168 | # id is used by TestStatus to uniquely identify each Test Status |
| 169 | # obj. |
| 170 | status_dict['id'] = [status_dict['reason'], status_dict['hostname'], |
| 171 | status_dict['test_name']] |
| 172 | test_status.append(status_dict) |
| 173 | |
| 174 | return [TestStatus(self, e) for e in test_status] |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 175 | |
| 176 | |
| 177 | def get_status_counts(self, job, **data): |
| 178 | entries = self.run('get_status_counts', |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 179 | group_by=['hostname', 'test_name', 'reason'], |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 180 | job_tag__startswith='%s-' % job, **data) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 181 | return [TestStatus(self, e) for e in entries['groups']] |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 182 | |
| 183 | |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 184 | class _StableVersionMap(object): |
| 185 | """ |
| 186 | A mapping from board names to strings naming software versions. |
| 187 | |
| 188 | The mapping is meant to allow finding a nominally "stable" version |
| 189 | of software associated with a given board. The mapping identifies |
| 190 | specific versions of software that should be installed during |
| 191 | operations such as repair. |
| 192 | |
| 193 | Conceptually, there are multiple version maps, each handling |
| 194 | different types of image. For instance, a single board may have |
| 195 | both a stable OS image (e.g. for CrOS), and a separate stable |
| 196 | firmware image. |
| 197 | |
| 198 | Each different type of image requires a certain amount of special |
| 199 | handling, implemented by a subclass of `StableVersionMap`. The |
| 200 | subclasses take care of pre-processing of arguments, delegating |
| 201 | actual RPC calls to this superclass. |
| 202 | |
| 203 | @property _afe AFE object through which to make the actual RPC |
| 204 | calls. |
| 205 | @property _android Value of the `android` parameter to be passed |
| 206 | when calling the `get_stable_version` RPC. |
| 207 | """ |
| 208 | |
Richard Barnette | 8117543 | 2018-07-06 17:21:59 -0700 | [diff] [blame] | 209 | def __init__(self, afe): |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 210 | self._afe = afe |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def get_all_versions(self): |
| 214 | """ |
| 215 | Get all mappings in the stable versions table. |
| 216 | |
| 217 | Extracts the full content of the `stable_version` table |
| 218 | in the AFE database, and returns it as a dictionary |
| 219 | mapping board names to version strings. |
| 220 | |
| 221 | @return A dictionary mapping board names to version strings. |
| 222 | """ |
| 223 | return self._afe.run('get_all_stable_versions') |
| 224 | |
| 225 | |
| 226 | def get_version(self, board): |
| 227 | """ |
| 228 | Get the mapping of one board in the stable versions table. |
| 229 | |
| 230 | Look up and return the version mapped to the given board in the |
| 231 | `stable_versions` table in the AFE database. |
| 232 | |
| 233 | @param board The board to be looked up. |
| 234 | |
| 235 | @return The version mapped for the given board. |
| 236 | """ |
Richard Barnette | 8117543 | 2018-07-06 17:21:59 -0700 | [diff] [blame] | 237 | return self._afe.run('get_stable_version', board=board) |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 238 | |
| 239 | |
| 240 | def set_version(self, board, version): |
| 241 | """ |
| 242 | Change the mapping of one board in the stable versions table. |
| 243 | |
| 244 | Set the mapping in the `stable_versions` table in the AFE |
| 245 | database for the given board to the given version. |
| 246 | |
| 247 | @param board The board to be updated. |
| 248 | @param version The new version to be assigned to the board. |
| 249 | """ |
Gregory Nisbet | 7f41066 | 2019-11-19 09:04:20 -0800 | [diff] [blame] | 250 | raise RuntimeError("server.frontend._StableVersionMap::set_version is intentionally deleted") |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 251 | |
| 252 | |
| 253 | def delete_version(self, board): |
| 254 | """ |
| 255 | Remove the mapping of one board in the stable versions table. |
| 256 | |
| 257 | Remove the mapping in the `stable_versions` table in the AFE |
| 258 | database for the given board. |
| 259 | |
| 260 | @param board The board to be updated. |
| 261 | """ |
Gregory Nisbet | 7f41066 | 2019-11-19 09:04:20 -0800 | [diff] [blame] | 262 | raise RuntimeError("server.frontend._StableVersionMap::delete_version is intentionally deleted") |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 263 | |
| 264 | |
| 265 | class _OSVersionMap(_StableVersionMap): |
| 266 | """ |
| 267 | Abstract stable version mapping for full OS images of various types. |
| 268 | """ |
| 269 | |
Richard Barnette | 206b25f | 2018-04-03 13:53:22 -0700 | [diff] [blame] | 270 | def _version_is_valid(self, version): |
| 271 | return True |
| 272 | |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 273 | def get_all_versions(self): |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 274 | versions = super(_OSVersionMap, self).get_all_versions() |
| 275 | for board in versions.keys(): |
Richard Barnette | 206b25f | 2018-04-03 13:53:22 -0700 | [diff] [blame] | 276 | if ('/' in board |
| 277 | or not self._version_is_valid(versions[board])): |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 278 | del versions[board] |
| 279 | return versions |
| 280 | |
Richard Barnette | 206b25f | 2018-04-03 13:53:22 -0700 | [diff] [blame] | 281 | def get_version(self, board): |
| 282 | version = super(_OSVersionMap, self).get_version(board) |
| 283 | return version if self._version_is_valid(version) else None |
| 284 | |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 285 | |
Richard Barnette | 08e487d | 2018-03-30 18:39:31 -0700 | [diff] [blame] | 286 | def format_cros_image_name(board, version): |
| 287 | """ |
| 288 | Return an image name for a given `board` and `version`. |
| 289 | |
| 290 | This formats `board` and `version` into a string identifying an |
| 291 | image file. The string represents part of a URL for access to |
| 292 | the image. |
| 293 | |
| 294 | The returned image name is typically of a form like |
| 295 | "falco-release/R55-8872.44.0". |
| 296 | """ |
| 297 | build_pattern = GLOBAL_CONFIG.get_config_value( |
| 298 | 'CROS', 'stable_build_pattern') |
| 299 | return build_pattern % (board, version) |
| 300 | |
| 301 | |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 302 | class _CrosVersionMap(_OSVersionMap): |
| 303 | """ |
| 304 | Stable version mapping for Chrome OS release images. |
| 305 | |
| 306 | This class manages a mapping of Chrome OS board names to known-good |
| 307 | release (or canary) images. The images selected can be installed on |
| 308 | DUTs during repair tasks, as a way of getting a DUT into a known |
| 309 | working state. |
| 310 | """ |
| 311 | |
Richard Barnette | 206b25f | 2018-04-03 13:53:22 -0700 | [diff] [blame] | 312 | def _version_is_valid(self, version): |
| 313 | return version is not None and '/' not in version |
| 314 | |
Richard Barnette | 383ef9c | 2016-12-13 11:56:49 -0800 | [diff] [blame] | 315 | def get_image_name(self, board): |
| 316 | """ |
| 317 | Return the full image name of the stable version for `board`. |
| 318 | |
| 319 | This finds the stable version for `board`, and returns a string |
Richard Barnette | 728e36f | 2016-11-03 16:04:29 -0700 | [diff] [blame] | 320 | identifying the associated image as for `format_image_name()`, |
| 321 | above. |
Richard Barnette | 383ef9c | 2016-12-13 11:56:49 -0800 | [diff] [blame] | 322 | |
| 323 | @return A string identifying the image file for the stable |
| 324 | image for `board`. |
| 325 | """ |
Richard Barnette | 08e487d | 2018-03-30 18:39:31 -0700 | [diff] [blame] | 326 | return format_cros_image_name(board, self.get_version(board)) |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 327 | |
| 328 | |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 329 | class _SuffixHackVersionMap(_StableVersionMap): |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 330 | """ |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 331 | Abstract super class for mappings using a pseudo-board name. |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 332 | |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 333 | For non-OS image type mappings, we look them up in the |
| 334 | `stable_versions` table by constructing a "pseudo-board" from the |
| 335 | real board name plus a suffix string that identifies the image type. |
| 336 | So, for instance the name "lulu/firmware" is used to look up the |
| 337 | FAFT firmware version for lulu boards. |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 338 | """ |
| 339 | |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 340 | # _SUFFIX - The suffix used in constructing the "pseudo-board" |
| 341 | # lookup key. Each subclass must define this value for itself. |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 342 | # |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 343 | _SUFFIX = None |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 344 | |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 345 | def get_all_versions(self): |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 346 | # Get all the mappings from the AFE, extract just the mappings |
| 347 | # with our suffix, and replace the pseudo-board name keys with |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 348 | # the real board names. |
| 349 | # |
| 350 | all_versions = super( |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 351 | _SuffixHackVersionMap, self).get_all_versions() |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 352 | return { |
| 353 | board[0 : -len(self._SUFFIX)]: all_versions[board] |
| 354 | for board in all_versions.keys() |
| 355 | if board.endswith(self._SUFFIX) |
| 356 | } |
| 357 | |
| 358 | |
| 359 | def get_version(self, board): |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 360 | board += self._SUFFIX |
| 361 | return super(_SuffixHackVersionMap, self).get_version(board) |
| 362 | |
| 363 | |
| 364 | def set_version(self, board, version): |
| 365 | board += self._SUFFIX |
| 366 | super(_SuffixHackVersionMap, self).set_version(board, version) |
| 367 | |
| 368 | |
| 369 | def delete_version(self, board): |
| 370 | board += self._SUFFIX |
| 371 | super(_SuffixHackVersionMap, self).delete_version(board) |
| 372 | |
| 373 | |
| 374 | class _FAFTVersionMap(_SuffixHackVersionMap): |
| 375 | """ |
| 376 | Stable version mapping for firmware versions used in FAFT repair. |
| 377 | |
| 378 | When DUTs used for FAFT fail repair, stable firmware may need to be |
| 379 | flashed directly from original tarballs. The FAFT firmware version |
| 380 | mapping finds the appropriate tarball for a given board. |
| 381 | """ |
| 382 | |
| 383 | _SUFFIX = '/firmware' |
| 384 | |
| 385 | def get_version(self, board): |
| 386 | # If there's no mapping for `board`, the lookup will return the |
| 387 | # default CrOS version mapping. To eliminate that case, we |
| 388 | # require a '/' character in the version, since CrOS versions |
| 389 | # won't match that. |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 390 | # |
| 391 | # TODO(jrbarnette): This is, of course, a hack. Ultimately, |
| 392 | # the right fix is to move handling to the RPC server side. |
| 393 | # |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 394 | version = super(_FAFTVersionMap, self).get_version(board) |
| 395 | return version if '/' in version else None |
| 396 | |
| 397 | |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 398 | class _FirmwareVersionMap(_SuffixHackVersionMap): |
| 399 | """ |
| 400 | Stable version mapping for firmware supplied in Chrome OS images. |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 401 | |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 402 | A Chrome OS image bundles a version of the firmware that the |
| 403 | device should update to when the OS version is installed during |
| 404 | AU. |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 405 | |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 406 | Test images suppress the firmware update during AU. Instead, during |
| 407 | repair and verify we check installed firmware on a DUT, compare it |
| 408 | against the stable version mapping for the board, and update when |
| 409 | the DUT is out-of-date. |
| 410 | """ |
| 411 | |
| 412 | _SUFFIX = '/rwfw' |
| 413 | |
| 414 | def get_version(self, board): |
| 415 | # If there's no mapping for `board`, the lookup will return the |
| 416 | # default CrOS version mapping. To eliminate that case, we |
| 417 | # require the version start with "Google_", since CrOS versions |
| 418 | # won't match that. |
| 419 | # |
| 420 | # TODO(jrbarnette): This is, of course, a hack. Ultimately, |
| 421 | # the right fix is to move handling to the RPC server side. |
| 422 | # |
| 423 | version = super(_FirmwareVersionMap, self).get_version(board) |
| 424 | return version if version.startswith('Google_') else None |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 425 | |
| 426 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 427 | class AFE(RpcClient): |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 428 | |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 429 | # Known image types for stable version mapping objects. |
| 430 | # CROS_IMAGE_TYPE - Mappings for Chrome OS images. |
| 431 | # FAFT_IMAGE_TYPE - Mappings for Firmware images for FAFT repair. |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 432 | # FIRMWARE_IMAGE_TYPE - Mappings for released RW Firmware images. |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 433 | # |
| 434 | CROS_IMAGE_TYPE = 'cros' |
| 435 | FAFT_IMAGE_TYPE = 'faft' |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 436 | FIRMWARE_IMAGE_TYPE = 'firmware' |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 437 | |
| 438 | _IMAGE_MAPPING_CLASSES = { |
| 439 | CROS_IMAGE_TYPE: _CrosVersionMap, |
| 440 | FAFT_IMAGE_TYPE: _FAFTVersionMap, |
Richard Barnette | e50453e | 2016-10-10 16:43:44 -0700 | [diff] [blame] | 441 | FIRMWARE_IMAGE_TYPE: _FirmwareVersionMap, |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | |
Prathmesh Prabhu | 6d5ba59 | 2017-01-05 13:56:04 -0800 | [diff] [blame] | 445 | def __init__(self, user=None, server=None, print_log=True, debug=False, |
| 446 | reply_debug=False, job=None): |
| 447 | self.job = job |
| 448 | super(AFE, self).__init__(path='/afe/server/noauth/rpc/', |
| 449 | user=user, |
| 450 | server=server, |
| 451 | print_log=print_log, |
| 452 | debug=debug, |
| 453 | reply_debug=reply_debug) |
| 454 | |
| 455 | |
Richard Barnette | 260cbd0 | 2016-10-06 12:23:28 -0700 | [diff] [blame] | 456 | def get_stable_version_map(self, image_type): |
| 457 | """ |
| 458 | Return a stable version mapping for the given image type. |
| 459 | |
| 460 | @return An object mapping board names to version strings for |
| 461 | software of the given image type. |
| 462 | """ |
| 463 | return self._IMAGE_MAPPING_CLASSES[image_type](self) |
| 464 | |
| 465 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 466 | def host_statuses(self, live=None): |
jamesren | 121eee6 | 2010-04-13 19:10:12 +0000 | [diff] [blame] | 467 | dead_statuses = ['Repair Failed', 'Repairing'] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 468 | statuses = self.run('get_static_data')['host_statuses'] |
| 469 | if live == True: |
mbligh | c2847b7 | 2009-03-25 19:32:20 +0000 | [diff] [blame] | 470 | return list(set(statuses) - set(dead_statuses)) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 471 | if live == False: |
| 472 | return dead_statuses |
| 473 | else: |
| 474 | return statuses |
| 475 | |
| 476 | |
mbligh | 7109401 | 2009-12-19 05:35:21 +0000 | [diff] [blame] | 477 | @staticmethod |
| 478 | def _dict_for_host_query(hostnames=(), status=None, label=None): |
| 479 | query_args = {} |
mbligh | 4e545a5 | 2009-12-19 05:30:39 +0000 | [diff] [blame] | 480 | if hostnames: |
| 481 | query_args['hostname__in'] = hostnames |
| 482 | if status: |
| 483 | query_args['status'] = status |
| 484 | if label: |
| 485 | query_args['labels__name'] = label |
mbligh | 7109401 | 2009-12-19 05:35:21 +0000 | [diff] [blame] | 486 | return query_args |
| 487 | |
| 488 | |
| 489 | def get_hosts(self, hostnames=(), status=None, label=None, **dargs): |
| 490 | query_args = dict(dargs) |
| 491 | query_args.update(self._dict_for_host_query(hostnames=hostnames, |
| 492 | status=status, |
| 493 | label=label)) |
| 494 | hosts = self.run('get_hosts', **query_args) |
| 495 | return [Host(self, h) for h in hosts] |
| 496 | |
| 497 | |
| 498 | def get_hostnames(self, status=None, label=None, **dargs): |
| 499 | """Like get_hosts() but returns hostnames instead of Host objects.""" |
| 500 | # This implementation can be replaced with a more efficient one |
| 501 | # that does not query for entire host objects in the future. |
| 502 | return [host_obj.hostname for host_obj in |
| 503 | self.get_hosts(status=status, label=label, **dargs)] |
| 504 | |
| 505 | |
| 506 | def reverify_hosts(self, hostnames=(), status=None, label=None): |
| 507 | query_args = dict(locked=False, |
| 508 | aclgroup__users__login=self.user) |
| 509 | query_args.update(self._dict_for_host_query(hostnames=hostnames, |
| 510 | status=status, |
| 511 | label=label)) |
mbligh | 4e545a5 | 2009-12-19 05:30:39 +0000 | [diff] [blame] | 512 | return self.run('reverify_hosts', **query_args) |
| 513 | |
| 514 | |
Richard Barnette | 73e74b0 | 2017-08-10 15:16:49 -0700 | [diff] [blame] | 515 | def repair_hosts(self, hostnames=(), status=None, label=None): |
| 516 | query_args = dict(locked=False, |
| 517 | aclgroup__users__login=self.user) |
| 518 | query_args.update(self._dict_for_host_query(hostnames=hostnames, |
| 519 | status=status, |
| 520 | label=label)) |
| 521 | return self.run('repair_hosts', **query_args) |
| 522 | |
| 523 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 524 | def create_host(self, hostname, **dargs): |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 525 | id = self.run('add_host', hostname=hostname, **dargs) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 526 | return self.get_hosts(id=id)[0] |
| 527 | |
| 528 | |
MK Ryu | acf3592 | 2014-10-03 14:56:49 -0700 | [diff] [blame] | 529 | def get_host_attribute(self, attr, **dargs): |
| 530 | host_attrs = self.run('get_host_attribute', attribute=attr, **dargs) |
| 531 | return [HostAttribute(self, a) for a in host_attrs] |
| 532 | |
| 533 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 534 | def set_host_attribute(self, attr, val, **dargs): |
| 535 | self.run('set_host_attribute', attribute=attr, value=val, **dargs) |
| 536 | |
| 537 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 538 | def get_labels(self, **dargs): |
| 539 | labels = self.run('get_labels', **dargs) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 540 | return [Label(self, l) for l in labels] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 541 | |
| 542 | |
| 543 | def create_label(self, name, **dargs): |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 544 | id = self.run('add_label', name=name, **dargs) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 545 | return self.get_labels(id=id)[0] |
| 546 | |
| 547 | |
| 548 | def get_acls(self, **dargs): |
| 549 | acls = self.run('get_acl_groups', **dargs) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 550 | return [Acl(self, a) for a in acls] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 551 | |
| 552 | |
| 553 | def create_acl(self, name, **dargs): |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 554 | id = self.run('add_acl_group', name=name, **dargs) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 555 | return self.get_acls(id=id)[0] |
| 556 | |
| 557 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 558 | def get_users(self, **dargs): |
| 559 | users = self.run('get_users', **dargs) |
| 560 | return [User(self, u) for u in users] |
| 561 | |
| 562 | |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 563 | def generate_control_file(self, tests, **dargs): |
| 564 | ret = self.run('generate_control_file', tests=tests, **dargs) |
| 565 | return ControlFile(self, ret) |
| 566 | |
| 567 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 568 | def get_jobs(self, summary=False, **dargs): |
| 569 | if summary: |
| 570 | jobs_data = self.run('get_jobs_summary', **dargs) |
| 571 | else: |
| 572 | jobs_data = self.run('get_jobs', **dargs) |
mbligh | afbba0c | 2009-06-08 16:44:45 +0000 | [diff] [blame] | 573 | jobs = [] |
| 574 | for j in jobs_data: |
| 575 | job = Job(self, j) |
| 576 | # Set up some extra information defaults |
| 577 | job.testname = re.sub('\s.*', '', job.name) # arbitrary default |
| 578 | job.platform_results = {} |
| 579 | job.platform_reasons = {} |
| 580 | jobs.append(job) |
| 581 | return jobs |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 582 | |
| 583 | |
Paul Hobbs | f6fbe2d | 2017-06-07 14:17:07 -0700 | [diff] [blame] | 584 | def get_host_queue_entries(self, **kwargs): |
| 585 | """Find JobStatus objects matching some constraints. |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 586 | |
Paul Hobbs | f6fbe2d | 2017-06-07 14:17:07 -0700 | [diff] [blame] | 587 | @param **kwargs: Arguments to pass to the RPC |
| 588 | """ |
| 589 | entries = self.run('get_host_queue_entries', **kwargs) |
| 590 | return self._entries_to_statuses(entries) |
| 591 | |
| 592 | |
| 593 | def get_host_queue_entries_by_insert_time(self, **kwargs): |
| 594 | """Like get_host_queue_entries, but using the insert index table. |
| 595 | |
| 596 | @param **kwargs: Arguments to pass to the RPC |
| 597 | """ |
| 598 | entries = self.run('get_host_queue_entries_by_insert_time', **kwargs) |
| 599 | return self._entries_to_statuses(entries) |
| 600 | |
| 601 | |
| 602 | def _entries_to_statuses(self, entries): |
| 603 | """Converts HQEs to JobStatuses |
| 604 | |
| 605 | Sadly, get_host_queue_entries doesn't return platforms, we have |
| 606 | to get those back from an explicit get_hosts queury, then patch |
| 607 | the new host objects back into the host list. |
| 608 | |
| 609 | :param entries: A list of HQEs from get_host_queue_entries or |
| 610 | get_host_queue_entries_by_insert_time. |
| 611 | """ |
| 612 | job_statuses = [JobStatus(self, e) for e in entries] |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 613 | hostnames = [s.host.hostname for s in job_statuses if s.host] |
Paul Hobbs | f6fbe2d | 2017-06-07 14:17:07 -0700 | [diff] [blame] | 614 | hosts = {} |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 615 | for host in self.get_hosts(hostname__in=hostnames): |
Paul Hobbs | f6fbe2d | 2017-06-07 14:17:07 -0700 | [diff] [blame] | 616 | hosts[host.hostname] = host |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 617 | for status in job_statuses: |
| 618 | if status.host: |
Paul Hobbs | f6fbe2d | 2017-06-07 14:17:07 -0700 | [diff] [blame] | 619 | status.host = hosts.get(status.host.hostname) |
mbligh | f9e3586 | 2009-02-26 01:03:11 +0000 | [diff] [blame] | 620 | # filter job statuses that have either host or meta_host |
| 621 | return [status for status in job_statuses if (status.host or |
| 622 | status.meta_host)] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 623 | |
| 624 | |
MK Ryu | 1b2d7f9 | 2015-02-24 17:45:02 -0800 | [diff] [blame] | 625 | def get_special_tasks(self, **data): |
| 626 | tasks = self.run('get_special_tasks', **data) |
| 627 | return [SpecialTask(self, t) for t in tasks] |
| 628 | |
| 629 | |
J. Richard Barnette | 9f10c9f | 2015-04-13 16:44:50 -0700 | [diff] [blame] | 630 | def get_host_special_tasks(self, host_id, **data): |
| 631 | tasks = self.run('get_host_special_tasks', |
| 632 | host_id=host_id, **data) |
| 633 | return [SpecialTask(self, t) for t in tasks] |
| 634 | |
| 635 | |
J. Richard Barnette | 8dbd6d3 | 2015-05-01 11:01:12 -0700 | [diff] [blame] | 636 | def get_host_status_task(self, host_id, end_time): |
| 637 | task = self.run('get_host_status_task', |
J. Richard Barnette | 8abbfd6 | 2015-06-23 12:46:54 -0700 | [diff] [blame] | 638 | host_id=host_id, end_time=end_time) |
J. Richard Barnette | bc9a795 | 2015-04-16 17:43:27 -0700 | [diff] [blame] | 639 | return SpecialTask(self, task) if task else None |
| 640 | |
| 641 | |
J. Richard Barnette | 8abbfd6 | 2015-06-23 12:46:54 -0700 | [diff] [blame] | 642 | def get_host_diagnosis_interval(self, host_id, end_time, success): |
| 643 | return self.run('get_host_diagnosis_interval', |
| 644 | host_id=host_id, end_time=end_time, |
| 645 | success=success) |
| 646 | |
| 647 | |
Allen Li | 352b86a | 2016-12-14 12:11:27 -0800 | [diff] [blame] | 648 | def create_job(self, control_file, name=' ', |
| 649 | priority=priorities.Priority.DEFAULT, |
| 650 | control_type=control_data.CONTROL_TYPE_NAMES.CLIENT, |
| 651 | **dargs): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 652 | id = self.run('create_job', name=name, priority=priority, |
| 653 | control_file=control_file, control_type=control_type, **dargs) |
| 654 | return self.get_jobs(id=id)[0] |
| 655 | |
| 656 | |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 657 | def abort_jobs(self, jobs): |
| 658 | """Abort a list of jobs. |
| 659 | |
| 660 | Already completed jobs will not be affected. |
| 661 | |
| 662 | @param jobs: List of job ids to abort. |
| 663 | """ |
| 664 | for job in jobs: |
| 665 | self.run('abort_host_queue_entries', job_id=job) |
| 666 | |
| 667 | |
Kevin Cheng | 1952198 | 2016-09-22 12:27:23 -0700 | [diff] [blame] | 668 | def get_hosts_by_attribute(self, attribute, value): |
| 669 | """ |
| 670 | Get the list of hosts that share the same host attribute value. |
| 671 | |
| 672 | @param attribute: String of the host attribute to check. |
| 673 | @param value: String of the value that is shared between hosts. |
| 674 | |
| 675 | @returns List of hostnames that all have the same host attribute and |
| 676 | value. |
| 677 | """ |
| 678 | return self.run('get_hosts_by_attribute', |
| 679 | attribute=attribute, value=value) |
| 680 | |
| 681 | |
| 682 | def lock_host(self, host, lock_reason, fail_if_locked=False): |
| 683 | """ |
| 684 | Lock the given host with the given lock reason. |
| 685 | |
| 686 | Locking a host that's already locked using the 'modify_hosts' rpc |
| 687 | will raise an exception. That's why fail_if_locked exists so the |
| 688 | caller can determine if the lock succeeded or failed. This will |
| 689 | save every caller from wrapping lock_host in a try-except. |
| 690 | |
| 691 | @param host: hostname of host to lock. |
| 692 | @param lock_reason: Reason for locking host. |
| 693 | @param fail_if_locked: Return False if host is already locked. |
| 694 | |
| 695 | @returns Boolean, True if lock was successful, False otherwise. |
| 696 | """ |
| 697 | try: |
| 698 | self.run('modify_hosts', |
| 699 | host_filter_data={'hostname': host}, |
| 700 | update_data={'locked': True, |
| 701 | 'lock_reason': lock_reason}) |
| 702 | except Exception: |
| 703 | return not fail_if_locked |
| 704 | return True |
| 705 | |
| 706 | |
| 707 | def unlock_hosts(self, locked_hosts): |
| 708 | """ |
| 709 | Unlock the hosts. |
| 710 | |
| 711 | Unlocking a host that's already unlocked will do nothing so we don't |
| 712 | need any special try-except clause here. |
| 713 | |
| 714 | @param locked_hosts: List of hostnames of hosts to unlock. |
| 715 | """ |
| 716 | self.run('modify_hosts', |
| 717 | host_filter_data={'hostname__in': locked_hosts}, |
| 718 | update_data={'locked': False, |
| 719 | 'lock_reason': ''}) |
| 720 | |
| 721 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 722 | class TestResults(object): |
| 723 | """ |
| 724 | Container class used to hold the results of the tests for a job |
| 725 | """ |
| 726 | def __init__(self): |
| 727 | self.good = [] |
| 728 | self.fail = [] |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 729 | self.pending = [] |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 730 | |
| 731 | |
| 732 | def add(self, result): |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 733 | if result.complete_count > result.pass_count: |
| 734 | self.fail.append(result) |
| 735 | elif result.incomplete_count > 0: |
| 736 | self.pending.append(result) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 737 | else: |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 738 | self.good.append(result) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 739 | |
| 740 | |
| 741 | class RpcObject(object): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 742 | """ |
| 743 | Generic object used to construct python objects from rpc calls |
| 744 | """ |
| 745 | def __init__(self, afe, hash): |
| 746 | self.afe = afe |
| 747 | self.hash = hash |
| 748 | self.__dict__.update(hash) |
| 749 | |
| 750 | |
| 751 | def __str__(self): |
| 752 | return dump_object(self.__repr__(), self) |
| 753 | |
| 754 | |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 755 | class ControlFile(RpcObject): |
| 756 | """ |
| 757 | AFE control file object |
| 758 | |
| 759 | Fields: synch_count, dependencies, control_file, is_server |
| 760 | """ |
| 761 | def __repr__(self): |
| 762 | return 'CONTROL FILE: %s' % self.control_file |
| 763 | |
| 764 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 765 | class Label(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 766 | """ |
| 767 | AFE label object |
| 768 | |
| 769 | Fields: |
| 770 | name, invalid, platform, kernel_config, id, only_if_needed |
| 771 | """ |
| 772 | def __repr__(self): |
| 773 | return 'LABEL: %s' % self.name |
| 774 | |
| 775 | |
| 776 | def add_hosts(self, hosts): |
Prathmesh Prabhu | ef7e5b0 | 2017-11-08 11:57:47 -0800 | [diff] [blame] | 777 | # We must use the label's name instead of the id because label ids are |
| 778 | # not consistent across master-shard. |
| 779 | return self.afe.run('label_add_hosts', id=self.name, hosts=hosts) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 780 | |
| 781 | |
| 782 | def remove_hosts(self, hosts): |
Prathmesh Prabhu | ef7e5b0 | 2017-11-08 11:57:47 -0800 | [diff] [blame] | 783 | # We must use the label's name instead of the id because label ids are |
| 784 | # not consistent across master-shard. |
| 785 | return self.afe.run('label_remove_hosts', id=self.name, hosts=hosts) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 786 | |
| 787 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 788 | class Acl(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 789 | """ |
| 790 | AFE acl object |
| 791 | |
| 792 | Fields: |
| 793 | users, hosts, description, name, id |
| 794 | """ |
| 795 | def __repr__(self): |
| 796 | return 'ACL: %s' % self.name |
| 797 | |
| 798 | |
| 799 | def add_hosts(self, hosts): |
| 800 | self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name)) |
| 801 | return self.afe.run('acl_group_add_hosts', self.id, hosts) |
| 802 | |
| 803 | |
| 804 | def remove_hosts(self, hosts): |
| 805 | self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name)) |
| 806 | return self.afe.run('acl_group_remove_hosts', self.id, hosts) |
| 807 | |
| 808 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 809 | def add_users(self, users): |
| 810 | self.afe.log('Adding users %s to ACL %s' % (users, self.name)) |
| 811 | return self.afe.run('acl_group_add_users', id=self.name, users=users) |
| 812 | |
| 813 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 814 | class Job(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 815 | """ |
| 816 | AFE job object |
| 817 | |
| 818 | Fields: |
| 819 | name, control_file, control_type, synch_count, reboot_before, |
| 820 | run_verify, priority, email_list, created_on, dependencies, |
| 821 | timeout, owner, reboot_after, id |
| 822 | """ |
| 823 | def __repr__(self): |
| 824 | return 'JOB: %s' % self.id |
| 825 | |
| 826 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 827 | class JobStatus(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 828 | """ |
| 829 | AFE job_status object |
| 830 | |
| 831 | Fields: |
| 832 | status, complete, deleted, meta_host, host, active, execution_subdir, id |
| 833 | """ |
| 834 | def __init__(self, afe, hash): |
MK Ryu | 1b2d7f9 | 2015-02-24 17:45:02 -0800 | [diff] [blame] | 835 | super(JobStatus, self).__init__(afe, hash) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 836 | self.job = Job(afe, self.job) |
Dale Curtis | 8adf789 | 2011-09-08 16:13:36 -0700 | [diff] [blame] | 837 | if getattr(self, 'host'): |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 838 | self.host = Host(afe, self.host) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 839 | |
| 840 | |
| 841 | def __repr__(self): |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 842 | if self.host and self.host.hostname: |
| 843 | hostname = self.host.hostname |
| 844 | else: |
| 845 | hostname = 'None' |
| 846 | return 'JOB STATUS: %s-%s' % (self.job.id, hostname) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 847 | |
| 848 | |
MK Ryu | 1b2d7f9 | 2015-02-24 17:45:02 -0800 | [diff] [blame] | 849 | class SpecialTask(RpcObject): |
| 850 | """ |
| 851 | AFE special task object |
| 852 | """ |
| 853 | def __init__(self, afe, hash): |
| 854 | super(SpecialTask, self).__init__(afe, hash) |
| 855 | self.host = Host(afe, self.host) |
| 856 | |
| 857 | |
| 858 | def __repr__(self): |
| 859 | return 'SPECIAL TASK: %s' % self.id |
| 860 | |
| 861 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 862 | class Host(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 863 | """ |
| 864 | AFE host object |
| 865 | |
| 866 | Fields: |
| 867 | status, lock_time, locked_by, locked, hostname, invalid, |
Allen Li | e4c0827 | 2017-02-01 16:40:53 -0800 | [diff] [blame] | 868 | labels, platform, protection, dirty, id |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 869 | """ |
| 870 | def __repr__(self): |
| 871 | return 'HOST OBJECT: %s' % self.hostname |
| 872 | |
| 873 | |
| 874 | def show(self): |
| 875 | labels = list(set(self.labels) - set([self.platform])) |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 876 | print('%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status, |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 877 | self.locked, self.platform, |
Derek Beckett | b05fb5a | 2020-08-11 14:49:47 -0700 | [diff] [blame] | 878 | ', '.join(labels))) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 879 | |
| 880 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 881 | def delete(self): |
| 882 | return self.afe.run('delete_host', id=self.id) |
| 883 | |
| 884 | |
mbligh | 6463c4b | 2009-01-30 00:33:37 +0000 | [diff] [blame] | 885 | def modify(self, **dargs): |
| 886 | return self.afe.run('modify_host', id=self.id, **dargs) |
| 887 | |
| 888 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 889 | def get_acls(self): |
| 890 | return self.afe.get_acls(hosts__hostname=self.hostname) |
| 891 | |
| 892 | |
| 893 | def add_acl(self, acl_name): |
| 894 | self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname)) |
| 895 | return self.afe.run('acl_group_add_hosts', id=acl_name, |
| 896 | hosts=[self.hostname]) |
| 897 | |
| 898 | |
| 899 | def remove_acl(self, acl_name): |
| 900 | self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname)) |
| 901 | return self.afe.run('acl_group_remove_hosts', id=acl_name, |
| 902 | hosts=[self.hostname]) |
| 903 | |
| 904 | |
| 905 | def get_labels(self): |
| 906 | return self.afe.get_labels(host__hostname__in=[self.hostname]) |
| 907 | |
| 908 | |
| 909 | def add_labels(self, labels): |
| 910 | self.afe.log('Adding labels %s to host %s' % (labels, self.hostname)) |
| 911 | return self.afe.run('host_add_labels', id=self.id, labels=labels) |
| 912 | |
| 913 | |
| 914 | def remove_labels(self, labels): |
| 915 | self.afe.log('Removing labels %s from host %s' % (labels,self.hostname)) |
| 916 | return self.afe.run('host_remove_labels', id=self.id, labels=labels) |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 917 | |
| 918 | |
Allen Li | f74957d | 2017-11-20 17:46:48 -0800 | [diff] [blame] | 919 | def is_available(self): |
| 920 | """Check whether DUT host is available. |
| 921 | |
| 922 | @return: bool |
| 923 | """ |
| 924 | return not (self.locked |
| 925 | or self.status in host_states.UNAVAILABLE_STATES) |
| 926 | |
| 927 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 928 | class User(RpcObject): |
| 929 | def __repr__(self): |
| 930 | return 'USER: %s' % self.login |
| 931 | |
| 932 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 933 | class TestStatus(RpcObject): |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 934 | """ |
| 935 | TKO test status object |
| 936 | |
| 937 | Fields: |
| 938 | test_idx, hostname, testname, id |
| 939 | complete_count, incomplete_count, group_count, pass_count |
| 940 | """ |
| 941 | def __repr__(self): |
| 942 | return 'TEST STATUS: %s' % self.id |
| 943 | |
| 944 | |
MK Ryu | acf3592 | 2014-10-03 14:56:49 -0700 | [diff] [blame] | 945 | class HostAttribute(RpcObject): |
| 946 | """ |
| 947 | AFE host attribute object |
| 948 | |
| 949 | Fields: |
| 950 | id, host, attribute, value |
| 951 | """ |
| 952 | def __repr__(self): |
| 953 | return 'HOST ATTRIBUTE %d' % self.id |