diff --git a/hooks/neutron_hooks.py b/hooks/neutron_hooks.py index 91ac5fec..ec507269 100755 --- a/hooks/neutron_hooks.py +++ b/hooks/neutron_hooks.py @@ -71,6 +71,7 @@ from neutron_utils import ( resume_unit_helper, remove_legacy_nova_metadata, disable_nova_metadata, + remove_old_packages, ) hooks = Hooks() @@ -160,6 +161,11 @@ def config_changed(): @harden() def upgrade_charm(): install() + packages_removed = remove_old_packages() + if packages_removed and not is_unit_paused_set(): + log("Package purge detected, restarting services", "INFO") + for s in services(): + service_restart(s) config_changed() update_legacy_ha_files(force=True) diff --git a/hooks/neutron_utils.py b/hooks/neutron_utils.py index 8757f211..b12c45eb 100644 --- a/hooks/neutron_utils.py +++ b/hooks/neutron_utils.py @@ -293,6 +293,18 @@ def get_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(get_purge_packages()) + if installed_packages: + apt_purge(installed_packages, fatal=True) + apt_autoremove(purge=True, fatal=True) + return bool(installed_packages) + + def determine_l3ha_packages(): if use_l3ha(): return L3HA_PACKAGES @@ -754,10 +766,8 @@ def do_openstack_upgrade(configs): apt_install(get_early_packages(), fatal=True) apt_install(get_packages(), fatal=True) - installed_packages = filter_missing_packages(get_purge_packages()) - if installed_packages: - apt_purge(installed_packages, fatal=True) - apt_autoremove(purge=True, fatal=True) + remove_old_packages() + # Bug #1802365 neutron-metadata-agent needs restarting after upgrade to # rocky. if CompareOpenStackReleases(os_release('neutron-common')) == 'rocky': diff --git a/unit_tests/test_neutron_hooks.py b/unit_tests/test_neutron_hooks.py index 49ac16ad..69dcc1e3 100644 --- a/unit_tests/test_neutron_hooks.py +++ b/unit_tests/test_neutron_hooks.py @@ -61,6 +61,8 @@ TO_PATCH = [ 'configure_apparmor', 'disable_nova_metadata', 'remove_legacy_nova_metadata', + 'services', + 'remove_old_packages', ] @@ -160,6 +162,7 @@ class TestQuantumHooks(CharmTestCase): _exit.assert_called_with(1) def test_upgrade_charm(self): + self.remove_old_packages.return_value = False _install = self.patch('install') _config_changed = self.patch('config_changed') self._call_hook('upgrade-charm') @@ -167,6 +170,18 @@ class TestQuantumHooks(CharmTestCase): self.assertTrue(_config_changed.called) self.assertTrue(self.install_systemd_override.called) + def test_upgrade_charm_purge(self): + self.is_unit_paused_set.return_value = False + self.remove_old_packages.return_value = True + self.services.return_value = ['neutron-metadata-agent'] + _install = self.patch('install') + _config_changed = self.patch('config_changed') + self._call_hook('upgrade-charm') + self.assertTrue(_install.called) + self.assertTrue(_config_changed.called) + self.assertTrue(self.install_systemd_override.called) + self.service_restart.assert_called_once_with('neutron-metadata-agent') + def test_amqp_joined(self): self._call_hook('amqp-relation-joined') self.relation_set.assert_called_with(