daemon-util: Require dashes in commands
[ganeti-local] / daemons / daemon-util.in
1 #!/bin/bash
2 #
3
4 # Copyright (C) 2009 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 set -e
22
23 defaults_file=@SYSCONFDIR@/default/ganeti
24
25 NODED_ARGS=
26 MASTERD_ARGS=
27 CONFD_ARGS=
28 RAPI_ARGS=
29
30 # Read defaults file if it exists
31 if [[ -s $defaults_file ]]; then
32   . $defaults_file
33 fi
34
35 _daemon_pidfile() {
36   echo "@LOCALSTATEDIR@/run/ganeti/$1.pid"
37 }
38
39 # Checks whether daemon is running
40 check() {
41   if [[ "$#" -lt 1 ]]; then
42     echo 'Missing daemon name.' >&2
43     exit 1
44   fi
45
46   local name="$1"; shift
47
48   start-stop-daemon --stop --signal 0 --quiet \
49     --pidfile $(_daemon_pidfile $name)
50 }
51
52 # Starts a daemon
53 start() {
54   if [[ "$#" -lt 1 ]]; then
55     echo 'Missing daemon name.' >&2
56     exit 1
57   fi
58
59   local name="$1"; shift
60
61   # Convert daemon name to uppercase after removing "ganeti-" prefix
62   local ucname=$(echo ${name#ganeti-} | tr a-z A-Z)
63
64   # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
65   eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
66
67   start-stop-daemon --start --quiet --oknodo \
68     --pidfile $(_daemon_pidfile $name) \
69     --startas "@PREFIX@/sbin/$name" \
70     -- $args "$@"
71 }
72
73 # Stops a daemon
74 stop() {
75   if [[ "$#" -lt 1 ]]; then
76     echo 'Missing daemon name.' >&2
77     exit 1
78   fi
79
80   local name="$1"; shift
81
82   start-stop-daemon --stop --quiet --oknodo --retry 30 \
83     --pidfile $(_daemon_pidfile $name)
84 }
85
86 # Starts a daemon if it's not yet running
87 check_and_start() {
88   local name="$1"
89
90   if ! check $name; then
91     start $name
92   fi
93 }
94
95 # Starts the master role
96 start_master() {
97   start ganeti-masterd
98   start ganeti-rapi
99 }
100
101 # Stops the master role
102 stop_master() {
103   stop ganeti-rapi
104   stop ganeti-masterd
105 }
106
107 # Reloads the SSH keys
108 reload_ssh_keys() {
109   @RPL_SSH_INITD_SCRIPT@ restart
110 }
111
112 if [[ "$#" -lt 1 ]]; then
113   echo "Usage: $0 <action>" >&2
114   exit 1
115 fi
116
117 orig_action=$1; shift
118
119 if [[ "$orig_action" == *_* ]]; then
120   echo "Command must not contain underscores" >&2
121   exit 1
122 fi
123
124 # Replace all dashes (-) with underlines (_)
125 action=${orig_action//-/_}
126
127 # Is it a known function?
128 if ! declare -F "$action" >/dev/null 2>&1; then
129   echo "Unknown command: $orig_action" >&2
130   exit 1
131 fi
132
133 # Call handler function
134 $action "$@"