OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Provisions Android devices with settings required for bots. | 7 """Provisions Android devices with settings required for bots. |
8 | 8 |
9 Usage: | 9 Usage: |
10 ./provision_devices.py [-d <device serial number>] | 10 ./provision_devices.py [-d <device serial number>] |
11 """ | 11 """ |
12 | 12 |
13 import logging | 13 import logging |
14 import optparse | 14 import optparse |
15 import os | 15 import os |
16 import re | 16 import re |
17 import subprocess | 17 import subprocess |
18 import sys | 18 import sys |
19 import time | 19 import time |
20 | 20 |
21 from pylib import android_commands | 21 from pylib import android_commands |
22 from pylib import constants | 22 from pylib import constants |
23 from pylib import device_settings | 23 from pylib import device_settings |
| 24 from pylib.device import device_errors |
24 from pylib.device import device_utils | 25 from pylib.device import device_utils |
25 | 26 |
26 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, | 27 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, |
27 'third_party', 'android_testrunner')) | 28 'third_party', 'android_testrunner')) |
28 import errors | 29 import errors |
29 | 30 |
30 def KillHostHeartbeat(): | 31 def KillHostHeartbeat(): |
31 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) | 32 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) |
32 stdout, _ = ps.communicate() | 33 stdout, _ = ps.communicate() |
33 matches = re.findall('\\n.*host_heartbeat.*', stdout) | 34 matches = re.findall('\\n.*host_heartbeat.*', stdout) |
(...skipping 18 matching lines...) Expand all Loading... |
52 Arguments: | 53 Arguments: |
53 devices: The list of serial numbers of the device to which the | 54 devices: The list of serial numbers of the device to which the |
54 adb_reboot binary should be pushed. | 55 adb_reboot binary should be pushed. |
55 target : The build target (example, Debug or Release) which helps in | 56 target : The build target (example, Debug or Release) which helps in |
56 locating the adb_reboot binary. | 57 locating the adb_reboot binary. |
57 """ | 58 """ |
58 for device_serial in devices: | 59 for device_serial in devices: |
59 print 'Will push and launch adb_reboot on %s' % device_serial | 60 print 'Will push and launch adb_reboot on %s' % device_serial |
60 device = device_utils.DeviceUtils(device_serial) | 61 device = device_utils.DeviceUtils(device_serial) |
61 # Kill if adb_reboot is already running. | 62 # Kill if adb_reboot is already running. |
62 device.old_interface.KillAllBlocking('adb_reboot', 2) | 63 try: |
| 64 # Don't try to kill adb_reboot more than once. We don't expect it to be |
| 65 # running at all. |
| 66 device.KillAll('adb_reboot', blocking=True, timeout=2, retries=0) |
| 67 except device_errors.CommandFailedError: |
| 68 # We can safely ignore the exception because we don't expect adb_reboot |
| 69 # to be running. |
| 70 pass |
63 # Push adb_reboot | 71 # Push adb_reboot |
64 print ' Pushing adb_reboot ...' | 72 print ' Pushing adb_reboot ...' |
65 adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT, | 73 adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT, |
66 'out/%s/adb_reboot' % target) | 74 'out/%s/adb_reboot' % target) |
67 device.old_interface.PushIfNeeded(adb_reboot, '/data/local/tmp/') | 75 device.old_interface.PushIfNeeded(adb_reboot, '/data/local/tmp/') |
68 # Launch adb_reboot | 76 # Launch adb_reboot |
69 print ' Launching adb_reboot ...' | 77 print ' Launching adb_reboot ...' |
70 device.old_interface.GetAndroidToolStatusAndOutput( | 78 device.old_interface.GetAndroidToolStatusAndOutput( |
71 '/data/local/tmp/adb_reboot') | 79 '/data/local/tmp/adb_reboot') |
72 LaunchHostHeartbeat() | 80 LaunchHostHeartbeat() |
73 | 81 |
74 | 82 |
75 def _ConfigureLocalProperties(device, is_perf): | 83 def _ConfigureLocalProperties(device, is_perf): |
76 """Set standard readonly testing device properties prior to reboot.""" | 84 """Set standard readonly testing device properties prior to reboot.""" |
77 local_props = [ | 85 local_props = [ |
78 'ro.monkey=1', | 86 'ro.monkey=1', |
79 'ro.test_harness=1', | 87 'ro.test_harness=1', |
80 'ro.audio.silent=1', | 88 'ro.audio.silent=1', |
81 'ro.setupwizard.mode=DISABLED', | 89 'ro.setupwizard.mode=DISABLED', |
82 ] | 90 ] |
83 if not is_perf: | 91 if not is_perf: |
84 local_props.append('%s=all' % android_commands.JAVA_ASSERT_PROPERTY) | 92 local_props.append('%s=all' % android_commands.JAVA_ASSERT_PROPERTY) |
85 local_props.append('debug.checkjni=1') | 93 local_props.append('debug.checkjni=1') |
86 device.old_interface.SetProtectedFileContents( | 94 device.old_interface.SetProtectedFileContents( |
87 constants.DEVICE_LOCAL_PROPERTIES_PATH, | 95 constants.DEVICE_LOCAL_PROPERTIES_PATH, |
88 '\n'.join(local_props)) | 96 '\n'.join(local_props)) |
89 # Android will not respect the local props file if it is world writable. | 97 # Android will not respect the local props file if it is world writable. |
90 device.RunShellCommand( | 98 device.RunShellCommand( |
91 'chmod 644 %s' % constants.DEVICE_LOCAL_PROPERTIES_PATH, | 99 'chmod 644 %s' % constants.DEVICE_LOCAL_PROPERTIES_PATH, |
92 root=True) | 100 as_root=True) |
93 | 101 |
94 # LOCAL_PROPERTIES_PATH = '/data/local.prop' | 102 # LOCAL_PROPERTIES_PATH = '/data/local.prop' |
95 | 103 |
96 | 104 |
97 def WipeDeviceData(device): | 105 def WipeDeviceData(device): |
98 """Wipes data from device, keeping only the adb_keys for authorization. | 106 """Wipes data from device, keeping only the adb_keys for authorization. |
99 | 107 |
100 After wiping data on a device that has been authorized, adb can still | 108 After wiping data on a device that has been authorized, adb can still |
101 communicate with the device, but after reboot the device will need to be | 109 communicate with the device, but after reboot the device will need to be |
102 re-authorized because the adb keys file is stored in /data/misc/adb/. | 110 re-authorized because the adb keys file is stored in /data/misc/adb/. |
103 Thus, adb_keys file is rewritten so the device does not need to be | 111 Thus, adb_keys file is rewritten so the device does not need to be |
104 re-authorized. | 112 re-authorized. |
105 | 113 |
106 Arguments: | 114 Arguments: |
107 device: the device to wipe | 115 device: the device to wipe |
108 """ | 116 """ |
109 device_authorized = device.old_interface.FileExistsOnDevice( | 117 device_authorized = device.old_interface.FileExistsOnDevice( |
110 constants.ADB_KEYS_FILE) | 118 constants.ADB_KEYS_FILE) |
111 if device_authorized: | 119 if device_authorized: |
112 adb_keys = device.RunShellCommand('cat %s' % constants.ADB_KEYS_FILE, | 120 adb_keys = device.RunShellCommand('cat %s' % constants.ADB_KEYS_FILE, |
113 root=True) | 121 as_root=True) |
114 device.RunShellCommand('wipe data', root=True) | 122 device.RunShellCommand('wipe data', as_root=True) |
115 if device_authorized: | 123 if device_authorized: |
116 path_list = constants.ADB_KEYS_FILE.split('/') | 124 path_list = constants.ADB_KEYS_FILE.split('/') |
117 dir_path = '/'.join(path_list[:len(path_list)-1]) | 125 dir_path = '/'.join(path_list[:len(path_list)-1]) |
118 device.RunShellCommand('mkdir -p %s' % dir_path, root=True) | 126 device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) |
119 device.RunShellCommand('echo %s > %s' % | 127 device.RunShellCommand('echo %s > %s' % |
120 (adb_keys[0], constants.ADB_KEYS_FILE)) | 128 (adb_keys[0], constants.ADB_KEYS_FILE)) |
121 for adb_key in adb_keys[1:]: | 129 for adb_key in adb_keys[1:]: |
122 device.RunShellCommand( | 130 device.RunShellCommand( |
123 'echo %s >> %s' % (adb_key, constants.ADB_KEYS_FILE)) | 131 'echo %s >> %s' % (adb_key, constants.ADB_KEYS_FILE)) |
124 | 132 |
125 | 133 |
126 def ProvisionDevices(options): | 134 def ProvisionDevices(options): |
127 is_perf = 'perf' in os.environ.get('BUILDBOT_BUILDERNAME', '').lower() | 135 is_perf = 'perf' in os.environ.get('BUILDBOT_BUILDERNAME', '').lower() |
128 # TODO(jbudorick): Parallelize provisioning of all attached devices after | 136 # TODO(jbudorick): Parallelize provisioning of all attached devices after |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 if not device.old_interface.IsDeviceCharging(): | 183 if not device.old_interface.IsDeviceCharging(): |
176 if device.old_interface.CanControlUsbCharging(): | 184 if device.old_interface.CanControlUsbCharging(): |
177 device.old_interface.EnableUsbCharging() | 185 device.old_interface.EnableUsbCharging() |
178 else: | 186 else: |
179 logging.error('Device is not charging') | 187 logging.error('Device is not charging') |
180 break | 188 break |
181 logging.info('Waiting for device to charge. Current level=%s', | 189 logging.info('Waiting for device to charge. Current level=%s', |
182 battery_info.get('level', 0)) | 190 battery_info.get('level', 0)) |
183 time.sleep(60) | 191 time.sleep(60) |
184 battery_info = device.old_interface.GetBatteryInfo() | 192 battery_info = device.old_interface.GetBatteryInfo() |
185 device.RunShellCommand('date -u %f' % time.time(), root=True) | 193 device.RunShellCommand('date -u %f' % time.time(), as_root=True) |
186 try: | 194 try: |
187 device_utils.DeviceUtils.parallel(devices).Reboot(True) | 195 device_utils.DeviceUtils.parallel(devices).Reboot(True) |
188 except errors.DeviceUnresponsiveError: | 196 except errors.DeviceUnresponsiveError: |
189 pass | 197 pass |
190 for device_serial in devices: | 198 for device_serial in devices: |
191 device = device_utils.DeviceUtils(device_serial) | 199 device = device_utils.DeviceUtils(device_serial) |
192 device.WaitUntilFullyBooted(timeout=90) | 200 device.WaitUntilFullyBooted(timeout=90) |
193 (_, props) = device.old_interface.GetShellCommandStatusAndOutput('getprop') | 201 (_, props) = device.old_interface.GetShellCommandStatusAndOutput('getprop') |
194 for prop in props: | 202 for prop in props: |
195 print prop | 203 print prop |
(...skipping 20 matching lines...) Expand all Loading... |
216 | 224 |
217 if args: | 225 if args: |
218 print >> sys.stderr, 'Unused args %s' % args | 226 print >> sys.stderr, 'Unused args %s' % args |
219 return 1 | 227 return 1 |
220 | 228 |
221 ProvisionDevices(options) | 229 ProvisionDevices(options) |
222 | 230 |
223 | 231 |
224 if __name__ == '__main__': | 232 if __name__ == '__main__': |
225 sys.exit(main(sys.argv)) | 233 sys.exit(main(sys.argv)) |
OLD | NEW |