Statistics
| Branch: | Tag: | Revision:

root / daemons / ganeti-confd @ 47f8a2d2

History | View | Annotate | Download (12.1 kB)

1 b84cb9a0 Guido Trotter
#!/usr/bin/python
2 b84cb9a0 Guido Trotter
#
3 b84cb9a0 Guido Trotter
4 b84cb9a0 Guido Trotter
# Copyright (C) 2009, Google Inc.
5 b84cb9a0 Guido Trotter
#
6 b84cb9a0 Guido Trotter
# This program is free software; you can redistribute it and/or modify
7 b84cb9a0 Guido Trotter
# it under the terms of the GNU General Public License as published by
8 b84cb9a0 Guido Trotter
# the Free Software Foundation; either version 2 of the License, or
9 b84cb9a0 Guido Trotter
# (at your option) any later version.
10 b84cb9a0 Guido Trotter
#
11 b84cb9a0 Guido Trotter
# This program is distributed in the hope that it will be useful, but
12 b84cb9a0 Guido Trotter
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 b84cb9a0 Guido Trotter
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 b84cb9a0 Guido Trotter
# General Public License for more details.
15 b84cb9a0 Guido Trotter
#
16 b84cb9a0 Guido Trotter
# You should have received a copy of the GNU General Public License
17 b84cb9a0 Guido Trotter
# along with this program; if not, write to the Free Software
18 b84cb9a0 Guido Trotter
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 b84cb9a0 Guido Trotter
# 02110-1301, USA.
20 b84cb9a0 Guido Trotter
21 b84cb9a0 Guido Trotter
22 b84cb9a0 Guido Trotter
"""Ganeti configuration daemon
23 b84cb9a0 Guido Trotter
24 b84cb9a0 Guido Trotter
Ganeti-confd is a daemon to query master candidates for configuration values.
25 b84cb9a0 Guido Trotter
It uses UDP+HMAC for authentication with a global cluster key.
26 b84cb9a0 Guido Trotter
27 b84cb9a0 Guido Trotter
"""
28 b84cb9a0 Guido Trotter
29 7260cfbe Iustin Pop
# pylint: disable-msg=C0103
30 7260cfbe Iustin Pop
# C0103: Invalid name ganeti-confd
31 7260cfbe Iustin Pop
32 b84cb9a0 Guido Trotter
import os
33 b84cb9a0 Guido Trotter
import sys
34 b84cb9a0 Guido Trotter
import logging
35 e2be81cf Guido Trotter
import time
36 b84cb9a0 Guido Trotter
37 ad54f3d2 Guido Trotter
try:
38 7260cfbe Iustin Pop
  # pylint: disable-msg=E0611
39 ad54f3d2 Guido Trotter
  from pyinotify import pyinotify
40 ad54f3d2 Guido Trotter
except ImportError:
41 ad54f3d2 Guido Trotter
  import pyinotify
42 ad54f3d2 Guido Trotter
43 b84cb9a0 Guido Trotter
from optparse import OptionParser
44 b84cb9a0 Guido Trotter
45 e1081705 Guido Trotter
from ganeti import asyncnotifier
46 e1081705 Guido Trotter
from ganeti import confd
47 e1081705 Guido Trotter
from ganeti.confd import server as confd_server
48 b84cb9a0 Guido Trotter
from ganeti import constants
49 b84cb9a0 Guido Trotter
from ganeti import errors
50 b84cb9a0 Guido Trotter
from ganeti import daemon
51 b84cb9a0 Guido Trotter
52 b84cb9a0 Guido Trotter
53 5f3269fc Guido Trotter
class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
54 b84cb9a0 Guido Trotter
  """The confd udp server, suitable for use with asyncore.
55 b84cb9a0 Guido Trotter
56 b84cb9a0 Guido Trotter
  """
57 b84cb9a0 Guido Trotter
  def __init__(self, bind_address, port, processor):
