blob: 43a64ad1875e8b64fc8bc33f540f84bdfe0820de [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"
13
14function remote_sh() {
Sean O'Connora6db82e2010-01-27 12:11:08 -080015 REMOTE_OUT=$(ssh -o StrictHostKeyChecking=no -o \
Ken Mixter689b9ee2010-01-07 18:23:52 -080016 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@")
17 return ${PIPESTATUS[0]}
18}
19
20function remote_sh_allow_changed_host_key() {
21 rm -f $TMP_KNOWN_HOSTS
22 remote_sh "$@"
23}
24
25function set_up_remote_access() {
26 if [ -z "$SSH_AGENT_PID" ]; then
Sean O'Connora6db82e2010-01-27 12:11:08 -080027 eval $(ssh-agent)
Sean O'Connor9969ce92010-02-01 17:10:03 -080028 OWN_SSH_AGENT=1
29 else
30 OWN_SSH_AGENT=0
Ken Mixter689b9ee2010-01-07 18:23:52 -080031 fi
32 cp $FLAGS_private_key $TMP_PRIVATE_KEY
33 chmod 0400 $TMP_PRIVATE_KEY
34 ssh-add $TMP_PRIVATE_KEY
35
36 # Verify the client is reachable before continuing
37 echo "Initiating first contact with remote host"
38 remote_sh "true"
39 echo "Connection OK"
40}
41
Sean O'Connor9969ce92010-02-01 17:10:03 -080042function cleanup_remote_access() {
43 # Call this function from the exit trap of the main script.
44 # Iff we started ssh-agent, be nice and clean it up.
45 # Note, only works if called from the main script - no subshells.
46 if [[ 1 -eq ${OWN_SSH_AGENT} ]]
47 then
48 kill ${SSH_AGENT_PID} 2>/dev/null
49 unset SSH_AGENT_PID SSH_AUTH_SOCK
50 fi
51}
52
Ken Mixter689b9ee2010-01-07 18:23:52 -080053function remote_access_init() {
54 TMP_PRIVATE_KEY=$TMP/private_key
55 TMP_KNOWN_HOSTS=$TMP/known_hosts
56 if [ -z "$FLAGS_remote" ]; then
57 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
58 exit 1
59 fi
60 set_up_remote_access
61}