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