add support for wndr3700 style power switch
[libremanage.git] / libremanage
1 #!/bin/bash
2
3 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
4 set -eE -o pipefail
5
6 # verbose command
7 v() {
8 printf "$0 running: %s\n" "$*"
9 "$@"
10 }
11
12
13 usage() {
14 cat <<EOF
15 Basic Usage: libremanage poweroff|poweron|reboot HOSTNAME
16
17 Available HOSTNAMEs defined in /etc/libremanage.conf on this machine:
18
19 EOF
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
29 Thats all you need to know unless you are defining configuration or
30 setting up a new device.
31
32
33
34
35
36 Advanced Usage: libremanage [--switch] poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID]
37
38 Note, the relay's channels default state when plugged in are off.
39
40 Example /etc/libremanage.conf:
41
42 # This config is sourced from bash, so make sure its valid bash.
43 cephserver3_channel=1
44 cephserver3_board_id=HURTM
45 cephserver2_channel=2
46 cephserver2_board_id=HURTM
47
48 librecmc1_type=switch
49 cephserver2_type=button # default
50
51 # end of config
52
53
54 Example use:
55
56 $ libremanage reboot cephserver3
57
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.
61
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.
65
66
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.
76
77 EOF
78 if [[ -e /etc/libremanage.conf ]]; then
79 echo "Current /etc/libremanage.conf:"
80 cat /etc/libremanage.conf
81 else
82 echo "Note: /etc/libremanage.conf does not exist on the current machine"
83 fi
84 exit 0
85 }
86
87
88 if (( $# < 2 )); then
89 usage
90 fi
91
92 switch=false
93 if [[ $1 == --switch ]]; then
94 switch=true
95 fi
96
97 read action chan board_id <<<"$@"
98
99 if [[ -e /etc/libremanage.conf ]]; then
100 source /etc/libremanage.conf
101 fi
102
103 # Use config vars when appropriate. We know the arg is a HOSTNAME if it
104 # doesn't start with a number.
105 if [[ $chan != [0-9]* ]]; then
106 if [[ ! $board_id ]]; then
107 board_id_var=${chan}_board_id
108 board_id=${!board_id_var}
109 fi
110 chan_var=${chan}_channel
111 chan=${!chan_var}
112
113 if [[ ! $chan ]]; then
114 echo "error: channel not found in /etc/libremanage.conf" >&2
115 exit 1
116 fi
117 fi
118
119
120 if hidusb-relay-cmd state | grep ON; then
121 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
122 # TODO: exit in this case and print out the command to turn it off.
123 fi
124
125
126 case $(hidusb-relay-cmd state|wc -l) in
127 0)
128 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
129 exit 1
130 ;;
131 1) : ;;
132 *)
133 if [[ ! $board_id ]]; then
134 echo "error: more than 1 relay device, so passing its id is required" >&2
135 exit 1
136 fi
137 ;;
138 esac
139
140
141 if [[ $board_id ]]; then
142 # leave this as an empty var if its not passed
143 board_id_arg="id=$board_id"
144 fi
145
146 # ignore errors and continue on if a command fails from here
147 set +eE +o pipefail
148 # ignore hup so we complete even if there is a connection problem, and
149 # force anyone to kill -9 so we might complete in more cases, for
150 # example if a reboot is happening
151 trap '' HUP INT QUIT TERM
152 echo "$0: doing $action. shell commands will be printed to the terminal."
153 case $action in
154 poweroff)
155 if $switch; then
156 v hidusb-relay-cmd $board_id_arg off $chan
157 else
158 v hidusb-relay-cmd $board_id_arg on $chan
159 v sleep 6
160 v hidusb-relay-cmd $board_id_arg off $chan
161 fi
162 ;;
163 poweron)
164 if $switch; then
165 v hidusb-relay-cmd $board_id_arg on $chan
166 else
167 v hidusb-relay-cmd $board_id_arg on $chan
168 v sleep 1
169 v hidusb-relay-cmd $board_id_arg off $chan
170 fi
171 ;;
172 reboot)
173 if $switch; then
174 v hidusb-relay-cmd $board_id_arg off $chan
175 v sleep 4
176 v hidusb-relay-cmd $board_id_arg on $chan
177 else
178 v hidusb-relay-cmd $board_id_arg on $chan
179 v sleep 6
180 v hidusb-relay-cmd $board_id_arg off $chan
181 v sleep 1
182 v hidusb-relay-cmd $board_id_arg on $chan
183 v sleep 1
184 v hidusb-relay-cmd $board_id_arg off $chan
185 fi
186 ;;
187 *)
188 echo "error: action arg not supported" >&2
189 exit 1
190 esac
191 echo "$0: script ended. exiting"