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 | 78476ab | 2011-01-13 11:16:51 -0800 | [diff] [blame] | 18 | REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 19 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \ |
| 20 | 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 | 78476ab | 2011-01-13 11:16:51 -0800 | [diff] [blame] | 27 | rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 28 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \ |
| 29 | --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 32 | function remote_sh() { |
Mandeep Singh Baines | 78476ab | 2011-01-13 11:16:51 -0800 | [diff] [blame] | 33 | REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 34 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ |
| 35 | root@$FLAGS_remote "$@") |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 36 | return ${PIPESTATUS[0]} |
| 37 | } |
| 38 | |
| 39 | function remote_sh_allow_changed_host_key() { |
| 40 | rm -f $TMP_KNOWN_HOSTS |
| 41 | remote_sh "$@" |
| 42 | } |
| 43 | |
| 44 | function set_up_remote_access() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 45 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 46 | chmod 0400 $TMP_PRIVATE_KEY |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 47 | |
| 48 | # Verify the client is reachable before continuing |
| 49 | echo "Initiating first contact with remote host" |
| 50 | remote_sh "true" |
| 51 | echo "Connection OK" |
| 52 | } |
| 53 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 54 | # Ask the target what board it is |
| 55 | function learn_board() { |
| 56 | [ -n "${FLAGS_board}" ] && return |
| 57 | remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
| 58 | FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 59 | if [ -z "${FLAGS_board}" ]; then |
| 60 | error "Board required" |
| 61 | exit 1 |
| 62 | fi |
| 63 | info "Target reports board is ${FLAGS_board}" |
| 64 | } |
| 65 | |
Chris Sosa | bb4661d | 2011-01-13 11:19:02 -0800 | [diff] [blame^] | 66 | # Checks to see if pid $1 is running. |
| 67 | function is_pid_running() { |
| 68 | ps -p ${1} 2>&1 > /dev/null |
| 69 | } |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 70 | |
Chris Sosa | bb4661d | 2011-01-13 11:19:02 -0800 | [diff] [blame^] | 71 | # Wait function given an additional timeout argument. |
| 72 | # $1 - pid to wait on. |
| 73 | # $2 - timeout to wait for. |
| 74 | function wait_with_timeout() { |
| 75 | local pid=$1 |
| 76 | local timeout=$2 |
| 77 | local -r TIMEOUT_INC=1 |
| 78 | local current_timeout=0 |
| 79 | while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do |
| 80 | sleep ${TIMEOUT_INC} |
| 81 | current_timeout=$((current_timeout + TIMEOUT_INC)) |
| 82 | done |
| 83 | ! is_pid_running ${pid} |
| 84 | } |
| 85 | |
| 86 | # Checks to see if a machine has rebooted using the presence of a tmp file. |
| 87 | function check_if_rebooted() { |
| 88 | local output_file="${TMP}/output" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 89 | while true; do |
| 90 | REMOTE_OUT="" |
| 91 | # This may fail while the machine is down so generate output and a |
| 92 | # boolean result to distinguish between down/timeout and real failure |
| 93 | ! remote_sh_allow_changed_host_key \ |
| 94 | "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" |
| 95 | echo "${REMOTE_OUT}" > "${output_file}" |
| 96 | if grep -q "0" "${output_file}"; then |
| 97 | if grep -q "1" "${output_file}"; then |
| 98 | info "Not yet rebooted" |
Chris Sosa | bb4661d | 2011-01-13 11:19:02 -0800 | [diff] [blame^] | 99 | sleep .5 |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 100 | else |
| 101 | info "Rebooted and responding" |
| 102 | break |
| 103 | fi |
| 104 | fi |
Chris Sosa | bb4661d | 2011-01-13 11:19:02 -0800 | [diff] [blame^] | 105 | done |
| 106 | } |
| 107 | |
| 108 | function remote_reboot() { |
| 109 | info "Rebooting." |
| 110 | remote_sh "touch /tmp/awaiting_reboot; reboot" |
| 111 | while true; do |
| 112 | check_if_rebooted & |
| 113 | local pid=$! |
| 114 | wait_with_timeout ${pid} 30 && break |
| 115 | ! kill -9 ${pid} 2> /dev/null |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 116 | done |
| 117 | } |
| 118 | |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 119 | function cleanup_remote_access() { |
| 120 | # Call this function from the exit trap of the main script. |
| 121 | # Iff we started ssh-agent, be nice and clean it up. |
| 122 | # Note, only works if called from the main script - no subshells. |
| 123 | if [[ 1 -eq ${OWN_SSH_AGENT} ]] |
| 124 | then |
| 125 | kill ${SSH_AGENT_PID} 2>/dev/null |
| 126 | unset SSH_AGENT_PID SSH_AUTH_SOCK |
| 127 | fi |
| 128 | } |
| 129 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 130 | function remote_access_init() { |
| 131 | TMP_PRIVATE_KEY=$TMP/private_key |
| 132 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 133 | if [ -z "$FLAGS_remote" ]; then |
| 134 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 135 | exit 1 |
| 136 | fi |
| 137 | set_up_remote_access |
| 138 | } |