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 )
49 # Read defaults file if it exists
50 if [[ -s $defaults_file ]]; then
54 # Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case
55 # start-stop-daemon is not available.
61 echo "$RUN_DIR/$1.pid"
64 _daemon_executable() {
65 echo "@PREFIX@/sbin/$1"
71 echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
74 echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
77 echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
80 echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
83 echo "root:@GNTDAEMONSGROUP@"
88 # Checks whether the local machine is part of a cluster
90 local server_pem=$DATA_DIR/server.pem
93 for fname in $server_pem; do
94 if [[ ! -f $fname ]]; then
95 echo "Missing configuration file $fname" >&2
103 # Checks the exit code of a daemon
105 if [[ "$#" -lt 1 ]]; then
106 echo 'Missing exit code.' >&2
126 # Prints path to PID file for a daemon.
128 if [[ "$#" -lt 1 ]]; then
129 echo 'Missing daemon name.' >&2
133 local name="$1"; shift
135 _daemon_pidfile $name
138 # Prints path to daemon executable.
139 daemon_executable() {
140 if [[ "$#" -lt 1 ]]; then
141 echo 'Missing daemon name.' >&2
145 local name="$1"; shift
147 _daemon_executable $name
150 # Prints a list of all daemons in the order in which they should be started
151 list_start_daemons() {
154 for name in "${DAEMONS[@]}"; do
159 # Prints a list of all daemons in the order in which they should be stopped
160 list_stop_daemons() {
161 list_start_daemons | tac
164 # Checks whether a daemon name is known
166 if [[ "$#" -lt 1 ]]; then
167 echo 'Missing daemon name.' >&2
171 local name="$1"; shift
173 for i in "${DAEMONS[@]}"; do
174 if [[ "$i" == "$name" ]]; then
179 echo "Unknown daemon name '$name'" >&2
183 # Checks whether daemon is running
185 if [[ "$#" -lt 1 ]]; then
186 echo 'Missing daemon name.' >&2
190 local name="$1"; shift
191 local pidfile=$(_daemon_pidfile $name)
192 local daemonexec=$(_daemon_executable $name)
194 if type -p start-stop-daemon >/dev/null; then
195 start-stop-daemon --stop --signal 0 --quiet \
198 _ignore_error status \
206 if [[ "$#" -lt 1 ]]; then
207 echo 'Missing daemon name.' >&2
211 local name="$1"; shift
212 # Convert daemon name to uppercase after removing "ganeti-" prefix
213 local plain_name=${name#ganeti-}
214 local ucname=$(tr a-z A-Z <<<$plain_name)
215 local pidfile=$(_daemon_pidfile $name)
216 local usergroup=$(_daemon_usergroup $plain_name)
217 local daemonexec=$(_daemon_executable $name)
219 if [[ "$name" == ganeti-confd ]] && ! _confd_enabled; then
220 echo 'ganeti-confd disabled at build time' >&2
224 # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
225 eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
227 @PKGLIBDIR@/ensure-dirs
229 if type -p start-stop-daemon >/dev/null; then
230 start-stop-daemon --start --quiet --oknodo \
232 --startas $daemonexec \
236 # TODO: Find a way to start daemon with a group, until then the group must
238 _ignore_error daemon \
240 --user ${usergroup%:*} \
241 $daemonexec $args "$@"
244 # FIXME: This is a workaround for issue 477. Remove this once confd does not
245 # mess up the permissions anymore.
246 if [[ "$name" == ganeti-confd ]]; then
247 @PKGLIBDIR@/ensure-dirs;
253 if [[ "$#" -lt 1 ]]; then
254 echo 'Missing daemon name.' >&2
258 local name="$1"; shift
259 local pidfile=$(_daemon_pidfile $name)
261 if type -p start-stop-daemon >/dev/null; then
262 start-stop-daemon --stop --quiet --oknodo --retry 30 \
265 _ignore_error killproc -p $pidfile $name
269 # Starts a daemon if it's not yet running
273 if ! check $name; then
278 # Starts the master role
284 # Stops the master role
292 for i in $(list_start_daemons); do
295 # Try to start daemon
298 if ! errmsg=$(check_exitcode $rc); then
309 for i in $(list_stop_daemons); do
314 # SIGHUP a process to force re-opening its logfiles
316 if [[ "$#" -lt 1 ]]; then
317 echo 'Missing daemon name.' >&2
321 local name="$1"; shift
322 local pidfile=$(_daemon_pidfile $name)
323 local daemonexec=$(_daemon_executable $name)
325 if type -p start-stop-daemon >/dev/null; then
326 start-stop-daemon --stop --signal HUP --quiet \
329 _ignore_error killproc \
335 # SIGHUP all processes
337 for i in $(list_stop_daemons); do
342 # Reloads the SSH keys
344 @RPL_SSH_INITD_SCRIPT@ restart
347 # Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
348 if ! type -p start-stop-daemon >/dev/null && \
349 [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
350 _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
353 if [[ "$#" -lt 1 ]]; then
354 echo "Usage: $0 <action>" >&2
358 orig_action=$1; shift
360 if [[ "$orig_action" == *_* ]]; then
361 echo "Command must not contain underscores" >&2
365 # Replace all dashes (-) with underlines (_)
366 action=${orig_action//-/_}
368 # Is it a known function?
369 if ! declare -F "$action" >/dev/null 2>&1; then
370 echo "Unknown command: $orig_action" >&2
374 # Call handler function