From f36d6e5cb1ada0cf104dcd891ebcb01b075514b2 Mon Sep 17 00:00:00 2001 From: Shane Peters Date: Thu, 9 Mar 2017 11:29:56 -0500 Subject: [PATCH] Set os-network relation data on cluster join By providing openstack network relation data during cluster join we allow haproxy backends to be configured in the corresponding os networks should a 'public-address' not be available. Change-Id: Ie370552410fffb859d236a011844427d8b3aff45 Closes-Bug: #1669044 --- hooks/ceilometer_hooks.py | 16 ++++++++++++-- tox.ini | 2 +- unit_tests/test_ceilometer_hooks.py | 34 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/hooks/ceilometer_hooks.py b/hooks/ceilometer_hooks.py index 66db49c..f28fb21 100755 --- a/hooks/ceilometer_hooks.py +++ b/hooks/ceilometer_hooks.py @@ -77,7 +77,8 @@ from charmhelpers.contrib.openstack.ip import ( from charmhelpers.contrib.charmsupport import nrpe from charmhelpers.contrib.network.ip import ( get_iface_for_address, - get_netmask_for_address + get_netmask_for_address, + get_address_in_network ) from charmhelpers.contrib.hahelpers.cluster import ( get_hacluster_config, @@ -223,6 +224,8 @@ def upgrade_charm(): install() update_nrpe_config() any_changed() + for rid in relation_ids('cluster'): + cluster_joined(relation_id=rid) def install_ceilometer_ocf(): @@ -247,7 +250,7 @@ def install_ceilometer_ocf(): @hooks.hook('cluster-relation-joined') @restart_on_change(restart_map(), stopstart=True) -def cluster_joined(): +def cluster_joined(relation_id=None): install_ceilometer_ocf() # If this node is the elected leader then share our secret with other nodes @@ -255,6 +258,15 @@ def cluster_joined(): peer_store('shared_secret', get_shared_secret()) CONFIGS.write_all() + for addr_type in [ADMIN, PUBLIC, INTERNAL]: + address = get_address_in_network( + config('os-{}-network'.format(addr_type)) + ) + if address: + relation_set( + relation_id=relation_id, + relation_settings={'{}-address'.format(addr_type): address} + ) @hooks.hook('cluster-relation-changed', diff --git a/tox.ini b/tox.ini index d8d8d03..1463d53 100644 --- a/tox.ini +++ b/tox.ini @@ -14,7 +14,7 @@ install_command = pip install --allow-unverified python-apt {opts} {packages} commands = ostestr {posargs} whitelist_externals = juju -passenv = HOME TERM AMULET_* +passenv = HOME TERM AMULET_* CS_API_URL [testenv:py27] basepython = python2.7 diff --git a/unit_tests/test_ceilometer_hooks.py b/unit_tests/test_ceilometer_hooks.py index aa5bf84..9956722 100644 --- a/unit_tests/test_ceilometer_hooks.py +++ b/unit_tests/test_ceilometer_hooks.py @@ -148,6 +148,15 @@ class CeilometerHooksTest(CharmTestCase): self.assertTrue(changed.called) self.assertTrue(install.called) + @patch('charmhelpers.core.hookenv.config') + @patch.object(hooks, 'cluster_joined') + def test_upgrade_charm_with_cluster(self, cluster_joined, mock_config): + self.relation_ids.return_value = ['ceilometer/0', + 'ceilometer/1', + 'ceilometer/2'] + hooks.hooks.execute(['hooks/upgrade-charm']) + self.assertEquals(cluster_joined.call_count, 3) + @patch.object(hooks, 'install_event_pipeline_setting') @patch('charmhelpers.core.hookenv.config') @patch.object(hooks, 'ceilometer_joined') @@ -280,6 +289,31 @@ class CeilometerHooksTest(CharmTestCase): self.peer_store.assert_called_with('shared_secret', 'secret') self.assertTrue(self.CONFIGS.write_all.called) + @patch('charmhelpers.core.hookenv.config') + @patch.object(hooks, 'get_address_in_network') + @patch.object(hooks, 'install_ceilometer_ocf') + @patch.object(hooks, 'is_elected_leader') + def test_cluster_joined_os_networks(self, mock_leader, mock_install_ocf, + get_addr, mock_config): + mock_leader.return_value = False + get_addr.return_value = '10.0.0.100' + rel_settings = {'int-address': '10.0.0.100'} + hooks.hooks.execute(['hooks/cluster-relation-joined']) + self.relation_set.assert_called_with(relation_id=None, + relation_settings=rel_settings) + + @patch('charmhelpers.core.hookenv.config') + @patch.object(hooks, 'get_address_in_network') + @patch.object(hooks, 'install_ceilometer_ocf') + @patch.object(hooks, 'is_elected_leader') + def test_cluster_joined_no_os_networks(self, mock_leader, + mock_install_ocf, + get_addr, mock_config): + mock_leader.return_value = False + get_addr.return_value = None + hooks.hooks.execute(['hooks/cluster-relation-joined']) + self.assertEquals(self.relation_set.call_count, 0) + @patch('charmhelpers.core.hookenv.config') @patch.object(hooks, 'set_shared_secret') def test_cluster_changed(self, shared_secret, mock_config):