4 # Copyright (C) 2009, 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Ganeti configuration daemon
24 Ganeti-confd is a daemon to query master candidates for configuration values.
25 It uses UDP+HMAC for authentication with a global cluster key.
29 # pylint: disable-msg=C0103
30 # C0103: Invalid name ganeti-confd
38 # pylint: disable-msg=E0611
39 from pyinotify import pyinotify
43 from optparse import OptionParser
45 from ganeti import asyncnotifier
46 from ganeti import confd
47 from ganeti.confd import server as confd_server
48 from ganeti import constants
49 from ganeti import errors
50 from ganeti import daemon
51 from ganeti import netutils
54 class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
55 """The confd udp server, suitable for use with asyncore.
58 def __init__(self, bind_address, port, processor):
59 """Constructor for ConfdAsyncUDPServer
61 @type bind_address: string
62 @param bind_address: socket bind address
65 @type processor: L{confd.server.ConfdProcessor}
66 @param processor: ConfdProcessor to use to handle queries
69 family = netutils.IPAddress.GetAddressFamily(bind_address)
70 daemon.AsyncUDPSocket.__init__(self, family)
71 self.bind_address = bind_address
73 self.processor = processor
74 self.bind((bind_address, port))
75 logging.debug("listening on ('%s':%d)", bind_address, port)
77 # this method is overriding a daemon.AsyncUDPSocket method
78 def handle_datagram(self, payload_in, ip, port):
80 query = confd.UnpackMagic(payload_in)
81 except errors.ConfdMagicError, err:
85 answer = self.processor.ExecQuery(query, ip, port)
86 if answer is not None:
88 self.enqueue_send(ip, port, confd.PackMagic(answer))
89 except errors.UdpDataSizeError:
90 logging.error("Reply too big to fit in an udp packet.")
93 class ConfdConfigurationReloader(object):
94 """Logic to control when to reload the ganeti configuration
96 This class is able to alter between inotify and polling, to rate-limit the
97 number of reloads. When using inotify it also supports a fallback timed
98 check, to verify that the reload hasn't failed.
101 def __init__(self, processor, mainloop):
102 """Constructor for ConfdConfigurationReloader
104 @type processor: L{confd.server.ConfdProcessor}
105 @param processor: ganeti-confd ConfdProcessor
106 @type mainloop: L{daemon.Mainloop}
107 @param mainloop: ganeti-confd mainloop
110 self.processor = processor
111 self.mainloop = mainloop
114 self.last_notification = 0
116 # Asyncronous inotify handler for config changes
117 cfg_file = constants.CLUSTER_CONF_FILE
118 self.wm = pyinotify.WatchManager()
119 self.inotify_handler = asyncnotifier.SingleFileEventHandler(self.wm,
122 notifier_class = asyncnotifier.ErrorLoggingAsyncNotifier
123 self.notifier = notifier_class(self.wm, self.inotify_handler)
125 self.timer_handle = None
128 def OnInotify(self, notifier_enabled):
129 """Receive an inotify notification.
131 @type notifier_enabled: boolean
132 @param notifier_enabled: whether the notifier is still enabled
135 current_time = time.time()
136 time_delta = current_time - self.last_notification
137 self.last_notification = current_time
139 if time_delta < constants.CONFD_CONFIG_RELOAD_RATELIMIT:
140 logging.debug("Moving from inotify mode to polling mode")
143 self.inotify_handler.disable()
145 if not self.polling and not notifier_enabled:
147 self.inotify_handler.enable()
148 except errors.InotifyError:
152 reloaded = self.processor.reader.Reload()
154 logging.info("Reloaded ganeti config")
156 logging.debug("Skipped double config reload")
157 except errors.ConfigurationError:
159 self.inotify_handler.disable()
162 # Reset the timer. If we're polling it will go to the polling rate, if
163 # we're not it will delay it again to its base safe timeout.
166 def _DisableTimer(self):
167 if self.timer_handle is not None:
168 self.mainloop.scheduler.cancel(self.timer_handle)
169 self.timer_handle = None
171 def _EnableTimer(self):
173 timeout = constants.CONFD_CONFIG_RELOAD_RATELIMIT
175 timeout = constants.CONFD_CONFIG_RELOAD_TIMEOUT
177 if self.timer_handle is None:
178 self.timer_handle = self.mainloop.scheduler.enter(
179 timeout, 1, self.OnTimer, [])
181 def _ResetTimer(self):
186 """Function called when the timer fires
189 self.timer_handle = None
193 if self.processor.reader is None:
198 reloaded = self.processor.reader.Reload()
199 except errors.ConfigurationError:
200 self.DisableConfd(silent=was_disabled)
203 if self.polling and reloaded:
204 logging.info("Reloaded ganeti config")
206 # We have reloaded the config files, but received no inotify event. If
207 # an event is pending though, we just happen to have timed out before
208 # receiving it, so this is not a problem, and we shouldn't alert
209 if not self.notifier.check_events() and not was_disabled:
210 logging.warning("Config file reload at timeout (inotify failure)")
212 # We're polling, but we haven't reloaded the config:
213 # Going back to inotify mode
214 logging.debug("Moving from polling mode to inotify mode")
217 self.inotify_handler.enable()
218 except errors.InotifyError:
221 logging.debug("Performed configuration check")
225 def DisableConfd(self, silent=False):
226 """Puts confd in non-serving mode
230 logging.warning("Confd is being disabled")
231 self.processor.Disable()
235 def EnableConfd(self):
236 self.processor.Enable()
237 logging.warning("Confd is being enabled")
242 def CheckConfd(_, args):
243 """Initial checks whether to run exit with a failure.
246 if args: # confd doesn't take any arguments
247 print >> sys.stderr, ("Usage: %s [-f] [-d] [-b ADDRESS]" % sys.argv[0])
248 sys.exit(constants.EXIT_FAILURE)
250 # TODO: collapse HMAC daemons handling in daemons GenericMain, when we'll
251 # have more than one.
252 if not os.path.isfile(constants.CONFD_HMAC_KEY):
253 print >> sys.stderr, "Need HMAC key %s to run" % constants.CONFD_HMAC_KEY
254 sys.exit(constants.EXIT_FAILURE)
256 # TODO: once we have a cluster param specifying the address family
257 # preference, we need to check if the requested options.bind_address does not
258 # conflict with that. If so, we might warn or EXIT_FAILURE.
261 def ExecConfd(options, _dummy1, _dummy2):
262 """Main confd function, executed with PID file held
265 # TODO: clarify how the server and reloader variables work (they are
267 # pylint: disable-msg=W0612
268 mainloop = daemon.Mainloop()
270 # Asyncronous confd UDP server
271 processor = confd_server.ConfdProcessor()
274 except errors.ConfigurationError:
275 # If enabling the processor has failed, we can still go on, but confd will
277 logging.warning("Confd is starting in disabled mode")
279 server = ConfdAsyncUDPServer(options.bind_address, options.port, processor)
281 # Configuration reloader
282 reloader = ConfdConfigurationReloader(processor, mainloop)
288 """Main function for the confd daemon.
291 parser = OptionParser(description="Ganeti configuration daemon",
292 usage="%prog [-f] [-d] [-b ADDRESS]",
293 version="%%prog (ganeti) %s" %
294 constants.RELEASE_VERSION)
296 daemon.GenericMain(constants.CONFD, parser, CheckConfd, None, ExecConfd)
299 if __name__ == "__main__":