blob: 6beb3be400e8b662b5c2bb7cbc9b91289e0edee7 [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
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
34from frontend.afe import readonly_connection
showard3bb499f2008-07-03 19:42:20 +000035from autotest_lib.client.common_lib import global_config
36
mblighe8819cd2008-02-15 16:48:40 +000037
38# labels
39
showard989f25d2008-10-01 11:38:11 +000040def add_label(name, kernel_config=None, platform=None, only_if_needed=None):
jadmanski0afbb632008-06-06 21:10:57 +000041 return models.Label.add_object(name=name, kernel_config=kernel_config,
showard989f25d2008-10-01 11:38:11 +000042 platform=platform,
43 only_if_needed=only_if_needed).id
mblighe8819cd2008-02-15 16:48:40 +000044
45
46def modify_label(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +000047 models.Label.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +000048
49
50def delete_label(id):
jadmanski0afbb632008-06-06 21:10:57 +000051 models.Label.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +000052
53
showardbbabf502008-06-06 00:02:02 +000054def label_add_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.add(*host_objs)
showardbbabf502008-06-06 00:02:02 +000057
58
59def label_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +000060 host_objs = [models.Host.smart_get(host) for host in hosts]
61 models.Label.smart_get(id).host_set.remove(*host_objs)
showardbbabf502008-06-06 00:02:02 +000062
63
mblighe8819cd2008-02-15 16:48:40 +000064def get_labels(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +000065 return rpc_utils.prepare_for_serialization(
66 models.Label.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +000067
68
69# hosts
70
showarddf062562008-07-03 19:56:37 +000071def add_host(hostname, status=None, locked=None, protection=None):
jadmanski0afbb632008-06-06 21:10:57 +000072 return models.Host.add_object(hostname=hostname, status=status,
showarddf062562008-07-03 19:56:37 +000073 locked=locked, protection=protection).id
mblighe8819cd2008-02-15 16:48:40 +000074
75
76def modify_host(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +000077 models.Host.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +000078
79
80def host_add_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.add(*labels)
mblighe8819cd2008-02-15 16:48:40 +000083
84
85def host_remove_labels(id, labels):
jadmanski0afbb632008-06-06 21:10:57 +000086 labels = [models.Label.smart_get(label) for label in labels]
87 models.Host.smart_get(id).labels.remove(*labels)
mblighe8819cd2008-02-15 16:48:40 +000088
89
90def delete_host(id):
jadmanski0afbb632008-06-06 21:10:57 +000091 models.Host.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +000092
93
showard8e3aa5e2008-04-08 19:42:32 +000094def get_hosts(multiple_labels=[], **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.
98 """
99 filter_data['extra_args'] = (
100 rpc_utils.extra_host_filters(multiple_labels))
101 hosts = models.Host.list_objects(filter_data)
102 for host in hosts:
103 host_obj = models.Host.objects.get(id=host['id'])
104 host['labels'] = [label.name
105 for label in host_obj.labels.all()]
106 platform = host_obj.platform()
107 host['platform'] = platform and platform.name or None
108 return rpc_utils.prepare_for_serialization(hosts)
mblighe8819cd2008-02-15 16:48:40 +0000109
110
showard8e3aa5e2008-04-08 19:42:32 +0000111def get_num_hosts(multiple_labels=[], **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000112 filter_data['extra_args'] = (
113 rpc_utils.extra_host_filters(multiple_labels))
114 return models.Host.query_count(filter_data)
showard1385b162008-03-13 15:59:40 +0000115
mblighe8819cd2008-02-15 16:48:40 +0000116
117# tests
118
showard909c7a62008-07-15 21:52:38 +0000119def add_test(name, test_type, path, author=None, dependencies=None,
showard3d9899a2008-07-31 02:11:58 +0000120 experimental=True, run_verify=None, test_class=None,
showard909c7a62008-07-15 21:52:38 +0000121 test_time=None, test_category=None, description=None,
122 sync_count=1):
jadmanski0afbb632008-06-06 21:10:57 +0000123 return models.Test.add_object(name=name, test_type=test_type, path=path,
showard909c7a62008-07-15 21:52:38 +0000124 author=author, dependencies=dependencies,
125 experimental=experimental,
126 run_verify=run_verify, test_time=test_time,
127 test_category=test_category,
128 sync_count=sync_count,
jadmanski0afbb632008-06-06 21:10:57 +0000129 test_class=test_class,
130 description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000131
132
133def modify_test(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000134 models.Test.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000135
136
137def delete_test(id):
jadmanski0afbb632008-06-06 21:10:57 +0000138 models.Test.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000139
140
141def get_tests(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000142 return rpc_utils.prepare_for_serialization(
143 models.Test.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000144
145
showard2b9a88b2008-06-13 20:55:03 +0000146# profilers
147
148def add_profiler(name, description=None):
149 return models.Profiler.add_object(name=name, description=description).id
150
151
152def modify_profiler(id, **data):
153 models.Profiler.smart_get(id).update_object(data)
154
155
156def delete_profiler(id):
157 models.Profiler.smart_get(id).delete()
158
159
160def get_profilers(**filter_data):
161 return rpc_utils.prepare_for_serialization(
162 models.Profiler.list_objects(filter_data))
163
164
mblighe8819cd2008-02-15 16:48:40 +0000165# users
166
167def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000168 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000169
170
171def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000172 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000173
174
175def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000176 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000177
178
179def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000180 return rpc_utils.prepare_for_serialization(
181 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000182
183
184# acl groups
185
186def add_acl_group(name, description=None):
showard04f2cd82008-07-25 20:53:31 +0000187 group = models.AclGroup.add_object(name=name, description=description)
188 group.users.add(thread_local.get_user())
189 return group.id
mblighe8819cd2008-02-15 16:48:40 +0000190
191
192def modify_acl_group(id, **data):
showard04f2cd82008-07-25 20:53:31 +0000193 group = models.AclGroup.smart_get(id)
194 group.check_for_acl_violation_acl_group()
195 group.update_object(data)
196 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000197
198
199def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000200 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000201 group.check_for_acl_violation_acl_group()
202 users = [models.User.smart_get(user) for user in users]
jadmanski0afbb632008-06-06 21:10:57 +0000203 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000204
205
206def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000207 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000208 group.check_for_acl_violation_acl_group()
209 users = [models.User.smart_get(user) for user in users]
jadmanski0afbb632008-06-06 21:10:57 +0000210 group.users.remove(*users)
showard04f2cd82008-07-25 20:53:31 +0000211 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000212
213
214def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000215 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000216 group.check_for_acl_violation_acl_group()
217 hosts = [models.Host.smart_get(host) for host in hosts]
jadmanski0afbb632008-06-06 21:10:57 +0000218 group.hosts.add(*hosts)
showard08f981b2008-06-24 21:59:03 +0000219 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000220
221
222def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000223 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000224 group.check_for_acl_violation_acl_group()
225 hosts = [models.Host.smart_get(host) for host in hosts]
jadmanski0afbb632008-06-06 21:10:57 +0000226 group.hosts.remove(*hosts)
showard08f981b2008-06-24 21:59:03 +0000227 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000228
229
230def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000231 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000232
233
234def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000235 acl_groups = models.AclGroup.list_objects(filter_data)
236 for acl_group in acl_groups:
237 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
238 acl_group['users'] = [user.login
239 for user in acl_group_obj.users.all()]
240 acl_group['hosts'] = [host.hostname
241 for host in acl_group_obj.hosts.all()]
242 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000243
244
245# jobs
246
showard2b9a88b2008-06-13 20:55:03 +0000247def generate_control_file(tests, kernel=None, label=None, profilers=[]):
jadmanski0afbb632008-06-06 21:10:57 +0000248 """\
249 Generates a client-side control file to load a kernel and run a set of
showard989f25d2008-10-01 11:38:11 +0000250 tests. Returns a dict with the following keys:
jadmanski0afbb632008-06-06 21:10:57 +0000251 control_file - the control file text
252 is_server - is the control file a server-side control file?
253 is_synchronous - should the control file be run synchronously?
showard989f25d2008-10-01 11:38:11 +0000254 dependencies - a list of the names of labels on which the job depends
mblighe8819cd2008-02-15 16:48:40 +0000255
jadmanski0afbb632008-06-06 21:10:57 +0000256 tests: list of tests to run
257 kernel: kernel to install in generated control file
258 label: name of label to grab kernel config from
showard2b9a88b2008-06-13 20:55:03 +0000259 profilers: list of profilers to activate during the job
jadmanski0afbb632008-06-06 21:10:57 +0000260 """
261 if not tests:
showard989f25d2008-10-01 11:38:11 +0000262 return dict(control_file='', is_server=False, is_synchronous=False,
263 dependencies=[])
mblighe8819cd2008-02-15 16:48:40 +0000264
showard989f25d2008-10-01 11:38:11 +0000265 cf_info, test_objects, profiler_objects, label = (
showard2b9a88b2008-06-13 20:55:03 +0000266 rpc_utils.prepare_generate_control_file(tests, kernel, label,
267 profilers))
showard989f25d2008-10-01 11:38:11 +0000268 cf_info['control_file'] = control_file.generate_control(
269 tests=test_objects, kernel=kernel, platform=label,
270 profilers=profiler_objects, is_server=cf_info['is_server'])
271 return cf_info
mblighe8819cd2008-02-15 16:48:40 +0000272
273
showard3bb499f2008-07-03 19:42:20 +0000274def create_job(name, priority, control_file, control_type, timeout=None,
showardb8471e32008-07-03 19:51:08 +0000275 is_synchronous=None, hosts=None, meta_hosts=None,
showard989f25d2008-10-01 11:38:11 +0000276 run_verify=True, one_time_hosts=None, email_list='',
277 dependencies=[]):
jadmanski0afbb632008-06-06 21:10:57 +0000278 """\
279 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000280
jadmanski0afbb632008-06-06 21:10:57 +0000281 priority: Low, Medium, High, Urgent
282 control_file: contents of control file
283 control_type: type of control file, Client or Server
284 is_synchronous: boolean indicating if a job is synchronous
285 hosts: list of hosts to run job on
286 meta_hosts: list where each entry is a label name, and for each entry
287 one host will be chosen from that label to run the job
288 on.
showard3bb499f2008-07-03 19:42:20 +0000289 timeout: hours until job times out
showard542e8402008-09-19 20:16:18 +0000290 email_list: string containing emails to mail when the job is done
showard989f25d2008-10-01 11:38:11 +0000291 dependencies: list of label names on which this job depends
jadmanski0afbb632008-06-06 21:10:57 +0000292 """
showard3bb499f2008-07-03 19:42:20 +0000293
294 if timeout is None:
295 timeout=global_config.global_config.get_config_value(
296 'AUTOTEST_WEB', 'job_timeout_default')
297
showardff901382008-07-07 23:22:16 +0000298 owner = thread_local.get_user().login
jadmanski0afbb632008-06-06 21:10:57 +0000299 # input validation
showardb8471e32008-07-03 19:51:08 +0000300 if not hosts and not meta_hosts and not one_time_hosts:
mblighec5546d2008-06-16 16:51:28 +0000301 raise model_logic.ValidationError({
showardb8471e32008-07-03 19:51:08 +0000302 'arguments' : "You must pass at least one of 'hosts', "
303 "'meta_hosts', or 'one_time_hosts'"
jadmanski0afbb632008-06-06 21:10:57 +0000304 })
mblighe8819cd2008-02-15 16:48:40 +0000305
showard989f25d2008-10-01 11:38:11 +0000306 labels_by_name = dict((label.name, label)
307 for label in models.Label.objects.all())
showardba872902008-06-28 00:51:08 +0000308
jadmanski0afbb632008-06-06 21:10:57 +0000309 # convert hostnames & meta hosts to host/label objects
310 host_objects = []
showard989f25d2008-10-01 11:38:11 +0000311 metahost_objects = []
312 metahost_counts = {}
jadmanski0afbb632008-06-06 21:10:57 +0000313 for host in hosts or []:
314 this_host = models.Host.smart_get(host)
315 host_objects.append(this_host)
316 for label in meta_hosts or []:
showard989f25d2008-10-01 11:38:11 +0000317 this_label = labels_by_name[label]
318 metahost_objects.append(this_label)
319 metahost_counts.setdefault(this_label, 0)
320 metahost_counts[this_label] += 1
showardb8471e32008-07-03 19:51:08 +0000321 for host in one_time_hosts or []:
322 this_host = models.Host.create_one_time_host(host)
323 host_objects.append(this_host)
showardba872902008-06-28 00:51:08 +0000324
325 # check that each metahost request has enough hosts under the label
showard989f25d2008-10-01 11:38:11 +0000326 for label, requested_count in metahost_counts.iteritems():
327 available_count = label.host_set.count()
328 if requested_count > available_count:
329 error = ("You have requested %d %s's, but there are only %d."
330 % (requested_count, label.name, available_count))
331 raise model_logic.ValidationError({'meta_hosts' : error})
mblighe8819cd2008-02-15 16:48:40 +0000332
jadmanski0afbb632008-06-06 21:10:57 +0000333 # default is_synchronous to some appropriate value
334 ControlType = models.Job.ControlType
335 control_type = ControlType.get_value(control_type)
336 if is_synchronous is None:
337 is_synchronous = (control_type == ControlType.SERVER)
338 # convert the synch flag to an actual type
339 if is_synchronous:
340 synch_type = models.Test.SynchType.SYNCHRONOUS
341 else:
342 synch_type = models.Test.SynchType.ASYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000343
showard989f25d2008-10-01 11:38:11 +0000344 rpc_utils.check_job_dependencies(host_objects, dependencies)
345 dependency_labels = [labels_by_name[label_name]
346 for label_name in dependencies]
347
jadmanski0afbb632008-06-06 21:10:57 +0000348 job = models.Job.create(owner=owner, name=name, priority=priority,
349 control_file=control_file,
350 control_type=control_type,
351 synch_type=synch_type,
showard989f25d2008-10-01 11:38:11 +0000352 hosts=host_objects + metahost_objects,
showard909c7a62008-07-15 21:52:38 +0000353 timeout=timeout,
showard542e8402008-09-19 20:16:18 +0000354 run_verify=run_verify,
showard989f25d2008-10-01 11:38:11 +0000355 email_list=email_list.strip(),
356 dependencies=dependency_labels)
357 job.queue(host_objects + metahost_objects)
jadmanski0afbb632008-06-06 21:10:57 +0000358 return job.id
mblighe8819cd2008-02-15 16:48:40 +0000359
360
mblighe8819cd2008-02-15 16:48:40 +0000361def abort_job(id):
jadmanski0afbb632008-06-06 21:10:57 +0000362 """\
363 Abort the job with the given id number.
showard9d821ab2008-07-11 16:54:29 +0000364
365 TODO: this method is deprecated in favor of abort_jobs(). We need
366 to eventually remove this rpc call entirely.
jadmanski0afbb632008-06-06 21:10:57 +0000367 """
368 job = models.Job.objects.get(id=id)
369 job.abort()
mblighe8819cd2008-02-15 16:48:40 +0000370
371
showard9d821ab2008-07-11 16:54:29 +0000372def abort_jobs(job_ids):
373 """\
374 Abort a list of jobs.
375 """
376 for job in models.Job.objects.in_bulk(job_ids).values():
377 job.abort()
378
379
mblighe8819cd2008-02-15 16:48:40 +0000380def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000381 """\
382 Extra filter args for get_jobs:
383 -not_yet_run: Include only jobs that have not yet started running.
384 -running: Include only jobs that have start running but for which not
385 all hosts have completed.
386 -finished: Include only jobs for which all hosts have completed (or
387 aborted).
388 At most one of these three fields should be specified.
389 """
390 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
391 running,
392 finished)
showard989f25d2008-10-01 11:38:11 +0000393 jobs = models.Job.list_objects(filter_data)
394 models.Job.objects.populate_dependencies(jobs)
395 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000396
397
398def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000399 **filter_data):
400 """\
401 See get_jobs() for documentation of extra filter parameters.
402 """
403 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
404 running,
405 finished)
406 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000407
408
mblighe8819cd2008-02-15 16:48:40 +0000409def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000410 """\
showarda8709c52008-07-03 19:44:54 +0000411 Like get_jobs(), but adds a 'status_counts' field, which is a dictionary
jadmanski0afbb632008-06-06 21:10:57 +0000412 mapping status strings to the number of hosts currently with that
413 status, i.e. {'Queued' : 4, 'Running' : 2}.
414 """
415 jobs = get_jobs(**filter_data)
416 ids = [job['id'] for job in jobs]
417 all_status_counts = models.Job.objects.get_status_counts(ids)
418 for job in jobs:
419 job['status_counts'] = all_status_counts[job['id']]
420 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000421
422
showard945072f2008-09-03 20:34:59 +0000423def get_info_for_clone(id, preserve_metahosts):
showarda8709c52008-07-03 19:44:54 +0000424 """\
425 Retrieves all the information needed to clone a job.
426 """
427 info = {}
428 job = models.Job.objects.get(id=id)
429 query = job.hostqueueentry_set.filter(deleted=False)
showard945072f2008-09-03 20:34:59 +0000430
431 hosts = []
432 meta_hosts = []
433
434 # For each queue entry, if the entry contains a host, add the entry into the
435 # hosts list if either:
436 # It is not a metahost.
437 # It was an assigned metahost, and the user wants to keep the specific
438 # assignments.
439 # Otherwise, add the metahost to the metahosts list.
440 for queue_entry in query:
441 if (queue_entry.host and (preserve_metahosts
442 or not queue_entry.meta_host)):
443 hosts.append(queue_entry.host)
444 else:
445 meta_hosts.append(queue_entry.meta_host.name)
446
showardd9992fe2008-07-31 02:15:03 +0000447 host_dicts = []
showarda8709c52008-07-03 19:44:54 +0000448
showardd9992fe2008-07-31 02:15:03 +0000449 for host in hosts:
showardd9992fe2008-07-31 02:15:03 +0000450 # one-time host
451 if host.invalid:
showardbad4f2d2008-08-15 18:13:47 +0000452 host_dict = {}
453 host_dict['hostname'] = host.hostname
454 host_dict['id'] = host.id
showardd9992fe2008-07-31 02:15:03 +0000455 host_dict['platform'] = '(one-time host)'
456 host_dict['locked_text'] = ''
showardd9992fe2008-07-31 02:15:03 +0000457 else:
showardbad4f2d2008-08-15 18:13:47 +0000458 host_dict = get_hosts(id=host.id)[0]
459 other_labels = host_dict['labels']
460 if host_dict['platform']:
461 other_labels.remove(host_dict['platform'])
462 host_dict['other_labels'] = ', '.join(other_labels)
showardd9992fe2008-07-31 02:15:03 +0000463 host_dicts.append(host_dict)
showarda8709c52008-07-03 19:44:54 +0000464
465 meta_host_counts = {}
466 for meta_host in meta_hosts:
467 meta_host_counts.setdefault(meta_host, 0)
468 meta_host_counts[meta_host] += 1
469
470 info['job'] = job.get_object_dict()
471 info['meta_host_counts'] = meta_host_counts
showardd9992fe2008-07-31 02:15:03 +0000472 info['hosts'] = host_dicts
showarda8709c52008-07-03 19:44:54 +0000473
474 return rpc_utils.prepare_for_serialization(info)
475
476
showard34dc5fa2008-04-24 20:58:40 +0000477# host queue entries
478
479def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000480 """\
481 TODO
482 """
483 query = models.HostQueueEntry.query_objects(filter_data)
484 all_dicts = []
485 for queue_entry in query.select_related():
486 entry_dict = queue_entry.get_object_dict()
487 if entry_dict['host'] is not None:
488 entry_dict['host'] = queue_entry.host.get_object_dict()
489 entry_dict['job'] = queue_entry.job.get_object_dict()
490 all_dicts.append(entry_dict)
491 return rpc_utils.prepare_for_serialization(all_dicts)
showard34dc5fa2008-04-24 20:58:40 +0000492
493
494def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000495 """\
496 Get the number of host queue entries associated with this job.
497 """
498 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000499
500
showard1e935f12008-07-11 00:11:36 +0000501def get_hqe_percentage_complete(**filter_data):
502 """
503 Computes the percentage of host queue entries matching the given filter data
504 that are complete.
505 """
506 query = models.HostQueueEntry.query_objects(filter_data)
507 complete_count = query.filter(complete=True).count()
508 total_count = query.count()
509 if total_count == 0:
510 return 1
511 return float(complete_count) / total_count
512
513
mblighe8819cd2008-02-15 16:48:40 +0000514# other
515
showarde0b63622008-08-04 20:58:47 +0000516def echo(data=""):
517 """\
518 Returns a passed in string. For doing a basic test to see if RPC calls
519 can successfully be made.
520 """
521 return data
522
523
mblighe8819cd2008-02-15 16:48:40 +0000524def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000525 """\
526 Returns a dictionary containing a bunch of data that shouldn't change
527 often and is otherwise inaccessible. This includes:
528 priorities: list of job priority choices
529 default_priority: default priority value for new jobs
530 users: sorted list of all users
531 labels: sorted list of all labels
532 tests: sorted list of all tests
showard2b9a88b2008-06-13 20:55:03 +0000533 profilers: sorted list of all profilers
jadmanski0afbb632008-06-06 21:10:57 +0000534 user_login: logged-in username
535 host_statuses: sorted list of possible Host statuses
536 job_statuses: sorted list of possible HostQueueEntry statuses
537 """
538 result = {}
539 result['priorities'] = models.Job.Priority.choices()
540 default_priority = models.Job.get_field_dict()['priority'].default
541 default_string = models.Job.Priority.get_string(default_priority)
542 result['default_priority'] = default_string
543 result['users'] = get_users(sort_by=['login'])
544 result['labels'] = get_labels(sort_by=['-platform', 'name'])
545 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +0000546 result['profilers'] = get_profilers(sort_by=['name'])
showardff901382008-07-07 23:22:16 +0000547 result['user_login'] = thread_local.get_user().login
showard2b9a88b2008-06-13 20:55:03 +0000548 result['host_statuses'] = sorted(models.Host.Status.names)
549 result['job_statuses'] = sorted(models.Job.Status.names)
showard3bb499f2008-07-03 19:42:20 +0000550 result['job_timeout_default'] = global_config.global_config.get_config_value(
551 'AUTOTEST_WEB', 'job_timeout_default')
showard8ac29b42008-07-17 17:01:55 +0000552
showard95128e52008-08-04 20:59:34 +0000553 result['status_dictionary'] = {"Abort": "Abort",
554 "Aborted": "Aborted",
showard8ac29b42008-07-17 17:01:55 +0000555 "Verifying": "Verifying Host",
556 "Pending": "Waiting on other hosts",
557 "Running": "Running autoserv",
558 "Completed": "Autoserv completed",
559 "Failed": "Failed to complete",
560 "Aborting": "Abort in progress",
showardd823b362008-07-24 16:35:46 +0000561 "Queued": "Queued",
562 "Starting": "Next in host's queue"}
jadmanski0afbb632008-06-06 21:10:57 +0000563 return result