From 3f71b464ad5cdd1f1b53f2a31a4eef4e2a5550cc Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Tue, 14 Jul 2009 12:35:04 +0200
Subject: [PATCH] ssconf.CheckMasterCandidate

This function checks that the current node is a master candidate, and
terminates otherwise. It will be used upon ganeti-confd startup.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/constants.py |  1 +
 lib/ssconf.py    | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/lib/constants.py b/lib/constants.py
index 38a329119..91ce86ba9 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -279,6 +279,7 @@ EXIT_NOTCLUSTER = 5
 EXIT_NOTMASTER = 11
 EXIT_NODESETUP_ERROR = 12
 EXIT_CONFIRMATION = 13 # need user confirmation
+EXIT_NOTCANDIDATE = 14
 
 # tags
 TAG_CLUSTER = "cluster"
diff --git a/lib/ssconf.py b/lib/ssconf.py
index 874b208bf..8ee84c112 100644
--- a/lib/ssconf.py
+++ b/lib/ssconf.py
@@ -290,3 +290,29 @@ def CheckMaster(debug, ss=None):
     if debug:
       sys.stderr.write("Not master, exiting.\n")
     sys.exit(constants.EXIT_NOTMASTER)
+
+
+def CheckMasterCandidate(debug, ss=None):
+  """Checks the node setup.
+
+  If this is a master candidate, the function will return. Otherwise it will
+  exit with an exit code based on the node status.
+
+  """
+  try:
+    if ss is None:
+      ss = SimpleStore()
+    myself = utils.HostInfo().name
+    candidates = ss.GetMasterCandidates()
+  except errors.ConfigurationError, err:
+    print "Cluster configuration incomplete: '%s'" % str(err)
+    sys.exit(constants.EXIT_NODESETUP_ERROR)
+  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 not in candidates:
+    if debug:
+      sys.stderr.write("Not master candidate, exiting.\n")
+    sys.exit(constants.EXIT_NOTCANDIDATE)
+
-- 
GitLab