mbligh | 6203ace | 2007-10-04 21:54:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
mbligh | 1ffd5dc | 2008-11-25 13:24:05 +0000 | [diff] [blame] | 2 | # Copyright 2007-2008 Martin J. Bligh <mbligh@google.com>, Google Inc. |
mbligh | 82648e5 | 2008-11-20 16:54:25 +0000 | [diff] [blame] | 3 | # Released under the GPL v2 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 4 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 5 | """ |
Aviv Keshet | de6bb19 | 2013-01-30 16:17:22 -0800 | [diff] [blame] | 6 | Run a control file through the server side engine |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | """ |
mbligh | 1ffd5dc | 2008-11-25 13:24:05 +0000 | [diff] [blame] | 8 | |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 9 | import datetime |
Paul Hobbs | 20cc72a | 2016-08-30 16:57:05 -0700 | [diff] [blame] | 10 | import contextlib |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 11 | import getpass |
| 12 | import logging |
| 13 | import os |
| 14 | import re |
Prathmesh Prabhu | 4604736 | 2018-03-16 10:33:19 -0700 | [diff] [blame] | 15 | import shutil |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 16 | import signal |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 17 | import socket |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 18 | import sys |
| 19 | import traceback |
| 20 | import time |
| 21 | import urllib2 |
mbligh | 1ffd5dc | 2008-11-25 13:24:05 +0000 | [diff] [blame] | 22 | |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 23 | import common |
Dan Shi | 4f8c024 | 2017-07-07 15:34:49 -0700 | [diff] [blame] | 24 | from autotest_lib.client.bin.result_tools import utils as result_utils |
| 25 | from autotest_lib.client.bin.result_tools import view as result_view |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 26 | from autotest_lib.client.common_lib import control_data |
Dan Shi | 32649b8 | 2015-08-29 20:53:36 -0700 | [diff] [blame] | 27 | from autotest_lib.client.common_lib import error |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 28 | from autotest_lib.client.common_lib import global_config |
Allen Li | f146e87 | 2017-08-15 18:24:31 -0700 | [diff] [blame] | 29 | from autotest_lib.server import results_mocker |
Prathmesh Prabhu | 4604736 | 2018-03-16 10:33:19 -0700 | [diff] [blame] | 30 | from autotest_lib.server.cros.dynamic_suite import suite |
Prathmesh Prabhu | a5eecda | 2016-11-23 16:48:40 -0800 | [diff] [blame] | 31 | |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 32 | try: |
| 33 | from chromite.lib import metrics |
Paul Hobbs | e9fd557 | 2017-08-22 02:48:25 -0700 | [diff] [blame] | 34 | from chromite.lib import cloud_trace |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 35 | except ImportError: |
Prathmesh Prabhu | d16c801 | 2017-08-28 11:42:46 -0700 | [diff] [blame] | 36 | from autotest_lib.client.common_lib import utils as common_utils |
| 37 | metrics = common_utils.metrics_mock |
Paul Hobbs | e9fd557 | 2017-08-22 02:48:25 -0700 | [diff] [blame] | 38 | import mock |
| 39 | cloud_trace = mock.MagicMock() |
Prathmesh Prabhu | a5eecda | 2016-11-23 16:48:40 -0800 | [diff] [blame] | 40 | |
Dan Shi | a06f3e2 | 2015-09-03 16:15:15 -0700 | [diff] [blame] | 41 | _CONFIG = global_config.global_config |
| 42 | |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 43 | |
Jakob Juelich | e497b55 | 2014-09-23 19:11:59 -0700 | [diff] [blame] | 44 | # Number of seconds to wait before returning if testing mode is enabled |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 45 | TESTING_MODE_SLEEP_SECS = 1 |
Jakob Juelich | e497b55 | 2014-09-23 19:11:59 -0700 | [diff] [blame] | 46 | |
mbligh | 9ff89cd | 2009-09-03 20:28:17 +0000 | [diff] [blame] | 47 | |
Kevin Cheng | 9b6930f | 2016-07-20 14:57:15 -0700 | [diff] [blame] | 48 | from autotest_lib.server import frontend |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 49 | from autotest_lib.server import server_logging_config |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 50 | from autotest_lib.server import server_job, utils, autoserv_parser, autotest |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 51 | from autotest_lib.server import utils as server_utils |
Paul Hobbs | 20cc72a | 2016-08-30 16:57:05 -0700 | [diff] [blame] | 52 | from autotest_lib.server import site_utils |
Kevin Cheng | adc99f9 | 2016-07-20 08:21:58 -0700 | [diff] [blame] | 53 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 54 | from autotest_lib.site_utils import job_directories |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 55 | from autotest_lib.site_utils import job_overhead |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 56 | from autotest_lib.site_utils import lxc |
Ben Kwa | 966db08 | 2017-06-05 14:17:23 -0700 | [diff] [blame] | 57 | from autotest_lib.site_utils.lxc import utils as lxc_utils |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 58 | from autotest_lib.client.common_lib import pidfile, logging_manager |
mbligh | 92c0fc2 | 2008-11-20 16:52:23 +0000 | [diff] [blame] | 59 | |
Paul Hobbs | 20cc72a | 2016-08-30 16:57:05 -0700 | [diff] [blame] | 60 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 61 | # Control segment to stage server-side package. |
| 62 | STAGE_SERVER_SIDE_PACKAGE_CONTROL_FILE = server_job._control_segment_path( |
| 63 | 'stage_server_side_package') |
| 64 | |
Dan Shi | a06f3e2 | 2015-09-03 16:15:15 -0700 | [diff] [blame] | 65 | # Command line to start servod in a moblab. |
| 66 | START_SERVOD_CMD = 'sudo start servod BOARD=%s PORT=%s' |
| 67 | STOP_SERVOD_CMD = 'sudo stop servod' |
| 68 | |
Prathmesh Prabhu | 4604736 | 2018-03-16 10:33:19 -0700 | [diff] [blame] | 69 | _AUTOTEST_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), '..')) |
| 70 | _CONTROL_FILE_FROM_CONTROL_NAME = 'control.from_control_name' |
| 71 | |
Alex Miller | f1af17e | 2013-01-09 22:50:32 -0800 | [diff] [blame] | 72 | def log_alarm(signum, frame): |
| 73 | logging.error("Received SIGALARM. Ignoring and continuing on.") |
Alex Miller | 0528d6f | 2013-01-11 10:49:48 -0800 | [diff] [blame] | 74 | sys.exit(1) |
Alex Miller | f1af17e | 2013-01-09 22:50:32 -0800 | [diff] [blame] | 75 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 76 | |
| 77 | def _get_machines(parser): |
| 78 | """Get a list of machine names from command line arg -m or a file. |
| 79 | |
| 80 | @param parser: Parser for the command line arguments. |
| 81 | |
| 82 | @return: A list of machine names from command line arg -m or the |
| 83 | machines file specified in the command line arg -M. |
| 84 | """ |
| 85 | if parser.options.machines: |
| 86 | machines = parser.options.machines.replace(',', ' ').strip().split() |
| 87 | else: |
| 88 | machines = [] |
| 89 | machines_file = parser.options.machines_file |
| 90 | if machines_file: |
| 91 | machines = [] |
| 92 | for m in open(machines_file, 'r').readlines(): |
| 93 | # remove comments, spaces |
| 94 | m = re.sub('#.*', '', m).strip() |
| 95 | if m: |
| 96 | machines.append(m) |
| 97 | logging.debug('Read list of machines from file: %s', machines_file) |
| 98 | logging.debug('Machines: %s', ','.join(machines)) |
| 99 | |
| 100 | if machines: |
| 101 | for machine in machines: |
| 102 | if not machine or re.search('\s', machine): |
| 103 | parser.parser.error("Invalid machine: %s" % str(machine)) |
| 104 | machines = list(set(machines)) |
| 105 | machines.sort() |
| 106 | return machines |
| 107 | |
| 108 | |
Prathmesh Prabhu | 588007d | 2017-06-15 00:31:31 -0700 | [diff] [blame] | 109 | def _stage_ssp(parser, resultsdir): |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 110 | """Stage server-side package. |
| 111 | |
| 112 | This function calls a control segment to stage server-side package based on |
| 113 | the job and autoserv command line option. The detail implementation could |
| 114 | be different for each host type. Currently, only CrosHost has |
| 115 | stage_server_side_package function defined. |
| 116 | The script returns None if no server-side package is available. However, |
| 117 | it may raise exception if it failed for reasons other than artifact (the |
| 118 | server-side package) not found. |
| 119 | |
| 120 | @param parser: Command line arguments parser passed in the autoserv process. |
Prathmesh Prabhu | 588007d | 2017-06-15 00:31:31 -0700 | [diff] [blame] | 121 | @param resultsdir: Folder to store results. This could be different from |
| 122 | parser.options.results: parser.options.results can be set to None |
| 123 | for results to be stored in a temp folder. resultsdir can be None |
| 124 | for autoserv run requires no logging. |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 125 | |
Dan Shi | 14de762 | 2016-08-22 11:09:06 -0700 | [diff] [blame] | 126 | @return: (ssp_url, error_msg), where |
| 127 | ssp_url is a url to the autotest server-side package. None if |
| 128 | server-side package is not supported. |
| 129 | error_msg is a string indicating the failures. None if server- |
| 130 | side package is staged successfully. |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 131 | """ |
Kevin Cheng | adc99f9 | 2016-07-20 08:21:58 -0700 | [diff] [blame] | 132 | machines_list = _get_machines(parser) |
Prathmesh Prabhu | 7fc39c5 | 2018-03-21 14:08:30 -0700 | [diff] [blame] | 133 | machines_list = server_job.get_machine_dicts( |
| 134 | machine_names=machines_list, |
| 135 | store_dir=os.path.join(resultsdir, parser.options.host_info_subdir), |
| 136 | in_lab=parser.options.lab, |
| 137 | use_shadow_store=not parser.options.local_only_host_info, |
| 138 | host_attributes=parser.options.host_attributes, |
| 139 | ) |
Kevin Cheng | adc99f9 | 2016-07-20 08:21:58 -0700 | [diff] [blame] | 140 | |
Kevin Cheng | adc99f9 | 2016-07-20 08:21:58 -0700 | [diff] [blame] | 141 | namespace = {'machines': machines_list, |
Richard Barnette | 71854c7 | 2018-03-30 14:22:09 -0700 | [diff] [blame] | 142 | 'image': parser.options.test_source_build} |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 143 | script_locals = {} |
| 144 | execfile(STAGE_SERVER_SIDE_PACKAGE_CONTROL_FILE, namespace, script_locals) |
Dan Shi | 14de762 | 2016-08-22 11:09:06 -0700 | [diff] [blame] | 145 | return script_locals['ssp_url'], script_locals['error_msg'] |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 146 | |
| 147 | |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 148 | def _run_with_ssp(job, container_id, job_id, results, parser, ssp_url, |
Dan Shi | 3be35af | 2016-08-25 23:22:40 -0700 | [diff] [blame] | 149 | job_folder, machines): |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 150 | """Run the server job with server-side packaging. |
| 151 | |
Dan Shi | 37befda | 2015-12-07 13:16:56 -0800 | [diff] [blame] | 152 | @param job: The server job object. |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 153 | @param container_id: ID of the container to run the test. |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 154 | @param job_id: ID of the test job. |
| 155 | @param results: Folder to store results. This could be different from |
| 156 | parser.options.results: |
| 157 | parser.options.results can be set to None for results to be |
| 158 | stored in a temp folder. |
| 159 | results can be None for autoserv run requires no logging. |
| 160 | @param parser: Command line parser that contains the options. |
| 161 | @param ssp_url: url of the staged server-side package. |
Dan Shi | afa6387 | 2016-02-23 15:32:31 -0800 | [diff] [blame] | 162 | @param job_folder: Name of the job result folder. |
Dan Shi | 3be35af | 2016-08-25 23:22:40 -0700 | [diff] [blame] | 163 | @param machines: A list of machines to run the test. |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 164 | """ |
| 165 | bucket = lxc.ContainerBucket() |
| 166 | control = (parser.args[0] if len(parser.args) > 0 and parser.args[0] != '' |
| 167 | else None) |
Dan Shi | 37befda | 2015-12-07 13:16:56 -0800 | [diff] [blame] | 168 | try: |
Dan Shi | 3be35af | 2016-08-25 23:22:40 -0700 | [diff] [blame] | 169 | dut_name = machines[0] if len(machines) >= 1 else None |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 170 | test_container = bucket.setup_test(container_id, job_id, ssp_url, |
Dan Shi | afa6387 | 2016-02-23 15:32:31 -0800 | [diff] [blame] | 171 | results, control=control, |
Dan Shi | 3be35af | 2016-08-25 23:22:40 -0700 | [diff] [blame] | 172 | job_folder=job_folder, |
| 173 | dut_name=dut_name) |
Dan Shi | 37befda | 2015-12-07 13:16:56 -0800 | [diff] [blame] | 174 | except Exception as e: |
| 175 | job.record('FAIL', None, None, |
| 176 | 'Failed to setup container for test: %s. Check logs in ' |
| 177 | 'ssp_logs folder for more details.' % e) |
| 178 | raise |
| 179 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 180 | args = sys.argv[:] |
| 181 | args.remove('--require-ssp') |
Dan Shi | 77b79a6 | 2015-07-29 16:22:05 -0700 | [diff] [blame] | 182 | # --parent_job_id is only useful in autoserv running in host, not in |
| 183 | # container. Include this argument will cause test to fail for builds before |
| 184 | # CL 286265 was merged. |
| 185 | if '--parent_job_id' in args: |
| 186 | index = args.index('--parent_job_id') |
| 187 | args.remove('--parent_job_id') |
| 188 | # Remove the actual parent job id in command line arg. |
| 189 | del args[index] |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 190 | |
| 191 | # A dictionary of paths to replace in the command line. Key is the path to |
| 192 | # be replaced with the one in value. |
| 193 | paths_to_replace = {} |
| 194 | # Replace the control file path with the one in container. |
| 195 | if control: |
| 196 | container_control_filename = os.path.join( |
| 197 | lxc.CONTROL_TEMP_PATH, os.path.basename(control)) |
| 198 | paths_to_replace[control] = container_control_filename |
| 199 | # Update result directory with the one in container. |
Dan Shi | 65374e2 | 2016-09-15 16:14:05 -0700 | [diff] [blame] | 200 | container_result_dir = os.path.join(lxc.RESULT_DIR_FMT % job_folder) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 201 | if parser.options.results: |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 202 | paths_to_replace[parser.options.results] = container_result_dir |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 203 | args = [paths_to_replace.get(arg, arg) for arg in args] |
| 204 | |
| 205 | # Apply --use-existing-results, results directory is aready created and |
| 206 | # mounted in container. Apply this arg to avoid exception being raised. |
| 207 | if not '--use-existing-results' in args: |
| 208 | args.append('--use-existing-results') |
| 209 | |
| 210 | # Make sure autoserv running in container using a different pid file. |
| 211 | if not '--pidfile-label' in args: |
| 212 | args.extend(['--pidfile-label', 'container_autoserv']) |
| 213 | |
Dan Shi | d1f5123 | 2015-04-18 00:29:14 -0700 | [diff] [blame] | 214 | cmd_line = ' '.join(["'%s'" % arg if ' ' in arg else arg for arg in args]) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 215 | logging.info('Run command in container: %s', cmd_line) |
Dan Shi | 37bee22 | 2015-04-13 15:46:47 -0700 | [diff] [blame] | 216 | success = False |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 217 | try: |
| 218 | test_container.attach_run(cmd_line) |
Dan Shi | 37bee22 | 2015-04-13 15:46:47 -0700 | [diff] [blame] | 219 | success = True |
Dan Shi | 9d3454e | 2015-12-08 09:16:08 -0800 | [diff] [blame] | 220 | except Exception as e: |
| 221 | # If the test run inside container fails without generating any log, |
| 222 | # write a message to status.log to help troubleshooting. |
| 223 | debug_files = os.listdir(os.path.join(results, 'debug')) |
| 224 | if not debug_files: |
| 225 | job.record('FAIL', None, None, |
| 226 | 'Failed to run test inside the container: %s. Check ' |
| 227 | 'logs in ssp_logs folder for more details.' % e) |
| 228 | raise |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 229 | finally: |
Prathmesh Prabhu | a5eecda | 2016-11-23 16:48:40 -0800 | [diff] [blame] | 230 | metrics.Counter( |
| 231 | 'chromeos/autotest/experimental/execute_job_in_ssp').increment( |
| 232 | fields={'success': success}) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 233 | test_container.destroy() |
| 234 | |
| 235 | |
Dan Shi | 3f1b8a5 | 2015-04-21 11:11:06 -0700 | [diff] [blame] | 236 | def correct_results_folder_permission(results): |
| 237 | """Make sure the results folder has the right permission settings. |
| 238 | |
| 239 | For tests running with server-side packaging, the results folder has the |
| 240 | owner of root. This must be changed to the user running the autoserv |
| 241 | process, so parsing job can access the results folder. |
| 242 | TODO(dshi): crbug.com/459344 Remove this function when test container can be |
| 243 | unprivileged container. |
| 244 | |
| 245 | @param results: Path to the results folder. |
| 246 | |
| 247 | """ |
| 248 | if not results: |
| 249 | return |
| 250 | |
Aviv Keshet | c03de79 | 2017-07-18 14:24:31 -0700 | [diff] [blame] | 251 | utils.run('sudo -n chown -R %s "%s"' % (os.getuid(), results)) |
| 252 | utils.run('sudo -n chgrp -R %s "%s"' % (os.getgid(), results)) |
Dan Shi | 3f1b8a5 | 2015-04-21 11:11:06 -0700 | [diff] [blame] | 253 | |
| 254 | |
Dan Shi | a06f3e2 | 2015-09-03 16:15:15 -0700 | [diff] [blame] | 255 | def _start_servod(machine): |
| 256 | """Try to start servod in moblab if it's not already running or running with |
| 257 | different board or port. |
| 258 | |
| 259 | @param machine: Name of the dut used for test. |
| 260 | """ |
| 261 | if not utils.is_moblab(): |
| 262 | return |
| 263 | |
Dan Shi | 1cded88 | 2015-09-23 16:52:26 -0700 | [diff] [blame] | 264 | logging.debug('Trying to start servod.') |
Dan Shi | a06f3e2 | 2015-09-03 16:15:15 -0700 | [diff] [blame] | 265 | try: |
Kevin Cheng | 9b6930f | 2016-07-20 14:57:15 -0700 | [diff] [blame] | 266 | afe = frontend.AFE() |
Dan Shi | a06f3e2 | 2015-09-03 16:15:15 -0700 | [diff] [blame] | 267 | board = server_utils.get_board_from_afe(machine, afe) |
| 268 | hosts = afe.get_hosts(hostname=machine) |
| 269 | servo_host = hosts[0].attributes.get('servo_host', None) |
| 270 | servo_port = hosts[0].attributes.get('servo_port', 9999) |
| 271 | if not servo_host in ['localhost', '127.0.0.1']: |
Dan Shi | 1cded88 | 2015-09-23 16:52:26 -0700 | [diff] [blame] | 272 | logging.warn('Starting servod is aborted. The dut\'s servo_host ' |
| 273 | 'attribute is not set to localhost.') |
Dan Shi | a06f3e2 | 2015-09-03 16:15:15 -0700 | [diff] [blame] | 274 | return |
| 275 | except (urllib2.HTTPError, urllib2.URLError): |
| 276 | # Ignore error if RPC failed to get board |
| 277 | logging.error('Failed to get board name from AFE. Start servod is ' |
| 278 | 'aborted') |
| 279 | return |
| 280 | |
| 281 | try: |
| 282 | pid = utils.run('pgrep servod').stdout |
| 283 | cmd_line = utils.run('ps -fp %s' % pid).stdout |
| 284 | if ('--board %s' % board in cmd_line and |
| 285 | '--port %s' % servo_port in cmd_line): |
| 286 | logging.debug('Servod is already running with given board and port.' |
| 287 | ' There is no need to restart servod.') |
| 288 | return |
| 289 | logging.debug('Servod is running with different board or port. ' |
| 290 | 'Stopping existing servod.') |
| 291 | utils.run('sudo stop servod') |
| 292 | except error.CmdError: |
| 293 | # servod is not running. |
| 294 | pass |
| 295 | |
| 296 | try: |
| 297 | utils.run(START_SERVOD_CMD % (board, servo_port)) |
| 298 | logging.debug('Servod is started') |
| 299 | except error.CmdError as e: |
| 300 | logging.error('Servod failed to be started, error: %s', e) |
| 301 | |
| 302 | |
Prathmesh Prabhu | 4604736 | 2018-03-16 10:33:19 -0700 | [diff] [blame] | 303 | def _control_path_on_disk(control_name): |
| 304 | """Find the control file corresponding to the given control name, on disk. |
| 305 | |
| 306 | @param control_name: NAME attribute of the control file to fetch. |
| 307 | @return: Path to the control file. |
| 308 | """ |
| 309 | cf_getter = suite.create_fs_getter(_AUTOTEST_ROOT) |
| 310 | control_name_predicate = suite.test_name_matches_pattern_predicate( |
| 311 | '^%s$' % control_name) |
| 312 | tests = suite.find_and_parse_tests(cf_getter, control_name_predicate) |
| 313 | if not tests: |
| 314 | raise error.AutoservError( |
| 315 | 'Failed to find any control files with NAME %s' % control_name) |
| 316 | if len(tests) > 1: |
| 317 | logging.error('Found more than one control file with NAME %s: %s', |
| 318 | control_name, [t.path for t in tests]) |
| 319 | raise error.AutoservError( |
| 320 | 'Found more than one control file with NAME %s' % control_name) |
| 321 | return tests[0].path |
| 322 | |
| 323 | |
| 324 | def _stage_control_file(parser, results_dir): |
| 325 | """Stage the control file to execute, returning the path to staged file. |
| 326 | |
| 327 | @param parser: Parser for autoserv options. |
| 328 | @param results_dir: Results directory to stage the control file into. |
| 329 | @return: Absolute path to the staged control file. |
| 330 | """ |
| 331 | # TODO(pprabhu) This function currently always stages the control file from |
| 332 | # the local filesystem. This means that both |
| 333 | # parser.options.test_source_build and parser.options.image are ignored. |
| 334 | # Support will be added once skylab gains support to run SSP tests. |
| 335 | control_path = _control_path_on_disk(parser.options.control_name) |
| 336 | new_control = os.path.join(results_dir, _CONTROL_FILE_FROM_CONTROL_NAME) |
| 337 | shutil.copy2(control_path, new_control) |
| 338 | return new_control |
| 339 | |
| 340 | |
| 341 | def _tweak_arguments_for_control_file(parser, control): |
| 342 | """Tweak parser arguments to pass in control. |
| 343 | |
| 344 | autoserv running within an SSP container may not support the --test-name |
| 345 | argument. We also do not want to duplicate the effort and logic to obtain |
| 346 | the right control file outside and inside the SSP container. Instead, we |
| 347 | tweak the parser commandline in order to pass in the given control file. |
| 348 | """ |
| 349 | # control_name overrides the control argument, so unset it to force the |
| 350 | # autoserv re-execution to use the control file set here. |
| 351 | parser.control_name = None |
| 352 | if parser.args: |
| 353 | parser.args[0] = control |
| 354 | else: |
| 355 | parser.args.append(control) |
| 356 | |
| 357 | |
Dan Shi | c68fefb | 2015-04-07 10:10:52 -0700 | [diff] [blame] | 358 | def run_autoserv(pid_file_manager, results, parser, ssp_url, use_ssp): |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 359 | """Run server job with given options. |
| 360 | |
| 361 | @param pid_file_manager: PidFileManager used to monitor the autoserv process |
| 362 | @param results: Folder to store results. |
| 363 | @param parser: Parser for the command line arguments. |
| 364 | @param ssp_url: Url to server-side package. |
Dan Shi | c68fefb | 2015-04-07 10:10:52 -0700 | [diff] [blame] | 365 | @param use_ssp: Set to True to run with server-side packaging. |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 366 | """ |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 367 | if parser.options.warn_no_ssp: |
Dan Shi | c68fefb | 2015-04-07 10:10:52 -0700 | [diff] [blame] | 368 | # Post a warning in the log. |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 369 | logging.warn('Autoserv is required to run with server-side packaging. ' |
| 370 | 'However, no drone is found to support server-side ' |
| 371 | 'packaging. The test will be executed in a drone without ' |
| 372 | 'server-side packaging supported.') |
| 373 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 374 | # send stdin to /dev/null |
| 375 | dev_null = os.open(os.devnull, os.O_RDONLY) |
| 376 | os.dup2(dev_null, sys.stdin.fileno()) |
| 377 | os.close(dev_null) |
mbligh | dbf3761 | 2007-11-24 19:38:11 +0000 | [diff] [blame] | 378 | |
Dan Shi | e8aeb66 | 2016-06-30 11:22:03 -0700 | [diff] [blame] | 379 | # Create separate process group if the process is not a process group |
| 380 | # leader. This allows autoserv process to keep running after the caller |
| 381 | # process (drone manager call) exits. |
| 382 | if os.getpid() != os.getpgid(0): |
| 383 | os.setsid() |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 384 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 385 | # Container name is predefined so the container can be destroyed in |
| 386 | # handle_sigterm. |
| 387 | job_or_task_id = job_directories.get_job_id_or_task_id( |
| 388 | parser.options.results) |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 389 | container_id = lxc.ContainerId(job_or_task_id, time.time(), os.getpid()) |
Dan Shi | afa6387 | 2016-02-23 15:32:31 -0800 | [diff] [blame] | 390 | job_folder = job_directories.get_job_folder_name(parser.options.results) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 391 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 392 | # Implement SIGTERM handler |
mbligh | c229956 | 2009-07-02 19:00:36 +0000 | [diff] [blame] | 393 | def handle_sigterm(signum, frame): |
Simran Basi | 9d9b729 | 2013-10-16 16:44:07 -0700 | [diff] [blame] | 394 | logging.debug('Received SIGTERM') |
mbligh | ff7d61f | 2008-12-22 14:53:35 +0000 | [diff] [blame] | 395 | if pid_file_manager: |
| 396 | pid_file_manager.close_file(1, signal.SIGTERM) |
Simran Basi | 49e21e6 | 2013-10-17 12:40:33 -0700 | [diff] [blame] | 397 | logging.debug('Finished writing to pid_file. Killing process.') |
Dan Shi | 3f1b8a5 | 2015-04-21 11:11:06 -0700 | [diff] [blame] | 398 | |
| 399 | # Update results folder's file permission. This needs to be done ASAP |
| 400 | # before the parsing process tries to access the log. |
| 401 | if use_ssp and results: |
| 402 | correct_results_folder_permission(results) |
| 403 | |
Simran Basi | d6b8377 | 2014-01-06 16:31:30 -0800 | [diff] [blame] | 404 | # TODO (sbasi) - remove the time.sleep when crbug.com/302815 is solved. |
| 405 | # This sleep allows the pending output to be logged before the kill |
| 406 | # signal is sent. |
| 407 | time.sleep(.1) |
Dan Shi | c68fefb | 2015-04-07 10:10:52 -0700 | [diff] [blame] | 408 | if use_ssp: |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 409 | logging.debug('Destroy container %s before aborting the autoserv ' |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 410 | 'process.', container_id) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 411 | try: |
| 412 | bucket = lxc.ContainerBucket() |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 413 | container = bucket.get_container(container_id) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 414 | if container: |
| 415 | container.destroy() |
| 416 | else: |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 417 | logging.debug('Container %s is not found.', container_id) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 418 | except: |
| 419 | # Handle any exception so the autoserv process can be aborted. |
Dan Shi | 65374e2 | 2016-09-15 16:14:05 -0700 | [diff] [blame] | 420 | logging.exception('Failed to destroy container %s.', |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 421 | container_id) |
Dan Shi | e4a4f9f | 2015-07-20 09:00:25 -0700 | [diff] [blame] | 422 | # Try to correct the result file permission again after the |
| 423 | # container is destroyed, as the container might have created some |
| 424 | # new files in the result folder. |
| 425 | if results: |
| 426 | correct_results_folder_permission(results) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 427 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 428 | os.killpg(os.getpgrp(), signal.SIGKILL) |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 429 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 430 | # Set signal handler |
mbligh | c229956 | 2009-07-02 19:00:36 +0000 | [diff] [blame] | 431 | signal.signal(signal.SIGTERM, handle_sigterm) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 432 | |
Simran Basi | d6b8377 | 2014-01-06 16:31:30 -0800 | [diff] [blame] | 433 | # faulthandler is only needed to debug in the Lab and is not avaliable to |
| 434 | # be imported in the chroot as part of VMTest, so Try-Except it. |
| 435 | try: |
| 436 | import faulthandler |
| 437 | faulthandler.register(signal.SIGTERM, all_threads=True, chain=True) |
| 438 | logging.debug('faulthandler registered on SIGTERM.') |
| 439 | except ImportError: |
Christopher Grant | 4beca02 | 2015-06-16 15:14:47 -0400 | [diff] [blame] | 440 | sys.exc_clear() |
Simran Basi | d6b8377 | 2014-01-06 16:31:30 -0800 | [diff] [blame] | 441 | |
David Rochberg | 8a60d1e | 2011-02-01 14:22:07 -0500 | [diff] [blame] | 442 | # Ignore SIGTTOU's generated by output from forked children. |
| 443 | signal.signal(signal.SIGTTOU, signal.SIG_IGN) |
| 444 | |
Alex Miller | f1af17e | 2013-01-09 22:50:32 -0800 | [diff] [blame] | 445 | # If we received a SIGALARM, let's be loud about it. |
| 446 | signal.signal(signal.SIGALRM, log_alarm) |
| 447 | |
mbligh | a5f5e54 | 2009-12-30 16:57:49 +0000 | [diff] [blame] | 448 | # Server side tests that call shell scripts often depend on $USER being set |
| 449 | # but depending on how you launch your autotest scheduler it may not be set. |
| 450 | os.environ['USER'] = getpass.getuser() |
| 451 | |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 452 | label = parser.options.label |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 453 | group_name = parser.options.group_name |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 454 | user = parser.options.user |
| 455 | client = parser.options.client |
| 456 | server = parser.options.server |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 457 | verify = parser.options.verify |
| 458 | repair = parser.options.repair |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 459 | cleanup = parser.options.cleanup |
Alex Miller | cb79ba7 | 2013-05-29 14:43:00 -0700 | [diff] [blame] | 460 | provision = parser.options.provision |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 461 | reset = parser.options.reset |
Alex Miller | 667b5f2 | 2014-02-28 15:33:39 -0800 | [diff] [blame] | 462 | job_labels = parser.options.job_labels |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 463 | no_tee = parser.options.no_tee |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 464 | execution_tag = parser.options.execution_tag |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 465 | ssh_user = parser.options.ssh_user |
| 466 | ssh_port = parser.options.ssh_port |
| 467 | ssh_pass = parser.options.ssh_pass |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 468 | collect_crashinfo = parser.options.collect_crashinfo |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 469 | control_filename = parser.options.control_filename |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 470 | test_retry = parser.options.test_retry |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 471 | verify_job_repo_url = parser.options.verify_job_repo_url |
Christopher Wiley | f594c5e | 2013-07-03 18:25:30 -0700 | [diff] [blame] | 472 | skip_crash_collection = parser.options.skip_crash_collection |
Aviv Keshet | 18ee314 | 2013-08-12 15:01:51 -0700 | [diff] [blame] | 473 | ssh_verbosity = int(parser.options.ssh_verbosity) |
Fang Deng | 6cc20de | 2013-09-06 15:47:32 -0700 | [diff] [blame] | 474 | ssh_options = parser.options.ssh_options |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 475 | no_use_packaging = parser.options.no_use_packaging |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 476 | in_lab = bool(parser.options.lab) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 477 | |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 478 | # can't be both a client and a server side test |
| 479 | if client and server: |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 480 | parser.parser.error("Can not specify a test as both server and client!") |
mbligh | b2bea30 | 2008-07-24 20:25:57 +0000 | [diff] [blame] | 481 | |
Alex Miller | cb79ba7 | 2013-05-29 14:43:00 -0700 | [diff] [blame] | 482 | if provision and client: |
| 483 | parser.parser.error("Cannot specify provisioning and client!") |
| 484 | |
| 485 | is_special_task = (verify or repair or cleanup or collect_crashinfo or |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 486 | provision or reset) |
Prathmesh Prabhu | 4604736 | 2018-03-16 10:33:19 -0700 | [diff] [blame] | 487 | if parser.options.control_name: |
| 488 | control = _stage_control_file(parser, results) |
| 489 | _tweak_arguments_for_control_file(parser, control) |
| 490 | elif parser.args: |
| 491 | control = parser.args[0] |
| 492 | else: |
| 493 | # Special tasks do not have any control file at all. |
| 494 | control = None |
| 495 | |
| 496 | if not any([is_special_task, control]): |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 497 | parser.parser.error("Missing argument: control file") |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 498 | |
Aviv Keshet | 18ee314 | 2013-08-12 15:01:51 -0700 | [diff] [blame] | 499 | if ssh_verbosity > 0: |
| 500 | # ssh_verbosity is an integer between 0 and 3, inclusive |
| 501 | ssh_verbosity_flag = '-' + 'v' * ssh_verbosity |
Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 502 | else: |
| 503 | ssh_verbosity_flag = '' |
Aviv Keshet | 18ee314 | 2013-08-12 15:01:51 -0700 | [diff] [blame] | 504 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 505 | machines = _get_machines(parser) |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 506 | if group_name and len(machines) < 2: |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 507 | parser.parser.error('-G %r may only be supplied with more than one ' |
| 508 | 'machine.' % group_name) |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 509 | |
Prathmesh Prabhu | 7fc39c5 | 2018-03-21 14:08:30 -0700 | [diff] [blame] | 510 | job_kwargs = { |
| 511 | 'control': control, |
| 512 | 'args': parser.args[1:], |
| 513 | 'resultdir': results, |
| 514 | 'label': label, |
| 515 | 'user': user, |
| 516 | 'machines': machines, |
| 517 | 'machine_dict_list': server_job.get_machine_dicts( |
| 518 | machine_names=machines, |
| 519 | store_dir=os.path.join(results, |
| 520 | parser.options.host_info_subdir), |
| 521 | in_lab=in_lab, |
| 522 | use_shadow_store=not parser.options.local_only_host_info, |
| 523 | host_attributes=parser.options.host_attributes, |
| 524 | ), |
| 525 | 'client': client, |
Prathmesh Prabhu | 7fc39c5 | 2018-03-21 14:08:30 -0700 | [diff] [blame] | 526 | 'ssh_user': ssh_user, |
| 527 | 'ssh_port': ssh_port, |
| 528 | 'ssh_pass': ssh_pass, |
| 529 | 'ssh_verbosity_flag': ssh_verbosity_flag, |
| 530 | 'ssh_options': ssh_options, |
| 531 | 'test_retry': test_retry, |
| 532 | 'group_name': group_name, |
| 533 | 'tag': execution_tag, |
| 534 | 'disable_sysinfo': parser.options.disable_sysinfo, |
| 535 | 'in_lab': in_lab, |
| 536 | } |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 537 | if parser.options.parent_job_id: |
Prathmesh Prabhu | 7fc39c5 | 2018-03-21 14:08:30 -0700 | [diff] [blame] | 538 | job_kwargs['parent_job_id'] = int(parser.options.parent_job_id) |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 539 | if control_filename: |
Prathmesh Prabhu | 7fc39c5 | 2018-03-21 14:08:30 -0700 | [diff] [blame] | 540 | job_kwargs['control_filename'] = control_filename |
| 541 | job = server_job.server_job(**job_kwargs) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 542 | |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 543 | job.logging.start_logging() |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 544 | |
mbligh | 161fe6f | 2008-06-19 16:26:04 +0000 | [diff] [blame] | 545 | # perform checks |
| 546 | job.precheck() |
| 547 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 548 | # run the job |
| 549 | exit_code = 0 |
Dan Shi | c1b8bdd | 2015-09-14 23:11:24 -0700 | [diff] [blame] | 550 | auto_start_servod = _CONFIG.get_config_value( |
| 551 | 'AUTOSERV', 'auto_start_servod', type=bool, default=False) |
Paul Hobbs | 20cc72a | 2016-08-30 16:57:05 -0700 | [diff] [blame] | 552 | |
Prathmesh Prabhu | 27bba96 | 2017-01-24 15:13:07 -0800 | [diff] [blame] | 553 | site_utils.SetupTsMonGlobalState('autoserv', indirect=False, |
Paul Hobbs | 66251f6 | 2017-08-22 02:31:07 -0700 | [diff] [blame] | 554 | auto_flush=False, short_lived=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 555 | try: |
Prathmesh Prabhu | 27bba96 | 2017-01-24 15:13:07 -0800 | [diff] [blame] | 556 | try: |
| 557 | if repair: |
| 558 | if auto_start_servod and len(machines) == 1: |
| 559 | _start_servod(machines[0]) |
| 560 | job.repair(job_labels) |
| 561 | elif verify: |
| 562 | job.verify(job_labels) |
| 563 | elif provision: |
| 564 | job.provision(job_labels) |
| 565 | elif reset: |
| 566 | job.reset(job_labels) |
| 567 | elif cleanup: |
| 568 | job.cleanup(job_labels) |
| 569 | else: |
| 570 | if auto_start_servod and len(machines) == 1: |
| 571 | _start_servod(machines[0]) |
| 572 | if use_ssp: |
| 573 | try: |
Ben Kwa | bedacad | 2017-08-28 12:20:38 -0700 | [diff] [blame] | 574 | _run_with_ssp(job, container_id, job_or_task_id, |
Prathmesh Prabhu | 27bba96 | 2017-01-24 15:13:07 -0800 | [diff] [blame] | 575 | results, parser, ssp_url, job_folder, |
| 576 | machines) |
| 577 | finally: |
| 578 | # Update the ownership of files in result folder. |
| 579 | correct_results_folder_permission(results) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 580 | else: |
Prathmesh Prabhu | 27bba96 | 2017-01-24 15:13:07 -0800 | [diff] [blame] | 581 | if collect_crashinfo: |
| 582 | # Update the ownership of files in result folder. If the |
| 583 | # job to collect crashinfo was running inside container |
| 584 | # (SSP) and crashed before correcting folder permission, |
| 585 | # the result folder might have wrong permission setting. |
Dan Shi | afa6387 | 2016-02-23 15:32:31 -0800 | [diff] [blame] | 586 | try: |
| 587 | correct_results_folder_permission(results) |
Prathmesh Prabhu | 27bba96 | 2017-01-24 15:13:07 -0800 | [diff] [blame] | 588 | except: |
| 589 | # Ignore any error as the user may not have root |
| 590 | # permission to run sudo command. |
| 591 | pass |
Aviv Keshet | 92bf7b6 | 2017-02-13 15:34:03 -0800 | [diff] [blame] | 592 | metric_name = ('chromeos/autotest/experimental/' |
| 593 | 'autoserv_job_run_duration') |
| 594 | f = {'in_container': utils.is_in_container(), |
| 595 | 'success': False} |
| 596 | with metrics.SecondsTimer(metric_name, fields=f) as c: |
Richard Barnette | 71854c7 | 2018-03-30 14:22:09 -0700 | [diff] [blame] | 597 | job.run(verify_job_repo_url=verify_job_repo_url, |
Aviv Keshet | 92bf7b6 | 2017-02-13 15:34:03 -0800 | [diff] [blame] | 598 | only_collect_crashinfo=collect_crashinfo, |
| 599 | skip_crash_collection=skip_crash_collection, |
| 600 | job_labels=job_labels, |
| 601 | use_packaging=(not no_use_packaging)) |
| 602 | c['success'] = True |
| 603 | |
Prathmesh Prabhu | 27bba96 | 2017-01-24 15:13:07 -0800 | [diff] [blame] | 604 | finally: |
Hidehiko Abe | 0689330 | 2017-06-24 07:32:38 +0900 | [diff] [blame] | 605 | job.close() |
Dan Shi | ffd5b82 | 2017-07-14 11:16:23 -0700 | [diff] [blame] | 606 | # Special task doesn't run parse, so result summary needs to be |
| 607 | # built here. |
| 608 | if results and (repair or verify or reset or cleanup or provision): |
Dan Shi | 4f8c024 | 2017-07-07 15:34:49 -0700 | [diff] [blame] | 609 | # Throttle the result on the server side. |
| 610 | try: |
| 611 | result_utils.execute( |
| 612 | results, control_data.DEFAULT_MAX_RESULT_SIZE_KB) |
| 613 | except: |
| 614 | logging.exception( |
| 615 | 'Non-critical failure: Failed to throttle results ' |
| 616 | 'in directory %s.', results) |
| 617 | # Build result view and report metrics for result sizes. |
Dan Shi | ffd5b82 | 2017-07-14 11:16:23 -0700 | [diff] [blame] | 618 | site_utils.collect_result_sizes(results) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 619 | except: |
jadmanski | 27b37ea | 2008-10-29 23:54:31 +0000 | [diff] [blame] | 620 | exit_code = 1 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 621 | traceback.print_exc() |
Prathmesh Prabhu | 27bba96 | 2017-01-24 15:13:07 -0800 | [diff] [blame] | 622 | finally: |
| 623 | metrics.Flush() |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 624 | |
jadmanski | 27b37ea | 2008-10-29 23:54:31 +0000 | [diff] [blame] | 625 | sys.exit(exit_code) |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 626 | |
| 627 | |
Paul Hobbs | 68d9859 | 2017-08-22 02:22:49 -0700 | [diff] [blame] | 628 | def record_autoserv(options, start_time): |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 629 | """Record autoserv end-to-end time in metadata db. |
| 630 | |
| 631 | @param options: parser options. |
Paul Hobbs | 68d9859 | 2017-08-22 02:22:49 -0700 | [diff] [blame] | 632 | @param start_time: When autoserv started |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 633 | """ |
| 634 | # Get machine hostname |
| 635 | machines = options.machines.replace( |
| 636 | ',', ' ').strip().split() if options.machines else [] |
| 637 | num_machines = len(machines) |
| 638 | if num_machines > 1: |
| 639 | # Skip the case where atomic group is used. |
| 640 | return |
| 641 | elif num_machines == 0: |
| 642 | machines.append('hostless') |
| 643 | |
| 644 | # Determine the status that will be reported. |
Paul Hobbs | 68d9859 | 2017-08-22 02:22:49 -0700 | [diff] [blame] | 645 | status = get_job_status(options) |
| 646 | is_special_task = status not in [ |
Paul Hobbs | e9fd557 | 2017-08-22 02:48:25 -0700 | [diff] [blame] | 647 | job_overhead.STATUS.RUNNING, job_overhead.STATUS.GATHERING] |
Paul Hobbs | 68d9859 | 2017-08-22 02:22:49 -0700 | [diff] [blame] | 648 | job_or_task_id = job_directories.get_job_id_or_task_id(options.results) |
| 649 | duration_secs = (datetime.datetime.now() - start_time).total_seconds() |
| 650 | job_overhead.record_state_duration( |
| 651 | job_or_task_id, machines[0], status, duration_secs, |
| 652 | is_special_task=is_special_task) |
| 653 | |
| 654 | |
| 655 | def get_job_status(options): |
| 656 | """Returns the HQE Status for this run. |
| 657 | |
| 658 | @param options: parser options. |
| 659 | """ |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 660 | s = job_overhead.STATUS |
| 661 | task_mapping = { |
| 662 | 'reset': s.RESETTING, 'verify': s.VERIFYING, |
| 663 | 'provision': s.PROVISIONING, 'repair': s.REPAIRING, |
| 664 | 'cleanup': s.CLEANING, 'collect_crashinfo': s.GATHERING} |
Paul Hobbs | 68d9859 | 2017-08-22 02:22:49 -0700 | [diff] [blame] | 665 | match = [task for task in task_mapping if getattr(options, task, False)] |
| 666 | return task_mapping[match[0]] if match else s.RUNNING |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 667 | |
| 668 | |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 669 | def main(): |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 670 | start_time = datetime.datetime.now() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 671 | # grab the parser |
| 672 | parser = autoserv_parser.autoserv_parser |
mbligh | a5cb406 | 2009-02-17 15:53:39 +0000 | [diff] [blame] | 673 | parser.parse_args() |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 674 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 675 | if len(sys.argv) == 1: |
| 676 | parser.parser.print_help() |
| 677 | sys.exit(1) |
mbligh | a6f1308 | 2008-06-05 23:53:46 +0000 | [diff] [blame] | 678 | |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 679 | if parser.options.no_logging: |
| 680 | results = None |
| 681 | else: |
| 682 | results = parser.options.results |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 683 | if not results: |
| 684 | results = 'results.' + time.strftime('%Y-%m-%d-%H.%M.%S') |
Dan Shi | 14de762 | 2016-08-22 11:09:06 -0700 | [diff] [blame] | 685 | results = os.path.abspath(results) |
showard | 566d3c0 | 2010-01-12 18:57:01 +0000 | [diff] [blame] | 686 | resultdir_exists = False |
| 687 | for filename in ('control.srv', 'status.log', '.autoserv_execute'): |
| 688 | if os.path.exists(os.path.join(results, filename)): |
| 689 | resultdir_exists = True |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 690 | if not parser.options.use_existing_results and resultdir_exists: |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 691 | error = "Error: results directory already exists: %s\n" % results |
| 692 | sys.stderr.write(error) |
| 693 | sys.exit(1) |
mbligh | a788dc4 | 2009-03-26 21:10:16 +0000 | [diff] [blame] | 694 | |
| 695 | # Now that we certified that there's no leftover results dir from |
| 696 | # previous jobs, lets create the result dir since the logging system |
| 697 | # needs to create the log file in there. |
| 698 | if not os.path.isdir(results): |
| 699 | os.makedirs(results) |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 700 | |
Prathmesh Prabhu | 7ae68ae | 2017-06-15 00:29:31 -0700 | [diff] [blame] | 701 | # If the job requires to run with server-side package, try to stage server- |
| 702 | # side package first. If that fails with error that autotest server package |
| 703 | # does not exist, fall back to run the job without using server-side |
| 704 | # packaging. If option warn_no_ssp is specified, that means autoserv is |
| 705 | # running in a drone does not support SSP, thus no need to stage server-side |
| 706 | # package. |
| 707 | ssp_url = None |
| 708 | ssp_url_warning = False |
| 709 | if (not parser.options.warn_no_ssp and parser.options.require_ssp): |
Prathmesh Prabhu | 588007d | 2017-06-15 00:31:31 -0700 | [diff] [blame] | 710 | ssp_url, ssp_error_msg = _stage_ssp(parser, results) |
Prathmesh Prabhu | 7ae68ae | 2017-06-15 00:29:31 -0700 | [diff] [blame] | 711 | # The build does not have autotest server package. Fall back to not |
| 712 | # to use server-side package. Logging is postponed until logging being |
| 713 | # set up. |
| 714 | ssp_url_warning = not ssp_url |
| 715 | |
Dan Shi | c68fefb | 2015-04-07 10:10:52 -0700 | [diff] [blame] | 716 | # Server-side packaging will only be used if it's required and the package |
| 717 | # is available. If warn_no_ssp is specified, it means that autoserv is |
| 718 | # running in a drone does not have SSP supported and a warning will be logs. |
| 719 | # Therefore, it should not run with SSP. |
| 720 | use_ssp = (not parser.options.warn_no_ssp and parser.options.require_ssp |
| 721 | and ssp_url) |
| 722 | if use_ssp: |
Dan Shi | e28de55 | 2015-05-06 16:51:58 -0700 | [diff] [blame] | 723 | log_dir = os.path.join(results, 'ssp_logs') if results else None |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 724 | if log_dir and not os.path.exists(log_dir): |
| 725 | os.makedirs(log_dir) |
| 726 | else: |
| 727 | log_dir = results |
Dan Shi | 3f1b8a5 | 2015-04-21 11:11:06 -0700 | [diff] [blame] | 728 | |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 729 | logging_manager.configure_logging( |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 730 | server_logging_config.ServerLoggingConfig(), |
| 731 | results_dir=log_dir, |
showard | 10d8417 | 2009-06-18 23:16:50 +0000 | [diff] [blame] | 732 | use_console=not parser.options.no_tee, |
| 733 | verbose=parser.options.verbose, |
| 734 | no_console_prefix=parser.options.no_console_prefix) |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 735 | |
Dan Shi | 0b754c5 | 2015-04-20 14:20:38 -0700 | [diff] [blame] | 736 | if ssp_url_warning: |
| 737 | logging.warn( |
| 738 | 'Autoserv is required to run with server-side packaging. ' |
Dan Shi | 6bfbdb6 | 2017-09-25 13:33:53 -0700 | [diff] [blame] | 739 | 'However, no server-side package can be staged based on ' |
Richard Barnette | 71854c7 | 2018-03-30 14:22:09 -0700 | [diff] [blame] | 740 | '`--test_source_build`, host attribute job_repo_url or host ' |
| 741 | 'OS version label. It could be that the build to test is ' |
| 742 | 'older than the minimum version that supports server-side ' |
| 743 | 'packaging, or no devserver can be found to stage server-side ' |
| 744 | 'package. The test will be executed without using server-side ' |
| 745 | 'packaging. Following is the detailed error:\n%s', |
| 746 | ssp_error_msg) |
Dan Shi | 0b754c5 | 2015-04-20 14:20:38 -0700 | [diff] [blame] | 747 | |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 748 | if results: |
mbligh | a788dc4 | 2009-03-26 21:10:16 +0000 | [diff] [blame] | 749 | logging.info("Results placed in %s" % results) |
mbligh | 1071763 | 2008-11-19 00:21:57 +0000 | [diff] [blame] | 750 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 751 | # wait until now to perform this check, so it get properly logged |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 752 | if (parser.options.use_existing_results and not resultdir_exists and |
Dan Shi | ff78f11 | 2015-06-12 13:34:02 -0700 | [diff] [blame] | 753 | not utils.is_in_container()): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 754 | logging.error("No existing results directory found: %s", results) |
| 755 | sys.exit(1) |
| 756 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 757 | logging.debug('autoserv is running in drone %s.', socket.gethostname()) |
Aviv Keshet | 5c40ec6 | 2013-08-20 12:11:12 -0700 | [diff] [blame] | 758 | logging.debug('autoserv command was: %s', ' '.join(sys.argv)) |
Prathmesh Prabhu | 7fc39c5 | 2018-03-21 14:08:30 -0700 | [diff] [blame] | 759 | logging.debug('autoserv parsed options: %s', parser.options) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 760 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 761 | if parser.options.write_pidfile and results: |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 762 | pid_file_manager = pidfile.PidFileManager(parser.options.pidfile_label, |
| 763 | results) |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 764 | pid_file_manager.open_file() |
mbligh | ff7d61f | 2008-12-22 14:53:35 +0000 | [diff] [blame] | 765 | else: |
| 766 | pid_file_manager = None |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 767 | |
Allen Li | d5abdab | 2017-02-07 16:03:43 -0800 | [diff] [blame] | 768 | autotest.Autotest.set_install_in_tmpdir( |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 769 | parser.options.install_in_tmpdir) |
| 770 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 771 | exit_code = 0 |
Allen Li | f146e87 | 2017-08-15 18:24:31 -0700 | [diff] [blame] | 772 | # TODO(beeps): Extend this to cover different failure modes. |
| 773 | # Testing exceptions are matched against labels sent to autoserv. Eg, |
| 774 | # to allow only the hostless job to run, specify |
| 775 | # testing_exceptions: test_suite in the shadow_config. To allow both |
| 776 | # the hostless job and dummy_Pass to run, specify |
| 777 | # testing_exceptions: test_suite,dummy_Pass. You can figure out |
| 778 | # what label autoserv is invoked with by looking through the logs of a test |
| 779 | # for the autoserv command's -l option. |
| 780 | testing_exceptions = _CONFIG.get_config_value( |
| 781 | 'AUTOSERV', 'testing_exceptions', type=list, default=[]) |
| 782 | test_mode = _CONFIG.get_config_value( |
| 783 | 'AUTOSERV', 'testing_mode', type=bool, default=False) |
| 784 | test_mode = (results_mocker and test_mode and not |
| 785 | any([ex in parser.options.label |
| 786 | for ex in testing_exceptions])) |
| 787 | is_task = (parser.options.verify or parser.options.repair or |
| 788 | parser.options.provision or parser.options.reset or |
| 789 | parser.options.cleanup or parser.options.collect_crashinfo) |
Paul Hobbs | e9fd557 | 2017-08-22 02:48:25 -0700 | [diff] [blame] | 790 | |
| 791 | trace_labels = { |
| 792 | 'job_id': job_directories.get_job_id_or_task_id( |
| 793 | parser.options.results) |
| 794 | } |
| 795 | trace = cloud_trace.SpanStack( |
| 796 | labels=trace_labels, |
| 797 | global_context=parser.options.cloud_trace_context) |
| 798 | trace.enabled = parser.options.cloud_trace_context_enabled == 'True' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 799 | try: |
| 800 | try: |
Allen Li | f146e87 | 2017-08-15 18:24:31 -0700 | [diff] [blame] | 801 | if test_mode: |
| 802 | # The parser doesn't run on tasks anyway, so we can just return |
| 803 | # happy signals without faking results. |
| 804 | if not is_task: |
| 805 | machine = parser.options.results.split('/')[-1] |
| 806 | |
| 807 | # TODO(beeps): The proper way to do this would be to |
| 808 | # refactor job creation so we can invoke job.record |
| 809 | # directly. To do that one needs to pipe the test_name |
| 810 | # through run_autoserv and bail just before invoking |
| 811 | # the server job. See the comment in |
| 812 | # puppylab/results_mocker for more context. |
| 813 | results_mocker.ResultsMocker( |
| 814 | 'unknown-test', parser.options.results, machine |
| 815 | ).mock_results() |
| 816 | return |
| 817 | else: |
Paul Hobbs | e9fd557 | 2017-08-22 02:48:25 -0700 | [diff] [blame] | 818 | with trace.Span(get_job_status(parser.options)): |
| 819 | run_autoserv(pid_file_manager, results, parser, ssp_url, |
| 820 | use_ssp) |
Aviv Keshet | 5c40ec6 | 2013-08-20 12:11:12 -0700 | [diff] [blame] | 821 | except SystemExit as e: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 822 | exit_code = e.code |
Aviv Keshet | 5c40ec6 | 2013-08-20 12:11:12 -0700 | [diff] [blame] | 823 | if exit_code: |
Aviv Keshet | 5ae0a00 | 2017-05-05 10:23:33 -0700 | [diff] [blame] | 824 | logging.exception('Uncaught SystemExit with code %s', exit_code) |
| 825 | except Exception: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 826 | # If we don't know what happened, we'll classify it as |
| 827 | # an 'abort' and return 1. |
Aviv Keshet | 5ae0a00 | 2017-05-05 10:23:33 -0700 | [diff] [blame] | 828 | logging.exception('Uncaught Exception, exit_code = 1.') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 829 | exit_code = 1 |
| 830 | finally: |
mbligh | ff7d61f | 2008-12-22 14:53:35 +0000 | [diff] [blame] | 831 | if pid_file_manager: |
| 832 | pid_file_manager.close_file(exit_code) |
Fang Deng | 042c147 | 2014-10-23 13:56:41 -0700 | [diff] [blame] | 833 | # Record the autoserv duration time. Must be called |
| 834 | # just before the system exits to ensure accuracy. |
Paul Hobbs | 68d9859 | 2017-08-22 02:22:49 -0700 | [diff] [blame] | 835 | record_autoserv(parser.options, start_time) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 836 | sys.exit(exit_code) |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 837 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 838 | |
mbligh | a46678d | 2008-05-01 20:00:01 +0000 | [diff] [blame] | 839 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 840 | main() |