blob: 8015912639352fa69556bbbfca5bd5bb146216f0 [file] [log] [blame]
mblighe8819cd2008-02-15 16:48:40 +00001"""\
2Functions to expose over the RPC interface.
3
4For all modify* and delete* functions that ask for an 'id' parameter to
5identify 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
11When specifying foreign key fields (i.e. adding hosts to a label, or adding
12users to an ACL group), the given value may be either the database row ID or the
13name of the object.
14
15All get* functions return lists of dictionaries. Each dictionary represents one
16object and maps field names to values.
17
18Some examples:
19modify_host(2, hostname='myhost') # modify hostname of host with database ID 2
20modify_host('ipaj2', hostname='myhost') # modify hostname of host 'ipaj2'
21modify_test('sleeptest', test_type='Client', params=', seconds=60')
22delete_acl_group(1) # delete by ID
23delete_acl_group('Everyone') # delete by name
24acl_group_add_users('Everyone', ['mbligh', 'showard'])
25get_jobs(owner='showard', status='Queued')
26
27See doctests/rpc_test.txt for (lots) more examples.
28"""
29
30__author__ = 'showard@google.com (Steve Howard)'
31
32import models, control_file, rpc_utils
33
34# labels
35
36def add_label(name, kernel_config=None, platform=None):
jadmanski0afbb632008-06-06 21:10:57 +000037 return models.Label.add_object(name=name, kernel_config=kernel_config,
38 platform=platform).id
mblighe8819cd2008-02-15 16:48:40 +000039
40
41def modify_label(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +000042 models.Label.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +000043
44
45def delete_label(id):
jadmanski0afbb632008-06-06 21:10:57 +000046 models.Label.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +000047
48
showardbbabf502008-06-06 00:02:02 +000049def label_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +000050 host_objs = [models.Host.smart_get(host) for host in hosts]
51 models.Label.smart_get(id).host_set.add(*host_objs)
showardbbabf502008-06-06 00:02:02 +000052
53
54def label_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +000055 host_objs = [models.Host.smart_get(host) for host in hosts]
56 models.Label.smart_get(id).host_set.remove(*host_objs)
showardbbabf502008-06-06 00:02:02 +000057
58
mblighe8819cd2008-02-15 16:48:40 +000059def get_labels(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +000060 return rpc_utils.prepare_for_serialization(
61 models.Label.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +000062
63
64# hosts
65
66def add_host(hostname, status=None, locked=None):
jadmanski0afbb632008-06-06 21:10:57 +000067 return models.Host.add_object(hostname=hostname, status=status,
68 locked=locked).id
mblighe8819cd2008-02-15 16:48:40 +000069
70
71def modify_host(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +000072 models.Host.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +000073
74
75def host_add_labels(id, labels):
jadmanski0afbb632008-06-06 21:10:57 +000076 labels = [models.Label.smart_get(label) for label in labels]
77 models.Host.smart_get(id).labels.add(*labels)
mblighe8819cd2008-02-15 16:48:40 +000078
79
80def host_remove_labels(id, labels):
jadmanski0afbb632008-06-06 21:10:57 +000081 labels = [models.Label.smart_get(label) for label in labels]
82 models.Host.smart_get(id).labels.remove(*labels)
mblighe8819cd2008-02-15 16:48:40 +000083
84
85def delete_host(id):
jadmanski0afbb632008-06-06 21:10:57 +000086 models.Host.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +000087
88
showard8e3aa5e2008-04-08 19:42:32 +000089def get_hosts(multiple_labels=[], **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +000090 """\
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))
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)
mblighe8819cd2008-02-15 16:48:40 +0000104
105
showard8e3aa5e2008-04-08 19:42:32 +0000106def get_num_hosts(multiple_labels=[], **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000107 filter_data['extra_args'] = (
108 rpc_utils.extra_host_filters(multiple_labels))
109 return models.Host.query_count(filter_data)
showard1385b162008-03-13 15:59:40 +0000110
mblighe8819cd2008-02-15 16:48:40 +0000111
112# tests
113
114def add_test(name, test_type, path, test_class=None, description=None):
jadmanski0afbb632008-06-06 21:10:57 +0000115 return models.Test.add_object(name=name, test_type=test_type, path=path,
116 test_class=test_class,
117 description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000118
119
120def modify_test(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000121 models.Test.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000122
123
124def delete_test(id):
jadmanski0afbb632008-06-06 21:10:57 +0000125 models.Test.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000126
127
128def get_tests(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000129 return rpc_utils.prepare_for_serialization(
130 models.Test.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000131
132
133# users
134
135def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000136 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000137
138
139def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000140 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000141
142
143def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000144 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000145
146
147def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000148 return rpc_utils.prepare_for_serialization(
149 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000150
151
152# acl groups
153
154def add_acl_group(name, description=None):
jadmanski0afbb632008-06-06 21:10:57 +0000155 return models.AclGroup.add_object(name=name, description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000156
157
158def modify_acl_group(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000159 models.AclGroup.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000160
161
162def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000163 users = [models.User.smart_get(user) for user in users]
164 group = models.AclGroup.smart_get(id)
165 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000166
167
168def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000169 users = [models.User.smart_get(user) for user in users]
170 group = models.AclGroup.smart_get(id)
171 group.users.remove(*users)
mblighe8819cd2008-02-15 16:48:40 +0000172
173
174def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000175 hosts = [models.Host.smart_get(host) for host in hosts]
176 group = models.AclGroup.smart_get(id)
177 group.hosts.add(*hosts)
mblighe8819cd2008-02-15 16:48:40 +0000178
179
180def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000181 hosts = [models.Host.smart_get(host) for host in hosts]
182 group = models.AclGroup.smart_get(id)
183 group.hosts.remove(*hosts)
mblighe8819cd2008-02-15 16:48:40 +0000184
185
186def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000187 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000188
189
190def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000191 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)
mblighe8819cd2008-02-15 16:48:40 +0000199
200
201# jobs
202
203def generate_control_file(tests, kernel=None, label=None):
jadmanski0afbb632008-06-06 21:10:57 +0000204 """\
205 Generates a client-side control file to load a kernel and run a set of
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?
mblighe8819cd2008-02-15 16:48:40 +0000210
jadmanski0afbb632008-06-06 21:10:57 +0000211 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
214 """
215 if not tests:
216 return '', False, False
mblighe8819cd2008-02-15 16:48:40 +0000217
jadmanski0afbb632008-06-06 21:10:57 +0000218 is_server, is_synchronous, test_objects, label = (
219 rpc_utils.prepare_generate_control_file(tests, kernel, label))
220 cf_text = control_file.generate_control(test_objects, kernel, label,
221 is_server)
222 return cf_text, is_server, is_synchronous
mblighe8819cd2008-02-15 16:48:40 +0000223
224
225def create_job(name, priority, control_file, control_type, is_synchronous=None,
jadmanski0afbb632008-06-06 21:10:57 +0000226 hosts=None, meta_hosts=None):
227 """\
228 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000229
jadmanski0afbb632008-06-06 21:10:57 +0000230 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 })
mblighe8819cd2008-02-15 16:48:40 +0000246
jadmanski0afbb632008-06-06 21:10:57 +0000247 # 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)
mblighe8819cd2008-02-15 16:48:40 +0000255
jadmanski0afbb632008-06-06 21:10:57 +0000256 # 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:
263 synch_type = models.Test.SynchType.SYNCHRONOUS
264 else:
265 synch_type = models.Test.SynchType.ASYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000266
jadmanski0afbb632008-06-06 21:10:57 +0000267 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
mblighe8819cd2008-02-15 16:48:40 +0000274
275
mbligh3cab4a72008-03-05 23:19:09 +0000276def requeue_job(id):
jadmanski0afbb632008-06-06 21:10:57 +0000277 """\
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
mbligh3cab4a72008-03-05 23:19:09 +0000283
284
mblighe8819cd2008-02-15 16:48:40 +0000285def abort_job(id):
jadmanski0afbb632008-06-06 21:10:57 +0000286 """\
287 Abort the job with the given id number.
288 """
289 job = models.Job.objects.get(id=id)
290 job.abort()
mblighe8819cd2008-02-15 16:48:40 +0000291
292
293def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000294 """\
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).
301 At most one of these three fields should be specified.
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))
mblighe8819cd2008-02-15 16:48:40 +0000308
309
310def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000311 **filter_data):
312 """\
313 See get_jobs() for documentation of extra filter parameters.
314 """
315 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
316 running,
317 finished)
318 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000319
320
mblighe8819cd2008-02-15 16:48:40 +0000321def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000322 """\
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)
mblighe8819cd2008-02-15 16:48:40 +0000333
334
showard34dc5fa2008-04-24 20:58:40 +0000335# host queue entries
336
337def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000338 """\
339 TODO
340 """
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)
showard34dc5fa2008-04-24 20:58:40 +0000350
351
352def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000353 """\
354 Get the number of host queue entries associated with this job.
355 """
356 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000357
358
mblighe8819cd2008-02-15 16:48:40 +0000359# other
360
361def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000362 """\
363 Returns a dictionary containing a bunch of data that shouldn't change
364 often and is otherwise inaccessible. This includes:
365 priorities: list of job priority choices
366 default_priority: default priority value for new jobs
367 users: sorted list of all users
368 labels: sorted list of all labels
369 tests: sorted list of all tests
370 user_login: logged-in username
371 host_statuses: sorted list of possible Host statuses
372 job_statuses: sorted list of possible HostQueueEntry statuses
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
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'])
382 result['user_login'] = rpc_utils.get_user().login
383 result['host_statuses'] = rpc_utils.sorted(models.Host.Status.names)
384 result['job_statuses'] = rpc_utils.sorted(models.Job.Status.names)
385 return result