improve docs
[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 Usage: libremanage poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID]
16
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.
20
21 Using a configuration (/etc/libremanage.conf) allows you to use HOSTNAME
22 instead of CHANNEL and BOARD_ID.
23
24 Note, the relay's channels default state when plugged in are off.
25
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.
35
36 Example config:
37
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.
40 cephserver3_channel=1
41 cephserver3_board_id=HURTM
42 cephserver2_channel=2
43 cephserver2_board_id=HURTM
44
45 EOF
46 if [[ -e /etc/libremanage.conf ]]; then
47 echo "Current config:"
48 v cat /etc/libremanage.conf
49 else
50 echo "Note: /etc/libremanage.conf does not exist on the current machine"
51 fi
52 exit 0
53 }
54
55
56 if (( $# < 2 )); then
57 usage
58 fi
59
60 read action chan board_id <<<"$@"
61
62 if [[ -e /etc/libremanage.conf ]]; then
63 source /etc/libremanage.conf
64 fi
65
66 # Use config vars. We know the arg is a HOSTNAME if it doesn't start
67 # with a number.
68 if [[ $chan != [0-9]* ]]; then
69 if [[ ! $board_id ]]; then
70 board_id_var=${chan}_board_id
71 board_id=${!board_id_var}
72 fi
73 chan_var=${chan}_channel
74 chan=${!chan_var}
75
76 if [[ ! $chan ]]; then
77 echo "error: channel not found in /etc/libremanage.conf" >&2
78 exit 1
79 fi
80 fi
81
82
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.
86 fi
87
88
89 case $(hidusb-relay-cmd state|wc -l) in
90 0)
91 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
92 exit 1
93 ;;
94 1) : ;;
95 *)
96 if [[ ! $board_id ]]; then
97 echo "error: more than 1 relay device, so passing its id is required" >&2
98 exit 1
99 fi
100 ;;
101 esac
102
103
104 if [[ $board_id ]]; then
105 # leave this as an empty var if its not passed
106 board_id_arg="id=$board_id"
107 fi
108
109 # ignore errors and continue on if a command fails from here
110 set +eE +o pipefail
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."
116 case $action in
117 poweroff)
118 v hidusb-relay-cmd $board_id_arg on $chan
119 v sleep 6
120 v hidusb-relay-cmd $board_id_arg off $chan
121 ;;
122 poweron)
123 v hidusb-relay-cmd $board_id_arg on $chan
124 v sleep 1
125 v hidusb-relay-cmd $board_id_arg off $chan
126 ;;
127 reboot)
128 v hidusb-relay-cmd $board_id_arg on $chan
129 v sleep 6
130 v hidusb-relay-cmd $board_id_arg off $chan
131 v sleep 1
132 v hidusb-relay-cmd $board_id_arg on $chan
133 v sleep 1
134 v hidusb-relay-cmd $board_id_arg off $chan
135 ;;
136 *)
137 echo "error: action arg not supported" >&2
138 exit 1
139 esac
140 echo "$0: script ended. exiting"