| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2015, Tim Hoiberg <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: bundler |
| 11 |
short_description: Manage Ruby Gem dependencies with Bundler |
| 12 |
description: |
| 13 |
- Manage installation and Gem version dependencies for Ruby using the Bundler gem. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
executable: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- The path to the bundler executable. |
| 26 |
state: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- The desired state of the Gem bundle. V(latest) updates gems to the most recent, acceptable version. |
| 30 |
choices: [present, latest] |
| 31 |
default: present |
| 32 |
chdir: |
| 33 |
type: path |
| 34 |
description: |
| 35 |
- The directory to execute the bundler commands from. This directory needs to contain a valid Gemfile or .bundle/ directory. |
| 36 |
- If not specified, it defaults to the temporary working directory. |
| 37 |
exclude_groups: |
| 38 |
type: list |
| 39 |
elements: str |
| 40 |
description: |
| 41 |
- A list of Gemfile groups to exclude during operations. This only applies when O(state=present). Bundler considers |
| 42 |
this a 'remembered' property for the Gemfile and automatically excludes groups in future operations even if O(exclude_groups) |
| 43 |
is not set. |
| 44 |
clean: |
| 45 |
description: |
| 46 |
- Only applies if O(state=present). If set removes any gems on the target host that are not in the gemfile. |
| 47 |
type: bool |
| 48 |
default: false |
| 49 |
gemfile: |
| 50 |
type: path |
| 51 |
description: |
| 52 |
- Only applies if O(state=present). The path to the gemfile to use to install gems. |
| 53 |
- If not specified it defaults to the Gemfile in current directory. |
| 54 |
local: |
| 55 |
description: |
| 56 |
- If set only installs gems from the cache on the target host. |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
deployment_mode: |
| 60 |
description: |
| 61 |
- Only applies if O(state=present). If set it installs gems in C(./vendor/bundle) instead of the default location. Requires |
| 62 |
a C(Gemfile.lock) file to have been created prior. |
| 63 |
type: bool |
| 64 |
default: false |
| 65 |
user_install: |
| 66 |
description: |
| 67 |
- Only applies if O(state=present). Installs gems in the local user's cache or for all users. |
| 68 |
type: bool |
| 69 |
default: true |
| 70 |
gem_path: |
| 71 |
type: path |
| 72 |
description: |
| 73 |
- Only applies if O(state=present). Specifies the directory to install the gems into. If O(chdir) is set then this path |
| 74 |
is relative to O(chdir). |
| 75 |
- If not specified the default RubyGems gem paths are used. |
| 76 |
binstub_directory: |
| 77 |
type: path |
| 78 |
description: |
| 79 |
- Only applies if O(state=present). Specifies the directory to install any gem bins files to. When executed the bin |
| 80 |
files run within the context of the Gemfile and fail if any required gem dependencies are not installed. If O(chdir) |
| 81 |
is set then this path is relative to O(chdir). |
| 82 |
extra_args: |
| 83 |
type: str |
| 84 |
description: |
| 85 |
- A space separated string of additional commands that can be applied to the Bundler command. Refer to the Bundler documentation |
| 86 |
for more information. |
| 87 |
author: "Tim Hoiberg (@thoiberg)" |
| 88 |
""" |
| 89 |
|
| 90 |
EXAMPLES = r""" |
| 91 |
- name: Install gems from a Gemfile in the current directory |
| 92 |
community.general.bundler: |
| 93 |
state: present |
| 94 |
executable: ~/.rvm/gems/2.1.5/bin/bundle |
| 95 |
|
| 96 |
- name: Exclude the production group from installing |
| 97 |
community.general.bundler: |
| 98 |
state: present |
| 99 |
exclude_groups: production |
| 100 |
|
| 101 |
- name: Install gems into ./vendor/bundle |
| 102 |
community.general.bundler: |
| 103 |
state: present |
| 104 |
deployment_mode: true |
| 105 |
|
| 106 |
- name: Install gems using a Gemfile in another directory |
| 107 |
community.general.bundler: |
| 108 |
state: present |
| 109 |
gemfile: ../rails_project/Gemfile |
| 110 |
|
| 111 |
- name: Update Gemfile in another directory |
| 112 |
community.general.bundler: |
| 113 |
state: latest |
| 114 |
chdir: ~/rails_project |
| 115 |
""" |
| 116 |
|
| 117 |
from ansible.module_utils.basic import AnsibleModule |
| 118 |
|
| 119 |
|
| 120 |
def get_bundler_executable(module): |
| 121 |
if module.params.get("executable"): |
| 122 |
result = module.params.get("executable").split(" ") |
| 123 |
else: |
| 124 |
result = [module.get_bin_path("bundle", True)] |
| 125 |
return result |
| 126 |
|
| 127 |
|
| 128 |
def main(): |
| 129 |
module = AnsibleModule( |
| 130 |
argument_spec=dict( |
| 131 |
executable=dict(), |
| 132 |
state=dict(default="present", choices=["present", "latest"]), |
| 133 |
chdir=dict(type="path"), |
| 134 |
exclude_groups=dict(type="list", elements="str"), |
| 135 |
clean=dict(default=False, type="bool"), |
| 136 |
gemfile=dict(type="path"), |
| 137 |
local=dict(default=False, type="bool"), |
| 138 |
deployment_mode=dict(default=False, type="bool"), |
| 139 |
user_install=dict(default=True, type="bool"), |
| 140 |
gem_path=dict(type="path"), |
| 141 |
binstub_directory=dict(type="path"), |
| 142 |
extra_args=dict(), |
| 143 |
), |
| 144 |
supports_check_mode=True, |
| 145 |
) |
| 146 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 147 |
|
| 148 |
state = module.params.get("state") |
| 149 |
chdir = module.params.get("chdir") |
| 150 |
exclude_groups = module.params.get("exclude_groups") |
| 151 |
clean = module.params.get("clean") |
| 152 |
gemfile = module.params.get("gemfile") |
| 153 |
local = module.params.get("local") |
| 154 |
deployment_mode = module.params.get("deployment_mode") |
| 155 |
user_install = module.params.get("user_install") |
| 156 |
gem_path = module.params.get("gem_path") |
| 157 |
binstub_directory = module.params.get("binstub_directory") |
| 158 |
extra_args = module.params.get("extra_args") |
| 159 |
|
| 160 |
cmd = get_bundler_executable(module) |
| 161 |
|
| 162 |
if module.check_mode: |
| 163 |
cmd.append("check") |
| 164 |
rc, out, err = module.run_command(cmd, cwd=chdir, check_rc=False) |
| 165 |
|
| 166 |
module.exit_json(changed=rc != 0, state=state, stdout=out, stderr=err) |
| 167 |
|
| 168 |
bundle_env = {} |
| 169 |
if state == "present": |
| 170 |
cmd.append("install") |
| 171 |
if exclude_groups: |
| 172 |
bundle_env["BUNDLE_WITHOUT"] = ":".join(exclude_groups) |
| 173 |
if clean: |
| 174 |
bundle_env["BUNDLE_CLEAN"] = "true" |
| 175 |
if gemfile: |
| 176 |
cmd.extend(["--gemfile", gemfile]) |
| 177 |
if local: |
| 178 |
cmd.append("--local") |
| 179 |
if deployment_mode: |
| 180 |
bundle_env["BUNDLE_DEPLOYMENT"] = "true" |
| 181 |
if not user_install: |
| 182 |
cmd.append("--system") |
| 183 |
if gem_path: |
| 184 |
bundle_env["BUNDLE_PATH"] = gem_path |
| 185 |
if binstub_directory: |
| 186 |
bundle_env["BUNDLE_BIN"] = binstub_directory |
| 187 |
else: |
| 188 |
cmd.append("update") |
| 189 |
if local: |
| 190 |
cmd.append("--local") |
| 191 |
|
| 192 |
if extra_args: |
| 193 |
cmd.extend(extra_args.split(" ")) |
| 194 |
|
| 195 |
rc, out, err = module.run_command(cmd, cwd=chdir, check_rc=True, environ_update=bundle_env) |
| 196 |
|
| 197 |
module.exit_json(changed="Installing" in out, state=state, stdout=out, stderr=err) |
| 198 |
|
| 199 |
|
| 200 |
if __name__ == "__main__": |
| 201 |
main() |