Export backend.GetMasterInfo over the rpc layer
[ganeti-local] / daemons / ganeti-noded
index 30f3b54..3de30d3 100755 (executable)
 
 import os
 import sys
-import resource
 import traceback
+import SocketServer
+import errno
+import logging
+import signal
 
 from optparse import OptionParser
 
-
 from ganeti import backend
 from ganeti import logger
 from ganeti import constants
 from ganeti import objects
 from ganeti import errors
+from ganeti import jstore
 from ganeti import ssconf
+from ganeti import http
 from ganeti import utils
 
-from twisted.spread import pb
-from twisted.internet import reactor
-from twisted.cred import checkers, portal
-from OpenSSL import SSL
 
+queue_lock = None
 
-class ServerContextFactory:
-  """SSL context factory class that uses a given certificate.
 
-  """
-  @staticmethod
-  def getContext():
-    """Return a customized context.
+def _RequireJobQueueLock(fn):
+  """Decorator for job queue manipulating functions.
 
-    The context will be set to use our certificate.
+  """
+  def wrapper(*args, **kwargs):
+    # Locking in exclusive, blocking mode because there could be several
+    # children running at the same time. Waiting up to 10 seconds.
+    queue_lock.Exclusive(blocking=True, timeout=10)
+    try:
+      return fn(*args, **kwargs)
+    finally:
+      queue_lock.Unlock()
+  return wrapper
 
-    """
-    ctx = SSL.Context(SSL.TLSv1_METHOD)
-    ctx.use_certificate_file(constants.SSL_CERT_FILE)
-    ctx.use_privatekey_file(constants.SSL_CERT_FILE)
-    return ctx
 
-class ServerObject(pb.Avatar):
+class NodeDaemonRequestHandler(http.HTTPRequestHandler):
   """The server implementation.
 
   This class holds all methods exposed over the RPC interface.
 
   """
-  def __init__(self, name):
-    self.name = name
+  def HandleRequest(self):
+    """Handle a request.
 
-  def perspectiveMessageReceived(self, broker, message, args, kw):
-    """Custom message dispatching function.
+    """
+    if self.command.upper() != "PUT":
+      raise http.HTTPBadRequest()
 
-    This function overrides the pb.Avatar function in order to provide
-    a simple form of exception passing (as text only).
+    path = self.path
+    if path.startswith("/"):
+      path = path[1:]
 
-    """
-    args = broker.unserialize(args, self)
-    kw = broker.unserialize(kw, self)
-    method = getattr(self, "perspective_%s" % message)
-    tb = None
-    state = None
-    try:
-      state = method(*args, **kw)
-    except:
-      tb = traceback.format_exc()
+    method = getattr(self, "perspective_%s" % path, None)
+    if method is None:
+      raise httperror.HTTPNotFound()
 
-    return broker.serialize((tb, state), self, method, args, kw)
+    try:
+      try:
+        return method(self.post_data)
+      except:
+        logging.exception("Error in RPC call")
+        raise
+    except errors.QuitGanetiException, err:
+      # Tell parent to quit
+      os.kill(self.server.noded_pid, signal.SIGTERM)
 
   # the new block devices  --------------------------
 
@@ -203,6 +207,23 @@ class ServerObject(pb.Avatar):
     cfbd = objects.Disk.FromDict(params[0])
     return backend.SnapshotBlockDevice(cfbd)
 
+  @staticmethod
+  def perspective_blockdev_grow(params):
+    """Grow a stack of devices.
+
+    """
+    cfbd = objects.Disk.FromDict(params[0])
+    amount = params[1]
+    return backend.GrowBlockDevice(cfbd, amount)
+
+  @staticmethod
+  def perspective_blockdev_close(params):
+    """Closes the given block devices.
+
+    """
+    disks = [objects.Disk.FromDict(cf) for cf in params]
+    return backend.CloseBlockDevices(disks)
+
   # export/import  --------------------------
 
   @staticmethod
@@ -333,6 +354,14 @@ class ServerObject(pb.Avatar):
     return backend.StartInstance(instance, extra_args)
 
   @staticmethod
