blazar/climate/utils/service.py
Yuriy Taraday ea0c144b13 Refactor context module to make it more flexible
- context data is stored in a dictionary now, so any key is allowed;
- list of allowed key for certain context is stored in special class
  variable;
- methods "current" and "elevated" of ClimateContext class are
  accessible from module level;
- moved to usage of threading.local instead of emulating one in code (as
  long as we use eventlet's monkey-patching, it's specific local() will
  be used).

Change-Id: I3c79c9cd0d16dfb5c61208f2aa71b5c59f3f40f3
2013-11-25 14:10:23 +04:00

78 lines
2.5 KiB
Python

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright © 2013 Julien Danjou <julien@danjou.info>
#
# 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
from oslo.config import cfg
from climate import context
from climate.openstack.common import log
from climate.openstack.common import rpc
import climate.openstack.common.rpc.proxy as rpc_proxy
class RpcProxy(rpc_proxy.RpcProxy):
def cast(self, name, topic=None, version=None, ctx=None, **kwargs):
if ctx is None:
ctx = context.current()
msg = self.make_msg(name, **kwargs)
return super(RpcProxy, self).cast(ctx, msg,
topic=topic, version=version)
def call(self, name, topic=None, version=None, ctx=None, **kwargs):
if ctx is None:
ctx = context.current()
msg = self.make_msg(name, **kwargs)
return super(RpcProxy, self).call(ctx, msg,
topic=topic, version=version)
def export_context(func):
@functools.wraps(func)
def decorator(manager, ctx, *args, **kwargs):
try:
context.current()
except RuntimeError:
new_ctx = context.ClimateContext(ctx.values)
with new_ctx:
return func(manager, *args, **kwargs)
else:
return func(manager, ctx, *args, **kwargs)
return decorator
def with_empty_context(func):
@functools.wraps(func)
def decorator(*args, **kwargs):
with context.ClimateContext():
return func(*args, **kwargs)
return decorator
def prepare_service(argv=[]):
rpc.set_defaults(control_exchange='climate')
cfg.set_defaults(log.log_opts,
default_log_levels=['amqplib=WARN',
'qpid.messaging=INFO',
'stevedore=INFO',
'eventlet.wsgi.server=WARN'
])
cfg.CONF(argv[1:], project='climate')
log.setup('climate')