| 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.cmd_helper import GetCmdOutput | |
| 25 from pylib.device import device_utils | 24 from pylib.device import device_utils |
| 26 | 25 |
| 27 def KillHostHeartbeat(): | 26 def KillHostHeartbeat(): |
| 28 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) | 27 ps = subprocess.Popen(['ps', 'aux'], stdout = subprocess.PIPE) |
| 29 stdout, _ = ps.communicate() | 28 stdout, _ = ps.communicate() |
| 30 matches = re.findall('\\n.*host_heartbeat.*', stdout) | 29 matches = re.findall('\\n.*host_heartbeat.*', stdout) |
| 31 for match in matches: | 30 for match in matches: |
| 32 print 'An instance of host heart beart running... will kill' | 31 print 'An instance of host heart beart running... will kill' |
| 33 pid = re.findall('(\d+)', match)[1] | 32 pid = re.findall('(\d+)', match)[1] |
| 34 subprocess.call(['kill', str(pid)]) | 33 subprocess.call(['kill', str(pid)]) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 87 |
| 89 # LOCAL_PROPERTIES_PATH = '/data/local.prop' | 88 # LOCAL_PROPERTIES_PATH = '/data/local.prop' |
| 90 | 89 |
| 91 | 90 |
| 92 def WipeDeviceData(device): | 91 def WipeDeviceData(device): |
| 93 """Wipes data from device, keeping only the adb_keys for authorization. | 92 """Wipes data from device, keeping only the adb_keys for authorization. |
| 94 | 93 |
| 95 After wiping data on a device that has been authorized, adb can still | 94 After wiping data on a device that has been authorized, adb can still |
| 96 communicate with the device, but after reboot the device will need to be | 95 communicate with the device, but after reboot the device will need to be |
| 97 re-authorized because the adb keys file is stored in /data/misc/adb/. | 96 re-authorized because the adb keys file is stored in /data/misc/adb/. |
| 98 Thus, before reboot the adb_keys file is rewritten so the device does not need | 97 Thus, adb_keys file is rewritten so the device does not need to be |
| 99 to be re-authorized. | 98 re-authorized. |
| 100 | 99 |
| 101 Arguments: | 100 Arguments: |
| 102 device: the device to wipe | 101 device: the device to wipe |
| 103 """ | 102 """ |
| 104 device_authorized = device.old_interface.FileExistsOnDevice( | 103 device_authorized = device.old_interface.FileExistsOnDevice( |
| 105 constants.ADB_KEYS_FILE) | 104 constants.ADB_KEYS_FILE) |
| 106 if device_authorized: | 105 if device_authorized: |
| 107 adb_keys = device.old_interface.RunShellCommandWithSU( | 106 adb_keys = device.old_interface.RunShellCommandWithSU( |
| 108 'cat %s' % constants.ADB_KEYS_FILE)[0] | 107 'cat %s' % constants.ADB_KEYS_FILE) |
| 109 device.old_interface.RunShellCommandWithSU('wipe data') | 108 device.old_interface.RunShellCommandWithSU('wipe data') |
| 110 if device_authorized: | 109 if device_authorized: |
| 111 path_list = constants.ADB_KEYS_FILE.split('/') | 110 path_list = constants.ADB_KEYS_FILE.split('/') |
| 112 dir_path = '/'.join(path_list[:len(path_list)-1]) | 111 dir_path = '/'.join(path_list[:len(path_list)-1]) |
| 113 device.old_interface.RunShellCommandWithSU('mkdir -p %s' % dir_path) | 112 device.old_interface.RunShellCommandWithSU('mkdir -p %s' % dir_path) |
| 114 adb_keys = device.old_interface.RunShellCommand( | 113 device.old_interface.RunShellCommandWithSU('touch %s' % |
| 115 'echo %s > %s' % (adb_keys, constants.ADB_KEYS_FILE)) | 114 constants.ADB_KEYS_FILE) |
| 116 device.old_interface.Reboot() | 115 for adb_key in adb_keys: |
| 116 device.old_interface.RunShellCommand( |
| 117 'echo %s >> %s' % (adb_key, constants.ADB_KEYS_FILE)) |
| 117 | 118 |
| 118 | 119 |
| 119 def ProvisionDevices(options): | 120 def ProvisionDevices(options): |
| 120 if options.device is not None: | 121 if options.device is not None: |
| 121 devices = [options.device] | 122 devices = [options.device] |
| 122 else: | 123 else: |
| 123 devices = android_commands.GetAttachedDevices() | 124 devices = android_commands.GetAttachedDevices() |
| 124 for device_serial in devices: | 125 for device_serial in devices: |
| 126 WipeDeviceData(device_utils.DeviceUtils(device_serial)) |
| 127 device_utils.RebootDevices() |
| 128 for device_serial in devices: |
| 125 device = device_utils.DeviceUtils(device_serial) | 129 device = device_utils.DeviceUtils(device_serial) |
| 126 device.old_interface.EnableAdbRoot() | 130 device.old_interface.EnableAdbRoot() |
| 127 install_output = GetCmdOutput( | |
| 128 ['%s/build/android/adb_install_apk.py' % constants.DIR_SOURCE_ROOT, | |
| 129 '--apk', | |
| 130 '%s/build/android/CheckInstallApk-debug.apk' % constants.DIR_SOURCE_ROOT | |
| 131 ]) | |
| 132 failure_string = 'Failure [INSTALL_FAILED_INSUFFICIENT_STORAGE]' | |
| 133 if failure_string in install_output: | |
| 134 WipeDeviceData(device) | |
| 135 _ConfigureLocalProperties(device) | 131 _ConfigureLocalProperties(device) |
| 136 device_settings.ConfigureContentSettingsDict( | 132 device_settings.ConfigureContentSettingsDict( |
| 137 device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) | 133 device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) |
| 138 # TODO(tonyg): We eventually want network on. However, currently radios | 134 # TODO(tonyg): We eventually want network on. However, currently radios |
| 139 # can cause perfbots to drain faster than they charge. | 135 # can cause perfbots to drain faster than they charge. |
| 140 if 'perf' in os.environ.get('BUILDBOT_BUILDERNAME', '').lower(): | 136 if 'perf' in os.environ.get('BUILDBOT_BUILDERNAME', '').lower(): |
| 141 device_settings.ConfigureContentSettingsDict( | 137 device_settings.ConfigureContentSettingsDict( |
| 142 device, device_settings.NETWORK_DISABLED_SETTINGS) | 138 device, device_settings.NETWORK_DISABLED_SETTINGS) |
| 143 device.old_interface.RunShellCommandWithSU('date -u %f' % time.time()) | 139 device.old_interface.RunShellCommandWithSU('date -u %f' % time.time()) |
| 144 if options.auto_reconnect: | 140 if options.auto_reconnect: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 | 156 |
| 161 if args: | 157 if args: |
| 162 print >> sys.stderr, 'Unused args %s' % args | 158 print >> sys.stderr, 'Unused args %s' % args |
| 163 return 1 | 159 return 1 |
| 164 | 160 |
| 165 ProvisionDevices(options) | 161 ProvisionDevices(options) |
| 166 | 162 |
| 167 | 163 |
| 168 if __name__ == '__main__': | 164 if __name__ == '__main__': |
| 169 sys.exit(main(sys.argv)) | 165 sys.exit(main(sys.argv)) |
| OLD | NEW |