From f8ffb0c2c7c9ecef8f524d092854fde49bd7abe3 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Thu, 28 Nov 2013 13:12:39 +0000 Subject: [PATCH] Stop services appropriate for the installation and convert unit_private_ip to hostnames if needed --- hooks/quantum_hooks.py | 7 +++---- hooks/quantum_utils.py | 27 ++++++++++++++++++------ revision | 2 +- unit_tests/test_quantum_hooks.py | 4 ++-- unit_tests/test_quantum_utils.py | 35 +++++++++++++++++++++++++++++--- 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/hooks/quantum_hooks.py b/hooks/quantum_hooks.py index ef53926e..10f7d3a2 100755 --- a/hooks/quantum_hooks.py +++ b/hooks/quantum_hooks.py @@ -17,7 +17,6 @@ from charmhelpers.fetch import ( from charmhelpers.core.host import ( restart_on_change, lsb_release, - service_stop ) from charmhelpers.contrib.hahelpers.cluster import( eligible_leader @@ -27,7 +26,7 @@ from charmhelpers.contrib.hahelpers.apache import( ) from charmhelpers.contrib.openstack.utils import ( configure_installation_source, - openstack_upgrade_available + openstack_upgrade_available, ) from charmhelpers.payload.execd import execd_preinstall @@ -42,6 +41,7 @@ from quantum_utils import ( valid_plugin, configure_ovs, reassign_agent_resources, + stop_services ) from quantum_contexts import ( DB_USER, QUANTUM_DB, @@ -143,8 +143,7 @@ def cluster_departed(): @hooks.hook('cluster-relation-broken') @hooks.hook('stop') def stop(): - service_stop('neutron-l3-agent') - + stop_services() if __name__ == '__main__': try: diff --git a/hooks/quantum_utils.py b/hooks/quantum_utils.py index 91bfd15c..a7861ee5 100644 --- a/hooks/quantum_utils.py +++ b/hooks/quantum_utils.py @@ -1,9 +1,12 @@ -from charmhelpers.core.host import service_running +from charmhelpers.core.host import ( + service_running, + service_stop +) from charmhelpers.core.hookenv import ( log, config, relations_of_type, - unit_private_ip, + unit_private_ip ) from charmhelpers.fetch import ( apt_install, @@ -12,12 +15,13 @@ from charmhelpers.fetch import ( from charmhelpers.contrib.network.ovs import ( add_bridge, add_bridge_port, - full_restart, + full_restart ) from charmhelpers.contrib.openstack.utils import ( configure_installation_source, get_os_codename_install_source, - get_os_codename_package + get_os_codename_package, + get_hostname ) import charmhelpers.contrib.openstack.context as context @@ -272,6 +276,16 @@ def register_configs(): return configs +def stop_services(): + name = networking_name() + svcs = set() + for ctxt in CONFIG_FILES[name][config('plugin')].itervalues(): + for svc in ctxt['services']: + svcs.add(svc) + for svc in svcs: + service_stop(svc) + + def restart_map(): ''' Determine the correct resource map to be passed to @@ -321,7 +335,8 @@ def reassign_agent_resources(): partner_gateways = [unit_private_ip().split('.')[0]] for partner_gateway in relations_of_type(reltype='cluster'): - partner_gateways.append(partner_gateway['private-address'].split('.')[0]) + gateway_hostname = get_hostname(partner_gateway['private-address']) + partner_gateways.append(gateway_hostname.partition('.')[0]) agents = quantum.list_agents(agent_type=DHCP_AGENT) dhcp_agents = [] @@ -335,7 +350,7 @@ def reassign_agent_resources(): agent['id'])['networks']: networks[network['id']] = agent['id'] else: - if agent['host'].split('.')[0] in partner_gateways: + if agent['host'].partition('.')[0] in partner_gateways: dhcp_agents.append(agent['id']) agents = quantum.list_agents(agent_type=L3_AGENT) diff --git a/revision b/revision index e1617e84..04f9fe46 100644 --- a/revision +++ b/revision @@ -1 +1 @@ -57 +59 diff --git a/unit_tests/test_quantum_hooks.py b/unit_tests/test_quantum_hooks.py index e12c2c29..dae751b1 100644 --- a/unit_tests/test_quantum_hooks.py +++ b/unit_tests/test_quantum_hooks.py @@ -35,7 +35,7 @@ TO_PATCH = [ 'get_common_package', 'execd_preinstall', 'lsb_release', - 'service_stop', + 'stop_services', ] @@ -161,4 +161,4 @@ class TestQuantumHooks(CharmTestCase): def test_stop(self): self._call_hook('stop') - self.service_stop.assert_called_with('neutron-l3-agent') + self.stop_services.assert_called diff --git a/unit_tests/test_quantum_utils.py b/unit_tests/test_quantum_utils.py index 173cf836..2fd7f0e2 100644 --- a/unit_tests/test_quantum_utils.py +++ b/unit_tests/test_quantum_utils.py @@ -36,6 +36,7 @@ TO_PATCH = [ 'NetworkServiceContext', 'unit_private_ip', 'relations_of_type', + 'service_stop', ] @@ -178,6 +179,30 @@ class TestQuantumUtils(CharmTestCase): ['hook_contexts'] ) + def test_stop_services_nvp(self): + self.config.return_value = 'nvp' + quantum_utils.stop_services() + calls = [call('neutron-dhcp-agent'), + call('nova-api-metadata'), + call('neutron-metadata-agent')] + self.service_stop.assert_has_calls( + calls, + any_order=True, + ) + + def test_stop_services_ovs(self): + self.config.return_value = 'ovs' + quantum_utils.stop_services() + calls = [call('neutron-dhcp-agent'), + call('neutron-plugin-openvswitch-agent'), + call('nova-api-metadata'), + call('neutron-l3-agent'), + call('neutron-metadata-agent')] + self.service_stop.assert_has_calls( + calls, + any_order=True, + ) + def test_restart_map_nvp(self): self.config.return_value = 'nvp' ex_map = { @@ -358,6 +383,7 @@ cluster1 = ['cluster1-machine1.internal'] cluster2 = ['cluster2-machine1.internal', 'cluster2-machine2.internal' 'cluster2-machine3.internal'] + class TestQuantumAgentReallocation(CharmTestCase): def setUp(self): if not neutronclient: @@ -393,14 +419,16 @@ class TestQuantumAgentReallocation(CharmTestCase): self.NetworkServiceContext.return_value = \ DummyNetworkServiceContext(return_value=network_context) dummy_client = MagicMock() - dummy_client.list_agents.side_effect = agents_some_dead_cl2.itervalues() + dummy_client.list_agents.side_effect = \ + agents_some_dead_cl2.itervalues() dummy_client.list_networks_on_dhcp_agent.return_value = \ dhcp_agent_networks dummy_client.list_routers_on_l3_agent.return_value = \ l3_agent_routers _client.return_value = dummy_client self.unit_private_ip.return_value = 'cluster2-machine1.internal' - self.relations_of_type.return_value = [ { 'private-address': 'cluster2-machine3.internal' }] + self.relations_of_type.return_value = \ + [{'private-address': 'cluster2-machine3.internal'}] quantum_utils.reassign_agent_resources() # Ensure routers removed from dead l3 agent @@ -433,7 +461,8 @@ class TestQuantumAgentReallocation(CharmTestCase): self.NetworkServiceContext.return_value = \ DummyNetworkServiceContext(return_value=network_context) dummy_client = MagicMock() - dummy_client.list_agents.side_effect = agents_some_dead_cl1.itervalues() + dummy_client.list_agents.side_effect = \ + agents_some_dead_cl1.itervalues() dummy_client.list_networks_on_dhcp_agent.return_value = \ dhcp_agent_networks dummy_client.list_routers_on_l3_agent.return_value = \