blob: 121d74905888a065a47265373afaab9cd9ddaf0d [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 \
Chris Sosa24da49e2011-02-01 17:06:12 -080019 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY $1 \
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -080020 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 \
Chris Sosa24da49e2011-02-01 17:06:12 -080028 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY" \
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -080029 --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 Bainesaef91ad2011-01-14 14:17:25 -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
Olof Johanssonf53fa0d2011-01-26 13:06:46 -080066function learn_arch() {
67 [ -n "${FLAGS_arch}" ] && return
68 remote_sh uname -m
69 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed s/armv7l/arm/g)
70 if [ -z "${FLAGS_arch}" ]; then
71 error "Arch required"
72 exit 1
73 fi
74 info "Target reports arch is ${FLAGS_arch}"
75}
76
Chris Sosa24da49e2011-02-01 17:06:12 -080077# Checks to see if pid $1 is running.
78function is_pid_running() {
79 ps -p ${1} 2>&1 > /dev/null
80}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -080081
Chris Sosa24da49e2011-02-01 17:06:12 -080082# Wait function given an additional timeout argument.
83# $1 - pid to wait on.
84# $2 - timeout to wait for.
85function wait_with_timeout() {
86 local pid=$1
87 local timeout=$2
88 local -r TIMEOUT_INC=1
89 local current_timeout=0
90 while is_pid_running ${pid} && [ ${current_timeout} -lt ${timeout} ]; do
91 sleep ${TIMEOUT_INC}
92 current_timeout=$((current_timeout + TIMEOUT_INC))
93 done
94 ! is_pid_running ${pid}
95}
96
97# Checks to see if a machine has rebooted using the presence of a tmp file.
98function check_if_rebooted() {
99 local output_file="${TMP}/output"
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800100 while true; do
101 REMOTE_OUT=""
102 # This may fail while the machine is down so generate output and a
103 # boolean result to distinguish between down/timeout and real failure
104 ! remote_sh_allow_changed_host_key \
105 "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true"
106 echo "${REMOTE_OUT}" > "${output_file}"
107 if grep -q "0" "${output_file}"; then
108 if grep -q "1" "${output_file}"; then
109 info "Not yet rebooted"
Chris Sosa24da49e2011-02-01 17:06:12 -0800110 sleep .5
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800111 else
112 info "Rebooted and responding"
113 break
114 fi
115 fi
Chris Sosa24da49e2011-02-01 17:06:12 -0800116 done
117}
118
119function remote_reboot() {
120 info "Rebooting."
121 remote_sh "touch /tmp/awaiting_reboot; reboot"
122 while true; do
123 check_if_rebooted &
124 local pid=$!
125 wait_with_timeout ${pid} 30 && break
126 ! kill -9 ${pid} 2> /dev/null
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800127 done
128}
129
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800130# Called by clients before exiting.
131# Part of the remote_access.sh interface but now empty.
Sean O'Connor9969ce92010-02-01 17:10:03 -0800132function cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800133 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800134}
135
Ken Mixter689b9ee2010-01-07 18:23:52 -0800136function remote_access_init() {
137 TMP_PRIVATE_KEY=$TMP/private_key
138 TMP_KNOWN_HOSTS=$TMP/known_hosts
139 if [ -z "$FLAGS_remote" ]; then
140 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
141 exit 1
142 fi
143 set_up_remote_access
144}