root / daemons / ganeti-masterd @ 72737a7f
History | View | Annotate | Download (12.7 kB)
1 | 685ee993 | Iustin Pop | #!/usr/bin/python -u |
---|---|---|---|
2 | ffeffa1d | Iustin Pop | # |
3 | ffeffa1d | Iustin Pop | |
4 | ffeffa1d | Iustin Pop | # Copyright (C) 2006, 2007 Google Inc. |
5 | ffeffa1d | Iustin Pop | # |
6 | ffeffa1d | Iustin Pop | # This program is free software; you can redistribute it and/or modify |
7 | ffeffa1d | Iustin Pop | # it under the terms of the GNU General Public License as published by |
8 | ffeffa1d | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or |
9 | ffeffa1d | Iustin Pop | # (at your option) any later version. |
10 | ffeffa1d | Iustin Pop | # |
11 | ffeffa1d | Iustin Pop | # This program is distributed in the hope that it will be useful, but |
12 | ffeffa1d | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | ffeffa1d | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | ffeffa1d | Iustin Pop | # General Public License for more details. |
15 | ffeffa1d | Iustin Pop | # |
16 | ffeffa1d | Iustin Pop | # You should have received a copy of the GNU General Public License |
17 | ffeffa1d | Iustin Pop | # along with this program; if not, write to the Free Software |
18 | ffeffa1d | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | ffeffa1d | Iustin Pop | # 02110-1301, USA. |
20 | ffeffa1d | Iustin Pop | |
21 | ffeffa1d | Iustin Pop | |
22 | ffeffa1d | Iustin Pop | """Master daemon program. |
23 | ffeffa1d | Iustin Pop | |
24 | ffeffa1d | Iustin Pop | Some classes deviates from the standard style guide since the |
25 | ffeffa1d | Iustin Pop | inheritance from parent classes requires it. |
26 | ffeffa1d | Iustin Pop | |
27 | ffeffa1d | Iustin Pop | """ |
28 | ffeffa1d | Iustin Pop | |
29 | ffeffa1d | Iustin Pop | |
30 | c1f2901b | Iustin Pop | import sys |
31 | ffeffa1d | Iustin Pop | import SocketServer |
32 | ffeffa1d | Iustin Pop | import time |
33 | ffeffa1d | Iustin Pop | import collections |
34 | ffeffa1d | Iustin Pop | import Queue |
35 | ffeffa1d | Iustin Pop | import random |
36 | ffeffa1d | Iustin Pop | import signal |
37 | ffeffa1d | Iustin Pop | import simplejson |
38 | 96cb3986 | Michael Hanselmann | import logging |
39 | ffeffa1d | Iustin Pop | |
40 | ffeffa1d | Iustin Pop | from cStringIO import StringIO |
41 | c1f2901b | Iustin Pop | from optparse import OptionParser |
42 | ffeffa1d | Iustin Pop | |
43 | 39dcf2ef | Guido Trotter | from ganeti import config |
44 | ffeffa1d | Iustin Pop | from ganeti import constants |
45 | ffeffa1d | Iustin Pop | from ganeti import mcpu |
46 | ffeffa1d | Iustin Pop | from ganeti import opcodes |
47 | ffeffa1d | Iustin Pop | from ganeti import jqueue |
48 | 39dcf2ef | Guido Trotter | from ganeti import locking |
49 | ffeffa1d | Iustin Pop | from ganeti import luxi |
50 | ffeffa1d | Iustin Pop | from ganeti import utils |
51 | c1f2901b | Iustin Pop | from ganeti import errors |
52 | c1f2901b | Iustin Pop | from ganeti import ssconf |
53 | 96cb3986 | Michael Hanselmann | from ganeti import logger |
54 | 23e50d39 | Michael Hanselmann | from ganeti import workerpool |
55 | b1b6ea87 | Iustin Pop | from ganeti import rpc |
56 | c1f2901b | Iustin Pop | |
57 | c1f2901b | Iustin Pop | |
58 | 23e50d39 | Michael Hanselmann | CLIENT_REQUEST_WORKERS = 16 |
59 | 23e50d39 | Michael Hanselmann | |
60 | c1f2901b | Iustin Pop | EXIT_NOTMASTER = constants.EXIT_NOTMASTER |
61 | c1f2901b | Iustin Pop | EXIT_NODESETUP_ERROR = constants.EXIT_NODESETUP_ERROR |
62 | ffeffa1d | Iustin Pop | |
63 | ffeffa1d | Iustin Pop | |
64 | 23e50d39 | Michael Hanselmann | class ClientRequestWorker(workerpool.BaseWorker): |
65 | 23e50d39 | Michael Hanselmann | def RunTask(self, server, request, client_address): |
66 | 23e50d39 | Michael Hanselmann | """Process the request. |
67 | 23e50d39 | Michael Hanselmann | |
68 | 23e50d39 | Michael Hanselmann | This is copied from the code in ThreadingMixIn. |
69 | 23e50d39 | Michael Hanselmann | |
70 | 23e50d39 | Michael Hanselmann | """ |
71 | 23e50d39 | Michael Hanselmann | try: |
72 | 23e50d39 | Michael Hanselmann | server.finish_request(request, client_address) |
73 | 23e50d39 | Michael Hanselmann | server.close_request(request) |
74 | 23e50d39 | Michael Hanselmann | except: |
75 | 23e50d39 | Michael Hanselmann | server.handle_error(request, client_address) |
76 | 23e50d39 | Michael Hanselmann | server.close_request(request) |
77 | 23e50d39 | Michael Hanselmann | |
78 | 23e50d39 | Michael Hanselmann | |
79 | ffeffa1d | Iustin Pop | class IOServer(SocketServer.UnixStreamServer): |
80 | ffeffa1d | Iustin Pop | """IO thread class. |
81 | ffeffa1d | Iustin Pop | |
82 | ffeffa1d | Iustin Pop | This class takes care of initializing the other threads, setting |
83 | ffeffa1d | Iustin Pop | signal handlers (which are processed only in this thread), and doing |
84 | ffeffa1d | Iustin Pop | cleanup at shutdown. |
85 | ffeffa1d | Iustin Pop | |
86 | ffeffa1d | Iustin Pop | """ |
87 | 9113300d | Michael Hanselmann | def __init__(self, address, rqhandler): |
88 | ce862cd5 | Guido Trotter | """IOServer constructor |
89 | ce862cd5 | Guido Trotter | |
90 | ce862cd5 | Guido Trotter | Args: |
91 | ce862cd5 | Guido Trotter | address: the address to bind this IOServer to |
92 | ce862cd5 | Guido Trotter | rqhandler: RequestHandler type object |
93 | ce862cd5 | Guido Trotter | |
94 | ce862cd5 | Guido Trotter | """ |
95 | ffeffa1d | Iustin Pop | SocketServer.UnixStreamServer.__init__(self, address, rqhandler) |
96 | 50a3fbb2 | Michael Hanselmann | |
97 | 50a3fbb2 | Michael Hanselmann | # We'll only start threads once we've forked. |
98 | 9113300d | Michael Hanselmann | self.context = None |
99 | 23e50d39 | Michael Hanselmann | self.request_workers = None |
100 | 50a3fbb2 | Michael Hanselmann | |
101 | 50a3fbb2 | Michael Hanselmann | def setup_queue(self): |
102 | 9113300d | Michael Hanselmann | self.context = GanetiContext() |
103 | 23e50d39 | Michael Hanselmann | self.request_workers = workerpool.WorkerPool(CLIENT_REQUEST_WORKERS, |
104 | 23e50d39 | Michael Hanselmann | ClientRequestWorker) |
105 | ffeffa1d | Iustin Pop | |
106 | ffeffa1d | Iustin Pop | def process_request(self, request, client_address): |
107 | 23e50d39 | Michael Hanselmann | """Add task to workerpool to process request. |
108 | ffeffa1d | Iustin Pop | |
109 | ffeffa1d | Iustin Pop | """ |
110 | 23e50d39 | Michael Hanselmann | self.request_workers.AddTask(self, request, client_address) |
111 | ffeffa1d | Iustin Pop | |
112 | ffeffa1d | Iustin Pop | def serve_forever(self): |
113 | ffeffa1d | Iustin Pop | """Handle one request at a time until told to quit.""" |
114 | 610bc9ee | Michael Hanselmann | sighandler = utils.SignalHandler([signal.SIGINT, signal.SIGTERM]) |
115 | 610bc9ee | Michael Hanselmann | try: |
116 | 610bc9ee | Michael Hanselmann | while not sighandler.called: |
117 | 610bc9ee | Michael Hanselmann | self.handle_request() |
118 | 610bc9ee | Michael Hanselmann | finally: |
119 | 610bc9ee | Michael Hanselmann | sighandler.Reset() |
120 | c1f2901b | Iustin Pop | |
121 | c1f2901b | Iustin Pop | def server_cleanup(self): |
122 | c1f2901b | Iustin Pop | """Cleanup the server. |
123 | c1f2901b | Iustin Pop | |
124 | c1f2901b | Iustin Pop | This involves shutting down the processor threads and the master |
125 | c1f2901b | Iustin Pop | socket. |
126 | c1f2901b | Iustin Pop | |
127 | c1f2901b | Iustin Pop | """ |
128 | 50a3fbb2 | Michael Hanselmann | try: |
129 | 50a3fbb2 | Michael Hanselmann | self.server_close() |
130 | 50a3fbb2 | Michael Hanselmann | finally: |
131 | 23e50d39 | Michael Hanselmann | if self.request_workers: |
132 | 36088c4c | Michael Hanselmann | self.request_workers.TerminateWorkers() |
133 | 9113300d | Michael Hanselmann | if self.context: |
134 | 9113300d | Michael Hanselmann | self.context.jobqueue.Shutdown() |
135 | ffeffa1d | Iustin Pop | |
136 | ffeffa1d | Iustin Pop | |
137 | ffeffa1d | Iustin Pop | class ClientRqHandler(SocketServer.BaseRequestHandler): |
138 | ffeffa1d | Iustin Pop | """Client handler""" |
139 | ffeffa1d | Iustin Pop | EOM = '\3' |
140 | ffeffa1d | Iustin Pop | READ_SIZE = 4096 |
141 | ffeffa1d | Iustin Pop | |
142 | ffeffa1d | Iustin Pop | def setup(self): |
143 | ffeffa1d | Iustin Pop | self._buffer = "" |
144 | ffeffa1d | Iustin Pop | self._msgs = collections.deque() |
145 | ffeffa1d | Iustin Pop | self._ops = ClientOps(self.server) |
146 | ffeffa1d | Iustin Pop | |
147 | ffeffa1d | Iustin Pop | def handle(self): |
148 | ffeffa1d | Iustin Pop | while True: |
149 | ffeffa1d | Iustin Pop | msg = self.read_message() |
150 | ffeffa1d | Iustin Pop | if msg is None: |
151 | 3d8548c4 | Michael Hanselmann | logging.info("client closed connection") |
152 | ffeffa1d | Iustin Pop | break |
153 | 3d8548c4 | Michael Hanselmann | |
154 | ffeffa1d | Iustin Pop | request = simplejson.loads(msg) |
155 | 3d8548c4 | Michael Hanselmann | logging.debug("request: %s", request) |
156 | ffeffa1d | Iustin Pop | if not isinstance(request, dict): |
157 | 3d8548c4 | Michael Hanselmann | logging.error("wrong request received: %s", msg) |
158 | ffeffa1d | Iustin Pop | break |
159 | 3d8548c4 | Michael Hanselmann | |
160 | 3d8548c4 | Michael Hanselmann | method = request.get(luxi.KEY_METHOD, None) |
161 | 3d8548c4 | Michael Hanselmann | args = request.get(luxi.KEY_ARGS, None) |
162 | 3d8548c4 | Michael Hanselmann | if method is None or args is None: |
163 | 3d8548c4 | Michael Hanselmann | logging.error("no method or args in request") |
164 | ffeffa1d | Iustin Pop | break |
165 | 3d8548c4 | Michael Hanselmann | |
166 | 3d8548c4 | Michael Hanselmann | success = False |
167 | 3d8548c4 | Michael Hanselmann | try: |
168 | 3d8548c4 | Michael Hanselmann | result = self._ops.handle_request(method, args) |
169 | 3d8548c4 | Michael Hanselmann | success = True |
170 | 3d8548c4 | Michael Hanselmann | except: |
171 | 3d8548c4 | Michael Hanselmann | logging.error("Unexpected exception", exc_info=True) |
172 | 3d8548c4 | Michael Hanselmann | err = sys.exc_info() |
173 | 3d8548c4 | Michael Hanselmann | result = "Caught exception: %s" % str(err[1]) |
174 | 3d8548c4 | Michael Hanselmann | |
175 | 3d8548c4 | Michael Hanselmann | response = { |
176 | 3d8548c4 | Michael Hanselmann | luxi.KEY_SUCCESS: success, |
177 | 3d8548c4 | Michael Hanselmann | luxi.KEY_RESULT: result, |
178 | 3d8548c4 | Michael Hanselmann | } |
179 | 3d8548c4 | Michael Hanselmann | logging.debug("response: %s", response) |
180 | 3d8548c4 | Michael Hanselmann | self.send_message(simplejson.dumps(response)) |
181 | ffeffa1d | Iustin Pop | |
182 | ffeffa1d | Iustin Pop | def read_message(self): |
183 | ffeffa1d | Iustin Pop | while not self._msgs: |
184 | ffeffa1d | Iustin Pop | data = self.request.recv(self.READ_SIZE) |
185 | ffeffa1d | Iustin Pop | if not data: |
186 | ffeffa1d | Iustin Pop | return None |
187 | ffeffa1d | Iustin Pop | new_msgs = (self._buffer + data).split(self.EOM) |
188 | ffeffa1d | Iustin Pop | self._buffer = new_msgs.pop() |
189 | ffeffa1d | Iustin Pop | self._msgs.extend(new_msgs) |
190 | ffeffa1d | Iustin Pop | return self._msgs.popleft() |
191 | ffeffa1d | Iustin Pop | |
192 | ffeffa1d | Iustin Pop | def send_message(self, msg): |
193 | ffeffa1d | Iustin Pop | #print "sending", msg |
194 | ffeffa1d | Iustin Pop | self.request.sendall(msg + self.EOM) |
195 | ffeffa1d | Iustin Pop | |
196 | ffeffa1d | Iustin Pop | |
197 | ffeffa1d | Iustin Pop | class ClientOps: |
198 | ffeffa1d | Iustin Pop | """Class holding high-level client operations.""" |
199 | ffeffa1d | Iustin Pop | def __init__(self, server): |
200 | ffeffa1d | Iustin Pop | self.server = server |
201 | ffeffa1d | Iustin Pop | |
202 | 0bbe448c | Michael Hanselmann | def handle_request(self, method, args): |
203 | 9113300d | Michael Hanselmann | queue = self.server.context.jobqueue |
204 | 0bbe448c | Michael Hanselmann | |
205 | 0bbe448c | Michael Hanselmann | # TODO: Parameter validation |
206 | 0bbe448c | Michael Hanselmann | |
207 | 0bbe448c | Michael Hanselmann | if method == luxi.REQ_SUBMIT_JOB: |
208 | 0bbe448c | Michael Hanselmann | ops = [opcodes.OpCode.LoadOpCode(state) for state in args] |
209 | 4c848b18 | Michael Hanselmann | return queue.SubmitJob(ops) |
210 | ffeffa1d | Iustin Pop | |
211 | 0bbe448c | Michael Hanselmann | elif method == luxi.REQ_CANCEL_JOB: |
212 | 3a2c7775 | Michael Hanselmann | job_id = args |
213 | 0bbe448c | Michael Hanselmann | return queue.CancelJob(job_id) |
214 | ffeffa1d | Iustin Pop | |
215 | 0bbe448c | Michael Hanselmann | elif method == luxi.REQ_ARCHIVE_JOB: |
216 | 3a2c7775 | Michael Hanselmann | job_id = args |
217 | 0bbe448c | Michael Hanselmann | return queue.ArchiveJob(job_id) |
218 | 0bbe448c | Michael Hanselmann | |
219 | 07cd723a | Iustin Pop | elif method == luxi.REQ_AUTOARCHIVE_JOBS: |
220 | 07cd723a | Iustin Pop | age = args |
221 | 07cd723a | Iustin Pop | return queue.AutoArchiveJobs(age) |
222 | 07cd723a | Iustin Pop | |
223 | dfe57c22 | Michael Hanselmann | elif method == luxi.REQ_WAIT_FOR_JOB_CHANGE: |
224 | 5c735209 | Iustin Pop | (job_id, fields, prev_job_info, prev_log_serial, timeout) = args |
225 | 6c5a7090 | Michael Hanselmann | return queue.WaitForJobChanges(job_id, fields, prev_job_info, |
226 | 5c735209 | Iustin Pop | prev_log_serial, timeout) |
227 | dfe57c22 | Michael Hanselmann | |
228 | 0bbe448c | Michael Hanselmann | elif method == luxi.REQ_QUERY_JOBS: |
229 | 0bbe448c | Michael Hanselmann | (job_ids, fields) = args |
230 | 0bbe448c | Michael Hanselmann | return queue.QueryJobs(job_ids, fields) |
231 | 0bbe448c | Michael Hanselmann | |
232 | ee6c7b94 | Michael Hanselmann | elif method == luxi.REQ_QUERY_INSTANCES: |
233 | ee6c7b94 | Michael Hanselmann | (names, fields) = args |
234 | ee6c7b94 | Michael Hanselmann | op = opcodes.OpQueryInstances(names=names, output_fields=fields) |
235 | ee6c7b94 | Michael Hanselmann | return self._Query(op) |
236 | ee6c7b94 | Michael Hanselmann | |
237 | 02f7fe54 | Michael Hanselmann | elif method == luxi.REQ_QUERY_NODES: |
238 | 02f7fe54 | Michael Hanselmann | (names, fields) = args |
239 | 02f7fe54 | Michael Hanselmann | op = opcodes.OpQueryNodes(names=names, output_fields=fields) |
240 | 02f7fe54 | Michael Hanselmann | return self._Query(op) |
241 | 02f7fe54 | Michael Hanselmann | |
242 | 32f93223 | Michael Hanselmann | elif method == luxi.REQ_QUERY_EXPORTS: |
243 | 32f93223 | Michael Hanselmann | nodes = args |
244 | 32f93223 | Michael Hanselmann | op = opcodes.OpQueryExports(nodes=nodes) |
245 | 32f93223 | Michael Hanselmann | return self._Query(op) |
246 | 32f93223 | Michael Hanselmann | |
247 | ae5849b5 | Michael Hanselmann | elif method == luxi.REQ_QUERY_CONFIG_VALUES: |
248 | ae5849b5 | Michael Hanselmann | fields = args |
249 | ae5849b5 | Michael Hanselmann | op = opcodes.OpQueryConfigValues(output_fields=fields) |
250 | ae5849b5 | Michael Hanselmann | return self._Query(op) |
251 | ae5849b5 | Michael Hanselmann | |
252 | 0bbe448c | Michael Hanselmann | else: |
253 | 0bbe448c | Michael Hanselmann | raise ValueError("Invalid operation") |
254 | ffeffa1d | Iustin Pop | |
255 | ee6c7b94 | Michael Hanselmann | def _DummyLog(self, *args): |
256 | ee6c7b94 | Michael Hanselmann | pass |
257 | ee6c7b94 | Michael Hanselmann | |
258 | ee6c7b94 | Michael Hanselmann | def _Query(self, op): |
259 | ee6c7b94 | Michael Hanselmann | """Runs the specified opcode and returns the result. |
260 | ee6c7b94 | Michael Hanselmann | |
261 | ee6c7b94 | Michael Hanselmann | """ |
262 | ee6c7b94 | Michael Hanselmann | proc = mcpu.Processor(self.server.context) |
263 | ee6c7b94 | Michael Hanselmann | # TODO: Where should log messages go? |
264 | e92376d7 | Iustin Pop | return proc.ExecOpCode(op, self._DummyLog, None) |
265 | ee6c7b94 | Michael Hanselmann | |
266 | ffeffa1d | Iustin Pop | |
267 | 39dcf2ef | Guido Trotter | class GanetiContext(object): |
268 | 39dcf2ef | Guido Trotter | """Context common to all ganeti threads. |
269 | 39dcf2ef | Guido Trotter | |
270 | 39dcf2ef | Guido Trotter | This class creates and holds common objects shared by all threads. |
271 | 39dcf2ef | Guido Trotter | |
272 | 39dcf2ef | Guido Trotter | """ |
273 | 39dcf2ef | Guido Trotter | _instance = None |
274 | 39dcf2ef | Guido Trotter | |
275 | 39dcf2ef | Guido Trotter | def __init__(self): |
276 | 39dcf2ef | Guido Trotter | """Constructs a new GanetiContext object. |
277 | 39dcf2ef | Guido Trotter | |
278 | 39dcf2ef | Guido Trotter | There should be only a GanetiContext object at any time, so this |
279 | 39dcf2ef | Guido Trotter | function raises an error if this is not the case. |
280 | 39dcf2ef | Guido Trotter | |
281 | 39dcf2ef | Guido Trotter | """ |
282 | 39dcf2ef | Guido Trotter | assert self.__class__._instance is None, "double GanetiContext instance" |
283 | 39dcf2ef | Guido Trotter | |
284 | 9113300d | Michael Hanselmann | # Create global configuration object |
285 | 39dcf2ef | Guido Trotter | self.cfg = config.ConfigWriter() |
286 | 9113300d | Michael Hanselmann | |
287 | 9113300d | Michael Hanselmann | # Locking manager |
288 | 984f7c32 | Guido Trotter | self.glm = locking.GanetiLockManager( |
289 | 39dcf2ef | Guido Trotter | self.cfg.GetNodeList(), |
290 | 39dcf2ef | Guido Trotter | self.cfg.GetInstanceList()) |
291 | 39dcf2ef | Guido Trotter | |
292 | 9113300d | Michael Hanselmann | # Job queue |
293 | 9113300d | Michael Hanselmann | self.jobqueue = jqueue.JobQueue(self) |
294 | 9113300d | Michael Hanselmann | |
295 | 39dcf2ef | Guido Trotter | # setting this also locks the class against attribute modifications |
296 | 39dcf2ef | Guido Trotter | self.__class__._instance = self |
297 | 39dcf2ef | Guido Trotter | |
298 | 39dcf2ef | Guido Trotter | def __setattr__(self, name, value): |
299 | 39dcf2ef | Guido Trotter | """Setting GanetiContext attributes is forbidden after initialization. |
300 | 39dcf2ef | Guido Trotter | |
301 | 39dcf2ef | Guido Trotter | """ |
302 | 39dcf2ef | Guido Trotter | assert self.__class__._instance is None, "Attempt to modify Ganeti Context" |
303 | 39dcf2ef | Guido Trotter | object.__setattr__(self, name, value) |
304 | 39dcf2ef | Guido Trotter | |
305 | d8470559 | Michael Hanselmann | def AddNode(self, node): |
306 | d8470559 | Michael Hanselmann | """Adds a node to the configuration and lock manager. |
307 | d8470559 | Michael Hanselmann | |
308 | d8470559 | Michael Hanselmann | """ |
309 | d8470559 | Michael Hanselmann | # Add it to the configuration |
310 | d8470559 | Michael Hanselmann | self.cfg.AddNode(node) |
311 | d8470559 | Michael Hanselmann | |
312 | c36176cc | Michael Hanselmann | # If preseeding fails it'll not be added |
313 | c36176cc | Michael Hanselmann | self.jobqueue.AddNode(node.name) |
314 | c36176cc | Michael Hanselmann | |
315 | d8470559 | Michael Hanselmann | # Add the new node to the Ganeti Lock Manager |
316 | d8470559 | Michael Hanselmann | self.glm.add(locking.LEVEL_NODE, node.name) |
317 | d8470559 | Michael Hanselmann | |
318 | d8470559 | Michael Hanselmann | def ReaddNode(self, node): |
319 | d8470559 | Michael Hanselmann | """Updates a node that's already in the configuration |
320 | d8470559 | Michael Hanselmann | |
321 | d8470559 | Michael Hanselmann | """ |
322 | c36176cc | Michael Hanselmann | # Synchronize the queue again |
323 | c36176cc | Michael Hanselmann | self.jobqueue.AddNode(node.name) |
324 | d8470559 | Michael Hanselmann | |
325 | d8470559 | Michael Hanselmann | def RemoveNode(self, name): |
326 | d8470559 | Michael Hanselmann | """Removes a node from the configuration and lock manager. |
327 | d8470559 | Michael Hanselmann | |
328 | d8470559 | Michael Hanselmann | """ |
329 | d8470559 | Michael Hanselmann | # Remove node from configuration |
330 | d8470559 | Michael Hanselmann | self.cfg.RemoveNode(name) |
331 | d8470559 | Michael Hanselmann | |
332 | c36176cc | Michael Hanselmann | # Notify job queue |
333 | c36176cc | Michael Hanselmann | self.jobqueue.RemoveNode(name) |
334 | c36176cc | Michael Hanselmann | |
335 | d8470559 | Michael Hanselmann | # Remove the node from the Ganeti Lock Manager |
336 | d8470559 | Michael Hanselmann | self.glm.remove(locking.LEVEL_NODE, name) |
337 | d8470559 | Michael Hanselmann | |
338 | 39dcf2ef | Guido Trotter | |
339 | c1f2901b | Iustin Pop | def ParseOptions(): |
340 | c1f2901b | Iustin Pop | """Parse the command line options. |
341 | c1f2901b | Iustin Pop | |
342 | c1f2901b | Iustin Pop | Returns: |
343 | c1f2901b | Iustin Pop | (options, args) as from OptionParser.parse_args() |
344 | c1f2901b | Iustin Pop | |
345 | c1f2901b | Iustin Pop | """ |
346 | c1f2901b | Iustin Pop | parser = OptionParser(description="Ganeti master daemon", |
347 | c1f2901b | Iustin Pop | usage="%prog [-f] [-d]", |
348 | c1f2901b | Iustin Pop | version="%%prog (ganeti) %s" % |
349 | c1f2901b | Iustin Pop | constants.RELEASE_VERSION) |
350 | c1f2901b | Iustin Pop | |
351 | c1f2901b | Iustin Pop | parser.add_option("-f", "--foreground", dest="fork", |
352 | c1f2901b | Iustin Pop | help="Don't detach from the current terminal", |
353 | c1f2901b | Iustin Pop | default=True, action="store_false") |
354 | c1f2901b | Iustin Pop | parser.add_option("-d", "--debug", dest="debug", |
355 | c1f2901b | Iustin Pop | help="Enable some debug messages", |
356 | c1f2901b | Iustin Pop | default=False, action="store_true") |
357 | c1f2901b | Iustin Pop | options, args = parser.parse_args() |
358 | c1f2901b | Iustin Pop | return options, args |
359 | c1f2901b | Iustin Pop | |
360 | c1f2901b | Iustin Pop | |
361 | 36205981 | Iustin Pop | def CheckAgreement(): |
362 | 36205981 | Iustin Pop | """Check the agreement on who is the master. |
363 | 36205981 | Iustin Pop | |
364 | 36205981 | Iustin Pop | The function uses a very simple algorithm: we must get more positive |
365 | 36205981 | Iustin Pop | than negative answers. Since in most of the cases we are the master, |
366 | 36205981 | Iustin Pop | we'll use our own config file for getting the node list. In the |
367 | 36205981 | Iustin Pop | future we could collect the current node list from our (possibly |
368 | 36205981 | Iustin Pop | obsolete) known nodes. |
369 | 36205981 | Iustin Pop | |
370 | 36205981 | Iustin Pop | """ |
371 | 36205981 | Iustin Pop | myself = utils.HostInfo().name |
372 | 36205981 | Iustin Pop | #temp instantiation of a config writer, used only to get the node list |
373 | 36205981 | Iustin Pop | cfg = config.ConfigWriter() |
374 | 36205981 | Iustin Pop | node_list = cfg.GetNodeList() |
375 | 36205981 | Iustin Pop | del cfg |
376 | 36205981 | Iustin Pop | try: |
377 | 36205981 | Iustin Pop | node_list.remove(myself) |
378 | 36205981 | Iustin Pop | except KeyError: |
379 | 36205981 | Iustin Pop | pass |
380 | 36205981 | Iustin Pop | if not node_list: |
381 | 36205981 | Iustin Pop | # either single node cluster, or a misconfiguration, but I won't |
382 | 36205981 | Iustin Pop | # break any other node, so I can proceed |
383 | 36205981 | Iustin Pop | return True |
384 | 72737a7f | Iustin Pop | results = rpc.RpcRunner.call_master_info(node_list) |
385 | 36205981 | Iustin Pop | if not isinstance(results, dict): |
386 | 36205981 | Iustin Pop | # this should not happen (unless internal error in rpc) |
387 | 36205981 | Iustin Pop | logging.critical("Can't complete rpc call, aborting master startup") |
388 | 36205981 | Iustin Pop | return False |
389 | 36205981 | Iustin Pop | positive = negative = 0 |
390 | 36205981 | Iustin Pop | other_masters = {} |
391 | 36205981 | Iustin Pop | for node in results: |
392 | 36205981 | Iustin Pop | if not isinstance(results[node], (tuple, list)) or len(results[node]) < 3: |
393 | 36205981 | Iustin Pop | logging.warning("Can't contact node %s", node) |
394 | 36205981 | Iustin Pop | continue |
395 | 36205981 | Iustin Pop | master_node = results[node][2] |
396 | 36205981 | Iustin Pop | if master_node == myself: |
397 | 36205981 | Iustin Pop | positive += 1 |
398 | 36205981 | Iustin Pop | else: |
399 | 36205981 | Iustin Pop | negative += 1 |
400 | 36205981 | Iustin Pop | if not master_node in other_masters: |
401 | 36205981 | Iustin Pop | other_masters[master_node] = 0 |
402 | 36205981 | Iustin Pop | other_masters[master_node] += 1 |
403 | 36205981 | Iustin Pop | if positive <= negative: |
404 | 36205981 | Iustin Pop | # bad! |
405 | 36205981 | Iustin Pop | logging.critical("It seems we are not the master (%d votes for," |
406 | 36205981 | Iustin Pop | " %d votes against)", positive, negative) |
407 | 36205981 | Iustin Pop | if len(other_masters) > 1: |
408 | 36205981 | Iustin Pop | logging.critical("The other nodes do not agree on a single master") |
409 | 36205981 | Iustin Pop | elif other_masters: |
410 | 36205981 | Iustin Pop | # TODO: resync my files from the master |
411 | 36205981 | Iustin Pop | logging.critical("It seems the real master is %s", |
412 | 36205981 | Iustin Pop | other_masters.keys()[0]) |
413 | 36205981 | Iustin Pop | else: |
414 | 36205981 | Iustin Pop | logging.critical("Can't contact any node for data, aborting startup") |
415 | 36205981 | Iustin Pop | return False |
416 | 36205981 | Iustin Pop | return True |
417 | 36205981 | Iustin Pop | |
418 | 36205981 | Iustin Pop | |
419 | ffeffa1d | Iustin Pop | def main(): |
420 | ffeffa1d | Iustin Pop | """Main function""" |
421 | ffeffa1d | Iustin Pop | |
422 | c1f2901b | Iustin Pop | options, args = ParseOptions() |
423 | c1f2901b | Iustin Pop | utils.debug = options.debug |
424 | b74159ee | Iustin Pop | utils.no_fork = True |
425 | c1f2901b | Iustin Pop | |
426 | 5675cd1f | Iustin Pop | ssconf.CheckMaster(options.debug) |
427 | c1f2901b | Iustin Pop | |
428 | 36205981 | Iustin Pop | # we believe we are the master, let's ask the other nodes... |
429 | 36205981 | Iustin Pop | if not CheckAgreement(): |
430 | 36205981 | Iustin Pop | return |
431 | 36205981 | Iustin Pop | |
432 | 9113300d | Michael Hanselmann | master = IOServer(constants.MASTER_SOCKET, ClientRqHandler) |
433 | ffeffa1d | Iustin Pop | |
434 | c1f2901b | Iustin Pop | # become a daemon |
435 | c1f2901b | Iustin Pop | if options.fork: |
436 | c1f2901b | Iustin Pop | utils.Daemonize(logfile=constants.LOG_MASTERDAEMON, |
437 | c1f2901b | Iustin Pop | noclose_fds=[master.fileno()]) |
438 | c1f2901b | Iustin Pop | |
439 | 99e88451 | Iustin Pop | utils.WritePidFile(constants.MASTERD_PID) |
440 | 8feda3ad | Guido Trotter | |
441 | 59f187eb | Iustin Pop | logger.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug, |
442 | 59f187eb | Iustin Pop | stderr_logging=not options.fork) |
443 | 3b316acb | Iustin Pop | |
444 | d4fa5c23 | Iustin Pop | logging.info("ganeti master daemon startup") |
445 | 3b316acb | Iustin Pop | |
446 | b1b6ea87 | Iustin Pop | # activate ip |
447 | a42872ff | Michael Hanselmann | master_node = ssconf.SimpleConfigReader().GetMasterNode() |
448 | 72737a7f | Iustin Pop | if not rpc.RpcRunner.call_node_start_master(master_node, False): |
449 | b1b6ea87 | Iustin Pop | logging.error("Can't activate master IP address") |
450 | b1b6ea87 | Iustin Pop | |
451 | d4fa5c23 | Iustin Pop | master.setup_queue() |
452 | c1f2901b | Iustin Pop | try: |
453 | d4fa5c23 | Iustin Pop | master.serve_forever() |
454 | a4af651e | Iustin Pop | finally: |
455 | d4fa5c23 | Iustin Pop | master.server_cleanup() |
456 | 99e88451 | Iustin Pop | utils.RemovePidFile(constants.MASTERD_PID) |
457 | a4af651e | Iustin Pop | |
458 | ffeffa1d | Iustin Pop | |
459 | ffeffa1d | Iustin Pop | if __name__ == "__main__": |
460 | ffeffa1d | Iustin Pop | main() |