Statistics
| Branch: | Tag: | Revision:

root / daemons / ganeti-masterd @ 19b9ba9a

History | View | Annotate | Download (18 kB)

1 834f8b67 Iustin Pop
#!/usr/bin/python
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 7260cfbe Iustin Pop
# pylint: disable-msg=C0103
30 7260cfbe Iustin Pop
# C0103: Invalid name ganeti-masterd
31 ffeffa1d Iustin Pop
32 bbfd0568 René Nussbaumer
import grp
33 bbfd0568 René Nussbaumer
import os
34 bbfd0568 René Nussbaumer
import pwd
35 c1f2901b Iustin Pop
import sys
36 cdd7f900 Guido Trotter
import socket
37 ffeffa1d Iustin Pop
import time
38 bbfd0568 René Nussbaumer
import tempfile
39 96cb3986 Michael Hanselmann
import logging
40 ffeffa1d Iustin Pop
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 04ccf5e9 Guido Trotter
from ganeti import daemon
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 a744b676 Manuel Franceschini
from ganeti import netutils
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 e0dbb89b Guido Trotter
  # pylint: disable-msg=W0221
68 7e5a6e86 Guido Trotter
  def RunTask(self, server, message, client):
69 23e50d39 Michael Hanselmann
    """Process the request.
70 23e50d39 Michael Hanselmann
71 23e50d39 Michael Hanselmann
    """
72 7e5a6e86 Guido Trotter
    client_ops = ClientOps(server)
73 7e5a6e86 Guido Trotter
74 23e50d39 Michael Hanselmann
    try:
75 7e5a6e86 Guido Trotter
      (method, args) = luxi.ParseRequest(message)
76 7e5a6e86 Guido Trotter
    except luxi.ProtocolError, err:
77 7e5a6e86 Guido Trotter
      logging.error("Protocol Error: %s", err)
78 7e5a6e86 Guido Trotter
      client.close_log()
79 7e5a6e86 Guido Trotter
      return
80 7e5a6e86 Guido Trotter
81 7e5a6e86 Guido Trotter
    success = False
82 7e5a6e86 Guido Trotter
    try:
83 7e5a6e86 Guido Trotter
      result = client_ops.handle_request(method, args)
84 7e5a6e86 Guido Trotter
      success = True
85 7e5a6e86 Guido Trotter
    except errors.GenericError, err:
86 7e5a6e86 Guido Trotter
      logging.exception("Unexpected exception")
87 7e5a6e86 Guido Trotter
      success = False
88 7e5a6e86 Guido Trotter
      result = errors.EncodeException(err)
89 7e5a6e86 Guido Trotter
    except:
90 7e5a6e86 Guido Trotter
      logging.exception("Unexpected exception")
91 7e5a6e86 Guido Trotter
      err = sys.exc_info()
92 7e5a6e86 Guido Trotter
      result = "Caught exception: %s" % str(err[1])
93 7e5a6e86 Guido Trotter
94 7e5a6e86 Guido Trotter
    try:
95 7e5a6e86 Guido Trotter
      reply = luxi.FormatResponse(success, result)
96 7e5a6e86 Guido Trotter
      client.send_message(reply)
97 7e5a6e86 Guido Trotter
      # awake the main thread so that it can write out the data.
98 7e5a6e86 Guido Trotter
      server.awaker.signal()
99 e0dbb89b Guido Trotter
    except: # pylint: disable-msg=W0702
100 7e5a6e86 Guido Trotter
      logging.exception("Send error")
101 7e5a6e86 Guido Trotter
      client.close_log()
102 7e5a6e86 Guido Trotter
103 7e5a6e86 Guido Trotter
104 7e5a6e86 Guido Trotter
class MasterClientHandler(daemon.AsyncTerminatedMessageStream):
105 7e5a6e86 Guido Trotter
  """Handler for master peers.
106 7e5a6e86 Guido Trotter
107 7e5a6e86 Guido Trotter
  """
108 7e5a6e86 Guido Trotter
  _MAX_UNHANDLED = 1
109 7e5a6e86 Guido Trotter
  def __init__(self, server, connected_socket, client_address, family):
110 7e5a6e86 Guido Trotter
    daemon.AsyncTerminatedMessageStream.__init__(self, connected_socket,
111 7e5a6e86 Guido Trotter
                                                 client_address,
112 7e5a6e86 Guido Trotter
                                                 constants.LUXI_EOM,
113 7e5a6e86 Guido Trotter
                                                 family, self._MAX_UNHANDLED)
114 7e5a6e86 Guido Trotter
    self.server = server
115 7e5a6e86 Guido Trotter
116 7e5a6e86 Guido Trotter
  def handle_message(self, message, _):
117 b2e8a4d9 Michael Hanselmann
    self.server.request_workers.AddTask((self.server, message, self))
