aboutsummaryrefslogtreecommitdiff
path: root/ca
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2016-04-26 20:07:41 +0000
committerRob Austein <sra@hactrn.net>2016-04-26 20:07:41 +0000
commit01697787912f143ab2b9a938e33c73c9a8a9ae0b (patch)
tree8065ac9a88d697678de0ad68c13683c6c2f5dd69 /ca
parent1447a61b235699163f186ef689ca8eaf898ee478 (diff)
Further consolidation of config file parsing, command line parsing,
and logging setup. Most programs now use the unified mechanism, although there are still a few holdouts: the GUI, which is a special case because it has no command line, and the rpki-rtr program, which, for historical reasons has its own implementation of the logging setup infrastructure. svn path=/branches/tk705/; revision=6390
Diffstat (limited to 'ca')
-rwxr-xr-xca/irbe_cli2
-rwxr-xr-xca/rpki-nanny85
-rw-r--r--ca/tests/smoketest.py7
-rw-r--r--ca/tests/testpoke.py2
-rw-r--r--ca/tests/yamlconf.py6
-rwxr-xr-xca/tests/yamltest.py13
6 files changed, 28 insertions, 87 deletions
diff --git a/ca/irbe_cli b/ca/irbe_cli
index 15a7a30d..7d62db9d 100755
--- a/ca/irbe_cli
+++ b/ca/irbe_cli
@@ -282,8 +282,6 @@ def usage(code = 1):
# Main program
-rpki.log.init("irbe_cli")
-
argv = sys.argv[1:]
if not argv:
diff --git a/ca/rpki-nanny b/ca/rpki-nanny
index a82f7501..914c8584 100755
--- a/ca/rpki-nanny
+++ b/ca/rpki-nanny
@@ -49,17 +49,6 @@ signames = dict((getattr(signal, sig), sig)
and sig.isupper()
and isinstance(getattr(signal, sig), int))
-# TODO:
-#
-# * Logging configuration is a mess. Daemons should be handling this
-# for themselves, from rpki.conf, and there should be a way to configure
-# logging for rpki-nanny itself.
-#
-# * Perhaps we should re-read the config file so we can turn individual
-# daemons on and off? Or is that unnecessary complexity?
-#
-# * rpki-nanny should probably daemonize itself before forking.
-
class Daemon(object):
"""
@@ -70,21 +59,8 @@ class Daemon(object):
self.name = name
self.proc = None
self.next_restart = 0
- if cfg.getboolean("start_" + name, False):
- log_file = os.path.join(args.log_directory, name + ".log")
- self.cmd = (os.path.join(rpki.autoconf.libexecdir, name),
- "--foreground",
- "--log-level", args.log_level)
- if args.log_file:
- self.cmd += ("--log-file", log_file)
- elif args.log_rotating_file_kbytes:
- self.cmd += ("--log-rotating-file", log_file,
- args.log_rotating_file_kbytes, args.log_backup_count)
- elif args.log_rotating_file_hours:
- self.cmd += ("--log-timed-rotating-file", log_file,
- args.log_rotating_file_hours, args.log_backup_count)
- else:
- self.cmd += ("--log-syslog", args.log_syslog)
+ if cfg.getboolean(option = "start_" + name, section = "myrpki", default = False):
+ self.cmd = (os.path.join(rpki.autoconf.libexecdir, name), "--foreground")
else:
self.cmd = ()
@@ -167,70 +143,29 @@ if __name__ == "__main__":
os.environ.update(TZ = "UTC")
time.tzset()
- cfg = rpki.config.argparser(section = "myrpki", doc = __doc__)
+ cfg = rpki.config.argparser(section = "rpki-nanny", doc = __doc__)
cfg.add_argument("--restart-delay", type = positive_integer, default = 60,
help = "how long to wait before restarting a crashed daemon")
cfg.add_argument("--pidfile",
default = os.path.join(rpki.daemonize.default_pid_directory, "rpki-nanny.pid"),
help = "override default location of pid file")
- cfg.add_boolean_argument("--daemonize", default = True,
- help = "whether to daemonize")
+ cfg.add_boolean_argument("--foreground", default = False,
+ help = "whether to stay in foreground rather than daemonizing")
cfg.add_boolean_argument("--capture-stdout-stderr", default = True,
- help = "whether to capture daemon output incorrectly sent to stdout or stderr")
-
- # This stuff is a mess. Daemons should control their own logging
- # via rpki.conf settings, but we haven't written that yet, and
- # this script is meant to be a replacement for rpki-start-servers,
- # so leave the mess in place for the moment and clean up later.
-
- cfg.argparser.add_argument("--log-directory", default = ".",
- help = "where to write write log files when not using syslog")
- cfg.argparser.add_argument("--log-backup-count", default = "7", type = non_negative_integer,
- help = "keep this many old log files when rotating")
- cfg.argparser.add_argument("--log-level", default = "warning",
- choices = ("debug", "info", "warning", "error", "critical"),
- help = "how verbosely to log")
- group = cfg.argparser.add_mutually_exclusive_group()
- group.add_argument("--log-file", action = "store_true",
- help = "log to files, reopening if rotated away")
- group.add_argument("--log-rotating-file-kbytes",type = non_negative_integer,
- help = "log to files, rotating after this many kbytes")
- group.add_argument("--log-rotating-file-hours", type = non_negative_integer,
- help = "log to files, rotating after this many hours")
- group.add_argument("--log-syslog", default = "daemon", nargs = "?",
- choices = sorted(SysLogHandler.facility_names.keys()),
- help = "log syslog")
+ help = "whether to capture output incorrectly sent to stdout/stderr")
+ cfg.add_logging_arguments()
args = cfg.argparser.parse_args()
# Drop privs before daemonizing or opening log file
-
pw = pwd.getpwnam(rpki.autoconf.RPKI_USER)
os.setgid(pw.pw_gid)
os.setuid(pw.pw_uid)
- # Log control mess here is continuation of log control mess above:
- # all the good names are taken by the pass-through kludge, we'd
- # have to reimplement all the common logic to use it ourselves
- # too. Just wire to stderr or rotating log file for now, using
- # same log file scheme as set in /etc/defaults/ on Debian/Ubuntu
- # and the log level wired to DEBUG, fix the whole logging mess later.
-
- if args.daemonize:
- log_handler = lambda: logging.handlers.TimedRotatingFileHandler(
- filename = os.path.join(args.log_directory, "rpki-nanny.log"),
- interval = 3,
- backupCount = 56,
- when = "H",
- utc = True)
- else:
- log_handler = logging.StreamHandler
-
- rpki.log.init(ident = "rpki-nanny",
- args = argparse.Namespace(log_level = logging.DEBUG,
- log_handler = log_handler))
- if args.daemonize:
+ cfg.configure_logging(ident = "rpki-nanny", args = args)
+
+ if not args.foreground:
rpki.daemonize.daemon(pidfile = args.pidfile)
if args.capture_stdout_stderr:
diff --git a/ca/tests/smoketest.py b/ca/tests/smoketest.py
index 6479883e..2bce936b 100644
--- a/ca/tests/smoketest.py
+++ b/ca/tests/smoketest.py
@@ -158,8 +158,11 @@ def main():
Main program.
"""
- rpki.log.init(smoketest_name, argparse.Namespace(log_level = logging.DEBUG,
- log_handler = lambda: logging.StreamHandler(sys.stdout)))
+ log_handler = logging.StreamHandler(sys.stdout)
+ log_handler.setFormatter(rpki.config.Formatter("smoketest", log_handler, logging.DEBUG))
+ logging.getLogger().addHandler(log_handler)
+ logging.getLogger().setLevel(logging.DEBUG)
+
logger.info("Starting")
rpki.http.http_client.timeout = rpki.sundial.timedelta(hours = 1)
diff --git a/ca/tests/testpoke.py b/ca/tests/testpoke.py
index 60cc5690..7ebe7d44 100644
--- a/ca/tests/testpoke.py
+++ b/ca/tests/testpoke.py
@@ -51,8 +51,6 @@ parser.add_argument("-d", "--debug",
help = "enable debugging")
args = parser.parse_args()
-rpki.log.init("testpoke")
-
yaml_data = yaml.load(args.yaml)
yaml_cmd = args.request
diff --git a/ca/tests/yamlconf.py b/ca/tests/yamlconf.py
index 2963a61f..db368320 100644
--- a/ca/tests/yamlconf.py
+++ b/ca/tests/yamlconf.py
@@ -670,8 +670,10 @@ def main():
quiet = args.quiet
yaml_file = args.yaml_file
- rpki.log.init("yamlconf", argparse.Namespace(log_level = logging.DEBUG,
- log_handler = lambda: logging.StreamHandler(sys.stdout)))
+ log_handler = logging.StreamHandler(sys.stdout)
+ log_handler.setFormatter(rpki.config.Formatter("yamlconf", log_handler, logging.DEBUG))
+ logging.getLogger().addHandler(log_handler)
+ logging.getLogger().setLevel(logging.DEBUG)
# Allow optional config file for this tool to override default
# passwords: this is mostly so that I can show a complete working
diff --git a/ca/tests/yamltest.py b/ca/tests/yamltest.py
index d467384a..c2959dc9 100755
--- a/ca/tests/yamltest.py
+++ b/ca/tests/yamltest.py
@@ -54,6 +54,7 @@ import rpki.log
import rpki.csv_utils
import rpki.x509
import rpki.relaxng
+import rpki.config
# pylint: disable=W0621
@@ -673,8 +674,10 @@ class allocation(object):
"""
basename = os.path.splitext(os.path.basename(prog))[0]
- cmd = [prog, "--foreground", "--log-level", "debug",
- "--log-file", self.path(basename + ".log")]
+ cmd = [prog, "--foreground",
+ "--log-level", "debug",
+ "--log-destination", "file",
+ "--log-filename", self.path(basename + ".log")]
if args.profile:
cmd.extend((
"--profile", self.path(basename + ".prof")))
@@ -831,8 +834,10 @@ try:
print "Writing pidfile", f.name
f.write("%s\n" % os.getpid())
- rpki.log.init("yamltest", argparse.Namespace(log_level = logging.DEBUG,
- log_handler = lambda: logging.StreamHandler(sys.stdout)))
+ log_handler = logging.StreamHandler(sys.stdout)
+ log_handler.setFormatter(rpki.config.Formatter("yamltest", log_handler, logging.DEBUG))
+ logging.getLogger().addHandler(log_handler)
+ logging.getLogger().setLevel(logging.DEBUG)
allocation.base_port = args.base_port