[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2013 The Chromium 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 | """Provisions Android devices with settings required for bots. |
| 8 | |
| 9 | Usage: |
| 10 | ./provision_devices.py [-d <device serial number>] |
| 11 | """ |
| 12 | |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 13 | import argparse |
[email protected] | 552df57 | 2014-02-26 20:31:35 | [diff] [blame] | 14 | import logging |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 15 | import os |
navabi | 00c5fd3 | 2015-02-26 22:06:47 | [diff] [blame] | 16 | import posixpath |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 17 | import re |
| 18 | import subprocess |
| 19 | import sys |
| 20 | import time |
| 21 | |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 22 | from pylib import constants |
[email protected] | 552df57 | 2014-02-26 20:31:35 | [diff] [blame] | 23 | from pylib import device_settings |
jbudorick | bfffb22e | 2015-04-16 14:10:02 | [diff] [blame^] | 24 | from pylib.device import adb_wrapper |
rnephew | 58368d3e | 2015-04-06 20:02:38 | [diff] [blame] | 25 | from pylib.device import battery_utils |
[email protected] | 021265ed | 2014-08-07 00:09:07 | [diff] [blame] | 26 | from pylib.device import device_blacklist |
[email protected] | 402e54cd | 2014-06-25 05:09:06 | [diff] [blame] | 27 | from pylib.device import device_errors |
jbudorick | bfffb22e | 2015-04-16 14:10:02 | [diff] [blame^] | 28 | from pylib.device import device_filter |
[email protected] | 044d79b | 2014-04-10 19:37:30 | [diff] [blame] | 29 | from pylib.device import device_utils |
[email protected] | e30597d4 | 2014-08-16 08:21:34 | [diff] [blame] | 30 | from pylib.utils import run_tests_helper |
jbudorick | d3f50b0 | 2015-02-24 18:52:03 | [diff] [blame] | 31 | from pylib.utils import timeout_retry |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 32 | |
[email protected] | 11ef9c0 | 2014-05-22 11:13:40 | [diff] [blame] | 33 | sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, |
| 34 | 'third_party', 'android_testrunner')) |
| 35 | import errors |
| 36 | |
jbudorick | b22b962 | 2015-01-29 00:26:21 | [diff] [blame] | 37 | |
| 38 | class _DEFAULT_TIMEOUTS(object): |
| 39 | # L can take a while to reboot after a wipe. |
| 40 | LOLLIPOP = 600 |
| 41 | PRE_LOLLIPOP = 180 |
| 42 | |
| 43 | HELP_TEXT = '{}s on L, {}s on pre-L'.format(LOLLIPOP, PRE_LOLLIPOP) |
| 44 | |
| 45 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 46 | class _PHASES(object): |
| 47 | WIPE = 'wipe' |
| 48 | PROPERTIES = 'properties' |
| 49 | FINISH = 'finish' |
| 50 | |
| 51 | ALL = [WIPE, PROPERTIES, FINISH] |
[email protected] | 7849a33 | 2013-07-12 01:40:09 | [diff] [blame] | 52 | |
| 53 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 54 | def ProvisionDevices(options): |
| 55 | if options.device is not None: |
| 56 | devices = [options.device] |
| 57 | else: |
jbudorick | bfffb22e | 2015-04-16 14:10:02 | [diff] [blame^] | 58 | devices = adb_wrapper.AdbWrapper.Devices( |
| 59 | filters=device_filter.DefaultFilters()) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 60 | |
| 61 | parallel_devices = device_utils.DeviceUtils.parallel(devices) |
| 62 | parallel_devices.pMap(ProvisionDevice, options) |
| 63 | if options.auto_reconnect: |
| 64 | _LaunchHostHeartbeat() |
| 65 | blacklist = device_blacklist.ReadBlacklist() |
| 66 | if all(d in blacklist for d in devices): |
| 67 | raise device_errors.NoDevicesError |
| 68 | return 0 |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 69 | |
| 70 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 71 | def ProvisionDevice(device, options): |
| 72 | if options.reboot_timeout: |
| 73 | reboot_timeout = options.reboot_timeout |
| 74 | elif (device.build_version_sdk >= |
| 75 | constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
| 76 | reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP |
| 77 | else: |
| 78 | reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP |
| 79 | |
| 80 | def should_run_phase(phase_name): |
| 81 | return not options.phases or phase_name in options.phases |
| 82 | |
| 83 | def run_phase(phase_func, reboot=True): |
| 84 | device.WaitUntilFullyBooted(timeout=reboot_timeout) |
| 85 | phase_func(device, options) |
| 86 | if reboot: |
| 87 | device.Reboot(False, retries=0) |
| 88 | device.adb.WaitForDevice() |
| 89 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 90 | try: |
| 91 | if should_run_phase(_PHASES.WIPE): |
| 92 | run_phase(WipeDevice) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 93 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 94 | if should_run_phase(_PHASES.PROPERTIES): |
| 95 | run_phase(SetProperties) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 96 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 97 | if should_run_phase(_PHASES.FINISH): |
| 98 | run_phase(FinishProvisioning, reboot=False) |
| 99 | |
| 100 | except (errors.WaitForResponseTimedOutError, |
| 101 | device_errors.CommandTimeoutError): |
| 102 | logging.exception('Timed out waiting for device %s. Adding to blacklist.', |
| 103 | str(device)) |
| 104 | device_blacklist.ExtendBlacklist([str(device)]) |
| 105 | |
| 106 | except device_errors.CommandFailedError: |
| 107 | logging.exception('Failed to provision device %s. Adding to blacklist.', |
| 108 | str(device)) |
| 109 | device_blacklist.ExtendBlacklist([str(device)]) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 110 | |
| 111 | |
| 112 | def WipeDevice(device, options): |
| 113 | """Wipes data from device, keeping only the adb_keys for authorization. |
| 114 | |
| 115 | After wiping data on a device that has been authorized, adb can still |
| 116 | communicate with the device, but after reboot the device will need to be |
| 117 | re-authorized because the adb keys file is stored in /data/misc/adb/. |
| 118 | Thus, adb_keys file is rewritten so the device does not need to be |
| 119 | re-authorized. |
| 120 | |
| 121 | Arguments: |
| 122 | device: the device to wipe |
| 123 | """ |
| 124 | if options.skip_wipe: |
| 125 | return |
| 126 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 127 | try: |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 128 | device.EnableRoot() |
| 129 | device_authorized = device.FileExists(constants.ADB_KEYS_FILE) |
| 130 | if device_authorized: |
| 131 | adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, |
| 132 | as_root=True).splitlines() |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 133 | device.RunShellCommand(['wipe', 'data'], |
jbudorick | 809d9aff | 2015-04-09 16:45:29 | [diff] [blame] | 134 | as_root=True, check_return=True) |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 135 | device.adb.WaitForDevice() |
| 136 | |
| 137 | if device_authorized: |
| 138 | adb_keys_set = set(adb_keys) |
| 139 | for adb_key_file in options.adb_key_files or []: |
| 140 | try: |
| 141 | with open(adb_key_file, 'r') as f: |
| 142 | adb_public_keys = f.readlines() |
| 143 | adb_keys_set.update(adb_public_keys) |
| 144 | except IOError: |
| 145 | logging.warning('Unable to find adb keys file %s.' % adb_key_file) |
| 146 | _WriteAdbKeysFile(device, '\n'.join(adb_keys_set)) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 147 | except device_errors.CommandFailedError: |
| 148 | logging.exception('Possible failure while wiping the device. ' |
| 149 | 'Attempting to continue.') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 150 | |
| 151 | |
| 152 | def _WriteAdbKeysFile(device, adb_keys_string): |
| 153 | dir_path = posixpath.dirname(constants.ADB_KEYS_FILE) |
| 154 | device.RunShellCommand(['mkdir', '-p', dir_path], |
| 155 | as_root=True, check_return=True) |
| 156 | device.RunShellCommand(['restorecon', dir_path], |
| 157 | as_root=True, check_return=True) |
| 158 | device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True) |
| 159 | device.RunShellCommand(['restorecon', constants.ADB_KEYS_FILE], |
| 160 | as_root=True, check_return=True) |
| 161 | |
| 162 | |
| 163 | def SetProperties(device, options): |
| 164 | try: |
| 165 | device.EnableRoot() |
| 166 | except device_errors.CommandFailedError as e: |
| 167 | logging.warning(str(e)) |
| 168 | |
| 169 | _ConfigureLocalProperties(device, options.enable_java_debug) |
| 170 | device_settings.ConfigureContentSettings( |
| 171 | device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) |
| 172 | if options.disable_location: |
| 173 | device_settings.ConfigureContentSettings( |
| 174 | device, device_settings.DISABLE_LOCATION_SETTINGS) |
| 175 | else: |
| 176 | device_settings.ConfigureContentSettings( |
| 177 | device, device_settings.ENABLE_LOCATION_SETTINGS) |
| 178 | device_settings.SetLockScreenSettings(device) |
| 179 | if options.disable_network: |
| 180 | device_settings.ConfigureContentSettings( |
| 181 | device, device_settings.NETWORK_DISABLED_SETTINGS) |
| 182 | |
| 183 | if options.min_battery_level is not None: |
| 184 | try: |
| 185 | battery = battery_utils.BatteryUtils(device) |
| 186 | battery.ChargeDeviceToLevel(options.min_battery_level) |
| 187 | except device_errors.CommandFailedError as e: |
| 188 | logging.exception('Unable to charge device to specified level.') |
| 189 | |
| 190 | |
| 191 | def _ConfigureLocalProperties(device, java_debug=True): |
| 192 | """Set standard readonly testing device properties prior to reboot.""" |
| 193 | local_props = [ |
| 194 | 'persist.sys.usb.config=adb', |
| 195 | 'ro.monkey=1', |
| 196 | 'ro.test_harness=1', |
| 197 | 'ro.audio.silent=1', |
| 198 | 'ro.setupwizard.mode=DISABLED', |
| 199 | ] |
| 200 | if java_debug: |
jbudorick | bfffb22e | 2015-04-16 14:10:02 | [diff] [blame^] | 201 | local_props.append( |
| 202 | '%s=all' % device_utils.DeviceUtils.JAVA_ASSERT_PROPERTY) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 203 | local_props.append('debug.checkjni=1') |
| 204 | try: |
| 205 | device.WriteFile( |
| 206 | constants.DEVICE_LOCAL_PROPERTIES_PATH, |
| 207 | '\n'.join(local_props), as_root=True) |
| 208 | # Android will not respect the local props file if it is world writable. |
| 209 | device.RunShellCommand( |
| 210 | ['chmod', '644', constants.DEVICE_LOCAL_PROPERTIES_PATH], |
| 211 | as_root=True, check_return=True) |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 212 | except device_errors.CommandFailedError: |
| 213 | logging.exception('Failed to configure local properties.') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 214 | |
| 215 | |
| 216 | def FinishProvisioning(device, options): |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 217 | device.RunShellCommand( |
| 218 | ['date', '-s', time.strftime('%Y%m%d.%H%M%S', time.gmtime())], |
| 219 | as_root=True, check_return=True) |
| 220 | props = device.RunShellCommand('getprop', check_return=True) |
| 221 | for prop in props: |
| 222 | logging.info(' %s' % prop) |
| 223 | if options.auto_reconnect: |
| 224 | _PushAndLaunchAdbReboot(device, options.target) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 225 | |
| 226 | |
| 227 | def _PushAndLaunchAdbReboot(device, target): |
[email protected] | c89aae0 | 2013-04-06 00:25:19 | [diff] [blame] | 228 | """Pushes and launches the adb_reboot binary on the device. |
| 229 | |
| 230 | Arguments: |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 231 | device: The DeviceUtils instance for the device to which the adb_reboot |
| 232 | binary should be pushed. |
| 233 | target: The build target (example, Debug or Release) which helps in |
| 234 | locating the adb_reboot binary. |
[email protected] | c89aae0 | 2013-04-06 00:25:19 | [diff] [blame] | 235 | """ |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 236 | logging.info('Will push and launch adb_reboot on %s' % str(device)) |
| 237 | # Kill if adb_reboot is already running. |
perezju | 84436c8 | 2015-04-15 10:19:22 | [diff] [blame] | 238 | device.KillAll('adb_reboot', blocking=True, timeout=2, quiet=True) |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 239 | # Push adb_reboot |
| 240 | logging.info(' Pushing adb_reboot ...') |
| 241 | adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT, |
| 242 | 'out/%s/adb_reboot' % target) |
jbudorick | 8c07d89 | 2014-10-13 11:39:48 | [diff] [blame] | 243 | device.PushChangedFiles([(adb_reboot, '/data/local/tmp/')]) |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 244 | # Launch adb_reboot |
| 245 | logging.info(' Launching adb_reboot ...') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 246 | device.RunShellCommand( |
| 247 | [device.GetDevicePieWrapper(), '/data/local/tmp/adb_reboot'], |
| 248 | check_return=True) |
[email protected] | c89aae0 | 2013-04-06 00:25:19 | [diff] [blame] | 249 | |
| 250 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 251 | def _LaunchHostHeartbeat(): |
| 252 | # Kill if existing host_heartbeat |
jbudorick | 809d9aff | 2015-04-09 16:45:29 | [diff] [blame] | 253 | KillHostHeartbeat() |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 254 | # Launch a new host_heartbeat |
| 255 | logging.info('Spawning host heartbeat...') |
| 256 | subprocess.Popen([os.path.join(constants.DIR_SOURCE_ROOT, |
| 257 | 'build/android/host_heartbeat.py')]) |
navabi | 00c5fd3 | 2015-02-26 22:06:47 | [diff] [blame] | 258 | |
[email protected] | 552df57 | 2014-02-26 20:31:35 | [diff] [blame] | 259 | |
jbudorick | 809d9aff | 2015-04-09 16:45:29 | [diff] [blame] | 260 | def KillHostHeartbeat(): |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 261 | ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE) |
| 262 | stdout, _ = ps.communicate() |
| 263 | matches = re.findall('\\n.*host_heartbeat.*', stdout) |
| 264 | for match in matches: |
| 265 | logging.info('An instance of host heart beart running... will kill') |
| 266 | pid = re.findall(r'(\S+)', match)[1] |
| 267 | subprocess.call(['kill', str(pid)]) |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 268 | |
| 269 | |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 270 | def main(): |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 271 | # Recommended options on perf bots: |
| 272 | # --disable-network |
| 273 | # TODO(tonyg): We eventually want network on. However, currently radios |
| 274 | # can cause perfbots to drain faster than they charge. |
perezju | 545a9ce | 2015-02-19 13:14:20 | [diff] [blame] | 275 | # --min-battery-level 95 |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 276 | # Some perf bots run benchmarks with USB charging disabled which leads |
| 277 | # to gradual draining of the battery. We must wait for a full charge |
| 278 | # before starting a run in order to keep the devices online. |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 279 | |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 280 | parser = argparse.ArgumentParser( |
| 281 | description='Provision Android devices with settings required for bots.') |
| 282 | parser.add_argument('-d', '--device', metavar='SERIAL', |
| 283 | help='the serial number of the device to be provisioned' |
| 284 | ' (the default is to provision all devices attached)') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 285 | parser.add_argument('--phase', action='append', choices=_PHASES.ALL, |
| 286 | dest='phases', |
| 287 | help='Phases of provisioning to run. ' |
| 288 | '(If omitted, all phases will be run.)') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 289 | parser.add_argument('--skip-wipe', action='store_true', default=False, |
| 290 | help="don't wipe device data during provisioning") |
jbudorick | b22b962 | 2015-01-29 00:26:21 | [diff] [blame] | 291 | parser.add_argument('--reboot-timeout', metavar='SECS', type=int, |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 292 | help='when wiping the device, max number of seconds to' |
jbudorick | b22b962 | 2015-01-29 00:26:21 | [diff] [blame] | 293 | ' wait after each reboot ' |
| 294 | '(default: %s)' % _DEFAULT_TIMEOUTS.HELP_TEXT) |
perezju | 545a9ce | 2015-02-19 13:14:20 | [diff] [blame] | 295 | parser.add_argument('--min-battery-level', type=int, metavar='NUM', |
| 296 | help='wait for the device to reach this minimum battery' |
| 297 | ' level before trying to continue') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 298 | parser.add_argument('--disable-location', action='store_true', |
| 299 | help='disable Google location services on devices') |
| 300 | parser.add_argument('--disable-network', action='store_true', |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 301 | help='disable network access on devices') |
| 302 | parser.add_argument('--disable-java-debug', action='store_false', |
perezju | 545a9ce | 2015-02-19 13:14:20 | [diff] [blame] | 303 | dest='enable_java_debug', default=True, |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 304 | help='disable Java property asserts and JNI checking') |
| 305 | parser.add_argument('-t', '--target', default='Debug', |
| 306 | help='the build target (default: %(default)s)') |
| 307 | parser.add_argument('-r', '--auto-reconnect', action='store_true', |
| 308 | help='push binary which will reboot the device on adb' |
| 309 | ' disconnections') |
navabi | 00c5fd3 | 2015-02-26 22:06:47 | [diff] [blame] | 310 | parser.add_argument('--adb-key-files', type=str, nargs='+', |
| 311 | help='list of adb keys to push to device') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 312 | parser.add_argument('-v', '--verbose', action='count', default=1, |
| 313 | help='Log more information.') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 314 | args = parser.parse_args() |
| 315 | constants.SetBuildType(args.target) |
| 316 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 317 | run_tests_helper.SetLogLevel(args.verbose) |
| 318 | |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 319 | return ProvisionDevices(args) |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 320 | |
| 321 | |
| 322 | if __name__ == '__main__': |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 323 | sys.exit(main()) |