Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 12 additions & 41 deletions src/python/Utils/PortForward.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
A decorator for swapping ports in an url
"""
from __future__ import print_function, division
from future import standard_library
standard_library.install_aliases()

from builtins import str

import logging
Comment thread
todor-ivanov marked this conversation as resolved.
from urllib.parse import urlparse, ParseResult
Comment thread
todor-ivanov marked this conversation as resolved.
from builtins import str, bytes


def portForward(port):
Expand All @@ -38,9 +32,6 @@ def portForwardDecorator(callFunc):
"""
The actual decorator
"""
urlMangleList = ['https://tivanov',
'https://alancc',
'https://cmsweb']

def portMangle(callObj, url, *args, **kwargs):
"""
Expand All @@ -57,40 +48,21 @@ def portMangle(callObj, url, *args, **kwargs):
:param *args: The positional argument list coming from the original function
:param *kwargs: The keywords argument list coming from the original function
"""
# As a first step try to get a logger from the calling object:
if callable(getattr(callObj, 'logger', None)):
logger = callObj.logger
else:
logger = logging.getLogger()

forwarded = False
try:
oldUrl = urlparse(url)
found = False
if isinstance(url, str):
for mUrl in urlMangleList:
if url.startswith(mUrl):
netlocStr = u'%s:%d' % (oldUrl.hostname, port)
found = True
break
urlToMangle = u'https://cmsweb'
if url.startswith(urlToMangle):
newUrl = url.replace(u'.cern.ch/', u'.cern.ch:%d/' % port, 1)
forwarded = True
elif isinstance(url, bytes):
for mUrl in urlMangleList:
if url.startswith(mUrl.encode('utf-8')):
netlocStr = b'%s:%d' % (oldUrl.hostname, port)
found = True
break
if found:
newUrl = ParseResult(scheme=oldUrl.scheme,
netloc=netlocStr,
path=oldUrl.path,
params=oldUrl.params,
query=oldUrl.query,
fragment=oldUrl.fragment)
newUrl = newUrl.geturl()
forwarded = True
except Exception as ex:
msg = "Failed to forward url: %s to port: %s due to ERROR: %s"
logger.exception(msg, url, port, str(ex))
urlToMangle = b'https://cmsweb'
if url.startswith(urlToMangle):
newUrl = url.replace(b'.cern.ch/', b'.cern.ch:%d/' % port, 1)
forwarded = True

except Exception:
pass
if forwarded:
return callFunc(callObj, newUrl, *args, **kwargs)
else:
Expand All @@ -115,7 +87,6 @@ def __init__(self, port):
The init method for the PortForward call class. This one is supposed
to simply provide an initial class instance with a logger.
"""
self.logger = logging.getLogger()
self.port = port

def __call__(self, url):
Expand Down
9 changes: 6 additions & 3 deletions test/python/Utils_t/PortForward_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class PortForwardTests(unittest.TestCase):

def __init__(self, *args, **kwargs):
super(PortForwardTests, self).__init__(*args, **kwargs)
self.urlTestList = ['https://cmsweb.cern.ch/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bydate?descending=true&limit=1',
self.urlResultList = []
self.urlTestList = ['https://cms-service-dqm.web.cern.ch/cms-service-dqm/CAF/certification/Collisions12/8TeV/Reprocessing/Cert_190456-195530_8TeV_08Jun2012ReReco_Collisions12_JSON.txt',
'https://cmsweb.cern.ch/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bydate?descending=true&limit=1',
'https://cmsweb.cern.ch/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bystatusandtime?startkey=%5B%22announced%22%2C+0%5D&endkey=%5B%22announced%22%2C+1616016936%5D&descending=false&stale=update_after&include_docs=false',
'https://cmsweb.cern.ch:8443/reqmgr2/js/?f=utils.js&f=ajax_utils.js&f=md5.js&f=task_splitting.js',
'https://cmsweb.cern.ch:443/wmstatsserver/data/filtered_requests?mask=RequestStatus&mask=RequestType&mask=RequestPriority&mask=Campaign&mask=RequestNumEvents',
Expand All @@ -43,10 +45,11 @@ def __init__(self, *args, **kwargs):
b'https://cmsweb.cern.ch/reqmgr2/js/?f=utils.js&f=ajax_utils.js&f=md5.js&f=task_splitting.js',
b'https://cmsweb.cern.ch/wmstatsserver/data/filtered_requests?mask=RequestStatus&mask=RequestType&mask=RequestPriority&mask=Campaign&mask=RequestNumEvents']

self.urlExpectedtList = ['https://cmsweb.cern.ch:8443/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bydate?descending=true&limit=1',
self.urlExpectedtList = ['https://cms-service-dqm.web.cern.ch/cms-service-dqm/CAF/certification/Collisions12/8TeV/Reprocessing/Cert_190456-195530_8TeV_08Jun2012ReReco_Collisions12_JSON.txt',
'https://cmsweb.cern.ch:8443/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bydate?descending=true&limit=1',
'https://cmsweb.cern.ch:8443/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bystatusandtime?startkey=%5B%22announced%22%2C+0%5D&endkey=%5B%22announced%22%2C+1616016936%5D&descending=false&stale=update_after&include_docs=false',
'https://cmsweb.cern.ch:8443/reqmgr2/js/?f=utils.js&f=ajax_utils.js&f=md5.js&f=task_splitting.js',
'https://cmsweb.cern.ch:8443/wmstatsserver/data/filtered_requests?mask=RequestStatus&mask=RequestType&mask=RequestPriority&mask=Campaign&mask=RequestNumEvents',
'https://cmsweb.cern.ch:443/wmstatsserver/data/filtered_requests?mask=RequestStatus&mask=RequestType&mask=RequestPriority&mask=Campaign&mask=RequestNumEvents',
u'https://cmsweb.cern.ch:8443/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bydate?descending=true&limit=1',
u'https://cmsweb.cern.ch:8443/couchdb/reqmgr_workload_cache/_design/ReqMgr/_view/bystatusandtime?startkey=%5B%22announced%22%2C+0%5D&endkey=%5B%22announced%22%2C+1616016936%5D&descending=false&stale=update_after&include_docs=false',
u'https://cmsweb.cern.ch:8443/reqmgr2/js/?f=utils.js&f=ajax_utils.js&f=md5.js&f=task_splitting.js',
Expand Down