1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Some very minor stylistic tweaks to the gem module.

This commit is contained in:
Michael DeHaan 2013-03-01 17:55:13 -05:00
parent 64c1cc3efd
commit 6ebf16bf33

View file

@ -34,13 +34,10 @@ options:
description: The desired state of the gem. C(latest) ensures that the latest version is installed. description: The desired state of the gem. C(latest) ensures that the latest version is installed.
required: true required: true
choices: [present, absent, latest] choices: [present, absent, latest]
gemsource: gem_source:
description: The path to a local gem used as installation source. description: The path to a local gem used as installation source.
required: false required: false
http-proxy: include_dependencies:
description: HTTP Proxy for remote operations.
required: false
includedependencies:
description: Wheter to include dependencies or not. description: Wheter to include dependencies or not.
required: false required: false
choices: [true, false] choices: [true, false]
@ -52,53 +49,59 @@ options:
version: version:
description: Version of the gem to be installed/removed. description: Version of the gem to be installed/removed.
required: false required: false
examples:
- code: gem name=vagrant version=1.0 state=present
description: Installs version 1.0 of vagrant.
- code: gem name=rake state=latest
description: Installs latest available version of rake.
- code: gem name=rake gemsource=/path/to/gems/rake-1.0.gem state=present
description: Installs rake version 1.0 from a local gem on disk.
author: Johan Wiren author: Johan Wiren
''' '''
EXAMPLES = '''
# Installs version 1.0 of vagrant.
gem: name=vagrant version=1.0 state=present
# Installs latest available version of rake.
gem: name=rake state=latest
# Installs rake version 1.0 from a local gem on disk.
gem: name=rake gem_source=/path/to/gems/rake-1.0.gem state=present
'''
import re import re
def getinstalledversions(module, remote=False): def get_installed_versions(module, remote=False):
cmd = [ module.get_bin_path('gem', True) ] cmd = [ module.get_bin_path('gem', True) ]
cmd.append('query') cmd.append('query')
if remote: if remote:
cmd.append('--remote') cmd.append('--remote')
if module.params['http-proxy']:
cmd.extend([ '--http-proxy', module.params['http-proxy'] ])
if module.params['repository']: if module.params['repository']:
cmd.extend([ '--source', module.params['repository'] ]) cmd.extend([ '--source', module.params['repository'] ])
cmd.append('-n') cmd.append('-n')
cmd.append('^%s$' % module.params['name']) cmd.append('^%s$' % module.params['name'])
(rc, out, err) = module.run_command(cmd, check_rc=True) (rc, out, err) = module.run_command(cmd, check_rc=True)
installedversions = [] installed_versions = []
for line in out.splitlines(): for line in out.splitlines():
match = re.match(r"\S+\s+\((.+)\)", line) match = re.match(r"\S+\s+\((.+)\)", line)
if match: if match:
versions = match.group(1) versions = match.group(1)
for version in versions.split(', '): for version in versions.split(', '):
installedversions.append(version) installed_versions.append(version)
return installedversions return installed_versions
def exists(module): def exists(module):
if module.params['state'] == 'latest': if module.params['state'] == 'latest':
remoteversions = getinstalledversions(module, remote=True) remoteversions = get_installed_versions(module, remote=True)
if remoteversions: if remoteversions:
module.params['version'] = remoteversions[0] module.params['version'] = remoteversions[0]
installedversions = getinstalledversions(module) installed_versions = get_installed_versions(module)
if module.params['version']: if module.params['version']:
if module.params['version'] in installedversions: if module.params['version'] in installed_versions:
return True return True
else: else:
if installedversions: if installed_versions:
return True return True
return False return False
def uninstall(module): def uninstall(module):
if module.check_mode: if module.check_mode:
return return
cmd = [ module.get_bin_path('gem', True) ] cmd = [ module.get_bin_path('gem', True) ]
@ -111,6 +114,7 @@ def uninstall(module):
module.run_command(cmd, check_rc=True) module.run_command(cmd, check_rc=True)
def install(module): def install(module):
if module.check_mode: if module.check_mode:
return return
cmd = [ module.get_bin_path('gem', True) ] cmd = [ module.get_bin_path('gem', True) ]
@ -119,41 +123,39 @@ def install(module):
cmd.extend([ '--version', module.params['version'] ]) cmd.extend([ '--version', module.params['version'] ])
if module.params['repository']: if module.params['repository']:
cmd.extend([ '--source', module.params['repository'] ]) cmd.extend([ '--source', module.params['repository'] ])
if module.params['http-proxy']: if module.params['include_dependencies']:
cmd.extend([ '--http-proxy', module.params['http-proxy'] ])
if module.params['includedependencies']:
cmd.append('--include-dependencies') cmd.append('--include-dependencies')
cmd.append('--no-rdoc') cmd.append('--no-rdoc')
cmd.append('--no-ri') cmd.append('--no-ri')
cmd.append(module.params['gemsource']) cmd.append(module.params['gem_source'])
module.run_command(cmd, check_rc=True) module.run_command(cmd, check_rc=True)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = { argument_spec = dict(
'gemsource': {'required': False}, gem_source = dict(required=False, type='str'),
'http-proxy': {'required': False}, include_dependencies = dict(required=False, default=True, type='bool'),
'includedependencies': {'required': False, 'default':True, 'choices':BOOLEANS}, name = dict(required=True, type='str'),
'name': {'required': True}, repository = dict(required=False, aliases=['source'], type='str'),
'repository': {'required': False, 'aliases':['source']}, state = dict(required=False, choices=['present','absent','latest'], type='str'),
'state': {'required': True, 'choices':['present', 'absent', 'latest']}, version = dict(required=False, type='str'),
'version': {'required': False}, ),
},
supports_check_mode = True, supports_check_mode = True,
mutually_exclusive = [ [ 'gemsource', 'repository' ], [ 'gemsource', 'version' ] ], mutually_exclusive = [ ['gem_source','repository'], ['gem_source','version'] ],
) )
if module.params['version'] and module.params['state'] == 'latest': if module.params['version'] and module.params['state'] == 'latest':
module.fail_json(msg="Cannot specify version when state=latest") module.fail_json(msg="Cannot specify version when state=latest")
if module.params['gemsource'] and module.params['state'] == 'latest': if module.params['gem_source'] and module.params['state'] == 'latest':
module.fail_json(msg="Cannot maintain state=latest when installing from local source") module.fail_json(msg="Cannot maintain state=latest when installing from local source")
if module.params['gemsource'] is not 'null': if module.params['gem_source'] is not 'null':
module.params['gemsource'] = module.params['name'] module.params['gem_source'] = module.params['name']
changed = False changed = False
if module.params['state'] in [ 'present', 'latest' ]: if module.params['state'] in [ 'present', 'latest']:
if not exists(module): if not exists(module):
install(module) install(module)
changed = True changed = True