finish deprecation of old libremanage command
[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: 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.
32
33 Example config:
34
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.
37 cephserver3_channel=1
38 cephserver3_board_id=HURTM
39 cephserver2_channel=2
40 cephserver2_board_id=HURTM
41
42 EOF
43 if [[ -e /etc/libremanage.conf ]]; then
44 echo "Current config:"
45 v cat /etc/libremanage.conf
46 else
47 echo "Note: /etc/libremanage.conf does not exist on the current machine"
48 fi
49 exit 0
50 }
51
52
53 if (( $# < 2 )); then
54 usage
55 fi
56
57 read action chan board_id <<<"$@"
58
59 if [[ -e /etc/libremanage.conf ]]; then
60 source /etc/libremanage.conf
61 fi
62
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}
68 fi
69 chan_var=${chan}_channel
70 chan=${!chan_var}
71
72 if [[ ! $chan ]]; then
73 echo "error: channel not found in /etc/libremanage.conf" >&2
74 exit 1
75 fi
76 fi
77
78
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.
82 fi
83
84
85 case $(hidusb-relay-cmd state|wc -l) in
86 0)
87 echo "error: got 0 lines from running hidusb-relay-cmd state" >&2
88 exit 1
89 ;;
90 1) : ;;
91 *)
92 if [[ ! $board_id ]]; then
93 echo "error: more than 1 relay device, so passing its id is required" >&2
94 exit 1
95 fi
96 ;;
97 esac
98
99
100 if [[ $board_id ]]; then
101 # leave this as an empty var if its not passed
102 board_id_arg="id=$board_id"
103 fi
104
105 # ignore errors and continue on if a command fails from here
106 set +eE +o pipefail
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."
112 case $action in
113 poweroff)
114 v hidusb-relay-cmd $board_id_arg on $chan
115 v sleep 6
116 v hidusb-relay-cmd $board_id_arg off $chan
117 ;;
118 poweron)
119 v hidusb-relay-cmd $board_id_arg on $chan
120 v sleep 1
121 v hidusb-relay-cmd $board_id_arg off $chan
122 ;;
123 reboot)
124 v hidusb-relay-cmd $board_id_arg on $chan
125 v sleep 6
126 v hidusb-relay-cmd $board_id_arg off $chan
127 v sleep 1
128 v hidusb-relay-cmd $board_id_arg on $chan
129 v sleep 1
130 v hidusb-relay-cmd $board_id_arg off $chan
131 ;;
132 *)
133 echo "error: action arg not supported" >&2
134 exit 1
135 esac
136 echo "$0: script ended. exiting"