Merge "Remove unused log hook code"

This commit is contained in:
Zuul 2022-03-26 06:37:13 +00:00 committed by Gerrit Code Review
commit 2243310f59
10 changed files with 0 additions and 352 deletions

View File

@ -21,7 +21,6 @@ from oslo_reports import guru_meditation_report as gmr
from designate.agent import service as agent_service
import designate.conf
from designate import heartbeat_emitter
from designate import hookpoints
from designate import service
from designate import utils
from designate import version
@ -36,8 +35,6 @@ def main():
logging.setup(CONF, 'designate')
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
server = agent_service.Service()
heartbeat = heartbeat_emitter.get_heartbeat_emitter(server.service_name)
service.serve(server, workers=CONF['service:agent'].workers)

View File

@ -22,7 +22,6 @@ from oslo_reports import guru_meditation_report as gmr
from designate.api import service as api_service
import designate.conf
from designate import heartbeat_emitter
from designate import hookpoints
from designate import service
from designate import utils
from designate import version
@ -38,8 +37,6 @@ def main():
logging.setup(CONF, 'designate')
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
server = api_service.Service()
heartbeat = heartbeat_emitter.get_heartbeat_emitter(server.service_name)
service.serve(server, workers=CONF['service:api'].workers)

View File

@ -21,7 +21,6 @@ from oslo_reports import guru_meditation_report as gmr
from designate.central import service as central_service
import designate.conf
from designate import heartbeat_emitter
from designate import hookpoints
from designate import service
from designate import utils
from designate import version
@ -36,8 +35,6 @@ def main():
logging.setup(CONF, 'designate')
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
server = central_service.Service()
heartbeat = heartbeat_emitter.get_heartbeat_emitter(server.service_name,
rpc_api=server)

View File

@ -24,7 +24,6 @@ from oslo_reports import guru_meditation_report as gmr
from stevedore.extension import ExtensionManager
import designate.conf
from designate import hookpoints
from designate import utils
from designate import version
@ -115,8 +114,6 @@ def main():
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
fn = CONF.category.action_fn
fn_args = fetch_func_args(fn)

View File

@ -20,7 +20,6 @@ from oslo_reports import guru_meditation_report as gmr
import designate.conf
from designate import heartbeat_emitter
from designate import hookpoints
from designate.mdns import service as mdns_service
from designate import service
from designate import utils
@ -36,8 +35,6 @@ def main():
logging.setup(CONF, 'designate')
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
server = mdns_service.Service()
heartbeat = heartbeat_emitter.get_heartbeat_emitter(server.service_name)
service.serve(server, workers=CONF['service:mdns'].workers)

View File

@ -20,7 +20,6 @@ from oslo_reports import guru_meditation_report as gmr
import designate.conf
from designate import heartbeat_emitter
from designate import hookpoints
from designate.producer import service as producer_service
from designate import service
from designate import utils
@ -36,8 +35,6 @@ def main():
logging.setup(CONF, 'designate')
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
server = producer_service.Service()
heartbeat = heartbeat_emitter.get_heartbeat_emitter(server.service_name)
service.serve(server, workers=CONF['service:producer'].workers)

View File

@ -20,7 +20,6 @@ from oslo_reports import guru_meditation_report as gmr
import designate.conf
from designate import heartbeat_emitter
from designate import hookpoints
from designate import service
from designate.sink import service as sink_service
from designate import utils
@ -36,8 +35,6 @@ def main():
logging.setup(CONF, 'designate')
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
server = sink_service.Service()
heartbeat = heartbeat_emitter.get_heartbeat_emitter(server.service_name)
service.serve(server, workers=CONF['service:sink'].workers)

View File

@ -20,7 +20,6 @@ from oslo_reports import guru_meditation_report as gmr
import designate.conf
from designate import heartbeat_emitter
from designate import hookpoints
from designate import service
from designate import utils
from designate import version
@ -36,8 +35,6 @@ def main():
logging.setup(CONF, 'designate')
gmr.TextGuruMeditation.setup_autorun(version)
hookpoints.log_hook_setup()
server = worker_service.Service()
heartbeat = heartbeat_emitter.get_heartbeat_emitter(server.service_name)
service.serve(server, workers=CONF['service:worker'].workers)

