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