Reopen log files upon SIGHUP in daemons
authorMichael Hanselmann <hansmi@google.com>
Tue, 1 Feb 2011 15:31:15 +0000 (16:31 +0100)
committerMichael Hanselmann <hansmi@google.com>
Wed, 2 Feb 2011 16:02:27 +0000 (17:02 +0100)
Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

lib/daemon.py
lib/utils/log.py
man/ganeti.rst

index 6fb2f69..12acc64 100644 (file)
@@ -40,6 +40,7 @@ from ganeti import errors
 from ganeti import netutils
 from ganeti import ssconf
 from ganeti import runtime
+from ganeti import compat
 
 
 class SchedulerBreakout(Exception):
@@ -539,6 +540,17 @@ def _BeautifyError(err):
     return "%s" % str(err)
 
 
+def _HandleSigHup(reopen_cb, signum, frame): # pylint: disable-msg=W0613
+  """Handler for SIGHUP.
+
+  @param reopen_cb: Callback function for reopening log files
+
+  """
+  assert callable(reopen_cb)
+  logging.info("Reopening log files after receiving SIGHUP")
+  reopen_cb()
+
+
 def GenericMain(daemon_name, optionparser,
                 check_fn, prepare_fn, exec_fn,
                 multithreaded=False, console_logging=False,
@@ -656,15 +668,20 @@ def GenericMain(daemon_name, optionparser,
   else:
     wpipe = None
 
+  log_reopen_fn = \
+    utils.SetupLogging(constants.DAEMONS_LOGFILES[daemon_name], daemon_name,
+                       debug=options.debug,
+                       stderr_logging=not options.fork,
+                       multithreaded=multithreaded,
+                       syslog=options.syslog,
+                       console_logging=console_logging)
+
+  # Reopen log file(s) on SIGHUP
+  signal.signal(signal.SIGHUP, compat.partial(_HandleSigHup, log_reopen_fn))
+
   utils.WritePidFile(utils.DaemonPidFileName(daemon_name))
   try:
     try:
-      utils.SetupLogging(constants.DAEMONS_LOGFILES[daemon_name], daemon_name,
-                         debug=options.debug,
-                         stderr_logging=not options.fork,
-                         multithreaded=multithreaded,
-                         syslog=options.syslog,
-                         console_logging=console_logging)
       if callable(prepare_fn):
         prep_results = prepare_fn(options, args)
       else:
index 309c28b..f02c823 100644 (file)
@@ -200,6 +200,8 @@ def SetupLogging(logfile, program, debug=0, stderr_logging=False,
   @param root_logger: Root logger to use (for unittests)
   @raise EnvironmentError: if we can't open the log file and
       syslog/stderr logging is disabled
+  @rtype: callable
+  @return: Function reopening all open log files when called
 
   """
   progname = os.path.basename(program)
index 97d1832..0bc1d02 100644 (file)
@@ -172,3 +172,10 @@ would succeed.
 
 The option ``--priority`` sets the priority for opcodes submitted
 by the command.
+
+
+Common daemon functionality
+---------------------------
+
+All Ganeti daemons re-open the log file(s) when sent a SIGHUP signal.
+**logrotate**(8) can be used to rotate Ganeti's log files.