58 b84cb9a0 Guido Trotter
    """Constructor for ConfdAsyncUDPServer
59 b84cb9a0 Guido Trotter
60 b84cb9a0 Guido Trotter
    @type bind_address: string
61 b84cb9a0 Guido Trotter
    @param bind_address: socket bind address ('' for all)
62 b84cb9a0 Guido Trotter
    @type port: int
63 b84cb9a0 Guido Trotter
    @param port: udp port
64 b84cb9a0 Guido Trotter
    @type processor: L{confd.server.ConfdProcessor}
65 fe759e4c Guido Trotter
    @param processor: ConfdProcessor to use to handle queries
66 b84cb9a0 Guido Trotter
67 b84cb9a0 Guido Trotter
    """
68 5f3269fc Guido Trotter
    daemon.AsyncUDPSocket.__init__(self)
69 b84cb9a0 Guido Trotter
    self.bind_address = bind_address
70 b84cb9a0 Guido Trotter
    self.port = port
71 b84cb9a0 Guido Trotter
    self.processor = processor
72 b84cb9a0 Guido Trotter
    self.bind((bind_address, port))
73 07b8a2b5 Iustin Pop
    logging.debug("listening on ('%s':%d)", bind_address, port)
74 b84cb9a0 Guido Trotter
75 5f3269fc Guido Trotter
  # this method is overriding a daemon.AsyncUDPSocket method
76 5f3269fc Guido Trotter
  def handle_datagram(self, payload_in, ip, port):
77 9748ab35 Guido Trotter
    try:
78 e1081705 Guido Trotter
      query = confd.UnpackMagic(payload_in)
79 9748ab35 Guido Trotter
    except errors.ConfdMagicError, err:
80 9748ab35 Guido Trotter
      logging.debug(err)
81 a3758ab2 Guido Trotter
      return
82 a3758ab2 Guido Trotter
83 a3758ab2 Guido Trotter
    answer =  self.processor.ExecQuery(query, ip, port)
84 a3758ab2 Guido Trotter
    if answer is not None:
85 86488201 Guido Trotter
      try:
86 e1081705 Guido Trotter
        self.enqueue_send(ip, port, confd.PackMagic(answer))
87 86488201 Guido Trotter
      except errors.UdpDataSizeError:
88 86488201 Guido Trotter
        logging.error("Reply too big to fit in an udp packet.")
89 b84cb9a0 Guido Trotter
90 b84cb9a0 Guido Trotter
91 b84cb9a0 Guido Trotter
class ConfdInotifyEventHandler(pyinotify.ProcessEvent):
92 b84cb9a0 Guido Trotter
93 4afe249b Guido Trotter
  def __init__(self, watch_manager, callback,
94 e2e10467 Iustin Pop
               filename=constants.CLUSTER_CONF_FILE):
95 b84cb9a0 Guido Trotter
    """Constructor for ConfdInotifyEventHandler
96 b84cb9a0 Guido Trotter
97 3a488770 Iustin Pop
    @type watch_manager: pyinotify.WatchManager
98 b84cb9a0 Guido Trotter
    @param watch_manager: ganeti-confd inotify watch manager
99 4afe249b Guido Trotter
    @type callback: function accepting a boolean
100 4afe249b Guido Trotter
    @param callback: function to call when an inotify event happens
101 e2e10467 Iustin Pop
    @type filename: string
102 e2e10467 Iustin Pop
    @param filename: config file to watch
103 b84cb9a0 Guido Trotter
104 b84cb9a0 Guido Trotter
    """
105 4eea1739 Guido Trotter
    # pylint: disable-msg=W0231
106 b84cb9a0 Guido Trotter
    # no need to call the parent's constructor
107 b84cb9a0 Guido Trotter
    self.watch_manager = watch_manager
108 4afe249b Guido Trotter
    self.callback = callback
109 675bf1b7 Guido Trotter
    self.mask = pyinotify.EventsCodes.ALL_FLAGS["IN_IGNORED"] | \
110 675bf1b7 Guido Trotter
                pyinotify.EventsCodes.ALL_FLAGS["IN_MODIFY"]