+  def perspective_instance_migrate(params):
+    """Migrates an instance.
+
+    """
+    instance, target, live = params
+    return backend.MigrateInstance(instance, target, live)
+
+  @staticmethod
   def perspective_instance_reboot(params):
     """Reboot an instance.
 
@@ -370,8 +399,8 @@ class ServerObject(pb.Avatar):
     """Do a TcpPing on the remote node.
 
     """
-    return utils.TcpPing(params[0], params[1], params[2],
-                         timeout=params[3], live_port_needed=params[4])
+    return utils.TcpPing(params[1], params[2], timeout=params[3],
+                         live_port_needed=params[4], source=params[0])
 
   @staticmethod
   def perspective_node_info(params):
@@ -401,14 +430,14 @@ class ServerObject(pb.Avatar):
     """Promote this node to master status.
 
     """
-    return backend.StartMaster()
+    return backend.StartMaster(params[0])
 
   @staticmethod
   def perspective_node_stop_master(params):
     """Demote this node from master status.
 
     """
-    return backend.StopMaster()
+    return backend.StopMaster(params[0])
 
   @staticmethod
   def perspective_node_leave_cluster(params):
@@ -443,6 +472,12 @@ class ServerObject(pb.Avatar):
     """
     return backend.UploadFile(*params)
 
+  @staticmethod
+  def perspective_master_info(params):
+    """Query master information.
+
+    """
+    return backend.GetMasterInfo()
 
   # os -----------------------
 
@@ -476,20 +511,108 @@ class ServerObject(pb.Avatar):
     hr = backend.HooksRunner()
     return hr.RunHooks(hpath, phase, env)
 
+  # iallocator -----------------
 
-class MyRealm:
-  """Simple realm that forwards all requests to a ServerObject.
+  @staticmethod
+  def perspective_iallocator_runner(params):
+    """Run an iallocator script.
 
-  """
-  __implements__ = portal.IRealm
+    """
+    name, idata = params
+    iar = backend.IAllocatorRunner()
+    return iar.Run(name, idata)
+
+  # test -----------------------
 
