chiark / gitweb /
config: fix client config (no SERVER)
[hippotat.git] / hippotatlib / __init__.py
index 7bdf4ea8e1730e49150933f56e08716c3424c3c0..16bba813016f88654f33d890ebb9588ef2794003 100644 (file)
@@ -100,22 +100,27 @@ def log_debug(dflag, msg, idof=None, d=None):
     msg += ' ' + d + trunc
   log.info('{dflag} {msgcore}', dflag=dflag, msgcore=msg)
 
+def logevent_is_boringtwisted(event):
+  try:
+    if event.get('log_level') != LogLevel.info:
+      return False
+    dflag = event.get('dflag')
+    if dflag is False                            : return False
+    if dflag                         in debug_set: return False
+    if dflag is None and DBG.TWISTED in debug_set: return False
+    return True
+  except Exception:
+    print(traceback.format_exc(), file=org_stderr)
+    return False
+
 @implementer(twisted.logger.ILogFilterPredicate)
 class LogNotBoringTwisted:
   def __call__(self, event):
-    yes = twisted.logger.PredicateResult.yes
-    no  = twisted.logger.PredicateResult.no
-    try:
-      if event.get('log_level') != LogLevel.info:
-        return yes
-      dflag = event.get('dflag')
-      if dflag is False                            : return yes
-      if dflag                         in debug_set: return yes
-      if dflag is None and DBG.TWISTED in debug_set: return yes
-      return no
-    except Exception:
-      print(traceback.format_exc(), file=org_stderr)
-      return yes
+    return (
+      twisted.logger.PredicateResult.no
+      if logevent_is_boringtwisted(event) else
+      twisted.logger.PredicateResult.yes
+    )
 
 #---------- default config ----------
 
@@ -138,8 +143,6 @@ ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip %(rnets)s
 # relating to virtual network
 mtu = 1500
 
-[SERVER]
-server = SERVER
 # addrs = 127.0.0.1 ::1
 # url
 
@@ -444,12 +447,15 @@ def cfg_process_saddrs(c, ss):
         self._endpointfactory = twisted.internet.endpoints.TCP6ServerEndpoint
         self._inurl = b'[%s]'
     def make_endpoint(self):
-      return self._endpointfactory(reactor, self.port, self.addr)
+      return self._endpointfactory(reactor, self.port,
+                                   interface= '%s' % self.addr)
     def url(self):
       url = b'http://' + (self._inurl % str(self.addr).encode('ascii'))
       if self.port != 80: url += b':%d' % self.port
       url += b'/'
       return url
+    def __repr__(self):
+      return 'ServerAddr'+repr((self.port,self.addr))
 
   c.port = cfg.getint(ss,'port')
   c.saddrs = [ ]
@@ -517,6 +523,10 @@ def cfg_process_ipif(c, sections, varmap):
 
 #---------- startup ----------
 
+def log_debug_config(m):
+  if not DBG.CONFIG in debug_set: return
+  print('DBG.CONFIG:', m)
+
 def common_startup(process_cfg):
   # calls process_cfg(putative_clients, putative_servers)
 
@@ -528,7 +538,7 @@ def common_startup(process_cfg):
   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))
+      log_debug_config('%s: %s' % (m, pathname))
 
     try:
       files = os.listdir(pathname)
@@ -546,7 +556,7 @@ def common_startup(process_cfg):
     # is a directory
     log('directory')
     re = regexp.compile('[^-A-Za-z0-9_]')
-    for f in os.listdir(cdir):
+    for f in os.listdir(pathname):
       if re.search(f): continue
       subpath = pathname + '/' + f
       try:
@@ -562,6 +572,19 @@ def common_startup(process_cfg):
     need_defcfg = False
     readconfig(value)
 
+  def oc_extra_config(od,os, value, op):
+    readconfig(value)
+
+  def read_defconfig():
+    readconfig('/etc/hippotat/config.d', False)
+    readconfig('/etc/hippotat/passwords.d', False)
+    readconfig('/etc/hippotat/master.cfg',   False)
+
+  def oc_defconfig(od,os, value, op):
+    nonlocal need_defcfg
+    need_defcfg = False
+    read_defconfig(value)
+
   def dfs_less_detailed(dl):
     return [df for df in DBG.iterconstants() if df <= dl]
 
@@ -625,12 +648,23 @@ just `+': all DFLAGs.
                        action='callback',
                        callback= oc_config)
 
+  optparser.add_option('--extra-config',
+                       nargs=1,
+                       type='string',
+                       metavar='CONFIGFILE',
+                       dest='configfile',
+                       action='callback',
+                       callback= oc_extra_config)
+
+  optparser.add_option('--default-config',
+                       action='callback',
+                       callback= oc_defconfig)
+
   (opts, args) = optparser.parse_args()
   if len(args): optparser.error('no non-option arguments please')
 
   if need_defcfg:
-    readconfig('/etc/hippotat/config',   False)
-    readconfig('/etc/hippotat/config.d', False)
+    read_defconfig()
 
   try:
     (pss, pcs) = _cfg_process_putatives()
@@ -662,4 +696,5 @@ just `+': all DFLAGs.
 def common_run():
   log_debug(DBG.INIT, 'entering reactor')
   if not _crashing: reactor.run()
-  print('CRASHED (end)', file=sys.stderr)
+  print('ENDED', file=sys.stderr)
+  sys.exit(16)