Capture and report errors in sibling installation

When sibling installation fails, we only get MODULE_FAILURE and no
feedback. Wrap the guts in an exception handler so we can figure out
what's wrong.

Change-Id: I42b61ad58415bacbd0d7b220fb47534745ddaa26
This commit is contained in:
Monty Taylor 2018-01-09 09:20:49 -06:00
parent d9bd856c59
commit 3830dafe22
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594

View File

@ -60,6 +60,7 @@ except ImportError:
import os import os
import subprocess import subprocess
import tempfile import tempfile
import traceback
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -156,53 +157,61 @@ def main():
changed = False changed = False
sibling_python_packages = get_sibling_python_packages(projects) try:
for name, root in sibling_python_packages.items(): sibling_python_packages = get_sibling_python_packages(projects)
log.append("Sibling {name} at {root}".format(name=name, root=root)) for name, root in sibling_python_packages.items():
found_sibling_packages = [] log.append("Sibling {name} at {root}".format(name=name, root=root))
for package in get_installed_packages(tox_python): found_sibling_packages = []
log.append( for package in get_installed_packages(tox_python):
"Found {name} python package installed".format(name=package.name))
if package.name == package_name:
# We don't need to re-process ourself. We've filtered ourselves
# from the source dir list, but let's be sure nothing is weird.
log.append( log.append(
"Skipping {name} because it's us".format(name=package.name)) "Found {name} python package installed".format(
continue name=package.name))
if package.name in sibling_python_packages: if package.name == package_name:
# We don't need to re-process ourself. We've filtered ourselves
# from the source dir list, but let's be sure nothing is weird.
log.append(
"Skipping {name} because it's us".format(
name=package.name))
continue
if package.name in sibling_python_packages:
log.append(
"Package {name} on system in {root}".format(
name=package.name,
root=sibling_python_packages[package.name]))
changed = True
log.append("Uninstalling {name}".format(name=package.name))
uninstall_output = subprocess.check_output(
[tox_python, '-m', 'pip', 'uninstall', '-y', package.name],
stderr=subprocess.STDOUT)
log.extend(uninstall_output.decode('utf-8').split('\n'))
found_sibling_packages.append(package.name)
args = [tox_python, '-m', 'pip', 'install']
if constraints:
constraints_file = write_new_constraints_file(
constraints, found_sibling_packages)
args.extend(['-c', constraints_file])
for sibling_package in found_sibling_packages:
log.append( log.append(
"Package {name} on system in {root}".format( "Installing {name} from {root}".format(
name=package.name, name=sibling_package,
root=sibling_python_packages[package.name])) root=sibling_python_packages[sibling_package]))
changed = True args.append('-e')
args.append(sibling_python_packages[sibling_package])
log.append("Uninstalling {name}".format(name=package.name)) install_output = subprocess.check_output(args)
uninstall_output = subprocess.check_output( log.extend(install_output.decode('utf-8').split('\n'))
[tox_python, '-m', 'pip', 'uninstall', '-y', package.name], except Exception as e:
stderr=subprocess.STDOUT) tb = traceback.format_exc()
log.extend(uninstall_output.decode('utf-8').split('\n')) log.append(str(e))
found_sibling_packages.append(package.name) log.append(tb)
module.fail_json(msg=str(e), log="\n".join(log))
args = [tox_python, '-m', 'pip', 'install'] finally:
log_text = "\n".join(log)
if constraints: module.append_to_file(log_file, log_text)
constraints_file = write_new_constraints_file(
constraints, found_sibling_packages)
args.extend(['-c', constraints_file])
for sibling_package in found_sibling_packages:
log.append(
"Installing {name} from {root}".format(
name=sibling_package,
root=sibling_python_packages[sibling_package]))
args.append('-e')
args.append(sibling_python_packages[sibling_package])
install_output = subprocess.check_output(args)
log.extend(install_output.decode('utf-8').split('\n'))
log_text = "\n".join(log)
module.append_to_file(log_file, log_text)
module.exit_json(changed=changed, msg=log_text) module.exit_json(changed=changed, msg=log_text)