111 e2e10467 Iustin Pop
    self.file = filename
112 46c9b31d Guido Trotter
    self.watch_handle = None
113 b84cb9a0 Guido Trotter
114 46c9b31d Guido Trotter
  def enable(self):
115 46c9b31d Guido Trotter
    """Watch the given file
116 b84cb9a0 Guido Trotter
117 b84cb9a0 Guido Trotter
    """
118 46c9b31d Guido Trotter
    if self.watch_handle is None:
119 46c9b31d Guido Trotter
      result = self.watch_manager.add_watch(self.file, self.mask)
120 46c9b31d Guido Trotter
      if not self.file in result or result[self.file] <= 0:
121 ef4ca33b Guido Trotter
        raise errors.InotifyError("Could not add inotify watcher")
122 46c9b31d Guido Trotter
      else:
123 46c9b31d Guido Trotter
        self.watch_handle = result[self.file]
124 46c9b31d Guido Trotter
125 46c9b31d Guido Trotter
  def disable(self):
126 46c9b31d Guido Trotter
    """Stop watching the given file
127 46c9b31d Guido Trotter
128 46c9b31d Guido Trotter
    """
129 46c9b31d Guido Trotter
    if self.watch_handle is not None:
130 46c9b31d Guido Trotter
      result = self.watch_manager.rm_watch(self.watch_handle)
131 46c9b31d Guido Trotter
      if result[self.watch_handle]:
132 46c9b31d Guido Trotter
        self.watch_handle = None
133 b84cb9a0 Guido Trotter
134 b84cb9a0 Guido Trotter
  def process_IN_IGNORED(self, event):
135 b84cb9a0 Guido Trotter
    # Due to the fact that we monitor just for the cluster config file (rather
136 b84cb9a0 Guido Trotter
    # than for the whole data dir) when the file is replaced with another one
137 b84cb9a0 Guido Trotter
    # (which is what happens normally in ganeti) we're going to receive an
138 b84cb9a0 Guido Trotter
    # IN_IGNORED event from inotify, because of the file removal (which is
139 b84cb9a0 Guido Trotter
    # contextual with the replacement). In such a case we need to create
140 b84cb9a0 Guido Trotter
    # another watcher for the "new" file.
141 07b8a2b5 Iustin Pop
    logging.debug("Received 'ignored' inotify event for %s", event.path)
142 46c9b31d Guido Trotter
    self.watch_handle = None
143 b84cb9a0 Guido Trotter
144 b84cb9a0 Guido Trotter
    try:
145 b84cb9a0 Guido Trotter
      # Since the kernel believes the file we were interested in is gone, it's
146 b84cb9a0 Guido Trotter
      # not going to notify us of any other events, until we set up, here, the
147 b84cb9a0 Guido Trotter
      # new watch. This is not a race condition, though, since we're anyway
148 b84cb9a0 Guido Trotter
      # going to realod the file after setting up the new watch.
149 4afe249b Guido Trotter
      self.callback(False)
150 b84cb9a0 Guido Trotter
    except errors.ConfdFatalError, err:
151 07b8a2b5 Iustin Pop
      logging.critical("Critical error, shutting down: %s", err)
152 b84cb9a0 Guido Trotter
      sys.exit(constants.EXIT_FAILURE)
153 b84cb9a0 Guido Trotter
    except:
154 b84cb9a0 Guido Trotter
      # we need to catch any exception here, log it, but proceed, because even
155 b84cb9a0 Guido Trotter
      # if we failed handling a single request, we still want the confd to
156 b84cb9a0 Guido Trotter
      # continue working.
157 b84cb9a0 Guido Trotter
      logging.error("Unexpected exception", exc_info=True)
158 b84cb9a0 Guido Trotter
159 b84cb9a0 Guido Trotter
  def process_IN_MODIFY(self, event):
160 b84cb9a0 Guido Trotter
    # This gets called when the config file is modified. Note that this doesn't
161 b84cb9a0 Guido Trotter
    # usually happen in Ganeti, as the config file is normally replaced by a
