Peter Wen | b51e454 | 2021-06-30 17:42:57 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 2 | # |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 3 | # Copyright 2013 The Chromium Authors |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 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 |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 14 | import datetime |
mikecase | eb1cfbbe | 2015-06-29 23:31:20 | [diff] [blame] | 15 | import json |
[email protected] | 552df57 | 2014-02-26 20:31:35 | [diff] [blame] | 16 | import logging |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 17 | import os |
navabi | 00c5fd3 | 2015-02-26 22:06:47 | [diff] [blame] | 18 | import posixpath |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 19 | import re |
| 20 | import subprocess |
| 21 | import sys |
| 22 | import time |
| 23 | |
stip | dfb2d01 | 2016-02-17 18:41:41 | [diff] [blame] | 24 | # Import _strptime before threaded code. datetime.datetime.strptime is |
| 25 | # threadsafe except for the initial import of the _strptime module. |
| 26 | # See crbug.com/584730 and https://bugs.python.org/issue7980. |
| 27 | import _strptime # pylint: disable=unused-import |
| 28 | |
jbudorick | 0c2a94a | 2015-12-04 14:27:43 | [diff] [blame] | 29 | import devil_chromium |
jbudorick | 06162944 | 2015-09-03 18:00:57 | [diff] [blame] | 30 | from devil.android import battery_utils |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 31 | from devil.android import device_denylist |
jbudorick | 06162944 | 2015-09-03 18:00:57 | [diff] [blame] | 32 | from devil.android import device_errors |
rnephew | 4597a22 | 2015-09-28 20:59:57 | [diff] [blame] | 33 | from devil.android import device_temp_file |
jbudorick | 06162944 | 2015-09-03 18:00:57 | [diff] [blame] | 34 | from devil.android import device_utils |
bpastene | 79b8679 | 2015-12-10 15:28:58 | [diff] [blame] | 35 | from devil.android.sdk import keyevent |
jbudorick | d292c5b | 2015-09-03 00:40:21 | [diff] [blame] | 36 | from devil.android.sdk import version_codes |
jbudorick | 78e09f91 | 2016-05-16 18:56:15 | [diff] [blame] | 37 | from devil.constants import exit_codes |
jbudorick | 06162944 | 2015-09-03 18:00:57 | [diff] [blame] | 38 | from devil.utils import run_tests_helper |
| 39 | from devil.utils import timeout_retry |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 40 | from pylib import constants |
[email protected] | 552df57 | 2014-02-26 20:31:35 | [diff] [blame] | 41 | from pylib import device_settings |
jbudorick | d28554a | 2016-01-11 16:22:59 | [diff] [blame] | 42 | from pylib.constants import host_paths |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 43 | |
perezju | 39b8fbd6 | 2015-07-17 11:06:39 | [diff] [blame] | 44 | _SYSTEM_WEBVIEW_PATHS = ['/system/app/webview', '/system/app/WebViewGoogle'] |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 45 | _CHROME_PACKAGE_REGEX = re.compile('.*chrom.*') |
| 46 | _TOMBSTONE_REGEX = re.compile('tombstone.*') |
perezju | 39b8fbd6 | 2015-07-17 11:06:39 | [diff] [blame] | 47 | |
| 48 | |
Yoshisato Yanagisawa | 9f47779 | 2021-12-09 00:00:25 | [diff] [blame] | 49 | class _DEFAULT_TIMEOUTS: |
jbudorick | b22b962 | 2015-01-29 00:26:21 | [diff] [blame] | 50 | # L can take a while to reboot after a wipe. |
| 51 | LOLLIPOP = 600 |
| 52 | PRE_LOLLIPOP = 180 |
| 53 | |
| 54 | HELP_TEXT = '{}s on L, {}s on pre-L'.format(LOLLIPOP, PRE_LOLLIPOP) |
| 55 | |
| 56 | |
Yoshisato Yanagisawa | 9f47779 | 2021-12-09 00:00:25 | [diff] [blame] | 57 | class _PHASES: |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 58 | WIPE = 'wipe' |
| 59 | PROPERTIES = 'properties' |
| 60 | FINISH = 'finish' |
| 61 | |
| 62 | ALL = [WIPE, PROPERTIES, FINISH] |
[email protected] | 7849a33 | 2013-07-12 01:40:09 | [diff] [blame] | 63 | |
| 64 | |
jbudorick | dde688fb | 2015-08-27 03:00:17 | [diff] [blame] | 65 | def ProvisionDevices(args): |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 66 | denylist = (device_denylist.Denylist(args.denylist_file) |
| 67 | if args.denylist_file else None) |
| 68 | devices = [ |
| 69 | d for d in device_utils.DeviceUtils.HealthyDevices(denylist) |
Nate Fischer | 1da733c9 | 2021-10-04 19:06:18 | [diff] [blame] | 70 | if not args.emulators or d.is_emulator |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 71 | ] |
jbudorick | dde688fb | 2015-08-27 03:00:17 | [diff] [blame] | 72 | if args.device: |
| 73 | devices = [d for d in devices if d == args.device] |
yolandyan | 140462c | 2015-11-07 02:47:06 | [diff] [blame] | 74 | if not devices: |
| 75 | raise device_errors.DeviceUnreachableError(args.device) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 76 | parallel_devices = device_utils.DeviceUtils.parallel(devices) |
yolandyan | 140462c | 2015-11-07 02:47:06 | [diff] [blame] | 77 | if args.emulators: |
| 78 | parallel_devices.pMap(SetProperties, args) |
| 79 | else: |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 80 | parallel_devices.pMap(ProvisionDevice, denylist, args) |
jbudorick | dde688fb | 2015-08-27 03:00:17 | [diff] [blame] | 81 | if args.auto_reconnect: |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 82 | _LaunchHostHeartbeat() |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 83 | denylisted_devices = denylist.Read() if denylist else [] |
| 84 | if args.output_device_denylist: |
| 85 | with open(args.output_device_denylist, 'w') as f: |
| 86 | json.dump(denylisted_devices, f) |
| 87 | if all(d in denylisted_devices for d in devices): |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 88 | raise device_errors.NoDevicesError |
| 89 | return 0 |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 90 | |
| 91 | |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 92 | def ProvisionDevice(device, denylist, options): |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 93 | def should_run_phase(phase_name): |
| 94 | return not options.phases or phase_name in options.phases |
| 95 | |
mikecase | 39419395 | 2016-06-10 21:20:02 | [diff] [blame] | 96 | def run_phase(phase_func, reboot_timeout, reboot=True): |
perezju | 386e022 | 2015-07-16 15:26:01 | [diff] [blame] | 97 | try: |
| 98 | device.WaitUntilFullyBooted(timeout=reboot_timeout, retries=0) |
| 99 | except device_errors.CommandTimeoutError: |
| 100 | logging.error('Device did not finish booting. Will try to reboot.') |
| 101 | device.Reboot(timeout=reboot_timeout) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 102 | phase_func(device, options) |
| 103 | if reboot: |
| 104 | device.Reboot(False, retries=0) |
| 105 | device.adb.WaitForDevice() |
| 106 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 107 | try: |
mikecase | 39419395 | 2016-06-10 21:20:02 | [diff] [blame] | 108 | if options.reboot_timeout: |
| 109 | reboot_timeout = options.reboot_timeout |
| 110 | elif device.build_version_sdk >= version_codes.LOLLIPOP: |
| 111 | reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP |
| 112 | else: |
| 113 | reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP |
| 114 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 115 | if should_run_phase(_PHASES.WIPE): |
bpastene | 79b8679 | 2015-12-10 15:28:58 | [diff] [blame] | 116 | if (options.chrome_specific_wipe or device.IsUserBuild() or |
| 117 | device.build_version_sdk >= version_codes.MARSHMALLOW): |
mikecase | 39419395 | 2016-06-10 21:20:02 | [diff] [blame] | 118 | run_phase(WipeChromeData, reboot_timeout) |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 119 | else: |
mikecase | 39419395 | 2016-06-10 21:20:02 | [diff] [blame] | 120 | run_phase(WipeDevice, reboot_timeout) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 121 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 122 | if should_run_phase(_PHASES.PROPERTIES): |
mikecase | 39419395 | 2016-06-10 21:20:02 | [diff] [blame] | 123 | run_phase(SetProperties, reboot_timeout) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 124 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 125 | if should_run_phase(_PHASES.FINISH): |
mikecase | 39419395 | 2016-06-10 21:20:02 | [diff] [blame] | 126 | run_phase(FinishProvisioning, reboot_timeout, reboot=False) |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 127 | |
mlliu | 42dd169 | 2015-09-16 02:16:03 | [diff] [blame] | 128 | if options.chrome_specific_wipe: |
| 129 | package = "com.google.android.gms" |
| 130 | version_name = device.GetApplicationVersion(package) |
| 131 | logging.info("Version name for %s is %s", package, version_name) |
| 132 | |
rnephew | 952bc2a | 2015-10-10 02:27:11 | [diff] [blame] | 133 | CheckExternalStorage(device) |
| 134 | |
jbudorick | cd661e4f | 2015-08-13 19:33:54 | [diff] [blame] | 135 | except device_errors.CommandTimeoutError: |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 136 | logging.exception('Timed out waiting for device %s. Adding to denylist.', |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 137 | str(device)) |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 138 | if denylist: |
| 139 | denylist.Extend([str(device)], reason='provision_timeout') |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 140 | |
perezju | 46865bb3 | 2017-04-13 12:19:51 | [diff] [blame] | 141 | except (device_errors.CommandFailedError, |
| 142 | device_errors.DeviceUnreachableError): |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 143 | logging.exception('Failed to provision device %s. Adding to denylist.', |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 144 | str(device)) |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 145 | if denylist: |
| 146 | denylist.Extend([str(device)], reason='provision_failure') |
| 147 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 148 | |
rnephew | 4597a22 | 2015-09-28 20:59:57 | [diff] [blame] | 149 | def CheckExternalStorage(device): |
| 150 | """Checks that storage is writable and if not makes it writable. |
| 151 | |
| 152 | Arguments: |
| 153 | device: The device to check. |
| 154 | """ |
| 155 | try: |
| 156 | with device_temp_file.DeviceTempFile( |
agrieve | fc5b96fe | 2015-10-16 15:34:08 | [diff] [blame] | 157 | device.adb, suffix='.sh', dir=device.GetExternalStoragePath()) as f: |
| 158 | device.WriteFile(f.name, 'test') |
rnephew | 4597a22 | 2015-09-28 20:59:57 | [diff] [blame] | 159 | except device_errors.CommandFailedError: |
| 160 | logging.info('External storage not writable. Remounting / as RW') |
| 161 | device.RunShellCommand(['mount', '-o', 'remount,rw', '/'], |
| 162 | check_return=True, as_root=True) |
rnephew | 952bc2a | 2015-10-10 02:27:11 | [diff] [blame] | 163 | device.EnableRoot() |
rnephew | 4597a22 | 2015-09-28 20:59:57 | [diff] [blame] | 164 | with device_temp_file.DeviceTempFile( |
agrieve | fc5b96fe | 2015-10-16 15:34:08 | [diff] [blame] | 165 | device.adb, suffix='.sh', dir=device.GetExternalStoragePath()) as f: |
| 166 | device.WriteFile(f.name, 'test') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 167 | |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 168 | def WipeChromeData(device, options): |
| 169 | """Wipes chrome specific data from device |
mlliu | 44ebcee | 2015-09-24 07:55:38 | [diff] [blame] | 170 | |
| 171 | (1) uninstall any app whose name matches *chrom*, except |
| 172 | com.android.chrome, which is the chrome stable package. Doing so also |
| 173 | removes the corresponding dirs under /data/data/ and /data/app/ |
| 174 | (2) remove any dir under /data/app-lib/ whose name matches *chrom* |
| 175 | (3) remove any files under /data/tombstones/ whose name matches "tombstone*" |
| 176 | (4) remove /data/local.prop if there is any |
| 177 | (5) remove /data/local/chrome-command-line if there is any |
| 178 | (6) remove anything under /data/local/.config/ if the dir exists |
| 179 | (this is telemetry related) |
| 180 | (7) remove anything under /data/local/tmp/ |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 181 | |
| 182 | Arguments: |
| 183 | device: the device to wipe |
| 184 | """ |
| 185 | if options.skip_wipe: |
| 186 | return |
| 187 | |
| 188 | try: |
bpastene | 79b8679 | 2015-12-10 15:28:58 | [diff] [blame] | 189 | if device.IsUserBuild(): |
| 190 | _UninstallIfMatch(device, _CHROME_PACKAGE_REGEX, |
| 191 | constants.PACKAGE_INFO['chrome_stable'].package) |
| 192 | device.RunShellCommand('rm -rf %s/*' % device.GetExternalStoragePath(), |
| 193 | check_return=True) |
| 194 | device.RunShellCommand('rm -rf /data/local/tmp/*', check_return=True) |
| 195 | else: |
| 196 | device.EnableRoot() |
| 197 | _UninstallIfMatch(device, _CHROME_PACKAGE_REGEX, |
| 198 | constants.PACKAGE_INFO['chrome_stable'].package) |
| 199 | _WipeUnderDirIfMatch(device, '/data/app-lib/', _CHROME_PACKAGE_REGEX) |
| 200 | _WipeUnderDirIfMatch(device, '/data/tombstones/', _TOMBSTONE_REGEX) |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 201 | |
bpastene | 79b8679 | 2015-12-10 15:28:58 | [diff] [blame] | 202 | _WipeFileOrDir(device, '/data/local.prop') |
| 203 | _WipeFileOrDir(device, '/data/local/chrome-command-line') |
| 204 | _WipeFileOrDir(device, '/data/local/.config/') |
| 205 | _WipeFileOrDir(device, '/data/local/tmp/') |
| 206 | device.RunShellCommand('rm -rf %s/*' % device.GetExternalStoragePath(), |
| 207 | check_return=True) |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 208 | except device_errors.CommandFailedError: |
| 209 | logging.exception('Possible failure while wiping the device. ' |
| 210 | 'Attempting to continue.') |
| 211 | |
| 212 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 213 | def WipeDevice(device, options): |
| 214 | """Wipes data from device, keeping only the adb_keys for authorization. |
| 215 | |
| 216 | After wiping data on a device that has been authorized, adb can still |
| 217 | communicate with the device, but after reboot the device will need to be |
| 218 | re-authorized because the adb keys file is stored in /data/misc/adb/. |
| 219 | Thus, adb_keys file is rewritten so the device does not need to be |
| 220 | re-authorized. |
| 221 | |
| 222 | Arguments: |
| 223 | device: the device to wipe |
| 224 | """ |
| 225 | if options.skip_wipe: |
| 226 | return |
| 227 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 228 | try: |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 229 | device.EnableRoot() |
| 230 | device_authorized = device.FileExists(constants.ADB_KEYS_FILE) |
| 231 | if device_authorized: |
| 232 | adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, |
| 233 | as_root=True).splitlines() |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 234 | device.RunShellCommand(['wipe', 'data'], |
jbudorick | 809d9aff | 2015-04-09 16:45:29 | [diff] [blame] | 235 | as_root=True, check_return=True) |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 236 | device.adb.WaitForDevice() |
| 237 | |
| 238 | if device_authorized: |
| 239 | adb_keys_set = set(adb_keys) |
| 240 | for adb_key_file in options.adb_key_files or []: |
| 241 | try: |
| 242 | with open(adb_key_file, 'r') as f: |
| 243 | adb_public_keys = f.readlines() |
| 244 | adb_keys_set.update(adb_public_keys) |
| 245 | except IOError: |
jbudorick | 58b4d36 | 2015-09-08 16:44:59 | [diff] [blame] | 246 | logging.warning('Unable to find adb keys file %s.', adb_key_file) |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 247 | _WriteAdbKeysFile(device, '\n'.join(adb_keys_set)) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 248 | except device_errors.CommandFailedError: |
| 249 | logging.exception('Possible failure while wiping the device. ' |
| 250 | 'Attempting to continue.') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 251 | |
| 252 | |
| 253 | def _WriteAdbKeysFile(device, adb_keys_string): |
| 254 | dir_path = posixpath.dirname(constants.ADB_KEYS_FILE) |
| 255 | device.RunShellCommand(['mkdir', '-p', dir_path], |
| 256 | as_root=True, check_return=True) |
| 257 | device.RunShellCommand(['restorecon', dir_path], |
| 258 | as_root=True, check_return=True) |
| 259 | device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True) |
| 260 | device.RunShellCommand(['restorecon', constants.ADB_KEYS_FILE], |
| 261 | as_root=True, check_return=True) |
| 262 | |
| 263 | |
| 264 | def SetProperties(device, options): |
| 265 | try: |
| 266 | device.EnableRoot() |
| 267 | except device_errors.CommandFailedError as e: |
| 268 | logging.warning(str(e)) |
| 269 | |
bpastene | 79b8679 | 2015-12-10 15:28:58 | [diff] [blame] | 270 | if not device.IsUserBuild(): |
| 271 | _ConfigureLocalProperties(device, options.enable_java_debug) |
| 272 | else: |
| 273 | logging.warning('Cannot configure properties in user builds.') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 274 | device_settings.ConfigureContentSettings( |
| 275 | device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) |
| 276 | if options.disable_location: |
| 277 | device_settings.ConfigureContentSettings( |
| 278 | device, device_settings.DISABLE_LOCATION_SETTINGS) |
| 279 | else: |
| 280 | device_settings.ConfigureContentSettings( |
| 281 | device, device_settings.ENABLE_LOCATION_SETTINGS) |
hush | 252db18 | 2015-07-08 21:42:00 | [diff] [blame] | 282 | |
hush | 4143b708 | 2015-07-09 18:41:11 | [diff] [blame] | 283 | if options.disable_mock_location: |
hush | 252db18 | 2015-07-08 21:42:00 | [diff] [blame] | 284 | device_settings.ConfigureContentSettings( |
| 285 | device, device_settings.DISABLE_MOCK_LOCATION_SETTINGS) |
hush | 4143b708 | 2015-07-09 18:41:11 | [diff] [blame] | 286 | else: |
| 287 | device_settings.ConfigureContentSettings( |
| 288 | device, device_settings.ENABLE_MOCK_LOCATION_SETTINGS) |
hush | 252db18 | 2015-07-08 21:42:00 | [diff] [blame] | 289 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 290 | device_settings.SetLockScreenSettings(device) |
| 291 | if options.disable_network: |
| 292 | device_settings.ConfigureContentSettings( |
| 293 | device, device_settings.NETWORK_DISABLED_SETTINGS) |
perezju | 154947a | 2016-08-08 13:59:47 | [diff] [blame] | 294 | if device.build_version_sdk >= version_codes.MARSHMALLOW: |
| 295 | # Ensure that NFC is also switched off. |
| 296 | device.RunShellCommand(['svc', 'nfc', 'disable'], |
| 297 | as_root=True, check_return=True) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 298 | |
perezju | dcc8680 | 2015-08-25 18:39:39 | [diff] [blame] | 299 | if options.disable_system_chrome: |
| 300 | # The system chrome version on the device interferes with some tests. |
| 301 | device.RunShellCommand(['pm', 'disable', 'com.android.chrome'], |
| 302 | check_return=True) |
| 303 | |
perezju | 39b8fbd6 | 2015-07-17 11:06:39 | [diff] [blame] | 304 | if options.remove_system_webview: |
yolandyan | 161d167a | 2016-05-11 00:52:26 | [diff] [blame] | 305 | if any(device.PathExists(p) for p in _SYSTEM_WEBVIEW_PATHS): |
| 306 | logging.info('System WebView exists and needs to be removed') |
| 307 | if device.HasRoot(): |
| 308 | # Disabled Marshmallow's Verity security feature |
| 309 | if device.build_version_sdk >= version_codes.MARSHMALLOW: |
| 310 | device.adb.DisableVerity() |
| 311 | device.Reboot() |
| 312 | device.WaitUntilFullyBooted() |
| 313 | device.EnableRoot() |
| 314 | |
| 315 | # This is required, e.g., to replace the system webview on a device. |
| 316 | device.adb.Remount() |
| 317 | device.RunShellCommand(['stop'], check_return=True) |
| 318 | device.RunShellCommand(['rm', '-rf'] + _SYSTEM_WEBVIEW_PATHS, |
| 319 | check_return=True) |
| 320 | device.RunShellCommand(['start'], check_return=True) |
| 321 | else: |
| 322 | logging.warning('Cannot remove system webview from a non-rooted device') |
perezju | 39b8fbd6 | 2015-07-17 11:06:39 | [diff] [blame] | 323 | else: |
yolandyan | 161d167a | 2016-05-11 00:52:26 | [diff] [blame] | 324 | logging.info('System WebView already removed') |
perezju | 39b8fbd6 | 2015-07-17 11:06:39 | [diff] [blame] | 325 | |
bpastene | 5c7a190 | 2015-12-14 22:18:09 | [diff] [blame] | 326 | # Some device types can momentarily disappear after setting properties. |
| 327 | device.adb.WaitForDevice() |
| 328 | |
perezju | 39b8fbd6 | 2015-07-17 11:06:39 | [diff] [blame] | 329 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 330 | def _ConfigureLocalProperties(device, java_debug=True): |
| 331 | """Set standard readonly testing device properties prior to reboot.""" |
| 332 | local_props = [ |
| 333 | 'persist.sys.usb.config=adb', |
| 334 | 'ro.monkey=1', |
| 335 | 'ro.test_harness=1', |
| 336 | 'ro.audio.silent=1', |
| 337 | 'ro.setupwizard.mode=DISABLED', |
| 338 | ] |
| 339 | if java_debug: |
jbudorick | bfffb22e | 2015-04-16 14:10:02 | [diff] [blame] | 340 | local_props.append( |
| 341 | '%s=all' % device_utils.DeviceUtils.JAVA_ASSERT_PROPERTY) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 342 | local_props.append('debug.checkjni=1') |
| 343 | try: |
| 344 | device.WriteFile( |
jbudorick | d292c5b | 2015-09-03 00:40:21 | [diff] [blame] | 345 | device.LOCAL_PROPERTIES_PATH, |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 346 | '\n'.join(local_props), as_root=True) |
| 347 | # Android will not respect the local props file if it is world writable. |
| 348 | device.RunShellCommand( |
jbudorick | d292c5b | 2015-09-03 00:40:21 | [diff] [blame] | 349 | ['chmod', '644', device.LOCAL_PROPERTIES_PATH], |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 350 | as_root=True, check_return=True) |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 351 | except device_errors.CommandFailedError: |
| 352 | logging.exception('Failed to configure local properties.') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 353 | |
| 354 | |
| 355 | def FinishProvisioning(device, options): |
bpastene | 79b8679 | 2015-12-10 15:28:58 | [diff] [blame] | 356 | # The lockscreen can't be disabled on user builds, so send a keyevent |
| 357 | # to unlock it. |
| 358 | if device.IsUserBuild(): |
| 359 | device.SendKeyEvent(keyevent.KEYCODE_MENU) |
| 360 | |
jbudorick | f9f6af8 | 2015-07-01 17:24:54 | [diff] [blame] | 361 | if options.min_battery_level is not None: |
rnephew | 0204d42 | 2016-02-11 21:46:36 | [diff] [blame] | 362 | battery = battery_utils.BatteryUtils(device) |
jbudorick | f9f6af8 | 2015-07-01 17:24:54 | [diff] [blame] | 363 | try: |
jbudorick | f9f6af8 | 2015-07-01 17:24:54 | [diff] [blame] | 364 | battery.ChargeDeviceToLevel(options.min_battery_level) |
rnephew | 0204d42 | 2016-02-11 21:46:36 | [diff] [blame] | 365 | except device_errors.DeviceChargingError: |
| 366 | device.Reboot() |
| 367 | battery.ChargeDeviceToLevel(options.min_battery_level) |
jbudorick | f9f6af8 | 2015-07-01 17:24:54 | [diff] [blame] | 368 | |
| 369 | if options.max_battery_temp is not None: |
| 370 | try: |
| 371 | battery = battery_utils.BatteryUtils(device) |
| 372 | battery.LetBatteryCoolToTemperature(options.max_battery_temp) |
| 373 | except device_errors.CommandFailedError: |
| 374 | logging.exception('Unable to let battery cool to specified temperature.') |
| 375 | |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 376 | def _set_and_verify_date(): |
jbudorick | 58b4d36 | 2015-09-08 16:44:59 | [diff] [blame] | 377 | if device.build_version_sdk >= version_codes.MARSHMALLOW: |
jbudorick | 7f5231e | 2015-08-27 23:02:39 | [diff] [blame] | 378 | date_format = '%m%d%H%M%Y.%S' |
jbudorick | 24b85fd | 2016-05-14 00:05:06 | [diff] [blame] | 379 | set_date_command = ['date', '-u'] |
jbudorick | 78e09f91 | 2016-05-16 18:56:15 | [diff] [blame] | 380 | get_date_command = ['date', '-u'] |
jbudorick | 7f5231e | 2015-08-27 23:02:39 | [diff] [blame] | 381 | else: |
| 382 | date_format = '%Y%m%d.%H%M%S' |
jbudorick | 78e09f91 | 2016-05-16 18:56:15 | [diff] [blame] | 383 | set_date_command = ['date', '-s'] |
| 384 | get_date_command = ['date'] |
| 385 | |
| 386 | # TODO(jbudorick): This is wrong on pre-M devices -- get/set are |
| 387 | # dealing in local time, but we're setting based on GMT. |
jbudorick | 7f5231e | 2015-08-27 23:02:39 | [diff] [blame] | 388 | strgmtime = time.strftime(date_format, time.gmtime()) |
| 389 | set_date_command.append(strgmtime) |
| 390 | device.RunShellCommand(set_date_command, as_root=True, check_return=True) |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 391 | |
jbudorick | 78e09f91 | 2016-05-16 18:56:15 | [diff] [blame] | 392 | get_date_command.append('+"%Y%m%d.%H%M%S"') |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 393 | device_time = device.RunShellCommand( |
jbudorick | 78e09f91 | 2016-05-16 18:56:15 | [diff] [blame] | 394 | get_date_command, as_root=True, single_line=True).replace('"', '') |
jbudorick | 7f5231e | 2015-08-27 23:02:39 | [diff] [blame] | 395 | device_time = datetime.datetime.strptime(device_time, "%Y%m%d.%H%M%S") |
| 396 | correct_time = datetime.datetime.strptime(strgmtime, date_format) |
Haiyang Pan | bb1655a | 2022-11-07 17:39:09 | [diff] [blame] | 397 | tdelta = abs(correct_time - device_time).seconds |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 398 | if tdelta <= 1: |
jbudorick | 7f5231e | 2015-08-27 23:02:39 | [diff] [blame] | 399 | logging.info('Date/time successfully set on %s', device) |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 400 | return True |
Yoshisato Yanagisawa | 0f42f10 | 2021-11-30 14:04:37 | [diff] [blame] | 401 | logging.error('Date mismatch. Device: %s Correct: %s', |
| 402 | device_time.isoformat(), correct_time.isoformat()) |
| 403 | return False |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 404 | |
| 405 | # Sometimes the date is not set correctly on the devices. Retry on failure. |
bpastene | 79b8679 | 2015-12-10 15:28:58 | [diff] [blame] | 406 | if device.IsUserBuild(): |
| 407 | # TODO(bpastene): Figure out how to set the date & time on user builds. |
| 408 | pass |
| 409 | else: |
| 410 | if not timeout_retry.WaitFor( |
| 411 | _set_and_verify_date, wait_period=1, max_tries=2): |
| 412 | raise device_errors.CommandFailedError( |
| 413 | 'Failed to set date & time.', device_serial=str(device)) |
navabi | 4bcff9e | 2015-08-24 16:41:31 | [diff] [blame] | 414 | |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 415 | props = device.RunShellCommand('getprop', check_return=True) |
| 416 | for prop in props: |
jbudorick | 58b4d36 | 2015-09-08 16:44:59 | [diff] [blame] | 417 | logging.info(' %s', prop) |
jbudorick | 9e8fa5da | 2015-04-09 23:40:25 | [diff] [blame] | 418 | if options.auto_reconnect: |
| 419 | _PushAndLaunchAdbReboot(device, options.target) |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 420 | |
| 421 | |
mlliu | 44ebcee | 2015-09-24 07:55:38 | [diff] [blame] | 422 | def _UninstallIfMatch(device, pattern, app_to_keep): |
| 423 | installed_packages = device.RunShellCommand(['pm', 'list', 'packages']) |
bpastene | 5c7a190 | 2015-12-14 22:18:09 | [diff] [blame] | 424 | installed_system_packages = [ |
| 425 | pkg.split(':')[1] for pkg in device.RunShellCommand(['pm', 'list', |
| 426 | 'packages', '-s'])] |
mlliu | 44ebcee | 2015-09-24 07:55:38 | [diff] [blame] | 427 | for package_output in installed_packages: |
| 428 | package = package_output.split(":")[1] |
| 429 | if pattern.match(package) and not package == app_to_keep: |
bpastene | 5c7a190 | 2015-12-14 22:18:09 | [diff] [blame] | 430 | if not device.IsUserBuild() or package not in installed_system_packages: |
| 431 | device.Uninstall(package) |
mlliu | 44ebcee | 2015-09-24 07:55:38 | [diff] [blame] | 432 | |
| 433 | |
| 434 | def _WipeUnderDirIfMatch(device, path, pattern): |
perezju | 5ad3722 | 2016-06-09 16:35:04 | [diff] [blame] | 435 | for filename in device.ListDirectory(path): |
| 436 | if pattern.match(filename): |
| 437 | _WipeFileOrDir(device, posixpath.join(path, filename)) |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 438 | |
| 439 | |
| 440 | def _WipeFileOrDir(device, path): |
| 441 | if device.PathExists(path): |
| 442 | device.RunShellCommand(['rm', '-rf', path], check_return=True) |
| 443 | |
| 444 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 445 | def _PushAndLaunchAdbReboot(device, target): |
[email protected] | c89aae0 | 2013-04-06 00:25:19 | [diff] [blame] | 446 | """Pushes and launches the adb_reboot binary on the device. |
| 447 | |
| 448 | Arguments: |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 449 | device: The DeviceUtils instance for the device to which the adb_reboot |
| 450 | binary should be pushed. |
| 451 | target: The build target (example, Debug or Release) which helps in |
| 452 | locating the adb_reboot binary. |
[email protected] | c89aae0 | 2013-04-06 00:25:19 | [diff] [blame] | 453 | """ |
jbudorick | 58b4d36 | 2015-09-08 16:44:59 | [diff] [blame] | 454 | logging.info('Will push and launch adb_reboot on %s', str(device)) |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 455 | # Kill if adb_reboot is already running. |
perezju | 84436c8 | 2015-04-15 10:19:22 | [diff] [blame] | 456 | device.KillAll('adb_reboot', blocking=True, timeout=2, quiet=True) |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 457 | # Push adb_reboot |
| 458 | logging.info(' Pushing adb_reboot ...') |
jbudorick | d28554a | 2016-01-11 16:22:59 | [diff] [blame] | 459 | adb_reboot = os.path.join(host_paths.DIR_SOURCE_ROOT, |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 460 | 'out/%s/adb_reboot' % target) |
jbudorick | 8c07d89 | 2014-10-13 11:39:48 | [diff] [blame] | 461 | device.PushChangedFiles([(adb_reboot, '/data/local/tmp/')]) |
[email protected] | af4457b | 2014-08-21 04:17:07 | [diff] [blame] | 462 | # Launch adb_reboot |
| 463 | logging.info(' Launching adb_reboot ...') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 464 | device.RunShellCommand( |
aurimas | 59c8117 | 2015-05-11 19:49:51 | [diff] [blame] | 465 | ['/data/local/tmp/adb_reboot'], |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 466 | check_return=True) |
[email protected] | c89aae0 | 2013-04-06 00:25:19 | [diff] [blame] | 467 | |
| 468 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 469 | def _LaunchHostHeartbeat(): |
| 470 | # Kill if existing host_heartbeat |
jbudorick | 809d9aff | 2015-04-09 16:45:29 | [diff] [blame] | 471 | KillHostHeartbeat() |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 472 | # Launch a new host_heartbeat |
| 473 | logging.info('Spawning host heartbeat...') |
jbudorick | d28554a | 2016-01-11 16:22:59 | [diff] [blame] | 474 | subprocess.Popen([os.path.join(host_paths.DIR_SOURCE_ROOT, |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 475 | 'build/android/host_heartbeat.py')]) |
navabi | 00c5fd3 | 2015-02-26 22:06:47 | [diff] [blame] | 476 | |
jbudorick | 809d9aff | 2015-04-09 16:45:29 | [diff] [blame] | 477 | def KillHostHeartbeat(): |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 478 | ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE) |
| 479 | stdout, _ = ps.communicate() |
| 480 | matches = re.findall('\\n.*host_heartbeat.*', stdout) |
| 481 | for match in matches: |
| 482 | logging.info('An instance of host heart beart running... will kill') |
| 483 | pid = re.findall(r'(\S+)', match)[1] |
| 484 | subprocess.call(['kill', str(pid)]) |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 485 | |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 486 | def main(): |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 487 | # Recommended options on perf bots: |
| 488 | # --disable-network |
| 489 | # TODO(tonyg): We eventually want network on. However, currently radios |
| 490 | # can cause perfbots to drain faster than they charge. |
perezju | 545a9ce | 2015-02-19 13:14:20 | [diff] [blame] | 491 | # --min-battery-level 95 |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 492 | # Some perf bots run benchmarks with USB charging disabled which leads |
| 493 | # to gradual draining of the battery. We must wait for a full charge |
| 494 | # before starting a run in order to keep the devices online. |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 495 | |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 496 | parser = argparse.ArgumentParser( |
| 497 | description='Provision Android devices with settings required for bots.') |
| 498 | parser.add_argument('-d', '--device', metavar='SERIAL', |
| 499 | help='the serial number of the device to be provisioned' |
| 500 | ' (the default is to provision all devices attached)') |
jbudorick | be1dd78 | 2016-01-12 22:52:25 | [diff] [blame] | 501 | parser.add_argument('--adb-path', |
| 502 | help='Absolute path to the adb binary to use.') |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 503 | parser.add_argument('--denylist-file', help='Device denylist JSON file.') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 504 | parser.add_argument('--phase', action='append', choices=_PHASES.ALL, |
| 505 | dest='phases', |
| 506 | help='Phases of provisioning to run. ' |
| 507 | '(If omitted, all phases will be run.)') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 508 | parser.add_argument('--skip-wipe', action='store_true', default=False, |
| 509 | help="don't wipe device data during provisioning") |
jbudorick | b22b962 | 2015-01-29 00:26:21 | [diff] [blame] | 510 | parser.add_argument('--reboot-timeout', metavar='SECS', type=int, |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 511 | help='when wiping the device, max number of seconds to' |
jbudorick | b22b962 | 2015-01-29 00:26:21 | [diff] [blame] | 512 | ' wait after each reboot ' |
| 513 | '(default: %s)' % _DEFAULT_TIMEOUTS.HELP_TEXT) |
perezju | 545a9ce | 2015-02-19 13:14:20 | [diff] [blame] | 514 | parser.add_argument('--min-battery-level', type=int, metavar='NUM', |
| 515 | help='wait for the device to reach this minimum battery' |
| 516 | ' level before trying to continue') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 517 | parser.add_argument('--disable-location', action='store_true', |
| 518 | help='disable Google location services on devices') |
hush | 4143b708 | 2015-07-09 18:41:11 | [diff] [blame] | 519 | parser.add_argument('--disable-mock-location', action='store_true', |
| 520 | default=False, help='Set ALLOW_MOCK_LOCATION to false') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 521 | parser.add_argument('--disable-network', action='store_true', |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 522 | help='disable network access on devices') |
| 523 | parser.add_argument('--disable-java-debug', action='store_false', |
perezju | 545a9ce | 2015-02-19 13:14:20 | [diff] [blame] | 524 | dest='enable_java_debug', default=True, |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 525 | help='disable Java property asserts and JNI checking') |
perezju | dcc8680 | 2015-08-25 18:39:39 | [diff] [blame] | 526 | parser.add_argument('--disable-system-chrome', action='store_true', |
| 527 | help='Disable the system chrome from devices.') |
perezju | 39b8fbd6 | 2015-07-17 11:06:39 | [diff] [blame] | 528 | parser.add_argument('--remove-system-webview', action='store_true', |
| 529 | help='Remove the system webview from devices.') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 530 | parser.add_argument('-t', '--target', default='Debug', |
| 531 | help='the build target (default: %(default)s)') |
| 532 | parser.add_argument('-r', '--auto-reconnect', action='store_true', |
| 533 | help='push binary which will reboot the device on adb' |
| 534 | ' disconnections') |
navabi | 00c5fd3 | 2015-02-26 22:06:47 | [diff] [blame] | 535 | parser.add_argument('--adb-key-files', type=str, nargs='+', |
| 536 | help='list of adb keys to push to device') |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 537 | parser.add_argument('-v', '--verbose', action='count', default=1, |
| 538 | help='Log more information.') |
rnephew | 7ce719b | 2015-05-07 05:35:54 | [diff] [blame] | 539 | parser.add_argument('--max-battery-temp', type=int, metavar='NUM', |
| 540 | help='Wait for the battery to have this temp or lower.') |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 541 | parser.add_argument('--output-device-denylist', |
| 542 | help='Json file to output the device denylist.') |
mlliu | 2f87a55 | 2015-08-25 18:21:35 | [diff] [blame] | 543 | parser.add_argument('--chrome-specific-wipe', action='store_true', |
| 544 | help='only wipe chrome specific data during provisioning') |
yolandyan | 140462c | 2015-11-07 02:47:06 | [diff] [blame] | 545 | parser.add_argument('--emulators', action='store_true', |
| 546 | help='provision only emulators and ignore usb devices') |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 547 | args = parser.parse_args() |
| 548 | constants.SetBuildType(args.target) |
| 549 | |
jbudorick | 7f46f7bf | 2015-04-08 22:57:39 | [diff] [blame] | 550 | run_tests_helper.SetLogLevel(args.verbose) |
| 551 | |
jbudorick | 3e0f4b2 | 2016-05-28 04:49:47 | [diff] [blame] | 552 | devil_chromium.Initialize(adb_path=args.adb_path) |
jbudorick | 0c2a94a | 2015-12-04 14:27:43 | [diff] [blame] | 553 | |
jbudorick | 78e09f91 | 2016-05-16 18:56:15 | [diff] [blame] | 554 | try: |
| 555 | return ProvisionDevices(args) |
| 556 | except (device_errors.DeviceUnreachableError, device_errors.NoDevicesError): |
mikecase | 371d404 | 2017-01-13 17:09:14 | [diff] [blame] | 557 | logging.exception('Unable to provision local devices.') |
jbudorick | 78e09f91 | 2016-05-16 18:56:15 | [diff] [blame] | 558 | return exit_codes.INFRA |
[email protected] | 12f36c8 | 2013-03-29 06:21:13 | [diff] [blame] | 559 | |
| 560 | |
| 561 | if __name__ == '__main__': |
perezju | b205693d | 2015-01-23 11:55:42 | [diff] [blame] | 562 | sys.exit(main()) |