blob: 336de0500668149e7aa7e1ddd08c0477b094527f [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
202 dargs['synch_count'] = control_file.synch_count
mbligh4e576612008-12-22 14:56:36 +0000203 jobs = []
204 if control_file.synch_count > 1:
205 # We don't trust the scheduler to do the groupings for us.
206 synch_count = control_file.synch_count
207 (pairs, failures) = form_ntuples_from_machines(hosts, synch_count)
208 for machines in pairs:
209 jobs.append(self.create_job(hosts=machines, **dargs))
210 else:
211 jobs.append(self.create_job(hosts=hosts, **dargs))
212 return jobs
mbligh67647152008-11-19 00:18:14 +0000213
214
215 def create_job(self, control_file, name=' ', priority='Medium',
216 control_type='Client', **dargs):
217 id = self.run('create_job', name=name, priority=priority,
218 control_file=control_file, control_type=control_type, **dargs)
219 return self.get_jobs(id=id)[0]
220
221
mbligh1f23f362008-12-22 14:46:12 +0000222 def run_test_suites(self, pairings, kernel, kernel_label, priority='Medium',
223 wait=True, poll_interval=5, email_from=None,
mbligh7b312282009-01-07 16:45:43 +0000224 email_to=None, timeout=168):
mbligh5b618382008-12-03 15:24:01 +0000225 """
226 Run a list of test suites on a particular kernel.
227
228 Poll for them to complete, and return whether they worked or not.
229
230 pairings: list of MachineTestPairing objects to invoke
231 kernel: name of the kernel to run
232 kernel_label: label of the kernel to run
233 (<kernel-version> : <config> : <date>)
234 wait: boolean - wait for the results to come back?
235 poll_interval: interval between polling for job results (in minutes)
mbligh45ffc432008-12-09 23:35:17 +0000236 email_from: send notification email upon completion from here
237 email_from: send notification email upon completion to here
mbligh5b618382008-12-03 15:24:01 +0000238 """
239 jobs = []
240 for pairing in pairings:
mbligh7b312282009-01-07 16:45:43 +0000241 new_jobs = self.invoke_test(pairing, kernel, kernel_label, priority,
242 timeout=timeout)
mbligh4e576612008-12-22 14:56:36 +0000243 for job in new_jobs:
244 job.notified = False
245 jobs += new_jobs
mbligh5280e3b2008-12-22 14:39:28 +0000246 # disabled - this is just for debugging: mbligh
247 # if email_from and email_to:
248 # subject = 'Testing started: %s : %s' % (job.name, job.id)
249 # utils.send_email(email_from, email_to, subject, subject)
mbligh5b618382008-12-03 15:24:01 +0000250 if not wait:
251 return
mbligh5280e3b2008-12-22 14:39:28 +0000252 tko = TKO()
mbligh5b618382008-12-03 15:24:01 +0000253 while True:
254 time.sleep(60 * poll_interval)
mbligh5280e3b2008-12-22 14:39:28 +0000255 result = self.poll_all_jobs(tko, jobs, email_from, email_to)
mbligh5b618382008-12-03 15:24:01 +0000256 if result is not None:
257 return result
258
259
mbligh45ffc432008-12-09 23:35:17 +0000260 def result_notify(self, job, email_from, email_to):
mbligh5b618382008-12-03 15:24:01 +0000261 """
mbligh45ffc432008-12-09 23:35:17 +0000262 Notify about the result of a job. Will always print, if email data
263 is provided, will send email for it as well.
264
265 job: job object to notify about
266 email_from: send notification email upon completion from here
267 email_from: send notification email upon completion to here
268 """
269 if job.result == True:
270 subject = 'Testing PASSED: '
271 else:
272 subject = 'Testing FAILED: '
273 subject += '%s : %s\n' % (job.name, job.id)
274 text = []
275 for platform in job.results_platform_map:
276 for status in job.results_platform_map[platform]:
277 if status == 'Total':
278 continue
mbligh451ede12009-02-12 21:54:03 +0000279 for host in job.results_platform_map[platform][status]:
280 text.append('%20s %10s %10s' % (platform, status, host))
281 if status == 'Failed':
282 for test_status in job.test_status[host].fail:
283 text.append('(%s, %s) : %s' % \
284 (host, test_status.test_name,
285 test_status.reason))
286 text.append('')
mbligh37eceaa2008-12-15 22:56:37 +0000287
mbligh451ede12009-02-12 21:54:03 +0000288 base_url = 'http://' + self.server
mbligh37eceaa2008-12-15 22:56:37 +0000289
290 params = ('columns=test',
291 'rows=machine_group',
292 "condition=tag~'%s-%%25'" % job.id,
293 'title=Report')
294 query_string = '&'.join(params)
mbligh451ede12009-02-12 21:54:03 +0000295 url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string)
296 text.append(url + '\n')
297 url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
298 text.append(url + '\n')
mbligh37eceaa2008-12-15 22:56:37 +0000299
300 body = '\n'.join(text)
301 print '---------------------------------------------------'
302 print 'Subject: ', subject
mbligh45ffc432008-12-09 23:35:17 +0000303 print body
mbligh37eceaa2008-12-15 22:56:37 +0000304 print '---------------------------------------------------'
mbligh45ffc432008-12-09 23:35:17 +0000305 if email_from and email_to:
mbligh37eceaa2008-12-15 22:56:37 +0000306 print 'Sending email ...'
mbligh45ffc432008-12-09 23:35:17 +0000307 utils.send_email(email_from, email_to, subject, body)
308 print
mbligh37eceaa2008-12-15 22:56:37 +0000309
mbligh45ffc432008-12-09 23:35:17 +0000310
mbligh1354c9d2008-12-22 14:56:13 +0000311 def print_job_result(self, job):
312 """
313 Print the result of a single job.
314 job: a job object
315 """
316 if job.result is None:
317 print 'PENDING',
318 elif job.result == True:
319 print 'PASSED',
320 elif job.result == False:
321 print 'FAILED',
322 print ' %s : %s' % (job.id, job.name)
323
324
mbligh451ede12009-02-12 21:54:03 +0000325 def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None):
mbligh45ffc432008-12-09 23:35:17 +0000326 """
327 Poll all jobs in a list.
328 jobs: list of job objects to poll
329 email_from: send notification email upon completion from here
330 email_from: send notification email upon completion to here
331
332 Returns:
mbligh5b618382008-12-03 15:24:01 +0000333 a) All complete successfully (return True)
334 b) One or more has failed (return False)
335 c) Cannot tell yet (return None)
336 """
mbligh45ffc432008-12-09 23:35:17 +0000337 results = []
mbligh5b618382008-12-03 15:24:01 +0000338 for job in jobs:
mbligh451ede12009-02-12 21:54:03 +0000339 job.result = self.poll_job_results(tko, job)
mbligh45ffc432008-12-09 23:35:17 +0000340 results.append(job.result)
341 if job.result is not None and not job.notified:
342 self.result_notify(job, email_from, email_to)
343 job.notified = True
344
mbligh1354c9d2008-12-22 14:56:13 +0000345 self.print_job_result(job)
mbligh45ffc432008-12-09 23:35:17 +0000346
347 if None in results:
348 return None
349 elif False in results:
350 return False
351 else:
352 return True
mbligh5b618382008-12-03 15:24:01 +0000353
354
mbligh1f23f362008-12-22 14:46:12 +0000355 def _included_platform(self, host, platforms):
356 """
357 See if host's platforms matches any of the patterns in the included
358 platforms list.
359 """
360 if not platforms:
361 return True # No filtering of platforms
362 for platform in platforms:
363 if re.search(platform, host.platform):
364 return True
365 return False
366
367
mbligh7b312282009-01-07 16:45:43 +0000368 def invoke_test(self, pairing, kernel, kernel_label, priority='Medium',
369 **dargs):
mbligh5b618382008-12-03 15:24:01 +0000370 """
371 Given a pairing of a control file to a machine label, find all machines
372 with that label, and submit that control file to them.
373
mbligh4e576612008-12-22 14:56:36 +0000374 Returns a list of job objects
mbligh5b618382008-12-03 15:24:01 +0000375 """
376 job_name = '%s : %s' % (pairing.machine_label, kernel_label)
377 hosts = self.get_hosts(multiple_labels=[pairing.machine_label])
mbligh1f23f362008-12-22 14:46:12 +0000378 platforms = pairing.platforms
379 hosts = [h for h in hosts if self._included_platform(h, platforms)]
mbligh45ffc432008-12-09 23:35:17 +0000380 host_list = [h.hostname for h in hosts if h.status != 'Repair Failed']
mbligh1f23f362008-12-22 14:46:12 +0000381 print 'HOSTS: %s' % host_list
mbligh4e576612008-12-22 14:56:36 +0000382 new_jobs = self.create_job_by_test(name=job_name,
mbligh7b312282009-01-07 16:45:43 +0000383 dependencies=[pairing.machine_label],
384 tests=[pairing.control_file],
385 priority=priority,
386 hosts=host_list,
387 kernel=kernel,
388 use_container=pairing.container,
389 **dargs)
mbligh4e576612008-12-22 14:56:36 +0000390 for new_job in new_jobs:
391 print 'Invoked test %s : %s' % (new_job.id, job_name)
392 return new_jobs
mbligh5b618382008-12-03 15:24:01 +0000393
394
mbligh451ede12009-02-12 21:54:03 +0000395 def _job_test_results(self, tko, job, debug):
mbligh5b618382008-12-03 15:24:01 +0000396 """
mbligh5280e3b2008-12-22 14:39:28 +0000397 Retrieve test results for a job
mbligh5b618382008-12-03 15:24:01 +0000398 """
mbligh5280e3b2008-12-22 14:39:28 +0000399 job.test_status = {}
400 try:
401 test_statuses = tko.get_status_counts(job=job.id)
402 except Exception:
403 print "Ignoring exception on poll job; RPC interface is flaky"
404 traceback.print_exc()
405 return
406
407 for test_status in test_statuses:
mbligh7479a182009-01-07 16:46:24 +0000408 # SERVER_JOB is buggy, and often gives false failures. Ignore it.
409 if test_status.test_name == 'SERVER_JOB':
410 continue
mbligh451ede12009-02-12 21:54:03 +0000411 if debug:
412 print test_status
mbligh5280e3b2008-12-22 14:39:28 +0000413 hostname = test_status.hostname
414 if hostname not in job.test_status:
415 job.test_status[hostname] = TestResults()
416 job.test_status[hostname].add(test_status)
417
418
mbligh451ede12009-02-12 21:54:03 +0000419 def _job_results_platform_map(self, job, debug):
mbligh5280e3b2008-12-22 14:39:28 +0000420 job.results_platform_map = {}
mbligh5b618382008-12-03 15:24:01 +0000421 try:
mbligh45ffc432008-12-09 23:35:17 +0000422 job_statuses = self.get_host_queue_entries(job=job.id)
mbligh5b618382008-12-03 15:24:01 +0000423 except Exception:
424 print "Ignoring exception on poll job; RPC interface is flaky"
425 traceback.print_exc()
426 return None
mbligh5280e3b2008-12-22 14:39:28 +0000427
mbligh5b618382008-12-03 15:24:01 +0000428 platform_map = {}
mbligh5280e3b2008-12-22 14:39:28 +0000429 job.job_status = {}
mbligh451ede12009-02-12 21:54:03 +0000430 job.metahost_index = {}
mbligh5b618382008-12-03 15:24:01 +0000431 for job_status in job_statuses:
mbligh451ede12009-02-12 21:54:03 +0000432 if job_status.host:
433 hostname = job_status.host.hostname
434 else: # This is a metahost
435 metahost = job_status.meta_host
436 index = job.metahost_index.get(metahost, 1)
437 job.metahost_index[metahost] = index + 1
438 hostname = '%s.%s' % (metahost, index)
mbligh5280e3b2008-12-22 14:39:28 +0000439 job.job_status[hostname] = job_status.status
mbligh5b618382008-12-03 15:24:01 +0000440 status = job_status.status
mbligh451ede12009-02-12 21:54:03 +0000441 # Skip hosts that failed verify - that's a machine failure,
442 # not a job failure
443 if hostname in job.test_status:
444 verify_failed = False
445 for failure in job.test_status[hostname].fail:
446 if failure.test_name == 'verify':
447 verify_failed = True
448 break
449 if verify_failed:
450 continue
mbligh5280e3b2008-12-22 14:39:28 +0000451 if hostname in job.test_status and job.test_status[hostname].fail:
452 # Job status doesn't reflect failed tests, override that
453 status = 'Failed'
mbligh451ede12009-02-12 21:54:03 +0000454 if job_status.host:
455 platform = job_status.host.platform
456 else: # This is a metahost
457 platform = job_status.meta_host
mbligh5b618382008-12-03 15:24:01 +0000458 if platform not in platform_map:
459 platform_map[platform] = {'Total' : [hostname]}
460 else:
461 platform_map[platform]['Total'].append(hostname)
462 new_host_list = platform_map[platform].get(status, []) + [hostname]
463 platform_map[platform][status] = new_host_list
mbligh45ffc432008-12-09 23:35:17 +0000464 job.results_platform_map = platform_map
mbligh5280e3b2008-12-22 14:39:28 +0000465
466
467 def poll_job_results(self, tko, job, debug=False):
468 """
469 Analyse all job results by platform, return:
mbligh5b618382008-12-03 15:24:01 +0000470
mbligh5280e3b2008-12-22 14:39:28 +0000471 False: if any platform has more than one failure
472 None: if any platform has more than one machine not yet Good.
473 True: if all platforms have at least all-but-one machines Good.
474 """
mbligh451ede12009-02-12 21:54:03 +0000475 self._job_test_results(tko, job, debug)
476 self._job_results_platform_map(job, debug)
mbligh5280e3b2008-12-22 14:39:28 +0000477
mbligh5b618382008-12-03 15:24:01 +0000478 good_platforms = []
479 bad_platforms = []
480 unknown_platforms = []
mbligh5280e3b2008-12-22 14:39:28 +0000481 platform_map = job.results_platform_map
mbligh5b618382008-12-03 15:24:01 +0000482 for platform in platform_map:
483 total = len(platform_map[platform]['Total'])
484 completed = len(platform_map[platform].get('Completed', []))
mbligh451ede12009-02-12 21:54:03 +0000485 failed = len(platform_map[platform].get('Failed', [])) + \
486 len(platform_map[platform].get('Aborted', []))
487 if (failed * 2 >= total) or (failed > 1):
mbligh5b618382008-12-03 15:24:01 +0000488 bad_platforms.append(platform)
mbligh451ede12009-02-12 21:54:03 +0000489 elif (completed >= 1) and (completed + 1 >= total):
mbligh5b618382008-12-03 15:24:01 +0000490 # if all or all but one are good, call the job good.
491 good_platforms.append(platform)
492 else:
493 unknown_platforms.append(platform)
494 detail = []
495 for status in platform_map[platform]:
496 if status == 'Total':
497 continue
498 detail.append('%s=%s' % (status,platform_map[platform][status]))
499 if debug:
500 print '%20s %d/%d %s' % (platform, completed, total,
501 ' '.join(detail))
502 print
503
504 if len(bad_platforms) > 0:
505 if debug:
506 print 'Result bad - platforms: ' + ' '.join(bad_platforms)
507 return False
508 if len(unknown_platforms) > 0:
509 if debug:
510 platform_list = ' '.join(unknown_platforms)
511 print 'Result unknown - platforms: ', platform_list
512 return None
513 if debug:
514 platform_list = ' '.join(good_platforms)
515 print 'Result good - all platforms passed: ', platform_list
516 return True
517
518
mbligh5280e3b2008-12-22 14:39:28 +0000519class TestResults(object):
520 """
521 Container class used to hold the results of the tests for a job
522 """
523 def __init__(self):
524 self.good = []
525 self.fail = []
mbligh451ede12009-02-12 21:54:03 +0000526 self.pending = []
mbligh5280e3b2008-12-22 14:39:28 +0000527
528
529 def add(self, result):
mbligh451ede12009-02-12 21:54:03 +0000530 if result.complete_count > result.pass_count:
531 self.fail.append(result)
532 elif result.incomplete_count > 0:
533 self.pending.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000534 else:
mbligh451ede12009-02-12 21:54:03 +0000535 self.good.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000536
537
538class RpcObject(object):
mbligh67647152008-11-19 00:18:14 +0000539 """
540 Generic object used to construct python objects from rpc calls
541 """
542 def __init__(self, afe, hash):
543 self.afe = afe
544 self.hash = hash
545 self.__dict__.update(hash)
546
547
548 def __str__(self):
549 return dump_object(self.__repr__(), self)
550
551
mbligh1354c9d2008-12-22 14:56:13 +0000552class ControlFile(RpcObject):
553 """
554 AFE control file object
555
556 Fields: synch_count, dependencies, control_file, is_server
557 """
558 def __repr__(self):
559 return 'CONTROL FILE: %s' % self.control_file
560
561
mbligh5280e3b2008-12-22 14:39:28 +0000562class Label(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000563 """
564 AFE label object
565
566 Fields:
567 name, invalid, platform, kernel_config, id, only_if_needed
568 """
569 def __repr__(self):
570 return 'LABEL: %s' % self.name
571
572
573 def add_hosts(self, hosts):
574 return self.afe.run('label_add_hosts', self.id, hosts)
575
576
577 def remove_hosts(self, hosts):
578 return self.afe.run('label_remove_hosts', self.id, hosts)
579
580
mbligh5280e3b2008-12-22 14:39:28 +0000581class Acl(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000582 """
583 AFE acl object
584
585 Fields:
586 users, hosts, description, name, id
587 """
588 def __repr__(self):
589 return 'ACL: %s' % self.name
590
591
592 def add_hosts(self, hosts):
593 self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name))
594 return self.afe.run('acl_group_add_hosts', self.id, hosts)
595
596
597 def remove_hosts(self, hosts):
598 self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name))
599 return self.afe.run('acl_group_remove_hosts', self.id, hosts)
600
601
mbligh54459c72009-01-21 19:26:44 +0000602 def add_users(self, users):
603 self.afe.log('Adding users %s to ACL %s' % (users, self.name))
604 return self.afe.run('acl_group_add_users', id=self.name, users=users)
605
606
mbligh5280e3b2008-12-22 14:39:28 +0000607class Job(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000608 """
609 AFE job object
610
611 Fields:
612 name, control_file, control_type, synch_count, reboot_before,
613 run_verify, priority, email_list, created_on, dependencies,
614 timeout, owner, reboot_after, id
615 """
616 def __repr__(self):
617 return 'JOB: %s' % self.id
618
619
mbligh5280e3b2008-12-22 14:39:28 +0000620class JobStatus(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000621 """
622 AFE job_status object
623
624 Fields:
625 status, complete, deleted, meta_host, host, active, execution_subdir, id
626 """
627 def __init__(self, afe, hash):
628 # This should call super
629 self.afe = afe
630 self.hash = hash
631 self.__dict__.update(hash)
mbligh5280e3b2008-12-22 14:39:28 +0000632 self.job = Job(afe, self.job)
mbligh67647152008-11-19 00:18:14 +0000633 if self.host:
634 self.host = afe.get_hosts(hostname=self.host['hostname'])[0]
635
636
637 def __repr__(self):
mbligh451ede12009-02-12 21:54:03 +0000638 if self.host and self.host.hostname:
639 hostname = self.host.hostname
640 else:
641 hostname = 'None'
642 return 'JOB STATUS: %s-%s' % (self.job.id, hostname)
mbligh67647152008-11-19 00:18:14 +0000643
644
mbligh5280e3b2008-12-22 14:39:28 +0000645class Host(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000646 """
647 AFE host object
648
649 Fields:
650 status, lock_time, locked_by, locked, hostname, invalid,
651 synch_id, labels, platform, protection, dirty, id
652 """
653 def __repr__(self):
654 return 'HOST OBJECT: %s' % self.hostname
655
656
657 def show(self):
658 labels = list(set(self.labels) - set([self.platform]))
659 print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
660 self.locked, self.platform,
661 ', '.join(labels))
662
663
mbligh54459c72009-01-21 19:26:44 +0000664 def delete(self):
665 return self.afe.run('delete_host', id=self.id)
666
667
mbligh6463c4b2009-01-30 00:33:37 +0000668 def modify(self, **dargs):
669 return self.afe.run('modify_host', id=self.id, **dargs)
670
671
mbligh67647152008-11-19 00:18:14 +0000672 def get_acls(self):
673 return self.afe.get_acls(hosts__hostname=self.hostname)
674
675
676 def add_acl(self, acl_name):
677 self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname))
678 return self.afe.run('acl_group_add_hosts', id=acl_name,
679 hosts=[self.hostname])
680
681
682 def remove_acl(self, acl_name):
683 self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname))
684 return self.afe.run('acl_group_remove_hosts', id=acl_name,
685 hosts=[self.hostname])
686
687
688 def get_labels(self):
689 return self.afe.get_labels(host__hostname__in=[self.hostname])
690
691
692 def add_labels(self, labels):
693 self.afe.log('Adding labels %s to host %s' % (labels, self.hostname))
694 return self.afe.run('host_add_labels', id=self.id, labels=labels)
695
696
697 def remove_labels(self, labels):
698 self.afe.log('Removing labels %s from host %s' % (labels,self.hostname))
699 return self.afe.run('host_remove_labels', id=self.id, labels=labels)
mbligh5b618382008-12-03 15:24:01 +0000700
701
mbligh54459c72009-01-21 19:26:44 +0000702class User(RpcObject):
703 def __repr__(self):
704 return 'USER: %s' % self.login
705
706
mbligh5280e3b2008-12-22 14:39:28 +0000707class TestStatus(RpcObject):
mblighc31e4022008-12-11 19:32:30 +0000708 """
709 TKO test status object
710
711 Fields:
712 test_idx, hostname, testname, id
713 complete_count, incomplete_count, group_count, pass_count
714 """
715 def __repr__(self):
716 return 'TEST STATUS: %s' % self.id
717
718
mbligh5b618382008-12-03 15:24:01 +0000719class MachineTestPairing(object):
720 """
721 Object representing the pairing of a machine label with a control file
mbligh1f23f362008-12-22 14:46:12 +0000722
723 machine_label: use machines from this label
724 control_file: use this control file (by name in the frontend)
725 platforms: list of rexeps to filter platforms by. [] => no filtering
mbligh5b618382008-12-03 15:24:01 +0000726 """
mbligh1354c9d2008-12-22 14:56:13 +0000727 def __init__(self, machine_label, control_file, platforms=[],
728 container=False):
mbligh5b618382008-12-03 15:24:01 +0000729 self.machine_label = machine_label
730 self.control_file = control_file
mbligh1f23f362008-12-22 14:46:12 +0000731 self.platforms = platforms
mbligh1354c9d2008-12-22 14:56:13 +0000732 self.container = container
733
734
735 def __repr__(self):
736 return '%s %s %s %s' % (self.machine_label, self.control_file,
737 self.platforms, self.container)