162 b84cb9a0 Guido Trotter
    # new one, at filesystem level, rather than actually modified (see
163 b84cb9a0 Guido Trotter
    # utils.WriteFile)
164 07b8a2b5 Iustin Pop
    logging.debug("Received 'modify' inotify event for %s", event.path)
165 b84cb9a0 Guido Trotter
166 b84cb9a0 Guido Trotter
    try:
167 4afe249b Guido Trotter
      self.callback(True)
168 b84cb9a0 Guido Trotter
    except errors.ConfdFatalError, err:
169 07b8a2b5 Iustin Pop
      logging.critical("Critical error, shutting down: %s", err)
170 b84cb9a0 Guido Trotter
      sys.exit(constants.EXIT_FAILURE)
171 b84cb9a0 Guido Trotter
    except:
172 b84cb9a0 Guido Trotter
      # we need to catch any exception here, log it, but proceed, because even
173 b84cb9a0 Guido Trotter
      # if we failed handling a single request, we still want the confd to
174 b84cb9a0 Guido Trotter
      # continue working.
175 b84cb9a0 Guido Trotter
      logging.error("Unexpected exception", exc_info=True)
176 b84cb9a0 Guido Trotter
177 b84cb9a0 Guido Trotter
  def process_default(self, event):
178 07b8a2b5 Iustin Pop
    logging.error("Received unhandled inotify event: %s", event)
179 b84cb9a0 Guido Trotter
180 b84cb9a0 Guido Trotter
181 562bee4d Guido Trotter
class ConfdConfigurationReloader(object):
182 562bee4d Guido Trotter
  """Logic to control when to reload the ganeti configuration
183 562bee4d Guido Trotter
184 562bee4d Guido Trotter
  This class is able to alter between inotify and polling, to rate-limit the
185 562bee4d Guido Trotter
  number of reloads. When using inotify it also supports a fallback timed
186 562bee4d Guido Trotter
  check, to verify that the reload hasn't failed.
187 562bee4d Guido Trotter
188 562bee4d Guido Trotter
  """
189 05f1ebf3 Guido Trotter
  def __init__(self, processor, mainloop):
190 562bee4d Guido Trotter
    """Constructor for ConfdConfigurationReloader
191 562bee4d Guido Trotter
192 05f1ebf3 Guido Trotter
    @type processor: L{confd.server.ConfdProcessor}
193 05f1ebf3 Guido Trotter
    @param processor: ganeti-confd ConfdProcessor
194 e2be81cf Guido Trotter
    @type mainloop: L{daemon.Mainloop}
195 e2be81cf Guido Trotter
    @param mainloop: ganeti-confd mainloop
196 562bee4d Guido Trotter
197 562bee4d Guido Trotter
    """
198 05f1ebf3 Guido Trotter
    self.processor = processor
199 e2be81cf Guido Trotter
    self.mainloop = mainloop
200 e2be81cf Guido Trotter
201 c6259dbc Guido Trotter
    self.polling = True
202 e2be81cf Guido Trotter
    self.last_notification = 0
203 562bee4d Guido Trotter
204 562bee4d Guido Trotter
    # Asyncronous inotify handler for config changes
205 562bee4d Guido Trotter
    self.wm = pyinotify.WatchManager()
206 4afe249b Guido Trotter
    self.inotify_handler = ConfdInotifyEventHandler(self.wm, self.OnInotify)
207 e1081705 Guido Trotter
    self.notifier = asyncnotifier.AsyncNotifier(self.wm, self.inotify_handler)
208 4afe249b Guido Trotter
209 e2be81cf Guido Trotter
    self.timer_handle = None
210 e2be81cf Guido Trotter
    self._EnableTimer()
211 e2be81cf Guido Trotter
212 4afe249b Guido Trotter
  def OnInotify(self, notifier_enabled):
