blob: e3160847dcc9b618777b8e89be4c9f127c04577f [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
mblighec5546d2008-06-16 16:51:28 +000032import models, model_logic, control_file, rpc_utils
mblighe8819cd2008-02-15 16:48:40 +000033
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
showard2b9a88b2008-06-13 20:55:03 +0000133# profilers
134
135def add_profiler(name, description=None):
136 return models.Profiler.add_object(name=name, description=description).id
137
138
139def modify_profiler(id, **data):
140 models.Profiler.smart_get(id).update_object(data)
141
142
143def delete_profiler(id):
144 models.Profiler.smart_get(id).delete()
145
146
147def get_profilers(**filter_data):
148 return rpc_utils.prepare_for_serialization(
149 models.Profiler.list_objects(filter_data))
150
151
mblighe8819cd2008-02-15 16:48:40 +0000152# users
153
154def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000155 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000156
157
158def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000159 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000160
161
162def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000163 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000164
165
166def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000167 return rpc_utils.prepare_for_serialization(
168 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000169
170
171# acl groups
172
173def add_acl_group(name, description=None):
jadmanski0afbb632008-06-06 21:10:57 +0000174 return models.AclGroup.add_object(name=name, description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000175
176
177def modify_acl_group(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000178 models.AclGroup.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000179
180
181def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000182 users = [models.User.smart_get(user) for user in users]
183 group = models.AclGroup.smart_get(id)
184 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000185
186
187def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000188 users = [models.User.smart_get(user) for user in users]
189 group = models.AclGroup.smart_get(id)
190 group.users.remove(*users)
mblighe8819cd2008-02-15 16:48:40 +0000191
192
193def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000194 hosts = [models.Host.smart_get(host) for host in hosts]
195 group = models.AclGroup.smart_get(id)
196 group.hosts.add(*hosts)
showard08f981b2008-06-24 21:59:03 +0000197 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000198
199
200def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000201 hosts = [models.Host.smart_get(host) for host in hosts]
202 group = models.AclGroup.smart_get(id)
203 group.hosts.remove(*hosts)
showard08f981b2008-06-24 21:59:03 +0000204 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000205
206
207def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000208 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000209
210
211def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000212 acl_groups = models.AclGroup.list_objects(filter_data)
213 for acl_group in acl_groups:
214 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
215 acl_group['users'] = [user.login
216 for user in acl_group_obj.users.all()]
217 acl_group['hosts'] = [host.hostname
218 for host in acl_group_obj.hosts.all()]
219 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000220
221
222# jobs
223
showard2b9a88b2008-06-13 20:55:03 +0000224def generate_control_file(tests, kernel=None, label=None, profilers=[]):
jadmanski0afbb632008-06-06 21:10:57 +0000225 """\
226 Generates a client-side control file to load a kernel and run a set of
227 tests. Returns a tuple (control_file, is_server, is_synchronous):
228 control_file - the control file text
229 is_server - is the control file a server-side control file?
230 is_synchronous - should the control file be run synchronously?
mblighe8819cd2008-02-15 16:48:40 +0000231
jadmanski0afbb632008-06-06 21:10:57 +0000232 tests: list of tests to run
233 kernel: kernel to install in generated control file
234 label: name of label to grab kernel config from
showard2b9a88b2008-06-13 20:55:03 +0000235 profilers: list of profilers to activate during the job
jadmanski0afbb632008-06-06 21:10:57 +0000236 """
237 if not tests:
238 return '', False, False
mblighe8819cd2008-02-15 16:48:40 +0000239
showard2b9a88b2008-06-13 20:55:03 +0000240 is_server, is_synchronous, test_objects, profiler_objects, label = (
241 rpc_utils.prepare_generate_control_file(tests, kernel, label,
242 profilers))
243 cf_text = control_file.generate_control(tests=test_objects, kernel=kernel,
244 platform=label,
245 profilers=profiler_objects,
246 is_server=is_server)
jadmanski0afbb632008-06-06 21:10:57 +0000247 return cf_text, is_server, is_synchronous
mblighe8819cd2008-02-15 16:48:40 +0000248
249
250def create_job(name, priority, control_file, control_type, is_synchronous=None,
jadmanski0afbb632008-06-06 21:10:57 +0000251 hosts=None, meta_hosts=None):
252 """\
253 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000254
jadmanski0afbb632008-06-06 21:10:57 +0000255 priority: Low, Medium, High, Urgent
256 control_file: contents of control file
257 control_type: type of control file, Client or Server
258 is_synchronous: boolean indicating if a job is synchronous
259 hosts: list of hosts to run job on
260 meta_hosts: list where each entry is a label name, and for each entry
261 one host will be chosen from that label to run the job
262 on.
263 """
264 owner = rpc_utils.get_user().login
265 # input validation
266 if not hosts and not meta_hosts:
mblighec5546d2008-06-16 16:51:28 +0000267 raise model_logic.ValidationError({
jadmanski0afbb632008-06-06 21:10:57 +0000268 'arguments' : "You must pass at least one of 'hosts' or "
269 "'meta_hosts'"
270 })
mblighe8819cd2008-02-15 16:48:40 +0000271
showardba872902008-06-28 00:51:08 +0000272 requested_host_counts = {}
273
jadmanski0afbb632008-06-06 21:10:57 +0000274 # convert hostnames & meta hosts to host/label objects
275 host_objects = []
276 for host in hosts or []:
277 this_host = models.Host.smart_get(host)
278 host_objects.append(this_host)
279 for label in meta_hosts or []:
280 this_label = models.Label.smart_get(label)
281 host_objects.append(this_label)
showardba872902008-06-28 00:51:08 +0000282 requested_host_counts.setdefault(this_label.name, 0)
283 requested_host_counts[this_label.name] += 1
284
285 # check that each metahost request has enough hosts under the label
286 if meta_hosts:
287 labels = models.Label.query_objects(
288 {'name__in': requested_host_counts.keys()})
289 for label in labels:
290 count = label.host_set.count()
291 if requested_host_counts[label.name] > count:
292 error = ("You have requested %d %s's, but there are only %d."
293 % (requested_host_counts[label.name],
294 label.name, count))
295 raise model_logic.ValidationError({'arguments' : error})
mblighe8819cd2008-02-15 16:48:40 +0000296
jadmanski0afbb632008-06-06 21:10:57 +0000297 # default is_synchronous to some appropriate value
298 ControlType = models.Job.ControlType
299 control_type = ControlType.get_value(control_type)
300 if is_synchronous is None:
301 is_synchronous = (control_type == ControlType.SERVER)
302 # convert the synch flag to an actual type
303 if is_synchronous:
304 synch_type = models.Test.SynchType.SYNCHRONOUS
305 else:
306 synch_type = models.Test.SynchType.ASYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000307
jadmanski0afbb632008-06-06 21:10:57 +0000308 job = models.Job.create(owner=owner, name=name, priority=priority,
309 control_file=control_file,
310 control_type=control_type,
311 synch_type=synch_type,
312 hosts=host_objects)
313 job.queue(host_objects)
314 return job.id
mblighe8819cd2008-02-15 16:48:40 +0000315
316
mbligh3cab4a72008-03-05 23:19:09 +0000317def requeue_job(id):
jadmanski0afbb632008-06-06 21:10:57 +0000318 """\
319 Create and enqueue a copy of the given job.
320 """
321 job = models.Job.objects.get(id=id)
322 new_job = job.requeue(rpc_utils.get_user().login)
323 return new_job.id
mbligh3cab4a72008-03-05 23:19:09 +0000324
325
mblighe8819cd2008-02-15 16:48:40 +0000326def abort_job(id):
jadmanski0afbb632008-06-06 21:10:57 +0000327 """\
328 Abort the job with the given id number.
329 """
330 job = models.Job.objects.get(id=id)
331 job.abort()
mblighe8819cd2008-02-15 16:48:40 +0000332
333
334def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000335 """\
336 Extra filter args for get_jobs:
337 -not_yet_run: Include only jobs that have not yet started running.
338 -running: Include only jobs that have start running but for which not
339 all hosts have completed.
340 -finished: Include only jobs for which all hosts have completed (or
341 aborted).
342 At most one of these three fields should be specified.
343 """
344 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
345 running,
346 finished)
347 return rpc_utils.prepare_for_serialization(
348 models.Job.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000349
350
351def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000352 **filter_data):
353 """\
354 See get_jobs() for documentation of extra filter parameters.
355 """
356 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
357 running,
358 finished)
359 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000360
361
mblighe8819cd2008-02-15 16:48:40 +0000362def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000363 """\
364 Like get_jobs(), but adds a 'stauts_counts' field, which is a dictionary
365 mapping status strings to the number of hosts currently with that
366 status, i.e. {'Queued' : 4, 'Running' : 2}.
367 """
368 jobs = get_jobs(**filter_data)
369 ids = [job['id'] for job in jobs]
370 all_status_counts = models.Job.objects.get_status_counts(ids)
371 for job in jobs:
372 job['status_counts'] = all_status_counts[job['id']]
373 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000374
375
showard34dc5fa2008-04-24 20:58:40 +0000376# host queue entries
377
378def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000379 """\
380 TODO
381 """
382 query = models.HostQueueEntry.query_objects(filter_data)
383 all_dicts = []
384 for queue_entry in query.select_related():
385 entry_dict = queue_entry.get_object_dict()
386 if entry_dict['host'] is not None:
387 entry_dict['host'] = queue_entry.host.get_object_dict()
388 entry_dict['job'] = queue_entry.job.get_object_dict()
389 all_dicts.append(entry_dict)
390 return rpc_utils.prepare_for_serialization(all_dicts)
showard34dc5fa2008-04-24 20:58:40 +0000391
392
393def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000394 """\
395 Get the number of host queue entries associated with this job.
396 """
397 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000398
399
mblighe8819cd2008-02-15 16:48:40 +0000400# other
401
402def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000403 """\
404 Returns a dictionary containing a bunch of data that shouldn't change
405 often and is otherwise inaccessible. This includes:
406 priorities: list of job priority choices
407 default_priority: default priority value for new jobs
408 users: sorted list of all users
409 labels: sorted list of all labels
410 tests: sorted list of all tests
showard2b9a88b2008-06-13 20:55:03 +0000411 profilers: sorted list of all profilers
jadmanski0afbb632008-06-06 21:10:57 +0000412 user_login: logged-in username
413 host_statuses: sorted list of possible Host statuses
414 job_statuses: sorted list of possible HostQueueEntry statuses
415 """
416 result = {}
417 result['priorities'] = models.Job.Priority.choices()
418 default_priority = models.Job.get_field_dict()['priority'].default
419 default_string = models.Job.Priority.get_string(default_priority)
420 result['default_priority'] = default_string
421 result['users'] = get_users(sort_by=['login'])
422 result['labels'] = get_labels(sort_by=['-platform', 'name'])
423 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +0000424 result['profilers'] = get_profilers(sort_by=['name'])
jadmanski0afbb632008-06-06 21:10:57 +0000425 result['user_login'] = rpc_utils.get_user().login
showard2b9a88b2008-06-13 20:55:03 +0000426 result['host_statuses'] = sorted(models.Host.Status.names)
427 result['job_statuses'] = sorted(models.Job.Status.names)
jadmanski0afbb632008-06-06 21:10:57 +0000428 return result