blob: 68e010d5afe8413452266dee6326da7edf4c9c66 [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"
Ken Mixter689b9ee2010-01-07 18:23:52 -080015
David Jamesf5850902011-09-30 10:51:48 -070016SSH_CONNECT_SETTINGS="-o Protocol=2 -o ConnectTimeout=30 \
17 -o ConnectionAttempts=4 -o ServerAliveInterval=10 \
18 -o ServerAliveCountMax=3 -o StrictHostKeyChecking=no"
19
Chris Sosaef964302010-04-27 13:21:08 -070020# Copies $1 to $2 on remote host
Mike Frysinger6b1abb22012-05-11 13:44:06 -040021remote_cp_to() {
David Jamesf5850902011-09-30 10:51:48 -070022 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \
23 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \
24 root@$FLAGS_remote:$2)
Chris Sosaef964302010-04-27 13:21:08 -070025 return ${PIPESTATUS[0]}
26}
27
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070028# Copies a list of remote files specified in file $1 to local location
29# $2. Directory paths in $1 are collapsed into $2.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040030remote_rsync_from() {
David Jamesf5850902011-09-30 10:51:48 -070031 rsync -e "ssh -p ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \
32 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -080033 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070034}
35
Mike Frysinger6b1abb22012-05-11 13:44:06 -040036_remote_sh() {
David Jamesf5850902011-09-30 10:51:48 -070037 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \
38 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \
39 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -080040 return ${PIPESTATUS[0]}
41}
42
Chris Sosafaeee5f2011-09-26 16:08:14 -070043# Wrapper for ssh that runs the commmand given by the args on the remote host
Chris Sosa539b3412012-02-27 14:46:10 -080044# If an ssh error occurs, re-runs the ssh command.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040045remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -070046 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -080047 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -070048 # 255 indicates an ssh error.
49 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -080050 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -070051 else
52 return ${ssh_status}
53 fi
54}
55
Mike Frysinger6b1abb22012-05-11 13:44:06 -040056remote_sh_raw() {
David Jamesf5850902011-09-30 10:51:48 -070057 ssh -p ${FLAGS_ssh_port} $SSH_CONNECT_SETTINGS \
58 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \
59 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -070060 return $?
61}
62
Mike Frysinger6b1abb22012-05-11 13:44:06 -040063remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -080064 rm -f $TMP_KNOWN_HOSTS
65 remote_sh "$@"
66}
67
Mike Frysinger6b1abb22012-05-11 13:44:06 -040068set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -080069 cp $FLAGS_private_key $TMP_PRIVATE_KEY
70 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -080071
72 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -070073 local output
74 local status=0
75 if output=$(remote_sh "true" 2>&1); then
76 :
77 else
78 status=$?
79 echo "Could not initiate first contact with remote host"
80 echo "$output"
81 fi
82 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -080083}
84
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070085# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -040086learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070087 [ -n "${FLAGS_board}" ] && return
88 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
89 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
90 if [ -z "${FLAGS_board}" ]; then
91 error "Board required"
92 exit 1
93 fi
94 info "Target reports board is ${FLAGS_board}"
95}
96
Mike Frysinger6b1abb22012-05-11 13:44:06 -040097learn_arch() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080098 [ -n "${FLAGS_arch}" ] && return
99 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -0700100 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800101 if [ -z "${FLAGS_arch}" ]; then
102 error "Arch required"
103 exit 1
104 fi
105 info "Target reports arch is ${FLAGS_arch}"
106}
107
Chris Wolfed91df7a2012-02-29 16:55:48 -0500108# Checks whether a remote device has rebooted successfully.
109#
110# This uses a rapidly-retried SSH connection, which will wait for at most
111# about ten seconds. If the network returns an error (e.g. host unreachable)
112# the actual delay may be shorter.
113#
114# Return values:
115# 0: The device has rebooted successfully
116# 1: The device has not yet rebooted
117# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400118_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500119 (
120 # In my tests SSH seems to be waiting rather longer than would be expected
121 # from these parameters. These values produce a ~10 second wait.
122 # (in a subshell to avoid clobbering the global settings)
123 SSH_CONNECT_SETTINGS="$(sed \
124 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
125 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
126 <<<"${SSH_CONNECT_SETTINGS}")"
127 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
128 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800129}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800130
Chris Wolfed91df7a2012-02-29 16:55:48 -0500131# Triggers a reboot on a remote device and waits for it to complete.
132#
133# This function will not return until the SSH server on the remote device
134# is available after the reboot.
135#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400136remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500137 info "Rebooting ${FLAGS_remote}..."
Chris Sosa24da49e2011-02-01 17:06:12 -0800138 remote_sh "touch /tmp/awaiting_reboot; reboot"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500139 local start_time=${SECONDS}
140
141 # Wait for five seconds before we start polling
142 sleep 5
143
144 # Add a hard timeout of 5 minutes before giving up.
145 local timeout=300
146 local timeout_expiry=$(( start_time + timeout ))
147 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
148 # Used to throttle the loop -- see step_remaining_time at the bottom.
149 local step_start_time=${SECONDS}
150
151 local status=0
152 _check_if_rebooted || status=$?
153
154 local elapsed=$(( SECONDS - start_time ))
155 case ${status} in
156 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
157 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
158 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
159 *) die " internal error" ;;
160 esac
161
162 # To keep the loop from spinning too fast, delay until it has taken at
163 # least five seconds. When we are actively trying SSH connections this
164 # should never happen.
165 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
166 if [ ${step_remaining_time} -gt 0 ]; then
167 sleep ${step_remaining_time}
168 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800169 done
Chris Wolfed91df7a2012-02-29 16:55:48 -0500170 die "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800171}
172
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800173# Called by clients before exiting.
174# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400175cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800176 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800177}
178
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400179remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800180 TMP_PRIVATE_KEY=$TMP/private_key
181 TMP_KNOWN_HOSTS=$TMP/known_hosts
182 if [ -z "$FLAGS_remote" ]; then
183 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
184 exit 1
185 fi
186 set_up_remote_access
187}