blob: 65f99898c4c6b56b1925374855b23952ff6980d8 [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
145 host_dicts.append(host_dict)
146 return rpc_utils.prepare_for_serialization(host_dicts)
mblighe8819cd2008-02-15 16:48:40 +0000147
148
showard43a3d262008-11-12 18:17:05 +0000149def get_num_hosts(multiple_labels=[], exclude_only_if_needed_labels=False,
150 **filter_data):
151 hosts = rpc_utils.get_host_query(multiple_labels,
152 exclude_only_if_needed_labels,
153 filter_data)
154 return hosts.count()
showard1385b162008-03-13 15:59:40 +0000155
mblighe8819cd2008-02-15 16:48:40 +0000156
157# tests
158
showard909c7a62008-07-15 21:52:38 +0000159def add_test(name, test_type, path, author=None, dependencies=None,
showard3d9899a2008-07-31 02:11:58 +0000160 experimental=True, run_verify=None, test_class=None,
showard909c7a62008-07-15 21:52:38 +0000161 test_time=None, test_category=None, description=None,
162 sync_count=1):
jadmanski0afbb632008-06-06 21:10:57 +0000163 return models.Test.add_object(name=name, test_type=test_type, path=path,
showard909c7a62008-07-15 21:52:38 +0000164 author=author, dependencies=dependencies,
165 experimental=experimental,
166 run_verify=run_verify, test_time=test_time,
167 test_category=test_category,
168 sync_count=sync_count,
jadmanski0afbb632008-06-06 21:10:57 +0000169 test_class=test_class,
170 description=description).id
mblighe8819cd2008-02-15 16:48:40 +0000171
172
173def modify_test(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000174 models.Test.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000175
176
177def delete_test(id):
jadmanski0afbb632008-06-06 21:10:57 +0000178 models.Test.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000179
180
181def get_tests(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000182 return rpc_utils.prepare_for_serialization(
183 models.Test.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000184
185
showard2b9a88b2008-06-13 20:55:03 +0000186# profilers
187
188def add_profiler(name, description=None):
189 return models.Profiler.add_object(name=name, description=description).id
190
191
192def modify_profiler(id, **data):
193 models.Profiler.smart_get(id).update_object(data)
194
195
196def delete_profiler(id):
197 models.Profiler.smart_get(id).delete()
198
199
200def get_profilers(**filter_data):
201 return rpc_utils.prepare_for_serialization(
202 models.Profiler.list_objects(filter_data))
203
204
mblighe8819cd2008-02-15 16:48:40 +0000205# users
206
207def add_user(login, access_level=None):
jadmanski0afbb632008-06-06 21:10:57 +0000208 return models.User.add_object(login=login, access_level=access_level).id
mblighe8819cd2008-02-15 16:48:40 +0000209
210
211def modify_user(id, **data):
jadmanski0afbb632008-06-06 21:10:57 +0000212 models.User.smart_get(id).update_object(data)
mblighe8819cd2008-02-15 16:48:40 +0000213
214
215def delete_user(id):
jadmanski0afbb632008-06-06 21:10:57 +0000216 models.User.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000217
218
219def get_users(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000220 return rpc_utils.prepare_for_serialization(
221 models.User.list_objects(filter_data))
mblighe8819cd2008-02-15 16:48:40 +0000222
223
224# acl groups
225
226def add_acl_group(name, description=None):
showard04f2cd82008-07-25 20:53:31 +0000227 group = models.AclGroup.add_object(name=name, description=description)
228 group.users.add(thread_local.get_user())
229 return group.id
mblighe8819cd2008-02-15 16:48:40 +0000230
231
232def modify_acl_group(id, **data):
showard04f2cd82008-07-25 20:53:31 +0000233 group = models.AclGroup.smart_get(id)
234 group.check_for_acl_violation_acl_group()
235 group.update_object(data)
236 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000237
238
239def acl_group_add_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000240 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000241 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000242 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000243 group.users.add(*users)
mblighe8819cd2008-02-15 16:48:40 +0000244
245
246def acl_group_remove_users(id, users):
jadmanski0afbb632008-06-06 21:10:57 +0000247 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000248 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000249 users = models.User.smart_get_bulk(users)
jadmanski0afbb632008-06-06 21:10:57 +0000250 group.users.remove(*users)
showard04f2cd82008-07-25 20:53:31 +0000251 group.add_current_user_if_empty()
mblighe8819cd2008-02-15 16:48:40 +0000252
253
254def acl_group_add_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000255 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000256 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000257 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000258 group.hosts.add(*hosts)
showard08f981b2008-06-24 21:59:03 +0000259 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000260
261
262def acl_group_remove_hosts(id, hosts):
jadmanski0afbb632008-06-06 21:10:57 +0000263 group = models.AclGroup.smart_get(id)
showard04f2cd82008-07-25 20:53:31 +0000264 group.check_for_acl_violation_acl_group()
showardbe3ec042008-11-12 18:16:07 +0000265 hosts = models.Host.smart_get_bulk(hosts)
jadmanski0afbb632008-06-06 21:10:57 +0000266 group.hosts.remove(*hosts)
showard08f981b2008-06-24 21:59:03 +0000267 group.on_host_membership_change()
mblighe8819cd2008-02-15 16:48:40 +0000268
269
270def delete_acl_group(id):
jadmanski0afbb632008-06-06 21:10:57 +0000271 models.AclGroup.smart_get(id).delete()
mblighe8819cd2008-02-15 16:48:40 +0000272
273
274def get_acl_groups(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000275 acl_groups = models.AclGroup.list_objects(filter_data)
276 for acl_group in acl_groups:
277 acl_group_obj = models.AclGroup.objects.get(id=acl_group['id'])
278 acl_group['users'] = [user.login
279 for user in acl_group_obj.users.all()]
280 acl_group['hosts'] = [host.hostname
281 for host in acl_group_obj.hosts.all()]
282 return rpc_utils.prepare_for_serialization(acl_groups)
mblighe8819cd2008-02-15 16:48:40 +0000283
284
285# jobs
286
mbligh120351e2009-01-24 01:40:45 +0000287def generate_control_file(tests=(), kernel=None, label=None, profilers=(),
288 client_control_file='', use_container=False):
jadmanski0afbb632008-06-06 21:10:57 +0000289 """
mbligh120351e2009-01-24 01:40:45 +0000290 Generates a client-side control file to load a kernel and run tests.
291
292 @param tests List of tests to run.
293 @param kernel Kernel to install in generated control file.
294 @param label Name of label to grab kernel config from.
295 @param profilers List of profilers to activate during the job.
296 @param client_control_file The contents of a client-side control file to
297 run at the end of all tests. If this is supplied, all tests must be
298 client side.
299 TODO: in the future we should support server control files directly
300 to wrap with a kernel. That'll require changing the parameter
301 name and adding a boolean to indicate if it is a client or server
302 control file.
303 @param use_container unused argument today. TODO: Enable containers
304 on the host during a client side test.
305
306 @returns a dict with the following keys:
307 control_file: str, The control file text.
308 is_server: bool, is the control file a server-side control file?
309 synch_count: How many machines the job uses per autoserv execution.
310 synch_count == 1 means the job is asynchronous.
311 dependencies: A list of the names of labels on which the job depends.
312 """
313 if not tests and not control_file:
showard2bab8f42008-11-12 18:15:22 +0000314 return dict(control_file='', is_server=False, synch_count=1,
showard989f25d2008-10-01 11:38:11 +0000315 dependencies=[])
mblighe8819cd2008-02-15 16:48:40 +0000316
showard989f25d2008-10-01 11:38:11 +0000317 cf_info, test_objects, profiler_objects, label = (
showard2b9a88b2008-06-13 20:55:03 +0000318 rpc_utils.prepare_generate_control_file(tests, kernel, label,
319 profilers))
showard989f25d2008-10-01 11:38:11 +0000320 cf_info['control_file'] = control_file.generate_control(
321 tests=test_objects, kernel=kernel, platform=label,
mbligh120351e2009-01-24 01:40:45 +0000322 profilers=profiler_objects, is_server=cf_info['is_server'],
323 client_control_file=client_control_file)
showard989f25d2008-10-01 11:38:11 +0000324 return cf_info
mblighe8819cd2008-02-15 16:48:40 +0000325
326
showard3bb499f2008-07-03 19:42:20 +0000327def create_job(name, priority, control_file, control_type, timeout=None,
showardc92da832009-04-07 18:14:34 +0000328 synch_count=None, hosts=(), meta_hosts=(),
329 run_verify=True, one_time_hosts=(), email_list='',
330 dependencies=(), reboot_before=None, reboot_after=None,
331 atomic_group_name=None):
jadmanski0afbb632008-06-06 21:10:57 +0000332 """\
333 Create and enqueue a job.
mblighe8819cd2008-02-15 16:48:40 +0000334
jadmanski0afbb632008-06-06 21:10:57 +0000335 priority: Low, Medium, High, Urgent
showardc92da832009-04-07 18:14:34 +0000336 control_file: String contents of the control file.
337 control_type: Type of control file, Client or Server.
338 timeout: Hours after this call returns until the job times out.
339 synch_count: How many machines the job uses per autoserv execution.
340 synch_count == 1 means the job is asynchronous. If an
341 atomic group is given this value is treated as a minimum.
342 hosts: List of hosts to run job on.
343 meta_hosts: List where each entry is a label name, and for each entry
jadmanski0afbb632008-06-06 21:10:57 +0000344 one host will be chosen from that label to run the job
345 on.
showardc92da832009-04-07 18:14:34 +0000346 run_verify: Should the host be verified before running the test?
347 one_time_hosts: List of hosts not in the database to run the job on.
348 email_list: String containing emails to mail when the job is done
349 dependencies: List of label names on which this job depends
mbligh94ae6d62009-02-03 17:47:49 +0000350 reboot_before: Never, If dirty, or Always
351 reboot_after: Never, If all tests passed, or Always
showardc92da832009-04-07 18:14:34 +0000352 atomic_group_name: The name of an atomic group to schedule the job on.
353
354 @returns The created Job id number.
jadmanski0afbb632008-06-06 21:10:57 +0000355 """
showard3bb499f2008-07-03 19:42:20 +0000356
357 if timeout is None:
358 timeout=global_config.global_config.get_config_value(
359 'AUTOTEST_WEB', 'job_timeout_default')
360
showardff901382008-07-07 23:22:16 +0000361 owner = thread_local.get_user().login
jadmanski0afbb632008-06-06 21:10:57 +0000362 # input validation
showardc92da832009-04-07 18:14:34 +0000363 if not (hosts or meta_hosts or one_time_hosts or atomic_group_name):
mblighec5546d2008-06-16 16:51:28 +0000364 raise model_logic.ValidationError({
showardb8471e32008-07-03 19:51:08 +0000365 'arguments' : "You must pass at least one of 'hosts', "
showardc92da832009-04-07 18:14:34 +0000366 "'meta_hosts', 'one_time_hosts', "
367 "or 'atomic_group_name'"
jadmanski0afbb632008-06-06 21:10:57 +0000368 })
mblighe8819cd2008-02-15 16:48:40 +0000369
showardc92da832009-04-07 18:14:34 +0000370 # Create and sanity check an AtomicGroup object if requested.
371 if atomic_group_name:
372 if one_time_hosts:
373 raise model_logic.ValidationError(
374 {'one_time_hosts':
375 'One time hosts cannot be used with an Atomic Group.'})
376 atomic_group = models.AtomicGroup.smart_get(atomic_group_name)
377 if synch_count and synch_count > atomic_group.max_number_of_machines:
378 raise model_logic.ValidationError(
379 {'atomic_group_name' :
380 'You have requested a synch_count (%d) greater than the '
381 'maximum machines in the requested Atomic Group (%d).' %
382 (synch_count, atomic_group.max_number_of_machines)})
383 else:
384 atomic_group = None
385
showard989f25d2008-10-01 11:38:11 +0000386 labels_by_name = dict((label.name, label)
387 for label in models.Label.objects.all())
showardba872902008-06-28 00:51:08 +0000388
jadmanski0afbb632008-06-06 21:10:57 +0000389 # convert hostnames & meta hosts to host/label objects
showardc92da832009-04-07 18:14:34 +0000390 host_objects = models.Host.smart_get_bulk(hosts)
showard989f25d2008-10-01 11:38:11 +0000391 metahost_objects = []
392 metahost_counts = {}
jadmanski0afbb632008-06-06 21:10:57 +0000393 for label in meta_hosts or []:
showard25087ac2008-11-24 22:12:03 +0000394 if label not in labels_by_name:
395 raise model_logic.ValidationError(
396 {'meta_hosts' : 'Label "%s" not found' % label})
showard989f25d2008-10-01 11:38:11 +0000397 this_label = labels_by_name[label]
398 metahost_objects.append(this_label)
399 metahost_counts.setdefault(this_label, 0)
400 metahost_counts[this_label] += 1
showardb8471e32008-07-03 19:51:08 +0000401 for host in one_time_hosts or []:
402 this_host = models.Host.create_one_time_host(host)
403 host_objects.append(this_host)
showardba872902008-06-28 00:51:08 +0000404
showard65c267d2009-01-20 23:24:47 +0000405 all_host_objects = host_objects + metahost_objects
406
showardba872902008-06-28 00:51:08 +0000407 # check that each metahost request has enough hosts under the label
showard989f25d2008-10-01 11:38:11 +0000408 for label, requested_count in metahost_counts.iteritems():
409 available_count = label.host_set.count()
410 if requested_count > available_count:
411 error = ("You have requested %d %s's, but there are only %d."
412 % (requested_count, label.name, available_count))
413 raise model_logic.ValidationError({'meta_hosts' : error})
mblighe8819cd2008-02-15 16:48:40 +0000414
showardc92da832009-04-07 18:14:34 +0000415 if atomic_group:
416 rpc_utils.check_atomic_group_create_job(
417 synch_count, host_objects, metahost_objects,
418 dependencies, atomic_group, labels_by_name)
419 else:
420 if synch_count is not None and synch_count > len(all_host_objects):
421 raise model_logic.ValidationError(
422 {'hosts':
423 'only %d hosts provided for job with synch_count = %d' %
424 (len(all_host_objects), synch_count)})
showard65c267d2009-01-20 23:24:47 +0000425
showard989f25d2008-10-01 11:38:11 +0000426 rpc_utils.check_job_dependencies(host_objects, dependencies)
427 dependency_labels = [labels_by_name[label_name]
428 for label_name in dependencies]
429
jadmanski0afbb632008-06-06 21:10:57 +0000430 job = models.Job.create(owner=owner, name=name, priority=priority,
431 control_file=control_file,
432 control_type=control_type,
showard2bab8f42008-11-12 18:15:22 +0000433 synch_count=synch_count,
showard65c267d2009-01-20 23:24:47 +0000434 hosts=all_host_objects,
showard909c7a62008-07-15 21:52:38 +0000435 timeout=timeout,
showard542e8402008-09-19 20:16:18 +0000436 run_verify=run_verify,
showard989f25d2008-10-01 11:38:11 +0000437 email_list=email_list.strip(),
showard21baa452008-10-21 00:08:39 +0000438 dependencies=dependency_labels,
439 reboot_before=reboot_before,
440 reboot_after=reboot_after)
showardc92da832009-04-07 18:14:34 +0000441 job.queue(all_host_objects, atomic_group=atomic_group)
jadmanski0afbb632008-06-06 21:10:57 +0000442 return job.id
mblighe8819cd2008-02-15 16:48:40 +0000443
444
showard9dbdcda2008-10-14 17:34:36 +0000445def abort_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000446 """\
showard9dbdcda2008-10-14 17:34:36 +0000447 Abort a set of host queue entries.
jadmanski0afbb632008-06-06 21:10:57 +0000448 """
showard9dbdcda2008-10-14 17:34:36 +0000449 query = models.HostQueueEntry.query_objects(filter_data)
showard0c185192009-01-16 03:07:57 +0000450 query = query.filter(complete=False)
showarddc817512008-11-12 18:16:41 +0000451 models.AclGroup.check_abort_permissions(query)
showard9dbdcda2008-10-14 17:34:36 +0000452 host_queue_entries = list(query.select_related())
showard2bab8f42008-11-12 18:15:22 +0000453 rpc_utils.check_abort_synchronous_jobs(host_queue_entries)
mblighe8819cd2008-02-15 16:48:40 +0000454
showard9dbdcda2008-10-14 17:34:36 +0000455 user = thread_local.get_user()
456 for queue_entry in host_queue_entries:
457 queue_entry.abort(user)
showard9d821ab2008-07-11 16:54:29 +0000458
459
mblighe8819cd2008-02-15 16:48:40 +0000460def get_jobs(not_yet_run=False, running=False, finished=False, **filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000461 """\
462 Extra filter args for get_jobs:
463 -not_yet_run: Include only jobs that have not yet started running.
464 -running: Include only jobs that have start running but for which not
465 all hosts have completed.
466 -finished: Include only jobs for which all hosts have completed (or
467 aborted).
468 At most one of these three fields should be specified.
469 """
470 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
471 running,
472 finished)
showard989f25d2008-10-01 11:38:11 +0000473 jobs = models.Job.list_objects(filter_data)
474 models.Job.objects.populate_dependencies(jobs)
475 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000476
477
478def get_num_jobs(not_yet_run=False, running=False, finished=False,
jadmanski0afbb632008-06-06 21:10:57 +0000479 **filter_data):
480 """\
481 See get_jobs() for documentation of extra filter parameters.
482 """
483 filter_data['extra_args'] = rpc_utils.extra_job_filters(not_yet_run,
484 running,
485 finished)
486 return models.Job.query_count(filter_data)
mblighe8819cd2008-02-15 16:48:40 +0000487
488
mblighe8819cd2008-02-15 16:48:40 +0000489def get_jobs_summary(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000490 """\
showarda8709c52008-07-03 19:44:54 +0000491 Like get_jobs(), but adds a 'status_counts' field, which is a dictionary
jadmanski0afbb632008-06-06 21:10:57 +0000492 mapping status strings to the number of hosts currently with that
493 status, i.e. {'Queued' : 4, 'Running' : 2}.
494 """
495 jobs = get_jobs(**filter_data)
496 ids = [job['id'] for job in jobs]
497 all_status_counts = models.Job.objects.get_status_counts(ids)
498 for job in jobs:
499 job['status_counts'] = all_status_counts[job['id']]
500 return rpc_utils.prepare_for_serialization(jobs)
mblighe8819cd2008-02-15 16:48:40 +0000501
502
showard945072f2008-09-03 20:34:59 +0000503def get_info_for_clone(id, preserve_metahosts):
showarda8709c52008-07-03 19:44:54 +0000504 """\
505 Retrieves all the information needed to clone a job.
506 """
507 info = {}
508 job = models.Job.objects.get(id=id)
showard45195442009-02-17 20:55:48 +0000509 query = job.hostqueueentry_set.filter()
showard945072f2008-09-03 20:34:59 +0000510
511 hosts = []
512 meta_hosts = []
showardc92da832009-04-07 18:14:34 +0000513 atomic_group_name = None
showard945072f2008-09-03 20:34:59 +0000514
515 # For each queue entry, if the entry contains a host, add the entry into the
516 # hosts list if either:
517 # It is not a metahost.
518 # It was an assigned metahost, and the user wants to keep the specific
519 # assignments.
520 # Otherwise, add the metahost to the metahosts list.
521 for queue_entry in query:
522 if (queue_entry.host and (preserve_metahosts
523 or not queue_entry.meta_host)):
showard45195442009-02-17 20:55:48 +0000524 if queue_entry.deleted:
525 continue
showard945072f2008-09-03 20:34:59 +0000526 hosts.append(queue_entry.host)
527 else:
528 meta_hosts.append(queue_entry.meta_host.name)
showardc92da832009-04-07 18:14:34 +0000529 if atomic_group_name is None:
530 atomic_group_name = queue_entry.atomic_group.name
531 else:
532 assert atomic_group_name == queue_entry.atomic_group.name, (
533 'DB inconsistency. HostQueueEntries with multiple atomic'
534 ' groups on job %s: %s != %s' % (
535 id, atomic_group_name, queue_entry.atomic_group.name))
showard945072f2008-09-03 20:34:59 +0000536
showardd9992fe2008-07-31 02:15:03 +0000537 host_dicts = []
showarda8709c52008-07-03 19:44:54 +0000538
showardd9992fe2008-07-31 02:15:03 +0000539 for host in hosts:
showardd9992fe2008-07-31 02:15:03 +0000540 # one-time host
541 if host.invalid:
showardbad4f2d2008-08-15 18:13:47 +0000542 host_dict = {}
543 host_dict['hostname'] = host.hostname
544 host_dict['id'] = host.id
showardd9992fe2008-07-31 02:15:03 +0000545 host_dict['platform'] = '(one-time host)'
546 host_dict['locked_text'] = ''
showardd9992fe2008-07-31 02:15:03 +0000547 else:
showardbad4f2d2008-08-15 18:13:47 +0000548 host_dict = get_hosts(id=host.id)[0]
549 other_labels = host_dict['labels']
550 if host_dict['platform']:
551 other_labels.remove(host_dict['platform'])
552 host_dict['other_labels'] = ', '.join(other_labels)
showardd9992fe2008-07-31 02:15:03 +0000553 host_dicts.append(host_dict)
showarda8709c52008-07-03 19:44:54 +0000554
555 meta_host_counts = {}
556 for meta_host in meta_hosts:
557 meta_host_counts.setdefault(meta_host, 0)
558 meta_host_counts[meta_host] += 1
559
560 info['job'] = job.get_object_dict()
showard2e9415f2008-12-05 19:48:38 +0000561 info['job']['dependencies'] = [label.name for label
562 in job.dependency_labels.all()]
showarda8709c52008-07-03 19:44:54 +0000563 info['meta_host_counts'] = meta_host_counts
showardd9992fe2008-07-31 02:15:03 +0000564 info['hosts'] = host_dicts
showardc92da832009-04-07 18:14:34 +0000565 info['atomic_group_name'] = atomic_group_name
showarda8709c52008-07-03 19:44:54 +0000566
567 return rpc_utils.prepare_for_serialization(info)
568
569
showard34dc5fa2008-04-24 20:58:40 +0000570# host queue entries
571
572def get_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000573 """\
showardc92da832009-04-07 18:14:34 +0000574 @returns A sequence of nested dictionaries of host and job information.
jadmanski0afbb632008-06-06 21:10:57 +0000575 """
showardc92da832009-04-07 18:14:34 +0000576 return rpc_utils.prepare_rows_as_nested_dicts(
577 models.HostQueueEntry.query_objects(filter_data),
578 ('host', 'atomic_group', 'job'))
showard34dc5fa2008-04-24 20:58:40 +0000579
580
581def get_num_host_queue_entries(**filter_data):
jadmanski0afbb632008-06-06 21:10:57 +0000582 """\
583 Get the number of host queue entries associated with this job.
584 """
585 return models.HostQueueEntry.query_count(filter_data)
showard34dc5fa2008-04-24 20:58:40 +0000586
587
showard1e935f12008-07-11 00:11:36 +0000588def get_hqe_percentage_complete(**filter_data):
589 """
showardc92da832009-04-07 18:14:34 +0000590 Computes the fraction of host queue entries matching the given filter data
showard1e935f12008-07-11 00:11:36 +0000591 that are complete.
592 """
593 query = models.HostQueueEntry.query_objects(filter_data)
594 complete_count = query.filter(complete=True).count()
595 total_count = query.count()
596 if total_count == 0:
597 return 1
598 return float(complete_count) / total_count
599
600
mblighe8819cd2008-02-15 16:48:40 +0000601# other
602
showarde0b63622008-08-04 20:58:47 +0000603def echo(data=""):
604 """\
605 Returns a passed in string. For doing a basic test to see if RPC calls
606 can successfully be made.
607 """
608 return data
609
610
mblighe8819cd2008-02-15 16:48:40 +0000611def get_static_data():
jadmanski0afbb632008-06-06 21:10:57 +0000612 """\
613 Returns a dictionary containing a bunch of data that shouldn't change
614 often and is otherwise inaccessible. This includes:
showardc92da832009-04-07 18:14:34 +0000615
616 priorities: List of job priority choices.
617 default_priority: Default priority value for new jobs.
618 users: Sorted list of all users.
619 labels: Sorted list of all labels.
620 atomic_groups: Sorted list of all atomic groups.
621 tests: Sorted list of all tests.
622 profilers: Sorted list of all profilers.
623 current_user: Logged-in username.
624 host_statuses: Sorted list of possible Host statuses.
625 job_statuses: Sorted list of possible HostQueueEntry statuses.
626 job_timeout_default: The default job timeout length in hours.
627 reboot_before_options: A list of valid RebootBefore string enums.
628 reboot_after_options: A list of valid RebootAfter string enums.
629 motd: Server's message of the day.
630 status_dictionary: A mapping from one word job status names to a more
631 informative description.
jadmanski0afbb632008-06-06 21:10:57 +0000632 """
showard21baa452008-10-21 00:08:39 +0000633
634 job_fields = models.Job.get_field_dict()
635
jadmanski0afbb632008-06-06 21:10:57 +0000636 result = {}
637 result['priorities'] = models.Job.Priority.choices()
showard21baa452008-10-21 00:08:39 +0000638 default_priority = job_fields['priority'].default
jadmanski0afbb632008-06-06 21:10:57 +0000639 default_string = models.Job.Priority.get_string(default_priority)
640 result['default_priority'] = default_string
641 result['users'] = get_users(sort_by=['login'])
642 result['labels'] = get_labels(sort_by=['-platform', 'name'])
showardc92da832009-04-07 18:14:34 +0000643 result['atomic_groups'] = get_atomic_groups(sort_by=['name'])
jadmanski0afbb632008-06-06 21:10:57 +0000644 result['tests'] = get_tests(sort_by=['name'])
showard2b9a88b2008-06-13 20:55:03 +0000645 result['profilers'] = get_profilers(sort_by=['name'])
showard0fc38302008-10-23 00:44:07 +0000646 result['current_user'] = rpc_utils.prepare_for_serialization(
647 thread_local.get_user().get_object_dict())
showard2b9a88b2008-06-13 20:55:03 +0000648 result['host_statuses'] = sorted(models.Host.Status.names)
mbligh5a198b92008-12-11 19:33:29 +0000649 result['job_statuses'] = sorted(models.HostQueueEntry.Status.names)
showardb1e51872008-10-07 11:08:18 +0000650 result['job_timeout_default'] = models.Job.DEFAULT_TIMEOUT
showard0fc38302008-10-23 00:44:07 +0000651 result['reboot_before_options'] = models.RebootBefore.names
652 result['reboot_after_options'] = models.RebootAfter.names
showard8fbae652009-01-20 23:23:10 +0000653 result['motd'] = rpc_utils.get_motd()
showard8ac29b42008-07-17 17:01:55 +0000654
showard95128e52008-08-04 20:59:34 +0000655 result['status_dictionary'] = {"Abort": "Abort",
656 "Aborted": "Aborted",
showard8ac29b42008-07-17 17:01:55 +0000657 "Verifying": "Verifying Host",
658 "Pending": "Waiting on other hosts",
659 "Running": "Running autoserv",
660 "Completed": "Autoserv completed",
661 "Failed": "Failed to complete",
662 "Aborting": "Abort in progress",
showardd823b362008-07-24 16:35:46 +0000663 "Queued": "Queued",
showard5deb6772008-11-04 21:54:33 +0000664 "Starting": "Next in host's queue",
665 "Stopped": "Other host(s) failed verify",
666 "Parsing": "Awaiting parse of final results"}
jadmanski0afbb632008-06-06 21:10:57 +0000667 return result