Merge branch 'devel-2.5'
[ganeti-local] / daemons / daemon-util.in
old mode 100755 (executable)
new mode 100644 (file)
index ea2bd5f..03decbf
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 
-# Copyright (C) 2009 Google Inc.
+# Copyright (C) 2009, 2011, 2012 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -29,9 +29,12 @@ DAEMONS=(
   ganeti-noded
   ganeti-masterd
   ganeti-rapi
-  ganeti-confd
   )
 
+if [[ "@CUSTOM_ENABLE_CONFD@" == True ]]; then
+  DAEMONS+=( ganeti-confd )
+fi
+
 NODED_ARGS=
 MASTERD_ARGS=
 CONFD_ARGS=
@@ -46,6 +49,30 @@ _daemon_pidfile() {
   echo "@LOCALSTATEDIR@/run/ganeti/$1.pid"
 }
 
+_daemon_executable() {
+  echo "@PREFIX@/sbin/$1"
+}
+
+_daemon_usergroup() {
+  case "$1" in
+    masterd)
+      echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
+      ;;
+    confd)
+      echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
+      ;;
+    rapi)
+      echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
+      ;;
+    noded)
+      echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
+      ;;
+    *)
+      echo "root:@GNTDAEMONSGROUP@"
+      ;;
+  esac
+}
+
 # Checks whether the local machine is part of a cluster
 check_config() {
   local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem
@@ -84,6 +111,30 @@ check_exitcode() {
   return 0
 }
 
+# Prints path to PID file for a daemon.
+daemon_pidfile() {
+  if [[ "$#" -lt 1 ]]; then
+    echo 'Missing daemon name.' >&2
+    return 1
+  fi
+
+  local name="$1"; shift
+
+  _daemon_pidfile $name
+}
+
+# Prints path to daemon executable.
+daemon_executable() {
+  if [[ "$#" -lt 1 ]]; then
+    echo 'Missing daemon name.' >&2
+    return 1
+  fi
+
+  local name="$1"; shift
+
+  _daemon_executable $name
+}
+
 # Prints a list of all daemons in the order in which they should be started
 list_start_daemons() {
   local name
@@ -121,7 +172,7 @@ is_daemon_name() {
 check() {
   if [[ "$#" -lt 1 ]]; then
     echo 'Missing daemon name.' >&2
-    exit 1
+    return 1
   fi
 
   local name="$1"; shift
@@ -134,20 +185,30 @@ check() {
 start() {
   if [[ "$#" -lt 1 ]]; then
     echo 'Missing daemon name.' >&2
-    exit 1
+    return 1
   fi
 
   local name="$1"; shift
 
+  if [[ "$name" == ganeti-confd &&
+        "@CUSTOM_ENABLE_CONFD@" == False ]]; then
+    echo 'ganeti-confd disabled at build time' >&2
+    return 1
+  fi
+
   # Convert daemon name to uppercase after removing "ganeti-" prefix
-  local ucname=$(echo ${name#ganeti-} | tr a-z A-Z)
+  local plain_name=${name#ganeti-}
+  local ucname=$(tr a-z A-Z <<<$plain_name)
 
   # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
   eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
 
+  @PKGLIBDIR@/ensure-dirs
+
   start-stop-daemon --start --quiet --oknodo \
     --pidfile $(_daemon_pidfile $name) \
-    --startas "@PREFIX@/sbin/$name" \
+    --startas $(_daemon_executable $name) \
+    --chuid $(_daemon_usergroup $plain_name) \
     -- $args "$@"
 }
 
@@ -155,7 +216,7 @@ start() {
 stop() {
   if [[ "$#" -lt 1 ]]; then
     echo 'Missing daemon name.' >&2
-    exit 1
+    return 1
   fi
 
   local name="$1"; shift
@@ -185,6 +246,30 @@ stop_master() {
   stop ganeti-masterd
 }
 
+# Start all daemons
+start_all() {
+  for i in $(list_start_daemons); do
+    local rc=0
+
+    # Try to start daemon
+    start $i || rc=$?
+
+    if ! errmsg=$(check_exitcode $rc); then
+      echo "$errmsg" >&2
+      return 1
+    fi
+  done
+
+  return 0
+}
+
+# Stop all daemons
+stop_all() {
+  for i in $(list_stop_daemons); do
+    stop $i
+  done
+}
+
 # Reloads the SSH keys
 reload_ssh_keys() {
   @RPL_SSH_INITD_SCRIPT@ restart