blob: 43fccc5be5dab8c98fb27e18752f4d272ed9182f [file] [log] [blame]
Alex Miller0516e4c2013-06-03 18:07:48 -07001# 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 Chengf8660142016-08-12 10:17:41 -07005import datetime
Simran Basiadf31312016-06-28 14:23:05 -07006import re
Alex Miller0516e4c2013-06-03 18:07:48 -07007
Kevin Chengf8660142016-08-12 10:17:41 -07008from chromite.lib import metrics
9
Hsinyu Chaoe0b08e62015-08-11 10:50:37 +000010from autotest_lib.client.cros import constants
Richard Barnette28aa6892016-06-10 13:00:58 -070011from autotest_lib.server import utils
Alex Miller0516e4c2013-06-03 18:07:48 -070012from autotest_lib.server.cros import provision
13
Simran Basiadf31312016-06-28 14:23:05 -070014LABEL_REGEX = r',.*:'
Kevin Chengf8660142016-08-12 10:17:41 -070015_LABEL_UPDATE_DURATION_METRIC = metrics.SecondsDistribution(
16 'chromeos/autotest/provision/label_update_durations')
Alex Miller0516e4c2013-06-03 18:07:48 -070017
Alex Miller667b5f22014-02-28 15:33:39 -080018# job_labels should be a string like "name:setting,name:setting"
Simran Basiadf31312016-06-28 14:23:05 -070019# However setting might also contain ',' therefore we need more advanced logic
20# than split.
Alex Miller0516e4c2013-06-03 18:07:48 -070021# non-provisionable labels are currently skipped, so they're safe to pass in.
Alex Miller667b5f22014-02-28 15:33:39 -080022job_labels = locals().get('job_labels') or ','.join(args)
Simran Basiadf31312016-06-28 14:23:05 -070023labels_list = []
24while job_labels:
25 # Split based off of a comma followed by colon regex.
26 split = re.split(LABEL_REGEX, job_labels)
27 # First value found is a proper key value pair.
28 labels_list.append(split[0].strip())
29 # Remove this key value pair.
30 job_labels = job_labels[len(split[0]):]
31 # If a comma remains at the start of the remaining labels, remove it.
32 # This should happen on every loop except the last one.
33 if job_labels.startswith(','):
34 job_labels = job_labels.lstrip(',')
Alex Miller0516e4c2013-06-03 18:07:48 -070035
36
37def provision_machine(machine):
38 """
39 Run the appropriate provisioning tests to make the machine's labels match
Alex Miller667b5f22014-02-28 15:33:39 -080040 those given in job_labels.
Alex Miller0516e4c2013-06-03 18:07:48 -070041 """
Simran Basiadf31312016-06-28 14:23:05 -070042 host = hosts.create_target_machine(machine, try_lab_servo=True)
Alex Miller0516e4c2013-06-03 18:07:48 -070043
Alex Miller0516e4c2013-06-03 18:07:48 -070044 job.record('START', None, 'provision')
Alex Miller0516e4c2013-06-03 18:07:48 -070045 try:
Alex Miller667b5f22014-02-28 15:33:39 -080046 job.sysinfo.add_logdir(constants.AUTOUPDATE_PRESERVE_LOG)
47 provision.run_special_task_actions(job, host, labels_list,
48 provision.Provision)
Richard Barnette459592e2016-04-20 16:06:25 -070049 host.verify()
Kevin Chengf8660142016-08-12 10:17:41 -070050
51 # Let's update the labels on the host and track how long it takes.
52 # Don't fail while updating the labels, provision is flaky enough by
53 # itself.
54 label_update_success = True
55 start_time = datetime.datetime.now()
56 try:
57 host.update_labels()
58 except Exception as e:
59 logging.exception('Exception while updating labels: %s', e)
60 label_update_success = False
61
62 end_time = datetime.datetime.now()
63 duration = (end_time - start_time).total_seconds()
64
65 fields = {'success': label_update_success,
66 # TODO(kevcheng): Need a better way of classifying testbeds.
67 'board': (host.get_board()
68 if not utils.machine_is_testbed(machine)
69 else host.get_platform())}
70 _LABEL_UPDATE_DURATION_METRIC.add(duration, fields=fields)
Alex Miller0516e4c2013-06-03 18:07:48 -070071 except Exception as e:
Alex Miller789d6f12014-05-02 13:11:15 -070072 logging.exception(e)
Alex Milleredb936d2013-12-05 16:53:21 -080073 job.record('END FAIL', None, 'provision')
74 # Raising a blank exception is done here because any message we can
75 # give here would be less useful than whatever the failing test left as
76 # its own exception message.
77 #
78 # The gory details of how raising a blank exception accomplishes this
79 # is as follows:
80 #
81 # The scheduler only looks at the return code of autoserv to see if
82 # the special task failed. Therefore we need python to exit because
83 # of an unhandled exception or because someone called sys.exit(1).
84 #
85 # We can't call sys.exit, since there's post-job-running logic (like
86 # cleanup) that we'd be skipping out on. So therefore, we need to
87 # raise an exception. However, if we raise an exception, this
88 # exception ends up triggering server_job to write an INFO line with
89 # job_abort_reason equal to str(e), which the tko parser then picks
90 # up as the reason field for the job when the status.log we generate is
91 # parsed as the job's results.
92 #
93 # So therefore, we raise a blank exception, which then generates an
94 # empty job_abort_reason which the tko parser ignores just inserts as
95 # a SERVER_JOB failure with no reason, which we then ignore at suite
96 # results reporting time.
97 raise Exception('')
Alex Miller0516e4c2013-06-03 18:07:48 -070098 else:
Alex Millerdfff2fd2013-05-28 13:05:06 -070099 # If we finish successfully, nothing in autotest ever looks at the
100 # status.log, so it's purely for human consumption and tracability.
Richard Barnette28aa6892016-06-10 13:00:58 -0700101 hostname = utils.get_hostname_from_machine(machine)
Alex Miller0516e4c2013-06-03 18:07:48 -0700102 job.record('END GOOD', None, 'provision',
Richard Barnette28aa6892016-06-10 13:00:58 -0700103 '%s provisioned successfully' % hostname)
Alex Miller0516e4c2013-06-03 18:07:48 -0700104
105
106job.parallel_simple(provision_machine, machines)
107
108# vim: set syntax=python :