blob: f778fae3452e4d6f8befcdfa32c7a5e67d8aa4df [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
Chris Sosaef964302010-04-27 13:21:08 -070016# Copies $1 to $2 on remote host
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070017function remote_cp_to() {
Mandeep Singh Baines78476ab2011-01-13 11:16:51 -080018 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \
19 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \
20 root@$FLAGS_remote:$2)
Chris Sosaef964302010-04-27 13:21:08 -070021 return ${PIPESTATUS[0]}
22}
23
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070024# Copies a list of remote files specified in file $1 to local location
25# $2. Directory paths in $1 are collapsed into $2.
26function remote_rsync_from() {
Mandeep Singh Baines78476ab2011-01-13 11:16:51 -080027 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \
28 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \
29 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070030}
31
Ken Mixter689b9ee2010-01-07 18:23:52 -080032function remote_sh() {
Mandeep Singh Baines78476ab2011-01-13 11:16:51 -080033 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \
34 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \
35 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -080036 return ${PIPESTATUS[0]}
37}
38
39function remote_sh_allow_changed_host_key() {
40 rm -f $TMP_KNOWN_HOSTS
41 remote_sh "$@"
42}
43
44function set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -080045 cp $FLAGS_private_key $TMP_PRIVATE_KEY
46 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -080047
48 # Verify the client is reachable before continuing
49 echo "Initiating first contact with remote host"
50 remote_sh "true"
51 echo "Connection OK"
52}
53
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070054# Ask the target what board it is
55function learn_board() {
56 [ -n "${FLAGS_board}" ] && return
57 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
58 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
59 if [ -z "${FLAGS_board}" ]; then
60 error "Board required"
61 exit 1
62 fi
63 info "Target reports board is ${FLAGS_board}"
64}
65
Chris Sosabb4661d2011-01-13 11:19:02 -080066# Checks to see if pid $1 is running.
67function is_pid_running() {
68 ps -p ${1} 2>&1 > /dev/null
69}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080070
Chris Sosabb4661d2011-01-13 11:19:02 -080071# Wait function given an additional timeout argument.
72# $1 - pid to wait on.
73# $2 - timeout to wait for.
74function wait_with_timeout() {
75 local pid=$1
76 local timeout=$2
77 local -r TIMEOUT_INC=1
78 local current_timeout=0
79 while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do
80 sleep ${TIMEOUT_INC}
81 current_timeout=$((current_timeout + TIMEOUT_INC))
82 done
83 ! is_pid_running ${pid}
84}
85
86# Checks to see if a machine has rebooted using the presence of a tmp file.
87function check_if_rebooted() {
88 local output_file="${TMP}/output"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080089 while true; do
90 REMOTE_OUT=""
91 # This may fail while the machine is down so generate output and a
92 # boolean result to distinguish between down/timeout and real failure
93 ! remote_sh_allow_changed_host_key \
94 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true"
95 echo "${REMOTE_OUT}" > "${output_file}"
96 if grep -q "0" "${output_file}"; then
97 if grep -q "1" "${output_file}"; then
98 info "Not yet rebooted"
Chris Sosabb4661d2011-01-13 11:19:02 -080099 sleep .5
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800100 else
101 info "Rebooted and responding"
102 break
103 fi
104 fi
Chris Sosabb4661d2011-01-13 11:19:02 -0800105 done
106}
107
108function remote_reboot() {
109 info "Rebooting."
110 remote_sh "touch /tmp/awaiting_reboot; reboot"
111 while true; do
112 check_if_rebooted &
113 local pid=$!
114 wait_with_timeout ${pid} 30 && break
115 ! kill -9 ${pid} 2> /dev/null
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800116 done
117}
118
Sean O'Connor9969ce92010-02-01 17:10:03 -0800119function cleanup_remote_access() {
120 # Call this function from the exit trap of the main script.
121 # Iff we started ssh-agent, be nice and clean it up.
122 # Note, only works if called from the main script - no subshells.
123 if [[ 1 -eq ${OWN_SSH_AGENT} ]]
124 then
125 kill ${SSH_AGENT_PID} 2>/dev/null
126 unset SSH_AGENT_PID SSH_AUTH_SOCK
127 fi
128}
129
Ken Mixter689b9ee2010-01-07 18:23:52 -0800130function remote_access_init() {
131 TMP_PRIVATE_KEY=$TMP/private_key
132 TMP_KNOWN_HOSTS=$TMP/known_hosts
133 if [ -z "$FLAGS_remote" ]; then
134 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
135 exit 1
136 fi
137 set_up_remote_access
138}