blob: 5b70154ca537c961e93cf67636e614f314bae985 [file] [log] [blame]
Ken Mixter689b9ee2010-01-07 18:23:52 -08001# 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'Connora6db82e2010-01-27 12:11:08 -08007DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\
8ssh_keys/testing_rsa"
Ken Mixter689b9ee2010-01-07 18:23:52 -08009
10DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance"
11DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \
12 "Private key of root account on remote host"
Zelidrag Hornung61d97682010-06-15 11:55:21 -070013DEFINE_integer ssh_port 22 \
14 "SSH port of the remote machine running Chromium OS instance"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070015DEFINE_integer ssh_connect_timeout 30 \
16 "SSH connect timeout in seconds"
17DEFINE_integer ssh_connection_attempts 4 \
18 "SSH connection attempts"
Ken Mixter689b9ee2010-01-07 18:23:52 -080019
Marc Herbert5d519fa2015-06-12 15:15:44 -070020# Returns true if $1 has at least two colons.
21has_two_colons_or_more() {
22 # IPv6 addresses have at least two colons while IPv4 addresses and
23 # hostnames have none.
24 [[ "$1" == *:*:* ]]
25}
26
27# Prints $1 enclosed with brackets if it looks like an IPv6 address
28# and unchanged otherwise.
29brackets_enclosed_if_ipv6() {
30 local rem="$1"
31 if has_two_colons_or_more "${rem}"; then
32 rem="[${rem}]"
33 fi
34 echo "${rem}"
35}
36
Gilad Arnold2ff2f112012-08-28 10:13:05 -070037ssh_connect_settings() {
38 if [[ -n "$SSH_CONNECT_SETTINGS" ]]; then
39 # If connection settings were fixed in an environment variable, just return
40 # those values.
41 echo -n "$SSH_CONNECT_SETTINGS"
42 else
43 # Otherwise, return the default (or user overridden) settings.
44 local settings=(
45 "Protocol=2"
46 "ConnectTimeout=${FLAGS_ssh_connect_timeout}"
47 "ConnectionAttempts=${FLAGS_ssh_connection_attempts}"
48 "ServerAliveInterval=10"
49 "ServerAliveCountMax=3"
50 "StrictHostKeyChecking=no"
51 )
52 printf -- '-o %s ' "${settings[@]}"
53 fi
54}
David Jamesf5850902011-09-30 10:51:48 -070055
Chris Sosaef964302010-04-27 13:21:08 -070056# Copies $1 to $2 on remote host
Mike Frysinger6b1abb22012-05-11 13:44:06 -040057remote_cp_to() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -070058 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -070059 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \
60 root@$FLAGS_remote:$2)
Chris Sosaef964302010-04-27 13:21:08 -070061 return ${PIPESTATUS[0]}
62}
63
Doug Anderson4e678382012-12-07 12:38:54 -080064# Raw rsync access to the remote
65# Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/
66remote_rsync_raw() {
67 rsync -e "ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
68 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \
69 "$@"
70}
71
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070072# Copies a list of remote files specified in file $1 to local location
73# $2. Directory paths in $1 are collapsed into $2.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040074remote_rsync_from() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070075 local rsync_rem
76 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
77 remote_rsync_raw --no-R --files-from="$1" \
78 root@"${rsync_rem}:/" "$2"
Doug Anderson4e678382012-12-07 12:38:54 -080079}
80
81# Send a directory from $1 to $2 on remote host
82#
83# Tries to use rsync -a but will fall back to tar if the remote doesn't
84# have rsync.
85#
86# Use like: remote_send_to /build/board/lib/modules/ /lib/modules/
87remote_send_to() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070088 local rsync_rem
Doug Anderson4e678382012-12-07 12:38:54 -080089 if [ ! -d "$1" ]; then
90 die "$1 must be a directory"
91 fi
92
93 if remote_sh rsync --version >/dev/null 2>&1; then
Marc Herbert5d519fa2015-06-12 15:15:44 -070094 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
95 remote_rsync_raw -a "$1/" root@"${rsync_rem}:$2/"
Doug Anderson4e678382012-12-07 12:38:54 -080096 else
97 tar -C "$1" -cz . | remote_sh tar -C "$2" -xz
98 fi
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070099}
100
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400101_remote_sh() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700102 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700103 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \
104 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -0800105 return ${PIPESTATUS[0]}
106}
107
Chris Sosafaeee5f2011-09-26 16:08:14 -0700108# Wrapper for ssh that runs the commmand given by the args on the remote host
Chris Sosa539b3412012-02-27 14:46:10 -0800109# If an ssh error occurs, re-runs the ssh command.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400110remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -0700111 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -0800112 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -0700113 # 255 indicates an ssh error.
114 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -0800115 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -0700116 else
117 return ${ssh_status}
118 fi
119}
120
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400121remote_sh_raw() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700122 ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700123 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \
124 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -0700125 return $?
126}
127
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400128remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800129 rm -f $TMP_KNOWN_HOSTS
130 remote_sh "$@"
131}
132
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400133set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800134 cp $FLAGS_private_key $TMP_PRIVATE_KEY
135 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -0800136
137 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700138 local output
139 local status=0
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400140 if output=$(remote_sh -n "true" 2>&1); then
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700141 :
142 else
143 status=$?
144 echo "Could not initiate first contact with remote host"
145 echo "$output"
146 fi
147 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -0800148}
149
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700150# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400151learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700152 [ -n "${FLAGS_board}" ] && return
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400153 remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700154 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
155 if [ -z "${FLAGS_board}" ]; then
156 error "Board required"
157 exit 1
158 fi
159 info "Target reports board is ${FLAGS_board}"
160}
161
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400162learn_arch() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800163 [ -n "${FLAGS_arch}" ] && return
164 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -0700165 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800166 if [ -z "${FLAGS_arch}" ]; then
167 error "Arch required"
168 exit 1
169 fi
170 info "Target reports arch is ${FLAGS_arch}"
171}
172
Chris Wolfed91df7a2012-02-29 16:55:48 -0500173# Checks whether a remote device has rebooted successfully.
174#
175# This uses a rapidly-retried SSH connection, which will wait for at most
176# about ten seconds. If the network returns an error (e.g. host unreachable)
177# the actual delay may be shorter.
178#
179# Return values:
180# 0: The device has rebooted successfully
181# 1: The device has not yet rebooted
182# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400183_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500184 (
185 # In my tests SSH seems to be waiting rather longer than would be expected
186 # from these parameters. These values produce a ~10 second wait.
187 # (in a subshell to avoid clobbering the global settings)
188 SSH_CONNECT_SETTINGS="$(sed \
189 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
190 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700191 <<<"$(ssh_connect_settings)")"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500192 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
193 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800194}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800195
Chris Wolfed91df7a2012-02-29 16:55:48 -0500196# Triggers a reboot on a remote device and waits for it to complete.
197#
198# This function will not return until the SSH server on the remote device
199# is available after the reboot.
200#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400201remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500202 info "Rebooting ${FLAGS_remote}..."
Chris Sosa24da49e2011-02-01 17:06:12 -0800203 remote_sh "touch /tmp/awaiting_reboot; reboot"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500204 local start_time=${SECONDS}
205
206 # Wait for five seconds before we start polling
207 sleep 5
208
209 # Add a hard timeout of 5 minutes before giving up.
210 local timeout=300
211 local timeout_expiry=$(( start_time + timeout ))
212 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
213 # Used to throttle the loop -- see step_remaining_time at the bottom.
214 local step_start_time=${SECONDS}
215
216 local status=0
217 _check_if_rebooted || status=$?
218
219 local elapsed=$(( SECONDS - start_time ))
220 case ${status} in
221 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
222 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
223 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
224 *) die " internal error" ;;
225 esac
226
227 # To keep the loop from spinning too fast, delay until it has taken at
228 # least five seconds. When we are actively trying SSH connections this
229 # should never happen.
230 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
231 if [ ${step_remaining_time} -gt 0 ]; then
232 sleep ${step_remaining_time}
233 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800234 done
Chris Wolfed91df7a2012-02-29 16:55:48 -0500235 die "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800236}
237
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800238# Called by clients before exiting.
239# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400240cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800241 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800242}
243
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400244remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800245 TMP_PRIVATE_KEY=$TMP/private_key
246 TMP_KNOWN_HOSTS=$TMP/known_hosts
247 if [ -z "$FLAGS_remote" ]; then
248 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
249 exit 1
250 fi
251 set_up_remote_access
252}