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 | |
| 27 | See doctests/rpc_test.txt for (lots) more examples. |
| 28 | """ |
| 29 | |
| 30 | __author__ = 'showard@google.com (Steve Howard)' |
| 31 | |
showard | ff90138 | 2008-07-07 23:22:16 +0000 | [diff] [blame] | 32 | from frontend import thread_local |
showard | 09096d8 | 2008-07-07 23:20:49 +0000 | [diff] [blame] | 33 | from frontend.afe import models, model_logic, control_file, rpc_utils |
| 34 | from frontend.afe import readonly_connection |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 35 | from autotest_lib.client.common_lib import global_config |
| 36 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 37 | |
| 38 | # labels |
| 39 | |
| 40 | def add_label(name, kernel_config=None, platform=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 41 | return models.Label.add_object(name=name, kernel_config=kernel_config, |
| 42 | platform=platform).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def modify_label(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 46 | models.Label.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def delete_label(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 50 | models.Label.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 51 | |
| 52 | |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 53 | def label_add_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 54 | host_objs = [models.Host.smart_get(host) for host in hosts] |
| 55 | models.Label.smart_get(id).host_set.add(*host_objs) |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | def label_remove_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 59 | host_objs = [models.Host.smart_get(host) for host in hosts] |
| 60 | models.Label.smart_get(id).host_set.remove(*host_objs) |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame] | 61 | |
| 62 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 63 | def get_labels(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 64 | return rpc_utils.prepare_for_serialization( |
| 65 | models.Label.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | # hosts |
| 69 | |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 70 | def add_host(hostname, status=None, locked=None, protection=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 71 | return models.Host.add_object(hostname=hostname, status=status, |
showard | df06256 | 2008-07-03 19:56:37 +0000 | [diff] [blame] | 72 | locked=locked, protection=protection).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def modify_host(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 76 | models.Host.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def host_add_labels(id, labels): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 80 | labels = [models.Label.smart_get(label) for label in labels] |
| 81 | models.Host.smart_get(id).labels.add(*labels) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | def host_remove_labels(id, labels): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 85 | labels = [models.Label.smart_get(label) for label in labels] |
| 86 | models.Host.smart_get(id).labels.remove(*labels) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def delete_host(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 90 | models.Host.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 91 | |
| 92 | |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 93 | def get_hosts(multiple_labels=[], **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 94 | """\ |
| 95 | multiple_labels: match hosts in all of the labels given. Should be a |
| 96 | list of label names. |
| 97 | """ |
| 98 | filter_data['extra_args'] = ( |
| 99 | rpc_utils.extra_host_filters(multiple_labels)) |
| 100 | hosts = models.Host.list_objects(filter_data) |
| 101 | for host in hosts: |
| 102 | host_obj = models.Host.objects.get(id=host['id']) |
| 103 | host['labels'] = [label.name |
| 104 | for label in host_obj.labels.all()] |
| 105 | platform = host_obj.platform() |
| 106 | host['platform'] = platform and platform.name or None |
| 107 | return rpc_utils.prepare_for_serialization(hosts) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 108 | |
| 109 | |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 110 | def get_num_hosts(multiple_labels=[], **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 111 | filter_data['extra_args'] = ( |
| 112 | rpc_utils.extra_host_filters(multiple_labels)) |
| 113 | return models.Host.query_count(filter_data) |
showard | 1385b16 | 2008-03-13 15:59:40 +0000 | [diff] [blame] | 114 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 115 | |
| 116 | # tests |
| 117 | |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 118 | def add_test(name, test_type, path, author=None, dependencies=None, |
| 119 | experimental=True, run_verify=None, test_class=None, |
| 120 | test_time=None, test_category=None, description=None, |
| 121 | sync_count=1): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 122 | return models.Test.add_object(name=name, test_type=test_type, path=path, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 123 | author=author, dependencies=dependencies, |
| 124 | experimental=experimental, |
| 125 | run_verify=run_verify, test_time=test_time, |
| 126 | test_category=test_category, |
| 127 | sync_count=sync_count, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 128 | test_class=test_class, |
| 129 | description=description).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 130 | |
| 131 | |
| 132 | def modify_test(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 133 | models.Test.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def delete_test(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 137 | models.Test.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | def get_tests(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 141 | return rpc_utils.prepare_for_serialization( |
| 142 | models.Test.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 143 | |
| 144 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 145 | # profilers |
| 146 | |
| 147 | def add_profiler(name, description=None): |
| 148 | return models.Profiler.add_object(name=name, description=description).id |
| 149 | |
| 150 | |
| 151 | def modify_profiler(id, **data): |
| 152 | models.Profiler.smart_get(id).update_object(data) |
| 153 | |
| 154 | |
| 155 | def delete_profiler(id): |
| 156 | models.Profiler.smart_get(id).delete() |
| 157 | |
| 158 | |
| 159 | def get_profilers(**filter_data): |
| 160 | return rpc_utils.prepare_for_serialization( |
| 161 | models.Profiler.list_objects(filter_data)) |
| 162 | |
| 163 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 164 | # users |
| 165 | |
| 166 | def add_user(login, access_level=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 167 | return models.User.add_object(login=login, access_level=access_level).id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 168 | |
| 169 | |
| 170 | def modify_user(id, **data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 171 | models.User.smart_get(id).update_object(data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | def delete_user(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 175 | models.User.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | def get_users(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 179 | return rpc_utils.prepare_for_serialization( |
| 180 | models.User.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | # acl groups |
| 184 | |
| 185 | def add_acl_group(name, description=None): |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame^] | 186 | group = models.AclGroup.add_object(name=name, description=description) |
| 187 | group.users.add(thread_local.get_user()) |
| 188 | return group.id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 189 | |
| 190 | |
| 191 | def modify_acl_group(id, **data): |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame^] | 192 | group = models.AclGroup.smart_get(id) |
| 193 | group.check_for_acl_violation_acl_group() |
| 194 | group.update_object(data) |
| 195 | group.add_current_user_if_empty() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 196 | |
| 197 | |
| 198 | def acl_group_add_users(id, users): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 199 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame^] | 200 | group.check_for_acl_violation_acl_group() |
| 201 | users = [models.User.smart_get(user) for user in users] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 202 | group.users.add(*users) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 203 | |
| 204 | |
| 205 | def acl_group_remove_users(id, users): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 206 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame^] | 207 | group.check_for_acl_violation_acl_group() |
| 208 | users = [models.User.smart_get(user) for user in users] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 209 | group.users.remove(*users) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame^] | 210 | group.add_current_user_if_empty() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def acl_group_add_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 214 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame^] | 215 | group.check_for_acl_violation_acl_group() |
| 216 | hosts = [models.Host.smart_get(host) for host in hosts] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 217 | group.hosts.add(*hosts) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 218 | group.on_host_membership_change() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | def acl_group_remove_hosts(id, hosts): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 222 | group = models.AclGroup.smart_get(id) |
showard | 04f2cd8 | 2008-07-25 20:53:31 +0000 | [diff] [blame^] | 223 | group.check_for_acl_violation_acl_group() |
| 224 | hosts = [models.Host.smart_get(host) for host in hosts] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 225 | group.hosts.remove(*hosts) |
showard | 08f981b | 2008-06-24 21:59:03 +0000 | [diff] [blame] | 226 | group.on_host_membership_change() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 227 | |
| 228 | |
| 229 | def delete_acl_group(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 230 | models.AclGroup.smart_get(id).delete() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | def get_acl_groups(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 234 | acl_groups = models.AclGroup.list_objects(filter_data) |
| 235 | for acl_group in acl_groups: |
| 236 | acl_group_obj = models.AclGroup.objects.get(id=acl_group['id']) |
| 237 | acl_group['users'] = [user.login |
| 238 | for user in acl_group_obj.users.all()] |
| 239 | acl_group['hosts'] = [host.hostname |
| 240 | for host in acl_group_obj.hosts.all()] |
| 241 | return rpc_utils.prepare_for_serialization(acl_groups) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 242 | |
| 243 | |
| 244 | # jobs |
| 245 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 246 | def generate_control_file(tests, kernel=None, label=None, profilers=[]): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 247 | """\ |
| 248 | Generates a client-side control file to load a kernel and run a set of |
| 249 | tests. Returns a tuple (control_file, is_server, is_synchronous): |
| 250 | control_file - the control file text |
| 251 | is_server - is the control file a server-side control file? |
| 252 | is_synchronous - should the control file be run synchronously? |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 253 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 254 | tests: list of tests to run |
| 255 | kernel: kernel to install in generated control file |
| 256 | label: name of label to grab kernel config from |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 257 | profilers: list of profilers to activate during the job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 258 | """ |
| 259 | if not tests: |
| 260 | return '', False, False |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 261 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 262 | is_server, is_synchronous, test_objects, profiler_objects, label = ( |
| 263 | rpc_utils.prepare_generate_control_file(tests, kernel, label, |
| 264 | profilers)) |
| 265 | cf_text = control_file.generate_control(tests=test_objects, kernel=kernel, |
| 266 | platform=label, |
| 267 | profilers=profiler_objects, |
| 268 | is_server=is_server) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 269 | return cf_text, is_server, is_synchronous |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 270 | |
| 271 | |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 272 | def create_job(name, priority, control_file, control_type, timeout=None, |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 273 | is_synchronous=None, hosts=None, meta_hosts=None, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 274 | run_verify=True, one_time_hosts=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 275 | """\ |
| 276 | Create and enqueue a job. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 277 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 278 | priority: Low, Medium, High, Urgent |
| 279 | control_file: contents of control file |
| 280 | control_type: type of control file, Client or Server |
| 281 | is_synchronous: boolean indicating if a job is synchronous |
| 282 | hosts: list of hosts to run job on |
| 283 | meta_hosts: list where each entry is a label name, and for each entry |
| 284 | one host will be chosen from that label to run the job |
| 285 | on. |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 286 | timeout: hours until job times out |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 287 | """ |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 288 | |
| 289 | if timeout is None: |
| 290 | timeout=global_config.global_config.get_config_value( |
| 291 | 'AUTOTEST_WEB', 'job_timeout_default') |
| 292 | |
showard | ff90138 | 2008-07-07 23:22:16 +0000 | [diff] [blame] | 293 | owner = thread_local.get_user().login |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 294 | # input validation |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 295 | if not hosts and not meta_hosts and not one_time_hosts: |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 296 | raise model_logic.ValidationError({ |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 297 | 'arguments' : "You must pass at least one of 'hosts', " |
| 298 | "'meta_hosts', or 'one_time_hosts'" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 299 | }) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 300 | |
showard | ba87290 | 2008-06-28 00:51:08 +0000 | [diff] [blame] | 301 | requested_host_counts = {} |
| 302 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 303 | # convert hostnames & meta hosts to host/label objects |
| 304 | host_objects = [] |
| 305 | for host in hosts or []: |
| 306 | this_host = models.Host.smart_get(host) |
| 307 | host_objects.append(this_host) |
| 308 | for label in meta_hosts or []: |
| 309 | this_label = models.Label.smart_get(label) |
| 310 | host_objects.append(this_label) |
showard | ba87290 | 2008-06-28 00:51:08 +0000 | [diff] [blame] | 311 | requested_host_counts.setdefault(this_label.name, 0) |
| 312 | requested_host_counts[this_label.name] += 1 |
showard | b8471e3 | 2008-07-03 19:51:08 +0000 | [diff] [blame] | 313 | for host in one_time_hosts or []: |
| 314 | this_host = models.Host.create_one_time_host(host) |
| 315 | host_objects.append(this_host) |
showard | ba87290 | 2008-06-28 00:51:08 +0000 | [diff] [blame] | 316 | |
| 317 | # check that each metahost request has enough hosts under the label |
| 318 | if meta_hosts: |
showard | 09096d8 | 2008-07-07 23:20:49 +0000 | [diff] [blame] | 319 | labels = models.Label.objects.filter( |
| 320 | name__in=requested_host_counts.keys()) |
showard | ba87290 | 2008-06-28 00:51:08 +0000 | [diff] [blame] | 321 | for label in labels: |
| 322 | count = label.host_set.count() |
| 323 | if requested_host_counts[label.name] > count: |
| 324 | error = ("You have requested %d %s's, but there are only %d." |
| 325 | % (requested_host_counts[label.name], |
| 326 | label.name, count)) |
| 327 | raise model_logic.ValidationError({'arguments' : error}) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 328 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 329 | # default is_synchronous to some appropriate value |
| 330 | ControlType = models.Job.ControlType |
| 331 | control_type = ControlType.get_value(control_type) |
| 332 | if is_synchronous is None: |
| 333 | is_synchronous = (control_type == ControlType.SERVER) |
| 334 | # convert the synch flag to an actual type |
| 335 | if is_synchronous: |
| 336 | synch_type = models.Test.SynchType.SYNCHRONOUS |
| 337 | else: |
| 338 | synch_type = models.Test.SynchType.ASYNCHRONOUS |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 339 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 340 | job = models.Job.create(owner=owner, name=name, priority=priority, |
| 341 | control_file=control_file, |
| 342 | control_type=control_type, |
| 343 | synch_type=synch_type, |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 344 | hosts=host_objects, |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 345 | timeout=timeout, |
| 346 | run_verify=run_verify) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 347 | job.queue(host_objects) |
| 348 | return job.id |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 349 | |
| 350 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 351 | def abort_job(id): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 352 | """\ |
| 353 | Abort the job with the given id number. |
showard | 9d821ab | 2008-07-11 16:54:29 +0000 | [diff] [blame] | 354 | |
| 355 | TODO: this method is deprecated in favor of abort_jobs(). We need |
| 356 | to eventually remove this rpc call entirely. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 357 | """ |
| 358 | job = models.Job.objects.get(id=id) |
| 359 | job.abort() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 360 | |
| 361 | |
showard | 9d821ab | 2008-07-11 16:54:29 +0000 | [diff] [blame] | 362 | def abort_jobs(job_ids): |
| 363 | """\ |
| 364 | Abort a list of jobs. |
| 365 | """ |
| 366 | for job in models.Job.objects.in_bulk(job_ids).values(): |
| 367 | job.abort() |
| 368 | |
| 369 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 370 | def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 371 | """\ |
| 372 | Extra filter args for get_jobs: |
| 373 | -not_yet_run: Include only jobs that have not yet started running. |
| 374 | -running: Include only jobs that have start running but for which not |
| 375 | all hosts have completed. |
| 376 | -finished: Include only jobs for which all hosts have completed (or |
| 377 | aborted). |
| 378 | At most one of these three fields should be specified. |
| 379 | """ |
| 380 | filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run, |
| 381 | running, |
| 382 | finished) |
| 383 | return rpc_utils.prepare_for_serialization( |
| 384 | models.Job.list_objects(filter_data)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 385 | |
| 386 | |
| 387 | def get_num_jobs(not_yet_run=False, running=False, finished=False, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 388 | **filter_data): |
| 389 | """\ |
| 390 | See get_jobs() for documentation of extra filter parameters. |
| 391 | """ |
| 392 | filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run, |
| 393 | running, |
| 394 | finished) |
| 395 | return models.Job.query_count(filter_data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 396 | |
| 397 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 398 | def get_jobs_summary(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 399 | """\ |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 400 | Like get_jobs(), but adds a 'status_counts' field, which is a dictionary |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 401 | mapping status strings to the number of hosts currently with that |
| 402 | status, i.e. {'Queued' : 4, 'Running' : 2}. |
| 403 | """ |
| 404 | jobs = get_jobs(**filter_data) |
| 405 | ids = [job['id'] for job in jobs] |
| 406 | all_status_counts = models.Job.objects.get_status_counts(ids) |
| 407 | for job in jobs: |
| 408 | job['status_counts'] = all_status_counts[job['id']] |
| 409 | return rpc_utils.prepare_for_serialization(jobs) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 410 | |
| 411 | |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 412 | def get_info_for_clone(id): |
| 413 | """\ |
| 414 | Retrieves all the information needed to clone a job. |
| 415 | """ |
| 416 | info = {} |
| 417 | job = models.Job.objects.get(id=id) |
| 418 | query = job.hostqueueentry_set.filter(deleted=False) |
| 419 | hosts = [queue_entry.host for queue_entry |
showard | e48cef5 | 2008-07-24 16:32:59 +0000 | [diff] [blame] | 420 | in query if queue_entry.host and not queue_entry.meta_host] |
showard | a8709c5 | 2008-07-03 19:44:54 +0000 | [diff] [blame] | 421 | meta_hosts = [queue_entry.meta_host.name for queue_entry |
| 422 | in query if queue_entry.meta_host] |
| 423 | host_info = get_hosts(hostname__in=[host.hostname for host in hosts]) |
| 424 | |
| 425 | for host in host_info: |
| 426 | platform = host['platform'] |
| 427 | if platform is not None and platform in host['labels']: |
| 428 | host['labels'].remove(platform) |
| 429 | host['other_labels'] = ', '.join(host['labels']) |
| 430 | |
| 431 | meta_host_counts = {} |
| 432 | for meta_host in meta_hosts: |
| 433 | meta_host_counts.setdefault(meta_host, 0) |
| 434 | meta_host_counts[meta_host] += 1 |
| 435 | |
| 436 | info['job'] = job.get_object_dict() |
| 437 | info['meta_host_counts'] = meta_host_counts |
| 438 | info['hosts'] = host_info |
| 439 | |
| 440 | return rpc_utils.prepare_for_serialization(info) |
| 441 | |
| 442 | |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 443 | # host queue entries |
| 444 | |
| 445 | def get_host_queue_entries(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 446 | """\ |
| 447 | TODO |
| 448 | """ |
| 449 | query = models.HostQueueEntry.query_objects(filter_data) |
| 450 | all_dicts = [] |
| 451 | for queue_entry in query.select_related(): |
| 452 | entry_dict = queue_entry.get_object_dict() |
| 453 | if entry_dict['host'] is not None: |
| 454 | entry_dict['host'] = queue_entry.host.get_object_dict() |
| 455 | entry_dict['job'] = queue_entry.job.get_object_dict() |
| 456 | all_dicts.append(entry_dict) |
| 457 | return rpc_utils.prepare_for_serialization(all_dicts) |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 458 | |
| 459 | |
| 460 | def get_num_host_queue_entries(**filter_data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 461 | """\ |
| 462 | Get the number of host queue entries associated with this job. |
| 463 | """ |
| 464 | return models.HostQueueEntry.query_count(filter_data) |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 465 | |
| 466 | |
showard | 1e935f1 | 2008-07-11 00:11:36 +0000 | [diff] [blame] | 467 | def get_hqe_percentage_complete(**filter_data): |
| 468 | """ |
| 469 | Computes the percentage of host queue entries matching the given filter data |
| 470 | that are complete. |
| 471 | """ |
| 472 | query = models.HostQueueEntry.query_objects(filter_data) |
| 473 | complete_count = query.filter(complete=True).count() |
| 474 | total_count = query.count() |
| 475 | if total_count == 0: |
| 476 | return 1 |
| 477 | return float(complete_count) / total_count |
| 478 | |
| 479 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 480 | # other |
| 481 | |
| 482 | def get_static_data(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 483 | """\ |
| 484 | Returns a dictionary containing a bunch of data that shouldn't change |
| 485 | often and is otherwise inaccessible. This includes: |
| 486 | priorities: list of job priority choices |
| 487 | default_priority: default priority value for new jobs |
| 488 | users: sorted list of all users |
| 489 | labels: sorted list of all labels |
| 490 | tests: sorted list of all tests |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 491 | profilers: sorted list of all profilers |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 492 | user_login: logged-in username |
| 493 | host_statuses: sorted list of possible Host statuses |
| 494 | job_statuses: sorted list of possible HostQueueEntry statuses |
| 495 | """ |
| 496 | result = {} |
| 497 | result['priorities'] = models.Job.Priority.choices() |
| 498 | default_priority = models.Job.get_field_dict()['priority'].default |
| 499 | default_string = models.Job.Priority.get_string(default_priority) |
| 500 | result['default_priority'] = default_string |
| 501 | result['users'] = get_users(sort_by=['login']) |
| 502 | result['labels'] = get_labels(sort_by=['-platform', 'name']) |
| 503 | result['tests'] = get_tests(sort_by=['name']) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 504 | result['profilers'] = get_profilers(sort_by=['name']) |
showard | ff90138 | 2008-07-07 23:22:16 +0000 | [diff] [blame] | 505 | result['user_login'] = thread_local.get_user().login |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 506 | result['host_statuses'] = sorted(models.Host.Status.names) |
| 507 | result['job_statuses'] = sorted(models.Job.Status.names) |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 508 | result['job_timeout_default'] = global_config.global_config.get_config_value( |
| 509 | 'AUTOTEST_WEB', 'job_timeout_default') |
showard | 8ac29b4 | 2008-07-17 17:01:55 +0000 | [diff] [blame] | 510 | |
| 511 | result['status_dictionary'] = {"Aborted": "Aborted", |
| 512 | "Verifying": "Verifying Host", |
| 513 | "Pending": "Waiting on other hosts", |
| 514 | "Running": "Running autoserv", |
| 515 | "Completed": "Autoserv completed", |
| 516 | "Failed": "Failed to complete", |
| 517 | "Aborting": "Abort in progress", |
showard | d823b36 | 2008-07-24 16:35:46 +0000 | [diff] [blame] | 518 | "Queued": "Queued", |
| 519 | "Starting": "Next in host's queue"} |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 520 | return result |