OLD | NEW |
1 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 # 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # Library for setting up remote access and running remote commands. | 5 # Library for setting up remote access and running remote commands. |
6 | 6 |
7 DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\ | 7 DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\ |
8 ssh_keys/testing_rsa" | 8 ssh_keys/testing_rsa" |
9 | 9 |
10 DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" | 10 DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" |
11 DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \ | 11 DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \ |
12 "Private key of root account on remote host" | 12 "Private key of root account on remote host" |
13 | 13 |
14 function remote_sh() { | 14 function remote_sh() { |
15 REMOTE_OUT=$(ssh -o StrictHostKeyChecking=no -o \ | 15 REMOTE_OUT=$(ssh -o StrictHostKeyChecking=no -o \ |
16 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") | 16 UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") |
17 return ${PIPESTATUS[0]} | 17 return ${PIPESTATUS[0]} |
18 } | 18 } |
19 | 19 |
20 function remote_sh_allow_changed_host_key() { | 20 function remote_sh_allow_changed_host_key() { |
21 rm -f $TMP_KNOWN_HOSTS | 21 rm -f $TMP_KNOWN_HOSTS |
22 remote_sh "$@" | 22 remote_sh "$@" |
23 } | 23 } |
24 | 24 |
25 function set_up_remote_access() { | 25 function set_up_remote_access() { |
26 if [ -z "$SSH_AGENT_PID" ]; then | 26 if [ -z "$SSH_AGENT_PID" ]; then |
27 eval $(ssh-agent) | 27 eval $(ssh-agent) |
| 28 OWN_SSH_AGENT=1 |
| 29 else |
| 30 OWN_SSH_AGENT=0 |
28 fi | 31 fi |
29 cp $FLAGS_private_key $TMP_PRIVATE_KEY | 32 cp $FLAGS_private_key $TMP_PRIVATE_KEY |
30 chmod 0400 $TMP_PRIVATE_KEY | 33 chmod 0400 $TMP_PRIVATE_KEY |
31 ssh-add $TMP_PRIVATE_KEY | 34 ssh-add $TMP_PRIVATE_KEY |
32 | 35 |
33 # Verify the client is reachable before continuing | 36 # Verify the client is reachable before continuing |
34 echo "Initiating first contact with remote host" | 37 echo "Initiating first contact with remote host" |
35 remote_sh "true" | 38 remote_sh "true" |
36 echo "Connection OK" | 39 echo "Connection OK" |
37 } | 40 } |
38 | 41 |
| 42 function 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 |
39 function remote_access_init() { | 53 function remote_access_init() { |
40 TMP_PRIVATE_KEY=$TMP/private_key | 54 TMP_PRIVATE_KEY=$TMP/private_key |
41 TMP_KNOWN_HOSTS=$TMP/known_hosts | 55 TMP_KNOWN_HOSTS=$TMP/known_hosts |
42 if [ -z "$FLAGS_remote" ]; then | 56 if [ -z "$FLAGS_remote" ]; then |
43 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" | 57 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
44 exit 1 | 58 exit 1 |
45 fi | 59 fi |
46 set_up_remote_access | 60 set_up_remote_access |
47 } | 61 } |
OLD | NEW |