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: If you lose access to the machine controlling a relay and a
27 relay channel is stuck on, you will need to manually replug usb power to
28 the relay so the channel goes back to its default off state before you
29 can turn on the server it is connected to, because its as if the servers
30 power button is stuck being held down. That is why this script runs
31 locally and ignores signals when its running.
35 # This config is sourced from bash, so make sure its valid bash.
36 # board_id is not needed if only 1 relay device is connected.
38 cephserver3_board_id=HURTM
40 cephserver2_board_id=HURTM
43 if [[ -e /etc
/libremanage.conf
]]; then
44 echo "Current config:"
45 v
cat /etc
/libremanage.conf
47 echo "Note: /etc/libremanage.conf does not exist on the current machine"
57 read action chan board_id
<<<"$@"
59 if [[ -e /etc
/libremanage.conf
]]; then
60 source /etc
/libremanage.conf
63 # we know HOSTNAME is given if it doesn't start with a number.
64 if [[ $chan != [0-9]* ]]; then
65 if [[ ! $board_id ]]; then
66 board_id_var
=${chan}_board_id
67 board_id
=${!board_id_var}
69 chan_var
=${chan}_channel
72 if [[ ! $chan ]]; then
73 echo "error: channel not found in /etc/libremanage.conf" >&2
79 if hidusb-relay-cmd state |
grep ON
; then
80 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
81 # TODO: exit in this case and print out the command to turn it off.
85 case $
(hidusb-relay-cmd state|
wc -l) in
87 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
92 if [[ ! $board_id ]]; then
93 echo "error: more than 1 relay device, so passing its id is required" >&2
100 if [[ $board_id ]]; then
101 # leave this as an empty var if its not passed
102 board_id_arg
="id=$board_id"
105 # ignore errors and continue on if a command fails from here
107 # ignore hup so we complete even if there is a connection problem, and
108 # force anyone to kill -9 so we might complete in more cases, for
109 # example if a reboot is happening
110 trap '' HUP INT QUIT TERM
111 echo "$0: doing $action. shell commands will be printed to the terminal."
114 v hidusb-relay-cmd
$board_id_arg on
$chan
116 v hidusb-relay-cmd
$board_id_arg off
$chan
119 v hidusb-relay-cmd
$board_id_arg on
$chan
121 v hidusb-relay-cmd
$board_id_arg off
$chan
124 v hidusb-relay-cmd
$board_id_arg on
$chan
126 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
133 echo "error: action arg not supported" >&2
136 echo "$0: script ended. exiting"