Modify gnt-node add to call external script
[ganeti-local] / daemons / ganeti-confd
index 24245fa..d19157e 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2009, Google Inc.
+# Copyright (C) 2009, 2010 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -26,12 +26,16 @@ It uses UDP+HMAC for authentication with a global cluster key.
 
 """
 
+# pylint: disable-msg=C0103
+# C0103: Invalid name ganeti-confd
+
 import os
 import sys
 import logging
 import time
 
 try:
+  # pylint: disable-msg=E0611
   from pyinotify import pyinotify
 except ImportError:
   import pyinotify
@@ -44,7 +48,7 @@ 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 import netutils
 
 
 class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
@@ -55,14 +59,15 @@ class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
     """Constructor for ConfdAsyncUDPServer
 
     @type bind_address: string
-    @param bind_address: socket bind address ('' for all)
+    @param bind_address: socket bind address
     @type port: int
     @param port: udp port
     @type processor: L{confd.server.ConfdProcessor}
     @param processor: ConfdProcessor to use to handle queries
 
     """
-    daemon.AsyncUDPSocket.__init__(self)
+    family = netutils.IPAddress.GetAddressFamily(bind_address)
+    daemon.AsyncUDPSocket.__init__(self, family)
     self.bind_address = bind_address
     self.port = port
     self.processor = processor
@@ -85,95 +90,6 @@ class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
         logging.error("Reply too big to fit in an udp packet.")
 
 
-class ConfdInotifyEventHandler(pyinotify.ProcessEvent):
-
-  def __init__(self, watch_manager, callback,
-               file=constants.CLUSTER_CONF_FILE):
-    """Constructor for ConfdInotifyEventHandler
-
-    @type watch_manager: L{pyinotify.WatchManager}
-    @param watch_manager: ganeti-confd inotify watch manager
-    @type callback: function accepting a boolean
-    @param callback: function to call when an inotify event happens
-    @type file: string
-    @param file: config file to watch
-
-    """
-    # no need to call the parent's constructor
-    self.watch_manager = watch_manager
-    self.callback = callback
-    self.mask = pyinotify.EventsCodes.IN_IGNORED | \
-                pyinotify.EventsCodes.IN_MODIFY
-    self.file = file
-    self.watch_handle = None
-
-  def enable(self):
-    """Watch the given file
-
-    """
-    if self.watch_handle is None:
-      result = self.watch_manager.add_watch(self.file, self.mask)
-      if not self.file in result or result[self.file] <= 0:
-        raise errors.InotifyError("Could not add inotify watcher")
-      else:
-        self.watch_handle = result[self.file]
-
-  def disable(self):
-    """Stop watching the given file
-
-    """
-    if self.watch_handle is not None:
-      result = self.watch_manager.rm_watch(self.watch_handle)
-      if result[self.watch_handle]:
-        self.watch_handle = None
-
-  def process_IN_IGNORED(self, event):
-    # Due to the fact that we monitor just for the cluster config file (rather
-    # than for the whole data dir) when the file is replaced with another one
-    # (which is what happens normally in ganeti) we're going to receive an
-    # IN_IGNORED event from inotify, because of the file removal (which is
-    # contextual with the replacement). In such a case we need to create
-    # another watcher for the "new" file.
-    logging.debug("Received 'ignored' inotify event for %s", event.path)
-    self.watch_handle = None
-
-    try:
-      # Since the kernel believes the file we were interested in is gone, it's
-      # not going to notify us of any other events, until we set up, here, the
-      # new watch. This is not a race condition, though, since we're anyway
-      # going to realod the file after setting up the new watch.
-      self.callback(False)
-    except errors.ConfdFatalError, err:
-      logging.critical("Critical error, shutting down: %s", err)
-      sys.exit(constants.EXIT_FAILURE)
-    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)
-
-  def process_IN_MODIFY(self, event):
-    # This gets called when the config file is modified. Note that this doesn't
-    # usually happen in Ganeti, as the config file is normally replaced by a
-    # new one, at filesystem level, rather than actually modified (see
-    # utils.WriteFile)
-    logging.debug("Received 'modify' inotify event for %s", event.path)
-
-    try:
-      self.callback(True)
-    except errors.ConfdFatalError, err:
-      logging.critical("Critical error, shutting down: %s", err)
-      sys.exit(constants.EXIT_FAILURE)
-    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)
-
-  def process_default(self, event):
-    logging.error("Received unhandled inotify event: %s", event)
-
-
 class ConfdConfigurationReloader(object):
   """Logic to control when to reload the ganeti configuration
 
@@ -198,9 +114,13 @@ class ConfdConfigurationReloader(object):
     self.last_notification = 0
 
     # Asyncronous inotify handler for config changes
+    cfg_file = constants.CLUSTER_CONF_FILE
     self.wm = pyinotify.WatchManager()
-    self.inotify_handler = ConfdInotifyEventHandler(self.wm, self.OnInotify)
-    self.notifier = asyncnotifier.AsyncNotifier(self.wm, self.inotify_handler)
+    self.inotify_handler = asyncnotifier.SingleFileEventHandler(self.wm,
+                                                                self.OnInotify,
+                                                                cfg_file)
+    notifier_class = asyncnotifier.ErrorLoggingAsyncNotifier
+    self.notifier = notifier_class(self.wm, self.inotify_handler)
 
     self.timer_handle = None
     self._EnableTimer()
@@ -319,21 +239,32 @@ class ConfdConfigurationReloader(object):
     self._ResetTimer()
 
 
-def CheckConfd(options, args):
+def CheckConfd(_, args):
   """Initial checks whether to run exit with a failure.
 
   """
+  if args: # confd doesn't take any arguments
+    print >> sys.stderr, ("Usage: %s [-f] [-d] [-b ADDRESS]" % sys.argv[0])
+    sys.exit(constants.EXIT_FAILURE)
+
   # TODO: collapse HMAC daemons handling in daemons GenericMain, when we'll
   # have more than one.
-  if not os.path.isfile(constants.HMAC_CLUSTER_KEY):
-    print >> sys.stderr, "Need HMAC key %s to run" % constants.HMAC_CLUSTER_KEY
+  if not os.path.isfile(constants.CONFD_HMAC_KEY):
+    print >> sys.stderr, "Need HMAC key %s to run" % constants.CONFD_HMAC_KEY
     sys.exit(constants.EXIT_FAILURE)
 
+  # TODO: once we have a cluster param specifying the address family
+  # preference, we need to check if the requested options.bind_address does not
+  # conflict with that. If so, we might warn or EXIT_FAILURE.
 
-def ExecConfd(options, args):
+
+def ExecConfd(options, _):
   """Main confd function, executed with PID file held
 
   """
+  # TODO: clarify how the server and reloader variables work (they are
+  # not used)
+  # pylint: disable-msg=W0612
   mainloop = daemon.Mainloop()
 
   # Asyncronous confd UDP server
@@ -344,7 +275,7 @@ def ExecConfd(options, args):
     # If enabling the processor has failed, we can still go on, but confd will
     # be disabled
     logging.warning("Confd is starting in disabled mode")
-    pass
+
   server = ConfdAsyncUDPServer(options.bind_address, options.port, processor)
 
   # Configuration reloader