3 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
8 printf "$0 running: %s\n" "$*"
15 Usage: libremanage poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID]
17 BOARD_ID is not needed if only 1 relay device is connected. To
18 understand CHANNEL and BOARD_ID, run hidusb-relay-cmd and
19 hidusb-relay-cmd state.
21 Using a configuration (/etc/libremanage.conf) allows you to use HOSTNAME
22 instead of CHANNEL and BOARD_ID.
24 Note, the relay's channels default state when plugged in are off.
26 WARNING: Avoid ever manually running "hidusb-relay-cmd on" in
27 production, because if you lose connectivity and can't turn it off, its
28 like the server's power button is stuck being pushed in and it won't
29 turn on in that state. You will need to physically replug usb power to
30 the relay so the channel goes back to its default off state. If this
31 script somehow dies after turning a relay on, running it again will turn
32 it off. That is why this script runs locally and ignores signals when
33 its running. If you run this script over ssh and lose connection as this
34 script is running, it will continue to run and complete.
38 # This config is sourced from bash, so make sure its valid bash.
39 # board_id is not needed if only 1 relay device is connected.
41 cephserver3_board_id=HURTM
43 cephserver2_board_id=HURTM
46 if [[ -e /etc
/libremanage.conf
]]; then
47 echo "Current config:"
48 v
cat /etc
/libremanage.conf
50 echo "Note: /etc/libremanage.conf does not exist on the current machine"
60 read action chan board_id
<<<"$@"
62 if [[ -e /etc
/libremanage.conf
]]; then
63 source /etc
/libremanage.conf
66 # Use config vars. We know the arg is a HOSTNAME if it doesn't start
68 if [[ $chan != [0-9]* ]]; then
69 if [[ ! $board_id ]]; then
70 board_id_var
=${chan}_board_id
71 board_id
=${!board_id_var}
73 chan_var
=${chan}_channel
76 if [[ ! $chan ]]; then
77 echo "error: channel not found in /etc/libremanage.conf" >&2
83 if hidusb-relay-cmd state |
grep ON
; then
84 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
85 # TODO: exit in this case and print out the command to turn it off.
89 case $
(hidusb-relay-cmd state|
wc -l) in
91 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
96 if [[ ! $board_id ]]; then
97 echo "error: more than 1 relay device, so passing its id is required" >&2
104 if [[ $board_id ]]; then
105 # leave this as an empty var if its not passed
106 board_id_arg
="id=$board_id"
109 # ignore errors and continue on if a command fails from here
111 # ignore hup so we complete even if there is a connection problem, and
112 # force anyone to kill -9 so we might complete in more cases, for
113 # example if a reboot is happening
114 trap '' HUP INT QUIT TERM
115 echo "$0: doing $action. shell commands will be printed to the terminal."
118 v hidusb-relay-cmd
$board_id_arg on
$chan
120 v hidusb-relay-cmd
$board_id_arg off
$chan
123 v hidusb-relay-cmd
$board_id_arg on
$chan
125 v hidusb-relay-cmd
$board_id_arg off
$chan
128 v hidusb-relay-cmd
$board_id_arg on
$chan
130 v hidusb-relay-cmd
$board_id_arg off
$chan
132 v hidusb-relay-cmd
$board_id_arg on
$chan
134 v hidusb-relay-cmd
$board_id_arg off
$chan
137 echo "error: action arg not supported" >&2
140 echo "$0: script ended. exiting"