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 local pidfile=$(_daemon_pidfile $name)
207 local usergroup=$(_daemon_usergroup $plain_name)
208 local daemonexec=$(_daemon_executable $name)
210 if [[ "$name" == ganeti-confd &&
211 "@CUSTOM_ENABLE_CONFD@" == False ]]; then
212 echo 'ganeti-confd disabled at build time' >&2
216 # Convert daemon name to uppercase after removing "ganeti-" prefix
217 local plain_name=${name#ganeti-}
218 local ucname=$(tr a-z A-Z <<<$plain_name)
220 # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
221 eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
223 @PKGLIBDIR@/ensure-dirs
225 if type -p start-stop-daemon >/dev/null; then
226 start-stop-daemon --start --quiet --oknodo \
228 --startas $daemonexec \
232 # TODO: Find a way to start daemon with a group, until then the group must
234 _ignore_error daemon \
236 --user ${usergroup%:*} \
237 $daemonexec $args "$@"
243 if [[ "$#" -lt 1 ]]; then
244 echo 'Missing daemon name.' >&2
248 local name="$1"; shift
249 local pidfile=$(_daemon_pidfile $name)
251 if type -p start-stop-daemon >/dev/null; then
252 start-stop-daemon --stop --quiet --oknodo --retry 30 \
255 _ignore_error killproc -p $pidfile $name
259 # Starts a daemon if it's not yet running
263 if ! check $name; then
268 # Starts the master role
274 # Stops the master role
282 for i in $(list_start_daemons); do
285 # Try to start daemon
288 if ! errmsg=$(check_exitcode $rc); then
299 for i in $(list_stop_daemons); do
304 # Reloads the SSH keys
306 @RPL_SSH_INITD_SCRIPT@ restart
309 # Read @SYSCONFDIR@/rc.d/init.d/functions if start-stop-daemon not available
310 if ! type -p start-stop-daemon >/dev/null && \
311 [[ -f @SYSCONFDIR@/rc.d/init.d/functions ]]; then
312 _ignore_error . @SYSCONFDIR@/rc.d/init.d/functions
315 if [[ "$#" -lt 1 ]]; then
316 echo "Usage: $0 <action>" >&2
320 orig_action=$1; shift
322 if [[ "$orig_action" == *_* ]]; then
323 echo "Command must not contain underscores" >&2
327 # Replace all dashes (-) with underlines (_)
328 action=${orig_action//-/_}
330 # Is it a known function?
331 if ! declare -F "$action" >/dev/null 2>&1; then
332 echo "Unknown command: $orig_action" >&2
336 # Call handler function