118 23e50d39 Michael Hanselmann
119 23e50d39 Michael Hanselmann
120 cdd7f900 Guido Trotter
class MasterServer(daemon.AsyncStreamServer):
121 cdd7f900 Guido Trotter
  """Master Server.
122 ffeffa1d Iustin Pop
123 cdd7f900 Guido Trotter
  This is the main asynchronous master server. It handles connections to the
124 cdd7f900 Guido Trotter
  master socket.
125 ffeffa1d Iustin Pop
126 ffeffa1d Iustin Pop
  """
127 7e5a6e86 Guido Trotter
  family = socket.AF_UNIX
128 7e5a6e86 Guido Trotter
129 7e5a6e86 Guido Trotter
  def __init__(self, mainloop, address, uid, gid):
130 cdd7f900 Guido Trotter
    """MasterServer constructor
131 ce862cd5 Guido Trotter
132 cdd7f900 Guido Trotter
    @type mainloop: ganeti.daemon.Mainloop
133 cdd7f900 Guido Trotter
    @param mainloop: Mainloop used to poll for I/O events
134 cdd7f900 Guido Trotter
    @param address: the unix socket address to bind the MasterServer to
135 bbfd0568 René Nussbaumer
    @param uid: The uid of the owner of the socket
136 bbfd0568 René Nussbaumer
    @param gid: The gid of the owner of the socket
137 ce862cd5 Guido Trotter
138 ce862cd5 Guido Trotter
    """
139 bbfd0568 René Nussbaumer
    temp_name = tempfile.mktemp(dir=os.path.dirname(address))
140 7e5a6e86 Guido Trotter
    daemon.AsyncStreamServer.__init__(self, self.family, temp_name)
141 bbfd0568 René Nussbaumer
    os.chmod(temp_name, 0770)
142 bbfd0568 René Nussbaumer
    os.chown(temp_name, uid, gid)
143 bbfd0568 René Nussbaumer
    os.rename(temp_name, address)
144 bbfd0568 René Nussbaumer
145 cdd7f900 Guido Trotter
    self.mainloop = mainloop
146 7e5a6e86 Guido Trotter
    self.awaker = daemon.AsyncAwaker()
147 50a3fbb2 Michael Hanselmann
148 50a3fbb2 Michael Hanselmann
    # We'll only start threads once we've forked.
149 9113300d Michael Hanselmann
    self.context = None
150 23e50d39 Michael Hanselmann
    self.request_workers = None
151 50a3fbb2 Michael Hanselmann
152 cdd7f900 Guido Trotter
  def handle_connection(self, connected_socket, client_address):
153 7e5a6e86 Guido Trotter
    # TODO: add connection count and limit the number of open connections to a
154 7e5a6e86 Guido Trotter
    # maximum number to avoid breaking for lack of file descriptors or memory.
155 7e5a6e86 Guido Trotter
    MasterClientHandler(self, connected_socket, client_address, self.family)
156 cdd7f900 Guido Trotter
157 50a3fbb2 Michael Hanselmann
  def setup_queue(self):
158 9113300d Michael Hanselmann
    self.context = GanetiContext()
159 89e2b4d2 Michael Hanselmann
    self.request_workers = workerpool.WorkerPool("ClientReq",
160 89e2b4d2 Michael Hanselmann
                                                 CLIENT_REQUEST_WORKERS,
161 23e50d39 Michael Hanselmann
                                                 ClientRequestWorker)
162 ffeffa1d Iustin Pop
163 c1f2901b Iustin Pop
  def server_cleanup(self):
164 c1f2901b Iustin Pop
    """Cleanup the server.
165 c1f2901b Iustin Pop
166 c1f2901b Iustin Pop
    This involves shutting down the processor threads and the master
167 c1f2901b Iustin Pop
    socket.
168 c1f2901b Iustin Pop
169 c1f2901b Iustin Pop
    """
170 50a3fbb2 Michael Hanselmann
    try:
171 cdd7f900 Guido Trotter
      self.close()
172 50a3fbb2 Michael Hanselmann
    finally:
173 23e50d39 Michael Hanselmann
      if self.request_workers:
174 36088c4c Michael Hanselmann
        self.request_workers.TerminateWorkers()
175 9113300d Michael Hanselmann
      if self.context:
176 9113300d Michael Hanselmann
        self.context.jobqueue.Shutdown()
177 ffeffa1d Iustin Pop
178 ffeffa1d Iustin Pop
179 ffeffa1d Iustin Pop
class ClientOps:
180 ffeffa1d Iustin Pop
  """Class holding high-level client operations."""
181 ffeffa1d Iustin Pop
  def __init__(self, server):
182 ffeffa1d Iustin Pop
    self.server = server
183 ffeffa1d Iustin Pop
184 7260cfbe Iustin Pop
  def handle_request(self, method, args): # pylint: disable-msg=R0911
185 9113300d Michael Hanselmann
    queue = self.server.context.jobqueue
186 0bbe448c Michael Hanselmann
187 0bbe448c Michael Hanselmann
    # TODO: Parameter validation
