finish deprecation of old libremanage command
[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
4e2acbd1
IK
17BOARD_ID is not needed if only 1 relay device is connected. To
18understand CHANNEL and BOARD_ID, run hidusb-relay-cmd and
19hidusb-relay-cmd state.
21ead159 20
4e2acbd1
IK
21Using a configuration (/etc/libremanage.conf) allows you to use HOSTNAME
22instead of CHANNEL and BOARD_ID.
d8ddde1a 23
4e2acbd1 24Note, the relay's channels default state when plugged in are off.
6e59af0c 25
4e2acbd1
IK
26WARNING: If you lose access to the machine controlling a relay and a
27relay channel is stuck on, you will need to manually replug usb power to
28the relay so the channel goes back to its default off state before you
29can turn on the server it is connected to, because its as if the servers
30power button is stuck being held down. That is why this script runs
31locally and ignores signals when its running.
e14d7650 32
4e2acbd1 33Example config:
e14d7650 34
4e2acbd1
IK
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.
37cephserver3_channel=1
38cephserver3_board_id=HURTM
39cephserver2_channel=2
40cephserver2_board_id=HURTM
e14d7650 41
4e2acbd1
IK
42EOF
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
d8ddde1a
AR
50}
51
22f864bb 52
4e2acbd1
IK
53if (( $# < 2 )); then
54 usage
55fi
56
57read action chan board_id <<<"$@"
58
59if [[ -e /etc/libremanage.conf ]]; then
60 source /etc/libremanage.conf
61fi
62
63# we know HOSTNAME is given if it doesn't start with a number.
64if [[ $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
76fi
77
78
79if 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.
82fi
83
84
85case $(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 ;;
97esac
98
99
100if [[ $board_id ]]; then
101 # leave this as an empty var if its not passed
102 board_id_arg="id=$board_id"
103fi
104
105# ignore errors and continue on if a command fails from here
106set +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
110trap '' HUP INT QUIT TERM
111echo "$0: doing $action. shell commands will be printed to the terminal."
112case $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
135esac
136echo "$0: script ended. exiting"