Add the ability to specify an install_dir to the gem module (#38195)

* Add the ability to specify an install_dir to the gem module

* Add GEM_HOME when installing a non-global gem

* Add tests for custom gem path

* Fix sanity tests

* Add changelog entry

* Rebase and add tests for incorrect options

Co-authored by: Antoine Catton <devel@antoine.catton.fr>
This commit is contained in:
Antoine Catton 2018-05-21 15:55:43 +02:00 committed by Sam Doran
parent fc8663edc0
commit 39f9d3e4a6
4 changed files with 243 additions and 24 deletions

View file

@ -58,6 +58,13 @@ options:
- Override the path to the gem executable
required: false
version_added: "1.4"
install_dir:
description:
- Install the gems into a specific directory.
These gems will be independant from the global installed ones.
Specifying this requires user_install to be false.
required: false
version_added: "2.6"
env_shebang:
description:
- Rewrite the shebang line on installed scripts to use /usr/bin/env.
@ -133,6 +140,12 @@ def get_rubygems_version(module):
return tuple(int(x) for x in match.groups())
def get_rubygems_environ(module):
if module.params['install_dir']:
return {'GEM_HOME': module.params['install_dir']}
return None
def get_installed_versions(module, remote=False):
cmd = get_rubygems_path(module)
@ -143,7 +156,9 @@ def get_installed_versions(module, remote=False):
cmd.extend(['--source', module.params['repository']])
cmd.append('-n')
cmd.append('^%s$' % module.params['name'])
(rc, out, err) = module.run_command(cmd, check_rc=True)
environ = get_rubygems_environ(module)
(rc, out, err) = module.run_command(cmd, environ_update=environ, check_rc=True)
installed_versions = []
for line in out.splitlines():
match = re.match(r"\S+\s+\((.+)\)", line)
@ -155,7 +170,6 @@ def get_installed_versions(module, remote=False):
def exists(module):
if module.params['state'] == 'latest':
remoteversions = get_installed_versions(module, remote=True)
if remoteversions:
@ -175,14 +189,18 @@ def uninstall(module):
if module.check_mode:
return
cmd = get_rubygems_path(module)
environ = get_rubygems_environ(module)
cmd.append('uninstall')
if module.params['install_dir']:
cmd.extend(['--install-dir', module.params['install_dir']])
if module.params['version']:
cmd.extend(['--version', module.params['version']])
else:
cmd.append('--all')
cmd.append('--executable')
cmd.append(module.params['name'])
module.run_command(cmd, check_rc=True)
module.run_command(cmd, environ_update=environ, check_rc=True)
def install(module):
@ -211,6 +229,8 @@ def install(module):
cmd.append('--user-install')
else:
cmd.append('--no-user-install')
if module.params['install_dir']:
cmd.extend(['--install-dir', module.params['install_dir']])
if module.params['pre_release']:
cmd.append('--pre')
if not module.params['include_doc']:
@ -238,6 +258,7 @@ def main():
repository=dict(required=False, aliases=['source'], type='str'),
state=dict(required=False, default='present', choices=['present', 'absent', 'latest'], type='str'),
user_install=dict(required=False, default=True, type='bool'),
install_dir=dict(required=False, type='path'),
pre_release=dict(required=False, default=False, type='bool'),
include_doc=dict(required=False, default=False, type='bool'),
env_shebang=dict(required=False, default=False, type='bool'),
@ -252,6 +273,8 @@ def main():
module.fail_json(msg="Cannot specify version when 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")
if module.params['user_install'] and module.params['install_dir']:
module.fail_json(msg="install_dir requires user_install=false")
if not module.params['gem_source']:
module.params['gem_source'] = module.params['name']