blob: a0faa785ed63cfdb3b58c0c3be6431b48441a4a7 [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)
mblighe8819cd2008-02-15 16:48:40 +0000197
198
199def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000200 hosts = [models.Host.smart_get(host) for host in hosts]
201 group = models.AclGroup.smart_get(id)
202 group.hosts.remove(*hosts)
mblighe8819cd2008-02-15 16:48:40 +0000203
204
205def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000206 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000207
208
209def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000210 acl_groups = models.AclGroup.list_objects(filter_data)
211 for acl_group in acl_groups:
212 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
213 acl_group['users'] = [user.login
214 for user in acl_group_obj.users.all()]
215 acl_group['hosts'] = [host.hostname
216 for host in acl_group_obj.hosts.all()]
217 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000218
219
220# jobs
221
showard2b9a88b2008-06-13 20:55:03 +0000222def generate_control_file(tests, kernel=None, label=None, profilers=[]):
jadmanski0afbb632008-06-06 21:10:57 +0000223 """\
224 Generates a client-side control file to load a kernel and run a set of
225 tests. Returns a tuple (control_file, is_server, is_synchronous):
226 control_file - the control file text
227 is_server - is the control file a server-side control file?
228 is_synchronous - should the control file be run synchronously?
mblighe8819cd2008-02-15 16:48:40 +0000229
jadmanski0afbb632008-06-06 21:10:57 +0000230 tests: list of tests to run
231 kernel: kernel to install in generated control file
232 label: name of label to grab kernel config from
showard2b9a88b2008-06-13 20:55:03 +0000233 profilers: list of profilers to activate during the job
jadmanski0afbb632008-06-06 21:10:57 +0000234 """
235 if not tests:
236 return '', False, False
mblighe8819cd2008-02-15 16:48:40 +0000237
showard2b9a88b2008-06-13 20:55:03 +0000238 is_server, is_synchronous, test_objects, profiler_objects, label = (
239 rpc_utils.prepare_generate_control_file(tests, kernel, label,
240 profilers))
241 cf_text = control_file.generate_control(tests=test_objects, kernel=kernel,
242 platform=label,
243 profilers=profiler_objects,
244 is_server=is_server)
jadmanski0afbb632008-06-06 21:10:57 +0000245 return cf_text, is_server, is_synchronous
mblighe8819cd2008-02-15 16:48:40 +0000246
247
248def create_job(name, priority, control_file, control_type, is_synchronous=None,
jadmanski0afbb632008-06-06 21:10:57 +0000249 hosts=None, meta_hosts=None):
250 """\
251 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000252
jadmanski0afbb632008-06-06 21:10:57 +0000253 priority: Low, Medium, High, Urgent
254 control_file: contents of control file
255 control_type: type of control file, Client or Server
256 is_synchronous: boolean indicating if a job is synchronous
257 hosts: list of hosts to run job on
258 meta_hosts: list where each entry is a label name, and for each entry
259 one host will be chosen from that label to run the job
260 on.
261 """
262 owner = rpc_utils.get_user().login
263 # input validation
264 if not hosts and not meta_hosts:
mblighec5546d2008-06-16 16:51:28 +0000265 raise model_logic.ValidationError({
jadmanski0afbb632008-06-06 21:10:57 +0000266 'arguments' : "You must pass at least one of 'hosts' or "
267 "'meta_hosts'"
268 })
mblighe8819cd2008-02-15 16:48:40 +0000269
jadmanski0afbb632008-06-06 21:10:57 +0000270 # convert hostnames & meta hosts to host/label objects
271 host_objects = []
272 for host in hosts or []:
273 this_host = models.Host.smart_get(host)
274 host_objects.append(this_host)
275 for label in meta_hosts or []:
276 this_label = models.Label.smart_get(label)
277 host_objects.append(this_label)
mblighe8819cd2008-02-15 16:48:40 +0000278
jadmanski0afbb632008-06-06 21:10:57 +0000279 # default is_synchronous to some appropriate value
280 ControlType = models.Job.ControlType
281 control_type = ControlType.get_value(control_type)
282 if is_synchronous is None:
283 is_synchronous = (control_type == ControlType.SERVER)
284 # convert the synch flag to an actual type
285 if is_synchronous:
286 synch_type = models.Test.SynchType.SYNCHRONOUS
287 else:
288 synch_type = models.Test.SynchType.ASYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000289
jadmanski0afbb632008-06-06 21:10:57 +0000290 job = models.Job.create(owner=owner, name=name, priority=priority,
291 control_file=control_file,
292 control_type=control_type,
293 synch_type=synch_type,
294 hosts=host_objects)
295 job.queue(host_objects)
296 return job.id
mblighe8819cd2008-02-15 16:48:40 +0000297
298
mbligh3cab4a72008-03-05 23:19:09 +0000299def requeue_job(id):
jadmanski0afbb632008-06-06 21:10:57 +0000300 """\
301 Create and enqueue a copy of the given job.
302 """
303 job = models.Job.objects.get(id=id)
304 new_job = job.requeue(rpc_utils.get_user().login)
305 return new_job.id
mbligh3cab4a72008-03-05 23:19:09 +0000306
307
mblighe8819cd2008-02-15 16:48:40 +0000308def abort_job(id):
jadmanski0afbb632008-06-06 21:10:57 +0000309 """\
310 Abort the job with the given id number.
311 """
312 job = models.Job.objects.get(id=id)
313 job.abort()
mblighe8819cd2008-02-15 16:48:40 +0000314
315
316def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000317 """\
318 Extra filter args for get_jobs:
319 -not_yet_run: Include only jobs that have not yet started running.
320 -running: Include only jobs that have start running but for which not
321 all hosts have completed.
322 -finished: Include only jobs for which all hosts have completed (or
323 aborted).
324 At most one of these three fields should be specified.
325 """
326 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
327 running,
328 finished)
329 return rpc_utils.prepare_for_serialization(
330 models.Job.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000331
332
333def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000334 **filter_data):
335 """\
336 See get_jobs() for documentation of extra filter parameters.
337 """
338 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
339 running,
340 finished)
341 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000342
343
mblighe8819cd2008-02-15 16:48:40 +0000344def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000345 """\
346 Like get_jobs(), but adds a 'stauts_counts' field, which is a dictionary
347 mapping status strings to the number of hosts currently with that
348 status, i.e. {'Queued' : 4, 'Running' : 2}.
349 """
350 jobs = get_jobs(**filter_data)
351 ids = [job['id'] for job in jobs]
352 all_status_counts = models.Job.objects.get_status_counts(ids)
353 for job in jobs:
354 job['status_counts'] = all_status_counts[job['id']]
355 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000356
357
showard34dc5fa2008-04-24 20:58:40 +0000358# host queue entries
359
360def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000361 """\
362 TODO
363 """
364 query = models.HostQueueEntry.query_objects(filter_data)
365 all_dicts = []
366 for queue_entry in query.select_related():
367 entry_dict = queue_entry.get_object_dict()
368 if entry_dict['host'] is not None:
369 entry_dict['host'] = queue_entry.host.get_object_dict()
370 entry_dict['job'] = queue_entry.job.get_object_dict()
371 all_dicts.append(entry_dict)
372 return rpc_utils.prepare_for_serialization(all_dicts)
showard34dc5fa2008-04-24 20:58:40 +0000373
374
375def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000376 """\
377 Get the number of host queue entries associated with this job.
378 """
379 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000380
381
mblighe8819cd2008-02-15 16:48:40 +0000382# other
383
384def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000385 """\
386 Returns a dictionary containing a bunch of data that shouldn't change
387 often and is otherwise inaccessible. This includes:
388 priorities: list of job priority choices
389 default_priority: default priority value for new jobs
390 users: sorted list of all users
391 labels: sorted list of all labels
392 tests: sorted list of all tests
showard2b9a88b2008-06-13 20:55:03 +0000393 profilers: sorted list of all profilers
jadmanski0afbb632008-06-06 21:10:57 +0000394 user_login: logged-in username
395 host_statuses: sorted list of possible Host statuses
396 job_statuses: sorted list of possible HostQueueEntry statuses
397 """
398 result = {}
399 result['priorities'] = models.Job.Priority.choices()
400 default_priority = models.Job.get_field_dict()['priority'].default
401 default_string = models.Job.Priority.get_string(default_priority)
402 result['default_priority'] = default_string
403 result['users'] = get_users(sort_by=['login'])
404 result['labels'] = get_labels(sort_by=['-platform', 'name'])
405 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +0000406 result['profilers'] = get_profilers(sort_by=['name'])
jadmanski0afbb632008-06-06 21:10:57 +0000407 result['user_login'] = rpc_utils.get_user().login
showard2b9a88b2008-06-13 20:55:03 +0000408 result['host_statuses'] = sorted(models.Host.Status.names)
409 result['job_statuses'] = sorted(models.Job.Status.names)
jadmanski0afbb632008-06-06 21:10:57 +0000410 return result