3 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
8 printf "$0 running: %s\n" "$*"
15 Basic Usage: libremanage poweroff|poweron|reboot HOSTNAME
17 Available HOSTNAMEs defined in /etc/libremanage.conf on this machine:
21 if [[ -e /etc
/libremanage.conf
]]; then
22 sed -r '/\s*#/d;/^\s*$/d;s/_.*//' /etc
/libremanage.conf
24 echo "/etc/libremanage.conf does not exist."
29 Thats all you need to know unless you are defining configuration or
30 setting up a new device.
36 Advanced Usage: libremanage [--switch] poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID]
38 Note, the relay's channels default state when plugged in are off.
40 Example /etc/libremanage.conf:
42 # This config is sourced from bash, so make sure its valid bash.
44 cephserver3_board_id=HURTM
46 cephserver2_board_id=HURTM
49 cephserver2_type=button # default
56 $ libremanage reboot cephserver3
58 --switch means means that the relay controls on an on/off switch (with
59 default on), instead of a pc power button, which is the default. This
60 corresponds to the config option HOST_type=switch.
62 BOARD_ID is not needed if only 1 relay device is connected, or if it is
63 defined in the config. To understand CHANNEL and BOARD_ID, run
64 hidusb-relay-cmd and hidusb-relay-cmd state.
67 WARNING: Avoid ever manually running "hidusb-relay-cmd on" in
68 production, because if you lose connectivity and can't turn it off, its
69 like the server's power button is stuck being pushed in and it won't
70 turn on in that state. You will need to physically replug usb power to
71 the relay so the channel goes back to its default off state. If this
72 script somehow dies after turning a relay on, running it again will turn
73 it off. That is why this script runs locally and ignores signals when
74 its running. If you run this script over ssh and lose connection as this
75 script is running, it will continue to run and complete.
78 if [[ -e /etc
/libremanage.conf
]]; then
79 echo "Current /etc/libremanage.conf:"
80 cat /etc
/libremanage.conf
82 echo "Note: /etc/libremanage.conf does not exist on the current machine"
90 if [[ $1 == --switch ]]; then
99 read action hostchan board_id
<<<"$@"
101 if [[ -e /etc
/libremanage.conf
]]; then
102 source /etc
/libremanage.conf
105 # Use config vars when appropriate. We know the arg is a HOSTNAME if it
106 # doesn't start with a number.
113 # allow board_id to be empty if its unset
114 if [[ ! $board_id ]]; then
115 board_id_var
=${hostchan}_board_id
116 board_id
=${!board_id_var}
119 chan_var
=${hostchan}_channel
121 if [[ ! $chan ]]; then
122 echo "error: channel not set on cli and not found in /etc/libremanage.conf" >&2
126 type_var
=${hostchan}_type
128 if [[ $type == switch
]]; then
135 if ! $switch && hidusb-relay-cmd state |
grep -F R
$CHAN=ON
; then
137 $0: ERROR: Output from hidusb-relay-cmd state shows an ON relay, as if
138 the power button is stuck in the pressed state. This could mean another
139 command instance is running, or it got stuck on due to an uncompleted
140 run. Exiting now so you can investigate. To manually switch it back to
141 OFF, run the following command, or unplug the relay from usb power.
143 hidusb-relay-cmd $board_id_arg off $chan
150 case $
(hidusb-relay-cmd state|
wc -l) in
152 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
157 if [[ ! $board_id ]]; then
158 echo "error: more than 1 relay device, so passing its id is required" >&2
165 if [[ $board_id ]]; then
166 # leave this as an empty var if its not passed
167 board_id_arg
="id=$board_id"
170 # ignore errors and continue on if a command fails from here
172 # ignore hup so we complete even if there is a connection problem, and
173 # force anyone to kill -9 so we might complete in more cases, for
174 # example if a reboot is happening
175 trap '' HUP INT QUIT TERM
176 echo "$0: doing $action. shell commands will be printed to the terminal."
180 v hidusb-relay-cmd
$board_id_arg on
$chan
182 v hidusb-relay-cmd
$board_id_arg on
$chan
184 v hidusb-relay-cmd
$board_id_arg off
$chan
189 v hidusb-relay-cmd
$board_id_arg off
$chan
191 v hidusb-relay-cmd
$board_id_arg on
$chan
193 v hidusb-relay-cmd
$board_id_arg off
$chan
198 v hidusb-relay-cmd
$board_id_arg on
$chan
200 v hidusb-relay-cmd
$board_id_arg off
$chan
202 v hidusb-relay-cmd
$board_id_arg on
$chan
204 v hidusb-relay-cmd
$board_id_arg off
$chan
206 v hidusb-relay-cmd
$board_id_arg on
$chan
208 v hidusb-relay-cmd
$board_id_arg off
$chan
212 echo "error: action arg not supported" >&2
215 echo "$0: script ended. exiting"