blob: 727df44e6f6fb8d7dbdbc3550f2e799e3ded1c47 [file] [log] [blame]
mbligh67647152008-11-19 00:18:14 +00001# Copyright Martin J. Bligh, Google Inc 2008
2# Released under the GPL v2
3
4"""
5This class allows you to communicate with the frontend to submit jobs etc
6It is designed for writing more sophisiticated server-side control files that
7can recursively add and manage other jobs.
8
9We turn the JSON dictionaries into real objects that are more idiomatic
10
mblighc31e4022008-12-11 19:32:30 +000011For docs, see:
jamesren1a2914a2010-02-12 00:44:31 +000012 http://autotest/afe/server/rpc_doc/
13 http://autotest/new_tko/server/rpc_doc/
mblighc31e4022008-12-11 19:32:30 +000014 http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-api
mbligh67647152008-11-19 00:18:14 +000015"""
16
mblighdb59e3c2009-11-21 01:45:18 +000017import getpass, os, time, traceback, re
mbligh67647152008-11-19 00:18:14 +000018import common
19from autotest_lib.frontend.afe import rpc_client_lib
mbligh37eceaa2008-12-15 22:56:37 +000020from autotest_lib.client.common_lib import global_config
mbligh67647152008-11-19 00:18:14 +000021from autotest_lib.client.common_lib import utils
mbligh4e576612008-12-22 14:56:36 +000022try:
23 from autotest_lib.server.site_common import site_utils as server_utils
24except:
25 from autotest_lib.server import utils as server_utils
26form_ntuples_from_machines = server_utils.form_ntuples_from_machines
mbligh67647152008-11-19 00:18:14 +000027
mbligh37eceaa2008-12-15 22:56:37 +000028GLOBAL_CONFIG = global_config.global_config
29DEFAULT_SERVER = 'autotest'
30
mbligh67647152008-11-19 00:18:14 +000031def dump_object(header, obj):
32 """
33 Standard way to print out the frontend objects (eg job, host, acl, label)
34 in a human-readable fashion for debugging
35 """
36 result = header + '\n'
37 for key in obj.hash:
38 if key == 'afe' or key == 'hash':
39 continue
40 result += '%20s: %s\n' % (key, obj.hash[key])
41 return result
42
43
mbligh5280e3b2008-12-22 14:39:28 +000044class RpcClient(object):
mbligh67647152008-11-19 00:18:14 +000045 """
mbligh451ede12009-02-12 21:54:03 +000046 Abstract RPC class for communicating with the autotest frontend
47 Inherited for both TKO and AFE uses.
mbligh67647152008-11-19 00:18:14 +000048
mbligh1ef218d2009-08-03 16:57:56 +000049 All the constructors go in the afe / tko class.
mbligh451ede12009-02-12 21:54:03 +000050 Manipulating methods go in the object classes themselves
mbligh67647152008-11-19 00:18:14 +000051 """
mbligh99b24f42009-06-08 16:45:55 +000052 def __init__(self, path, user, server, print_log, debug, reply_debug):
mbligh67647152008-11-19 00:18:14 +000053 """
mbligh451ede12009-02-12 21:54:03 +000054 Create a cached instance of a connection to the frontend
mbligh67647152008-11-19 00:18:14 +000055
56 user: username to connect as
mbligh451ede12009-02-12 21:54:03 +000057 server: frontend server to connect to
mbligh67647152008-11-19 00:18:14 +000058 print_log: pring a logging message to stdout on every operation
59 debug: print out all RPC traffic
60 """
mblighc31e4022008-12-11 19:32:30 +000061 if not user:
mblighdb59e3c2009-11-21 01:45:18 +000062 user = getpass.getuser()
mbligh451ede12009-02-12 21:54:03 +000063 if not server:
mbligh475f7762009-01-30 00:34:04 +000064 if 'AUTOTEST_WEB' in os.environ:
mbligh451ede12009-02-12 21:54:03 +000065 server = os.environ['AUTOTEST_WEB']
mbligh475f7762009-01-30 00:34:04 +000066 else:
mbligh451ede12009-02-12 21:54:03 +000067 server = GLOBAL_CONFIG.get_config_value('SERVER', 'hostname',
68 default=DEFAULT_SERVER)
69 self.server = server
mbligh67647152008-11-19 00:18:14 +000070 self.user = user
71 self.print_log = print_log
72 self.debug = debug
mbligh99b24f42009-06-08 16:45:55 +000073 self.reply_debug = reply_debug
jamesren1a2914a2010-02-12 00:44:31 +000074 http_server = 'http://' + server
75 headers = rpc_client_lib.authorization_headers(user, http_server)
76 rpc_server = http_server + path
mbligh1354c9d2008-12-22 14:56:13 +000077 if debug:
78 print 'SERVER: %s' % rpc_server
79 print 'HEADERS: %s' % headers
mbligh67647152008-11-19 00:18:14 +000080 self.proxy = rpc_client_lib.get_proxy(rpc_server, headers=headers)
81
82
83 def run(self, call, **dargs):
84 """
85 Make a RPC call to the AFE server
86 """
87 rpc_call = getattr(self.proxy, call)
88 if self.debug:
89 print 'DEBUG: %s %s' % (call, dargs)
mbligh451ede12009-02-12 21:54:03 +000090 try:
mbligh99b24f42009-06-08 16:45:55 +000091 result = utils.strip_unicode(rpc_call(**dargs))
92 if self.reply_debug:
93 print result
94 return result
mbligh451ede12009-02-12 21:54:03 +000095 except Exception:
96 print 'FAILED RPC CALL: %s %s' % (call, dargs)
97 raise
mbligh67647152008-11-19 00:18:14 +000098
99
100 def log(self, message):
101 if self.print_log:
102 print message
103
104
jamesrenc3940222010-02-19 21:57:37 +0000105class Planner(RpcClient):
106 def __init__(self, user=None, server=None, print_log=True, debug=False,
107 reply_debug=False):
108 super(Planner, self).__init__(path='/planner/server/rpc/',
109 user=user,
110 server=server,
111 print_log=print_log,
112 debug=debug,
113 reply_debug=reply_debug)
114
115
mbligh5280e3b2008-12-22 14:39:28 +0000116class TKO(RpcClient):
mbligh99b24f42009-06-08 16:45:55 +0000117 def __init__(self, user=None, server=None, print_log=True, debug=False,
118 reply_debug=False):
jamesren1a2914a2010-02-12 00:44:31 +0000119 super(TKO, self).__init__(path='/new_tko/server/rpc/',
mbligh99b24f42009-06-08 16:45:55 +0000120 user=user,
121 server=server,
122 print_log=print_log,
123 debug=debug,
124 reply_debug=reply_debug)
mblighc31e4022008-12-11 19:32:30 +0000125
126
127 def get_status_counts(self, job, **data):
128 entries = self.run('get_status_counts',
mbligh1ef218d2009-08-03 16:57:56 +0000129 group_by=['hostname', 'test_name', 'reason'],
mblighc31e4022008-12-11 19:32:30 +0000130 job_tag__startswith='%s-' % job, **data)
mbligh5280e3b2008-12-22 14:39:28 +0000131 return [TestStatus(self, e) for e in entries['groups']]
mblighc31e4022008-12-11 19:32:30 +0000132
133
mbligh5280e3b2008-12-22 14:39:28 +0000134class AFE(RpcClient):
mbligh17c75e62009-06-08 16:18:21 +0000135 def __init__(self, user=None, server=None, print_log=True, debug=False,
mbligh99b24f42009-06-08 16:45:55 +0000136 reply_debug=False, job=None):
mbligh17c75e62009-06-08 16:18:21 +0000137 self.job = job
jamesren1a2914a2010-02-12 00:44:31 +0000138 super(AFE, self).__init__(path='/afe/server/rpc/',
mbligh99b24f42009-06-08 16:45:55 +0000139 user=user,
140 server=server,
141 print_log=print_log,
142 debug=debug,
143 reply_debug=reply_debug)
mblighc31e4022008-12-11 19:32:30 +0000144
mbligh1ef218d2009-08-03 16:57:56 +0000145
mbligh67647152008-11-19 00:18:14 +0000146 def host_statuses(self, live=None):
mblighc2847b72009-03-25 19:32:20 +0000147 dead_statuses = ['Dead', 'Repair Failed', 'Repairing']
mbligh67647152008-11-19 00:18:14 +0000148 statuses = self.run('get_static_data')['host_statuses']
149 if live == True:
mblighc2847b72009-03-25 19:32:20 +0000150 return list(set(statuses) - set(dead_statuses))
mbligh67647152008-11-19 00:18:14 +0000151 if live == False:
152 return dead_statuses
153 else:
154 return statuses
155
156
mbligh71094012009-12-19 05:35:21 +0000157 @staticmethod
158 def _dict_for_host_query(hostnames=(), status=None, label=None):
159 query_args = {}
mbligh4e545a52009-12-19 05:30:39 +0000160 if hostnames:
161 query_args['hostname__in'] = hostnames
162 if status:
163 query_args['status'] = status
164 if label:
165 query_args['labels__name'] = label
mbligh71094012009-12-19 05:35:21 +0000166 return query_args
167
168
169 def get_hosts(self, hostnames=(), status=None, label=None, **dargs):
170 query_args = dict(dargs)
171 query_args.update(self._dict_for_host_query(hostnames=hostnames,
172 status=status,
173 label=label))
174 hosts = self.run('get_hosts', **query_args)
175 return [Host(self, h) for h in hosts]
176
177
178 def get_hostnames(self, status=None, label=None, **dargs):
179 """Like get_hosts() but returns hostnames instead of Host objects."""
180 # This implementation can be replaced with a more efficient one
181 # that does not query for entire host objects in the future.
182 return [host_obj.hostname for host_obj in
183 self.get_hosts(status=status, label=label, **dargs)]
184
185
186 def reverify_hosts(self, hostnames=(), status=None, label=None):
187 query_args = dict(locked=False,
188 aclgroup__users__login=self.user)
189 query_args.update(self._dict_for_host_query(hostnames=hostnames,
190 status=status,
191 label=label))
mbligh4e545a52009-12-19 05:30:39 +0000192 return self.run('reverify_hosts', **query_args)
193
194
mbligh67647152008-11-19 00:18:14 +0000195 def create_host(self, hostname, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000196 id = self.run('add_host', hostname=hostname, **dargs)
mbligh67647152008-11-19 00:18:14 +0000197 return self.get_hosts(id=id)[0]
198
199
200 def get_labels(self, **dargs):
201 labels = self.run('get_labels', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000202 return [Label(self, l) for l in labels]
mbligh67647152008-11-19 00:18:14 +0000203
204
205 def create_label(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000206 id = self.run('add_label', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000207 return self.get_labels(id=id)[0]
208
209
210 def get_acls(self, **dargs):
211 acls = self.run('get_acl_groups', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000212 return [Acl(self, a) for a in acls]
mbligh67647152008-11-19 00:18:14 +0000213
214
215 def create_acl(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000216 id = self.run('add_acl_group', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000217 return self.get_acls(id=id)[0]
218
219
mbligh54459c72009-01-21 19:26:44 +0000220 def get_users(self, **dargs):
221 users = self.run('get_users', **dargs)
222 return [User(self, u) for u in users]
223
224
mbligh1354c9d2008-12-22 14:56:13 +0000225 def generate_control_file(self, tests, **dargs):
226 ret = self.run('generate_control_file', tests=tests, **dargs)
227 return ControlFile(self, ret)
228
229
mbligh67647152008-11-19 00:18:14 +0000230 def get_jobs(self, summary=False, **dargs):
231 if summary:
232 jobs_data = self.run('get_jobs_summary', **dargs)
233 else:
234 jobs_data = self.run('get_jobs', **dargs)
mblighafbba0c2009-06-08 16:44:45 +0000235 jobs = []
236 for j in jobs_data:
237 job = Job(self, j)
238 # Set up some extra information defaults
239 job.testname = re.sub('\s.*', '', job.name) # arbitrary default
240 job.platform_results = {}
241 job.platform_reasons = {}
242 jobs.append(job)
243 return jobs
mbligh67647152008-11-19 00:18:14 +0000244
245
246 def get_host_queue_entries(self, **data):
247 entries = self.run('get_host_queue_entries', **data)
mblighf9e35862009-02-26 01:03:11 +0000248 job_statuses = [JobStatus(self, e) for e in entries]
mbligh99b24f42009-06-08 16:45:55 +0000249
250 # Sadly, get_host_queue_entries doesn't return platforms, we have
251 # to get those back from an explicit get_hosts queury, then patch
252 # the new host objects back into the host list.
253 hostnames = [s.host.hostname for s in job_statuses if s.host]
254 host_hash = {}
255 for host in self.get_hosts(hostname__in=hostnames):
256 host_hash[host.hostname] = host
257 for status in job_statuses:
258 if status.host:
259 status.host = host_hash[status.host.hostname]
mblighf9e35862009-02-26 01:03:11 +0000260 # filter job statuses that have either host or meta_host
261 return [status for status in job_statuses if (status.host or
262 status.meta_host)]
mbligh67647152008-11-19 00:18:14 +0000263
264
mblighb9db5162009-04-17 22:21:41 +0000265 def create_job_by_test(self, tests, kernel=None, use_container=False,
mbligh1354c9d2008-12-22 14:56:13 +0000266 **dargs):
mbligh67647152008-11-19 00:18:14 +0000267 """
268 Given a test name, fetch the appropriate control file from the server
mbligh4e576612008-12-22 14:56:36 +0000269 and submit it.
270
271 Returns a list of job objects
mbligh67647152008-11-19 00:18:14 +0000272 """
mblighb9db5162009-04-17 22:21:41 +0000273 assert ('hosts' in dargs or
274 'atomic_group_name' in dargs and 'synch_count' in dargs)
showarda2cd72b2009-10-01 18:43:53 +0000275 if kernel:
276 kernel_list = re.split('[\s,]+', kernel.strip())
277 kernel_info = [{'version': version} for version in kernel_list]
278 else:
279 kernel_info = None
280 control_file = self.generate_control_file(
281 tests=tests, kernel=kernel_info, use_container=use_container,
282 do_push_packages=True)
mbligh1354c9d2008-12-22 14:56:13 +0000283 if control_file.is_server:
mbligh67647152008-11-19 00:18:14 +0000284 dargs['control_type'] = 'Server'
285 else:
286 dargs['control_type'] = 'Client'
287 dargs['dependencies'] = dargs.get('dependencies', []) + \
mbligh1354c9d2008-12-22 14:56:13 +0000288 control_file.dependencies
289 dargs['control_file'] = control_file.control_file
mbligh672666c2009-07-28 23:22:13 +0000290 if not dargs.get('synch_count', None):
mblighc99fccf2009-07-11 00:59:33 +0000291 dargs['synch_count'] = control_file.synch_count
mblighb9db5162009-04-17 22:21:41 +0000292 if 'hosts' in dargs and len(dargs['hosts']) < dargs['synch_count']:
293 # will not be able to satisfy this request
mbligh38b09152009-04-28 18:34:25 +0000294 return None
295 return self.create_job(**dargs)
mbligh67647152008-11-19 00:18:14 +0000296
297
298 def create_job(self, control_file, name=' ', priority='Medium',
299 control_type='Client', **dargs):
300 id = self.run('create_job', name=name, priority=priority,
301 control_file=control_file, control_type=control_type, **dargs)
302 return self.get_jobs(id=id)[0]
303
304
mbligh282ce892010-01-06 18:40:17 +0000305 def run_test_suites(self, pairings, kernel, kernel_label=None,
306 priority='Medium', wait=True, poll_interval=10,
307 email_from=None, email_to=None, timeout=168):
mbligh5b618382008-12-03 15:24:01 +0000308 """
309 Run a list of test suites on a particular kernel.
mbligh1ef218d2009-08-03 16:57:56 +0000310
mbligh5b618382008-12-03 15:24:01 +0000311 Poll for them to complete, and return whether they worked or not.
mbligh1ef218d2009-08-03 16:57:56 +0000312
mbligh282ce892010-01-06 18:40:17 +0000313 @param pairings: List of MachineTestPairing objects to invoke.
314 @param kernel: Name of the kernel to run.
315 @param kernel_label: Label (string) of the kernel to run such as
316 '<kernel-version> : <config> : <date>'
317 If any pairing object has its job_label attribute set it
318 will override this value for that particular job.
319 @param wait: boolean - Wait for the results to come back?
320 @param poll_interval: Interval between polling for job results (in mins)
321 @param email_from: Send notification email upon completion from here.
322 @param email_from: Send notification email upon completion to here.
mbligh5b618382008-12-03 15:24:01 +0000323 """
324 jobs = []
325 for pairing in pairings:
mbligh0c4f8d72009-05-12 20:52:18 +0000326 try:
327 new_job = self.invoke_test(pairing, kernel, kernel_label,
328 priority, timeout=timeout)
329 if not new_job:
330 continue
mbligh0c4f8d72009-05-12 20:52:18 +0000331 jobs.append(new_job)
332 except Exception, e:
333 traceback.print_exc()
mblighb9db5162009-04-17 22:21:41 +0000334 if not wait or not jobs:
mbligh5b618382008-12-03 15:24:01 +0000335 return
mbligh5280e3b2008-12-22 14:39:28 +0000336 tko = TKO()
mbligh5b618382008-12-03 15:24:01 +0000337 while True:
338 time.sleep(60 * poll_interval)
mbligh5280e3b2008-12-22 14:39:28 +0000339 result = self.poll_all_jobs(tko, jobs, email_from, email_to)
mbligh5b618382008-12-03 15:24:01 +0000340 if result is not None:
341 return result
342
343
mbligh45ffc432008-12-09 23:35:17 +0000344 def result_notify(self, job, email_from, email_to):
mbligh5b618382008-12-03 15:24:01 +0000345 """
mbligh45ffc432008-12-09 23:35:17 +0000346 Notify about the result of a job. Will always print, if email data
347 is provided, will send email for it as well.
348
349 job: job object to notify about
350 email_from: send notification email upon completion from here
351 email_from: send notification email upon completion to here
352 """
353 if job.result == True:
354 subject = 'Testing PASSED: '
355 else:
356 subject = 'Testing FAILED: '
357 subject += '%s : %s\n' % (job.name, job.id)
358 text = []
359 for platform in job.results_platform_map:
360 for status in job.results_platform_map[platform]:
361 if status == 'Total':
362 continue
mbligh451ede12009-02-12 21:54:03 +0000363 for host in job.results_platform_map[platform][status]:
364 text.append('%20s %10s %10s' % (platform, status, host))
365 if status == 'Failed':
366 for test_status in job.test_status[host].fail:
367 text.append('(%s, %s) : %s' % \
368 (host, test_status.test_name,
369 test_status.reason))
370 text.append('')
mbligh37eceaa2008-12-15 22:56:37 +0000371
mbligh451ede12009-02-12 21:54:03 +0000372 base_url = 'http://' + self.server
mbligh37eceaa2008-12-15 22:56:37 +0000373
374 params = ('columns=test',
375 'rows=machine_group',
376 "condition=tag~'%s-%%25'" % job.id,
377 'title=Report')
378 query_string = '&'.join(params)
mbligh451ede12009-02-12 21:54:03 +0000379 url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string)
380 text.append(url + '\n')
381 url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
382 text.append(url + '\n')
mbligh37eceaa2008-12-15 22:56:37 +0000383
384 body = '\n'.join(text)
385 print '---------------------------------------------------'
386 print 'Subject: ', subject
mbligh45ffc432008-12-09 23:35:17 +0000387 print body
mbligh37eceaa2008-12-15 22:56:37 +0000388 print '---------------------------------------------------'
mbligh45ffc432008-12-09 23:35:17 +0000389 if email_from and email_to:
mbligh37eceaa2008-12-15 22:56:37 +0000390 print 'Sending email ...'
mbligh45ffc432008-12-09 23:35:17 +0000391 utils.send_email(email_from, email_to, subject, body)
392 print
mbligh37eceaa2008-12-15 22:56:37 +0000393
mbligh45ffc432008-12-09 23:35:17 +0000394
mbligh1354c9d2008-12-22 14:56:13 +0000395 def print_job_result(self, job):
396 """
397 Print the result of a single job.
398 job: a job object
399 """
400 if job.result is None:
401 print 'PENDING',
402 elif job.result == True:
403 print 'PASSED',
404 elif job.result == False:
405 print 'FAILED',
mbligh912c3f32009-03-25 19:31:30 +0000406 elif job.result == "Abort":
407 print 'ABORT',
mbligh1354c9d2008-12-22 14:56:13 +0000408 print ' %s : %s' % (job.id, job.name)
409
410
mbligh451ede12009-02-12 21:54:03 +0000411 def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None):
mbligh45ffc432008-12-09 23:35:17 +0000412 """
413 Poll all jobs in a list.
414 jobs: list of job objects to poll
415 email_from: send notification email upon completion from here
416 email_from: send notification email upon completion to here
417
418 Returns:
mbligh5b618382008-12-03 15:24:01 +0000419 a) All complete successfully (return True)
420 b) One or more has failed (return False)
421 c) Cannot tell yet (return None)
422 """
mbligh45ffc432008-12-09 23:35:17 +0000423 results = []
mbligh5b618382008-12-03 15:24:01 +0000424 for job in jobs:
mbligh676dcbe2009-06-15 21:57:27 +0000425 if getattr(job, 'result', None) is None:
426 job.result = self.poll_job_results(tko, job)
427 if job.result is not None:
428 self.result_notify(job, email_from, email_to)
mbligh45ffc432008-12-09 23:35:17 +0000429
mbligh676dcbe2009-06-15 21:57:27 +0000430 results.append(job.result)
mbligh1354c9d2008-12-22 14:56:13 +0000431 self.print_job_result(job)
mbligh45ffc432008-12-09 23:35:17 +0000432
433 if None in results:
434 return None
mbligh912c3f32009-03-25 19:31:30 +0000435 elif False in results or "Abort" in results:
mbligh45ffc432008-12-09 23:35:17 +0000436 return False
437 else:
438 return True
mbligh5b618382008-12-03 15:24:01 +0000439
440
mbligh1f23f362008-12-22 14:46:12 +0000441 def _included_platform(self, host, platforms):
442 """
443 See if host's platforms matches any of the patterns in the included
444 platforms list.
445 """
446 if not platforms:
447 return True # No filtering of platforms
448 for platform in platforms:
449 if re.search(platform, host.platform):
450 return True
451 return False
452
453
mbligh7b312282009-01-07 16:45:43 +0000454 def invoke_test(self, pairing, kernel, kernel_label, priority='Medium',
455 **dargs):
mbligh5b618382008-12-03 15:24:01 +0000456 """
457 Given a pairing of a control file to a machine label, find all machines
458 with that label, and submit that control file to them.
mbligh1ef218d2009-08-03 16:57:56 +0000459
mbligh282ce892010-01-06 18:40:17 +0000460 @param kernel_label: Label (string) of the kernel to run such as
461 '<kernel-version> : <config> : <date>'
462 If any pairing object has its job_label attribute set it
463 will override this value for that particular job.
464
465 @returns A list of job objects.
mbligh5b618382008-12-03 15:24:01 +0000466 """
mbligh282ce892010-01-06 18:40:17 +0000467 # The pairing can override the job label.
468 if pairing.job_label:
469 kernel_label = pairing.job_label
mbligh5b618382008-12-03 15:24:01 +0000470 job_name = '%s : %s' % (pairing.machine_label, kernel_label)
471 hosts = self.get_hosts(multiple_labels=[pairing.machine_label])
mbligh1f23f362008-12-22 14:46:12 +0000472 platforms = pairing.platforms
473 hosts = [h for h in hosts if self._included_platform(h, platforms)]
mblighc2847b72009-03-25 19:32:20 +0000474 dead_statuses = self.host_statuses(live=False)
475 host_list = [h.hostname for h in hosts if h.status not in dead_statuses]
mbligh1f23f362008-12-22 14:46:12 +0000476 print 'HOSTS: %s' % host_list
mblighb9db5162009-04-17 22:21:41 +0000477 if pairing.atomic_group_sched:
mblighc99fccf2009-07-11 00:59:33 +0000478 dargs['synch_count'] = pairing.synch_count
mblighb9db5162009-04-17 22:21:41 +0000479 dargs['atomic_group_name'] = pairing.machine_label
480 else:
481 dargs['hosts'] = host_list
mbligh38b09152009-04-28 18:34:25 +0000482 new_job = self.create_job_by_test(name=job_name,
mbligh17c75e62009-06-08 16:18:21 +0000483 dependencies=[pairing.machine_label],
484 tests=[pairing.control_file],
485 priority=priority,
486 kernel=kernel,
487 use_container=pairing.container,
488 **dargs)
mbligh38b09152009-04-28 18:34:25 +0000489 if new_job:
mbligh17c75e62009-06-08 16:18:21 +0000490 if pairing.testname:
491 new_job.testname = pairing.testname
mbligh4e576612008-12-22 14:56:36 +0000492 print 'Invoked test %s : %s' % (new_job.id, job_name)
mbligh38b09152009-04-28 18:34:25 +0000493 return new_job
mbligh5b618382008-12-03 15:24:01 +0000494
495
mblighb9db5162009-04-17 22:21:41 +0000496 def _job_test_results(self, tko, job, debug, tests=[]):
mbligh5b618382008-12-03 15:24:01 +0000497 """
mbligh5280e3b2008-12-22 14:39:28 +0000498 Retrieve test results for a job
mbligh5b618382008-12-03 15:24:01 +0000499 """
mbligh5280e3b2008-12-22 14:39:28 +0000500 job.test_status = {}
501 try:
502 test_statuses = tko.get_status_counts(job=job.id)
503 except Exception:
504 print "Ignoring exception on poll job; RPC interface is flaky"
505 traceback.print_exc()
506 return
507
508 for test_status in test_statuses:
mbligh7479a182009-01-07 16:46:24 +0000509 # SERVER_JOB is buggy, and often gives false failures. Ignore it.
510 if test_status.test_name == 'SERVER_JOB':
511 continue
mblighb9db5162009-04-17 22:21:41 +0000512 # if tests is not empty, restrict list of test_statuses to tests
513 if tests and test_status.test_name not in tests:
514 continue
mbligh451ede12009-02-12 21:54:03 +0000515 if debug:
516 print test_status
mbligh5280e3b2008-12-22 14:39:28 +0000517 hostname = test_status.hostname
518 if hostname not in job.test_status:
519 job.test_status[hostname] = TestResults()
520 job.test_status[hostname].add(test_status)
521
522
mbligh451ede12009-02-12 21:54:03 +0000523 def _job_results_platform_map(self, job, debug):
mblighc9e427e2009-04-28 18:35:06 +0000524 # Figure out which hosts passed / failed / aborted in a job
525 # Creates a 2-dimensional hash, stored as job.results_platform_map
526 # 1st index - platform type (string)
527 # 2nd index - Status (string)
528 # 'Completed' / 'Failed' / 'Aborted'
529 # Data indexed by this hash is a list of hostnames (text strings)
mbligh5280e3b2008-12-22 14:39:28 +0000530 job.results_platform_map = {}
mbligh5b618382008-12-03 15:24:01 +0000531 try:
mbligh45ffc432008-12-09 23:35:17 +0000532 job_statuses = self.get_host_queue_entries(job=job.id)
mbligh5b618382008-12-03 15:24:01 +0000533 except Exception:
534 print "Ignoring exception on poll job; RPC interface is flaky"
535 traceback.print_exc()
536 return None
mbligh5280e3b2008-12-22 14:39:28 +0000537
mbligh5b618382008-12-03 15:24:01 +0000538 platform_map = {}
mbligh5280e3b2008-12-22 14:39:28 +0000539 job.job_status = {}
mbligh451ede12009-02-12 21:54:03 +0000540 job.metahost_index = {}
mbligh5b618382008-12-03 15:24:01 +0000541 for job_status in job_statuses:
mblighc9e427e2009-04-28 18:35:06 +0000542 # This is basically "for each host / metahost in the job"
mbligh451ede12009-02-12 21:54:03 +0000543 if job_status.host:
544 hostname = job_status.host.hostname
545 else: # This is a metahost
546 metahost = job_status.meta_host
547 index = job.metahost_index.get(metahost, 1)
548 job.metahost_index[metahost] = index + 1
549 hostname = '%s.%s' % (metahost, index)
mbligh5280e3b2008-12-22 14:39:28 +0000550 job.job_status[hostname] = job_status.status
mbligh5b618382008-12-03 15:24:01 +0000551 status = job_status.status
mbligh0ecbe632009-05-13 21:34:56 +0000552 # Skip hosts that failed verify or repair:
553 # that's a machine failure, not a job failure
mbligh451ede12009-02-12 21:54:03 +0000554 if hostname in job.test_status:
555 verify_failed = False
556 for failure in job.test_status[hostname].fail:
mbligh0ecbe632009-05-13 21:34:56 +0000557 if (failure.test_name == 'verify' or
558 failure.test_name == 'repair'):
mbligh451ede12009-02-12 21:54:03 +0000559 verify_failed = True
560 break
561 if verify_failed:
562 continue
mblighc9e427e2009-04-28 18:35:06 +0000563 if hostname in job.test_status and job.test_status[hostname].fail:
564 # If the any tests failed in the job, we want to mark the
565 # job result as failed, overriding the default job status.
566 if status != "Aborted": # except if it's an aborted job
567 status = 'Failed'
mbligh451ede12009-02-12 21:54:03 +0000568 if job_status.host:
569 platform = job_status.host.platform
570 else: # This is a metahost
571 platform = job_status.meta_host
mbligh5b618382008-12-03 15:24:01 +0000572 if platform not in platform_map:
573 platform_map[platform] = {'Total' : [hostname]}
574 else:
575 platform_map[platform]['Total'].append(hostname)
576 new_host_list = platform_map[platform].get(status, []) + [hostname]
577 platform_map[platform][status] = new_host_list
mbligh45ffc432008-12-09 23:35:17 +0000578 job.results_platform_map = platform_map
mbligh5280e3b2008-12-22 14:39:28 +0000579
580
mbligh17c75e62009-06-08 16:18:21 +0000581 def set_platform_results(self, test_job, platform, result):
582 """
583 Result must be None, 'FAIL', 'WARN' or 'GOOD'
584 """
585 if test_job.platform_results[platform] is not None:
586 # We're already done, and results recorded. This can't change later.
587 return
588 test_job.platform_results[platform] = result
589 # Note that self.job refers to the metajob we're IN, not the job
590 # that we're excuting from here.
591 testname = '%s.%s' % (test_job.testname, platform)
592 if self.job:
593 self.job.record(result, None, testname, status='')
594
595
mbligh5280e3b2008-12-22 14:39:28 +0000596 def poll_job_results(self, tko, job, debug=False):
597 """
598 Analyse all job results by platform, return:
mbligh1ef218d2009-08-03 16:57:56 +0000599
mbligh5280e3b2008-12-22 14:39:28 +0000600 False: if any platform has more than one failure
601 None: if any platform has more than one machine not yet Good.
602 True: if all platforms have at least all-but-one machines Good.
603 """
mbligh451ede12009-02-12 21:54:03 +0000604 self._job_test_results(tko, job, debug)
mblighe7fcf562009-05-21 01:43:17 +0000605 if job.test_status == {}:
606 return None
mbligh451ede12009-02-12 21:54:03 +0000607 self._job_results_platform_map(job, debug)
mbligh5280e3b2008-12-22 14:39:28 +0000608
mbligh5b618382008-12-03 15:24:01 +0000609 good_platforms = []
mbligh912c3f32009-03-25 19:31:30 +0000610 failed_platforms = []
611 aborted_platforms = []
mbligh5b618382008-12-03 15:24:01 +0000612 unknown_platforms = []
mbligh5280e3b2008-12-22 14:39:28 +0000613 platform_map = job.results_platform_map
mbligh5b618382008-12-03 15:24:01 +0000614 for platform in platform_map:
mbligh17c75e62009-06-08 16:18:21 +0000615 if not job.platform_results.has_key(platform):
616 # record test start, but there's no way to do this right now
617 job.platform_results[platform] = None
mbligh5b618382008-12-03 15:24:01 +0000618 total = len(platform_map[platform]['Total'])
619 completed = len(platform_map[platform].get('Completed', []))
mbligh912c3f32009-03-25 19:31:30 +0000620 failed = len(platform_map[platform].get('Failed', []))
621 aborted = len(platform_map[platform].get('Aborted', []))
mbligh17c75e62009-06-08 16:18:21 +0000622
mbligh1ef218d2009-08-03 16:57:56 +0000623 # We set up what we want to record here, but don't actually do
mbligh17c75e62009-06-08 16:18:21 +0000624 # it yet, until we have a decisive answer for this platform
625 if aborted or failed:
626 bad = aborted + failed
627 if (bad > 1) or (bad * 2 >= total):
628 platform_test_result = 'FAIL'
629 else:
630 platform_test_result = 'WARN'
631
mbligh912c3f32009-03-25 19:31:30 +0000632 if aborted > 1:
633 aborted_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000634 self.set_platform_results(job, platform, platform_test_result)
mbligh912c3f32009-03-25 19:31:30 +0000635 elif (failed * 2 >= total) or (failed > 1):
636 failed_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000637 self.set_platform_results(job, platform, platform_test_result)
mbligh451ede12009-02-12 21:54:03 +0000638 elif (completed >= 1) and (completed + 1 >= total):
mbligh5b618382008-12-03 15:24:01 +0000639 # if all or all but one are good, call the job good.
640 good_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000641 self.set_platform_results(job, platform, 'GOOD')
mbligh5b618382008-12-03 15:24:01 +0000642 else:
643 unknown_platforms.append(platform)
644 detail = []
645 for status in platform_map[platform]:
646 if status == 'Total':
647 continue
648 detail.append('%s=%s' % (status,platform_map[platform][status]))
649 if debug:
mbligh1ef218d2009-08-03 16:57:56 +0000650 print '%20s %d/%d %s' % (platform, completed, total,
mbligh5b618382008-12-03 15:24:01 +0000651 ' '.join(detail))
652 print
mbligh1ef218d2009-08-03 16:57:56 +0000653
mbligh912c3f32009-03-25 19:31:30 +0000654 if len(aborted_platforms) > 0:
mbligh5b618382008-12-03 15:24:01 +0000655 if debug:
mbligh17c75e62009-06-08 16:18:21 +0000656 print 'Result aborted - platforms: ',
657 print ' '.join(aborted_platforms)
mbligh912c3f32009-03-25 19:31:30 +0000658 return "Abort"
659 if len(failed_platforms) > 0:
660 if debug:
661 print 'Result bad - platforms: ' + ' '.join(failed_platforms)
mbligh5b618382008-12-03 15:24:01 +0000662 return False
663 if len(unknown_platforms) > 0:
664 if debug:
665 platform_list = ' '.join(unknown_platforms)
666 print 'Result unknown - platforms: ', platform_list
667 return None
668 if debug:
669 platform_list = ' '.join(good_platforms)
670 print 'Result good - all platforms passed: ', platform_list
671 return True
672
673
mbligh5280e3b2008-12-22 14:39:28 +0000674class TestResults(object):
675 """
676 Container class used to hold the results of the tests for a job
677 """
678 def __init__(self):
679 self.good = []
680 self.fail = []
mbligh451ede12009-02-12 21:54:03 +0000681 self.pending = []
mbligh5280e3b2008-12-22 14:39:28 +0000682
683
684 def add(self, result):
mbligh451ede12009-02-12 21:54:03 +0000685 if result.complete_count > result.pass_count:
686 self.fail.append(result)
687 elif result.incomplete_count > 0:
688 self.pending.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000689 else:
mbligh451ede12009-02-12 21:54:03 +0000690 self.good.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000691
692
693class RpcObject(object):
mbligh67647152008-11-19 00:18:14 +0000694 """
695 Generic object used to construct python objects from rpc calls
696 """
697 def __init__(self, afe, hash):
698 self.afe = afe
699 self.hash = hash
700 self.__dict__.update(hash)
701
702
703 def __str__(self):
704 return dump_object(self.__repr__(), self)
705
706
mbligh1354c9d2008-12-22 14:56:13 +0000707class ControlFile(RpcObject):
708 """
709 AFE control file object
710
711 Fields: synch_count, dependencies, control_file, is_server
712 """
713 def __repr__(self):
714 return 'CONTROL FILE: %s' % self.control_file
715
716
mbligh5280e3b2008-12-22 14:39:28 +0000717class Label(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000718 """
719 AFE label object
720
721 Fields:
722 name, invalid, platform, kernel_config, id, only_if_needed
723 """
724 def __repr__(self):
725 return 'LABEL: %s' % self.name
726
727
728 def add_hosts(self, hosts):
729 return self.afe.run('label_add_hosts', self.id, hosts)
730
731
732 def remove_hosts(self, hosts):
733 return self.afe.run('label_remove_hosts', self.id, hosts)
734
735
mbligh5280e3b2008-12-22 14:39:28 +0000736class Acl(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000737 """
738 AFE acl object
739
740 Fields:
741 users, hosts, description, name, id
742 """
743 def __repr__(self):
744 return 'ACL: %s' % self.name
745
746
747 def add_hosts(self, hosts):
748 self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name))
749 return self.afe.run('acl_group_add_hosts', self.id, hosts)
750
751
752 def remove_hosts(self, hosts):
753 self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name))
754 return self.afe.run('acl_group_remove_hosts', self.id, hosts)
755
756
mbligh54459c72009-01-21 19:26:44 +0000757 def add_users(self, users):
758 self.afe.log('Adding users %s to ACL %s' % (users, self.name))
759 return self.afe.run('acl_group_add_users', id=self.name, users=users)
760
761
mbligh5280e3b2008-12-22 14:39:28 +0000762class Job(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000763 """
764 AFE job object
765
766 Fields:
767 name, control_file, control_type, synch_count, reboot_before,
768 run_verify, priority, email_list, created_on, dependencies,
769 timeout, owner, reboot_after, id
770 """
771 def __repr__(self):
772 return 'JOB: %s' % self.id
773
774
mbligh5280e3b2008-12-22 14:39:28 +0000775class JobStatus(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000776 """
777 AFE job_status object
778
779 Fields:
780 status, complete, deleted, meta_host, host, active, execution_subdir, id
781 """
782 def __init__(self, afe, hash):
783 # This should call super
784 self.afe = afe
785 self.hash = hash
786 self.__dict__.update(hash)
mbligh5280e3b2008-12-22 14:39:28 +0000787 self.job = Job(afe, self.job)
mbligh67647152008-11-19 00:18:14 +0000788 if self.host:
mbligh99b24f42009-06-08 16:45:55 +0000789 self.host = Host(afe, self.host)
mbligh67647152008-11-19 00:18:14 +0000790
791
792 def __repr__(self):
mbligh451ede12009-02-12 21:54:03 +0000793 if self.host and self.host.hostname:
794 hostname = self.host.hostname
795 else:
796 hostname = 'None'
797 return 'JOB STATUS: %s-%s' % (self.job.id, hostname)
mbligh67647152008-11-19 00:18:14 +0000798
799
mbligh5280e3b2008-12-22 14:39:28 +0000800class Host(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000801 """
802 AFE host object
803
804 Fields:
805 status, lock_time, locked_by, locked, hostname, invalid,
806 synch_id, labels, platform, protection, dirty, id
807 """
808 def __repr__(self):
809 return 'HOST OBJECT: %s' % self.hostname
810
811
812 def show(self):
813 labels = list(set(self.labels) - set([self.platform]))
814 print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
815 self.locked, self.platform,
816 ', '.join(labels))
817
818
mbligh54459c72009-01-21 19:26:44 +0000819 def delete(self):
820 return self.afe.run('delete_host', id=self.id)
821
822
mbligh6463c4b2009-01-30 00:33:37 +0000823 def modify(self, **dargs):
824 return self.afe.run('modify_host', id=self.id, **dargs)
825
826
mbligh67647152008-11-19 00:18:14 +0000827 def get_acls(self):
828 return self.afe.get_acls(hosts__hostname=self.hostname)
829
830
831 def add_acl(self, acl_name):
832 self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname))
833 return self.afe.run('acl_group_add_hosts', id=acl_name,
834 hosts=[self.hostname])
835
836
837 def remove_acl(self, acl_name):
838 self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname))
839 return self.afe.run('acl_group_remove_hosts', id=acl_name,
840 hosts=[self.hostname])
841
842
843 def get_labels(self):
844 return self.afe.get_labels(host__hostname__in=[self.hostname])
845
846
847 def add_labels(self, labels):
848 self.afe.log('Adding labels %s to host %s' % (labels, self.hostname))
849 return self.afe.run('host_add_labels', id=self.id, labels=labels)
850
851
852 def remove_labels(self, labels):
853 self.afe.log('Removing labels %s from host %s' % (labels,self.hostname))
854 return self.afe.run('host_remove_labels', id=self.id, labels=labels)
mbligh5b618382008-12-03 15:24:01 +0000855
856
mbligh54459c72009-01-21 19:26:44 +0000857class User(RpcObject):
858 def __repr__(self):
859 return 'USER: %s' % self.login
860
861
mbligh5280e3b2008-12-22 14:39:28 +0000862class TestStatus(RpcObject):
mblighc31e4022008-12-11 19:32:30 +0000863 """
864 TKO test status object
865
866 Fields:
867 test_idx, hostname, testname, id
868 complete_count, incomplete_count, group_count, pass_count
869 """
870 def __repr__(self):
871 return 'TEST STATUS: %s' % self.id
872
873
mbligh5b618382008-12-03 15:24:01 +0000874class MachineTestPairing(object):
875 """
876 Object representing the pairing of a machine label with a control file
mbligh1f23f362008-12-22 14:46:12 +0000877
878 machine_label: use machines from this label
879 control_file: use this control file (by name in the frontend)
880 platforms: list of rexeps to filter platforms by. [] => no filtering
mbligh282ce892010-01-06 18:40:17 +0000881 job_label: The label (name) to give to the autotest job launched
882 to run this pairing. '<kernel-version> : <config> : <date>'
mbligh5b618382008-12-03 15:24:01 +0000883 """
mbligh1354c9d2008-12-22 14:56:13 +0000884 def __init__(self, machine_label, control_file, platforms=[],
mbligh17c75e62009-06-08 16:18:21 +0000885 container=False, atomic_group_sched=False, synch_count=0,
mbligh282ce892010-01-06 18:40:17 +0000886 testname=None, job_label=None):
mbligh5b618382008-12-03 15:24:01 +0000887 self.machine_label = machine_label
888 self.control_file = control_file
mbligh1f23f362008-12-22 14:46:12 +0000889 self.platforms = platforms
mbligh1354c9d2008-12-22 14:56:13 +0000890 self.container = container
mblighb9db5162009-04-17 22:21:41 +0000891 self.atomic_group_sched = atomic_group_sched
892 self.synch_count = synch_count
mbligh17c75e62009-06-08 16:18:21 +0000893 self.testname = testname
mbligh282ce892010-01-06 18:40:17 +0000894 self.job_label = job_label
mbligh1354c9d2008-12-22 14:56:13 +0000895
896
897 def __repr__(self):
898 return '%s %s %s %s' % (self.machine_label, self.control_file,
899 self.platforms, self.container)