From 5c021fec3ce19212351a449d28d4c26f0c3580c0 Mon Sep 17 00:00:00 2001 From: Andy Trevorah Date: Wed, 12 Mar 2014 11:15:27 +0000 Subject: [PATCH] reworked apt-repository auto-install to be like yum --- packaging/apt_repository | 49 ++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/packaging/apt_repository b/packaging/apt_repository index 34cdc6f4c96..a55fa07a247 100644 --- a/packaging/apt_repository +++ b/packaging/apt_repository @@ -95,6 +95,26 @@ except ImportError: VALID_SOURCE_TYPES = ('deb', 'deb-src') +def install_python_apt(module): + + if not module.check_mode: + apt_get_path = module.get_bin_path('apt-get') + if apt_get_path: + rc, so, se = module.run_command('%s update && %s install python-apt -y -q' % (apt_get_path, apt_get_path)) + if rc == 0: + global apt, apt_pkg + import apt + import apt_pkg + +def install_python_pycurl(module): + + if not module.check_mode: + apt_get_path = module.get_bin_path('apt-get') + if apt_get_path: + rc, so, se = module.run_command('%s update && %s install python-pycurl -y -q' % (apt_get_path, apt_get_path)) + if rc == 0: + global pycurl + import pycurl class CurlCallback: def __init__(self): @@ -361,25 +381,24 @@ def main(): repo=dict(required=True), state=dict(choices=['present', 'absent'], default='present'), update_cache = dict(aliases=['update-cache'], type='bool', default='yes'), + # this should not be needed, but exists as a failsafe + install_python_apt=dict(required=False, default="yes", type='bool'), + # this should not be needed, but exists as a failsafe + install_python_pycurl=dict(required=False, default="yes", type='bool'), ), supports_check_mode=True, ) - if not HAVE_PYTHON_APT: - try: - module.run_command('apt-get update && apt-get install python-apt -y -q') - global apt, apt_pkg - import apt - import apt_pkg - except: - module.fail_json(msg='Could not import python modules: apt, apt_pkg. Please install python-apt package.') - - if not HAVE_PYCURL: - module.fail_json(msg='Could not import python modules: pycurl. Please install python-pycurl package.') - - repo = module.params['repo'] - state = module.params['state'] - update_cache = module.params['update_cache'] + params = module.params + if params['install_python_apt'] and not HAVE_PYTHON_APT and not module.check_mode: + install_python_apt(module) + + if params['install_python_pycurl'] and not HAVE_PYCURL and not module.check_mode: + install_python_pycurl(module) + + repo = params['repo'] + state = params['state'] + update_cache = params['update_cache'] sourceslist = None if isinstance(distro, aptsources.distro.UbuntuDistribution):