Dan Shi | 4df3925 | 2013-03-19 13:19:45 -0700 | [diff] [blame] | 1 | # pylint: disable-msg=C0111 |
| 2 | |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | __author__ = 'cmasone@chromium.org (Chris Masone)' |
| 8 | |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 9 | # The boto module is only available/used in Moblab for validation of cloud |
| 10 | # storage access. The module is not available in the test lab environment, |
| 11 | # and the import error is handled. |
| 12 | try: |
| 13 | import boto |
| 14 | except ImportError: |
| 15 | boto = None |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 16 | import common |
Simran Basi | 773a86e | 2015-05-13 19:15:42 -0700 | [diff] [blame] | 17 | import ConfigParser |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 18 | import datetime |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 19 | import logging |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 20 | import os |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 21 | import re |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 22 | import shutil |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 23 | import socket |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 24 | |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 25 | from autotest_lib.frontend.afe import models |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 26 | from autotest_lib.client.common_lib import control_data |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 27 | from autotest_lib.client.common_lib import error |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 28 | from autotest_lib.client.common_lib import global_config |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 29 | from autotest_lib.client.common_lib import priorities |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 30 | from autotest_lib.client.common_lib import time_utils |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 31 | from autotest_lib.client.common_lib.cros import dev_server |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 32 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
Jakob Juelich | 9fffe4f | 2014-08-14 18:07:05 -0700 | [diff] [blame] | 33 | from autotest_lib.frontend.afe import rpc_utils |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 34 | from autotest_lib.server import utils |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 35 | from autotest_lib.server.cros import provision |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 36 | from autotest_lib.server.cros.dynamic_suite import constants |
Chris Masone | b493555 | 2012-08-14 12:05:54 -0700 | [diff] [blame] | 37 | from autotest_lib.server.cros.dynamic_suite import control_file_getter |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 38 | from autotest_lib.server.cros.dynamic_suite import tools |
xixuan | 0f7755d | 2016-04-18 14:49:12 -0700 | [diff] [blame] | 39 | from autotest_lib.server.cros.dynamic_suite import suite as SuiteBase |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 40 | from autotest_lib.server.cros.dynamic_suite.suite import Suite |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 41 | from autotest_lib.server.hosts import moblab_host |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 42 | from autotest_lib.site_utils import host_history |
Dan Shi | 193905e | 2014-07-25 23:33:09 -0700 | [diff] [blame] | 43 | from autotest_lib.site_utils import job_history |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 44 | from autotest_lib.site_utils import server_manager_utils |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 45 | from autotest_lib.site_utils import stable_version_utils |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 46 | |
| 47 | |
| 48 | _CONFIG = global_config.global_config |
| 49 | MOBLAB_BOTO_LOCATION = '/home/moblab/.boto' |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 50 | |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 51 | # Google Cloud Storage bucket url regex pattern. The pattern is used to extract |
| 52 | # the bucket name from the bucket URL. For example, "gs://image_bucket/google" |
| 53 | # should result in a bucket name "image_bucket". |
| 54 | GOOGLE_STORAGE_BUCKET_URL_PATTERN = re.compile( |
| 55 | r'gs://(?P<bucket>[a-zA-Z][a-zA-Z0-9-_]*)/?.*') |
| 56 | |
| 57 | # Constants used in JSON RPC field names. |
| 58 | _USE_EXISTING_BOTO_FILE = 'use_existing_boto_file' |
| 59 | _GS_ACCESS_KEY_ID = 'gs_access_key_id' |
| 60 | _GS_SECRETE_ACCESS_KEY = 'gs_secret_access_key' |
| 61 | _IMAGE_STORAGE_SERVER = 'image_storage_server' |
| 62 | _RESULT_STORAGE_SERVER = 'results_storage_server' |
Chris Masone | f8b5306 | 2012-05-08 22:14:18 -0700 | [diff] [blame] | 63 | # Relevant CrosDynamicSuiteExceptions are defined in client/common_lib/error.py. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 64 | |
| 65 | |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 66 | def canonicalize_suite_name(suite_name): |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 67 | # Do not change this naming convention without updating |
| 68 | # site_utils.parse_job_name. |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 69 | return 'test_suites/control.%s' % suite_name |
| 70 | |
| 71 | |
Chris Masone | aa10f8e | 2012-05-15 13:34:21 -0700 | [diff] [blame] | 72 | def formatted_now(): |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 73 | return datetime.datetime.now().strftime(time_utils.TIME_FMT) |
Chris Masone | aa10f8e | 2012-05-15 13:34:21 -0700 | [diff] [blame] | 74 | |
| 75 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 76 | def _get_control_file_contents_by_name(build, ds, suite_name): |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 77 | """Return control file contents for |suite_name|. |
| 78 | |
| 79 | Query the dev server at |ds| for the control file |suite_name|, included |
| 80 | in |build| for |board|. |
| 81 | |
| 82 | @param build: unique name by which to refer to the image from now on. |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 83 | @param ds: a dev_server.DevServer instance to fetch control file with. |
| 84 | @param suite_name: canonicalized suite name, e.g. test_suites/control.bvt. |
| 85 | @raises ControlFileNotFound if a unique suite control file doesn't exist. |
| 86 | @raises NoControlFileList if we can't list the control files at all. |
| 87 | @raises ControlFileEmpty if the control file exists on the server, but |
| 88 | can't be read. |
| 89 | |
| 90 | @return the contents of the desired control file. |
| 91 | """ |
| 92 | getter = control_file_getter.DevServerGetter.create(build, ds) |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 93 | timer = autotest_stats.Timer('control_files.parse.%s.%s' % |
| 94 | (ds.get_server_name(ds.url() |
| 95 | ).replace('.', '_'), |
| 96 | suite_name.rsplit('.')[-1])) |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 97 | # Get the control file for the suite. |
| 98 | try: |
Prashanth Balasubramanian | abe3bb7 | 2014-11-20 12:00:37 -0800 | [diff] [blame] | 99 | with timer: |
| 100 | control_file_in = getter.get_control_file_contents_by_name( |
| 101 | suite_name) |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 102 | except error.CrosDynamicSuiteException as e: |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 103 | raise type(e)("%s while testing %s." % (e, build)) |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 104 | if not control_file_in: |
| 105 | raise error.ControlFileEmpty( |
| 106 | "Fetching %s returned no data." % suite_name) |
Alex Miller | a713e25 | 2013-03-01 10:45:44 -0800 | [diff] [blame] | 107 | # Force control files to only contain ascii characters. |
| 108 | try: |
| 109 | control_file_in.encode('ascii') |
| 110 | except UnicodeDecodeError as e: |
| 111 | raise error.ControlFileMalformed(str(e)) |
| 112 | |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 113 | return control_file_in |
| 114 | |
| 115 | |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 116 | def _stage_build_artifacts(build, hostname=None): |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 117 | """ |
| 118 | Ensure components of |build| necessary for installing images are staged. |
| 119 | |
| 120 | @param build image we want to stage. |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 121 | @param hostname hostname of a dut may run test on. This is to help to locate |
| 122 | a devserver closer to duts if needed. Default is None. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 123 | |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 124 | @raises StageControlFileFailure: if the dev server throws 500 while staging |
| 125 | suite control files. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 126 | |
| 127 | @return: dev_server.ImageServer instance to use with this build. |
| 128 | @return: timings dictionary containing staging start/end times. |
| 129 | """ |
| 130 | timings = {} |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 131 | # Ensure components of |build| necessary for installing images are staged |
| 132 | # on the dev server. However set synchronous to False to allow other |
| 133 | # components to be downloaded in the background. |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 134 | ds = dev_server.resolve(build, hostname=hostname) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 135 | timings[constants.DOWNLOAD_STARTED_TIME] = formatted_now() |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 136 | timer = autotest_stats.Timer('control_files.stage.%s' % ( |
| 137 | ds.get_server_name(ds.url()).replace('.', '_'))) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 138 | try: |
Prashanth Balasubramanian | abe3bb7 | 2014-11-20 12:00:37 -0800 | [diff] [blame] | 139 | with timer: |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 140 | ds.stage_artifacts(image=build, artifacts=['test_suites']) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 141 | except dev_server.DevServerException as e: |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 142 | raise error.StageControlFileFailure( |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 143 | "Failed to stage %s: %s" % (build, e)) |
| 144 | timings[constants.PAYLOAD_FINISHED_TIME] = formatted_now() |
| 145 | return (ds, timings) |
| 146 | |
| 147 | |
MK Ryu | e301eb7 | 2015-06-25 12:51:02 -0700 | [diff] [blame] | 148 | @rpc_utils.route_rpc_to_master |
Dan Shi | 5984d78 | 2016-04-05 18:43:51 -0700 | [diff] [blame] | 149 | def create_suite_job(name='', board='', pool='', control_file='', |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 150 | check_hosts=True, num=None, file_bugs=False, timeout=24, |
| 151 | timeout_mins=None, priority=priorities.Priority.DEFAULT, |
Fang Deng | 058860c | 2014-05-15 15:41:50 -0700 | [diff] [blame] | 152 | suite_args=None, wait_for_results=True, job_retry=False, |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 153 | max_retries=None, max_runtime_mins=None, suite_min_duts=0, |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 154 | offload_failures_only=False, builds={}, |
Dan Shi | 059261a | 2016-02-22 12:06:37 -0800 | [diff] [blame] | 155 | test_source_build=None, run_prod_code=False, |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 156 | delay_minutes=0, is_cloning=False, **kwargs): |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 157 | """ |
| 158 | Create a job to run a test suite on the given device with the given image. |
| 159 | |
| 160 | When the timeout specified in the control file is reached, the |
| 161 | job is guaranteed to have completed and results will be available. |
| 162 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 163 | @param name: The test name if control_file is supplied, otherwise the name |
| 164 | of the test suite to run, e.g. 'bvt'. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 165 | @param board: the kind of device to run the tests on. |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 166 | @param builds: the builds to install e.g. |
| 167 | {'cros-version:': 'x86-alex-release/R18-1655.0.0', |
Dan Shi | 5984d78 | 2016-04-05 18:43:51 -0700 | [diff] [blame] | 168 | 'fwrw-version:': 'x86-alex-firmware/R36-5771.50.0', |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 169 | 'fwro-version:': 'x86-alex-firmware/R36-5771.49.0'} |
| 170 | If builds is given a value, it overrides argument build. |
| 171 | @param test_source_build: Build that contains the server-side test code. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 172 | @param pool: Specify the pool of machines to use for scheduling |
| 173 | purposes. |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 174 | @param check_hosts: require appropriate live hosts to exist in the lab. |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 175 | @param num: Specify the number of machines to schedule across (integer). |
| 176 | Leave unspecified or use None to use default sharding factor. |
Alex Miller | c577f3e | 2012-09-27 14:06:07 -0700 | [diff] [blame] | 177 | @param file_bugs: File a bug on each test failure in this suite. |
Alex Miller | 139690b | 2013-09-07 15:35:49 -0700 | [diff] [blame] | 178 | @param timeout: The max lifetime of this suite, in hours. |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 179 | @param timeout_mins: The max lifetime of this suite, in minutes. Takes |
| 180 | priority over timeout. |
Alex Miller | 139690b | 2013-09-07 15:35:49 -0700 | [diff] [blame] | 181 | @param priority: Integer denoting priority. Higher is more important. |
Aviv Keshet | 7cd1231 | 2013-07-25 10:25:55 -0700 | [diff] [blame] | 182 | @param suite_args: Optional arguments which will be parsed by the suite |
| 183 | control file. Used by control.test_that_wrapper to |
| 184 | determine which tests to run. |
Dan Shi | 9512241 | 2013-11-12 16:20:33 -0800 | [diff] [blame] | 185 | @param wait_for_results: Set to False to run the suite job without waiting |
| 186 | for test jobs to finish. Default is True. |
Fang Deng | 058860c | 2014-05-15 15:41:50 -0700 | [diff] [blame] | 187 | @param job_retry: Set to True to enable job-level retry. Default is False. |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 188 | @param max_retries: Integer, maximum job retries allowed at suite level. |
| 189 | None for no max. |
Simran Basi | 102e352 | 2014-09-11 11:46:10 -0700 | [diff] [blame] | 190 | @param max_runtime_mins: Maximum amount of time a job can be running in |
| 191 | minutes. |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 192 | @param suite_min_duts: Integer. Scheduler will prioritize getting the |
| 193 | minimum number of machines for the suite when it is |
| 194 | competing with another suite that has a higher |
| 195 | priority but already got minimum machines it needs. |
Simran Basi | 1e10e92 | 2015-04-16 15:09:56 -0700 | [diff] [blame] | 196 | @param offload_failures_only: Only enable gs_offloading for failed jobs. |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 197 | @param run_prod_code: If True, the suite will run the test code that |
| 198 | lives in prod aka the test code currently on the |
| 199 | lab servers. If False, the control files and test |
| 200 | code for this suite run will be retrieved from the |
| 201 | build artifacts. |
Dan Shi | 059261a | 2016-02-22 12:06:37 -0800 | [diff] [blame] | 202 | @param delay_minutes: Delay the creation of test jobs for a given number of |
| 203 | minutes. |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 204 | @param is_cloning: True if creating a cloning job. |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 205 | @param kwargs: extra keyword args. NOT USED. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 206 | |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 207 | @raises ControlFileNotFound: if a unique suite control file doesn't exist. |
| 208 | @raises NoControlFileList: if we can't list the control files at all. |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 209 | @raises StageControlFileFailure: If the dev server throws 500 while |
| 210 | staging test_suites. |
Chris Masone | 8dd27e0 | 2012-06-25 15:59:43 -0700 | [diff] [blame] | 211 | @raises ControlFileEmpty: if the control file exists on the server, but |
| 212 | can't be read. |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 213 | |
| 214 | @return: the job ID of the suite; -1 on error. |
| 215 | """ |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 216 | if type(num) is not int and num is not None: |
Chris Sosa | 18c70b3 | 2013-02-15 14:12:43 -0800 | [diff] [blame] | 217 | raise error.SuiteArgumentException('Ill specified num argument %r. ' |
| 218 | 'Must be an integer or None.' % num) |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 219 | if num == 0: |
| 220 | logging.warning("Can't run on 0 hosts; using default.") |
| 221 | num = None |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 222 | |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 223 | # Default test source build to CrOS build if it's not specified and |
| 224 | # run_prod_code is set to False. |
| 225 | if not run_prod_code: |
| 226 | test_source_build = Suite.get_test_source_build( |
| 227 | builds, test_source_build=test_source_build) |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 228 | |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 229 | # If 'prefer_local_devserver' is True in global setting, and both board |
| 230 | # and pool are specified, pick a dut in the given board and pool, and |
| 231 | # use that to help to pick a devserver in the same subnet of the duts |
| 232 | # to be used to run tests. |
| 233 | if dev_server.PREFER_LOCAL_DEVSERVER and pool and board: |
| 234 | sample_dut = rpc_utils.get_sample_dut(board, pool) |
| 235 | else: |
| 236 | sample_dut = None |
| 237 | |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 238 | suite_name = canonicalize_suite_name(name) |
| 239 | if run_prod_code: |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 240 | ds = dev_server.resolve(test_source_build, hostname=sample_dut) |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 241 | keyvals = {} |
| 242 | getter = control_file_getter.FileSystemGetter( |
| 243 | [_CONFIG.get_config_value('SCHEDULER', |
| 244 | 'drone_installation_directory')]) |
| 245 | control_file = getter.get_control_file_contents_by_name(suite_name) |
| 246 | else: |
Dan Shi | 5e8fa18 | 2016-04-15 11:04:36 -0700 | [diff] [blame] | 247 | (ds, keyvals) = _stage_build_artifacts( |
| 248 | test_source_build, hostname=sample_dut) |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 249 | keyvals[constants.SUITE_MIN_DUTS_KEY] = suite_min_duts |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 250 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 251 | if not control_file: |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 252 | # No control file was supplied so look it up from the build artifacts. |
| 253 | suite_name = canonicalize_suite_name(name) |
| 254 | control_file = _get_control_file_contents_by_name(test_source_build, |
| 255 | ds, suite_name) |
Simran Basi | 86fe9c9 | 2016-02-09 17:58:20 -0800 | [diff] [blame] | 256 | # Do not change this naming convention without updating |
| 257 | # site_utils.parse_job_name. |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 258 | if not run_prod_code: |
| 259 | name = '%s-%s' % (test_source_build, suite_name) |
| 260 | else: |
| 261 | # If run_prod_code is True, test_source_build is not set, use the |
| 262 | # first build in the builds list for the sutie job name. |
| 263 | name = '%s-%s' % (builds.values()[0], suite_name) |
Chris Masone | 46d0eb1 | 2012-07-27 18:56:39 -0700 | [diff] [blame] | 264 | |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 265 | timeout_mins = timeout_mins or timeout * 60 |
Simran Basi | 102e352 | 2014-09-11 11:46:10 -0700 | [diff] [blame] | 266 | max_runtime_mins = max_runtime_mins or timeout * 60 |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 267 | |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 268 | if not board: |
Dan Shi | d215dbe | 2015-06-18 16:14:59 -0700 | [diff] [blame] | 269 | board = utils.ParseBuildName(builds[provision.CROS_VERSION_PREFIX])[0] |
Chris Masone | 46d0eb1 | 2012-07-27 18:56:39 -0700 | [diff] [blame] | 270 | |
Dan Shi | 5984d78 | 2016-04-05 18:43:51 -0700 | [diff] [blame] | 271 | # Prepend builds and board to the control file. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 272 | inject_dict = {'board': board, |
Dan Shi | 6dc22d1 | 2016-04-06 22:10:04 -0700 | [diff] [blame] | 273 | # `build` is needed for suites like AU to stage image inside |
| 274 | # suite control file. |
| 275 | 'build': test_source_build, |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 276 | 'builds': builds, |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 277 | 'check_hosts': check_hosts, |
Chris Masone | 46d0eb1 | 2012-07-27 18:56:39 -0700 | [diff] [blame] | 278 | 'pool': pool, |
Aviv Keshet | d83ef44 | 2013-01-16 16:19:35 -0800 | [diff] [blame] | 279 | 'num': num, |
Dan Shi | b8a9911 | 2013-06-18 13:46:10 -0700 | [diff] [blame] | 280 | 'file_bugs': file_bugs, |
Alex Miller | 139690b | 2013-09-07 15:35:49 -0700 | [diff] [blame] | 281 | 'timeout': timeout, |
Simran Basi | 7e60574 | 2013-11-12 13:43:36 -0800 | [diff] [blame] | 282 | 'timeout_mins': timeout_mins, |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 283 | 'devserver_url': ds.url(), |
Aviv Keshet | 7cd1231 | 2013-07-25 10:25:55 -0700 | [diff] [blame] | 284 | 'priority': priority, |
Dan Shi | 9512241 | 2013-11-12 16:20:33 -0800 | [diff] [blame] | 285 | 'suite_args' : suite_args, |
Fang Deng | 058860c | 2014-05-15 15:41:50 -0700 | [diff] [blame] | 286 | 'wait_for_results': wait_for_results, |
Simran Basi | 102e352 | 2014-09-11 11:46:10 -0700 | [diff] [blame] | 287 | 'job_retry': job_retry, |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 288 | 'max_retries': max_retries, |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 289 | 'max_runtime_mins': max_runtime_mins, |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 290 | 'offload_failures_only': offload_failures_only, |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 291 | 'test_source_build': test_source_build, |
Dan Shi | 059261a | 2016-02-22 12:06:37 -0800 | [diff] [blame] | 292 | 'run_prod_code': run_prod_code, |
| 293 | 'delay_minutes': delay_minutes, |
Aviv Keshet | 7cd1231 | 2013-07-25 10:25:55 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 296 | if is_cloning: |
| 297 | control_file = tools.remove_injection(control_file) |
Simran Basi | b6ec8ae | 2014-04-23 12:05:08 -0700 | [diff] [blame] | 298 | control_file = tools.inject_vars(inject_dict, control_file) |
Chris Masone | 859fdec | 2012-01-30 08:38:09 -0800 | [diff] [blame] | 299 | |
Jakob Juelich | 9fffe4f | 2014-08-14 18:07:05 -0700 | [diff] [blame] | 300 | return rpc_utils.create_job_common(name, |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 301 | priority=priority, |
| 302 | timeout_mins=timeout_mins, |
| 303 | max_runtime_mins=max_runtime_mins, |
| 304 | control_type='Server', |
| 305 | control_file=control_file, |
| 306 | hostless=True, |
Fang Deng | cbc0121 | 2014-11-25 16:09:46 -0800 | [diff] [blame] | 307 | keyvals=keyvals) |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 308 | |
| 309 | |
| 310 | # TODO: hide the following rpcs under is_moblab |
| 311 | def moblab_only(func): |
| 312 | """Ensure moblab specific functions only run on Moblab devices.""" |
| 313 | def verify(*args, **kwargs): |
| 314 | if not utils.is_moblab(): |
| 315 | raise error.RPCException('RPC: %s can only run on Moblab Systems!', |
| 316 | func.__name__) |
| 317 | return func(*args, **kwargs) |
| 318 | return verify |
| 319 | |
| 320 | |
| 321 | @moblab_only |
| 322 | def get_config_values(): |
| 323 | """Returns all config values parsed from global and shadow configs. |
| 324 | |
| 325 | Config values are grouped by sections, and each section is composed of |
| 326 | a list of name value pairs. |
| 327 | """ |
| 328 | sections =_CONFIG.get_sections() |
| 329 | config_values = {} |
| 330 | for section in sections: |
| 331 | config_values[section] = _CONFIG.config.items(section) |
Jakob Juelich | 9fffe4f | 2014-08-14 18:07:05 -0700 | [diff] [blame] | 332 | return rpc_utils.prepare_for_serialization(config_values) |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 333 | |
| 334 | |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 335 | def _write_config_file(config_file, config_values, overwrite=False): |
| 336 | """Writes out a configuration file. |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 337 | |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 338 | @param config_file: The name of the configuration file. |
| 339 | @param config_values: The ConfigParser object. |
| 340 | @param ovewrite: Flag on if overwriting is allowed. |
| 341 | """ |
| 342 | if not config_file: |
| 343 | raise error.RPCException('Empty config file name.') |
| 344 | if not overwrite and os.path.exists(config_file): |
| 345 | raise error.RPCException('Config file already exists.') |
| 346 | |
| 347 | if config_values: |
| 348 | with open(config_file, 'w') as config_file: |
| 349 | config_values.write(config_file) |
| 350 | |
| 351 | |
| 352 | def _read_original_config(): |
| 353 | """Reads the orginal configuratino without shadow. |
| 354 | |
| 355 | @return: A configuration object, see global_config_class. |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 356 | """ |
Simran Basi | 773a86e | 2015-05-13 19:15:42 -0700 | [diff] [blame] | 357 | original_config = global_config.global_config_class() |
| 358 | original_config.set_config_files(shadow_file='') |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 359 | return original_config |
| 360 | |
| 361 | |
| 362 | def _read_raw_config(config_file): |
| 363 | """Reads the raw configuration from a configuration file. |
| 364 | |
| 365 | @param: config_file: The path of the configuration file. |
| 366 | |
| 367 | @return: A ConfigParser object. |
| 368 | """ |
| 369 | shadow_config = ConfigParser.RawConfigParser() |
| 370 | shadow_config.read(config_file) |
| 371 | return shadow_config |
| 372 | |
| 373 | |
| 374 | def _get_shadow_config_from_partial_update(config_values): |
| 375 | """Finds out the new shadow configuration based on a partial update. |
| 376 | |
| 377 | Since the input is only a partial config, we should not lose the config |
| 378 | data inside the existing shadow config file. We also need to distinguish |
| 379 | if the input config info overrides with a new value or reverts back to |
| 380 | an original value. |
| 381 | |
| 382 | @param config_values: See get_moblab_settings(). |
| 383 | |
| 384 | @return: The new shadow configuration as ConfigParser object. |
| 385 | """ |
| 386 | original_config = _read_original_config() |
| 387 | existing_shadow = _read_raw_config(_CONFIG.shadow_file) |
| 388 | for section, config_value_list in config_values.iteritems(): |
| 389 | for key, value in config_value_list: |
| 390 | if original_config.get_config_value(section, key, |
| 391 | default='', |
| 392 | allow_blank=True) != value: |
| 393 | if not existing_shadow.has_section(section): |
| 394 | existing_shadow.add_section(section) |
| 395 | existing_shadow.set(section, key, value) |
| 396 | elif existing_shadow.has_option(section, key): |
| 397 | existing_shadow.remove_option(section, key) |
| 398 | return existing_shadow |
| 399 | |
| 400 | |
| 401 | def _update_partial_config(config_values): |
| 402 | """Updates the shadow configuration file with a partial config udpate. |
| 403 | |
| 404 | @param config_values: See get_moblab_settings(). |
| 405 | """ |
| 406 | existing_config = _get_shadow_config_from_partial_update(config_values) |
| 407 | _write_config_file(_CONFIG.shadow_file, existing_config, True) |
| 408 | |
| 409 | |
| 410 | @moblab_only |
| 411 | def update_config_handler(config_values): |
| 412 | """Update config values and override shadow config. |
| 413 | |
| 414 | @param config_values: See get_moblab_settings(). |
| 415 | """ |
| 416 | original_config = _read_original_config() |
Simran Basi | 773a86e | 2015-05-13 19:15:42 -0700 | [diff] [blame] | 417 | new_shadow = ConfigParser.RawConfigParser() |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 418 | for section, config_value_list in config_values.iteritems(): |
| 419 | for key, value in config_value_list: |
Simran Basi | 773a86e | 2015-05-13 19:15:42 -0700 | [diff] [blame] | 420 | if original_config.get_config_value(section, key, |
| 421 | default='', |
| 422 | allow_blank=True) != value: |
| 423 | if not new_shadow.has_section(section): |
| 424 | new_shadow.add_section(section) |
| 425 | new_shadow.set(section, key, value) |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 426 | |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 427 | if not _CONFIG.shadow_file or not os.path.exists(_CONFIG.shadow_file): |
| 428 | raise error.RPCException('Shadow config file does not exist.') |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 429 | _write_config_file(_CONFIG.shadow_file, new_shadow, True) |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 430 | |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 431 | # TODO (sbasi) crbug.com/403916 - Remove the reboot command and |
| 432 | # instead restart the services that rely on the config values. |
| 433 | os.system('sudo reboot') |
| 434 | |
| 435 | |
| 436 | @moblab_only |
| 437 | def reset_config_settings(): |
| 438 | with open(_CONFIG.shadow_file, 'w') as config_file: |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 439 | pass |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 440 | os.system('sudo reboot') |
| 441 | |
| 442 | |
| 443 | @moblab_only |
Michael Tang | c05c9ef | 2016-03-25 14:31:14 -0700 | [diff] [blame] | 444 | def reboot_moblab(): |
| 445 | """Simply reboot the device.""" |
| 446 | os.system('sudo reboot') |
| 447 | |
| 448 | @moblab_only |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 449 | def set_boto_key(boto_key): |
| 450 | """Update the boto_key file. |
| 451 | |
| 452 | @param boto_key: File name of boto_key uploaded through handle_file_upload. |
| 453 | """ |
| 454 | if not os.path.exists(boto_key): |
| 455 | raise error.RPCException('Boto key: %s does not exist!' % boto_key) |
| 456 | shutil.copyfile(boto_key, moblab_host.MOBLAB_BOTO_LOCATION) |
Dan Shi | 193905e | 2014-07-25 23:33:09 -0700 | [diff] [blame] | 457 | |
| 458 | |
Dan Shi | aec9901 | 2016-01-07 09:09:16 -0800 | [diff] [blame] | 459 | @moblab_only |
| 460 | def set_launch_control_key(launch_control_key): |
| 461 | """Update the launch_control_key file. |
| 462 | |
| 463 | @param launch_control_key: File name of launch_control_key uploaded through |
| 464 | handle_file_upload. |
| 465 | """ |
| 466 | if not os.path.exists(launch_control_key): |
| 467 | raise error.RPCException('Launch Control key: %s does not exist!' % |
| 468 | launch_control_key) |
| 469 | shutil.copyfile(launch_control_key, |
| 470 | moblab_host.MOBLAB_LAUNCH_CONTROL_KEY_LOCATION) |
| 471 | # Restart the devserver service. |
| 472 | os.system('sudo restart moblab-devserver-init') |
| 473 | |
| 474 | |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 475 | ###########Moblab Config Wizard RPCs ####################### |
| 476 | def _get_public_ip_address(socket_handle): |
| 477 | """Gets the public IP address. |
| 478 | |
| 479 | Connects to Google DNS server using a socket and gets the preferred IP |
| 480 | address from the connection. |
| 481 | |
| 482 | @param: socket_handle: a unix socket. |
| 483 | |
| 484 | @return: public ip address as string. |
| 485 | """ |
| 486 | try: |
| 487 | socket_handle.settimeout(1) |
| 488 | socket_handle.connect(('8.8.8.8', 53)) |
| 489 | socket_name = socket_handle.getsockname() |
| 490 | if socket_name is not None: |
| 491 | logging.info('Got socket name from UDP socket.') |
| 492 | return socket_name[0] |
| 493 | logging.warn('Created UDP socket but with no socket_name.') |
| 494 | except socket.error: |
| 495 | logging.warn('Could not get socket name from UDP socket.') |
| 496 | return None |
| 497 | |
| 498 | |
| 499 | def _get_network_info(): |
| 500 | """Gets the network information. |
| 501 | |
| 502 | TCP socket is used to test the connectivity. If there is no connectivity, try to |
| 503 | get the public IP with UDP socket. |
| 504 | |
| 505 | @return: a tuple as (public_ip_address, connected_to_internet). |
| 506 | """ |
| 507 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 508 | ip = _get_public_ip_address(s) |
| 509 | if ip is not None: |
| 510 | logging.info('Established TCP connection with well known server.') |
| 511 | return (ip, True) |
| 512 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 513 | return (_get_public_ip_address(s), False) |
| 514 | |
| 515 | |
| 516 | @moblab_only |
| 517 | def get_network_info(): |
| 518 | """Returns the server ip addresses, and if the server connectivity. |
| 519 | |
| 520 | The server ip addresses as an array of strings, and the connectivity as a |
| 521 | flag. |
| 522 | """ |
| 523 | network_info = {} |
| 524 | info = _get_network_info() |
| 525 | if info[0] is not None: |
| 526 | network_info['server_ips'] = [info[0]] |
| 527 | network_info['is_connected'] = info[1] |
| 528 | |
| 529 | return rpc_utils.prepare_for_serialization(network_info) |
| 530 | |
| 531 | |
| 532 | # Gets the boto configuration. |
| 533 | def _get_boto_config(): |
| 534 | """Reads the boto configuration from the boto file. |
| 535 | |
| 536 | @return: Boto configuration as ConfigParser object. |
| 537 | """ |
| 538 | boto_config = ConfigParser.ConfigParser() |
| 539 | boto_config.read(MOBLAB_BOTO_LOCATION) |
| 540 | return boto_config |
| 541 | |
| 542 | |
| 543 | @moblab_only |
| 544 | def get_cloud_storage_info(): |
| 545 | """RPC handler to get the cloud storage access information. |
| 546 | """ |
| 547 | cloud_storage_info = {} |
| 548 | value =_CONFIG.get_config_value('CROS', _IMAGE_STORAGE_SERVER) |
| 549 | if value is not None: |
| 550 | cloud_storage_info[_IMAGE_STORAGE_SERVER] = value |
Michael Tang | 0a30d46 | 2016-06-27 11:44:04 -0700 | [diff] [blame] | 551 | value = _CONFIG.get_config_value('CROS', _RESULT_STORAGE_SERVER, |
| 552 | default=None) |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 553 | if value is not None: |
| 554 | cloud_storage_info[_RESULT_STORAGE_SERVER] = value |
| 555 | |
| 556 | boto_config = _get_boto_config() |
| 557 | sections = boto_config.sections() |
| 558 | |
| 559 | if sections: |
| 560 | cloud_storage_info[_USE_EXISTING_BOTO_FILE] = True |
| 561 | else: |
| 562 | cloud_storage_info[_USE_EXISTING_BOTO_FILE] = False |
| 563 | if 'Credentials' in sections: |
| 564 | options = boto_config.options('Credentials') |
| 565 | if _GS_ACCESS_KEY_ID in options: |
| 566 | value = boto_config.get('Credentials', _GS_ACCESS_KEY_ID) |
| 567 | cloud_storage_info[_GS_ACCESS_KEY_ID] = value |
| 568 | if _GS_SECRETE_ACCESS_KEY in options: |
| 569 | value = boto_config.get('Credentials', _GS_SECRETE_ACCESS_KEY) |
| 570 | cloud_storage_info[_GS_SECRETE_ACCESS_KEY] = value |
| 571 | |
| 572 | return rpc_utils.prepare_for_serialization(cloud_storage_info) |
| 573 | |
| 574 | |
| 575 | def _get_bucket_name_from_url(bucket_url): |
| 576 | """Gets the bucket name from a bucket url. |
| 577 | |
| 578 | @param: bucket_url: the bucket url string. |
| 579 | """ |
| 580 | if bucket_url: |
| 581 | match = GOOGLE_STORAGE_BUCKET_URL_PATTERN.match(bucket_url) |
| 582 | if match: |
| 583 | return match.group('bucket') |
| 584 | return None |
| 585 | |
| 586 | |
| 587 | def _is_valid_boto_key(key_id, key_secret): |
| 588 | """Checks if the boto key is valid. |
| 589 | |
| 590 | @param: key_id: The boto key id string. |
| 591 | @param: key_secret: The boto key string. |
| 592 | |
| 593 | @return: A tuple as (valid_boolean, details_string). |
| 594 | """ |
| 595 | if not key_id or not key_secret: |
| 596 | return (False, "Empty key id or secret.") |
| 597 | conn = boto.connect_gs(key_id, key_secret) |
| 598 | try: |
| 599 | buckets = conn.get_all_buckets() |
| 600 | return (True, None) |
| 601 | except boto.exception.GSResponseError: |
| 602 | details = "The boto access key is not valid" |
| 603 | return (False, details) |
| 604 | finally: |
| 605 | conn.close() |
| 606 | |
| 607 | |
| 608 | def _is_valid_bucket(key_id, key_secret, bucket_name): |
| 609 | """Checks if a bucket is valid and accessible. |
| 610 | |
| 611 | @param: key_id: The boto key id string. |
| 612 | @param: key_secret: The boto key string. |
| 613 | @param: bucket name string. |
| 614 | |
| 615 | @return: A tuple as (valid_boolean, details_string). |
| 616 | """ |
| 617 | if not key_id or not key_secret or not bucket_name: |
| 618 | return (False, "Server error: invalid argument") |
| 619 | conn = boto.connect_gs(key_id, key_secret) |
| 620 | bucket = conn.lookup(bucket_name) |
| 621 | conn.close() |
| 622 | if bucket: |
| 623 | return (True, None) |
| 624 | return (False, "Bucket %s does not exist." % bucket_name) |
| 625 | |
| 626 | |
| 627 | def _is_valid_bucket_url(key_id, key_secret, bucket_url): |
| 628 | """Validates the bucket url is accessible. |
| 629 | |
| 630 | @param: key_id: The boto key id string. |
| 631 | @param: key_secret: The boto key string. |
| 632 | @param: bucket url string. |
| 633 | |
| 634 | @return: A tuple as (valid_boolean, details_string). |
| 635 | """ |
| 636 | bucket_name = _get_bucket_name_from_url(bucket_url) |
| 637 | if bucket_name: |
| 638 | return _is_valid_bucket(key_id, key_secret, bucket_name) |
| 639 | return (False, "Bucket url %s is not valid" % bucket_url) |
| 640 | |
| 641 | |
| 642 | def _validate_cloud_storage_info(cloud_storage_info): |
| 643 | """Checks if the cloud storage information is valid. |
| 644 | |
| 645 | @param: cloud_storage_info: The JSON RPC object for cloud storage info. |
| 646 | |
| 647 | @return: A tuple as (valid_boolean, details_string). |
| 648 | """ |
| 649 | valid = True |
| 650 | details = None |
| 651 | if not cloud_storage_info[_USE_EXISTING_BOTO_FILE]: |
| 652 | key_id = cloud_storage_info[_GS_ACCESS_KEY_ID] |
| 653 | key_secret = cloud_storage_info[_GS_SECRETE_ACCESS_KEY] |
| 654 | valid, details = _is_valid_boto_key(key_id, key_secret) |
| 655 | |
| 656 | if valid: |
| 657 | valid, details = _is_valid_bucket_url( |
| 658 | key_id, key_secret, cloud_storage_info[_IMAGE_STORAGE_SERVER]) |
| 659 | |
Michael Tang | 0a30d46 | 2016-06-27 11:44:04 -0700 | [diff] [blame] | 660 | # allows result bucket to be empty. |
| 661 | if valid and cloud_storage_info[_RESULT_STORAGE_SERVER]: |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 662 | valid, details = _is_valid_bucket_url( |
| 663 | key_id, key_secret, cloud_storage_info[_RESULT_STORAGE_SERVER]) |
| 664 | return (valid, details) |
| 665 | |
| 666 | |
| 667 | def _create_operation_status_response(is_ok, details): |
| 668 | """Helper method to create a operation status reponse. |
| 669 | |
| 670 | @param: is_ok: Boolean for if the operation is ok. |
| 671 | @param: details: A detailed string. |
| 672 | |
| 673 | @return: A serialized JSON RPC object. |
| 674 | """ |
| 675 | status_response = {'status_ok': is_ok} |
| 676 | if details: |
| 677 | status_response['status_details'] = details |
| 678 | return rpc_utils.prepare_for_serialization(status_response) |
| 679 | |
| 680 | |
| 681 | @moblab_only |
| 682 | def validate_cloud_storage_info(cloud_storage_info): |
| 683 | """RPC handler to check if the cloud storage info is valid. |
| 684 | """ |
| 685 | valid, details = _validate_cloud_storage_info(cloud_storage_info) |
| 686 | return _create_operation_status_response(valid, details) |
| 687 | |
| 688 | |
| 689 | @moblab_only |
| 690 | def submit_wizard_config_info(cloud_storage_info): |
| 691 | """RPC handler to submit the cloud storage info. |
| 692 | """ |
| 693 | valid, details = _validate_cloud_storage_info(cloud_storage_info) |
| 694 | if not valid: |
| 695 | return _create_operation_status_response(valid, details) |
| 696 | config_update = {} |
| 697 | config_update['CROS'] = [ |
| 698 | (_IMAGE_STORAGE_SERVER, cloud_storage_info[_IMAGE_STORAGE_SERVER]), |
| 699 | (_RESULT_STORAGE_SERVER, cloud_storage_info[_RESULT_STORAGE_SERVER]) |
| 700 | ] |
| 701 | _update_partial_config(config_update) |
| 702 | |
| 703 | if not cloud_storage_info[_USE_EXISTING_BOTO_FILE]: |
| 704 | boto_config = ConfigParser.RawConfigParser() |
| 705 | boto_config.add_section('Credentials') |
| 706 | boto_config.set('Credentials', _GS_ACCESS_KEY_ID, |
| 707 | cloud_storage_info[_GS_ACCESS_KEY_ID]) |
| 708 | boto_config.set('Credentials', _GS_SECRETE_ACCESS_KEY, |
| 709 | cloud_storage_info[_GS_SECRETE_ACCESS_KEY]) |
| 710 | _write_config_file(MOBLAB_BOTO_LOCATION, boto_config, True) |
| 711 | |
| 712 | _CONFIG.parse_config_file() |
| 713 | |
Michael Tang | c05c9ef | 2016-03-25 14:31:14 -0700 | [diff] [blame] | 714 | # TODO(ntang): replace reboot with less intrusive reloading. |
| 715 | os.system('sudo reboot') |
| 716 | |
Michael Tang | 9afc74b | 2016-03-21 10:19:23 -0700 | [diff] [blame] | 717 | return _create_operation_status_response(True, None) |
| 718 | |
| 719 | |
Dan Shi | 193905e | 2014-07-25 23:33:09 -0700 | [diff] [blame] | 720 | def get_job_history(**filter_data): |
| 721 | """Get history of the job, including the special tasks executed for the job |
| 722 | |
| 723 | @param filter_data: filter for the call, should at least include |
| 724 | {'job_id': [job id]} |
| 725 | @returns: JSON string of the job's history, including the information such |
| 726 | as the hosts run the job and the special tasks executed before |
| 727 | and after the job. |
| 728 | """ |
| 729 | job_id = filter_data['job_id'] |
| 730 | job_info = job_history.get_job_info(job_id) |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 731 | return rpc_utils.prepare_for_serialization(job_info.get_history()) |
| 732 | |
| 733 | |
| 734 | def get_host_history(start_time, end_time, hosts=None, board=None, pool=None): |
| 735 | """Get history of a list of host. |
| 736 | |
| 737 | The return is a JSON string of host history for each host, for example, |
| 738 | {'172.22.33.51': [{'status': 'Resetting' |
| 739 | 'start_time': '2014-08-07 10:02:16', |
| 740 | 'end_time': '2014-08-07 10:03:16', |
| 741 | 'log_url': 'http://autotest/reset-546546/debug', |
| 742 | 'dbg_str': 'Task: Special Task 19441991 (host ...)'}, |
| 743 | {'status': 'Running' |
| 744 | 'start_time': '2014-08-07 10:03:18', |
| 745 | 'end_time': '2014-08-07 10:13:00', |
| 746 | 'log_url': 'http://autotest/reset-546546/debug', |
| 747 | 'dbg_str': 'HQE: 15305005, for job: 14995562'} |
| 748 | ] |
| 749 | } |
| 750 | @param start_time: start time to search for history, can be string value or |
| 751 | epoch time. |
| 752 | @param end_time: end time to search for history, can be string value or |
| 753 | epoch time. |
| 754 | @param hosts: A list of hosts to search for history. Default is None. |
| 755 | @param board: board type of hosts. Default is None. |
| 756 | @param pool: pool type of hosts. Default is None. |
| 757 | @returns: JSON string of the host history. |
| 758 | """ |
| 759 | return rpc_utils.prepare_for_serialization( |
| 760 | host_history.get_history_details( |
| 761 | start_time=start_time, end_time=end_time, |
| 762 | hosts=hosts, board=board, pool=pool, |
| 763 | process_pool_size=4)) |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 764 | |
| 765 | |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 766 | def shard_heartbeat(shard_hostname, jobs=(), hqes=(), known_job_ids=(), |
| 767 | known_host_ids=(), known_host_statuses=()): |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 768 | """Receive updates for job statuses from shards and assign hosts and jobs. |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 769 | |
| 770 | @param shard_hostname: Hostname of the calling shard |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 771 | @param jobs: Jobs in serialized form that should be updated with newer |
| 772 | status from a shard. |
| 773 | @param hqes: Hostqueueentries in serialized form that should be updated with |
| 774 | newer status from a shard. Note that for every hostqueueentry |
| 775 | the corresponding job must be in jobs. |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 776 | @param known_job_ids: List of ids of jobs the shard already has. |
| 777 | @param known_host_ids: List of ids of hosts the shard already has. |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 778 | @param known_host_statuses: List of statuses of hosts the shard already has. |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 779 | |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 780 | @returns: Serialized representations of hosts, jobs, suite job keyvals |
| 781 | and their dependencies to be inserted into a shard's database. |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 782 | """ |
Jakob Juelich | 1b52574 | 2014-09-30 13:08:07 -0700 | [diff] [blame] | 783 | # The following alternatives to sending host and job ids in every heartbeat |
| 784 | # have been considered: |
| 785 | # 1. Sending the highest known job and host ids. This would work for jobs: |
| 786 | # Newer jobs always have larger ids. Also, if a job is not assigned to a |
| 787 | # particular shard during a heartbeat, it never will be assigned to this |
| 788 | # shard later. |
| 789 | # This is not true for hosts though: A host that is leased won't be sent |
| 790 | # to the shard now, but might be sent in a future heartbeat. This means |
| 791 | # sometimes hosts should be transfered that have a lower id than the |
| 792 | # maximum host id the shard knows. |
| 793 | # 2. Send the number of jobs/hosts the shard knows to the master in each |
| 794 | # heartbeat. Compare these to the number of records that already have |
| 795 | # the shard_id set to this shard. In the normal case, they should match. |
| 796 | # In case they don't, resend all entities of that type. |
| 797 | # This would work well for hosts, because there aren't that many. |
| 798 | # Resending all jobs is quite a big overhead though. |
| 799 | # Also, this approach might run into edge cases when entities are |
| 800 | # ever deleted. |
| 801 | # 3. Mixtures of the above: Use 1 for jobs and 2 for hosts. |
| 802 | # Using two different approaches isn't consistent and might cause |
| 803 | # confusion. Also the issues with the case of deletions might still |
| 804 | # occur. |
| 805 | # |
| 806 | # The overhead of sending all job and host ids in every heartbeat is low: |
| 807 | # At peaks one board has about 1200 created but unfinished jobs. |
| 808 | # See the numbers here: http://goo.gl/gQCGWH |
| 809 | # Assuming that job id's have 6 digits and that json serialization takes a |
| 810 | # comma and a space as overhead, the traffic per id sent is about 8 bytes. |
| 811 | # If 5000 ids need to be sent, this means 40 kilobytes of traffic. |
| 812 | # A NOT IN query with 5000 ids took about 30ms in tests made. |
| 813 | # These numbers seem low enough to outweigh the disadvantages of the |
| 814 | # solutions described above. |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 815 | timer = autotest_stats.Timer('shard_heartbeat') |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 816 | with timer: |
| 817 | shard_obj = rpc_utils.retrieve_shard(shard_hostname=shard_hostname) |
Jakob Juelich | a94efe6 | 2014-09-18 16:02:49 -0700 | [diff] [blame] | 818 | rpc_utils.persist_records_sent_from_shard(shard_obj, jobs, hqes) |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 819 | assert len(known_host_ids) == len(known_host_statuses) |
| 820 | for i in range(len(known_host_ids)): |
| 821 | host_model = models.Host.objects.get(pk=known_host_ids[i]) |
| 822 | if host_model.status != known_host_statuses[i]: |
| 823 | host_model.status = known_host_statuses[i] |
| 824 | host_model.save() |
| 825 | |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 826 | hosts, jobs, suite_keyvals = rpc_utils.find_records_for_shard( |
MK Ryu | 07a109f | 2015-07-21 17:44:32 -0700 | [diff] [blame] | 827 | shard_obj, known_job_ids=known_job_ids, |
| 828 | known_host_ids=known_host_ids) |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 829 | return { |
| 830 | 'hosts': [host.serialize() for host in hosts], |
| 831 | 'jobs': [job.serialize() for job in jobs], |
Fang Deng | f370599 | 2014-12-16 17:32:18 -0800 | [diff] [blame] | 832 | 'suite_keyvals': [kv.serialize() for kv in suite_keyvals], |
Jakob Juelich | 59cfe54 | 2014-09-02 16:37:46 -0700 | [diff] [blame] | 833 | } |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 834 | |
| 835 | |
| 836 | def get_shards(**filter_data): |
| 837 | """Return a list of all shards. |
| 838 | |
| 839 | @returns A sequence of nested dictionaries of shard information. |
| 840 | """ |
| 841 | shards = models.Shard.query_objects(filter_data) |
| 842 | serialized_shards = rpc_utils.prepare_rows_as_nested_dicts(shards, ()) |
| 843 | for serialized, shard in zip(serialized_shards, shards): |
| 844 | serialized['labels'] = [label.name for label in shard.labels.all()] |
| 845 | |
| 846 | return serialized_shards |
| 847 | |
| 848 | |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 849 | def _assign_board_to_shard_precheck(labels): |
| 850 | """Verify whether board labels are valid to be added to a given shard. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 851 | |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 852 | First check whether board label is in correct format. Second, check whether |
| 853 | the board label exist. Third, check whether the board has already been |
| 854 | assigned to shard. |
| 855 | |
| 856 | @param labels: Board labels separated by comma. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 857 | |
Jakob Juelich | 8b110ee | 2014-09-15 16:13:42 -0700 | [diff] [blame] | 858 | @raises error.RPCException: If label provided doesn't start with `board:` |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 859 | or board has been added to shard already. |
Jakob Juelich | 8b110ee | 2014-09-15 16:13:42 -0700 | [diff] [blame] | 860 | @raises models.Label.DoesNotExist: If the label specified doesn't exist. |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 861 | |
| 862 | @returns: A list of label models that ready to be added to shard. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 863 | """ |
MK Ryu | 5dfcc89 | 2015-07-16 15:34:04 -0700 | [diff] [blame] | 864 | labels = labels.split(',') |
| 865 | label_models = [] |
| 866 | for label in labels: |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 867 | # Check whether the board label is in correct format. |
MK Ryu | 5dfcc89 | 2015-07-16 15:34:04 -0700 | [diff] [blame] | 868 | if not label.startswith('board:'): |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 869 | raise error.RPCException('Sharding only supports `board:.*` label.') |
| 870 | # Check whether the board label exist. If not, exception will be thrown |
| 871 | # by smart_get function. |
| 872 | label = models.Label.smart_get(label) |
| 873 | label_id = models.Label.list_objects({'name':label})[0].get('id') |
| 874 | # Check whether the board has been sharded already |
| 875 | try: |
| 876 | shard = models.Shard.objects.get(labels=label) |
| 877 | raise error.RPCException( |
| 878 | '%s is already on shard %s' % (label, shard.hostname)) |
| 879 | except models.Shard.DoesNotExist as e: |
| 880 | # board is not on any shard, so it's valid. |
| 881 | label_models.append(label) |
| 882 | return label_models |
Jakob Juelich | 8b110ee | 2014-09-15 16:13:42 -0700 | [diff] [blame] | 883 | |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 884 | |
| 885 | def add_shard(hostname, labels): |
| 886 | """Add a shard and start running jobs on it. |
| 887 | |
| 888 | @param hostname: The hostname of the shard to be added; needs to be unique. |
| 889 | @param labels: Board labels separated by comma. Jobs of one of the labels |
| 890 | will be assigned to the shard. |
| 891 | |
| 892 | @raises error.RPCException: If label provided doesn't start with `board:` or |
| 893 | board has been added to shard already. |
| 894 | @raises model_logic.ValidationError: If a shard with the given hostname |
| 895 | already exist. |
| 896 | @raises models.Label.DoesNotExist: If the label specified doesn't exist. |
| 897 | |
| 898 | @returns: The id of the added shard. |
| 899 | """ |
| 900 | labels = _assign_board_to_shard_precheck(labels) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 901 | shard = models.Shard.add_object(hostname=hostname) |
Shuqian Zhao | 68139fb | 2016-08-03 16:29:56 -0700 | [diff] [blame] | 902 | for label in labels: |
| 903 | shard.labels.add(label) |
| 904 | return shard.id |
| 905 | |
| 906 | |
| 907 | def add_board_to_shard(hostname, labels): |
| 908 | """Add boards to a given shard |
| 909 | |
| 910 | @param hostname: The hostname of the shard to be changed. |
| 911 | @param labels: Board labels separated by comma. |
| 912 | |
| 913 | @raises error.RPCException: If label provided doesn't start with `board:` or |
| 914 | board has been added to shard already. |
| 915 | @raises models.Label.DoesNotExist: If the label specified doesn't exist. |
| 916 | |
| 917 | @returns: The id of the changed shard. |
| 918 | """ |
| 919 | labels = _assign_board_to_shard_precheck(labels) |
| 920 | shard = models.Shard.objects.get(hostname=hostname) |
| 921 | for label in labels: |
MK Ryu | 5dfcc89 | 2015-07-16 15:34:04 -0700 | [diff] [blame] | 922 | shard.labels.add(label) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 923 | return shard.id |
| 924 | |
| 925 | |
| 926 | def delete_shard(hostname): |
| 927 | """Delete a shard and reclaim all resources from it. |
| 928 | |
| 929 | This claims back all assigned hosts from the shard. To ensure all DUTs are |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 930 | in a sane state, a Reboot task with highest priority is scheduled for them. |
| 931 | This reboots the DUTs and then all left tasks continue to run in drone of |
| 932 | the master. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 933 | |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 934 | The procedure for deleting a shard: |
| 935 | * Lock all unlocked hosts on that shard. |
| 936 | * Remove shard information . |
| 937 | * Assign a reboot task with highest priority to these hosts. |
| 938 | * Unlock these hosts, then, the reboot tasks run in front of all other |
| 939 | tasks. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 940 | |
| 941 | The status of jobs that haven't been reported to be finished yet, will be |
| 942 | lost. The master scheduler will pick up the jobs and execute them. |
| 943 | |
| 944 | @param hostname: Hostname of the shard to delete. |
| 945 | """ |
| 946 | shard = rpc_utils.retrieve_shard(shard_hostname=hostname) |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 947 | hostnames_to_lock = [h.hostname for h in |
| 948 | models.Host.objects.filter(shard=shard, locked=False)] |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 949 | |
| 950 | # TODO(beeps): Power off shard |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 951 | # For ChromeOS hosts, a reboot test with the highest priority is added to |
| 952 | # the DUT. After a reboot it should be ganranteed that no processes from |
| 953 | # prior tests that were run by a shard are still running on. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 954 | |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 955 | # Lock all unlocked hosts. |
| 956 | dicts = {'locked': True, 'lock_time': datetime.datetime.now()} |
| 957 | models.Host.objects.filter(hostname__in=hostnames_to_lock).update(**dicts) |
| 958 | |
| 959 | # Remove shard information. |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 960 | models.Host.objects.filter(shard=shard).update(shard=None) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 961 | models.Job.objects.filter(shard=shard).update(shard=None) |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 962 | shard.labels.clear() |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 963 | shard.delete() |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 964 | |
xixuan | 03cb93f | 2016-03-22 16:21:41 -0700 | [diff] [blame] | 965 | # Assign a reboot task with highest priority: Super. |
| 966 | t = models.Test.objects.get(name='platform_BootPerfServer:shard') |
| 967 | c = utils.read_file(os.path.join(common.autotest_dir, t.path)) |
| 968 | if hostnames_to_lock: |
| 969 | rpc_utils.create_job_common( |
| 970 | 'reboot_dut_for_shard_deletion', |
| 971 | priority=priorities.Priority.SUPER, |
| 972 | control_type='Server', |
| 973 | control_file=c, hosts=hostnames_to_lock) |
| 974 | |
| 975 | # Unlock these shard-related hosts. |
| 976 | dicts = {'locked': False, 'lock_time': None} |
| 977 | models.Host.objects.filter(hostname__in=hostnames_to_lock).update(**dicts) |
| 978 | |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 979 | |
MK Ryu | a34e3b1 | 2015-08-21 16:20:47 -0700 | [diff] [blame] | 980 | def get_servers(hostname=None, role=None, status=None): |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 981 | """Get a list of servers with matching role and status. |
| 982 | |
MK Ryu | a34e3b1 | 2015-08-21 16:20:47 -0700 | [diff] [blame] | 983 | @param hostname: FQDN of the server. |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 984 | @param role: Name of the server role, e.g., drone, scheduler. Default to |
| 985 | None to match any role. |
| 986 | @param status: Status of the server, e.g., primary, backup, repair_required. |
| 987 | Default to None to match any server status. |
| 988 | |
| 989 | @raises error.RPCException: If server database is not used. |
| 990 | @return: A list of server names for servers with matching role and status. |
| 991 | """ |
| 992 | if not server_manager_utils.use_server_db(): |
| 993 | raise error.RPCException('Server database is not enabled. Please try ' |
| 994 | 'retrieve servers from global config.') |
MK Ryu | a34e3b1 | 2015-08-21 16:20:47 -0700 | [diff] [blame] | 995 | servers = server_manager_utils.get_servers(hostname=hostname, role=role, |
Dan Shi | d7bb4f1 | 2015-01-06 10:53:50 -0800 | [diff] [blame] | 996 | status=status) |
| 997 | return [s.get_details() for s in servers] |
| 998 | |
| 999 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1000 | @rpc_utils.route_rpc_to_master |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1001 | def get_stable_version(board=stable_version_utils.DEFAULT, android=False): |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 1002 | """Get stable version for the given board. |
| 1003 | |
| 1004 | @param board: Name of the board. |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1005 | @param android: If True, the given board is an Android-based device. If |
| 1006 | False, assume its a Chrome OS-based device. |
| 1007 | |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 1008 | @return: Stable version of the given board. Return global configure value |
| 1009 | of CROS.stable_cros_version if stable_versinos table does not have |
| 1010 | entry of board DEFAULT. |
| 1011 | """ |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1012 | return stable_version_utils.get(board=board, android=android) |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 1013 | |
| 1014 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1015 | @rpc_utils.route_rpc_to_master |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 1016 | def get_all_stable_versions(): |
| 1017 | """Get stable versions for all boards. |
| 1018 | |
| 1019 | @return: A dictionary of board:version. |
| 1020 | """ |
| 1021 | return stable_version_utils.get_all() |
| 1022 | |
| 1023 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1024 | @rpc_utils.route_rpc_to_master |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 1025 | def set_stable_version(version, board=stable_version_utils.DEFAULT): |
| 1026 | """Modify stable version for the given board. |
| 1027 | |
| 1028 | @param version: The new value of stable version for given board. |
| 1029 | @param board: Name of the board, default to value `DEFAULT`. |
| 1030 | """ |
| 1031 | stable_version_utils.set(version=version, board=board) |
| 1032 | |
| 1033 | |
MK Ryu | fbb002c | 2015-06-08 14:13:16 -0700 | [diff] [blame] | 1034 | @rpc_utils.route_rpc_to_master |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 1035 | def delete_stable_version(board): |
| 1036 | """Modify stable version for the given board. |
| 1037 | |
| 1038 | Delete a stable version entry in afe_stable_versions table for a given |
| 1039 | board, so default stable version will be used. |
| 1040 | |
| 1041 | @param board: Name of the board. |
| 1042 | """ |
| 1043 | stable_version_utils.delete(board=board) |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1044 | |
| 1045 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 1046 | def _initialize_control_file_getter(build): |
| 1047 | """Get the remote control file getter. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1048 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 1049 | @param build: unique name by which to refer to a remote build image. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1050 | |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 1051 | @return: A control file getter object. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1052 | """ |
| 1053 | # Stage the test artifacts. |
| 1054 | try: |
| 1055 | ds = dev_server.ImageServer.resolve(build) |
| 1056 | build = ds.translate(build) |
| 1057 | except dev_server.DevServerException as e: |
| 1058 | raise ValueError('Could not resolve build %s: %s' % (build, e)) |
| 1059 | |
| 1060 | try: |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1061 | ds.stage_artifacts(image=build, artifacts=['test_suites']) |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1062 | except dev_server.DevServerException as e: |
| 1063 | raise error.StageControlFileFailure( |
| 1064 | 'Failed to stage %s: %s' % (build, e)) |
| 1065 | |
| 1066 | # Collect the control files specified in this build |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 1067 | return control_file_getter.DevServerGetter.create(build, ds) |
| 1068 | |
| 1069 | |
| 1070 | def get_tests_by_build(build, ignore_invalid_tests=True): |
| 1071 | """Get the tests that are available for the specified build. |
| 1072 | |
| 1073 | @param build: unique name by which to refer to the image. |
| 1074 | @param ignore_invalid_tests: flag on if unparsable tests are ignored. |
| 1075 | |
| 1076 | @return: A sorted list of all tests that are in the build specified. |
| 1077 | """ |
| 1078 | # Collect the control files specified in this build |
| 1079 | cfile_getter = _initialize_control_file_getter(build) |
xixuan | 0f7755d | 2016-04-18 14:49:12 -0700 | [diff] [blame] | 1080 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 1081 | control_file_info_list = cfile_getter.get_suite_info() |
| 1082 | control_file_list = control_file_info_list.keys() |
| 1083 | else: |
| 1084 | control_file_list = cfile_getter.get_control_file_list() |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1085 | |
| 1086 | test_objects = [] |
| 1087 | _id = 0 |
| 1088 | for control_file_path in control_file_list: |
| 1089 | # Read and parse the control file |
xixuan | 0f7755d | 2016-04-18 14:49:12 -0700 | [diff] [blame] | 1090 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 1091 | control_file = control_file_info_list[control_file_path] |
| 1092 | else: |
| 1093 | control_file = cfile_getter.get_control_file_contents( |
| 1094 | control_file_path) |
Michael Tang | 340efe3 | 2016-04-16 12:15:17 -0700 | [diff] [blame] | 1095 | try: |
| 1096 | control_obj = control_data.parse_control_string(control_file) |
| 1097 | except: |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 1098 | logging.info('Failed to parse control file: %s', control_file_path) |
Michael Tang | 340efe3 | 2016-04-16 12:15:17 -0700 | [diff] [blame] | 1099 | if not ignore_invalid_tests: |
| 1100 | raise |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1101 | |
| 1102 | # Extract the values needed for the AFE from the control_obj. |
| 1103 | # The keys list represents attributes in the control_obj that |
| 1104 | # are required by the AFE |
| 1105 | keys = ['author', 'doc', 'name', 'time', 'test_type', 'experimental', |
| 1106 | 'test_category', 'test_class', 'dependencies', 'run_verify', |
| 1107 | 'sync_count', 'job_retries', 'retries', 'path'] |
| 1108 | |
| 1109 | test_object = {} |
| 1110 | for key in keys: |
| 1111 | test_object[key] = getattr(control_obj, key) if hasattr( |
| 1112 | control_obj, key) else '' |
| 1113 | |
| 1114 | # Unfortunately, the AFE expects different key-names for certain |
| 1115 | # values, these must be corrected to avoid the risk of tests |
| 1116 | # being omitted by the AFE. |
| 1117 | # The 'id' is an additional value used in the AFE. |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 1118 | # The control_data parsing does not reference 'run_reset', but it |
| 1119 | # is also used in the AFE and defaults to True. |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1120 | test_object['id'] = _id |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 1121 | test_object['run_reset'] = True |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1122 | test_object['description'] = test_object.get('doc', '') |
| 1123 | test_object['test_time'] = test_object.get('time', 0) |
| 1124 | test_object['test_retry'] = test_object.get('retries', 0) |
| 1125 | |
| 1126 | # Fix the test name to be consistent with the current presentation |
| 1127 | # of test names in the AFE. |
| 1128 | testpath, subname = os.path.split(control_file_path) |
| 1129 | testname = os.path.basename(testpath) |
| 1130 | subname = subname.split('.')[1:] |
| 1131 | if subname: |
| 1132 | testname = '%s:%s' % (testname, ':'.join(subname)) |
| 1133 | |
| 1134 | test_object['name'] = testname |
| 1135 | |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 1136 | # Correct the test path as parse_control_string sets an empty string. |
| 1137 | test_object['path'] = control_file_path |
| 1138 | |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1139 | _id += 1 |
| 1140 | test_objects.append(test_object) |
| 1141 | |
Matthew Sartori | 1043809 | 2015-06-24 14:30:18 -0700 | [diff] [blame] | 1142 | test_objects = sorted(test_objects, key=lambda x: x.get('name')) |
Matthew Sartori | d96fb9b | 2015-05-19 18:04:58 -0700 | [diff] [blame] | 1143 | return rpc_utils.prepare_for_serialization(test_objects) |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 1144 | |
| 1145 | |
| 1146 | def get_test_control_files_by_build(tests, build, ignore_invalid_tests=False): |
| 1147 | """Get the test control files that are available for the specified build. |
| 1148 | |
| 1149 | @param tests A sequence of test objects to run. |
| 1150 | @param build: unique name by which to refer to the image. |
| 1151 | @param ignore_invalid_tests: flag on if unparsable tests are ignored. |
| 1152 | |
| 1153 | @return: A sorted list of all tests that are in the build specified. |
| 1154 | """ |
| 1155 | raw_control_files = [] |
| 1156 | # shortcut to avoid staging the image. |
| 1157 | if not tests: |
| 1158 | return raw_control_files |
| 1159 | |
| 1160 | cfile_getter = _initialize_control_file_getter(build) |
| 1161 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 1162 | control_file_info_list = cfile_getter.get_suite_info() |
| 1163 | |
| 1164 | for test in tests: |
| 1165 | # Read and parse the control file |
| 1166 | if SuiteBase.ENABLE_CONTROLS_IN_BATCH: |
| 1167 | control_file = control_file_info_list[test.path] |
| 1168 | else: |
| 1169 | control_file = cfile_getter.get_control_file_contents( |
| 1170 | test.path) |
| 1171 | raw_control_files.append(control_file) |
| 1172 | return raw_control_files |