Revision 04ccf5e9 daemons/ganeti-masterd

b/daemons/ganeti-masterd
43 43

  
44 44
from ganeti import config
45 45
from ganeti import constants
46
from ganeti import daemon
46 47
from ganeti import mcpu
47 48
from ganeti import opcodes
48 49
from ganeti import jqueue
......
383 384
    self.glm.remove(locking.LEVEL_NODE, name)
384 385

  
385 386

  
386
def ParseOptions():
387
  """Parse the command line options.
388

  
389
  @return: (options, args) as from OptionParser.parse_args()
390

  
391
  """
392
  parser = OptionParser(description="Ganeti master daemon",
393
                        usage="%prog [-f] [-d]",
394
                        version="%%prog (ganeti) %s" %
395
                        constants.RELEASE_VERSION)
396

  
397
  parser.add_option("-f", "--foreground", dest="fork",
398
                    help="Don't detach from the current terminal",
399
                    default=True, action="store_false")
400
  parser.add_option("-d", "--debug", dest="debug",
401
                    help="Enable some debug messages",
402
                    default=False, action="store_true")
403
  parser.add_option("--no-voting", dest="no_voting",
404
                    help="Do not check that the nodes agree on this node"
405
                    " being the master and start the daemon unconditionally",
406
                    default=False, action="store_true")
407
  parser.add_option("--yes-do-it", dest="yes_do_it",
408
                    help="Override interactive check for --no-voting",
409
                    default=False, action="store_true")
410

  
411
  options, args = parser.parse_args()
412
  return options, args
413

  
414

  
415 387
def CheckAgreement():
416 388
  """Check the agreement on who is the master.
417 389

  
......
468 440

  
469 441
  return result
470 442

  
443
def CheckMASTERD(options, args):
444
  """Initial checks whether to run or exit with a failure
471 445

  
472
def main():
473
  """Main function"""
474

  
475
  options, args = ParseOptions()
476
  utils.no_fork = True
477
  daemon_name = constants.MASTERD
478

  
479
  if options.fork:
480
    utils.CloseFDs()
481

  
446
  """
482 447
  rpc.Init()
483 448
  try:
484 449
    ssconf.CheckMaster(options.debug)
......
496 461
    elif not options.no_voting:
497 462
      if not CheckAgreement():
498 463
        return
499

  
500
    dirs = [(constants.RUN_GANETI_DIR, constants.RUN_DIRS_MODE),
501
            (constants.SOCKET_DIR, constants.SOCKET_DIR_MODE),
502
           ]
503
    utils.EnsureDirs(dirs)
504

  
505
    # This is safe to do as the pid file guarantees against
506
    # concurrent execution.
507
    utils.RemoveFile(constants.MASTER_SOCKET)
508

  
509
    master = IOServer(constants.MASTER_SOCKET, ClientRqHandler)
510 464
  finally:
511 465
    rpc.Shutdown()
512 466

  
513
  # become a daemon
514
  if options.fork:
515
    utils.Daemonize(logfile=constants.DAEMONS_LOGFILES[daemon_name])
516 467

  
517
  utils.WritePidFile(daemon_name)
518
  try:
519
    utils.SetupLogging(constants.DAEMONS_LOGFILES[daemon_name],
520
                       debug=options.debug,
521
                       stderr_logging=not options.fork, multithreaded=True)
468
def ExecMASTERD(options, args):
469
  """Main MASTERD function, executed with the pidfile held.
522 470

  
523
    logging.info("Ganeti master daemon startup")
471
  """
472
  # This is safe to do as the pid file guarantees against
473
  # concurrent execution.
474
  utils.RemoveFile(constants.MASTER_SOCKET)
524 475

  
476
  master = IOServer(constants.MASTER_SOCKET, ClientRqHandler)
477
  try:
525 478
    rpc.Init()
526 479
    try:
527 480
      # activate ip
......
539 492
    finally:
540 493
      rpc.Shutdown()
541 494
  finally:
542
    utils.RemovePidFile(daemon_name)
543 495
    utils.RemoveFile(constants.MASTER_SOCKET)
544 496

  
545 497

  
498
def main():
499
  """Main function"""
500
  parser = OptionParser(description="Ganeti master daemon",
501
                        usage="%prog [-f] [-d]",
502
                        version="%%prog (ganeti) %s" %
503
                        constants.RELEASE_VERSION)
504
  parser.add_option("--no-voting", dest="no_voting",
505
                    help="Do not check that the nodes agree on this node"
506
                    " being the master and start the daemon unconditionally",
507
                    default=False, action="store_true")
508
  parser.add_option("--yes-do-it", dest="yes_do_it",
509
                    help="Override interactive check for --no-voting",
510
                    default=False, action="store_true")
511
  dirs = [(constants.RUN_GANETI_DIR, constants.RUN_DIRS_MODE),
512
          (constants.SOCKET_DIR, constants.SOCKET_DIR_MODE),
513
         ]
514
  daemon.GenericMain(constants.MASTERD, parser, dirs,
515
                     CheckMASTERD, ExecMASTERD)
516

  
546 517
if __name__ == "__main__":
547 518
  main()

Also available in: Unified diff