blob: 092555a5bd61f9e8ea3bb0714eda7ac816ceb469 [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 Sosacc09f842010-09-21 17:09:51 -070018 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \
Chris Sosaef964302010-04-27 13:21:08 -070019 UserKnownHostsFile=$TMP_KNOWN_HOSTS $1 root@$FLAGS_remote:$2)
20 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() {
26 rsync -e "ssh -o StrictHostKeyChecking=no -o \
27 UserKnownHostsFile=$TMP_KNOWN_HOSTS" --no-R \
28 --files-from=$1 root@${FLAGS_remote}:/ $2
29}
30
Ken Mixter689b9ee2010-01-07 18:23:52 -080031function remote_sh() {
Zelidrag Hornung61d97682010-06-15 11:55:21 -070032 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \
Ken Mixter689b9ee2010-01-07 18:23:52 -080033 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@")
34 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() {
43 if [ -z "$SSH_AGENT_PID" ]; then
Sean O'Connora6db82e2010-01-27 12:11:08 -080044 eval $(ssh-agent)
Sean O'Connor9969ce92010-02-01 17:10:03 -080045 OWN_SSH_AGENT=1
46 else
47 OWN_SSH_AGENT=0
Ken Mixter689b9ee2010-01-07 18:23:52 -080048 fi
49 cp $FLAGS_private_key $TMP_PRIVATE_KEY
50 chmod 0400 $TMP_PRIVATE_KEY
51 ssh-add $TMP_PRIVATE_KEY
52
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
Sean O'Connor9969ce92010-02-01 17:10:03 -080071function cleanup_remote_access() {
72 # Call this function from the exit trap of the main script.
73 # Iff we started ssh-agent, be nice and clean it up.
74 # Note, only works if called from the main script - no subshells.
75 if [[ 1 -eq ${OWN_SSH_AGENT} ]]
76 then
77 kill ${SSH_AGENT_PID} 2>/dev/null
78 unset SSH_AGENT_PID SSH_AUTH_SOCK
79 fi
80}
81
Ken Mixter689b9ee2010-01-07 18:23:52 -080082function remote_access_init() {
83 TMP_PRIVATE_KEY=$TMP/private_key
84 TMP_KNOWN_HOSTS=$TMP/known_hosts
85 if [ -z "$FLAGS_remote" ]; then
86 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
87 exit 1
88 fi
89 set_up_remote_access
90}