Add new Galera Cluster bootstraping method

For systems with systemd there is a wrapper program to bootstrap
Galera Cluster - galera_new_cluster.

This patch introduces a simple method to find a location of a given
executable.

Using this method we can check if a command 'galera_new_cluster' exists
- if it is present it is used to bootstrap Galera Cluster.

Co-Authored-By: Jacek Kaniuk <j.kaniuk@samsung.com>

Change-Id: Iab8f6dd7f908f1e031135e2136abeb228890108a
Signed-off-by: Kasper Hasior <k.hasior@samsung.com>
This commit is contained in:
Kasper Hasior 2019-02-28 12:22:13 +01:00
parent c2911f5525
commit 97d9c34baf
3 changed files with 96 additions and 4 deletions

View File

@ -102,6 +102,26 @@ def exists(path, is_directory=False, as_root=False):
return found
def find_executable(executable, path=None):
"""Finds a location of an executable in the locations listed in 'path'
:param executable File to search.
:type executable string
:param path Lookup directories separated by a path
separartor.
:type path string
"""
if path is None:
path = os.environ.get('PATH', os.defpath)
dirs = path.split(os.pathsep)
for directory in dirs:
exec_path = os.path.join(directory, executable)
if os.path.isfile(exec_path) and os.access(exec_path, os.X_OK):
return exec_path
return None
def _read_file_as_root(path, open_flag, convert_func):
"""Read a file as root.

View File

@ -49,10 +49,13 @@ class MariaDBApp(galera_service.GaleraApp):
"sudo service %s bootstrap"
% result['service'])
elif result['type'] == 'systemd':
# TODO(mwj 2016/01/28): determine RHEL start for MariaDB Cluster
result['cmd_bootstrap_galera_cluster'] = (
"sudo systemctl start %s@bootstrap.service"
% result['service'])
if operating_system.find_executable('galera_new_cluster'):
result['cmd_bootstrap_galera_cluster'] = (
"sudo galera_new_cluster")
else:
result['cmd_bootstrap_galera_cluster'] = (
"sudo systemctl start %s@bootstrap.service"
% result['service'])
return result
@property

View File

@ -984,6 +984,75 @@ class TestOperatingSystem(trove_testtools.TestCase):
"Got unknown keyword args: {'_unknown_kw': 0}"),
'path', _unknown_kw=0)
def test_find_executable_without_path(self):
command = "command"
self._delegate_assert_find_executable(command=command,
path=None,
isfile=True,
access=True,
expected_return_value=(
"/usr/bin/command"))
self._delegate_assert_find_executable(command=command,
path=None,
isfile=True,
access=False,
expected_return_value=None)
self._delegate_assert_find_executable(command=command,
path=None,
isfile=False,
access=True,
expected_return_value=None)
self._delegate_assert_find_executable(command=command,
path=None,
isfile=False,
access=False,
expected_return_value=None)
def test_find_executable_with_path(self):
command = "command"
path = "/home"
self._delegate_assert_find_executable(command=command,
path=path,
isfile=True,
access=True,
expected_return_value=(
"/home/command"))
self._delegate_assert_find_executable(command=command,
path=path,
isfile=True,
access=False,
expected_return_value=None)
self._delegate_assert_find_executable(command=command,
path=path,
isfile=False,
access=True,
expected_return_value=None)
self._delegate_assert_find_executable(command=command,
path=path,
isfile=False,
access=False,
expected_return_value=None)
def _delegate_assert_find_executable(self, command, path, isfile,
access, expected_return_value):
self._assert_find_executable(command, path, isfile, access,
expected_return_value)
@patch.object(os, 'access')
@patch.object(os.path, 'isfile')
@patch.object(os.environ, 'get', return_value="/usr/bin")
def _assert_find_executable(self, command, path, isfile, access,
expected_return_value, mock_environ,
mock_isfile, mock_access):
mock_access.return_value = access
mock_isfile.return_value = isfile
actual_result = operating_system.find_executable(command, path)
self.assertEqual(expected_return_value, actual_result)
if path is None:
mock_environ.assert_called_once()
else:
mock_environ.assert_not_called()
def test_exists(self):
self.assertFalse(
operating_system.exists(tempfile.gettempdir(), is_directory=False))