Statistics
| Branch: | Tag: | Revision:

root / lib / server / masterd.py @ 87ed6b79

History | View | Annotate | Download (23.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2010, 2011, 2012 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Master daemon program.
23

24
Some classes deviates from the standard style guide since the
25
inheritance from parent classes requires it.
26

27
"""
28

    
29
# pylint: disable=C0103
30
# C0103: Invalid name ganeti-masterd
31

    
32
import grp
33
import os
34
import pwd
35
import sys
36
import socket
37
import time
38
import tempfile
39
import logging
40

    
41
from optparse import OptionParser
42

    
43
from ganeti import config
44
from ganeti import constants
45
from ganeti import daemon
46
from ganeti import mcpu
47
from ganeti import opcodes
48
from ganeti import jqueue
49
from ganeti import locking
50
from ganeti import luxi
51
import ganeti.rpc.errors as rpcerr
52
from ganeti import utils
53
from ganeti import errors
54
from ganeti import ssconf
55
from ganeti import workerpool
56
import ganeti.rpc.node as rpc
57
import ganeti.rpc.client as rpccl
58
from ganeti import bootstrap
59
from ganeti import netutils
60
from ganeti import objects
61
from ganeti import query
62
from ganeti import runtime
63
from ganeti import pathutils
64
from ganeti import ht
65

    
66
from ganeti.utils import version
67

    
68

    
69
CLIENT_REQUEST_WORKERS = 16
70

    
71
EXIT_NOTMASTER = constants.EXIT_NOTMASTER
72
EXIT_NODESETUP_ERROR = constants.EXIT_NODESETUP_ERROR
73

    
74

    
75
def _LogNewJob(status, info, ops):
76
  """Log information about a recently submitted job.
77

78
  """
79
  op_summary = utils.CommaJoin(op.Summary() for op in ops)
80

    
81
  if status:
82
    logging.info("New job with id %s, summary: %s", info, op_summary)
83
  else:
84
    logging.info("Failed to submit job, reason: '%s', summary: %s",
85
                 info, op_summary)
86

    
87

    
88
class ClientRequestWorker(workerpool.BaseWorker):
89
  # pylint: disable=W0221
90
  def RunTask(self, server, message, client):
91
    """Process the request.
92

93
    """
94
    client_ops = ClientOps(server)
95

    
96
    try:
97
      (method, args, ver) = rpccl.ParseRequest(message)
98
    except rpcerr.ProtocolError, err:
99
      logging.error("Protocol Error: %s", err)
100
      client.close_log()
101
      return
102

    
103
    success = False
104
    try:
105
      # Verify client's version if there was one in the request
106
      if ver is not None and ver != constants.LUXI_VERSION:
107
        raise errors.LuxiError("LUXI version mismatch, server %s, request %s" %
108
                               (constants.LUXI_VERSION, ver))
109

    
110
      result = client_ops.handle_request(method, args)
111
      success = True
112
    except errors.GenericError, err:
113
      logging.exception("Unexpected exception")
114
      success = False
115
      result = errors.EncodeException(err)
116
    except:
117
      logging.exception("Unexpected exception")
118
      err = sys.exc_info()
119
      result = "Caught exception: %s" % str(err[1])
120

    
121
    try:
122
      reply = rpccl.FormatResponse(success, result)
123
      client.send_message(reply)
124
      # awake the main thread so that it can write out the data.
125
      server.awaker.signal()
126
    except: # pylint: disable=W0702
127
      logging.exception("Send error")
128
      client.close_log()
129

    
130

    
131
class MasterClientHandler(daemon.AsyncTerminatedMessageStream):
132
  """Handler for master peers.
133

134
  """
135
  _MAX_UNHANDLED = 1
136

    
137
  def __init__(self, server, connected_socket, client_address, family):
138
    daemon.AsyncTerminatedMessageStream.__init__(self, connected_socket,
139
                                                 client_address,
140
                                                 constants.LUXI_EOM,
141
                                                 family, self._MAX_UNHANDLED)
142
    self.server = server
143

    
144
  def handle_message(self, message, _):
145
    self.server.request_workers.AddTask((self.server, message, self))
146

    
147

    
148
class _MasterShutdownCheck:
149
  """Logic for master daemon shutdown.
150

151
  """
152
  #: How long to wait between checks
153
  _CHECK_INTERVAL = 5.0
154

    
155
  #: How long to wait after all jobs are done (e.g. to give clients time to
156
  #: retrieve the job status)
157
  _SHUTDOWN_LINGER = 5.0
158

    
159
  def __init__(self):
160
    """Initializes this class.
161

162
    """
163
    self._had_active_jobs = None
164
    self._linger_timeout = None
165

    
166
  def __call__(self, jq_prepare_result):
167
    """Determines if master daemon is ready for shutdown.
168

169
    @param jq_prepare_result: Result of L{jqueue.JobQueue.PrepareShutdown}
170
    @rtype: None or number
171
    @return: None if master daemon is ready, timeout if the check must be
172
             repeated
173

174
    """
175
    if jq_prepare_result:
176
      # Check again shortly
177
      logging.info("Job queue has been notified for shutdown but is still"
178
                   " busy; next check in %s seconds", self._CHECK_INTERVAL)
179
      self._had_active_jobs = True
180
      return self._CHECK_INTERVAL
181

    
182
    if not self._had_active_jobs:
183
      # Can shut down as there were no active jobs on the first check
184
      return None
185

    
186
    # No jobs are running anymore, but maybe some clients want to collect some
187
    # information. Give them a short amount of time.
188
    if self._linger_timeout is None:
189
      self._linger_timeout = utils.RunningTimeout(self._SHUTDOWN_LINGER, True)
190

    
191
    remaining = self._linger_timeout.Remaining()
192

    
193
    logging.info("Job queue no longer busy; shutting down master daemon"
194
                 " in %s seconds", remaining)
195

    
196
    # TODO: Should the master daemon socket be closed at this point? Doing so
197
    # wouldn't affect existing connections.
198

    
199
    if remaining < 0:
200
      return None
201
    else:
202
      return remaining
203

    
204

    
205
class MasterServer(daemon.AsyncStreamServer):
206
  """Master Server.
207

208
  This is the main asynchronous master server. It handles connections to the
209
  master socket.
210

211
  """
212
  family = socket.AF_UNIX
213

    
214
  def __init__(self, address, uid, gid):
215
    """MasterServer constructor
216

217
    @param address: the unix socket address to bind the MasterServer to
218
    @param uid: The uid of the owner of the socket
219
    @param gid: The gid of the owner of the socket
220

221
    """
222
    temp_name = tempfile.mktemp(dir=os.path.dirname(address))
223
    daemon.AsyncStreamServer.__init__(self, self.family, temp_name)
224
    os.chmod(temp_name, 0770)
225
    os.chown(temp_name, uid, gid)
226
    os.rename(temp_name, address)
227

    
228
    self.awaker = daemon.AsyncAwaker()
229

    
230
    # We'll only start threads once we've forked.
231
    self.context = None
232
    self.request_workers = None
233

    
234
    self._shutdown_check = None
235

    
236
  def handle_connection(self, connected_socket, client_address):
237
    # TODO: add connection count and limit the number of open connections to a
238
    # maximum number to avoid breaking for lack of file descriptors or memory.
239
    MasterClientHandler(self, connected_socket, client_address, self.family)
240

    
241
  def setup_context(self):
242
    self.context = GanetiContext()
243
    self.request_workers = workerpool.WorkerPool("ClientReq",
244
                                                 CLIENT_REQUEST_WORKERS,
245
                                                 ClientRequestWorker)
246

    
247
  def WaitForShutdown(self):
248
    """Prepares server for shutdown.
249

250
    """
251
    if self._shutdown_check is None:
252
      self._shutdown_check = _MasterShutdownCheck()
253

    
254
    return self._shutdown_check(self.context.jobqueue.PrepareShutdown())
255

    
256
  def server_cleanup(self):
257
    """Cleanup the server.
258

259
    This involves shutting down the processor threads and the master
260
    socket.
261

262
    """
263
    try:
264
      self.close()
265
    finally:
266
      if self.request_workers:
267
        self.request_workers.TerminateWorkers()
268
      if self.context:
269
        self.context.jobqueue.Shutdown()
270
        self.context.livelock.close()
271

    
272

    
273
class ClientOps:
274
  """Class holding high-level client operations."""
275
  def __init__(self, server):
276
    self.server = server
277

    
278
  def handle_request(self, method, args): # pylint: disable=R0911
279
    context = self.server.context
280
    queue = context.jobqueue
281

    
282
    # TODO: Parameter validation
283
    if not isinstance(args, (tuple, list)):
284
      logging.info("Received invalid arguments of type '%s'", type(args))
285
      raise ValueError("Invalid arguments type '%s'" % type(args))
286

    
287
    if method not in luxi.REQ_ALL:
288
      logging.info("Received invalid request '%s'", method)
289
      raise ValueError("Invalid operation '%s'" % method)
290

    
291
    # TODO: Rewrite to not exit in each 'if/elif' branch
292

    
293
    if method == luxi.REQ_SUBMIT_JOB:
294
      logging.info("Receiving new job")
295
      (job_def, ) = args
296
      ops = [opcodes.OpCode.LoadOpCode(state) for state in job_def]
297
      job_id = queue.SubmitJob(ops)
298
      _LogNewJob(True, job_id, ops)
299
      return job_id
300

    
301
    elif method == luxi.REQ_PICKUP_JOB:
302
      logging.info("Picking up new job from queue")
303
      (job_id, ) = args
304
      queue.PickupJob(job_id)
305

    
306
    elif method == luxi.REQ_SUBMIT_JOB_TO_DRAINED_QUEUE:
307
      logging.info("Forcefully receiving new job")
308
      (job_def, ) = args
309
      ops = [opcodes.OpCode.LoadOpCode(state) for state in job_def]
310
      job_id = queue.SubmitJobToDrainedQueue(ops)
311
      _LogNewJob(True, job_id, ops)
312
      return job_id
313

    
314
    elif method == luxi.REQ_SUBMIT_MANY_JOBS:
315
      logging.info("Receiving multiple jobs")
316
      (job_defs, ) = args
317
      jobs = []
318
      for ops in job_defs:
319
        jobs.append([opcodes.OpCode.LoadOpCode(state) for state in ops])
320
      job_ids = queue.SubmitManyJobs(jobs)
321
      for ((status, job_id), ops) in zip(job_ids, jobs):
322
        _LogNewJob(status, job_id, ops)
323
      return job_ids
324

    
325
    elif method == luxi.REQ_CANCEL_JOB:
326
      (job_id, ) = args
327
      logging.info("Received job cancel request for %s", job_id)
328
      return queue.CancelJob(job_id)
329

    
330
    elif method == luxi.REQ_CHANGE_JOB_PRIORITY:
331
      (job_id, priority) = args
332
      logging.info("Received request to change priority for job %s to %s",
333
                   job_id, priority)
334
      return queue.ChangeJobPriority(job_id, priority)
335

    
336
    elif method == luxi.REQ_ARCHIVE_JOB:
337
      (job_id, ) = args
338
      logging.info("Received job archive request for %s", job_id)
339
      return queue.ArchiveJob(job_id)
340

    
341
    elif method == luxi.REQ_AUTO_ARCHIVE_JOBS:
342
      (age, timeout) = args
343
      logging.info("Received job autoarchive request for age %s, timeout %s",
344
                   age, timeout)
345
      return queue.AutoArchiveJobs(age, timeout)
346

    
347
    elif method == luxi.REQ_WAIT_FOR_JOB_CHANGE:
348
      (job_id, fields, prev_job_info, prev_log_serial, timeout) = args
349
      logging.info("Received job poll request for %s", job_id)
350
      return queue.WaitForJobChanges(job_id, fields, prev_job_info,
351
                                     prev_log_serial, timeout)
352

    
353
    elif method == luxi.REQ_QUERY:
354
      (what, fields, qfilter) = args
355

    
356
      if what in constants.QR_VIA_OP:
357
        result = self._Query(opcodes.OpQuery(what=what, fields=fields,
358
                                             qfilter=qfilter))
359
      elif what == constants.QR_LOCK:
360
        if qfilter is not None:
361
          raise errors.OpPrereqError("Lock queries can't be filtered",
362
                                     errors.ECODE_INVAL)
363
        return context.glm.QueryLocks(fields)
364
      elif what == constants.QR_JOB:
365
        return queue.QueryJobs(fields, qfilter)
366
      elif what in constants.QR_VIA_LUXI:
367
        luxi_client = runtime.GetClient()
368
        result = luxi_client.Query(what, fields, qfilter).ToDict()
369
      else:
370
        raise errors.OpPrereqError("Resource type '%s' unknown" % what,
371
                                   errors.ECODE_INVAL)
372

    
373
      return result
374

    
375
    elif method == luxi.REQ_QUERY_FIELDS:
376
      (what, fields) = args
377
      req = objects.QueryFieldsRequest(what=what, fields=fields)
378

    
379
      try:
380
        fielddefs = query.ALL_FIELDS[req.what]
381
      except KeyError:
382
        raise errors.OpPrereqError("Resource type '%s' unknown" % req.what,
383
                                   errors.ECODE_INVAL)
384

    
385
      return query.QueryFields(fielddefs, req.fields)
386

    
387
    elif method == luxi.REQ_QUERY_JOBS:
388
      (job_ids, fields) = args
389
      if isinstance(job_ids, (tuple, list)) and job_ids:
390
        msg = utils.CommaJoin(job_ids)
391
      else:
392
        msg = str(job_ids)
393
      logging.info("Received job query request for %s", msg)
394
      return queue.OldStyleQueryJobs(job_ids, fields)
395

    
396
    elif method == luxi.REQ_QUERY_CONFIG_VALUES:
397
      (fields, ) = args
398
      logging.info("Received config values query request for %s", fields)
399
      op = opcodes.OpClusterConfigQuery(output_fields=fields)
400
      return self._Query(op)
401

    
402
    elif method == luxi.REQ_QUERY_CLUSTER_INFO:
403
      logging.info("Received cluster info query request")
404
      op = opcodes.OpClusterQuery()
405
      return self._Query(op)
406

    
407
    elif method == luxi.REQ_QUERY_TAGS:
408
      (kind, name) = args
409
      logging.info("Received tags query request")
410
      op = opcodes.OpTagsGet(kind=kind, name=name, use_locking=False)
411
      return self._Query(op)
412

    
413
    elif method == luxi.REQ_SET_DRAIN_FLAG:
414
      (drain_flag, ) = args
415
      logging.info("Received queue drain flag change request to %s",
416
                   drain_flag)
417
      return queue.SetDrainFlag(drain_flag)
418

    
419
    elif method == luxi.REQ_SET_WATCHER_PAUSE:
420
      (until, ) = args
421

    
422
      return _SetWatcherPause(context, until)
423

    
424
    else:
425
      logging.critical("Request '%s' in luxi.REQ_ALL, but not known", method)
426
      raise errors.ProgrammerError("Operation '%s' in luxi.REQ_ALL,"
427
                                   " but not implemented" % method)
428

    
429
  def _Query(self, op):
430
    """Runs the specified opcode and returns the result.
431

432
    """
433
    # Queries don't have a job id
434
    proc = mcpu.Processor(self.server.context, None, enable_locks=False)
435

    
436
    # TODO: Executing an opcode using locks will acquire them in blocking mode.
437
    # Consider using a timeout for retries.
438
    return proc.ExecOpCode(op, None)
439

    
440

    
441
class GanetiContext(object):
442
  """Context common to all ganeti threads.
443

444
  This class creates and holds common objects shared by all threads.
445

446
  """
447
  # pylint: disable=W0212
448
  # we do want to ensure a singleton here
449
  _instance = None
450

    
451
  def __init__(self):
452
    """Constructs a new GanetiContext object.
453

454
    There should be only a GanetiContext object at any time, so this
455
    function raises an error if this is not the case.
456

457
    """
458
    assert self.__class__._instance is None, "double GanetiContext instance"
459

    
460
    # Create a livelock file
461
    self.livelock = utils.livelock.LiveLock("masterd")
462

    
463
    # Create global configuration object
464
    self.cfg = config.ConfigWriter()
465

    
466
    # Locking manager
467
    self.glm = locking.GanetiLockManager(
468
      self.cfg.GetNodeList(),
469
      self.cfg.GetNodeGroupList(),
470
      [inst.name for inst in self.cfg.GetAllInstancesInfo().values()],
471
      self.cfg.GetNetworkList())
472

    
473
    self.cfg.SetContext(self)
474

    
475
    # RPC runner
476
    self.rpc = rpc.RpcRunner(self.cfg, self.glm.AddToLockMonitor)
477

    
478
    # Job queue
479
    self.jobqueue = jqueue.JobQueue(self)
480

    
481
    # setting this also locks the class against attribute modifications
482
    self.__class__._instance = self
483

    
484
  def __setattr__(self, name, value):
485
    """Setting GanetiContext attributes is forbidden after initialization.
486

487
    """
488
    assert self.__class__._instance is None, "Attempt to modify Ganeti Context"
489
    object.__setattr__(self, name, value)
490

    
491
  def AddNode(self, node, ec_id):
492
    """Adds a node to the configuration.
493

494
    """
495
    # Add it to the configuration
496
    self.cfg.AddNode(node, ec_id)
497

    
498
    # If preseeding fails it'll not be added
499
    self.jobqueue.AddNode(node)
500

    
501
  def ReaddNode(self, node):
502
    """Updates a node that's already in the configuration
503

504
    """
505
    # Synchronize the queue again
506
    self.jobqueue.AddNode(node)
507

    
508
  def RemoveNode(self, node):
509
    """Removes a node from the configuration and lock manager.
510

511
    """
512
    # Remove node from configuration
513
    self.cfg.RemoveNode(node.uuid)
514

    
515
    # Notify job queue
516
    self.jobqueue.RemoveNode(node.name)
517

    
518

    
519
def _SetWatcherPause(context, until):
520
  """Creates or removes the watcher pause file.
521

522
  @type context: L{GanetiContext}
523
  @param context: Global Ganeti context
524
  @type until: None or int
525
  @param until: Unix timestamp saying until when the watcher shouldn't run
526

527
  """
528
  node_names = context.cfg.GetNodeList()
529

    
530
  if until is None:
531
    logging.info("Received request to no longer pause watcher")
532
  else:
533
    if not ht.TNumber(until):
534
      raise TypeError("Duration must be numeric")
535

    
536
    if until < time.time():
537
      raise errors.GenericError("Unable to set pause end time in the past")
538

    
539
    logging.info("Received request to pause watcher until %s", until)
540

    
541
  result = context.rpc.call_set_watcher_pause(node_names, until)
542

    
543
  errmsg = utils.CommaJoin("%s (%s)" % (node_name, nres.fail_msg)
544
                           for (node_name, nres) in result.items()
545
                           if nres.fail_msg and not nres.offline)
546
  if errmsg:
547
    raise errors.OpExecError("Watcher pause was set where possible, but failed"
548
                             " on the following node(s): %s" % errmsg)
549

    
550
  return until
551

    
552

    
553
@rpc.RunWithRPC
554
def CheckAgreement():
555
  """Check the agreement on who is the master.
556

557
  The function uses a very simple algorithm: we must get more positive
558
  than negative answers. Since in most of the cases we are the master,
559
  we'll use our own config file for getting the node list. In the
560
  future we could collect the current node list from our (possibly
561
  obsolete) known nodes.
562

563
  In order to account for cold-start of all nodes, we retry for up to
564
  a minute until we get a real answer as the top-voted one. If the
565
  nodes are more out-of-sync, for now manual startup of the master
566
  should be attempted.
567

568
  Note that for a even number of nodes cluster, we need at least half
569
  of the nodes (beside ourselves) to vote for us. This creates a
570
  problem on two-node clusters, since in this case we require the
571
  other node to be up too to confirm our status.
572

573
  """
574
  myself = netutils.Hostname.GetSysName()
575
  #temp instantiation of a config writer, used only to get the node list
576
  cfg = config.ConfigWriter()
577
  node_names = cfg.GetNodeNames(cfg.GetNodeList())
578
  del cfg
579
  retries = 6
580
  while retries > 0:
581
    votes = bootstrap.GatherMasterVotes(node_names)
582
    if not votes:
583
      # empty node list, this is a one node cluster
584
      return True
585
    if votes[0][0] is None:
586
      retries -= 1
587
      time.sleep(10)
588
      continue
589
    break
590
  if retries == 0:
591
    logging.critical("Cluster inconsistent, most of the nodes didn't answer"
592
                     " after multiple retries. Aborting startup")
593
    logging.critical("Use the --no-voting option if you understand what"
594
                     " effects it has on the cluster state")
595
    return False
596
  # here a real node is at the top of the list
597
  all_votes = sum(item[1] for item in votes)
598
  top_node, top_votes = votes[0]
599

    
600
  result = False
601
  if top_node != myself:
602
    logging.critical("It seems we are not the master (top-voted node"
603
                     " is %s with %d out of %d votes)", top_node, top_votes,
604
                     all_votes)
605
  elif top_votes < all_votes - top_votes:
606
    logging.critical("It seems we are not the master (%d votes for,"
607
                     " %d votes against)", top_votes, all_votes - top_votes)
608
  else:
609
    result = True
610

    
611
  return result
612

    
613

    
614
@rpc.RunWithRPC
615
def ActivateMasterIP():
616
  # activate ip
617
  cfg = config.ConfigWriter()
618
  master_params = cfg.GetMasterNetworkParameters()
619
  ems = cfg.GetUseExternalMipScript()
620
  runner = rpc.BootstrapRunner()
621
  # we use the node name, as the configuration is only available here yet
622
  result = runner.call_node_activate_master_ip(
623
             cfg.GetNodeName(master_params.uuid), master_params, ems)
624

    
625
  msg = result.fail_msg
626
  if msg:
627
    logging.error("Can't activate master IP address: %s", msg)
628

    
629

    
630
def CheckMasterd(options, args):
631
  """Initial checks whether to run or exit with a failure.
632

633
  """
634
  if args: # masterd doesn't take any arguments
635
    print >> sys.stderr, ("Usage: %s [-f] [-d]" % sys.argv[0])
636
    sys.exit(constants.EXIT_FAILURE)
637

    
638
  ssconf.CheckMaster(options.debug)
639

    
640
  try:
641
    options.uid = pwd.getpwnam(constants.MASTERD_USER).pw_uid
642
    options.gid = grp.getgrnam(constants.DAEMONS_GROUP).gr_gid
643
  except KeyError:
644
    print >> sys.stderr, ("User or group not existing on system: %s:%s" %
645
                          (constants.MASTERD_USER, constants.DAEMONS_GROUP))
646
    sys.exit(constants.EXIT_FAILURE)
647

    
648
  # Determine static runtime architecture information
649
  runtime.InitArchInfo()
650

    
651
  # Check the configuration is sane before anything else
652
  try:
653
    config.ConfigWriter()
654
  except errors.ConfigVersionMismatch, err:
655
    v1 = "%s.%s.%s" % version.SplitVersion(err.args[0])
656
    v2 = "%s.%s.%s" % version.SplitVersion(err.args[1])
657
    print >> sys.stderr,  \
658
        ("Configuration version mismatch. The current Ganeti software"
659
         " expects version %s, but the on-disk configuration file has"
660
         " version %s. This is likely the result of upgrading the"
661
         " software without running the upgrade procedure. Please contact"
662
         " your cluster administrator or complete the upgrade using the"
663
         " cfgupgrade utility, after reading the upgrade notes." %
664
         (v1, v2))
665
    sys.exit(constants.EXIT_FAILURE)
666
  except errors.ConfigurationError, err:
667
    print >> sys.stderr, \
668
        ("Configuration error while opening the configuration file: %s\n"
669
         "This might be caused by an incomplete software upgrade or"
670
         " by a corrupted configuration file. Until the problem is fixed"
671
         " the master daemon cannot start." % str(err))
672
    sys.exit(constants.EXIT_FAILURE)
673

    
674
  # If CheckMaster didn't fail we believe we are the master, but we have to
675
  # confirm with the other nodes.
676
  if options.no_voting:
677
    if not options.yes_do_it:
678
      sys.stdout.write("The 'no voting' option has been selected.\n")
679
      sys.stdout.write("This is dangerous, please confirm by"
680
                       " typing uppercase 'yes': ")
681
      sys.stdout.flush()
682

    
683
      confirmation = sys.stdin.readline().strip()
684
      if confirmation != "YES":
685
        print >> sys.stderr, "Aborting."
686
        sys.exit(constants.EXIT_FAILURE)
687

    
688
  else:
689
    # CheckAgreement uses RPC and threads, hence it needs to be run in
690
    # a separate process before we call utils.Daemonize in the current
691
    # process.
692
    if not utils.RunInSeparateProcess(CheckAgreement):
693
      sys.exit(constants.EXIT_FAILURE)
694

    
695
  # ActivateMasterIP also uses RPC/threads, so we run it again via a
696
  # separate process.
697

    
698
  # TODO: decide whether failure to activate the master IP is a fatal error
699
  utils.RunInSeparateProcess(ActivateMasterIP)
700

    
701

    
702
def PrepMasterd(options, _):
703
  """Prep master daemon function, executed with the PID file held.
704

705
  """
706
  # This is safe to do as the pid file guarantees against
707
  # concurrent execution.
708
  utils.RemoveFile(pathutils.MASTER_SOCKET)
709

    
710
  mainloop = daemon.Mainloop()
711
  master = MasterServer(pathutils.MASTER_SOCKET, options.uid, options.gid)
712
  return (mainloop, master)
713

    
714

    
715
def ExecMasterd(options, args, prep_data): # pylint: disable=W0613
716
  """Main master daemon function, executed with the PID file held.
717

718
  """
719
  (mainloop, master) = prep_data
720
  try:
721
    rpc.Init()
722
    try:
723
      master.setup_context()
724
      try:
725
        mainloop.Run(shutdown_wait_fn=master.WaitForShutdown)
726
      finally:
727
        master.server_cleanup()
728
    finally:
729
      rpc.Shutdown()
730
  finally:
731
    utils.RemoveFile(pathutils.MASTER_SOCKET)
732

    
733
  logging.info("Clean master daemon shutdown")
734

    
735

    
736
def Main():
737
  """Main function"""
738
  parser = OptionParser(description="Ganeti master daemon",
739
                        usage="%prog [-f] [-d]",
740
                        version="%%prog (ganeti) %s" %
741
                        constants.RELEASE_VERSION)
742
  parser.add_option("--no-voting", dest="no_voting",
743
                    help="Do not check that the nodes agree on this node"
744
                    " being the master and start the daemon unconditionally",
745
                    default=False, action="store_true")
746
  parser.add_option("--yes-do-it", dest="yes_do_it",
747
                    help="Override interactive check for --no-voting",
748
                    default=False, action="store_true")
749
  daemon.GenericMain(constants.MASTERD, parser, CheckMasterd, PrepMasterd,
750
                     ExecMasterd, multithreaded=True)