188 0bbe448c Michael Hanselmann
189 7260cfbe Iustin Pop
    # TODO: Rewrite to not exit in each 'if/elif' branch
190 7260cfbe Iustin Pop
191 0bbe448c Michael Hanselmann
    if method == luxi.REQ_SUBMIT_JOB:
192 e566ddbd Iustin Pop
      logging.info("Received new job")
193 0bbe448c Michael Hanselmann
      ops = [opcodes.OpCode.LoadOpCode(state) for state in args]
194 4c848b18 Michael Hanselmann
      return queue.SubmitJob(ops)
195 ffeffa1d Iustin Pop
196 2971c913 Iustin Pop
    if method == luxi.REQ_SUBMIT_MANY_JOBS:
197 2971c913 Iustin Pop
      logging.info("Received multiple jobs")
198 2971c913 Iustin Pop
      jobs = []
199 2971c913 Iustin Pop
      for ops in args:
200 2971c913 Iustin Pop
        jobs.append([opcodes.OpCode.LoadOpCode(state) for state in ops])
201 2971c913 Iustin Pop
      return queue.SubmitManyJobs(jobs)
202 2971c913 Iustin Pop
203 0bbe448c Michael Hanselmann
    elif method == luxi.REQ_CANCEL_JOB:
204 3a2c7775 Michael Hanselmann
      job_id = args
205 e566ddbd Iustin Pop
      logging.info("Received job cancel request for %s", job_id)
206 0bbe448c Michael Hanselmann
      return queue.CancelJob(job_id)
207 ffeffa1d Iustin Pop
208 0bbe448c Michael Hanselmann
    elif method == luxi.REQ_ARCHIVE_JOB:
209 3a2c7775 Michael Hanselmann
      job_id = args
210 e566ddbd Iustin Pop
      logging.info("Received job archive request for %s", job_id)
211 0bbe448c Michael Hanselmann
      return queue.ArchiveJob(job_id)
212 0bbe448c Michael Hanselmann
213 07cd723a Iustin Pop
    elif method == luxi.REQ_AUTOARCHIVE_JOBS:
214 f8ad5591 Michael Hanselmann
      (age, timeout) = args
215 e566ddbd Iustin Pop
      logging.info("Received job autoarchive request for age %s, timeout %s",
216 e566ddbd Iustin Pop
                   age, timeout)
217 f8ad5591 Michael Hanselmann
      return queue.AutoArchiveJobs(age, timeout)
218 07cd723a Iustin Pop
219 dfe57c22 Michael Hanselmann
    elif method == luxi.REQ_WAIT_FOR_JOB_CHANGE:
220 5c735209 Iustin Pop
      (job_id, fields, prev_job_info, prev_log_serial, timeout) = args
221 e566ddbd Iustin Pop
      logging.info("Received job poll request for %s", job_id)
222 6c5a7090 Michael Hanselmann
      return queue.WaitForJobChanges(job_id, fields, prev_job_info,
223 5c735209 Iustin Pop
                                     prev_log_serial, timeout)
224 dfe57c22 Michael Hanselmann
225 0bbe448c Michael Hanselmann
    elif method == luxi.REQ_QUERY_JOBS:
226 0bbe448c Michael Hanselmann
      (job_ids, fields) = args
227 e566ddbd Iustin Pop
      if isinstance(job_ids, (tuple, list)) and job_ids:
228 1f864b60 Iustin Pop
        msg = utils.CommaJoin(job_ids)
229 e566ddbd Iustin Pop
      else:
230 e566ddbd Iustin Pop
        msg = str(job_ids)
231 e566ddbd Iustin Pop
      logging.info("Received job query request for %s", msg)
232 0bbe448c Michael Hanselmann
      return queue.QueryJobs(job_ids, fields)
233 0bbe448c Michael Hanselmann
234 ee6c7b94 Michael Hanselmann
    elif method == luxi.REQ_QUERY_INSTANCES:
235 ec79568d Iustin Pop
      (names, fields, use_locking) = args
236 e566ddbd Iustin Pop
      logging.info("Received instance query request for %s", names)
237 77921a95 Iustin Pop
      if use_locking:
238 debac808 Iustin Pop
        raise errors.OpPrereqError("Sync queries are not allowed",
239 debac808 Iustin Pop
                                   errors.ECODE_INVAL)
240 ec79568d Iustin Pop
      op = opcodes.OpQueryInstances(names=names, output_fields=fields,
241 ec79568d Iustin Pop
                                    use_locking=use_locking)
242 ee6c7b94 Michael Hanselmann
      return self._Query(op)
243 ee6c7b94 Michael Hanselmann
244 02f7fe54 Michael Hanselmann
    elif method == luxi.REQ_QUERY_NODES:
245 ec79568d Iustin Pop
      (names, fields, use_locking) = args
246 e566ddbd Iustin Pop
      logging.info("Received node query request for %s", names)
