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 -o \ | 18 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ |
19 UserKnownHostsFile=$TMP_KNOWN_HOSTS $1 root@$FLAGS_remote:$2) | 19 ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS $1 \ |
20 root@$FLAGS_remote:$2) | |
20 return ${PIPESTATUS[0]} | 21 return ${PIPESTATUS[0]} |
21 } | 22 } |
22 | 23 |
23 # Copies a list of remote files specified in file $1 to local location | 24 # Copies a list of remote files specified in file $1 to local location |
24 # $2. Directory paths in $1 are collapsed into $2. | 25 # $2. Directory paths in $1 are collapsed into $2. |
25 function remote_rsync_from() { | 26 function remote_rsync_from() { |
26 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ | 27 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ |
27 UserKnownHostsFile=$TMP_KNOWN_HOSTS" --no-R \ | 28 ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS" \ |
28 --files-from=$1 root@${FLAGS_remote}:/ $2 | 29 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 |
29 } | 30 } |
30 | 31 |
31 function remote_sh() { | 32 function remote_sh() { |
32 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ | 33 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ |
33 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") | 34 ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS \ |
35 root@$FLAGS_remote "$@") | |
34 return ${PIPESTATUS[0]} | 36 return ${PIPESTATUS[0]} |
35 } | 37 } |
36 | 38 |
37 function remote_sh_allow_changed_host_key() { | 39 function remote_sh_allow_changed_host_key() { |
38 rm -f $TMP_KNOWN_HOSTS | 40 rm -f $TMP_KNOWN_HOSTS |
39 remote_sh "$@" | 41 remote_sh "$@" |
40 } | 42 } |
41 | 43 |
42 function set_up_remote_access() { | 44 function set_up_remote_access() { |
43 if [ -z "$SSH_AGENT_PID" ]; then | 45 if [ -z "$SSH_AGENT_PID" ]; then |
(...skipping 17 matching lines...) Expand all Loading... | |
61 [ -n "${FLAGS_board}" ] && return | 63 [ -n "${FLAGS_board}" ] && return |
62 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release | 64 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
63 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) | 65 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
64 if [ -z "${FLAGS_board}" ]; then | 66 if [ -z "${FLAGS_board}" ]; then |
65 error "Board required" | 67 error "Board required" |
66 exit 1 | 68 exit 1 |
67 fi | 69 fi |
68 info "Target reports board is ${FLAGS_board}" | 70 info "Target reports board is ${FLAGS_board}" |
69 } | 71 } |
70 | 72 |
71 # Checks to see if pid $1 is running. | 73 function remote_reboot { |
72 function is_pid_running() { | 74 info "Rebooting." |
73 ps -p ${1} 2>&1 > /dev/null | 75 remote_sh "touch /tmp/awaiting_reboot; reboot" |
74 } | 76 local output_file |
77 output_file="${TMP}/output" | |
75 | 78 |
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" | |
94 while true; do | 79 while true; do |
95 REMOTE_OUT="" | 80 REMOTE_OUT="" |
96 # This may fail while the machine is down so generate output and a | 81 # This may fail while the machine is down so generate output and a |
97 # boolean result to distinguish between down/timeout and real failure | 82 # boolean result to distinguish between down/timeout and real failure |
98 ! remote_sh_allow_changed_host_key \ | 83 ! remote_sh_allow_changed_host_key \ |
99 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" | 84 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" |
100 echo "${REMOTE_OUT}" > "${output_file}" | 85 echo "${REMOTE_OUT}" > "${output_file}" |
101 if grep -q "0" "${output_file}"; then | 86 if grep -q "0" "${output_file}"; then |
102 if grep -q "1" "${output_file}"; then | 87 if grep -q "1" "${output_file}"; then |
103 info "Not yet rebooted" | 88 info "Not yet rebooted" |
104 sleep .5 | |
105 else | 89 else |
106 info "Rebooted and responding" | 90 info "Rebooted and responding" |
107 break | 91 break |
108 fi | 92 fi |
109 fi | 93 fi |
94 sleep .5 | |
petkov
2011/01/14 18:19:23
do you want to move this back into the "not yet re
| |
110 done | 95 done |
111 } | 96 } |
112 | 97 |
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 | |
124 function cleanup_remote_access() { | 98 function cleanup_remote_access() { |
125 # Call this function from the exit trap of the main script. | 99 # Call this function from the exit trap of the main script. |
126 # Iff we started ssh-agent, be nice and clean it up. | 100 # Iff we started ssh-agent, be nice and clean it up. |
127 # Note, only works if called from the main script - no subshells. | 101 # Note, only works if called from the main script - no subshells. |
128 if [[ 1 -eq ${OWN_SSH_AGENT} ]] | 102 if [[ 1 -eq ${OWN_SSH_AGENT} ]] |
129 then | 103 then |
130 kill ${SSH_AGENT_PID} 2>/dev/null | 104 kill ${SSH_AGENT_PID} 2>/dev/null |
131 unset SSH_AGENT_PID SSH_AUTH_SOCK | 105 unset SSH_AGENT_PID SSH_AUTH_SOCK |
132 fi | 106 fi |
133 } | 107 } |
134 | 108 |
135 function remote_access_init() { | 109 function remote_access_init() { |
136 TMP_PRIVATE_KEY=$TMP/private_key | 110 TMP_PRIVATE_KEY=$TMP/private_key |
137 TMP_KNOWN_HOSTS=$TMP/known_hosts | 111 TMP_KNOWN_HOSTS=$TMP/known_hosts |
138 if [ -z "$FLAGS_remote" ]; then | 112 if [ -z "$FLAGS_remote" ]; then |
139 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" | 113 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
140 exit 1 | 114 exit 1 |
141 fi | 115 fi |
142 set_up_remote_access | 116 set_up_remote_access |
143 } | 117 } |
OLD | NEW |