mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1 | """\ |
| 2 | Functions to expose over the RPC interface. |
| 3 | |
| 4 | For all modify* and delete* functions that ask for an 'id' parameter to |
| 5 | identify the object to operate on, the id may be either |
| 6 | * the database row ID |
| 7 | * the name of the object (label name, hostname, user login, etc.) |
| 8 | * a dictionary containing uniquely identifying field (this option should seldom |
| 9 | be used) |
| 10 | |
| 11 | When specifying foreign key fields (i.e. adding hosts to a label, or adding |
| 12 | users to an ACL group), the given value may be either the database row ID or the |
| 13 | name of the object. |
| 14 | |
| 15 | All get* functions return lists of dictionaries. Each dictionary represents one |
| 16 | object and maps field names to values. |
| 17 | |
| 18 | Some examples: |
| 19 | modify_host(2, hostname='myhost') # modify hostname of host with database ID 2 |
| 20 | modify_host('ipaj2', hostname='myhost') # modify hostname of host 'ipaj2' |
| 21 | modify_test('sleeptest', test_type='Client', params=', seconds=60') |
| 22 | delete_acl_group(1) # delete by ID |
| 23 | delete_acl_group('Everyone') # delete by name |
| 24 | acl_group_add_users('Everyone', ['mbligh', 'showard']) |
| 25 | get_jobs(owner='showard', status='Queued') |
| 26 | |
mbligh | 93c80e6 | 2009-02-03 17:48:30 +0000 | [diff] [blame] | 27 | See doctests/001_rpc_test.txt for (lots) more examples. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 28 | """ |
| 29 | |
| 30 | __author__ = 'showard@google.com (Steve Howard)' |
| 31 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 32 | import datetime |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 33 | import common |
| 34 | from autotest_lib.frontend import thread_local |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 35 | from autotest_lib.frontend.afe import models, model_logic |
| 36 | from autotest_lib.frontend.afe import control_file, rpc_utils |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 37 | from autotest_lib.client.common_lib import global_config |
| 38 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 39 | |
| 40 | # labels |
| 41 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 42 | def add_label(name, kernel_config=None, platform=None, only_if_needed=None): |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 43 | return models.Label.add_object( |
| 44 | name=name, kernel_config=kernel_config, platform=platform, |
| 45 | only_if_needed=only_if_needed).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def modify_label(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 49 | models.Label.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def delete_label(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 53 | models.Label.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 54 | |
| 55 | |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 56 | def label_add_hosts(id, hosts): |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 57 | host_objs = models.Host.smart_get_bulk(hosts) |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 58 | label = models.Label.smart_get(id) |
| 59 | if label.platform: |
| 60 | models.Host.check_no_platform(host_objs) |
| 61 | label.host_set.add(*host_objs) |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | def label_remove_hosts(id, hosts): |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 65 | host_objs = models.Host.smart_get_bulk(hosts) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 66 | models.Label.smart_get(id).host_set.remove(*host_objs) |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 67 | |
| 68 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 69 | def get_labels(**filter_data): |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 70 | """\ |
| 71 | @returns A sequence of nested dictionaries of label information. |
| 72 | """ |
| 73 | return rpc_utils.prepare_rows_as_nested_dicts( |
| 74 | models.Label.query_objects(filter_data), |
| 75 | ('atomic_group',)) |
| 76 | |
| 77 | |
| 78 | # atomic groups |
| 79 | |
showard | e9450c9 | 2009-06-30 01:58:52 +0000 | [diff] [blame] | 80 | def add_atomic_group(name, max_number_of_machines=None, description=None): |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 81 | return models.AtomicGroup.add_object( |
| 82 | name=name, max_number_of_machines=max_number_of_machines, |
| 83 | description=description).id |
| 84 | |
| 85 | |
| 86 | def modify_atomic_group(id, **data): |
| 87 | models.AtomicGroup.smart_get(id).update_object(data) |
| 88 | |
| 89 | |
| 90 | def delete_atomic_group(id): |
| 91 | models.AtomicGroup.smart_get(id).delete() |
| 92 | |
| 93 | |
| 94 | def atomic_group_add_labels(id, labels): |
| 95 | label_objs = models.Label.smart_get_bulk(labels) |
| 96 | models.AtomicGroup.smart_get(id).label_set.add(*label_objs) |
| 97 | |
| 98 | |
| 99 | def atomic_group_remove_labels(id, labels): |
| 100 | label_objs = models.Label.smart_get_bulk(labels) |
| 101 | models.AtomicGroup.smart_get(id).label_set.remove(*label_objs) |
| 102 | |
| 103 | |
| 104 | def get_atomic_groups(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 105 | return rpc_utils.prepare_for_serialization( |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 106 | models.AtomicGroup.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | # hosts |
| 110 | |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 111 | def add_host(hostname, status=None, locked=None, protection=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 112 | return models.Host.add_object(hostname=hostname, status=status, |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 113 | locked=locked, protection=protection).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def modify_host(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 117 | models.Host.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 118 | |
| 119 | |
showard | 276f944 | 2009-05-20 00:33:16 +0000 | [diff] [blame] | 120 | def modify_hosts(host_filter_data, update_data): |
| 121 | """ |
| 122 | @param host_filter_data filters out which hosts to modify |
| 123 | @param update_data dictionary with the changes to make to the hosts |
| 124 | """ |
| 125 | hosts = models.Host.query_objects(host_filter_data) |
| 126 | for host in hosts: |
| 127 | host.update_object(update_data) |
| 128 | |
| 129 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 130 | def host_add_labels(id, labels): |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 131 | labels = models.Label.smart_get_bulk(labels) |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 132 | host = models.Host.smart_get(id) |
| 133 | |
| 134 | platforms = [label.name for label in labels if label.platform] |
| 135 | if len(platforms) > 1: |
| 136 | raise model_logic.ValidationError( |
| 137 | {'labels': 'Adding more than one platform label: %s' % |
| 138 | ', '.join(platforms)}) |
| 139 | if len(platforms) == 1: |
| 140 | models.Host.check_no_platform([host]) |
| 141 | host.labels.add(*labels) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | def host_remove_labels(id, labels): |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 145 | labels = models.Label.smart_get_bulk(labels) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 146 | models.Host.smart_get(id).labels.remove(*labels) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 147 | |
| 148 | |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 149 | def set_host_attribute(attribute, value, **host_filter_data): |
| 150 | """ |
| 151 | @param attribute string name of attribute |
| 152 | @param value string, or None to delete an attribute |
| 153 | @param host_filter_data filter data to apply to Hosts to choose hosts to act |
| 154 | upon |
| 155 | """ |
| 156 | assert host_filter_data # disallow accidental actions on all hosts |
| 157 | hosts = models.Host.query_objects(host_filter_data) |
| 158 | models.AclGroup.check_for_acl_violation_hosts(hosts) |
| 159 | |
| 160 | for host in hosts: |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 161 | host.set_or_delete_attribute(attribute, value) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 162 | |
| 163 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 164 | def delete_host(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 165 | models.Host.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 166 | |
| 167 | |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 168 | def get_hosts(multiple_labels=[], exclude_only_if_needed_labels=False, |
| 169 | **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 170 | """\ |
| 171 | multiple_labels: match hosts in all of the labels given. Should be a |
| 172 | list of label names. |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 173 | exclude_only_if_needed_labels: exclude hosts with at least one |
| 174 | "only_if_needed" label applied. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 175 | """ |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 176 | hosts = rpc_utils.get_host_query(multiple_labels, |
| 177 | exclude_only_if_needed_labels, |
| 178 | filter_data) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 179 | hosts = list(hosts) |
| 180 | models.Host.objects.populate_relationships(hosts, models.Label, |
| 181 | 'label_list') |
| 182 | models.Host.objects.populate_relationships(hosts, models.AclGroup, |
| 183 | 'acl_list') |
| 184 | models.Host.objects.populate_relationships(hosts, models.HostAttribute, |
| 185 | 'attribute_list') |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 186 | host_dicts = [] |
| 187 | for host_obj in hosts: |
| 188 | host_dict = host_obj.get_object_dict() |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 189 | host_dict['labels'] = [label.name for label in host_obj.label_list] |
| 190 | host_dict['platform'] = rpc_utils.find_platform(host_obj) |
| 191 | host_dict['acls'] = [acl.name for acl in host_obj.acl_list] |
| 192 | host_dict['attributes'] = dict((attribute.attribute, attribute.value) |
| 193 | for attribute in host_obj.attribute_list) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 194 | host_dicts.append(host_dict) |
| 195 | return rpc_utils.prepare_for_serialization(host_dicts) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 196 | |
| 197 | |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 198 | def get_num_hosts(multiple_labels=[], exclude_only_if_needed_labels=False, |
| 199 | **filter_data): |
| 200 | hosts = rpc_utils.get_host_query(multiple_labels, |
| 201 | exclude_only_if_needed_labels, |
| 202 | filter_data) |
| 203 | return hosts.count() |
showard | 1385b16 | 2008-03-13 15:59:40 +0000 | [diff] [blame] | 204 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 205 | |
| 206 | # tests |
| 207 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 208 | def add_test(name, test_type, path, author=None, dependencies=None, |
showard | 3d9899a | 2008-07-31 02:11:58 +0000 | [diff] [blame] | 209 | experimental=True, run_verify=None, test_class=None, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 210 | test_time=None, test_category=None, description=None, |
| 211 | sync_count=1): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 212 | return models.Test.add_object(name=name, test_type=test_type, path=path, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 213 | author=author, dependencies=dependencies, |
| 214 | experimental=experimental, |
| 215 | run_verify=run_verify, test_time=test_time, |
| 216 | test_category=test_category, |
| 217 | sync_count=sync_count, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 218 | test_class=test_class, |
| 219 | description=description).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 220 | |
| 221 | |
| 222 | def modify_test(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 223 | models.Test.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 224 | |
| 225 | |
| 226 | def delete_test(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 227 | models.Test.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | def get_tests(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 231 | return rpc_utils.prepare_for_serialization( |
| 232 | models.Test.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 233 | |
| 234 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 235 | # profilers |
| 236 | |
| 237 | def add_profiler(name, description=None): |
| 238 | return models.Profiler.add_object(name=name, description=description).id |
| 239 | |
| 240 | |
| 241 | def modify_profiler(id, **data): |
| 242 | models.Profiler.smart_get(id).update_object(data) |
| 243 | |
| 244 | |
| 245 | def delete_profiler(id): |
| 246 | models.Profiler.smart_get(id).delete() |
| 247 | |
| 248 | |
| 249 | def get_profilers(**filter_data): |
| 250 | return rpc_utils.prepare_for_serialization( |
| 251 | models.Profiler.list_objects(filter_data)) |
| 252 | |
| 253 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 254 | # users |
| 255 | |
| 256 | def add_user(login, access_level=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 257 | return models.User.add_object(login=login, access_level=access_level).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 258 | |
| 259 | |
| 260 | def modify_user(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 261 | models.User.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 262 | |
| 263 | |
| 264 | def delete_user(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 265 | models.User.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 266 | |
| 267 | |
| 268 | def get_users(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 269 | return rpc_utils.prepare_for_serialization( |
| 270 | models.User.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | # acl groups |
| 274 | |
| 275 | def add_acl_group(name, description=None): |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 276 | group = models.AclGroup.add_object(name=name, description=description) |
| 277 | group.users.add(thread_local.get_user()) |
| 278 | return group.id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 279 | |
| 280 | |
| 281 | def modify_acl_group(id, **data): |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 282 | group = models.AclGroup.smart_get(id) |
| 283 | group.check_for_acl_violation_acl_group() |
| 284 | group.update_object(data) |
| 285 | group.add_current_user_if_empty() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | def acl_group_add_users(id, users): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 289 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 290 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 291 | users = models.User.smart_get_bulk(users) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 292 | group.users.add(*users) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 293 | |
| 294 | |
| 295 | def acl_group_remove_users(id, users): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 296 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 297 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 298 | users = models.User.smart_get_bulk(users) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 299 | group.users.remove(*users) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 300 | group.add_current_user_if_empty() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 301 | |
| 302 | |
| 303 | def acl_group_add_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 304 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 305 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 306 | hosts = models.Host.smart_get_bulk(hosts) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 307 | group.hosts.add(*hosts) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 308 | group.on_host_membership_change() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 309 | |
| 310 | |
| 311 | def acl_group_remove_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 312 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame] | 313 | group.check_for_acl_violation_acl_group() |
showard | be3ec04 | 2008-11-12 18:16:07 +0000 | [diff] [blame] | 314 | hosts = models.Host.smart_get_bulk(hosts) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 315 | group.hosts.remove(*hosts) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 316 | group.on_host_membership_change() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | def delete_acl_group(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 320 | models.AclGroup.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 321 | |
| 322 | |
| 323 | def get_acl_groups(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 324 | acl_groups = models.AclGroup.list_objects(filter_data) |
| 325 | for acl_group in acl_groups: |
| 326 | acl_group_obj = models.AclGroup.objects.get(id=acl_group['id']) |
| 327 | acl_group['users'] = [user.login |
| 328 | for user in acl_group_obj.users.all()] |
| 329 | acl_group['hosts'] = [host.hostname |
| 330 | for host in acl_group_obj.hosts.all()] |
| 331 | return rpc_utils.prepare_for_serialization(acl_groups) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 332 | |
| 333 | |
| 334 | # jobs |
| 335 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 336 | def generate_control_file(tests=(), kernel=None, label=None, profilers=(), |
| 337 | client_control_file='', use_container=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 338 | """ |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 339 | Generates a client-side control file to load a kernel and run tests. |
| 340 | |
| 341 | @param tests List of tests to run. |
| 342 | @param kernel Kernel to install in generated control file. |
| 343 | @param label Name of label to grab kernel config from. |
| 344 | @param profilers List of profilers to activate during the job. |
| 345 | @param client_control_file The contents of a client-side control file to |
| 346 | run at the end of all tests. If this is supplied, all tests must be |
| 347 | client side. |
| 348 | TODO: in the future we should support server control files directly |
| 349 | to wrap with a kernel. That'll require changing the parameter |
| 350 | name and adding a boolean to indicate if it is a client or server |
| 351 | control file. |
| 352 | @param use_container unused argument today. TODO: Enable containers |
| 353 | on the host during a client side test. |
| 354 | |
| 355 | @returns a dict with the following keys: |
| 356 | control_file: str, The control file text. |
| 357 | is_server: bool, is the control file a server-side control file? |
| 358 | synch_count: How many machines the job uses per autoserv execution. |
| 359 | synch_count == 1 means the job is asynchronous. |
| 360 | dependencies: A list of the names of labels on which the job depends. |
| 361 | """ |
showard | d86debe | 2009-06-10 17:37:56 +0000 | [diff] [blame] | 362 | if not tests and not client_control_file: |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 363 | return dict(control_file='', is_server=False, synch_count=1, |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 364 | dependencies=[]) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 365 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 366 | cf_info, test_objects, profiler_objects, label = ( |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 367 | rpc_utils.prepare_generate_control_file(tests, kernel, label, |
| 368 | profilers)) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 369 | cf_info['control_file'] = control_file.generate_control( |
| 370 | tests=test_objects, kernel=kernel, platform=label, |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 371 | profilers=profiler_objects, is_server=cf_info['is_server'], |
| 372 | client_control_file=client_control_file) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 373 | return cf_info |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 374 | |
| 375 | |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 376 | def create_job(name, priority, control_file, control_type, |
| 377 | hosts=(), meta_hosts=(), one_time_hosts=(), |
| 378 | atomic_group_name=None, synch_count=None, is_template=False, |
| 379 | timeout=None, max_runtime_hrs=None, run_verify=True, |
| 380 | email_list='', dependencies=(), reboot_before=None, |
| 381 | reboot_after=None, parse_failed_repair=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 382 | """\ |
| 383 | Create and enqueue a job. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 384 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 385 | @param name name of this job |
| 386 | @param priority Low, Medium, High, Urgent |
| 387 | @param control_file String contents of the control file. |
| 388 | @param control_type Type of control file, Client or Server. |
| 389 | @param synch_count How many machines the job uses per autoserv execution. |
| 390 | synch_count == 1 means the job is asynchronous. If an atomic group is |
| 391 | given this value is treated as a minimum. |
| 392 | @param is_template If true then create a template job. |
| 393 | @param timeout Hours after this call returns until the job times out. |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 394 | @param max_runtime_hrs Hours from job starting time until job times out |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 395 | @param run_verify Should the host be verified before running the test? |
| 396 | @param email_list String containing emails to mail when the job is done |
| 397 | @param dependencies List of label names on which this job depends |
| 398 | @param reboot_before Never, If dirty, or Always |
| 399 | @param reboot_after Never, If all tests passed, or Always |
| 400 | @param parse_failed_repair if true, results of failed repairs launched by |
| 401 | this job will be parsed as part of the job. |
| 402 | |
| 403 | @param hosts List of hosts to run job on. |
| 404 | @param meta_hosts List where each entry is a label name, and for each entry |
| 405 | one host will be chosen from that label to run the job on. |
| 406 | @param one_time_hosts List of hosts not in the database to run the job on. |
| 407 | @param atomic_group_name The name of an atomic group to schedule the job on. |
| 408 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 409 | |
| 410 | @returns The created Job id number. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 411 | """ |
showard | 8765816 | 2009-05-29 18:39:50 +0000 | [diff] [blame] | 412 | user = thread_local.get_user() |
| 413 | owner = user.login |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 414 | # input validation |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 415 | if not (hosts or meta_hosts or one_time_hosts or atomic_group_name): |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 416 | raise model_logic.ValidationError({ |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 417 | 'arguments' : "You must pass at least one of 'hosts', " |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 418 | "'meta_hosts', 'one_time_hosts', " |
| 419 | "or 'atomic_group_name'" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 420 | }) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 421 | |
showard | bc93f0f | 2009-06-10 00:16:21 +0000 | [diff] [blame] | 422 | labels_by_name = dict((label.name, label) |
| 423 | for label in models.Label.objects.all()) |
| 424 | atomic_groups_by_name = dict((ag.name, ag) |
| 425 | for ag in models.AtomicGroup.objects.all()) |
| 426 | |
showard | c873032 | 2009-06-30 01:56:38 +0000 | [diff] [blame] | 427 | # Schedule on an atomic group automagically if one of the labels given |
| 428 | # is an atomic group label and no explicit atomic_group_name was supplied. |
| 429 | if not atomic_group_name: |
| 430 | for label_name in meta_hosts or []: |
| 431 | label = labels_by_name.get(label_name) |
| 432 | if label and label.atomic_group: |
| 433 | atomic_group_name = label.atomic_group.name |
| 434 | break |
| 435 | |
showard | bc93f0f | 2009-06-10 00:16:21 +0000 | [diff] [blame] | 436 | # convert hostnames & meta hosts to host/label objects |
| 437 | host_objects = models.Host.smart_get_bulk(hosts) |
| 438 | metahost_objects = [] |
showard | c873032 | 2009-06-30 01:56:38 +0000 | [diff] [blame] | 439 | for label_name in meta_hosts or []: |
| 440 | if label_name in labels_by_name: |
| 441 | label = labels_by_name[label_name] |
| 442 | metahost_objects.append(label) |
| 443 | elif label_name in atomic_groups_by_name: |
| 444 | # If given a metahost name that isn't a Label, check to |
| 445 | # see if the user was specifying an Atomic Group instead. |
| 446 | atomic_group = atomic_groups_by_name[label_name] |
showard | bc93f0f | 2009-06-10 00:16:21 +0000 | [diff] [blame] | 447 | if atomic_group_name and atomic_group_name != atomic_group.name: |
| 448 | raise model_logic.ValidationError({ |
| 449 | 'meta_hosts': ( |
| 450 | 'Label "%s" not found. If assumed to be an ' |
| 451 | 'atomic group it would conflict with the ' |
| 452 | 'supplied atomic group "%s".' % ( |
showard | c873032 | 2009-06-30 01:56:38 +0000 | [diff] [blame] | 453 | label_name, atomic_group_name))}) |
showard | bc93f0f | 2009-06-10 00:16:21 +0000 | [diff] [blame] | 454 | atomic_group_name = atomic_group.name |
| 455 | else: |
| 456 | raise model_logic.ValidationError( |
| 457 | {'meta_hosts' : 'Label "%s" not found' % label}) |
| 458 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 459 | # Create and sanity check an AtomicGroup object if requested. |
| 460 | if atomic_group_name: |
| 461 | if one_time_hosts: |
| 462 | raise model_logic.ValidationError( |
| 463 | {'one_time_hosts': |
| 464 | 'One time hosts cannot be used with an Atomic Group.'}) |
| 465 | atomic_group = models.AtomicGroup.smart_get(atomic_group_name) |
| 466 | if synch_count and synch_count > atomic_group.max_number_of_machines: |
| 467 | raise model_logic.ValidationError( |
| 468 | {'atomic_group_name' : |
| 469 | 'You have requested a synch_count (%d) greater than the ' |
| 470 | 'maximum machines in the requested Atomic Group (%d).' % |
| 471 | (synch_count, atomic_group.max_number_of_machines)}) |
| 472 | else: |
| 473 | atomic_group = None |
| 474 | |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 475 | for host in one_time_hosts or []: |
| 476 | this_host = models.Host.create_one_time_host(host) |
| 477 | host_objects.append(this_host) |
showard | ba87290 | 2008-06-28 00:51:08 +0000 | [diff] [blame] | 478 | |
showard | 8765816 | 2009-05-29 18:39:50 +0000 | [diff] [blame] | 479 | if reboot_before is None: |
| 480 | reboot_before = user.get_reboot_before_display() |
| 481 | if reboot_after is None: |
| 482 | reboot_after = user.get_reboot_after_display() |
| 483 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 484 | options = dict(name=name, |
| 485 | priority=priority, |
| 486 | control_file=control_file, |
| 487 | control_type=control_type, |
| 488 | is_template=is_template, |
| 489 | timeout=timeout, |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 490 | max_runtime_hrs=max_runtime_hrs, |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 491 | synch_count=synch_count, |
| 492 | run_verify=run_verify, |
| 493 | email_list=email_list, |
| 494 | dependencies=dependencies, |
| 495 | reboot_before=reboot_before, |
| 496 | reboot_after=reboot_after, |
| 497 | parse_failed_repair=parse_failed_repair) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 498 | return rpc_utils.create_new_job(owner=owner, |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 499 | options=options, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 500 | host_objects=host_objects, |
| 501 | metahost_objects=metahost_objects, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 502 | atomic_group=atomic_group) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 503 | |
| 504 | |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 505 | def abort_host_queue_entries(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 506 | """\ |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 507 | Abort a set of host queue entries. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 508 | """ |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 509 | query = models.HostQueueEntry.query_objects(filter_data) |
showard | 0c18519 | 2009-01-16 03:07:57 +0000 | [diff] [blame] | 510 | query = query.filter(complete=False) |
showard | dc81751 | 2008-11-12 18:16:41 +0000 | [diff] [blame] | 511 | models.AclGroup.check_abort_permissions(query) |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 512 | host_queue_entries = list(query.select_related()) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 513 | rpc_utils.check_abort_synchronous_jobs(host_queue_entries) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 514 | |
showard | 9dbdcda | 2008-10-14 17:34:36 +0000 | [diff] [blame] | 515 | user = thread_local.get_user() |
| 516 | for queue_entry in host_queue_entries: |
| 517 | queue_entry.abort(user) |
showard | 9d821ab | 2008-07-11 16:54:29 +0000 | [diff] [blame] | 518 | |
| 519 | |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 520 | def reverify_hosts(**filter_data): |
| 521 | """\ |
| 522 | Schedules a set of hosts for verify. |
| 523 | """ |
| 524 | hosts = models.Host.query_objects(filter_data) |
| 525 | models.AclGroup.check_for_acl_violation_hosts(hosts) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 526 | models.SpecialTask.schedule_special_task(hosts, |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 527 | models.SpecialTask.Task.VERIFY) |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 528 | |
| 529 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 530 | def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 531 | """\ |
| 532 | Extra filter args for get_jobs: |
| 533 | -not_yet_run: Include only jobs that have not yet started running. |
| 534 | -running: Include only jobs that have start running but for which not |
| 535 | all hosts have completed. |
| 536 | -finished: Include only jobs for which all hosts have completed (or |
| 537 | aborted). |
| 538 | At most one of these three fields should be specified. |
| 539 | """ |
| 540 | filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run, |
| 541 | running, |
| 542 | finished) |
showard | 0957a84 | 2009-05-11 19:25:08 +0000 | [diff] [blame] | 543 | job_dicts = [] |
| 544 | jobs = list(models.Job.query_objects(filter_data)) |
| 545 | models.Job.objects.populate_relationships(jobs, models.Label, |
| 546 | 'dependencies') |
| 547 | for job in jobs: |
| 548 | job_dict = job.get_object_dict() |
| 549 | job_dict['dependencies'] = ','.join(label.name |
| 550 | for label in job.dependencies) |
| 551 | job_dicts.append(job_dict) |
| 552 | return rpc_utils.prepare_for_serialization(job_dicts) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 553 | |
| 554 | |
| 555 | def get_num_jobs(not_yet_run=False, running=False, finished=False, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 556 | **filter_data): |
| 557 | """\ |
| 558 | See get_jobs() for documentation of extra filter parameters. |
| 559 | """ |
| 560 | filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run, |
| 561 | running, |
| 562 | finished) |
| 563 | return models.Job.query_count(filter_data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 564 | |
| 565 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 566 | def get_jobs_summary(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 567 | """\ |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 568 | Like get_jobs(), but adds a 'status_counts' field, which is a dictionary |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 569 | mapping status strings to the number of hosts currently with that |
| 570 | status, i.e. {'Queued' : 4, 'Running' : 2}. |
| 571 | """ |
| 572 | jobs = get_jobs(**filter_data) |
| 573 | ids = [job['id'] for job in jobs] |
| 574 | all_status_counts = models.Job.objects.get_status_counts(ids) |
| 575 | for job in jobs: |
| 576 | job['status_counts'] = all_status_counts[job['id']] |
| 577 | return rpc_utils.prepare_for_serialization(jobs) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 578 | |
| 579 | |
showard | a965cef | 2009-05-15 23:17:41 +0000 | [diff] [blame] | 580 | def get_info_for_clone(id, preserve_metahosts, queue_entry_filter_data=None): |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 581 | """\ |
| 582 | Retrieves all the information needed to clone a job. |
| 583 | """ |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 584 | job = models.Job.objects.get(id=id) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 585 | job_info = rpc_utils.get_job_info(job, |
showard | a965cef | 2009-05-15 23:17:41 +0000 | [diff] [blame] | 586 | preserve_metahosts, |
| 587 | queue_entry_filter_data) |
showard | 945072f | 2008-09-03 20:34:59 +0000 | [diff] [blame] | 588 | |
showard | d9992fe | 2008-07-31 02:15:03 +0000 | [diff] [blame] | 589 | host_dicts = [] |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 590 | for host in job_info['hosts']: |
| 591 | host_dict = get_hosts(id=host.id)[0] |
| 592 | other_labels = host_dict['labels'] |
| 593 | if host_dict['platform']: |
| 594 | other_labels.remove(host_dict['platform']) |
| 595 | host_dict['other_labels'] = ', '.join(other_labels) |
showard | d9992fe | 2008-07-31 02:15:03 +0000 | [diff] [blame] | 596 | host_dicts.append(host_dict) |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 597 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 598 | for host in job_info['one_time_hosts']: |
| 599 | host_dict = dict(hostname=host.hostname, |
| 600 | id=host.id, |
| 601 | platform='(one-time host)', |
| 602 | locked_text='') |
| 603 | host_dicts.append(host_dict) |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 604 | |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 605 | # convert keys from Label objects to strings (names of labels) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 606 | meta_host_counts = dict((meta_host.name, count) for meta_host, count |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 607 | in job_info['meta_host_counts'].iteritems()) |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 608 | |
| 609 | info = dict(job=job.get_object_dict(), |
| 610 | meta_host_counts=meta_host_counts, |
| 611 | hosts=host_dicts) |
| 612 | info['job']['dependencies'] = job_info['dependencies'] |
| 613 | if job_info['atomic_group']: |
| 614 | info['atomic_group_name'] = (job_info['atomic_group']).name |
| 615 | else: |
| 616 | info['atomic_group_name'] = None |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 617 | |
| 618 | return rpc_utils.prepare_for_serialization(info) |
| 619 | |
| 620 | |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 621 | # host queue entries |
| 622 | |
| 623 | def get_host_queue_entries(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 624 | """\ |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 625 | @returns A sequence of nested dictionaries of host and job information. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 626 | """ |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 627 | return rpc_utils.prepare_rows_as_nested_dicts( |
| 628 | models.HostQueueEntry.query_objects(filter_data), |
| 629 | ('host', 'atomic_group', 'job')) |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 630 | |
| 631 | |
| 632 | def get_num_host_queue_entries(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 633 | """\ |
| 634 | Get the number of host queue entries associated with this job. |
| 635 | """ |
| 636 | return models.HostQueueEntry.query_count(filter_data) |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 637 | |
| 638 | |
showard | 1e935f1 | 2008-07-11 00:11:36 +0000 | [diff] [blame] | 639 | def get_hqe_percentage_complete(**filter_data): |
| 640 | """ |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 641 | Computes the fraction of host queue entries matching the given filter data |
showard | 1e935f1 | 2008-07-11 00:11:36 +0000 | [diff] [blame] | 642 | that are complete. |
| 643 | """ |
| 644 | query = models.HostQueueEntry.query_objects(filter_data) |
| 645 | complete_count = query.filter(complete=True).count() |
| 646 | total_count = query.count() |
| 647 | if total_count == 0: |
| 648 | return 1 |
| 649 | return float(complete_count) / total_count |
| 650 | |
| 651 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 652 | # recurring run |
| 653 | |
| 654 | def get_recurring(**filter_data): |
| 655 | return rpc_utils.prepare_rows_as_nested_dicts( |
| 656 | models.RecurringRun.query_objects(filter_data), |
| 657 | ('job', 'owner')) |
| 658 | |
| 659 | |
| 660 | def get_num_recurring(**filter_data): |
| 661 | return models.RecurringRun.query_count(filter_data) |
| 662 | |
| 663 | |
| 664 | def delete_recurring_runs(**filter_data): |
| 665 | to_delete = models.RecurringRun.query_objects(filter_data) |
| 666 | to_delete.delete() |
| 667 | |
| 668 | |
| 669 | def create_recurring_run(job_id, start_date, loop_period, loop_count): |
| 670 | owner = thread_local.get_user().login |
| 671 | job = models.Job.objects.get(id=job_id) |
| 672 | return job.create_recurring_job(start_date=start_date, |
| 673 | loop_period=loop_period, |
| 674 | loop_count=loop_count, |
| 675 | owner=owner) |
| 676 | |
| 677 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 678 | # other |
| 679 | |
showard | e0b6362 | 2008-08-04 20:58:47 +0000 | [diff] [blame] | 680 | def echo(data=""): |
| 681 | """\ |
| 682 | Returns a passed in string. For doing a basic test to see if RPC calls |
| 683 | can successfully be made. |
| 684 | """ |
| 685 | return data |
| 686 | |
| 687 | |
showard | b7a52fd | 2009-04-27 20:10:56 +0000 | [diff] [blame] | 688 | def get_motd(): |
| 689 | """\ |
| 690 | Returns the message of the day as a string. |
| 691 | """ |
| 692 | return rpc_utils.get_motd() |
| 693 | |
| 694 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 695 | def get_static_data(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 696 | """\ |
| 697 | Returns a dictionary containing a bunch of data that shouldn't change |
| 698 | often and is otherwise inaccessible. This includes: |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 699 | |
| 700 | priorities: List of job priority choices. |
| 701 | default_priority: Default priority value for new jobs. |
| 702 | users: Sorted list of all users. |
| 703 | labels: Sorted list of all labels. |
| 704 | atomic_groups: Sorted list of all atomic groups. |
| 705 | tests: Sorted list of all tests. |
| 706 | profilers: Sorted list of all profilers. |
| 707 | current_user: Logged-in username. |
| 708 | host_statuses: Sorted list of possible Host statuses. |
| 709 | job_statuses: Sorted list of possible HostQueueEntry statuses. |
| 710 | job_timeout_default: The default job timeout length in hours. |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 711 | parse_failed_repair_default: Default value for the parse_failed_repair job |
| 712 | option. |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 713 | reboot_before_options: A list of valid RebootBefore string enums. |
| 714 | reboot_after_options: A list of valid RebootAfter string enums. |
| 715 | motd: Server's message of the day. |
| 716 | status_dictionary: A mapping from one word job status names to a more |
| 717 | informative description. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 718 | """ |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 719 | |
| 720 | job_fields = models.Job.get_field_dict() |
| 721 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 722 | result = {} |
| 723 | result['priorities'] = models.Job.Priority.choices() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 724 | default_priority = job_fields['priority'].default |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 725 | default_string = models.Job.Priority.get_string(default_priority) |
| 726 | result['default_priority'] = default_string |
| 727 | result['users'] = get_users(sort_by=['login']) |
| 728 | result['labels'] = get_labels(sort_by=['-platform', 'name']) |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 729 | result['atomic_groups'] = get_atomic_groups(sort_by=['name']) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 730 | result['tests'] = get_tests(sort_by=['name']) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 731 | result['profilers'] = get_profilers(sort_by=['name']) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 732 | result['current_user'] = rpc_utils.prepare_for_serialization( |
| 733 | thread_local.get_user().get_object_dict()) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 734 | result['host_statuses'] = sorted(models.Host.Status.names) |
mbligh | 5a198b9 | 2008-12-11 19:33:29 +0000 | [diff] [blame] | 735 | result['job_statuses'] = sorted(models.HostQueueEntry.Status.names) |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 736 | result['job_timeout_default'] = models.Job.DEFAULT_TIMEOUT |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 737 | result['job_max_runtime_hrs_default'] = models.Job.DEFAULT_MAX_RUNTIME_HRS |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 738 | result['parse_failed_repair_default'] = bool( |
| 739 | models.Job.DEFAULT_PARSE_FAILED_REPAIR) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 740 | result['reboot_before_options'] = models.RebootBefore.names |
| 741 | result['reboot_after_options'] = models.RebootAfter.names |
showard | 8fbae65 | 2009-01-20 23:23:10 +0000 | [diff] [blame] | 742 | result['motd'] = rpc_utils.get_motd() |
showard | 8ac29b4 | 2008-07-17 17:01:55 +0000 | [diff] [blame] | 743 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 744 | result['status_dictionary'] = {"Aborted": "Aborted", |
showard | 8ac29b4 | 2008-07-17 17:01:55 +0000 | [diff] [blame] | 745 | "Verifying": "Verifying Host", |
| 746 | "Pending": "Waiting on other hosts", |
| 747 | "Running": "Running autoserv", |
| 748 | "Completed": "Autoserv completed", |
| 749 | "Failed": "Failed to complete", |
showard | d823b36 | 2008-07-24 16:35:46 +0000 | [diff] [blame] | 750 | "Queued": "Queued", |
showard | 5deb677 | 2008-11-04 21:54:33 +0000 | [diff] [blame] | 751 | "Starting": "Next in host's queue", |
| 752 | "Stopped": "Other host(s) failed verify", |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 753 | "Parsing": "Awaiting parse of final results", |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 754 | "Gathering": "Gathering log files", |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 755 | "Template": "Template job for recurring run"} |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 756 | return result |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 757 | |
| 758 | |
| 759 | def get_server_time(): |
| 760 | return datetime.datetime.now().strftime("%Y-%m-%d %H:%M") |