Change in python/osmo-python-tests[master]: ctrl2cgi: fix broken config override

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Wed Dec 5 12:54:10 UTC 2018


Max has uploaded this change for review. ( https://gerrit.osmocom.org/12134


Change subject: ctrl2cgi: fix broken config override
......................................................................

ctrl2cgi: fix broken config override

Previously command-line arguments without defaults took precedence over
config file variables while values from config file which had
command-line counterparts with default value were silently ignored.

Let's fix this by making config file values to be always preferred over
command-line equivalents.

The easiest way is to use TrapFactory as argparse namespace. This means
that some parameter values won't be known initially so logging is moved
directly to main.

Change-Id: I471b5a6497eadce6456e835233fdaba88a593324
Related: SYS#4399
---
M scripts/ctrl2cgi.py
1 file changed, 21 insertions(+), 39 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/python/osmo-python-tests refs/changes/34/12134/1

diff --git a/scripts/ctrl2cgi.py b/scripts/ctrl2cgi.py
index 1d6813d..89cbf50 100755
--- a/scripts/ctrl2cgi.py
+++ b/scripts/ctrl2cgi.py
@@ -22,7 +22,7 @@
  */
 """
 
-__version__ = "0.0.4" # bump this on every non-trivial change
+__version__ = "0.0.5" # bump this on every non-trivial change
 
 import argparse, os, logging, logging.handlers
 import hashlib
@@ -90,7 +90,7 @@
         """
         Logging wrapper, calling super() is necessary not to break reconnection logic
         """
-        self.factory.log.info("Connected to CTRL@%s:%d" % (self.factory.host, self.factory.port))
+        self.factory.log.info("Connected to CTRL@%s:%d" % (self.factory.addr_ctrl, self.factory.port_ctrl))
         super(CTRL, self).connectionMade()
 
     @defer.inlineCallbacks
@@ -120,25 +120,14 @@
     """
     Store CGI information so TRAP handler can use it for requests
     """
-    location = None
-    log = None
-    semaphore = None
-    client = None
-    host = None
-    port = None
-    secret_key = None
-    def __init__(self, host, port, proto, semaphore, log, location, secret_key):
-        self.host = host # for logging only,
-        self.port = port # seems to be no way to get it from ReconnectingClientFactory
+    def __init__(self, proto, log):
+        self.semaphore = defer.DeferredSemaphore(self.num_max_conn)
         self.log = log
-        self.semaphore = semaphore
-        self.location = location
-        self.secret_key = secret_key
         level = self.log.getEffectiveLevel()
         self.log.setLevel(logging.WARNING) # we do not need excessive debug from lower levels
         super(TrapFactory, self).__init__(proto, self.log)
         self.log.setLevel(level)
-        self.log.debug("Using IPA %s, CGI server: %s" % (Ctrl.version, self.location))
+        self.log.debug("Using Osmocom IPA library v%s" % Ctrl.version)
 
 
 if __name__ == '__main__':
@@ -151,31 +140,24 @@
     p.add_argument('-o', '--output', action='store_true', help="Log to STDOUT in addition to SYSLOG")
     p.add_argument('-l', '--location', help="Location URL of the CGI server")
     p.add_argument('-s', '--secret-key', help="Secret key used to generate verification token")
-    p.add_argument('-c', '--config-file', help="Path Config file. Cmd line args override values in config file")
-    args = p.parse_args()
+    p.add_argument('-c', '--config-file', help="Path to config file (in INI format). Values from config file override command line options.")
+    args = p.parse_args(namespace=TrapFactory)
 
     log = debug_init('CTRL2CGI', args.debug, args.output)
 
-    location_cfgfile = None
-    secret_key_cfgfile = None
-    port_ctrl_cfgfile = None
-    addr_ctrl_cfgfile = None
-    num_max_conn_cfgfile = None
-    if args.config_file:
-        config = configparser.ConfigParser()
-        config.read(args.config_file)
-        if 'main' in config:
-            location_cfgfile = config['main'].get('location', None)
-            secret_key_cfgfile = config['main'].get('secret_key', None)
-            addr_ctrl_cfgfile = config['main'].get('addr_ctrl', None)
-            port_ctrl_cfgfile = config['main'].get('port_ctrl', None)
-            num_max_conn_cfgfile = config['main'].get('num_max_conn', None)
-    location = args.location if args.location is not None else location_cfgfile
-    secret_key = args.secret_key  if args.secret_key is not None else secret_key_cfgfile
-    addr_ctrl = args.addr_ctrl if args.addr_ctrl is not None else addr_ctrl_cfgfile
-    port_ctrl = args.port_ctrl if args.port_ctrl is not None else port_ctrl_cfgfile
-    num_max_conn = args.num_max_conn if args.num_max_conn is not None else num_max_conn_cfgfile
+    T = TrapFactory(Trap, log)
 
-    log.info("CGI proxy %s starting with PID %d ..." % (__version__, os.getpid()))
-    reactor.connectTCP(addr_ctrl, port_ctrl, TrapFactory(addr_ctrl, port_ctrl, Trap, defer.DeferredSemaphore(num_max_conn), log, location, secret_key))
+    if args.config_file:
+        config = configparser.ConfigParser(interpolation=None)
+        config.read(args.config_file)
+        T.location = config['main'].get('location', T.location)
+        T.addr_ctrl = config['main'].get('addr_ctrl', T.addr_ctrl)
+        T.port_ctrl = config['main'].getint('port_ctrl', T.port_ctrl)
+        T.num_max_conn = config['main'].getint('num_max_conn', T.num_max_conn)
+        T.secret_key = config['main'].get('secret_key', T.secret_key)
+
+    log.info("CGI proxy v%s starting with PID %d:" % (__version__, os.getpid()))
+    log.info("destination %s (concurrency %d)" % (T.location, T.num_max_conn))
+    log.info("connecting to %s:%d..." % (T.addr_ctrl, T.port_ctrl))
+    reactor.connectTCP(T.addr_ctrl, T.port_ctrl, T)
     reactor.run()

-- 
To view, visit https://gerrit.osmocom.org/12134
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: python/osmo-python-tests
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I471b5a6497eadce6456e835233fdaba88a593324
Gerrit-Change-Number: 12134
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181205/4062849e/attachment.htm>


More information about the gerrit-log mailing list