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 ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS \ | 19 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \ |
20 -i $TMP_PRIVATE_KEY $1 \ | |
21 root@$FLAGS_remote:$2) | 20 root@$FLAGS_remote:$2) |
22 return ${PIPESTATUS[0]} | 21 return ${PIPESTATUS[0]} |
23 } | 22 } |
24 | 23 |
25 # 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 |
26 # $2. Directory paths in $1 are collapsed into $2. | 25 # $2. Directory paths in $1 are collapsed into $2. |
27 function remote_rsync_from() { | 26 function remote_rsync_from() { |
28 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ | 27 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
29 -o ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS \ | 28 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \ |
30 -i $TMP_PRIVATE_KEY" \ | |
31 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 | 29 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 |
32 } | 30 } |
33 | 31 |
34 function remote_sh() { | 32 function remote_sh() { |
35 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ | 33 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
36 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ | 34 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ |
37 -o ServerAliveInterval=60 \ | |
38 root@$FLAGS_remote "$@") | 35 root@$FLAGS_remote "$@") |
39 return ${PIPESTATUS[0]} | 36 return ${PIPESTATUS[0]} |
40 } | 37 } |
41 | 38 |
42 function remote_sh_allow_changed_host_key() { | 39 function remote_sh_allow_changed_host_key() { |
43 rm -f $TMP_KNOWN_HOSTS | 40 rm -f $TMP_KNOWN_HOSTS |
44 remote_sh "$@" | 41 remote_sh "$@" |
45 } | 42 } |
46 | 43 |
47 function set_up_remote_access() { | 44 function set_up_remote_access() { |
(...skipping 22 matching lines...) Expand all Loading... |
70 [ -n "${FLAGS_arch}" ] && return | 67 [ -n "${FLAGS_arch}" ] && return |
71 remote_sh uname -m | 68 remote_sh uname -m |
72 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed s/armv7l/arm/g) | 69 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed s/armv7l/arm/g) |
73 if [ -z "${FLAGS_arch}" ]; then | 70 if [ -z "${FLAGS_arch}" ]; then |
74 error "Arch required" | 71 error "Arch required" |
75 exit 1 | 72 exit 1 |
76 fi | 73 fi |
77 info "Target reports arch is ${FLAGS_arch}" | 74 info "Target reports arch is ${FLAGS_arch}" |
78 } | 75 } |
79 | 76 |
80 function remote_reboot { | 77 # Checks to see if pid $1 is running. |
81 info "Rebooting." | 78 function is_pid_running() { |
82 remote_sh "touch /tmp/awaiting_reboot; reboot" | 79 ps -p ${1} 2>&1 > /dev/null |
83 local output_file | 80 } |
84 output_file="${TMP}/output" | |
85 | 81 |
| 82 # Wait function given an additional timeout argument. |
| 83 # $1 - pid to wait on. |
| 84 # $2 - timeout to wait for. |
| 85 function wait_with_timeout() { |
| 86 local pid=$1 |
| 87 local timeout=$2 |
| 88 local -r TIMEOUT_INC=1 |
| 89 local current_timeout=0 |
| 90 while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do |
| 91 sleep ${TIMEOUT_INC} |
| 92 current_timeout=$((current_timeout + TIMEOUT_INC)) |
| 93 done |
| 94 ! is_pid_running ${pid} |
| 95 } |
| 96 |
| 97 # Checks to see if a machine has rebooted using the presence of a tmp file. |
| 98 function check_if_rebooted() { |
| 99 local output_file="${TMP}/output" |
86 while true; do | 100 while true; do |
87 REMOTE_OUT="" | 101 REMOTE_OUT="" |
88 # This may fail while the machine is down so generate output and a | 102 # This may fail while the machine is down so generate output and a |
89 # boolean result to distinguish between down/timeout and real failure | 103 # boolean result to distinguish between down/timeout and real failure |
90 ! remote_sh_allow_changed_host_key \ | 104 ! remote_sh_allow_changed_host_key \ |
91 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" | 105 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" |
92 echo "${REMOTE_OUT}" > "${output_file}" | 106 echo "${REMOTE_OUT}" > "${output_file}" |
93 if grep -q "0" "${output_file}"; then | 107 if grep -q "0" "${output_file}"; then |
94 if grep -q "1" "${output_file}"; then | 108 if grep -q "1" "${output_file}"; then |
95 info "Not yet rebooted" | 109 info "Not yet rebooted" |
| 110 sleep .5 |
96 else | 111 else |
97 info "Rebooted and responding" | 112 info "Rebooted and responding" |
98 break | 113 break |
99 fi | 114 fi |
100 fi | 115 fi |
101 sleep .5 | |
102 done | 116 done |
103 } | 117 } |
104 | 118 |
| 119 function remote_reboot() { |
| 120 info "Rebooting." |
| 121 remote_sh "touch /tmp/awaiting_reboot; reboot" |
| 122 while true; do |
| 123 check_if_rebooted & |
| 124 local pid=$! |
| 125 wait_with_timeout ${pid} 30 && break |
| 126 ! kill -9 ${pid} 2> /dev/null |
| 127 done |
| 128 } |
| 129 |
105 # Called by clients before exiting. | 130 # Called by clients before exiting. |
106 # Part of the remote_access.sh interface but now empty. | 131 # Part of the remote_access.sh interface but now empty. |
107 function cleanup_remote_access() { | 132 function cleanup_remote_access() { |
108 true | 133 true |
109 } | 134 } |
110 | 135 |
111 function remote_access_init() { | 136 function remote_access_init() { |
112 TMP_PRIVATE_KEY=$TMP/private_key | 137 TMP_PRIVATE_KEY=$TMP/private_key |
113 TMP_KNOWN_HOSTS=$TMP/known_hosts | 138 TMP_KNOWN_HOSTS=$TMP/known_hosts |
114 if [ -z "$FLAGS_remote" ]; then | 139 if [ -z "$FLAGS_remote" ]; then |
115 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" | 140 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
116 exit 1 | 141 exit 1 |
117 fi | 142 fi |
118 set_up_remote_access | 143 set_up_remote_access |
119 } | 144 } |
OLD | NEW |