fix bug in last commit
[libremanage.git] / libremanage
CommitLineData
4e2acbd1 1#!/bin/bash
bbc21163 2
4e2acbd1
IK
3trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
4set -eE -o pipefail
3f7ab73a 5
4e2acbd1
IK
6# verbose command
7v() {
8 printf "$0 running: %s\n" "$*"
9 "$@"
10}
21ead159 11
21ead159 12
4e2acbd1
IK
13usage() {
14 cat <<EOF
f083a667 15Basic Usage: libremanage poweroff|poweron|reboot HOSTNAME
e799dcd1 16
f083a667
IK
17Available HOSTNAMEs defined in /etc/libremanage.conf on this machine:
18
19EOF
20
21 if [[ -e /etc/libremanage.conf ]]; then
22 sed -r '/\s*#/d;/^\s*$/d;s/_.*//' /etc/libremanage.conf
23 else
24 echo "/etc/libremanage.conf does not exist."
25 fi
26
27 cat <<EOF
28
29Thats all you need to know unless you are defining configuration or
ea470321
IK
30setting up a new device.
31
32
33
34
f083a667 35
8de1992e 36Advanced Usage: libremanage [--switch] poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID]
d8ddde1a 37
4e2acbd1 38Note, the relay's channels default state when plugged in are off.
6e59af0c 39
f083a667 40Example /etc/libremanage.conf:
a7d15c92
IK
41
42# This config is sourced from bash, so make sure its valid bash.
43cephserver3_channel=1
44cephserver3_board_id=HURTM
45cephserver2_channel=2
46cephserver2_board_id=HURTM
8de1992e
IK
47
48librecmc1_type=switch
49cephserver2_type=button # default
50
a7d15c92
IK
51# end of config
52
a7d15c92 53
8de1992e
IK
54Example use:
55
56$ libremanage reboot cephserver3
57
58--switch means means that the relay controls on an on/off switch (with
59default on), instead of a pc power button, which is the default. This
60corresponds to the config option HOST_type=switch.
a7d15c92
IK
61
62BOARD_ID is not needed if only 1 relay device is connected, or if it is
63defined in the config. To understand CHANNEL and BOARD_ID, run
64hidusb-relay-cmd and hidusb-relay-cmd state.
65
66
397823a6
IK
67WARNING: Avoid ever manually running "hidusb-relay-cmd on" in
68production, because if you lose connectivity and can't turn it off, its
69like the server's power button is stuck being pushed in and it won't
70turn on in that state. You will need to physically replug usb power to
71the relay so the channel goes back to its default off state. If this
72script somehow dies after turning a relay on, running it again will turn
73it off. That is why this script runs locally and ignores signals when
74its running. If you run this script over ssh and lose connection as this
75script is running, it will continue to run and complete.
e14d7650 76
4e2acbd1
IK
77EOF
78 if [[ -e /etc/libremanage.conf ]]; then
f083a667 79 echo "Current /etc/libremanage.conf:"
a7d15c92 80 cat /etc/libremanage.conf
4e2acbd1
IK
81 else
82 echo "Note: /etc/libremanage.conf does not exist on the current machine"
83 fi
84 exit 0
d8ddde1a
AR
85}
86
22f864bb 87
4e2acbd1
IK
88if (( $# < 2 )); then
89 usage
90fi
91
8de1992e
IK
92switch=false
93if [[ $1 == --switch ]]; then
94 switch=true
f00bdb0e 95 shift
8de1992e
IK
96fi
97
4e2acbd1
IK
98read action chan board_id <<<"$@"
99
100if [[ -e /etc/libremanage.conf ]]; then
101 source /etc/libremanage.conf
102fi
103
a7d15c92
IK
104# Use config vars when appropriate. We know the arg is a HOSTNAME if it
105# doesn't start with a number.
4e2acbd1
IK
106if [[ $chan != [0-9]* ]]; then
107 if [[ ! $board_id ]]; then
108 board_id_var=${chan}_board_id
109 board_id=${!board_id_var}
110 fi
111 chan_var=${chan}_channel
112 chan=${!chan_var}
113
114 if [[ ! $chan ]]; then
115 echo "error: channel not found in /etc/libremanage.conf" >&2
116 exit 1
117 fi
118fi
119
120
121if hidusb-relay-cmd state | grep ON; then
122 printf "%s\n" "WARNING: output from hidusb-relay-cmd state shows an ON relay. this could mean another command instance is running, or it got stuck on due to an uncompleted command." >&2
123 # TODO: exit in this case and print out the command to turn it off.
124fi
125
126
127case $(hidusb-relay-cmd state|wc -l) in
128 0)
129 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
130 exit 1
131 ;;
132 1) : ;;
133 *)
134 if [[ ! $board_id ]]; then
135 echo "error: more than 1 relay device, so passing its id is required" >&2
136 exit 1
137 fi
138 ;;
139esac
140
141
142if [[ $board_id ]]; then
143 # leave this as an empty var if its not passed
144 board_id_arg="id=$board_id"
145fi
146
147# ignore errors and continue on if a command fails from here
148set +eE +o pipefail
149# ignore hup so we complete even if there is a connection problem, and
150# force anyone to kill -9 so we might complete in more cases, for
151# example if a reboot is happening
152trap '' HUP INT QUIT TERM
153echo "$0: doing $action. shell commands will be printed to the terminal."
154case $action in
155 poweroff)
8de1992e
IK
156 if $switch; then
157 v hidusb-relay-cmd $board_id_arg off $chan
158 else
159 v hidusb-relay-cmd $board_id_arg on $chan
160 v sleep 6
161 v hidusb-relay-cmd $board_id_arg off $chan
162 fi
4e2acbd1
IK
163 ;;
164 poweron)
8de1992e
IK
165 if $switch; then
166 v hidusb-relay-cmd $board_id_arg on $chan
167 else
168 v hidusb-relay-cmd $board_id_arg on $chan
169 v sleep 1
170 v hidusb-relay-cmd $board_id_arg off $chan
171 fi
4e2acbd1
IK
172 ;;
173 reboot)
8de1992e
IK
174 if $switch; then
175 v hidusb-relay-cmd $board_id_arg off $chan
176 v sleep 4
177 v hidusb-relay-cmd $board_id_arg on $chan
178 else
179 v hidusb-relay-cmd $board_id_arg on $chan
180 v sleep 6
181 v hidusb-relay-cmd $board_id_arg off $chan
182 v sleep 1
183 v hidusb-relay-cmd $board_id_arg on $chan
184 v sleep 1
185 v hidusb-relay-cmd $board_id_arg off $chan
186 fi
4e2acbd1
IK
187 ;;
188 *)
189 echo "error: action arg not supported" >&2
190 exit 1
191esac
192echo "$0: script ended. exiting"