2014-03-04 21:38:29 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2014, Dimitrios Tydeas Mengidis <tydeas.dr@gmail.com>
|
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: composer
|
2015-09-11 21:26:23 +02:00
|
|
|
author:
|
|
|
|
- "Dimitrios Tydeas Mengidis (@dmtrs)"
|
|
|
|
- "René Moser (@resmo)"
|
2014-03-04 21:38:29 +01:00
|
|
|
short_description: Dependency Manager for PHP
|
2014-03-16 21:11:45 +01:00
|
|
|
version_added: "1.6"
|
2014-03-04 21:38:29 +01:00
|
|
|
description:
|
|
|
|
- Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you
|
|
|
|
options:
|
2014-05-21 18:12:53 +02:00
|
|
|
command:
|
2014-08-08 20:46:49 +02:00
|
|
|
version_added: "1.8"
|
2014-05-21 18:12:53 +02:00
|
|
|
description:
|
2014-05-21 18:14:21 +02:00
|
|
|
- Composer command like "install", "update" and so on
|
2014-05-21 18:12:53 +02:00
|
|
|
required: false
|
|
|
|
default: install
|
2015-10-21 15:20:53 +02:00
|
|
|
arguments:
|
2015-10-23 10:00:52 +02:00
|
|
|
version_added: "2.0"
|
2015-10-21 15:20:53 +02:00
|
|
|
description:
|
|
|
|
- Composer arguments like required package, version and so on
|
|
|
|
required: false
|
|
|
|
default: null
|
2014-03-04 21:38:29 +01:00
|
|
|
working_dir:
|
|
|
|
description:
|
|
|
|
- Directory of your project ( see --working-dir )
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: [ "working-dir" ]
|
|
|
|
prefer_source:
|
|
|
|
description:
|
|
|
|
- Forces installation from package sources when possible ( see --prefer-source )
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
aliases: [ "prefer-source" ]
|
|
|
|
prefer_dist:
|
|
|
|
description:
|
2014-12-11 20:30:00 +01:00
|
|
|
- Forces installation from package dist even for dev versions ( see --prefer-dist )
|
2014-03-04 21:38:29 +01:00
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
aliases: [ "prefer-dist" ]
|
|
|
|
no_dev:
|
|
|
|
description:
|
|
|
|
- Disables installation of require-dev packages ( see --no-dev )
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
aliases: [ "no-dev" ]
|
|
|
|
no_scripts:
|
|
|
|
description:
|
|
|
|
- Skips the execution of all scripts defined in composer.json ( see --no-scripts )
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
aliases: [ "no-scripts" ]
|
|
|
|
no_plugins:
|
|
|
|
description:
|
|
|
|
- Disables all plugins ( see --no-plugins )
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
aliases: [ "no-plugins" ]
|
|
|
|
optimize_autoloader:
|
|
|
|
description:
|
|
|
|
- Optimize autoloader during autoloader dump ( see --optimize-autoloader ). Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but can take a bit of time to run so it is currently not done by default.
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
aliases: [ "optimize-autoloader" ]
|
2015-06-03 00:57:15 +02:00
|
|
|
ignore_platform_reqs:
|
|
|
|
version_added: "2.0"
|
|
|
|
description:
|
|
|
|
- Ignore php, hhvm, lib-* and ext-* requirements and force the installation even if the local machine does not fulfill these.
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
aliases: [ "ignore-platform-reqs" ]
|
2014-03-04 21:38:29 +01:00
|
|
|
requirements:
|
|
|
|
- php
|
|
|
|
- composer installed in bin path (recommended /usr/local/bin)
|
|
|
|
notes:
|
2015-09-11 21:26:23 +02:00
|
|
|
- Default options that are always appended in each execution are --no-ansi, --no-interaction and --no-progress if available.
|
2014-03-04 21:38:29 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Downloads and installs all the libs and dependencies outlined in the /path/to/project/composer.lock
|
2014-05-21 18:12:53 +02:00
|
|
|
- composer: command=install working_dir=/path/to/project
|
2015-10-21 15:20:53 +02:00
|
|
|
|
|
|
|
- composer:
|
2015-10-30 11:24:42 +01:00
|
|
|
command: "require"
|
|
|
|
arguments: "my/package"
|
2015-10-21 15:20:53 +02:00
|
|
|
working_dir: "/path/to/project"
|
|
|
|
|
|
|
|
# Clone project and install with all dependencies
|
|
|
|
- composer:
|
|
|
|
command: "create-project"
|
|
|
|
arguments: "package/package /path/to/project ~1.0"
|
|
|
|
working_dir: "/path/to/project"
|
|
|
|
prefer_dist: "yes"
|
2014-03-04 21:38:29 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2015-09-11 21:26:23 +02:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
2014-03-04 21:38:29 +01:00
|
|
|
def parse_out(string):
|
|
|
|
return re.sub("\s+", " ", string).strip()
|
|
|
|
|
|
|
|
def has_changed(string):
|
2015-09-11 21:20:20 +02:00
|
|
|
return "Nothing to install or update" not in string
|
2014-03-04 21:38:29 +01:00
|
|
|
|
2015-09-11 21:26:23 +02:00
|
|
|
def get_available_options(module, command='install'):
|
|
|
|
# get all availabe options from a composer command using composer help to json
|
|
|
|
rc, out, err = composer_command(module, "help %s --format=json" % command)
|
|
|
|
if rc != 0:
|
|
|
|
output = parse_out(err)
|
|
|
|
module.fail_json(msg=output)
|
|
|
|
|
|
|
|
command_help_json = json.loads(out)
|
|
|
|
return command_help_json['definition']['options']
|
|
|
|
|
2015-10-21 15:20:53 +02:00
|
|
|
def composer_command(module, command, arguments = "", options=[]):
|
2014-03-04 21:38:29 +01:00
|
|
|
php_path = module.get_bin_path("php", True, ["/usr/local/bin"])
|
|
|
|
composer_path = module.get_bin_path("composer", True, ["/usr/local/bin"])
|
2015-10-21 15:20:53 +02:00
|
|
|
cmd = "%s %s %s %s %s" % (php_path, composer_path, command, " ".join(options), arguments)
|
2014-03-04 21:38:29 +01:00
|
|
|
return module.run_command(cmd)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2015-06-03 00:57:15 +02:00
|
|
|
command = dict(default="install", type="str", required=False),
|
2015-10-21 15:20:53 +02:00
|
|
|
arguments = dict(default="", type="str", required=False),
|
2015-06-03 00:57:15 +02:00
|
|
|
working_dir = dict(aliases=["working-dir"], required=True),
|
|
|
|
prefer_source = dict(default="no", type="bool", aliases=["prefer-source"]),
|
|
|
|
prefer_dist = dict(default="no", type="bool", aliases=["prefer-dist"]),
|
|
|
|
no_dev = dict(default="yes", type="bool", aliases=["no-dev"]),
|
|
|
|
no_scripts = dict(default="no", type="bool", aliases=["no-scripts"]),
|
|
|
|
no_plugins = dict(default="no", type="bool", aliases=["no-plugins"]),
|
|
|
|
optimize_autoloader = dict(default="yes", type="bool", aliases=["optimize-autoloader"]),
|
|
|
|
ignore_platform_reqs = dict(default="no", type="bool", aliases=["ignore-platform-reqs"]),
|
2014-03-04 21:38:29 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2015-09-11 21:26:23 +02:00
|
|
|
# Get composer command with fallback to default
|
|
|
|
command = module.params['command']
|
2015-10-29 22:21:46 +01:00
|
|
|
if re.search(r"\s", command):
|
|
|
|
module.fail_json(msg="Use the 'arguments' param for passing arguments with the 'command'")
|
2015-10-30 11:24:42 +01:00
|
|
|
|
2015-10-21 15:20:53 +02:00
|
|
|
arguments = module.params['arguments']
|
2015-09-11 21:26:23 +02:00
|
|
|
available_options = get_available_options(module=module, command=command)
|
|
|
|
|
2014-08-18 09:28:32 +02:00
|
|
|
options = []
|
2014-03-04 21:38:29 +01:00
|
|
|
|
|
|
|
# Default options
|
2015-09-11 21:26:23 +02:00
|
|
|
default_options = [
|
|
|
|
'no-ansi',
|
|
|
|
'no-interaction',
|
|
|
|
'no-progress',
|
|
|
|
]
|
2014-03-04 21:38:29 +01:00
|
|
|
|
2015-09-11 21:26:23 +02:00
|
|
|
for option in default_options:
|
|
|
|
if option in available_options:
|
|
|
|
option = "--%s" % option
|
|
|
|
options.append(option)
|
2014-03-04 21:38:29 +01:00
|
|
|
|
2015-09-11 21:26:23 +02:00
|
|
|
options.extend(['--working-dir', os.path.abspath(module.params['working_dir'])])
|
2014-05-21 18:01:33 +02:00
|
|
|
|
2015-09-11 21:26:23 +02:00
|
|
|
option_params = {
|
|
|
|
'prefer_source': 'prefer-source',
|
|
|
|
'prefer_dist': 'prefer-dist',
|
|
|
|
'no_dev': 'no-dev',
|
|
|
|
'no_scripts': 'no-scripts',
|
|
|
|
'no_plugins': 'no_plugins',
|
|
|
|
'optimize_autoloader': 'optimize-autoloader',
|
|
|
|
'ignore_platform_reqs': 'ignore-platform-reqs',
|
|
|
|
}
|
|
|
|
|
|
|
|
for param, option in option_params.iteritems():
|
|
|
|
if module.params.get(param) and option in available_options:
|
|
|
|
option = "--%s" % option
|
|
|
|
options.append(option)
|
2014-08-18 09:28:32 +02:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
options.append('--dry-run')
|
2014-03-04 21:38:29 +01:00
|
|
|
|
2015-10-21 15:20:53 +02:00
|
|
|
rc, out, err = composer_command(module, command, arguments, options)
|
2014-03-04 21:38:29 +01:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
output = parse_out(err)
|
|
|
|
module.fail_json(msg=output)
|
|
|
|
else:
|
2015-03-03 20:47:13 +01:00
|
|
|
# Composer version > 1.0.0-alpha9 now use stderr for standard notification messages
|
|
|
|
output = parse_out(out + err)
|
2014-03-04 21:38:29 +01:00
|
|
|
module.exit_json(changed=has_changed(output), msg=output)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2015-09-11 21:26:23 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|