aboutsummaryrefslogtreecommitdiff
path: root/rpki/config.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2016-01-29 02:27:20 +0000
committerRob Austein <sra@hactrn.net>2016-01-29 02:27:20 +0000
commit2a598fa8adb5ae40eb05aa2406d523f2a2155790 (patch)
treef7b14f3be342dda26c8493a5c0a85d6950ee70aa /rpki/config.py
parent7b450b49e5d07c1b194ae1eea137a5cf4dc32f93 (diff)
Whack rcynicng to produce an XML file which rcynic-html can parse:
needs further work, but produces somewhat-useful output now. Start merging command line argument (argparse) and config file (ConfigParser) support into a unified library module, in an attempt to reduce the amount of code duplication and reign in some of the configuration chaos. Trigger for this is all of the RP programs now needing SQL configuration. Whack rpki.rcynicdb.models to work with PostgreSQL, which has length restrictions on indexed columns. svn path=/branches/tk705/; revision=6239
Diffstat (limited to 'rpki/config.py')
-rw-r--r--rpki/config.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/rpki/config.py b/rpki/config.py
index a9bd3219..3cc16626 100644
--- a/rpki/config.py
+++ b/rpki/config.py
@@ -23,6 +23,7 @@ ConfigParser module.
"""
import ConfigParser
+import argparse
import logging
import os
import re
@@ -272,3 +273,60 @@ class parser(object):
rpki.up_down.content_type = self.get("up_down_content_type")
except ConfigParser.NoOptionError:
pass
+
+
+def argparser(section = None, doc = None, cfg_optional = False):
+ """
+ First cut at a combined configuration mechanism based on ConfigParser and argparse.
+
+ General idea here is to do an initial pass on the arguments to handle the config file,
+ then return the config file and a parser to use for the rest of the arguments.
+ """
+
+ # Basic approach here is a variation on:
+ # http://blog.vwelch.com/2011/04/combining-configparser-and-argparse.html
+
+ # For most of our uses of argparse, this should be a trivial
+ # drop-in, and should reduce the amount of repetitive code. There
+ # are a couple of special cases which will require attention:
+ #
+ # - rpki.rtr: These modules have their own handling of all the
+ # logging setup, and use an argparse subparser. I -think- that
+ # the way they're already handling the logging setup should work
+ # fine, but there may be a few tricky bits reconciling this code
+ # with the more generalized version in rpki.log.
+ #
+ # - rpki.rpkic: Use of argparse here is very complicated due to
+ # support for both the external command line and the internal
+ # command loop. Overall it works quite well, but the setup is
+ # tricky. rpki.rpkic.main.top_argparse may need to move outside
+ # the main class, but that may raise its own issues. Maybe we
+ # can get away with just replacing the current setup of
+ # top_argparser with a call to this function and otherwise
+ # leaving the whole structure alone? Try and see, I guess.
+
+ # Setting cfg_optional here doesn't really work, because the cfg
+ # object returned here is separate from the one that the Django
+ # ORM gets when it tries to look for databases. Given that just
+ # about everything which uses this module also uses Django,
+ # perhaps we should just resign ourselves to the config being a
+ # global thing we read exactly once, so we can stop playing this
+ # game.
+
+ topparser = argparse.ArgumentParser(add_help = False)
+ topparser.add_argument("-c", "--config",
+ default = os.getenv(rpki_conf_envname, default_filename),
+ help = "override default location of configuration file")
+
+ cfgparser = argparse.ArgumentParser(parents = [topparser], add_help = False)
+ cfgparser.add_argument("-h", "--help", action = "store_true")
+
+ args, remaining_argv = cfgparser.parse_known_args()
+
+ cfg = parser(section = section,
+ set_filename = args.config,
+ allow_missing = cfg_optional or args.help)
+
+ argparser = argparse.ArgumentParser(parents = [topparser], description = doc)
+
+ return cfg, argparser