| OLD | NEW |
| 1 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # Library for setting up remote access and running remote commands. | 5 # Library for setting up remote access and running remote commands. |
| 6 | 6 |
| 7 DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\ | 7 DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\ |
| 8 ssh_keys/testing_rsa" | 8 ssh_keys/testing_rsa" |
| 9 | 9 |
| 10 DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" | 10 DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 UserKnownHostsFile=$TMP_KNOWN_HOSTS" --no-R \ | 27 UserKnownHostsFile=$TMP_KNOWN_HOSTS" --no-R \ |
| 28 --files-from=$1 root@${FLAGS_remote}:/ $2 | 28 --files-from=$1 root@${FLAGS_remote}:/ $2 |
| 29 } | 29 } |
| 30 | 30 |
| 31 function remote_sh() { | 31 function remote_sh() { |
| 32 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ | 32 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ |
| 33 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") | 33 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") |
| 34 return ${PIPESTATUS[0]} | 34 return ${PIPESTATUS[0]} |
| 35 } | 35 } |
| 36 | 36 |
| 37 # Checks to see if pid $1 is running. | |
| 38 function is_pid_running() { | |
| 39 ps -p ${1} 2>&1 > /dev/null | |
| 40 } | |
| 41 | |
| 42 # Wait function given an additional timeout argument. | |
| 43 # $1 - pid to wait on. | |
| 44 # $2 - timeout to wait for. | |
| 45 function wait_with_timeout() { | |
| 46 local pid=$1 | |
| 47 local timeout=$2 | |
| 48 local -r TIMEOUT_INC=1 | |
| 49 local current_timeout=0 | |
| 50 while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do | |
| 51 sleep ${TIMEOUT_INC} | |
| 52 current_timeout=$((current_timeout + TIMEOUT_INC)) | |
| 53 done | |
| 54 is_pid_running ${pid} | |
| 55 } | |
| 56 | |
| 57 # Robust ping that will monitor ssh and not hang even if ssh hangs. | |
| 58 function ping_ssh() { | |
| 59 remote_sh "true" & | |
| 60 local pid=$! | |
| 61 wait_with_timeout ${pid} 5 | |
| 62 ! kill -9 ${pid} 2> /dev/null | |
| 63 } | |
| 64 | |
| 65 function remote_sh_allow_changed_host_key() { | 37 function remote_sh_allow_changed_host_key() { |
| 66 rm -f $TMP_KNOWN_HOSTS | 38 rm -f $TMP_KNOWN_HOSTS |
| 67 ping_ssh | |
| 68 remote_sh "$@" | 39 remote_sh "$@" |
| 69 } | 40 } |
| 70 | 41 |
| 71 function set_up_remote_access() { | 42 function set_up_remote_access() { |
| 72 if [ -z "$SSH_AGENT_PID" ]; then | 43 if [ -z "$SSH_AGENT_PID" ]; then |
| 73 eval $(ssh-agent) | 44 eval $(ssh-agent) |
| 74 OWN_SSH_AGENT=1 | 45 OWN_SSH_AGENT=1 |
| 75 else | 46 else |
| 76 OWN_SSH_AGENT=0 | 47 OWN_SSH_AGENT=0 |
| 77 fi | 48 fi |
| (...skipping 12 matching lines...) Expand all Loading... |
| 90 [ -n "${FLAGS_board}" ] && return | 61 [ -n "${FLAGS_board}" ] && return |
| 91 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release | 62 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
| 92 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) | 63 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 93 if [ -z "${FLAGS_board}" ]; then | 64 if [ -z "${FLAGS_board}" ]; then |
| 94 error "Board required" | 65 error "Board required" |
| 95 exit 1 | 66 exit 1 |
| 96 fi | 67 fi |
| 97 info "Target reports board is ${FLAGS_board}" | 68 info "Target reports board is ${FLAGS_board}" |
| 98 } | 69 } |
| 99 | 70 |
| 100 function remote_reboot { | 71 # Checks to see if pid $1 is running. |
| 101 info "Rebooting." | 72 function is_pid_running() { |
| 102 remote_sh "touch /tmp/awaiting_reboot; reboot" | 73 ps -p ${1} 2>&1 > /dev/null |
| 103 local output_file | 74 } |
| 104 output_file="${TMP}/output" | |
| 105 | 75 |
| 76 # Wait function given an additional timeout argument. |
| 77 # $1 - pid to wait on. |
| 78 # $2 - timeout to wait for. |
| 79 function wait_with_timeout() { |
| 80 local pid=$1 |
| 81 local timeout=$2 |
| 82 local -r TIMEOUT_INC=1 |
| 83 local current_timeout=0 |
| 84 while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do |
| 85 sleep ${TIMEOUT_INC} |
| 86 current_timeout=$((current_timeout + TIMEOUT_INC)) |
| 87 done |
| 88 ! is_pid_running ${pid} |
| 89 } |
| 90 |
| 91 # Checks to see if a machine has rebooted using the presence of a tmp file. |
| 92 function check_if_rebooted() { |
| 93 local output_file="${TMP}/output" |
| 106 while true; do | 94 while true; do |
| 107 REMOTE_OUT="" | 95 REMOTE_OUT="" |
| 108 # This may fail while the machine is down so generate output and a | 96 # This may fail while the machine is down so generate output and a |
| 109 # boolean result to distinguish between down/timeout and real failure | 97 # boolean result to distinguish between down/timeout and real failure |
| 110 ! remote_sh_allow_changed_host_key \ | 98 ! remote_sh_allow_changed_host_key \ |
| 111 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" | 99 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" |
| 112 echo "${REMOTE_OUT}" > "${output_file}" | 100 echo "${REMOTE_OUT}" > "${output_file}" |
| 113 if grep -q "0" "${output_file}"; then | 101 if grep -q "0" "${output_file}"; then |
| 114 if grep -q "1" "${output_file}"; then | 102 if grep -q "1" "${output_file}"; then |
| 115 info "Not yet rebooted" | 103 info "Not yet rebooted" |
| 104 sleep .5 |
| 116 else | 105 else |
| 117 info "Rebooted and responding" | 106 info "Rebooted and responding" |
| 118 break | 107 break |
| 119 fi | 108 fi |
| 120 fi | 109 fi |
| 121 sleep .5 | |
| 122 done | 110 done |
| 123 } | 111 } |
| 124 | 112 |
| 113 function remote_reboot() { |
| 114 info "Rebooting." |
| 115 remote_sh "touch /tmp/awaiting_reboot; reboot" |
| 116 while true; do |
| 117 check_if_rebooted & |
| 118 local pid=$! |
| 119 wait_with_timeout ${pid} 30 && break |
| 120 ! kill -9 ${pid} 2> /dev/null |
| 121 done |
| 122 } |
| 123 |
| 125 function cleanup_remote_access() { | 124 function cleanup_remote_access() { |
| 126 # Call this function from the exit trap of the main script. | 125 # Call this function from the exit trap of the main script. |
| 127 # Iff we started ssh-agent, be nice and clean it up. | 126 # Iff we started ssh-agent, be nice and clean it up. |
| 128 # Note, only works if called from the main script - no subshells. | 127 # Note, only works if called from the main script - no subshells. |
| 129 if [[ 1 -eq ${OWN_SSH_AGENT} ]] | 128 if [[ 1 -eq ${OWN_SSH_AGENT} ]] |
| 130 then | 129 then |
| 131 kill ${SSH_AGENT_PID} 2>/dev/null | 130 kill ${SSH_AGENT_PID} 2>/dev/null |
| 132 unset SSH_AGENT_PID SSH_AUTH_SOCK | 131 unset SSH_AGENT_PID SSH_AUTH_SOCK |
| 133 fi | 132 fi |
| 134 } | 133 } |
| 135 | 134 |
| 136 function remote_access_init() { | 135 function remote_access_init() { |
| 137 TMP_PRIVATE_KEY=$TMP/private_key | 136 TMP_PRIVATE_KEY=$TMP/private_key |
| 138 TMP_KNOWN_HOSTS=$TMP/known_hosts | 137 TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 139 if [ -z "$FLAGS_remote" ]; then | 138 if [ -z "$FLAGS_remote" ]; then |
| 140 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" | 139 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 141 exit 1 | 140 exit 1 |
| 142 fi | 141 fi |
| 143 set_up_remote_access | 142 set_up_remote_access |
| 144 } | 143 } |
| OLD | NEW |