4 # Copyright (C) 2009, 2011, 2012 Google Inc.
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.
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.
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
25 readonly defaults_file="$SYSCONFDIR/default/ganeti"
27 # This is a list of all daemons and the order in which they're started. The
28 # order is important as there are dependencies between them. On shutdown,
29 # they're stopped in reverse order.
37 [[ "@CUSTOM_ENABLE_CONFD@" == True ]]
40 if _confd_enabled; then
41 DAEMONS+=( ganeti-confd )
42 DAEMONS+=( ganeti-luxid )
46 [[ "@CUSTOM_ENABLE_MOND@" == True ]]
49 if _mond_enabled; then
50 DAEMONS+=( ganeti-mond )
60 # Read defaults file if it exists
61 if [[ -s $defaults_file ]]; then
65 # Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case
66 # start-stop-daemon is not available.
72 echo "$RUN_DIR/$1.pid"
75 _daemon_executable() {
76 echo "@PREFIX@/sbin/$1"
82 echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
85 echo "@GNTCONFDUSER@:@GNTDAEMONSGROUP@"
88 echo "@GNTLUXIDUSER@:@GNTLUXIDGROUP@"
91 echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
94 echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
97 echo "@GNTMONDUSER@:@GNTMONDGROUP@"
100 echo "root:@GNTDAEMONSGROUP@"
105 # Checks whether the local machine is part of a cluster
107 local server_pem=$DATA_DIR/server.pem
110 for fname in $server_pem; do
111 if [[ ! -f $fname ]]; then
112 echo "Missing configuration file $fname" >&2
120 # Checks the exit code of a daemon
122 if [[ "$#" -lt 1 ]]; then
123 echo 'Missing exit code.' >&2
143 # Prints path to PID file for a daemon.
145 if [[ "$#" -lt 1 ]]; then
146 echo 'Missing daemon name.' >&2
150 local name="$1"; shift
152 _daemon_pidfile $name
155 # Prints path to daemon executable.
156 daemon_executable() {
157 if [[ "$#" -lt 1 ]]; then
158 echo 'Missing daemon name.' >&2
162 local name="$1"; shift
164 _daemon_executable $name
167 # Prints a list of all daemons in the order in which they should be started
168 list_start_daemons() {
171 for name in "${DAEMONS[@]}"; do
176 # Prints a list of all daemons in the order in which they should be stopped
177 list_stop_daemons() {
178 list_start_daemons | tac
181 # Checks whether a daemon name is known
183 if [[ "$#" -lt 1 ]]; then
184 echo 'Missing daemon name.' >&2
188 local name="$1"; shift
190 for i in "${DAEMONS[@]}"; do
191 if [[ "$i" == "$name" ]]; then
196 echo "Unknown daemon name '$name'" >&2
200 # Checks whether daemon is running
202 if [[ "$#" -lt 1 ]]; then
203 echo 'Missing daemon name.' >&2
207 local name="$1"; shift
208 local pidfile=$(_daemon_pidfile $name)
209 local daemonexec=$(_daemon_executable $name)
211 if type -p start-stop-daemon >/dev/null; then
212 start-stop-daemon --stop --signal 0 --quiet \
215 _ignore_error status \
223 if [[ "$#" -lt 1 ]]; then
224 echo 'Missing daemon name.' >&2
228 local name="$1"; shift
229 # Convert daemon name to uppercase after removing "ganeti-" prefix
230 local plain_name=${name#ganeti-}
231 local ucname=$(tr a-z A-Z <<<$plain_name)
232 local pidfile=$(_daemon_pidfile $name)
233 local usergroup=$(_daemon_usergroup $plain_name)
234 local daemonexec=$(_daemon_executable $name)
236 if ( [[ "$name" == ganeti-confd ]] || [[ "$name" == ganeti-luxid ]] ) \
237 && ! _confd_enabled; then
238 echo 'ganeti-confd disabled at build time' >&2
242 # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
243 eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
245 @PKGLIBDIR@/ensure-dirs
247 if type -p start-stop-daemon >/dev/null; then
248 start-stop-daemon --start --quiet --oknodo \
250 --startas $daemonexec \
254 # TODO: Find a way to start daemon with a group, until then the group must
256 _ignore_error daemon \
258 --user ${usergroup%:*} \
259 $daemonexec $args "$@"
266 if [[ "$#" -lt 1 ]]; then
267 echo 'Missing daemon name.' >&2
271 local name="$1"; shift
272 local pidfile=$(_daemon_pidfile $name)
274 if type -p start-stop-daemon >/dev/null; then
275 start-stop-daemon --stop --quiet --oknodo --retry 30 \
278 _ignore_error killproc -p $pidfile $name
282 # Starts a daemon if it's not yet running
286 if ! check $name; then
291 # Starts the master role
297 # Stops the master role
305 for i in $(list_start_daemons); do
308 # Try to start daemon
311 if ! errmsg=$(check_exitcode $rc); then
322 for i in $(list_stop_daemons); do
327 # Reloads the SSH keys
329 @RPL_SSH_INITD_SCRIPT@ restart
332 # Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
333 if ! type -p start-stop-daemon >/dev/null && \
334 [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
335 _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
338 if [[ "$#" -lt 1 ]]; then
339 echo "Usage: $0 <action>" >&2
343 orig_action=$1; shift
345 if [[ "$orig_action" == *_* ]]; then
346 echo "Command must not contain underscores" >&2
350 # Replace all dashes (-) with underlines (_)
351 action=${orig_action//-/_}
353 # Is it a known function?
354 if ! declare -F "$action" >/dev/null 2>&1; then
355 echo "Unknown command: $orig_action" >&2
359 # Call handler function