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 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 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.Provision.run_task_actions(job, host, labels_list) 53 host.verify() 54 55 # Let's update the labels on the host and track how long it takes. 56 # Don't fail while updating the labels, provision is flaky enough by 57 # itself. 58 label_update_success = True 59 start_time = datetime.datetime.now() 60 try: 61 host.labels.update_labels(host, keep_pool=True) 62 except Exception: 63 logging.exception('Exception while updating labels.') 64 label_update_success = False 65 66 end_time = datetime.datetime.now() 67 duration = (end_time - start_time).total_seconds() 68 69 fields = {'success': label_update_success, 70 'board': host.get_board()} 71 _LABEL_UPDATE_DURATION_METRIC.add(duration, fields=fields) 72 except Exception: 73 logging.exception('Provision failed due to Exception.') 74 job.record('END FAIL', None, 'provision') 75 # Raising a blank exception is done here because any message we can 76 # give here would be less useful than whatever the failing test left as 77 # its own exception message. 78 # 79 # The gory details of how raising a blank exception accomplishes this 80 # is as follows: 81 # 82 # The scheduler only looks at the return code of autoserv to see if 83 # the special task failed. Therefore we need python to exit because 84 # of an unhandled exception or because someone called sys.exit(1). 85 # 86 # We can't call sys.exit, since there's post-job-running logic (like 87 # cleanup) that we'd be skipping out on. So therefore, we need to 88 # raise an exception. However, if we raise an exception, this 89 # exception ends up triggering server_job to write an INFO line with 90 # job_abort_reason equal to str(e), which the tko parser then picks 91 # up as the reason field for the job when the status.log we generate is 92 # parsed as the job's results. 93 # 94 # So therefore, we raise a blank exception, which then generates an 95 # empty job_abort_reason which the tko parser ignores just inserts as 96 # a SERVER_JOB failure with no reason, which we then ignore at suite 97 # results reporting time. 98 raise Exception('') 99 else: 100 # If we finish successfully, nothing in autotest ever looks at the 101 # status.log, so it's purely for human consumption and tracability. 102 hostname = utils.get_hostname_from_machine(machine) 103 job.record('END GOOD', None, 'provision', 104 '%s provisioned successfully' % hostname) 105 106 107job.parallel_simple(provision_machine, machines) 108 109# vim: set syntax=python : 110