diff --git a/hooks/neutron_api_hooks.py b/hooks/neutron_api_hooks.py index 3fcee0e3..9e7a4b08 100755 --- a/hooks/neutron_api_hooks.py +++ b/hooks/neutron_api_hooks.py @@ -42,6 +42,7 @@ from charmhelpers.core.hookenv import ( from charmhelpers.core.host import ( mkdir, service_reload, + service_restart, ) from charmhelpers.fetch import ( @@ -86,6 +87,7 @@ from neutron_api_utils import ( check_local_db_actions_complete, pause_unit_helper, resume_unit_helper, + remove_old_packages, ) from neutron_api_context import ( get_dns_domain, @@ -295,9 +297,14 @@ def config_changed(): apt_install(filter_installed_packages( determine_packages(config('openstack-origin'))), fatal=True) + packages_removed = remove_old_packages() configure_https() update_nrpe_config() CONFIGS.write_all() + if packages_removed and not is_unit_paused_set(): + log("Package purge detected, restarting services", "INFO") + for s in services(): + service_restart(s) for r_id in relation_ids('neutron-api'): neutron_api_relation_joined(rid=r_id) for r_id in relation_ids('neutron-plugin-api'): diff --git a/hooks/neutron_api_utils.py b/hooks/neutron_api_utils.py index ad402178..6be0e8dc 100755 --- a/hooks/neutron_api_utils.py +++ b/hooks/neutron_api_utils.py @@ -427,6 +427,18 @@ def determine_purge_packages(): return [] +def remove_old_packages(): + '''Purge any packages that need ot be removed. + + :returns: bool Whether packages were removed. + ''' + installed_packages = filter_missing_packages(determine_purge_packages()) + if installed_packages: + apt_purge(installed_packages, fatal=True) + apt_autoremove(purge=True, fatal=True) + return bool(installed_packages) + + def determine_ports(): '''Assemble a list of API ports for services we are managing''' ports = [] @@ -549,10 +561,7 @@ def do_openstack_upgrade(configs): options=dpkg_opts, fatal=True) - installed_packages = filter_missing_packages(determine_purge_packages()) - if installed_packages: - apt_purge(installed_packages, fatal=True) - apt_autoremove(purge=True, fatal=True) + remove_old_packages() # set CONFIGS to load templates from new release configs.set_release(openstack_release=new_os_rel) diff --git a/unit_tests/test_neutron_api_hooks.py b/unit_tests/test_neutron_api_hooks.py index 5a8a7dee..2c4cfbee 100644 --- a/unit_tests/test_neutron_api_hooks.py +++ b/unit_tests/test_neutron_api_hooks.py @@ -91,6 +91,9 @@ TO_PATCH = [ 'get_relation_ip', 'update_dns_ha_resource_params', 'is_nsg_logging_enabled', + 'remove_old_packages', + 'services', + 'service_restart', ] NEUTRON_CONF_DIR = "/etc/neutron" @@ -222,6 +225,7 @@ class NeutronAPIHooksTests(CharmTestCase): self._call_hook('config-changed') def test_config_changed_with_openstack_upgrade_action(self): + self.remove_old_packages.return_value = False self.openstack_upgrade_available.return_value = True self.test_config.set('action-managed-upgrade', True) @@ -229,6 +233,14 @@ class NeutronAPIHooksTests(CharmTestCase): self.assertFalse(self.do_openstack_upgrade.called) + def test_config_changed_with_purge(self): + self.remove_old_packages.return_value = True + self.services.return_value = ['neutron-server'] + self.openstack_upgrade_available.return_value = False + self._call_hook('config-changed') + self.remove_old_packages.assert_called_once_with() + self.service_restart.assert_called_once_with('neutron-server') + def test_amqp_joined(self): self._call_hook('amqp-relation-joined') self.relation_set.assert_called_with(