ganeti.initd: Add “status” action
authorMichael Hanselmann <hansmi@google.com>
Wed, 28 Mar 2012 17:00:49 +0000 (19:00 +0200)
committerMichael Hanselmann <hansmi@google.com>
Wed, 28 Mar 2012 17:24:54 +0000 (19:24 +0200)
Eric Rostetter sent a patch adding a “status” action, but unfortunately
his code was apparently specific to Red Hat. I hope this implementation
is more distribution-agnostic; after all “status_of_proc” is part of
LSB. Example output:

$ /etc/init.d/ganeti status
ganeti-noded is not running ... failed!
ganeti-masterd is running.
ganeti-rapi is running.
ganeti-confd is running.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

daemons/daemon-util.in
doc/examples/ganeti.initd.in

index 819fd6b..789487d 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 
-# Copyright (C) 2009 Google Inc.
+# Copyright (C) 2009, 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
@@ -108,6 +108,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
index 94be6da..cba7fb9 100644 (file)
@@ -75,6 +75,30 @@ stop_all() {
     done
 }
 
+status_all() {
+    local daemons="$1" status ret
+
+    if [ -z "$daemons" ]; then
+      daemons=$($DAEMON_UTIL list-start-daemons)
+    fi
+
+    status=0
+
+    for i in $daemons; do
+      if status_of_proc $($DAEMON_UTIL daemon-executable $i) $i; then
+          ret=0
+      else
+          ret=$?
+          # Use exit code from first failed call
+          if [ "$status" -eq 0 ]; then
+              status=$ret
+          fi
+      fi
+    done
+
+    exit $status
+}
+
 if [ -n "$2" ] && ! errmsg=$($DAEMON_UTIL is-daemon-name "$2" 2>&1); then
     log_failure_msg "$errmsg"
     exit 1
@@ -94,6 +118,9 @@ case "$1" in
         stop_all "$2"
         start_all "$2"
         ;;
+    status)
+        status_all "$2"
+        ;;
     *)
         log_success_msg "Usage: $SCRIPTNAME {start|stop|force-reload|restart}"
         exit 1