213 4afe249b Guido Trotter
    """Receive an inotify notification.
214 4afe249b Guido Trotter
215 4afe249b Guido Trotter
    @type notifier_enabled: boolean
216 4afe249b Guido Trotter
    @param notifier_enabled: whether the notifier is still enabled
217 4afe249b Guido Trotter
218 4afe249b Guido Trotter
    """
219 e2be81cf Guido Trotter
    current_time = time.time()
220 e2be81cf Guido Trotter
    time_delta = current_time - self.last_notification
221 e2be81cf Guido Trotter
    self.last_notification = current_time
222 e2be81cf Guido Trotter
223 e2be81cf Guido Trotter
    if time_delta < constants.CONFD_CONFIG_RELOAD_RATELIMIT:
224 e2be81cf Guido Trotter
      logging.debug("Moving from inotify mode to polling mode")
225 e2be81cf Guido Trotter
      self.polling = True
226 e2be81cf Guido Trotter
      if notifier_enabled:
227 176d3122 Guido Trotter
        self.inotify_handler.disable()
228 e2be81cf Guido Trotter
229 e2be81cf Guido Trotter
    if not self.polling and not notifier_enabled:
230 ef4ca33b Guido Trotter
      try:
231 ef4ca33b Guido Trotter
        self.inotify_handler.enable()
232 ef4ca33b Guido Trotter
      except errors.InotifyError:
233 22d3e184 Guido Trotter
        self.polling = True
234 4afe249b Guido Trotter
235 4afe249b Guido Trotter
    try:
236 05f1ebf3 Guido Trotter
      reloaded = self.processor.reader.Reload()
237 4afe249b Guido Trotter
      if reloaded:
238 4afe249b Guido Trotter
        logging.info("Reloaded ganeti config")
239 4afe249b Guido Trotter
      else:
240 4afe249b Guido Trotter
        logging.debug("Skipped double config reload")
241 4afe249b Guido Trotter
    except errors.ConfigurationError:
242 22d3e184 Guido Trotter
      self.DisableConfd()
243 22d3e184 Guido Trotter
      self.inotify_handler.disable()
244 22d3e184 Guido Trotter
      return
245 4afe249b Guido Trotter
246 e2be81cf Guido Trotter
    # Reset the timer. If we're polling it will go to the polling rate, if
247 e2be81cf Guido Trotter
    # we're not it will delay it again to its base safe timeout.
248 22d3e184 Guido Trotter
    self._ResetTimer()
249 e2be81cf Guido Trotter
250 e2be81cf Guido Trotter
  def _DisableTimer(self):
251 e2be81cf Guido Trotter
    if self.timer_handle is not None:
252 e2be81cf Guido Trotter
      self.mainloop.scheduler.cancel(self.timer_handle)
253 e2be81cf Guido Trotter
      self.timer_handle = None
254 e2be81cf Guido Trotter
255 e2be81cf Guido Trotter
  def _EnableTimer(self):
256 e2be81cf Guido Trotter
    if self.polling:
257 e2be81cf Guido Trotter
      timeout = constants.CONFD_CONFIG_RELOAD_RATELIMIT
258 e2be81cf Guido Trotter
    else:
259 e2be81cf Guido Trotter
      timeout = constants.CONFD_CONFIG_RELOAD_TIMEOUT
260 e2be81cf Guido Trotter
261 e2be81cf Guido Trotter
    if self.timer_handle is None:
262 e2be81cf Guido Trotter
      self.timer_handle = self.mainloop.scheduler.enter(
263 e2be81cf Guido Trotter
        timeout, 1, self.OnTimer, [])
264 e2be81cf Guido Trotter
265 22d3e184 Guido Trotter
  def _ResetTimer(self):
266 22d3e184 Guido Trotter
    self._DisableTimer()
267 22d3e184 Guido Trotter
    self._EnableTimer()
268 22d3e184 Guido Trotter
269 e2be81cf Guido Trotter
  def OnTimer(self):
270 e2be81cf Guido Trotter
    """Function called when the timer fires
271 e2be81cf Guido Trotter
272 e2be81cf Guido Trotter
    """
