blob: 48f90739381877341e0ccd05ff92ba3df3b15677 [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
79def get_hosts(**filter_data):
80 hosts = models.Host.list_objects(filter_data)
81 for host in hosts:
82 host_obj = models.Host.objects.get(id=host['id'])
83 host['labels'] = [label.name
84 for label in host_obj.labels.all()]
85 platform = host_obj.platform()
86 host['platform'] = platform and platform.name or None
87 return rpc_utils.prepare_for_serialization(hosts)
88
89
showard1c8c2212008-04-03 20:33:58 +000090def get_num_hosts(**filter_data):
91 return models.Host.query_count(filter_data)
showard1385b162008-03-13 15:59:40 +000092
mblighe8819cd2008-02-15 16:48:40 +000093
94# tests
95
96def add_test(name, test_type, path, test_class=None, description=None):
97 return models.Test.add_object(name=name, test_type=test_type, path=path,
98 test_class=test_class,
99 description=description).id
100
101
102def modify_test(id, **data):
103 models.Test.smart_get(id).update_object(data)
104
105
106def delete_test(id):
107 models.Test.smart_get(id).delete()
108
109
110def get_tests(**filter_data):
111 return rpc_utils.prepare_for_serialization(
112 models.Test.list_objects(filter_data))
113
114
115# users
116
117def add_user(login, access_level=None):
118 return models.User.add_object(login=login, access_level=access_level).id
119
120
121def modify_user(id, **data):
122 models.User.smart_get(id).update_object(data)
123
124
125def delete_user(id):
126 models.User.smart_get(id).delete()
127
128
129def get_users(**filter_data):
130 return rpc_utils.prepare_for_serialization(
131 models.User.list_objects(filter_data))
132
133
134# acl groups
135
136def add_acl_group(name, description=None):
137 return models.AclGroup.add_object(name=name, description=description).id
138
139
140def modify_acl_group(id, **data):
141 models.AclGroup.smart_get(id).update_object(data)
142
143
144def acl_group_add_users(id, users):
145 users = [models.User.smart_get(user) for user in users]
146 models.AclGroup.smart_get(id).users.add(*users)
147
148
149def acl_group_remove_users(id, users):
150 users = [models.User.smart_get(user) for user in users]
151 models.AclGroup.smart_get(id).users.remove(*users)
152
153
154def acl_group_add_hosts(id, hosts):
155 hosts = [models.Host.smart_get(host) for host in hosts]
156 models.AclGroup.smart_get(id).hosts.add(*hosts)
157
158
159def acl_group_remove_hosts(id, hosts):
160 hosts = [models.Host.smart_get(host) for host in hosts]
161 models.AclGroup.smart_get(id).hosts.remove(*hosts)
162
163
164def delete_acl_group(id):
165 models.AclGroup.smart_get(id).delete()
166
167
168def get_acl_groups(**filter_data):
169 acl_groups = models.AclGroup.list_objects(filter_data)
170 for acl_group in acl_groups:
171 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
172 acl_group['users'] = [user.login
173 for user in acl_group_obj.users.all()]
174 acl_group['hosts'] = [host.hostname
175 for host in acl_group_obj.hosts.all()]
176 return rpc_utils.prepare_for_serialization(acl_groups)
177
178
179# jobs
180
181def generate_control_file(tests, kernel=None, label=None):
182 """\
183 Generates a client-side control file to load a kernel and run a set of
showard8fd58242008-03-10 21:29:07 +0000184 tests. Returns a tuple (control_file, is_server, is_synchronous):
185 control_file - the control file text
186 is_server - is the control file a server-side control file?
187 is_synchronous - should the control file be run synchronously?
mblighe8819cd2008-02-15 16:48:40 +0000188
189 tests: list of tests to run
190 kernel: kernel to install in generated control file
191 label: name of label to grab kernel config from
mblighe8819cd2008-02-15 16:48:40 +0000192 """
193 if not tests:
showard8fd58242008-03-10 21:29:07 +0000194 return '', False, False
mblighe8819cd2008-02-15 16:48:40 +0000195
showard8fd58242008-03-10 21:29:07 +0000196 is_server, is_synchronous, test_objects, label = (
mblighe8819cd2008-02-15 16:48:40 +0000197 rpc_utils.prepare_generate_control_file(tests, kernel, label))
showard1d445e92008-03-12 21:33:31 +0000198 cf_text = control_file.generate_control(test_objects, kernel, label,
199 is_server)
showard8fd58242008-03-10 21:29:07 +0000200 return cf_text, is_server, is_synchronous
mblighe8819cd2008-02-15 16:48:40 +0000201
202
203def create_job(name, priority, control_file, control_type, is_synchronous=None,
204 hosts=None, meta_hosts=None):
205 """\
206 Create and enqueue a job.
207
208 priority: Low, Medium, High, Urgent
209 control_file: contents of control file
210 control_type: type of control file, Client or Server
211 is_synchronous: boolean indicating if a job is synchronous
212 hosts: list of hosts to run job on
213 meta_hosts: list where each entry is a label name, and for each entry
214 one host will be chosen from that label to run the job
215 on.
216 """
217 owner = rpc_utils.get_user().login
218 # input validation
219 if not hosts and not meta_hosts:
220 raise models.ValidationError({
221 'arguments' : "You must pass at least one of 'hosts' or "
222 "'meta_hosts'"
223 })
224
225 # convert hostnames & meta hosts to host/label objects
226 host_objects = []
227 for host in hosts or []:
228 this_host = models.Host.smart_get(host)
229 host_objects.append(this_host)
230 for label in meta_hosts or []:
231 this_label = models.Label.smart_get(label)
232 host_objects.append(this_label)
233
234 # default is_synchronous to some appropriate value
235 ControlType = models.Job.ControlType
236 control_type = ControlType.get_value(control_type)
237 if is_synchronous is None:
238 is_synchronous = (control_type == ControlType.SERVER)
239 # convert the synch flag to an actual type
240 if is_synchronous:
showard8fd58242008-03-10 21:29:07 +0000241 synch_type = models.Test.SynchType.SYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000242 else:
showard8fd58242008-03-10 21:29:07 +0000243 synch_type = models.Test.SynchType.ASYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000244
245 job = models.Job.create(owner=owner, name=name, priority=priority,
246 control_file=control_file,
247 control_type=control_type,
248 synch_type=synch_type,
249 hosts=host_objects)
250 job.queue(host_objects)
251 return job.id
252
253
mbligh3cab4a72008-03-05 23:19:09 +0000254def requeue_job(id):
255 """\
256 Create and enqueue a copy of the given job.
257 """
258 job = models.Job.objects.get(id=id)
259 new_job = job.requeue(rpc_utils.get_user().login)
260 return new_job.id
261
262
mblighe8819cd2008-02-15 16:48:40 +0000263def abort_job(id):
264 """\
265 Abort the job with the given id number.
266 """
267 job = models.Job.objects.get(id=id)
268 job.abort()
269
270
271def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
272 """\
273 Extra filter args for get_jobs:
274 -not_yet_run: Include only jobs that have not yet started running.
275 -running: Include only jobs that have start running but for which not
276 all hosts have completed.
277 -finished: Include only jobs for which all hosts have completed (or
278 aborted).
279 At most one of these fields should be specified.
280 """
281 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
282 running,
283 finished)
284 return rpc_utils.prepare_for_serialization(
285 models.Job.list_objects(filter_data))
286
287
288def get_num_jobs(not_yet_run=False, running=False, finished=False,
289 **filter_data):
290 """\
showard1c8c2212008-04-03 20:33:58 +0000291 See get_jobs() for documentation of extra filter parameters.
mblighe8819cd2008-02-15 16:48:40 +0000292 """
showard1c8c2212008-04-03 20:33:58 +0000293 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000294
295
showard1c8c2212008-04-03 20:33:58 +0000296def job_status(job_id, **filter_data):
mblighe8819cd2008-02-15 16:48:40 +0000297 """\
298 Get status of job with the given id number. Returns a dictionary
299 mapping hostnames to dictionaries with two keys each:
300 * 'status' : the job status for that host
301 * 'meta_count' : For meta host entires, gives a count of how many
302 entries there are for this label (the hostname is
303 then a label name). For real host entries,
304 meta_count is None.
305 """
showard1c8c2212008-04-03 20:33:58 +0000306 filter_data['job'] = job_id
307 job_entries = models.HostQueueEntry.query_objects(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000308 hosts_status = {}
showard1c8c2212008-04-03 20:33:58 +0000309 for queue_entry in job_entries:
mblighe8819cd2008-02-15 16:48:40 +0000310 is_meta = queue_entry.is_meta_host_entry()
311 if is_meta:
312 name = queue_entry.meta_host.name
313 hosts_status.setdefault(name, {'meta_count': 0})
314 hosts_status[name]['meta_count'] += 1
315 else:
316 name = queue_entry.host.hostname
317 hosts_status[name] = {'meta_count': None}
318 hosts_status[name]['status'] = queue_entry.status
319 return hosts_status
320
321
showard1c8c2212008-04-03 20:33:58 +0000322def job_num_entries(job_id, **filter_data):
323 """\
324 Get the number of host queue entries associated with this job.
325 """
326 filter_data['job'] = job_id
327 return models.HostQueueEntry.query_count(filter_data)
328
329
mblighe8819cd2008-02-15 16:48:40 +0000330def get_jobs_summary(**filter_data):
331 """\
332 Like get_jobs(), but adds a 'stauts_counts' field, which is a dictionary
333 mapping status strings to the number of hosts currently with that
334 status, i.e. {'Queued' : 4, 'Running' : 2}.
335 """
336 jobs = get_jobs(**filter_data)
337 ids = [job['id'] for job in jobs]
338 all_status_counts = models.Job.objects.get_status_counts(ids)
339 for job in jobs:
340 job['status_counts'] = all_status_counts[job['id']]
341 return rpc_utils.prepare_for_serialization(jobs)
342
343
344# other
345
346def get_static_data():
347 """\
348 Returns a dictionary containing a bunch of data that shouldn't change
349 often. This includes:
350 priorities: list of job priority choices
351 default_priority: default priority value for new jobs
352 users: sorted list of all usernames
353 labels: sorted list of all label names
354 tests: sorted list of all test names
showard1385b162008-03-13 15:59:40 +0000355 user_login: logged-in username
mblighe8819cd2008-02-15 16:48:40 +0000356 """
357 result = {}
358 result['priorities'] = models.Job.Priority.choices()
359 default_priority = models.Job.get_field_dict()['priority'].default
360 default_string = models.Job.Priority.get_string(default_priority)
361 result['default_priority'] = default_string
362 result['users'] = [user.login for user in
363 models.User.objects.all().order_by('login')]
364 result['labels'] = [label.name for label in
365 models.Label.objects.all().order_by('name')]
366 result['tests'] = get_tests(sort_by='name')
367 result['user_login'] = rpc_utils.get_user().login
showard1c8c2212008-04-03 20:33:58 +0000368 result['host_statuses'] = models.Host.Status.names
369 result['job_statuses'] = models.Job.Status.names
mblighe8819cd2008-02-15 16:48:40 +0000370 return result