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 )
45 [[ "@CUSTOM_ENABLE_MOND@" == True ]]
48 if _mond_enabled; then
49 DAEMONS+=( ganeti-mond )
58 # Read defaults file if it exists
59 if [[ -s $defaults_file ]]; then
63 # Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case
64 # start-stop-daemon is not available.
70 echo "$RUN_DIR/$1.pid"
73 _daemon_executable() {
74 echo "@PREFIX@/sbin/$1"
80 echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
83 echo "@GNTCONFDUSER@:@GNTDAEMONSGROUP@"
86 echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
89 echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
92 echo "@GNTMONDUSER@:@GNTMONDGROUP@"
95 echo "root:@GNTDAEMONSGROUP@"
100 # Checks whether the local machine is part of a cluster
102 local server_pem=$DATA_DIR/server.pem
105 for fname in $server_pem; do
106 if [[ ! -f $fname ]]; then
107 echo "Missing configuration file $fname" >&2
115 # Checks the exit code of a daemon
117 if [[ "$#" -lt 1 ]]; then
118 echo 'Missing exit code.' >&2
138 # Prints path to PID file for a daemon.
140 if [[ "$#" -lt 1 ]]; then
141 echo 'Missing daemon name.' >&2
145 local name="$1"; shift
147 _daemon_pidfile $name
150 # Prints path to daemon executable.
151 daemon_executable() {
152 if [[ "$#" -lt 1 ]]; then
153 echo 'Missing daemon name.' >&2
157 local name="$1"; shift
159 _daemon_executable $name
162 # Prints a list of all daemons in the order in which they should be started
163 list_start_daemons() {
166 for name in "${DAEMONS[@]}"; do
171 # Prints a list of all daemons in the order in which they should be stopped
172 list_stop_daemons() {
173 list_start_daemons | tac
176 # Checks whether a daemon name is known
178 if [[ "$#" -lt 1 ]]; then
179 echo 'Missing daemon name.' >&2
183 local name="$1"; shift
185 for i in "${DAEMONS[@]}"; do
186 if [[ "$i" == "$name" ]]; then
191 echo "Unknown daemon name '$name'" >&2
195 # Checks whether daemon is running
197 if [[ "$#" -lt 1 ]]; then
198 echo 'Missing daemon name.' >&2
202 local name="$1"; shift
203 local pidfile=$(_daemon_pidfile $name)
204 local daemonexec=$(_daemon_executable $name)
206 if type -p start-stop-daemon >/dev/null; then
207 start-stop-daemon --stop --signal 0 --quiet \
210 _ignore_error status \
218 if [[ "$#" -lt 1 ]]; then
219 echo 'Missing daemon name.' >&2
223 local name="$1"; shift
224 # Convert daemon name to uppercase after removing "ganeti-" prefix
225 local plain_name=${name#ganeti-}
226 local ucname=$(tr a-z A-Z <<<$plain_name)
227 local pidfile=$(_daemon_pidfile $name)
228 local usergroup=$(_daemon_usergroup $plain_name)
229 local daemonexec=$(_daemon_executable $name)
231 if [[ "$name" == ganeti-confd ]] && ! _confd_enabled; then
232 echo 'ganeti-confd disabled at build time' >&2
236 # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
237 eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
239 @PKGLIBDIR@/ensure-dirs
241 if type -p start-stop-daemon >/dev/null; then
242 start-stop-daemon --start --quiet --oknodo \
244 --startas $daemonexec \
248 # TODO: Find a way to start daemon with a group, until then the group must
250 _ignore_error daemon \
252 --user ${usergroup%:*} \
253 $daemonexec $args "$@"
260 if [[ "$#" -lt 1 ]]; then
261 echo 'Missing daemon name.' >&2
265 local name="$1"; shift
266 local pidfile=$(_daemon_pidfile $name)
268 if type -p start-stop-daemon >/dev/null; then
269 start-stop-daemon --stop --quiet --oknodo --retry 30 \
272 _ignore_error killproc -p $pidfile $name
276 # Starts a daemon if it's not yet running
280 if ! check $name; then
285 # Starts the master role
291 # Stops the master role
299 for i in $(list_start_daemons); do
302 # Try to start daemon
305 if ! errmsg=$(check_exitcode $rc); then
316 for i in $(list_stop_daemons); do
321 # Reloads the SSH keys
323 @RPL_SSH_INITD_SCRIPT@ restart
326 # Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
327 if ! type -p start-stop-daemon >/dev/null && \
328 [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
329 _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
332 if [[ "$#" -lt 1 ]]; then
333 echo "Usage: $0 <action>" >&2
337 orig_action=$1; shift
339 if [[ "$orig_action" == *_* ]]; then
340 echo "Command must not contain underscores" >&2
344 # Replace all dashes (-) with underlines (_)
345 action=${orig_action//-/_}
347 # Is it a known function?
348 if ! declare -F "$action" >/dev/null 2>&1; then
349 echo "Unknown command: $orig_action" >&2
353 # Call handler function