Fix burnin problems when using http checks
[ganeti-local] / daemons / ganeti-masterd
index d50909d..ac0af6d 100755 (executable)
@@ -27,6 +27,8 @@ inheritance from parent classes requires it.
 """
 
 
+import os
+import errno
 import sys
 import SocketServer
 import time
@@ -87,9 +89,8 @@ class IOServer(SocketServer.UnixStreamServer):
   def __init__(self, address, rqhandler):
     """IOServer constructor
 
-    Args:
-      address: the address to bind this IOServer to
-      rqhandler: RequestHandler type object
+    @param address: the address to bind this IOServer to
+    @param rqhandler: RequestHandler type object
 
     """
     SocketServer.UnixStreamServer.__init__(self, address, rqhandler)
@@ -220,8 +221,8 @@ class ClientOps:
       return queue.ArchiveJob(job_id)
 
     elif method == luxi.REQ_AUTOARCHIVE_JOBS:
-      age = args
-      return queue.AutoArchiveJobs(age)
+      (age, timeout) = args
+      return queue.AutoArchiveJobs(age, timeout)
 
     elif method == luxi.REQ_WAIT_FOR_JOB_CHANGE:
       (job_id, fields, prev_job_info, prev_log_serial, timeout) = args
@@ -346,8 +347,7 @@ class GanetiContext(object):
 def ParseOptions():
   """Parse the command line options.
 
-  Returns:
-    (options, args) as from OptionParser.parse_args()
+  @return: (options, args) as from OptionParser.parse_args()
 
   """
   parser = OptionParser(description="Ganeti master daemon",
@@ -402,16 +402,17 @@ def CheckAgreement():
       continue
     break
   if retries == 0:
-      logging.critical("Cluster inconsistent, most of the nodes didn't answer"
-                       " after multiple retries. Aborting startup")
-      return False
+    logging.critical("Cluster inconsistent, most of the nodes didn't answer"
+                     " after multiple retries. Aborting startup")
+    return False
   # here a real node is at the top of the list
   all_votes = sum(item[1] for item in votes)
   top_node, top_votes = votes[0]
   result = False
   if top_node != myself:
     logging.critical("It seems we are not the master (top-voted node"
-                     " is %s)", top_node)
+                     " is %s with %d out of %d votes)", top_node, top_votes,
+                     all_votes)
   elif top_votes < all_votes - top_votes:
     logging.critical("It seems we are not the master (%d votes for,"
                      " %d votes against)", top_votes, all_votes - top_votes)
@@ -428,37 +429,66 @@ def main():
   utils.debug = options.debug
   utils.no_fork = True
 
-  ssconf.CheckMaster(options.debug)
+  if options.fork:
+    utils.CloseFDs()
+
+  rpc.Init()
+  try:
+    ssconf.CheckMaster(options.debug)
 
-  # we believe we are the master, let's ask the other nodes...
-  if not CheckAgreement():
-    return
+    # we believe we are the master, let's ask the other nodes...
+    if not CheckAgreement():
+      return
 
-  master = IOServer(constants.MASTER_SOCKET, ClientRqHandler)
+    dirs = [(constants.RUN_GANETI_DIR, constants.RUN_DIRS_MODE),
+            (constants.SOCKET_DIR, constants.SOCKET_DIR_MODE),
+           ]
+    for dir, mode in dirs:
+      try:
+        os.mkdir(dir, mode)
+      except EnvironmentError, err:
+        if err.errno != errno.EEXIST:
+          raise errors.GenericError("Cannot create needed directory"
+            " '%s': %s" % (constants.SOCKET_DIR, err))
+      if not os.path.isdir(dir):
+        raise errors.GenericError("%s is not a directory" % dir)
+
+    # This is safe to do as the pid file guarantees against
+    # concurrent execution.
+    utils.RemoveFile(constants.MASTER_SOCKET)
+
+    master = IOServer(constants.MASTER_SOCKET, ClientRqHandler)
+  finally:
+    rpc.Shutdown()
 
   # become a daemon
   if options.fork:
-    utils.Daemonize(logfile=constants.LOG_MASTERDAEMON,
-                    noclose_fds=[master.fileno()])
+    utils.Daemonize(logfile=constants.LOG_MASTERDAEMON)
 
   utils.WritePidFile(constants.MASTERD_PID)
+  try:
+    utils.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug,
+                       stderr_logging=not options.fork)
 
-  utils.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug,
-                     stderr_logging=not options.fork)
-
-  logging.info("ganeti master daemon startup")
+    logging.info("Ganeti master daemon startup")
 
-  # activate ip
-  master_node = ssconf.SimpleConfigReader().GetMasterNode()
-  if not rpc.RpcRunner.call_node_start_master(master_node, False):
-    logging.error("Can't activate master IP address")
+    rpc.Init()
+    try:
+      # activate ip
+      master_node = ssconf.SimpleConfigReader().GetMasterNode()
+      if not rpc.RpcRunner.call_node_start_master(master_node, False):
+        logging.error("Can't activate master IP address")
 
-  master.setup_queue()
-  try:
-    master.serve_forever()
+      master.setup_queue()
+      try:
+        master.serve_forever()
+      finally:
+        master.server_cleanup()
+    finally:
+      rpc.Shutdown()
   finally:
-    master.server_cleanup()
     utils.RemovePidFile(constants.MASTERD_PID)
+    utils.RemoveFile(constants.MASTER_SOCKET)
 
 
 if __name__ == "__main__":