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 | |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 16 | SSH_CONNECT_SETTINGS="-o Protocol=2 -o ConnectTimeout=30 \ |
| 17 | -o ConnectionAttempts=4 -o ServerAliveInterval=10 \ |
| 18 | -o ServerAliveCountMax=3 -o StrictHostKeyChecking=no" |
| 19 | |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 20 | # Copies $1 to $2 on remote host |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 21 | function remote_cp_to() { |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 22 | REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \ |
| 23 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \ |
| 24 | root@$FLAGS_remote:$2) |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 25 | return ${PIPESTATUS[0]} |
| 26 | } |
| 27 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 28 | # Copies a list of remote files specified in file $1 to local location |
| 29 | # $2. Directory paths in $1 are collapsed into $2. |
| 30 | function remote_rsync_from() { |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 31 | rsync -e "ssh -p ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \ |
| 32 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \ |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 33 | --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 36 | function _remote_sh() { |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 37 | REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \ |
| 38 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ |
| 39 | root@$FLAGS_remote "$@") |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 40 | return ${PIPESTATUS[0]} |
| 41 | } |
| 42 | |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 43 | # Wrapper for ssh that runs the commmand given by the args on the remote host |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 44 | # If an ssh error occurs, re-runs the ssh command. |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 45 | function remote_sh() { |
| 46 | local ssh_status=0 |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 47 | _remote_sh "$@" || ssh_status=$? |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 48 | # 255 indicates an ssh error. |
| 49 | if [ ${ssh_status} -eq 255 ]; then |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 50 | _remote_sh "$@" |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 51 | else |
| 52 | return ${ssh_status} |
| 53 | fi |
| 54 | } |
| 55 | |
Andrew de los Reyes | e08639b | 2011-09-21 15:44:05 -0700 | [diff] [blame] | 56 | function remote_sh_raw() { |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 57 | ssh -p ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \ |
| 58 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ |
| 59 | $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@" |
Andrew de los Reyes | e08639b | 2011-09-21 15:44:05 -0700 | [diff] [blame] | 60 | return $? |
| 61 | } |
| 62 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 63 | function remote_sh_allow_changed_host_key() { |
| 64 | rm -f $TMP_KNOWN_HOSTS |
| 65 | remote_sh "$@" |
| 66 | } |
| 67 | |
| 68 | function set_up_remote_access() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 69 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 70 | chmod 0400 $TMP_PRIVATE_KEY |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 71 | |
| 72 | # Verify the client is reachable before continuing |
Gaurav Shah | af7d5d1 | 2011-09-21 16:42:16 -0700 | [diff] [blame] | 73 | local output |
| 74 | local status=0 |
| 75 | if output=$(remote_sh "true" 2>&1); then |
| 76 | : |
| 77 | else |
| 78 | status=$? |
| 79 | echo "Could not initiate first contact with remote host" |
| 80 | echo "$output" |
| 81 | fi |
| 82 | return $status |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 85 | # Ask the target what board it is |
| 86 | function learn_board() { |
| 87 | [ -n "${FLAGS_board}" ] && return |
| 88 | remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
| 89 | FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 90 | if [ -z "${FLAGS_board}" ]; then |
| 91 | error "Board required" |
| 92 | exit 1 |
| 93 | fi |
| 94 | info "Target reports board is ${FLAGS_board}" |
| 95 | } |
| 96 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame] | 97 | function learn_arch() { |
| 98 | [ -n "${FLAGS_arch}" ] && return |
| 99 | remote_sh uname -m |
Mandeep Singh Baines | 175422f | 2011-05-31 10:51:02 -0700 | [diff] [blame] | 100 | 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] | 101 | if [ -z "${FLAGS_arch}" ]; then |
| 102 | error "Arch required" |
| 103 | exit 1 |
| 104 | fi |
| 105 | info "Target reports arch is ${FLAGS_arch}" |
| 106 | } |
| 107 | |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 108 | # Checks whether a remote device has rebooted successfully. |
| 109 | # |
| 110 | # This uses a rapidly-retried SSH connection, which will wait for at most |
| 111 | # about ten seconds. If the network returns an error (e.g. host unreachable) |
| 112 | # the actual delay may be shorter. |
| 113 | # |
| 114 | # Return values: |
| 115 | # 0: The device has rebooted successfully |
| 116 | # 1: The device has not yet rebooted |
| 117 | # 255: Unable to communicate with the device |
| 118 | function _check_if_rebooted() { |
| 119 | ( |
| 120 | # In my tests SSH seems to be waiting rather longer than would be expected |
| 121 | # from these parameters. These values produce a ~10 second wait. |
| 122 | # (in a subshell to avoid clobbering the global settings) |
| 123 | SSH_CONNECT_SETTINGS="$(sed \ |
| 124 | -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \ |
| 125 | -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \ |
| 126 | <<<"${SSH_CONNECT_SETTINGS}")" |
| 127 | remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]' |
| 128 | ) |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 129 | } |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 130 | |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 131 | # Triggers a reboot on a remote device and waits for it to complete. |
| 132 | # |
| 133 | # This function will not return until the SSH server on the remote device |
| 134 | # is available after the reboot. |
| 135 | # |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 136 | function remote_reboot() { |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 137 | info "Rebooting ${FLAGS_remote}..." |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 138 | remote_sh "touch /tmp/awaiting_reboot; reboot" |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 139 | local start_time=${SECONDS} |
| 140 | |
| 141 | # Wait for five seconds before we start polling |
| 142 | sleep 5 |
| 143 | |
| 144 | # Add a hard timeout of 5 minutes before giving up. |
| 145 | local timeout=300 |
| 146 | local timeout_expiry=$(( start_time + timeout )) |
| 147 | while [ ${SECONDS} -lt ${timeout_expiry} ]; do |
| 148 | # Used to throttle the loop -- see step_remaining_time at the bottom. |
| 149 | local step_start_time=${SECONDS} |
| 150 | |
| 151 | local status=0 |
| 152 | _check_if_rebooted || status=$? |
| 153 | |
| 154 | local elapsed=$(( SECONDS - start_time )) |
| 155 | case ${status} in |
| 156 | 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;; |
| 157 | 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;; |
| 158 | 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;; |
| 159 | *) die " internal error" ;; |
| 160 | esac |
| 161 | |
| 162 | # To keep the loop from spinning too fast, delay until it has taken at |
| 163 | # least five seconds. When we are actively trying SSH connections this |
| 164 | # should never happen. |
| 165 | local step_remaining_time=$(( step_start_time + 5 - SECONDS )) |
| 166 | if [ ${step_remaining_time} -gt 0 ]; then |
| 167 | sleep ${step_remaining_time} |
| 168 | fi |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 169 | done |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 170 | die "Reboot has not completed after ${timeout} seconds; giving up." |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 173 | # Called by clients before exiting. |
| 174 | # Part of the remote_access.sh interface but now empty. |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 175 | function cleanup_remote_access() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 176 | true |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 179 | function remote_access_init() { |
| 180 | TMP_PRIVATE_KEY=$TMP/private_key |
| 181 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 182 | if [ -z "$FLAGS_remote" ]; then |
| 183 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 184 | exit 1 |
| 185 | fi |
| 186 | set_up_remote_access |
| 187 | } |