From 0bf0ddaa790698b3baf0dd86a9eef46a081a9cf4 Mon Sep 17 00:00:00 2001 From: Bass T Date: Fri, 8 Apr 2016 15:54:31 -0400 Subject: [PATCH] Helper method to use dnf instead of yum on fedora >=22 There are multiple places where packages may be installed at cluster launch time or even during the run of an EDP job (for instance on-demand manila share mounting). Fedora is transitioning from use of 'yum' to 'dnf'. While yum is still available in F22 and F23, it has been deprecated and ultimately will be replaced with dnf. This helper method will process said install commands using dnf on yum 22 and above and yum otherwise. Based on feedback, replacing the new method by modifying the one in ssh_remote instead. Change-Id: If0f0356710293da16e047481166991a0287d6f17 Closes-Bug:1561153 --- sahara/tests/unit/utils/test_ssh_remote.py | 28 ++++++++++++++++++---- sahara/utils/ssh_remote.py | 22 +++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/sahara/tests/unit/utils/test_ssh_remote.py b/sahara/tests/unit/utils/test_ssh_remote.py index 7e004160a0..3f8f0b123d 100644 --- a/sahara/tests/unit/utils/test_ssh_remote.py +++ b/sahara/tests/unit/utils/test_ssh_remote.py @@ -42,9 +42,11 @@ class TestGetOsDistrib(testtools.TestCase): class TestInstallPackages(testtools.TestCase): + @mock.patch('sahara.utils.ssh_remote._get_os_version') @mock.patch('sahara.utils.ssh_remote._get_os_distrib') @mock.patch('sahara.utils.ssh_remote._execute_command') - def test_install_packages(self, p_execute_command, p_get_os_distrib): + def test_install_packages(self, p_execute_command, p_get_os_distrib, + p_get_os_version): packages = ('git', 'emacs', 'tree') # test ubuntu @@ -60,13 +62,22 @@ class TestInstallPackages(testtools.TestCase): 'yum install -y git emacs tree', run_as_root=True) - # test fedora + # test fedora < 22 p_get_os_distrib.return_value = 'fedora' + p_get_os_version.return_value = 20 ssh_remote._install_packages(packages) p_execute_command.assert_called_with( 'yum install -y git emacs tree', run_as_root=True) + # test fedora >=22 + p_get_os_distrib.return_value = 'fedora' + p_get_os_version.return_value = 23 + ssh_remote._install_packages(packages) + p_execute_command.assert_called_with( + 'dnf install -y git emacs tree', + run_as_root=True) + # test redhat p_get_os_distrib.return_value = 'redhat' ssh_remote._install_packages(packages) @@ -84,9 +95,11 @@ class TestInstallPackages(testtools.TestCase): class TestUpdateRepository(testtools.TestCase): + @mock.patch('sahara.utils.ssh_remote._get_os_version') @mock.patch('sahara.utils.ssh_remote._get_os_distrib') @mock.patch('sahara.utils.ssh_remote._execute_command') - def test_update_repository(self, p_execute_command, p_get_os_distrib): + def test_update_repository(self, p_execute_command, p_get_os_distrib, + p_get_os_version): # test ubuntu p_get_os_distrib.return_value = 'ubuntu' ssh_remote._update_repository() @@ -99,12 +112,19 @@ class TestUpdateRepository(testtools.TestCase): p_execute_command.assert_called_with( 'yum clean all', run_as_root=True) - # test fedora + # test fedora < 22 p_get_os_distrib.return_value = 'fedora' + p_get_os_version.return_value = 20 ssh_remote._update_repository() p_execute_command.assert_called_with( 'yum clean all', run_as_root=True) + # test fedora >=22 + p_get_os_distrib.return_value = 'fedora' + p_get_os_version.return_value = 23 + ssh_remote._update_repository() + p_execute_command.assert_called_with( + 'dnf clean all', run_as_root=True) # test redhat p_get_os_distrib.return_value = 'redhat' ssh_remote._update_repository() diff --git a/sahara/utils/ssh_remote.py b/sahara/utils/ssh_remote.py index e833515813..057346fdb8 100644 --- a/sahara/utils/ssh_remote.py +++ b/sahara/utils/ssh_remote.py @@ -360,11 +360,23 @@ def _get_os_distrib(): run_as_root=False)[1].lower() +def _get_os_version(): + return _execute_command( + ('printf "import platform\nprint(platform.linux_distribution()[1])"' + ' | python'), run_as_root=False) + + def _install_packages(packages): distrib = _get_os_distrib() if distrib == 'ubuntu': cmd = 'RUNLEVEL=1 apt-get install -y %(pkgs)s' - elif distrib in ('redhat', 'centos', 'fedora'): + elif distrib == 'fedora': + fversion = _get_os_version() + if fversion >= 22: + cmd = 'dnf install -y %(pkgs)s' + else: + cmd = 'yum install -y %(pkgs)s' + elif distrib in ('redhat', 'centos'): cmd = 'yum install -y %(pkgs)s' else: raise ex.NotImplementedException( @@ -379,7 +391,13 @@ def _update_repository(): distrib = _get_os_distrib() if distrib == 'ubuntu': cmd = 'apt-get update' - elif distrib in ('redhat', 'centos', 'fedora'): + elif distrib == 'fedora': + fversion = _get_os_version() + if fversion >= 22: + cmd = 'dnf clean all' + else: + cmd = 'yum clean all' + elif distrib in ('redhat', 'centos'): cmd = 'yum clean all' else: raise ex.NotImplementedException(