247 77921a95 Iustin Pop
      if use_locking:
248 debac808 Iustin Pop
        raise errors.OpPrereqError("Sync queries are not allowed",
249 debac808 Iustin Pop
                                   errors.ECODE_INVAL)
250 ec79568d Iustin Pop
      op = opcodes.OpQueryNodes(names=names, output_fields=fields,
251 ec79568d Iustin Pop
                                use_locking=use_locking)
252 02f7fe54 Michael Hanselmann
      return self._Query(op)
253 02f7fe54 Michael Hanselmann
254 32f93223 Michael Hanselmann
    elif method == luxi.REQ_QUERY_EXPORTS:
255 ec79568d Iustin Pop
      nodes, use_locking = args
256 77921a95 Iustin Pop
      if use_locking:
257 debac808 Iustin Pop
        raise errors.OpPrereqError("Sync queries are not allowed",
258 debac808 Iustin Pop
                                   errors.ECODE_INVAL)
259 e566ddbd Iustin Pop
      logging.info("Received exports query request")
260 ec79568d Iustin Pop
      op = opcodes.OpQueryExports(nodes=nodes, use_locking=use_locking)
261 32f93223 Michael Hanselmann
      return self._Query(op)
262 32f93223 Michael Hanselmann
263 ae5849b5 Michael Hanselmann
    elif method == luxi.REQ_QUERY_CONFIG_VALUES:
264 ae5849b5 Michael Hanselmann
      fields = args
265 e566ddbd Iustin Pop
      logging.info("Received config values query request for %s", fields)
266 ae5849b5 Michael Hanselmann
      op = opcodes.OpQueryConfigValues(output_fields=fields)
267 ae5849b5 Michael Hanselmann
      return self._Query(op)
268 ae5849b5 Michael Hanselmann
269 66baeccc Iustin Pop
    elif method == luxi.REQ_QUERY_CLUSTER_INFO:
270 e566ddbd Iustin Pop
      logging.info("Received cluster info query request")
271 66baeccc Iustin Pop
      op = opcodes.OpQueryClusterInfo()
272 66baeccc Iustin Pop
      return self._Query(op)
273 66baeccc Iustin Pop
274 7699c3af Iustin Pop
    elif method == luxi.REQ_QUERY_TAGS:
275 7699c3af Iustin Pop
      kind, name = args
276 7699c3af Iustin Pop
      logging.info("Received tags query request")
277 7699c3af Iustin Pop
      op = opcodes.OpGetTags(kind=kind, name=name)
278 7699c3af Iustin Pop
      return self._Query(op)
279 7699c3af Iustin Pop
280 19b9ba9a Michael Hanselmann
    elif method == luxi.REQ_QUERY_LOCKS:
281 19b9ba9a Michael Hanselmann
      (fields, sync) = args
282 19b9ba9a Michael Hanselmann
      logging.info("Received locks query request")
283 19b9ba9a Michael Hanselmann
      return self.server.context.glm.QueryLocks(fields, sync)
284 19b9ba9a Michael Hanselmann
285 3ccafd0e Iustin Pop
    elif method == luxi.REQ_QUEUE_SET_DRAIN_FLAG:
286 3ccafd0e Iustin Pop
      drain_flag = args
287 e566ddbd Iustin Pop
      logging.info("Received queue drain flag change request to %s",
288 e566ddbd Iustin Pop
                   drain_flag)
289 3ccafd0e Iustin Pop
      return queue.SetDrainFlag(drain_flag)
290 3ccafd0e Iustin Pop
291 05e50653 Michael Hanselmann
    elif method == luxi.REQ_SET_WATCHER_PAUSE:
292 05e50653 Michael Hanselmann
      (until, ) = args
293 05e50653 Michael Hanselmann
294 05e50653 Michael Hanselmann
      if until is None:
295 05e50653 Michael Hanselmann
        logging.info("Received request to no longer pause the watcher")
296 05e50653 Michael Hanselmann
      else:
297 05e50653 Michael Hanselmann
        if not isinstance(until, (int, float)):
298 05e50653 Michael Hanselmann
          raise TypeError("Duration must be an integer or float")
299 05e50653 Michael Hanselmann
300 05e50653 Michael Hanselmann
        if until < time.time():
301 05e50653 Michael Hanselmann
          raise errors.GenericError("Unable to set pause end time in the past")
302 05e50653 Michael Hanselmann
303 05e50653 Michael Hanselmann
        logging.info("Received request to pause the watcher until %s", until)
304 05e50653 Michael Hanselmann
305 05e50653 Michael Hanselmann
      return _SetWatcherPause(until)
306 05e50653 Michael Hanselmann
307 0bbe448c Michael Hanselmann
    else:
308 e566ddbd Iustin Pop
      logging.info("Received invalid request '%s'", method)
309 e566ddbd Iustin Pop
      raise ValueError("Invalid operation '%s'" % method)
