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 5import datetime 6import re 7 8from autotest_lib.client.bin import sysinfo 9from autotest_lib.client.cros import constants 10from autotest_lib.server import utils 11from autotest_lib.server.cros import provision 12 13try: 14 from autotest_lib.utils.frozen_chromite.lib import metrics 15except ImportError: 16 metrics = utils.metrics_mock 17 18 19LABEL_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. 27job_labels = locals().get('job_labels') or ','.join(args) 28labels_list = [] 29while 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 42def 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 # Determine if we should initialize servo based on request type as 49 # we don't need servo in OS only provision. 50 need_servo = False 51 for label in labels_list: 52 if (label.startswith(provision.FW_RW_VERSION_PREFIX) or 53 label.startswith(provision.FW_RO_VERSION_PREFIX)): 54 need_servo = True 55 host = hosts.create_target_machine(machine, try_lab_servo=need_servo) 56 try: 57 job.sysinfo.add_logdir( 58 sysinfo.logdir(constants.AUTOUPDATE_PRESERVE_LOG)) 59 provision.Provision.run_task_actions(job, host, labels_list) 60 host.verify() 61 62 # Let's update the labels on the host and track how long it takes. 63 # Don't fail while updating the labels, provision is flaky enough by 64 # itself. 65 label_update_success = True 66 start_time = datetime.datetime.now() 67 try: 68 host.labels.update_labels(host, keep_pool=True) 69 except Exception: 70 logging.exception('Exception while updating labels.') 71 label_update_success = False 72 73 end_time = datetime.datetime.now() 74 duration = (end_time - start_time).total_seconds() 75 76 fields = {'success': label_update_success, 77 'board': host.get_board()} 78 _LABEL_UPDATE_DURATION_METRIC.add(duration, fields=fields) 79 except Exception: 80 logging.exception('Provision failed due to Exception.') 81 job.record('END FAIL', None, 'provision') 82 # Raising a blank exception is done here because any message we can 83 # give here would be less useful than whatever the failing test left as 84 # its own exception message. 85 # 86 # The gory details of how raising a blank exception accomplishes this 87 # is as follows: 88 # 89 # The scheduler only looks at the return code of autoserv to see if 90 # the special task failed. Therefore we need python to exit because 91 # of an unhandled exception or because someone called sys.exit(1). 92 # 93 # We can't call sys.exit, since there's post-job-running logic (like 94 # cleanup) that we'd be skipping out on. So therefore, we need to 95 # raise an exception. However, if we raise an exception, this 96 # exception ends up triggering server_job to write an INFO line with 97 # job_abort_reason equal to str(e), which the tko parser then picks 98 # up as the reason field for the job when the status.log we generate is 99 # parsed as the job's results. 100 # 101 # So therefore, we raise a blank exception, which then generates an 102 # empty job_abort_reason which the tko parser ignores just inserts as 103 # a SERVER_JOB failure with no reason, which we then ignore at suite 104 # results reporting time. 105 raise Exception('') 106 else: 107 # If we finish successfully, nothing in autotest ever looks at the 108 # status.log, so it's purely for human consumption and tracability. 109 hostname = utils.get_hostname_from_machine(machine) 110 job.record('END GOOD', None, 'provision', 111 '%s provisioned successfully' % hostname) 112 113 114job.parallel_simple(provision_machine, machines) 115 116# vim: set syntax=python : 117