273 e2be81cf Guido Trotter
    self.timer_handle = None
274 22d3e184 Guido Trotter
    reloaded = False
275 22d3e184 Guido Trotter
    was_disabled = False
276 e2be81cf Guido Trotter
    try:
277 22d3e184 Guido Trotter
      if self.processor.reader is None:
278 22d3e184 Guido Trotter
        was_disabled = True
279 22d3e184 Guido Trotter
        self.EnableConfd()
280 22d3e184 Guido Trotter
        reloaded = True
281 22d3e184 Guido Trotter
      else:
282 22d3e184 Guido Trotter
        reloaded = self.processor.reader.Reload()
283 e2be81cf Guido Trotter
    except errors.ConfigurationError:
284 a544f755 Guido Trotter
      self.DisableConfd(silent=was_disabled)
285 22d3e184 Guido Trotter
      return
286 e2be81cf Guido Trotter
287 e2be81cf Guido Trotter
    if self.polling and reloaded:
288 e2be81cf Guido Trotter
      logging.info("Reloaded ganeti config")
289 e2be81cf Guido Trotter
    elif reloaded:
290 e2be81cf Guido Trotter
      # We have reloaded the config files, but received no inotify event.  If
291 e2be81cf Guido Trotter
      # an event is pending though, we just happen to have timed out before
292 e2be81cf Guido Trotter
      # receiving it, so this is not a problem, and we shouldn't alert
293 22d3e184 Guido Trotter
      if not self.notifier.check_events() and not was_disabled:
294 e2be81cf Guido Trotter
        logging.warning("Config file reload at timeout (inotify failure)")
295 e2be81cf Guido Trotter
    elif self.polling:
296 e2be81cf Guido Trotter
      # We're polling, but we haven't reloaded the config:
297 e2be81cf Guido Trotter
      # Going back to inotify mode
298 e2be81cf Guido Trotter
      logging.debug("Moving from polling mode to inotify mode")
299 e2be81cf Guido Trotter
      self.polling = False
300 22d3e184 Guido Trotter
      try:
301 22d3e184 Guido Trotter
        self.inotify_handler.enable()
302 22d3e184 Guido Trotter
      except errors.InotifyError:
303 22d3e184 Guido Trotter
        self.polling = True
304 e2be81cf Guido Trotter
    else:
305 e2be81cf Guido Trotter
      logging.debug("Performed configuration check")
306 e2be81cf Guido Trotter
307 e2be81cf Guido Trotter
    self._EnableTimer()
308 562bee4d Guido Trotter
309 a544f755 Guido Trotter
  def DisableConfd(self, silent=False):
310 22d3e184 Guido Trotter
    """Puts confd in non-serving mode
311 22d3e184 Guido Trotter
312 22d3e184 Guido Trotter
    """
313 a544f755 Guido Trotter
    if not silent:
314 a544f755 Guido Trotter
      logging.warning("Confd is being disabled")
315 22d3e184 Guido Trotter
    self.processor.Disable()
316 22d3e184 Guido Trotter
    self.polling = False
317 22d3e184 Guido Trotter
    self._ResetTimer()
318 22d3e184 Guido Trotter
319 22d3e184 Guido Trotter
  def EnableConfd(self):
320 22d3e184 Guido Trotter
    self.processor.Enable()
321 22d3e184 Guido Trotter
    logging.warning("Confd is being enabled")
322 22d3e184 Guido Trotter
    self.polling = True
323 22d3e184 Guido Trotter
    self._ResetTimer()
324 22d3e184 Guido Trotter
325 562bee4d Guido Trotter
326 2d54e29c Iustin Pop
def CheckConfd(_, args):
327 6c948699 Michael Hanselmann
  """Initial checks whether to run exit with a failure.
328 b84cb9a0 Guido Trotter
329 b84cb9a0 Guido Trotter
  """
330 f93427cd Iustin Pop
  if args: # confd doesn't take any arguments
331 f93427cd Iustin Pop
    print >> sys.stderr, ("Usage: %s [-f] [-d] [-b ADDRESS]" % sys.argv[0])
