Return user-friendly error message when monkey_patch enabled

If user has enabled “monkey_patch” config option and kept the default
“monkey_patch_modules” config option as is, then service gives
ValueError at the time of starting itself. The default value given to
the “monkey_patch_modules” config option is not according to the format
expected which results into ValueError at service starting.

This patch fixes this issue by making monkey_patch_modules's correct
default value and if user wants to apply monkey patching then syntax
must be correct so handled that value error.

Closes-Bug: #1660545
Change-Id: I637a36eacdabca8c4edbba201d491744690869ef
This commit is contained in:
poojajadhav 2017-02-09 18:59:29 +05:30
parent f8c25dd2aa
commit b18793c672
2 changed files with 8 additions and 3 deletions

View File

@ -31,7 +31,7 @@ Related options:
"""),
cfg.ListOpt(
'monkey_patch_modules',
default=['masakarimonitors.cmd'],
default=[],
help="""
List of modules/decorators to monkey patch.

View File

@ -27,7 +27,7 @@ from oslo_utils import importutils
import six
import masakarimonitors.conf
from masakarimonitors.i18n import _LE
from masakarimonitors.i18n import _, _LE
from masakarimonitors import privsep
@ -59,7 +59,12 @@ def monkey_patch():
return inspect.ismethod(obj) or inspect.isfunction(obj)
# Get list of modules and decorators
for module_and_decorator in CONF.monkey_patch_modules:
module, decorator_name = module_and_decorator.split(':')
md_value = module_and_decorator.split(':')
if len(md_value) != 2:
msg = _("'monkey_patch_modules' config option is not configured "
"correctly")
raise Exception(msg)
module, decorator_name = md_value
# import decorator function
decorator = importutils.import_class(decorator_name)
__import__(module)