diff options
author | OpenShift Bot <eparis+openshiftbot@redhat.com> | 2017-06-06 11:09:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-06 11:09:13 -0400 |
commit | 07aa3e0fb2e8eb822ad67c2e24e81a8e028280ff (patch) | |
tree | 75dafc899e6a4f3406aa768dc0d6efa92621dd31 /roles/openshift_health_checker/library | |
parent | 2d4709b5a65c5bb2777277420ee08f8554efb1ab (diff) | |
parent | 2e53dbb4c0d9bfe79cd79e0a0ece9db065b286df (diff) | |
download | openshift-07aa3e0fb2e8eb822ad67c2e24e81a8e028280ff.tar.gz openshift-07aa3e0fb2e8eb822ad67c2e24e81a8e028280ff.tar.bz2 openshift-07aa3e0fb2e8eb822ad67c2e24e81a8e028280ff.tar.xz openshift-07aa3e0fb2e8eb822ad67c2e24e81a8e028280ff.zip |
Merge pull request #3643 from juanvallejo/jvallejo/elastic-search-check
Merged by openshift-bot
Diffstat (limited to 'roles/openshift_health_checker/library')
-rw-r--r-- | roles/openshift_health_checker/library/ocutil.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/roles/openshift_health_checker/library/ocutil.py b/roles/openshift_health_checker/library/ocutil.py new file mode 100644 index 000000000..2e60735d6 --- /dev/null +++ b/roles/openshift_health_checker/library/ocutil.py @@ -0,0 +1,74 @@ +#!/usr/bin/python +"""Interface to OpenShift oc command""" + +import os +import shlex +import shutil +import subprocess + +from ansible.module_utils.basic import AnsibleModule + + +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 + + +def main(): + """Module that executes commands on a remote OpenShift cluster""" + + module = AnsibleModule( + argument_spec=dict( + namespace=dict(type="str", required=True), + config_file=dict(type="str", required=True), + cmd=dict(type="str", required=True), + extra_args=dict(type="list", default=[]), + ), + ) + + cmd = [ + locate_oc_binary(), + '--config', module.params["config_file"], + '-n', module.params["namespace"], + ] + shlex.split(module.params["cmd"]) + + failed = True + try: + cmd_result = subprocess.check_output(list(cmd), stderr=subprocess.STDOUT) + failed = False + except subprocess.CalledProcessError as exc: + cmd_result = '[rc {}] {}\n{}'.format(exc.returncode, ' '.join(exc.cmd), exc.output) + except OSError as exc: + # we get this when 'oc' is not there + cmd_result = str(exc) + + module.exit_json( + changed=False, + failed=failed, + result=cmd_result, + ) + + +if __name__ == '__main__': + main() |