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