Adapt daemon-util to ENABLE_CONFD
[ganeti-local] / daemons / daemon-util.in
1 #!/bin/bash
2 #
3
4 # Copyright (C) 2009, 2011 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 # This is a list of all daemons and the order in which they're started. The
26 # order is important as there are dependencies between them. On shutdown,
27 # they're stopped in reverse order.
28 DAEMONS=(
29   ganeti-noded
30   ganeti-masterd
31   ganeti-rapi
32   )
33
34 if [[ "@CUSTOM_ENABLE_CONFD@" == True ]]; then
35   DAEMONS+=( ganeti-confd )
36 fi
37
38 NODED_ARGS=
39 MASTERD_ARGS=
40 CONFD_ARGS=
41 RAPI_ARGS=
42
43 # Read defaults file if it exists
44 if [[ -s $defaults_file ]]; then
45   . $defaults_file
46 fi
47
48 _daemon_pidfile() {
49   echo "@LOCALSTATEDIR@/run/ganeti/$1.pid"
50 }
51
52 _daemon_executable() {
53   echo "@PREFIX@/sbin/$1"
54 }
55
56 _daemon_usergroup() {
57   case "$1" in
58     masterd)
59       echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
60       ;;
61     confd)
62       echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
63       ;;
64     rapi)
65       echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
66       ;;
67     noded)
68       echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
69       ;;
70     *)
71       echo "root:@GNTDAEMONSGROUP@"
72       ;;
73   esac
74 }
75
76 # Checks whether the local machine is part of a cluster
77 check_config() {
78   local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem
79   local fname
80
81   for fname in $server_pem; do
82     if [[ ! -f $fname ]]; then
83       echo "Missing configuration file $fname" >&2
84       return 1
85     fi
86   done
87
88   return 0
89 }
90
91 # Checks the exit code of a daemon
92 check_exitcode() {
93   if [[ "$#" -lt 1 ]]; then
94     echo 'Missing exit code.' >&2
95     return 1
96   fi
97
98   local rc="$1"; shift
99
100   case "$rc" in
101     0) ;;
102     11)
103       echo "not master"
104     ;;
105     *)
106       echo "exit code $rc"
107       return 1
108     ;;
109   esac
110
111   return 0
112 }
113
114 # Prints a list of all daemons in the order in which they should be started
115 list_start_daemons() {
116   local name
117
118   for name in "${DAEMONS[@]}"; do
119     echo "$name"
120   done
121 }
122
123 # Prints a list of all daemons in the order in which they should be stopped
124 list_stop_daemons() {
125   list_start_daemons | tac
126 }
127
128 # Checks whether a daemon name is known
129 is_daemon_name() {
130   if [[ "$#" -lt 1 ]]; then
131     echo 'Missing daemon name.' >&2
132     return 1
133   fi
134
135   local name="$1"; shift
136
137   for i in "${DAEMONS[@]}"; do
138     if [[ "$i" == "$name" ]]; then
139       return 0
140     fi
141   done
142
143   echo "Unknown daemon name '$name'" >&2
144   return 1
145 }
146
147 # Checks whether daemon is running
148 check() {
149   if [[ "$#" -lt 1 ]]; then
150     echo 'Missing daemon name.' >&2
151     return 1
152   fi
153
154   local name="$1"; shift
155
156   start-stop-daemon --stop --signal 0 --quiet \
157     --pidfile $(_daemon_pidfile $name)
158 }
159
160 # Starts a daemon
161 start() {
162   if [[ "$#" -lt 1 ]]; then
163     echo 'Missing daemon name.' >&2
164     return 1
165   fi
166
167   local name="$1"; shift
168
169   if [[ "$name" == ganeti-confd &&
170         "@CUSTOM_ENABLE_CONFD@" == False ]]; then
171     echo 'ganeti-confd disabled at build time' >&2
172     return 1
173   fi
174
175   # Convert daemon name to uppercase after removing "ganeti-" prefix
176   local plain_name=${name#ganeti-}
177   local ucname=$(tr a-z A-Z <<<$plain_name)
178
179   # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
180   eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
181
182   @PKGLIBDIR@/ensure-dirs
183
184   start-stop-daemon --start --quiet --oknodo \
185     --pidfile $(_daemon_pidfile $name) \
186     --startas $(_daemon_executable $name) \
187     --chuid $(_daemon_usergroup $plain_name) \
188     -- $args "$@"
189 }
190
191 # Stops a daemon
192 stop() {
193   if [[ "$#" -lt 1 ]]; then
194     echo 'Missing daemon name.' >&2
195     return 1
196   fi
197
198   local name="$1"; shift
199
200   start-stop-daemon --stop --quiet --oknodo --retry 30 \
201     --pidfile $(_daemon_pidfile $name)
202 }
203
204 # Starts a daemon if it's not yet running
205 check_and_start() {
206   local name="$1"
207
208   if ! check $name; then
209     start $name
210   fi
211 }
212
213 # Starts the master role
214 start_master() {
215   start ganeti-masterd
216   start ganeti-rapi
217 }
218
219 # Stops the master role
220 stop_master() {
221   stop ganeti-rapi
222   stop ganeti-masterd
223 }
224
225 # Start all daemons
226 start_all() {
227   for i in $(list_start_daemons); do
228     local rc=0
229
230     # Try to start daemon
231     start $i || rc=$?
232
233     if ! errmsg=$(check_exitcode $rc); then
234       echo "$errmsg" >&2
235       return 1
236     fi
237   done
238
239   return 0
240 }
241
242 # Stop all daemons
243 stop_all() {
244   for i in $(list_stop_daemons); do
245     stop $i
246   done
247 }
248
249 # Reloads the SSH keys
250 reload_ssh_keys() {
251   @RPL_SSH_INITD_SCRIPT@ restart
252 }
253
254 if [[ "$#" -lt 1 ]]; then
255   echo "Usage: $0 <action>" >&2
256   exit 1
257 fi
258
259 orig_action=$1; shift
260
261 if [[ "$orig_action" == *_* ]]; then
262   echo "Command must not contain underscores" >&2
263   exit 1
264 fi
265
266 # Replace all dashes (-) with underlines (_)
267 action=${orig_action//-/_}
268
269 # Is it a known function?
270 if ! declare -F "$action" >/dev/null 2>&1; then
271   echo "Unknown command: $orig_action" >&2
272   exit 1
273 fi
274
275 # Call handler function
276 $action "$@"