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