blob: 4447736b960c880ec10f30a7c171014d00ceab45 [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
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):
jadmanski0afbb632008-06-06 21:10:57 +000054 host_objs = [models.Host.smart_get(host) for host in hosts]
55 models.Label.smart_get(id).host_set.add(*host_objs)
showardbbabf502008-06-06 00:02:02 +000056
57
58def label_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +000059 host_objs = [models.Host.smart_get(host) for host in hosts]
60 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):
jadmanski0afbb632008-06-06 21:10:57 +000080 labels = [models.Label.smart_get(label) for label in labels]
81 models.Host.smart_get(id).labels.add(*labels)
mblighe8819cd2008-02-15 16:48:40 +000082
83
84def host_remove_labels(id, labels):
jadmanski0afbb632008-06-06 21:10:57 +000085 labels = [models.Label.smart_get(label) for label in labels]
86 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
showard8e3aa5e2008-04-08 19:42:32 +000093def get_hosts(multiple_labels=[], **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +000094 """\
95 multiple_labels: match hosts in all of the labels given. Should be a
96 list of label names.
97 """
98 filter_data['extra_args'] = (
99 rpc_utils.extra_host_filters(multiple_labels))
100 hosts = models.Host.list_objects(filter_data)
101 for host in hosts:
102 host_obj = models.Host.objects.get(id=host['id'])
103 host['labels'] = [label.name
104 for label in host_obj.labels.all()]
105 platform = host_obj.platform()
106 host['platform'] = platform and platform.name or None
107 return rpc_utils.prepare_for_serialization(hosts)
mblighe8819cd2008-02-15 16:48:40 +0000108
109
showard8e3aa5e2008-04-08 19:42:32 +0000110def get_num_hosts(multiple_labels=[], **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000111 filter_data['extra_args'] = (
112 rpc_utils.extra_host_filters(multiple_labels))
113 return models.Host.query_count(filter_data)
showard1385b162008-03-13 15:59:40 +0000114
mblighe8819cd2008-02-15 16:48:40 +0000115
116# tests
117
showard909c7a62008-07-15 21:52:38 +0000118def add_test(name, test_type, path, author=None, dependencies=None,
showard3d9899a2008-07-31 02:11:58 +0000119 experimental=True, run_verify=None, test_class=None,
showard909c7a62008-07-15 21:52:38 +0000120 test_time=None, test_category=None, description=None,
121 sync_count=1):
jadmanski0afbb632008-06-06 21:10:57 +0000122 return models.Test.add_object(name=name, test_type=test_type, path=path,
showard909c7a62008-07-15 21:52:38 +0000123 author=author, dependencies=dependencies,
124 experimental=experimental,
125 run_verify=run_verify, test_time=test_time,
126 test_category=test_category,
127 sync_count=sync_count,
jadmanski0afbb632008-06-06 21:10:57 +0000128 test_class=test_class,
129 description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000130
131
132def modify_test(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000133 models.Test.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000134
135
136def delete_test(id):
jadmanski0afbb632008-06-06 21:10:57 +0000137 models.Test.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000138
139
140def get_tests(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000141 return rpc_utils.prepare_for_serialization(
142 models.Test.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000143
144
showard2b9a88b2008-06-13 20:55:03 +0000145# profilers
146
147def add_profiler(name, description=None):
148 return models.Profiler.add_object(name=name, description=description).id
149
150
151def modify_profiler(id, **data):
152 models.Profiler.smart_get(id).update_object(data)
153
154
155def delete_profiler(id):
156 models.Profiler.smart_get(id).delete()
157
158
159def get_profilers(**filter_data):
160 return rpc_utils.prepare_for_serialization(
161 models.Profiler.list_objects(filter_data))
162
163
mblighe8819cd2008-02-15 16:48:40 +0000164# users
165
166def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000167 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000168
169
170def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000171 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000172
173
174def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000175 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000176
177
178def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000179 return rpc_utils.prepare_for_serialization(
180 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000181
182
183# acl groups
184
185def add_acl_group(name, description=None):
showard04f2cd82008-07-25 20:53:31 +0000186 group = models.AclGroup.add_object(name=name, description=description)
187 group.users.add(thread_local.get_user())
188 return group.id
mblighe8819cd2008-02-15 16:48:40 +0000189
190
191def modify_acl_group(id, **data):
showard04f2cd82008-07-25 20:53:31 +0000192 group = models.AclGroup.smart_get(id)
193 group.check_for_acl_violation_acl_group()
194 group.update_object(data)
195 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000196
197
198def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000199 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000200 group.check_for_acl_violation_acl_group()
201 users = [models.User.smart_get(user) for user in users]
jadmanski0afbb632008-06-06 21:10:57 +0000202 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000203
204
205def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000206 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000207 group.check_for_acl_violation_acl_group()
208 users = [models.User.smart_get(user) for user in users]
jadmanski0afbb632008-06-06 21:10:57 +0000209 group.users.remove(*users)
showard04f2cd82008-07-25 20:53:31 +0000210 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000211
212
213def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000214 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000215 group.check_for_acl_violation_acl_group()
216 hosts = [models.Host.smart_get(host) for host in hosts]
jadmanski0afbb632008-06-06 21:10:57 +0000217 group.hosts.add(*hosts)
showard08f981b2008-06-24 21:59:03 +0000218 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000219
220
221def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000222 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000223 group.check_for_acl_violation_acl_group()
224 hosts = [models.Host.smart_get(host) for host in hosts]
jadmanski0afbb632008-06-06 21:10:57 +0000225 group.hosts.remove(*hosts)
showard08f981b2008-06-24 21:59:03 +0000226 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000227
228
229def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000230 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000231
232
233def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000234 acl_groups = models.AclGroup.list_objects(filter_data)
235 for acl_group in acl_groups:
236 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
237 acl_group['users'] = [user.login
238 for user in acl_group_obj.users.all()]
239 acl_group['hosts'] = [host.hostname
240 for host in acl_group_obj.hosts.all()]
241 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000242
243
244# jobs
245
showard2b9a88b2008-06-13 20:55:03 +0000246def generate_control_file(tests, kernel=None, label=None, profilers=[]):
jadmanski0afbb632008-06-06 21:10:57 +0000247 """\
248 Generates a client-side control file to load a kernel and run a set of
showard989f25d2008-10-01 11:38:11 +0000249 tests. Returns a dict with the following keys:
jadmanski0afbb632008-06-06 21:10:57 +0000250 control_file - the control file text
251 is_server - is the control file a server-side control file?
252 is_synchronous - should the control file be run synchronously?
showard989f25d2008-10-01 11:38:11 +0000253 dependencies - a list of the names of labels on which the job depends
mblighe8819cd2008-02-15 16:48:40 +0000254
jadmanski0afbb632008-06-06 21:10:57 +0000255 tests: list of tests to run
256 kernel: kernel to install in generated control file
257 label: name of label to grab kernel config from
showard2b9a88b2008-06-13 20:55:03 +0000258 profilers: list of profilers to activate during the job
jadmanski0afbb632008-06-06 21:10:57 +0000259 """
260 if not tests:
showard989f25d2008-10-01 11:38:11 +0000261 return dict(control_file='', is_server=False, is_synchronous=False,
262 dependencies=[])
mblighe8819cd2008-02-15 16:48:40 +0000263
showard989f25d2008-10-01 11:38:11 +0000264 cf_info, test_objects, profiler_objects, label = (
showard2b9a88b2008-06-13 20:55:03 +0000265 rpc_utils.prepare_generate_control_file(tests, kernel, label,
266 profilers))
showard989f25d2008-10-01 11:38:11 +0000267 cf_info['control_file'] = control_file.generate_control(
268 tests=test_objects, kernel=kernel, platform=label,
269 profilers=profiler_objects, is_server=cf_info['is_server'])
270 return cf_info
mblighe8819cd2008-02-15 16:48:40 +0000271
272
showard3bb499f2008-07-03 19:42:20 +0000273def create_job(name, priority, control_file, control_type, timeout=None,
showardb8471e32008-07-03 19:51:08 +0000274 is_synchronous=None, hosts=None, meta_hosts=None,
showard989f25d2008-10-01 11:38:11 +0000275 run_verify=True, one_time_hosts=None, email_list='',
showard21baa452008-10-21 00:08:39 +0000276 dependencies=[], reboot_before=None, reboot_after=None):
jadmanski0afbb632008-06-06 21:10:57 +0000277 """\
278 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000279
jadmanski0afbb632008-06-06 21:10:57 +0000280 priority: Low, Medium, High, Urgent
281 control_file: contents of control file
282 control_type: type of control file, Client or Server
283 is_synchronous: boolean indicating if a job is synchronous
284 hosts: list of hosts to run job on
285 meta_hosts: list where each entry is a label name, and for each entry
286 one host will be chosen from that label to run the job
287 on.
showard3bb499f2008-07-03 19:42:20 +0000288 timeout: hours until job times out
showard542e8402008-09-19 20:16:18 +0000289 email_list: string containing emails to mail when the job is done
showard989f25d2008-10-01 11:38:11 +0000290 dependencies: list of label names on which this job depends
jadmanski0afbb632008-06-06 21:10:57 +0000291 """
showard3bb499f2008-07-03 19:42:20 +0000292
293 if timeout is None:
294 timeout=global_config.global_config.get_config_value(
295 'AUTOTEST_WEB', 'job_timeout_default')
296
showardff901382008-07-07 23:22:16 +0000297 owner = thread_local.get_user().login
jadmanski0afbb632008-06-06 21:10:57 +0000298 # input validation
showardb8471e32008-07-03 19:51:08 +0000299 if not hosts and not meta_hosts and not one_time_hosts:
mblighec5546d2008-06-16 16:51:28 +0000300 raise model_logic.ValidationError({
showardb8471e32008-07-03 19:51:08 +0000301 'arguments' : "You must pass at least one of 'hosts', "
302 "'meta_hosts', or 'one_time_hosts'"
jadmanski0afbb632008-06-06 21:10:57 +0000303 })
mblighe8819cd2008-02-15 16:48:40 +0000304
showard989f25d2008-10-01 11:38:11 +0000305 labels_by_name = dict((label.name, label)
306 for label in models.Label.objects.all())
showardba872902008-06-28 00:51:08 +0000307
jadmanski0afbb632008-06-06 21:10:57 +0000308 # convert hostnames & meta hosts to host/label objects
309 host_objects = []
showard989f25d2008-10-01 11:38:11 +0000310 metahost_objects = []
311 metahost_counts = {}
jadmanski0afbb632008-06-06 21:10:57 +0000312 for host in hosts or []:
313 this_host = models.Host.smart_get(host)
314 host_objects.append(this_host)
315 for label in meta_hosts or []:
showard989f25d2008-10-01 11:38:11 +0000316 this_label = labels_by_name[label]
317 metahost_objects.append(this_label)
318 metahost_counts.setdefault(this_label, 0)
319 metahost_counts[this_label] += 1
showardb8471e32008-07-03 19:51:08 +0000320 for host in one_time_hosts or []:
321 this_host = models.Host.create_one_time_host(host)
322 host_objects.append(this_host)
showardba872902008-06-28 00:51:08 +0000323
324 # check that each metahost request has enough hosts under the label
showard989f25d2008-10-01 11:38:11 +0000325 for label, requested_count in metahost_counts.iteritems():
326 available_count = label.host_set.count()
327 if requested_count > available_count:
328 error = ("You have requested %d %s's, but there are only %d."
329 % (requested_count, label.name, available_count))
330 raise model_logic.ValidationError({'meta_hosts' : error})
mblighe8819cd2008-02-15 16:48:40 +0000331
jadmanski0afbb632008-06-06 21:10:57 +0000332 # default is_synchronous to some appropriate value
333 ControlType = models.Job.ControlType
334 control_type = ControlType.get_value(control_type)
335 if is_synchronous is None:
336 is_synchronous = (control_type == ControlType.SERVER)
337 # convert the synch flag to an actual type
338 if is_synchronous:
339 synch_type = models.Test.SynchType.SYNCHRONOUS
340 else:
341 synch_type = models.Test.SynchType.ASYNCHRONOUS
mblighe8819cd2008-02-15 16:48:40 +0000342
showard989f25d2008-10-01 11:38:11 +0000343 rpc_utils.check_job_dependencies(host_objects, dependencies)
344 dependency_labels = [labels_by_name[label_name]
345 for label_name in dependencies]
346
jadmanski0afbb632008-06-06 21:10:57 +0000347 job = models.Job.create(owner=owner, name=name, priority=priority,
348 control_file=control_file,
349 control_type=control_type,
350 synch_type=synch_type,
showard989f25d2008-10-01 11:38:11 +0000351 hosts=host_objects + metahost_objects,
showard909c7a62008-07-15 21:52:38 +0000352 timeout=timeout,
showard542e8402008-09-19 20:16:18 +0000353 run_verify=run_verify,
showard989f25d2008-10-01 11:38:11 +0000354 email_list=email_list.strip(),
showard21baa452008-10-21 00:08:39 +0000355 dependencies=dependency_labels,
356 reboot_before=reboot_before,
357 reboot_after=reboot_after)
showard989f25d2008-10-01 11:38:11 +0000358 job.queue(host_objects + metahost_objects)
jadmanski0afbb632008-06-06 21:10:57 +0000359 return job.id
mblighe8819cd2008-02-15 16:48:40 +0000360
361
showard9dbdcda2008-10-14 17:34:36 +0000362def abort_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000363 """\
showard9dbdcda2008-10-14 17:34:36 +0000364 Abort a set of host queue entries.
jadmanski0afbb632008-06-06 21:10:57 +0000365 """
showard9dbdcda2008-10-14 17:34:36 +0000366 query = models.HostQueueEntry.query_objects(filter_data)
367 host_queue_entries = list(query.select_related())
368 models.AclGroup.check_for_acl_violation_queue_entries(host_queue_entries)
mblighe8819cd2008-02-15 16:48:40 +0000369
showard9dbdcda2008-10-14 17:34:36 +0000370 user = thread_local.get_user()
371 for queue_entry in host_queue_entries:
372 queue_entry.abort(user)
showard9d821ab2008-07-11 16:54:29 +0000373
374
mblighe8819cd2008-02-15 16:48:40 +0000375def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000376 """\
377 Extra filter args for get_jobs:
378 -not_yet_run: Include only jobs that have not yet started running.
379 -running: Include only jobs that have start running but for which not
380 all hosts have completed.
381 -finished: Include only jobs for which all hosts have completed (or
382 aborted).
383 At most one of these three fields should be specified.
384 """
385 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
386 running,
387 finished)
showard989f25d2008-10-01 11:38:11 +0000388 jobs = models.Job.list_objects(filter_data)
389 models.Job.objects.populate_dependencies(jobs)
390 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000391
392
393def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000394 **filter_data):
395 """\
396 See get_jobs() for documentation of extra filter parameters.
397 """
398 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
399 running,
400 finished)
401 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000402
403
mblighe8819cd2008-02-15 16:48:40 +0000404def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000405 """\
showarda8709c52008-07-03 19:44:54 +0000406 Like get_jobs(), but adds a 'status_counts' field, which is a dictionary
jadmanski0afbb632008-06-06 21:10:57 +0000407 mapping status strings to the number of hosts currently with that
408 status, i.e. {'Queued' : 4, 'Running' : 2}.
409 """
410 jobs = get_jobs(**filter_data)
411 ids = [job['id'] for job in jobs]
412 all_status_counts = models.Job.objects.get_status_counts(ids)
413 for job in jobs:
414 job['status_counts'] = all_status_counts[job['id']]
415 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000416
417
showard945072f2008-09-03 20:34:59 +0000418def get_info_for_clone(id, preserve_metahosts):
showarda8709c52008-07-03 19:44:54 +0000419 """\
420 Retrieves all the information needed to clone a job.
421 """
422 info = {}
423 job = models.Job.objects.get(id=id)
424 query = job.hostqueueentry_set.filter(deleted=False)
showard945072f2008-09-03 20:34:59 +0000425
426 hosts = []
427 meta_hosts = []
428
429 # For each queue entry, if the entry contains a host, add the entry into the
430 # hosts list if either:
431 # It is not a metahost.
432 # It was an assigned metahost, and the user wants to keep the specific
433 # assignments.
434 # Otherwise, add the metahost to the metahosts list.
435 for queue_entry in query:
436 if (queue_entry.host and (preserve_metahosts
437 or not queue_entry.meta_host)):
438 hosts.append(queue_entry.host)
439 else:
440 meta_hosts.append(queue_entry.meta_host.name)
441
showardd9992fe2008-07-31 02:15:03 +0000442 host_dicts = []
showarda8709c52008-07-03 19:44:54 +0000443
showardd9992fe2008-07-31 02:15:03 +0000444 for host in hosts:
showardd9992fe2008-07-31 02:15:03 +0000445 # one-time host
446 if host.invalid:
showardbad4f2d2008-08-15 18:13:47 +0000447 host_dict = {}
448 host_dict['hostname'] = host.hostname
449 host_dict['id'] = host.id
showardd9992fe2008-07-31 02:15:03 +0000450 host_dict['platform'] = '(one-time host)'
451 host_dict['locked_text'] = ''
showardd9992fe2008-07-31 02:15:03 +0000452 else:
showardbad4f2d2008-08-15 18:13:47 +0000453 host_dict = get_hosts(id=host.id)[0]
454 other_labels = host_dict['labels']
455 if host_dict['platform']:
456 other_labels.remove(host_dict['platform'])
457 host_dict['other_labels'] = ', '.join(other_labels)
showardd9992fe2008-07-31 02:15:03 +0000458 host_dicts.append(host_dict)
showarda8709c52008-07-03 19:44:54 +0000459
460 meta_host_counts = {}
461 for meta_host in meta_hosts:
462 meta_host_counts.setdefault(meta_host, 0)
463 meta_host_counts[meta_host] += 1
464
465 info['job'] = job.get_object_dict()
466 info['meta_host_counts'] = meta_host_counts
showardd9992fe2008-07-31 02:15:03 +0000467 info['hosts'] = host_dicts
showarda8709c52008-07-03 19:44:54 +0000468
469 return rpc_utils.prepare_for_serialization(info)
470
471
showard34dc5fa2008-04-24 20:58:40 +0000472# host queue entries
473
474def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000475 """\
476 TODO
477 """
478 query = models.HostQueueEntry.query_objects(filter_data)
479 all_dicts = []
480 for queue_entry in query.select_related():
481 entry_dict = queue_entry.get_object_dict()
482 if entry_dict['host'] is not None:
483 entry_dict['host'] = queue_entry.host.get_object_dict()
484 entry_dict['job'] = queue_entry.job.get_object_dict()
485 all_dicts.append(entry_dict)
486 return rpc_utils.prepare_for_serialization(all_dicts)
showard34dc5fa2008-04-24 20:58:40 +0000487
488
489def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000490 """\
491 Get the number of host queue entries associated with this job.
492 """
493 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000494
495
showard1e935f12008-07-11 00:11:36 +0000496def get_hqe_percentage_complete(**filter_data):
497 """
498 Computes the percentage of host queue entries matching the given filter data
499 that are complete.
500 """
501 query = models.HostQueueEntry.query_objects(filter_data)
502 complete_count = query.filter(complete=True).count()
503 total_count = query.count()
504 if total_count == 0:
505 return 1
506 return float(complete_count) / total_count
507
508
mblighe8819cd2008-02-15 16:48:40 +0000509# other
510
showarde0b63622008-08-04 20:58:47 +0000511def echo(data=""):
512 """\
513 Returns a passed in string. For doing a basic test to see if RPC calls
514 can successfully be made.
515 """
516 return data
517
518
mblighe8819cd2008-02-15 16:48:40 +0000519def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000520 """\
521 Returns a dictionary containing a bunch of data that shouldn't change
522 often and is otherwise inaccessible. This includes:
523 priorities: list of job priority choices
524 default_priority: default priority value for new jobs
525 users: sorted list of all users
526 labels: sorted list of all labels
527 tests: sorted list of all tests
showard2b9a88b2008-06-13 20:55:03 +0000528 profilers: sorted list of all profilers
jadmanski0afbb632008-06-06 21:10:57 +0000529 user_login: logged-in username
530 host_statuses: sorted list of possible Host statuses
531 job_statuses: sorted list of possible HostQueueEntry statuses
532 """
showard21baa452008-10-21 00:08:39 +0000533
534 job_fields = models.Job.get_field_dict()
535
jadmanski0afbb632008-06-06 21:10:57 +0000536 result = {}
537 result['priorities'] = models.Job.Priority.choices()
showard21baa452008-10-21 00:08:39 +0000538 default_priority = job_fields['priority'].default
jadmanski0afbb632008-06-06 21:10:57 +0000539 default_string = models.Job.Priority.get_string(default_priority)
540 result['default_priority'] = default_string
541 result['users'] = get_users(sort_by=['login'])
542 result['labels'] = get_labels(sort_by=['-platform', 'name'])
543 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +0000544 result['profilers'] = get_profilers(sort_by=['name'])
showard0fc38302008-10-23 00:44:07 +0000545 result['current_user'] = rpc_utils.prepare_for_serialization(
546 thread_local.get_user().get_object_dict())
showard2b9a88b2008-06-13 20:55:03 +0000547 result['host_statuses'] = sorted(models.Host.Status.names)
548 result['job_statuses'] = sorted(models.Job.Status.names)
showardb1e51872008-10-07 11:08:18 +0000549 result['job_timeout_default'] = models.Job.DEFAULT_TIMEOUT
showard0fc38302008-10-23 00:44:07 +0000550 result['reboot_before_options'] = models.RebootBefore.names
551 result['reboot_after_options'] = models.RebootAfter.names
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",
showard5deb6772008-11-04 21:54:33 +0000562 "Starting": "Next in host's queue",
563 "Stopped": "Other host(s) failed verify",
564 "Parsing": "Awaiting parse of final results"}
jadmanski0afbb632008-06-06 21:10:57 +0000565 return result