From ce35d8809474d2a1dee1f2cefd5416c491b97594 Mon Sep 17 00:00:00 2001 From: Stanislav German-Evtushenko Date: Mon, 21 Jun 2021 16:53:03 +0900 Subject: [PATCH] gem_module: Add bindir option (#2837) * gem_module: Add bindir option This option allows to specify directory to install executables, e.g. `/home/user/bin` or `/home/user/.local/bin`. This comes especially handy when used with user_install option as the default path of executables is not in PATH. * Update changelogs/fragments/gem_module_add_bindir_option.yml Co-authored-by: Ajpantuso * gem_module: Integration tests for bindir option * gem_module: Update Integration tests for bindir option * gem_module: Update Integration tests for bindir option Make sure gist is not installed system-wide prior the tests * Revert "gem_module: Update Integration tests for bindir option" This reverts commit 04eec6db27aa90d2b23ead7941aeb5889a7c6437. * Do not check "install_gem_result is changed" for ansible develop on openSUSE * Revert "Do not check "install_gem_result is changed" for ansible develop on openSUSE" This reverts commit 48ecb27889a6d86b91eb70a5b1432a5649846b99. * gem_module: Use --norc to avoid surprises Run install and uninstall actions with `--norc`. This way ansible has more control over the way gems are installed. * Revert "gem_module: Use --norc to avoid surprises" This reverts commit 66f40bcfe684ba306759a0fdc028a21ba73ba1dd. * gem_module: bindir - Ignore openSUSE Leap * Update plugins/modules/packaging/language/gem.py Co-authored-by: Felix Fontein * gem_module: Use --norc to avoid surprises Run install and uninstall actions with `--norc` when supported (rubygems >= 2.5.2). This way ansible has more control over the way gems are installed. * Try distutils.version instead of packaging * ver is an list, not string * ver is not list either but tuple * Update changelogs/fragments/gem_module_add_bindir_option.yml Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * ver can be None (when can this happen?) * gem: Add norc option * Apply suggestions from code review Co-authored-by: Felix Fontein * Update plugins/modules/packaging/language/gem.py Co-authored-by: Felix Fontein * Use tuples to compare versions * Apply suggestions from code review Co-authored-by: Amin Vakil * Update plugins/modules/packaging/language/gem.py Co-authored-by: Amin Vakil * lost norc option check is back * Move handling norc option to separate function * cosmetic * fix for the previos commit * Apply suggestions from code review Co-authored-by: Felix Fontein * Cache result of get_rubygems_version Co-authored-by: Ajpantuso Co-authored-by: Felix Fontein Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> Co-authored-by: Amin Vakil --- .../gem_module_add_bindir_option.yml | 3 ++ plugins/modules/packaging/language/gem.py | 47 ++++++++++++++++--- tests/integration/targets/gem/tasks/main.yml | 41 ++++++++++++++++ 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/gem_module_add_bindir_option.yml diff --git a/changelogs/fragments/gem_module_add_bindir_option.yml b/changelogs/fragments/gem_module_add_bindir_option.yml new file mode 100644 index 0000000000..f47b6deb27 --- /dev/null +++ b/changelogs/fragments/gem_module_add_bindir_option.yml @@ -0,0 +1,3 @@ +minor_changes: + - gem - add ``bindir`` option to specify an installation path for executables such as ``/home/user/bin`` or ``/home/user/.local/bin`` (https://github.com/ansible-collections/community.general/pull/2837). + - gem - add ``norc`` option to avoid loading any ``.gemrc`` file (https://github.com/ansible-collections/community.general/pull/2837). diff --git a/plugins/modules/packaging/language/gem.py b/plugins/modules/packaging/language/gem.py index 516c9b0a41..c7ccdec498 100644 --- a/plugins/modules/packaging/language/gem.py +++ b/plugins/modules/packaging/language/gem.py @@ -62,6 +62,19 @@ options: These gems will be independent from the global installed ones. Specifying this requires user_install to be false. required: false + bindir: + type: path + description: + - Install executables into a specific directory. + version_added: 3.3.0 + norc: + type: bool + default: false + description: + - Avoid loading any C(.gemrc) file. Ignored for RubyGems prior to 2.5.2. + - "The current default value will be deprecated in community.general 4.0.0: if the value is not explicitly specified, a deprecation message will be shown." + - From community.general 5.0.0 on, the default will be changed to C(true). + version_added: 3.3.0 env_shebang: description: - Rewrite the shebang line on installed scripts to use /usr/bin/env. @@ -134,6 +147,9 @@ def get_rubygems_path(module): def get_rubygems_version(module): + if hasattr(get_rubygems_version, "ver"): + return get_rubygems_version.ver + cmd = get_rubygems_path(module) + ['--version'] (rc, out, err) = module.run_command(cmd, check_rc=True) @@ -141,7 +157,10 @@ def get_rubygems_version(module): if not match: return None - return tuple(int(x) for x in match.groups()) + ver = tuple(int(x) for x in match.groups()) + get_rubygems_version.ver = ver + + return ver def get_rubygems_environ(module): @@ -154,6 +173,7 @@ def get_installed_versions(module, remote=False): cmd = get_rubygems_path(module) cmd.append('query') + cmd.extend(common_opts(module)) if remote: cmd.append('--remote') if module.params['repository']: @@ -188,6 +208,14 @@ def exists(module): return False +def common_opts(module): + opts = [] + ver = get_rubygems_version(module) + if module.params['norc'] and ver and ver >= (2, 5, 2): + opts.append('--norc') + return opts + + def uninstall(module): if module.check_mode: @@ -195,9 +223,13 @@ def uninstall(module): cmd = get_rubygems_path(module) environ = get_rubygems_environ(module) cmd.append('uninstall') + cmd.extend(common_opts(module)) if module.params['install_dir']: cmd.extend(['--install-dir', module.params['install_dir']]) + if module.params['bindir']: + cmd.extend(['--bindir', module.params['bindir']]) + if module.params['version']: cmd.extend(['--version', module.params['version']]) else: @@ -213,13 +245,10 @@ def install(module): return ver = get_rubygems_version(module) - if ver: - major = ver[0] - else: - major = None cmd = get_rubygems_path(module) cmd.append('install') + cmd.extend(common_opts(module)) if module.params['version']: cmd.extend(['--version', module.params['version']]) if module.params['repository']: @@ -227,7 +256,7 @@ def install(module): if not module.params['include_dependencies']: cmd.append('--ignore-dependencies') else: - if major and major < 2: + if ver and ver < (2, 0, 0): cmd.append('--include-dependencies') if module.params['user_install']: cmd.append('--user-install') @@ -235,10 +264,12 @@ def install(module): cmd.append('--no-user-install') if module.params['install_dir']: cmd.extend(['--install-dir', module.params['install_dir']]) + if module.params['bindir']: + cmd.extend(['--bindir', module.params['bindir']]) if module.params['pre_release']: cmd.append('--pre') if not module.params['include_doc']: - if major and major < 2: + if ver and ver < (2, 0, 0): cmd.append('--no-rdoc') cmd.append('--no-ri') else: @@ -265,6 +296,8 @@ def main(): 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'), + bindir=dict(type='path'), + norc=dict(default=False, type='bool'), 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'), diff --git a/tests/integration/targets/gem/tasks/main.yml b/tests/integration/targets/gem/tasks/main.yml index ce64364d78..499057775c 100644 --- a/tests/integration/targets/gem/tasks/main.yml +++ b/tests/integration/targets/gem/tasks/main.yml @@ -178,3 +178,44 @@ that: - install_gem_result is changed - gem_search.files | length == 0 + +# Custom directory for executables (--bindir) +- name: Install gem with custom bindir + gem: + name: gist + state: present + bindir: "{{ output_dir }}/custom_bindir" + norc: yes + user_install: no # Avoid conflicts between --install-dir and --user-install when running as root on CentOS / Fedora / RHEL + register: install_gem_result + +- name: Get stats of gem executable + stat: + path: "{{ output_dir }}/custom_bindir/gist" + register: gem_bindir_stat + +- name: Ensure gem executable was installed in custom directory + assert: + that: + - install_gem_result is changed + - gem_bindir_stat.stat.exists and gem_bindir_stat.stat.isreg + +- name: Remove gem with custom bindir + gem: + name: gist + state: absent + bindir: "{{ output_dir }}/custom_bindir" + norc: yes + user_install: no # Avoid conflicts between --install-dir and --user-install when running as root on CentOS / Fedora / RHEL + register: install_gem_result + +- name: Get stats of gem executable + stat: + path: "{{ output_dir }}/custom_bindir/gist" + register: gem_bindir_stat + +- name: Ensure gem executable was removed from custom directory + assert: + that: + - install_gem_result is changed + - not gem_bindir_stat.stat.exists