Commit | Line | Data |
---|---|---|
4e2acbd1 | 1 | #!/bin/bash |
bbc21163 | 2 | |
4e2acbd1 IK |
3 | trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR |
4 | set -eE -o pipefail | |
3f7ab73a | 5 | |
4e2acbd1 IK |
6 | # verbose command |
7 | v() { | |
8 | printf "$0 running: %s\n" "$*" | |
9 | "$@" | |
10 | } | |
21ead159 | 11 | |
21ead159 | 12 | |
4e2acbd1 IK |
13 | usage() { |
14 | cat <<EOF | |
15 | Usage: libremanage poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID] | |
e799dcd1 | 16 | |
a7d15c92 | 17 | HOSTNAME is based on configuration in /etc/libremanage.conf. |
d8ddde1a | 18 | |
4e2acbd1 | 19 | Note, the relay's channels default state when plugged in are off. |
6e59af0c | 20 | |
a7d15c92 IK |
21 | Example config: |
22 | ||
23 | # This config is sourced from bash, so make sure its valid bash. | |
24 | cephserver3_channel=1 | |
25 | cephserver3_board_id=HURTM | |
26 | cephserver2_channel=2 | |
27 | cephserver2_board_id=HURTM | |
28 | # end of config | |
29 | ||
30 | Example usage given the above config: | |
31 | ||
32 | libremanage reboot cephserver3 | |
33 | ||
34 | BOARD_ID is not needed if only 1 relay device is connected, or if it is | |
35 | defined in the config. To understand CHANNEL and BOARD_ID, run | |
36 | hidusb-relay-cmd and hidusb-relay-cmd state. | |
37 | ||
38 | ||
397823a6 IK |
39 | WARNING: Avoid ever manually running "hidusb-relay-cmd on" in |
40 | production, because if you lose connectivity and can't turn it off, its | |
41 | like the server's power button is stuck being pushed in and it won't | |
42 | turn on in that state. You will need to physically replug usb power to | |
43 | the relay so the channel goes back to its default off state. If this | |
44 | script somehow dies after turning a relay on, running it again will turn | |
45 | it off. That is why this script runs locally and ignores signals when | |
46 | its running. If you run this script over ssh and lose connection as this | |
47 | script is running, it will continue to run and complete. | |
e14d7650 | 48 | |
4e2acbd1 IK |
49 | EOF |
50 | if [[ -e /etc/libremanage.conf ]]; then | |
51 | echo "Current config:" | |
a7d15c92 | 52 | cat /etc/libremanage.conf |
4e2acbd1 IK |
53 | else |
54 | echo "Note: /etc/libremanage.conf does not exist on the current machine" | |
55 | fi | |
56 | exit 0 | |
d8ddde1a AR |
57 | } |
58 | ||
22f864bb | 59 | |
4e2acbd1 IK |
60 | if (( $# < 2 )); then |
61 | usage | |
62 | fi | |
63 | ||
64 | read action chan board_id <<<"$@" | |
65 | ||
66 | if [[ -e /etc/libremanage.conf ]]; then | |
67 | source /etc/libremanage.conf | |
68 | fi | |
69 | ||
a7d15c92 IK |
70 | # Use config vars when appropriate. We know the arg is a HOSTNAME if it |
71 | # doesn't start with a number. | |
4e2acbd1 IK |
72 | if [[ $chan != [0-9]* ]]; then |
73 | if [[ ! $board_id ]]; then | |
74 | board_id_var=${chan}_board_id | |
75 | board_id=${!board_id_var} | |
76 | fi | |
77 | chan_var=${chan}_channel | |
78 | chan=${!chan_var} | |
79 | ||
80 | if [[ ! $chan ]]; then | |
81 | echo "error: channel not found in /etc/libremanage.conf" >&2 | |
82 | exit 1 | |
83 | fi | |
84 | fi | |
85 | ||
86 | ||
87 | if hidusb-relay-cmd state | grep ON; then | |
88 | 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 | |
89 | # TODO: exit in this case and print out the command to turn it off. | |
90 | fi | |
91 | ||
92 | ||
93 | case $(hidusb-relay-cmd state|wc -l) in | |
94 | 0) | |
95 | echo "error: got 0 lines from running hidusb-relay-cmd state" >&2 | |
96 | exit 1 | |
97 | ;; | |
98 | 1) : ;; | |
99 | *) | |
100 | if [[ ! $board_id ]]; then | |
101 | echo "error: more than 1 relay device, so passing its id is required" >&2 | |
102 | exit 1 | |
103 | fi | |
104 | ;; | |
105 | esac | |
106 | ||
107 | ||
108 | if [[ $board_id ]]; then | |
109 | # leave this as an empty var if its not passed | |
110 | board_id_arg="id=$board_id" | |
111 | fi | |
112 | ||
113 | # ignore errors and continue on if a command fails from here | |
114 | set +eE +o pipefail | |
115 | # ignore hup so we complete even if there is a connection problem, and | |
116 | # force anyone to kill -9 so we might complete in more cases, for | |
117 | # example if a reboot is happening | |
118 | trap '' HUP INT QUIT TERM | |
119 | echo "$0: doing $action. shell commands will be printed to the terminal." | |
120 | case $action in | |
121 | poweroff) | |
122 | v hidusb-relay-cmd $board_id_arg on $chan | |
123 | v sleep 6 | |
124 | v hidusb-relay-cmd $board_id_arg off $chan | |
125 | ;; | |
126 | poweron) | |
127 | v hidusb-relay-cmd $board_id_arg on $chan | |
128 | v sleep 1 | |
129 | v hidusb-relay-cmd $board_id_arg off $chan | |
130 | ;; | |
131 | reboot) | |
132 | v hidusb-relay-cmd $board_id_arg on $chan | |
133 | v sleep 6 | |
134 | v hidusb-relay-cmd $board_id_arg off $chan | |
135 | v sleep 1 | |
136 | v hidusb-relay-cmd $board_id_arg on $chan | |
137 | v sleep 1 | |
138 | v hidusb-relay-cmd $board_id_arg off $chan | |
139 | ;; | |
140 | *) | |
141 | echo "error: action arg not supported" >&2 | |
142 | exit 1 | |
143 | esac | |
144 | echo "$0: script ended. exiting" |