Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 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 |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # Library for setting up remote access and running remote commands. |
| 6 | |
Sean O'Connor | a6db82e | 2010-01-27 12:11:08 -0800 | [diff] [blame] | 7 | DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\ |
| 8 | ssh_keys/testing_rsa" |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 9 | |
| 10 | DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" |
| 11 | DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \ |
| 12 | "Private key of root account on remote host" |
| 13 | |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame^] | 14 | # Copies $1 to $2 on remote host |
| 15 | function remote_cp() { |
| 16 | REMOTE_OUT=$(scp -o StrictHostKeyChecking=no -o \ |
| 17 | UserKnownHostsFile=$TMP_KNOWN_HOSTS $1 root@$FLAGS_remote:$2) |
| 18 | return ${PIPESTATUS[0]} |
| 19 | } |
| 20 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 21 | function remote_sh() { |
Sean O'Connor | a6db82e | 2010-01-27 12:11:08 -0800 | [diff] [blame] | 22 | REMOTE_OUT=$(ssh -o StrictHostKeyChecking=no -o \ |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 23 | UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") |
| 24 | return ${PIPESTATUS[0]} |
| 25 | } |
| 26 | |
| 27 | function remote_sh_allow_changed_host_key() { |
| 28 | rm -f $TMP_KNOWN_HOSTS |
| 29 | remote_sh "$@" |
| 30 | } |
| 31 | |
| 32 | function set_up_remote_access() { |
| 33 | if [ -z "$SSH_AGENT_PID" ]; then |
Sean O'Connor | a6db82e | 2010-01-27 12:11:08 -0800 | [diff] [blame] | 34 | eval $(ssh-agent) |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 35 | OWN_SSH_AGENT=1 |
| 36 | else |
| 37 | OWN_SSH_AGENT=0 |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 38 | fi |
| 39 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 40 | chmod 0400 $TMP_PRIVATE_KEY |
| 41 | ssh-add $TMP_PRIVATE_KEY |
| 42 | |
| 43 | # Verify the client is reachable before continuing |
| 44 | echo "Initiating first contact with remote host" |
| 45 | remote_sh "true" |
| 46 | echo "Connection OK" |
| 47 | } |
| 48 | |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 49 | function cleanup_remote_access() { |
| 50 | # Call this function from the exit trap of the main script. |
| 51 | # Iff we started ssh-agent, be nice and clean it up. |
| 52 | # Note, only works if called from the main script - no subshells. |
| 53 | if [[ 1 -eq ${OWN_SSH_AGENT} ]] |
| 54 | then |
| 55 | kill ${SSH_AGENT_PID} 2>/dev/null |
| 56 | unset SSH_AGENT_PID SSH_AUTH_SOCK |
| 57 | fi |
| 58 | } |
| 59 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 60 | function remote_access_init() { |
| 61 | TMP_PRIVATE_KEY=$TMP/private_key |
| 62 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 63 | if [ -z "$FLAGS_remote" ]; then |
| 64 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 65 | exit 1 |
| 66 | fi |
| 67 | set_up_remote_access |
| 68 | } |