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
23 defaults_file=@SYSCONFDIR@/default/ganeti
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.
34 if [[ "@CUSTOM_ENABLE_CONFD@" == True ]]; then
35 DAEMONS+=( ganeti-confd )
43 # Read defaults file if it exists
44 if [[ -s $defaults_file ]]; then
48 # Meant to facilitate use utilities in /etc/rc.d/init.d/functions in case
49 # start-stop-daemon is not available.
55 echo "@LOCALSTATEDIR@/run/ganeti/$1.pid"
58 _daemon_executable() {
59 echo "@PREFIX@/sbin/$1"
65 echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
68 echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
71 echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
74 echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
77 echo "root:@GNTDAEMONSGROUP@"
82 # Checks whether the local machine is part of a cluster
84 local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem
87 for fname in $server_pem; do
88 if [[ ! -f $fname ]]; then
89 echo "Missing configuration file $fname" >&2
97 # Checks the exit code of a daemon
99 if [[ "$#" -lt 1 ]]; then
100 echo 'Missing exit code.' >&2
120 # Prints path to PID file for a daemon.
122 if [[ "$#" -lt 1 ]]; then
123 echo 'Missing daemon name.' >&2
127 local name="$1"; shift
129 _daemon_pidfile $name
132 # Prints path to daemon executable.
133 daemon_executable() {
134 if [[ "$#" -lt 1 ]]; then
135 echo 'Missing daemon name.' >&2
139 local name="$1"; shift
141 _daemon_executable $name
144 # Prints a list of all daemons in the order in which they should be started
145 list_start_daemons() {
148 for name in "${DAEMONS[@]}"; do
153 # Prints a list of all daemons in the order in which they should be stopped
154 list_stop_daemons() {
155 list_start_daemons | tac
158 # Checks whether a daemon name is known
160 if [[ "$#" -lt 1 ]]; then
161 echo 'Missing daemon name.' >&2
165 local name="$1"; shift
167 for i in "${DAEMONS[@]}"; do
168 if [[ "$i" == "$name" ]]; then
173 echo "Unknown daemon name '$name'" >&2
177 # Checks whether daemon is running
179 if [[ "$#" -lt 1 ]]; then
180 echo 'Missing daemon name.' >&2
184 local name="$1"; shift
185 local pidfile=$(_daemon_pidfile $name)
186 local daemonexec=$(_daemon_executable $name)
188 if type -p start-stop-daemon >/dev/null; then
189 start-stop-daemon --stop --signal 0 --quiet \
192 _ignore_error status \
200 if [[ "$#" -lt 1 ]]; then
201 echo 'Missing daemon name.' >&2
205 local name="$1"; shift
206 # Convert daemon name to uppercase after removing "ganeti-" prefix
207 local plain_name=${name#ganeti-}
208 local ucname=$(tr a-z A-Z <<<$plain_name)
209 local pidfile=$(_daemon_pidfile $name)
210 local usergroup=$(_daemon_usergroup $plain_name)
211 local daemonexec=$(_daemon_executable $name)
213 if [[ "$name" == ganeti-confd &&
214 "@CUSTOM_ENABLE_CONFD@" == False ]]; then
215 echo 'ganeti-confd disabled at build time' >&2
219 # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
220 eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
222 @PKGLIBDIR@/ensure-dirs
224 if type -p start-stop-daemon >/dev/null; then
225 start-stop-daemon --start --quiet --oknodo \
227 --startas $daemonexec \
231 # TODO: Find a way to start daemon with a group, until then the group must
233 _ignore_error daemon \
235 --user ${usergroup%:*} \
236 $daemonexec $args "$@"
242 if [[ "$#" -lt 1 ]]; then
243 echo 'Missing daemon name.' >&2
247 local name="$1"; shift
248 local pidfile=$(_daemon_pidfile $name)
250 if type -p start-stop-daemon >/dev/null; then
251 start-stop-daemon --stop --quiet --oknodo --retry 30 \
254 _ignore_error killproc -p $pidfile $name
258 # Starts a daemon if it's not yet running
262 if ! check $name; then
267 # Starts the master role
273 # Stops the master role
281 for i in $(list_start_daemons); do
284 # Try to start daemon
287 if ! errmsg=$(check_exitcode $rc); then
298 for i in $(list_stop_daemons); do
303 # Reloads the SSH keys
305 @RPL_SSH_INITD_SCRIPT@ restart
308 # Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
309 if ! type -p start-stop-daemon >/dev/null && \
310 [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
311 _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
314 if [[ "$#" -lt 1 ]]; then
315 echo "Usage: $0 <action>" >&2
319 orig_action=$1; shift
321 if [[ "$orig_action" == *_* ]]; then
322 echo "Command must not contain underscores" >&2
326 # Replace all dashes (-) with underlines (_)
327 action=${orig_action//-/_}
329 # Is it a known function?
330 if ! declare -F "$action" >/dev/null 2>&1; then
331 echo "Unknown command: $orig_action" >&2
335 # Call handler function