Statistics
| Branch: | Tag: | Revision:

root / daemons / ganeti-confd @ 7699c3af

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 b84cb9a0 Guido Trotter
    @type watch_manager: L{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 b84cb9a0 Guido Trotter
    # no need to call the parent's constructor
106 b84cb9a0 Guido Trotter
    self.watch_manager = watch_manager
107 4afe249b Guido Trotter
    self.callback = callback
108 7260cfbe Iustin Pop
    # pylint: disable-msg=E1103
109 7260cfbe Iustin Pop
    # pylint for some reason doesn't see the below constants
110 b84cb9a0 Guido Trotter
    self.mask = pyinotify.EventsCodes.IN_IGNORED | \
111 b84cb9a0 Guido Trotter
                pyinotify.EventsCodes.IN_MODIFY
112 e2e10467 Iustin Pop
    self.file = filename
113 46c9b31d Guido Trotter
    self.watch_handle = None
114 b84cb9a0 Guido Trotter
115 46c9b31d Guido Trotter
  def enable(self):
116 46c9b31d Guido Trotter
    """Watch the given file
117 b84cb9a0 Guido Trotter
118 b84cb9a0 Guido Trotter
    """
119 46c9b31d Guido Trotter
    if self.watch_handle is None:
120 46c9b31d Guido Trotter
      result = self.watch_manager.add_watch(self.file, self.mask)
121 46c9b31d Guido Trotter
      if not self.file in result or result[self.file] <= 0:
122 ef4ca33b Guido Trotter
        raise errors.InotifyError("Could not add inotify watcher")
123 46c9b31d Guido Trotter
      else:
124 46c9b31d Guido Trotter
        self.watch_handle = result[self.file]
125 46c9b31d Guido Trotter
126 46c9b31d Guido Trotter
  def disable(self):
127 46c9b31d Guido Trotter
    """Stop watching the given file
128 46c9b31d Guido Trotter
129 46c9b31d Guido Trotter
    """
130 46c9b31d Guido Trotter
    if self.watch_handle is not None:
131 46c9b31d Guido Trotter
      result = self.watch_manager.rm_watch(self.watch_handle)
132 46c9b31d Guido Trotter
      if result[self.watch_handle]:
133 46c9b31d Guido Trotter
        self.watch_handle = None
134 b84cb9a0 Guido Trotter
135 b84cb9a0 Guido Trotter
  def process_IN_IGNORED(self, event):
136 b84cb9a0 Guido Trotter
    # Due to the fact that we monitor just for the cluster config file (rather
137 b84cb9a0 Guido Trotter
    # than for the whole data dir) when the file is replaced with another one
138 b84cb9a0 Guido Trotter
    # (which is what happens normally in ganeti) we're going to receive an
139 b84cb9a0 Guido Trotter
    # IN_IGNORED event from inotify, because of the file removal (which is
140 b84cb9a0 Guido Trotter
    # contextual with the replacement). In such a case we need to create
141 b84cb9a0 Guido Trotter
    # another watcher for the "new" file.
142 07b8a2b5 Iustin Pop
    logging.debug("Received 'ignored' inotify event for %s", event.path)
143 46c9b31d Guido Trotter
    self.watch_handle = None
144 b84cb9a0 Guido Trotter
145 b84cb9a0 Guido Trotter
    try:
146 b84cb9a0 Guido Trotter
      # Since the kernel believes the file we were interested in is gone, it's
147 b84cb9a0 Guido Trotter
      # not going to notify us of any other events, until we set up, here, the
148 b84cb9a0 Guido Trotter
      # new watch. This is not a race condition, though, since we're anyway
149 b84cb9a0 Guido Trotter
      # going to realod the file after setting up the new watch.
150 4afe249b Guido Trotter
      self.callback(False)
151 b84cb9a0 Guido Trotter
    except errors.ConfdFatalError, err:
152 07b8a2b5 Iustin Pop
      logging.critical("Critical error, shutting down: %s", err)
153 b84cb9a0 Guido Trotter
      sys.exit(constants.EXIT_FAILURE)
154 b84cb9a0 Guido Trotter
    except:
155 b84cb9a0 Guido Trotter
      # we need to catch any exception here, log it, but proceed, because even
156 b84cb9a0 Guido Trotter
      # if we failed handling a single request, we still want the confd to
157 b84cb9a0 Guido Trotter
      # continue working.
158 b84cb9a0 Guido Trotter
      logging.error("Unexpected exception", exc_info=True)
159 b84cb9a0 Guido Trotter
160 b84cb9a0 Guido Trotter
  def process_IN_MODIFY(self, event):
161 b84cb9a0 Guido Trotter
    # This gets called when the config file is modified. Note that this doesn't
162 b84cb9a0 Guido Trotter
    # usually happen in Ganeti, as the config file is normally replaced by a
163 b84cb9a0 Guido Trotter
    # new one, at filesystem level, rather than actually modified (see
164 b84cb9a0 Guido Trotter
    # utils.WriteFile)
165 07b8a2b5 Iustin Pop
    logging.debug("Received 'modify' inotify event for %s", event.path)
166 b84cb9a0 Guido Trotter
167 b84cb9a0 Guido Trotter
    try:
168 4afe249b Guido Trotter
      self.callback(True)
169 b84cb9a0 Guido Trotter
    except errors.ConfdFatalError, err:
170 07b8a2b5 Iustin Pop
      logging.critical("Critical error, shutting down: %s", err)
171 b84cb9a0 Guido Trotter
      sys.exit(constants.EXIT_FAILURE)
172 b84cb9a0 Guido Trotter
    except:
173 b84cb9a0 Guido Trotter
      # we need to catch any exception here, log it, but proceed, because even
174 b84cb9a0 Guido Trotter
      # if we failed handling a single request, we still want the confd to
175 b84cb9a0 Guido Trotter
      # continue working.
176 b84cb9a0 Guido Trotter
      logging.error("Unexpected exception", exc_info=True)
177 b84cb9a0 Guido Trotter
178 b84cb9a0 Guido Trotter
  def process_default(self, event):
179 07b8a2b5 Iustin Pop
    logging.error("Received unhandled inotify event: %s", event)
180 b84cb9a0 Guido Trotter
181 b84cb9a0 Guido Trotter
182 562bee4d Guido Trotter
class ConfdConfigurationReloader(object):
183 562bee4d Guido Trotter
  """Logic to control when to reload the ganeti configuration
184 562bee4d Guido Trotter
185 562bee4d Guido Trotter
  This class is able to alter between inotify and polling, to rate-limit the
186 562bee4d Guido Trotter
  number of reloads. When using inotify it also supports a fallback timed
187 562bee4d Guido Trotter
  check, to verify that the reload hasn't failed.
188 562bee4d Guido Trotter
189 562bee4d Guido Trotter
  """
190 05f1ebf3 Guido Trotter
  def __init__(self, processor, mainloop):
191 562bee4d Guido Trotter
    """Constructor for ConfdConfigurationReloader
192 562bee4d Guido Trotter
193 05f1ebf3 Guido Trotter
    @type processor: L{confd.server.ConfdProcessor}
194 05f1ebf3 Guido Trotter
    @param processor: ganeti-confd ConfdProcessor
195 e2be81cf Guido Trotter
    @type mainloop: L{daemon.Mainloop}
196 e2be81cf Guido Trotter
    @param mainloop: ganeti-confd mainloop
197 562bee4d Guido Trotter
198 562bee4d Guido Trotter
    """
199 05f1ebf3 Guido Trotter
    self.processor = processor
200 e2be81cf Guido Trotter
    self.mainloop = mainloop
201 e2be81cf Guido Trotter
202 c6259dbc Guido Trotter
    self.polling = True
203 e2be81cf Guido Trotter
    self.last_notification = 0
204 562bee4d Guido Trotter
205 562bee4d Guido Trotter
    # Asyncronous inotify handler for config changes
206 562bee4d Guido Trotter
    self.wm = pyinotify.WatchManager()
207 4afe249b Guido Trotter
    self.inotify_handler = ConfdInotifyEventHandler(self.wm, self.OnInotify)
208 e1081705 Guido Trotter
    self.notifier = asyncnotifier.AsyncNotifier(self.wm, self.inotify_handler)
209 4afe249b Guido Trotter
210 e2be81cf Guido Trotter
    self.timer_handle = None
211 e2be81cf Guido Trotter
    self._EnableTimer()
212 e2be81cf Guido Trotter
213 4afe249b Guido Trotter
  def OnInotify(self, notifier_enabled):
214 4afe249b Guido Trotter
    """Receive an inotify notification.
215 4afe249b Guido Trotter
216 4afe249b Guido Trotter
    @type notifier_enabled: boolean
217 4afe249b Guido Trotter
    @param notifier_enabled: whether the notifier is still enabled
218 4afe249b Guido Trotter
219 4afe249b Guido Trotter
    """
220 e2be81cf Guido Trotter
    current_time = time.time()
221 e2be81cf Guido Trotter
    time_delta = current_time - self.last_notification
222 e2be81cf Guido Trotter
    self.last_notification = current_time
223 e2be81cf Guido Trotter
224 e2be81cf Guido Trotter
    if time_delta < constants.CONFD_CONFIG_RELOAD_RATELIMIT:
225 e2be81cf Guido Trotter
      logging.debug("Moving from inotify mode to polling mode")
226 e2be81cf Guido Trotter
      self.polling = True
227 e2be81cf Guido Trotter
      if notifier_enabled:
228 176d3122 Guido Trotter
        self.inotify_handler.disable()
229 e2be81cf Guido Trotter
230 e2be81cf Guido Trotter
    if not self.polling and not notifier_enabled:
231 ef4ca33b Guido Trotter
      try:
232 ef4ca33b Guido Trotter
        self.inotify_handler.enable()
233 ef4ca33b Guido Trotter
      except errors.InotifyError:
234 22d3e184 Guido Trotter
        self.polling = True
235 4afe249b Guido Trotter
236 4afe249b Guido Trotter
    try:
237 05f1ebf3 Guido Trotter
      reloaded = self.processor.reader.Reload()
238 4afe249b Guido Trotter
      if reloaded:
239 4afe249b Guido Trotter
        logging.info("Reloaded ganeti config")
240 4afe249b Guido Trotter
      else:
241 4afe249b Guido Trotter
        logging.debug("Skipped double config reload")
242 4afe249b Guido Trotter
    except errors.ConfigurationError:
243 22d3e184 Guido Trotter
      self.DisableConfd()
244 22d3e184 Guido Trotter
      self.inotify_handler.disable()
245 22d3e184 Guido Trotter
      return
246 4afe249b Guido Trotter
247 e2be81cf Guido Trotter
    # Reset the timer. If we're polling it will go to the polling rate, if
248 e2be81cf Guido Trotter
    # we're not it will delay it again to its base safe timeout.
249 22d3e184 Guido Trotter
    self._ResetTimer()
250 e2be81cf Guido Trotter
251 e2be81cf Guido Trotter
  def _DisableTimer(self):
252 e2be81cf Guido Trotter
    if self.timer_handle is not None:
253 e2be81cf Guido Trotter
      self.mainloop.scheduler.cancel(self.timer_handle)
254 e2be81cf Guido Trotter
      self.timer_handle = None
255 e2be81cf Guido Trotter
256 e2be81cf Guido Trotter
  def _EnableTimer(self):
257 e2be81cf Guido Trotter
    if self.polling:
258 e2be81cf Guido Trotter
      timeout = constants.CONFD_CONFIG_RELOAD_RATELIMIT
259 e2be81cf Guido Trotter
    else:
260 e2be81cf Guido Trotter
      timeout = constants.CONFD_CONFIG_RELOAD_TIMEOUT
261 e2be81cf Guido Trotter
262 e2be81cf Guido Trotter
    if self.timer_handle is None:
263 e2be81cf Guido Trotter
      self.timer_handle = self.mainloop.scheduler.enter(
264 e2be81cf Guido Trotter
        timeout, 1, self.OnTimer, [])
265 e2be81cf Guido Trotter
266 22d3e184 Guido Trotter
  def _ResetTimer(self):
267 22d3e184 Guido Trotter
    self._DisableTimer()
268 22d3e184 Guido Trotter
    self._EnableTimer()
269 22d3e184 Guido Trotter
270 e2be81cf Guido Trotter
  def OnTimer(self):
271 e2be81cf Guido Trotter
    """Function called when the timer fires
272 e2be81cf Guido Trotter
273 e2be81cf Guido Trotter
    """
274 e2be81cf Guido Trotter
    self.timer_handle = None
275 22d3e184 Guido Trotter
    reloaded = False
276 22d3e184 Guido Trotter
    was_disabled = False
277 e2be81cf Guido Trotter
    try:
278 22d3e184 Guido Trotter
      if self.processor.reader is None:
279 22d3e184 Guido Trotter
        was_disabled = True
280 22d3e184 Guido Trotter
        self.EnableConfd()
281 22d3e184 Guido Trotter
        reloaded = True
282 22d3e184 Guido Trotter
      else:
283 22d3e184 Guido Trotter
        reloaded = self.processor.reader.Reload()
284 e2be81cf Guido Trotter
    except errors.ConfigurationError:
285 a544f755 Guido Trotter
      self.DisableConfd(silent=was_disabled)
286 22d3e184 Guido Trotter
      return
287 e2be81cf Guido Trotter
288 e2be81cf Guido Trotter
    if self.polling and reloaded:
289 e2be81cf Guido Trotter
      logging.info("Reloaded ganeti config")
290 e2be81cf Guido Trotter
    elif reloaded:
291 e2be81cf Guido Trotter
      # We have reloaded the config files, but received no inotify event.  If
292 e2be81cf Guido Trotter
      # an event is pending though, we just happen to have timed out before
293 e2be81cf Guido Trotter
      # receiving it, so this is not a problem, and we shouldn't alert
294 22d3e184 Guido Trotter
      if not self.notifier.check_events() and not was_disabled:
295 e2be81cf Guido Trotter
        logging.warning("Config file reload at timeout (inotify failure)")
296 e2be81cf Guido Trotter
    elif self.polling:
297 e2be81cf Guido Trotter
      # We're polling, but we haven't reloaded the config:
298 e2be81cf Guido Trotter
      # Going back to inotify mode
299 e2be81cf Guido Trotter
      logging.debug("Moving from polling mode to inotify mode")
300 e2be81cf Guido Trotter
      self.polling = False
301 22d3e184 Guido Trotter
      try:
302 22d3e184 Guido Trotter
        self.inotify_handler.enable()
303 22d3e184 Guido Trotter
      except errors.InotifyError:
304 22d3e184 Guido Trotter
        self.polling = True
305 e2be81cf Guido Trotter
    else:
306 e2be81cf Guido Trotter
      logging.debug("Performed configuration check")
307 e2be81cf Guido Trotter
308 e2be81cf Guido Trotter
    self._EnableTimer()
309 562bee4d Guido Trotter
310 a544f755 Guido Trotter
  def DisableConfd(self, silent=False):
311 22d3e184 Guido Trotter
    """Puts confd in non-serving mode
312 22d3e184 Guido Trotter
313 22d3e184 Guido Trotter
    """
314 a544f755 Guido Trotter
    if not silent:
315 a544f755 Guido Trotter
      logging.warning("Confd is being disabled")
316 22d3e184 Guido Trotter
    self.processor.Disable()
317 22d3e184 Guido Trotter
    self.polling = False
318 22d3e184 Guido Trotter
    self._ResetTimer()
319 22d3e184 Guido Trotter
320 22d3e184 Guido Trotter
  def EnableConfd(self):
321 22d3e184 Guido Trotter
    self.processor.Enable()
322 22d3e184 Guido Trotter
    logging.warning("Confd is being enabled")
323 22d3e184 Guido Trotter
    self.polling = True
324 22d3e184 Guido Trotter
    self._ResetTimer()
325 22d3e184 Guido Trotter
326 562bee4d Guido Trotter
327 2d54e29c Iustin Pop
def CheckConfd(_, args):
328 6c948699 Michael Hanselmann
  """Initial checks whether to run exit with a failure.
329 b84cb9a0 Guido Trotter
330 b84cb9a0 Guido Trotter
  """
331 f93427cd Iustin Pop
  if args: # confd doesn't take any arguments
332 f93427cd Iustin Pop
    print >> sys.stderr, ("Usage: %s [-f] [-d] [-b ADDRESS]" % sys.argv[0])
333 f93427cd Iustin Pop
    sys.exit(constants.EXIT_FAILURE)
334 f93427cd Iustin Pop
335 b84cb9a0 Guido Trotter
  # TODO: collapse HMAC daemons handling in daemons GenericMain, when we'll
336 b84cb9a0 Guido Trotter
  # have more than one.
337 b84cb9a0 Guido Trotter
  if not os.path.isfile(constants.HMAC_CLUSTER_KEY):
338 b84cb9a0 Guido Trotter
    print >> sys.stderr, "Need HMAC key %s to run" % constants.HMAC_CLUSTER_KEY
339 b84cb9a0 Guido Trotter
    sys.exit(constants.EXIT_FAILURE)
340 b84cb9a0 Guido Trotter
341 b84cb9a0 Guido Trotter
342 2d54e29c Iustin Pop
def ExecConfd(options, _):
343 6c948699 Michael Hanselmann
  """Main confd function, executed with PID file held
344 b84cb9a0 Guido Trotter
345 b84cb9a0 Guido Trotter
  """
346 c9ca81c9 Iustin Pop
  # TODO: clarify how the server and reloader variables work (they are
347 c9ca81c9 Iustin Pop
  # not used)
348 c9ca81c9 Iustin Pop
  # pylint: disable-msg=W0612
349 f91c7223 Guido Trotter
  mainloop = daemon.Mainloop()
350 f91c7223 Guido Trotter
351 b84cb9a0 Guido Trotter
  # Asyncronous confd UDP server
352 e1081705 Guido Trotter
  processor = confd_server.ConfdProcessor()
353 e369f21d Guido Trotter
  try:
354 e369f21d Guido Trotter
    processor.Enable()
355 e369f21d Guido Trotter
  except errors.ConfigurationError:
356 4d4a651d Michael Hanselmann
    # If enabling the processor has failed, we can still go on, but confd will
357 4d4a651d Michael Hanselmann
    # be disabled
358 a544f755 Guido Trotter
    logging.warning("Confd is starting in disabled mode")
359 2d54e29c Iustin Pop
360 b84cb9a0 Guido Trotter
  server = ConfdAsyncUDPServer(options.bind_address, options.port, processor)
361 b84cb9a0 Guido Trotter
362 562bee4d Guido Trotter
  # Configuration reloader
363 05f1ebf3 Guido Trotter
  reloader = ConfdConfigurationReloader(processor, mainloop)
364 f91c7223 Guido Trotter
365 f91c7223 Guido Trotter
  mainloop.Run()
366 b84cb9a0 Guido Trotter
367 b84cb9a0 Guido Trotter
368 b84cb9a0 Guido Trotter
def main():
369 b84cb9a0 Guido Trotter
  """Main function for the confd daemon.
370 b84cb9a0 Guido Trotter
371 b84cb9a0 Guido Trotter
  """
372 b84cb9a0 Guido Trotter
  parser = OptionParser(description="Ganeti configuration daemon",
373 b84cb9a0 Guido Trotter
                        usage="%prog [-f] [-d] [-b ADDRESS]",
374 b84cb9a0 Guido Trotter
                        version="%%prog (ganeti) %s" %
375 b84cb9a0 Guido Trotter
                        constants.RELEASE_VERSION)
376 b84cb9a0 Guido Trotter
377 b84cb9a0 Guido Trotter
  dirs = [(val, constants.RUN_DIRS_MODE) for val in constants.SUB_RUN_DIRS]
378 b84cb9a0 Guido Trotter
  dirs.append((constants.LOCK_DIR, 1777))
379 6c948699 Michael Hanselmann
  daemon.GenericMain(constants.CONFD, parser, dirs, CheckConfd, ExecConfd)
380 b84cb9a0 Guido Trotter
381 b84cb9a0 Guido Trotter
382 6c948699 Michael Hanselmann
if __name__ == "__main__":
383 b84cb9a0 Guido Trotter
  main()