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 | |
| 32 | import models, control_file, rpc_utils |
| 33 | |
| 34 | # labels |
| 35 | |
| 36 | def add_label(name, kernel_config=None, platform=None): |
| 37 | return models.Label.add_object(name=name, kernel_config=kernel_config, |
| 38 | platform=platform).id |
| 39 | |
| 40 | |
| 41 | def modify_label(id, **data): |
| 42 | models.Label.smart_get(id).update_object(data) |
| 43 | |
| 44 | |
| 45 | def delete_label(id): |
| 46 | models.Label.smart_get(id).delete() |
| 47 | |
| 48 | |
showard | bbabf50 | 2008-06-06 00:02:02 +0000 | [diff] [blame^] | 49 | def label_add_hosts(id, hosts): |
| 50 | label = models.Label.smart_get(id) |
| 51 | [models.Host.smart_get(host).labels.add(label) for host in hosts] |
| 52 | |
| 53 | |
| 54 | def label_remove_hosts(id, hosts): |
| 55 | label = models.Label.smart_get(id) |
| 56 | [models.Host.smart_get(host).labels.remove(label) for host in hosts] |
| 57 | |
| 58 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 59 | def get_labels(**filter_data): |
| 60 | return rpc_utils.prepare_for_serialization( |
| 61 | models.Label.list_objects(filter_data)) |
| 62 | |
| 63 | |
| 64 | # hosts |
| 65 | |
| 66 | def add_host(hostname, status=None, locked=None): |
| 67 | return models.Host.add_object(hostname=hostname, status=status, |
| 68 | locked=locked).id |
| 69 | |
| 70 | |
| 71 | def modify_host(id, **data): |
| 72 | models.Host.smart_get(id).update_object(data) |
| 73 | |
| 74 | |
| 75 | def host_add_labels(id, labels): |
| 76 | labels = [models.Label.smart_get(label) for label in labels] |
| 77 | models.Host.smart_get(id).labels.add(*labels) |
| 78 | |
| 79 | |
| 80 | def host_remove_labels(id, labels): |
| 81 | labels = [models.Label.smart_get(label) for label in labels] |
| 82 | models.Host.smart_get(id).labels.remove(*labels) |
| 83 | |
| 84 | |
| 85 | def delete_host(id): |
| 86 | models.Host.smart_get(id).delete() |
| 87 | |
| 88 | |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 89 | def get_hosts(multiple_labels=[], **filter_data): |
| 90 | """\ |
| 91 | multiple_labels: match hosts in all of the labels given. Should be a |
| 92 | list of label names. |
| 93 | """ |
| 94 | filter_data['extra_args'] = ( |
| 95 | rpc_utils.extra_host_filters(multiple_labels)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 96 | hosts = models.Host.list_objects(filter_data) |
| 97 | for host in hosts: |
| 98 | host_obj = models.Host.objects.get(id=host['id']) |
| 99 | host['labels'] = [label.name |
| 100 | for label in host_obj.labels.all()] |
| 101 | platform = host_obj.platform() |
| 102 | host['platform'] = platform and platform.name or None |
| 103 | return rpc_utils.prepare_for_serialization(hosts) |
| 104 | |
| 105 | |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 106 | def get_num_hosts(multiple_labels=[], **filter_data): |
| 107 | filter_data['extra_args'] = ( |
| 108 | rpc_utils.extra_host_filters(multiple_labels)) |
showard | 1c8c221 | 2008-04-03 20:33:58 +0000 | [diff] [blame] | 109 | return models.Host.query_count(filter_data) |
showard | 1385b16 | 2008-03-13 15:59:40 +0000 | [diff] [blame] | 110 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 111 | |
| 112 | # tests |
| 113 | |
| 114 | def add_test(name, test_type, path, test_class=None, description=None): |
| 115 | return models.Test.add_object(name=name, test_type=test_type, path=path, |
| 116 | test_class=test_class, |
| 117 | description=description).id |
| 118 | |
| 119 | |
| 120 | def modify_test(id, **data): |
| 121 | models.Test.smart_get(id).update_object(data) |
| 122 | |
| 123 | |
| 124 | def delete_test(id): |
| 125 | models.Test.smart_get(id).delete() |
| 126 | |
| 127 | |
| 128 | def get_tests(**filter_data): |
| 129 | return rpc_utils.prepare_for_serialization( |
| 130 | models.Test.list_objects(filter_data)) |
| 131 | |
| 132 | |
| 133 | # users |
| 134 | |
| 135 | def add_user(login, access_level=None): |
| 136 | return models.User.add_object(login=login, access_level=access_level).id |
| 137 | |
| 138 | |
| 139 | def modify_user(id, **data): |
| 140 | models.User.smart_get(id).update_object(data) |
| 141 | |
| 142 | |
| 143 | def delete_user(id): |
| 144 | models.User.smart_get(id).delete() |
| 145 | |
| 146 | |
| 147 | def get_users(**filter_data): |
| 148 | return rpc_utils.prepare_for_serialization( |
| 149 | models.User.list_objects(filter_data)) |
| 150 | |
| 151 | |
| 152 | # acl groups |
| 153 | |
| 154 | def add_acl_group(name, description=None): |
| 155 | return models.AclGroup.add_object(name=name, description=description).id |
| 156 | |
| 157 | |
| 158 | def modify_acl_group(id, **data): |
| 159 | models.AclGroup.smart_get(id).update_object(data) |
| 160 | |
| 161 | |
| 162 | def acl_group_add_users(id, users): |
| 163 | users = [models.User.smart_get(user) for user in users] |
showard | c2992ba | 2008-05-06 21:19:20 +0000 | [diff] [blame] | 164 | group = models.AclGroup.smart_get(id) |
| 165 | group.users.add(*users) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | def acl_group_remove_users(id, users): |
| 169 | users = [models.User.smart_get(user) for user in users] |
showard | c2992ba | 2008-05-06 21:19:20 +0000 | [diff] [blame] | 170 | group = models.AclGroup.smart_get(id) |
showard | c2992ba | 2008-05-06 21:19:20 +0000 | [diff] [blame] | 171 | group.users.remove(*users) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | def acl_group_add_hosts(id, hosts): |
| 175 | hosts = [models.Host.smart_get(host) for host in hosts] |
showard | c2992ba | 2008-05-06 21:19:20 +0000 | [diff] [blame] | 176 | group = models.AclGroup.smart_get(id) |
| 177 | group.hosts.add(*hosts) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def acl_group_remove_hosts(id, hosts): |
| 181 | hosts = [models.Host.smart_get(host) for host in hosts] |
showard | c2992ba | 2008-05-06 21:19:20 +0000 | [diff] [blame] | 182 | group = models.AclGroup.smart_get(id) |
| 183 | group.hosts.remove(*hosts) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | def delete_acl_group(id): |
| 187 | models.AclGroup.smart_get(id).delete() |
| 188 | |
| 189 | |
| 190 | def get_acl_groups(**filter_data): |
| 191 | acl_groups = models.AclGroup.list_objects(filter_data) |
| 192 | for acl_group in acl_groups: |
| 193 | acl_group_obj = models.AclGroup.objects.get(id=acl_group['id']) |
| 194 | acl_group['users'] = [user.login |
| 195 | for user in acl_group_obj.users.all()] |
| 196 | acl_group['hosts'] = [host.hostname |
| 197 | for host in acl_group_obj.hosts.all()] |
| 198 | return rpc_utils.prepare_for_serialization(acl_groups) |
| 199 | |
| 200 | |
| 201 | # jobs |
| 202 | |
| 203 | def generate_control_file(tests, kernel=None, label=None): |
| 204 | """\ |
| 205 | Generates a client-side control file to load a kernel and run a set of |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 206 | tests. Returns a tuple (control_file, is_server, is_synchronous): |
| 207 | control_file - the control file text |
| 208 | is_server - is the control file a server-side control file? |
| 209 | is_synchronous - should the control file be run synchronously? |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 210 | |
| 211 | tests: list of tests to run |
| 212 | kernel: kernel to install in generated control file |
| 213 | label: name of label to grab kernel config from |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 214 | """ |
| 215 | if not tests: |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 216 | return '', False, False |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 217 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 218 | is_server, is_synchronous, test_objects, label = ( |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 219 | rpc_utils.prepare_generate_control_file(tests, kernel, label)) |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 220 | cf_text = control_file.generate_control(test_objects, kernel, label, |
| 221 | is_server) |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 222 | return cf_text, is_server, is_synchronous |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | def create_job(name, priority, control_file, control_type, is_synchronous=None, |
| 226 | hosts=None, meta_hosts=None): |
| 227 | """\ |
| 228 | Create and enqueue a job. |
| 229 | |
| 230 | priority: Low, Medium, High, Urgent |
| 231 | control_file: contents of control file |
| 232 | control_type: type of control file, Client or Server |
| 233 | is_synchronous: boolean indicating if a job is synchronous |
| 234 | hosts: list of hosts to run job on |
| 235 | meta_hosts: list where each entry is a label name, and for each entry |
| 236 | one host will be chosen from that label to run the job |
| 237 | on. |
| 238 | """ |
| 239 | owner = rpc_utils.get_user().login |
| 240 | # input validation |
| 241 | if not hosts and not meta_hosts: |
| 242 | raise models.ValidationError({ |
| 243 | 'arguments' : "You must pass at least one of 'hosts' or " |
| 244 | "'meta_hosts'" |
| 245 | }) |
| 246 | |
| 247 | # convert hostnames & meta hosts to host/label objects |
| 248 | host_objects = [] |
| 249 | for host in hosts or []: |
| 250 | this_host = models.Host.smart_get(host) |
| 251 | host_objects.append(this_host) |
| 252 | for label in meta_hosts or []: |
| 253 | this_label = models.Label.smart_get(label) |
| 254 | host_objects.append(this_label) |
| 255 | |
| 256 | # default is_synchronous to some appropriate value |
| 257 | ControlType = models.Job.ControlType |
| 258 | control_type = ControlType.get_value(control_type) |
| 259 | if is_synchronous is None: |
| 260 | is_synchronous = (control_type == ControlType.SERVER) |
| 261 | # convert the synch flag to an actual type |
| 262 | if is_synchronous: |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 263 | synch_type = models.Test.SynchType.SYNCHRONOUS |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 264 | else: |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 265 | synch_type = models.Test.SynchType.ASYNCHRONOUS |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 266 | |
| 267 | job = models.Job.create(owner=owner, name=name, priority=priority, |
| 268 | control_file=control_file, |
| 269 | control_type=control_type, |
| 270 | synch_type=synch_type, |
| 271 | hosts=host_objects) |
| 272 | job.queue(host_objects) |
| 273 | return job.id |
| 274 | |
| 275 | |
mbligh | 3cab4a7 | 2008-03-05 23:19:09 +0000 | [diff] [blame] | 276 | def requeue_job(id): |
| 277 | """\ |
| 278 | Create and enqueue a copy of the given job. |
| 279 | """ |
| 280 | job = models.Job.objects.get(id=id) |
| 281 | new_job = job.requeue(rpc_utils.get_user().login) |
| 282 | return new_job.id |
| 283 | |
| 284 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 285 | def abort_job(id): |
| 286 | """\ |
| 287 | Abort the job with the given id number. |
| 288 | """ |
| 289 | job = models.Job.objects.get(id=id) |
| 290 | job.abort() |
| 291 | |
| 292 | |
| 293 | def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data): |
| 294 | """\ |
| 295 | Extra filter args for get_jobs: |
| 296 | -not_yet_run: Include only jobs that have not yet started running. |
| 297 | -running: Include only jobs that have start running but for which not |
| 298 | all hosts have completed. |
| 299 | -finished: Include only jobs for which all hosts have completed (or |
| 300 | aborted). |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 301 | At most one of these three fields should be specified. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 302 | """ |
| 303 | filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run, |
| 304 | running, |
| 305 | finished) |
| 306 | return rpc_utils.prepare_for_serialization( |
| 307 | models.Job.list_objects(filter_data)) |
| 308 | |
| 309 | |
| 310 | def get_num_jobs(not_yet_run=False, running=False, finished=False, |
| 311 | **filter_data): |
| 312 | """\ |
showard | 1c8c221 | 2008-04-03 20:33:58 +0000 | [diff] [blame] | 313 | See get_jobs() for documentation of extra filter parameters. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 314 | """ |
showard | 4f53659 | 2008-04-08 19:41:20 +0000 | [diff] [blame] | 315 | filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run, |
| 316 | running, |
| 317 | finished) |
showard | 1c8c221 | 2008-04-03 20:33:58 +0000 | [diff] [blame] | 318 | return models.Job.query_count(filter_data) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 319 | |
| 320 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 321 | def get_jobs_summary(**filter_data): |
| 322 | """\ |
| 323 | Like get_jobs(), but adds a 'stauts_counts' field, which is a dictionary |
| 324 | mapping status strings to the number of hosts currently with that |
| 325 | status, i.e. {'Queued' : 4, 'Running' : 2}. |
| 326 | """ |
| 327 | jobs = get_jobs(**filter_data) |
| 328 | ids = [job['id'] for job in jobs] |
| 329 | all_status_counts = models.Job.objects.get_status_counts(ids) |
| 330 | for job in jobs: |
| 331 | job['status_counts'] = all_status_counts[job['id']] |
| 332 | return rpc_utils.prepare_for_serialization(jobs) |
| 333 | |
| 334 | |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 335 | # host queue entries |
| 336 | |
| 337 | def get_host_queue_entries(**filter_data): |
| 338 | """\ |
| 339 | TODO |
| 340 | """ |
showard | b8d3424 | 2008-04-25 18:11:16 +0000 | [diff] [blame] | 341 | query = models.HostQueueEntry.query_objects(filter_data) |
| 342 | all_dicts = [] |
| 343 | for queue_entry in query.select_related(): |
| 344 | entry_dict = queue_entry.get_object_dict() |
| 345 | if entry_dict['host'] is not None: |
| 346 | entry_dict['host'] = queue_entry.host.get_object_dict() |
| 347 | entry_dict['job'] = queue_entry.job.get_object_dict() |
| 348 | all_dicts.append(entry_dict) |
| 349 | return rpc_utils.prepare_for_serialization(all_dicts) |
showard | 34dc5fa | 2008-04-24 20:58:40 +0000 | [diff] [blame] | 350 | |
| 351 | |
| 352 | def get_num_host_queue_entries(**filter_data): |
| 353 | """\ |
| 354 | Get the number of host queue entries associated with this job. |
| 355 | """ |
| 356 | return models.HostQueueEntry.query_count(filter_data) |
| 357 | |
| 358 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 359 | # other |
| 360 | |
| 361 | def get_static_data(): |
| 362 | """\ |
| 363 | Returns a dictionary containing a bunch of data that shouldn't change |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 364 | often and is otherwise inaccessible. This includes: |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 365 | priorities: list of job priority choices |
| 366 | default_priority: default priority value for new jobs |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 367 | users: sorted list of all users |
| 368 | labels: sorted list of all labels |
| 369 | tests: sorted list of all tests |
showard | 1385b16 | 2008-03-13 15:59:40 +0000 | [diff] [blame] | 370 | user_login: logged-in username |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 371 | host_statuses: sorted list of possible Host statuses |
| 372 | job_statuses: sorted list of possible HostQueueEntry statuses |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 373 | """ |
| 374 | result = {} |
| 375 | result['priorities'] = models.Job.Priority.choices() |
| 376 | default_priority = models.Job.get_field_dict()['priority'].default |
| 377 | default_string = models.Job.Priority.get_string(default_priority) |
| 378 | result['default_priority'] = default_string |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 379 | result['users'] = get_users(sort_by=['login']) |
| 380 | result['labels'] = get_labels(sort_by=['-platform', 'name']) |
| 381 | result['tests'] = get_tests(sort_by=['name']) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 382 | result['user_login'] = rpc_utils.get_user().login |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 383 | result['host_statuses'] = rpc_utils.sorted(models.Host.Status.names) |
| 384 | result['job_statuses'] = rpc_utils.sorted(models.Job.Status.names) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 385 | return result |