ctrl2cgi: update shared helper

* split hash generation into simpler functions
* move them into helper file
* split log init into separate function

This facilitates code sharing with new version of ctrl2cgi proxy in
folow-up patches.

Change-Id: Ic5c349349d2c246f934d6701435b1a2d9de77de1
Related: SYS#4399
changes/73/12473/2
Maxim Suraev 4 years ago
parent 7230f68311
commit 19b77a426c
  1. 37
      osmopy/trap_helper.py
  2. 19
      scripts/ctrl2cgi.py

@ -22,7 +22,7 @@
*/
"""
import sys, os, datetime, signal, logging, logging.handlers
import hashlib, sys, os, datetime, signal, logging, logging.handlers
from functools import partial
from osmopy.osmo_ipa import Ctrl
@ -95,16 +95,39 @@ def reloader(path, script, log, dbg1, dbg2, signum, _):
sys.argv.remove(dbg2)
os.execl(path, script, *sys.argv[1:])
def debug_init(name, is_debug):
def add_keys(inp, params, l):
"""
Initialize signal handlers and logging
Add given list of keys to input string.
"""
for key in l:
inp += str(params.get(key))
return inp
def gen_hash(params, skey):
"""
Make mandatory parameter for HTTP request.
"""
inp = add_keys('', params, ['time_stamp', 'position_validity', 'admin_status', 'policy_status'])
inp += skey
inp = add_keys(inp, params, ['bsc_id', 'lat', 'lon', 'position_validity'])
m = hashlib.md5()
m.update(inp.encode('utf-8'))
return m.hexdigest()
def log_init(name, is_debug):
"""
Initialize stdout logging.
"""
log = logging.getLogger(name)
if is_debug:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
log.setLevel(logging.DEBUG if is_debug else logging.INFO)
log.addHandler(logging.StreamHandler(sys.stdout))
return log
def debug_init(name, is_debug):
"""
Initialize signal handlers and logging
"""
log = log_init(name, is_debug)
reboot = partial(reloader, os.path.abspath(__file__), os.path.basename(__file__), log, '-d', '--debug') # keep in sync with caller's add_argument()
signal.signal(signal.SIGHUP, reboot)

@ -22,7 +22,7 @@
*/
"""
__version__ = "0.1.0" # bump this on every non-trivial change
__version__ = "0.1.1" # bump this on every non-trivial change
import argparse, os, logging, logging.handlers, datetime
import hashlib
@ -32,7 +32,7 @@ from functools import partial
from distutils.version import StrictVersion as V
from twisted.internet import defer, reactor
from treq import post, collect
from osmopy.trap_helper import debug_init, get_type, get_r, p_h, make_params, comm_proc
from osmopy.trap_helper import debug_init, get_type, get_r, p_h, gen_hash, make_params, comm_proc
from osmopy.twisted_ipa import CTRL, IPAFactory, __version__ as twisted_ipa_version
from osmopy.osmo_ipa import Ctrl
@ -57,21 +57,6 @@ def handle_reply(ts, ts_http, bid, f, log, resp):
log_duration(log, bid, ts, ts_http)
comm_proc(decoded.get('commands'), bid, f, log)
def gen_hash(params, skey):
"""
Make mandatory parameter for http request
"""
inp = ''
for key in ['time_stamp', 'position_validity', 'admin_status', 'policy_status']:
inp += str(params.get(key))
inp += skey
for key in ['bsc_id', 'lat', 'lon', 'position_validity']:
inp += str(params.get(key))
m = hashlib.md5()
m.update(inp.encode('utf-8'))
res = m.hexdigest()
return res
def make_async_req(ts, dst, par, f_write, f_log, tout):
"""
Assemble deferred request parameters and partially instantiate response handler

Loading…
Cancel
Save