blob: f34f30b0b6640ebb86c088a9abb2db68ceb6cb10 [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
17function remote_cp() {
18 REMOTE_OUT=$(scp -o StrictHostKeyChecking=no -o \
19 UserKnownHostsFile=$TMP_KNOWN_HOSTS $1 root@$FLAGS_remote:$2)
20 return ${PIPESTATUS[0]}
21}
22
Ken Mixter689b9ee2010-01-07 18:23:52 -080023function remote_sh() {
Zelidrag Hornung61d97682010-06-15 11:55:21 -070024 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \
Ken Mixter689b9ee2010-01-07 18:23:52 -080025 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@")
26 return ${PIPESTATUS[0]}
27}
28
29function remote_sh_allow_changed_host_key() {
30 rm -f $TMP_KNOWN_HOSTS
31 remote_sh "$@"
32}
33
34function set_up_remote_access() {
35 if [ -z "$SSH_AGENT_PID" ]; then
Sean O'Connora6db82e2010-01-27 12:11:08 -080036 eval $(ssh-agent)
Sean O'Connor9969ce92010-02-01 17:10:03 -080037 OWN_SSH_AGENT=1
38 else
39 OWN_SSH_AGENT=0
Ken Mixter689b9ee2010-01-07 18:23:52 -080040 fi
41 cp $FLAGS_private_key $TMP_PRIVATE_KEY
42 chmod 0400 $TMP_PRIVATE_KEY
43 ssh-add $TMP_PRIVATE_KEY
44
45 # Verify the client is reachable before continuing
46 echo "Initiating first contact with remote host"
47 remote_sh "true"
48 echo "Connection OK"
49}
50
Sean O'Connor9969ce92010-02-01 17:10:03 -080051function cleanup_remote_access() {
52 # Call this function from the exit trap of the main script.
53 # Iff we started ssh-agent, be nice and clean it up.
54 # Note, only works if called from the main script - no subshells.
55 if [[ 1 -eq ${OWN_SSH_AGENT} ]]
56 then
57 kill ${SSH_AGENT_PID} 2>/dev/null
58 unset SSH_AGENT_PID SSH_AUTH_SOCK
59 fi
60}
61
Ken Mixter689b9ee2010-01-07 18:23:52 -080062function remote_access_init() {
63 TMP_PRIVATE_KEY=$TMP/private_key
64 TMP_KNOWN_HOSTS=$TMP/known_hosts
65 if [ -z "$FLAGS_remote" ]; then
66 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
67 exit 1
68 fi
69 set_up_remote_access
70}