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() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 18 | REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
Chris Sosa | 637b553 | 2011-01-24 14:48:02 -0800 | [diff] [blame] | 19 | -o ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS \ |
| 20 | -i $TMP_PRIVATE_KEY $1 \ |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 21 | root@$FLAGS_remote:$2) |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 22 | return ${PIPESTATUS[0]} |
| 23 | } |
| 24 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 25 | # Copies a list of remote files specified in file $1 to local location |
| 26 | # $2. Directory paths in $1 are collapsed into $2. |
| 27 | function remote_rsync_from() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 28 | rsync -e "ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
Chris Sosa | 637b553 | 2011-01-24 14:48:02 -0800 | [diff] [blame] | 29 | -o ServerAliveInterval=60 -o UserKnownHostsFile=$TMP_KNOWN_HOSTS \ |
| 30 | -i $TMP_PRIVATE_KEY" \ |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 31 | --no-R --files-from=$1 root@${FLAGS_remote}:/ $2 |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 34 | function remote_sh() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 35 | REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} -o StrictHostKeyChecking=no \ |
| 36 | -o UserKnownHostsFile=$TMP_KNOWN_HOSTS -i $TMP_PRIVATE_KEY \ |
Chris Sosa | 637b553 | 2011-01-24 14:48:02 -0800 | [diff] [blame] | 37 | -o ServerAliveInterval=60 \ |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 38 | root@$FLAGS_remote "$@") |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 39 | return ${PIPESTATUS[0]} |
| 40 | } |
| 41 | |
| 42 | function remote_sh_allow_changed_host_key() { |
| 43 | rm -f $TMP_KNOWN_HOSTS |
| 44 | remote_sh "$@" |
| 45 | } |
| 46 | |
| 47 | function set_up_remote_access() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 48 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 49 | chmod 0400 $TMP_PRIVATE_KEY |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 50 | |
| 51 | # Verify the client is reachable before continuing |
| 52 | echo "Initiating first contact with remote host" |
| 53 | remote_sh "true" |
| 54 | echo "Connection OK" |
| 55 | } |
| 56 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 57 | # Ask the target what board it is |
| 58 | function learn_board() { |
| 59 | [ -n "${FLAGS_board}" ] && return |
| 60 | remote_sh grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
| 61 | FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 62 | if [ -z "${FLAGS_board}" ]; then |
| 63 | error "Board required" |
| 64 | exit 1 |
| 65 | fi |
| 66 | info "Target reports board is ${FLAGS_board}" |
| 67 | } |
| 68 | |
Olof Johansson | f53fa0d | 2011-01-26 13:06:46 -0800 | [diff] [blame^] | 69 | function learn_arch() { |
| 70 | [ -n "${FLAGS_arch}" ] && return |
| 71 | remote_sh uname -m |
| 72 | FLAGS_arch=$(echo "${REMOTE_OUT}" | sed s/armv7l/arm/g) |
| 73 | if [ -z "${FLAGS_arch}" ]; then |
| 74 | error "Arch required" |
| 75 | exit 1 |
| 76 | fi |
| 77 | info "Target reports arch is ${FLAGS_arch}" |
| 78 | } |
| 79 | |
Chris Sosa | 637b553 | 2011-01-24 14:48:02 -0800 | [diff] [blame] | 80 | function remote_reboot { |
| 81 | info "Rebooting." |
| 82 | remote_sh "touch /tmp/awaiting_reboot; reboot" |
| 83 | local output_file |
| 84 | output_file="${TMP}/output" |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 85 | |
| 86 | while true; do |
| 87 | REMOTE_OUT="" |
| 88 | # This may fail while the machine is down so generate output and a |
| 89 | # boolean result to distinguish between down/timeout and real failure |
| 90 | ! remote_sh_allow_changed_host_key \ |
| 91 | "echo 0; [ -e /tmp/awaiting_reboot ] && echo '1'; true" |
| 92 | echo "${REMOTE_OUT}" > "${output_file}" |
| 93 | if grep -q "0" "${output_file}"; then |
| 94 | if grep -q "1" "${output_file}"; then |
| 95 | info "Not yet rebooted" |
| 96 | else |
| 97 | info "Rebooted and responding" |
| 98 | break |
| 99 | fi |
| 100 | fi |
Chris Sosa | 637b553 | 2011-01-24 14:48:02 -0800 | [diff] [blame] | 101 | sleep .5 |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 102 | done |
| 103 | } |
| 104 | |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 105 | # Called by clients before exiting. |
| 106 | # Part of the remote_access.sh interface but now empty. |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 107 | function cleanup_remote_access() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 108 | true |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 111 | function remote_access_init() { |
| 112 | TMP_PRIVATE_KEY=$TMP/private_key |
| 113 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
| 114 | if [ -z "$FLAGS_remote" ]; then |
| 115 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 116 | exit 1 |
| 117 | fi |
| 118 | set_up_remote_access |
| 119 | } |