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