blob: 8c87340d907ade9c5ade66040f4c5bb572960f75 [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):
37 return models.Label.add_object(name=name, kernel_config=kernel_config,
38 platform=platform).id
39
40
41def modify_label(id, **data):
42 models.Label.smart_get(id).update_object(data)
43
44
45def delete_label(id):
46 models.Label.smart_get(id).delete()
47
48
49def get_labels(**filter_data):
50 return rpc_utils.prepare_for_serialization(
51 models.Label.list_objects(filter_data))
52
53
54# hosts
55
56def add_host(hostname, status=None, locked=None):
57 return models.Host.add_object(hostname=hostname, status=status,
58 locked=locked).id
59
60
61def modify_host(id, **data):
62 models.Host.smart_get(id).update_object(data)
63
64
65def 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
70def 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
75def delete_host(id):
76 models.Host.smart_get(id).delete()
77
78
showard8e3aa5e2008-04-08 19:42:32 +000079def 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))
mblighe8819cd2008-02-15 16:48:40 +000086 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
showard8e3aa5e2008-04-08 19:42:32 +000096def get_num_hosts(multiple_labels=[], **filter_data):
97 filter_data['extra_args'] = (
98 rpc_utils.extra_host_filters(multiple_labels))
showard1c8c2212008-04-03 20:33:58 +000099 return models.Host.query_count(filter_data)
showard1385b162008-03-13 15:59:40 +0000100
mblighe8819cd2008-02-15 16:48:40 +0000101
102# tests
103
104def 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
110def modify_test(id, **data):
111 models.Test.smart_get(id).update_object(data)
112
113
114def delete_test(id):
115 models.Test.smart_get(id).delete()
116
117
118def get_tests(**filter_data):
119 return rpc_utils.prepare_for_serialization(
120 models.Test.list_objects(filter_data))
121
122
123# users
124
125def add_user(login, access_level=None):
126 return models.User.add_object(login=login, access_level=access_level).id
127
128
129def modify_user(id, **data):
130 models.User.smart_get(id).update_object(data)
131
132
133def delete_user(id):
134 models.User.smart_get(id).delete()
135
136
137def get_users(**filter_data):
138 return rpc_utils.prepare_for_serialization(
139 models.User.list_objects(filter_data))
140
141
142# acl groups
143
144def add_acl_group(name, description=None):
145 return models.AclGroup.add_object(name=name, description=description).id
146
147
148def modify_acl_group(id, **data):
149 models.AclGroup.smart_get(id).update_object(data)
150
151
152def 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)
155
156
157def acl_group_remove_users(id, users):
158 users = [models.User.smart_get(user) for user in users]
159 models.AclGroup.smart_get(id).users.remove(*users)
160
161
162def acl_group_add_hosts(id, hosts):
163 hosts = [models.Host.smart_get(host) for host in hosts]
164 models.AclGroup.smart_get(id).hosts.add(*hosts)
165
166
167def acl_group_remove_hosts(id, hosts):
168 hosts = [models.Host.smart_get(host) for host in hosts]
169 models.AclGroup.smart_get(id).hosts.remove(*hosts)
170
171
172def delete_acl_group(id):
173 models.AclGroup.smart_get(id).delete()
174
175
176def get_acl_groups(**filter_data):
177 acl_groups = models.AclGroup.list_objects(filter_data)
178 for acl_group in acl_groups:
179 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
180 acl_group['users'] = [user.login
181 for user in acl_group_obj.users.all()]
182 acl_group['hosts'] = [host.hostname
183 for host in acl_group_obj.hosts.all()]
184 return rpc_utils.prepare_for_serialization(acl_groups)
185
186
187# jobs
188
189def generate_control_file(tests, kernel=None, label=None):
190 """\
191 Generates a client-side control file to load a kernel and run a set of
showard8fd58242008-03-10 21:29:07 +0000192 tests. Returns a tuple (control_file, is_server, is_synchronous):
193 control_file - the control file text
194 is_server - is the control file a server-side control file?
195 is_synchronous - should the control file be run synchronously?
mblighe8819cd2008-02-15 16:48:40 +0000196
197 tests: list of tests to run
198 kernel: kernel to install in generated control file
199 label: name of label to grab kernel config from
mblighe8819cd2008-02-15 16:48:40 +0000200 """
201 if not tests:
showard8fd58242008-03-10 21:29:07 +0000202 return '', False, False
mblighe8819cd2008-02-15 16:48:40 +0000203
showard8fd58242008-03-10 21:29:07 +0000204 is_server, is_synchronous, test_objects, label = (
mblighe8819cd2008-02-15 16:48:40 +0000205 rpc_utils.prepare_generate_control_file(tests, kernel, label))
showard1d445e92008-03-12 21:33:31 +0000206 cf_text = control_file.generate_control(test_objects, kernel, label,
207 is_server)
showard8fd58242008-03-10 21:29:07 +0000208 return cf_text, is_server, is_synchronous
mblighe8819cd2008-02-15 16:48:40 +0000209
210
211def create_job(name, priority, control_file, control_type, is_synchronous=None,
212 hosts=None, meta_hosts=None):
213 """\
214 Create and enqueue a job.
215
216 priority: Low, Medium, High, Urgent
217 control_file: contents of control file
218 control_type: type of control file, Client or Server
219 is_synchronous: boolean indicating if a job is synchronous
220 hosts: list of hosts to run job on
221 meta_hosts: list where each entry is a label name, and for each entry
222 one host will be chosen from that label to run the job
223 on.
224 """
225 owner = rpc_utils.get_user().login
226 # input validation
227 if not hosts and not meta_hosts:
228 raise models.ValidationError({
229 'arguments' : "You must pass at least one of 'hosts' or "
230 "'meta_hosts'"
231 })
232
233 # convert hostnames & meta hosts to host/label objects
234 host_objects = []
235 for host in hosts or []:
236 this_host = models.Host.smart_get(host)
237 host_objects.append(this_host)
238 for label in meta_hosts or []:
239 this_label = models.Label.smart_get(label)
240 host_objects.append(this_label)
241
242 # default is_synchronous to some appropriate value
243 ControlType = models.Job.ControlType
244 control_type = ControlType.get_value(control_type)
245 if is_synchronous is None:
246 is_synchronous = (control_type == ControlType.SERVER)
247 # convert the synch flag to an actual type
248 if is_synchronous:
showard8fd58242008-03-10 21:29:07 +0000249 synch_type = models.Test.SynchType.SYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000250 else:
showard8fd58242008-03-10 21:29:07 +0000251 synch_type = models.Test.SynchType.ASYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000252
253 job = models.Job.create(owner=owner, name=name, priority=priority,
254 control_file=control_file,
255 control_type=control_type,
256 synch_type=synch_type,
257 hosts=host_objects)
258 job.queue(host_objects)
259 return job.id
260
261
mbligh3cab4a72008-03-05 23:19:09 +0000262def requeue_job(id):
263 """\
264 Create and enqueue a copy of the given job.
265 """
266 job = models.Job.objects.get(id=id)
267 new_job = job.requeue(rpc_utils.get_user().login)
268 return new_job.id
269
270
mblighe8819cd2008-02-15 16:48:40 +0000271def abort_job(id):
272 """\
273 Abort the job with the given id number.
274 """
275 job = models.Job.objects.get(id=id)
276 job.abort()
277
278
279def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
280 """\
281 Extra filter args for get_jobs:
282 -not_yet_run: Include only jobs that have not yet started running.
283 -running: Include only jobs that have start running but for which not
284 all hosts have completed.
285 -finished: Include only jobs for which all hosts have completed (or
286 aborted).
showard8e3aa5e2008-04-08 19:42:32 +0000287 At most one of these three fields should be specified.
mblighe8819cd2008-02-15 16:48:40 +0000288 """
289 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
290 running,
291 finished)
292 return rpc_utils.prepare_for_serialization(
293 models.Job.list_objects(filter_data))
294
295
296def get_num_jobs(not_yet_run=False, running=False, finished=False,
297 **filter_data):
298 """\
showard1c8c2212008-04-03 20:33:58 +0000299 See get_jobs() for documentation of extra filter parameters.
mblighe8819cd2008-02-15 16:48:40 +0000300 """
showard4f536592008-04-08 19:41:20 +0000301 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
302 running,
303 finished)
showard1c8c2212008-04-03 20:33:58 +0000304 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000305
306
showard1c8c2212008-04-03 20:33:58 +0000307def job_status(job_id, **filter_data):
mblighe8819cd2008-02-15 16:48:40 +0000308 """\
309 Get status of job with the given id number. Returns a dictionary
310 mapping hostnames to dictionaries with two keys each:
311 * 'status' : the job status for that host
312 * 'meta_count' : For meta host entires, gives a count of how many
313 entries there are for this label (the hostname is
314 then a label name). For real host entries,
315 meta_count is None.
316 """
showard1c8c2212008-04-03 20:33:58 +0000317 filter_data['job'] = job_id
318 job_entries = models.HostQueueEntry.query_objects(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000319 hosts_status = {}
showard1c8c2212008-04-03 20:33:58 +0000320 for queue_entry in job_entries:
mblighe8819cd2008-02-15 16:48:40 +0000321 is_meta = queue_entry.is_meta_host_entry()
322 if is_meta:
323 name = queue_entry.meta_host.name
324 hosts_status.setdefault(name, {'meta_count': 0})
325 hosts_status[name]['meta_count'] += 1
326 else:
327 name = queue_entry.host.hostname
328 hosts_status[name] = {'meta_count': None}
329 hosts_status[name]['status'] = queue_entry.status
330 return hosts_status
331
332
showard1c8c2212008-04-03 20:33:58 +0000333def job_num_entries(job_id, **filter_data):
334 """\
335 Get the number of host queue entries associated with this job.
336 """
337 filter_data['job'] = job_id
338 return models.HostQueueEntry.query_count(filter_data)
339
340
mblighe8819cd2008-02-15 16:48:40 +0000341def get_jobs_summary(**filter_data):
342 """\
343 Like get_jobs(), but adds a 'stauts_counts' field, which is a dictionary
344 mapping status strings to the number of hosts currently with that
345 status, i.e. {'Queued' : 4, 'Running' : 2}.
346 """
347 jobs = get_jobs(**filter_data)
348 ids = [job['id'] for job in jobs]
349 all_status_counts = models.Job.objects.get_status_counts(ids)
350 for job in jobs:
351 job['status_counts'] = all_status_counts[job['id']]
352 return rpc_utils.prepare_for_serialization(jobs)
353
354
355# other
356
357def get_static_data():
358 """\
359 Returns a dictionary containing a bunch of data that shouldn't change
showard8e3aa5e2008-04-08 19:42:32 +0000360 often and is otherwise inaccessible. This includes:
mblighe8819cd2008-02-15 16:48:40 +0000361 priorities: list of job priority choices
362 default_priority: default priority value for new jobs
showard8e3aa5e2008-04-08 19:42:32 +0000363 users: sorted list of all users
364 labels: sorted list of all labels
365 tests: sorted list of all tests
showard1385b162008-03-13 15:59:40 +0000366 user_login: logged-in username
showard8e3aa5e2008-04-08 19:42:32 +0000367 host_statuses: sorted list of possible Host statuses
368 job_statuses: sorted list of possible HostQueueEntry statuses
mblighe8819cd2008-02-15 16:48:40 +0000369 """
370 result = {}
371 result['priorities'] = models.Job.Priority.choices()
372 default_priority = models.Job.get_field_dict()['priority'].default
373 default_string = models.Job.Priority.get_string(default_priority)
374 result['default_priority'] = default_string
showard8e3aa5e2008-04-08 19:42:32 +0000375 result['users'] = get_users(sort_by=['login'])
376 result['labels'] = get_labels(sort_by=['-platform', 'name'])
377 result['tests'] = get_tests(sort_by=['name'])
mblighe8819cd2008-02-15 16:48:40 +0000378 result['user_login'] = rpc_utils.get_user().login
showard8e3aa5e2008-04-08 19:42:32 +0000379 result['host_statuses'] = rpc_utils.sorted(models.Host.Status.names)
380 result['job_statuses'] = rpc_utils.sorted(models.Job.Status.names)
mblighe8819cd2008-02-15 16:48:40 +0000381 return result