-  def requestAvatar(self, avatarId, mind, *interfaces):
-    """Return an avatar based on our ServerObject class.
+  @staticmethod
+  def perspective_test_delay(params):
+    """Run test delay.
+
+    """
+    duration = params[0]
+    return utils.TestDelay(duration)
+
+  # file storage ---------------
+
+  @staticmethod
+  def perspective_file_storage_dir_create(params):
+    """Create the file storage directory.
 
     """
-    if pb.IPerspective not in interfaces:
-      raise NotImplementedError
-    return pb.IPerspective, ServerObject(avatarId), lambda:None
+    file_storage_dir = params[0]
+    return backend.CreateFileStorageDir(file_storage_dir)
+
+  @staticmethod
+  def perspective_file_storage_dir_remove(params):
+    """Remove the file storage directory.
+
+    """
+    file_storage_dir = params[0]
+    return backend.RemoveFileStorageDir(file_storage_dir)
+
+  @staticmethod
+  def perspective_file_storage_dir_rename(params):
+    """Rename the file storage directory.
+
+    """
+    old_file_storage_dir = params[0]
+    new_file_storage_dir = params[1]
+    return backend.RenameFileStorageDir(old_file_storage_dir,
+                                        new_file_storage_dir)
+
+  # jobs ------------------------
+
+  @staticmethod
+  @_RequireJobQueueLock
+  def perspective_jobqueue_update(params):
+    """Update job queue.
+
+    """
+    (file_name, content) = params
+    return backend.JobQueueUpdate(file_name, content)
+
+  @staticmethod
+  @_RequireJobQueueLock
+  def perspective_jobqueue_purge(params):
+    """Purge job queue.
+
+    """
+    return backend.JobQueuePurge()
+
+  @staticmethod
+  @_RequireJobQueueLock
+  def perspective_jobqueue_rename(params):
+    """Rename a job queue file.
+
+    """
+    (old, new) = params
+
+    return backend.JobQueueRename(old, new)
+
+
+class NodeDaemonHttpServer(http.HTTPServer):
+  def __init__(self, server_address):
+    http.HTTPServer.__init__(self, server_address, NodeDaemonRequestHandler)
+    self.noded_pid = os.getpid()
+
+  def serve_forever(self):
+    """Handle requests until told to quit."""
+    sighandler = utils.SignalHandler([signal.SIGINT, signal.SIGTERM])
+    try:
+      while not sighandler.called:
+        self.handle_request()
+      # TODO: There could be children running at this point
+    finally:
+      sighandler.Reset()
+
+
+class ForkingHTTPServer(SocketServer.ForkingMixIn, NodeDaemonHttpServer):
+  """Forking HTTP Server.
+
+  This inherits from ForkingMixIn and HTTPServer in order to fork for each
+  request we handle. This allows more requests to be handled concurrently.
+
+  """
 
 
 def ParseOptions():
@@ -518,7 +641,10 @@ def main():
   """Main function for the node daemon.
 
   """
+  global queue_lock
+
   options, args = ParseOptions()
+  utils.debug = options.debug
   for fname in (constants.SSL_CERT_FILE,):
     if not os.path.isfile(fname):
       print "config %s not there, will not run." % fname
@@ -532,73 +658,43 @@ def main():
     print "Cluster configuration incomplete: '%s'" % str(err)
     sys.exit(5)
 
+  # create the various SUB_RUN_DIRS, if not existing, so that we handle the
+  # situation where RUN_DIR is tmpfs
+  for dir_name in constants.SUB_RUN_DIRS:
+    if not os.path.exists(dir_name):
+      try:
+        os.mkdir(dir_name, 0755)
+      except EnvironmentError, err:
+        if err.errno != errno.EEXIST:
+          print ("Node setup wrong, cannot create directory %s: %s" %
+                 (dir_name, err))
+          sys.exit(5)
+    if not os.path.isdir(dir_name):
+      print ("Node setup wrong, %s is not a directory" % dir_name)
+      sys.exit(5)
+
   # become a daemon
   if options.fork:
-    createDaemon()
+    utils.Daemonize(logfile=constants.LOG_NODESERVER)
 
-  logger.SetupLogging(twisted_workaround=True, debug=options.debug,
-                      program="ganeti-noded")
+  utils.WritePidFile(constants.NODED_PID)
 
-  p = portal.Portal(MyRealm())
-  p.registerChecker(
-    checkers.InMemoryUsernamePasswordDatabaseDontUse(master_node=pwdata))
-  reactor.listenSSL(port, pb.PBServerFactory(p), ServerContextFactory())
-  reactor.run()
+  logger.SetupLogging(logfile=constants.LOG_NODESERVER, debug=options.debug,
+                      stderr_logging=not options.fork)
+  logging.info("ganeti node daemon startup")
 
+  # Prepare job queue
+  queue_lock = jstore.InitAndVerifyQueue(must_lock=False)
 
-def createDaemon():
-  """Detach a process from the controlling terminal and run it in the
-  background as a daemon.
-
-  """
-  UMASK = 077
-  WORKDIR = "/"
-  # Default maximum for the number of available file descriptors.
-  if 'SC_OPEN_MAX' in os.sysconf_names:
-    try:
-      MAXFD = os.sysconf('SC_OPEN_MAX')
-      if MAXFD < 0:
-        MAXFD = 1024
-    except OSError:
-      MAXFD = 1024
-  else:
-    MAXFD = 1024
-  # The standard I/O file descriptors are redirected to /dev/null by default.
-  #REDIRECT_TO = getattr(os, "devnull", "/dev/null")
-  REDIRECT_TO = constants.LOG_NODESERVER
-  try:
-    pid = os.fork()
-  except OSError, e:
-    raise Exception("%s [%d]" % (e.strerror, e.errno))
-  if (pid == 0):  # The first child.
-    os.setsid()
-    try:
-      pid = os.fork() # Fork a second child.
-    except OSError, e:
-      raise Exception("%s [%d]" % (e.strerror, e.errno))
-    if (pid == 0):  # The second child.
-      os.chdir(WORKDIR)
-      os.umask(UMASK)
-    else:
-      # exit() or _exit()?  See below.
-      os._exit(0) # Exit parent (the first child) of the second child.
+  if options.fork:
+    server = ForkingHTTPServer(('', port))
   else:
-    os._exit(0) # Exit parent of the first child.
-  maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
-  if (maxfd == resource.RLIM_INFINITY):
-    maxfd = MAXFD
+    server = NodeDaemonHttpServer(('', port))
 
-  # Iterate through and close all file descriptors.
-  for fd in range(0, maxfd):
-    try:
-      os.close(fd)
-    except OSError: # ERROR, fd wasn't open to begin with (ignored)
-      pass
-  os.open(REDIRECT_TO, os.O_RDWR|os.O_CREAT|os.O_APPEND, 0600)
-  # Duplicate standard input to standard output and standard error.
-  os.dup2(0, 1)     # standard output (1)
-  os.dup2(0, 2)     # standard error (2)
-  return(0)
+  try:
+    server.serve_forever()
+  finally:
+    utils.RemovePidFile(constants.NODED_PID)
 
 
 if __name__ == '__main__':