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 --- .../library/oc_serviceaccount_secret.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'roles/lib_openshift/library/oc_serviceaccount_secret.py') 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, -- cgit v1.2.1