blob: a4d3fba2f416f4bffc9f9a8c82701dac3d3a243d [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):
showardc92da832009-04-07 18:14:34 +000040 return models.Label.add_object(
41 name=name, kernel_config=kernel_config, 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):
showardc92da832009-04-07 18:14:34 +000064 """\
65 @returns A sequence of nested dictionaries of label information.
66 """
67 return rpc_utils.prepare_rows_as_nested_dicts(
68 models.Label.query_objects(filter_data),
69 ('atomic_group',))
70
71
72# atomic groups
73
74def add_atomic_group(name, max_number_of_machines, description=None):
75 return models.AtomicGroup.add_object(
76 name=name, max_number_of_machines=max_number_of_machines,
77 description=description).id
78
79
80def modify_atomic_group(id, **data):
81 models.AtomicGroup.smart_get(id).update_object(data)
82
83
84def delete_atomic_group(id):
85 models.AtomicGroup.smart_get(id).delete()
86
87
88def atomic_group_add_labels(id, labels):
89 label_objs = models.Label.smart_get_bulk(labels)
90 models.AtomicGroup.smart_get(id).label_set.add(*label_objs)
91
92
93def atomic_group_remove_labels(id, labels):
94 label_objs = models.Label.smart_get_bulk(labels)
95 models.AtomicGroup.smart_get(id).label_set.remove(*label_objs)
96
97
98def get_atomic_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +000099 return rpc_utils.prepare_for_serialization(
showardc92da832009-04-07 18:14:34 +0000100 models.AtomicGroup.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000101
102
103# hosts
104
showarddf062562008-07-03 19:56:37 +0000105def add_host(hostname, status=None, locked=None, protection=None):
jadmanski0afbb632008-06-06 21:10:57 +0000106 return models.Host.add_object(hostname=hostname, status=status,
showarddf062562008-07-03 19:56:37 +0000107 locked=locked, protection=protection).id
mblighe8819cd2008-02-15 16:48:40 +0000108
109
110def modify_host(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000111 models.Host.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000112
113
114def host_add_labels(id, labels):
showardbe3ec042008-11-12 18:16:07 +0000115 labels = models.Label.smart_get_bulk(labels)
jadmanski0afbb632008-06-06 21:10:57 +0000116 models.Host.smart_get(id).labels.add(*labels)
mblighe8819cd2008-02-15 16:48:40 +0000117
118
119def host_remove_labels(id, labels):
showardbe3ec042008-11-12 18:16:07 +0000120 labels = models.Label.smart_get_bulk(labels)
jadmanski0afbb632008-06-06 21:10:57 +0000121 models.Host.smart_get(id).labels.remove(*labels)
mblighe8819cd2008-02-15 16:48:40 +0000122
123
124def delete_host(id):
jadmanski0afbb632008-06-06 21:10:57 +0000125 models.Host.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000126
127
showard43a3d262008-11-12 18:17:05 +0000128def get_hosts(multiple_labels=[], exclude_only_if_needed_labels=False,
129 **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000130 """\
131 multiple_labels: match hosts in all of the labels given. Should be a
132 list of label names.
showard43a3d262008-11-12 18:17:05 +0000133 exclude_only_if_needed_labels: exclude hosts with at least one
134 "only_if_needed" label applied.
jadmanski0afbb632008-06-06 21:10:57 +0000135 """
showard43a3d262008-11-12 18:17:05 +0000136 hosts = rpc_utils.get_host_query(multiple_labels,
137 exclude_only_if_needed_labels,
138 filter_data)
139 host_dicts = []
140 for host_obj in hosts:
141 host_dict = host_obj.get_object_dict()
142 host_dict['labels'] = [label.name for label in host_obj.labels.all()]
jadmanski0afbb632008-06-06 21:10:57 +0000143 platform = host_obj.platform()
showard43a3d262008-11-12 18:17:05 +0000144 host_dict['platform'] = platform and platform.name or None
showardd04b4b62009-04-27 20:12:00 +0000145 host_dict['acls'] = [acl.name for acl in host_obj.aclgroup_set.all()]
showard43a3d262008-11-12 18:17:05 +0000146 host_dicts.append(host_dict)
147 return rpc_utils.prepare_for_serialization(host_dicts)
mblighe8819cd2008-02-15 16:48:40 +0000148
149
showard43a3d262008-11-12 18:17:05 +0000150def get_num_hosts(multiple_labels=[], exclude_only_if_needed_labels=False,
151 **filter_data):
152 hosts = rpc_utils.get_host_query(multiple_labels,
153 exclude_only_if_needed_labels,
154 filter_data)
155 return hosts.count()
showard1385b162008-03-13 15:59:40 +0000156
mblighe8819cd2008-02-15 16:48:40 +0000157
158# tests
159
showard909c7a62008-07-15 21:52:38 +0000160def add_test(name, test_type, path, author=None, dependencies=None,
showard3d9899a2008-07-31 02:11:58 +0000161 experimental=True, run_verify=None, test_class=None,
showard909c7a62008-07-15 21:52:38 +0000162 test_time=None, test_category=None, description=None,
163 sync_count=1):
jadmanski0afbb632008-06-06 21:10:57 +0000164 return models.Test.add_object(name=name, test_type=test_type, path=path,
showard909c7a62008-07-15 21:52:38 +0000165 author=author, dependencies=dependencies,
166 experimental=experimental,
167 run_verify=run_verify, test_time=test_time,
168 test_category=test_category,
169 sync_count=sync_count,
jadmanski0afbb632008-06-06 21:10:57 +0000170 test_class=test_class,
171 description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000172
173
174def modify_test(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000175 models.Test.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000176
177
178def delete_test(id):
jadmanski0afbb632008-06-06 21:10:57 +0000179 models.Test.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000180
181
182def get_tests(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000183 return rpc_utils.prepare_for_serialization(
184 models.Test.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000185
186
showard2b9a88b2008-06-13 20:55:03 +0000187# profilers
188
189def add_profiler(name, description=None):
190 return models.Profiler.add_object(name=name, description=description).id
191
192
193def modify_profiler(id, **data):
194 models.Profiler.smart_get(id).update_object(data)
195
196
197def delete_profiler(id):
198 models.Profiler.smart_get(id).delete()
199
200
201def get_profilers(**filter_data):
202 return rpc_utils.prepare_for_serialization(
203 models.Profiler.list_objects(filter_data))
204
205
mblighe8819cd2008-02-15 16:48:40 +0000206# users
207
208def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000209 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000210
211
212def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000213 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000214
215
216def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000217 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000218
219
220def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000221 return rpc_utils.prepare_for_serialization(
222 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000223
224
225# acl groups
226
227def add_acl_group(name, description=None):
showard04f2cd82008-07-25 20:53:31 +0000228 group = models.AclGroup.add_object(name=name, description=description)
229 group.users.add(thread_local.get_user())
230 return group.id
mblighe8819cd2008-02-15 16:48:40 +0000231
232
233def modify_acl_group(id, **data):
showard04f2cd82008-07-25 20:53:31 +0000234 group = models.AclGroup.smart_get(id)
235 group.check_for_acl_violation_acl_group()
236 group.update_object(data)
237 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000238
239
240def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000241 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000242 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000243 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000244 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000245
246
247def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000248 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000249 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000250 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000251 group.users.remove(*users)
showard04f2cd82008-07-25 20:53:31 +0000252 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000253
254
255def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000256 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000257 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000258 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000259 group.hosts.add(*hosts)
showard08f981b2008-06-24 21:59:03 +0000260 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000261
262
263def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000264 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000265 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000266 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000267 group.hosts.remove(*hosts)
showard08f981b2008-06-24 21:59:03 +0000268 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000269
270
271def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000272 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000273
274
275def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000276 acl_groups = models.AclGroup.list_objects(filter_data)
277 for acl_group in acl_groups:
278 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
279 acl_group['users'] = [user.login
280 for user in acl_group_obj.users.all()]
281 acl_group['hosts'] = [host.hostname
282 for host in acl_group_obj.hosts.all()]
283 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000284
285
286# jobs
287
mbligh120351e2009-01-24 01:40:45 +0000288def generate_control_file(tests=(), kernel=None, label=None, profilers=(),
289 client_control_file='', use_container=False):
jadmanski0afbb632008-06-06 21:10:57 +0000290 """
mbligh120351e2009-01-24 01:40:45 +0000291 Generates a client-side control file to load a kernel and run tests.
292
293 @param tests List of tests to run.
294 @param kernel Kernel to install in generated control file.
295 @param label Name of label to grab kernel config from.
296 @param profilers List of profilers to activate during the job.
297 @param client_control_file The contents of a client-side control file to
298 run at the end of all tests. If this is supplied, all tests must be
299 client side.
300 TODO: in the future we should support server control files directly
301 to wrap with a kernel. That'll require changing the parameter
302 name and adding a boolean to indicate if it is a client or server
303 control file.
304 @param use_container unused argument today. TODO: Enable containers
305 on the host during a client side test.
306
307 @returns a dict with the following keys:
308 control_file: str, The control file text.
309 is_server: bool, is the control file a server-side control file?
310 synch_count: How many machines the job uses per autoserv execution.
311 synch_count == 1 means the job is asynchronous.
312 dependencies: A list of the names of labels on which the job depends.
313 """
314 if not tests and not control_file:
showard2bab8f42008-11-12 18:15:22 +0000315 return dict(control_file='', is_server=False, synch_count=1,
showard989f25d2008-10-01 11:38:11 +0000316 dependencies=[])
mblighe8819cd2008-02-15 16:48:40 +0000317
showard989f25d2008-10-01 11:38:11 +0000318 cf_info, test_objects, profiler_objects, label = (
showard2b9a88b2008-06-13 20:55:03 +0000319 rpc_utils.prepare_generate_control_file(tests, kernel, label,
320 profilers))
showard989f25d2008-10-01 11:38:11 +0000321 cf_info['control_file'] = control_file.generate_control(
322 tests=test_objects, kernel=kernel, platform=label,
mbligh120351e2009-01-24 01:40:45 +0000323 profilers=profiler_objects, is_server=cf_info['is_server'],
324 client_control_file=client_control_file)
showard989f25d2008-10-01 11:38:11 +0000325 return cf_info
mblighe8819cd2008-02-15 16:48:40 +0000326
327
showard3bb499f2008-07-03 19:42:20 +0000328def create_job(name, priority, control_file, control_type, timeout=None,
showardc92da832009-04-07 18:14:34 +0000329 synch_count=None, hosts=(), meta_hosts=(),
330 run_verify=True, one_time_hosts=(), email_list='',
331 dependencies=(), reboot_before=None, reboot_after=None,
332 atomic_group_name=None):
jadmanski0afbb632008-06-06 21:10:57 +0000333 """\
334 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000335
jadmanski0afbb632008-06-06 21:10:57 +0000336 priority: Low, Medium, High, Urgent
showardc92da832009-04-07 18:14:34 +0000337 control_file: String contents of the control file.
338 control_type: Type of control file, Client or Server.
339 timeout: Hours after this call returns until the job times out.
340 synch_count: How many machines the job uses per autoserv execution.
341 synch_count == 1 means the job is asynchronous. If an
342 atomic group is given this value is treated as a minimum.
343 hosts: List of hosts to run job on.
344 meta_hosts: List where each entry is a label name, and for each entry
jadmanski0afbb632008-06-06 21:10:57 +0000345 one host will be chosen from that label to run the job
346 on.
showardc92da832009-04-07 18:14:34 +0000347 run_verify: Should the host be verified before running the test?
348 one_time_hosts: List of hosts not in the database to run the job on.
349 email_list: String containing emails to mail when the job is done
350 dependencies: List of label names on which this job depends
mbligh94ae6d62009-02-03 17:47:49 +0000351 reboot_before: Never, If dirty, or Always
352 reboot_after: Never, If all tests passed, or Always
showardc92da832009-04-07 18:14:34 +0000353 atomic_group_name: The name of an atomic group to schedule the job on.
354
355 @returns The created Job id number.
jadmanski0afbb632008-06-06 21:10:57 +0000356 """
showard3bb499f2008-07-03 19:42:20 +0000357
358 if timeout is None:
359 timeout=global_config.global_config.get_config_value(
360 'AUTOTEST_WEB', 'job_timeout_default')
361
showardff901382008-07-07 23:22:16 +0000362 owner = thread_local.get_user().login
jadmanski0afbb632008-06-06 21:10:57 +0000363 # input validation
showardc92da832009-04-07 18:14:34 +0000364 if not (hosts or meta_hosts or one_time_hosts or atomic_group_name):
mblighec5546d2008-06-16 16:51:28 +0000365 raise model_logic.ValidationError({
showardb8471e32008-07-03 19:51:08 +0000366 'arguments' : "You must pass at least one of 'hosts', "
showardc92da832009-04-07 18:14:34 +0000367 "'meta_hosts', 'one_time_hosts', "
368 "or 'atomic_group_name'"
jadmanski0afbb632008-06-06 21:10:57 +0000369 })
mblighe8819cd2008-02-15 16:48:40 +0000370
showardc92da832009-04-07 18:14:34 +0000371 # Create and sanity check an AtomicGroup object if requested.
372 if atomic_group_name:
373 if one_time_hosts:
374 raise model_logic.ValidationError(
375 {'one_time_hosts':
376 'One time hosts cannot be used with an Atomic Group.'})
377 atomic_group = models.AtomicGroup.smart_get(atomic_group_name)
378 if synch_count and synch_count > atomic_group.max_number_of_machines:
379 raise model_logic.ValidationError(
380 {'atomic_group_name' :
381 'You have requested a synch_count (%d) greater than the '
382 'maximum machines in the requested Atomic Group (%d).' %
383 (synch_count, atomic_group.max_number_of_machines)})
384 else:
385 atomic_group = None
386
showard989f25d2008-10-01 11:38:11 +0000387 labels_by_name = dict((label.name, label)
388 for label in models.Label.objects.all())
showardba872902008-06-28 00:51:08 +0000389
jadmanski0afbb632008-06-06 21:10:57 +0000390 # convert hostnames & meta hosts to host/label objects
showardc92da832009-04-07 18:14:34 +0000391 host_objects = models.Host.smart_get_bulk(hosts)
showard989f25d2008-10-01 11:38:11 +0000392 metahost_objects = []
393 metahost_counts = {}
jadmanski0afbb632008-06-06 21:10:57 +0000394 for label in meta_hosts or []:
showard25087ac2008-11-24 22:12:03 +0000395 if label not in labels_by_name:
396 raise model_logic.ValidationError(
397 {'meta_hosts' : 'Label "%s" not found' % label})
showard989f25d2008-10-01 11:38:11 +0000398 this_label = labels_by_name[label]
399 metahost_objects.append(this_label)
400 metahost_counts.setdefault(this_label, 0)
401 metahost_counts[this_label] += 1
showardb8471e32008-07-03 19:51:08 +0000402 for host in one_time_hosts or []:
403 this_host = models.Host.create_one_time_host(host)
404 host_objects.append(this_host)
showardba872902008-06-28 00:51:08 +0000405
showard65c267d2009-01-20 23:24:47 +0000406 all_host_objects = host_objects + metahost_objects
407
showardba872902008-06-28 00:51:08 +0000408 # check that each metahost request has enough hosts under the label
showard989f25d2008-10-01 11:38:11 +0000409 for label, requested_count in metahost_counts.iteritems():
410 available_count = label.host_set.count()
411 if requested_count > available_count:
412 error = ("You have requested %d %s's, but there are only %d."
413 % (requested_count, label.name, available_count))
414 raise model_logic.ValidationError({'meta_hosts' : error})
mblighe8819cd2008-02-15 16:48:40 +0000415
showardc92da832009-04-07 18:14:34 +0000416 if atomic_group:
417 rpc_utils.check_atomic_group_create_job(
418 synch_count, host_objects, metahost_objects,
419 dependencies, atomic_group, labels_by_name)
420 else:
421 if synch_count is not None and synch_count > len(all_host_objects):
422 raise model_logic.ValidationError(
423 {'hosts':
424 'only %d hosts provided for job with synch_count = %d' %
425 (len(all_host_objects), synch_count)})
showard47d9e7d2009-04-20 19:33:56 +0000426 atomic_hosts = models.Host.objects.filter(
427 id__in=[host.id for host in host_objects],
428 labels__atomic_group=True)
429 unusable_host_names = [host.hostname for host in atomic_hosts]
430 if unusable_host_names:
431 raise model_logic.ValidationError(
432 {'hosts':
433 'Host(s) "%s" are atomic group hosts but no '
434 'atomic group was specified for this job.' %
435 (', '.join(unusable_host_names),)})
436
showard65c267d2009-01-20 23:24:47 +0000437
showard989f25d2008-10-01 11:38:11 +0000438 rpc_utils.check_job_dependencies(host_objects, dependencies)
439 dependency_labels = [labels_by_name[label_name]
440 for label_name in dependencies]
441
showard3181c7b2009-04-20 19:34:42 +0000442 for label in metahost_objects + dependency_labels:
443 if label.atomic_group and not atomic_group:
444 raise model_logic.ValidationError(
445 {'atomic_group_name':
446 'Some meta_hosts or dependencies require an atomic group '
447 'but no atomic_group_name was specified for this job.'})
448 elif (label.atomic_group and
449 label.atomic_group.name != atomic_group_name):
450 raise model_logic.ValidationError(
451 {'atomic_group_name':
452 'Some meta_hosts or dependencies require an atomic group '
453 'other than the one requested for this job.'})
454
jadmanski0afbb632008-06-06 21:10:57 +0000455 job = models.Job.create(owner=owner, name=name, priority=priority,
456 control_file=control_file,
457 control_type=control_type,
showard2bab8f42008-11-12 18:15:22 +0000458 synch_count=synch_count,
showard65c267d2009-01-20 23:24:47 +0000459 hosts=all_host_objects,
showard909c7a62008-07-15 21:52:38 +0000460 timeout=timeout,
showard542e8402008-09-19 20:16:18 +0000461 run_verify=run_verify,
showard989f25d2008-10-01 11:38:11 +0000462 email_list=email_list.strip(),
showard21baa452008-10-21 00:08:39 +0000463 dependencies=dependency_labels,
464 reboot_before=reboot_before,
465 reboot_after=reboot_after)
showardc92da832009-04-07 18:14:34 +0000466 job.queue(all_host_objects, atomic_group=atomic_group)
jadmanski0afbb632008-06-06 21:10:57 +0000467 return job.id
mblighe8819cd2008-02-15 16:48:40 +0000468
469
showard9dbdcda2008-10-14 17:34:36 +0000470def abort_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000471 """\
showard9dbdcda2008-10-14 17:34:36 +0000472 Abort a set of host queue entries.
jadmanski0afbb632008-06-06 21:10:57 +0000473 """
showard9dbdcda2008-10-14 17:34:36 +0000474 query = models.HostQueueEntry.query_objects(filter_data)
showard0c185192009-01-16 03:07:57 +0000475 query = query.filter(complete=False)
showarddc817512008-11-12 18:16:41 +0000476 models.AclGroup.check_abort_permissions(query)
showard9dbdcda2008-10-14 17:34:36 +0000477 host_queue_entries = list(query.select_related())
showard2bab8f42008-11-12 18:15:22 +0000478 rpc_utils.check_abort_synchronous_jobs(host_queue_entries)
mblighe8819cd2008-02-15 16:48:40 +0000479
showard9dbdcda2008-10-14 17:34:36 +0000480 user = thread_local.get_user()
481 for queue_entry in host_queue_entries:
482 queue_entry.abort(user)
showard9d821ab2008-07-11 16:54:29 +0000483
484
mblighe8819cd2008-02-15 16:48:40 +0000485def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000486 """\
487 Extra filter args for get_jobs:
488 -not_yet_run: Include only jobs that have not yet started running.
489 -running: Include only jobs that have start running but for which not
490 all hosts have completed.
491 -finished: Include only jobs for which all hosts have completed (or
492 aborted).
493 At most one of these three fields should be specified.
494 """
495 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
496 running,
497 finished)
showard989f25d2008-10-01 11:38:11 +0000498 jobs = models.Job.list_objects(filter_data)
499 models.Job.objects.populate_dependencies(jobs)
500 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000501
502
503def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000504 **filter_data):
505 """\
506 See get_jobs() for documentation of extra filter parameters.
507 """
508 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
509 running,
510 finished)
511 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000512
513
mblighe8819cd2008-02-15 16:48:40 +0000514def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000515 """\
showarda8709c52008-07-03 19:44:54 +0000516 Like get_jobs(), but adds a 'status_counts' field, which is a dictionary
jadmanski0afbb632008-06-06 21:10:57 +0000517 mapping status strings to the number of hosts currently with that
518 status, i.e. {'Queued' : 4, 'Running' : 2}.
519 """
520 jobs = get_jobs(**filter_data)
521 ids = [job['id'] for job in jobs]
522 all_status_counts = models.Job.objects.get_status_counts(ids)
523 for job in jobs:
524 job['status_counts'] = all_status_counts[job['id']]
525 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000526
527
showard945072f2008-09-03 20:34:59 +0000528def get_info_for_clone(id, preserve_metahosts):
showarda8709c52008-07-03 19:44:54 +0000529 """\
530 Retrieves all the information needed to clone a job.
531 """
532 info = {}
533 job = models.Job.objects.get(id=id)
showard45195442009-02-17 20:55:48 +0000534 query = job.hostqueueentry_set.filter()
showard945072f2008-09-03 20:34:59 +0000535
536 hosts = []
537 meta_hosts = []
showardc92da832009-04-07 18:14:34 +0000538 atomic_group_name = None
showard945072f2008-09-03 20:34:59 +0000539
540 # For each queue entry, if the entry contains a host, add the entry into the
541 # hosts list if either:
542 # It is not a metahost.
543 # It was an assigned metahost, and the user wants to keep the specific
544 # assignments.
545 # Otherwise, add the metahost to the metahosts list.
546 for queue_entry in query:
547 if (queue_entry.host and (preserve_metahosts
548 or not queue_entry.meta_host)):
showard45195442009-02-17 20:55:48 +0000549 if queue_entry.deleted:
550 continue
showard945072f2008-09-03 20:34:59 +0000551 hosts.append(queue_entry.host)
552 else:
553 meta_hosts.append(queue_entry.meta_host.name)
showardc92da832009-04-07 18:14:34 +0000554 if atomic_group_name is None:
showard3562a062009-04-13 16:05:30 +0000555 if queue_entry.atomic_group is not None:
556 atomic_group_name = queue_entry.atomic_group.name
showardc92da832009-04-07 18:14:34 +0000557 else:
558 assert atomic_group_name == queue_entry.atomic_group.name, (
559 'DB inconsistency. HostQueueEntries with multiple atomic'
560 ' groups on job %s: %s != %s' % (
561 id, atomic_group_name, queue_entry.atomic_group.name))
showard945072f2008-09-03 20:34:59 +0000562
showardd9992fe2008-07-31 02:15:03 +0000563 host_dicts = []
showarda8709c52008-07-03 19:44:54 +0000564
showardd9992fe2008-07-31 02:15:03 +0000565 for host in hosts:
showardd9992fe2008-07-31 02:15:03 +0000566 # one-time host
567 if host.invalid:
showardbad4f2d2008-08-15 18:13:47 +0000568 host_dict = {}
569 host_dict['hostname'] = host.hostname
570 host_dict['id'] = host.id
showardd9992fe2008-07-31 02:15:03 +0000571 host_dict['platform'] = '(one-time host)'
572 host_dict['locked_text'] = ''
showardd9992fe2008-07-31 02:15:03 +0000573 else:
showardbad4f2d2008-08-15 18:13:47 +0000574 host_dict = get_hosts(id=host.id)[0]
575 other_labels = host_dict['labels']
576 if host_dict['platform']:
577 other_labels.remove(host_dict['platform'])
578 host_dict['other_labels'] = ', '.join(other_labels)
showardd9992fe2008-07-31 02:15:03 +0000579 host_dicts.append(host_dict)
showarda8709c52008-07-03 19:44:54 +0000580
581 meta_host_counts = {}
582 for meta_host in meta_hosts:
583 meta_host_counts.setdefault(meta_host, 0)
584 meta_host_counts[meta_host] += 1
585
586 info['job'] = job.get_object_dict()
showard2e9415f2008-12-05 19:48:38 +0000587 info['job']['dependencies'] = [label.name for label
588 in job.dependency_labels.all()]
showarda8709c52008-07-03 19:44:54 +0000589 info['meta_host_counts'] = meta_host_counts
showardd9992fe2008-07-31 02:15:03 +0000590 info['hosts'] = host_dicts
showardc92da832009-04-07 18:14:34 +0000591 info['atomic_group_name'] = atomic_group_name
showarda8709c52008-07-03 19:44:54 +0000592
593 return rpc_utils.prepare_for_serialization(info)
594
595
showard34dc5fa2008-04-24 20:58:40 +0000596# host queue entries
597
598def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000599 """\
showardc92da832009-04-07 18:14:34 +0000600 @returns A sequence of nested dictionaries of host and job information.
jadmanski0afbb632008-06-06 21:10:57 +0000601 """
showardc92da832009-04-07 18:14:34 +0000602 return rpc_utils.prepare_rows_as_nested_dicts(
603 models.HostQueueEntry.query_objects(filter_data),
604 ('host', 'atomic_group', 'job'))
showard34dc5fa2008-04-24 20:58:40 +0000605
606
607def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000608 """\
609 Get the number of host queue entries associated with this job.
610 """
611 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000612
613
showard1e935f12008-07-11 00:11:36 +0000614def get_hqe_percentage_complete(**filter_data):
615 """
showardc92da832009-04-07 18:14:34 +0000616 Computes the fraction of host queue entries matching the given filter data
showard1e935f12008-07-11 00:11:36 +0000617 that are complete.
618 """
619 query = models.HostQueueEntry.query_objects(filter_data)
620 complete_count = query.filter(complete=True).count()
621 total_count = query.count()
622 if total_count == 0:
623 return 1
624 return float(complete_count) / total_count
625
626
mblighe8819cd2008-02-15 16:48:40 +0000627# other
628
showarde0b63622008-08-04 20:58:47 +0000629def echo(data=""):
630 """\
631 Returns a passed in string. For doing a basic test to see if RPC calls
632 can successfully be made.
633 """
634 return data
635
636
showardb7a52fd2009-04-27 20:10:56 +0000637def get_motd():
638 """\
639 Returns the message of the day as a string.
640 """
641 return rpc_utils.get_motd()
642
643
mblighe8819cd2008-02-15 16:48:40 +0000644def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000645 """\
646 Returns a dictionary containing a bunch of data that shouldn't change
647 often and is otherwise inaccessible. This includes:
showardc92da832009-04-07 18:14:34 +0000648
649 priorities: List of job priority choices.
650 default_priority: Default priority value for new jobs.
651 users: Sorted list of all users.
652 labels: Sorted list of all labels.
653 atomic_groups: Sorted list of all atomic groups.
654 tests: Sorted list of all tests.
655 profilers: Sorted list of all profilers.
656 current_user: Logged-in username.
657 host_statuses: Sorted list of possible Host statuses.
658 job_statuses: Sorted list of possible HostQueueEntry statuses.
659 job_timeout_default: The default job timeout length in hours.
660 reboot_before_options: A list of valid RebootBefore string enums.
661 reboot_after_options: A list of valid RebootAfter string enums.
662 motd: Server's message of the day.
663 status_dictionary: A mapping from one word job status names to a more
664 informative description.
jadmanski0afbb632008-06-06 21:10:57 +0000665 """
showard21baa452008-10-21 00:08:39 +0000666
667 job_fields = models.Job.get_field_dict()
668
jadmanski0afbb632008-06-06 21:10:57 +0000669 result = {}
670 result['priorities'] = models.Job.Priority.choices()
showard21baa452008-10-21 00:08:39 +0000671 default_priority = job_fields['priority'].default
jadmanski0afbb632008-06-06 21:10:57 +0000672 default_string = models.Job.Priority.get_string(default_priority)
673 result['default_priority'] = default_string
674 result['users'] = get_users(sort_by=['login'])
675 result['labels'] = get_labels(sort_by=['-platform', 'name'])
showardc92da832009-04-07 18:14:34 +0000676 result['atomic_groups'] = get_atomic_groups(sort_by=['name'])
jadmanski0afbb632008-06-06 21:10:57 +0000677 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +0000678 result['profilers'] = get_profilers(sort_by=['name'])
showard0fc38302008-10-23 00:44:07 +0000679 result['current_user'] = rpc_utils.prepare_for_serialization(
680 thread_local.get_user().get_object_dict())
showard2b9a88b2008-06-13 20:55:03 +0000681 result['host_statuses'] = sorted(models.Host.Status.names)
mbligh5a198b92008-12-11 19:33:29 +0000682 result['job_statuses'] = sorted(models.HostQueueEntry.Status.names)
showardb1e51872008-10-07 11:08:18 +0000683 result['job_timeout_default'] = models.Job.DEFAULT_TIMEOUT
showard0fc38302008-10-23 00:44:07 +0000684 result['reboot_before_options'] = models.RebootBefore.names
685 result['reboot_after_options'] = models.RebootAfter.names
showard8fbae652009-01-20 23:23:10 +0000686 result['motd'] = rpc_utils.get_motd()
showard8ac29b42008-07-17 17:01:55 +0000687
showardd3dc1992009-04-22 21:01:40 +0000688 result['status_dictionary'] = {"Aborted": "Aborted",
showard8ac29b42008-07-17 17:01:55 +0000689 "Verifying": "Verifying Host",
690 "Pending": "Waiting on other hosts",
691 "Running": "Running autoserv",
692 "Completed": "Autoserv completed",
693 "Failed": "Failed to complete",
showardd823b362008-07-24 16:35:46 +0000694 "Queued": "Queued",
showard5deb6772008-11-04 21:54:33 +0000695 "Starting": "Next in host's queue",
696 "Stopped": "Other host(s) failed verify",
showardd3dc1992009-04-22 21:01:40 +0000697 "Parsing": "Awaiting parse of final results",
698 "Gathering": "Gathering log files"}
jadmanski0afbb632008-06-06 21:10:57 +0000699 return result