ganeti-confd: don't depend on the os log dir
[ganeti-local] / daemons / ganeti-confd
index b1696d6..671dbd2 100755 (executable)
@@ -37,15 +37,16 @@ import errno
 
 from optparse import OptionParser
 
+from ganeti import asyncnotifier
+from ganeti import confd
+from ganeti.confd import server as confd_server
 from ganeti import constants
 from ganeti import errors
 from ganeti import daemon
 from ganeti import ssconf
-from ganeti.asyncnotifier import AsyncNotifier
-from ganeti.confd.server import ConfdProcessor
 
 
-class ConfdAsyncUDPServer(asyncore.dispatcher):
+class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
   """The confd udp server, suitable for use with asyncore.
 
   """
@@ -57,50 +58,30 @@ class ConfdAsyncUDPServer(asyncore.dispatcher):
     @type port: int
     @param port: udp port
     @type processor: L{confd.server.ConfdProcessor}
-    @param reader: ConfigReader to use to access the config
+    @param processor: ConfdProcessor to use to handle queries
 
     """
-    asyncore.dispatcher.__init__(self)
+    daemon.AsyncUDPSocket.__init__(self)
     self.bind_address = bind_address
     self.port = port
     self.processor = processor
-    self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
     self.bind((bind_address, port))
     logging.debug("listening on ('%s':%d)" % (bind_address, port))
 
-  # this method is overriding an asyncore.dispatcher method
-  def handle_connect(self):
-    # Python thinks that the first udp message from a source qualifies as a
-    # "connect" and further ones are part of the same connection. We beg to
-    # differ and treat all messages equally.
-    pass
-
-  # this method is overriding an asyncore.dispatcher method
-  def handle_read(self):
+  # this method is overriding a daemon.AsyncUDPSocket method
+  def handle_datagram(self, payload_in, ip, port):
     try:
-      try:
-        payload_in, address = self.recvfrom(4096)
-      except socket.error, err:
-        if err.errno == errno.EINTR:
-          # we got a signal while trying to read. no need to do anything,
-          # handle_read will be called again if there is data on the socket.
-          return
-        else:
-          raise
-      ip, port = address
-      payload_out =  self.processor.ExecQuery(payload_in, ip, port)
-      if payload_out is not None:
-        self.sendto(payload_out, 0, (ip, port))
-    except:
-      # we need to catch any exception here, log it, but proceed, because even
-      # if we failed handling a single request, we still want the confd to
-      # continue working.
-      logging.error("Unexpected exception", exc_info=True)
+      query = confd.UnpackMagic(payload_in)
+    except errors.ConfdMagicError, err:
+      logging.debug(err)
+      return
 
-  # this method is overriding an asyncore.dispatcher method
-  def writable(self):
-    # No need to check if we can write to the UDP socket
-    return False
+    answer =  self.processor.ExecQuery(query, ip, port)
+    if answer is not None:
+      try:
+        self.enqueue_send(ip, port, confd.PackMagic(answer))
+      except errors.UdpDataSizeError:
+        logging.error("Reply too big to fit in an udp packet.")
 
 
 class ConfdInotifyEventHandler(pyinotify.ProcessEvent):
@@ -218,7 +199,7 @@ class ConfdConfigurationReloader(object):
     # Asyncronous inotify handler for config changes
     self.wm = pyinotify.WatchManager()
     self.inotify_handler = ConfdInotifyEventHandler(self.wm, self.OnInotify)
-    self.notifier = AsyncNotifier(self.wm, self.inotify_handler)
+    self.notifier = asyncnotifier.AsyncNotifier(self.wm, self.inotify_handler)
 
     self.timer_handle = None
     self._EnableTimer()
@@ -355,7 +336,7 @@ def ExecConfd(options, args):
   mainloop = daemon.Mainloop()
 
   # Asyncronous confd UDP server
-  processor = ConfdProcessor()
+  processor = confd_server.ConfdProcessor()
   try:
     processor.Enable()
   except errors.ConfigurationError:
@@ -381,7 +362,6 @@ def main():
                         constants.RELEASE_VERSION)
 
   dirs = [(val, constants.RUN_DIRS_MODE) for val in constants.SUB_RUN_DIRS]
-  dirs.append((constants.LOG_OS_DIR, 0750))
   dirs.append((constants.LOCK_DIR, 1777))
   daemon.GenericMain(constants.CONFD, parser, dirs, CheckConfd, ExecConfd)