Forward-port DrbdNetReconfig
[ganeti-local] / daemons / ganeti-masterd
index c2623b9..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,6 +429,9 @@ def main():
   utils.debug = options.debug
   utils.no_fork = True
 
+  if options.fork:
+    utils.CloseFDs()
+
   rpc.Init()
   try:
     ssconf.CheckMaster(options.debug)
@@ -436,14 +440,30 @@ def main():
     if not CheckAgreement():
       return
 
+    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:
@@ -468,6 +488,7 @@ def main():
       rpc.Shutdown()
   finally:
     utils.RemovePidFile(constants.MASTERD_PID)
+    utils.RemoveFile(constants.MASTER_SOCKET)
 
 
 if __name__ == "__main__":