blob: 5995622480cc0a11bdcb56a60b23c8c43715060d [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
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070021function remote_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.
30function remote_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
Chris Sosa539b3412012-02-27 14:46:10 -080036function _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.
Chris Sosafaeee5f2011-09-26 16:08:14 -070045function remote_sh() {
46 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
Andrew de los Reyese08639b2011-09-21 15:44:05 -070056function remote_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
Ken Mixter689b9ee2010-01-07 18:23:52 -080063function remote_sh_allow_changed_host_key() {
64 rm -f $TMP_KNOWN_HOSTS
65 remote_sh "$@"
66}
67
68function set_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
86function learn_board() {
87 [ -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
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080097function learn_arch() {
98 [ -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 Sosa24da49e2011-02-01 17:06:12 -0800108# Checks to see if pid $1 is running.
109function is_pid_running() {
110 ps -p ${1} 2>&1 > /dev/null
111}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800112
Chris Sosa24da49e2011-02-01 17:06:12 -0800113# Wait function given an additional timeout argument.
114# $1 - pid to wait on.
115# $2 - timeout to wait for.
116function wait_with_timeout() {
117 local pid=$1
118 local timeout=$2
119 local -r TIMEOUT_INC=1
120 local current_timeout=0
121 while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do
122 sleep ${TIMEOUT_INC}
123 current_timeout=$((current_timeout + TIMEOUT_INC))
124 done
125 ! is_pid_running ${pid}
126}
127
128# Checks to see if a machine has rebooted using the presence of a tmp file.
129function check_if_rebooted() {
130 local output_file="${TMP}/output"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800131 while true; do
132 REMOTE_OUT=""
133 # This may fail while the machine is down so generate output and a
134 # boolean result to distinguish between down/timeout and real failure
135 ! remote_sh_allow_changed_host_key \
136 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true"
137 echo "${REMOTE_OUT}" > "${output_file}"
138 if grep -q "0" "${output_file}"; then
139 if grep -q "1" "${output_file}"; then
140 info "Not yet rebooted"
Chris Sosa24da49e2011-02-01 17:06:12 -0800141 sleep .5
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800142 else
143 info "Rebooted and responding"
144 break
145 fi
146 fi
Chris Sosa24da49e2011-02-01 17:06:12 -0800147 done
148}
149
150function remote_reboot() {
151 info "Rebooting."
152 remote_sh "touch /tmp/awaiting_reboot; reboot"
153 while true; do
154 check_if_rebooted &
155 local pid=$!
156 wait_with_timeout ${pid} 30 && break
157 ! kill -9 ${pid} 2> /dev/null
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800158 done
159}
160
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800161# Called by clients before exiting.
162# Part of the remote_access.sh interface but now empty.
Sean O'Connor9969ce92010-02-01 17:10:03 -0800163function cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800164 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800165}
166
Ken Mixter689b9ee2010-01-07 18:23:52 -0800167function remote_access_init() {
168 TMP_PRIVATE_KEY=$TMP/private_key
169 TMP_KNOWN_HOSTS=$TMP/known_hosts
170 if [ -z "$FLAGS_remote" ]; then
171 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
172 exit 1
173 fi
174 set_up_remote_access
175}