]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume: util: look for executable in $PATH 32861/head
authorShyukri Shyukriev <shshyukriev@suse.com>
Thu, 21 Nov 2019 14:04:02 +0000 (15:04 +0100)
committerShyukri Shyukriev <shshyukriev@suse.com>
Sat, 25 Jan 2020 20:23:55 +0000 (22:23 +0200)
Fixes: https://tracker.ceph.com/issues/36728
Fallback to predefined paths for backward compatibility.
Alter test involved for partial match in warning

Signed-off-by: Shyukri Shyukriev <shshyukriev@suse.com>
(cherry picked from commit a8577085dc52b0f214d9568c29a9605d1a826a45)

src/ceph-volume/ceph_volume/tests/util/test_system.py
src/ceph-volume/ceph_volume/util/system.py

index 808048a6a9c056bb72c55389f6cdfd8c92c6594a..5ed6d24e3e523e083e4d9e11fb2888ee16a28cf4 100644 (file)
@@ -205,8 +205,9 @@ class TestWhich(object):
         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):
@@ -214,9 +215,7 @@ class TestWhich(object):
         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):
index b5c4ce940aed2691daf48d316810a6e50ccbeb2b..466f63244e7329458577c5805b11569ec2197f17 100644 (file)
@@ -33,7 +33,21 @@ 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',
@@ -41,13 +55,10 @@ def which(executable):
         '/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