View File

@ -1,183 +0,0 @@
# Copyright 2015 Rackspace Hosting.
#
# Author: Eric Larson <eric.larson@rackspace.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import functools
import sys
from oslo_config import cfg
from oslo_log import log
import pkg_resources
from stevedore import hook
class HookLog(object):
"""Since logs are applied at import time, we record the log
messages for later use.
"""
LVLS = dict(
debug=log.DEBUG,
info=log.INFO,
warning=log.WARNING,
error=log.ERROR,
critical=log.CRITICAL,
exception=log.ERROR,
)
def __init__(self):
self.messages = []
def log(self, logger=None):
logger = log.getLogger(__name__)
for level, msg, args, kw in self.messages:
logger.log(level, msg, *args, **kw)
__call__ = log
def capture(self, lvl, msg, *args, **kw):
self.messages.append((lvl, msg, args, kw))
def __getattr__(self, name):
if name in self.LVLS:
return functools.partial(self.capture, self.LVLS[name])
LOG = HookLog()
def log_hook_setup():
"""Replay the log messages during the hook point initialization.
Logging isn't configured when we set up the hook points, so this
will replay the log once the config has been loaded.
"""
LOG()
class BaseHook(object):
OPTS = [
cfg.BoolOpt('disabled', default=False)
]
def __init__(self, group):
self.group = group
@property
def disabled(self):
return cfg.CONF[self.group].get('disabled', False)
def wrapper(self, *args, **kw):
return self.hook_target(*args, **kw)
def __call__(self, f):
# Save our hook target as an attribute for our wrapper method
self.hook_target = f
@functools.wraps(self.hook_target)
def wrapper(*args, **kw):
if self.disabled:
return self.hook_target(*args, **kw)
return self.hook(*args, **kw)
return wrapper
class hook_point(object):
NAMESPACE = 'designate.hook_point'
LOG_LEVEL = log.INFO
def __init__(self, name=None):
self._name = name
self.log = []
def update_config_opts(self, group, hooks):
hooks_found = False
for hook_impl in hooks:
hooks_found = True
# Add any options defined by the hook
if hasattr(hook_impl.plugin, 'OPTS'):
cfg.CONF.register_opts(hook_impl.plugin.OPTS, group=group)
if not hooks_found:
LOG.debug('No hooks found for %s', group)
else:
LOG.debug('Created hook: %s', group)
def hook_manager(self, name):
LOG.debug('Looking for hooks with: %s %s', self.NAMESPACE, name)
return hook.HookManager(self.NAMESPACE, name)
def find_name(self, func=None):
"""Derive the hook target path from the function name, unless
a name has been passed in with the constructor.
"""
if self._name:
return self._name
if not func:
return None
# derive the name from the function
self._name = '%s.%s' % (func.__module__, func.__name__)
return self._name
def init_hook(self, f):
"""Set up our hook
Try to inspect the function for a hook target path if one
wasn't passed in and set up the necessary config options.
"""
LOG.debug('Initializing hook: %s', f)
self.name = self.find_name(f)
self.group = 'hook_point:%s' % self.name
self.hooks = self.hook_manager(self.name)
self.update_config_opts(self.group, self.hooks)
def enable_hook(self, ext, f):
"""Enable the hook.
This instantiates the hook object and decorates the original
function.
"""
decorator = ext.plugin(self.group)
f = decorator(f)
f._hook_point = self # add ourselves for inspection
return f
def __call__(self, f):
# Set up all our hook information based on the function or
# hook point
self.init_hook(f)
for h in self.hooks:
f = self.enable_hook(h, f)
return f
if __name__ == '__main__':
# Use this script to find existing hook points.
hook_names = sys.argv[1:]
print('Using namespace: %s' % hook_point.NAMESPACE)
print('pkg_resources has the following entry points:')
for ep in pkg_resources.iter_entry_points(hook_point.NAMESPACE):
print(ep)
print()
print('stevedore found the following hooks:')
for name in hook_names:
for hp in hook.HookManager(hook_point.NAMESPACE, name):
print(hp)

