blob: 9f148f22f385fb55b0e8e175849b2db676611c90 [file] [log] [blame]
J. Richard Barnette556038b2014-07-08 14:33:00 -07001#!/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 Barnette655318d2014-07-24 15:24:24 -070016# readlink -f $0, in case $0 is a symlink from somewhere else
17REPO=$(dirname $(readlink -f $0))/../../../../..
18REPO=$(readlink -f $REPO)
Tom Wai-Hong Tam175a69a2015-10-02 13:21:09 +080019PYTHON=$(readlink -f $REPO/chroot/usr/bin/python2.7)
J. Richard Barnette556038b2014-07-08 14:33:00 -070020HDCTOOLS=$(readlink -f $REPO/chroot/usr/lib/python2.7/site-packages/servo)
J. Richard Barnette655318d2014-07-24 15:24:24 -070021KEYFILE=$REPO
22KEYFILE=$KEYFILE/src/third_party/chromiumos-overlay
J. Richard Barnette086ca9d2014-10-10 12:33:47 -070023KEYFILE=$KEYFILE/chromeos-base/chromeos-ssh-testkeys/files/testing_rsa
J. Richard Barnette655318d2014-07-24 15:24:24 -070024
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
J. Richard Barnette556038b2014-07-08 14:33:00 -070038
39dut_control() {
Tom Wai-Hong Tam175a69a2015-10-02 13:21:09 +080040 timeout 90 $PYTHON $HDCTOOLS/dut_control.py "$@"
J. Richard Barnette556038b2014-07-08 14:33:00 -070041}
42
43remote() {
J. Richard Barnette655318d2014-07-24 15:24:24 -070044 local ssh_opts=( -n -o BatchMode=yes -o StrictHostKeyChecking=no
45 -o UserKnownHostsFile=$TMPHOSTS -i $TMPKEYS )
J. Richard Barnette556038b2014-07-08 14:33:00 -070046 local servo=$1
47 shift
J. Richard Barnette0c8dc5c2014-09-11 10:12:07 -070048 timeout 45 ssh "${ssh_opts[@]}" root@$servo "$@"
J. Richard Barnette556038b2014-07-08 14:33:00 -070049}
50
Kevin Chengf353b102017-02-01 14:17:14 -080051get_afe_host_attr() {
52 local host=$1
53 local attr=$2
54 local default=$3
55 atest host stat $host | awk "/^$attr *: / {count++; print \$3}
56 END {if (count != 1) print \"$default\"}"
57}
58
59get_servo() {
60 local host=$1
61
62 # Get the servo host from the afe. If not present, infer it from the hostname.
63 local servo_host=$(get_afe_host_attr $host servo_host ${host}-servo)
64 echo ${servo_host}.cros
65}
66
67get_servo_port() {
68 local host=$1
69
70 # Get the servo port from the afe. If not present, default 9999.
71 get_afe_host_attr $host servo_port 9999
72}
73
74
J. Richard Barnette556038b2014-07-08 14:33:00 -070075
76for H in "$@"
77do
Kevin Chengf353b102017-02-01 14:17:14 -080078 SERVO=$(get_servo $H)
79 SERVO_PORT=$(get_servo_port $H)
80 CONFIG=/var/lib/servod/config_$SERVO_PORT
J. Richard Barnette556038b2014-07-08 14:33:00 -070081 echo -n "$H ..."
82 STATUS=()
83
84 HAVE_SERVOD=1
85 BOARD=
86 VERSION=
87
88 echo -n "A"
89 if ping -c1 -w2 $SERVO >/dev/null 2>&1
90 then
91 echo -n "B"
Kevin Chengf353b102017-02-01 14:17:14 -080092 if BUTTON=$(dut_control -s $SERVO -p $SERVO_PORT pwr_button 2>&1)
J. Richard Barnette556038b2014-07-08 14:33:00 -070093 then
94 if [ "$BUTTON" != "pwr_button:release" ]
95 then
96 STATUS=("${STATUS[@]}" "pwr_button is '$BUTTON'")
97 else
98 echo -n "C"
Kevin Chengf353b102017-02-01 14:17:14 -080099 LID=$(dut_control -s $SERVO -p $SERVO_PORT lid_open 2>&1)
J. Richard Barnette36f77432014-10-01 14:36:06 -0700100 if [ "$LID" != "lid_open:yes" -a "$LID" != "lid_open:not_applicable" ]
J. Richard Barnette556038b2014-07-08 14:33:00 -0700101 then
102 STATUS=("${STATUS[@]}" "lid_open is '$LID'")
103 fi
104 fi
105 else
106 STATUS=("${STATUS[@]}" "not running servod")
107 HAVE_SERVOD=0
108 fi
109
110 echo -n "D"
111 if ! remote $SERVO true >/dev/null 2>&1
112 then
113 STATUS=("${STATUS[@]}" "ssh is down")
114 else
115 echo -n "E"
116 VERSION=$(
117 remote $SERVO grep CHROMEOS_RELEASE_VERSION /etc/lsb-release 2>&1)
118 if [ -z "$VERSION" ]
119 then
Kevin Chengf353b102017-02-01 14:17:14 -0800120 STATUS=("${STATUS[@]}" "not running chromeos")
J. Richard Barnette556038b2014-07-08 14:33:00 -0700121 fi
122 fi
123
124 if [ -n "$VERSION" ]
125 then
126 echo -n "F"
127 if remote $SERVO test -f $CONFIG
128 then
129 echo -n "G"
130 BOARD=$(remote $SERVO grep BOARD= $CONFIG)
131 fi
132 if [ $HAVE_SERVOD -eq 0 ]
133 then
134 if [ -z "$BOARD" ]
135 then
136 STATUS=("servod not configured")
137 else
138 echo -n "H"
139 JOB=$(remote $SERVO status servod | sed 's/,.*//')
140 if [ "$JOB" = "servod start/running" ]
141 then
142 STATUS=("servod failed")
143 fi
144 fi
145 fi
146 fi
147 else
148 STATUS=("${STATUS[@]}" "is down")
149 fi
150
151 if [ "${#STATUS}" -eq 0 ]
152 then
153 STATUS=("is up")
154 fi
155
156 if [ -n "$VERSION" ]
157 then
158 STATUS=("${STATUS[@]}" $BOARD $VERSION)
159 fi
160 echo " ${STATUS[@]}"
161done