From 5675cd1f3c5d848e8f9257f06f86d2d2121c6c1a Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 30 Jul 2008 08:33:49 +0000
Subject: [PATCH] Implement checking for the master role in rapi

This patch moves the CheckMaster function from ganeti-masterd to ssconf
(most logical place, it cannot go in utils since we would have recursive
imports between ssconf and utils) and changes ganeti-rapi to also call
this function.

This is needed so that starting ganeti-rapi on a non-master node does
the right thing.

Reviewed-by: ultrotter
---
 daemons/ganeti-masterd | 28 +---------------------------
 daemons/ganeti-rapi    |  3 +++
 lib/ssconf.py          | 26 ++++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/daemons/ganeti-masterd b/daemons/ganeti-masterd
index 9b866b083..cbccdce51 100755
--- a/daemons/ganeti-masterd
+++ b/daemons/ganeti-masterd
@@ -265,32 +265,6 @@ class GanetiContext(object):
     object.__setattr__(self, name, value)
 
 
-def CheckMaster(debug):
-  """Checks the node setup.
-
-  If this is the master, the function will return. Otherwise it will
-  exit with an exit code based on the node status.
-
-  """
-  try:
-    ss = ssconf.SimpleStore()
-    master_name = ss.GetMasterNode()
-  except errors.ConfigurationError, err:
-    print "Cluster configuration incomplete: '%s'" % str(err)
-    sys.exit(EXIT_NODESETUP_ERROR)
-
-  try:
-    myself = utils.HostInfo()
-  except errors.ResolverError, err:
-    sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
-    sys.exit(EXIT_NODESETUP_ERROR)
-
-  if myself.name != master_name:
-    if debug:
-      sys.stderr.write("Not master, exiting.\n")
-    sys.exit(EXIT_NOTMASTER)
-
-
 def ParseOptions():
   """Parse the command line options.
 
@@ -320,7 +294,7 @@ def main():
   utils.debug = options.debug
   utils.no_fork = True
 
-  CheckMaster(options.debug)
+  ssconf.CheckMaster(options.debug)
 
   master = IOServer(constants.MASTER_SOCKET, ClientRqHandler, GanetiContext())
 
diff --git a/daemons/ganeti-rapi b/daemons/ganeti-rapi
index 1d0795c90..c8e8953d3 100755
--- a/daemons/ganeti-rapi
+++ b/daemons/ganeti-rapi
@@ -31,6 +31,7 @@ from ganeti import constants
 from ganeti import errors
 from ganeti import http
 from ganeti import rpc
+from ganeti import ssconf
 from ganeti import utils
 from ganeti.rapi import connector
 
@@ -129,6 +130,8 @@ def main():
   """
   options, args = ParseOptions()
 
+  ssconf.CheckMaster(options.debug)
+
   if options.fork:
     utils.Daemonize(logfile=constants.LOG_RAPISERVER)
 
diff --git a/lib/ssconf.py b/lib/ssconf.py
index d73a267ac..a18a55648 100644
--- a/lib/ssconf.py
+++ b/lib/ssconf.py
@@ -27,6 +27,7 @@ configuration data, which is mostly static and available to all nodes.
 """
 
 import socket
+import sys
 
 from ganeti import errors
 from ganeti import constants
@@ -199,3 +200,28 @@ class WritableSimpleStore(SimpleStore):
     utils.WriteFile(file_name, data="%s\n" % str(value),
                     uid=0, gid=0, mode=0400)
 
+
+def CheckMaster(debug):
+  """Checks the node setup.
+
+  If this is the master, the function will return. Otherwise it will
+  exit with an exit code based on the node status.
+
+  """
+  try:
+    ss = SimpleStore()
+    master_name = ss.GetMasterNode()
+  except errors.ConfigurationError, err:
+    print "Cluster configuration incomplete: '%s'" % str(err)
+    sys.exit(constants.EXIT_NODESETUP_ERROR)
+
+  try:
+    myself = utils.HostInfo()
+  except errors.ResolverError, err:
+    sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
+    sys.exit(constants.EXIT_NODESETUP_ERROR)
+
+  if myself.name != master_name:
+    if debug:
+      sys.stderr.write("Not master, exiting.\n")
+    sys.exit(constants.EXIT_NOTMASTER)
-- 
GitLab