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