Skip to content

Commit

Permalink
Handle missing binary errors (resolves #262) (#263)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone authored Jun 2, 2020
1 parent a472305 commit cc62add
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions plugins/module_utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native

import os
import platform
import subprocess

HFC_IMPORT_ERR = None
try:
Expand All @@ -22,15 +27,39 @@
CRYPTOGRAPHY_IMPORT_ERR = str(e)


def missing_required_bin(binary, reason=None, url=None):
hostname = platform.node()
msg = "Failed to execute the required binary (%s) on %s." % (binary, hostname)
if reason:
msg += " This is required %s." % reason
if url:
msg += " See %s for more info." % url

msg += (" Please read module documentation and install in the appropriate location."
" If the required binary is installed, then the install directory may not be present in the PATH environment variable (%s)." % os.environ['PATH'])
return msg


class BlockchainModule(AnsibleModule):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.check_for_missing_libs()
self.check_for_missing_bins()

def check_for_missing_libs(self):
url = 'https://ibm-blockchain.github.io/ansible-collection/installation.html#requirements'
if not HAS_HFC:
self.fail_json(msg=missing_required_lib("fabric-sdk-py", url=url), exception=HFC_IMPORT_ERR)
if not HAS_CRYPTOGRAPHY:
self.fail_json(msg=missing_required_lib("cryptography", url=url), exception=CRYPTOGRAPHY_IMPORT_ERR)

def check_for_missing_bins(self):
url = 'https://ibm-blockchain.github.io/ansible-collection/installation.html#requirements'
for binary in ['peer', 'configtxlator']:
try:
process = subprocess.run([binary, 'version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, text=True, close_fds=True)
except Exception as e:
self.fail_json(msg=missing_required_bin(binary, url=url), exception=to_native(e), cmd=f'{binary} version')
if process.returncode != 0:
self.fail_json(msg=missing_required_bin(binary, url=url), rc=process.returncode, stdout=process.stdout, stderr=process.stderr, cmd=f'{binary} version')

0 comments on commit cc62add

Please sign in to comment.