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 "$@"
247 if [[ "$#" -lt 1 ]]; then
248 echo 'Missing daemon name.' >&2
252 local name="$1"; shift
253 local pidfile=$(_daemon_pidfile $name)
255 if type -p start-stop-daemon >/dev/null; then
256 start-stop-daemon --stop --quiet --oknodo --retry 30 \
259 _ignore_error killproc -p $pidfile $name
263 # Starts a daemon if it's not yet running
267 if ! check $name; then
272 # Starts the master role
278 # Stops the master role
286 for i in $(list_start_daemons); do
289 # Try to start daemon
292 if ! errmsg=$(check_exitcode $rc); then
303 for i in $(list_stop_daemons); do
308 # Reloads the SSH keys
310 @RPL_SSH_INITD_SCRIPT@ restart
313 # Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
314 if ! type -p start-stop-daemon >/dev/null && \
315 [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
316 _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
319 if [[ "$#" -lt 1 ]]; then
320 echo "Usage: $0 <action>" >&2
324 orig_action=$1; shift
326 if [[ "$orig_action" == *_* ]]; then
327 echo "Command must not contain underscores" >&2
331 # Replace all dashes (-) with underlines (_)
332 action=${orig_action//-/_}
334 # Is it a known function?
335 if ! declare -F "$action" >/dev/null 2>&1; then
336 echo "Unknown command: $orig_action" >&2
340 # Call handler function