310 ffeffa1d Iustin Pop
311 ee6c7b94 Michael Hanselmann
  def _Query(self, op):
312 ee6c7b94 Michael Hanselmann
    """Runs the specified opcode and returns the result.
313 ee6c7b94 Michael Hanselmann
314 ee6c7b94 Michael Hanselmann
    """
315 adfa97e3 Guido Trotter
    # Queries don't have a job id
316 adfa97e3 Guido Trotter
    proc = mcpu.Processor(self.server.context, None)
317 031a3e57 Michael Hanselmann
    return proc.ExecOpCode(op, None)
318 ee6c7b94 Michael Hanselmann
319 ffeffa1d Iustin Pop
320 39dcf2ef Guido Trotter
class GanetiContext(object):
321 39dcf2ef Guido Trotter
  """Context common to all ganeti threads.
322 39dcf2ef Guido Trotter
323 39dcf2ef Guido Trotter
  This class creates and holds common objects shared by all threads.
324 39dcf2ef Guido Trotter
325 39dcf2ef Guido Trotter
  """
326 7260cfbe Iustin Pop
  # pylint: disable-msg=W0212
327 7260cfbe Iustin Pop
  # we do want to ensure a singleton here
328 39dcf2ef Guido Trotter
  _instance = None
329 39dcf2ef Guido Trotter
330 39dcf2ef Guido Trotter
  def __init__(self):
331 39dcf2ef Guido Trotter
    """Constructs a new GanetiContext object.
332 39dcf2ef Guido Trotter
333 39dcf2ef Guido Trotter
    There should be only a GanetiContext object at any time, so this
334 39dcf2ef Guido Trotter
    function raises an error if this is not the case.
335 39dcf2ef Guido Trotter
336 39dcf2ef Guido Trotter
    """
337 39dcf2ef Guido Trotter
    assert self.__class__._instance is None, "double GanetiContext instance"
338 39dcf2ef Guido Trotter
339 9113300d Michael Hanselmann
    # Create global configuration object
340 39dcf2ef Guido Trotter
    self.cfg = config.ConfigWriter()
341 9113300d Michael Hanselmann
342 9113300d Michael Hanselmann
    # Locking manager
343 984f7c32 Guido Trotter
    self.glm = locking.GanetiLockManager(
344 39dcf2ef Guido Trotter
                self.cfg.GetNodeList(),
345 39dcf2ef Guido Trotter
                self.cfg.GetInstanceList())
346 39dcf2ef Guido Trotter
347 9113300d Michael Hanselmann
    # Job queue
348 9113300d Michael Hanselmann
    self.jobqueue = jqueue.JobQueue(self)
349 9113300d Michael Hanselmann
350 39dcf2ef Guido Trotter
    # setting this also locks the class against attribute modifications
351 39dcf2ef Guido Trotter
    self.__class__._instance = self
352 39dcf2ef Guido Trotter
353 39dcf2ef Guido Trotter
  def __setattr__(self, name, value):
354 39dcf2ef Guido Trotter
    """Setting GanetiContext attributes is forbidden after initialization.
355 39dcf2ef Guido Trotter
356 39dcf2ef Guido Trotter
    """
357 39dcf2ef Guido Trotter
    assert self.__class__._instance is None, "Attempt to modify Ganeti Context"
358 39dcf2ef Guido Trotter
    object.__setattr__(self, name, value)
359 39dcf2ef Guido Trotter
360 0debfb35 Guido Trotter
  def AddNode(self, node, ec_id):
361 d8470559 Michael Hanselmann
    """Adds a node to the configuration and lock manager.
362 d8470559 Michael Hanselmann
363 d8470559 Michael Hanselmann
    """
364 d8470559 Michael Hanselmann
    # Add it to the configuration
365 0debfb35 Guido Trotter
    self.cfg.AddNode(node, ec_id)
366 d8470559 Michael Hanselmann
367 c36176cc Michael Hanselmann
    # If preseeding fails it'll not be added
368 99aabbed Iustin Pop
    self.jobqueue.AddNode(node)
369 c36176cc Michael Hanselmann
370 d8470559 Michael Hanselmann
    # Add the new node to the Ganeti Lock Manager
371 d8470559 Michael Hanselmann
    self.glm.add(locking.LEVEL_NODE, node.name)
372 d8470559 Michael Hanselmann
373 d8470559 Michael Hanselmann
  def ReaddNode(self, node):
374 d8470559 Michael Hanselmann
    """Updates a node that's already in the configuration
375 d8470559 Michael Hanselmann
376 d8470559 Michael Hanselmann
    """
377 c36176cc Michael Hanselmann
    # Synchronize the queue again
378 99aabbed Iustin Pop
    self.jobqueue.AddNode(node)
379 d8470559 Michael Hanselmann
380 d8470559 Michael Hanselmann
  def RemoveNode(self, name):
381 d8470559 Michael Hanselmann
    """Removes a node from the configuration and lock manager.
382 d8470559 Michael Hanselmann
383 d8470559 Michael Hanselmann
    """
