root / daemons / ganeti-masterd @ 2503680f
History | View | Annotate | Download (15.9 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 | d823660a | Guido Trotter | import os |
31 | d823660a | Guido Trotter | import errno |
32 | c1f2901b | Iustin Pop | import sys |
33 | ffeffa1d | Iustin Pop | import SocketServer |
34 | ffeffa1d | Iustin Pop | import time |
35 | ffeffa1d | Iustin Pop | import collections |
36 | ffeffa1d | Iustin Pop | import Queue |
37 | ffeffa1d | Iustin Pop | import random |
38 | ffeffa1d | Iustin Pop | import signal |
39 | 96cb3986 | Michael Hanselmann | import logging |
40 | ffeffa1d | Iustin Pop | |
41 | ffeffa1d | Iustin Pop | from cStringIO import StringIO |
42 | c1f2901b | Iustin Pop | from optparse import OptionParser |
43 | ffeffa1d | Iustin Pop | |
44 | 39dcf2ef | Guido Trotter | from ganeti import config |
45 | ffeffa1d | Iustin Pop | from ganeti import constants |
46 | ffeffa1d | Iustin Pop | from ganeti import mcpu |
47 | ffeffa1d | Iustin Pop | from ganeti import opcodes |
48 | ffeffa1d | Iustin Pop | from ganeti import jqueue |
49 | 39dcf2ef | Guido Trotter | from ganeti import locking |
50 | ffeffa1d | Iustin Pop | from ganeti import luxi |
51 | ffeffa1d | Iustin Pop | from ganeti import utils |
52 | c1f2901b | Iustin Pop | from ganeti import errors |
53 | c1f2901b | Iustin Pop | from ganeti import ssconf |
54 | 23e50d39 | Michael Hanselmann | from ganeti import workerpool |
55 | b1b6ea87 | Iustin Pop | from ganeti import rpc |
56 | d7cdb55d | Iustin Pop | from ganeti import bootstrap |
57 | dd36d829 | Iustin Pop | from ganeti import serializer |
58 | c1f2901b | Iustin Pop | |
59 | c1f2901b | Iustin Pop | |
60 | 23e50d39 | Michael Hanselmann | CLIENT_REQUEST_WORKERS = 16 |
61 | 23e50d39 | Michael Hanselmann | |
62 | c1f2901b | Iustin Pop | EXIT_NOTMASTER = constants.EXIT_NOTMASTER |
63 | c1f2901b | Iustin Pop | EXIT_NODESETUP_ERROR = constants.EXIT_NODESETUP_ERROR |
64 | ffeffa1d | Iustin Pop | |
65 | ffeffa1d | Iustin Pop | |
66 | 23e50d39 | Michael Hanselmann | class ClientRequestWorker(workerpool.BaseWorker): |
67 | 23e50d39 | Michael Hanselmann | def RunTask(self, server, request, client_address): |
68 | 23e50d39 | Michael Hanselmann | """Process the request. |
69 | 23e50d39 | Michael Hanselmann | |
70 | 23e50d39 | Michael Hanselmann | This is copied from the code in ThreadingMixIn. |
71 | 23e50d39 | Michael Hanselmann | |
72 | 23e50d39 | Michael Hanselmann | """ |
73 | 23e50d39 | Michael Hanselmann | try: |
74 | 23e50d39 | Michael Hanselmann | server.finish_request(request, client_address) |
75 | 23e50d39 | Michael Hanselmann | server.close_request(request) |
76 | 23e50d39 | Michael Hanselmann | except: |
77 | 23e50d39 | Michael Hanselmann | server.handle_error(request, client_address) |
78 | 23e50d39 | Michael Hanselmann | server.close_request(request) |
79 | 23e50d39 | Michael Hanselmann | |
80 | 23e50d39 | Michael Hanselmann | |
81 | ffeffa1d | Iustin Pop | class IOServer(SocketServer.UnixStreamServer): |
82 | ffeffa1d | Iustin Pop | """IO thread class. |
83 | ffeffa1d | Iustin Pop | |
84 | ffeffa1d | Iustin Pop | This class takes care of initializing the other threads, setting |
85 | ffeffa1d | Iustin Pop | signal handlers (which are processed only in this thread), and doing |
86 | ffeffa1d | Iustin Pop | cleanup at shutdown. |
87 | ffeffa1d | Iustin Pop | |
88 | ffeffa1d | Iustin Pop | """ |
89 | 9113300d | Michael Hanselmann | def __init__(self, address, rqhandler): |
90 | ce862cd5 | Guido Trotter | """IOServer constructor |
91 | ce862cd5 | Guido Trotter | |
92 | c41eea6e | Iustin Pop | @param address: the address to bind this IOServer to |
93 | c41eea6e | Iustin Pop | @param rqhandler: RequestHandler type object |
94 | ce862cd5 | Guido Trotter | |
95 | ce862cd5 | Guido Trotter | """ |
96 | ffeffa1d | Iustin Pop | SocketServer.UnixStreamServer.__init__(self, address, rqhandler) |
97 | 50a3fbb2 | Michael Hanselmann | |
98 | 50a3fbb2 | Michael Hanselmann | # We'll only start threads once we've forked. |
99 | 9113300d | Michael Hanselmann | self.context = None |
100 | 23e50d39 | Michael Hanselmann | self.request_workers = None |
101 | 50a3fbb2 | Michael Hanselmann | |
102 | 50a3fbb2 | Michael Hanselmann | def setup_queue(self): |
103 | 9113300d | Michael Hanselmann | self.context = GanetiContext() |
104 | 23e50d39 | Michael Hanselmann | self.request_workers = workerpool.WorkerPool(CLIENT_REQUEST_WORKERS, |
105 | 23e50d39 | Michael Hanselmann | ClientRequestWorker) |
106 | ffeffa1d | Iustin Pop | |
107 | ffeffa1d | Iustin Pop | def process_request(self, request, client_address): |
108 | 23e50d39 | Michael Hanselmann | """Add task to workerpool to process request. |
109 | ffeffa1d | Iustin Pop | |
110 | ffeffa1d | Iustin Pop | """ |
111 | 23e50d39 | Michael Hanselmann | self.request_workers.AddTask(self, request, client_address) |
112 | ffeffa1d | Iustin Pop | |
113 | ffeffa1d | Iustin Pop | def serve_forever(self): |
114 | ffeffa1d | Iustin Pop | """Handle one request at a time until told to quit.""" |
115 | 610bc9ee | Michael Hanselmann | sighandler = utils.SignalHandler([signal.SIGINT, signal.SIGTERM]) |
116 | 610bc9ee | Michael Hanselmann | try: |
117 | 610bc9ee | Michael Hanselmann | while not sighandler.called: |
118 | 610bc9ee | Michael Hanselmann | self.handle_request() |
119 | 610bc9ee | Michael Hanselmann | finally: |
120 | 610bc9ee | Michael Hanselmann | sighandler.Reset() |
121 | c1f2901b | Iustin Pop | |
122 | c1f2901b | Iustin Pop | def server_cleanup(self): |
123 | c1f2901b | Iustin Pop | """Cleanup the server. |
124 | c1f2901b | Iustin Pop | |
125 | c1f2901b | Iustin Pop | This involves shutting down the processor threads and the master |
126 | c1f2901b | Iustin Pop | socket. |
127 | c1f2901b | Iustin Pop | |
128 | c1f2901b | Iustin Pop | """ |
129 | 50a3fbb2 | Michael Hanselmann | try: |
130 | 50a3fbb2 | Michael Hanselmann | self.server_close() |
131 | 50a3fbb2 | Michael Hanselmann | finally: |
132 | 23e50d39 | Michael Hanselmann | if self.request_workers: |
133 | 36088c4c | Michael Hanselmann | self.request_workers.TerminateWorkers() |
134 | 9113300d | Michael Hanselmann | if self.context: |
135 | 9113300d | Michael Hanselmann | self.context.jobqueue.Shutdown() |
136 | ffeffa1d | Iustin Pop | |
137 | ffeffa1d | Iustin Pop | |
138 | ffeffa1d | Iustin Pop | class ClientRqHandler(SocketServer.BaseRequestHandler): |
139 | ffeffa1d | Iustin Pop | """Client handler""" |
140 | ffeffa1d | Iustin Pop | EOM = '\3' |
141 | ffeffa1d | Iustin Pop | READ_SIZE = 4096 |
142 | ffeffa1d | Iustin Pop | |
143 | ffeffa1d | Iustin Pop | def setup(self): |
144 | ffeffa1d | Iustin Pop | self._buffer = "" |
145 | ffeffa1d | Iustin Pop | self._msgs = collections.deque() |
146 | ffeffa1d | Iustin Pop | self._ops = ClientOps(self.server) |
147 | ffeffa1d | Iustin Pop | |
148 | ffeffa1d | Iustin Pop | def handle(self): |
149 | ffeffa1d | Iustin Pop | while True: |
150 | ffeffa1d | Iustin Pop | msg = self.read_message() |
151 | ffeffa1d | Iustin Pop | if msg is None: |
152 | d21d09d6 | Iustin Pop | logging.debug("client closed connection") |
153 | ffeffa1d | Iustin Pop | break |
154 | 3d8548c4 | Michael Hanselmann | |
155 | dd36d829 | Iustin Pop | request = serializer.LoadJson(msg) |
156 | 3d8548c4 | Michael Hanselmann | logging.debug("request: %s", request) |
157 | ffeffa1d | Iustin Pop | if not isinstance(request, dict): |
158 | 3d8548c4 | Michael Hanselmann | logging.error("wrong request received: %s", msg) |
159 | ffeffa1d | Iustin Pop | break |
160 | 3d8548c4 | Michael Hanselmann | |
161 | 3d8548c4 | Michael Hanselmann | method = request.get(luxi.KEY_METHOD, None) |
162 | 3d8548c4 | Michael Hanselmann | args = request.get(luxi.KEY_ARGS, None) |
163 | 3d8548c4 | Michael Hanselmann | if method is None or args is None: |
164 | 3d8548c4 | Michael Hanselmann | logging.error("no method or args in request") |
165 | ffeffa1d | Iustin Pop | break |
166 | 3d8548c4 | Michael Hanselmann | |
167 | 3d8548c4 | Michael Hanselmann | success = False |
168 | 3d8548c4 | Michael Hanselmann | try: |
169 | 3d8548c4 | Michael Hanselmann | result = self._ops.handle_request(method, args) |
170 | 3d8548c4 | Michael Hanselmann | success = True |
171 | 6797ec29 | Iustin Pop | except errors.GenericError, err: |
172 | 6797ec29 | Iustin Pop | success = False |
173 | 6797ec29 | Iustin Pop | result = (err.__class__.__name__, err.args) |
174 | 3d8548c4 | Michael Hanselmann | except: |
175 | 3d8548c4 | Michael Hanselmann | logging.error("Unexpected exception", exc_info=True) |
176 | 3d8548c4 | Michael Hanselmann | err = sys.exc_info() |
177 | 3d8548c4 | Michael Hanselmann | result = "Caught exception: %s" % str(err[1]) |
178 | 3d8548c4 | Michael Hanselmann | |
179 | 3d8548c4 | Michael Hanselmann | response = { |
180 | 3d8548c4 | Michael Hanselmann | luxi.KEY_SUCCESS: success, |
181 | 3d8548c4 | Michael Hanselmann | luxi.KEY_RESULT: result, |
182 | 3d8548c4 | Michael Hanselmann | } |
183 | 3d8548c4 | Michael Hanselmann | logging.debug("response: %s", response) |
184 | dd36d829 | Iustin Pop | self.send_message(serializer.DumpJson(response)) |
185 | ffeffa1d | Iustin Pop | |
186 | ffeffa1d | Iustin Pop | def read_message(self): |
187 | ffeffa1d | Iustin Pop | while not self._msgs: |
188 | ffeffa1d | Iustin Pop | data = self.request.recv(self.READ_SIZE) |
189 | ffeffa1d | Iustin Pop | if not data: |
190 | ffeffa1d | Iustin Pop | return None |
191 | ffeffa1d | Iustin Pop | new_msgs = (self._buffer + data).split(self.EOM) |
192 | ffeffa1d | Iustin Pop | self._buffer = new_msgs.pop() |
193 | ffeffa1d | Iustin Pop | self._msgs.extend(new_msgs) |
194 | ffeffa1d | Iustin Pop | return self._msgs.popleft() |
195 | ffeffa1d | Iustin Pop | |
196 | ffeffa1d | Iustin Pop | def send_message(self, msg): |
197 | ffeffa1d | Iustin Pop | #print "sending", msg |
198 | ffeffa1d | Iustin Pop | self.request.sendall(msg + self.EOM) |
199 | ffeffa1d | Iustin Pop | |
200 | ffeffa1d | Iustin Pop | |
201 | ffeffa1d | Iustin Pop | class ClientOps: |
202 | ffeffa1d | Iustin Pop | """Class holding high-level client operations.""" |
203 | ffeffa1d | Iustin Pop | def __init__(self, server): |
204 | ffeffa1d | Iustin Pop | self.server = server |
205 | ffeffa1d | Iustin Pop | |
206 | 0bbe448c | Michael Hanselmann | def handle_request(self, method, args): |
207 | 9113300d | Michael Hanselmann | queue = self.server.context.jobqueue |
208 | 0bbe448c | Michael Hanselmann | |
209 | 0bbe448c | Michael Hanselmann | # TODO: Parameter validation |
210 | 0bbe448c | Michael Hanselmann | |
211 | 0bbe448c | Michael Hanselmann | if method == luxi.REQ_SUBMIT_JOB: |
212 | e566ddbd | Iustin Pop | logging.info("Received new job") |
213 | 0bbe448c | Michael Hanselmann | ops = [opcodes.OpCode.LoadOpCode(state) for state in args] |
214 | 4c848b18 | Michael Hanselmann | return queue.SubmitJob(ops) |
215 | ffeffa1d | Iustin Pop | |
216 | 0bbe448c | Michael Hanselmann | elif method == luxi.REQ_CANCEL_JOB: |
217 | 3a2c7775 | Michael Hanselmann | job_id = args |
218 | e566ddbd | Iustin Pop | logging.info("Received job cancel request for %s", job_id) |
219 | 0bbe448c | Michael Hanselmann | return queue.CancelJob(job_id) |
220 | ffeffa1d | Iustin Pop | |
221 | 0bbe448c | Michael Hanselmann | elif method == luxi.REQ_ARCHIVE_JOB: |
222 | 3a2c7775 | Michael Hanselmann | job_id = args |
223 | e566ddbd | Iustin Pop | logging.info("Received job archive request for %s", job_id) |
224 | 0bbe448c | Michael Hanselmann | return queue.ArchiveJob(job_id) |
225 | 0bbe448c | Michael Hanselmann | |
226 | 07cd723a | Iustin Pop | elif method == luxi.REQ_AUTOARCHIVE_JOBS: |
227 | f8ad5591 | Michael Hanselmann | (age, timeout) = args |
228 | e566ddbd | Iustin Pop | logging.info("Received job autoarchive request for age %s, timeout %s", |
229 | e566ddbd | Iustin Pop | age, timeout) |
230 | f8ad5591 | Michael Hanselmann | return queue.AutoArchiveJobs(age, timeout) |
231 | 07cd723a | Iustin Pop | |
232 | dfe57c22 | Michael Hanselmann | elif method == luxi.REQ_WAIT_FOR_JOB_CHANGE: |
233 | 5c735209 | Iustin Pop | (job_id, fields, prev_job_info, prev_log_serial, timeout) = args |
234 | e566ddbd | Iustin Pop | logging.info("Received job poll request for %s", job_id) |
235 | 6c5a7090 | Michael Hanselmann | return queue.WaitForJobChanges(job_id, fields, prev_job_info, |
236 | 5c735209 | Iustin Pop | prev_log_serial, timeout) |
237 | dfe57c22 | Michael Hanselmann | |
238 | 0bbe448c | Michael Hanselmann | elif method == luxi.REQ_QUERY_JOBS: |
239 | 0bbe448c | Michael Hanselmann | (job_ids, fields) = args |
240 | e566ddbd | Iustin Pop | if isinstance(job_ids, (tuple, list)) and job_ids: |
241 | e566ddbd | Iustin Pop | msg = ", ".join(job_ids) |
242 | e566ddbd | Iustin Pop | else: |
243 | e566ddbd | Iustin Pop | msg = str(job_ids) |
244 | e566ddbd | Iustin Pop | logging.info("Received job query request for %s", msg) |
245 | 0bbe448c | Michael Hanselmann | return queue.QueryJobs(job_ids, fields) |
246 | 0bbe448c | Michael Hanselmann | |
247 | ee6c7b94 | Michael Hanselmann | elif method == luxi.REQ_QUERY_INSTANCES: |
248 | ec79568d | Iustin Pop | (names, fields, use_locking) = args |
249 | e566ddbd | Iustin Pop | logging.info("Received instance query request for %s", names) |
250 | 77921a95 | Iustin Pop | if use_locking: |
251 | 77921a95 | Iustin Pop | raise errors.OpPrereqError("Sync queries are not allowed") |
252 | ec79568d | Iustin Pop | op = opcodes.OpQueryInstances(names=names, output_fields=fields, |
253 | ec79568d | Iustin Pop | use_locking=use_locking) |
254 | ee6c7b94 | Michael Hanselmann | return self._Query(op) |
255 | ee6c7b94 | Michael Hanselmann | |
256 | 02f7fe54 | Michael Hanselmann | elif method == luxi.REQ_QUERY_NODES: |
257 | ec79568d | Iustin Pop | (names, fields, use_locking) = args |
258 | e566ddbd | Iustin Pop | logging.info("Received node query request for %s", names) |
259 | 77921a95 | Iustin Pop | if use_locking: |
260 | 77921a95 | Iustin Pop | raise errors.OpPrereqError("Sync queries are not allowed") |
261 | ec79568d | Iustin Pop | op = opcodes.OpQueryNodes(names=names, output_fields=fields, |
262 | ec79568d | Iustin Pop | use_locking=use_locking) |
263 | 02f7fe54 | Michael Hanselmann | return self._Query(op) |
264 | 02f7fe54 | Michael Hanselmann | |
265 | 32f93223 | Michael Hanselmann | elif method == luxi.REQ_QUERY_EXPORTS: |
266 | ec79568d | Iustin Pop | nodes, use_locking = args |
267 | 77921a95 | Iustin Pop | if use_locking: |
268 | 77921a95 | Iustin Pop | raise errors.OpPrereqError("Sync queries are not allowed") |
269 | e566ddbd | Iustin Pop | logging.info("Received exports query request") |
270 | ec79568d | Iustin Pop | op = opcodes.OpQueryExports(nodes=nodes, use_locking=use_locking) |
271 | 32f93223 | Michael Hanselmann | return self._Query(op) |
272 | 32f93223 | Michael Hanselmann | |
273 | ae5849b5 | Michael Hanselmann | elif method == luxi.REQ_QUERY_CONFIG_VALUES: |
274 | ae5849b5 | Michael Hanselmann | fields = args |
275 | e566ddbd | Iustin Pop | logging.info("Received config values query request for %s", fields) |
276 | ae5849b5 | Michael Hanselmann | op = opcodes.OpQueryConfigValues(output_fields=fields) |
277 | ae5849b5 | Michael Hanselmann | return self._Query(op) |
278 | ae5849b5 | Michael Hanselmann | |
279 | 66baeccc | Iustin Pop | elif method == luxi.REQ_QUERY_CLUSTER_INFO: |
280 | e566ddbd | Iustin Pop | logging.info("Received cluster info query request") |
281 | 66baeccc | Iustin Pop | op = opcodes.OpQueryClusterInfo() |
282 | 66baeccc | Iustin Pop | return self._Query(op) |
283 | 66baeccc | Iustin Pop | |
284 | 3ccafd0e | Iustin Pop | elif method == luxi.REQ_QUEUE_SET_DRAIN_FLAG: |
285 | 3ccafd0e | Iustin Pop | drain_flag = args |
286 | e566ddbd | Iustin Pop | logging.info("Received queue drain flag change request to %s", |
287 | e566ddbd | Iustin Pop | drain_flag) |
288 | 3ccafd0e | Iustin Pop | return queue.SetDrainFlag(drain_flag) |
289 | 3ccafd0e | Iustin Pop | |
290 | 0bbe448c | Michael Hanselmann | else: |
291 | e566ddbd | Iustin Pop | logging.info("Received invalid request '%s'", method) |
292 | e566ddbd | Iustin Pop | raise ValueError("Invalid operation '%s'" % method) |
293 | ffeffa1d | Iustin Pop | |
294 | ee6c7b94 | Michael Hanselmann | def _DummyLog(self, *args): |
295 | ee6c7b94 | Michael Hanselmann | pass |
296 | ee6c7b94 | Michael Hanselmann | |
297 | ee6c7b94 | Michael Hanselmann | def _Query(self, op): |
298 | ee6c7b94 | Michael Hanselmann | """Runs the specified opcode and returns the result. |
299 | ee6c7b94 | Michael Hanselmann | |
300 | ee6c7b94 | Michael Hanselmann | """ |
301 | ee6c7b94 | Michael Hanselmann | proc = mcpu.Processor(self.server.context) |
302 | ee6c7b94 | Michael Hanselmann | # TODO: Where should log messages go? |
303 | e92376d7 | Iustin Pop | return proc.ExecOpCode(op, self._DummyLog, None) |
304 | ee6c7b94 | Michael Hanselmann | |
305 | ffeffa1d | Iustin Pop | |
306 | 39dcf2ef | Guido Trotter | class GanetiContext(object): |
307 | 39dcf2ef | Guido Trotter | """Context common to all ganeti threads. |
308 | 39dcf2ef | Guido Trotter | |
309 | 39dcf2ef | Guido Trotter | This class creates and holds common objects shared by all threads. |
310 | 39dcf2ef | Guido Trotter | |
311 | 39dcf2ef | Guido Trotter | """ |
312 | 39dcf2ef | Guido Trotter | _instance = None |
313 | 39dcf2ef | Guido Trotter | |
314 | 39dcf2ef | Guido Trotter | def __init__(self): |
315 | 39dcf2ef | Guido Trotter | """Constructs a new GanetiContext object. |
316 | 39dcf2ef | Guido Trotter | |
317 | 39dcf2ef | Guido Trotter | There should be only a GanetiContext object at any time, so this |
318 | 39dcf2ef | Guido Trotter | function raises an error if this is not the case. |
319 | 39dcf2ef | Guido Trotter | |
320 | 39dcf2ef | Guido Trotter | """ |
321 | 39dcf2ef | Guido Trotter | assert self.__class__._instance is None, "double GanetiContext instance" |
322 | 39dcf2ef | Guido Trotter | |
323 | 9113300d | Michael Hanselmann | # Create global configuration object |
324 | 39dcf2ef | Guido Trotter | self.cfg = config.ConfigWriter() |
325 | 9113300d | Michael Hanselmann | |
326 | 9113300d | Michael Hanselmann | # Locking manager |
327 | 984f7c32 | Guido Trotter | self.glm = locking.GanetiLockManager( |
328 | 39dcf2ef | Guido Trotter | self.cfg.GetNodeList(), |
329 | 39dcf2ef | Guido Trotter | self.cfg.GetInstanceList()) |
330 | 39dcf2ef | Guido Trotter | |
331 | 9113300d | Michael Hanselmann | # Job queue |
332 | 9113300d | Michael Hanselmann | self.jobqueue = jqueue.JobQueue(self) |
333 | 9113300d | Michael Hanselmann | |
334 | 39dcf2ef | Guido Trotter | # setting this also locks the class against attribute modifications |
335 | 39dcf2ef | Guido Trotter | self.__class__._instance = self |
336 | 39dcf2ef | Guido Trotter | |
337 | 39dcf2ef | Guido Trotter | def __setattr__(self, name, value): |
338 | 39dcf2ef | Guido Trotter | """Setting GanetiContext attributes is forbidden after initialization. |
339 | 39dcf2ef | Guido Trotter | |
340 | 39dcf2ef | Guido Trotter | """ |
341 | 39dcf2ef | Guido Trotter | assert self.__class__._instance is None, "Attempt to modify Ganeti Context" |
342 | 39dcf2ef | Guido Trotter | object.__setattr__(self, name, value) |
343 | 39dcf2ef | Guido Trotter | |
344 | d8470559 | Michael Hanselmann | def AddNode(self, node): |
345 | d8470559 | Michael Hanselmann | """Adds a node to the configuration and lock manager. |
346 | d8470559 | Michael Hanselmann | |
347 | d8470559 | Michael Hanselmann | """ |
348 | d8470559 | Michael Hanselmann | # Add it to the configuration |
349 | d8470559 | Michael Hanselmann | self.cfg.AddNode(node) |
350 | d8470559 | Michael Hanselmann | |
351 | c36176cc | Michael Hanselmann | # If preseeding fails it'll not be added |
352 | 99aabbed | Iustin Pop | self.jobqueue.AddNode(node) |
353 | c36176cc | Michael Hanselmann | |
354 | d8470559 | Michael Hanselmann | # Add the new node to the Ganeti Lock Manager |
355 | d8470559 | Michael Hanselmann | self.glm.add(locking.LEVEL_NODE, node.name) |
356 | d8470559 | Michael Hanselmann | |
357 | d8470559 | Michael Hanselmann | def ReaddNode(self, node): |
358 | d8470559 | Michael Hanselmann | """Updates a node that's already in the configuration |
359 | d8470559 | Michael Hanselmann | |
360 | d8470559 | Michael Hanselmann | """ |
361 | c36176cc | Michael Hanselmann | # Synchronize the queue again |
362 | 99aabbed | Iustin Pop | self.jobqueue.AddNode(node) |
363 | d8470559 | Michael Hanselmann | |
364 | d8470559 | Michael Hanselmann | def RemoveNode(self, name): |
365 | d8470559 | Michael Hanselmann | """Removes a node from the configuration and lock manager. |
366 | d8470559 | Michael Hanselmann | |
367 | d8470559 | Michael Hanselmann | """ |
368 | d8470559 | Michael Hanselmann | # Remove node from configuration |
369 | d8470559 | Michael Hanselmann | self.cfg.RemoveNode(name) |
370 | d8470559 | Michael Hanselmann | |
371 | c36176cc | Michael Hanselmann | # Notify job queue |
372 | c36176cc | Michael Hanselmann | self.jobqueue.RemoveNode(name) |
373 | c36176cc | Michael Hanselmann | |
374 | d8470559 | Michael Hanselmann | # Remove the node from the Ganeti Lock Manager |
375 | d8470559 | Michael Hanselmann | self.glm.remove(locking.LEVEL_NODE, name) |
376 | d8470559 | Michael Hanselmann | |
377 | 39dcf2ef | Guido Trotter | |
378 | c1f2901b | Iustin Pop | def ParseOptions(): |
379 | c1f2901b | Iustin Pop | """Parse the command line options. |
380 | c1f2901b | Iustin Pop | |
381 | c41eea6e | Iustin Pop | @return: (options, args) as from OptionParser.parse_args() |
382 | c1f2901b | Iustin Pop | |
383 | c1f2901b | Iustin Pop | """ |
384 | c1f2901b | Iustin Pop | parser = OptionParser(description="Ganeti master daemon", |
385 | c1f2901b | Iustin Pop | usage="%prog [-f] [-d]", |
386 | c1f2901b | Iustin Pop | version="%%prog (ganeti) %s" % |
387 | c1f2901b | Iustin Pop | constants.RELEASE_VERSION) |
388 | c1f2901b | Iustin Pop | |
389 | c1f2901b | Iustin Pop | parser.add_option("-f", "--foreground", dest="fork", |
390 | c1f2901b | Iustin Pop | help="Don't detach from the current terminal", |
391 | c1f2901b | Iustin Pop | default=True, action="store_false") |
392 | c1f2901b | Iustin Pop | parser.add_option("-d", "--debug", dest="debug", |
393 | c1f2901b | Iustin Pop | help="Enable some debug messages", |
394 | c1f2901b | Iustin Pop | default=False, action="store_true") |
395 | 5de4474d | Iustin Pop | parser.add_option("--no-voting", dest="no_voting", |
396 | 5de4474d | Iustin Pop | help="Do not check that the nodes agree on this node" |
397 | 5de4474d | Iustin Pop | " being the master and start the daemon unconditionally", |
398 | 5de4474d | Iustin Pop | default=False, action="store_true") |
399 | 5e96d216 | Guido Trotter | parser.add_option("--yes-do-it", dest="yes_do_it", |
400 | 5e96d216 | Guido Trotter | help="Override interactive check for --no-voting", |
401 | 5e96d216 | Guido Trotter | default=False, action="store_true") |
402 | 5e96d216 | Guido Trotter | |
403 | c1f2901b | Iustin Pop | options, args = parser.parse_args() |
404 | c1f2901b | Iustin Pop | return options, args |
405 | c1f2901b | Iustin Pop | |
406 | c1f2901b | Iustin Pop | |
407 | 36205981 | Iustin Pop | def CheckAgreement(): |
408 | 36205981 | Iustin Pop | """Check the agreement on who is the master. |
409 | 36205981 | Iustin Pop | |
410 | 36205981 | Iustin Pop | The function uses a very simple algorithm: we must get more positive |
411 | 36205981 | Iustin Pop | than negative answers. Since in most of the cases we are the master, |
412 | 36205981 | Iustin Pop | we'll use our own config file for getting the node list. In the |
413 | 36205981 | Iustin Pop | future we could collect the current node list from our (possibly |
414 | 36205981 | Iustin Pop | obsolete) known nodes. |
415 | 36205981 | Iustin Pop | |
416 | d7cdb55d | Iustin Pop | In order to account for cold-start of all nodes, we retry for up to |
417 | d7cdb55d | Iustin Pop | a minute until we get a real answer as the top-voted one. If the |
418 | d7cdb55d | Iustin Pop | nodes are more out-of-sync, for now manual startup of the master |
419 | d7cdb55d | Iustin Pop | should be attempted. |
420 | d7cdb55d | Iustin Pop | |
421 | d7cdb55d | Iustin Pop | Note that for a even number of nodes cluster, we need at least half |
422 | d7cdb55d | Iustin Pop | of the nodes (beside ourselves) to vote for us. This creates a |
423 | d7cdb55d | Iustin Pop | problem on two-node clusters, since in this case we require the |
424 | d7cdb55d | Iustin Pop | other node to be up too to confirm our status. |
425 | d7cdb55d | Iustin Pop | |
426 | 36205981 | Iustin Pop | """ |
427 | 36205981 | Iustin Pop | myself = utils.HostInfo().name |
428 | 36205981 | Iustin Pop | #temp instantiation of a config writer, used only to get the node list |
429 | 36205981 | Iustin Pop | cfg = config.ConfigWriter() |
430 | 36205981 | Iustin Pop | node_list = cfg.GetNodeList() |
431 | 36205981 | Iustin Pop | del cfg |
432 | d7cdb55d | Iustin Pop | retries = 6 |
433 | d7cdb55d | Iustin Pop | while retries > 0: |
434 | d7cdb55d | Iustin Pop | votes = bootstrap.GatherMasterVotes(node_list) |
435 | d7cdb55d | Iustin Pop | if not votes: |
436 | d7cdb55d | Iustin Pop | # empty node list, this is a one node cluster |
437 | d7cdb55d | Iustin Pop | return True |
438 | d7cdb55d | Iustin Pop | if votes[0][0] is None: |
439 | d7cdb55d | Iustin Pop | retries -= 1 |
440 | d7cdb55d | Iustin Pop | time.sleep(10) |
441 | 36205981 | Iustin Pop | continue |
442 | d7cdb55d | Iustin Pop | break |
443 | d7cdb55d | Iustin Pop | if retries == 0: |
444 | e09fdcfa | Iustin Pop | logging.critical("Cluster inconsistent, most of the nodes didn't answer" |
445 | e09fdcfa | Iustin Pop | " after multiple retries. Aborting startup") |
446 | e09fdcfa | Iustin Pop | return False |
447 | d7cdb55d | Iustin Pop | # here a real node is at the top of the list |
448 | d7cdb55d | Iustin Pop | all_votes = sum(item[1] for item in votes) |
449 | d7cdb55d | Iustin Pop | top_node, top_votes = votes[0] |
450 | d7cdb55d | Iustin Pop | result = False |
451 | d7cdb55d | Iustin Pop | if top_node != myself: |
452 | d7cdb55d | Iustin Pop | logging.critical("It seems we are not the master (top-voted node" |
453 | bbe19c17 | Iustin Pop | " is %s with %d out of %d votes)", top_node, top_votes, |
454 | bbe19c17 | Iustin Pop | all_votes) |
455 | d7cdb55d | Iustin Pop | elif top_votes < all_votes - top_votes: |
456 | 36205981 | Iustin Pop | logging.critical("It seems we are not the master (%d votes for," |
457 | d7cdb55d | Iustin Pop | " %d votes against)", top_votes, all_votes - top_votes) |
458 | d7cdb55d | Iustin Pop | else: |
459 | d7cdb55d | Iustin Pop | result = True |
460 | d7cdb55d | Iustin Pop | |
461 | d7cdb55d | Iustin Pop | return result |
462 | 36205981 | Iustin Pop | |
463 | 36205981 | Iustin Pop | |
464 | ffeffa1d | Iustin Pop | def main(): |
465 | ffeffa1d | Iustin Pop | """Main function""" |
466 | ffeffa1d | Iustin Pop | |
467 | c1f2901b | Iustin Pop | options, args = ParseOptions() |
468 | c1f2901b | Iustin Pop | utils.debug = options.debug |
469 | b74159ee | Iustin Pop | utils.no_fork = True |
470 | c1f2901b | Iustin Pop | |
471 | 7d88772a | Iustin Pop | if options.fork: |
472 | 7d88772a | Iustin Pop | utils.CloseFDs() |
473 | 7d88772a | Iustin Pop | |
474 | 4331f6cd | Michael Hanselmann | rpc.Init() |
475 | 4331f6cd | Michael Hanselmann | try: |
476 | 4331f6cd | Michael Hanselmann | ssconf.CheckMaster(options.debug) |
477 | c1f2901b | Iustin Pop | |
478 | 4331f6cd | Michael Hanselmann | # we believe we are the master, let's ask the other nodes... |
479 | 5e96d216 | Guido Trotter | if options.no_voting and not options.yes_do_it: |
480 | 5de4474d | Iustin Pop | sys.stdout.write("The 'no voting' option has been selected.\n") |
481 | 5de4474d | Iustin Pop | sys.stdout.write("This is dangerous, please confirm by" |
482 | 5de4474d | Iustin Pop | " typing uppercase 'yes': ") |
483 | 5de4474d | Iustin Pop | sys.stdout.flush() |
484 | 5de4474d | Iustin Pop | confirmation = sys.stdin.readline().strip() |
485 | 5de4474d | Iustin Pop | if confirmation != "YES": |
486 | 5de4474d | Iustin Pop | print "Aborting." |
487 | 5de4474d | Iustin Pop | return |
488 | 5e96d216 | Guido Trotter | elif not options.no_voting: |
489 | 5de4474d | Iustin Pop | if not CheckAgreement(): |
490 | 5de4474d | Iustin Pop | return |
491 | 36205981 | Iustin Pop | |
492 | 1cb8d376 | Guido Trotter | dirs = [(constants.RUN_GANETI_DIR, constants.RUN_DIRS_MODE), |
493 | 1cb8d376 | Guido Trotter | (constants.SOCKET_DIR, constants.SOCKET_DIR_MODE), |
494 | 1cb8d376 | Guido Trotter | ] |
495 | 9dae41ad | Guido Trotter | utils.EnsureDirs(dirs) |
496 | d823660a | Guido Trotter | |
497 | 227647ac | Guido Trotter | # This is safe to do as the pid file guarantees against |
498 | 227647ac | Guido Trotter | # concurrent execution. |
499 | 227647ac | Guido Trotter | utils.RemoveFile(constants.MASTER_SOCKET) |
500 | 227647ac | Guido Trotter | |
501 | 4331f6cd | Michael Hanselmann | master = IOServer(constants.MASTER_SOCKET, ClientRqHandler) |
502 | 4331f6cd | Michael Hanselmann | finally: |
503 | 4331f6cd | Michael Hanselmann | rpc.Shutdown() |
504 | ffeffa1d | Iustin Pop | |
505 | c1f2901b | Iustin Pop | # become a daemon |
506 | c1f2901b | Iustin Pop | if options.fork: |
507 | 7d88772a | Iustin Pop | utils.Daemonize(logfile=constants.LOG_MASTERDAEMON) |
508 | c1f2901b | Iustin Pop | |
509 | 99e88451 | Iustin Pop | utils.WritePidFile(constants.MASTERD_PID) |
510 | 4331f6cd | Michael Hanselmann | try: |
511 | 15486fa7 | Michael Hanselmann | utils.SetupLogging(constants.LOG_MASTERDAEMON, debug=options.debug, |
512 | d21d09d6 | Iustin Pop | stderr_logging=not options.fork, multithreaded=True) |
513 | 3b316acb | Iustin Pop | |
514 | 15486fa7 | Michael Hanselmann | logging.info("Ganeti master daemon startup") |
515 | b1b6ea87 | Iustin Pop | |
516 | 15486fa7 | Michael Hanselmann | rpc.Init() |
517 | 4331f6cd | Michael Hanselmann | try: |
518 | 15486fa7 | Michael Hanselmann | # activate ip |
519 | 15486fa7 | Michael Hanselmann | master_node = ssconf.SimpleConfigReader().GetMasterNode() |
520 | 2503680f | Guido Trotter | if not rpc.RpcRunner.call_node_start_master(master_node, False, False): |
521 | 15486fa7 | Michael Hanselmann | logging.error("Can't activate master IP address") |
522 | 15486fa7 | Michael Hanselmann | |
523 | 15486fa7 | Michael Hanselmann | master.setup_queue() |
524 | 15486fa7 | Michael Hanselmann | try: |
525 | 15486fa7 | Michael Hanselmann | master.serve_forever() |
526 | 15486fa7 | Michael Hanselmann | finally: |
527 | 15486fa7 | Michael Hanselmann | master.server_cleanup() |
528 | 4331f6cd | Michael Hanselmann | finally: |
529 | 15486fa7 | Michael Hanselmann | rpc.Shutdown() |
530 | a4af651e | Iustin Pop | finally: |
531 | 15486fa7 | Michael Hanselmann | utils.RemovePidFile(constants.MASTERD_PID) |
532 | 227647ac | Guido Trotter | utils.RemoveFile(constants.MASTER_SOCKET) |
533 | a4af651e | Iustin Pop | |
534 | ffeffa1d | Iustin Pop | |
535 | ffeffa1d | Iustin Pop | if __name__ == "__main__": |
536 | ffeffa1d | Iustin Pop | main() |