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" |
Zelidrag Hornung | 61d9768 | 2010-06-15 11:55:21 -0700 | [diff] [blame] | 13 | DEFINE_integer ssh_port 22 \ |
| 14 | "SSH port of the remote machine running Chromium OS instance" |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 15 | |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 16 | # Copies $1 to $2 on remote host |
| 17 | function 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 Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 23 | function remote_sh() { |
Zelidrag Hornung | 61d9768 | 2010-06-15 11:55:21 -0700 | [diff] [blame] | 24 | REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 25 | UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") |
| 26 | return ${PIPESTATUS[0]} |
| 27 | } |
| 28 | |
| 29 | function remote_sh_allow_changed_host_key() { |
| 30 | rm -f $TMP_KNOWN_HOSTS |
| 31 | remote_sh "$@" |
| 32 | } |
| 33 | |
| 34 | function set_up_remote_access() { |
| 35 | if [ -z "$SSH_AGENT_PID" ]; then |
Sean O'Connor | a6db82e | 2010-01-27 12:11:08 -0800 | [diff] [blame] | 36 | eval $(ssh-agent) |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 37 | OWN_SSH_AGENT=1 |
| 38 | else |
| 39 | OWN_SSH_AGENT=0 |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 40 | 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'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 51 | function 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 Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 62 | function 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 | } |