1import logging 2import os 3 4from autotest_lib.client.common_lib import error 5from autotest_lib.server import utils 6from autotest_lib.server.cros import provision 7 8try: 9 from chromite.lib import metrics 10except ImportError: 11 metrics = utils.metrics_mock 12 13 14DURATION_METRIC = 'chromeos/autotest/autoserv/cleanup_duration' 15 16 17# A string of the form 'label1,label2:value,label3'. 18job_labels = locals().get('job_labels') or ','.join(args) 19labels_list = [l.strip() for l in job_labels.split(',') if l] 20 21 22def cleanup(machine): 23 try: 24 hostname = utils.get_hostname_from_machine(machine) 25 job.record('START', None, 'cleanup') 26 host = hosts.create_host(machine, try_lab_servo=True) 27 with metrics.SecondsTimer(DURATION_METRIC, 28 fields={'dut_host_name': hostname}): 29 # Try to save /var/log files. If the dut is not sshable, try to 30 # restart with servo, to collect logs for test failed with dut 31 # not returning from reboot. 32 # TODO: This is a temp fix and should no longer be necessary 33 # Depended on crbug.com/336985, fixed in 2015 34 try: 35 host.ssh_ping() 36 except error.AutoservSshPingHostError: 37 # Try to restart dut with servo. 38 host._servo_repair_power() 39 local_log_dir = os.path.join(job.resultdir, hostname) 40 host.collect_logs('/var/log', local_log_dir, ignore_errors=True) 41 42 host.cleanup() 43 provision.Cleanup.run_task_actions(job, host, labels_list) 44 except Exception: 45 logging.exception('Cleanup failed due to Exception.') 46 job.record('END FAIL', None, 'cleanup') 47 # See the provision control segment for the explanation of why we're 48 # doing this. 49 raise Exception('') 50 else: 51 job.record('END GOOD', None, 'cleanup', 52 '%s cleaned successfully' % hostname) 53 54 55job.parallel_simple(cleanup, machines, log=False) 56 57# vim: set syntax=python : 58