diff --git a/hooks/ceilometer_hooks.py b/hooks/ceilometer_hooks.py index c554877..b15bcd4 100755 --- a/hooks/ceilometer_hooks.py +++ b/hooks/ceilometer_hooks.py @@ -1,6 +1,7 @@ #!/usr/bin/python import sys +import json from charmhelpers.fetch import ( apt_install, filter_installed_packages, apt_update @@ -8,7 +9,8 @@ from charmhelpers.fetch import ( from charmhelpers.core.hookenv import ( config, Hooks, UnregisteredHookError, - log + log, + relation_set ) from charmhelpers.core.host import ( restart_on_change @@ -19,7 +21,8 @@ from charmhelpers.contrib.openstack.utils import ( from ceilometer_utils import ( restart_map, register_configs, - CEILOMETER_AGENT_PACKAGES + CEILOMETER_AGENT_PACKAGES, + NOVA_SETTINGS ) hooks = Hooks() @@ -34,9 +37,10 @@ def install(): filter_installed_packages(CEILOMETER_AGENT_PACKAGES), fatal=True) -# TODO(jamespage): Locally scoped relation for nova and others -#ceilometer_utils.modify_config_file(ceilometer_utils.NOVA_CONF -# ceilometer_utils.NOVA_SETTINGS) + +@hooks.hook('nova-ceilometer-relation-joined') +def nova_ceilometer_joined(): + relation_set(subordinate_configuration=json.dumps(NOVA_SETTINGS)) @hooks.hook("ceilometer-service-relation-changed", diff --git a/hooks/ceilometer_utils.py b/hooks/ceilometer_utils.py index e5db4af..34c0cbd 100644 --- a/hooks/ceilometer_utils.py +++ b/hooks/ceilometer_utils.py @@ -18,11 +18,20 @@ CEILOMETER_AGENT_PACKAGES = [ ] NOVA_CONF = "/etc/nova/nova.conf" -NOVA_SETTINGS = [ - ('DEFAULT', 'instance_usage_audit', 'True'), - ('DEFAULT', 'instance_usage_audit_period', 'hour'), - ('DEFAULT', 'notification_driver', 'ceilometer.compute.nova_notifier') -] + +NOVA_SETTINGS = { + "nova": { + "/etc/nova/nova.conf": { + "sections": { + "DEFAULT": [ + ('instance_usage_audit', 'True'), + ('instance_usage_audit_period', 'hour'), + ('notification_driver', 'ceilometer.compute.nova_notifier') + ] + } + } + } +} CONFIG_FILES = { CEILOMETER_CONF: { diff --git a/metadata.yaml b/metadata.yaml index 65a5b2a..16a48e7 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -14,6 +14,10 @@ description: | categories: - miscellaneous - openstack +provides: + nova-ceilometer: + interface: nova-ceilometer + scope: container requires: container: interface: juju-info diff --git a/unit_tests/test_ceilometer_hooks.py b/unit_tests/test_ceilometer_hooks.py index 35db78b..1a49537 100644 --- a/unit_tests/test_ceilometer_hooks.py +++ b/unit_tests/test_ceilometer_hooks.py @@ -1,3 +1,4 @@ +import json from mock import patch, MagicMock import ceilometer_utils @@ -19,6 +20,7 @@ TO_PATCH = [ 'config', 'filter_installed_packages', 'CONFIGS', + 'relation_set', ] @@ -46,3 +48,8 @@ class CeilometerHooksTest(CharmTestCase): def test_ceilometer_changed(self): hooks.hooks.execute(['hooks/ceilometer-service-relation-changed']) self.assertTrue(self.CONFIGS.write_all.called) + + def test_nova_ceilometer_joined(self): + hooks.hooks.execute(['hooks/nova-ceilometer-relation-joined']) + self.relation_set.assert_called_with( + subordinate_configuration=json.dumps(ceilometer_utils.NOVA_SETTINGS)) diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index e90679e..060ba5f 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -58,6 +58,7 @@ def get_default_config(): class CharmTestCase(unittest.TestCase): + def setUp(self, obj, patches): super(CharmTestCase, self).setUp() self.patches = patches @@ -78,6 +79,7 @@ class CharmTestCase(unittest.TestCase): class TestConfig(object): + def __init__(self): self.config = get_default_config() @@ -97,6 +99,7 @@ class TestConfig(object): class TestRelation(object): + def __init__(self, relation_data={}): self.relation_data = relation_data