Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 1 | # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # Library for setting up remote access and running remote commands. |
| 6 | |
Sean O'Connor | a6db82e | 2010-01-27 12:11:08 -0800 | [diff] [blame] | 7 | DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\ |
| 8 | ssh_keys/testing_rsa" |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 9 | |
| 10 | DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" |
| 11 | DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \ |
| 12 | "Private key of root account on remote host" |
Zelidrag Hornung | 61d9768 | 2010-06-15 11:55:21 -0700 | [diff] [blame] | 13 | DEFINE_integer ssh_port 22 \ |
| 14 | "SSH port of the remote machine running Chromium OS instance" |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 15 | |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 16 | # Copies $1 to $2 on remote host |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 17 | function remote_cp_to() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 18 | REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
David James | 985cd88 | 2011-08-21 10:02:30 -0700 | [diff] [blame] | 19 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \ |
| 20 | -i $TMP_PRIVATE_KEY $1 root@$FLAGS_remote:$2) |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 21 | return ${PIPESTATUS[0]} |
| 22 | } |
| 23 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 24 | # Copies a list of remote files specified in file $1 to local location |
| 25 | # $2. Directory paths in $1 are collapsed into $2. |
| 26 | function remote_rsync_from() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 27 | rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
David James | 985cd88 | 2011-08-21 10:02:30 -0700 | [diff] [blame] | 28 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \ |
| 29 | -i $TMP_PRIVATE_KEY" \ |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 30 | --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame^] | 33 | function _verbose_remote_sh() { |
| 34 | REMOTE_OUT=$(ssh -vp ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 35 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \ |
| 36 | -i $TMP_PRIVATE_KEY root@$FLAGS_remote "$@") |
| 37 | return ${PIPESTATUS[0]} |
| 38 | } |
| 39 | |
| 40 | function _non_verbose_remote_sh() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 41 | REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
David James | 985cd88 | 2011-08-21 10:02:30 -0700 | [diff] [blame] | 42 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \ |
| 43 | -i $TMP_PRIVATE_KEY root@$FLAGS_remote "$@") |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 44 | return ${PIPESTATUS[0]} |
| 45 | } |
| 46 | |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame^] | 47 | # Wrapper for ssh that runs the commmand given by the args on the remote host |
| 48 | # If an ssh error occurs, re-runs the ssh command with verbose flag set. |
| 49 | function remote_sh() { |
| 50 | local ssh_status=0 |
| 51 | _non_verbose_remote_sh "$@" || ssh_status=$? |
| 52 | # 255 indicates an ssh error. |
| 53 | if [ ${ssh_status} -eq 255 ]; then |
| 54 | _verbose_remote_sh "$@" |
| 55 | else |
| 56 | return ${ssh_status} |
| 57 | fi |
| 58 | } |
| 59 | |
Andrew de los Reyes | e08639b | 2011-09-21 15:44:05 -0700 | [diff] [blame] | 60 | function remote_sh_raw() { |
| 61 | ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 62 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \ |
| 63 | -i $TMP_PRIVATE_KEY $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@" |
| 64 | return $? |
| 65 | } |
| 66 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 67 | function remote_sh_allow_changed_host_key() { |
| 68 | rm -f $TMP_KNOWN_HOSTS |
| 69 | remote_sh "$@" |
| 70 | } |
| 71 | |
| 72 | function set_up_remote_access() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 73 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 74 | chmod 0400 $TMP_PRIVATE_KEY |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 75 | |
| 76 | # Verify the client is reachable before continuing |
Gaurav Shah | af7d5d1 | 2011-09-21 16:42:16 -0700 | [diff] [blame] | 77 | local output |
| 78 | local status=0 |
| 79 | if output=$(remote_sh "true" 2>&1); then |
| 80 | : |
| 81 | else |
| 82 | status=$? |
| 83 | echo "Could not initiate first contact with remote host" |
| 84 | echo "$output" |
| 85 | fi |
| 86 | return $status |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 89 | # Ask the target what board it is |
| 90 | function learn_board() { |
| 91 | [ -n "${FLAGS_board}" ] && return |
| 92 | remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
| 93 | FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 94 | if [ -z "${FLAGS_board}" ]; then |
| 95 | error "Board required" |
| 96 | exit 1 |
| 97 | fi |
| 98 | info "Target reports board is ${FLAGS_board}" |
| 99 | } |
| 100 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 101 | function learn_arch() { |
| 102 | [ -n "${FLAGS_arch}" ] && return |
| 103 | remote_sh uname -m |
Mandeep Singh Baines | 175422f | 2011-05-31 10:51:02 -0700 | [diff] [blame] | 104 | FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ ) |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 105 | if [ -z "${FLAGS_arch}" ]; then |
| 106 | error "Arch required" |
| 107 | exit 1 |
| 108 | fi |
| 109 | info "Target reports arch is ${FLAGS_arch}" |
| 110 | } |
| 111 | |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 112 | # Checks to see if pid $1 is running. |
| 113 | function is_pid_running() { |
| 114 | ps -p ${1} 2>&1 > /dev/null |
| 115 | } |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 116 | |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 117 | # Wait function given an additional timeout argument. |
| 118 | # $1 - pid to wait on. |
| 119 | # $2 - timeout to wait for. |
| 120 | function wait_with_timeout() { |
| 121 | local pid=$1 |
| 122 | local timeout=$2 |
| 123 | local -r TIMEOUT_INC=1 |
| 124 | local current_timeout=0 |
| 125 | while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do |
| 126 | sleep ${TIMEOUT_INC} |
| 127 | current_timeout=$((current_timeout + TIMEOUT_INC)) |
| 128 | done |
| 129 | ! is_pid_running ${pid} |
| 130 | } |
| 131 | |
| 132 | # Checks to see if a machine has rebooted using the presence of a tmp file. |
| 133 | function check_if_rebooted() { |
| 134 | local output_file="${TMP}/output" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 135 | while true; do |
| 136 | REMOTE_OUT="" |
| 137 | # This may fail while the machine is down so generate output and a |
| 138 | # boolean result to distinguish between down/timeout and real failure |
| 139 | ! remote_sh_allow_changed_host_key \ |
| 140 | "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" |
| 141 | echo "${REMOTE_OUT}" > "${output_file}" |
| 142 | if grep -q "0" "${output_file}"; then |
| 143 | if grep -q "1" "${output_file}"; then |
| 144 | info "Not yet rebooted" |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 145 | sleep .5 |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 146 | else |
| 147 | info "Rebooted and responding" |
| 148 | break |
| 149 | fi |
| 150 | fi |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 151 | done |
| 152 | } |
| 153 | |
| 154 | function remote_reboot() { |
| 155 | info "Rebooting." |
| 156 | remote_sh "touch /tmp/awaiting_reboot; reboot" |
| 157 | while true; do |
| 158 | check_if_rebooted & |
| 159 | local pid=$! |
| 160 | wait_with_timeout ${pid} 30 && break |
| 161 | ! kill -9 ${pid} 2> /dev/null |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 162 | done |
| 163 | } |
| 164 | |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 165 | # Called by clients before exiting. |
| 166 | # Part of the remote_access.sh interface but now empty. |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 167 | function cleanup_remote_access() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 168 | true |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 171 | function remote_access_init() { |
| 172 | TMP_PRIVATE_KEY=$TMP/private_key |
| 173 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 174 | if [ -z "$FLAGS_remote" ]; then |
| 175 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 176 | exit 1 |
| 177 | fi |
| 178 | set_up_remote_access |
| 179 | } |