Home | History | Annotate | Download | only in control_segments
      1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import datetime
      6 import re
      7 
      8 from autotest_lib.client.bin import sysinfo
      9 from autotest_lib.client.cros import constants
     10 from autotest_lib.server import utils
     11 from autotest_lib.server.cros import provision
     12 
     13 try:
     14     from chromite.lib import metrics
     15 except ImportError:
     16     metrics = utils.metrics_mock
     17 
     18 
     19 LABEL_REGEX = r',.*:'
     20 _LABEL_UPDATE_DURATION_METRIC = metrics.SecondsDistribution(
     21         'chromeos/autotest/provision/label_update_durations')
     22 
     23 # job_labels should be a string like "name:setting,name:setting"
     24 # However setting might also contain ',' therefore we need more advanced logic
     25 # than split.
     26 # non-provisionable labels are currently skipped, so they're safe to pass in.
     27 job_labels = locals().get('job_labels') or ','.join(args)
     28 labels_list = []
     29 while job_labels:
     30     # Split based off of a comma followed by colon regex.
     31     split = re.split(LABEL_REGEX, job_labels)
     32     # First value found is a proper key value pair.
     33     labels_list.append(split[0].strip())
     34     # Remove this key value pair.
     35     job_labels = job_labels[len(split[0]):]
     36     # If a comma remains at the start of the remaining labels, remove it.
     37     # This should happen on every loop except the last one.
     38     if job_labels.startswith(','):
     39         job_labels = job_labels.lstrip(',')
     40 
     41 
     42 def provision_machine(machine):
     43     """
     44     Run the appropriate provisioning tests to make the machine's labels match
     45     those given in job_labels.
     46     """
     47     job.record('START', None, 'provision')
     48     host = hosts.create_target_machine(machine, try_lab_servo=True)
     49     try:
     50         job.sysinfo.add_logdir(
     51                 sysinfo.logdir(constants.AUTOUPDATE_PRESERVE_LOG))
     52         provision.run_special_task_actions(job, host, labels_list,
     53                                            provision.Provision)
     54         host.verify()
     55 
     56         # Let's update the labels on the host and track how long it takes.
     57         # Don't fail while updating the labels, provision is flaky enough by
     58         # itself.
     59         label_update_success = True
     60         start_time = datetime.datetime.now()
     61         try:
     62             host.update_labels()
     63         except Exception:
     64             logging.exception('Exception while updating labels.')
     65             label_update_success = False
     66 
     67         end_time = datetime.datetime.now()
     68         duration = (end_time - start_time).total_seconds()
     69 
     70         fields = {'success': label_update_success,
     71                   # TODO(kevcheng): Need a better way of classifying testbeds.
     72                   'board': (host.get_board()
     73                             if not utils.machine_is_testbed(machine)
     74                             else host.get_platform())}
     75         _LABEL_UPDATE_DURATION_METRIC.add(duration, fields=fields)
     76     except Exception:
     77         logging.exception('Provision failed due to Exception.')
     78         job.record('END FAIL', None, 'provision')
     79         # Raising a blank exception is done here because any message we can
     80         # give here would be less useful than whatever the failing test left as
     81         # its own exception message.
     82         #
     83         # The gory details of how raising a blank exception accomplishes this
     84         # is as follows:
     85         #
     86         # The scheduler only looks at the return code of autoserv to see if
     87         # the special task failed.  Therefore we need python to exit because
     88         # of an unhandled exception or because someone called sys.exit(1).
     89         #
     90         # We can't call sys.exit, since there's post-job-running logic (like
     91         # cleanup) that we'd be skipping out on.  So therefore, we need to
     92         # raise an exception.  However, if we raise an exception, this
     93         # exception ends up triggering server_job to write an INFO line with
     94         # job_abort_reason equal to str(e), which the tko parser then picks
     95         # up as the reason field for the job when the status.log we generate is
     96         # parsed as the job's results.
     97         #
     98         # So therefore, we raise a blank exception, which then generates an
     99         # empty job_abort_reason which the tko parser ignores just inserts as
    100         # a SERVER_JOB failure with no reason, which we then ignore at suite
    101         # results reporting time.
    102         raise Exception('')
    103     else:
    104         # If we finish successfully, nothing in autotest ever looks at the
    105         # status.log, so it's purely for human consumption and tracability.
    106         hostname = utils.get_hostname_from_machine(machine)
    107         job.record('END GOOD', None, 'provision',
    108                    '%s provisioned successfully' % hostname)
    109 
    110 
    111 job.parallel_simple(provision_machine, machines)
    112 
    113 # vim: set syntax=python :
    114