blob: 5637c911a29bd9578f8f5a212db4c641879b6570 [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
mbligh1f23f362008-12-22 14:46:12 +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 +000031
32def dump_object(header, obj):
33 """
34 Standard way to print out the frontend objects (eg job, host, acl, label)
35 in a human-readable fashion for debugging
36 """
37 result = header + '\n'
38 for key in obj.hash:
39 if key == 'afe' or key == 'hash':
40 continue
41 result += '%20s: %s\n' % (key, obj.hash[key])
42 return result
43
44
mbligh5280e3b2008-12-22 14:39:28 +000045class RpcClient(object):
mbligh67647152008-11-19 00:18:14 +000046 """
mbligh451ede12009-02-12 21:54:03 +000047 Abstract RPC class for communicating with the autotest frontend
48 Inherited for both TKO and AFE uses.
mbligh67647152008-11-19 00:18:14 +000049
mbligh451ede12009-02-12 21:54:03 +000050 All the constructors go in the afe / tko class.
51 Manipulating methods go in the object classes themselves
mbligh67647152008-11-19 00:18:14 +000052 """
mbligh451ede12009-02-12 21:54:03 +000053 def __init__(self, path, user, server, print_log, debug):
mbligh67647152008-11-19 00:18:14 +000054 """
mbligh451ede12009-02-12 21:54:03 +000055 Create a cached instance of a connection to the frontend
mbligh67647152008-11-19 00:18:14 +000056
57 user: username to connect as
mbligh451ede12009-02-12 21:54:03 +000058 server: frontend server to connect to
mbligh67647152008-11-19 00:18:14 +000059 print_log: pring a logging message to stdout on every operation
60 debug: print out all RPC traffic
61 """
mblighc31e4022008-12-11 19:32:30 +000062 if not user:
63 user = os.environ.get('LOGNAME')
mbligh451ede12009-02-12 21:54:03 +000064 if not server:
mbligh475f7762009-01-30 00:34:04 +000065 if 'AUTOTEST_WEB' in os.environ:
mbligh451ede12009-02-12 21:54:03 +000066 server = os.environ['AUTOTEST_WEB']
mbligh475f7762009-01-30 00:34:04 +000067 else:
mbligh451ede12009-02-12 21:54:03 +000068 server = GLOBAL_CONFIG.get_config_value('SERVER', 'hostname',
69 default=DEFAULT_SERVER)
70 self.server = server
mbligh67647152008-11-19 00:18:14 +000071 self.user = user
72 self.print_log = print_log
73 self.debug = debug
74 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:
90 return utils.strip_unicode(rpc_call(**dargs))
91 except Exception:
92 print 'FAILED RPC CALL: %s %s' % (call, dargs)
93 raise
mbligh67647152008-11-19 00:18:14 +000094
95
96 def log(self, message):
97 if self.print_log:
98 print message
99
100
mbligh5280e3b2008-12-22 14:39:28 +0000101class TKO(RpcClient):
mbligh451ede12009-02-12 21:54:03 +0000102 def __init__(self, user=None, server=None, print_log=True, debug=False):
mbligh5280e3b2008-12-22 14:39:28 +0000103 super(TKO, self).__init__('/new_tko/server/noauth/rpc/', user,
mbligh451ede12009-02-12 21:54:03 +0000104 server, print_log, debug)
mblighc31e4022008-12-11 19:32:30 +0000105
106
107 def get_status_counts(self, job, **data):
108 entries = self.run('get_status_counts',
mbligh451ede12009-02-12 21:54:03 +0000109 group_by=['hostname', 'test_name', 'reason'],
mblighc31e4022008-12-11 19:32:30 +0000110 job_tag__startswith='%s-' % job, **data)
mbligh5280e3b2008-12-22 14:39:28 +0000111 return [TestStatus(self, e) for e in entries['groups']]
mblighc31e4022008-12-11 19:32:30 +0000112
113
mbligh5280e3b2008-12-22 14:39:28 +0000114class AFE(RpcClient):
mbligh451ede12009-02-12 21:54:03 +0000115 def __init__(self, user=None, server=None, print_log=True, debug=False):
116 super(AFE, self).__init__('/afe/server/noauth/rpc/', user, server,
mblighc31e4022008-12-11 19:32:30 +0000117 print_log, debug)
118
119
mbligh67647152008-11-19 00:18:14 +0000120 def host_statuses(self, live=None):
121 dead_statuses = ['Dead', 'Repair Failed']
122 statuses = self.run('get_static_data')['host_statuses']
123 if live == True:
124 return list(set(statuses) - set(['Dead', 'Repair Failed']))
125 if live == False:
126 return dead_statuses
127 else:
128 return statuses
129
130
131 def get_hosts(self, **dargs):
132 hosts = self.run('get_hosts', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000133 return [Host(self, h) for h in hosts]
mbligh67647152008-11-19 00:18:14 +0000134
135
136 def create_host(self, hostname, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000137 id = self.run('add_host', hostname=hostname, **dargs)
mbligh67647152008-11-19 00:18:14 +0000138 return self.get_hosts(id=id)[0]
139
140
141 def get_labels(self, **dargs):
142 labels = self.run('get_labels', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000143 return [Label(self, l) for l in labels]
mbligh67647152008-11-19 00:18:14 +0000144
145
146 def create_label(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000147 id = self.run('add_label', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000148 return self.get_labels(id=id)[0]
149
150
151 def get_acls(self, **dargs):
152 acls = self.run('get_acl_groups', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000153 return [Acl(self, a) for a in acls]
mbligh67647152008-11-19 00:18:14 +0000154
155
156 def create_acl(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000157 id = self.run('add_acl_group', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000158 return self.get_acls(id=id)[0]
159
160
mbligh54459c72009-01-21 19:26:44 +0000161 def get_users(self, **dargs):
162 users = self.run('get_users', **dargs)
163 return [User(self, u) for u in users]
164
165
mbligh1354c9d2008-12-22 14:56:13 +0000166 def generate_control_file(self, tests, **dargs):
167 ret = self.run('generate_control_file', tests=tests, **dargs)
168 return ControlFile(self, ret)
169
170
mbligh67647152008-11-19 00:18:14 +0000171 def get_jobs(self, summary=False, **dargs):
172 if summary:
173 jobs_data = self.run('get_jobs_summary', **dargs)
174 else:
175 jobs_data = self.run('get_jobs', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000176 return [Job(self, j) for j in jobs_data]
mbligh67647152008-11-19 00:18:14 +0000177
178
179 def get_host_queue_entries(self, **data):
180 entries = self.run('get_host_queue_entries', **data)
mbligh5280e3b2008-12-22 14:39:28 +0000181 return [JobStatus(self, e) for e in entries]
mbligh67647152008-11-19 00:18:14 +0000182
183
mbligh4e576612008-12-22 14:56:36 +0000184 def create_job_by_test(self, tests, hosts, kernel=None, use_container=False,
mbligh1354c9d2008-12-22 14:56:13 +0000185 **dargs):
mbligh67647152008-11-19 00:18:14 +0000186 """
187 Given a test name, fetch the appropriate control file from the server
mbligh4e576612008-12-22 14:56:36 +0000188 and submit it.
189
190 Returns a list of job objects
mbligh67647152008-11-19 00:18:14 +0000191 """
mbligh1354c9d2008-12-22 14:56:13 +0000192 control_file = self.generate_control_file(tests=tests, kernel=kernel,
193 use_container=use_container,
194 do_push_packages=True)
195 if control_file.is_server:
mbligh67647152008-11-19 00:18:14 +0000196 dargs['control_type'] = 'Server'
197 else:
198 dargs['control_type'] = 'Client'
199 dargs['dependencies'] = dargs.get('dependencies', []) + \
mbligh1354c9d2008-12-22 14:56:13 +0000200 control_file.dependencies
201 dargs['control_file'] = control_file.control_file
mblighb1ce3b32009-02-17 16:00:50 +0000202 if 'synch_count' not in dargs:
203 dargs['synch_count'] = control_file.synch_count
204 else:
205 control_file.synch_count = dargs['synch_count']
mbligh4e576612008-12-22 14:56:36 +0000206 jobs = []
207 if control_file.synch_count > 1:
208 # We don't trust the scheduler to do the groupings for us.
209 synch_count = control_file.synch_count
210 (pairs, failures) = form_ntuples_from_machines(hosts, synch_count)
211 for machines in pairs:
212 jobs.append(self.create_job(hosts=machines, **dargs))
213 else:
214 jobs.append(self.create_job(hosts=hosts, **dargs))
215 return jobs
mbligh67647152008-11-19 00:18:14 +0000216
217
218 def create_job(self, control_file, name=' ', priority='Medium',
219 control_type='Client', **dargs):
220 id = self.run('create_job', name=name, priority=priority,
221 control_file=control_file, control_type=control_type, **dargs)
222 return self.get_jobs(id=id)[0]
223
224
mbligh1f23f362008-12-22 14:46:12 +0000225 def run_test_suites(self, pairings, kernel, kernel_label, priority='Medium',
226 wait=True, poll_interval=5, email_from=None,
mbligh7b312282009-01-07 16:45:43 +0000227 email_to=None, timeout=168):
mbligh5b618382008-12-03 15:24:01 +0000228 """
229 Run a list of test suites on a particular kernel.
230
231 Poll for them to complete, and return whether they worked or not.
232
233 pairings: list of MachineTestPairing objects to invoke
234 kernel: name of the kernel to run
235 kernel_label: label of the kernel to run
236 (<kernel-version> : <config> : <date>)
237 wait: boolean - wait for the results to come back?
238 poll_interval: interval between polling for job results (in minutes)
mbligh45ffc432008-12-09 23:35:17 +0000239 email_from: send notification email upon completion from here
240 email_from: send notification email upon completion to here
mbligh5b618382008-12-03 15:24:01 +0000241 """
242 jobs = []
243 for pairing in pairings:
mbligh7b312282009-01-07 16:45:43 +0000244 new_jobs = self.invoke_test(pairing, kernel, kernel_label, priority,
245 timeout=timeout)
mbligh4e576612008-12-22 14:56:36 +0000246 for job in new_jobs:
247 job.notified = False
248 jobs += new_jobs
mbligh5280e3b2008-12-22 14:39:28 +0000249 # disabled - this is just for debugging: mbligh
250 # if email_from and email_to:
251 # subject = 'Testing started: %s : %s' % (job.name, job.id)
252 # utils.send_email(email_from, email_to, subject, subject)
mbligh5b618382008-12-03 15:24:01 +0000253 if not wait:
254 return
mbligh5280e3b2008-12-22 14:39:28 +0000255 tko = TKO()
mbligh5b618382008-12-03 15:24:01 +0000256 while True:
257 time.sleep(60 * poll_interval)
mbligh5280e3b2008-12-22 14:39:28 +0000258 result = self.poll_all_jobs(tko, jobs, email_from, email_to)
mbligh5b618382008-12-03 15:24:01 +0000259 if result is not None:
260 return result
261
262
mbligh45ffc432008-12-09 23:35:17 +0000263 def result_notify(self, job, email_from, email_to):
mbligh5b618382008-12-03 15:24:01 +0000264 """
mbligh45ffc432008-12-09 23:35:17 +0000265 Notify about the result of a job. Will always print, if email data
266 is provided, will send email for it as well.
267
268 job: job object to notify about
269 email_from: send notification email upon completion from here
270 email_from: send notification email upon completion to here
271 """
272 if job.result == True:
273 subject = 'Testing PASSED: '
274 else:
275 subject = 'Testing FAILED: '
276 subject += '%s : %s\n' % (job.name, job.id)
277 text = []
278 for platform in job.results_platform_map:
279 for status in job.results_platform_map[platform]:
280 if status == 'Total':
281 continue
mbligh451ede12009-02-12 21:54:03 +0000282 for host in job.results_platform_map[platform][status]:
283 text.append('%20s %10s %10s' % (platform, status, host))
284 if status == 'Failed':
285 for test_status in job.test_status[host].fail:
286 text.append('(%s, %s) : %s' % \
287 (host, test_status.test_name,
288 test_status.reason))
289 text.append('')
mbligh37eceaa2008-12-15 22:56:37 +0000290
mbligh451ede12009-02-12 21:54:03 +0000291 base_url = 'http://' + self.server
mbligh37eceaa2008-12-15 22:56:37 +0000292
293 params = ('columns=test',
294 'rows=machine_group',
295 "condition=tag~'%s-%%25'" % job.id,
296 'title=Report')
297 query_string = '&'.join(params)
mbligh451ede12009-02-12 21:54:03 +0000298 url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string)
299 text.append(url + '\n')
300 url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
301 text.append(url + '\n')
mbligh37eceaa2008-12-15 22:56:37 +0000302
303 body = '\n'.join(text)
304 print '---------------------------------------------------'
305 print 'Subject: ', subject
mbligh45ffc432008-12-09 23:35:17 +0000306 print body
mbligh37eceaa2008-12-15 22:56:37 +0000307 print '---------------------------------------------------'
mbligh45ffc432008-12-09 23:35:17 +0000308 if email_from and email_to:
mbligh37eceaa2008-12-15 22:56:37 +0000309 print 'Sending email ...'
mbligh45ffc432008-12-09 23:35:17 +0000310 utils.send_email(email_from, email_to, subject, body)
311 print
mbligh37eceaa2008-12-15 22:56:37 +0000312
mbligh45ffc432008-12-09 23:35:17 +0000313
mbligh1354c9d2008-12-22 14:56:13 +0000314 def print_job_result(self, job):
315 """
316 Print the result of a single job.
317 job: a job object
318 """
319 if job.result is None:
320 print 'PENDING',
321 elif job.result == True:
322 print 'PASSED',
323 elif job.result == False:
324 print 'FAILED',
325 print ' %s : %s' % (job.id, job.name)
326
327
mbligh451ede12009-02-12 21:54:03 +0000328 def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None):
mbligh45ffc432008-12-09 23:35:17 +0000329 """
330 Poll all jobs in a list.
331 jobs: list of job objects to poll
332 email_from: send notification email upon completion from here
333 email_from: send notification email upon completion to here
334
335 Returns:
mbligh5b618382008-12-03 15:24:01 +0000336 a) All complete successfully (return True)
337 b) One or more has failed (return False)
338 c) Cannot tell yet (return None)
339 """
mbligh45ffc432008-12-09 23:35:17 +0000340 results = []
mbligh5b618382008-12-03 15:24:01 +0000341 for job in jobs:
mbligh451ede12009-02-12 21:54:03 +0000342 job.result = self.poll_job_results(tko, job)
mbligh45ffc432008-12-09 23:35:17 +0000343 results.append(job.result)
344 if job.result is not None and not job.notified:
345 self.result_notify(job, email_from, email_to)
346 job.notified = True
347
mbligh1354c9d2008-12-22 14:56:13 +0000348 self.print_job_result(job)
mbligh45ffc432008-12-09 23:35:17 +0000349
350 if None in results:
351 return None
352 elif False in results:
353 return False
354 else:
355 return True
mbligh5b618382008-12-03 15:24:01 +0000356
357
mbligh1f23f362008-12-22 14:46:12 +0000358 def _included_platform(self, host, platforms):
359 """
360 See if host's platforms matches any of the patterns in the included
361 platforms list.
362 """
363 if not platforms:
364 return True # No filtering of platforms
365 for platform in platforms:
366 if re.search(platform, host.platform):
367 return True
368 return False
369
370
mbligh7b312282009-01-07 16:45:43 +0000371 def invoke_test(self, pairing, kernel, kernel_label, priority='Medium',
372 **dargs):
mbligh5b618382008-12-03 15:24:01 +0000373 """
374 Given a pairing of a control file to a machine label, find all machines
375 with that label, and submit that control file to them.
376
mbligh4e576612008-12-22 14:56:36 +0000377 Returns a list of job objects
mbligh5b618382008-12-03 15:24:01 +0000378 """
379 job_name = '%s : %s' % (pairing.machine_label, kernel_label)
380 hosts = self.get_hosts(multiple_labels=[pairing.machine_label])
mbligh1f23f362008-12-22 14:46:12 +0000381 platforms = pairing.platforms
382 hosts = [h for h in hosts if self._included_platform(h, platforms)]
mbligh45ffc432008-12-09 23:35:17 +0000383 host_list = [h.hostname for h in hosts if h.status != 'Repair Failed']
mbligh1f23f362008-12-22 14:46:12 +0000384 print 'HOSTS: %s' % host_list
mblighb1ce3b32009-02-17 16:00:50 +0000385 if pairing.synch_job:
386 dargs['synch_count'] = len(host_list)
mbligh4e576612008-12-22 14:56:36 +0000387 new_jobs = self.create_job_by_test(name=job_name,
mbligh7b312282009-01-07 16:45:43 +0000388 dependencies=[pairing.machine_label],
389 tests=[pairing.control_file],
390 priority=priority,
391 hosts=host_list,
392 kernel=kernel,
393 use_container=pairing.container,
394 **dargs)
mbligh4e576612008-12-22 14:56:36 +0000395 for new_job in new_jobs:
396 print 'Invoked test %s : %s' % (new_job.id, job_name)
397 return new_jobs
mbligh5b618382008-12-03 15:24:01 +0000398
399
mbligh451ede12009-02-12 21:54:03 +0000400 def _job_test_results(self, tko, job, debug):
mbligh5b618382008-12-03 15:24:01 +0000401 """
mbligh5280e3b2008-12-22 14:39:28 +0000402 Retrieve test results for a job
mbligh5b618382008-12-03 15:24:01 +0000403 """
mbligh5280e3b2008-12-22 14:39:28 +0000404 job.test_status = {}
405 try:
406 test_statuses = tko.get_status_counts(job=job.id)
407 except Exception:
408 print "Ignoring exception on poll job; RPC interface is flaky"
409 traceback.print_exc()
410 return
411
412 for test_status in test_statuses:
mbligh7479a182009-01-07 16:46:24 +0000413 # SERVER_JOB is buggy, and often gives false failures. Ignore it.
414 if test_status.test_name == 'SERVER_JOB':
415 continue
mbligh451ede12009-02-12 21:54:03 +0000416 if debug:
417 print test_status
mbligh5280e3b2008-12-22 14:39:28 +0000418 hostname = test_status.hostname
419 if hostname not in job.test_status:
420 job.test_status[hostname] = TestResults()
421 job.test_status[hostname].add(test_status)
422
423
mbligh451ede12009-02-12 21:54:03 +0000424 def _job_results_platform_map(self, job, debug):
mbligh5280e3b2008-12-22 14:39:28 +0000425 job.results_platform_map = {}
mbligh5b618382008-12-03 15:24:01 +0000426 try:
mbligh45ffc432008-12-09 23:35:17 +0000427 job_statuses = self.get_host_queue_entries(job=job.id)
mbligh5b618382008-12-03 15:24:01 +0000428 except Exception:
429 print "Ignoring exception on poll job; RPC interface is flaky"
430 traceback.print_exc()
431 return None
mbligh5280e3b2008-12-22 14:39:28 +0000432
mbligh5b618382008-12-03 15:24:01 +0000433 platform_map = {}
mbligh5280e3b2008-12-22 14:39:28 +0000434 job.job_status = {}
mbligh451ede12009-02-12 21:54:03 +0000435 job.metahost_index = {}
mbligh5b618382008-12-03 15:24:01 +0000436 for job_status in job_statuses:
mbligh451ede12009-02-12 21:54:03 +0000437 if job_status.host:
438 hostname = job_status.host.hostname
439 else: # This is a metahost
440 metahost = job_status.meta_host
441 index = job.metahost_index.get(metahost, 1)
442 job.metahost_index[metahost] = index + 1
443 hostname = '%s.%s' % (metahost, index)
mbligh5280e3b2008-12-22 14:39:28 +0000444 job.job_status[hostname] = job_status.status
mbligh5b618382008-12-03 15:24:01 +0000445 status = job_status.status
mbligh451ede12009-02-12 21:54:03 +0000446 # Skip hosts that failed verify - that's a machine failure,
447 # not a job failure
448 if hostname in job.test_status:
449 verify_failed = False
450 for failure in job.test_status[hostname].fail:
451 if failure.test_name == 'verify':
452 verify_failed = True
453 break
454 if verify_failed:
455 continue
mbligh5280e3b2008-12-22 14:39:28 +0000456 if hostname in job.test_status and job.test_status[hostname].fail:
457 # Job status doesn't reflect failed tests, override that
458 status = 'Failed'
mbligh451ede12009-02-12 21:54:03 +0000459 if job_status.host:
460 platform = job_status.host.platform
461 else: # This is a metahost
462 platform = job_status.meta_host
mbligh5b618382008-12-03 15:24:01 +0000463 if platform not in platform_map:
464 platform_map[platform] = {'Total' : [hostname]}
465 else:
466 platform_map[platform]['Total'].append(hostname)
467 new_host_list = platform_map[platform].get(status, []) + [hostname]
468 platform_map[platform][status] = new_host_list
mbligh45ffc432008-12-09 23:35:17 +0000469 job.results_platform_map = platform_map
mbligh5280e3b2008-12-22 14:39:28 +0000470
471
472 def poll_job_results(self, tko, job, debug=False):
473 """
474 Analyse all job results by platform, return:
mbligh5b618382008-12-03 15:24:01 +0000475
mbligh5280e3b2008-12-22 14:39:28 +0000476 False: if any platform has more than one failure
477 None: if any platform has more than one machine not yet Good.
478 True: if all platforms have at least all-but-one machines Good.
479 """
mbligh451ede12009-02-12 21:54:03 +0000480 self._job_test_results(tko, job, debug)
481 self._job_results_platform_map(job, debug)
mbligh5280e3b2008-12-22 14:39:28 +0000482
mbligh5b618382008-12-03 15:24:01 +0000483 good_platforms = []
484 bad_platforms = []
485 unknown_platforms = []
mbligh5280e3b2008-12-22 14:39:28 +0000486 platform_map = job.results_platform_map
mbligh5b618382008-12-03 15:24:01 +0000487 for platform in platform_map:
488 total = len(platform_map[platform]['Total'])
489 completed = len(platform_map[platform].get('Completed', []))
mbligh451ede12009-02-12 21:54:03 +0000490 failed = len(platform_map[platform].get('Failed', [])) + \
491 len(platform_map[platform].get('Aborted', []))
492 if (failed * 2 >= total) or (failed > 1):
mbligh5b618382008-12-03 15:24:01 +0000493 bad_platforms.append(platform)
mbligh451ede12009-02-12 21:54:03 +0000494 elif (completed >= 1) and (completed + 1 >= total):
mbligh5b618382008-12-03 15:24:01 +0000495 # if all or all but one are good, call the job good.
496 good_platforms.append(platform)
497 else:
498 unknown_platforms.append(platform)
499 detail = []
500 for status in platform_map[platform]:
501 if status == 'Total':
502 continue
503 detail.append('%s=%s' % (status,platform_map[platform][status]))
504 if debug:
505 print '%20s %d/%d %s' % (platform, completed, total,
506 ' '.join(detail))
507 print
508
509 if len(bad_platforms) > 0:
510 if debug:
511 print 'Result bad - platforms: ' + ' '.join(bad_platforms)
512 return False
513 if len(unknown_platforms) > 0:
514 if debug:
515 platform_list = ' '.join(unknown_platforms)
516 print 'Result unknown - platforms: ', platform_list
517 return None
518 if debug:
519 platform_list = ' '.join(good_platforms)
520 print 'Result good - all platforms passed: ', platform_list
521 return True
522
523
mbligh5280e3b2008-12-22 14:39:28 +0000524class TestResults(object):
525 """
526 Container class used to hold the results of the tests for a job
527 """
528 def __init__(self):
529 self.good = []
530 self.fail = []
mbligh451ede12009-02-12 21:54:03 +0000531 self.pending = []
mbligh5280e3b2008-12-22 14:39:28 +0000532
533
534 def add(self, result):
mbligh451ede12009-02-12 21:54:03 +0000535 if result.complete_count > result.pass_count:
536 self.fail.append(result)
537 elif result.incomplete_count > 0:
538 self.pending.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000539 else:
mbligh451ede12009-02-12 21:54:03 +0000540 self.good.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000541
542
543class RpcObject(object):
mbligh67647152008-11-19 00:18:14 +0000544 """
545 Generic object used to construct python objects from rpc calls
546 """
547 def __init__(self, afe, hash):
548 self.afe = afe
549 self.hash = hash
550 self.__dict__.update(hash)
551
552
553 def __str__(self):
554 return dump_object(self.__repr__(), self)
555
556
mbligh1354c9d2008-12-22 14:56:13 +0000557class ControlFile(RpcObject):
558 """
559 AFE control file object
560
561 Fields: synch_count, dependencies, control_file, is_server
562 """
563 def __repr__(self):
564 return 'CONTROL FILE: %s' % self.control_file
565
566
mbligh5280e3b2008-12-22 14:39:28 +0000567class Label(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000568 """
569 AFE label object
570
571 Fields:
572 name, invalid, platform, kernel_config, id, only_if_needed
573 """
574 def __repr__(self):
575 return 'LABEL: %s' % self.name
576
577
578 def add_hosts(self, hosts):
579 return self.afe.run('label_add_hosts', self.id, hosts)
580
581
582 def remove_hosts(self, hosts):
583 return self.afe.run('label_remove_hosts', self.id, hosts)
584
585
mbligh5280e3b2008-12-22 14:39:28 +0000586class Acl(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000587 """
588 AFE acl object
589
590 Fields:
591 users, hosts, description, name, id
592 """
593 def __repr__(self):
594 return 'ACL: %s' % self.name
595
596
597 def add_hosts(self, hosts):
598 self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name))
599 return self.afe.run('acl_group_add_hosts', self.id, hosts)
600
601
602 def remove_hosts(self, hosts):
603 self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name))
604 return self.afe.run('acl_group_remove_hosts', self.id, hosts)
605
606
mbligh54459c72009-01-21 19:26:44 +0000607 def add_users(self, users):
608 self.afe.log('Adding users %s to ACL %s' % (users, self.name))
609 return self.afe.run('acl_group_add_users', id=self.name, users=users)
610
611
mbligh5280e3b2008-12-22 14:39:28 +0000612class Job(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000613 """
614 AFE job object
615
616 Fields:
617 name, control_file, control_type, synch_count, reboot_before,
618 run_verify, priority, email_list, created_on, dependencies,
619 timeout, owner, reboot_after, id
620 """
621 def __repr__(self):
622 return 'JOB: %s' % self.id
623
624
mbligh5280e3b2008-12-22 14:39:28 +0000625class JobStatus(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000626 """
627 AFE job_status object
628
629 Fields:
630 status, complete, deleted, meta_host, host, active, execution_subdir, id
631 """
632 def __init__(self, afe, hash):
633 # This should call super
634 self.afe = afe
635 self.hash = hash
636 self.__dict__.update(hash)
mbligh5280e3b2008-12-22 14:39:28 +0000637 self.job = Job(afe, self.job)
mbligh67647152008-11-19 00:18:14 +0000638 if self.host:
639 self.host = afe.get_hosts(hostname=self.host['hostname'])[0]
640
641
642 def __repr__(self):
mbligh451ede12009-02-12 21:54:03 +0000643 if self.host and self.host.hostname:
644 hostname = self.host.hostname
645 else:
646 hostname = 'None'
647 return 'JOB STATUS: %s-%s' % (self.job.id, hostname)
mbligh67647152008-11-19 00:18:14 +0000648
649
mbligh5280e3b2008-12-22 14:39:28 +0000650class Host(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000651 """
652 AFE host object
653
654 Fields:
655 status, lock_time, locked_by, locked, hostname, invalid,
656 synch_id, labels, platform, protection, dirty, id
657 """
658 def __repr__(self):
659 return 'HOST OBJECT: %s' % self.hostname
660
661
662 def show(self):
663 labels = list(set(self.labels) - set([self.platform]))
664 print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
665 self.locked, self.platform,
666 ', '.join(labels))
667
668
mbligh54459c72009-01-21 19:26:44 +0000669 def delete(self):
670 return self.afe.run('delete_host', id=self.id)
671
672
mbligh6463c4b2009-01-30 00:33:37 +0000673 def modify(self, **dargs):
674 return self.afe.run('modify_host', id=self.id, **dargs)
675
676
mbligh67647152008-11-19 00:18:14 +0000677 def get_acls(self):
678 return self.afe.get_acls(hosts__hostname=self.hostname)
679
680
681 def add_acl(self, acl_name):
682 self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname))
683 return self.afe.run('acl_group_add_hosts', id=acl_name,
684 hosts=[self.hostname])
685
686
687 def remove_acl(self, acl_name):
688 self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname))
689 return self.afe.run('acl_group_remove_hosts', id=acl_name,
690 hosts=[self.hostname])
691
692
693 def get_labels(self):
694 return self.afe.get_labels(host__hostname__in=[self.hostname])
695
696
697 def add_labels(self, labels):
698 self.afe.log('Adding labels %s to host %s' % (labels, self.hostname))
699 return self.afe.run('host_add_labels', id=self.id, labels=labels)
700
701
702 def remove_labels(self, labels):
703 self.afe.log('Removing labels %s from host %s' % (labels,self.hostname))
704 return self.afe.run('host_remove_labels', id=self.id, labels=labels)
mbligh5b618382008-12-03 15:24:01 +0000705
706
mbligh54459c72009-01-21 19:26:44 +0000707class User(RpcObject):
708 def __repr__(self):
709 return 'USER: %s' % self.login
710
711
mbligh5280e3b2008-12-22 14:39:28 +0000712class TestStatus(RpcObject):
mblighc31e4022008-12-11 19:32:30 +0000713 """
714 TKO test status object
715
716 Fields:
717 test_idx, hostname, testname, id
718 complete_count, incomplete_count, group_count, pass_count
719 """
720 def __repr__(self):
721 return 'TEST STATUS: %s' % self.id
722
723
mbligh5b618382008-12-03 15:24:01 +0000724class MachineTestPairing(object):
725 """
726 Object representing the pairing of a machine label with a control file
mbligh1f23f362008-12-22 14:46:12 +0000727
728 machine_label: use machines from this label
729 control_file: use this control file (by name in the frontend)
730 platforms: list of rexeps to filter platforms by. [] => no filtering
mbligh5b618382008-12-03 15:24:01 +0000731 """
mbligh1354c9d2008-12-22 14:56:13 +0000732 def __init__(self, machine_label, control_file, platforms=[],
mblighb1ce3b32009-02-17 16:00:50 +0000733 container=False, synch_job=False):
mbligh5b618382008-12-03 15:24:01 +0000734 self.machine_label = machine_label
735 self.control_file = control_file
mbligh1f23f362008-12-22 14:46:12 +0000736 self.platforms = platforms
mbligh1354c9d2008-12-22 14:56:13 +0000737 self.container = container
mblighb1ce3b32009-02-17 16:00:50 +0000738 self.synch_job = synch_job
mbligh1354c9d2008-12-22 14:56:13 +0000739
740
741 def __repr__(self):
742 return '%s %s %s %s' % (self.machine_label, self.control_file,
743 self.platforms, self.container)