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 restart 30 # with servo. This is a temp fix to collect log for test failed with dut 31 # not returning from reboot. 32 # TODO(dshi): This temp fix should be removed after crash collect work 33 # is completed, crbug.com/336985 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.run_special_task_actions(job, host, labels_list, 44 provision.Cleanup) 45 except Exception: 46 logging.exception('Cleanup failed due to Exception.') 47 job.record('END FAIL', None, 'cleanup') 48 # See the provision control segment for the explanation of why we're 49 # doing this. 50 raise Exception('') 51 else: 52 job.record('END GOOD', None, 'cleanup', 53 '%s cleaned successfully' % hostname) 54 55 56job.parallel_simple(cleanup, machines, log=False) 57 58# vim: set syntax=python : 59