docs
[libremanage.git] / libremanage
CommitLineData
4e2acbd1 1#!/bin/bash
bbc21163 2
4e2acbd1
IK
3trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
4set -eE -o pipefail
3f7ab73a 5
4e2acbd1
IK
6# verbose command
7v() {
8 printf "$0 running: %s\n" "$*"
9 "$@"
10}
21ead159 11
21ead159 12
4e2acbd1
IK
13usage() {
14 cat <<EOF
15Usage: libremanage poweroff|poweron|reboot HOSTNAME|CHANNEL [BOARD_ID]
e799dcd1 16
a7d15c92 17HOSTNAME is based on configuration in /etc/libremanage.conf.
d8ddde1a 18
4e2acbd1 19Note, the relay's channels default state when plugged in are off.
6e59af0c 20
a7d15c92
IK
21Example config:
22
23# This config is sourced from bash, so make sure its valid bash.
24cephserver3_channel=1
25cephserver3_board_id=HURTM
26cephserver2_channel=2
27cephserver2_board_id=HURTM
28# end of config
29
30Example usage given the above config:
31
32libremanage reboot cephserver3
33
34BOARD_ID is not needed if only 1 relay device is connected, or if it is
35defined in the config. To understand CHANNEL and BOARD_ID, run
36hidusb-relay-cmd and hidusb-relay-cmd state.
37
38
397823a6
IK
39WARNING: Avoid ever manually running "hidusb-relay-cmd on" in
40production, because if you lose connectivity and can't turn it off, its
41like the server's power button is stuck being pushed in and it won't
42turn on in that state. You will need to physically replug usb power to
43the relay so the channel goes back to its default off state. If this
44script somehow dies after turning a relay on, running it again will turn
45it off. That is why this script runs locally and ignores signals when
46its running. If you run this script over ssh and lose connection as this
47script is running, it will continue to run and complete.
e14d7650 48
4e2acbd1
IK
49EOF
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
60if (( $# < 2 )); then
61 usage
62fi
63
64read action chan board_id <<<"$@"
65
66if [[ -e /etc/libremanage.conf ]]; then
67 source /etc/libremanage.conf
68fi
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
72if [[ $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
84fi
85
86
87if 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.
90fi
91
92
93case $(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 ;;
105esac
106
107
108if [[ $board_id ]]; then
109 # leave this as an empty var if its not passed
110 board_id_arg="id=$board_id"
111fi
112
113# ignore errors and continue on if a command fails from here
114set +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
118trap '' HUP INT QUIT TERM
119echo "$0: doing $action. shell commands will be printed to the terminal."
120case $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
143esac
144echo "$0: script ended. exiting"