blob: 28dfe2961443e00d212c3c5d440152dc883412ca [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:
12 http://autotest/afe/server/noauth/rpc/
13 http://autotest/new_tko/server/noauth/rpc/
14 http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-api
mbligh67647152008-11-19 00:18:14 +000015"""
16
mblighb64d1762009-05-12 20:52:37 +000017import 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
mbligh451ede12009-02-12 21:54:03 +000049 All the constructors go in the afe / tko class.
50 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:
62 user = os.environ.get('LOGNAME')
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
mbligh67647152008-11-19 00:18:14 +000074 headers = {'AUTHORIZATION' : self.user}
mbligh451ede12009-02-12 21:54:03 +000075 rpc_server = 'http://' + server + path
mbligh1354c9d2008-12-22 14:56:13 +000076 if debug:
77 print 'SERVER: %s' % rpc_server
78 print 'HEADERS: %s' % headers
mbligh67647152008-11-19 00:18:14 +000079 self.proxy = rpc_client_lib.get_proxy(rpc_server, headers=headers)
80
81
82 def run(self, call, **dargs):
83 """
84 Make a RPC call to the AFE server
85 """
86 rpc_call = getattr(self.proxy, call)
87 if self.debug:
88 print 'DEBUG: %s %s' % (call, dargs)
mbligh451ede12009-02-12 21:54:03 +000089 try:
mbligh99b24f42009-06-08 16:45:55 +000090 result = utils.strip_unicode(rpc_call(**dargs))
91 if self.reply_debug:
92 print result
93 return result
mbligh451ede12009-02-12 21:54:03 +000094 except Exception:
95 print 'FAILED RPC CALL: %s %s' % (call, dargs)
96 raise
mbligh67647152008-11-19 00:18:14 +000097
98
99 def log(self, message):
100 if self.print_log:
101 print message
102
103
mbligh5280e3b2008-12-22 14:39:28 +0000104class TKO(RpcClient):
mbligh99b24f42009-06-08 16:45:55 +0000105 def __init__(self, user=None, server=None, print_log=True, debug=False,
106 reply_debug=False):
107 super(TKO, self).__init__(path='/new_tko/server/noauth/rpc/',
108 user=user,
109 server=server,
110 print_log=print_log,
111 debug=debug,
112 reply_debug=reply_debug)
mblighc31e4022008-12-11 19:32:30 +0000113
114
115 def get_status_counts(self, job, **data):
116 entries = self.run('get_status_counts',
mbligh451ede12009-02-12 21:54:03 +0000117 group_by=['hostname', 'test_name', 'reason'],
mblighc31e4022008-12-11 19:32:30 +0000118 job_tag__startswith='%s-' % job, **data)
mbligh5280e3b2008-12-22 14:39:28 +0000119 return [TestStatus(self, e) for e in entries['groups']]
mblighc31e4022008-12-11 19:32:30 +0000120
121
mbligh5280e3b2008-12-22 14:39:28 +0000122class AFE(RpcClient):
mbligh17c75e62009-06-08 16:18:21 +0000123 def __init__(self, user=None, server=None, print_log=True, debug=False,
mbligh99b24f42009-06-08 16:45:55 +0000124 reply_debug=False, job=None):
mbligh17c75e62009-06-08 16:18:21 +0000125 self.job = job
mbligh99b24f42009-06-08 16:45:55 +0000126 super(AFE, self).__init__(path='/afe/server/noauth/rpc/',
127 user=user,
128 server=server,
129 print_log=print_log,
130 debug=debug,
131 reply_debug=reply_debug)
mblighc31e4022008-12-11 19:32:30 +0000132
133
mbligh67647152008-11-19 00:18:14 +0000134 def host_statuses(self, live=None):
mblighc2847b72009-03-25 19:32:20 +0000135 dead_statuses = ['Dead', 'Repair Failed', 'Repairing']
mbligh67647152008-11-19 00:18:14 +0000136 statuses = self.run('get_static_data')['host_statuses']
137 if live == True:
mblighc2847b72009-03-25 19:32:20 +0000138 return list(set(statuses) - set(dead_statuses))
mbligh67647152008-11-19 00:18:14 +0000139 if live == False:
140 return dead_statuses
141 else:
142 return statuses
143
144
145 def get_hosts(self, **dargs):
146 hosts = self.run('get_hosts', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000147 return [Host(self, h) for h in hosts]
mbligh67647152008-11-19 00:18:14 +0000148
149
150 def create_host(self, hostname, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000151 id = self.run('add_host', hostname=hostname, **dargs)
mbligh67647152008-11-19 00:18:14 +0000152 return self.get_hosts(id=id)[0]
153
154
155 def get_labels(self, **dargs):
156 labels = self.run('get_labels', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000157 return [Label(self, l) for l in labels]
mbligh67647152008-11-19 00:18:14 +0000158
159
160 def create_label(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000161 id = self.run('add_label', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000162 return self.get_labels(id=id)[0]
163
164
165 def get_acls(self, **dargs):
166 acls = self.run('get_acl_groups', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000167 return [Acl(self, a) for a in acls]
mbligh67647152008-11-19 00:18:14 +0000168
169
170 def create_acl(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000171 id = self.run('add_acl_group', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000172 return self.get_acls(id=id)[0]
173
174
mbligh54459c72009-01-21 19:26:44 +0000175 def get_users(self, **dargs):
176 users = self.run('get_users', **dargs)
177 return [User(self, u) for u in users]
178
179
mbligh1354c9d2008-12-22 14:56:13 +0000180 def generate_control_file(self, tests, **dargs):
181 ret = self.run('generate_control_file', tests=tests, **dargs)
182 return ControlFile(self, ret)
183
184
mbligh67647152008-11-19 00:18:14 +0000185 def get_jobs(self, summary=False, **dargs):
186 if summary:
187 jobs_data = self.run('get_jobs_summary', **dargs)
188 else:
189 jobs_data = self.run('get_jobs', **dargs)
mblighafbba0c2009-06-08 16:44:45 +0000190 jobs = []
191 for j in jobs_data:
192 job = Job(self, j)
193 # Set up some extra information defaults
194 job.testname = re.sub('\s.*', '', job.name) # arbitrary default
195 job.platform_results = {}
196 job.platform_reasons = {}
197 jobs.append(job)
198 return jobs
mbligh67647152008-11-19 00:18:14 +0000199
200
201 def get_host_queue_entries(self, **data):
202 entries = self.run('get_host_queue_entries', **data)
mblighf9e35862009-02-26 01:03:11 +0000203 job_statuses = [JobStatus(self, e) for e in entries]
mbligh99b24f42009-06-08 16:45:55 +0000204
205 # Sadly, get_host_queue_entries doesn't return platforms, we have
206 # to get those back from an explicit get_hosts queury, then patch
207 # the new host objects back into the host list.
208 hostnames = [s.host.hostname for s in job_statuses if s.host]
209 host_hash = {}
210 for host in self.get_hosts(hostname__in=hostnames):
211 host_hash[host.hostname] = host
212 for status in job_statuses:
213 if status.host:
214 status.host = host_hash[status.host.hostname]
mblighf9e35862009-02-26 01:03:11 +0000215 # filter job statuses that have either host or meta_host
216 return [status for status in job_statuses if (status.host or
217 status.meta_host)]
mbligh67647152008-11-19 00:18:14 +0000218
219
mblighb9db5162009-04-17 22:21:41 +0000220 def create_job_by_test(self, tests, kernel=None, use_container=False,
mbligh1354c9d2008-12-22 14:56:13 +0000221 **dargs):
mbligh67647152008-11-19 00:18:14 +0000222 """
223 Given a test name, fetch the appropriate control file from the server
mbligh4e576612008-12-22 14:56:36 +0000224 and submit it.
225
226 Returns a list of job objects
mbligh67647152008-11-19 00:18:14 +0000227 """
mblighb9db5162009-04-17 22:21:41 +0000228 assert ('hosts' in dargs or
229 'atomic_group_name' in dargs and 'synch_count' in dargs)
mbligh1354c9d2008-12-22 14:56:13 +0000230 control_file = self.generate_control_file(tests=tests, kernel=kernel,
231 use_container=use_container,
232 do_push_packages=True)
233 if control_file.is_server:
mbligh67647152008-11-19 00:18:14 +0000234 dargs['control_type'] = 'Server'
235 else:
236 dargs['control_type'] = 'Client'
237 dargs['dependencies'] = dargs.get('dependencies', []) + \
mbligh1354c9d2008-12-22 14:56:13 +0000238 control_file.dependencies
239 dargs['control_file'] = control_file.control_file
mblighc99fccf2009-07-11 00:59:33 +0000240 if not dargs['synch_count']:
241 dargs['synch_count'] = control_file.synch_count
mblighb9db5162009-04-17 22:21:41 +0000242 if 'hosts' in dargs and len(dargs['hosts']) < dargs['synch_count']:
243 # will not be able to satisfy this request
mbligh38b09152009-04-28 18:34:25 +0000244 return None
245 return self.create_job(**dargs)
mbligh67647152008-11-19 00:18:14 +0000246
247
248 def create_job(self, control_file, name=' ', priority='Medium',
249 control_type='Client', **dargs):
250 id = self.run('create_job', name=name, priority=priority,
251 control_file=control_file, control_type=control_type, **dargs)
252 return self.get_jobs(id=id)[0]
253
254
mbligh1f23f362008-12-22 14:46:12 +0000255 def run_test_suites(self, pairings, kernel, kernel_label, priority='Medium',
mblighd50b1252009-06-08 16:43:37 +0000256 wait=True, poll_interval=10, email_from=None,
mbligh7b312282009-01-07 16:45:43 +0000257 email_to=None, timeout=168):
mbligh5b618382008-12-03 15:24:01 +0000258 """
259 Run a list of test suites on a particular kernel.
260
261 Poll for them to complete, and return whether they worked or not.
262
263 pairings: list of MachineTestPairing objects to invoke
264 kernel: name of the kernel to run
265 kernel_label: label of the kernel to run
266 (<kernel-version> : <config> : <date>)
267 wait: boolean - wait for the results to come back?
268 poll_interval: interval between polling for job results (in minutes)
mbligh45ffc432008-12-09 23:35:17 +0000269 email_from: send notification email upon completion from here
270 email_from: send notification email upon completion to here
mbligh5b618382008-12-03 15:24:01 +0000271 """
272 jobs = []
273 for pairing in pairings:
mbligh0c4f8d72009-05-12 20:52:18 +0000274 try:
275 new_job = self.invoke_test(pairing, kernel, kernel_label,
276 priority, timeout=timeout)
277 if not new_job:
278 continue
mbligh0c4f8d72009-05-12 20:52:18 +0000279 jobs.append(new_job)
280 except Exception, e:
281 traceback.print_exc()
mblighb9db5162009-04-17 22:21:41 +0000282 if not wait or not jobs:
mbligh5b618382008-12-03 15:24:01 +0000283 return
mbligh5280e3b2008-12-22 14:39:28 +0000284 tko = TKO()
mbligh5b618382008-12-03 15:24:01 +0000285 while True:
286 time.sleep(60 * poll_interval)
mbligh5280e3b2008-12-22 14:39:28 +0000287 result = self.poll_all_jobs(tko, jobs, email_from, email_to)
mbligh5b618382008-12-03 15:24:01 +0000288 if result is not None:
289 return result
290
291
mbligh45ffc432008-12-09 23:35:17 +0000292 def result_notify(self, job, email_from, email_to):
mbligh5b618382008-12-03 15:24:01 +0000293 """
mbligh45ffc432008-12-09 23:35:17 +0000294 Notify about the result of a job. Will always print, if email data
295 is provided, will send email for it as well.
296
297 job: job object to notify about
298 email_from: send notification email upon completion from here
299 email_from: send notification email upon completion to here
300 """
301 if job.result == True:
302 subject = 'Testing PASSED: '
303 else:
304 subject = 'Testing FAILED: '
305 subject += '%s : %s\n' % (job.name, job.id)
306 text = []
307 for platform in job.results_platform_map:
308 for status in job.results_platform_map[platform]:
309 if status == 'Total':
310 continue
mbligh451ede12009-02-12 21:54:03 +0000311 for host in job.results_platform_map[platform][status]:
312 text.append('%20s %10s %10s' % (platform, status, host))
313 if status == 'Failed':
314 for test_status in job.test_status[host].fail:
315 text.append('(%s, %s) : %s' % \
316 (host, test_status.test_name,
317 test_status.reason))
318 text.append('')
mbligh37eceaa2008-12-15 22:56:37 +0000319
mbligh451ede12009-02-12 21:54:03 +0000320 base_url = 'http://' + self.server
mbligh37eceaa2008-12-15 22:56:37 +0000321
322 params = ('columns=test',
323 'rows=machine_group',
324 "condition=tag~'%s-%%25'" % job.id,
325 'title=Report')
326 query_string = '&'.join(params)
mbligh451ede12009-02-12 21:54:03 +0000327 url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string)
328 text.append(url + '\n')
329 url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
330 text.append(url + '\n')
mbligh37eceaa2008-12-15 22:56:37 +0000331
332 body = '\n'.join(text)
333 print '---------------------------------------------------'
334 print 'Subject: ', subject
mbligh45ffc432008-12-09 23:35:17 +0000335 print body
mbligh37eceaa2008-12-15 22:56:37 +0000336 print '---------------------------------------------------'
mbligh45ffc432008-12-09 23:35:17 +0000337 if email_from and email_to:
mbligh37eceaa2008-12-15 22:56:37 +0000338 print 'Sending email ...'
mbligh45ffc432008-12-09 23:35:17 +0000339 utils.send_email(email_from, email_to, subject, body)
340 print
mbligh37eceaa2008-12-15 22:56:37 +0000341
mbligh45ffc432008-12-09 23:35:17 +0000342
mbligh1354c9d2008-12-22 14:56:13 +0000343 def print_job_result(self, job):
344 """
345 Print the result of a single job.
346 job: a job object
347 """
348 if job.result is None:
349 print 'PENDING',
350 elif job.result == True:
351 print 'PASSED',
352 elif job.result == False:
353 print 'FAILED',
mbligh912c3f32009-03-25 19:31:30 +0000354 elif job.result == "Abort":
355 print 'ABORT',
mbligh1354c9d2008-12-22 14:56:13 +0000356 print ' %s : %s' % (job.id, job.name)
357
358
mbligh451ede12009-02-12 21:54:03 +0000359 def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None):
mbligh45ffc432008-12-09 23:35:17 +0000360 """
361 Poll all jobs in a list.
362 jobs: list of job objects to poll
363 email_from: send notification email upon completion from here
364 email_from: send notification email upon completion to here
365
366 Returns:
mbligh5b618382008-12-03 15:24:01 +0000367 a) All complete successfully (return True)
368 b) One or more has failed (return False)
369 c) Cannot tell yet (return None)
370 """
mbligh45ffc432008-12-09 23:35:17 +0000371 results = []
mbligh5b618382008-12-03 15:24:01 +0000372 for job in jobs:
mbligh676dcbe2009-06-15 21:57:27 +0000373 if getattr(job, 'result', None) is None:
374 job.result = self.poll_job_results(tko, job)
375 if job.result is not None:
376 self.result_notify(job, email_from, email_to)
mbligh45ffc432008-12-09 23:35:17 +0000377
mbligh676dcbe2009-06-15 21:57:27 +0000378 results.append(job.result)
mbligh1354c9d2008-12-22 14:56:13 +0000379 self.print_job_result(job)
mbligh45ffc432008-12-09 23:35:17 +0000380
381 if None in results:
382 return None
mbligh912c3f32009-03-25 19:31:30 +0000383 elif False in results or "Abort" in results:
mbligh45ffc432008-12-09 23:35:17 +0000384 return False
385 else:
386 return True
mbligh5b618382008-12-03 15:24:01 +0000387
388
mbligh1f23f362008-12-22 14:46:12 +0000389 def _included_platform(self, host, platforms):
390 """
391 See if host's platforms matches any of the patterns in the included
392 platforms list.
393 """
394 if not platforms:
395 return True # No filtering of platforms
396 for platform in platforms:
397 if re.search(platform, host.platform):
398 return True
399 return False
400
401
mbligh7b312282009-01-07 16:45:43 +0000402 def invoke_test(self, pairing, kernel, kernel_label, priority='Medium',
403 **dargs):
mbligh5b618382008-12-03 15:24:01 +0000404 """
405 Given a pairing of a control file to a machine label, find all machines
406 with that label, and submit that control file to them.
407
mbligh4e576612008-12-22 14:56:36 +0000408 Returns a list of job objects
mbligh5b618382008-12-03 15:24:01 +0000409 """
410 job_name = '%s : %s' % (pairing.machine_label, kernel_label)
411 hosts = self.get_hosts(multiple_labels=[pairing.machine_label])
mbligh1f23f362008-12-22 14:46:12 +0000412 platforms = pairing.platforms
413 hosts = [h for h in hosts if self._included_platform(h, platforms)]
mblighc2847b72009-03-25 19:32:20 +0000414 dead_statuses = self.host_statuses(live=False)
415 host_list = [h.hostname for h in hosts if h.status not in dead_statuses]
mbligh1f23f362008-12-22 14:46:12 +0000416 print 'HOSTS: %s' % host_list
mblighb9db5162009-04-17 22:21:41 +0000417 if pairing.atomic_group_sched:
mblighc99fccf2009-07-11 00:59:33 +0000418 dargs['synch_count'] = pairing.synch_count
mblighb9db5162009-04-17 22:21:41 +0000419 dargs['atomic_group_name'] = pairing.machine_label
420 else:
421 dargs['hosts'] = host_list
mbligh38b09152009-04-28 18:34:25 +0000422 new_job = self.create_job_by_test(name=job_name,
mbligh17c75e62009-06-08 16:18:21 +0000423 dependencies=[pairing.machine_label],
424 tests=[pairing.control_file],
425 priority=priority,
426 kernel=kernel,
427 use_container=pairing.container,
428 **dargs)
mbligh38b09152009-04-28 18:34:25 +0000429 if new_job:
mbligh17c75e62009-06-08 16:18:21 +0000430 if pairing.testname:
431 new_job.testname = pairing.testname
mbligh4e576612008-12-22 14:56:36 +0000432 print 'Invoked test %s : %s' % (new_job.id, job_name)
mbligh38b09152009-04-28 18:34:25 +0000433 return new_job
mbligh5b618382008-12-03 15:24:01 +0000434
435
mblighb9db5162009-04-17 22:21:41 +0000436 def _job_test_results(self, tko, job, debug, tests=[]):
mbligh5b618382008-12-03 15:24:01 +0000437 """
mbligh5280e3b2008-12-22 14:39:28 +0000438 Retrieve test results for a job
mbligh5b618382008-12-03 15:24:01 +0000439 """
mbligh5280e3b2008-12-22 14:39:28 +0000440 job.test_status = {}
441 try:
442 test_statuses = tko.get_status_counts(job=job.id)
443 except Exception:
444 print "Ignoring exception on poll job; RPC interface is flaky"
445 traceback.print_exc()
446 return
447
448 for test_status in test_statuses:
mbligh7479a182009-01-07 16:46:24 +0000449 # SERVER_JOB is buggy, and often gives false failures. Ignore it.
450 if test_status.test_name == 'SERVER_JOB':
451 continue
mblighb9db5162009-04-17 22:21:41 +0000452 # if tests is not empty, restrict list of test_statuses to tests
453 if tests and test_status.test_name not in tests:
454 continue
mbligh451ede12009-02-12 21:54:03 +0000455 if debug:
456 print test_status
mbligh5280e3b2008-12-22 14:39:28 +0000457 hostname = test_status.hostname
458 if hostname not in job.test_status:
459 job.test_status[hostname] = TestResults()
460 job.test_status[hostname].add(test_status)
461
462
mbligh451ede12009-02-12 21:54:03 +0000463 def _job_results_platform_map(self, job, debug):
mblighc9e427e2009-04-28 18:35:06 +0000464 # Figure out which hosts passed / failed / aborted in a job
465 # Creates a 2-dimensional hash, stored as job.results_platform_map
466 # 1st index - platform type (string)
467 # 2nd index - Status (string)
468 # 'Completed' / 'Failed' / 'Aborted'
469 # Data indexed by this hash is a list of hostnames (text strings)
mbligh5280e3b2008-12-22 14:39:28 +0000470 job.results_platform_map = {}
mbligh5b618382008-12-03 15:24:01 +0000471 try:
mbligh45ffc432008-12-09 23:35:17 +0000472 job_statuses = self.get_host_queue_entries(job=job.id)
mbligh5b618382008-12-03 15:24:01 +0000473 except Exception:
474 print "Ignoring exception on poll job; RPC interface is flaky"
475 traceback.print_exc()
476 return None
mbligh5280e3b2008-12-22 14:39:28 +0000477
mbligh5b618382008-12-03 15:24:01 +0000478 platform_map = {}
mbligh5280e3b2008-12-22 14:39:28 +0000479 job.job_status = {}
mbligh451ede12009-02-12 21:54:03 +0000480 job.metahost_index = {}
mbligh5b618382008-12-03 15:24:01 +0000481 for job_status in job_statuses:
mblighc9e427e2009-04-28 18:35:06 +0000482 # This is basically "for each host / metahost in the job"
mbligh451ede12009-02-12 21:54:03 +0000483 if job_status.host:
484 hostname = job_status.host.hostname
485 else: # This is a metahost
486 metahost = job_status.meta_host
487 index = job.metahost_index.get(metahost, 1)
488 job.metahost_index[metahost] = index + 1
489 hostname = '%s.%s' % (metahost, index)
mbligh5280e3b2008-12-22 14:39:28 +0000490 job.job_status[hostname] = job_status.status
mbligh5b618382008-12-03 15:24:01 +0000491 status = job_status.status
mbligh0ecbe632009-05-13 21:34:56 +0000492 # Skip hosts that failed verify or repair:
493 # that's a machine failure, not a job failure
mbligh451ede12009-02-12 21:54:03 +0000494 if hostname in job.test_status:
495 verify_failed = False
496 for failure in job.test_status[hostname].fail:
mbligh0ecbe632009-05-13 21:34:56 +0000497 if (failure.test_name == 'verify' or
498 failure.test_name == 'repair'):
mbligh451ede12009-02-12 21:54:03 +0000499 verify_failed = True
500 break
501 if verify_failed:
502 continue
mblighc9e427e2009-04-28 18:35:06 +0000503 if hostname in job.test_status and job.test_status[hostname].fail:
504 # If the any tests failed in the job, we want to mark the
505 # job result as failed, overriding the default job status.
506 if status != "Aborted": # except if it's an aborted job
507 status = 'Failed'
mbligh451ede12009-02-12 21:54:03 +0000508 if job_status.host:
509 platform = job_status.host.platform
510 else: # This is a metahost
511 platform = job_status.meta_host
mbligh5b618382008-12-03 15:24:01 +0000512 if platform not in platform_map:
513 platform_map[platform] = {'Total' : [hostname]}
514 else:
515 platform_map[platform]['Total'].append(hostname)
516 new_host_list = platform_map[platform].get(status, []) + [hostname]
517 platform_map[platform][status] = new_host_list
mbligh45ffc432008-12-09 23:35:17 +0000518 job.results_platform_map = platform_map
mbligh5280e3b2008-12-22 14:39:28 +0000519
520
mbligh17c75e62009-06-08 16:18:21 +0000521 def set_platform_results(self, test_job, platform, result):
522 """
523 Result must be None, 'FAIL', 'WARN' or 'GOOD'
524 """
525 if test_job.platform_results[platform] is not None:
526 # We're already done, and results recorded. This can't change later.
527 return
528 test_job.platform_results[platform] = result
529 # Note that self.job refers to the metajob we're IN, not the job
530 # that we're excuting from here.
531 testname = '%s.%s' % (test_job.testname, platform)
532 if self.job:
533 self.job.record(result, None, testname, status='')
534
535
mbligh5280e3b2008-12-22 14:39:28 +0000536 def poll_job_results(self, tko, job, debug=False):
537 """
538 Analyse all job results by platform, return:
mbligh5b618382008-12-03 15:24:01 +0000539
mbligh5280e3b2008-12-22 14:39:28 +0000540 False: if any platform has more than one failure
541 None: if any platform has more than one machine not yet Good.
542 True: if all platforms have at least all-but-one machines Good.
543 """
mbligh451ede12009-02-12 21:54:03 +0000544 self._job_test_results(tko, job, debug)
mblighe7fcf562009-05-21 01:43:17 +0000545 if job.test_status == {}:
546 return None
mbligh451ede12009-02-12 21:54:03 +0000547 self._job_results_platform_map(job, debug)
mbligh5280e3b2008-12-22 14:39:28 +0000548
mbligh5b618382008-12-03 15:24:01 +0000549 good_platforms = []
mbligh912c3f32009-03-25 19:31:30 +0000550 failed_platforms = []
551 aborted_platforms = []
mbligh5b618382008-12-03 15:24:01 +0000552 unknown_platforms = []
mbligh5280e3b2008-12-22 14:39:28 +0000553 platform_map = job.results_platform_map
mbligh5b618382008-12-03 15:24:01 +0000554 for platform in platform_map:
mbligh17c75e62009-06-08 16:18:21 +0000555 if not job.platform_results.has_key(platform):
556 # record test start, but there's no way to do this right now
557 job.platform_results[platform] = None
mbligh5b618382008-12-03 15:24:01 +0000558 total = len(platform_map[platform]['Total'])
559 completed = len(platform_map[platform].get('Completed', []))
mbligh912c3f32009-03-25 19:31:30 +0000560 failed = len(platform_map[platform].get('Failed', []))
561 aborted = len(platform_map[platform].get('Aborted', []))
mbligh17c75e62009-06-08 16:18:21 +0000562
563 # We set up what we want to record here, but don't actually do
564 # it yet, until we have a decisive answer for this platform
565 if aborted or failed:
566 bad = aborted + failed
567 if (bad > 1) or (bad * 2 >= total):
568 platform_test_result = 'FAIL'
569 else:
570 platform_test_result = 'WARN'
571
mbligh912c3f32009-03-25 19:31:30 +0000572 if aborted > 1:
573 aborted_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000574 self.set_platform_results(job, platform, platform_test_result)
mbligh912c3f32009-03-25 19:31:30 +0000575 elif (failed * 2 >= total) or (failed > 1):
576 failed_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000577 self.set_platform_results(job, platform, platform_test_result)
mbligh451ede12009-02-12 21:54:03 +0000578 elif (completed >= 1) and (completed + 1 >= total):
mbligh5b618382008-12-03 15:24:01 +0000579 # if all or all but one are good, call the job good.
580 good_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000581 self.set_platform_results(job, platform, 'GOOD')
mbligh5b618382008-12-03 15:24:01 +0000582 else:
583 unknown_platforms.append(platform)
584 detail = []
585 for status in platform_map[platform]:
586 if status == 'Total':
587 continue
588 detail.append('%s=%s' % (status,platform_map[platform][status]))
589 if debug:
590 print '%20s %d/%d %s' % (platform, completed, total,
591 ' '.join(detail))
592 print
593
mbligh912c3f32009-03-25 19:31:30 +0000594 if len(aborted_platforms) > 0:
mbligh5b618382008-12-03 15:24:01 +0000595 if debug:
mbligh17c75e62009-06-08 16:18:21 +0000596 print 'Result aborted - platforms: ',
597 print ' '.join(aborted_platforms)
mbligh912c3f32009-03-25 19:31:30 +0000598 return "Abort"
599 if len(failed_platforms) > 0:
600 if debug:
601 print 'Result bad - platforms: ' + ' '.join(failed_platforms)
mbligh5b618382008-12-03 15:24:01 +0000602 return False
603 if len(unknown_platforms) > 0:
604 if debug:
605 platform_list = ' '.join(unknown_platforms)
606 print 'Result unknown - platforms: ', platform_list
607 return None
608 if debug:
609 platform_list = ' '.join(good_platforms)
610 print 'Result good - all platforms passed: ', platform_list
611 return True
612
613
mbligh5280e3b2008-12-22 14:39:28 +0000614class TestResults(object):
615 """
616 Container class used to hold the results of the tests for a job
617 """
618 def __init__(self):
619 self.good = []
620 self.fail = []
mbligh451ede12009-02-12 21:54:03 +0000621 self.pending = []
mbligh5280e3b2008-12-22 14:39:28 +0000622
623
624 def add(self, result):
mbligh451ede12009-02-12 21:54:03 +0000625 if result.complete_count > result.pass_count:
626 self.fail.append(result)
627 elif result.incomplete_count > 0:
628 self.pending.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000629 else:
mbligh451ede12009-02-12 21:54:03 +0000630 self.good.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000631
632
633class RpcObject(object):
mbligh67647152008-11-19 00:18:14 +0000634 """
635 Generic object used to construct python objects from rpc calls
636 """
637 def __init__(self, afe, hash):
638 self.afe = afe
639 self.hash = hash
640 self.__dict__.update(hash)
641
642
643 def __str__(self):
644 return dump_object(self.__repr__(), self)
645
646
mbligh1354c9d2008-12-22 14:56:13 +0000647class ControlFile(RpcObject):
648 """
649 AFE control file object
650
651 Fields: synch_count, dependencies, control_file, is_server
652 """
653 def __repr__(self):
654 return 'CONTROL FILE: %s' % self.control_file
655
656
mbligh5280e3b2008-12-22 14:39:28 +0000657class Label(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000658 """
659 AFE label object
660
661 Fields:
662 name, invalid, platform, kernel_config, id, only_if_needed
663 """
664 def __repr__(self):
665 return 'LABEL: %s' % self.name
666
667
668 def add_hosts(self, hosts):
669 return self.afe.run('label_add_hosts', self.id, hosts)
670
671
672 def remove_hosts(self, hosts):
673 return self.afe.run('label_remove_hosts', self.id, hosts)
674
675
mbligh5280e3b2008-12-22 14:39:28 +0000676class Acl(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000677 """
678 AFE acl object
679
680 Fields:
681 users, hosts, description, name, id
682 """
683 def __repr__(self):
684 return 'ACL: %s' % self.name
685
686
687 def add_hosts(self, hosts):
688 self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name))
689 return self.afe.run('acl_group_add_hosts', self.id, hosts)
690
691
692 def remove_hosts(self, hosts):
693 self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name))
694 return self.afe.run('acl_group_remove_hosts', self.id, hosts)
695
696
mbligh54459c72009-01-21 19:26:44 +0000697 def add_users(self, users):
698 self.afe.log('Adding users %s to ACL %s' % (users, self.name))
699 return self.afe.run('acl_group_add_users', id=self.name, users=users)
700
701
mbligh5280e3b2008-12-22 14:39:28 +0000702class Job(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000703 """
704 AFE job object
705
706 Fields:
707 name, control_file, control_type, synch_count, reboot_before,
708 run_verify, priority, email_list, created_on, dependencies,
709 timeout, owner, reboot_after, id
710 """
711 def __repr__(self):
712 return 'JOB: %s' % self.id
713
714
mbligh5280e3b2008-12-22 14:39:28 +0000715class JobStatus(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000716 """
717 AFE job_status object
718
719 Fields:
720 status, complete, deleted, meta_host, host, active, execution_subdir, id
721 """
722 def __init__(self, afe, hash):
723 # This should call super
724 self.afe = afe
725 self.hash = hash
726 self.__dict__.update(hash)
mbligh5280e3b2008-12-22 14:39:28 +0000727 self.job = Job(afe, self.job)
mbligh67647152008-11-19 00:18:14 +0000728 if self.host:
mbligh99b24f42009-06-08 16:45:55 +0000729 self.host = Host(afe, self.host)
mbligh67647152008-11-19 00:18:14 +0000730
731
732 def __repr__(self):
mbligh451ede12009-02-12 21:54:03 +0000733 if self.host and self.host.hostname:
734 hostname = self.host.hostname
735 else:
736 hostname = 'None'
737 return 'JOB STATUS: %s-%s' % (self.job.id, hostname)
mbligh67647152008-11-19 00:18:14 +0000738
739
mbligh5280e3b2008-12-22 14:39:28 +0000740class Host(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000741 """
742 AFE host object
743
744 Fields:
745 status, lock_time, locked_by, locked, hostname, invalid,
746 synch_id, labels, platform, protection, dirty, id
747 """
748 def __repr__(self):
749 return 'HOST OBJECT: %s' % self.hostname
750
751
752 def show(self):
753 labels = list(set(self.labels) - set([self.platform]))
754 print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
755 self.locked, self.platform,
756 ', '.join(labels))
757
758
mbligh54459c72009-01-21 19:26:44 +0000759 def delete(self):
760 return self.afe.run('delete_host', id=self.id)
761
762
mbligh6463c4b2009-01-30 00:33:37 +0000763 def modify(self, **dargs):
764 return self.afe.run('modify_host', id=self.id, **dargs)
765
766
mbligh67647152008-11-19 00:18:14 +0000767 def get_acls(self):
768 return self.afe.get_acls(hosts__hostname=self.hostname)
769
770
771 def add_acl(self, acl_name):
772 self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname))
773 return self.afe.run('acl_group_add_hosts', id=acl_name,
774 hosts=[self.hostname])
775
776
777 def remove_acl(self, acl_name):
778 self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname))
779 return self.afe.run('acl_group_remove_hosts', id=acl_name,
780 hosts=[self.hostname])
781
782
783 def get_labels(self):
784 return self.afe.get_labels(host__hostname__in=[self.hostname])
785
786
787 def add_labels(self, labels):
788 self.afe.log('Adding labels %s to host %s' % (labels, self.hostname))
789 return self.afe.run('host_add_labels', id=self.id, labels=labels)
790
791
792 def remove_labels(self, labels):
793 self.afe.log('Removing labels %s from host %s' % (labels,self.hostname))
794 return self.afe.run('host_remove_labels', id=self.id, labels=labels)
mbligh5b618382008-12-03 15:24:01 +0000795
796
mbligh54459c72009-01-21 19:26:44 +0000797class User(RpcObject):
798 def __repr__(self):
799 return 'USER: %s' % self.login
800
801
mbligh5280e3b2008-12-22 14:39:28 +0000802class TestStatus(RpcObject):
mblighc31e4022008-12-11 19:32:30 +0000803 """
804 TKO test status object
805
806 Fields:
807 test_idx, hostname, testname, id
808 complete_count, incomplete_count, group_count, pass_count
809 """
810 def __repr__(self):
811 return 'TEST STATUS: %s' % self.id
812
813
mbligh5b618382008-12-03 15:24:01 +0000814class MachineTestPairing(object):
815 """
816 Object representing the pairing of a machine label with a control file
mbligh1f23f362008-12-22 14:46:12 +0000817
818 machine_label: use machines from this label
819 control_file: use this control file (by name in the frontend)
820 platforms: list of rexeps to filter platforms by. [] => no filtering
mbligh5b618382008-12-03 15:24:01 +0000821 """
mbligh1354c9d2008-12-22 14:56:13 +0000822 def __init__(self, machine_label, control_file, platforms=[],
mbligh17c75e62009-06-08 16:18:21 +0000823 container=False, atomic_group_sched=False, synch_count=0,
824 testname=None):
mbligh5b618382008-12-03 15:24:01 +0000825 self.machine_label = machine_label
826 self.control_file = control_file
mbligh1f23f362008-12-22 14:46:12 +0000827 self.platforms = platforms
mbligh1354c9d2008-12-22 14:56:13 +0000828 self.container = container
mblighb9db5162009-04-17 22:21:41 +0000829 self.atomic_group_sched = atomic_group_sched
830 self.synch_count = synch_count
mbligh17c75e62009-06-08 16:18:21 +0000831 self.testname = testname
mbligh1354c9d2008-12-22 14:56:13 +0000832
833
834 def __repr__(self):
835 return '%s %s %s %s' % (self.machine_label, self.control_file,
836 self.platforms, self.container)