From 4404ffad6d6cead8dcb46f9aee6f38bbc9aaa7ca Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 29 Jul 2010 17:41:24 -0400
Subject: [PATCH] Implement gnt-cluster master-ping

This can be used from shell-scripts to quickly check the status of the
master node, before launching a series of jobs (and handling the failure
of the jobs due to masterd other issues).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 man/gnt-cluster.sgml | 21 +++++++++++++++++++++
 scripts/gnt-cluster  | 21 +++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/man/gnt-cluster.sgml b/man/gnt-cluster.sgml
index da3e5473e..c58e1aef5 100644
--- a/man/gnt-cluster.sgml
+++ b/man/gnt-cluster.sgml
@@ -590,6 +590,27 @@
 
     </refsect2>
 
+    <refsect2>
+      <title>MASTER-PING</title>
+
+      <cmdsynopsis>
+        <command>master-ping</command>
+      </cmdsynopsis>
+
+      <para>
+        Checks if the master daemon is alive.
+      </para>
+
+      <para>
+        If the master daemon is alive and can respond to a basic query
+        (the equivalent of <command>gnt-cluster info</command>), then
+        the exit code of the command will be 0. If the master daemon
+        is not alive (either due to a crash or because this is not the
+        master node), the exit code will be 1.
+      </para>
+
+    </refsect2>
+
     <refsect2>
       <title>MODIFY</title>
 
diff --git a/scripts/gnt-cluster b/scripts/gnt-cluster
index 5a2b6c563..efd49eaa4 100755
--- a/scripts/gnt-cluster
+++ b/scripts/gnt-cluster
@@ -517,6 +517,24 @@ def MasterFailover(opts, args):
   return bootstrap.MasterFailover(no_voting=opts.no_voting)
 
 
+def MasterPing(opts, args):
+  """Checks if the master is alive.
+
+  @param opts: the command line options selected by the user
+  @type args: list
+  @param args: should be an empty list
+  @rtype: int
+  @return: the desired exit code
+
+  """
+  try:
+    cl = GetClient()
+    cl.QueryClusterInfo()
+    return 0
+  except Exception: # pylint: disable-msg=W0703
+    return 1
+
+
 def SearchTags(opts, args):
   """Searches the tags on all the cluster.
 
@@ -857,6 +875,9 @@ commands = {
   'master-failover': (
     MasterFailover, ARGS_NONE, [NOVOTING_OPT],
     "", "Makes the current node the master"),
+  'master-ping': (
+    MasterPing, ARGS_NONE, [],
+    "", "Checks if the master is alive"),
   'version': (
     ShowClusterVersion, ARGS_NONE, [],
     "", "Shows the cluster version"),
-- 
GitLab