J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Usage: |
| 4 | # servo-stat DUT ... |
| 5 | # |
| 6 | # Reports the status of the servo (if any) attached to the DUT. |
| 7 | # The DUT name is the host name without the .cros, or -servo. |
| 8 | # For each named DUT, reports a line something like this: |
| 9 | # DUT ...ABCDEFG is up BOARD=swanky CHROMEOS_RELEASE_VERSION=5995.0.0 |
| 10 | # |
| 11 | # The letters are just arbitrary tags printed before any |
| 12 | # long-running operation that might time out. It allows you to see |
| 13 | # progress, and if things get hung up, you can see where. |
| 14 | |
| 15 | |
J. Richard Barnette | 655318d | 2014-07-24 15:24:24 -0700 | [diff] [blame] | 16 | # readlink -f $0, in case $0 is a symlink from somewhere else |
| 17 | REPO=$(dirname $(readlink -f $0))/../../../../.. |
| 18 | REPO=$(readlink -f $REPO) |
Tom Wai-Hong Tam | 175a69a | 2015-10-02 13:21:09 +0800 | [diff] [blame] | 19 | PYTHON=$(readlink -f $REPO/chroot/usr/bin/python2.7) |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 20 | HDCTOOLS=$(readlink -f $REPO/chroot/usr/lib/python2.7/site-packages/servo) |
J. Richard Barnette | 655318d | 2014-07-24 15:24:24 -0700 | [diff] [blame] | 21 | KEYFILE=$REPO |
| 22 | KEYFILE=$KEYFILE/src/third_party/chromiumos-overlay |
J. Richard Barnette | 086ca9d | 2014-10-10 12:33:47 -0700 | [diff] [blame] | 23 | KEYFILE=$KEYFILE/chromeos-base/chromeos-ssh-testkeys/files/testing_rsa |
J. Richard Barnette | 655318d | 2014-07-24 15:24:24 -0700 | [diff] [blame] | 24 | |
| 25 | # Need some temporary files to keep ssh happy: |
| 26 | # + Just setting StrictHostKeyChecking=no won't silence all |
| 27 | # possible errors about host keys, so we need a temporary file |
| 28 | # where host keys can be cached. |
| 29 | # + We don't want to require the user to edit or provide the |
| 30 | # standard test keys, so we use the keys from the repo. But... |
| 31 | # The file must be user-readable only, so we need a copy with |
| 32 | # the correct modes (mktemp is 600 by default). |
| 33 | |
| 34 | TMPKEYS=$(mktemp) |
| 35 | TMPHOSTS=$(mktemp) |
Richard Barnette | c75769d | 2017-08-24 11:17:05 -0700 | [diff] [blame] | 36 | |
J. Richard Barnette | 655318d | 2014-07-24 15:24:24 -0700 | [diff] [blame] | 37 | trap 'rm $TMPKEYS $TMPHOSTS' EXIT |
| 38 | cp $KEYFILE $TMPKEYS |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 39 | |
Richard Barnette | c75769d | 2017-08-24 11:17:05 -0700 | [diff] [blame] | 40 | _ssh() { |
| 41 | local ssh_opts=( -n -o BatchMode=yes -o StrictHostKeyChecking=no |
| 42 | -o UserKnownHostsFile=$TMPHOSTS -i $TMPKEYS ) |
| 43 | local timeout=$1 |
| 44 | local servo=$2 |
| 45 | shift 2 |
| 46 | timeout "${timeout}" ssh "${ssh_opts[@]}" "root@${servo}" "$@" |
| 47 | } |
| 48 | |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 49 | dut_control() { |
Richard Barnette | c75769d | 2017-08-24 11:17:05 -0700 | [diff] [blame] | 50 | local servo=$1 |
| 51 | shift |
| 52 | _ssh 90 $servo dut-control "$@" |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | remote() { |
Richard Barnette | c75769d | 2017-08-24 11:17:05 -0700 | [diff] [blame] | 56 | _ssh 45 "$@" |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Kevin Cheng | f353b10 | 2017-02-01 14:17:14 -0800 | [diff] [blame] | 59 | get_afe_host_attr() { |
| 60 | local host=$1 |
| 61 | local attr=$2 |
| 62 | local default=$3 |
| 63 | atest host stat $host | awk "/^$attr *: / {count++; print \$3} |
| 64 | END {if (count != 1) print \"$default\"}" |
| 65 | } |
| 66 | |
| 67 | get_servo() { |
| 68 | local host=$1 |
| 69 | |
| 70 | # Get the servo host from the afe. If not present, infer it from the hostname. |
| 71 | local servo_host=$(get_afe_host_attr $host servo_host ${host}-servo) |
| 72 | echo ${servo_host}.cros |
| 73 | } |
| 74 | |
| 75 | get_servo_port() { |
| 76 | local host=$1 |
| 77 | |
| 78 | # Get the servo port from the afe. If not present, default 9999. |
| 79 | get_afe_host_attr $host servo_port 9999 |
| 80 | } |
| 81 | |
| 82 | |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 83 | |
| 84 | for H in "$@" |
| 85 | do |
Kevin Cheng | f353b10 | 2017-02-01 14:17:14 -0800 | [diff] [blame] | 86 | SERVO=$(get_servo $H) |
| 87 | SERVO_PORT=$(get_servo_port $H) |
| 88 | CONFIG=/var/lib/servod/config_$SERVO_PORT |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 89 | echo -n "$H ..." |
| 90 | STATUS=() |
| 91 | |
| 92 | HAVE_SERVOD=1 |
| 93 | BOARD= |
| 94 | VERSION= |
| 95 | |
| 96 | echo -n "A" |
| 97 | if ping -c1 -w2 $SERVO >/dev/null 2>&1 |
| 98 | then |
| 99 | echo -n "B" |
Richard Barnette | c75769d | 2017-08-24 11:17:05 -0700 | [diff] [blame] | 100 | if BUTTON=$(dut_control $SERVO -p $SERVO_PORT pwr_button 2>/dev/null) |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 101 | then |
| 102 | if [ "$BUTTON" != "pwr_button:release" ] |
| 103 | then |
| 104 | STATUS=("${STATUS[@]}" "pwr_button is '$BUTTON'") |
| 105 | else |
| 106 | echo -n "C" |
Richard Barnette | c75769d | 2017-08-24 11:17:05 -0700 | [diff] [blame] | 107 | LID=$(dut_control $SERVO -p $SERVO_PORT lid_open 2>/dev/null) |
J. Richard Barnette | 36f7743 | 2014-10-01 14:36:06 -0700 | [diff] [blame] | 108 | if [ "$LID" != "lid_open:yes" -a "$LID" != "lid_open:not_applicable" ] |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 109 | then |
| 110 | STATUS=("${STATUS[@]}" "lid_open is '$LID'") |
| 111 | fi |
| 112 | fi |
| 113 | else |
| 114 | STATUS=("${STATUS[@]}" "not running servod") |
| 115 | HAVE_SERVOD=0 |
| 116 | fi |
| 117 | |
| 118 | echo -n "D" |
| 119 | if ! remote $SERVO true >/dev/null 2>&1 |
| 120 | then |
| 121 | STATUS=("${STATUS[@]}" "ssh is down") |
| 122 | else |
| 123 | echo -n "E" |
| 124 | VERSION=$( |
| 125 | remote $SERVO grep CHROMEOS_RELEASE_VERSION /etc/lsb-release 2>&1) |
| 126 | if [ -z "$VERSION" ] |
| 127 | then |
Kevin Cheng | f353b10 | 2017-02-01 14:17:14 -0800 | [diff] [blame] | 128 | STATUS=("${STATUS[@]}" "not running chromeos") |
J. Richard Barnette | 556038b | 2014-07-08 14:33:00 -0700 | [diff] [blame] | 129 | fi |
| 130 | fi |
| 131 | |
| 132 | if [ -n "$VERSION" ] |
| 133 | then |
| 134 | echo -n "F" |
| 135 | if remote $SERVO test -f $CONFIG |
| 136 | then |
| 137 | echo -n "G" |
| 138 | BOARD=$(remote $SERVO grep BOARD= $CONFIG) |
| 139 | fi |
| 140 | if [ $HAVE_SERVOD -eq 0 ] |
| 141 | then |
| 142 | if [ -z "$BOARD" ] |
| 143 | then |
| 144 | STATUS=("servod not configured") |
| 145 | else |
| 146 | echo -n "H" |
| 147 | JOB=$(remote $SERVO status servod | sed 's/,.*//') |
| 148 | if [ "$JOB" = "servod start/running" ] |
| 149 | then |
| 150 | STATUS=("servod failed") |
| 151 | fi |
| 152 | fi |
| 153 | fi |
| 154 | fi |
| 155 | else |
| 156 | STATUS=("${STATUS[@]}" "is down") |
| 157 | fi |
| 158 | |
| 159 | if [ "${#STATUS}" -eq 0 ] |
| 160 | then |
| 161 | STATUS=("is up") |
| 162 | fi |
| 163 | |
| 164 | if [ -n "$VERSION" ] |
| 165 | then |
| 166 | STATUS=("${STATUS[@]}" $BOARD $VERSION) |
| 167 | fi |
| 168 | echo " ${STATUS[@]}" |
| 169 | done |