384 d8470559 Michael Hanselmann
    # Remove node from configuration
385 d8470559 Michael Hanselmann
    self.cfg.RemoveNode(name)
386 d8470559 Michael Hanselmann
387 c36176cc Michael Hanselmann
    # Notify job queue
388 c36176cc Michael Hanselmann
    self.jobqueue.RemoveNode(name)
389 c36176cc Michael Hanselmann
390 d8470559 Michael Hanselmann
    # Remove the node from the Ganeti Lock Manager
391 d8470559 Michael Hanselmann
    self.glm.remove(locking.LEVEL_NODE, name)
392 d8470559 Michael Hanselmann
393 39dcf2ef Guido Trotter
394 05e50653 Michael Hanselmann
def _SetWatcherPause(until):
395 05e50653 Michael Hanselmann
  """Creates or removes the watcher pause file.
396 05e50653 Michael Hanselmann
397 05e50653 Michael Hanselmann
  @type until: None or int
398 05e50653 Michael Hanselmann
  @param until: Unix timestamp saying until when the watcher shouldn't run
399 05e50653 Michael Hanselmann
400 05e50653 Michael Hanselmann
  """
401 05e50653 Michael Hanselmann
  if until is None:
402 05e50653 Michael Hanselmann
    utils.RemoveFile(constants.WATCHER_PAUSEFILE)
403 05e50653 Michael Hanselmann
  else:
404 05e50653 Michael Hanselmann
    utils.WriteFile(constants.WATCHER_PAUSEFILE,
405 05e50653 Michael Hanselmann
                    data="%d\n" % (until, ))
406 05e50653 Michael Hanselmann
407 28b498cd Michael Hanselmann
  return until
408 28b498cd Michael Hanselmann
409 05e50653 Michael Hanselmann
410 e0e916fe Iustin Pop
@rpc.RunWithRPC
411 36205981 Iustin Pop
def CheckAgreement():
412 36205981 Iustin Pop
  """Check the agreement on who is the master.
413 36205981 Iustin Pop
414 36205981 Iustin Pop
  The function uses a very simple algorithm: we must get more positive
415 36205981 Iustin Pop
  than negative answers. Since in most of the cases we are the master,
416 36205981 Iustin Pop
  we'll use our own config file for getting the node list. In the
417 36205981 Iustin Pop
  future we could collect the current node list from our (possibly
418 36205981 Iustin Pop
  obsolete) known nodes.
419 36205981 Iustin Pop
420 d7cdb55d Iustin Pop
  In order to account for cold-start of all nodes, we retry for up to
421 d7cdb55d Iustin Pop
  a minute until we get a real answer as the top-voted one. If the
422 d7cdb55d Iustin Pop
  nodes are more out-of-sync, for now manual startup of the master
423 d7cdb55d Iustin Pop
  should be attempted.
424 d7cdb55d Iustin Pop
425 d7cdb55d Iustin Pop
  Note that for a even number of nodes cluster, we need at least half
426 d7cdb55d Iustin Pop
  of the nodes (beside ourselves) to vote for us. This creates a
427 d7cdb55d Iustin Pop
  problem on two-node clusters, since in this case we require the
428 d7cdb55d Iustin Pop
  other node to be up too to confirm our status.
429 d7cdb55d Iustin Pop
430 36205981 Iustin Pop
  """
431 a744b676 Manuel Franceschini
  myself = netutils.HostInfo().name
432 36205981 Iustin Pop
  #temp instantiation of a config writer, used only to get the node list
433 36205981 Iustin Pop
  cfg = config.ConfigWriter()
434 36205981 Iustin Pop
  node_list = cfg.GetNodeList()
435 36205981 Iustin Pop
  del cfg
436 d7cdb55d Iustin Pop
  retries = 6
437 d7cdb55d Iustin Pop
  while retries > 0:
438 d7cdb55d Iustin Pop
    votes = bootstrap.GatherMasterVotes(node_list)
439 d7cdb55d Iustin Pop
    if not votes:
440 d7cdb55d Iustin Pop
      # empty node list, this is a one node cluster
441 d7cdb55d Iustin Pop
      return True
442 d7cdb55d Iustin Pop
    if votes[0][0] is None:
443 d7cdb55d Iustin Pop
      retries -= 1
444 d7cdb55d Iustin Pop
      time.sleep(10)
445 36205981 Iustin Pop
      continue
446 d7cdb55d Iustin Pop
    break
447 d7cdb55d Iustin Pop
  if retries == 0:
448 e09fdcfa Iustin Pop
    logging.critical("Cluster inconsistent, most of the nodes didn't answer"
449 e09fdcfa Iustin Pop
                     " after multiple retries. Aborting startup")
450 d8f5a37d Iustin Pop
    logging.critical("Use the --no-voting option if you understand what"
451 d8f5a37d Iustin Pop
                     " effects it has on the cluster state")
