blob: 4b0202a607d10361df11b8441eba9651a23721ca [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"
Dmitry Torokhovec132152016-05-13 11:22:59 -070051 "IdentitiesOnly=yes"
52 "IdentityFile=${TMP_PRIVATE_KEY}"
53 "UserKnownHostsFile=${TMP_KNOWN_HOSTS}"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070054 )
55 printf -- '-o %s ' "${settings[@]}"
56 fi
57}
David Jamesf5850902011-09-30 10:51:48 -070058
Chris Sosaef964302010-04-27 13:21:08 -070059# Copies $1 to $2 on remote host
Mike Frysinger6b1abb22012-05-11 13:44:06 -040060remote_cp_to() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -070061 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $(ssh_connect_settings) \
Dmitry Torokhovec132152016-05-13 11:22:59 -070062 $1 root@$FLAGS_remote:$2)
Chris Sosaef964302010-04-27 13:21:08 -070063 return ${PIPESTATUS[0]}
64}
65
Doug Anderson4e678382012-12-07 12:38:54 -080066# Raw rsync access to the remote
67# Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/
68remote_rsync_raw() {
Dmitry Torokhovec132152016-05-13 11:22:59 -070069 rsync -e "ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings)" "$@"
Doug Anderson4e678382012-12-07 12:38:54 -080070}
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 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -0800104 return ${PIPESTATUS[0]}
105}
106
Chris Sosafaeee5f2011-09-26 16:08:14 -0700107# Wrapper for ssh that runs the commmand given by the args on the remote host
Chris Sosa539b3412012-02-27 14:46:10 -0800108# If an ssh error occurs, re-runs the ssh command.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400109remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -0700110 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -0800111 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -0700112 # 255 indicates an ssh error.
113 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -0800114 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -0700115 else
116 return ${ssh_status}
117 fi
118}
119
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400120remote_sh_raw() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700121 ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700122 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -0700123 return $?
124}
125
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400126remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800127 rm -f $TMP_KNOWN_HOSTS
128 remote_sh "$@"
129}
130
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400131set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800132 cp $FLAGS_private_key $TMP_PRIVATE_KEY
133 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -0800134
135 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700136 local output
137 local status=0
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400138 if output=$(remote_sh -n "true" 2>&1); then
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700139 :
140 else
141 status=$?
142 echo "Could not initiate first contact with remote host"
143 echo "$output"
144 fi
145 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -0800146}
147
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700148# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400149learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700150 [ -n "${FLAGS_board}" ] && return
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400151 remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700152 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
153 if [ -z "${FLAGS_board}" ]; then
154 error "Board required"
155 exit 1
156 fi
157 info "Target reports board is ${FLAGS_board}"
158}
159
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400160learn_arch() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800161 [ -n "${FLAGS_arch}" ] && return
162 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -0700163 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800164 if [ -z "${FLAGS_arch}" ]; then
165 error "Arch required"
166 exit 1
167 fi
168 info "Target reports arch is ${FLAGS_arch}"
169}
170
Chris Wolfed91df7a2012-02-29 16:55:48 -0500171# Checks whether a remote device has rebooted successfully.
172#
173# This uses a rapidly-retried SSH connection, which will wait for at most
174# about ten seconds. If the network returns an error (e.g. host unreachable)
175# the actual delay may be shorter.
176#
177# Return values:
178# 0: The device has rebooted successfully
179# 1: The device has not yet rebooted
180# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400181_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500182 (
183 # In my tests SSH seems to be waiting rather longer than would be expected
184 # from these parameters. These values produce a ~10 second wait.
185 # (in a subshell to avoid clobbering the global settings)
186 SSH_CONNECT_SETTINGS="$(sed \
187 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
188 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700189 <<<"$(ssh_connect_settings)")"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500190 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
191 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800192}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800193
Chris Wolfed91df7a2012-02-29 16:55:48 -0500194# Triggers a reboot on a remote device and waits for it to complete.
195#
196# This function will not return until the SSH server on the remote device
197# is available after the reboot.
198#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400199remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500200 info "Rebooting ${FLAGS_remote}..."
Chris Sosa24da49e2011-02-01 17:06:12 -0800201 remote_sh "touch /tmp/awaiting_reboot; reboot"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500202 local start_time=${SECONDS}
203
204 # Wait for five seconds before we start polling
205 sleep 5
206
207 # Add a hard timeout of 5 minutes before giving up.
208 local timeout=300
209 local timeout_expiry=$(( start_time + timeout ))
210 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
211 # Used to throttle the loop -- see step_remaining_time at the bottom.
212 local step_start_time=${SECONDS}
213
214 local status=0
215 _check_if_rebooted || status=$?
216
217 local elapsed=$(( SECONDS - start_time ))
218 case ${status} in
219 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
220 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
221 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
222 *) die " internal error" ;;
223 esac
224
225 # To keep the loop from spinning too fast, delay until it has taken at
226 # least five seconds. When we are actively trying SSH connections this
227 # should never happen.
228 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
229 if [ ${step_remaining_time} -gt 0 ]; then
230 sleep ${step_remaining_time}
231 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800232 done
Chris Wolfed91df7a2012-02-29 16:55:48 -0500233 die "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800234}
235
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800236# Called by clients before exiting.
237# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400238cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800239 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800240}
241
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400242remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800243 TMP_PRIVATE_KEY=$TMP/private_key
244 TMP_KNOWN_HOSTS=$TMP/known_hosts
245 if [ -z "$FLAGS_remote" ]; then
246 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
247 exit 1
248 fi
249 set_up_remote_access
250}