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
16# readlink -f $0, in case $0 is a symlink from somewhere else
17REPO=$(dirname $(readlink -f $0))/../../../../..
18REPO=$(readlink -f $REPO)
19PYTHON=$(readlink -f $REPO/chroot/usr/bin/python2.7)
20HDCTOOLS=$(readlink -f $REPO/chroot/usr/lib/python2.7/site-packages/servo)
21KEYFILE=$REPO
22KEYFILE=$KEYFILE/src/third_party/chromiumos-overlay
23KEYFILE=$KEYFILE/chromeos-base/chromeos-ssh-testkeys/files/testing_rsa
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
34TMPKEYS=$(mktemp)
35TMPHOSTS=$(mktemp)
36trap 'rm $TMPKEYS $TMPHOSTS' EXIT
37cp $KEYFILE $TMPKEYS
38
39dut_control() {
40  timeout 90 $PYTHON $HDCTOOLS/dut_control.py "$@"
41}
42
43remote() {
44    local ssh_opts=( -n -o BatchMode=yes -o StrictHostKeyChecking=no
45                     -o UserKnownHostsFile=$TMPHOSTS -i $TMPKEYS )
46    local servo=$1
47    shift
48    timeout 45 ssh "${ssh_opts[@]}" root@$servo "$@"
49}
50
51CONFIG=/var/lib/servod/config
52
53for H in "$@"
54do
55  SERVO=$H-servo.cros
56  echo -n "$H ..."
57  STATUS=()
58
59  HAVE_SERVOD=1
60  BOARD=
61  VERSION=
62
63  echo -n "A"
64  if ping -c1 -w2 $SERVO >/dev/null 2>&1
65  then
66    echo -n "B"
67    if BUTTON=$(dut_control -s $SERVO pwr_button 2>&1)
68    then
69      if [ "$BUTTON" != "pwr_button:release" ]
70      then
71        STATUS=("${STATUS[@]}" "pwr_button is '$BUTTON'")
72      else
73        echo -n "C"
74        LID=$(dut_control -s $SERVO lid_open 2>&1)
75        if [ "$LID" != "lid_open:yes" -a "$LID" != "lid_open:not_applicable" ]
76        then
77          STATUS=("${STATUS[@]}" "lid_open is '$LID'")
78        fi
79      fi
80    else
81      STATUS=("${STATUS[@]}" "not running servod")
82      HAVE_SERVOD=0
83    fi
84
85    echo -n "D"
86    if ! remote $SERVO true >/dev/null 2>&1
87    then
88      STATUS=("${STATUS[@]}" "ssh is down")
89    else
90      echo -n "E"
91      VERSION=$(
92          remote $SERVO grep CHROMEOS_RELEASE_VERSION /etc/lsb-release 2>&1)
93      if [ -z "$VERSION" ]
94      then
95        STATUS=("${STATUS[@]}" "not running brillo")
96      fi
97    fi
98
99    if [ -n "$VERSION" ]
100    then
101      echo -n "F"
102      if remote $SERVO test -f $CONFIG
103      then
104        echo -n "G"
105        BOARD=$(remote $SERVO grep BOARD= $CONFIG)
106      fi
107      if [ $HAVE_SERVOD -eq 0 ]
108      then
109        if [ -z "$BOARD" ]
110        then
111          STATUS=("servod not configured")
112        else
113          echo -n "H"
114          JOB=$(remote $SERVO status servod | sed 's/,.*//')
115          if [ "$JOB" = "servod start/running" ]
116          then
117              STATUS=("servod failed")
118          fi
119        fi
120      fi
121    fi
122  else
123    STATUS=("${STATUS[@]}" "is down")
124  fi
125
126  if [ "${#STATUS}" -eq 0 ]
127  then
128    STATUS=("is up")
129  fi
130
131  if [ -n "$VERSION" ]
132  then
133    STATUS=("${STATUS[@]}" $BOARD $VERSION)
134  fi
135  echo " ${STATUS[@]}"
136done
137