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 |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 17 | function remote_cp_to() { |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 18 | REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 19 | UserKnownHostsFile=$TMP_KNOWN_HOSTS $1 root@$FLAGS_remote:$2) |
| 20 | return ${PIPESTATUS[0]} |
| 21 | } |
| 22 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 23 | # Copies a list of remote files specified in file $1 to local location |
| 24 | # $2. Directory paths in $1 are collapsed into $2. |
| 25 | function remote_rsync_from() { |
| 26 | rsync -e "ssh -o StrictHostKeyChecking=no -o \ |
| 27 | UserKnownHostsFile=$TMP_KNOWN_HOSTS" --no-R \ |
| 28 | --files-from=$1 root@${FLAGS_remote}:/ $2 |
| 29 | } |
| 30 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 31 | function remote_sh() { |
Zelidrag Hornung | 61d9768 | 2010-06-15 11:55:21 -0700 | [diff] [blame] | 32 | REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no -o \ |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 33 | UserKnownHostsFile=$TMP_KNOWN_HOSTS root@$FLAGS_remote "$@") |
| 34 | return ${PIPESTATUS[0]} |
| 35 | } |
| 36 | |
| 37 | function remote_sh_allow_changed_host_key() { |
| 38 | rm -f $TMP_KNOWN_HOSTS |
| 39 | remote_sh "$@" |
| 40 | } |
| 41 | |
| 42 | function set_up_remote_access() { |
| 43 | if [ -z "$SSH_AGENT_PID" ]; then |
Sean O'Connor | a6db82e | 2010-01-27 12:11:08 -0800 | [diff] [blame] | 44 | eval $(ssh-agent) |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 45 | OWN_SSH_AGENT=1 |
| 46 | else |
| 47 | OWN_SSH_AGENT=0 |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 48 | fi |
| 49 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 50 | chmod 0400 $TMP_PRIVATE_KEY |
| 51 | ssh-add $TMP_PRIVATE_KEY |
| 52 | |
| 53 | # Verify the client is reachable before continuing |
| 54 | echo "Initiating first contact with remote host" |
| 55 | remote_sh "true" |
| 56 | echo "Connection OK" |
| 57 | } |
| 58 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 59 | # Ask the target what board it is |
| 60 | function learn_board() { |
| 61 | [ -n "${FLAGS_board}" ] && return |
| 62 | remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
| 63 | FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 64 | if [ -z "${FLAGS_board}" ]; then |
| 65 | error "Board required" |
| 66 | exit 1 |
| 67 | fi |
| 68 | info "Target reports board is ${FLAGS_board}" |
| 69 | } |
| 70 | |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 71 | function cleanup_remote_access() { |
| 72 | # Call this function from the exit trap of the main script. |
| 73 | # Iff we started ssh-agent, be nice and clean it up. |
| 74 | # Note, only works if called from the main script - no subshells. |
| 75 | if [[ 1 -eq ${OWN_SSH_AGENT} ]] |
| 76 | then |
| 77 | kill ${SSH_AGENT_PID} 2>/dev/null |
| 78 | unset SSH_AGENT_PID SSH_AUTH_SOCK |
| 79 | fi |
| 80 | } |
| 81 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 82 | function remote_access_init() { |
| 83 | TMP_PRIVATE_KEY=$TMP/private_key |
| 84 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 85 | if [ -z "$FLAGS_remote" ]; then |
| 86 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 87 | exit 1 |
| 88 | fi |
| 89 | set_up_remote_access |
| 90 | } |