Use openstack.common.cfg for ceilometer options

Change-Id: Iba7df922a7d6b3eec7db427b4135e6fa2ad2d416
Signed-off-by: Julien Danjou <julien.danjou@enovance.com>
This commit is contained in:
Julien Danjou 2012-05-29 16:18:04 +02:00
parent bbc706c4c0
commit a49e59b115
5 changed files with 38 additions and 19 deletions

View File

@ -18,14 +18,13 @@
import pkg_resources import pkg_resources
from nova import flags
from nova import log as logging from nova import log as logging
from nova import manager from nova import manager
from nova import rpc from nova import rpc
from ceilometer import meter from ceilometer import meter
from ceilometer import cfg
FLAGS = flags.FLAGS
# FIXME(dhellmann): We need to have the main program set up logging # FIXME(dhellmann): We need to have the main program set up logging
# correctly so messages from modules outside of the nova package # correctly so messages from modules outside of the nova package
@ -77,9 +76,9 @@ class AgentManager(manager.Manager):
'args': {'data': meter.meter_message_from_counter(c), 'args': {'data': meter.meter_message_from_counter(c),
}, },
} }
rpc.cast(context, FLAGS.metering_topic, msg) rpc.cast(context, cfg.CONF.metering_topic, msg)
rpc.cast(context, rpc.cast(context,
FLAGS.metering_topic + '.' + c.type, cfg.CONF.metering_topic + '.' + c.type,
msg) msg)
except Exception as err: except Exception as err:
LOG.warning('Continuing after error from %s: %s', name, err) LOG.warning('Continuing after error from %s: %s', name, err)

21
ceilometer/cfg.py Normal file
View File

@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
#
# Copyright © 2012 eNovance <licensing@enovance.com>
#
# Author: 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.
from openstack.common.cfg import *
CONF = ConfigOpts()

View File

@ -25,6 +25,7 @@ from nova.rpc import dispatcher as rpc_dispatcher
from ceilometer import rpc from ceilometer import rpc
from ceilometer import meter from ceilometer import meter
from ceilometer import cfg
from ceilometer.collector import dispatcher from ceilometer.collector import dispatcher
# FIXME(dhellmann): There must be another way to do this. # FIXME(dhellmann): There must be another way to do this.
@ -61,9 +62,9 @@ class CollectorManager(manager.Manager):
# Set ourselves up as a separate worker for the metering data, # Set ourselves up as a separate worker for the metering data,
# since the default for manager is to use create_consumer(). # since the default for manager is to use create_consumer().
self.connection.create_worker( self.connection.create_worker(
flags.FLAGS.metering_topic, cfg.CONF.metering_topic,
rpc_dispatcher.RpcDispatcher([self]), rpc_dispatcher.RpcDispatcher([self]),
'ceilometer.collector.' + flags.FLAGS.metering_topic, 'ceilometer.collector.' + cfg.CONF.metering_topic,
) )
self.connection.consume_in_thread() self.connection.consume_in_thread()
@ -81,10 +82,10 @@ class CollectorManager(manager.Manager):
}, },
} }
ctxt = context.get_admin_context() ctxt = context.get_admin_context()
nova_rpc.cast(ctxt, FLAGS.metering_topic, msg) nova_rpc.cast(ctxt, cfg.CONF.metering_topic, msg)
nova_rpc.cast(ctxt, nova_rpc.cast(ctxt,
FLAGS.metering_topic + '.' + counter.type, cfg.CONF.metering_topic + '.' + counter.type,
msg) msg)
def record_metering_data(self, context, data): def record_metering_data(self, context, data):
"""This method is triggered when metering data is """This method is triggered when metering data is

View File

@ -22,8 +22,7 @@ import hashlib
import hmac import hmac
import uuid import uuid
from nova import flags from ceilometer import cfg
from nova.openstack.common import cfg
METER_OPTS = [ METER_OPTS = [
cfg.StrOpt('metering_secret', cfg.StrOpt('metering_secret',
@ -36,13 +35,13 @@ METER_OPTS = [
), ),
] ]
flags.FLAGS.register_opts(METER_OPTS) cfg.CONF.register_opts(METER_OPTS)
def compute_signature(message): def compute_signature(message):
"""Return the signature for a message dictionary. """Return the signature for a message dictionary.
""" """
digest_maker = hmac.new(flags.FLAGS.metering_secret, '', hashlib.sha256) digest_maker = hmac.new(cfg.CONF.metering_secret, '', hashlib.sha256)
for name, value in sorted(message.iteritems()): for name, value in sorted(message.iteritems()):
if name == 'message_signature': if name == 'message_signature':
# Skip any existing signature value, which would not have # Skip any existing signature value, which would not have
@ -70,7 +69,7 @@ def meter_message_from_counter(counter):
'resource_metadata': counter.resource_metadata, 'resource_metadata': counter.resource_metadata,
'message_id': str(uuid.uuid1()), 'message_id': str(uuid.uuid1()),
# This field is used by the notification system. # This field is used by the notification system.
'event_type': '%s.%s' % (flags.FLAGS.metering_topic, counter.type), 'event_type': '%s.%s' % (cfg.CONF.metering_topic, counter.type),
} }
msg['message_signature'] = compute_signature(msg) msg['message_signature'] = compute_signature(msg)
return msg return msg

View File

@ -20,8 +20,7 @@
from ceilometer import counter from ceilometer import counter
from ceilometer import meter from ceilometer import meter
from ceilometer import cfg
from nova import flags
def test_compute_signature_change_key(): def test_compute_signature_change_key():
@ -53,12 +52,12 @@ def test_compute_signature_signed():
def test_compute_signature_use_configured_secret(): def test_compute_signature_use_configured_secret():
data = {'a': 'A', 'b': 'B'} data = {'a': 'A', 'b': 'B'}
sig1 = meter.compute_signature(data) sig1 = meter.compute_signature(data)
old_secret = flags.FLAGS.metering_secret old_secret = cfg.CONF.metering_secret
try: try:
flags.FLAGS.metering_secret = 'not the default value' cfg.CONF.metering_secret = 'not the default value'
sig2 = meter.compute_signature(data) sig2 = meter.compute_signature(data)
finally: finally:
flags.FLAGS.metering_secret = old_secret cfg.CONF.metering_secret = old_secret
assert sig1 != sig2 assert sig1 != sig2