View File

@ -1,145 +0,0 @@
# Copyright 2015 Rackspace Hosting.
#
# Author: Eric Larson <eric.larson@rackspace.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from unittest.mock import Mock
from unittest.mock import patch
from oslo_config import cfg
from stevedore.extension import Extension
from stevedore.hook import HookManager
from designate.hookpoints import BaseHook
from designate.hookpoints import hook_point
from designate.tests import TestCase
class AddHook(BaseHook):
OPTS = [
cfg.Opt('bar'),
]
@property
def bar(self):
return cfg.CONF[self.group].bar
def hook(self, *args, **kw):
return self.hook_target(*args, **kw) + 1
def get_hook_manager(*hooks):
hooks = hooks or [AddHook]
group = 'hook_point:foo'
ext = [
Extension('designate_hook', 'foo', hook, hook(group))
for hook in hooks
]
return HookManager.make_test_instance(ext, 'designate_hook')
def inc(num):
return num + 1
class TestHookpoints(TestCase):
def setUp(self):
TestCase.setUp(self)
group = 'hook_point:foo'
self.CONF.register_group(cfg.OptGroup(group))
self.CONF.register_opts(BaseHook.OPTS, group=group)
def test_no_hookpoint_is_noop(self):
def doit(self, name):
return 'done: %s' % name
self.assertEqual(doit, hook_point('foo')(doit))
def test_hook_is_decorator(self):
hp = hook_point('foo')
hp.hook_manager = Mock(return_value=get_hook_manager())
assert hp(inc)(1) == 3
def test_apply_N_hooks(self):
hp = hook_point('foo')
hp.hook_manager = Mock(return_value=get_hook_manager(AddHook, AddHook))
assert hp(inc)(1) == 4
def test_hook_init(self):
hp = hook_point('foo')
# Make sure we set up our object when the hook point is
# applied to a function / method.
hp.find_name = Mock(return_value='foo.bar.baz')
hp.hook_manager = Mock(return_value=get_hook_manager())
hp.find_config = Mock(return_value={'enabled': True})
hp.update_config_opts = Mock()
hp(inc)
self.assertEqual(hp.name, 'foo.bar.baz')
self.assertEqual(hp.group, 'hook_point:foo.bar.baz')
hp.update_config_opts.assert_called_with(hp.group, hp.hooks)
class TestHookpointsConfigOpts(TestCase):
"""Make sure hooks add the necessary config opts.
"""
def test_hook_adds_config_opts(self):
hp = hook_point('foo')
hp.hook_manager = Mock(return_value=get_hook_manager())
hp(inc)
assert hp.group in self.CONF.keys()
class TestHookpointsEnabling(TestCase):
def setUp(self):
TestCase.setUp(self)
# NOTE: The options need to be added here via the test classes
# CONF in order to fall through
group = 'hook_point:foo'
self.CONF.register_group(cfg.OptGroup(group))
self.CONF.register_opts(BaseHook.OPTS, group=group)
@patch.object(hook_point, 'hook_manager',
Mock(return_value=get_hook_manager()))
def test_hook_disabled(self):
hp = hook_point('foo')
result_func = hp(inc)
# We should now have a config option we can set to disabled
self.config(disabled=True, group='hook_point:foo')
# The result is 2 so no extra add hook was applied
self.assertEqual(result_func(1), 2)
@patch.object(hook_point, 'hook_manager',
Mock(return_value=get_hook_manager()))
def test_hook_enabled_when_config_key_exists(self):
hp = hook_point('foo')
hp(inc)
# Add our config
self.config(bar='from config', group='hook_point:foo')
# reapply our hook
result_func = hp(inc)
# The result is 3 so the extra add hook was applied
self.assertEqual(result_func(1), 3)