James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | # URL from which the latest version of this script can be downloaded. |
Benjamin Lerman | 507bb1a | 2015-02-26 13:15:17 +0100 | [diff] [blame] | 7 | # Gitiles returns the result as base64 formatted, so the result needs to be |
| 8 | # decoded. See https://code.google.com/p/gitiles/issues/detail?id=7 for |
| 9 | # more information about this security precaution. |
| 10 | script_url="https://chromium.googlesource.com/chromium/src.git/+/master" |
| 11 | script_url+="/tools/android/adb_remote_setup.sh" |
| 12 | script_url+="?format=TEXT" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 13 | |
| 14 | # Replaces this file with the latest version of the script and runs it. |
| 15 | update-self() { |
| 16 | local script="${BASH_SOURCE[0]}" |
| 17 | local new_script="${script}.new" |
| 18 | local updater_script="${script}.updater" |
Benjamin Lerman | 507bb1a | 2015-02-26 13:15:17 +0100 | [diff] [blame] | 19 | curl -sSf "$script_url" | base64 --decode > "$new_script" || return |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 20 | chmod +x "$new_script" || return |
| 21 | |
| 22 | # Replace this file with the newly downloaded script. |
| 23 | cat > "$updater_script" << EOF |
| 24 | #!/bin/bash |
| 25 | if mv "$new_script" "$script"; then |
| 26 | rm -- "$updater_script" |
| 27 | else |
| 28 | echo "Note: script update failed." |
| 29 | fi |
| 30 | ADB_REMOTE_SETUP_NO_UPDATE=1 exec /bin/bash "$script" $@ |
| 31 | EOF |
| 32 | exec /bin/bash "$updater_script" "$@" |
| 33 | } |
| 34 | |
| 35 | if [[ "$ADB_REMOTE_SETUP_NO_UPDATE" -ne 1 ]]; then |
| 36 | update-self "$@" || echo 'Note: script update failed' |
| 37 | fi |
| 38 | |
| 39 | if [[ $# -ne 1 && $# -ne 2 ]]; then |
| 40 | cat <<'EOF' |
| 41 | Usage: adb_remote_setup.sh REMOTE_HOST [REMOTE_ADB] |
| 42 | |
| 43 | Configures adb on a remote machine to communicate with a device attached to the |
| 44 | local machine. This is useful for installing APKs, running tests, etc while |
| 45 | working remotely. |
| 46 | |
| 47 | Arguments: |
| 48 | REMOTE_HOST hostname of remote machine |
| 49 | REMOTE_ADB path to adb on the remote machine (you can omit this if adb is in |
| 50 | the remote host's path) |
| 51 | EOF |
| 52 | exit 1 |
| 53 | fi |
| 54 | |
| 55 | remote_host="$1" |
| 56 | remote_adb="${2:-adb}" |
| 57 | |
| 58 | # Ensure adb is in the local machine's path. |
| 59 | if ! which adb >/dev/null; then |
| 60 | echo "error: adb must be in your local machine's path." |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 64 | # Ensure local and remote versions of adb are the same. |
| 65 | remote_adb_version=$(ssh "$remote_host" "$remote_adb version") |
| 66 | local_adb_version=$(adb version) |
| 67 | if [[ "$local_adb_version" != "$remote_adb_version" ]]; then |
| 68 | echo >&2 |
| 69 | echo "WARNING: local adb is not the same version as remote adb." >&2 |
| 70 | echo "This should be fixed since it may result in protocol errors." >&2 |
| 71 | echo " local adb: $local_adb_version" >&2 |
| 72 | echo " remote adb: $remote_adb_version" >&2 |
| 73 | echo >&2 |
| 74 | sleep 5 |
| 75 | fi |
| 76 | |
| 77 | # Kill the adb server on the remote host. |
| 78 | ssh "$remote_host" "$remote_adb kill-server" |
| 79 | |
| 80 | # Start the adb server locally. |
| 81 | adb start-server |
| 82 | |
| 83 | # Forward various ports from the remote host to the local host: |
| 84 | # 5037: adb |
| 85 | # 8001: http server |
| 86 | # 9031: sync server |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 87 | # 9041: search by image server |
| 88 | # 9051: policy server |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 89 | # 10000: net unittests |
| 90 | # 10201: net unittests |
| 91 | ssh -C \ |
| 92 | -R 5037:localhost:5037 \ |
| 93 | -L 8001:localhost:8001 \ |
| 94 | -L 9031:localhost:9031 \ |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 95 | -L 9041:localhost:9041 \ |
| 96 | -L 9051:localhost:9051 \ |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 97 | -R 10000:localhost:10000 \ |
| 98 | -R 10201:localhost:10201 \ |
| 99 | "$remote_host" |