Revision 04ccf5e9 lib/daemon.py

b/lib/daemon.py
26 26
import signal
27 27
import errno
28 28
import time
29
import logging
29 30

  
30 31
from ganeti import utils
32
from ganeti import constants
31 33

  
32 34

  
33 35
class Timer(object):
......
297 299

  
298 300
    """
299 301
    self._timer_remove.append(timer_id)
302

  
303

  
304
def GenericMain(daemon_name, optionparser, dirs, check_fn, exec_fn):
305
  """Shared main function for daemons.
306

  
307
  @type daemon_name: string
308
  @param daemon_name: daemon name
309
  @type optionparser: L{optparse.OptionParser}
310
  @param optionparser: initialized optionparser with daemon-specific options
311
                       (common -f -d options will be handled by this module)
312
  @type options: object @param options: OptionParser result, should contain at
313
                 least the fork and the debug options
314
  @type dirs: list of strings
315
  @param dirs: list of directories that must exist for this daemon to work
316
  @type check_fn: function which accepts (options, args)
317
  @param check_fn: function that checks start conditions and exits if they're
318
                   not met
319
  @type exec_fn: function which accepts (options, args)
320
  @param exec_fn: function that's executed with the daemon's pid file held, and
321
                  runs the daemon itself.
322

  
323
  """
324
  optionparser.add_option("-f", "--foreground", dest="fork",
325
                          help="Don't detach from the current terminal",
326
                          default=True, action="store_false")
327
  optionparser.add_option("-d", "--debug", dest="debug",
328
                          help="Enable some debug messages",
329
                          default=False, action="store_true")
330
  if daemon_name in constants.DAEMONS_PORTS:
331
    # for networked daemons we also allow choosing the bind port and address.
332
    # by default we use the port provided by utils.GetDaemonPort, and bind to
333
    # 0.0.0.0 (which is represented by and empty bind address.
334
    port = utils.GetDaemonPort(daemon_name)
335
    optionparser.add_option("-p", "--port", dest="port",
336
                            help="Network port (%s default)." % port,
337
                            default=port, type="int")
338
    optionparser.add_option("-b", "--bind", dest="bind_address",
339
                            help="Bind address",
340
                            default="", metavar="ADDRESS")
341

  
342
  multithread = utils.no_fork = daemon_name in constants.MULTITHREADED_DAEMONS
343

  
344
  options, args = optionparser.parse_args()
345

  
346
  check_fn(options, args)
347
  utils.EnsureDirs(dirs)
348

  
349
  if options.fork:
350
    utils.CloseFDs()
351
    utils.Daemonize(logfile=constants.DAEMONS_LOGFILES[daemon_name])
352

  
353
  utils.WritePidFile(daemon_name)
354
  try:
355
    utils.SetupLogging(logfile=constants.DAEMONS_LOGFILES[daemon_name],
356
                       debug=options.debug,
357
                       stderr_logging=not options.fork,
358
                       multithreaded=multithread)
359
    logging.info("%s daemon startup" % daemon_name)
360
    exec_fn(options, args)
361
  finally:
362
    utils.RemovePidFile(daemon_name)
363

  

Also available in: Unified diff