blob: 2c482ccdd30e2e7651ae06aaaefb64e4f48d5aa6 [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 Bainesaef91ad2011-01-14 14:17:25 -080018 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \
David James985cd882011-08-21 10:02:30 -070019 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \
20 -i $TMP_PRIVATE_KEY $1 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 Bainesaef91ad2011-01-14 14:17:25 -080027 rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \
David James985cd882011-08-21 10:02:30 -070028 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \
29 -i $TMP_PRIVATE_KEY" \
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -080030 --no-R --files-from=$1 root@${FLAGS_remote}:/ $2
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070031}
32
Ken Mixter689b9ee2010-01-07 18:23:52 -080033function remote_sh() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -080034 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \
David James985cd882011-08-21 10:02:30 -070035 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \
36 -i $TMP_PRIVATE_KEY root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -080037 return ${PIPESTATUS[0]}
38}
39
Andrew de los Reyese08639b2011-09-21 15:44:05 -070040function remote_sh_raw() {
41 ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \
42 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -o ConnectTimeout=120 \
43 -i $TMP_PRIVATE_KEY $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
44 return $?
45}
46
Ken Mixter689b9ee2010-01-07 18:23:52 -080047function remote_sh_allow_changed_host_key() {
48 rm -f $TMP_KNOWN_HOSTS
49 remote_sh "$@"
50}
51
52function set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -080053 cp $FLAGS_private_key $TMP_PRIVATE_KEY
54 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -080055
56 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -070057 local output
58 local status=0
59 if output=$(remote_sh "true" 2>&1); then
60 :
61 else
62 status=$?
63 echo "Could not initiate first contact with remote host"
64 echo "$output"
65 fi
66 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -080067}
68
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070069# Ask the target what board it is
70function learn_board() {
71 [ -n "${FLAGS_board}" ] && return
72 remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
73 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
74 if [ -z "${FLAGS_board}" ]; then
75 error "Board required"
76 exit 1
77 fi
78 info "Target reports board is ${FLAGS_board}"
79}
80
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080081function learn_arch() {
82 [ -n "${FLAGS_arch}" ] && return
83 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -070084 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080085 if [ -z "${FLAGS_arch}" ]; then
86 error "Arch required"
87 exit 1
88 fi
89 info "Target reports arch is ${FLAGS_arch}"
90}
91
Chris Sosa24da49e2011-02-01 17:06:12 -080092# Checks to see if pid $1 is running.
93function is_pid_running() {
94 ps -p ${1} 2>&1 > /dev/null
95}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080096
Chris Sosa24da49e2011-02-01 17:06:12 -080097# Wait function given an additional timeout argument.
98# $1 - pid to wait on.
99# $2 - timeout to wait for.
100function wait_with_timeout() {
101 local pid=$1
102 local timeout=$2
103 local -r TIMEOUT_INC=1
104 local current_timeout=0
105 while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do
106 sleep ${TIMEOUT_INC}
107 current_timeout=$((current_timeout + TIMEOUT_INC))
108 done
109 ! is_pid_running ${pid}
110}
111
112# Checks to see if a machine has rebooted using the presence of a tmp file.
113function check_if_rebooted() {
114 local output_file="${TMP}/output"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800115 while true; do
116 REMOTE_OUT=""
117 # This may fail while the machine is down so generate output and a
118 # boolean result to distinguish between down/timeout and real failure
119 ! remote_sh_allow_changed_host_key \
120 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true"
121 echo "${REMOTE_OUT}" > "${output_file}"
122 if grep -q "0" "${output_file}"; then
123 if grep -q "1" "${output_file}"; then
124 info "Not yet rebooted"
Chris Sosa24da49e2011-02-01 17:06:12 -0800125 sleep .5
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800126 else
127 info "Rebooted and responding"
128 break
129 fi
130 fi
Chris Sosa24da49e2011-02-01 17:06:12 -0800131 done
132}
133
134function remote_reboot() {
135 info "Rebooting."
136 remote_sh "touch /tmp/awaiting_reboot; reboot"
137 while true; do
138 check_if_rebooted &
139 local pid=$!
140 wait_with_timeout ${pid} 30 && break
141 ! kill -9 ${pid} 2> /dev/null
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800142 done
143}
144
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800145# Called by clients before exiting.
146# Part of the remote_access.sh interface but now empty.
Sean O'Connor9969ce92010-02-01 17:10:03 -0800147function cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800148 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800149}
150
Ken Mixter689b9ee2010-01-07 18:23:52 -0800151function remote_access_init() {
152 TMP_PRIVATE_KEY=$TMP/private_key
153 TMP_KNOWN_HOSTS=$TMP/known_hosts
154 if [ -z "$FLAGS_remote" ]; then
155 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
156 exit 1
157 fi
158 set_up_remote_access
159}