chiark / gitweb /
config redo
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Apr 2017 09:50:15 +0000 (10:50 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Apr 2017 09:50:15 +0000 (10:50 +0100)
hippotat/__init__.py
test.cfg

index d8b119a..6b87da9 100644 (file)
@@ -4,6 +4,7 @@ import signal
 signal.signal(signal.SIGINT, signal.SIG_DFL)
 
 import sys
 signal.signal(signal.SIGINT, signal.SIG_DFL)
 
 import sys
+import os
 
 from zope.interface import implementer
 
 
 from zope.interface import implementer
 
@@ -35,6 +36,7 @@ import hippotat.slip as slip
 
 class DBG(twisted.python.constants.Names):
   INIT = NamedConstant()
 
 class DBG(twisted.python.constants.Names):
   INIT = NamedConstant()
+  CONFIG = NamedConstant()
   ROUTE = NamedConstant()
   DROP = NamedConstant()
   FLOW = NamedConstant()
   ROUTE = NamedConstant()
   DROP = NamedConstant()
   FLOW = NamedConstant()
@@ -135,7 +137,7 @@ target_requests_outstanding = 10  # used by server
 '''
 
 # these need to be defined here so that they can be imported by import *
 '''
 
 # these need to be defined here so that they can be imported by import *
-cfg = ConfigParser()
+cfg = ConfigParser(strict=False)
 optparser = OptionParser()
 
 _mimetrans = bytes.maketrans(b'-'+slip.esc, slip.esc+b'-')
 optparser = OptionParser()
 
 _mimetrans = bytes.maketrans(b'-'+slip.esc, slip.esc+b'-')
@@ -402,8 +404,46 @@ def process_cfg_clients(constructor):
 #---------- startup ----------
 
 def common_startup():
 #---------- startup ----------
 
 def common_startup():
-  optparser.add_option('-c', '--config', dest='configfile',
-                       default='/etc/hippotat/config')
+  re = regexp.compile('#.*')
+  cfg.read_string(re.sub('', defcfg))
+  need_defcfg = True
+
+  def readconfig(pathname, mandatory=True):
+    def log(m, p=pathname):
+      if not DBG.CONFIG in debug_set: return
+      print('DBG.CONFIG: %s: %s' % (m, pathname))
+
+    try:
+      files = os.listdir(pathname)
+
+    except FileNotFoundError:
+      if mandatory: raise
+      log('skipped')
+      return
+
+    except NotADirectoryError:
+      cfg.read(pathname)
+      log('read file')
+      return
+
+    # is a directory
+    log('directory')
+    re = regexp.compile('[^-A-Za-z0-9_]')
+    for f in os.listdir(cdir):
+      if re.search(f): continue
+      subpath = pathname + '/' + f
+      try:
+        os.stat(subpath)
+      except FileNotFoundError:
+        log('entry skipped', subpath)
+        continue
+      cfg.read(subpath)
+      log('entry read', subpath)
+      
+  def oc_config(od,os, value, op):
+    nonlocal need_defcfg
+    need_defcfg = False
+    readconfig(value)
 
   def dfs_less_detailed(dl):
     return [df for df in DBG.iterconstants() if df <= dl]
 
   def dfs_less_detailed(dl):
     return [df for df in DBG.iterconstants() if df <= dl]
@@ -459,14 +499,22 @@ just `+': all DFLAGs.
                        action='callback',
                        callback= ds_select)
 
                        action='callback',
                        callback= ds_select)
 
+  optparser.add_option('-c', '--config',
+                       nargs=1,
+                       type='string',
+                       metavar='CONFIGFILE',
+                       dest='configfile',
+                       action='callback',
+                       callback= oc_config)
+
   (opts, args) = optparser.parse_args()
   if len(args): optparser.error('no non-option arguments please')
 
   (opts, args) = optparser.parse_args()
   if len(args): optparser.error('no non-option arguments please')
 
-  #print(repr(debug_set), file=sys.stderr)
+  if need_defcfg:
+    readconfig('/etc/hippotat/config',   False)
+    readconfig('/etc/hippotat/config.d', False)
 
 
-  re = regexp.compile('#.*')
-  cfg.read_string(re.sub('', defcfg))
-  cfg.read(opts.configfile)
+  #print(repr(debug_set), file=sys.stderr)
 
   log_formatter = twisted.logger.formatEventAsClassicLogText
   stdout_obs = twisted.logger.FileLogObserver(sys.stdout, log_formatter)
 
   log_formatter = twisted.logger.formatEventAsClassicLogText
   stdout_obs = twisted.logger.FileLogObserver(sys.stdout, log_formatter)
index e959f7b..ca05da5 100644 (file)
--- a/test.cfg
+++ b/test.cfg
@@ -10,4 +10,6 @@ network = 192.0.2.0/24
 
 [192.0.2.3]
 password = sesame
 
 [192.0.2.3]
 password = sesame
+
+[192.0.2.3]
 ipif = PATH=/usr/local/sbin:/sbin:/usr/sbin:$PATH really ./fake-userv /home/ian/things/Userv/userv-utils.git/ipif/service \* -- %(local)s,%(peer)s,%(mtu)s,slip '%(rnets)s'
 ipif = PATH=/usr/local/sbin:/sbin:/usr/sbin:$PATH really ./fake-userv /home/ian/things/Userv/userv-utils.git/ipif/service \* -- %(local)s,%(peer)s,%(mtu)s,slip '%(rnets)s'