Aviv Keshet | 0b9cfc9 | 2013-02-05 11:36:02 -0800 | [diff] [blame] | 1 | # pylint: disable-msg=C0111 |
| 2 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 3 | """\ |
| 4 | Functions to expose over the RPC interface. |
| 5 | |
| 6 | For all modify* and delete* functions that ask for an 'id' parameter to |
| 7 | identify 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 | |
| 13 | When specifying foreign key fields (i.e. adding hosts to a label, or adding |
| 14 | users to an ACL group), the given value may be either the database row ID or the |
| 15 | name of the object. |
| 16 | |
| 17 | All get* functions return lists of dictionaries. Each dictionary represents one |
| 18 | object and maps field names to values. |
| 19 | |
| 20 | Some examples: |
| 21 | modify_host(2, hostname='myhost') # modify hostname of host with database ID 2 |
| 22 | modify_host('ipaj2', hostname='myhost') # modify hostname of host 'ipaj2' |
| 23 | modify_test('sleeptest', test_type='Client', params=', seconds=60') |
| 24 | delete_acl_group(1) # delete by ID |
| 25 | delete_acl_group('Everyone') # delete by name |
| 26 | acl_group_add_users('Everyone', ['mbligh', 'showard']) |
| 27 | get_jobs(owner='showard', status='Queued') |
| 28 | |
mbligh | 93c80e6 | 2009-02-03 17:48:30 +0000 | [diff] [blame] | 29 | See doctests/001_rpc_test.txt for (lots) more examples. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 30 | """ |
| 31 | |
| 32 | __author__ = 'showard@google.com (Steve Howard)' |
| 33 | |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 34 | import sys |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 35 | import datetime |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 36 | |
Moises Osorio | 2dc7a10 | 2014-12-02 18:24:02 -0800 | [diff] [blame] | 37 | from django.db.models import Count |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 38 | import common |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 39 | from autotest_lib.client.common_lib import priorities |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 40 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 41 | from autotest_lib.frontend.afe import control_file, rpc_utils |
J. Richard Barnette | b5164d6 | 2015-04-13 12:59:31 -0700 | [diff] [blame] | 42 | from autotest_lib.frontend.afe import models, model_logic, model_attributes |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 43 | from autotest_lib.frontend.afe import site_rpc_interface |
Moises Osorio | 2dc7a10 | 2014-12-02 18:24:02 -0800 | [diff] [blame] | 44 | from autotest_lib.frontend.tko import models as tko_models |
Jiaxi Luo | aac5457 | 2014-06-04 13:57:02 -0700 | [diff] [blame] | 45 | from autotest_lib.frontend.tko import rpc_interface as tko_rpc_interface |
J. Richard Barnette | b5164d6 | 2015-04-13 12:59:31 -0700 | [diff] [blame] | 46 | from autotest_lib.server import frontend |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 47 | from autotest_lib.server import utils |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 48 | from autotest_lib.server.cros.dynamic_suite import tools |
J. Richard Barnette | 39255fa | 2015-04-14 17:23:41 -0700 | [diff] [blame] | 49 | from autotest_lib.site_utils import status_history |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 50 | |
Moises Osorio | 2dc7a10 | 2014-12-02 18:24:02 -0800 | [diff] [blame] | 51 | |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 52 | _timer = autotest_stats.Timer('rpc_interface') |
Moises Osorio | 2dc7a10 | 2014-12-02 18:24:02 -0800 | [diff] [blame] | 53 | |
Eric Li | d23bc19 | 2011-02-09 14:38:57 -0800 | [diff] [blame] | 54 | def get_parameterized_autoupdate_image_url(job): |
| 55 | """Get the parameterized autoupdate image url from a parameterized job.""" |
| 56 | known_test_obj = models.Test.smart_get('autoupdate_ParameterizedJob') |
| 57 | image_parameter = known_test_obj.testparameter_set.get(test=known_test_obj, |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 58 | name='image') |
Eric Li | d23bc19 | 2011-02-09 14:38:57 -0800 | [diff] [blame] | 59 | para_set = job.parameterized_job.parameterizedjobparameter_set |
| 60 | job_test_para = para_set.get(test_parameter=image_parameter) |
| 61 | return job_test_para.parameter_value |
| 62 | |
| 63 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 64 | # labels |
| 65 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 66 | def modify_label(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 67 | models.Label.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def delete_label(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 71 | models.Label.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 72 | |
Prashanth Balasubramanian | 744898f | 2015-01-13 05:04:16 -0800 | [diff] [blame] | 73 | |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 74 | def add_label(name, ignore_exception_if_exists=False, **kwargs): |
MK Ryu | cf027c6 | 2015-03-04 12:00:50 -0800 | [diff] [blame] | 75 | """Adds a new label of a given name. |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 76 | |
| 77 | @param name: label name. |
| 78 | @param ignore_exception_if_exists: If True and the exception was |
| 79 | thrown due to the duplicated label name when adding a label, |
| 80 | then suppress the exception. Default is False. |
| 81 | @param kwargs: keyword args that store more info about a label |
| 82 | other than the name. |
| 83 | @return: int/long id of a new label. |
| 84 | """ |
| 85 | # models.Label.add_object() throws model_logic.ValidationError |
| 86 | # when it is given a label name that already exists. |
| 87 | # However, ValidationError can be thrown with different errors, |
| 88 | # and those errors should be thrown up to the call chain. |
| 89 | try: |
| 90 | label = models.Label.add_object(name=name, **kwargs) |
| 91 | except: |
| 92 | exc_info = sys.exc_info() |
| 93 | if ignore_exception_if_exists: |
| 94 | label = rpc_utils.get_label(name) |
| 95 | # If the exception is raised not because of duplicated |
| 96 | # "name", then raise the original exception. |
| 97 | if label is None: |
| 98 | raise exc_info[0], exc_info[1], exc_info[2] |
| 99 | else: |
| 100 | raise exc_info[0], exc_info[1], exc_info[2] |
| 101 | return label.id |
| 102 | |
| 103 | |
| 104 | def add_label_to_hosts(id, hosts): |
MK Ryu | cf027c6 | 2015-03-04 12:00:50 -0800 | [diff] [blame] | 105 | """Adds a label of the given id to the given hosts only in local DB. |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 106 | |
| 107 | @param id: id or name of a label. More often a label name. |
| 108 | @param hosts: The hostnames of hosts that need the label. |
| 109 | |
| 110 | @raises models.Label.DoesNotExist: If the label with id doesn't exist. |
| 111 | """ |
| 112 | label = models.Label.smart_get(id) |
| 113 | host_objs = models.Host.smart_get_bulk(hosts) |
| 114 | if label.platform: |
| 115 | models.Host.check_no_platform(host_objs) |
| 116 | label.host_set.add(*host_objs) |
| 117 | |
| 118 | |
| 119 | def label_add_hosts(id, hosts): |
MK Ryu | cf027c6 | 2015-03-04 12:00:50 -0800 | [diff] [blame] | 120 | """Adds a label with the given id to the given hosts. |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 121 | |
| 122 | This method should be run only on master not shards. |
Prashanth Balasubramanian | 5949b4a | 2014-11-23 12:58:30 -0800 | [diff] [blame] | 123 | The given label will be created if it doesn't exist, provided the `id` |
| 124 | supplied is a label name not an int/long id. |
| 125 | |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 126 | @param id: id or name of a label. More often a label name. |
Prashanth Balasubramanian | 5949b4a | 2014-11-23 12:58:30 -0800 | [diff] [blame] | 127 | @param hosts: A list of hostnames or ids. More often hostnames. |
| 128 | |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 129 | @raises ValueError: If the id specified is an int/long (label id) |
| 130 | while the label does not exist. |
Prashanth Balasubramanian | 5949b4a | 2014-11-23 12:58:30 -0800 | [diff] [blame] | 131 | """ |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 132 | # This RPC call should be accepted only by master. |
| 133 | if utils.is_shard(): |
| 134 | rpc_utils.route_rpc_to_master('label_add_hosts', id=id, hosts=hosts) |
| 135 | return |
| 136 | |
Prashanth Balasubramanian | 5949b4a | 2014-11-23 12:58:30 -0800 | [diff] [blame] | 137 | try: |
Prashanth Balasubramanian | 5949b4a | 2014-11-23 12:58:30 -0800 | [diff] [blame] | 138 | label = models.Label.smart_get(id) |
| 139 | except models.Label.DoesNotExist: |
| 140 | # This matches the type checks in smart_get, which is a hack |
| 141 | # in and off itself. The aim here is to create any non-existent |
| 142 | # label, which we cannot do if the 'id' specified isn't a label name. |
| 143 | if isinstance(id, basestring): |
| 144 | label = models.Label.smart_get(add_label(id)) |
| 145 | else: |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 146 | raise ValueError('Label id (%s) does not exist. Please specify ' |
| 147 | 'the argument, id, as a string (label name).' |
| 148 | % id) |
| 149 | add_label_to_hosts(id, hosts) |
MK Ryu | cf027c6 | 2015-03-04 12:00:50 -0800 | [diff] [blame] | 150 | |
| 151 | host_objs = models.Host.smart_get_bulk(hosts) |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 152 | # Make sure the label exists on the shard with the same id |
| 153 | # as it is on the master. |
MK Ryu | cf027c6 | 2015-03-04 12:00:50 -0800 | [diff] [blame] | 154 | # It is possible that the label is already in a shard because |
| 155 | # we are adding a new label only to shards of hosts that the label |
| 156 | # is going to be attached. |
| 157 | # For example, we add a label L1 to a host in shard S1. |
| 158 | # Master and S1 will have L1 but other shards won't. |
| 159 | # Later, when we add the same label L1 to hosts in shards S1 and S2, |
| 160 | # S1 already has the label but S2 doesn't. |
| 161 | # S2 should have the new label without any problem. |
MK Ryu | 9c5fbbe | 2015-02-11 15:46:22 -0800 | [diff] [blame] | 162 | # We ignore exception in such a case. |
| 163 | rpc_utils.fanout_rpc( |
| 164 | host_objs, 'add_label', name=label.name, id=label.id, |
| 165 | include_hostnames=False, ignore_exception_if_exists=True) |
| 166 | rpc_utils.fanout_rpc(host_objs, 'add_label_to_hosts', id=id) |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 167 | |
| 168 | |
MK Ryu | cf027c6 | 2015-03-04 12:00:50 -0800 | [diff] [blame] | 169 | def remove_label_from_hosts(id, hosts): |
| 170 | """Removes a label of the given id from the given hosts only in local DB. |
| 171 | |
| 172 | @param id: id or name of a label. |
| 173 | @param hosts: The hostnames of hosts that need to remove the label from. |
| 174 | """ |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 175 | host_objs = models.Host.smart_get_bulk(hosts) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 176 | models.Label.smart_get(id).host_set.remove(*host_objs) |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 177 | |
| 178 | |
MK Ryu | cf027c6 | 2015-03-04 12:00:50 -0800 | [diff] [blame] | 179 | def label_remove_hosts(id, hosts): |
| 180 | """Removes a label of the given id from the given hosts. |
| 181 | |
| 182 | This method should be run only on master not shards. |
| 183 | |
| 184 | @param id: id or name of a label. |
| 185 | @param hosts: A list of hostnames or ids. More often hostnames. |
| 186 | """ |
| 187 | # This RPC call should be accepted only by master. |
| 188 | if utils.is_shard(): |
| 189 | rpc_utils.route_rpc_to_master('label_remove_hosts', id=id, hosts=hosts) |
| 190 | return |
| 191 | |
| 192 | remove_label_from_hosts(id, hosts) |
| 193 | host_objs = models.Host.smart_get_bulk(hosts) |
| 194 | rpc_utils.fanout_rpc(host_objs, 'remove_label_from_hosts', id=id) |
| 195 | |
| 196 | |
Jiaxi Luo | 3187459 | 2014-06-11 10:36:35 -0700 | [diff] [blame] | 197 | def get_labels(exclude_filters=(), **filter_data): |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 198 | """\ |
Jiaxi Luo | 3187459 | 2014-06-11 10:36:35 -0700 | [diff] [blame] | 199 | @param exclude_filters: A sequence of dictionaries of filters. |
| 200 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 201 | @returns A sequence of nested dictionaries of label information. |
| 202 | """ |
Jiaxi Luo | 3187459 | 2014-06-11 10:36:35 -0700 | [diff] [blame] | 203 | labels = models.Label.query_objects(filter_data) |
| 204 | for exclude_filter in exclude_filters: |
| 205 | labels = labels.exclude(**exclude_filter) |
| 206 | return rpc_utils.prepare_rows_as_nested_dicts(labels, ('atomic_group',)) |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 207 | |
| 208 | |
| 209 | # atomic groups |
| 210 | |
showard | e9450c9 | 2009-06-30 01:58:52 +0000 | [diff] [blame] | 211 | def add_atomic_group(name, max_number_of_machines=None, description=None): |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 212 | return models.AtomicGroup.add_object( |
| 213 | name=name, max_number_of_machines=max_number_of_machines, |
| 214 | description=description).id |
| 215 | |
| 216 | |
| 217 | def modify_atomic_group(id, **data): |
| 218 | models.AtomicGroup.smart_get(id).update_object(data) |
| 219 | |
| 220 | |
| 221 | def delete_atomic_group(id): |
| 222 | models.AtomicGroup.smart_get(id).delete() |
| 223 | |
| 224 | |
| 225 | def atomic_group_add_labels(id, labels): |
| 226 | label_objs = models.Label.smart_get_bulk(labels) |
| 227 | models.AtomicGroup.smart_get(id).label_set.add(*label_objs) |
| 228 | |
| 229 | |
| 230 | def atomic_group_remove_labels(id, labels): |
| 231 | label_objs = models.Label.smart_get_bulk(labels) |
| 232 | models.AtomicGroup.smart_get(id).label_set.remove(*label_objs) |
| 233 | |
| 234 | |
| 235 | def get_atomic_groups(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 236 | return rpc_utils.prepare_for_serialization( |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 237 | models.AtomicGroup.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 238 | |
| 239 | |
| 240 | # hosts |
| 241 | |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 242 | def add_host(hostname, status=None, locked=None, lock_reason='', protection=None): |
| 243 | if locked and not lock_reason: |
| 244 | raise model_logic.ValidationError( |
| 245 | {'locked': 'Please provide a reason for locking when adding host.'}) |
| 246 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 247 | return models.Host.add_object(hostname=hostname, status=status, |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 248 | locked=locked, lock_reason=lock_reason, |
| 249 | protection=protection).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 250 | |
| 251 | |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 252 | @rpc_utils.forward_single_host_rpc_to_shard |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 253 | def modify_host(id, **data): |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 254 | """Modify local attributes of a host. |
| 255 | |
| 256 | If this is called on the master, but the host is assigned to a shard, this |
| 257 | will also forward the call to the responsible shard. This means i.e. if a |
| 258 | host is being locked using this function, this change will also propagate to |
| 259 | shards. |
| 260 | |
| 261 | @param id: id of the host to modify. |
| 262 | @param **data: key=value pairs of values to set on the host. |
| 263 | """ |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 264 | rpc_utils.check_modify_host(data) |
showard | ce7c092 | 2009-09-11 18:39:24 +0000 | [diff] [blame] | 265 | host = models.Host.smart_get(id) |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 266 | |
showard | ce7c092 | 2009-09-11 18:39:24 +0000 | [diff] [blame] | 267 | rpc_utils.check_modify_host_locking(host, data) |
| 268 | host.update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 269 | |
| 270 | |
showard | 276f944 | 2009-05-20 00:33:16 +0000 | [diff] [blame] | 271 | def modify_hosts(host_filter_data, update_data): |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 272 | """Modify local attributes of multiple hosts. |
| 273 | |
| 274 | If this is called on the master, but one of the hosts in that match the |
| 275 | filters is assigned to a shard, this will also forward the call to the |
| 276 | responsible shard. |
| 277 | |
| 278 | The filters are always applied on the master, not on the shards. This means |
| 279 | if the states of a host differ on the master and a shard, the state on the |
| 280 | master will be used. I.e. this means: |
| 281 | A host was synced to Shard 1. On Shard 1 the status of the host was set to |
| 282 | 'Repair Failed'. |
| 283 | - A call to modify_hosts with host_filter_data={'status': 'Ready'} will |
| 284 | update the host (both on the shard and on the master), because the state |
| 285 | of the host as the master knows it is still 'Ready'. |
| 286 | - A call to modify_hosts with host_filter_data={'status': 'Repair failed' |
| 287 | will not update the host, because the filter doesn't apply on the master. |
| 288 | |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 289 | @param host_filter_data: Filters out which hosts to modify. |
| 290 | @param update_data: A dictionary with the changes to make to the hosts. |
showard | 276f944 | 2009-05-20 00:33:16 +0000 | [diff] [blame] | 291 | """ |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 292 | rpc_utils.check_modify_host(update_data) |
showard | 276f944 | 2009-05-20 00:33:16 +0000 | [diff] [blame] | 293 | hosts = models.Host.query_objects(host_filter_data) |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 294 | |
| 295 | affected_shard_hostnames = set() |
| 296 | affected_host_ids = [] |
| 297 | |
Alex Miller | 9658a95 | 2013-05-14 16:40:02 -0700 | [diff] [blame] | 298 | # Check all hosts before changing data for exception safety. |
| 299 | for host in hosts: |
| 300 | rpc_utils.check_modify_host_locking(host, update_data) |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 301 | if host.shard: |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 302 | affected_shard_hostnames.add(host.shard.rpc_hostname()) |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 303 | affected_host_ids.append(host.id) |
| 304 | |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 305 | if not utils.is_shard(): |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 306 | # Caution: Changing the filter from the original here. See docstring. |
| 307 | rpc_utils.run_rpc_on_multiple_hostnames( |
| 308 | 'modify_hosts', affected_shard_hostnames, |
| 309 | host_filter_data={'id__in': affected_host_ids}, |
| 310 | update_data=update_data) |
| 311 | |
showard | 276f944 | 2009-05-20 00:33:16 +0000 | [diff] [blame] | 312 | for host in hosts: |
| 313 | host.update_object(update_data) |
| 314 | |
| 315 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 316 | def host_add_labels(id, labels): |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 317 | labels = models.Label.smart_get_bulk(labels) |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 318 | host = models.Host.smart_get(id) |
| 319 | |
| 320 | platforms = [label.name for label in labels if label.platform] |
| 321 | if len(platforms) > 1: |
| 322 | raise model_logic.ValidationError( |
| 323 | {'labels': 'Adding more than one platform label: %s' % |
| 324 | ', '.join(platforms)}) |
| 325 | if len(platforms) == 1: |
| 326 | models.Host.check_no_platform([host]) |
| 327 | host.labels.add(*labels) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | def host_remove_labels(id, labels): |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 331 | labels = models.Label.smart_get_bulk(labels) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 332 | models.Host.smart_get(id).labels.remove(*labels) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 333 | |
| 334 | |
MK Ryu | acf3592 | 2014-10-03 14:56:49 -0700 | [diff] [blame] | 335 | def get_host_attribute(attribute, **host_filter_data): |
| 336 | """ |
| 337 | @param attribute: string name of attribute |
| 338 | @param host_filter_data: filter data to apply to Hosts to choose hosts to |
| 339 | act upon |
| 340 | """ |
| 341 | hosts = rpc_utils.get_host_query((), False, False, True, host_filter_data) |
| 342 | hosts = list(hosts) |
| 343 | models.Host.objects.populate_relationships(hosts, models.HostAttribute, |
| 344 | 'attribute_list') |
| 345 | host_attr_dicts = [] |
| 346 | for host_obj in hosts: |
| 347 | for attr_obj in host_obj.attribute_list: |
| 348 | if attr_obj.attribute == attribute: |
| 349 | host_attr_dicts.append(attr_obj.get_object_dict()) |
| 350 | return rpc_utils.prepare_for_serialization(host_attr_dicts) |
| 351 | |
| 352 | |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 353 | def set_host_attribute(attribute, value, **host_filter_data): |
| 354 | """ |
| 355 | @param attribute string name of attribute |
| 356 | @param value string, or None to delete an attribute |
| 357 | @param host_filter_data filter data to apply to Hosts to choose hosts to act |
| 358 | upon |
| 359 | """ |
| 360 | assert host_filter_data # disallow accidental actions on all hosts |
| 361 | hosts = models.Host.query_objects(host_filter_data) |
| 362 | models.AclGroup.check_for_acl_violation_hosts(hosts) |
| 363 | |
| 364 | for host in hosts: |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 365 | host.set_or_delete_attribute(attribute, value) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 366 | |
| 367 | |
Jakob Juelich | 50e91f7 | 2014-10-01 12:43:23 -0700 | [diff] [blame] | 368 | @rpc_utils.forward_single_host_rpc_to_shard |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 369 | def delete_host(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 370 | models.Host.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 371 | |
| 372 | |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 373 | def get_hosts(multiple_labels=(), exclude_only_if_needed_labels=False, |
showard | 8aa84fc | 2009-09-16 17:17:55 +0000 | [diff] [blame] | 374 | exclude_atomic_group_hosts=False, valid_only=True, **filter_data): |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 375 | """ |
| 376 | @param multiple_labels: match hosts in all of the labels given. Should |
| 377 | be a list of label names. |
| 378 | @param exclude_only_if_needed_labels: Exclude hosts with at least one |
| 379 | "only_if_needed" label applied. |
| 380 | @param exclude_atomic_group_hosts: Exclude hosts that have one or more |
| 381 | atomic group labels associated with them. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 382 | """ |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 383 | hosts = rpc_utils.get_host_query(multiple_labels, |
| 384 | exclude_only_if_needed_labels, |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 385 | exclude_atomic_group_hosts, |
showard | 8aa84fc | 2009-09-16 17:17:55 +0000 | [diff] [blame] | 386 | valid_only, filter_data) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 387 | hosts = list(hosts) |
| 388 | models.Host.objects.populate_relationships(hosts, models.Label, |
| 389 | 'label_list') |
| 390 | models.Host.objects.populate_relationships(hosts, models.AclGroup, |
| 391 | 'acl_list') |
| 392 | models.Host.objects.populate_relationships(hosts, models.HostAttribute, |
| 393 | 'attribute_list') |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 394 | host_dicts = [] |
| 395 | for host_obj in hosts: |
| 396 | host_dict = host_obj.get_object_dict() |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 397 | host_dict['labels'] = [label.name for label in host_obj.label_list] |
showard | 909c914 | 2009-07-07 20:54:42 +0000 | [diff] [blame] | 398 | host_dict['platform'], host_dict['atomic_group'] = (rpc_utils. |
| 399 | find_platform_and_atomic_group(host_obj)) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 400 | host_dict['acls'] = [acl.name for acl in host_obj.acl_list] |
| 401 | host_dict['attributes'] = dict((attribute.attribute, attribute.value) |
| 402 | for attribute in host_obj.attribute_list) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 403 | host_dicts.append(host_dict) |
| 404 | return rpc_utils.prepare_for_serialization(host_dicts) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 405 | |
| 406 | |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 407 | def get_num_hosts(multiple_labels=(), exclude_only_if_needed_labels=False, |
showard | 8aa84fc | 2009-09-16 17:17:55 +0000 | [diff] [blame] | 408 | exclude_atomic_group_hosts=False, valid_only=True, |
| 409 | **filter_data): |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 410 | """ |
| 411 | Same parameters as get_hosts(). |
| 412 | |
| 413 | @returns The number of matching hosts. |
| 414 | """ |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 415 | hosts = rpc_utils.get_host_query(multiple_labels, |
| 416 | exclude_only_if_needed_labels, |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 417 | exclude_atomic_group_hosts, |
showard | 8aa84fc | 2009-09-16 17:17:55 +0000 | [diff] [blame] | 418 | valid_only, filter_data) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 419 | return hosts.count() |
showard | 1385b16 | 2008-03-13 15:59:40 +0000 | [diff] [blame] | 420 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 421 | |
| 422 | # tests |
| 423 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 424 | def add_test(name, test_type, path, author=None, dependencies=None, |
showard | 3d9899a | 2008-07-31 02:11:58 +0000 | [diff] [blame] | 425 | experimental=True, run_verify=None, test_class=None, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 426 | test_time=None, test_category=None, description=None, |
| 427 | sync_count=1): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 428 | return models.Test.add_object(name=name, test_type=test_type, path=path, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 429 | author=author, dependencies=dependencies, |
| 430 | experimental=experimental, |
| 431 | run_verify=run_verify, test_time=test_time, |
| 432 | test_category=test_category, |
| 433 | sync_count=sync_count, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 434 | test_class=test_class, |
| 435 | description=description).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 436 | |
| 437 | |
| 438 | def modify_test(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 439 | models.Test.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 440 | |
| 441 | |
| 442 | def delete_test(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 443 | models.Test.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 444 | |
| 445 | |
| 446 | def get_tests(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 447 | return rpc_utils.prepare_for_serialization( |
| 448 | models.Test.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 449 | |
| 450 | |
Moises Osorio | 2dc7a10 | 2014-12-02 18:24:02 -0800 | [diff] [blame] | 451 | @_timer.decorate |
| 452 | def get_tests_status_counts_by_job_name_label(job_name_prefix, label_name): |
| 453 | """Gets the counts of all passed and failed tests from the matching jobs. |
| 454 | |
| 455 | @param job_name_prefix: Name prefix of the jobs to get the summary from, e.g., |
| 456 | 'butterfly-release/R40-6457.21.0/bvt-cq/'. |
| 457 | @param label_name: Label that must be set in the jobs, e.g., |
| 458 | 'cros-version:butterfly-release/R40-6457.21.0'. |
| 459 | |
| 460 | @returns A summary of the counts of all the passed and failed tests. |
| 461 | """ |
| 462 | job_ids = list(models.Job.objects.filter( |
| 463 | name__startswith=job_name_prefix, |
| 464 | dependency_labels__name=label_name).values_list( |
| 465 | 'pk', flat=True)) |
| 466 | summary = {'passed': 0, 'failed': 0} |
| 467 | if not job_ids: |
| 468 | return summary |
| 469 | |
| 470 | counts = (tko_models.TestView.objects.filter( |
| 471 | afe_job_id__in=job_ids).exclude( |
| 472 | test_name='SERVER_JOB').exclude( |
| 473 | test_name__startswith='CLIENT_JOB').values( |
| 474 | 'status').annotate( |
| 475 | count=Count('status'))) |
| 476 | for status in counts: |
| 477 | if status['status'] == 'GOOD': |
| 478 | summary['passed'] += status['count'] |
| 479 | else: |
| 480 | summary['failed'] += status['count'] |
| 481 | return summary |
| 482 | |
| 483 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 484 | # profilers |
| 485 | |
| 486 | def add_profiler(name, description=None): |
| 487 | return models.Profiler.add_object(name=name, description=description).id |
| 488 | |
| 489 | |
| 490 | def modify_profiler(id, **data): |
| 491 | models.Profiler.smart_get(id).update_object(data) |
| 492 | |
| 493 | |
| 494 | def delete_profiler(id): |
| 495 | models.Profiler.smart_get(id).delete() |
| 496 | |
| 497 | |
| 498 | def get_profilers(**filter_data): |
| 499 | return rpc_utils.prepare_for_serialization( |
| 500 | models.Profiler.list_objects(filter_data)) |
| 501 | |
| 502 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 503 | # users |
| 504 | |
| 505 | def add_user(login, access_level=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 506 | return models.User.add_object(login=login, access_level=access_level).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 507 | |
| 508 | |
| 509 | def modify_user(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 510 | models.User.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 511 | |
| 512 | |
| 513 | def delete_user(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 514 | models.User.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 515 | |
| 516 | |
| 517 | def get_users(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 518 | return rpc_utils.prepare_for_serialization( |
| 519 | models.User.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 520 | |
| 521 | |
| 522 | # acl groups |
| 523 | |
| 524 | def add_acl_group(name, description=None): |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 525 | group = models.AclGroup.add_object(name=name, description=description) |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 526 | group.users.add(models.User.current_user()) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 527 | return group.id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 528 | |
| 529 | |
| 530 | def modify_acl_group(id, **data): |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 531 | group = models.AclGroup.smart_get(id) |
| 532 | group.check_for_acl_violation_acl_group() |
| 533 | group.update_object(data) |
| 534 | group.add_current_user_if_empty() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 535 | |
| 536 | |
| 537 | def acl_group_add_users(id, users): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 538 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 539 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 540 | users = models.User.smart_get_bulk(users) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 541 | group.users.add(*users) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 542 | |
| 543 | |
| 544 | def acl_group_remove_users(id, users): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 545 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 546 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 547 | users = models.User.smart_get_bulk(users) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 548 | group.users.remove(*users) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 549 | group.add_current_user_if_empty() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 550 | |
| 551 | |
| 552 | def acl_group_add_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 553 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 554 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 555 | hosts = models.Host.smart_get_bulk(hosts) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 556 | group.hosts.add(*hosts) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 557 | group.on_host_membership_change() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 558 | |
| 559 | |
| 560 | def acl_group_remove_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 561 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 562 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 563 | hosts = models.Host.smart_get_bulk(hosts) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 564 | group.hosts.remove(*hosts) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 565 | group.on_host_membership_change() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 566 | |
| 567 | |
| 568 | def delete_acl_group(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 569 | models.AclGroup.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 570 | |
| 571 | |
| 572 | def get_acl_groups(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 573 | acl_groups = models.AclGroup.list_objects(filter_data) |
| 574 | for acl_group in acl_groups: |
| 575 | acl_group_obj = models.AclGroup.objects.get(id=acl_group['id']) |
| 576 | acl_group['users'] = [user.login |
| 577 | for user in acl_group_obj.users.all()] |
| 578 | acl_group['hosts'] = [host.hostname |
| 579 | for host in acl_group_obj.hosts.all()] |
| 580 | return rpc_utils.prepare_for_serialization(acl_groups) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 581 | |
| 582 | |
| 583 | # jobs |
| 584 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 585 | def generate_control_file(tests=(), kernel=None, label=None, profilers=(), |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 586 | client_control_file='', use_container=False, |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 587 | profile_only=None, upload_kernel_config=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 588 | """ |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 589 | Generates a client-side control file to load a kernel and run tests. |
| 590 | |
| 591 | @param tests List of tests to run. |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 592 | @param kernel A list of kernel info dictionaries configuring which kernels |
| 593 | to boot for this job and other options for them |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 594 | @param label Name of label to grab kernel config from. |
| 595 | @param profilers List of profilers to activate during the job. |
| 596 | @param client_control_file The contents of a client-side control file to |
| 597 | run at the end of all tests. If this is supplied, all tests must be |
| 598 | client side. |
| 599 | TODO: in the future we should support server control files directly |
| 600 | to wrap with a kernel. That'll require changing the parameter |
| 601 | name and adding a boolean to indicate if it is a client or server |
| 602 | control file. |
| 603 | @param use_container unused argument today. TODO: Enable containers |
| 604 | on the host during a client side test. |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 605 | @param profile_only A boolean that indicates what default profile_only |
| 606 | mode to use in the control file. Passing None will generate a |
| 607 | control file that does not explcitly set the default mode at all. |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 608 | @param upload_kernel_config: if enabled it will generate server control |
| 609 | file code that uploads the kernel config file to the client and |
| 610 | tells the client of the new (local) path when compiling the kernel; |
| 611 | the tests must be server side tests |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 612 | |
| 613 | @returns a dict with the following keys: |
| 614 | control_file: str, The control file text. |
| 615 | is_server: bool, is the control file a server-side control file? |
| 616 | synch_count: How many machines the job uses per autoserv execution. |
| 617 | synch_count == 1 means the job is asynchronous. |
| 618 | dependencies: A list of the names of labels on which the job depends. |
| 619 | """ |
showard | d86debe | 2009-06-10 17:37:56 +0000 | [diff] [blame] | 620 | if not tests and not client_control_file: |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 621 | return dict(control_file='', is_server=False, synch_count=1, |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 622 | dependencies=[]) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 623 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 624 | cf_info, test_objects, profiler_objects, label = ( |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 625 | rpc_utils.prepare_generate_control_file(tests, kernel, label, |
| 626 | profilers)) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 627 | cf_info['control_file'] = control_file.generate_control( |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 628 | tests=test_objects, kernels=kernel, platform=label, |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 629 | profilers=profiler_objects, is_server=cf_info['is_server'], |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 630 | client_control_file=client_control_file, profile_only=profile_only, |
| 631 | upload_kernel_config=upload_kernel_config) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 632 | return cf_info |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 633 | |
| 634 | |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 635 | def create_parameterized_job(name, priority, test, parameters, kernel=None, |
| 636 | label=None, profilers=(), profiler_parameters=None, |
| 637 | use_container=False, profile_only=None, |
| 638 | upload_kernel_config=False, hosts=(), |
| 639 | meta_hosts=(), one_time_hosts=(), |
| 640 | atomic_group_name=None, synch_count=None, |
| 641 | is_template=False, timeout=None, |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 642 | timeout_mins=None, max_runtime_mins=None, |
| 643 | run_verify=False, email_list='', dependencies=(), |
| 644 | reboot_before=None, reboot_after=None, |
| 645 | parse_failed_repair=None, hostless=False, |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 646 | keyvals=None, drone_set=None, run_reset=True, |
| 647 | require_ssq=None): |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 648 | """ |
| 649 | Creates and enqueues a parameterized job. |
| 650 | |
| 651 | Most parameters a combination of the parameters for generate_control_file() |
| 652 | and create_job(), with the exception of: |
| 653 | |
| 654 | @param test name or ID of the test to run |
| 655 | @param parameters a map of parameter name -> |
| 656 | tuple of (param value, param type) |
| 657 | @param profiler_parameters a dictionary of parameters for the profilers: |
| 658 | key: profiler name |
| 659 | value: dict of param name -> tuple of |
| 660 | (param value, |
| 661 | param type) |
| 662 | """ |
| 663 | # Save the values of the passed arguments here. What we're going to do with |
| 664 | # them is pass them all to rpc_utils.get_create_job_common_args(), which |
| 665 | # will extract the subset of these arguments that apply for |
| 666 | # rpc_utils.create_job_common(), which we then pass in to that function. |
| 667 | args = locals() |
| 668 | |
| 669 | # Set up the parameterized job configs |
| 670 | test_obj = models.Test.smart_get(test) |
Aviv Keshet | 3dd8beb | 2013-05-13 17:36:04 -0700 | [diff] [blame] | 671 | control_type = test_obj.test_type |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 672 | |
| 673 | try: |
| 674 | label = models.Label.smart_get(label) |
| 675 | except models.Label.DoesNotExist: |
| 676 | label = None |
| 677 | |
| 678 | kernel_objs = models.Kernel.create_kernels(kernel) |
| 679 | profiler_objs = [models.Profiler.smart_get(profiler) |
| 680 | for profiler in profilers] |
| 681 | |
| 682 | parameterized_job = models.ParameterizedJob.objects.create( |
| 683 | test=test_obj, label=label, use_container=use_container, |
| 684 | profile_only=profile_only, |
| 685 | upload_kernel_config=upload_kernel_config) |
| 686 | parameterized_job.kernels.add(*kernel_objs) |
| 687 | |
| 688 | for profiler in profiler_objs: |
| 689 | parameterized_profiler = models.ParameterizedJobProfiler.objects.create( |
| 690 | parameterized_job=parameterized_job, |
| 691 | profiler=profiler) |
| 692 | profiler_params = profiler_parameters.get(profiler.name, {}) |
| 693 | for name, (value, param_type) in profiler_params.iteritems(): |
| 694 | models.ParameterizedJobProfilerParameter.objects.create( |
| 695 | parameterized_job_profiler=parameterized_profiler, |
| 696 | parameter_name=name, |
| 697 | parameter_value=value, |
| 698 | parameter_type=param_type) |
| 699 | |
| 700 | try: |
| 701 | for parameter in test_obj.testparameter_set.all(): |
| 702 | if parameter.name in parameters: |
| 703 | param_value, param_type = parameters.pop(parameter.name) |
| 704 | parameterized_job.parameterizedjobparameter_set.create( |
| 705 | test_parameter=parameter, parameter_value=param_value, |
| 706 | parameter_type=param_type) |
| 707 | |
| 708 | if parameters: |
| 709 | raise Exception('Extra parameters remain: %r' % parameters) |
| 710 | |
| 711 | return rpc_utils.create_job_common( |
| 712 | parameterized_job=parameterized_job.id, |
| 713 | control_type=control_type, |
| 714 | **rpc_utils.get_create_job_common_args(args)) |
| 715 | except: |
| 716 | parameterized_job.delete() |
| 717 | raise |
| 718 | |
| 719 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 720 | def create_job_page_handler(name, priority, control_file, control_type, |
| 721 | image=None, hostless=False, **kwargs): |
| 722 | """\ |
| 723 | Create and enqueue a job. |
| 724 | |
| 725 | @param name name of this job |
| 726 | @param priority Integer priority of this job. Higher is more important. |
| 727 | @param control_file String contents of the control file. |
| 728 | @param control_type Type of control file, Client or Server. |
| 729 | @param kwargs extra args that will be required by create_suite_job or |
| 730 | create_job. |
| 731 | |
| 732 | @returns The created Job id number. |
| 733 | """ |
| 734 | control_file = rpc_utils.encode_ascii(control_file) |
Jiaxi Luo | dd67beb | 2014-07-18 16:28:31 -0700 | [diff] [blame] | 735 | if not control_file: |
| 736 | raise model_logic.ValidationError({ |
| 737 | 'control_file' : "Control file cannot be empty"}) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 738 | |
| 739 | if image and hostless: |
| 740 | return site_rpc_interface.create_suite_job( |
| 741 | name=name, control_file=control_file, priority=priority, |
| 742 | build=image, **kwargs) |
| 743 | return create_job(name, priority, control_file, control_type, image=image, |
| 744 | hostless=hostless, **kwargs) |
| 745 | |
| 746 | |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 747 | def create_job(name, priority, control_file, control_type, |
| 748 | hosts=(), meta_hosts=(), one_time_hosts=(), |
| 749 | atomic_group_name=None, synch_count=None, is_template=False, |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 750 | timeout=None, timeout_mins=None, max_runtime_mins=None, |
| 751 | run_verify=False, email_list='', dependencies=(), |
| 752 | reboot_before=None, reboot_after=None, parse_failed_repair=None, |
| 753 | hostless=False, keyvals=None, drone_set=None, image=None, |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 754 | parent_job_id=None, test_retry=0, run_reset=True, |
| 755 | require_ssp=None, args=(), **kwargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 756 | """\ |
| 757 | Create and enqueue a job. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 758 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 759 | @param name name of this job |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 760 | @param priority Integer priority of this job. Higher is more important. |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 761 | @param control_file String contents of the control file. |
| 762 | @param control_type Type of control file, Client or Server. |
| 763 | @param synch_count How many machines the job uses per autoserv execution. |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 764 | synch_count == 1 means the job is asynchronous. If an atomic group is |
| 765 | given this value is treated as a minimum. |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 766 | @param is_template If true then create a template job. |
| 767 | @param timeout Hours after this call returns until the job times out. |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 768 | @param timeout_mins Minutes after this call returns until the job times |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 769 | out. |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 770 | @param max_runtime_mins Minutes from job starting time until job times out |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 771 | @param run_verify Should the host be verified before running the test? |
| 772 | @param email_list String containing emails to mail when the job is done |
| 773 | @param dependencies List of label names on which this job depends |
| 774 | @param reboot_before Never, If dirty, or Always |
| 775 | @param reboot_after Never, If all tests passed, or Always |
| 776 | @param parse_failed_repair if true, results of failed repairs launched by |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 777 | this job will be parsed as part of the job. |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 778 | @param hostless if true, create a hostless job |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 779 | @param keyvals dict of keyvals to associate with the job |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 780 | @param hosts List of hosts to run job on. |
| 781 | @param meta_hosts List where each entry is a label name, and for each entry |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 782 | one host will be chosen from that label to run the job on. |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 783 | @param one_time_hosts List of hosts not in the database to run the job on. |
| 784 | @param atomic_group_name The name of an atomic group to schedule the job on. |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 785 | @param drone_set The name of the drone set to run this test on. |
Paul Pendlebury | 5a8c6ad | 2011-02-01 07:20:17 -0800 | [diff] [blame] | 786 | @param image OS image to install before running job. |
Aviv Keshet | 0b9cfc9 | 2013-02-05 11:36:02 -0800 | [diff] [blame] | 787 | @param parent_job_id id of a job considered to be parent of created job. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 788 | @param test_retry Number of times to retry test if the test did not |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 789 | complete successfully. (optional, default: 0) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 790 | @param run_reset Should the host be reset before running the test? |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 791 | @param require_ssp Set to True to require server-side packaging to run the |
| 792 | test. If it's set to None, drone will still try to run |
| 793 | the server side with server-side packaging. If the |
| 794 | autotest-server package doesn't exist for the build or |
| 795 | image is not set, drone will run the test without server- |
| 796 | side packaging. Default is None. |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 797 | @param args A list of args to be injected into control file. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 798 | @param kwargs extra keyword args. NOT USED. |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 799 | |
| 800 | @returns The created Job id number. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 801 | """ |
Jiaxi Luo | 90190c9 | 2014-06-18 12:35:57 -0700 | [diff] [blame] | 802 | if args: |
| 803 | control_file = tools.inject_vars({'args': args}, control_file) |
| 804 | |
Simran Basi | ab5a1bf | 2014-05-28 15:39:44 -0700 | [diff] [blame] | 805 | if image is None: |
| 806 | return rpc_utils.create_job_common( |
| 807 | **rpc_utils.get_create_job_common_args(locals())) |
| 808 | |
| 809 | # When image is supplied use a known parameterized test already in the |
| 810 | # database to pass the OS image path from the front end, through the |
| 811 | # scheduler, and finally to autoserv as the --image parameter. |
| 812 | |
| 813 | # The test autoupdate_ParameterizedJob is in afe_autotests and used to |
| 814 | # instantiate a Test object and from there a ParameterizedJob. |
| 815 | known_test_obj = models.Test.smart_get('autoupdate_ParameterizedJob') |
| 816 | known_parameterized_job = models.ParameterizedJob.objects.create( |
| 817 | test=known_test_obj) |
| 818 | |
| 819 | # autoupdate_ParameterizedJob has a single parameter, the image parameter, |
| 820 | # stored in the table afe_test_parameters. We retrieve and set this |
| 821 | # instance of the parameter to the OS image path. |
| 822 | image_parameter = known_test_obj.testparameter_set.get(test=known_test_obj, |
| 823 | name='image') |
| 824 | known_parameterized_job.parameterizedjobparameter_set.create( |
| 825 | test_parameter=image_parameter, parameter_value=image, |
| 826 | parameter_type='string') |
| 827 | |
| 828 | # By passing a parameterized_job to create_job_common the job entry in |
| 829 | # the afe_jobs table will have the field parameterized_job_id set. |
| 830 | # The scheduler uses this id in the afe_parameterized_jobs table to |
| 831 | # match this job to our known test, and then with the |
| 832 | # afe_parameterized_job_parameters table to get the actual image path. |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 833 | return rpc_utils.create_job_common( |
Simran Basi | ab5a1bf | 2014-05-28 15:39:44 -0700 | [diff] [blame] | 834 | parameterized_job=known_parameterized_job.id, |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 835 | **rpc_utils.get_create_job_common_args(locals())) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 836 | |
| 837 | |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 838 | def abort_host_queue_entries(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 839 | """\ |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 840 | Abort a set of host queue entries. |
Fang Deng | 63b0e45 | 2014-12-19 14:38:15 -0800 | [diff] [blame] | 841 | |
| 842 | @return: A list of dictionaries, each contains information |
| 843 | about an aborted HQE. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 844 | """ |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 845 | query = models.HostQueueEntry.query_objects(filter_data) |
beeps | faecbce | 2013-10-29 11:35:10 -0700 | [diff] [blame] | 846 | |
| 847 | # Dont allow aborts on: |
| 848 | # 1. Jobs that have already completed (whether or not they were aborted) |
| 849 | # 2. Jobs that we have already been aborted (but may not have completed) |
| 850 | query = query.filter(complete=False).filter(aborted=False) |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 851 | models.AclGroup.check_abort_permissions(query) |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 852 | host_queue_entries = list(query.select_related()) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 853 | rpc_utils.check_abort_synchronous_jobs(host_queue_entries) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 854 | |
Simran Basi | c1b2676 | 2013-06-26 14:23:21 -0700 | [diff] [blame] | 855 | models.HostQueueEntry.abort_host_queue_entries(host_queue_entries) |
Fang Deng | 63b0e45 | 2014-12-19 14:38:15 -0800 | [diff] [blame] | 856 | hqe_info = [{'HostQueueEntry': hqe.id, 'Job': hqe.job_id, |
| 857 | 'Job name': hqe.job.name} for hqe in host_queue_entries] |
| 858 | return hqe_info |
showard | 9d821ab | 2008-07-11 16:54:29 +0000 | [diff] [blame] | 859 | |
| 860 | |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 861 | def abort_special_tasks(**filter_data): |
| 862 | """\ |
| 863 | Abort the special task, or tasks, specified in the filter. |
| 864 | """ |
| 865 | query = models.SpecialTask.query_objects(filter_data) |
| 866 | special_tasks = query.filter(is_active=True) |
| 867 | for task in special_tasks: |
| 868 | task.abort() |
| 869 | |
| 870 | |
Simran Basi | 73dae55 | 2013-02-25 14:57:46 -0800 | [diff] [blame] | 871 | def _call_special_tasks_on_hosts(task, hosts): |
| 872 | """\ |
| 873 | Schedules a set of hosts for a special task. |
| 874 | |
| 875 | @returns A list of hostnames that a special task was created for. |
| 876 | """ |
| 877 | models.AclGroup.check_for_acl_violation_hosts(hosts) |
Prashanth Balasubramanian | 6edaaf9 | 2014-11-24 16:36:25 -0800 | [diff] [blame] | 878 | shard_host_map = rpc_utils.bucket_hosts_by_shard(hosts) |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 879 | if shard_host_map and not utils.is_shard(): |
Prashanth Balasubramanian | 6edaaf9 | 2014-11-24 16:36:25 -0800 | [diff] [blame] | 880 | raise ValueError('The following hosts are on shards, please ' |
| 881 | 'follow the link to the shards and create jobs ' |
| 882 | 'there instead. %s.' % shard_host_map) |
Simran Basi | 73dae55 | 2013-02-25 14:57:46 -0800 | [diff] [blame] | 883 | for host in hosts: |
| 884 | models.SpecialTask.schedule_special_task(host, task) |
| 885 | return list(sorted(host.hostname for host in hosts)) |
| 886 | |
| 887 | |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 888 | def reverify_hosts(**filter_data): |
| 889 | """\ |
| 890 | Schedules a set of hosts for verify. |
mbligh | 4e545a5 | 2009-12-19 05:30:39 +0000 | [diff] [blame] | 891 | |
| 892 | @returns A list of hostnames that a verify task was created for. |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 893 | """ |
Prashanth Balasubramanian | 4098123 | 2014-12-16 19:01:58 -0800 | [diff] [blame] | 894 | hosts = models.Host.query_objects(filter_data) |
| 895 | shard_host_map = rpc_utils.bucket_hosts_by_shard(hosts, rpc_hostnames=True) |
| 896 | |
| 897 | # Filter out hosts on a shard from those on the master, forward |
| 898 | # rpcs to the shard with an additional hostname__in filter, and |
| 899 | # create a local SpecialTask for each remaining host. |
Prashanth Balasubramanian | 8c98ac1 | 2014-12-23 11:26:44 -0800 | [diff] [blame] | 900 | if shard_host_map and not utils.is_shard(): |
Prashanth Balasubramanian | 4098123 | 2014-12-16 19:01:58 -0800 | [diff] [blame] | 901 | hosts = [h for h in hosts if h.shard is None] |
| 902 | for shard, hostnames in shard_host_map.iteritems(): |
| 903 | |
| 904 | # The main client of this module is the frontend website, and |
| 905 | # it invokes it with an 'id' or an 'id__in' filter. Regardless, |
| 906 | # the 'hostname' filter should narrow down the list of hosts on |
| 907 | # each shard even though we supply all the ids in filter_data. |
| 908 | # This method uses hostname instead of id because it fits better |
| 909 | # with the overall architecture of redirection functions in rpc_utils. |
| 910 | shard_filter = filter_data.copy() |
| 911 | shard_filter['hostname__in'] = hostnames |
| 912 | rpc_utils.run_rpc_on_multiple_hostnames( |
| 913 | 'reverify_hosts', [shard], **shard_filter) |
| 914 | |
| 915 | # There is a race condition here if someone assigns a shard to one of these |
| 916 | # hosts before we create the task. The host will stay on the master if: |
| 917 | # 1. The host is not Ready |
| 918 | # 2. The host is Ready but has a task |
| 919 | # But if the host is Ready and doesn't have a task yet, it will get sent |
| 920 | # to the shard as we're creating a task here. |
| 921 | |
| 922 | # Given that we only rarely verify Ready hosts it isn't worth putting this |
| 923 | # entire method in a transaction. The worst case scenario is that we have |
| 924 | # a verify running on a Ready host while the shard is using it, if the verify |
| 925 | # fails no subsequent tasks will be created against the host on the master, |
| 926 | # and verifies are safe enough that this is OK. |
| 927 | return _call_special_tasks_on_hosts(models.SpecialTask.Task.VERIFY, hosts) |
Simran Basi | 73dae55 | 2013-02-25 14:57:46 -0800 | [diff] [blame] | 928 | |
| 929 | |
| 930 | def repair_hosts(**filter_data): |
| 931 | """\ |
| 932 | Schedules a set of hosts for repair. |
| 933 | |
| 934 | @returns A list of hostnames that a repair task was created for. |
| 935 | """ |
| 936 | return _call_special_tasks_on_hosts(models.SpecialTask.Task.REPAIR, |
| 937 | models.Host.query_objects(filter_data)) |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 938 | |
| 939 | |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 940 | def get_jobs(not_yet_run=False, running=False, finished=False, |
| 941 | suite=False, sub=False, standalone=False, **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 942 | """\ |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 943 | Extra status filter args for get_jobs: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 944 | -not_yet_run: Include only jobs that have not yet started running. |
| 945 | -running: Include only jobs that have start running but for which not |
| 946 | all hosts have completed. |
| 947 | -finished: Include only jobs for which all hosts have completed (or |
| 948 | aborted). |
| 949 | At most one of these three fields should be specified. |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 950 | |
| 951 | Extra type filter args for get_jobs: |
| 952 | -suite: Include only jobs with child jobs. |
| 953 | -sub: Include only jobs with a parent job. |
| 954 | -standalone: Inlcude only jobs with no child or parent jobs. |
| 955 | At most one of these three fields should be specified. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 956 | """ |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 957 | extra_args = rpc_utils.extra_job_status_filters(not_yet_run, |
| 958 | running, |
| 959 | finished) |
| 960 | filter_data['extra_args'] = rpc_utils.extra_job_type_filters(extra_args, |
| 961 | suite, |
| 962 | sub, |
| 963 | standalone) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 964 | job_dicts = [] |
| 965 | jobs = list(models.Job.query_objects(filter_data)) |
| 966 | models.Job.objects.populate_relationships(jobs, models.Label, |
| 967 | 'dependencies') |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 968 | models.Job.objects.populate_relationships(jobs, models.JobKeyval, 'keyvals') |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 969 | for job in jobs: |
| 970 | job_dict = job.get_object_dict() |
| 971 | job_dict['dependencies'] = ','.join(label.name |
| 972 | for label in job.dependencies) |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 973 | job_dict['keyvals'] = dict((keyval.key, keyval.value) |
| 974 | for keyval in job.keyvals) |
Eric Li | d23bc19 | 2011-02-09 14:38:57 -0800 | [diff] [blame] | 975 | if job.parameterized_job: |
| 976 | job_dict['image'] = get_parameterized_autoupdate_image_url(job) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 977 | job_dicts.append(job_dict) |
| 978 | return rpc_utils.prepare_for_serialization(job_dicts) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 979 | |
| 980 | |
| 981 | def get_num_jobs(not_yet_run=False, running=False, finished=False, |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 982 | suite=False, sub=False, standalone=False, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 983 | **filter_data): |
| 984 | """\ |
| 985 | See get_jobs() for documentation of extra filter parameters. |
| 986 | """ |
Jiaxi Luo | 15cbf37 | 2014-07-01 19:20:20 -0700 | [diff] [blame] | 987 | extra_args = rpc_utils.extra_job_status_filters(not_yet_run, |
| 988 | running, |
| 989 | finished) |
| 990 | filter_data['extra_args'] = rpc_utils.extra_job_type_filters(extra_args, |
| 991 | suite, |
| 992 | sub, |
| 993 | standalone) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 994 | return models.Job.query_count(filter_data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 995 | |
| 996 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 997 | def get_jobs_summary(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 998 | """\ |
Jiaxi Luo | aac5457 | 2014-06-04 13:57:02 -0700 | [diff] [blame] | 999 | Like get_jobs(), but adds 'status_counts' and 'result_counts' field. |
| 1000 | |
| 1001 | 'status_counts' filed is a dictionary mapping status strings to the number |
| 1002 | of hosts currently with that status, i.e. {'Queued' : 4, 'Running' : 2}. |
| 1003 | |
| 1004 | 'result_counts' field is piped to tko's rpc_interface and has the return |
| 1005 | format specified under get_group_counts. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1006 | """ |
| 1007 | jobs = get_jobs(**filter_data) |
| 1008 | ids = [job['id'] for job in jobs] |
| 1009 | all_status_counts = models.Job.objects.get_status_counts(ids) |
| 1010 | for job in jobs: |
| 1011 | job['status_counts'] = all_status_counts[job['id']] |
Jiaxi Luo | aac5457 | 2014-06-04 13:57:02 -0700 | [diff] [blame] | 1012 | job['result_counts'] = tko_rpc_interface.get_status_counts( |
| 1013 | ['afe_job_id', 'afe_job_id'], |
| 1014 | header_groups=[['afe_job_id'], ['afe_job_id']], |
| 1015 | **{'afe_job_id': job['id']}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1016 | return rpc_utils.prepare_for_serialization(jobs) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1017 | |
| 1018 | |
showard | a965cef | 2009-05-15 23:17:41 +0000 | [diff] [blame] | 1019 | def get_info_for_clone(id, preserve_metahosts, queue_entry_filter_data=None): |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 1020 | """\ |
| 1021 | Retrieves all the information needed to clone a job. |
| 1022 | """ |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 1023 | job = models.Job.objects.get(id=id) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1024 | job_info = rpc_utils.get_job_info(job, |
showard | a965cef | 2009-05-15 23:17:41 +0000 | [diff] [blame] | 1025 | preserve_metahosts, |
| 1026 | queue_entry_filter_data) |
showard | 945072f | 2008-09-03 20:34:59 +0000 | [diff] [blame] | 1027 | |
showard | d9992fe | 2008-07-31 02:15:03 +0000 | [diff] [blame] | 1028 | host_dicts = [] |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1029 | for host in job_info['hosts']: |
| 1030 | host_dict = get_hosts(id=host.id)[0] |
| 1031 | other_labels = host_dict['labels'] |
| 1032 | if host_dict['platform']: |
| 1033 | other_labels.remove(host_dict['platform']) |
| 1034 | host_dict['other_labels'] = ', '.join(other_labels) |
showard | d9992fe | 2008-07-31 02:15:03 +0000 | [diff] [blame] | 1035 | host_dicts.append(host_dict) |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 1036 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1037 | for host in job_info['one_time_hosts']: |
| 1038 | host_dict = dict(hostname=host.hostname, |
| 1039 | id=host.id, |
| 1040 | platform='(one-time host)', |
| 1041 | locked_text='') |
| 1042 | host_dicts.append(host_dict) |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 1043 | |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 1044 | # convert keys from Label objects to strings (names of labels) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1045 | meta_host_counts = dict((meta_host.name, count) for meta_host, count |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 1046 | in job_info['meta_host_counts'].iteritems()) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1047 | |
| 1048 | info = dict(job=job.get_object_dict(), |
| 1049 | meta_host_counts=meta_host_counts, |
| 1050 | hosts=host_dicts) |
| 1051 | info['job']['dependencies'] = job_info['dependencies'] |
| 1052 | if job_info['atomic_group']: |
| 1053 | info['atomic_group_name'] = (job_info['atomic_group']).name |
| 1054 | else: |
| 1055 | info['atomic_group_name'] = None |
jamesren | 2275ef1 | 2010-04-12 18:25:06 +0000 | [diff] [blame] | 1056 | info['hostless'] = job_info['hostless'] |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1057 | info['drone_set'] = job.drone_set and job.drone_set.name |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 1058 | |
Eric Li | d23bc19 | 2011-02-09 14:38:57 -0800 | [diff] [blame] | 1059 | if job.parameterized_job: |
| 1060 | info['job']['image'] = get_parameterized_autoupdate_image_url(job) |
| 1061 | |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 1062 | return rpc_utils.prepare_for_serialization(info) |
| 1063 | |
| 1064 | |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 1065 | # host queue entries |
| 1066 | |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1067 | def get_host_queue_entries(start_time=None, end_time=None, **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1068 | """\ |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 1069 | @returns A sequence of nested dictionaries of host and job information. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1070 | """ |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1071 | filter_data = rpc_utils.inject_times_to_filter('started_on__gte', |
| 1072 | 'started_on__lte', |
| 1073 | start_time, |
| 1074 | end_time, |
| 1075 | **filter_data) |
J. Richard Barnette | b5164d6 | 2015-04-13 12:59:31 -0700 | [diff] [blame] | 1076 | return rpc_utils.prepare_rows_as_nested_dicts( |
| 1077 | models.HostQueueEntry.query_objects(filter_data), |
| 1078 | ('host', 'atomic_group', 'job')) |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 1079 | |
| 1080 | |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1081 | def get_num_host_queue_entries(start_time=None, end_time=None, **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1082 | """\ |
| 1083 | Get the number of host queue entries associated with this job. |
| 1084 | """ |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1085 | filter_data = rpc_utils.inject_times_to_filter('started_on__gte', |
| 1086 | 'started_on__lte', |
| 1087 | start_time, |
| 1088 | end_time, |
| 1089 | **filter_data) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1090 | return models.HostQueueEntry.query_count(filter_data) |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 1091 | |
| 1092 | |
showard | 1e935f1 | 2008-07-11 00:11:36 +0000 | [diff] [blame] | 1093 | def get_hqe_percentage_complete(**filter_data): |
| 1094 | """ |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 1095 | Computes the fraction of host queue entries matching the given filter data |
showard | 1e935f1 | 2008-07-11 00:11:36 +0000 | [diff] [blame] | 1096 | that are complete. |
| 1097 | """ |
| 1098 | query = models.HostQueueEntry.query_objects(filter_data) |
| 1099 | complete_count = query.filter(complete=True).count() |
| 1100 | total_count = query.count() |
| 1101 | if total_count == 0: |
| 1102 | return 1 |
| 1103 | return float(complete_count) / total_count |
| 1104 | |
| 1105 | |
showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 1106 | # special tasks |
| 1107 | |
| 1108 | def get_special_tasks(**filter_data): |
J. Richard Barnette | b5164d6 | 2015-04-13 12:59:31 -0700 | [diff] [blame] | 1109 | """Get special task entries from the local database. |
| 1110 | |
| 1111 | Query the special tasks table for tasks matching the given |
| 1112 | `filter_data`, and return a list of the results. No attempt is |
| 1113 | made to forward the call to shards; the buck will stop here. |
| 1114 | The caller is expected to know the target shard for such reasons |
| 1115 | as: |
| 1116 | * The caller is a service (such as gs_offloader) configured |
| 1117 | to operate on behalf of one specific shard, and no other. |
| 1118 | * The caller has a host as a parameter, and knows that this is |
| 1119 | the shard assigned to that host. |
| 1120 | |
| 1121 | @param filter_data Filter keywords to pass to the underlying |
| 1122 | database query. |
| 1123 | |
| 1124 | """ |
J. Richard Barnette | fdfcd66 | 2015-04-13 17:20:29 -0700 | [diff] [blame] | 1125 | return rpc_utils.prepare_rows_as_nested_dicts( |
| 1126 | models.SpecialTask.query_objects(filter_data), |
| 1127 | ('host', 'queue_entry')) |
J. Richard Barnette | b5164d6 | 2015-04-13 12:59:31 -0700 | [diff] [blame] | 1128 | |
| 1129 | |
| 1130 | def get_host_special_tasks(host_id, **filter_data): |
| 1131 | """Get special task entries for a given host. |
| 1132 | |
| 1133 | Query the special tasks table for tasks that ran on the host |
| 1134 | given by `host_id` and matching the given `filter_data`. |
| 1135 | Return a list of the results. If the host is assigned to a |
| 1136 | shard, forward this call to that shard. |
| 1137 | |
| 1138 | @param host_id Id in the database of the target host. |
| 1139 | @param filter_data Filter keywords to pass to the underlying |
| 1140 | database query. |
| 1141 | |
| 1142 | """ |
| 1143 | host = models.Host.smart_get(host_id) |
| 1144 | if not host.shard: |
J. Richard Barnette | fdfcd66 | 2015-04-13 17:20:29 -0700 | [diff] [blame] | 1145 | return get_special_tasks(host_id=host_id, **filter_data) |
J. Richard Barnette | b5164d6 | 2015-04-13 12:59:31 -0700 | [diff] [blame] | 1146 | else: |
J. Richard Barnette | 39255fa | 2015-04-14 17:23:41 -0700 | [diff] [blame] | 1147 | # The return values from AFE methods are post-processed |
| 1148 | # objects that aren't JSON-serializable. So, we have to |
| 1149 | # call AFE.run() to get the raw, serializable output from |
| 1150 | # the shard. |
J. Richard Barnette | b5164d6 | 2015-04-13 12:59:31 -0700 | [diff] [blame] | 1151 | shard_afe = frontend.AFE(server=host.shard.rpc_hostname()) |
| 1152 | return shard_afe.run('get_special_tasks', |
| 1153 | host_id=host_id, **filter_data) |
showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 1154 | |
| 1155 | |
J. Richard Barnette | 39255fa | 2015-04-14 17:23:41 -0700 | [diff] [blame] | 1156 | def get_status_task(host_id, end_time): |
J. Richard Barnette | 4d7e6e6 | 2015-05-01 10:47:34 -0700 | [diff] [blame^] | 1157 | """Get the "status task" for a host from the local shard. |
J. Richard Barnette | 39255fa | 2015-04-14 17:23:41 -0700 | [diff] [blame] | 1158 | |
J. Richard Barnette | 4d7e6e6 | 2015-05-01 10:47:34 -0700 | [diff] [blame^] | 1159 | Returns a single special task representing the given host's |
| 1160 | "status task". The status task is a completed special task that |
| 1161 | identifies whether the corresponding host was working or broken |
| 1162 | when it completed. A successful task indicates a working host; |
| 1163 | a failed task indicates broken. |
J. Richard Barnette | 39255fa | 2015-04-14 17:23:41 -0700 | [diff] [blame] | 1164 | |
J. Richard Barnette | 4d7e6e6 | 2015-05-01 10:47:34 -0700 | [diff] [blame^] | 1165 | This call will not be forward to a shard; the receiving server |
| 1166 | must be the shard that owns the host. |
| 1167 | |
| 1168 | @param host_id Id in the database of the target host. |
| 1169 | @param end_time Time reference for the host's status. |
| 1170 | |
| 1171 | @return A single task; its status (successful or not) |
| 1172 | corresponds to the status of the host (working or |
| 1173 | broken) at the given time. If no task is found, return |
| 1174 | `None`. |
| 1175 | |
| 1176 | """ |
| 1177 | tasklist = rpc_utils.prepare_rows_as_nested_dicts( |
| 1178 | status_history.get_status_task(host_id, end_time), |
| 1179 | ('host', 'queue_entry')) |
| 1180 | return tasklist[0] if tasklist else None |
| 1181 | |
| 1182 | |
| 1183 | def get_host_status_task(host_id, end_time): |
| 1184 | """Get the "status task" for a host from its owning shard. |
| 1185 | |
| 1186 | Finds the given host's owning shard, and forwards to it a call |
| 1187 | to `get_status_task()` (see above). |
J. Richard Barnette | 39255fa | 2015-04-14 17:23:41 -0700 | [diff] [blame] | 1188 | |
| 1189 | @param host_id Id in the database of the target host. |
| 1190 | @param end_time Time reference for the host's status. |
| 1191 | |
| 1192 | @return A single task; its status (successful or not) |
| 1193 | corresponds to the status of the host (working or |
| 1194 | broken) at the given time. If no task is found, return |
| 1195 | `None`. |
| 1196 | |
| 1197 | """ |
| 1198 | host = models.Host.smart_get(host_id) |
| 1199 | if not host.shard: |
J. Richard Barnette | 4d7e6e6 | 2015-05-01 10:47:34 -0700 | [diff] [blame^] | 1200 | return get_status_task(host_id, end_time) |
J. Richard Barnette | 39255fa | 2015-04-14 17:23:41 -0700 | [diff] [blame] | 1201 | else: |
| 1202 | # The return values from AFE methods are post-processed |
| 1203 | # objects that aren't JSON-serializable. So, we have to |
| 1204 | # call AFE.run() to get the raw, serializable output from |
| 1205 | # the shard. |
| 1206 | shard_afe = frontend.AFE(server=host.shard.rpc_hostname()) |
| 1207 | return shard_afe.run('get_status_task', |
| 1208 | host_id=host_id, end_time=end_time) |
| 1209 | |
| 1210 | |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 1211 | # support for host detail view |
| 1212 | |
Jiaxi Luo | 79ce642 | 2014-06-13 17:08:09 -0700 | [diff] [blame] | 1213 | def get_host_queue_entries_and_special_tasks(host_id, query_start=None, |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1214 | query_limit=None, start_time=None, |
| 1215 | end_time=None): |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 1216 | """ |
| 1217 | @returns an interleaved list of HostQueueEntries and SpecialTasks, |
| 1218 | in approximate run order. each dict contains keys for type, host, |
| 1219 | job, status, started_on, execution_path, and ID. |
| 1220 | """ |
| 1221 | total_limit = None |
| 1222 | if query_limit is not None: |
| 1223 | total_limit = query_start + query_limit |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1224 | filter_data_common = {'host': host_id, |
| 1225 | 'query_limit': total_limit, |
| 1226 | 'sort_by': ['-id']} |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 1227 | |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1228 | filter_data_queue_entries, filter_data_special_tasks = ( |
| 1229 | rpc_utils.inject_times_to_hqe_special_tasks_filters( |
| 1230 | filter_data_common, start_time, end_time)) |
| 1231 | |
| 1232 | queue_entries = list(models.HostQueueEntry.query_objects( |
| 1233 | filter_data_queue_entries)) |
| 1234 | special_tasks = list(models.SpecialTask.query_objects( |
| 1235 | filter_data_special_tasks)) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 1236 | |
| 1237 | interleaved_entries = rpc_utils.interleave_entries(queue_entries, |
| 1238 | special_tasks) |
| 1239 | if query_start is not None: |
| 1240 | interleaved_entries = interleaved_entries[query_start:] |
| 1241 | if query_limit is not None: |
| 1242 | interleaved_entries = interleaved_entries[:query_limit] |
| 1243 | return rpc_utils.prepare_for_serialization(interleaved_entries) |
| 1244 | |
| 1245 | |
Jiaxi Luo | 57bc195 | 2014-07-22 15:27:30 -0700 | [diff] [blame] | 1246 | def get_num_host_queue_entries_and_special_tasks(host_id, start_time=None, |
| 1247 | end_time=None): |
| 1248 | filter_data_common = {'host': host_id} |
| 1249 | |
| 1250 | filter_data_queue_entries, filter_data_special_tasks = ( |
| 1251 | rpc_utils.inject_times_to_hqe_special_tasks_filters( |
| 1252 | filter_data_common, start_time, end_time)) |
| 1253 | |
| 1254 | return (models.HostQueueEntry.query_count(filter_data_queue_entries) |
| 1255 | + models.SpecialTask.query_count(filter_data_special_tasks)) |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 1256 | |
| 1257 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1258 | # recurring run |
| 1259 | |
| 1260 | def get_recurring(**filter_data): |
| 1261 | return rpc_utils.prepare_rows_as_nested_dicts( |
| 1262 | models.RecurringRun.query_objects(filter_data), |
| 1263 | ('job', 'owner')) |
| 1264 | |
| 1265 | |
| 1266 | def get_num_recurring(**filter_data): |
| 1267 | return models.RecurringRun.query_count(filter_data) |
| 1268 | |
| 1269 | |
| 1270 | def delete_recurring_runs(**filter_data): |
| 1271 | to_delete = models.RecurringRun.query_objects(filter_data) |
| 1272 | to_delete.delete() |
| 1273 | |
| 1274 | |
| 1275 | def create_recurring_run(job_id, start_date, loop_period, loop_count): |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1276 | owner = models.User.current_user().login |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1277 | job = models.Job.objects.get(id=job_id) |
| 1278 | return job.create_recurring_job(start_date=start_date, |
| 1279 | loop_period=loop_period, |
| 1280 | loop_count=loop_count, |
| 1281 | owner=owner) |
| 1282 | |
| 1283 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1284 | # other |
| 1285 | |
showard | e0b6362 | 2008-08-04 20:58:47 +0000 | [diff] [blame] | 1286 | def echo(data=""): |
| 1287 | """\ |
| 1288 | Returns a passed in string. For doing a basic test to see if RPC calls |
| 1289 | can successfully be made. |
| 1290 | """ |
| 1291 | return data |
| 1292 | |
| 1293 | |
showard | b7a52fd | 2009-04-27 20:10:56 +0000 | [diff] [blame] | 1294 | def get_motd(): |
| 1295 | """\ |
| 1296 | Returns the message of the day as a string. |
| 1297 | """ |
| 1298 | return rpc_utils.get_motd() |
| 1299 | |
| 1300 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1301 | def get_static_data(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1302 | """\ |
| 1303 | Returns a dictionary containing a bunch of data that shouldn't change |
| 1304 | often and is otherwise inaccessible. This includes: |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 1305 | |
| 1306 | priorities: List of job priority choices. |
| 1307 | default_priority: Default priority value for new jobs. |
| 1308 | users: Sorted list of all users. |
Jiaxi Luo | 3187459 | 2014-06-11 10:36:35 -0700 | [diff] [blame] | 1309 | labels: Sorted list of labels not start with 'cros-version' and |
| 1310 | 'fw-version'. |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 1311 | atomic_groups: Sorted list of all atomic groups. |
| 1312 | tests: Sorted list of all tests. |
| 1313 | profilers: Sorted list of all profilers. |
| 1314 | current_user: Logged-in username. |
| 1315 | host_statuses: Sorted list of possible Host statuses. |
| 1316 | job_statuses: Sorted list of possible HostQueueEntry statuses. |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 1317 | job_timeout_default: The default job timeout length in minutes. |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1318 | parse_failed_repair_default: Default value for the parse_failed_repair job |
Jiaxi Luo | 3187459 | 2014-06-11 10:36:35 -0700 | [diff] [blame] | 1319 | option. |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 1320 | reboot_before_options: A list of valid RebootBefore string enums. |
| 1321 | reboot_after_options: A list of valid RebootAfter string enums. |
| 1322 | motd: Server's message of the day. |
| 1323 | status_dictionary: A mapping from one word job status names to a more |
| 1324 | informative description. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1325 | """ |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1326 | |
| 1327 | job_fields = models.Job.get_field_dict() |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1328 | default_drone_set_name = models.DroneSet.default_drone_set_name() |
| 1329 | drone_sets = ([default_drone_set_name] + |
| 1330 | sorted(drone_set.name for drone_set in |
| 1331 | models.DroneSet.objects.exclude( |
| 1332 | name=default_drone_set_name))) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1333 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1334 | result = {} |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 1335 | result['priorities'] = priorities.Priority.choices() |
| 1336 | default_priority = priorities.Priority.DEFAULT |
| 1337 | result['default_priority'] = 'Default' |
| 1338 | result['max_schedulable_priority'] = priorities.Priority.DEFAULT |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1339 | result['users'] = get_users(sort_by=['login']) |
Jiaxi Luo | 3187459 | 2014-06-11 10:36:35 -0700 | [diff] [blame] | 1340 | |
| 1341 | label_exclude_filters = [{'name__startswith': 'cros-version'}, |
| 1342 | {'name__startswith': 'fw-version'}] |
| 1343 | result['labels'] = get_labels( |
| 1344 | label_exclude_filters, |
| 1345 | sort_by=['-platform', 'name']) |
| 1346 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 1347 | result['atomic_groups'] = get_atomic_groups(sort_by=['name']) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1348 | result['tests'] = get_tests(sort_by=['name']) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 1349 | result['profilers'] = get_profilers(sort_by=['name']) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 1350 | result['current_user'] = rpc_utils.prepare_for_serialization( |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1351 | models.User.current_user().get_object_dict()) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 1352 | result['host_statuses'] = sorted(models.Host.Status.names) |
mbligh | 5a198b9 | 2008-12-11 19:33:29 +0000 | [diff] [blame] | 1353 | result['job_statuses'] = sorted(models.HostQueueEntry.Status.names) |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 1354 | result['job_timeout_mins_default'] = models.Job.DEFAULT_TIMEOUT_MINS |
Simran Basi | 3421702 | 2012-11-06 13:43:15 -0800 | [diff] [blame] | 1355 | result['job_max_runtime_mins_default'] = ( |
| 1356 | models.Job.DEFAULT_MAX_RUNTIME_MINS) |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1357 | result['parse_failed_repair_default'] = bool( |
| 1358 | models.Job.DEFAULT_PARSE_FAILED_REPAIR) |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 1359 | result['reboot_before_options'] = model_attributes.RebootBefore.names |
| 1360 | result['reboot_after_options'] = model_attributes.RebootAfter.names |
showard | 8fbae65 | 2009-01-20 23:23:10 +0000 | [diff] [blame] | 1361 | result['motd'] = rpc_utils.get_motd() |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1362 | result['drone_sets_enabled'] = models.DroneSet.drone_sets_enabled() |
| 1363 | result['drone_sets'] = drone_sets |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 1364 | result['parameterized_jobs'] = models.Job.parameterized_jobs_enabled() |
showard | 8ac29b4 | 2008-07-17 17:01:55 +0000 | [diff] [blame] | 1365 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1366 | result['status_dictionary'] = {"Aborted": "Aborted", |
showard | 8ac29b4 | 2008-07-17 17:01:55 +0000 | [diff] [blame] | 1367 | "Verifying": "Verifying Host", |
Alex Miller | dfff2fd | 2013-05-28 13:05:06 -0700 | [diff] [blame] | 1368 | "Provisioning": "Provisioning Host", |
showard | 8ac29b4 | 2008-07-17 17:01:55 +0000 | [diff] [blame] | 1369 | "Pending": "Waiting on other hosts", |
| 1370 | "Running": "Running autoserv", |
| 1371 | "Completed": "Autoserv completed", |
| 1372 | "Failed": "Failed to complete", |
showard | d823b36 | 2008-07-24 16:35:46 +0000 | [diff] [blame] | 1373 | "Queued": "Queued", |
showard | 5deb677 | 2008-11-04 21:54:33 +0000 | [diff] [blame] | 1374 | "Starting": "Next in host's queue", |
| 1375 | "Stopped": "Other host(s) failed verify", |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1376 | "Parsing": "Awaiting parse of final results", |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1377 | "Gathering": "Gathering log files", |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1378 | "Template": "Template job for recurring run", |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1379 | "Waiting": "Waiting for scheduler action", |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 1380 | "Archiving": "Archiving results", |
| 1381 | "Resetting": "Resetting hosts"} |
Jiaxi Luo | 421608e | 2014-07-07 14:38:00 -0700 | [diff] [blame] | 1382 | |
| 1383 | result['wmatrix_url'] = rpc_utils.get_wmatrix_url() |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 1384 | result['is_moblab'] = bool(utils.is_moblab()) |
Jiaxi Luo | 421608e | 2014-07-07 14:38:00 -0700 | [diff] [blame] | 1385 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1386 | return result |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1387 | |
| 1388 | |
| 1389 | def get_server_time(): |
| 1390 | return datetime.datetime.now().strftime("%Y-%m-%d %H:%M") |