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>] |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 '\n'.join(local_props), as_root=True) | 106 '\n'.join(local_props), as_root=True) |
107 # Android will not respect the local props file if it is world writable. | 107 # Android will not respect the local props file if it is world writable. |
108 device.RunShellCommand( | 108 device.RunShellCommand( |
109 ['chmod', '644', constants.DEVICE_LOCAL_PROPERTIES_PATH], | 109 ['chmod', '644', constants.DEVICE_LOCAL_PROPERTIES_PATH], |
110 as_root=True) | 110 as_root=True) |
111 except device_errors.CommandFailedError as e: | 111 except device_errors.CommandFailedError as e: |
112 logging.warning(str(e)) | 112 logging.warning(str(e)) |
113 | 113 |
114 # LOCAL_PROPERTIES_PATH = '/data/local.prop' | 114 # LOCAL_PROPERTIES_PATH = '/data/local.prop' |
115 | 115 |
116 def WriteAdbKeysFile(device, adb_keys_string): | |
117 path_list = constants.ADB_KEYS_FILE.split('/') | |
118 dir_path = '/'.join(path_list[:len(path_list)-1]) | |
perezju
2015/02/25 09:35:41
nit: maybe that's just me, but I would go for:
d
jbudorick
2015/02/25 14:09:07
I didn't know about posixpath. We should be using
navabi
2015/02/25 18:57:25
Yeah I don't like this split. But how about using:
jbudorick
2015/02/25 21:47:38
It depends on the path being manipulated:
- for h
| |
119 device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) | |
120 device.RunShellCommand('restorecon %s' % dir_path, as_root=True) | |
121 device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True) | |
122 device.RunShellCommand('restorecon %s' % constants.ADB_KEYS_FILE, | |
123 as_root=True) | |
124 | |
116 | 125 |
117 def WipeDeviceData(device): | 126 def WipeDeviceData(device): |
118 """Wipes data from device, keeping only the adb_keys for authorization. | 127 """Wipes data from device, keeping only the adb_keys for authorization. |
119 | 128 |
120 After wiping data on a device that has been authorized, adb can still | 129 After wiping data on a device that has been authorized, adb can still |
121 communicate with the device, but after reboot the device will need to be | 130 communicate with the device, but after reboot the device will need to be |
122 re-authorized because the adb keys file is stored in /data/misc/adb/. | 131 re-authorized because the adb keys file is stored in /data/misc/adb/. |
123 Thus, adb_keys file is rewritten so the device does not need to be | 132 Thus, adb_keys file is rewritten so the device does not need to be |
124 re-authorized. | 133 re-authorized. |
125 | 134 |
126 Arguments: | 135 Arguments: |
127 device: the device to wipe | 136 device: the device to wipe |
128 """ | 137 """ |
129 device_authorized = device.FileExists(constants.ADB_KEYS_FILE) | 138 device_authorized = device.FileExists(constants.ADB_KEYS_FILE) |
130 if device_authorized: | 139 if device_authorized: |
131 adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, as_root=True) | 140 adb_keys = device.ReadFile(constants.ADB_KEYS_FILE, as_root=True) |
132 device.RunShellCommand('wipe data', as_root=True) | 141 device.RunShellCommand('wipe data', as_root=True) |
133 if device_authorized: | 142 if device_authorized: |
134 path_list = constants.ADB_KEYS_FILE.split('/') | 143 WriteAdbKeysFile(device, adb_keys) |
135 dir_path = '/'.join(path_list[:len(path_list)-1]) | |
136 device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True) | |
137 device.RunShellCommand('restorecon %s' % dir_path, as_root=True) | |
138 device.WriteFile(constants.ADB_KEYS_FILE, adb_keys, as_root=True) | |
139 device.RunShellCommand('restorecon %s' % constants.ADB_KEYS_FILE, | |
140 as_root=True) | |
141 | 144 |
142 | 145 |
143 def WipeDeviceIfPossible(device, timeout): | 146 def WipeDeviceIfPossible(device, timeout): |
144 try: | 147 try: |
145 device.EnableRoot() | 148 device.EnableRoot() |
146 WipeDeviceData(device) | 149 WipeDeviceData(device) |
147 device.Reboot(True, timeout=timeout, retries=0) | 150 device.Reboot(True, timeout=timeout, retries=0) |
148 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): | 151 except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError): |
149 pass | 152 pass |
150 | 153 |
151 | 154 |
152 def ProvisionDevice(device, options): | 155 def ProvisionDevice(device, options): |
153 if options.reboot_timeout: | 156 if options.reboot_timeout: |
154 reboot_timeout = options.reboot_timeout | 157 reboot_timeout = options.reboot_timeout |
155 elif (device.build_version_sdk >= | 158 elif (device.build_version_sdk >= |
156 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): | 159 constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP): |
157 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP | 160 reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP |
158 else: | 161 else: |
159 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP | 162 reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP |
160 | 163 |
164 if options.adb_key_files: | |
165 adb_keys = set() | |
perezju
2015/02/25 09:35:41
nit: does it really need to be a set? do we expect
navabi
2015/02/25 18:57:25
Not the way the current adb keys on the host are,
| |
166 for adb_key_file in options.adb_key_files: | |
167 with open(adb_key_file, 'r') as f: | |
168 adb_public_key = f.readlines()[0] | |
perezju
2015/02/25 09:35:41
why do we keep only the first line? (earlier versi
navabi
2015/02/25 18:57:25
Because before we were reading the adb keys file o
| |
169 adb_keys.add(adb_public_key) | |
170 adb_key_contents = '\n'.join(adb_keys) | |
171 WriteAdbKeysFile(device, adb_key_contents) | |
jbudorick
2015/02/25 14:09:07
This will wipe existing keys from the device. Woul
navabi
2015/02/25 18:57:25
This will not cause problems before the hosts have
jbudorick
2015/02/25 21:47:38
This makes me a little nervous, but ok.
| |
172 | |
161 try: | 173 try: |
162 if not options.skip_wipe: | 174 if not options.skip_wipe: |
163 WipeDeviceIfPossible(device, reboot_timeout) | 175 WipeDeviceIfPossible(device, reboot_timeout) |
164 try: | 176 try: |
165 device.EnableRoot() | 177 device.EnableRoot() |
166 except device_errors.CommandFailedError as e: | 178 except device_errors.CommandFailedError as e: |
167 logging.warning(str(e)) | 179 logging.warning(str(e)) |
168 _ConfigureLocalProperties(device, options.enable_java_debug) | 180 _ConfigureLocalProperties(device, options.enable_java_debug) |
169 device_settings.ConfigureContentSettings( | 181 device_settings.ConfigureContentSettings( |
170 device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) | 182 device, device_settings.DETERMINISTIC_DEVICE_SETTINGS) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 parser.add_argument('--disable-network', action='store_true', | 281 parser.add_argument('--disable-network', action='store_true', |
270 help='disable network access on devices') | 282 help='disable network access on devices') |
271 parser.add_argument('--disable-java-debug', action='store_false', | 283 parser.add_argument('--disable-java-debug', action='store_false', |
272 dest='enable_java_debug', default=True, | 284 dest='enable_java_debug', default=True, |
273 help='disable Java property asserts and JNI checking') | 285 help='disable Java property asserts and JNI checking') |
274 parser.add_argument('-t', '--target', default='Debug', | 286 parser.add_argument('-t', '--target', default='Debug', |
275 help='the build target (default: %(default)s)') | 287 help='the build target (default: %(default)s)') |
276 parser.add_argument('-r', '--auto-reconnect', action='store_true', | 288 parser.add_argument('-r', '--auto-reconnect', action='store_true', |
277 help='push binary which will reboot the device on adb' | 289 help='push binary which will reboot the device on adb' |
278 ' disconnections') | 290 ' disconnections') |
291 parser.add_argument('--adb-key-files', type=str, nargs='+', | |
292 help='list of adb keys to push to device') | |
279 args = parser.parse_args() | 293 args = parser.parse_args() |
280 constants.SetBuildType(args.target) | 294 constants.SetBuildType(args.target) |
281 | 295 |
282 return ProvisionDevices(args) | 296 return ProvisionDevices(args) |
283 | 297 |
284 | 298 |
285 if __name__ == '__main__': | 299 if __name__ == '__main__': |
286 sys.exit(main()) | 300 sys.exit(main()) |
OLD | NEW |