blob: 092250f13f718bd599eeeffc31572d8d21150dda [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
mbligh93c80e62009-02-03 17:48:30 +000027See doctests/001_rpc_test.txt for (lots) more examples.
mblighe8819cd2008-02-15 16:48:40 +000028"""
29
30__author__ = 'showard@google.com (Steve Howard)'
31
showardff901382008-07-07 23:22:16 +000032from frontend import thread_local
showard09096d82008-07-07 23:20:49 +000033from frontend.afe import models, model_logic, control_file, rpc_utils
showard3bb499f2008-07-03 19:42:20 +000034from autotest_lib.client.common_lib import global_config
35
mblighe8819cd2008-02-15 16:48:40 +000036
37# labels
38
showard989f25d2008-10-01 11:38:11 +000039def add_label(name, kernel_config=None, platform=None, only_if_needed=None):
jadmanski0afbb632008-06-06 21:10:57 +000040 return models.Label.add_object(name=name, kernel_config=kernel_config,
showard989f25d2008-10-01 11:38:11 +000041 platform=platform,
42 only_if_needed=only_if_needed).id
mblighe8819cd2008-02-15 16:48:40 +000043
44
45def modify_label(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +000046 models.Label.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +000047
48
49def delete_label(id):
jadmanski0afbb632008-06-06 21:10:57 +000050 models.Label.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +000051
52
showardbbabf502008-06-06 00:02:02 +000053def label_add_hosts(id, hosts):
showardbe3ec042008-11-12 18:16:07 +000054 host_objs = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +000055 models.Label.smart_get(id).host_set.add(*host_objs)
showardbbabf502008-06-06 00:02:02 +000056
57
58def label_remove_hosts(id, hosts):
showardbe3ec042008-11-12 18:16:07 +000059 host_objs = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +000060 models.Label.smart_get(id).host_set.remove(*host_objs)
showardbbabf502008-06-06 00:02:02 +000061
62
mblighe8819cd2008-02-15 16:48:40 +000063def get_labels(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +000064 return rpc_utils.prepare_for_serialization(
65 models.Label.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +000066
67
68# hosts
69
showarddf062562008-07-03 19:56:37 +000070def add_host(hostname, status=None, locked=None, protection=None):
jadmanski0afbb632008-06-06 21:10:57 +000071 return models.Host.add_object(hostname=hostname, status=status,
showarddf062562008-07-03 19:56:37 +000072 locked=locked, protection=protection).id
mblighe8819cd2008-02-15 16:48:40 +000073
74
75def modify_host(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +000076 models.Host.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +000077
78
79def host_add_labels(id, labels):
showardbe3ec042008-11-12 18:16:07 +000080 labels = models.Label.smart_get_bulk(labels)
jadmanski0afbb632008-06-06 21:10:57 +000081 models.Host.smart_get(id).labels.add(*labels)
mblighe8819cd2008-02-15 16:48:40 +000082
83
84def host_remove_labels(id, labels):
showardbe3ec042008-11-12 18:16:07 +000085 labels = models.Label.smart_get_bulk(labels)
jadmanski0afbb632008-06-06 21:10:57 +000086 models.Host.smart_get(id).labels.remove(*labels)
mblighe8819cd2008-02-15 16:48:40 +000087
88
89def delete_host(id):
jadmanski0afbb632008-06-06 21:10:57 +000090 models.Host.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +000091
92
showard43a3d262008-11-12 18:17:05 +000093def get_hosts(multiple_labels=[], exclude_only_if_needed_labels=False,
94 **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +000095 """\
96 multiple_labels: match hosts in all of the labels given. Should be a
97 list of label names.
showard43a3d262008-11-12 18:17:05 +000098 exclude_only_if_needed_labels: exclude hosts with at least one
99 "only_if_needed" label applied.
jadmanski0afbb632008-06-06 21:10:57 +0000100 """
showard43a3d262008-11-12 18:17:05 +0000101 hosts = rpc_utils.get_host_query(multiple_labels,
102 exclude_only_if_needed_labels,
103 filter_data)
104 host_dicts = []
105 for host_obj in hosts:
106 host_dict = host_obj.get_object_dict()
107 host_dict['labels'] = [label.name for label in host_obj.labels.all()]
jadmanski0afbb632008-06-06 21:10:57 +0000108 platform = host_obj.platform()
showard43a3d262008-11-12 18:17:05 +0000109 host_dict['platform'] = platform and platform.name or None
110 host_dicts.append(host_dict)
111 return rpc_utils.prepare_for_serialization(host_dicts)
mblighe8819cd2008-02-15 16:48:40 +0000112
113
showard43a3d262008-11-12 18:17:05 +0000114def get_num_hosts(multiple_labels=[], exclude_only_if_needed_labels=False,
115 **filter_data):
116 hosts = rpc_utils.get_host_query(multiple_labels,
117 exclude_only_if_needed_labels,
118 filter_data)
119 return hosts.count()
showard1385b162008-03-13 15:59:40 +0000120
mblighe8819cd2008-02-15 16:48:40 +0000121
122# tests
123
showard909c7a62008-07-15 21:52:38 +0000124def add_test(name, test_type, path, author=None, dependencies=None,
showard3d9899a2008-07-31 02:11:58 +0000125 experimental=True, run_verify=None, test_class=None,
showard909c7a62008-07-15 21:52:38 +0000126 test_time=None, test_category=None, description=None,
127 sync_count=1):
jadmanski0afbb632008-06-06 21:10:57 +0000128 return models.Test.add_object(name=name, test_type=test_type, path=path,
showard909c7a62008-07-15 21:52:38 +0000129 author=author, dependencies=dependencies,
130 experimental=experimental,
131 run_verify=run_verify, test_time=test_time,
132 test_category=test_category,
133 sync_count=sync_count,
jadmanski0afbb632008-06-06 21:10:57 +0000134 test_class=test_class,
135 description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000136
137
138def modify_test(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000139 models.Test.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000140
141
142def delete_test(id):
jadmanski0afbb632008-06-06 21:10:57 +0000143 models.Test.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000144
145
146def get_tests(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000147 return rpc_utils.prepare_for_serialization(
148 models.Test.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000149
150
showard2b9a88b2008-06-13 20:55:03 +0000151# profilers
152
153def add_profiler(name, description=None):
154 return models.Profiler.add_object(name=name, description=description).id
155
156
157def modify_profiler(id, **data):
158 models.Profiler.smart_get(id).update_object(data)
159
160
161def delete_profiler(id):
162 models.Profiler.smart_get(id).delete()
163
164
165def get_profilers(**filter_data):
166 return rpc_utils.prepare_for_serialization(
167 models.Profiler.list_objects(filter_data))
168
169
mblighe8819cd2008-02-15 16:48:40 +0000170# users
171
172def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000173 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000174
175
176def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000177 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000178
179
180def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000181 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000182
183
184def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000185 return rpc_utils.prepare_for_serialization(
186 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000187
188
189# acl groups
190
191def add_acl_group(name, description=None):
showard04f2cd82008-07-25 20:53:31 +0000192 group = models.AclGroup.add_object(name=name, description=description)
193 group.users.add(thread_local.get_user())
194 return group.id
mblighe8819cd2008-02-15 16:48:40 +0000195
196
197def modify_acl_group(id, **data):
showard04f2cd82008-07-25 20:53:31 +0000198 group = models.AclGroup.smart_get(id)
199 group.check_for_acl_violation_acl_group()
200 group.update_object(data)
201 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000202
203
204def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000205 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000206 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000207 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000208 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000209
210
211def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000212 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000213 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000214 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000215 group.users.remove(*users)
showard04f2cd82008-07-25 20:53:31 +0000216 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000217
218
219def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000220 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000221 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000222 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000223 group.hosts.add(*hosts)
showard08f981b2008-06-24 21:59:03 +0000224 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000225
226
227def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000228 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000229 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000230 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000231 group.hosts.remove(*hosts)
showard08f981b2008-06-24 21:59:03 +0000232 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000233
234
235def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000236 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000237
238
239def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000240 acl_groups = models.AclGroup.list_objects(filter_data)
241 for acl_group in acl_groups:
242 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
243 acl_group['users'] = [user.login
244 for user in acl_group_obj.users.all()]
245 acl_group['hosts'] = [host.hostname
246 for host in acl_group_obj.hosts.all()]
247 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000248
249
250# jobs
251
mbligh120351e2009-01-24 01:40:45 +0000252def generate_control_file(tests=(), kernel=None, label=None, profilers=(),
253 client_control_file='', use_container=False):
jadmanski0afbb632008-06-06 21:10:57 +0000254 """
mbligh120351e2009-01-24 01:40:45 +0000255 Generates a client-side control file to load a kernel and run tests.
256
257 @param tests List of tests to run.
258 @param kernel Kernel to install in generated control file.
259 @param label Name of label to grab kernel config from.
260 @param profilers List of profilers to activate during the job.
261 @param client_control_file The contents of a client-side control file to
262 run at the end of all tests. If this is supplied, all tests must be
263 client side.
264 TODO: in the future we should support server control files directly
265 to wrap with a kernel. That'll require changing the parameter
266 name and adding a boolean to indicate if it is a client or server
267 control file.
268 @param use_container unused argument today. TODO: Enable containers
269 on the host during a client side test.
270
271 @returns a dict with the following keys:
272 control_file: str, The control file text.
273 is_server: bool, is the control file a server-side control file?
274 synch_count: How many machines the job uses per autoserv execution.
275 synch_count == 1 means the job is asynchronous.
276 dependencies: A list of the names of labels on which the job depends.
277 """
278 if not tests and not control_file:
showard2bab8f42008-11-12 18:15:22 +0000279 return dict(control_file='', is_server=False, synch_count=1,
showard989f25d2008-10-01 11:38:11 +0000280 dependencies=[])
mblighe8819cd2008-02-15 16:48:40 +0000281
showard989f25d2008-10-01 11:38:11 +0000282 cf_info, test_objects, profiler_objects, label = (
showard2b9a88b2008-06-13 20:55:03 +0000283 rpc_utils.prepare_generate_control_file(tests, kernel, label,
284 profilers))
showard989f25d2008-10-01 11:38:11 +0000285 cf_info['control_file'] = control_file.generate_control(
286 tests=test_objects, kernel=kernel, platform=label,
mbligh120351e2009-01-24 01:40:45 +0000287 profilers=profiler_objects, is_server=cf_info['is_server'],
288 client_control_file=client_control_file)
showard989f25d2008-10-01 11:38:11 +0000289 return cf_info
mblighe8819cd2008-02-15 16:48:40 +0000290
291
showard3bb499f2008-07-03 19:42:20 +0000292def create_job(name, priority, control_file, control_type, timeout=None,
showard2bab8f42008-11-12 18:15:22 +0000293 synch_count=None, hosts=None, meta_hosts=None,
showard989f25d2008-10-01 11:38:11 +0000294 run_verify=True, one_time_hosts=None, email_list='',
showard21baa452008-10-21 00:08:39 +0000295 dependencies=[], reboot_before=None, reboot_after=None):
jadmanski0afbb632008-06-06 21:10:57 +0000296 """\
297 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000298
jadmanski0afbb632008-06-06 21:10:57 +0000299 priority: Low, Medium, High, Urgent
300 control_file: contents of control file
301 control_type: type of control file, Client or Server
showard2bab8f42008-11-12 18:15:22 +0000302 synch_count: how many machines the job uses per autoserv execution.
303 synch_count == 1 means the job is asynchronous.
jadmanski0afbb632008-06-06 21:10:57 +0000304 hosts: list of hosts to run job on
305 meta_hosts: list where each entry is a label name, and for each entry
306 one host will be chosen from that label to run the job
307 on.
showard3bb499f2008-07-03 19:42:20 +0000308 timeout: hours until job times out
showard542e8402008-09-19 20:16:18 +0000309 email_list: string containing emails to mail when the job is done
showard989f25d2008-10-01 11:38:11 +0000310 dependencies: list of label names on which this job depends
mbligh94ae6d62009-02-03 17:47:49 +0000311 reboot_before: Never, If dirty, or Always
312 reboot_after: Never, If all tests passed, or Always
jadmanski0afbb632008-06-06 21:10:57 +0000313 """
showard3bb499f2008-07-03 19:42:20 +0000314
315 if timeout is None:
316 timeout=global_config.global_config.get_config_value(
317 'AUTOTEST_WEB', 'job_timeout_default')
318
showardff901382008-07-07 23:22:16 +0000319 owner = thread_local.get_user().login
jadmanski0afbb632008-06-06 21:10:57 +0000320 # input validation
showardb8471e32008-07-03 19:51:08 +0000321 if not hosts and not meta_hosts and not one_time_hosts:
mblighec5546d2008-06-16 16:51:28 +0000322 raise model_logic.ValidationError({
showardb8471e32008-07-03 19:51:08 +0000323 'arguments' : "You must pass at least one of 'hosts', "
324 "'meta_hosts', or 'one_time_hosts'"
jadmanski0afbb632008-06-06 21:10:57 +0000325 })
mblighe8819cd2008-02-15 16:48:40 +0000326
showard989f25d2008-10-01 11:38:11 +0000327 labels_by_name = dict((label.name, label)
328 for label in models.Label.objects.all())
showardba872902008-06-28 00:51:08 +0000329
jadmanski0afbb632008-06-06 21:10:57 +0000330 # convert hostnames & meta hosts to host/label objects
showardbe3ec042008-11-12 18:16:07 +0000331 host_objects = models.Host.smart_get_bulk(hosts or [])
showard989f25d2008-10-01 11:38:11 +0000332 metahost_objects = []
333 metahost_counts = {}
jadmanski0afbb632008-06-06 21:10:57 +0000334 for label in meta_hosts or []:
showard25087ac2008-11-24 22:12:03 +0000335 if label not in labels_by_name:
336 raise model_logic.ValidationError(
337 {'meta_hosts' : 'Label "%s" not found' % label})
showard989f25d2008-10-01 11:38:11 +0000338 this_label = labels_by_name[label]
339 metahost_objects.append(this_label)
340 metahost_counts.setdefault(this_label, 0)
341 metahost_counts[this_label] += 1
showardb8471e32008-07-03 19:51:08 +0000342 for host in one_time_hosts or []:
343 this_host = models.Host.create_one_time_host(host)
344 host_objects.append(this_host)
showardba872902008-06-28 00:51:08 +0000345
showard65c267d2009-01-20 23:24:47 +0000346 all_host_objects = host_objects + metahost_objects
347
showardba872902008-06-28 00:51:08 +0000348 # check that each metahost request has enough hosts under the label
showard989f25d2008-10-01 11:38:11 +0000349 for label, requested_count in metahost_counts.iteritems():
350 available_count = label.host_set.count()
351 if requested_count > available_count:
352 error = ("You have requested %d %s's, but there are only %d."
353 % (requested_count, label.name, available_count))
354 raise model_logic.ValidationError({'meta_hosts' : error})
mblighe8819cd2008-02-15 16:48:40 +0000355
showard65c267d2009-01-20 23:24:47 +0000356 if synch_count is not None and synch_count > len(all_host_objects):
357 raise model_logic.ValidationError(
358 {'hosts': 'only %d hosts provided for job with synch_count = %d'
359 % (len(all_host_objects), synch_count)})
360
showard989f25d2008-10-01 11:38:11 +0000361 rpc_utils.check_job_dependencies(host_objects, dependencies)
362 dependency_labels = [labels_by_name[label_name]
363 for label_name in dependencies]
364
jadmanski0afbb632008-06-06 21:10:57 +0000365 job = models.Job.create(owner=owner, name=name, priority=priority,
366 control_file=control_file,
367 control_type=control_type,
showard2bab8f42008-11-12 18:15:22 +0000368 synch_count=synch_count,
showard65c267d2009-01-20 23:24:47 +0000369 hosts=all_host_objects,
showard909c7a62008-07-15 21:52:38 +0000370 timeout=timeout,
showard542e8402008-09-19 20:16:18 +0000371 run_verify=run_verify,
showard989f25d2008-10-01 11:38:11 +0000372 email_list=email_list.strip(),
showard21baa452008-10-21 00:08:39 +0000373 dependencies=dependency_labels,
374 reboot_before=reboot_before,
375 reboot_after=reboot_after)
showard65c267d2009-01-20 23:24:47 +0000376 job.queue(all_host_objects)
jadmanski0afbb632008-06-06 21:10:57 +0000377 return job.id
mblighe8819cd2008-02-15 16:48:40 +0000378
379
showard9dbdcda2008-10-14 17:34:36 +0000380def abort_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000381 """\
showard9dbdcda2008-10-14 17:34:36 +0000382 Abort a set of host queue entries.
jadmanski0afbb632008-06-06 21:10:57 +0000383 """
showard9dbdcda2008-10-14 17:34:36 +0000384 query = models.HostQueueEntry.query_objects(filter_data)
showard0c185192009-01-16 03:07:57 +0000385 query = query.filter(complete=False)
showarddc817512008-11-12 18:16:41 +0000386 models.AclGroup.check_abort_permissions(query)
showard9dbdcda2008-10-14 17:34:36 +0000387 host_queue_entries = list(query.select_related())
showard2bab8f42008-11-12 18:15:22 +0000388 rpc_utils.check_abort_synchronous_jobs(host_queue_entries)
mblighe8819cd2008-02-15 16:48:40 +0000389
showard9dbdcda2008-10-14 17:34:36 +0000390 user = thread_local.get_user()
391 for queue_entry in host_queue_entries:
392 queue_entry.abort(user)
showard9d821ab2008-07-11 16:54:29 +0000393
394
mblighe8819cd2008-02-15 16:48:40 +0000395def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000396 """\
397 Extra filter args for get_jobs:
398 -not_yet_run: Include only jobs that have not yet started running.
399 -running: Include only jobs that have start running but for which not
400 all hosts have completed.
401 -finished: Include only jobs for which all hosts have completed (or
402 aborted).
403 At most one of these three fields should be specified.
404 """
405 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
406 running,
407 finished)
showard989f25d2008-10-01 11:38:11 +0000408 jobs = models.Job.list_objects(filter_data)
409 models.Job.objects.populate_dependencies(jobs)
410 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000411
412
413def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000414 **filter_data):
415 """\
416 See get_jobs() for documentation of extra filter parameters.
417 """
418 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
419 running,
420 finished)
421 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000422
423
mblighe8819cd2008-02-15 16:48:40 +0000424def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000425 """\
showarda8709c52008-07-03 19:44:54 +0000426 Like get_jobs(), but adds a 'status_counts' field, which is a dictionary
jadmanski0afbb632008-06-06 21:10:57 +0000427 mapping status strings to the number of hosts currently with that
428 status, i.e. {'Queued' : 4, 'Running' : 2}.
429 """
430 jobs = get_jobs(**filter_data)
431 ids = [job['id'] for job in jobs]
432 all_status_counts = models.Job.objects.get_status_counts(ids)
433 for job in jobs:
434 job['status_counts'] = all_status_counts[job['id']]
435 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000436
437
showard945072f2008-09-03 20:34:59 +0000438def get_info_for_clone(id, preserve_metahosts):
showarda8709c52008-07-03 19:44:54 +0000439 """\
440 Retrieves all the information needed to clone a job.
441 """
442 info = {}
443 job = models.Job.objects.get(id=id)
444 query = job.hostqueueentry_set.filter(deleted=False)
showard945072f2008-09-03 20:34:59 +0000445
446 hosts = []
447 meta_hosts = []
448
449 # For each queue entry, if the entry contains a host, add the entry into the
450 # hosts list if either:
451 # It is not a metahost.
452 # It was an assigned metahost, and the user wants to keep the specific
453 # assignments.
454 # Otherwise, add the metahost to the metahosts list.
455 for queue_entry in query:
456 if (queue_entry.host and (preserve_metahosts
457 or not queue_entry.meta_host)):
458 hosts.append(queue_entry.host)
459 else:
460 meta_hosts.append(queue_entry.meta_host.name)
461
showardd9992fe2008-07-31 02:15:03 +0000462 host_dicts = []
showarda8709c52008-07-03 19:44:54 +0000463
showardd9992fe2008-07-31 02:15:03 +0000464 for host in hosts:
showardd9992fe2008-07-31 02:15:03 +0000465 # one-time host
466 if host.invalid:
showardbad4f2d2008-08-15 18:13:47 +0000467 host_dict = {}
468 host_dict['hostname'] = host.hostname
469 host_dict['id'] = host.id
showardd9992fe2008-07-31 02:15:03 +0000470 host_dict['platform'] = '(one-time host)'
471 host_dict['locked_text'] = ''
showardd9992fe2008-07-31 02:15:03 +0000472 else:
showardbad4f2d2008-08-15 18:13:47 +0000473 host_dict = get_hosts(id=host.id)[0]
474 other_labels = host_dict['labels']
475 if host_dict['platform']:
476 other_labels.remove(host_dict['platform'])
477 host_dict['other_labels'] = ', '.join(other_labels)
showardd9992fe2008-07-31 02:15:03 +0000478 host_dicts.append(host_dict)
showarda8709c52008-07-03 19:44:54 +0000479
480 meta_host_counts = {}
481 for meta_host in meta_hosts:
482 meta_host_counts.setdefault(meta_host, 0)
483 meta_host_counts[meta_host] += 1
484
485 info['job'] = job.get_object_dict()
showard2e9415f2008-12-05 19:48:38 +0000486 info['job']['dependencies'] = [label.name for label
487 in job.dependency_labels.all()]
showarda8709c52008-07-03 19:44:54 +0000488 info['meta_host_counts'] = meta_host_counts
showardd9992fe2008-07-31 02:15:03 +0000489 info['hosts'] = host_dicts
showarda8709c52008-07-03 19:44:54 +0000490
491 return rpc_utils.prepare_for_serialization(info)
492
493
showard34dc5fa2008-04-24 20:58:40 +0000494# host queue entries
495
496def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000497 """\
498 TODO
499 """
500 query = models.HostQueueEntry.query_objects(filter_data)
501 all_dicts = []
502 for queue_entry in query.select_related():
503 entry_dict = queue_entry.get_object_dict()
504 if entry_dict['host'] is not None:
505 entry_dict['host'] = queue_entry.host.get_object_dict()
506 entry_dict['job'] = queue_entry.job.get_object_dict()
507 all_dicts.append(entry_dict)
508 return rpc_utils.prepare_for_serialization(all_dicts)
showard34dc5fa2008-04-24 20:58:40 +0000509
510
511def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000512 """\
513 Get the number of host queue entries associated with this job.
514 """
515 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000516
517
showard1e935f12008-07-11 00:11:36 +0000518def get_hqe_percentage_complete(**filter_data):
519 """
520 Computes the percentage of host queue entries matching the given filter data
521 that are complete.
522 """
523 query = models.HostQueueEntry.query_objects(filter_data)
524 complete_count = query.filter(complete=True).count()
525 total_count = query.count()
526 if total_count == 0:
527 return 1
528 return float(complete_count) / total_count
529
530
mblighe8819cd2008-02-15 16:48:40 +0000531# other
532
showarde0b63622008-08-04 20:58:47 +0000533def echo(data=""):
534 """\
535 Returns a passed in string. For doing a basic test to see if RPC calls
536 can successfully be made.
537 """
538 return data
539
540
mblighe8819cd2008-02-15 16:48:40 +0000541def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000542 """\
543 Returns a dictionary containing a bunch of data that shouldn't change
544 often and is otherwise inaccessible. This includes:
545 priorities: list of job priority choices
546 default_priority: default priority value for new jobs
547 users: sorted list of all users
548 labels: sorted list of all labels
549 tests: sorted list of all tests
showard2b9a88b2008-06-13 20:55:03 +0000550 profilers: sorted list of all profilers
jadmanski0afbb632008-06-06 21:10:57 +0000551 user_login: logged-in username
552 host_statuses: sorted list of possible Host statuses
553 job_statuses: sorted list of possible HostQueueEntry statuses
554 """
showard21baa452008-10-21 00:08:39 +0000555
556 job_fields = models.Job.get_field_dict()
557
jadmanski0afbb632008-06-06 21:10:57 +0000558 result = {}
559 result['priorities'] = models.Job.Priority.choices()
showard21baa452008-10-21 00:08:39 +0000560 default_priority = job_fields['priority'].default
jadmanski0afbb632008-06-06 21:10:57 +0000561 default_string = models.Job.Priority.get_string(default_priority)
562 result['default_priority'] = default_string
563 result['users'] = get_users(sort_by=['login'])
564 result['labels'] = get_labels(sort_by=['-platform', 'name'])
565 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +0000566 result['profilers'] = get_profilers(sort_by=['name'])
showard0fc38302008-10-23 00:44:07 +0000567 result['current_user'] = rpc_utils.prepare_for_serialization(
568 thread_local.get_user().get_object_dict())
showard2b9a88b2008-06-13 20:55:03 +0000569 result['host_statuses'] = sorted(models.Host.Status.names)
mbligh5a198b92008-12-11 19:33:29 +0000570 result['job_statuses'] = sorted(models.HostQueueEntry.Status.names)
showardb1e51872008-10-07 11:08:18 +0000571 result['job_timeout_default'] = models.Job.DEFAULT_TIMEOUT
showard0fc38302008-10-23 00:44:07 +0000572 result['reboot_before_options'] = models.RebootBefore.names
573 result['reboot_after_options'] = models.RebootAfter.names
showard8fbae652009-01-20 23:23:10 +0000574 result['motd'] = rpc_utils.get_motd()
showard8ac29b42008-07-17 17:01:55 +0000575
showard95128e52008-08-04 20:59:34 +0000576 result['status_dictionary'] = {"Abort": "Abort",
577 "Aborted": "Aborted",
showard8ac29b42008-07-17 17:01:55 +0000578 "Verifying": "Verifying Host",
579 "Pending": "Waiting on other hosts",
580 "Running": "Running autoserv",
581 "Completed": "Autoserv completed",
582 "Failed": "Failed to complete",
583 "Aborting": "Abort in progress",
showardd823b362008-07-24 16:35:46 +0000584 "Queued": "Queued",
showard5deb6772008-11-04 21:54:33 +0000585 "Starting": "Next in host's queue",
586 "Stopped": "Other host(s) failed verify",
587 "Parsing": "Awaiting parse of final results"}
jadmanski0afbb632008-06-06 21:10:57 +0000588 return result