From 5d8acbdd9064fc61f501167acef91462faf9d916 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 11 Mar 2010 16:52:17 +0100
Subject: [PATCH] ganeti.initd: Move all daemon names from init script to
 daemon-util
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The list of daemon names will be used in daemon-util, too.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 daemons/daemon-util.in       | 43 ++++++++++++++++++++++++++++++++++++
 doc/examples/ganeti.initd.in | 19 +++++-----------
 2 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/daemons/daemon-util.in b/daemons/daemon-util.in
index 05e97b47e..ea2bd5f41 100755
--- a/daemons/daemon-util.in
+++ b/daemons/daemon-util.in
@@ -22,6 +22,16 @@ set -e
 
 defaults_file=@SYSCONFDIR@/default/ganeti
 
+# This is a list of all daemons and the order in which they're started. The
+# order is important as there are dependencies between them. On shutdown,
+# they're stopped in reverse order.
+DAEMONS=(
+  ganeti-noded
+  ganeti-masterd
+  ganeti-rapi
+  ganeti-confd
+  )
+
 NODED_ARGS=
 MASTERD_ARGS=
 CONFD_ARGS=
@@ -74,6 +84,39 @@ check_exitcode() {
   return 0
 }
 
+# Prints a list of all daemons in the order in which they should be started
+list_start_daemons() {
+  local name
+
+  for name in "${DAEMONS[@]}"; do
+    echo "$name"
+  done
+}
+
+# Prints a list of all daemons in the order in which they should be stopped
+list_stop_daemons() {
+  list_start_daemons | tac
+}
+
+# Checks whether a daemon name is known
+is_daemon_name() {
+  if [[ "$#" -lt 1 ]]; then
+    echo 'Missing daemon name.' >&2
+    return 1
+  fi
+
+  local name="$1"; shift
+
+  for i in "${DAEMONS[@]}"; do
+    if [[ "$i" == "$name" ]]; then
+      return 0
+    fi
+  done
+
+  echo "Unknown daemon name '$name'" >&2
+  return 1
+}
+
 # Checks whether daemon is running
 check() {
   if [[ "$#" -lt 1 ]]; then
diff --git a/doc/examples/ganeti.initd.in b/doc/examples/ganeti.initd.in
index 617d06df9..cd137bdf7 100644
--- a/doc/examples/ganeti.initd.in
+++ b/doc/examples/ganeti.initd.in
@@ -14,16 +14,11 @@
 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
 DESC="Ganeti cluster"
 
-NODED="ganeti-noded"
-MASTERD="ganeti-masterd"
-CONFD="ganeti-confd"
-RAPI="ganeti-rapi"
-
 DAEMON_UTIL=@PKGLIBDIR@/daemon-util
 
 SCRIPTNAME="@SYSCONFDIR@/init.d/ganeti"
 
-test -f "@PREFIX@/sbin/$NODED" || exit 0
+test -f "$DAEMON_UTIL" || exit 0
 
 . /lib/lsb/init-functions
 
@@ -69,23 +64,19 @@ start_all() {
         exit 0
     fi
 
-    for i in $NODED $MASTERD $CONFD $RAPI; do \
+    for i in $($DAEMON_UTIL list-start-daemons); do
         maybe_do "$1" start_action $i
     done
 }
 
 stop_all() {
-    for i in $RAPI $CONFD $MASTERD $NODED; do \
+    for i in $($DAEMON_UTIL list-stop-daemons); do
         maybe_do "$1" stop_action $i
     done
 }
 
-if [ -n "$2" -a \
-    "$2" != "$NODED" -a \
-    "$2" != "$CONFD" -a \
-    "$2" != "$MASTERD" -a \
-    "$2" != "$RAPI" ]; then
-    log_failure_msg "Unknown daemon '$2' requested"
+if [ -n "$2" ] && ! errmsg=$($DAEMON_UTIL is-daemon-name "$2" 2>&1); then
+    log_failure_msg "$errmsg"
     exit 1
 fi
 
-- 
GitLab