From 4b8328fe94015b25209d9b3e6c94b2a3cd0cff40 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sat, 18 Feb 2017 10:21:56 -0500 Subject: roles/lib_openshift: Handle /usr/local/bin/oc with sudo The real changes here are in `src/lib/{base,import}.py` remember, the rest is committed to git rather than built for some reason. This is tested on CentOS AH (python2), I haven't yet tested the Python 3 path here. I tried the suggestion in the PR for using Ansible's `module` but AFAICS that would require passing down the `module` variable pretty far down into this code. This implementation seems OK too. Closes: https://github.com/openshift/openshift-ansible/issues/3410 --- roles/lib_openshift/library/oadm_manage_node.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_edit.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_env.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_label.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_obj.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_process.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_route.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_scale.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_secret.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_service.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_serviceaccount.py | 20 ++++++++++++++++---- .../library/oc_serviceaccount_secret.py | 20 ++++++++++++++++---- roles/lib_openshift/library/oc_version.py | 20 ++++++++++++++++---- 13 files changed, 208 insertions(+), 52 deletions(-) (limited to 'roles/lib_openshift/library') diff --git a/roles/lib_openshift/library/oadm_manage_node.py b/roles/lib_openshift/library/oadm_manage_node.py index 0ac233c72..840882bbc 100644 --- a/roles/lib_openshift/library/oadm_manage_node.py +++ b/roles/lib_openshift/library/oadm_manage_node.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -963,10 +964,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -982,7 +991,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_edit.py b/roles/lib_openshift/library/oc_edit.py index 1b1faf496..9d174ba31 100644 --- a/roles/lib_openshift/library/oc_edit.py +++ b/roles/lib_openshift/library/oc_edit.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -991,10 +992,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1010,7 +1019,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_env.py b/roles/lib_openshift/library/oc_env.py index 1426565b4..d32773c38 100644 --- a/roles/lib_openshift/library/oc_env.py +++ b/roles/lib_openshift/library/oc_env.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -958,10 +959,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -977,7 +986,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_label.py b/roles/lib_openshift/library/oc_label.py index 0db8585a4..0f98728f8 100644 --- a/roles/lib_openshift/library/oc_label.py +++ b/roles/lib_openshift/library/oc_label.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -967,10 +968,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -986,7 +995,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_obj.py b/roles/lib_openshift/library/oc_obj.py index 6d0b391b9..39da0ed5c 100644 --- a/roles/lib_openshift/library/oc_obj.py +++ b/roles/lib_openshift/library/oc_obj.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -970,10 +971,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -989,7 +998,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_process.py b/roles/lib_openshift/library/oc_process.py index a1ee79f6e..d3b2de4dc 100644 --- a/roles/lib_openshift/library/oc_process.py +++ b/roles/lib_openshift/library/oc_process.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -959,10 +960,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -978,7 +987,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_route.py b/roles/lib_openshift/library/oc_route.py index 4b5c4460c..d8d5e1891 100644 --- a/roles/lib_openshift/library/oc_route.py +++ b/roles/lib_openshift/library/oc_route.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -1001,10 +1002,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1020,7 +1029,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_scale.py b/roles/lib_openshift/library/oc_scale.py index c73e96e10..e2a004f54 100644 --- a/roles/lib_openshift/library/oc_scale.py +++ b/roles/lib_openshift/library/oc_scale.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -945,10 +946,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -964,7 +973,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_secret.py b/roles/lib_openshift/library/oc_secret.py index 6ab5e81b2..23d61a4fb 100644 --- a/roles/lib_openshift/library/oc_secret.py +++ b/roles/lib_openshift/library/oc_secret.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -991,10 +992,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1010,7 +1019,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_service.py b/roles/lib_openshift/library/oc_service.py index 319ec4bd7..6e7b1b52e 100644 --- a/roles/lib_openshift/library/oc_service.py +++ b/roles/lib_openshift/library/oc_service.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -997,10 +998,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1016,7 +1025,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_serviceaccount.py b/roles/lib_openshift/library/oc_serviceaccount.py index 7104355f0..59dee8efb 100644 --- a/roles/lib_openshift/library/oc_serviceaccount.py +++ b/roles/lib_openshift/library/oc_serviceaccount.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -943,10 +944,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -962,7 +971,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_serviceaccount_secret.py b/roles/lib_openshift/library/oc_serviceaccount_secret.py index 3a9380661..906134797 100644 --- a/roles/lib_openshift/library/oc_serviceaccount_secret.py +++ b/roles/lib_openshift/library/oc_serviceaccount_secret.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -943,10 +944,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -962,7 +971,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_version.py b/roles/lib_openshift/library/oc_version.py index 9b660e1d3..eeb28e7fc 100644 --- a/roles/lib_openshift/library/oc_version.py +++ b/roles/lib_openshift/library/oc_version.py @@ -36,6 +36,7 @@ import atexit import copy import json import os +import sys import re import shutil import subprocess @@ -915,10 +916,18 @@ class OpenShiftCLI(object): def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' cmds = [] - if oadm: - cmds = ['oadm'] + basecmd = 'oadm' if oadm else 'oc' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + if sys.version_info[0] == 3: + basepath = shutil.which(basecmd) # pylint: disable=no-member else: - cmds = ['oc'] + import distutils.spawn + basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module + if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): + basecmd = '/usr/local/bin/' + basecmd + cmds.append(basecmd) if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -934,7 +943,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, -- cgit v1.2.3 From a3603a5187839e25e9d8a51de70fea0b68439839 Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Sat, 18 Feb 2017 20:01:53 -0500 Subject: lib_openshift oc file lookup improvements - Move binary file search to separate method called from constructor - Use six.PY3 instaed of sys.version_info - Combine additional paths with the paths from the environment - For py3 pass the combined paths to shutil.which - For py2 explictly search for existance of file from combined paths instead of using distutils.spawn.find_executable and falling back - Use 'oc adm' instead of 'oadm' - Fix generate_validation test issue - fix tests for oc binary location - add tests for file lookup --- roles/lib_openshift/library/oadm_manage_node.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_adm_registry.py | 39 ++++++++++++++++--- roles/lib_openshift/library/oc_adm_router.py | 39 ++++++++++++++++--- roles/lib_openshift/library/oc_edit.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_env.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_label.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_obj.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_process.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_route.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_scale.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_secret.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_service.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_serviceaccount.py | 45 +++++++++++++++------- .../library/oc_serviceaccount_secret.py | 45 +++++++++++++++------- roles/lib_openshift/library/oc_version.py | 45 +++++++++++++++------- 15 files changed, 471 insertions(+), 192 deletions(-) (limited to 'roles/lib_openshift/library') diff --git a/roles/lib_openshift/library/oadm_manage_node.py b/roles/lib_openshift/library/oadm_manage_node.py index 840882bbc..f2035c973 100644 --- a/roles/lib_openshift/library/oadm_manage_node.py +++ b/roles/lib_openshift/library/oadm_manage_node.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -749,6 +748,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -762,6 +787,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -963,19 +989,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_adm_registry.py b/roles/lib_openshift/library/oc_adm_registry.py index 32a4f8637..5be9e3e0a 100644 --- a/roles/lib_openshift/library/oc_adm_registry.py +++ b/roles/lib_openshift/library/oc_adm_registry.py @@ -852,6 +852,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -865,6 +891,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -1066,11 +1093,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] + cmds = [self.oc_binary] + if oadm: - cmds = ['oadm'] - else: - cmds = ['oc'] + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1086,7 +1112,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_adm_router.py b/roles/lib_openshift/library/oc_adm_router.py index e6d0f795e..673c389a5 100644 --- a/roles/lib_openshift/library/oc_adm_router.py +++ b/roles/lib_openshift/library/oc_adm_router.py @@ -877,6 +877,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -890,6 +916,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -1091,11 +1118,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] + cmds = [self.oc_binary] + if oadm: - cmds = ['oadm'] - else: - cmds = ['oc'] + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1111,7 +1137,10 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - returncode, stdout, stderr = self._run(cmds, input_data) + try: + returncode, stdout, stderr = self._run(cmds, input_data) + except OSError as ex: + returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex) rval = {"returncode": returncode, "results": results, diff --git a/roles/lib_openshift/library/oc_edit.py b/roles/lib_openshift/library/oc_edit.py index 9d174ba31..e38cf443f 100644 --- a/roles/lib_openshift/library/oc_edit.py +++ b/roles/lib_openshift/library/oc_edit.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -777,6 +776,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -790,6 +815,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -991,19 +1017,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_env.py b/roles/lib_openshift/library/oc_env.py index d32773c38..1e089198f 100644 --- a/roles/lib_openshift/library/oc_env.py +++ b/roles/lib_openshift/library/oc_env.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -744,6 +743,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -757,6 +782,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -958,19 +984,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_label.py b/roles/lib_openshift/library/oc_label.py index 0f98728f8..981e4ef2d 100644 --- a/roles/lib_openshift/library/oc_label.py +++ b/roles/lib_openshift/library/oc_label.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -753,6 +752,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -766,6 +791,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -967,19 +993,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_obj.py b/roles/lib_openshift/library/oc_obj.py index 39da0ed5c..3b9c79fae 100644 --- a/roles/lib_openshift/library/oc_obj.py +++ b/roles/lib_openshift/library/oc_obj.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -756,6 +755,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -769,6 +794,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -970,19 +996,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_process.py b/roles/lib_openshift/library/oc_process.py index d3b2de4dc..996dcefa1 100644 --- a/roles/lib_openshift/library/oc_process.py +++ b/roles/lib_openshift/library/oc_process.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -745,6 +744,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -758,6 +783,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -959,19 +985,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_route.py b/roles/lib_openshift/library/oc_route.py index d8d5e1891..cc096bb88 100644 --- a/roles/lib_openshift/library/oc_route.py +++ b/roles/lib_openshift/library/oc_route.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -787,6 +786,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -800,6 +825,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -1001,19 +1027,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_scale.py b/roles/lib_openshift/library/oc_scale.py index e2a004f54..b70be4dad 100644 --- a/roles/lib_openshift/library/oc_scale.py +++ b/roles/lib_openshift/library/oc_scale.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -731,6 +730,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -744,6 +769,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -945,19 +971,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_secret.py b/roles/lib_openshift/library/oc_secret.py index 23d61a4fb..68213d4d9 100644 --- a/roles/lib_openshift/library/oc_secret.py +++ b/roles/lib_openshift/library/oc_secret.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -777,6 +776,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -790,6 +815,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -991,19 +1017,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_service.py b/roles/lib_openshift/library/oc_service.py index 6e7b1b52e..35c00c189 100644 --- a/roles/lib_openshift/library/oc_service.py +++ b/roles/lib_openshift/library/oc_service.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -783,6 +782,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -796,6 +821,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -997,19 +1023,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_serviceaccount.py b/roles/lib_openshift/library/oc_serviceaccount.py index 59dee8efb..2feb7f32e 100644 --- a/roles/lib_openshift/library/oc_serviceaccount.py +++ b/roles/lib_openshift/library/oc_serviceaccount.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -729,6 +728,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -742,6 +767,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -943,19 +969,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_serviceaccount_secret.py b/roles/lib_openshift/library/oc_serviceaccount_secret.py index 906134797..676337149 100644 --- a/roles/lib_openshift/library/oc_serviceaccount_secret.py +++ b/roles/lib_openshift/library/oc_serviceaccount_secret.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -729,6 +728,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -742,6 +767,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -943,19 +969,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) diff --git a/roles/lib_openshift/library/oc_version.py b/roles/lib_openshift/library/oc_version.py index eeb28e7fc..933bcc331 100644 --- a/roles/lib_openshift/library/oc_version.py +++ b/roles/lib_openshift/library/oc_version.py @@ -36,7 +36,6 @@ import atexit import copy import json import os -import sys import re import shutil import subprocess @@ -701,6 +700,32 @@ class OpenShiftCLIError(Exception): pass +ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')] + + +def locate_oc_binary(): + ''' Find and return oc binary file ''' + # https://github.com/openshift/openshift-ansible/issues/3410 + # oc can be in /usr/local/bin in some cases, but that may not + # be in $PATH due to ansible/sudo + paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS + + oc_binary = 'oc' + + # Use shutil.which if it is available, otherwise fallback to a naive path search + try: + which_result = shutil.which(oc_binary, path=os.pathsep.join(paths)) + if which_result is not None: + oc_binary = which_result + except AttributeError: + for path in paths: + if os.path.exists(os.path.join(path, oc_binary)): + oc_binary = os.path.join(path, oc_binary) + break + + return oc_binary + + # pylint: disable=too-few-public-methods class OpenShiftCLI(object): ''' Class to wrap the command line tools ''' @@ -714,6 +739,7 @@ class OpenShiftCLI(object): self.verbose = verbose self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig) self.all_namespaces = all_namespaces + self.oc_binary = locate_oc_binary() # Pylint allows only 5 arguments to be passed. # pylint: disable=too-many-arguments @@ -915,19 +941,10 @@ class OpenShiftCLI(object): # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' - cmds = [] - basecmd = 'oadm' if oadm else 'oc' - # https://github.com/openshift/openshift-ansible/issues/3410 - # oc can be in /usr/local/bin in some cases, but that may not - # be in $PATH due to ansible/sudo - if sys.version_info[0] == 3: - basepath = shutil.which(basecmd) # pylint: disable=no-member - else: - import distutils.spawn - basepath = distutils.spawn.find_executable(basecmd) # pylint: disable=no-name-in-module - if basepath is None and os.path.isfile('/usr/local/bin/' + basecmd): - basecmd = '/usr/local/bin/' + basecmd - cmds.append(basecmd) + cmds = [self.oc_binary] + + if oadm: + cmds.append('adm') if self.all_namespaces: cmds.extend(['--all-namespaces']) -- cgit v1.2.3