summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/library/oc_route.py
diff options
context:
space:
mode:
authorJason DeTiberus <jdetiber@redhat.com>2017-02-18 20:01:53 -0500
committerJason DeTiberus <jdetiber@redhat.com>2017-02-21 23:39:32 -0500
commita3603a5187839e25e9d8a51de70fea0b68439839 (patch)
treec6f9dd5beb19a1d6a7df42ee77a3688e9237e3bc /roles/lib_openshift/library/oc_route.py
parent4b8328fe94015b25209d9b3e6c94b2a3cd0cff40 (diff)
downloadopenshift-a3603a5187839e25e9d8a51de70fea0b68439839.tar.gz
openshift-a3603a5187839e25e9d8a51de70fea0b68439839.tar.bz2
openshift-a3603a5187839e25e9d8a51de70fea0b68439839.tar.xz
openshift-a3603a5187839e25e9d8a51de70fea0b68439839.zip
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
Diffstat (limited to 'roles/lib_openshift/library/oc_route.py')
-rw-r--r--roles/lib_openshift/library/oc_route.py45
1 files changed, 31 insertions, 14 deletions
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'])