Skip to content

Commit

Permalink
Merge pull request ceph#32860 from shyukri/wip-43281-nautilus
Browse files Browse the repository at this point in the history
nautilus: ceph-volume: util: look for executable in $PATH
  • Loading branch information
jan--f committed Feb 11, 2020
2 parents 7d55a95 + 259dd74 commit 4744db7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/ceph-volume/ceph_volume/tests/util/test_system.py
Expand Up @@ -206,18 +206,17 @@ def test_executable_does_not_exist(self, monkeypatch):
assert system.which('exedir') == 'exedir'

def test_executable_exists_as_file(self, monkeypatch):
monkeypatch.setattr(system.os.path, 'isfile', lambda x: True)
monkeypatch.setattr(system.os.path, 'exists', lambda x: True)
monkeypatch.setattr(system.os, 'getenv', lambda x, y: '')
monkeypatch.setattr(system.os.path, 'isfile', lambda x: x != 'ceph')
monkeypatch.setattr(system.os.path, 'exists', lambda x: x != 'ceph')
assert system.which('ceph') == '/usr/local/bin/ceph'

def test_warnings_when_executable_isnt_matched(self, monkeypatch, capsys):
monkeypatch.setattr(system.os.path, 'isfile', lambda x: True)
monkeypatch.setattr(system.os.path, 'exists', lambda x: False)
system.which('exedir')
cap = capsys.readouterr()
assert 'Absolute path not found for executable: exedir' in cap.err
assert 'Ensure $PATH environment variable contains common executable locations' in cap.err

assert 'Executable exedir not in PATH' in cap.err

@pytest.fixture
def stub_which(monkeypatch):
Expand Down
27 changes: 19 additions & 8 deletions src/ceph-volume/ceph_volume/util/system.py
Expand Up @@ -39,21 +39,32 @@ def generate_uuid():

def which(executable):
"""find the location of an executable"""
locations = (
def _get_path(executable, locations):
for location in locations:
executable_path = os.path.join(location, executable)
if os.path.exists(executable_path) and os.path.isfile(executable_path):
return executable_path
return None

path = os.getenv('PATH', '')
path_locations = path.split(':')
exec_in_path = _get_path(executable, path_locations)
if exec_in_path:
return exec_in_path
mlogger.warning('Executable {} not in PATH: {}'.format(executable, path))

static_locations = (
'/usr/local/bin',
'/bin',
'/usr/bin',
'/usr/local/sbin',
'/usr/sbin',
'/sbin',
)

for location in locations:
executable_path = os.path.join(location, executable)
if os.path.exists(executable_path) and os.path.isfile(executable_path):
return executable_path
mlogger.warning('Absolute path not found for executable: %s', executable)
mlogger.warning('Ensure $PATH environment variable contains common executable locations')
exec_in_static_locations = _get_path(executable, static_locations)
if exec_in_static_locations:
mlogger.warning('Found executable under {}, please ensure $PATH is set correctly!'.format(exec_in_static_locations))
return exec_in_static_locations
# fallback to just returning the argument as-is, to prevent a hard fail,
# and hoping that the system might have the executable somewhere custom
return executable
Expand Down

0 comments on commit 4744db7

Please sign in to comment.