3fb21a5281
This updates apt, apt_repository, command, cron, easy_install, facter, fireball, git, group, mount, ohai, pip, service, setup, subversion, supervisorctl, svr4pkg, user, and yum to take advantage of run_command in module_common.py.
205 lines
6.7 KiB
Python
205 lines
6.7 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2012, Matt Wright <matt@nobien.net>
|
|
#
|
|
# 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: pip
|
|
short_description: Manages Python library dependencies.
|
|
description:
|
|
- Manage Python library dependencies.
|
|
version_added: "0.7"
|
|
options:
|
|
name:
|
|
description:
|
|
- The name of a Python library to install
|
|
required: true
|
|
default: null
|
|
version:
|
|
description:
|
|
- The version number to install of the Python library specified in the I(name) parameter
|
|
required: false
|
|
default: null
|
|
requirements:
|
|
description:
|
|
- The path to a pip requirements file
|
|
required: false
|
|
default: null
|
|
virtualenv:
|
|
description:
|
|
- An optional path to a I(virtualenv) directory to install into
|
|
required: false
|
|
default: null
|
|
use_mirrors:
|
|
description:
|
|
- Whether to use mirrors when installing python libraries. If using
|
|
an older version of pip (< 1.0), you should set this to no because
|
|
older versions of pip do not support I(--use-mirrors).
|
|
required: false
|
|
default: yes
|
|
choices: [ "yes", "no" ]
|
|
version_added: "1.0"
|
|
state:
|
|
description:
|
|
- The state of module
|
|
required: false
|
|
default: present
|
|
choices: [ "present", "absent", "latest" ]
|
|
extra_args:
|
|
description:
|
|
- Extra arguments passed to pip.
|
|
required: false
|
|
default: null
|
|
version_added: "1.0"
|
|
examples:
|
|
- code: "pip: name=flask"
|
|
description: Install I(flask) python package.
|
|
- code: "pip: name=flask version=0.8"
|
|
description: Install I(flask) python package on version 0.8.
|
|
- code: "pip: name=flask virtualenv=/my_app/venv"
|
|
description: "Install I(Flask) (U(http://flask.pocoo.org/)) into the specified I(virtualenv)"
|
|
- code: "pip: requirements=/my_app/requirements.txt"
|
|
description: Install specified python requirements.
|
|
- code: "pip: requirements=/my_app/requirements.txt virtualenv=/my_app/venv"
|
|
description: Install specified python requirements in indicated I(virtualenv).
|
|
- code: "pip: requirements=/my_app/requirements.txt extra_args='-i https://example.com/pypi/simple'"
|
|
description: Install specified python requirements and custom Index URL.
|
|
notes:
|
|
- Please note that U(http://www.virtualenv.org/, virtualenv) must be installed on the remote host if the virtualenv parameter is specified.
|
|
requirements: [ "virtualenv", "pip" ]
|
|
author: Matt Wright
|
|
'''
|
|
|
|
|
|
def _get_full_name(name, version=None):
|
|
if version is None:
|
|
resp = name
|
|
else:
|
|
resp = name + '==' + version
|
|
return resp
|
|
|
|
|
|
def _get_pip(module, env):
|
|
# On Debian and Ubuntu, pip is pip.
|
|
# On Fedora18 and up, pip is python-pip.
|
|
# On Fedora17 and below, CentOS and RedHat 6 and 5, pip is pip-python.
|
|
# On Fedora, CentOS, and RedHat, the exception is in the virtualenv.
|
|
# There, pip is just pip.
|
|
# Try pip with the virtualenv directory first.
|
|
pip = module.get_bin_path('pip', False, ['%s/bin' % env])
|
|
for p in ['python-pip', 'pip-python']:
|
|
if not pip:
|
|
pip = module.get_bin_path(p, False, ['%s/bin' % env])
|
|
# pip should have been found by now. The final call to get_bin_path
|
|
# will trigger fail_json.
|
|
if not pip:
|
|
pip = module.get_bin_path('pip', True, ['%s/bin' % env])
|
|
return pip
|
|
|
|
|
|
def _fail(module, cmd, out, err):
|
|
msg = ''
|
|
if out:
|
|
msg += "stdout: %s" % (out, )
|
|
if err:
|
|
msg += "\n:stderr: %s" % (err, )
|
|
module.fail_json(cmd=cmd, msg=msg)
|
|
|
|
|
|
def main():
|
|
state_map = dict(
|
|
present='install',
|
|
absent='uninstall -y',
|
|
latest='install -U',
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
argument_spec=dict(
|
|
state=dict(default='present', choices=state_map.keys()),
|
|
name=dict(default=None, required=False),
|
|
version=dict(default=None, required=False),
|
|
requirements=dict(default=None, required=False),
|
|
virtualenv=dict(default=None, required=False),
|
|
use_mirrors=dict(default='yes', choices=BOOLEANS),
|
|
extra_args=dict(default=None, required=False),
|
|
),
|
|
required_one_of=[['name', 'requirements']],
|
|
mutually_exclusive=[['name', 'requirements']],
|
|
)
|
|
|
|
state = module.params['state']
|
|
name = module.params['name']
|
|
version = module.params['version']
|
|
requirements = module.params['requirements']
|
|
use_mirrors = module.boolean(module.params['use_mirrors'])
|
|
extra_args = module.params['extra_args']
|
|
|
|
if state == 'latest' and version is not None:
|
|
module.fail_json(msg='version is incompatible with state=latest')
|
|
if name and '=' in name:
|
|
module.fail_json(msg='version must be specified in the version parameter')
|
|
|
|
err = ''
|
|
out = ''
|
|
|
|
env = module.params['virtualenv']
|
|
|
|
if env:
|
|
virtualenv = module.get_bin_path('virtualenv', True)
|
|
if not os.path.exists(os.path.join(env, 'bin', 'activate')):
|
|
cmd = '%s %s' % (virtualenv, env)
|
|
rc, out_venv, err_venv = module.run_command(cmd)
|
|
out += out_venv
|
|
err += err_venv
|
|
if rc != 0:
|
|
_fail(module, cmd, out, err)
|
|
|
|
pip = _get_pip(module, env)
|
|
|
|
cmd = '%s %s' % (pip, state_map[state])
|
|
if state != 'absent' and use_mirrors:
|
|
cmd += ' --use-mirrors'
|
|
if extra_args:
|
|
cmd += ' %s' % extra_args
|
|
if name:
|
|
cmd += ' %s' % _get_full_name(name, version)
|
|
elif requirements:
|
|
cmd += ' -r %s' % requirements
|
|
|
|
rc, out_pip, err_pip = module.run_command(cmd)
|
|
out += out_pip
|
|
err += err_pip
|
|
if rc == 1 and state == 'absent' and 'not installed' in out_pip:
|
|
pass # rc is 1 when attempting to uninstall non-installed package
|
|
elif rc != 0:
|
|
_fail(module, cmd, out, err)
|
|
|
|
if state == 'absent':
|
|
changed = 'Successfully uninstalled' in out_pip
|
|
else:
|
|
changed = 'Successfully installed' in out_pip
|
|
|
|
module.exit_json(changed=changed, cmd=cmd, name=name, version=version,
|
|
state=state, requirements=requirements, virtualenv=env)
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|