From 2851aefeb592e7c9bfdbb81185920d5226629c1c Mon Sep 17 00:00:00 2001 From: James Page Date: Thu, 15 Nov 2018 12:25:16 +0000 Subject: [PATCH] Deal with py2-py3 migration during charm upgrade On charm upgrade the charm may switch to py3 packages. If so, ensure the old py2 packages are purged after any new packages have been installed. If packages where removed during the upgrade, then restart all services to pickup any py2->py3 switches. Change-Id: I8a17f679e85448680ab0b1f5ae362ef7a65b035c Closes-Bug: 1803451 --- hooks/keystone_hooks.py | 7 +++++++ hooks/keystone_utils.py | 17 +++++++++++++---- unit_tests/test_keystone_hooks.py | 6 ++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/hooks/keystone_hooks.py b/hooks/keystone_hooks.py index 5d514a0f..2af8ea73 100755 --- a/hooks/keystone_hooks.py +++ b/hooks/keystone_hooks.py @@ -123,6 +123,7 @@ from keystone_utils import ( key_write, pause_unit_helper, resume_unit_helper, + remove_old_packages, ) from charmhelpers.contrib.hahelpers.cluster import ( @@ -707,6 +708,7 @@ def configure_https(): def upgrade_charm(): status_set('maintenance', 'Installing apt packages') apt_install(filter_installed_packages(determine_packages())) + packages_removed = remove_old_packages() if run_in_apache(): disable_unused_apache_sites() @@ -718,6 +720,11 @@ def upgrade_charm(): update_nrpe_config() + if packages_removed: + log("Package purge detected, restarting services", "INFO") + for s in services(): + service_restart(s) + if is_elected_leader(CLUSTER_RES): log('Cluster leader - ensuring endpoint configuration is up to ' 'date', level=DEBUG) diff --git a/hooks/keystone_utils.py b/hooks/keystone_utils.py index 8136ba67..16354e56 100644 --- a/hooks/keystone_utils.py +++ b/hooks/keystone_utils.py @@ -647,6 +647,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 save_script_rc(): env_vars = {'OPENSTACK_SERVICE_KEYSTONE': 'keystone', 'OPENSTACK_PORT_ADMIN': determine_api_port( @@ -680,10 +692,7 @@ def do_openstack_upgrade(configs): apt_install(packages=determine_packages(), options=dpkg_opts, fatal=True) - installed_pkgs = filter_missing_packages(determine_purge_packages()) - if installed_pkgs: - apt_purge(installed_pkgs, fatal=True) - apt_autoremove(purge=True, fatal=True) + remove_old_packages() else: # TODO: Add support for upgrade from deb->snap # NOTE(thedac): Setting devmode until LP#1719636 is fixed diff --git a/unit_tests/test_keystone_hooks.py b/unit_tests/test_keystone_hooks.py index 2fe4786e..d942f952 100644 --- a/unit_tests/test_keystone_hooks.py +++ b/unit_tests/test_keystone_hooks.py @@ -90,6 +90,8 @@ TO_PATCH = [ 'key_leader_set', 'key_setup', 'key_write', + 'remove_old_packages', + 'services', # other 'check_call', 'execd_preinstall', @@ -700,11 +702,15 @@ class KeystoneRelationTests(CharmTestCase): mock_is_db_initialised.return_value = True mock_is_db_ready.return_value = True mock_relation_ids.return_value = [] + self.remove_old_packages.return_value = True + self.services.return_value = ['apache2'] self.filter_installed_packages.return_value = [] hooks.upgrade_charm() self.assertTrue(self.apt_install.called) self.assertTrue(update.called) + self.remove_old_packages.assert_called_once_with() + self.service_restart.assert_called_with('apache2') @patch.object(hooks, 'update_all_identity_relation_units') @patch.object(hooks, 'is_db_initialised')