| 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" |
| 11 DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \ | 11 DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \ |
| 12 "Private key of root account on remote host" | 12 "Private key of root account on remote host" |
| 13 DEFINE_integer ssh_port 22 \ | 13 DEFINE_integer ssh_port 22 \ |
| 14 "SSH port of the remote machine running Chromium OS instance" | 14 "SSH port of the remote machine running Chromium OS instance" |
| 15 | 15 |
| 16 # Copies $1 to $2 on remote host | 16 # Copies $1 to $2 on remote host |
| 17 function remote_cp_to() { | 17 function remote_cp_to() { |
| 18 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ | 18 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 19 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \ | 19 -o ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS \ |
| 20 -i $TMP_PRIVATE_KEY $1 \ |
| 20 root@$FLAGS_remote:$2) | 21 root@$FLAGS_remote:$2) |
| 21 return ${PIPESTATUS[0]} | 22 return ${PIPESTATUS[0]} |
| 22 } | 23 } |
| 23 | 24 |
| 24 # Copies a list of remote files specified in file $1 to local location | 25 # Copies a list of remote files specified in file $1 to local location |
| 25 # $2. Directory paths in $1 are collapsed into $2. | 26 # $2. Directory paths in $1 are collapsed into $2. |
| 26 function remote_rsync_from() { | 27 function remote_rsync_from() { |
| 27 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ | 28 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 28 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \ | 29 -o ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS \ |
| 30 -i $TMP_PRIVATE_KEY" \ |
| 29 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 | 31 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 |
| 30 } | 32 } |
| 31 | 33 |
| 32 function remote_sh() { | 34 function remote_sh() { |
| 33 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ | 35 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 34 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ | 36 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ |
| 37 -o ServerAliveInterval=60 \ |
| 35 root@$FLAGS_remote "$@") | 38 root@$FLAGS_remote "$@") |
| 36 return ${PIPESTATUS[0]} | 39 return ${PIPESTATUS[0]} |
| 37 } | 40 } |
| 38 | 41 |
| 39 function remote_sh_allow_changed_host_key() { | 42 function remote_sh_allow_changed_host_key() { |
| 40 rm -f $TMP_KNOWN_HOSTS | 43 rm -f $TMP_KNOWN_HOSTS |
| 41 remote_sh "$@" | 44 remote_sh "$@" |
| 42 } | 45 } |
| 43 | 46 |
| 44 function set_up_remote_access() { | 47 function set_up_remote_access() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 56 [ -n "${FLAGS_board}" ] && return | 59 [ -n "${FLAGS_board}" ] && return |
| 57 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release | 60 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
| 58 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) | 61 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 59 if [ -z "${FLAGS_board}" ]; then | 62 if [ -z "${FLAGS_board}" ]; then |
| 60 error "Board required" | 63 error "Board required" |
| 61 exit 1 | 64 exit 1 |
| 62 fi | 65 fi |
| 63 info "Target reports board is ${FLAGS_board}" | 66 info "Target reports board is ${FLAGS_board}" |
| 64 } | 67 } |
| 65 | 68 |
| 66 # Checks to see if pid $1 is running. | 69 function remote_reboot { |
| 67 function is_pid_running() { | 70 info "Rebooting." |
| 68 ps -p ${1} 2>&1 > /dev/null | 71 remote_sh "touch /tmp/awaiting_reboot; reboot" |
| 69 } | 72 local output_file |
| 73 output_file="${TMP}/output" |
| 70 | 74 |
| 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" | |
| 89 while true; do | 75 while true; do |
| 90 REMOTE_OUT="" | 76 REMOTE_OUT="" |
| 91 # This may fail while the machine is down so generate output and a | 77 # This may fail while the machine is down so generate output and a |
| 92 # boolean result to distinguish between down/timeout and real failure | 78 # boolean result to distinguish between down/timeout and real failure |
| 93 ! remote_sh_allow_changed_host_key \ | 79 ! remote_sh_allow_changed_host_key \ |
| 94 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" | 80 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" |
| 95 echo "${REMOTE_OUT}" > "${output_file}" | 81 echo "${REMOTE_OUT}" > "${output_file}" |
| 96 if grep -q "0" "${output_file}"; then | 82 if grep -q "0" "${output_file}"; then |
| 97 if grep -q "1" "${output_file}"; then | 83 if grep -q "1" "${output_file}"; then |
| 98 info "Not yet rebooted" | 84 info "Not yet rebooted" |
| 99 sleep .5 | |
| 100 else | 85 else |
| 101 info "Rebooted and responding" | 86 info "Rebooted and responding" |
| 102 break | 87 break |
| 103 fi | 88 fi |
| 104 fi | 89 fi |
| 90 sleep .5 |
| 105 done | 91 done |
| 106 } | 92 } |
| 107 | 93 |
| 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 | |
| 116 done | |
| 117 } | |
| 118 | |
| 119 # Called by clients before exiting. | 94 # Called by clients before exiting. |
| 120 # Part of the remote_access.sh interface but now empty. | 95 # Part of the remote_access.sh interface but now empty. |
| 121 function cleanup_remote_access() { | 96 function cleanup_remote_access() { |
| 122 true | 97 true |
| 123 } | 98 } |
| 124 | 99 |
| 125 function remote_access_init() { | 100 function remote_access_init() { |
| 126 TMP_PRIVATE_KEY=$TMP/private_key | 101 TMP_PRIVATE_KEY=$TMP/private_key |
| 127 TMP_KNOWN_HOSTS=$TMP/known_hosts | 102 TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 128 if [ -z "$FLAGS_remote" ]; then | 103 if [ -z "$FLAGS_remote" ]; then |
| 129 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" | 104 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 130 exit 1 | 105 exit 1 |
| 131 fi | 106 fi |
| 132 set_up_remote_access | 107 set_up_remote_access |
| 133 } | 108 } |
| OLD | NEW |