452 e09fdcfa Iustin Pop
    return False
453 d7cdb55d Iustin Pop
  # here a real node is at the top of the list
454 d7cdb55d Iustin Pop
  all_votes = sum(item[1] for item in votes)
455 d7cdb55d Iustin Pop
  top_node, top_votes = votes[0]
456 8a20c732 Michael Hanselmann
457 d7cdb55d Iustin Pop
  result = False
458 d7cdb55d Iustin Pop
  if top_node != myself:
459 d7cdb55d Iustin Pop
    logging.critical("It seems we are not the master (top-voted node"
460 bbe19c17 Iustin Pop
                     " is %s with %d out of %d votes)", top_node, top_votes,
461 bbe19c17 Iustin Pop
                     all_votes)
462 d7cdb55d Iustin Pop
  elif top_votes < all_votes - top_votes:
463 36205981 Iustin Pop
    logging.critical("It seems we are not the master (%d votes for,"
464 d7cdb55d Iustin Pop
                     " %d votes against)", top_votes, all_votes - top_votes)
465 d7cdb55d Iustin Pop
  else:
466 d7cdb55d Iustin Pop
    result = True
467 d7cdb55d Iustin Pop
468 d7cdb55d Iustin Pop
  return result
469 36205981 Iustin Pop
470 6c948699 Michael Hanselmann
471 340f4757 Iustin Pop
@rpc.RunWithRPC
472 340f4757 Iustin Pop
def ActivateMasterIP():
473 340f4757 Iustin Pop
  # activate ip
474 340f4757 Iustin Pop
  master_node = ssconf.SimpleStore().GetMasterNode()
475 340f4757 Iustin Pop
  result = rpc.RpcRunner.call_node_start_master(master_node, False, False)
476 340f4757 Iustin Pop
  msg = result.fail_msg
477 340f4757 Iustin Pop
  if msg:
478 340f4757 Iustin Pop
    logging.error("Can't activate master IP address: %s", msg)
479 340f4757 Iustin Pop
480 340f4757 Iustin Pop
481 ed0efaa5 Michael Hanselmann
def CheckMasterd(options, args):
482 ed0efaa5 Michael Hanselmann
  """Initial checks whether to run or exit with a failure.
483 ed0efaa5 Michael Hanselmann
484 ed0efaa5 Michael Hanselmann
  """
485 f93427cd Iustin Pop
  if args: # masterd doesn't take any arguments
486 f93427cd Iustin Pop
    print >> sys.stderr, ("Usage: %s [-f] [-d]" % sys.argv[0])
487 f93427cd Iustin Pop
    sys.exit(constants.EXIT_FAILURE)
488 f93427cd Iustin Pop
489 ed0efaa5 Michael Hanselmann
  ssconf.CheckMaster(options.debug)
490 ed0efaa5 Michael Hanselmann
491 bbfd0568 René Nussbaumer
  try:
492 bbfd0568 René Nussbaumer
    options.uid = pwd.getpwnam(constants.MASTERD_USER).pw_uid
493 bbfd0568 René Nussbaumer
    options.gid = grp.getgrnam(constants.DAEMONS_GROUP).gr_gid
494 bbfd0568 René Nussbaumer
  except KeyError:
495 bbfd0568 René Nussbaumer
    print >> sys.stderr, ("User or group not existing on system: %s:%s" %
496 bbfd0568 René Nussbaumer
                          (constants.MASTERD_USER, constants.DAEMONS_GROUP))
497 bbfd0568 René Nussbaumer
    sys.exit(constants.EXIT_FAILURE)
498 bbfd0568 René Nussbaumer
499 bbfd0568 René Nussbaumer
500 ed0efaa5 Michael Hanselmann
  # If CheckMaster didn't fail we believe we are the master, but we have to
501 ed0efaa5 Michael Hanselmann
  # confirm with the other nodes.
502 ed0efaa5 Michael Hanselmann
  if options.no_voting:
503 ed0efaa5 Michael Hanselmann
    if options.yes_do_it:
504 ed0efaa5 Michael Hanselmann
      return
505 ed0efaa5 Michael Hanselmann
506 ed0efaa5 Michael Hanselmann
    sys.stdout.write("The 'no voting' option has been selected.\n")
507 ed0efaa5 Michael Hanselmann
    sys.stdout.write("This is dangerous, please confirm by"
508 ed0efaa5 Michael Hanselmann
                     " typing uppercase 'yes': ")
509 ed0efaa5 Michael Hanselmann
    sys.stdout.flush()
510 ed0efaa5 Michael Hanselmann
511 ed0efaa5 Michael Hanselmann
    confirmation = sys.stdin.readline().strip()
512 ed0efaa5 Michael Hanselmann
    if confirmation != "YES":
513 7260cfbe Iustin Pop
      print >> sys.stderr, "Aborting."
514 ed0efaa5 Michael Hanselmann
      sys.exit(constants.EXIT_FAILURE)
