From 047ab5ae99eafbc7331791c95c5633c5f31ab87a Mon Sep 17 00:00:00 2001 From: Alina Nesterova Date: Tue, 9 Aug 2016 18:34:44 +0300 Subject: [PATCH] Replace 'lsb_release -is' with the method 'get_os_distrib' It helps identify the distribution using method 'get_is_distrib' closes-bug: #1609505 Change-Id: I59f1f521cd82cb8f5ed65c1a82744deb3268ac2d --- .coveragerc | 2 +- sahara/plugins/cdh/commands.py | 2 +- sahara/plugins/mapr/domain/distro.py | 2 +- sahara/service/ntp_service.py | 8 ++------ sahara/service/shares.py | 3 +-- sahara/service/volumes.py | 2 +- sahara/tests/unit/service/test_ntp_service.py | 3 +++ sahara/tests/unit/service/test_shares.py | 6 +++--- sahara/tests/unit/utils/test_ssh_remote.py | 6 ++++-- sahara/utils/ssh_remote.py | 15 +++++++++++++-- 10 files changed, 30 insertions(+), 19 deletions(-) diff --git a/.coveragerc b/.coveragerc index 1e3cd11537..d1aa06385e 100644 --- a/.coveragerc +++ b/.coveragerc @@ -10,4 +10,4 @@ source = sahara [report] ignore_errors = True -precision = 3 +precision = 3 \ No newline at end of file diff --git a/sahara/plugins/cdh/commands.py b/sahara/plugins/cdh/commands.py index 56faeefa40..8df6ac6550 100644 --- a/sahara/plugins/cdh/commands.py +++ b/sahara/plugins/cdh/commands.py @@ -22,7 +22,7 @@ def _root(remote, cmd, **kwargs): def _get_os_distrib(remote): - return remote.execute_command('lsb_release -is')[1].strip().lower() + return remote.get_os_distrib() def is_centos_os(remote): diff --git a/sahara/plugins/mapr/domain/distro.py b/sahara/plugins/mapr/domain/distro.py index 9937ae5061..8198b84e4f 100644 --- a/sahara/plugins/mapr/domain/distro.py +++ b/sahara/plugins/mapr/domain/distro.py @@ -75,7 +75,7 @@ def get_all(): def get(instance): with instance.remote() as r: - name = r.execute_command('lsb_release -is', run_as_root=True)[1] + name = r.get_os_distrib() for d in get_all(): if d.name in name: return d diff --git a/sahara/service/ntp_service.py b/sahara/service/ntp_service.py index 5e4d36d7b5..cbd2b6afeb 100644 --- a/sahara/service/ntp_service.py +++ b/sahara/service/ntp_service.py @@ -38,16 +38,12 @@ ntp_opts = [ CONF.register_opts(ntp_opts) -def _get_os_distrib(remote): - return remote.execute_command('lsb_release -is')[1].strip().lower() - - def _sudo(remote, cmd): remote.execute_command(cmd, run_as_root=True) def _restart_ntp(remote): - distrib = _get_os_distrib(remote) + distrib = remote.get_os_distrib() cmd = "service %s restart" if distrib == 'ubuntu': cmd = cmd % "ntp" @@ -58,7 +54,7 @@ def _restart_ntp(remote): def _verify_installation(remote): - distrib = _get_os_distrib(remote) + distrib = remote.get_os_distrib() if distrib == 'ubuntu': return remote.execute_command("dpkg -s ntp") else: diff --git a/sahara/service/shares.py b/sahara/service/shares.py index 2d0a2e24a2..05abe92cbd 100644 --- a/sahara/service/shares.py +++ b/sahara/service/shares.py @@ -231,8 +231,7 @@ class _NFSMounter(_ShareHandler): @classmethod def setup_instance(cls, remote): """Prepares an instance to mount this type of share.""" - response = remote.execute_command('lsb_release -is') - distro = response[1].strip().lower() + distro = remote.get_os_distrib() if distro in cls._NFS_CHECKS: command = cls._NFS_CHECKS[distro] remote.execute_command(command, run_as_root=True) diff --git a/sahara/service/volumes.py b/sahara/service/volumes.py index 3e498c41ec..9fd5bc2b87 100644 --- a/sahara/service/volumes.py +++ b/sahara/service/volumes.py @@ -59,7 +59,7 @@ def _is_xfs_enabled(cluster): def _get_os_distrib(remote): - return remote.execute_command('lsb_release -is')[1].strip().lower() + return remote.get_os_distrib() def _check_installed_xfs(instance): diff --git a/sahara/tests/unit/service/test_ntp_service.py b/sahara/tests/unit/service/test_ntp_service.py index 1ab3559c57..7a9d2ffe3b 100644 --- a/sahara/tests/unit/service/test_ntp_service.py +++ b/sahara/tests/unit/service/test_ntp_service.py @@ -45,6 +45,9 @@ class FakeRemote(object): def append_to_file(self, file, text, run_as_root=False): return self.execute_command(file, run_as_root) + def get_os_distrib(self): + return self.execute_command('get_os_distrib') + class FakeInstance(object): def __init__(self, effects, id): diff --git a/sahara/tests/unit/service/test_shares.py b/sahara/tests/unit/service/test_shares.py index b608fa0f56..6ec6267ef6 100644 --- a/sahara/tests/unit/service/test_shares.py +++ b/sahara/tests/unit/service/test_shares.py @@ -45,7 +45,7 @@ def _mock_node_group(ips, share_list): # Returns a mocked node group and a list of mocked # execute_command functions for its instances. - execute_mocks = [mock.Mock(return_value=(None, "centos")) for ip in ips] + execute_mocks = [mock.Mock(return_value="centos") for ip in ips] get_id = mock.Mock(return_value=uuid.uuid4()) instances = [ mock.Mock( @@ -54,7 +54,8 @@ def _mock_node_group(ips, share_list): return_value=mock.Mock( __enter__=mock.Mock( return_value=mock.Mock( - execute_command=execute_mocks[index])), + execute_command=execute_mocks[index], + get_os_distrib=execute_mocks[index])), __exit__=mock.Mock()))) for index, ip in enumerate(ips)] @@ -66,7 +67,6 @@ def _mock_node_group(ips, share_list): def _setup_calls(): return [ - mock.call('lsb_release -is'), mock.call('rpm -q nfs-utils || yum install -y nfs-utils', run_as_root=True)] diff --git a/sahara/tests/unit/utils/test_ssh_remote.py b/sahara/tests/unit/utils/test_ssh_remote.py index b6cf1befbe..73733acbf6 100644 --- a/sahara/tests/unit/utils/test_ssh_remote.py +++ b/sahara/tests/unit/utils/test_ssh_remote.py @@ -33,11 +33,13 @@ class TestEscapeQuotes(testtools.TestCase): class TestGetOsDistrib(testtools.TestCase): @mock.patch('sahara.utils.ssh_remote._execute_command', return_value=[1, 'Ubuntu']) - def test_get_os_distrib(self, p_execute_command): + @mock.patch('sahara.utils.ssh_remote.__get_python_to_execute', + return_value='python3') + def test_get_os_distrib(self, python, p_execute_command): d = ssh_remote._get_os_distrib() p_execute_command.assert_called_once_with( ('printf "import platform\nprint(platform.linux_distribution(' - 'full_distribution_name=0)[0])" | python'), + 'full_distribution_name=0)[0])" | python3'), run_as_root=False) self.assertEqual('ubuntu', d) diff --git a/sahara/utils/ssh_remote.py b/sahara/utils/ssh_remote.py index c4385b3d02..8ef4d0ebbf 100644 --- a/sahara/utils/ssh_remote.py +++ b/sahara/utils/ssh_remote.py @@ -354,17 +354,28 @@ def _read_file_from(remote_file, run_as_root=False): 'rm %s' % fl, run_as_root=True, raise_when_error=False) +def __get_python_to_execute(): + try: + _execute_command('python3 --version') + except Exception: + _execute_command('python2 --version') + return 'python2' + return 'python3' + + def _get_os_distrib(): + python_version = __get_python_to_execute() return _execute_command( ('printf "import platform\nprint(platform.linux_distribution(' - 'full_distribution_name=0)[0])" | python'), + 'full_distribution_name=0)[0])" | {}'.format(python_version)), run_as_root=False)[1].lower().strip() def _get_os_version(): + python_version = __get_python_to_execute() return _execute_command( ('printf "import platform\nprint(platform.linux_distribution()[1])"' - ' | python'), run_as_root=False)[1].strip() + ' | {}'.format(python_version)), run_as_root=False)[1].strip() def _install_packages(packages):