Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 1 | #pylint: disable-msg=C0111 |
xixuan | ba232a3 | 2016-08-25 17:01:59 -0700 | [diff] [blame] | 2 | """ |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 3 | Utility functions for rpc_interface.py. We keep them in a separate file so that |
| 4 | only RPC interface functions go into that file. |
| 5 | """ |
| 6 | |
| 7 | __author__ = 'showard@google.com (Steve Howard)' |
| 8 | |
Allen Li | 3d4e611 | 2016-12-28 11:10:25 -0800 | [diff] [blame] | 9 | import collections |
MK Ryu | 84573e1 | 2015-02-18 15:54:09 -0800 | [diff] [blame] | 10 | import datetime |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 11 | from functools import wraps |
MK Ryu | 84573e1 | 2015-02-18 15:54:09 -0800 | [diff] [blame] | 12 | import inspect |
| 13 | import os |
| 14 | import sys |
Fang Deng | 7051fe4 | 2015-10-20 14:57:28 -0700 | [diff] [blame] | 15 | import django.db.utils |
showard | 3d6ae11 | 2009-05-02 00:45:48 +0000 | [diff] [blame] | 16 | import django.http |
MK Ryu | 0a9c82e | 2015-09-17 17:54:01 -0700 | [diff] [blame] | 17 | |
| 18 | from autotest_lib.frontend import thread_local |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 19 | from autotest_lib.frontend.afe import models, model_logic |
Alex Miller | 4a19369 | 2013-08-21 13:59:01 -0700 | [diff] [blame] | 20 | from autotest_lib.client.common_lib import control_data, error |
Allen Li | 352b86a | 2016-12-14 12:11:27 -0800 | [diff] [blame] | 21 | from autotest_lib.client.common_lib import global_config |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 22 | from autotest_lib.client.common_lib import time_utils |
Allen Li | 3d43e60 | 2016-12-08 15:09:51 -0800 | [diff] [blame] | 23 | from autotest_lib.client.common_lib.cros import dev_server |
Aviv Keshet | 14cac44 | 2016-11-20 21:44:11 -0800 | [diff] [blame] | 24 | # TODO(akeshet): Replace with monarch once we know how to instrument rpc server |
| 25 | # with ts_mon. |
MK Ryu | 509516b | 2015-05-18 12:00:47 -0700 | [diff] [blame] | 26 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 27 | from autotest_lib.server import utils as server_utils |
MK Ryu | 9651ca5 | 2015-06-08 17:48:22 -0700 | [diff] [blame] | 28 | from autotest_lib.server.cros import provision |
| 29 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 30 | |
showard | a62866b | 2008-07-28 21:27:41 +0000 | [diff] [blame] | 31 | NULL_DATETIME = datetime.datetime.max |
| 32 | NULL_DATE = datetime.date.max |
Fang Deng | 7051fe4 | 2015-10-20 14:57:28 -0700 | [diff] [blame] | 33 | DUPLICATE_KEY_MSG = 'Duplicate entry' |
showard | a62866b | 2008-07-28 21:27:41 +0000 | [diff] [blame] | 34 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 35 | def prepare_for_serialization(objects): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 36 | """ |
| 37 | Prepare Python objects to be returned via RPC. |
Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 38 | @param objects: objects to be prepared. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | """ |
| 40 | if (isinstance(objects, list) and len(objects) and |
| 41 | isinstance(objects[0], dict) and 'id' in objects[0]): |
Allen Li | afb7d32 | 2016-12-14 18:40:20 -0800 | [diff] [blame] | 42 | objects = _gather_unique_dicts(objects) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 43 | return _prepare_data(objects) |
showard | b8d3424 | 2008-04-25 18:11:16 +0000 | [diff] [blame] | 44 | |
| 45 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 46 | def prepare_rows_as_nested_dicts(query, nested_dict_column_names): |
| 47 | """ |
| 48 | Prepare a Django query to be returned via RPC as a sequence of nested |
| 49 | dictionaries. |
| 50 | |
| 51 | @param query - A Django model query object with a select_related() method. |
| 52 | @param nested_dict_column_names - A list of column/attribute names for the |
| 53 | rows returned by query to expand into nested dictionaries using |
| 54 | their get_object_dict() method when not None. |
| 55 | |
| 56 | @returns An list suitable to returned in an RPC. |
| 57 | """ |
| 58 | all_dicts = [] |
| 59 | for row in query.select_related(): |
| 60 | row_dict = row.get_object_dict() |
| 61 | for column in nested_dict_column_names: |
| 62 | if row_dict[column] is not None: |
| 63 | row_dict[column] = getattr(row, column).get_object_dict() |
| 64 | all_dicts.append(row_dict) |
| 65 | return prepare_for_serialization(all_dicts) |
| 66 | |
| 67 | |
showard | b8d3424 | 2008-04-25 18:11:16 +0000 | [diff] [blame] | 68 | def _prepare_data(data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 69 | """ |
| 70 | Recursively process data structures, performing necessary type |
| 71 | conversions to values in data to allow for RPC serialization: |
| 72 | -convert datetimes to strings |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 73 | -convert tuples and sets to lists |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 74 | """ |
| 75 | if isinstance(data, dict): |
| 76 | new_data = {} |
| 77 | for key, value in data.iteritems(): |
| 78 | new_data[key] = _prepare_data(value) |
| 79 | return new_data |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 80 | elif (isinstance(data, list) or isinstance(data, tuple) or |
| 81 | isinstance(data, set)): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 82 | return [_prepare_data(item) for item in data] |
showard | 9865997 | 2008-07-17 17:00:07 +0000 | [diff] [blame] | 83 | elif isinstance(data, datetime.date): |
showard | a62866b | 2008-07-28 21:27:41 +0000 | [diff] [blame] | 84 | if data is NULL_DATETIME or data is NULL_DATE: |
| 85 | return None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 86 | return str(data) |
| 87 | else: |
| 88 | return data |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 89 | |
| 90 | |
Moises Osorio | 2dda22e | 2014-09-16 15:56:24 -0700 | [diff] [blame] | 91 | def fetchall_as_list_of_dicts(cursor): |
| 92 | """ |
| 93 | Converts each row in the cursor to a dictionary so that values can be read |
| 94 | by using the column name. |
| 95 | @param cursor: The database cursor to read from. |
| 96 | @returns: A list of each row in the cursor as a dictionary. |
| 97 | """ |
| 98 | desc = cursor.description |
| 99 | return [ dict(zip([col[0] for col in desc], row)) |
| 100 | for row in cursor.fetchall() ] |
| 101 | |
| 102 | |
showard | 3d6ae11 | 2009-05-02 00:45:48 +0000 | [diff] [blame] | 103 | def raw_http_response(response_data, content_type=None): |
| 104 | response = django.http.HttpResponse(response_data, mimetype=content_type) |
| 105 | response['Content-length'] = str(len(response.content)) |
| 106 | return response |
| 107 | |
| 108 | |
Allen Li | afb7d32 | 2016-12-14 18:40:20 -0800 | [diff] [blame] | 109 | def _gather_unique_dicts(dict_iterable): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 110 | """\ |
| 111 | Pick out unique objects (by ID) from an iterable of object dicts. |
| 112 | """ |
Allen Li | 07478c0 | 2016-12-14 18:39:30 -0800 | [diff] [blame] | 113 | objects = collections.OrderedDict() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 114 | for obj in dict_iterable: |
Allen Li | 07478c0 | 2016-12-14 18:39:30 -0800 | [diff] [blame] | 115 | objects.setdefault(obj['id'], obj) |
| 116 | return objects.values() |
showard | b0dfb9f | 2008-06-06 18:08:02 +0000 | [diff] [blame] | 117 | |
| 118 | |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 119 | def extra_job_status_filters(not_yet_run=False, running=False, finished=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 120 | """\ |
| 121 | Generate a SQL WHERE clause for job status filtering, and return it in |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 122 | a dict of keyword args to pass to query.extra(). |
showard | 6c65d25 | 2009-10-01 18:45:22 +0000 | [diff] [blame] | 123 | * not_yet_run: all HQEs are Queued |
| 124 | * finished: all HQEs are complete |
| 125 | * running: everything else |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 126 | """ |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 127 | if not (not_yet_run or running or finished): |
| 128 | return {} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 129 | not_queued = ('(SELECT job_id FROM afe_host_queue_entries ' |
| 130 | 'WHERE status != "%s")' |
showard | 6c65d25 | 2009-10-01 18:45:22 +0000 | [diff] [blame] | 131 | % models.HostQueueEntry.Status.QUEUED) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 132 | not_finished = ('(SELECT job_id FROM afe_host_queue_entries ' |
| 133 | 'WHERE not complete)') |
showard | 6c65d25 | 2009-10-01 18:45:22 +0000 | [diff] [blame] | 134 | |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 135 | where = [] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 136 | if not_yet_run: |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 137 | where.append('id NOT IN ' + not_queued) |
| 138 | if running: |
| 139 | where.append('(id IN %s) AND (id IN %s)' % (not_queued, not_finished)) |
| 140 | if finished: |
| 141 | where.append('id NOT IN ' + not_finished) |
| 142 | return {'where': [' OR '.join(['(%s)' % x for x in where])]} |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 143 | |
| 144 | |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 145 | def extra_job_type_filters(extra_args, suite=False, |
| 146 | sub=False, standalone=False): |
| 147 | """\ |
| 148 | Generate a SQL WHERE clause for job status filtering, and return it in |
| 149 | a dict of keyword args to pass to query.extra(). |
| 150 | |
| 151 | param extra_args: a dict of existing extra_args. |
| 152 | |
| 153 | No more than one of the parameters should be passed as True: |
| 154 | * suite: job which is parent of other jobs |
| 155 | * sub: job with a parent job |
| 156 | * standalone: job with no child or parent jobs |
| 157 | """ |
| 158 | assert not ((suite and sub) or |
| 159 | (suite and standalone) or |
| 160 | (sub and standalone)), ('Cannot specify more than one ' |
| 161 | 'filter to this function') |
| 162 | |
| 163 | where = extra_args.get('where', []) |
| 164 | parent_job_id = ('DISTINCT parent_job_id') |
| 165 | child_job_id = ('id') |
| 166 | filter_common = ('(SELECT %s FROM afe_jobs ' |
| 167 | 'WHERE parent_job_id IS NOT NULL)') |
| 168 | |
| 169 | if suite: |
| 170 | where.append('id IN ' + filter_common % parent_job_id) |
| 171 | elif sub: |
| 172 | where.append('id IN ' + filter_common % child_job_id) |
| 173 | elif standalone: |
| 174 | where.append('NOT EXISTS (SELECT 1 from afe_jobs AS sub_query ' |
| 175 | 'WHERE parent_job_id IS NOT NULL' |
| 176 | ' AND (sub_query.parent_job_id=afe_jobs.id' |
| 177 | ' OR sub_query.id=afe_jobs.id))') |
| 178 | else: |
| 179 | return extra_args |
| 180 | |
| 181 | extra_args['where'] = where |
| 182 | return extra_args |
| 183 | |
| 184 | |
| 185 | |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 186 | def extra_host_filters(multiple_labels=()): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 187 | """\ |
| 188 | Generate SQL WHERE clauses for matching hosts in an intersection of |
| 189 | labels. |
| 190 | """ |
| 191 | extra_args = {} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 192 | where_str = ('afe_hosts.id in (select host_id from afe_hosts_labels ' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 193 | 'where label_id=%s)') |
| 194 | extra_args['where'] = [where_str] * len(multiple_labels) |
| 195 | extra_args['params'] = [models.Label.smart_get(label).id |
| 196 | for label in multiple_labels] |
| 197 | return extra_args |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 198 | |
| 199 | |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 200 | def get_host_query(multiple_labels, exclude_only_if_needed_labels, |
showard | 8aa84fc | 2009-09-16 17:17:55 +0000 | [diff] [blame] | 201 | exclude_atomic_group_hosts, valid_only, filter_data): |
| 202 | if valid_only: |
| 203 | query = models.Host.valid_objects.all() |
| 204 | else: |
| 205 | query = models.Host.objects.all() |
| 206 | |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 207 | if exclude_only_if_needed_labels: |
| 208 | only_if_needed_labels = models.Label.valid_objects.filter( |
| 209 | only_if_needed=True) |
showard | f7eac6f | 2008-11-13 21:18:01 +0000 | [diff] [blame] | 210 | if only_if_needed_labels.count() > 0: |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 211 | only_if_needed_ids = ','.join( |
| 212 | str(label['id']) |
| 213 | for label in only_if_needed_labels.values('id')) |
showard | f7eac6f | 2008-11-13 21:18:01 +0000 | [diff] [blame] | 214 | query = models.Host.objects.add_join( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 215 | query, 'afe_hosts_labels', join_key='host_id', |
| 216 | join_condition=('afe_hosts_labels_exclude_OIN.label_id IN (%s)' |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 217 | % only_if_needed_ids), |
| 218 | suffix='_exclude_OIN', exclude=True) |
showard | 8aa84fc | 2009-09-16 17:17:55 +0000 | [diff] [blame] | 219 | |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 220 | if exclude_atomic_group_hosts: |
| 221 | atomic_group_labels = models.Label.valid_objects.filter( |
| 222 | atomic_group__isnull=False) |
| 223 | if atomic_group_labels.count() > 0: |
| 224 | atomic_group_label_ids = ','.join( |
| 225 | str(atomic_group['id']) |
| 226 | for atomic_group in atomic_group_labels.values('id')) |
| 227 | query = models.Host.objects.add_join( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 228 | query, 'afe_hosts_labels', join_key='host_id', |
| 229 | join_condition=( |
| 230 | 'afe_hosts_labels_exclude_AG.label_id IN (%s)' |
| 231 | % atomic_group_label_ids), |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 232 | suffix='_exclude_AG', exclude=True) |
Fang Deng | 04d3061 | 2013-04-10 18:13:13 -0700 | [diff] [blame] | 233 | try: |
| 234 | assert 'extra_args' not in filter_data |
| 235 | filter_data['extra_args'] = extra_host_filters(multiple_labels) |
| 236 | return models.Host.query_objects(filter_data, initial_query=query) |
Allen Li | 12b9bc1 | 2016-12-14 18:41:19 -0800 | [diff] [blame] | 237 | except models.Label.DoesNotExist: |
Fang Deng | 04d3061 | 2013-04-10 18:13:13 -0700 | [diff] [blame] | 238 | return models.Host.objects.none() |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 239 | |
| 240 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 241 | class InconsistencyException(Exception): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 242 | 'Raised when a list of objects does not have a consistent value' |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 243 | |
| 244 | |
| 245 | def get_consistent_value(objects, field): |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 246 | if not objects: |
| 247 | # well a list of nothing is consistent |
| 248 | return None |
| 249 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 250 | value = getattr(objects[0], field) |
| 251 | for obj in objects: |
| 252 | this_value = getattr(obj, field) |
| 253 | if this_value != value: |
| 254 | raise InconsistencyException(objects[0], obj) |
| 255 | return value |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 256 | |
| 257 | |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 258 | def afe_test_dict_to_test_object(test_dict): |
| 259 | if not isinstance(test_dict, dict): |
| 260 | return test_dict |
| 261 | |
| 262 | numerized_dict = {} |
| 263 | for key, value in test_dict.iteritems(): |
| 264 | try: |
| 265 | numerized_dict[key] = int(value) |
| 266 | except (ValueError, TypeError): |
| 267 | numerized_dict[key] = value |
| 268 | |
| 269 | return type('TestObject', (object,), numerized_dict) |
| 270 | |
| 271 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 272 | def _check_is_server_test(test_type): |
| 273 | """Checks if the test type is a server test. |
| 274 | |
| 275 | @param test_type The test type in enum integer or string. |
| 276 | |
| 277 | @returns A boolean to identify if the test type is server test. |
| 278 | """ |
| 279 | if test_type is not None: |
| 280 | if isinstance(test_type, basestring): |
| 281 | try: |
| 282 | test_type = control_data.CONTROL_TYPE.get_value(test_type) |
| 283 | except AttributeError: |
| 284 | return False |
| 285 | return (test_type == control_data.CONTROL_TYPE.SERVER) |
| 286 | return False |
| 287 | |
| 288 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 289 | def prepare_generate_control_file(tests, profilers, db_tests=True): |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 290 | if db_tests: |
| 291 | test_objects = [models.Test.smart_get(test) for test in tests] |
| 292 | else: |
| 293 | test_objects = [afe_test_dict_to_test_object(test) for test in tests] |
| 294 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 295 | profiler_objects = [models.Profiler.smart_get(profiler) |
| 296 | for profiler in profilers] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 297 | # ensure tests are all the same type |
| 298 | try: |
| 299 | test_type = get_consistent_value(test_objects, 'test_type') |
| 300 | except InconsistencyException, exc: |
| 301 | test1, test2 = exc.args |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 302 | raise model_logic.ValidationError( |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 303 | {'tests' : 'You cannot run both test_suites and server-side ' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 304 | 'tests together (tests %s and %s differ' % ( |
| 305 | test1.name, test2.name)}) |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 306 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 307 | is_server = _check_is_server_test(test_type) |
showard | 14374b1 | 2009-01-31 00:11:54 +0000 | [diff] [blame] | 308 | if test_objects: |
| 309 | synch_count = max(test.sync_count for test in test_objects) |
| 310 | else: |
| 311 | synch_count = 1 |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 312 | |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 313 | if db_tests: |
| 314 | dependencies = set(label.name for label |
| 315 | in models.Label.objects.filter(test__in=test_objects)) |
| 316 | else: |
| 317 | dependencies = reduce( |
| 318 | set.union, [set(test.dependencies) for test in test_objects]) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 319 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 320 | cf_info = dict(is_server=is_server, synch_count=synch_count, |
| 321 | dependencies=list(dependencies)) |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 322 | return cf_info, test_objects, profiler_objects |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 323 | |
| 324 | |
| 325 | def check_job_dependencies(host_objects, job_dependencies): |
| 326 | """ |
| 327 | Check that a set of machines satisfies a job's dependencies. |
| 328 | host_objects: list of models.Host objects |
| 329 | job_dependencies: list of names of labels |
| 330 | """ |
| 331 | # check that hosts satisfy dependencies |
| 332 | host_ids = [host.id for host in host_objects] |
| 333 | hosts_in_job = models.Host.objects.filter(id__in=host_ids) |
| 334 | ok_hosts = hosts_in_job |
| 335 | for index, dependency in enumerate(job_dependencies): |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 336 | if not provision.is_for_special_action(dependency): |
Aviv Keshet | c68807e | 2013-07-31 16:13:01 -0700 | [diff] [blame] | 337 | ok_hosts = ok_hosts.filter(labels__name=dependency) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 338 | failing_hosts = (set(host.hostname for host in host_objects) - |
| 339 | set(host.hostname for host in ok_hosts)) |
| 340 | if failing_hosts: |
| 341 | raise model_logic.ValidationError( |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 342 | {'hosts' : 'Host(s) failed to meet job dependencies (' + |
| 343 | (', '.join(job_dependencies)) + '): ' + |
| 344 | (', '.join(failing_hosts))}) |
| 345 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 346 | |
Alex Miller | 4a19369 | 2013-08-21 13:59:01 -0700 | [diff] [blame] | 347 | def check_job_metahost_dependencies(metahost_objects, job_dependencies): |
| 348 | """ |
| 349 | Check that at least one machine within the metahost spec satisfies the job's |
| 350 | dependencies. |
| 351 | |
| 352 | @param metahost_objects A list of label objects representing the metahosts. |
| 353 | @param job_dependencies A list of strings of the required label names. |
| 354 | @raises NoEligibleHostException If a metahost cannot run the job. |
| 355 | """ |
| 356 | for metahost in metahost_objects: |
| 357 | hosts = models.Host.objects.filter(labels=metahost) |
| 358 | for label_name in job_dependencies: |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 359 | if not provision.is_for_special_action(label_name): |
Alex Miller | 4a19369 | 2013-08-21 13:59:01 -0700 | [diff] [blame] | 360 | hosts = hosts.filter(labels__name=label_name) |
| 361 | if not any(hosts): |
| 362 | raise error.NoEligibleHostException("No hosts within %s satisfy %s." |
| 363 | % (metahost.name, ', '.join(job_dependencies))) |
| 364 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 365 | |
| 366 | def _execution_key_for(host_queue_entry): |
| 367 | return (host_queue_entry.job.id, host_queue_entry.execution_subdir) |
| 368 | |
| 369 | |
| 370 | def check_abort_synchronous_jobs(host_queue_entries): |
| 371 | # ensure user isn't aborting part of a synchronous autoserv execution |
| 372 | count_per_execution = {} |
| 373 | for queue_entry in host_queue_entries: |
| 374 | key = _execution_key_for(queue_entry) |
| 375 | count_per_execution.setdefault(key, 0) |
| 376 | count_per_execution[key] += 1 |
| 377 | |
| 378 | for queue_entry in host_queue_entries: |
| 379 | if not queue_entry.execution_subdir: |
| 380 | continue |
| 381 | execution_count = count_per_execution[_execution_key_for(queue_entry)] |
| 382 | if execution_count < queue_entry.job.synch_count: |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 383 | raise model_logic.ValidationError( |
| 384 | {'' : 'You cannot abort part of a synchronous job execution ' |
| 385 | '(%d/%s), %d included, %d expected' |
| 386 | % (queue_entry.job.id, queue_entry.execution_subdir, |
| 387 | execution_count, queue_entry.job.synch_count)}) |
showard | 8fbae65 | 2009-01-20 23:23:10 +0000 | [diff] [blame] | 388 | |
| 389 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 390 | def check_atomic_group_create_job(synch_count, host_objects, metahost_objects, |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 391 | dependencies, atomic_group): |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 392 | """ |
| 393 | Attempt to reject create_job requests with an atomic group that |
| 394 | will be impossible to schedule. The checks are not perfect but |
| 395 | should catch the most obvious issues. |
| 396 | |
| 397 | @param synch_count - The job's minimum synch count. |
| 398 | @param host_objects - A list of models.Host instances. |
| 399 | @param metahost_objects - A list of models.Label instances. |
| 400 | @param dependencies - A list of job dependency label names. |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 401 | @param labels_by_name - A dictionary mapping label names to models.Label |
| 402 | instance. Used to look up instances for dependencies. |
| 403 | |
| 404 | @raises model_logic.ValidationError - When an issue is found. |
| 405 | """ |
Allen Li | 224aa55 | 2016-12-14 17:17:22 -0800 | [diff] [blame] | 406 | if synch_count and synch_count > atomic_group.max_number_of_machines: |
| 407 | raise model_logic.ValidationError( |
| 408 | {'atomic_group_name' : |
| 409 | 'You have requested a synch_count (%d) greater than the ' |
| 410 | 'maximum machines in the requested Atomic Group (%d).' % |
| 411 | (synch_count, atomic_group.max_number_of_machines)}) |
| 412 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 413 | # If specific host objects were supplied with an atomic group, verify |
| 414 | # that there are enough to satisfy the synch_count. |
| 415 | minimum_required = synch_count or 1 |
| 416 | if (host_objects and not metahost_objects and |
| 417 | len(host_objects) < minimum_required): |
| 418 | raise model_logic.ValidationError( |
| 419 | {'hosts': |
| 420 | 'only %d hosts provided for job with synch_count = %d' % |
| 421 | (len(host_objects), synch_count)}) |
| 422 | |
| 423 | # Check that the atomic group has a hope of running this job |
| 424 | # given any supplied metahosts and dependancies that may limit. |
| 425 | |
| 426 | # Get a set of hostnames in the atomic group. |
| 427 | possible_hosts = set() |
| 428 | for label in atomic_group.label_set.all(): |
| 429 | possible_hosts.update(h.hostname for h in label.host_set.all()) |
| 430 | |
| 431 | # Filter out hosts that don't match all of the job dependency labels. |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 432 | for label in models.Label.objects.filter(name__in=dependencies): |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 433 | hosts_in_label = (h.hostname for h in label.host_set.all()) |
| 434 | possible_hosts.intersection_update(hosts_in_label) |
| 435 | |
showard | 225bdc1 | 2009-04-13 16:09:21 +0000 | [diff] [blame] | 436 | if not host_objects and not metahost_objects: |
| 437 | # No hosts or metahosts are required to queue an atomic group Job. |
| 438 | # However, if they are given, we respect them below. |
| 439 | host_set = possible_hosts |
| 440 | else: |
| 441 | host_set = set(host.hostname for host in host_objects) |
| 442 | unusable_host_set = host_set.difference(possible_hosts) |
| 443 | if unusable_host_set: |
| 444 | raise model_logic.ValidationError( |
| 445 | {'hosts': 'Hosts "%s" are not in Atomic Group "%s"' % |
| 446 | (', '.join(sorted(unusable_host_set)), atomic_group.name)}) |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 447 | |
| 448 | # Lookup hosts provided by each meta host and merge them into the |
| 449 | # host_set for final counting. |
| 450 | for meta_host in metahost_objects: |
| 451 | meta_possible = possible_hosts.copy() |
| 452 | hosts_in_meta_host = (h.hostname for h in meta_host.host_set.all()) |
| 453 | meta_possible.intersection_update(hosts_in_meta_host) |
| 454 | |
| 455 | # Count all hosts that this meta_host will provide. |
| 456 | host_set.update(meta_possible) |
| 457 | |
| 458 | if len(host_set) < minimum_required: |
| 459 | raise model_logic.ValidationError( |
| 460 | {'atomic_group_name': |
| 461 | 'Insufficient hosts in Atomic Group "%s" with the' |
| 462 | ' supplied dependencies and meta_hosts.' % |
| 463 | (atomic_group.name,)}) |
| 464 | |
| 465 | |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 466 | def check_modify_host(update_data): |
| 467 | """ |
| 468 | Sanity check modify_host* requests. |
| 469 | |
| 470 | @param update_data: A dictionary with the changes to make to a host |
| 471 | or hosts. |
| 472 | """ |
| 473 | # Only the scheduler (monitor_db) is allowed to modify Host status. |
| 474 | # Otherwise race conditions happen as a hosts state is changed out from |
| 475 | # beneath tasks being run on a host. |
| 476 | if 'status' in update_data: |
| 477 | raise model_logic.ValidationError({ |
| 478 | 'status': 'Host status can not be modified by the frontend.'}) |
| 479 | |
| 480 | |
showard | ce7c092 | 2009-09-11 18:39:24 +0000 | [diff] [blame] | 481 | def check_modify_host_locking(host, update_data): |
| 482 | """ |
| 483 | Checks when locking/unlocking has been requested if the host is already |
| 484 | locked/unlocked. |
| 485 | |
| 486 | @param host: models.Host object to be modified |
| 487 | @param update_data: A dictionary with the changes to make to the host. |
| 488 | """ |
| 489 | locked = update_data.get('locked', None) |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 490 | lock_reason = update_data.get('lock_reason', None) |
showard | ce7c092 | 2009-09-11 18:39:24 +0000 | [diff] [blame] | 491 | if locked is not None: |
| 492 | if locked and host.locked: |
| 493 | raise model_logic.ValidationError({ |
Shuqian Zhao | 4c0d290 | 2016-01-12 17:03:15 -0800 | [diff] [blame] | 494 | 'locked': 'Host %s already locked by %s on %s.' % |
| 495 | (host.hostname, host.locked_by, host.lock_time)}) |
showard | ce7c092 | 2009-09-11 18:39:24 +0000 | [diff] [blame] | 496 | if not locked and not host.locked: |
| 497 | raise model_logic.ValidationError({ |
Shuqian Zhao | 4c0d290 | 2016-01-12 17:03:15 -0800 | [diff] [blame] | 498 | 'locked': 'Host %s already unlocked.' % host.hostname}) |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 499 | if locked and not lock_reason and not host.locked: |
| 500 | raise model_logic.ValidationError({ |
Shuqian Zhao | 4c0d290 | 2016-01-12 17:03:15 -0800 | [diff] [blame] | 501 | 'locked': 'Please provide a reason for locking Host %s' % |
| 502 | host.hostname}) |
showard | ce7c092 | 2009-09-11 18:39:24 +0000 | [diff] [blame] | 503 | |
| 504 | |
showard | 8fbae65 | 2009-01-20 23:23:10 +0000 | [diff] [blame] | 505 | def get_motd(): |
| 506 | dirname = os.path.dirname(__file__) |
| 507 | filename = os.path.join(dirname, "..", "..", "motd.txt") |
| 508 | text = '' |
| 509 | try: |
| 510 | fp = open(filename, "r") |
| 511 | try: |
| 512 | text = fp.read() |
| 513 | finally: |
| 514 | fp.close() |
| 515 | except: |
| 516 | pass |
| 517 | |
| 518 | return text |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 519 | |
| 520 | |
| 521 | def _get_metahost_counts(metahost_objects): |
| 522 | metahost_counts = {} |
| 523 | for metahost in metahost_objects: |
| 524 | metahost_counts.setdefault(metahost, 0) |
| 525 | metahost_counts[metahost] += 1 |
| 526 | return metahost_counts |
| 527 | |
| 528 | |
showard | a965cef | 2009-05-15 23:17:41 +0000 | [diff] [blame] | 529 | def get_job_info(job, preserve_metahosts=False, queue_entry_filter_data=None): |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 530 | hosts = [] |
| 531 | one_time_hosts = [] |
| 532 | meta_hosts = [] |
| 533 | atomic_group = None |
jamesren | 2275ef1 | 2010-04-12 18:25:06 +0000 | [diff] [blame] | 534 | hostless = False |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 535 | |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 536 | queue_entries = job.hostqueueentry_set.all() |
showard | a965cef | 2009-05-15 23:17:41 +0000 | [diff] [blame] | 537 | if queue_entry_filter_data: |
| 538 | queue_entries = models.HostQueueEntry.query_objects( |
| 539 | queue_entry_filter_data, initial_query=queue_entries) |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 540 | |
| 541 | for queue_entry in queue_entries: |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 542 | if (queue_entry.host and (preserve_metahosts or |
| 543 | not queue_entry.meta_host)): |
| 544 | if queue_entry.deleted: |
| 545 | continue |
| 546 | if queue_entry.host.invalid: |
| 547 | one_time_hosts.append(queue_entry.host) |
| 548 | else: |
| 549 | hosts.append(queue_entry.host) |
jamesren | 2275ef1 | 2010-04-12 18:25:06 +0000 | [diff] [blame] | 550 | elif queue_entry.meta_host: |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 551 | meta_hosts.append(queue_entry.meta_host) |
jamesren | 2275ef1 | 2010-04-12 18:25:06 +0000 | [diff] [blame] | 552 | else: |
| 553 | hostless = True |
| 554 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 555 | if atomic_group is None: |
| 556 | if queue_entry.atomic_group is not None: |
| 557 | atomic_group = queue_entry.atomic_group |
| 558 | else: |
| 559 | assert atomic_group.name == queue_entry.atomic_group.name, ( |
| 560 | 'DB inconsistency. HostQueueEntries with multiple atomic' |
| 561 | ' groups on job %s: %s != %s' % ( |
| 562 | id, atomic_group.name, queue_entry.atomic_group.name)) |
| 563 | |
| 564 | meta_host_counts = _get_metahost_counts(meta_hosts) |
| 565 | |
| 566 | info = dict(dependencies=[label.name for label |
| 567 | in job.dependency_labels.all()], |
| 568 | hosts=hosts, |
| 569 | meta_hosts=meta_hosts, |
| 570 | meta_host_counts=meta_host_counts, |
| 571 | one_time_hosts=one_time_hosts, |
jamesren | 2275ef1 | 2010-04-12 18:25:06 +0000 | [diff] [blame] | 572 | atomic_group=atomic_group, |
| 573 | hostless=hostless) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 574 | return info |
| 575 | |
| 576 | |
showard | 09d80f9 | 2009-11-19 01:01:19 +0000 | [diff] [blame] | 577 | def check_for_duplicate_hosts(host_objects): |
Allen Li | 8239c35 | 2016-12-14 17:53:01 -0800 | [diff] [blame] | 578 | host_counts = collections.Counter(host_objects) |
| 579 | duplicate_hostnames = {host.hostname |
| 580 | for host, count in host_counts.iteritems() |
| 581 | if count > 1} |
showard | 09d80f9 | 2009-11-19 01:01:19 +0000 | [diff] [blame] | 582 | if duplicate_hostnames: |
| 583 | raise model_logic.ValidationError( |
| 584 | {'hosts' : 'Duplicate hosts: %s' |
| 585 | % ', '.join(duplicate_hostnames)}) |
| 586 | |
| 587 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 588 | def create_new_job(owner, options, host_objects, metahost_objects, |
| 589 | atomic_group=None): |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 590 | all_host_objects = host_objects + metahost_objects |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 591 | dependencies = options.get('dependencies', []) |
| 592 | synch_count = options.get('synch_count') |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 593 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 594 | if atomic_group: |
| 595 | check_atomic_group_create_job( |
| 596 | synch_count, host_objects, metahost_objects, |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 597 | dependencies, atomic_group) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 598 | else: |
| 599 | if synch_count is not None and synch_count > len(all_host_objects): |
| 600 | raise model_logic.ValidationError( |
| 601 | {'hosts': |
| 602 | 'only %d hosts provided for job with synch_count = %d' % |
| 603 | (len(all_host_objects), synch_count)}) |
| 604 | atomic_hosts = models.Host.objects.filter( |
| 605 | id__in=[host.id for host in host_objects], |
| 606 | labels__atomic_group=True) |
| 607 | unusable_host_names = [host.hostname for host in atomic_hosts] |
| 608 | if unusable_host_names: |
| 609 | raise model_logic.ValidationError( |
| 610 | {'hosts': |
| 611 | 'Host(s) "%s" are atomic group hosts but no ' |
| 612 | 'atomic group was specified for this job.' % |
| 613 | (', '.join(unusable_host_names),)}) |
| 614 | |
showard | 09d80f9 | 2009-11-19 01:01:19 +0000 | [diff] [blame] | 615 | check_for_duplicate_hosts(host_objects) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 616 | |
Aviv Keshet | c68807e | 2013-07-31 16:13:01 -0700 | [diff] [blame] | 617 | for label_name in dependencies: |
Alex Miller | aa77200 | 2014-04-10 17:51:21 -0700 | [diff] [blame] | 618 | if provision.is_for_special_action(label_name): |
Aviv Keshet | c68807e | 2013-07-31 16:13:01 -0700 | [diff] [blame] | 619 | # TODO: We could save a few queries |
| 620 | # if we had a bulk ensure-label-exists function, which used |
| 621 | # a bulk .get() call. The win is probably very small. |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 622 | _ensure_label_exists(label_name) |
Aviv Keshet | c68807e | 2013-07-31 16:13:01 -0700 | [diff] [blame] | 623 | |
Alex Miller | 4a19369 | 2013-08-21 13:59:01 -0700 | [diff] [blame] | 624 | # This only checks targeted hosts, not hosts eligible due to the metahost |
| 625 | check_job_dependencies(host_objects, dependencies) |
| 626 | check_job_metahost_dependencies(metahost_objects, dependencies) |
| 627 | |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 628 | options['dependencies'] = list( |
| 629 | models.Label.objects.filter(name__in=dependencies)) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 630 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 631 | for label in metahost_objects + options['dependencies']: |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 632 | if label.atomic_group and not atomic_group: |
| 633 | raise model_logic.ValidationError( |
| 634 | {'atomic_group_name': |
showard | c873032 | 2009-06-30 01:56:38 +0000 | [diff] [blame] | 635 | 'Dependency %r requires an atomic group but no ' |
| 636 | 'atomic_group_name or meta_host in an atomic group was ' |
| 637 | 'specified for this job.' % label.name}) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 638 | elif (label.atomic_group and |
| 639 | label.atomic_group.name != atomic_group.name): |
| 640 | raise model_logic.ValidationError( |
| 641 | {'atomic_group_name': |
showard | c873032 | 2009-06-30 01:56:38 +0000 | [diff] [blame] | 642 | 'meta_hosts or dependency %r requires atomic group ' |
| 643 | '%r instead of the supplied atomic_group_name=%r.' % |
| 644 | (label.name, label.atomic_group.name, atomic_group.name)}) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 645 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 646 | job = models.Job.create(owner=owner, options=options, |
| 647 | hosts=all_host_objects) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 648 | job.queue(all_host_objects, atomic_group=atomic_group, |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 649 | is_template=options.get('is_template', False)) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 650 | return job.id |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 651 | |
| 652 | |
Aviv Keshet | c68807e | 2013-07-31 16:13:01 -0700 | [diff] [blame] | 653 | def _ensure_label_exists(name): |
| 654 | """ |
| 655 | Ensure that a label called |name| exists in the Django models. |
| 656 | |
| 657 | This function is to be called from within afe rpcs only, as an |
| 658 | alternative to server.cros.provision.ensure_label_exists(...). It works |
| 659 | by Django model manipulation, rather than by making another create_label |
| 660 | rpc call. |
| 661 | |
| 662 | @param name: the label to check for/create. |
| 663 | @raises ValidationError: There was an error in the response that was |
| 664 | not because the label already existed. |
| 665 | @returns True is a label was created, False otherwise. |
| 666 | """ |
MK Ryu | 73be986 | 2015-07-06 12:25:00 -0700 | [diff] [blame] | 667 | # Make sure this function is not called on shards but only on master. |
| 668 | assert not server_utils.is_shard() |
Aviv Keshet | c68807e | 2013-07-31 16:13:01 -0700 | [diff] [blame] | 669 | try: |
| 670 | models.Label.objects.get(name=name) |
| 671 | except models.Label.DoesNotExist: |
Fang Deng | 7051fe4 | 2015-10-20 14:57:28 -0700 | [diff] [blame] | 672 | try: |
| 673 | new_label = models.Label.objects.create(name=name) |
| 674 | new_label.save() |
| 675 | return True |
| 676 | except django.db.utils.IntegrityError as e: |
| 677 | # It is possible that another suite/test already |
| 678 | # created the label between the check and save. |
| 679 | if DUPLICATE_KEY_MSG in str(e): |
| 680 | return False |
| 681 | else: |
| 682 | raise |
Aviv Keshet | c68807e | 2013-07-31 16:13:01 -0700 | [diff] [blame] | 683 | return False |
| 684 | |
| 685 | |
showard | 909c914 | 2009-07-07 20:54:42 +0000 | [diff] [blame] | 686 | def find_platform_and_atomic_group(host): |
| 687 | """ |
| 688 | Figure out the platform name and atomic group name for the given host |
| 689 | object. If none, the return value for either will be None. |
| 690 | |
| 691 | @returns (platform name, atomic group name) for the given host. |
| 692 | """ |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 693 | platforms = [label.name for label in host.label_list if label.platform] |
| 694 | if not platforms: |
showard | 909c914 | 2009-07-07 20:54:42 +0000 | [diff] [blame] | 695 | platform = None |
| 696 | else: |
| 697 | platform = platforms[0] |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 698 | if len(platforms) > 1: |
| 699 | raise ValueError('Host %s has more than one platform: %s' % |
| 700 | (host.hostname, ', '.join(platforms))) |
showard | 909c914 | 2009-07-07 20:54:42 +0000 | [diff] [blame] | 701 | for label in host.label_list: |
| 702 | if label.atomic_group: |
| 703 | atomic_group_name = label.atomic_group.name |
| 704 | break |
| 705 | else: |
| 706 | atomic_group_name = None |
| 707 | # Don't check for multiple atomic groups on a host here. That is an |
| 708 | # error but should not trip up the RPC interface. monitor_db_cleanup |
| 709 | # deals with it. This just returns the first one found. |
| 710 | return platform, atomic_group_name |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 711 | |
| 712 | |
| 713 | # support for get_host_queue_entries_and_special_tasks() |
| 714 | |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 715 | def _common_entry_to_dict(entry, type, job_dict, exec_path, status, started_on): |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 716 | return dict(type=type, |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 717 | host=entry['host'], |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 718 | job=job_dict, |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 719 | execution_path=exec_path, |
| 720 | status=status, |
| 721 | started_on=started_on, |
| 722 | id=str(entry['id']) + type, |
| 723 | oid=entry['id']) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 724 | |
| 725 | |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 726 | def _special_task_to_dict(task, queue_entries): |
| 727 | """Transforms a special task dictionary to another form of dictionary. |
| 728 | |
| 729 | @param task Special task as a dictionary type |
| 730 | @param queue_entries Host queue entries as a list of dictionaries. |
| 731 | |
| 732 | @return Transformed dictionary for a special task. |
| 733 | """ |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 734 | job_dict = None |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 735 | if task['queue_entry']: |
| 736 | # Scan queue_entries to get the job detail info. |
| 737 | for qentry in queue_entries: |
| 738 | if task['queue_entry']['id'] == qentry['id']: |
| 739 | job_dict = qentry['job'] |
| 740 | break |
| 741 | # If not found, get it from DB. |
| 742 | if job_dict is None: |
| 743 | job = models.Job.objects.get(id=task['queue_entry']['job']) |
| 744 | job_dict = job.get_object_dict() |
| 745 | |
| 746 | exec_path = server_utils.get_special_task_exec_path( |
| 747 | task['host']['hostname'], task['id'], task['task'], |
| 748 | time_utils.time_string_to_datetime(task['time_requested'])) |
| 749 | status = server_utils.get_special_task_status( |
| 750 | task['is_complete'], task['success'], task['is_active']) |
| 751 | return _common_entry_to_dict(task, task['task'], job_dict, |
| 752 | exec_path, status, task['time_started']) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 753 | |
| 754 | |
| 755 | def _queue_entry_to_dict(queue_entry): |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 756 | job_dict = queue_entry['job'] |
| 757 | tag = server_utils.get_job_tag(job_dict['id'], job_dict['owner']) |
| 758 | exec_path = server_utils.get_hqe_exec_path(tag, |
| 759 | queue_entry['execution_subdir']) |
| 760 | return _common_entry_to_dict(queue_entry, 'Job', job_dict, exec_path, |
| 761 | queue_entry['status'], queue_entry['started_on']) |
| 762 | |
| 763 | |
| 764 | def prepare_host_queue_entries_and_special_tasks(interleaved_entries, |
| 765 | queue_entries): |
| 766 | """ |
| 767 | Prepare for serialization the interleaved entries of host queue entries |
| 768 | and special tasks. |
| 769 | Each element in the entries is a dictionary type. |
| 770 | The special task dictionary has only a job id for a job and lacks |
| 771 | the detail of the job while the host queue entry dictionary has. |
| 772 | queue_entries is used to look up the job detail info. |
| 773 | |
| 774 | @param interleaved_entries Host queue entries and special tasks as a list |
| 775 | of dictionaries. |
| 776 | @param queue_entries Host queue entries as a list of dictionaries. |
| 777 | |
| 778 | @return A post-processed list of dictionaries that is to be serialized. |
| 779 | """ |
| 780 | dict_list = [] |
| 781 | for e in interleaved_entries: |
| 782 | # Distinguish the two mixed entries based on the existence of |
| 783 | # the key "task". If an entry has the key, the entry is for |
| 784 | # special task. Otherwise, host queue entry. |
| 785 | if 'task' in e: |
| 786 | dict_list.append(_special_task_to_dict(e, queue_entries)) |
| 787 | else: |
| 788 | dict_list.append(_queue_entry_to_dict(e)) |
| 789 | return prepare_for_serialization(dict_list) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 790 | |
| 791 | |
| 792 | def _compute_next_job_for_tasks(queue_entries, special_tasks): |
| 793 | """ |
| 794 | For each task, try to figure out the next job that ran after that task. |
| 795 | This is done using two pieces of information: |
| 796 | * if the task has a queue entry, we can use that entry's job ID. |
| 797 | * if the task has a time_started, we can try to compare that against the |
| 798 | started_on field of queue_entries. this isn't guaranteed to work perfectly |
| 799 | since queue_entries may also have null started_on values. |
| 800 | * if the task has neither, or if use of time_started fails, just use the |
| 801 | last computed job ID. |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 802 | |
| 803 | @param queue_entries Host queue entries as a list of dictionaries. |
| 804 | @param special_tasks Special tasks as a list of dictionaries. |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 805 | """ |
| 806 | next_job_id = None # most recently computed next job |
| 807 | hqe_index = 0 # index for scanning by started_on times |
| 808 | for task in special_tasks: |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 809 | if task['queue_entry']: |
| 810 | next_job_id = task['queue_entry']['job'] |
| 811 | elif task['time_started'] is not None: |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 812 | for queue_entry in queue_entries[hqe_index:]: |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 813 | if queue_entry['started_on'] is None: |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 814 | continue |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 815 | t1 = time_utils.time_string_to_datetime( |
| 816 | queue_entry['started_on']) |
| 817 | t2 = time_utils.time_string_to_datetime(task['time_started']) |
| 818 | if t1 < t2: |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 819 | break |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 820 | next_job_id = queue_entry['job']['id'] |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 821 | |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 822 | task['next_job_id'] = next_job_id |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 823 | |
| 824 | # advance hqe_index to just after next_job_id |
| 825 | if next_job_id is not None: |
| 826 | for queue_entry in queue_entries[hqe_index:]: |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 827 | if queue_entry['job']['id'] < next_job_id: |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 828 | break |
| 829 | hqe_index += 1 |
| 830 | |
| 831 | |
| 832 | def interleave_entries(queue_entries, special_tasks): |
| 833 | """ |
| 834 | Both lists should be ordered by descending ID. |
| 835 | """ |
| 836 | _compute_next_job_for_tasks(queue_entries, special_tasks) |
| 837 | |
| 838 | # start with all special tasks that've run since the last job |
| 839 | interleaved_entries = [] |
| 840 | for task in special_tasks: |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 841 | if task['next_job_id'] is not None: |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 842 | break |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 843 | interleaved_entries.append(task) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 844 | |
| 845 | # now interleave queue entries with the remaining special tasks |
| 846 | special_task_index = len(interleaved_entries) |
| 847 | for queue_entry in queue_entries: |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 848 | interleaved_entries.append(queue_entry) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 849 | # add all tasks that ran between this job and the previous one |
| 850 | for task in special_tasks[special_task_index:]: |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 851 | if task['next_job_id'] < queue_entry['job']['id']: |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 852 | break |
MK Ryu | 0c1a37d | 2015-04-30 12:00:55 -0700 | [diff] [blame] | 853 | interleaved_entries.append(task) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 854 | special_task_index += 1 |
| 855 | |
| 856 | return interleaved_entries |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 857 | |
| 858 | |
Prashanth Balasubramanian | 6edaaf9 | 2014-11-24 16:36:25 -0800 | [diff] [blame] | 859 | def bucket_hosts_by_shard(host_objs, rpc_hostnames=False): |
| 860 | """Figure out which hosts are on which shards. |
| 861 | |
| 862 | @param host_objs: A list of host objects. |
| 863 | @param rpc_hostnames: If True, the rpc_hostnames of a shard are returned |
| 864 | instead of the 'real' shard hostnames. This only matters for testing |
| 865 | environments. |
| 866 | |
| 867 | @return: A map of shard hostname: list of hosts on the shard. |
| 868 | """ |
Allen Li | 3d4e611 | 2016-12-28 11:10:25 -0800 | [diff] [blame] | 869 | shard_host_map = collections.defaultdict(list) |
Prashanth Balasubramanian | 6edaaf9 | 2014-11-24 16:36:25 -0800 | [diff] [blame] | 870 | for host in host_objs: |
| 871 | if host.shard: |
| 872 | shard_name = (host.shard.rpc_hostname() if rpc_hostnames |
| 873 | else host.shard.hostname) |
Allen Li | 3d4e611 | 2016-12-28 11:10:25 -0800 | [diff] [blame] | 874 | shard_host_map[shard_name].append(host.hostname) |
Prashanth Balasubramanian | 6edaaf9 | 2014-11-24 16:36:25 -0800 | [diff] [blame] | 875 | return shard_host_map |
| 876 | |
| 877 | |
Allen Li | 1453fdf | 2016-12-14 12:16:37 -0800 | [diff] [blame] | 878 | def create_job_common( |
| 879 | name, |
| 880 | priority, |
| 881 | control_type, |
| 882 | control_file=None, |
| 883 | hosts=(), |
| 884 | meta_hosts=(), |
| 885 | one_time_hosts=(), |
| 886 | atomic_group_name=None, |
| 887 | synch_count=None, |
| 888 | is_template=False, |
| 889 | timeout=None, |
| 890 | timeout_mins=None, |
| 891 | max_runtime_mins=None, |
| 892 | run_verify=True, |
| 893 | email_list='', |
| 894 | dependencies=(), |
| 895 | reboot_before=None, |
| 896 | reboot_after=None, |
| 897 | parse_failed_repair=None, |
| 898 | hostless=False, |
| 899 | keyvals=None, |
| 900 | drone_set=None, |
| 901 | parameterized_job=None, |
| 902 | parent_job_id=None, |
| 903 | test_retry=0, |
| 904 | run_reset=True, |
| 905 | require_ssp=None): |
Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 906 | #pylint: disable-msg=C0111 |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 907 | """ |
| 908 | Common code between creating "standard" jobs and creating parameterized jobs |
| 909 | """ |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 910 | # input validation |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 911 | host_args_passed = any(( |
| 912 | hosts, meta_hosts, one_time_hosts, atomic_group_name)) |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 913 | if hostless: |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 914 | if host_args_passed: |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 915 | raise model_logic.ValidationError({ |
| 916 | 'hostless': 'Hostless jobs cannot include any hosts!'}) |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 917 | if control_type != control_data.CONTROL_TYPE_NAMES.SERVER: |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 918 | raise model_logic.ValidationError({ |
| 919 | 'control_type': 'Hostless jobs cannot use client-side ' |
| 920 | 'control files'}) |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 921 | elif not host_args_passed: |
| 922 | raise model_logic.ValidationError({ |
| 923 | 'arguments' : "For host jobs, you must pass at least one of" |
| 924 | " 'hosts', 'meta_hosts', 'one_time_hosts'," |
| 925 | " 'atomic_group_name'." |
| 926 | }) |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 927 | |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 928 | atomic_groups_by_name = { |
| 929 | group.name: group for group in models.AtomicGroup.objects.all() |
| 930 | } |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 931 | label_objects = list(models.Label.objects.filter(name__in=meta_hosts)) |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 932 | |
| 933 | # Schedule on an atomic group automagically if one of the labels given |
| 934 | # is an atomic group label and no explicit atomic_group_name was supplied. |
| 935 | if not atomic_group_name: |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 936 | atomic_group_name = _get_atomic_group_name_from_labels(label_objects) |
| 937 | |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 938 | # convert hostnames & meta hosts to host/label objects |
| 939 | host_objects = models.Host.smart_get_bulk(hosts) |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 940 | _validate_host_job_sharding(host_objects) |
Allen Li | dbc0866 | 2016-12-14 16:38:21 -0800 | [diff] [blame] | 941 | for host in one_time_hosts: |
| 942 | this_host = models.Host.create_one_time_host(host) |
| 943 | host_objects.append(this_host) |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 944 | |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 945 | metahost_objects = [] |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 946 | meta_host_labels_by_name = {label.name: label for label in label_objects} |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 947 | for label_name in meta_hosts: |
Alex Miller | 871291b | 2013-08-08 01:19:20 -0700 | [diff] [blame] | 948 | if label_name in meta_host_labels_by_name: |
| 949 | metahost_objects.append(meta_host_labels_by_name[label_name]) |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 950 | elif label_name in atomic_groups_by_name: |
| 951 | # If given a metahost name that isn't a Label, check to |
| 952 | # see if the user was specifying an Atomic Group instead. |
| 953 | atomic_group = atomic_groups_by_name[label_name] |
| 954 | if atomic_group_name and atomic_group_name != atomic_group.name: |
| 955 | raise model_logic.ValidationError({ |
| 956 | 'meta_hosts': ( |
| 957 | 'Label "%s" not found. If assumed to be an ' |
| 958 | 'atomic group it would conflict with the ' |
| 959 | 'supplied atomic group "%s".' % ( |
| 960 | label_name, atomic_group_name))}) |
| 961 | atomic_group_name = atomic_group.name |
| 962 | else: |
| 963 | raise model_logic.ValidationError( |
| 964 | {'meta_hosts' : 'Label "%s" not found' % label_name}) |
| 965 | |
| 966 | # Create and sanity check an AtomicGroup object if requested. |
| 967 | if atomic_group_name: |
| 968 | if one_time_hosts: |
| 969 | raise model_logic.ValidationError( |
| 970 | {'one_time_hosts': |
| 971 | 'One time hosts cannot be used with an Atomic Group.'}) |
| 972 | atomic_group = models.AtomicGroup.smart_get(atomic_group_name) |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 973 | else: |
| 974 | atomic_group = None |
| 975 | |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 976 | options = dict(name=name, |
| 977 | priority=priority, |
| 978 | control_file=control_file, |
| 979 | control_type=control_type, |
| 980 | is_template=is_template, |
| 981 | timeout=timeout, |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 982 | timeout_mins=timeout_mins, |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 983 | max_runtime_mins=max_runtime_mins, |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 984 | synch_count=synch_count, |
| 985 | run_verify=run_verify, |
| 986 | email_list=email_list, |
| 987 | dependencies=dependencies, |
| 988 | reboot_before=reboot_before, |
| 989 | reboot_after=reboot_after, |
| 990 | parse_failed_repair=parse_failed_repair, |
| 991 | keyvals=keyvals, |
| 992 | drone_set=drone_set, |
Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 993 | parameterized_job=parameterized_job, |
Aviv Keshet | cd1ff9b | 2013-03-01 14:55:19 -0800 | [diff] [blame] | 994 | parent_job_id=parent_job_id, |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 995 | test_retry=test_retry, |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 996 | run_reset=run_reset, |
| 997 | require_ssp=require_ssp) |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 998 | |
| 999 | return create_new_job(owner=models.User.current_user().login, |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1000 | options=options, |
| 1001 | host_objects=host_objects, |
| 1002 | metahost_objects=metahost_objects, |
| 1003 | atomic_group=atomic_group) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 1004 | |
| 1005 | |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 1006 | def _get_atomic_group_name_from_labels(label_objects): |
| 1007 | """Get atomic group name from label objects. |
| 1008 | |
| 1009 | @returns: atomic group name string or None |
| 1010 | """ |
| 1011 | for label in label_objects: |
| 1012 | if label and label.atomic_group: |
| 1013 | return label.atomic_group.name |
| 1014 | |
| 1015 | |
| 1016 | def _validate_host_job_sharding(host_objects): |
| 1017 | """Check that the hosts obey job sharding rules.""" |
| 1018 | if not (server_utils.is_shard() |
| 1019 | or _allowed_hosts_for_master_job(host_objects)): |
| 1020 | shard_host_map = bucket_hosts_by_shard(host_objects) |
| 1021 | raise ValueError( |
| 1022 | 'The following hosts are on shard(s), please create ' |
| 1023 | 'seperate jobs for hosts on each shard: %s ' % |
| 1024 | shard_host_map) |
| 1025 | |
| 1026 | |
| 1027 | def _allowed_hosts_for_master_job(host_objects): |
| 1028 | """Check that the hosts are allowed for a job on master.""" |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 1029 | # We disallow the following jobs on master: |
| 1030 | # num_shards > 1: this is a job spanning across multiple shards. |
| 1031 | # num_shards == 1 but number of hosts on shard is less |
| 1032 | # than total number of hosts: this is a job that spans across |
| 1033 | # one shard and the master. |
Allen Li | acb9792 | 2016-12-14 13:45:50 -0800 | [diff] [blame] | 1034 | shard_host_map = bucket_hosts_by_shard(host_objects) |
| 1035 | num_shards = len(shard_host_map) |
| 1036 | if num_shards > 1: |
| 1037 | return False |
| 1038 | if num_shards == 1: |
| 1039 | hosts_on_shard = shard_host_map.values()[0] |
| 1040 | assert len(hosts_on_shard) <= len(host_objects) |
| 1041 | return len(hosts_on_shard) == len(host_objects) |
| 1042 | else: |
| 1043 | return True |
Allen Li | e620319 | 2016-12-14 13:05:53 -0800 | [diff] [blame] | 1044 | |
| 1045 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 1046 | def encode_ascii(control_file): |
| 1047 | """Force a control file to only contain ascii characters. |
| 1048 | |
| 1049 | @param control_file: Control file to encode. |
| 1050 | |
| 1051 | @returns the control file in an ascii encoding. |
| 1052 | |
| 1053 | @raises error.ControlFileMalformed: if encoding fails. |
| 1054 | """ |
| 1055 | try: |
| 1056 | return control_file.encode('ascii') |
| 1057 | except UnicodeDecodeError as e: |
Jiaxi Luo | 421608e | 2014-07-07 14:38:00 -0700 | [diff] [blame] | 1058 | raise error.ControlFileMalformed(str(e)) |
| 1059 | |
| 1060 | |
| 1061 | def get_wmatrix_url(): |
| 1062 | """Get wmatrix url from config file. |
| 1063 | |
| 1064 | @returns the wmatrix url or an empty string. |
| 1065 | """ |
| 1066 | return global_config.global_config.get_config_value('AUTOTEST_WEB', |
| 1067 | 'wmatrix_url', |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 1068 | default='') |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1069 | |
| 1070 | |
| 1071 | def inject_times_to_filter(start_time_key=None, end_time_key=None, |
| 1072 | start_time_value=None, end_time_value=None, |
| 1073 | **filter_data): |
| 1074 | """Inject the key value pairs of start and end time if provided. |
| 1075 | |
| 1076 | @param start_time_key: A string represents the filter key of start_time. |
| 1077 | @param end_time_key: A string represents the filter key of end_time. |
| 1078 | @param start_time_value: Start_time value. |
| 1079 | @param end_time_value: End_time value. |
| 1080 | |
| 1081 | @returns the injected filter_data. |
| 1082 | """ |
| 1083 | if start_time_value: |
| 1084 | filter_data[start_time_key] = start_time_value |
| 1085 | if end_time_value: |
| 1086 | filter_data[end_time_key] = end_time_value |
| 1087 | return filter_data |
| 1088 | |
| 1089 | |
| 1090 | def inject_times_to_hqe_special_tasks_filters(filter_data_common, |
| 1091 | start_time, end_time): |
| 1092 | """Inject start and end time to hqe and special tasks filters. |
| 1093 | |
| 1094 | @param filter_data_common: Common filter for hqe and special tasks. |
| 1095 | @param start_time_key: A string represents the filter key of start_time. |
| 1096 | @param end_time_key: A string represents the filter key of end_time. |
| 1097 | |
| 1098 | @returns a pair of hqe and special tasks filters. |
| 1099 | """ |
| 1100 | filter_data_special_tasks = filter_data_common.copy() |
| 1101 | return (inject_times_to_filter('started_on__gte', 'started_on__lte', |
| 1102 | start_time, end_time, **filter_data_common), |
| 1103 | inject_times_to_filter('time_started__gte', 'time_started__lte', |
| 1104 | start_time, end_time, |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1105 | **filter_data_special_tasks)) |
| 1106 | |
| 1107 | |
| 1108 | def retrieve_shard(shard_hostname): |
| 1109 | """ |
Jakob Juelich | 7745757 | 2014-09-22 17:02:43 -0700 | [diff] [blame] | 1110 | Retrieves the shard with the given hostname from the database. |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1111 | |
| 1112 | @param shard_hostname: Hostname of the shard to retrieve |
| 1113 | |
Jakob Juelich | 7745757 | 2014-09-22 17:02:43 -0700 | [diff] [blame] | 1114 | @raises models.Shard.DoesNotExist, if no shard with this hostname was found. |
| 1115 | |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1116 | @returns: Shard object |
| 1117 | """ |
MK Ryu | 509516b | 2015-05-18 12:00:47 -0700 | [diff] [blame] | 1118 | timer = autotest_stats.Timer('shard_heartbeat.retrieve_shard') |
| 1119 | with timer: |
| 1120 | return models.Shard.smart_get(shard_hostname) |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1121 | |
| 1122 | |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 1123 | def find_records_for_shard(shard, known_job_ids, known_host_ids): |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1124 | """Find records that should be sent to a shard. |
| 1125 | |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1126 | @param shard: Shard to find records for. |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 1127 | @param known_job_ids: List of ids of jobs the shard already has. |
| 1128 | @param known_host_ids: List of ids of hosts the shard already has. |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1129 | |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 1130 | @returns: Tuple of three lists for hosts, jobs, and suite job keyvals: |
| 1131 | (hosts, jobs, suite_job_keyvals). |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 1132 | """ |
MK Ryu | 509516b | 2015-05-18 12:00:47 -0700 | [diff] [blame] | 1133 | timer = autotest_stats.Timer('shard_heartbeat') |
| 1134 | with timer.get_client('find_hosts'): |
| 1135 | hosts = models.Host.assign_to_shard(shard, known_host_ids) |
| 1136 | with timer.get_client('find_jobs'): |
| 1137 | jobs = models.Job.assign_to_shard(shard, known_job_ids) |
| 1138 | with timer.get_client('find_suite_job_keyvals'): |
| 1139 | parent_job_ids = [job.parent_job_id for job in jobs] |
| 1140 | suite_job_keyvals = models.JobKeyval.objects.filter( |
| 1141 | job_id__in=parent_job_ids) |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 1142 | return hosts, jobs, suite_job_keyvals |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1143 | |
| 1144 | |
| 1145 | def _persist_records_with_type_sent_from_shard( |
| 1146 | shard, records, record_type, *args, **kwargs): |
| 1147 | """ |
| 1148 | Handle records of a specified type that were sent to the shard master. |
| 1149 | |
| 1150 | @param shard: The shard the records were sent from. |
| 1151 | @param records: The records sent in their serialized format. |
| 1152 | @param record_type: Type of the objects represented by records. |
| 1153 | @param args: Additional arguments that will be passed on to the sanity |
| 1154 | checks. |
| 1155 | @param kwargs: Additional arguments that will be passed on to the sanity |
| 1156 | checks. |
| 1157 | |
| 1158 | @raises error.UnallowedRecordsSentToMaster if any of the sanity checks fail. |
| 1159 | |
| 1160 | @returns: List of primary keys of the processed records. |
| 1161 | """ |
| 1162 | pks = [] |
| 1163 | for serialized_record in records: |
| 1164 | pk = serialized_record['id'] |
| 1165 | try: |
| 1166 | current_record = record_type.objects.get(pk=pk) |
| 1167 | except record_type.DoesNotExist: |
| 1168 | raise error.UnallowedRecordsSentToMaster( |
| 1169 | 'Object with pk %s of type %s does not exist on master.' % ( |
| 1170 | pk, record_type)) |
| 1171 | |
| 1172 | current_record.sanity_check_update_from_shard( |
| 1173 | shard, serialized_record, *args, **kwargs) |
| 1174 | |
| 1175 | current_record.update_from_serialized(serialized_record) |
| 1176 | pks.append(pk) |
| 1177 | return pks |
| 1178 | |
| 1179 | |
| 1180 | def persist_records_sent_from_shard(shard, jobs, hqes): |
| 1181 | """ |
| 1182 | Sanity checking then saving serialized records sent to master from shard. |
| 1183 | |
| 1184 | During heartbeats shards upload jobs and hostqueuentries. This performs |
| 1185 | some sanity checks on these and then updates the existing records for those |
| 1186 | entries with the updated ones from the heartbeat. |
| 1187 | |
| 1188 | The sanity checks include: |
| 1189 | - Checking if the objects sent already exist on the master. |
| 1190 | - Checking if the objects sent were assigned to this shard. |
| 1191 | - hostqueueentries must be sent together with their jobs. |
| 1192 | |
| 1193 | @param shard: The shard the records were sent from. |
| 1194 | @param jobs: The jobs the shard sent. |
| 1195 | @param hqes: The hostqueuentries the shart sent. |
| 1196 | |
| 1197 | @raises error.UnallowedRecordsSentToMaster if any of the sanity checks fail. |
| 1198 | """ |
MK Ryu | 509516b | 2015-05-18 12:00:47 -0700 | [diff] [blame] | 1199 | timer = autotest_stats.Timer('shard_heartbeat') |
| 1200 | with timer.get_client('persist_jobs'): |
| 1201 | job_ids_sent = _persist_records_with_type_sent_from_shard( |
| 1202 | shard, jobs, models.Job) |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 1203 | |
MK Ryu | 509516b | 2015-05-18 12:00:47 -0700 | [diff] [blame] | 1204 | with timer.get_client('persist_hqes'): |
| 1205 | _persist_records_with_type_sent_from_shard( |
| 1206 | shard, hqes, models.HostQueueEntry, job_ids_sent=job_ids_sent) |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 1207 | |
| 1208 | |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 1209 | def forward_single_host_rpc_to_shard(func): |
| 1210 | """This decorator forwards rpc calls that modify a host to a shard. |
| 1211 | |
| 1212 | If a host is assigned to a shard, rpcs that change his attributes should be |
| 1213 | forwarded to the shard. |
| 1214 | |
| 1215 | This assumes the first argument of the function represents a host id. |
| 1216 | |
| 1217 | @param func: The function to decorate |
| 1218 | |
| 1219 | @returns: The function to replace func with. |
| 1220 | """ |
| 1221 | def replacement(**kwargs): |
| 1222 | # Only keyword arguments can be accepted here, as we need the argument |
| 1223 | # names to send the rpc. serviceHandler always provides arguments with |
| 1224 | # their keywords, so this is not a problem. |
MK Ryu | 8e2c2d0 | 2016-01-06 15:24:38 -0800 | [diff] [blame] | 1225 | |
| 1226 | # A host record (identified by kwargs['id']) can be deleted in |
| 1227 | # func(). Therefore, we should save the data that can be needed later |
| 1228 | # before func() is called. |
| 1229 | shard_hostname = None |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 1230 | host = models.Host.smart_get(kwargs['id']) |
MK Ryu | 8e2c2d0 | 2016-01-06 15:24:38 -0800 | [diff] [blame] | 1231 | if host and host.shard: |
| 1232 | shard_hostname = host.shard.rpc_hostname() |
| 1233 | ret = func(**kwargs) |
| 1234 | if shard_hostname and not server_utils.is_shard(): |
MK Ryu | 26f0c93 | 2015-05-28 18:14:33 -0700 | [diff] [blame] | 1235 | run_rpc_on_multiple_hostnames(func.func_name, |
MK Ryu | 8e2c2d0 | 2016-01-06 15:24:38 -0800 | [diff] [blame] | 1236 | [shard_hostname], |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 1237 | **kwargs) |
MK Ryu | 8e2c2d0 | 2016-01-06 15:24:38 -0800 | [diff] [blame] | 1238 | return ret |
Prashanth Balasubramanian | 5949b4a | 2014-11-23 12:58:30 -0800 | [diff] [blame] | 1239 | |
| 1240 | return replacement |
| 1241 | |
| 1242 | |
MK Ryu | fb5e3a8 | 2015-07-01 12:21:20 -0700 | [diff] [blame] | 1243 | def fanout_rpc(host_objs, rpc_name, include_hostnames=True, **kwargs): |
| 1244 | """Fanout the given rpc to shards of given hosts. |
| 1245 | |
| 1246 | @param host_objs: Host objects for the rpc. |
| 1247 | @param rpc_name: The name of the rpc. |
| 1248 | @param include_hostnames: If True, include the hostnames in the kwargs. |
| 1249 | Hostnames are not always necessary, this functions is designed to |
| 1250 | send rpcs to the shard a host is on, the rpcs themselves could be |
| 1251 | related to labels, acls etc. |
| 1252 | @param kwargs: The kwargs for the rpc. |
| 1253 | """ |
| 1254 | # Figure out which hosts are on which shards. |
| 1255 | shard_host_map = bucket_hosts_by_shard( |
| 1256 | host_objs, rpc_hostnames=True) |
| 1257 | |
| 1258 | # Execute the rpc against the appropriate shards. |
| 1259 | for shard, hostnames in shard_host_map.iteritems(): |
| 1260 | if include_hostnames: |
| 1261 | kwargs['hosts'] = hostnames |
| 1262 | try: |
| 1263 | run_rpc_on_multiple_hostnames(rpc_name, [shard], **kwargs) |
| 1264 | except: |
| 1265 | ei = sys.exc_info() |
| 1266 | new_exc = error.RPCException('RPC %s failed on shard %s due to ' |
| 1267 | '%s: %s' % (rpc_name, shard, ei[0].__name__, ei[1])) |
| 1268 | raise new_exc.__class__, new_exc, ei[2] |
| 1269 | |
| 1270 | |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 1271 | def run_rpc_on_multiple_hostnames(rpc_call, shard_hostnames, **kwargs): |
| 1272 | """Runs an rpc to multiple AFEs |
| 1273 | |
| 1274 | This is i.e. used to propagate changes made to hosts after they are assigned |
| 1275 | to a shard. |
| 1276 | |
| 1277 | @param rpc_call: Name of the rpc endpoint to call. |
| 1278 | @param shard_hostnames: List of hostnames to run the rpcs on. |
| 1279 | @param **kwargs: Keyword arguments to pass in the rpcs. |
| 1280 | """ |
MK Ryu | fb5e3a8 | 2015-07-01 12:21:20 -0700 | [diff] [blame] | 1281 | # Make sure this function is not called on shards but only on master. |
| 1282 | assert not server_utils.is_shard() |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 1283 | for shard_hostname in shard_hostnames: |
MK Ryu | 0a9c82e | 2015-09-17 17:54:01 -0700 | [diff] [blame] | 1284 | afe = frontend_wrappers.RetryingAFE(server=shard_hostname, |
| 1285 | user=thread_local.get_user()) |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 1286 | afe.run(rpc_call, **kwargs) |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 1287 | |
| 1288 | |
| 1289 | def get_label(name): |
| 1290 | """Gets a label object using a given name. |
| 1291 | |
| 1292 | @param name: Label name. |
| 1293 | @raises model.Label.DoesNotExist: when there is no label matching |
| 1294 | the given name. |
| 1295 | @return: a label object matching the given name. |
| 1296 | """ |
| 1297 | try: |
| 1298 | label = models.Label.smart_get(name) |
| 1299 | except models.Label.DoesNotExist: |
| 1300 | return None |
| 1301 | return label |
| 1302 | |
| 1303 | |
xixuan | ba232a3 | 2016-08-25 17:01:59 -0700 | [diff] [blame] | 1304 | # TODO: hide the following rpcs under is_moblab |
| 1305 | def moblab_only(func): |
| 1306 | """Ensure moblab specific functions only run on Moblab devices.""" |
| 1307 | def verify(*args, **kwargs): |
| 1308 | if not server_utils.is_moblab(): |
| 1309 | raise error.RPCException('RPC: %s can only run on Moblab Systems!', |
| 1310 | func.__name__) |
| 1311 | return func(*args, **kwargs) |
| 1312 | return verify |
| 1313 | |
| 1314 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1315 | def route_rpc_to_master(func): |
J. Richard Barnette | fdfcd66 | 2015-04-13 17:20:29 -0700 | [diff] [blame] | 1316 | """Route RPC to master AFE. |
MK Ryu | 2d10756 | 2015-02-24 17:45:02 -0800 | [diff] [blame] | 1317 | |
MK Ryu | 6f5eadb | 2015-09-04 10:50:47 -0700 | [diff] [blame] | 1318 | When a shard receives an RPC decorated by this, the RPC is just |
| 1319 | forwarded to the master. |
| 1320 | When the master gets the RPC, the RPC function is executed. |
J. Richard Barnette | fdfcd66 | 2015-04-13 17:20:29 -0700 | [diff] [blame] | 1321 | |
MK Ryu | 6f5eadb | 2015-09-04 10:50:47 -0700 | [diff] [blame] | 1322 | @param func: An RPC function to decorate |
| 1323 | |
| 1324 | @returns: A function replacing the RPC func. |
MK Ryu | 2d10756 | 2015-02-24 17:45:02 -0800 | [diff] [blame] | 1325 | """ |
Allen Li | 54121d0 | 2016-12-12 17:35:53 -0800 | [diff] [blame] | 1326 | argspec = inspect.getargspec(func) |
| 1327 | if argspec.varargs is not None: |
| 1328 | raise Exception('RPC function must not have *args.') |
| 1329 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1330 | @wraps(func) |
MK Ryu | f6ab8a7 | 2015-07-06 10:19:48 -0700 | [diff] [blame] | 1331 | def replacement(*args, **kwargs): |
Allen Li | ce51f37 | 2016-12-12 17:48:51 -0800 | [diff] [blame] | 1332 | """We need special handling when decorating an RPC that can be called |
| 1333 | directly using positional arguments. |
| 1334 | |
| 1335 | One example is rpc_interface.create_job(). |
| 1336 | rpc_interface.create_job_page_handler() calls the function using both |
| 1337 | positional and keyword arguments. Since frontend.RpcClient.run() |
| 1338 | takes only keyword arguments for an RPC, positional arguments of the |
| 1339 | RPC function need to be transformed into keyword arguments. |
MK Ryu | 6f5eadb | 2015-09-04 10:50:47 -0700 | [diff] [blame] | 1340 | """ |
Allen Li | 416c405 | 2016-12-12 17:46:46 -0800 | [diff] [blame] | 1341 | kwargs = _convert_to_kwargs_only(func, args, kwargs) |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1342 | if server_utils.is_shard(): |
MK Ryu | 9651ca5 | 2015-06-08 17:48:22 -0700 | [diff] [blame] | 1343 | afe = frontend_wrappers.RetryingAFE( |
Fang Deng | 0cb2a3b | 2015-12-10 17:59:00 -0800 | [diff] [blame] | 1344 | server=server_utils.get_global_afe_hostname(), |
MK Ryu | 0a9c82e | 2015-09-17 17:54:01 -0700 | [diff] [blame] | 1345 | user=thread_local.get_user()) |
MK Ryu | 9651ca5 | 2015-06-08 17:48:22 -0700 | [diff] [blame] | 1346 | return afe.run(func.func_name, **kwargs) |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1347 | return func(**kwargs) |
Allen Li | 54121d0 | 2016-12-12 17:35:53 -0800 | [diff] [blame] | 1348 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1349 | return replacement |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | |
Allen Li | 416c405 | 2016-12-12 17:46:46 -0800 | [diff] [blame] | 1352 | def _convert_to_kwargs_only(func, args, kwargs): |
| 1353 | """Convert a function call's arguments to a kwargs dict. |
| 1354 | |
| 1355 | This is best illustrated with an example. Given: |
| 1356 | |
Allen Li | ab8d379 | 2016-12-12 18:00:31 -0800 | [diff] [blame] | 1357 | def foo(a, b, **kwargs): |
| 1358 | pass |
| 1359 | _to_kwargs(foo, (1, 2), {'c': 3}) # corresponding to foo(1, 2, c=3) |
Allen Li | 416c405 | 2016-12-12 17:46:46 -0800 | [diff] [blame] | 1360 | |
| 1361 | foo(**kwargs) |
| 1362 | |
| 1363 | @param func: function whose signature to use |
| 1364 | @param args: positional arguments of call |
| 1365 | @param kwargs: keyword arguments of call |
| 1366 | |
| 1367 | @returns: kwargs dict |
| 1368 | """ |
Allen Li | 416c405 | 2016-12-12 17:46:46 -0800 | [diff] [blame] | 1369 | argspec = inspect.getargspec(func) |
Allen Li | ab8d379 | 2016-12-12 18:00:31 -0800 | [diff] [blame] | 1370 | # callargs looks like {'a': 1, 'b': 2, 'kwargs': {'c': 3}} |
| 1371 | callargs = inspect.getcallargs(func, *args, **kwargs) |
| 1372 | if argspec.keywords is None: |
| 1373 | kwargs = {} |
| 1374 | else: |
| 1375 | kwargs = callargs.pop(argspec.keywords) |
| 1376 | kwargs.update(callargs) |
Allen Li | 416c405 | 2016-12-12 17:46:46 -0800 | [diff] [blame] | 1377 | return kwargs |
| 1378 | |
| 1379 | |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 1380 | def get_sample_dut(board, pool): |
| 1381 | """Get a dut with the given board and pool. |
| 1382 | |
| 1383 | This method is used to help to locate a dut with the given board and pool. |
| 1384 | The dut then can be used to identify a devserver in the same subnet. |
| 1385 | |
| 1386 | @param board: Name of the board. |
| 1387 | @param pool: Name of the pool. |
| 1388 | |
| 1389 | @return: Name of a dut with the given board and pool. |
| 1390 | """ |
Allen Li | 3d43e60 | 2016-12-08 15:09:51 -0800 | [diff] [blame] | 1391 | if not (dev_server.PREFER_LOCAL_DEVSERVER and pool and board): |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 1392 | return None |
Dan Shi | c3d702b | 2016-12-21 03:05:09 +0000 | [diff] [blame] | 1393 | |
| 1394 | hosts = get_host_query( |
| 1395 | ('pool:%s' % pool, 'board:%s' % board), False, False, True, {}) |
| 1396 | if not hosts: |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 1397 | return None |
Dan Shi | c3d702b | 2016-12-21 03:05:09 +0000 | [diff] [blame] | 1398 | |
| 1399 | return list(hosts)[0].get_object_dict()['hostname'] |