515 ed0efaa5 Michael Hanselmann
516 ed0efaa5 Michael Hanselmann
    return
517 ed0efaa5 Michael Hanselmann
518 ed0efaa5 Michael Hanselmann
  # CheckAgreement uses RPC and threads, hence it needs to be run in a separate
519 ed0efaa5 Michael Hanselmann
  # process before we call utils.Daemonize in the current process.
520 e0e916fe Iustin Pop
  if not utils.RunInSeparateProcess(CheckAgreement):
521 ed0efaa5 Michael Hanselmann
    sys.exit(constants.EXIT_FAILURE)
522 ed0efaa5 Michael Hanselmann
523 340f4757 Iustin Pop
  # ActivateMasterIP also uses RPC/threads, so we run it again via a
524 340f4757 Iustin Pop
  # separate process.
525 340f4757 Iustin Pop
526 340f4757 Iustin Pop
  # TODO: decide whether failure to activate the master IP is a fatal error
527 340f4757 Iustin Pop
  utils.RunInSeparateProcess(ActivateMasterIP)
528 340f4757 Iustin Pop
529 ed0efaa5 Michael Hanselmann
530 33231500 Michael Hanselmann
def ExecMasterd(options, args): # pylint: disable-msg=W0613
531 6c948699 Michael Hanselmann
  """Main master daemon function, executed with the PID file held.
532 3b316acb Iustin Pop
533 04ccf5e9 Guido Trotter
  """
534 04ccf5e9 Guido Trotter
  # This is safe to do as the pid file guarantees against
535 04ccf5e9 Guido Trotter
  # concurrent execution.
536 04ccf5e9 Guido Trotter
  utils.RemoveFile(constants.MASTER_SOCKET)
537 b1b6ea87 Iustin Pop
538 cdd7f900 Guido Trotter
  mainloop = daemon.Mainloop()
539 7e5a6e86 Guido Trotter
  master = MasterServer(mainloop, constants.MASTER_SOCKET,
540 bbfd0568 René Nussbaumer
                        options.uid, options.gid)
541 04ccf5e9 Guido Trotter
  try:
542 15486fa7 Michael Hanselmann
    rpc.Init()
543 4331f6cd Michael Hanselmann
    try:
544 15486fa7 Michael Hanselmann
      master.setup_queue()
545 15486fa7 Michael Hanselmann
      try:
546 cdd7f900 Guido Trotter
        mainloop.Run()
547 15486fa7 Michael Hanselmann
      finally:
548 15486fa7 Michael Hanselmann
        master.server_cleanup()
549 4331f6cd Michael Hanselmann
    finally:
550 15486fa7 Michael Hanselmann
      rpc.Shutdown()
551 a4af651e Iustin Pop
  finally:
552 227647ac Guido Trotter
    utils.RemoveFile(constants.MASTER_SOCKET)
553 a4af651e Iustin Pop
554 ffeffa1d Iustin Pop
555 04ccf5e9 Guido Trotter
def main():
556 04ccf5e9 Guido Trotter
  """Main function"""
557 04ccf5e9 Guido Trotter
  parser = OptionParser(description="Ganeti master daemon",
558 04ccf5e9 Guido Trotter
                        usage="%prog [-f] [-d]",
559 04ccf5e9 Guido Trotter
                        version="%%prog (ganeti) %s" %
560 04ccf5e9 Guido Trotter
                        constants.RELEASE_VERSION)
561 04ccf5e9 Guido Trotter
  parser.add_option("--no-voting", dest="no_voting",
562 04ccf5e9 Guido Trotter
                    help="Do not check that the nodes agree on this node"
563 04ccf5e9 Guido Trotter
                    " being the master and start the daemon unconditionally",
564 04ccf5e9 Guido Trotter
                    default=False, action="store_true")
565 04ccf5e9 Guido Trotter
  parser.add_option("--yes-do-it", dest="yes_do_it",
566 04ccf5e9 Guido Trotter
                    help="Override interactive check for --no-voting",
567 04ccf5e9 Guido Trotter
                    default=False, action="store_true")
568 04ccf5e9 Guido Trotter
  dirs = [(constants.RUN_GANETI_DIR, constants.RUN_DIRS_MODE),
569 04ccf5e9 Guido Trotter
          (constants.SOCKET_DIR, constants.SOCKET_DIR_MODE),
570 04ccf5e9 Guido Trotter
         ]
571 04ccf5e9 Guido Trotter
  daemon.GenericMain(constants.MASTERD, parser, dirs,
572 30dabd03 Michael Hanselmann
                     CheckMasterd, ExecMasterd,
573 30dabd03 Michael Hanselmann
                     multithreaded=True)
574 6c948699 Michael Hanselmann
575 04ccf5e9 Guido Trotter
576 ffeffa1d Iustin Pop
if __name__ == "__main__":
577 ffeffa1d Iustin Pop
  main()