fix install script from returning false error
[libremanage.git] / libremanage
... / ...
CommitLineData
1#!/bin/bash
2
3trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
4set -eE -o pipefail
5
6# verbose command
7v() {
8 printf "$0 running: %s\n" "$*"
9 "$@"
10}
11
12
13usage() {
14 cat <<EOF
15Basic Usage: libremanage poweroff|poweron|reboot HOSTNAME
16
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
30setting up a new device.
31
32
33
34
35
36Advanced Usage: libremanage [--switch] poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID]
37
38Note, the relay's channels default state when plugged in are off.
39
40Example /etc/libremanage.conf:
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
47
48librecmc1_type=switch
49cephserver2_type=button # default
50
51# end of config
52
53
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.
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
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.
76
77EOF
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
89switch=false
90if [[ $1 == --switch ]]; then
91 switch=true
92 shift
93fi
94
95if (( $# < 2 )); then
96 usage
97fi
98
99read action hostchan board_id <<<"$@"
100
101if [[ -e /etc/libremanage.conf ]]; then
102 source /etc/libremanage.conf
103fi
104
105# Use config vars when appropriate. We know the arg is a HOSTNAME if it
106# doesn't start with a number.
107case $hostchan in
108 [0-9]*)
109 chan=$hostchan
110 ;;
111 *)
112
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}
117 fi
118
119 chan_var=${hostchan}_channel
120 chan=${!chan_var}
121 if [[ ! $chan ]]; then
122 echo "error: channel not set on cli and not found in /etc/libremanage.conf" >&2
123 exit 1
124 fi
125
126 type_var=${hostchan}_type
127 type=${!type_var}
128 if [[ $type == switch ]]; then
129 switch=true
130 fi
131 ;;
132esac
133
134
135if ! $switch && hidusb-relay-cmd state | grep -F R$CHAN=ON; then
136 cat >&2 <<EOF
137$0: ERROR: Output from hidusb-relay-cmd state shows an ON relay, as if
138the power button is stuck in the pressed state. This could mean another
139command instance is running, or it got stuck on due to an uncompleted
140run. Exiting now so you can investigate. To manually switch it back to
141OFF, run the following command, or unplug the relay from usb power.
142
143hidusb-relay-cmd $board_id_arg off $chan
144
145EOF
146 exit 1
147fi
148
149
150case $(hidusb-relay-cmd state|wc -l) in
151 0)
152 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
153 exit 1
154 ;;
155 1) : ;;
156 *)
157 if [[ ! $board_id ]]; then
158 echo "error: more than 1 relay device, so passing its id is required" >&2
159 exit 1
160 fi
161 ;;
162esac
163
164
165if [[ $board_id ]]; then
166 # leave this as an empty var if its not passed
167 board_id_arg="id=$board_id"
168fi
169
170# ignore errors and continue on if a command fails from here
171set +eE +o pipefail
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
175trap '' HUP INT QUIT TERM
176echo "$0: doing $action. shell commands will be printed to the terminal."
177case $action in
178 poweroff)
179 if $switch; then
180 v hidusb-relay-cmd $board_id_arg on $chan
181 else
182 v hidusb-relay-cmd $board_id_arg on $chan
183 v sleep 6
184 v hidusb-relay-cmd $board_id_arg off $chan
185 fi
186 ;;
187 poweron)
188 if $switch; then
189 v hidusb-relay-cmd $board_id_arg off $chan
190 else
191 v hidusb-relay-cmd $board_id_arg on $chan
192 v sleep 1
193 v hidusb-relay-cmd $board_id_arg off $chan
194 fi
195 ;;
196 reboot)
197 if $switch; then
198 v hidusb-relay-cmd $board_id_arg on $chan
199 v sleep 4
200 v hidusb-relay-cmd $board_id_arg off $chan
201 else
202 v hidusb-relay-cmd $board_id_arg on $chan
203 v sleep 6
204 v hidusb-relay-cmd $board_id_arg off $chan
205 v sleep 1
206 v hidusb-relay-cmd $board_id_arg on $chan
207 v sleep 1
208 v hidusb-relay-cmd $board_id_arg off $chan
209 fi
210 ;;
211 *)
212 echo "error: action arg not supported" >&2
213 exit 1
214esac
215echo "$0: script ended. exiting"