blob: 1a19802ab31cd3b83651097879890fc65da79c1b [file] [log] [blame]
Aviv Keshet0b9cfc92013-02-05 11:36:02 -08001# pylint: disable-msg=C0111
2
mblighe8819cd2008-02-15 16:48:40 +00003"""\
4Functions to expose over the RPC interface.
5
6For all modify* and delete* functions that ask for an 'id' parameter to
7identify the object to operate on, the id may be either
8 * the database row ID
9 * the name of the object (label name, hostname, user login, etc.)
10 * a dictionary containing uniquely identifying field (this option should seldom
11 be used)
12
13When specifying foreign key fields (i.e. adding hosts to a label, or adding
14users to an ACL group), the given value may be either the database row ID or the
15name of the object.
16
17All get* functions return lists of dictionaries. Each dictionary represents one
18object and maps field names to values.
19
20Some examples:
21modify_host(2, hostname='myhost') # modify hostname of host with database ID 2
22modify_host('ipaj2', hostname='myhost') # modify hostname of host 'ipaj2'
23modify_test('sleeptest', test_type='Client', params=', seconds=60')
24delete_acl_group(1) # delete by ID
25delete_acl_group('Everyone') # delete by name
26acl_group_add_users('Everyone', ['mbligh', 'showard'])
27get_jobs(owner='showard', status='Queued')
28
mbligh93c80e62009-02-03 17:48:30 +000029See doctests/001_rpc_test.txt for (lots) more examples.
mblighe8819cd2008-02-15 16:48:40 +000030"""
31
32__author__ = 'showard@google.com (Steve Howard)'
33
MK Ryu9c5fbbe2015-02-11 15:46:22 -080034import sys
showard29f7cd22009-04-29 21:16:24 +000035import datetime
MK Ryu9c5fbbe2015-02-11 15:46:22 -080036
Moises Osorio2dc7a102014-12-02 18:24:02 -080037from django.db.models import Count
showardcafd16e2009-05-29 18:37:49 +000038import common
Simran Basib6ec8ae2014-04-23 12:05:08 -070039from autotest_lib.client.common_lib import priorities
Gabe Black1e1c41b2015-02-04 23:55:15 -080040from autotest_lib.client.common_lib.cros.graphite import autotest_stats
showard6d7b2ff2009-06-10 00:16:47 +000041from autotest_lib.frontend.afe import control_file, rpc_utils
J. Richard Barnetteb5164d62015-04-13 12:59:31 -070042from autotest_lib.frontend.afe import models, model_logic, model_attributes
Simran Basib6ec8ae2014-04-23 12:05:08 -070043from autotest_lib.frontend.afe import site_rpc_interface
Moises Osorio2dc7a102014-12-02 18:24:02 -080044from autotest_lib.frontend.tko import models as tko_models
Jiaxi Luoaac54572014-06-04 13:57:02 -070045from autotest_lib.frontend.tko import rpc_interface as tko_rpc_interface
J. Richard Barnetteb5164d62015-04-13 12:59:31 -070046from autotest_lib.server import frontend
Simran Basi71206ef2014-08-13 13:51:18 -070047from autotest_lib.server import utils
Dan Shid215dbe2015-06-18 16:14:59 -070048from autotest_lib.server.cros import provision
Jiaxi Luo90190c92014-06-18 12:35:57 -070049from autotest_lib.server.cros.dynamic_suite import tools
J. Richard Barnette39255fa2015-04-14 17:23:41 -070050from autotest_lib.site_utils import status_history
mblighe8819cd2008-02-15 16:48:40 +000051
Moises Osorio2dc7a102014-12-02 18:24:02 -080052
Gabe Black1e1c41b2015-02-04 23:55:15 -080053_timer = autotest_stats.Timer('rpc_interface')
Moises Osorio2dc7a102014-12-02 18:24:02 -080054
Eric Lid23bc192011-02-09 14:38:57 -080055def get_parameterized_autoupdate_image_url(job):
56 """Get the parameterized autoupdate image url from a parameterized job."""
57 known_test_obj = models.Test.smart_get('autoupdate_ParameterizedJob')
58 image_parameter = known_test_obj.testparameter_set.get(test=known_test_obj,
beeps8bb1f7d2013-08-05 01:30:09 -070059 name='image')
Eric Lid23bc192011-02-09 14:38:57 -080060 para_set = job.parameterized_job.parameterizedjobparameter_set
61 job_test_para = para_set.get(test_parameter=image_parameter)
62 return job_test_para.parameter_value
63
64
mblighe8819cd2008-02-15 16:48:40 +000065# labels
66
mblighe8819cd2008-02-15 16:48:40 +000067def modify_label(id, **data):
MK Ryu8c554cf2015-06-12 11:45:50 -070068 """Modify a label.
69
70 @param id: id or name of a label. More often a label name.
71 @param data: New data for a label.
72 """
73 label_model = models.Label.smart_get(id)
74
75 # Master forwards the RPC to shards
76 if not utils.is_shard():
77 rpc_utils.fanout_rpc(label_model.host_set.all(), 'modify_label', False,
78 id=id, **data)
79
80 label_model.update_object(data)
mblighe8819cd2008-02-15 16:48:40 +000081
82
83def delete_label(id):
MK Ryu8c554cf2015-06-12 11:45:50 -070084 """Delete a label.
85
86 @param id: id or name of a label. More often a label name.
87 """
88 label_model = models.Label.smart_get(id)
89
90 # Master forwards the RPC to shards
91 if not utils.is_shard():
92 rpc_utils.fanout_rpc(label_model.host_set.all(), 'delete_label', False,
93 id=id)
94
95 label_model.delete()
mblighe8819cd2008-02-15 16:48:40 +000096
Prashanth Balasubramanian744898f2015-01-13 05:04:16 -080097
MK Ryu9c5fbbe2015-02-11 15:46:22 -080098def add_label(name, ignore_exception_if_exists=False, **kwargs):
MK Ryucf027c62015-03-04 12:00:50 -080099 """Adds a new label of a given name.
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800100
101 @param name: label name.
102 @param ignore_exception_if_exists: If True and the exception was
103 thrown due to the duplicated label name when adding a label,
104 then suppress the exception. Default is False.
105 @param kwargs: keyword args that store more info about a label
106 other than the name.
107 @return: int/long id of a new label.
108 """
109 # models.Label.add_object() throws model_logic.ValidationError
110 # when it is given a label name that already exists.
111 # However, ValidationError can be thrown with different errors,
112 # and those errors should be thrown up to the call chain.
113 try:
114 label = models.Label.add_object(name=name, **kwargs)
115 except:
116 exc_info = sys.exc_info()
117 if ignore_exception_if_exists:
118 label = rpc_utils.get_label(name)
119 # If the exception is raised not because of duplicated
120 # "name", then raise the original exception.
121 if label is None:
122 raise exc_info[0], exc_info[1], exc_info[2]
123 else:
124 raise exc_info[0], exc_info[1], exc_info[2]
125 return label.id
126
127
128def add_label_to_hosts(id, hosts):
MK Ryucf027c62015-03-04 12:00:50 -0800129 """Adds a label of the given id to the given hosts only in local DB.
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800130
131 @param id: id or name of a label. More often a label name.
132 @param hosts: The hostnames of hosts that need the label.
133
134 @raises models.Label.DoesNotExist: If the label with id doesn't exist.
135 """
136 label = models.Label.smart_get(id)
137 host_objs = models.Host.smart_get_bulk(hosts)
138 if label.platform:
139 models.Host.check_no_platform(host_objs)
140 label.host_set.add(*host_objs)
141
142
MK Ryufbb002c2015-06-08 14:13:16 -0700143@rpc_utils.route_rpc_to_master
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800144def label_add_hosts(id, hosts):
MK Ryucf027c62015-03-04 12:00:50 -0800145 """Adds a label with the given id to the given hosts.
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800146
147 This method should be run only on master not shards.
Prashanth Balasubramanian5949b4a2014-11-23 12:58:30 -0800148 The given label will be created if it doesn't exist, provided the `id`
149 supplied is a label name not an int/long id.
150
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800151 @param id: id or name of a label. More often a label name.
Prashanth Balasubramanian5949b4a2014-11-23 12:58:30 -0800152 @param hosts: A list of hostnames or ids. More often hostnames.
153
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800154 @raises ValueError: If the id specified is an int/long (label id)
155 while the label does not exist.
Prashanth Balasubramanian5949b4a2014-11-23 12:58:30 -0800156 """
Prashanth Balasubramanian5949b4a2014-11-23 12:58:30 -0800157 try:
Prashanth Balasubramanian5949b4a2014-11-23 12:58:30 -0800158 label = models.Label.smart_get(id)
159 except models.Label.DoesNotExist:
160 # This matches the type checks in smart_get, which is a hack
161 # in and off itself. The aim here is to create any non-existent
162 # label, which we cannot do if the 'id' specified isn't a label name.
163 if isinstance(id, basestring):
164 label = models.Label.smart_get(add_label(id))
165 else:
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800166 raise ValueError('Label id (%s) does not exist. Please specify '
167 'the argument, id, as a string (label name).'
168 % id)
MK Ryucf027c62015-03-04 12:00:50 -0800169
170 host_objs = models.Host.smart_get_bulk(hosts)
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800171 # Make sure the label exists on the shard with the same id
172 # as it is on the master.
MK Ryucf027c62015-03-04 12:00:50 -0800173 # It is possible that the label is already in a shard because
174 # we are adding a new label only to shards of hosts that the label
175 # is going to be attached.
176 # For example, we add a label L1 to a host in shard S1.
177 # Master and S1 will have L1 but other shards won't.
178 # Later, when we add the same label L1 to hosts in shards S1 and S2,
179 # S1 already has the label but S2 doesn't.
180 # S2 should have the new label without any problem.
MK Ryu9c5fbbe2015-02-11 15:46:22 -0800181 # We ignore exception in such a case.
182 rpc_utils.fanout_rpc(
183 host_objs, 'add_label', name=label.name, id=label.id,
184 include_hostnames=False, ignore_exception_if_exists=True)
185 rpc_utils.fanout_rpc(host_objs, 'add_label_to_hosts', id=id)
showardbbabf502008-06-06 00:02:02 +0000186
MK Ryu26f0c932015-05-28 18:14:33 -0700187 add_label_to_hosts(id, hosts)
188
showardbbabf502008-06-06 00:02:02 +0000189
MK Ryucf027c62015-03-04 12:00:50 -0800190def remove_label_from_hosts(id, hosts):
191 """Removes a label of the given id from the given hosts only in local DB.
192
193 @param id: id or name of a label.
194 @param hosts: The hostnames of hosts that need to remove the label from.
195 """
showardbe3ec042008-11-12 18:16:07 +0000196 host_objs = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000197 models.Label.smart_get(id).host_set.remove(*host_objs)
showardbbabf502008-06-06 00:02:02 +0000198
199
MK Ryufbb002c2015-06-08 14:13:16 -0700200@rpc_utils.route_rpc_to_master
MK Ryucf027c62015-03-04 12:00:50 -0800201def label_remove_hosts(id, hosts):
202 """Removes a label of the given id from the given hosts.
203
204 This method should be run only on master not shards.
205
206 @param id: id or name of a label.
207 @param hosts: A list of hostnames or ids. More often hostnames.
208 """
MK Ryucf027c62015-03-04 12:00:50 -0800209 host_objs = models.Host.smart_get_bulk(hosts)
210 rpc_utils.fanout_rpc(host_objs, 'remove_label_from_hosts', id=id)
211
MK Ryu26f0c932015-05-28 18:14:33 -0700212 remove_label_from_hosts(id, hosts)
213
MK Ryucf027c62015-03-04 12:00:50 -0800214
Jiaxi Luo31874592014-06-11 10:36:35 -0700215def get_labels(exclude_filters=(), **filter_data):
showardc92da832009-04-07 18:14:34 +0000216 """\
Jiaxi Luo31874592014-06-11 10:36:35 -0700217 @param exclude_filters: A sequence of dictionaries of filters.
218
showardc92da832009-04-07 18:14:34 +0000219 @returns A sequence of nested dictionaries of label information.
220 """
Jiaxi Luo31874592014-06-11 10:36:35 -0700221 labels = models.Label.query_objects(filter_data)
222 for exclude_filter in exclude_filters:
223 labels = labels.exclude(**exclude_filter)
224 return rpc_utils.prepare_rows_as_nested_dicts(labels, ('atomic_group',))
showardc92da832009-04-07 18:14:34 +0000225
226
227# atomic groups
228
showarde9450c92009-06-30 01:58:52 +0000229def add_atomic_group(name, max_number_of_machines=None, description=None):
showardc92da832009-04-07 18:14:34 +0000230 return models.AtomicGroup.add_object(
231 name=name, max_number_of_machines=max_number_of_machines,
232 description=description).id
233
234
235def modify_atomic_group(id, **data):
236 models.AtomicGroup.smart_get(id).update_object(data)
237
238
239def delete_atomic_group(id):
240 models.AtomicGroup.smart_get(id).delete()
241
242
243def atomic_group_add_labels(id, labels):
244 label_objs = models.Label.smart_get_bulk(labels)
245 models.AtomicGroup.smart_get(id).label_set.add(*label_objs)
246
247
248def atomic_group_remove_labels(id, labels):
249 label_objs = models.Label.smart_get_bulk(labels)
250 models.AtomicGroup.smart_get(id).label_set.remove(*label_objs)
251
252
253def get_atomic_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000254 return rpc_utils.prepare_for_serialization(
showardc92da832009-04-07 18:14:34 +0000255 models.AtomicGroup.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000256
257
258# hosts
259
Matthew Sartori68186332015-04-27 17:19:53 -0700260def add_host(hostname, status=None, locked=None, lock_reason='', protection=None):
261 if locked and not lock_reason:
262 raise model_logic.ValidationError(
263 {'locked': 'Please provide a reason for locking when adding host.'})
264
jadmanski0afbb632008-06-06 21:10:57 +0000265 return models.Host.add_object(hostname=hostname, status=status,
Matthew Sartori68186332015-04-27 17:19:53 -0700266 locked=locked, lock_reason=lock_reason,
267 protection=protection).id
mblighe8819cd2008-02-15 16:48:40 +0000268
269
Jakob Juelich50e91f72014-10-01 12:43:23 -0700270@rpc_utils.forward_single_host_rpc_to_shard
mblighe8819cd2008-02-15 16:48:40 +0000271def modify_host(id, **data):
Jakob Juelich50e91f72014-10-01 12:43:23 -0700272 """Modify local attributes of a host.
273
274 If this is called on the master, but the host is assigned to a shard, this
275 will also forward the call to the responsible shard. This means i.e. if a
276 host is being locked using this function, this change will also propagate to
277 shards.
278
279 @param id: id of the host to modify.
280 @param **data: key=value pairs of values to set on the host.
281 """
showardbe0d8692009-08-20 23:42:44 +0000282 rpc_utils.check_modify_host(data)
showardce7c0922009-09-11 18:39:24 +0000283 host = models.Host.smart_get(id)
Jakob Juelich50e91f72014-10-01 12:43:23 -0700284
showardce7c0922009-09-11 18:39:24 +0000285 rpc_utils.check_modify_host_locking(host, data)
286 host.update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000287
288
showard276f9442009-05-20 00:33:16 +0000289def modify_hosts(host_filter_data, update_data):
Jakob Juelich50e91f72014-10-01 12:43:23 -0700290 """Modify local attributes of multiple hosts.
291
292 If this is called on the master, but one of the hosts in that match the
293 filters is assigned to a shard, this will also forward the call to the
294 responsible shard.
295
296 The filters are always applied on the master, not on the shards. This means
297 if the states of a host differ on the master and a shard, the state on the
298 master will be used. I.e. this means:
299 A host was synced to Shard 1. On Shard 1 the status of the host was set to
300 'Repair Failed'.
301 - A call to modify_hosts with host_filter_data={'status': 'Ready'} will
302 update the host (both on the shard and on the master), because the state
303 of the host as the master knows it is still 'Ready'.
304 - A call to modify_hosts with host_filter_data={'status': 'Repair failed'
305 will not update the host, because the filter doesn't apply on the master.
306
showardbe0d8692009-08-20 23:42:44 +0000307 @param host_filter_data: Filters out which hosts to modify.
308 @param update_data: A dictionary with the changes to make to the hosts.
showard276f9442009-05-20 00:33:16 +0000309 """
showardbe0d8692009-08-20 23:42:44 +0000310 rpc_utils.check_modify_host(update_data)
showard276f9442009-05-20 00:33:16 +0000311 hosts = models.Host.query_objects(host_filter_data)
Jakob Juelich50e91f72014-10-01 12:43:23 -0700312
313 affected_shard_hostnames = set()
314 affected_host_ids = []
315
Alex Miller9658a952013-05-14 16:40:02 -0700316 # Check all hosts before changing data for exception safety.
317 for host in hosts:
318 rpc_utils.check_modify_host_locking(host, update_data)
Jakob Juelich50e91f72014-10-01 12:43:23 -0700319 if host.shard:
Prashanth Balasubramanian8c98ac12014-12-23 11:26:44 -0800320 affected_shard_hostnames.add(host.shard.rpc_hostname())
Jakob Juelich50e91f72014-10-01 12:43:23 -0700321 affected_host_ids.append(host.id)
322
Prashanth Balasubramanian8c98ac12014-12-23 11:26:44 -0800323 if not utils.is_shard():
Jakob Juelich50e91f72014-10-01 12:43:23 -0700324 # Caution: Changing the filter from the original here. See docstring.
325 rpc_utils.run_rpc_on_multiple_hostnames(
326 'modify_hosts', affected_shard_hostnames,
327 host_filter_data={'id__in': affected_host_ids},
328 update_data=update_data)
329
showard276f9442009-05-20 00:33:16 +0000330 for host in hosts:
331 host.update_object(update_data)
332
333
MK Ryufbb002c2015-06-08 14:13:16 -0700334def add_labels_to_host(id, labels):
335 """Adds labels to a given host only in local DB.
showardcafd16e2009-05-29 18:37:49 +0000336
MK Ryufbb002c2015-06-08 14:13:16 -0700337 @param id: id or hostname for a host.
338 @param labels: ids or names for labels.
339 """
340 label_objs = models.Label.smart_get_bulk(labels)
341 models.Host.smart_get(id).labels.add(*label_objs)
342
343
344@rpc_utils.route_rpc_to_master
345def host_add_labels(id, labels):
346 """Adds labels to a given host.
347
348 @param id: id or hostname for a host.
349 @param labels: ids or names for labels.
350
351 @raises ValidationError: If adding more than one platform label.
352 """
353 label_objs = models.Label.smart_get_bulk(labels)
354 platforms = [label.name for label in label_objs if label.platform]
showardcafd16e2009-05-29 18:37:49 +0000355 if len(platforms) > 1:
356 raise model_logic.ValidationError(
357 {'labels': 'Adding more than one platform label: %s' %
358 ', '.join(platforms)})
MK Ryufbb002c2015-06-08 14:13:16 -0700359
360 host_obj = models.Host.smart_get(id)
showardcafd16e2009-05-29 18:37:49 +0000361 if len(platforms) == 1:
MK Ryufbb002c2015-06-08 14:13:16 -0700362 models.Host.check_no_platform([host_obj])
363
364 rpc_utils.fanout_rpc([host_obj], 'add_labels_to_host', False,
365 id=id, labels=labels)
366 add_labels_to_host(id, labels)
mblighe8819cd2008-02-15 16:48:40 +0000367
368
MK Ryufbb002c2015-06-08 14:13:16 -0700369def remove_labels_from_host(id, labels):
370 """Removes labels from a given host only in local DB.
371
372 @param id: id or hostname for a host.
373 @param labels: ids or names for labels.
374 """
375 label_objs = models.Label.smart_get_bulk(labels)
376 models.Host.smart_get(id).labels.remove(*label_objs)
377
378
379@rpc_utils.route_rpc_to_master
mblighe8819cd2008-02-15 16:48:40 +0000380def host_remove_labels(id, labels):
MK Ryufbb002c2015-06-08 14:13:16 -0700381 """Removes labels from a given host.
382
383 @param id: id or hostname for a host.
384 @param labels: ids or names for labels.
385 """
386 host_obj = models.Host.smart_get(id)
387 rpc_utils.fanout_rpc([host_obj], 'remove_labels_from_host', False,
388 id=id, labels=labels)
389 remove_labels_from_host(id, labels)
mblighe8819cd2008-02-15 16:48:40 +0000390
391
MK Ryuacf35922014-10-03 14:56:49 -0700392def get_host_attribute(attribute, **host_filter_data):
393 """
394 @param attribute: string name of attribute
395 @param host_filter_data: filter data to apply to Hosts to choose hosts to
396 act upon
397 """
398 hosts = rpc_utils.get_host_query((), False, False, True, host_filter_data)
399 hosts = list(hosts)
400 models.Host.objects.populate_relationships(hosts, models.HostAttribute,
401 'attribute_list')
402 host_attr_dicts = []
403 for host_obj in hosts:
404 for attr_obj in host_obj.attribute_list:
405 if attr_obj.attribute == attribute:
406 host_attr_dicts.append(attr_obj.get_object_dict())
407 return rpc_utils.prepare_for_serialization(host_attr_dicts)
408
409
showard0957a842009-05-11 19:25:08 +0000410def set_host_attribute(attribute, value, **host_filter_data):
411 """
MK Ryu26f0c932015-05-28 18:14:33 -0700412 @param attribute: string name of attribute
413 @param value: string, or None to delete an attribute
414 @param host_filter_data: filter data to apply to Hosts to choose hosts to
415 act upon
showard0957a842009-05-11 19:25:08 +0000416 """
417 assert host_filter_data # disallow accidental actions on all hosts
418 hosts = models.Host.query_objects(host_filter_data)
419 models.AclGroup.check_for_acl_violation_hosts(hosts)
420
MK Ryu26f0c932015-05-28 18:14:33 -0700421 # Master forwards this RPC to shards.
422 if not utils.is_shard():
423 rpc_utils.fanout_rpc(hosts, 'set_host_attribute', False,
424 attribute=attribute, value=value, **host_filter_data)
425
showard0957a842009-05-11 19:25:08 +0000426 for host in hosts:
showardf8b19042009-05-12 17:22:49 +0000427 host.set_or_delete_attribute(attribute, value)
showard0957a842009-05-11 19:25:08 +0000428
429
Jakob Juelich50e91f72014-10-01 12:43:23 -0700430@rpc_utils.forward_single_host_rpc_to_shard
mblighe8819cd2008-02-15 16:48:40 +0000431def delete_host(id):
jadmanski0afbb632008-06-06 21:10:57 +0000432 models.Host.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000433
434
showard87cc38f2009-08-20 23:37:04 +0000435def get_hosts(multiple_labels=(), exclude_only_if_needed_labels=False,
showard8aa84fc2009-09-16 17:17:55 +0000436 exclude_atomic_group_hosts=False, valid_only=True, **filter_data):
showard87cc38f2009-08-20 23:37:04 +0000437 """
438 @param multiple_labels: match hosts in all of the labels given. Should
439 be a list of label names.
440 @param exclude_only_if_needed_labels: Exclude hosts with at least one
441 "only_if_needed" label applied.
442 @param exclude_atomic_group_hosts: Exclude hosts that have one or more
443 atomic group labels associated with them.
jadmanski0afbb632008-06-06 21:10:57 +0000444 """
showard43a3d262008-11-12 18:17:05 +0000445 hosts = rpc_utils.get_host_query(multiple_labels,
446 exclude_only_if_needed_labels,
showard87cc38f2009-08-20 23:37:04 +0000447 exclude_atomic_group_hosts,
showard8aa84fc2009-09-16 17:17:55 +0000448 valid_only, filter_data)
showard0957a842009-05-11 19:25:08 +0000449 hosts = list(hosts)
450 models.Host.objects.populate_relationships(hosts, models.Label,
451 'label_list')
452 models.Host.objects.populate_relationships(hosts, models.AclGroup,
453 'acl_list')
454 models.Host.objects.populate_relationships(hosts, models.HostAttribute,
455 'attribute_list')
showard43a3d262008-11-12 18:17:05 +0000456 host_dicts = []
457 for host_obj in hosts:
458 host_dict = host_obj.get_object_dict()
showard0957a842009-05-11 19:25:08 +0000459 host_dict['labels'] = [label.name for label in host_obj.label_list]
showard909c9142009-07-07 20:54:42 +0000460 host_dict['platform'], host_dict['atomic_group'] = (rpc_utils.
461 find_platform_and_atomic_group(host_obj))
showard0957a842009-05-11 19:25:08 +0000462 host_dict['acls'] = [acl.name for acl in host_obj.acl_list]
463 host_dict['attributes'] = dict((attribute.attribute, attribute.value)
464 for attribute in host_obj.attribute_list)
showard43a3d262008-11-12 18:17:05 +0000465 host_dicts.append(host_dict)
466 return rpc_utils.prepare_for_serialization(host_dicts)
mblighe8819cd2008-02-15 16:48:40 +0000467
468
showard87cc38f2009-08-20 23:37:04 +0000469def get_num_hosts(multiple_labels=(), exclude_only_if_needed_labels=False,
showard8aa84fc2009-09-16 17:17:55 +0000470 exclude_atomic_group_hosts=False, valid_only=True,
471 **filter_data):
showard87cc38f2009-08-20 23:37:04 +0000472 """
473 Same parameters as get_hosts().
474
475 @returns The number of matching hosts.
476 """
showard43a3d262008-11-12 18:17:05 +0000477 hosts = rpc_utils.get_host_query(multiple_labels,
478 exclude_only_if_needed_labels,
showard87cc38f2009-08-20 23:37:04 +0000479 exclude_atomic_group_hosts,
showard8aa84fc2009-09-16 17:17:55 +0000480 valid_only, filter_data)
showard43a3d262008-11-12 18:17:05 +0000481 return hosts.count()
showard1385b162008-03-13 15:59:40 +0000482
mblighe8819cd2008-02-15 16:48:40 +0000483
484# tests
485
showard909c7a62008-07-15 21:52:38 +0000486def add_test(name, test_type, path, author=None, dependencies=None,
showard3d9899a2008-07-31 02:11:58 +0000487 experimental=True, run_verify=None, test_class=None,
showard909c7a62008-07-15 21:52:38 +0000488 test_time=None, test_category=None, description=None,
489 sync_count=1):
jadmanski0afbb632008-06-06 21:10:57 +0000490 return models.Test.add_object(name=name, test_type=test_type, path=path,
showard909c7a62008-07-15 21:52:38 +0000491 author=author, dependencies=dependencies,
492 experimental=experimental,
493 run_verify=run_verify, test_time=test_time,
494 test_category=test_category,
495 sync_count=sync_count,
jadmanski0afbb632008-06-06 21:10:57 +0000496 test_class=test_class,
497 description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000498
499
500def modify_test(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000501 models.Test.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000502
503
504def delete_test(id):
jadmanski0afbb632008-06-06 21:10:57 +0000505 models.Test.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000506
507
508def get_tests(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000509 return rpc_utils.prepare_for_serialization(
510 models.Test.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000511
512
Moises Osorio2dc7a102014-12-02 18:24:02 -0800513@_timer.decorate
514def get_tests_status_counts_by_job_name_label(job_name_prefix, label_name):
515 """Gets the counts of all passed and failed tests from the matching jobs.
516
517 @param job_name_prefix: Name prefix of the jobs to get the summary from, e.g.,
518 'butterfly-release/R40-6457.21.0/bvt-cq/'.
519 @param label_name: Label that must be set in the jobs, e.g.,
520 'cros-version:butterfly-release/R40-6457.21.0'.
521
522 @returns A summary of the counts of all the passed and failed tests.
523 """
524 job_ids = list(models.Job.objects.filter(
525 name__startswith=job_name_prefix,
526 dependency_labels__name=label_name).values_list(
527 'pk', flat=True))
528 summary = {'passed': 0, 'failed': 0}
529 if not job_ids:
530 return summary
531
532 counts = (tko_models.TestView.objects.filter(
533 afe_job_id__in=job_ids).exclude(
534 test_name='SERVER_JOB').exclude(
535 test_name__startswith='CLIENT_JOB').values(
536 'status').annotate(
537 count=Count('status')))
538 for status in counts:
539 if status['status'] == 'GOOD':
540 summary['passed'] += status['count']
541 else:
542 summary['failed'] += status['count']
543 return summary
544
545
showard2b9a88b2008-06-13 20:55:03 +0000546# profilers
547
548def add_profiler(name, description=None):
549 return models.Profiler.add_object(name=name, description=description).id
550
551
552def modify_profiler(id, **data):
553 models.Profiler.smart_get(id).update_object(data)
554
555
556def delete_profiler(id):
557 models.Profiler.smart_get(id).delete()
558
559
560def get_profilers(**filter_data):
561 return rpc_utils.prepare_for_serialization(
562 models.Profiler.list_objects(filter_data))
563
564
mblighe8819cd2008-02-15 16:48:40 +0000565# users
566
567def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000568 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000569
570
571def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000572 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000573
574
575def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000576 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000577
578
579def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000580 return rpc_utils.prepare_for_serialization(
581 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000582
583
584# acl groups
585
586def add_acl_group(name, description=None):
showard04f2cd82008-07-25 20:53:31 +0000587 group = models.AclGroup.add_object(name=name, description=description)
showard64a95952010-01-13 21:27:16 +0000588 group.users.add(models.User.current_user())
showard04f2cd82008-07-25 20:53:31 +0000589 return group.id
mblighe8819cd2008-02-15 16:48:40 +0000590
591
592def modify_acl_group(id, **data):
showard04f2cd82008-07-25 20:53:31 +0000593 group = models.AclGroup.smart_get(id)
594 group.check_for_acl_violation_acl_group()
595 group.update_object(data)
596 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000597
598
599def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000600 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000601 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000602 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000603 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000604
605
606def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000607 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000608 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000609 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000610 group.users.remove(*users)
showard04f2cd82008-07-25 20:53:31 +0000611 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000612
613
614def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000615 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000616 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000617 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000618 group.hosts.add(*hosts)
showard08f981b2008-06-24 21:59:03 +0000619 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000620
621
622def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000623 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000624 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000625 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000626 group.hosts.remove(*hosts)
showard08f981b2008-06-24 21:59:03 +0000627 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000628
629
630def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000631 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000632
633
634def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000635 acl_groups = models.AclGroup.list_objects(filter_data)
636 for acl_group in acl_groups:
637 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
638 acl_group['users'] = [user.login
639 for user in acl_group_obj.users.all()]
640 acl_group['hosts'] = [host.hostname
641 for host in acl_group_obj.hosts.all()]
642 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000643
644
645# jobs
646
mbligh120351e2009-01-24 01:40:45 +0000647def generate_control_file(tests=(), kernel=None, label=None, profilers=(),
showard91f85102009-10-12 20:34:52 +0000648 client_control_file='', use_container=False,
showard232b7ae2009-11-10 00:46:48 +0000649 profile_only=None, upload_kernel_config=False):
jadmanski0afbb632008-06-06 21:10:57 +0000650 """
mbligh120351e2009-01-24 01:40:45 +0000651 Generates a client-side control file to load a kernel and run tests.
652
653 @param tests List of tests to run.
mbligha3c58d22009-08-24 22:01:51 +0000654 @param kernel A list of kernel info dictionaries configuring which kernels
655 to boot for this job and other options for them
mbligh120351e2009-01-24 01:40:45 +0000656 @param label Name of label to grab kernel config from.
657 @param profilers List of profilers to activate during the job.
658 @param client_control_file The contents of a client-side control file to
659 run at the end of all tests. If this is supplied, all tests must be
660 client side.
661 TODO: in the future we should support server control files directly
662 to wrap with a kernel. That'll require changing the parameter
663 name and adding a boolean to indicate if it is a client or server
664 control file.
665 @param use_container unused argument today. TODO: Enable containers
666 on the host during a client side test.
showard91f85102009-10-12 20:34:52 +0000667 @param profile_only A boolean that indicates what default profile_only
668 mode to use in the control file. Passing None will generate a
669 control file that does not explcitly set the default mode at all.
showard232b7ae2009-11-10 00:46:48 +0000670 @param upload_kernel_config: if enabled it will generate server control
671 file code that uploads the kernel config file to the client and
672 tells the client of the new (local) path when compiling the kernel;
673 the tests must be server side tests
mbligh120351e2009-01-24 01:40:45 +0000674
675 @returns a dict with the following keys:
676 control_file: str, The control file text.
677 is_server: bool, is the control file a server-side control file?
678 synch_count: How many machines the job uses per autoserv execution.
679 synch_count == 1 means the job is asynchronous.
680 dependencies: A list of the names of labels on which the job depends.
681 """
showardd86debe2009-06-10 17:37:56 +0000682 if not tests and not client_control_file:
showard2bab8f42008-11-12 18:15:22 +0000683 return dict(control_file='', is_server=False, synch_count=1,
showard989f25d2008-10-01 11:38:11 +0000684 dependencies=[])
mblighe8819cd2008-02-15 16:48:40 +0000685
showard989f25d2008-10-01 11:38:11 +0000686 cf_info, test_objects, profiler_objects, label = (
showard2b9a88b2008-06-13 20:55:03 +0000687 rpc_utils.prepare_generate_control_file(tests, kernel, label,
688 profilers))
showard989f25d2008-10-01 11:38:11 +0000689 cf_info['control_file'] = control_file.generate_control(
mbligha3c58d22009-08-24 22:01:51 +0000690 tests=test_objects, kernels=kernel, platform=label,
mbligh120351e2009-01-24 01:40:45 +0000691 profilers=profiler_objects, is_server=cf_info['is_server'],
showard232b7ae2009-11-10 00:46:48 +0000692 client_control_file=client_control_file, profile_only=profile_only,
693 upload_kernel_config=upload_kernel_config)
showard989f25d2008-10-01 11:38:11 +0000694 return cf_info
mblighe8819cd2008-02-15 16:48:40 +0000695
696
jamesren4a41e012010-07-16 22:33:48 +0000697def create_parameterized_job(name, priority, test, parameters, kernel=None,
698 label=None, profilers=(), profiler_parameters=None,
699 use_container=False, profile_only=None,
700 upload_kernel_config=False, hosts=(),
701 meta_hosts=(), one_time_hosts=(),
702 atomic_group_name=None, synch_count=None,
703 is_template=False, timeout=None,
Simran Basi7e605742013-11-12 13:43:36 -0800704 timeout_mins=None, max_runtime_mins=None,
705 run_verify=False, email_list='', dependencies=(),
706 reboot_before=None, reboot_after=None,
707 parse_failed_repair=None, hostless=False,
Dan Shiec1d47d2015-02-13 11:38:13 -0800708 keyvals=None, drone_set=None, run_reset=True,
709 require_ssq=None):
jamesren4a41e012010-07-16 22:33:48 +0000710 """
711 Creates and enqueues a parameterized job.
712
713 Most parameters a combination of the parameters for generate_control_file()
714 and create_job(), with the exception of:
715
716 @param test name or ID of the test to run
717 @param parameters a map of parameter name ->
718 tuple of (param value, param type)
719 @param profiler_parameters a dictionary of parameters for the profilers:
720 key: profiler name
721 value: dict of param name -> tuple of
722 (param value,
723 param type)
724 """
725 # Save the values of the passed arguments here. What we're going to do with
726 # them is pass them all to rpc_utils.get_create_job_common_args(), which
727 # will extract the subset of these arguments that apply for
728 # rpc_utils.create_job_common(), which we then pass in to that function.
729 args = locals()
730
731 # Set up the parameterized job configs
732 test_obj = models.Test.smart_get(test)
Aviv Keshet3dd8beb2013-05-13 17:36:04 -0700733 control_type = test_obj.test_type
jamesren4a41e012010-07-16 22:33:48 +0000734
735 try:
736 label = models.Label.smart_get(label)
737 except models.Label.DoesNotExist:
738 label = None
739
740 kernel_objs = models.Kernel.create_kernels(kernel)
741 profiler_objs = [models.Profiler.smart_get(profiler)
742 for profiler in profilers]
743
744 parameterized_job = models.ParameterizedJob.objects.create(
745 test=test_obj, label=label, use_container=use_container,
746 profile_only=profile_only,
747 upload_kernel_config=upload_kernel_config)
748 parameterized_job.kernels.add(*kernel_objs)
749
750 for profiler in profiler_objs:
751 parameterized_profiler = models.ParameterizedJobProfiler.objects.create(
752 parameterized_job=parameterized_job,
753 profiler=profiler)
754 profiler_params = profiler_parameters.get(profiler.name, {})
755 for name, (value, param_type) in profiler_params.iteritems():
756 models.ParameterizedJobProfilerParameter.objects.create(
757 parameterized_job_profiler=parameterized_profiler,
758 parameter_name=name,
759 parameter_value=value,
760 parameter_type=param_type)
761
762 try:
763 for parameter in test_obj.testparameter_set.all():
764 if parameter.name in parameters:
765 param_value, param_type = parameters.pop(parameter.name)
766 parameterized_job.parameterizedjobparameter_set.create(
767 test_parameter=parameter, parameter_value=param_value,
768 parameter_type=param_type)
769
770 if parameters:
771 raise Exception('Extra parameters remain: %r' % parameters)
772
773 return rpc_utils.create_job_common(
774 parameterized_job=parameterized_job.id,
775 control_type=control_type,
776 **rpc_utils.get_create_job_common_args(args))
777 except:
778 parameterized_job.delete()
779 raise
780
781
Simran Basib6ec8ae2014-04-23 12:05:08 -0700782def create_job_page_handler(name, priority, control_file, control_type,
Dan Shid215dbe2015-06-18 16:14:59 -0700783 image=None, hostless=False, firmware_rw_build=None,
784 firmware_ro_build=None, test_source_build=None,
785 **kwargs):
Simran Basib6ec8ae2014-04-23 12:05:08 -0700786 """\
787 Create and enqueue a job.
788
789 @param name name of this job
790 @param priority Integer priority of this job. Higher is more important.
791 @param control_file String contents of the control file.
792 @param control_type Type of control file, Client or Server.
Dan Shid215dbe2015-06-18 16:14:59 -0700793 @param image: ChromeOS build to be installed in the dut. Default to None.
794 @param firmware_rw_build: Firmware build to update RW firmware. Default to
795 None, i.e., RW firmware will not be updated.
796 @param firmware_ro_build: Firmware build to update RO firmware. Default to
797 None, i.e., RO firmware will not be updated.
798 @param test_source_build: Build to be used to retrieve test code. Default
799 to None.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700800 @param kwargs extra args that will be required by create_suite_job or
801 create_job.
802
803 @returns The created Job id number.
804 """
805 control_file = rpc_utils.encode_ascii(control_file)
Jiaxi Luodd67beb2014-07-18 16:28:31 -0700806 if not control_file:
807 raise model_logic.ValidationError({
808 'control_file' : "Control file cannot be empty"})
Simran Basib6ec8ae2014-04-23 12:05:08 -0700809
810 if image and hostless:
Dan Shid215dbe2015-06-18 16:14:59 -0700811 builds = {}
812 builds[provision.CROS_VERSION_PREFIX] = image
813 if firmware_rw_build:
814 builds[provision.FW_VERSION_PREFIX] = firmware_rw_build
815 if firmware_ro_build:
816 builds[provision.FW_RO_VERSION_PREFIX] = firmware_ro_build
Simran Basib6ec8ae2014-04-23 12:05:08 -0700817 return site_rpc_interface.create_suite_job(
818 name=name, control_file=control_file, priority=priority,
Dan Shid215dbe2015-06-18 16:14:59 -0700819 builds=builds, test_source_build=test_source_build, **kwargs)
Simran Basib6ec8ae2014-04-23 12:05:08 -0700820 return create_job(name, priority, control_file, control_type, image=image,
821 hostless=hostless, **kwargs)
822
823
showard12f3e322009-05-13 21:27:42 +0000824def create_job(name, priority, control_file, control_type,
825 hosts=(), meta_hosts=(), one_time_hosts=(),
826 atomic_group_name=None, synch_count=None, is_template=False,
Simran Basi7e605742013-11-12 13:43:36 -0800827 timeout=None, timeout_mins=None, max_runtime_mins=None,
828 run_verify=False, email_list='', dependencies=(),
829 reboot_before=None, reboot_after=None, parse_failed_repair=None,
830 hostless=False, keyvals=None, drone_set=None, image=None,
Dan Shiec1d47d2015-02-13 11:38:13 -0800831 parent_job_id=None, test_retry=0, run_reset=True,
832 require_ssp=None, args=(), **kwargs):
jadmanski0afbb632008-06-06 21:10:57 +0000833 """\
834 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000835
showarda1e74b32009-05-12 17:32:04 +0000836 @param name name of this job
Alex Miller7d658cf2013-09-04 16:00:35 -0700837 @param priority Integer priority of this job. Higher is more important.
showarda1e74b32009-05-12 17:32:04 +0000838 @param control_file String contents of the control file.
839 @param control_type Type of control file, Client or Server.
840 @param synch_count How many machines the job uses per autoserv execution.
Jiaxi Luo90190c92014-06-18 12:35:57 -0700841 synch_count == 1 means the job is asynchronous. If an atomic group is
842 given this value is treated as a minimum.
showarda1e74b32009-05-12 17:32:04 +0000843 @param is_template If true then create a template job.
844 @param timeout Hours after this call returns until the job times out.
Simran Basi7e605742013-11-12 13:43:36 -0800845 @param timeout_mins Minutes after this call returns until the job times
Jiaxi Luo90190c92014-06-18 12:35:57 -0700846 out.
Simran Basi34217022012-11-06 13:43:15 -0800847 @param max_runtime_mins Minutes from job starting time until job times out
showarda1e74b32009-05-12 17:32:04 +0000848 @param run_verify Should the host be verified before running the test?
849 @param email_list String containing emails to mail when the job is done
850 @param dependencies List of label names on which this job depends
851 @param reboot_before Never, If dirty, or Always
852 @param reboot_after Never, If all tests passed, or Always
853 @param parse_failed_repair if true, results of failed repairs launched by
Jiaxi Luo90190c92014-06-18 12:35:57 -0700854 this job will be parsed as part of the job.
showarda9545c02009-12-18 22:44:26 +0000855 @param hostless if true, create a hostless job
showardc1a98d12010-01-15 00:22:22 +0000856 @param keyvals dict of keyvals to associate with the job
showarda1e74b32009-05-12 17:32:04 +0000857 @param hosts List of hosts to run job on.
858 @param meta_hosts List where each entry is a label name, and for each entry
Jiaxi Luo90190c92014-06-18 12:35:57 -0700859 one host will be chosen from that label to run the job on.
showarda1e74b32009-05-12 17:32:04 +0000860 @param one_time_hosts List of hosts not in the database to run the job on.
861 @param atomic_group_name The name of an atomic group to schedule the job on.
jamesren76fcf192010-04-21 20:39:50 +0000862 @param drone_set The name of the drone set to run this test on.
Paul Pendlebury5a8c6ad2011-02-01 07:20:17 -0800863 @param image OS image to install before running job.
Aviv Keshet0b9cfc92013-02-05 11:36:02 -0800864 @param parent_job_id id of a job considered to be parent of created job.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700865 @param test_retry Number of times to retry test if the test did not
Jiaxi Luo90190c92014-06-18 12:35:57 -0700866 complete successfully. (optional, default: 0)
Simran Basib6ec8ae2014-04-23 12:05:08 -0700867 @param run_reset Should the host be reset before running the test?
Dan Shiec1d47d2015-02-13 11:38:13 -0800868 @param require_ssp Set to True to require server-side packaging to run the
869 test. If it's set to None, drone will still try to run
870 the server side with server-side packaging. If the
871 autotest-server package doesn't exist for the build or
872 image is not set, drone will run the test without server-
873 side packaging. Default is None.
Jiaxi Luo90190c92014-06-18 12:35:57 -0700874 @param args A list of args to be injected into control file.
Simran Basib6ec8ae2014-04-23 12:05:08 -0700875 @param kwargs extra keyword args. NOT USED.
showardc92da832009-04-07 18:14:34 +0000876
877 @returns The created Job id number.
jadmanski0afbb632008-06-06 21:10:57 +0000878 """
Jiaxi Luo90190c92014-06-18 12:35:57 -0700879 if args:
880 control_file = tools.inject_vars({'args': args}, control_file)
881
Simran Basiab5a1bf2014-05-28 15:39:44 -0700882 if image is None:
883 return rpc_utils.create_job_common(
884 **rpc_utils.get_create_job_common_args(locals()))
885
886 # When image is supplied use a known parameterized test already in the
887 # database to pass the OS image path from the front end, through the
888 # scheduler, and finally to autoserv as the --image parameter.
889
890 # The test autoupdate_ParameterizedJob is in afe_autotests and used to
891 # instantiate a Test object and from there a ParameterizedJob.
892 known_test_obj = models.Test.smart_get('autoupdate_ParameterizedJob')
893 known_parameterized_job = models.ParameterizedJob.objects.create(
894 test=known_test_obj)
895
896 # autoupdate_ParameterizedJob has a single parameter, the image parameter,
897 # stored in the table afe_test_parameters. We retrieve and set this
898 # instance of the parameter to the OS image path.
899 image_parameter = known_test_obj.testparameter_set.get(test=known_test_obj,
900 name='image')
901 known_parameterized_job.parameterizedjobparameter_set.create(
902 test_parameter=image_parameter, parameter_value=image,
903 parameter_type='string')
904
Dan Shid215dbe2015-06-18 16:14:59 -0700905 # TODO(crbug.com/502638): save firmware build etc to parameterized_job.
906
Simran Basiab5a1bf2014-05-28 15:39:44 -0700907 # By passing a parameterized_job to create_job_common the job entry in
908 # the afe_jobs table will have the field parameterized_job_id set.
909 # The scheduler uses this id in the afe_parameterized_jobs table to
910 # match this job to our known test, and then with the
911 # afe_parameterized_job_parameters table to get the actual image path.
jamesren4a41e012010-07-16 22:33:48 +0000912 return rpc_utils.create_job_common(
Simran Basiab5a1bf2014-05-28 15:39:44 -0700913 parameterized_job=known_parameterized_job.id,
jamesren4a41e012010-07-16 22:33:48 +0000914 **rpc_utils.get_create_job_common_args(locals()))
mblighe8819cd2008-02-15 16:48:40 +0000915
916
showard9dbdcda2008-10-14 17:34:36 +0000917def abort_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000918 """\
showard9dbdcda2008-10-14 17:34:36 +0000919 Abort a set of host queue entries.
Fang Deng63b0e452014-12-19 14:38:15 -0800920
921 @return: A list of dictionaries, each contains information
922 about an aborted HQE.
jadmanski0afbb632008-06-06 21:10:57 +0000923 """
showard9dbdcda2008-10-14 17:34:36 +0000924 query = models.HostQueueEntry.query_objects(filter_data)
beepsfaecbce2013-10-29 11:35:10 -0700925
926 # Dont allow aborts on:
927 # 1. Jobs that have already completed (whether or not they were aborted)
928 # 2. Jobs that we have already been aborted (but may not have completed)
929 query = query.filter(complete=False).filter(aborted=False)
showarddc817512008-11-12 18:16:41 +0000930 models.AclGroup.check_abort_permissions(query)
showard9dbdcda2008-10-14 17:34:36 +0000931 host_queue_entries = list(query.select_related())
showard2bab8f42008-11-12 18:15:22 +0000932 rpc_utils.check_abort_synchronous_jobs(host_queue_entries)
mblighe8819cd2008-02-15 16:48:40 +0000933
Simran Basic1b26762013-06-26 14:23:21 -0700934 models.HostQueueEntry.abort_host_queue_entries(host_queue_entries)
Fang Deng63b0e452014-12-19 14:38:15 -0800935 hqe_info = [{'HostQueueEntry': hqe.id, 'Job': hqe.job_id,
936 'Job name': hqe.job.name} for hqe in host_queue_entries]
937 return hqe_info
showard9d821ab2008-07-11 16:54:29 +0000938
939
beeps8bb1f7d2013-08-05 01:30:09 -0700940def abort_special_tasks(**filter_data):
941 """\
942 Abort the special task, or tasks, specified in the filter.
943 """
944 query = models.SpecialTask.query_objects(filter_data)
945 special_tasks = query.filter(is_active=True)
946 for task in special_tasks:
947 task.abort()
948
949
Simran Basi73dae552013-02-25 14:57:46 -0800950def _call_special_tasks_on_hosts(task, hosts):
951 """\
952 Schedules a set of hosts for a special task.
953
954 @returns A list of hostnames that a special task was created for.
955 """
956 models.AclGroup.check_for_acl_violation_hosts(hosts)
Prashanth Balasubramanian6edaaf92014-11-24 16:36:25 -0800957 shard_host_map = rpc_utils.bucket_hosts_by_shard(hosts)
Prashanth Balasubramanian8c98ac12014-12-23 11:26:44 -0800958 if shard_host_map and not utils.is_shard():
Prashanth Balasubramanian6edaaf92014-11-24 16:36:25 -0800959 raise ValueError('The following hosts are on shards, please '
960 'follow the link to the shards and create jobs '
961 'there instead. %s.' % shard_host_map)
Simran Basi73dae552013-02-25 14:57:46 -0800962 for host in hosts:
963 models.SpecialTask.schedule_special_task(host, task)
964 return list(sorted(host.hostname for host in hosts))
965
966
showard1ff7b2e2009-05-15 23:17:18 +0000967def reverify_hosts(**filter_data):
968 """\
969 Schedules a set of hosts for verify.
mbligh4e545a52009-12-19 05:30:39 +0000970
971 @returns A list of hostnames that a verify task was created for.
showard1ff7b2e2009-05-15 23:17:18 +0000972 """
Prashanth Balasubramanian40981232014-12-16 19:01:58 -0800973 hosts = models.Host.query_objects(filter_data)
974 shard_host_map = rpc_utils.bucket_hosts_by_shard(hosts, rpc_hostnames=True)
975
976 # Filter out hosts on a shard from those on the master, forward
977 # rpcs to the shard with an additional hostname__in filter, and
978 # create a local SpecialTask for each remaining host.
Prashanth Balasubramanian8c98ac12014-12-23 11:26:44 -0800979 if shard_host_map and not utils.is_shard():
Prashanth Balasubramanian40981232014-12-16 19:01:58 -0800980 hosts = [h for h in hosts if h.shard is None]
981 for shard, hostnames in shard_host_map.iteritems():
982
983 # The main client of this module is the frontend website, and
984 # it invokes it with an 'id' or an 'id__in' filter. Regardless,
985 # the 'hostname' filter should narrow down the list of hosts on
986 # each shard even though we supply all the ids in filter_data.
987 # This method uses hostname instead of id because it fits better
988 # with the overall architecture of redirection functions in rpc_utils.
989 shard_filter = filter_data.copy()
990 shard_filter['hostname__in'] = hostnames
991 rpc_utils.run_rpc_on_multiple_hostnames(
992 'reverify_hosts', [shard], **shard_filter)
993
994 # There is a race condition here if someone assigns a shard to one of these
995 # hosts before we create the task. The host will stay on the master if:
996 # 1. The host is not Ready
997 # 2. The host is Ready but has a task
998 # But if the host is Ready and doesn't have a task yet, it will get sent
999 # to the shard as we're creating a task here.
1000
1001 # Given that we only rarely verify Ready hosts it isn't worth putting this
1002 # entire method in a transaction. The worst case scenario is that we have
1003 # a verify running on a Ready host while the shard is using it, if the verify
1004 # fails no subsequent tasks will be created against the host on the master,
1005 # and verifies are safe enough that this is OK.
1006 return _call_special_tasks_on_hosts(models.SpecialTask.Task.VERIFY, hosts)
Simran Basi73dae552013-02-25 14:57:46 -08001007
1008
1009def repair_hosts(**filter_data):
1010 """\
1011 Schedules a set of hosts for repair.
1012
1013 @returns A list of hostnames that a repair task was created for.
1014 """
1015 return _call_special_tasks_on_hosts(models.SpecialTask.Task.REPAIR,
1016 models.Host.query_objects(filter_data))
showard1ff7b2e2009-05-15 23:17:18 +00001017
1018
Jiaxi Luo15cbf372014-07-01 19:20:20 -07001019def get_jobs(not_yet_run=False, running=False, finished=False,
1020 suite=False, sub=False, standalone=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +00001021 """\
Jiaxi Luo15cbf372014-07-01 19:20:20 -07001022 Extra status filter args for get_jobs:
jadmanski0afbb632008-06-06 21:10:57 +00001023 -not_yet_run: Include only jobs that have not yet started running.
1024 -running: Include only jobs that have start running but for which not
1025 all hosts have completed.
1026 -finished: Include only jobs for which all hosts have completed (or
1027 aborted).
1028 At most one of these three fields should be specified.
Jiaxi Luo15cbf372014-07-01 19:20:20 -07001029
1030 Extra type filter args for get_jobs:
1031 -suite: Include only jobs with child jobs.
1032 -sub: Include only jobs with a parent job.
1033 -standalone: Inlcude only jobs with no child or parent jobs.
1034 At most one of these three fields should be specified.
jadmanski0afbb632008-06-06 21:10:57 +00001035 """
Jiaxi Luo15cbf372014-07-01 19:20:20 -07001036 extra_args = rpc_utils.extra_job_status_filters(not_yet_run,
1037 running,
1038 finished)
1039 filter_data['extra_args'] = rpc_utils.extra_job_type_filters(extra_args,
1040 suite,
1041 sub,
1042 standalone)
showard0957a842009-05-11 19:25:08 +00001043 job_dicts = []
1044 jobs = list(models.Job.query_objects(filter_data))
1045 models.Job.objects.populate_relationships(jobs, models.Label,
1046 'dependencies')
showardc1a98d12010-01-15 00:22:22 +00001047 models.Job.objects.populate_relationships(jobs, models.JobKeyval, 'keyvals')
showard0957a842009-05-11 19:25:08 +00001048 for job in jobs:
1049 job_dict = job.get_object_dict()
1050 job_dict['dependencies'] = ','.join(label.name
1051 for label in job.dependencies)
showardc1a98d12010-01-15 00:22:22 +00001052 job_dict['keyvals'] = dict((keyval.key, keyval.value)
1053 for keyval in job.keyvals)
Eric Lid23bc192011-02-09 14:38:57 -08001054 if job.parameterized_job:
1055 job_dict['image'] = get_parameterized_autoupdate_image_url(job)
showard0957a842009-05-11 19:25:08 +00001056 job_dicts.append(job_dict)
1057 return rpc_utils.prepare_for_serialization(job_dicts)
mblighe8819cd2008-02-15 16:48:40 +00001058
1059
1060def get_num_jobs(not_yet_run=False, running=False, finished=False,
Jiaxi Luo15cbf372014-07-01 19:20:20 -07001061 suite=False, sub=False, standalone=False,
jadmanski0afbb632008-06-06 21:10:57 +00001062 **filter_data):
1063 """\
1064 See get_jobs() for documentation of extra filter parameters.
1065 """
Jiaxi Luo15cbf372014-07-01 19:20:20 -07001066 extra_args = rpc_utils.extra_job_status_filters(not_yet_run,
1067 running,
1068 finished)
1069 filter_data['extra_args'] = rpc_utils.extra_job_type_filters(extra_args,
1070 suite,
1071 sub,
1072 standalone)
jadmanski0afbb632008-06-06 21:10:57 +00001073 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +00001074
1075
mblighe8819cd2008-02-15 16:48:40 +00001076def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +00001077 """\
Jiaxi Luoaac54572014-06-04 13:57:02 -07001078 Like get_jobs(), but adds 'status_counts' and 'result_counts' field.
1079
1080 'status_counts' filed is a dictionary mapping status strings to the number
1081 of hosts currently with that status, i.e. {'Queued' : 4, 'Running' : 2}.
1082
1083 'result_counts' field is piped to tko's rpc_interface and has the return
1084 format specified under get_group_counts.
jadmanski0afbb632008-06-06 21:10:57 +00001085 """
1086 jobs = get_jobs(**filter_data)
1087 ids = [job['id'] for job in jobs]
1088 all_status_counts = models.Job.objects.get_status_counts(ids)
1089 for job in jobs:
1090 job['status_counts'] = all_status_counts[job['id']]
Jiaxi Luoaac54572014-06-04 13:57:02 -07001091 job['result_counts'] = tko_rpc_interface.get_status_counts(
1092 ['afe_job_id', 'afe_job_id'],
1093 header_groups=[['afe_job_id'], ['afe_job_id']],
1094 **{'afe_job_id': job['id']})
jadmanski0afbb632008-06-06 21:10:57 +00001095 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +00001096
1097
showarda965cef2009-05-15 23:17:41 +00001098def get_info_for_clone(id, preserve_metahosts, queue_entry_filter_data=None):
showarda8709c52008-07-03 19:44:54 +00001099 """\
1100 Retrieves all the information needed to clone a job.
1101 """
showarda8709c52008-07-03 19:44:54 +00001102 job = models.Job.objects.get(id=id)
showard29f7cd22009-04-29 21:16:24 +00001103 job_info = rpc_utils.get_job_info(job,
showarda965cef2009-05-15 23:17:41 +00001104 preserve_metahosts,
1105 queue_entry_filter_data)
showard945072f2008-09-03 20:34:59 +00001106
showardd9992fe2008-07-31 02:15:03 +00001107 host_dicts = []
showard29f7cd22009-04-29 21:16:24 +00001108 for host in job_info['hosts']:
1109 host_dict = get_hosts(id=host.id)[0]
1110 other_labels = host_dict['labels']
1111 if host_dict['platform']:
1112 other_labels.remove(host_dict['platform'])
1113 host_dict['other_labels'] = ', '.join(other_labels)
showardd9992fe2008-07-31 02:15:03 +00001114 host_dicts.append(host_dict)
showarda8709c52008-07-03 19:44:54 +00001115
showard29f7cd22009-04-29 21:16:24 +00001116 for host in job_info['one_time_hosts']:
1117 host_dict = dict(hostname=host.hostname,
1118 id=host.id,
1119 platform='(one-time host)',
1120 locked_text='')
1121 host_dicts.append(host_dict)
showarda8709c52008-07-03 19:44:54 +00001122
showard4d077562009-05-08 18:24:36 +00001123 # convert keys from Label objects to strings (names of labels)
showard29f7cd22009-04-29 21:16:24 +00001124 meta_host_counts = dict((meta_host.name, count) for meta_host, count
showard4d077562009-05-08 18:24:36 +00001125 in job_info['meta_host_counts'].iteritems())
showard29f7cd22009-04-29 21:16:24 +00001126
1127 info = dict(job=job.get_object_dict(),
1128 meta_host_counts=meta_host_counts,
1129 hosts=host_dicts)
1130 info['job']['dependencies'] = job_info['dependencies']
1131 if job_info['atomic_group']:
1132 info['atomic_group_name'] = (job_info['atomic_group']).name
1133 else:
1134 info['atomic_group_name'] = None
jamesren2275ef12010-04-12 18:25:06 +00001135 info['hostless'] = job_info['hostless']
jamesren76fcf192010-04-21 20:39:50 +00001136 info['drone_set'] = job.drone_set and job.drone_set.name
showarda8709c52008-07-03 19:44:54 +00001137
Eric Lid23bc192011-02-09 14:38:57 -08001138 if job.parameterized_job:
1139 info['job']['image'] = get_parameterized_autoupdate_image_url(job)
1140
showarda8709c52008-07-03 19:44:54 +00001141 return rpc_utils.prepare_for_serialization(info)
1142
1143
showard34dc5fa2008-04-24 20:58:40 +00001144# host queue entries
1145
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001146def get_host_queue_entries(start_time=None, end_time=None, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +00001147 """\
showardc92da832009-04-07 18:14:34 +00001148 @returns A sequence of nested dictionaries of host and job information.
jadmanski0afbb632008-06-06 21:10:57 +00001149 """
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001150 filter_data = rpc_utils.inject_times_to_filter('started_on__gte',
1151 'started_on__lte',
1152 start_time,
1153 end_time,
1154 **filter_data)
J. Richard Barnetteb5164d62015-04-13 12:59:31 -07001155 return rpc_utils.prepare_rows_as_nested_dicts(
1156 models.HostQueueEntry.query_objects(filter_data),
1157 ('host', 'atomic_group', 'job'))
showard34dc5fa2008-04-24 20:58:40 +00001158
1159
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001160def get_num_host_queue_entries(start_time=None, end_time=None, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +00001161 """\
1162 Get the number of host queue entries associated with this job.
1163 """
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001164 filter_data = rpc_utils.inject_times_to_filter('started_on__gte',
1165 'started_on__lte',
1166 start_time,
1167 end_time,
1168 **filter_data)
jadmanski0afbb632008-06-06 21:10:57 +00001169 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +00001170
1171
showard1e935f12008-07-11 00:11:36 +00001172def get_hqe_percentage_complete(**filter_data):
1173 """
showardc92da832009-04-07 18:14:34 +00001174 Computes the fraction of host queue entries matching the given filter data
showard1e935f12008-07-11 00:11:36 +00001175 that are complete.
1176 """
1177 query = models.HostQueueEntry.query_objects(filter_data)
1178 complete_count = query.filter(complete=True).count()
1179 total_count = query.count()
1180 if total_count == 0:
1181 return 1
1182 return float(complete_count) / total_count
1183
1184
showard1a5a4082009-07-28 20:01:37 +00001185# special tasks
1186
1187def get_special_tasks(**filter_data):
J. Richard Barnetteb5164d62015-04-13 12:59:31 -07001188 """Get special task entries from the local database.
1189
1190 Query the special tasks table for tasks matching the given
1191 `filter_data`, and return a list of the results. No attempt is
1192 made to forward the call to shards; the buck will stop here.
1193 The caller is expected to know the target shard for such reasons
1194 as:
1195 * The caller is a service (such as gs_offloader) configured
1196 to operate on behalf of one specific shard, and no other.
1197 * The caller has a host as a parameter, and knows that this is
1198 the shard assigned to that host.
1199
1200 @param filter_data Filter keywords to pass to the underlying
1201 database query.
1202
1203 """
J. Richard Barnettefdfcd662015-04-13 17:20:29 -07001204 return rpc_utils.prepare_rows_as_nested_dicts(
1205 models.SpecialTask.query_objects(filter_data),
1206 ('host', 'queue_entry'))
J. Richard Barnetteb5164d62015-04-13 12:59:31 -07001207
1208
1209def get_host_special_tasks(host_id, **filter_data):
1210 """Get special task entries for a given host.
1211
1212 Query the special tasks table for tasks that ran on the host
1213 given by `host_id` and matching the given `filter_data`.
1214 Return a list of the results. If the host is assigned to a
1215 shard, forward this call to that shard.
1216
1217 @param host_id Id in the database of the target host.
1218 @param filter_data Filter keywords to pass to the underlying
1219 database query.
1220
1221 """
MK Ryu0c1a37d2015-04-30 12:00:55 -07001222 # Retrieve host data even if the host is in an invalid state.
1223 host = models.Host.smart_get(host_id, False)
J. Richard Barnetteb5164d62015-04-13 12:59:31 -07001224 if not host.shard:
J. Richard Barnettefdfcd662015-04-13 17:20:29 -07001225 return get_special_tasks(host_id=host_id, **filter_data)
J. Richard Barnetteb5164d62015-04-13 12:59:31 -07001226 else:
J. Richard Barnette39255fa2015-04-14 17:23:41 -07001227 # The return values from AFE methods are post-processed
1228 # objects that aren't JSON-serializable. So, we have to
1229 # call AFE.run() to get the raw, serializable output from
1230 # the shard.
J. Richard Barnetteb5164d62015-04-13 12:59:31 -07001231 shard_afe = frontend.AFE(server=host.shard.rpc_hostname())
1232 return shard_afe.run('get_special_tasks',
1233 host_id=host_id, **filter_data)
showard1a5a4082009-07-28 20:01:37 +00001234
1235
MK Ryu0c1a37d2015-04-30 12:00:55 -07001236def get_num_special_tasks(**kwargs):
1237 """Get the number of special task entries from the local database.
1238
1239 Query the special tasks table for tasks matching the given 'kwargs',
1240 and return the number of the results. No attempt is made to forward
1241 the call to shards; the buck will stop here.
1242
1243 @param kwargs Filter keywords to pass to the underlying database query.
1244
1245 """
1246 return models.SpecialTask.query_count(kwargs)
1247
1248
1249def get_host_num_special_tasks(host, **kwargs):
1250 """Get special task entries for a given host.
1251
1252 Query the special tasks table for tasks that ran on the host
1253 given by 'host' and matching the given 'kwargs'.
1254 Return a list of the results. If the host is assigned to a
1255 shard, forward this call to that shard.
1256
1257 @param host id or name of a host. More often a hostname.
1258 @param kwargs Filter keywords to pass to the underlying database query.
1259
1260 """
1261 # Retrieve host data even if the host is in an invalid state.
1262 host_model = models.Host.smart_get(host, False)
1263 if not host_model.shard:
1264 return get_num_special_tasks(host=host, **kwargs)
1265 else:
1266 shard_afe = frontend.AFE(server=host_model.shard.rpc_hostname())
1267 return shard_afe.run('get_num_special_tasks', host=host, **kwargs)
1268
1269
J. Richard Barnette39255fa2015-04-14 17:23:41 -07001270def get_status_task(host_id, end_time):
J. Richard Barnette4d7e6e62015-05-01 10:47:34 -07001271 """Get the "status task" for a host from the local shard.
J. Richard Barnette39255fa2015-04-14 17:23:41 -07001272
J. Richard Barnette4d7e6e62015-05-01 10:47:34 -07001273 Returns a single special task representing the given host's
1274 "status task". The status task is a completed special task that
1275 identifies whether the corresponding host was working or broken
1276 when it completed. A successful task indicates a working host;
1277 a failed task indicates broken.
J. Richard Barnette39255fa2015-04-14 17:23:41 -07001278
J. Richard Barnette4d7e6e62015-05-01 10:47:34 -07001279 This call will not be forward to a shard; the receiving server
1280 must be the shard that owns the host.
1281
1282 @param host_id Id in the database of the target host.
1283 @param end_time Time reference for the host's status.
1284
1285 @return A single task; its status (successful or not)
1286 corresponds to the status of the host (working or
1287 broken) at the given time. If no task is found, return
1288 `None`.
1289
1290 """
1291 tasklist = rpc_utils.prepare_rows_as_nested_dicts(
1292 status_history.get_status_task(host_id, end_time),
1293 ('host', 'queue_entry'))
1294 return tasklist[0] if tasklist else None
1295
1296
1297def get_host_status_task(host_id, end_time):
1298 """Get the "status task" for a host from its owning shard.
1299
1300 Finds the given host's owning shard, and forwards to it a call
1301 to `get_status_task()` (see above).
J. Richard Barnette39255fa2015-04-14 17:23:41 -07001302
1303 @param host_id Id in the database of the target host.
1304 @param end_time Time reference for the host's status.
1305
1306 @return A single task; its status (successful or not)
1307 corresponds to the status of the host (working or
1308 broken) at the given time. If no task is found, return
1309 `None`.
1310
1311 """
1312 host = models.Host.smart_get(host_id)
1313 if not host.shard:
J. Richard Barnette4d7e6e62015-05-01 10:47:34 -07001314 return get_status_task(host_id, end_time)
J. Richard Barnette39255fa2015-04-14 17:23:41 -07001315 else:
1316 # The return values from AFE methods are post-processed
1317 # objects that aren't JSON-serializable. So, we have to
1318 # call AFE.run() to get the raw, serializable output from
1319 # the shard.
1320 shard_afe = frontend.AFE(server=host.shard.rpc_hostname())
1321 return shard_afe.run('get_status_task',
1322 host_id=host_id, end_time=end_time)
1323
1324
showardc0ac3a72009-07-08 21:14:45 +00001325# support for host detail view
1326
MK Ryu0c1a37d2015-04-30 12:00:55 -07001327def get_host_queue_entries_and_special_tasks(host, query_start=None,
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001328 query_limit=None, start_time=None,
1329 end_time=None):
showardc0ac3a72009-07-08 21:14:45 +00001330 """
1331 @returns an interleaved list of HostQueueEntries and SpecialTasks,
1332 in approximate run order. each dict contains keys for type, host,
1333 job, status, started_on, execution_path, and ID.
1334 """
1335 total_limit = None
1336 if query_limit is not None:
1337 total_limit = query_start + query_limit
MK Ryu0c1a37d2015-04-30 12:00:55 -07001338 filter_data_common = {'host': host,
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001339 'query_limit': total_limit,
1340 'sort_by': ['-id']}
showardc0ac3a72009-07-08 21:14:45 +00001341
MK Ryu0c1a37d2015-04-30 12:00:55 -07001342 filter_data_special_tasks = rpc_utils.inject_times_to_filter(
1343 'time_started__gte', 'time_started__lte', start_time, end_time,
1344 **filter_data_common)
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001345
MK Ryu0c1a37d2015-04-30 12:00:55 -07001346 queue_entries = get_host_queue_entries(
1347 start_time, end_time, **filter_data_common)
1348 special_tasks = get_host_special_tasks(host, **filter_data_special_tasks)
showardc0ac3a72009-07-08 21:14:45 +00001349
1350 interleaved_entries = rpc_utils.interleave_entries(queue_entries,
1351 special_tasks)
1352 if query_start is not None:
1353 interleaved_entries = interleaved_entries[query_start:]
1354 if query_limit is not None:
1355 interleaved_entries = interleaved_entries[:query_limit]
MK Ryu0c1a37d2015-04-30 12:00:55 -07001356 return rpc_utils.prepare_host_queue_entries_and_special_tasks(
1357 interleaved_entries, queue_entries)
showardc0ac3a72009-07-08 21:14:45 +00001358
1359
MK Ryu0c1a37d2015-04-30 12:00:55 -07001360def get_num_host_queue_entries_and_special_tasks(host, start_time=None,
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001361 end_time=None):
MK Ryu0c1a37d2015-04-30 12:00:55 -07001362 filter_data_common = {'host': host}
Jiaxi Luo57bc1952014-07-22 15:27:30 -07001363
1364 filter_data_queue_entries, filter_data_special_tasks = (
1365 rpc_utils.inject_times_to_hqe_special_tasks_filters(
1366 filter_data_common, start_time, end_time))
1367
1368 return (models.HostQueueEntry.query_count(filter_data_queue_entries)
MK Ryu0c1a37d2015-04-30 12:00:55 -07001369 + get_host_num_special_tasks(**filter_data_special_tasks))
showardc0ac3a72009-07-08 21:14:45 +00001370
1371
showard29f7cd22009-04-29 21:16:24 +00001372# recurring run
1373
1374def get_recurring(**filter_data):
1375 return rpc_utils.prepare_rows_as_nested_dicts(
1376 models.RecurringRun.query_objects(filter_data),
1377 ('job', 'owner'))
1378
1379
1380def get_num_recurring(**filter_data):
1381 return models.RecurringRun.query_count(filter_data)
1382
1383
1384def delete_recurring_runs(**filter_data):
1385 to_delete = models.RecurringRun.query_objects(filter_data)
1386 to_delete.delete()
1387
1388
1389def create_recurring_run(job_id, start_date, loop_period, loop_count):
showard64a95952010-01-13 21:27:16 +00001390 owner = models.User.current_user().login
showard29f7cd22009-04-29 21:16:24 +00001391 job = models.Job.objects.get(id=job_id)
1392 return job.create_recurring_job(start_date=start_date,
1393 loop_period=loop_period,
1394 loop_count=loop_count,
1395 owner=owner)
1396
1397
mblighe8819cd2008-02-15 16:48:40 +00001398# other
1399
showarde0b63622008-08-04 20:58:47 +00001400def echo(data=""):
1401 """\
1402 Returns a passed in string. For doing a basic test to see if RPC calls
1403 can successfully be made.
1404 """
1405 return data
1406
1407
showardb7a52fd2009-04-27 20:10:56 +00001408def get_motd():
1409 """\
1410 Returns the message of the day as a string.
1411 """
1412 return rpc_utils.get_motd()
1413
1414
mblighe8819cd2008-02-15 16:48:40 +00001415def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +00001416 """\
1417 Returns a dictionary containing a bunch of data that shouldn't change
1418 often and is otherwise inaccessible. This includes:
showardc92da832009-04-07 18:14:34 +00001419
1420 priorities: List of job priority choices.
1421 default_priority: Default priority value for new jobs.
1422 users: Sorted list of all users.
Jiaxi Luo31874592014-06-11 10:36:35 -07001423 labels: Sorted list of labels not start with 'cros-version' and
1424 'fw-version'.
showardc92da832009-04-07 18:14:34 +00001425 atomic_groups: Sorted list of all atomic groups.
1426 tests: Sorted list of all tests.
1427 profilers: Sorted list of all profilers.
1428 current_user: Logged-in username.
1429 host_statuses: Sorted list of possible Host statuses.
1430 job_statuses: Sorted list of possible HostQueueEntry statuses.
Simran Basi7e605742013-11-12 13:43:36 -08001431 job_timeout_default: The default job timeout length in minutes.
showarda1e74b32009-05-12 17:32:04 +00001432 parse_failed_repair_default: Default value for the parse_failed_repair job
Jiaxi Luo31874592014-06-11 10:36:35 -07001433 option.
showardc92da832009-04-07 18:14:34 +00001434 reboot_before_options: A list of valid RebootBefore string enums.
1435 reboot_after_options: A list of valid RebootAfter string enums.
1436 motd: Server's message of the day.
1437 status_dictionary: A mapping from one word job status names to a more
1438 informative description.
jadmanski0afbb632008-06-06 21:10:57 +00001439 """
showard21baa452008-10-21 00:08:39 +00001440
1441 job_fields = models.Job.get_field_dict()
jamesren76fcf192010-04-21 20:39:50 +00001442 default_drone_set_name = models.DroneSet.default_drone_set_name()
1443 drone_sets = ([default_drone_set_name] +
1444 sorted(drone_set.name for drone_set in
1445 models.DroneSet.objects.exclude(
1446 name=default_drone_set_name)))
showard21baa452008-10-21 00:08:39 +00001447
jadmanski0afbb632008-06-06 21:10:57 +00001448 result = {}
Alex Miller7d658cf2013-09-04 16:00:35 -07001449 result['priorities'] = priorities.Priority.choices()
1450 default_priority = priorities.Priority.DEFAULT
1451 result['default_priority'] = 'Default'
1452 result['max_schedulable_priority'] = priorities.Priority.DEFAULT
jadmanski0afbb632008-06-06 21:10:57 +00001453 result['users'] = get_users(sort_by=['login'])
Jiaxi Luo31874592014-06-11 10:36:35 -07001454
1455 label_exclude_filters = [{'name__startswith': 'cros-version'},
1456 {'name__startswith': 'fw-version'}]
1457 result['labels'] = get_labels(
1458 label_exclude_filters,
1459 sort_by=['-platform', 'name'])
1460
showardc92da832009-04-07 18:14:34 +00001461 result['atomic_groups'] = get_atomic_groups(sort_by=['name'])
jadmanski0afbb632008-06-06 21:10:57 +00001462 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +00001463 result['profilers'] = get_profilers(sort_by=['name'])
showard0fc38302008-10-23 00:44:07 +00001464 result['current_user'] = rpc_utils.prepare_for_serialization(
showard64a95952010-01-13 21:27:16 +00001465 models.User.current_user().get_object_dict())
showard2b9a88b2008-06-13 20:55:03 +00001466 result['host_statuses'] = sorted(models.Host.Status.names)
mbligh5a198b92008-12-11 19:33:29 +00001467 result['job_statuses'] = sorted(models.HostQueueEntry.Status.names)
Simran Basi7e605742013-11-12 13:43:36 -08001468 result['job_timeout_mins_default'] = models.Job.DEFAULT_TIMEOUT_MINS
Simran Basi34217022012-11-06 13:43:15 -08001469 result['job_max_runtime_mins_default'] = (
1470 models.Job.DEFAULT_MAX_RUNTIME_MINS)
showarda1e74b32009-05-12 17:32:04 +00001471 result['parse_failed_repair_default'] = bool(
1472 models.Job.DEFAULT_PARSE_FAILED_REPAIR)
jamesrendd855242010-03-02 22:23:44 +00001473 result['reboot_before_options'] = model_attributes.RebootBefore.names
1474 result['reboot_after_options'] = model_attributes.RebootAfter.names
showard8fbae652009-01-20 23:23:10 +00001475 result['motd'] = rpc_utils.get_motd()
jamesren76fcf192010-04-21 20:39:50 +00001476 result['drone_sets_enabled'] = models.DroneSet.drone_sets_enabled()
1477 result['drone_sets'] = drone_sets
jamesren4a41e012010-07-16 22:33:48 +00001478 result['parameterized_jobs'] = models.Job.parameterized_jobs_enabled()
showard8ac29b42008-07-17 17:01:55 +00001479
showardd3dc1992009-04-22 21:01:40 +00001480 result['status_dictionary'] = {"Aborted": "Aborted",
showard8ac29b42008-07-17 17:01:55 +00001481 "Verifying": "Verifying Host",
Alex Millerdfff2fd2013-05-28 13:05:06 -07001482 "Provisioning": "Provisioning Host",
showard8ac29b42008-07-17 17:01:55 +00001483 "Pending": "Waiting on other hosts",
1484 "Running": "Running autoserv",
1485 "Completed": "Autoserv completed",
1486 "Failed": "Failed to complete",
showardd823b362008-07-24 16:35:46 +00001487 "Queued": "Queued",
showard5deb6772008-11-04 21:54:33 +00001488 "Starting": "Next in host's queue",
1489 "Stopped": "Other host(s) failed verify",
showardd3dc1992009-04-22 21:01:40 +00001490 "Parsing": "Awaiting parse of final results",
showard29f7cd22009-04-29 21:16:24 +00001491 "Gathering": "Gathering log files",
showard8cc058f2009-09-08 16:26:33 +00001492 "Template": "Template job for recurring run",
mbligh4608b002010-01-05 18:22:35 +00001493 "Waiting": "Waiting for scheduler action",
Dan Shi07e09af2013-04-12 09:31:29 -07001494 "Archiving": "Archiving results",
1495 "Resetting": "Resetting hosts"}
Jiaxi Luo421608e2014-07-07 14:38:00 -07001496
1497 result['wmatrix_url'] = rpc_utils.get_wmatrix_url()
Simran Basi71206ef2014-08-13 13:51:18 -07001498 result['is_moblab'] = bool(utils.is_moblab())
Jiaxi Luo421608e2014-07-07 14:38:00 -07001499
jadmanski0afbb632008-06-06 21:10:57 +00001500 return result
showard29f7cd22009-04-29 21:16:24 +00001501
1502
1503def get_server_time():
1504 return datetime.datetime.now().strftime("%Y-%m-%d %H:%M")