Home | History | Annotate | Download | only in control_segments
      1 import logging
      2 import os
      3 
      4 from autotest_lib.client.common_lib import error
      5 from autotest_lib.server import utils
      6 from autotest_lib.server.cros import provision
      7 
      8 try:
      9     from chromite.lib import metrics
     10 except ImportError:
     11     metrics = utils.metrics_mock
     12 
     13 
     14 DURATION_METRIC = 'chromeos/autotest/autoserv/cleanup_duration'
     15 
     16 
     17 # A string of the form 'label1,label2:value,label3'.
     18 job_labels = locals().get('job_labels') or ','.join(args)
     19 labels_list = [l.strip() for l in job_labels.split(',') if l]
     20 
     21 
     22 def 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.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 
     55 job.parallel_simple(cleanup, machines, log=False)
     56 
     57 # vim: set syntax=python :
     58