| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2014, Dimitrios Tydeas Mengidis <[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: composer |
| 11 |
author: |
| 12 |
- "Dimitrios Tydeas Mengidis (@dmtrs)" |
| 13 |
- "René Moser (@resmo)" |
| 14 |
short_description: Dependency Manager for PHP |
| 15 |
description: |
| 16 |
- Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs |
| 17 |
and it installs them in your project for you. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
command: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- Composer command like V(install), V(update) and so on. |
| 30 |
default: install |
| 31 |
arguments: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- Composer arguments like required package, version and so on. |
| 35 |
default: '' |
| 36 |
executable: |
| 37 |
type: path |
| 38 |
description: |
| 39 |
- Path to PHP executable on the remote host, if PHP is not in E(PATH). |
| 40 |
aliases: [php_path] |
| 41 |
working_dir: |
| 42 |
type: path |
| 43 |
description: |
| 44 |
- Directory of your project (see C(--working-dir)). This is required when the command is not run globally. |
| 45 |
- This is ignored if O(global_command=true). |
| 46 |
global_command: |
| 47 |
description: |
| 48 |
- Runs the specified command globally. |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
prefer_source: |
| 52 |
description: |
| 53 |
- Forces installation from package sources when possible (see C(--prefer-source)). |
| 54 |
default: false |
| 55 |
type: bool |
| 56 |
prefer_dist: |
| 57 |
description: |
| 58 |
- Forces installation from package dist even for dev versions (see C(--prefer-dist)). |
| 59 |
default: false |
| 60 |
type: bool |
| 61 |
no_dev: |
| 62 |
description: |
| 63 |
- Disables installation of require-dev packages (see C(--no-dev)). |
| 64 |
default: true |
| 65 |
type: bool |
| 66 |
no_scripts: |
| 67 |
description: |
| 68 |
- Skips the execution of all scripts defined in composer.json (see C(--no-scripts)). |
| 69 |
default: false |
| 70 |
type: bool |
| 71 |
no_plugins: |
| 72 |
description: |
| 73 |
- Disables all plugins (see C(--no-plugins)). |
| 74 |
default: false |
| 75 |
type: bool |
| 76 |
optimize_autoloader: |
| 77 |
description: |
| 78 |
- Optimize autoloader during autoloader dump (see C(--optimize-autoloader)). |
| 79 |
- Convert PSR-0/4 autoloading to classmap to get a faster autoloader. |
| 80 |
- Recommended especially for production, but can take a bit of time to run. |
| 81 |
default: true |
| 82 |
type: bool |
| 83 |
classmap_authoritative: |
| 84 |
description: |
| 85 |
- Autoload classes from classmap only. |
| 86 |
- Implicitly enable optimize_autoloader. |
| 87 |
- Recommended especially for production, but can take a bit of time to run. |
| 88 |
default: false |
| 89 |
type: bool |
| 90 |
apcu_autoloader: |
| 91 |
description: |
| 92 |
- Uses APCu to cache found/not-found classes. |
| 93 |
default: false |
| 94 |
type: bool |
| 95 |
ignore_platform_reqs: |
| 96 |
description: |
| 97 |
- Ignore C(php), C(hhvm), C(lib-*) and C(ext-*) requirements and force the installation even if the local machine does not fulfill |
| 98 |
these. |
| 99 |
default: false |
| 100 |
type: bool |
| 101 |
force: |
| 102 |
description: |
| 103 |
- When O(command) is V(create-project), the module checks whether a V(composer.json) already |
| 104 |
exists in O(working_dir) and skips the command if it does, making the task idempotent. |
| 105 |
- Set to V(true) to always run the command regardless. |
| 106 |
default: false |
| 107 |
type: bool |
| 108 |
version_added: 12.6.0 |
| 109 |
composer_executable: |
| 110 |
type: path |
| 111 |
description: |
| 112 |
- Path to composer executable on the remote host, if composer is not in E(PATH) or a custom composer is needed. |
| 113 |
version_added: 3.2.0 |
| 114 |
requirements: |
| 115 |
- php |
| 116 |
- composer installed in bin path (recommended C(/usr/local/bin)) or specified in O(composer_executable) |
| 117 |
notes: |
| 118 |
- Default options that are always appended in each execution are C(--no-ansi), C(--no-interaction) and C(--no-progress) |
| 119 |
if available. |
| 120 |
- We received reports about issues on macOS if composer was installed by Homebrew. Please use the official install method |
| 121 |
to avoid issues. |
| 122 |
""" |
| 123 |
|
| 124 |
EXAMPLES = r""" |
| 125 |
- name: Download and installs all libs and dependencies outlined in the /path/to/project/composer.lock |
| 126 |
community.general.composer: |
| 127 |
command: install |
| 128 |
working_dir: /path/to/project |
| 129 |
|
| 130 |
- name: Install a new package |
| 131 |
community.general.composer: |
| 132 |
command: require |
| 133 |
arguments: my/package |
| 134 |
working_dir: /path/to/project |
| 135 |
|
| 136 |
- name: Clone and install a project with all dependencies |
| 137 |
community.general.composer: |
| 138 |
command: create-project |
| 139 |
arguments: package/package /path/to/project ~1.0 |
| 140 |
working_dir: /path/to/project |
| 141 |
prefer_dist: true |
| 142 |
|
| 143 |
- name: Install a package globally |
| 144 |
community.general.composer: |
| 145 |
command: require |
| 146 |
global_command: true |
| 147 |
arguments: my/package |
| 148 |
""" |
| 149 |
|
| 150 |
import os |
| 151 |
import re |
| 152 |
import shlex |
| 153 |
|
| 154 |
from ansible.module_utils.basic import AnsibleModule |
| 155 |
|
| 156 |
|
| 157 |
def parse_out(string): |
| 158 |
return re.sub(r"\s+", " ", string).strip() |
| 159 |
|
| 160 |
|
| 161 |
def has_changed(string): |
| 162 |
return all( |
| 163 |
no_change not in string |
| 164 |
for no_change in ["Nothing to install or update", "Nothing to install, update or remove"] |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
def get_available_options(module, command="install"): |
| 169 |
# get all available options from a composer command using composer help to json |
| 170 |
rc, out, err = composer_command(module, ["help", command], arguments=["--no-interaction", "--format=json"]) |
| 171 |
if rc != 0: |
| 172 |
output = parse_out(err) |
| 173 |
module.fail_json(msg=output) |
| 174 |
|
| 175 |
command_help_json = module.from_json(out) |
| 176 |
return command_help_json["definition"]["options"] |
| 177 |
|
| 178 |
|
| 179 |
def composer_command(module, command, arguments=None, options=None): |
| 180 |
if options is None: |
| 181 |
options = [] |
| 182 |
if arguments is None: |
| 183 |
arguments = [] |
| 184 |
|
| 185 |
global_command = module.params["global_command"] |
| 186 |
|
| 187 |
if global_command: |
| 188 |
global_arg = ["global"] |
| 189 |
else: |
| 190 |
global_arg = [] |
| 191 |
options.extend(["--working-dir", module.params["working_dir"]]) |
| 192 |
|
| 193 |
if module.params["executable"] is None: |
| 194 |
php_path = module.get_bin_path("php", True, ["/usr/local/bin"]) |
| 195 |
else: |
| 196 |
php_path = module.params["executable"] |
| 197 |
|
| 198 |
if module.params["composer_executable"] is None: |
| 199 |
composer_path = module.get_bin_path("composer", True, ["/usr/local/bin"]) |
| 200 |
else: |
| 201 |
composer_path = module.params["composer_executable"] |
| 202 |
|
| 203 |
cmd = [php_path, composer_path] + global_arg + command + options + arguments |
| 204 |
return module.run_command(cmd) |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
module = AnsibleModule( |
| 209 |
argument_spec=dict( |
| 210 |
command=dict(default="install", type="str"), |
| 211 |
arguments=dict(default="", type="str"), |
| 212 |
executable=dict(type="path", aliases=["php_path"]), |
| 213 |
working_dir=dict(type="path"), |
| 214 |
global_command=dict(default=False, type="bool"), |
| 215 |
prefer_source=dict(default=False, type="bool"), |
| 216 |
prefer_dist=dict(default=False, type="bool"), |
| 217 |
no_dev=dict(default=True, type="bool"), |
| 218 |
no_scripts=dict(default=False, type="bool"), |
| 219 |
no_plugins=dict(default=False, type="bool"), |
| 220 |
apcu_autoloader=dict(default=False, type="bool"), |
| 221 |
optimize_autoloader=dict(default=True, type="bool"), |
| 222 |
classmap_authoritative=dict(default=False, type="bool"), |
| 223 |
ignore_platform_reqs=dict(default=False, type="bool"), |
| 224 |
force=dict(default=False, type="bool"), |
| 225 |
composer_executable=dict(type="path"), |
| 226 |
), |
| 227 |
required_if=[("global_command", False, ["working_dir"])], |
| 228 |
supports_check_mode=True, |
| 229 |
) |
| 230 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 231 |
|
| 232 |
# Get composer command with fallback to default |
| 233 |
command = module.params["command"] |
| 234 |
if re.search(r"\s", command): |
| 235 |
module.fail_json(msg="Use the 'arguments' param for passing arguments with the 'command'") |
| 236 |
|
| 237 |
arguments = shlex.split(module.params["arguments"]) |
| 238 |
available_options = get_available_options(module=module, command=command) |
| 239 |
|
| 240 |
options = [] |
| 241 |
|
| 242 |
# Default options |
| 243 |
default_options = [ |
| 244 |
"no-ansi", |
| 245 |
"no-interaction", |
| 246 |
"no-progress", |
| 247 |
] |
| 248 |
|
| 249 |
for option in default_options: |
| 250 |
if option in available_options: |
| 251 |
option = f"--{option}" |
| 252 |
options.append(option) |
| 253 |
|
| 254 |
option_params = { |
| 255 |
"prefer_source": "prefer-source", |
| 256 |
"prefer_dist": "prefer-dist", |
| 257 |
"no_dev": "no-dev", |
| 258 |
"no_scripts": "no-scripts", |
| 259 |
"no_plugins": "no-plugins", |
| 260 |
"apcu_autoloader": "acpu-autoloader", |
| 261 |
"optimize_autoloader": "optimize-autoloader", |
| 262 |
"classmap_authoritative": "classmap-authoritative", |
| 263 |
"ignore_platform_reqs": "ignore-platform-reqs", |
| 264 |
} |
| 265 |
|
| 266 |
for param, option in option_params.items(): |
| 267 |
if module.params.get(param) and option in available_options: |
| 268 |
option = f"--{option}" |
| 269 |
options.append(option) |
| 270 |
|
| 271 |
working_dir = module.params["working_dir"] |
| 272 |
|
| 273 |
if command == "create-project" and not module.params["force"]: |
| 274 |
if working_dir and os.path.exists(os.path.join(working_dir, "composer.json")): |
| 275 |
module.exit_json(changed=False, msg="composer.json already exists in working_dir, skipping create-project") |
| 276 |
|
| 277 |
if module.check_mode: |
| 278 |
if "dry-run" in available_options: |
| 279 |
options.append("--dry-run") |
| 280 |
else: |
| 281 |
module.exit_json(skipped=True, msg=f"command '{command}' does not support check mode, skipping") |
| 282 |
|
| 283 |
rc, out, err = composer_command(module, [command], arguments, options) |
| 284 |
|
| 285 |
if rc != 0: |
| 286 |
output = parse_out(err) |
| 287 |
module.fail_json(msg=output, stdout=err) |
| 288 |
else: |
| 289 |
# Composer version > 1.0.0-alpha9 now use stderr for standard notification messages |
| 290 |
output = parse_out(out + err) |
| 291 |
module.exit_json(changed=has_changed(output), msg=output, stdout=out + err) |
| 292 |
|
| 293 |
|
| 294 |
if __name__ == "__main__": |
| 295 |
main() |