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