332 f93427cd Iustin Pop
    sys.exit(constants.EXIT_FAILURE)
333 f93427cd Iustin Pop
334 b84cb9a0 Guido Trotter
  # TODO: collapse HMAC daemons handling in daemons GenericMain, when we'll
335 b84cb9a0 Guido Trotter
  # have more than one.
336 6b7d5878 Michael Hanselmann
  if not os.path.isfile(constants.CONFD_HMAC_KEY):
337 6b7d5878 Michael Hanselmann
    print >> sys.stderr, "Need HMAC key %s to run" % constants.CONFD_HMAC_KEY
338 b84cb9a0 Guido Trotter
    sys.exit(constants.EXIT_FAILURE)
339 b84cb9a0 Guido Trotter
340 b84cb9a0 Guido Trotter
341 2d54e29c Iustin Pop
def ExecConfd(options, _):
342 6c948699 Michael Hanselmann
  """Main confd function, executed with PID file held
343 b84cb9a0 Guido Trotter
344 b84cb9a0 Guido Trotter
  """
345 c9ca81c9 Iustin Pop
  # TODO: clarify how the server and reloader variables work (they are
346 c9ca81c9 Iustin Pop
  # not used)
347 c9ca81c9 Iustin Pop
  # pylint: disable-msg=W0612
348 f91c7223 Guido Trotter
  mainloop = daemon.Mainloop()
349 f91c7223 Guido Trotter
350 b84cb9a0 Guido Trotter
  # Asyncronous confd UDP server
351 e1081705 Guido Trotter
  processor = confd_server.ConfdProcessor()
352 e369f21d Guido Trotter
  try:
353 e369f21d Guido Trotter
    processor.Enable()
354 e369f21d Guido Trotter
  except errors.ConfigurationError:
355 4d4a651d Michael Hanselmann
    # If enabling the processor has failed, we can still go on, but confd will
356 4d4a651d Michael Hanselmann
    # be disabled
357 a544f755 Guido Trotter
    logging.warning("Confd is starting in disabled mode")
358 2d54e29c Iustin Pop
359 b84cb9a0 Guido Trotter
  server = ConfdAsyncUDPServer(options.bind_address, options.port, processor)
360 b84cb9a0 Guido Trotter
361 562bee4d Guido Trotter
  # Configuration reloader
362 05f1ebf3 Guido Trotter
  reloader = ConfdConfigurationReloader(processor, mainloop)
363 f91c7223 Guido Trotter
364 f91c7223 Guido Trotter
  mainloop.Run()
365 b84cb9a0 Guido Trotter
366 b84cb9a0 Guido Trotter
367 b84cb9a0 Guido Trotter
def main():
368 b84cb9a0 Guido Trotter
  """Main function for the confd daemon.
369 b84cb9a0 Guido Trotter
370 b84cb9a0 Guido Trotter
  """
371 b84cb9a0 Guido Trotter
  parser = OptionParser(description="Ganeti configuration daemon",
372 b84cb9a0 Guido Trotter
                        usage="%prog [-f] [-d] [-b ADDRESS]",
373 b84cb9a0 Guido Trotter
                        version="%%prog (ganeti) %s" %
374 b84cb9a0 Guido Trotter
                        constants.RELEASE_VERSION)
375 b84cb9a0 Guido Trotter
376 b84cb9a0 Guido Trotter
  dirs = [(val, constants.RUN_DIRS_MODE) for val in constants.SUB_RUN_DIRS]
377 b84cb9a0 Guido Trotter
  dirs.append((constants.LOCK_DIR, 1777))
378 6c948699 Michael Hanselmann
  daemon.GenericMain(constants.CONFD, parser, dirs, CheckConfd, ExecConfd)
379 b84cb9a0 Guido Trotter
380 b84cb9a0 Guido Trotter
381 6c948699 Michael Hanselmann
if __name__ == "__main__":
382 b84cb9a0 Guido Trotter
  main()