2012-11-27 23:49:09 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Franck Cuny <franck@lumberjaph.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: cpanm
|
|
|
|
short_description: Manages Perl library dependencies.
|
|
|
|
description:
|
|
|
|
- Manage Perl library dependencies.
|
2014-03-20 13:10:02 +01:00
|
|
|
version_added: "1.6"
|
2012-11-27 23:49:09 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2014-10-17 02:44:07 +02:00
|
|
|
- The name of the Perl library to install. You may use the "full distribution path", e.g. MIYAGAWA/Plack-0.99_05.tar.gz
|
2012-11-27 23:49:09 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2012-11-29 01:04:10 +01:00
|
|
|
aliases: ["pkg"]
|
2012-11-27 23:49:09 +01:00
|
|
|
from_path:
|
|
|
|
description:
|
|
|
|
- The local directory from where to install
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
notest:
|
|
|
|
description:
|
|
|
|
- Do not run unit tests
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
locallib:
|
|
|
|
description:
|
|
|
|
- Specify the install base to install modules
|
|
|
|
required: false
|
|
|
|
default: false
|
2012-11-28 18:47:20 +01:00
|
|
|
mirror:
|
|
|
|
description:
|
|
|
|
- Specifies the base URL for the CPAN mirror to use
|
|
|
|
required: false
|
|
|
|
default: false
|
2014-10-13 15:05:39 +02:00
|
|
|
mirror_only:
|
|
|
|
description:
|
|
|
|
- Use the mirror's index file instead of the CPAN Meta DB
|
|
|
|
required: false
|
|
|
|
default: false
|
2015-08-20 09:57:46 +02:00
|
|
|
installdeps:
|
|
|
|
description:
|
|
|
|
- Only install dependencies
|
|
|
|
required: false
|
|
|
|
default: false
|
2015-08-25 22:18:52 +02:00
|
|
|
version_added: "2.0"
|
2015-11-12 21:38:15 +01:00
|
|
|
version:
|
|
|
|
description:
|
|
|
|
- minimum version of perl module to consider acceptable
|
|
|
|
required: false
|
|
|
|
default: false
|
2015-12-15 03:56:50 +01:00
|
|
|
version_added: "2.1"
|
2015-10-02 01:59:51 +02:00
|
|
|
system_lib:
|
2015-09-23 00:53:58 +02:00
|
|
|
description:
|
2015-10-02 01:59:51 +02:00
|
|
|
- Use this if you want to install modules to the system perl include path. You must be root or have "passwordless" sudo for this to work.
|
|
|
|
- This uses the cpanm commandline option '--sudo', which has nothing to do with ansible privilege escalation.
|
2015-09-23 00:53:58 +02:00
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
version_added: "2.0"
|
2015-10-02 01:59:51 +02:00
|
|
|
aliases: ['use_sudo']
|
2012-11-27 23:49:09 +01:00
|
|
|
notes:
|
|
|
|
- Please note that U(http://search.cpan.org/dist/App-cpanminus/bin/cpanm, cpanm) must be installed on the remote host.
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Franck Cuny (@franckcuny)"
|
2012-11-27 23:49:09 +01:00
|
|
|
'''
|
|
|
|
|
2015-07-25 13:58:45 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# install Dancer perl package
|
|
|
|
- cpanm: name=Dancer
|
|
|
|
|
|
|
|
# install version 0.99_05 of the Plack perl package
|
|
|
|
- cpanm: name=MIYAGAWA/Plack-0.99_05.tar.gz
|
|
|
|
|
|
|
|
# install Dancer into the specified locallib
|
|
|
|
- cpanm: name=Dancer locallib=/srv/webapps/my_app/extlib
|
|
|
|
|
|
|
|
# install perl dependencies from local directory
|
|
|
|
- cpanm: from_path=/srv/webapps/my_app/src/
|
|
|
|
|
|
|
|
# install Dancer perl package without running the unit tests in indicated locallib
|
|
|
|
- cpanm: name=Dancer notest=True locallib=/srv/webapps/my_app/extlib
|
|
|
|
|
|
|
|
# install Dancer perl package from a specific mirror
|
|
|
|
- cpanm: name=Dancer mirror=http://cpan.cpantesters.org/
|
2015-09-23 00:53:58 +02:00
|
|
|
|
2015-09-23 15:40:36 +02:00
|
|
|
# install Dancer perl package into the system root path
|
2015-10-02 01:59:51 +02:00
|
|
|
- cpanm: name=Dancer system_lib=yes
|
2015-11-12 21:38:15 +01:00
|
|
|
|
|
|
|
# install Dancer if it's not already installed
|
|
|
|
# OR the installed version is older than version 1.0
|
|
|
|
- cpanm: name=Dancer version=1.0
|
2015-07-25 13:58:45 +02:00
|
|
|
'''
|
|
|
|
|
2015-11-12 21:38:15 +01:00
|
|
|
def _is_package_installed(module, name, locallib, cpanm, version):
|
2012-11-28 18:47:20 +01:00
|
|
|
cmd = ""
|
|
|
|
if locallib:
|
2014-03-12 21:44:03 +01:00
|
|
|
os.environ["PERL5LIB"] = "%s/lib/perl5" % locallib
|
2015-11-12 21:38:15 +01:00
|
|
|
cmd = "%s perl -e ' use %s" % (cmd, name)
|
|
|
|
if version:
|
|
|
|
cmd = "%s %s;'" % (cmd, version)
|
|
|
|
else:
|
|
|
|
cmd = "%s;'" % cmd
|
2014-03-03 19:37:32 +01:00
|
|
|
res, stdout, stderr = module.run_command(cmd, check_rc=False)
|
2014-03-12 22:25:32 +01:00
|
|
|
if res == 0:
|
2014-03-12 21:44:03 +01:00
|
|
|
return True
|
2015-08-20 09:57:46 +02:00
|
|
|
else:
|
2014-03-12 21:44:03 +01:00
|
|
|
return False
|
2012-11-28 18:47:20 +01:00
|
|
|
|
2015-10-25 22:02:25 +01:00
|
|
|
def _build_cmd_line(name, from_path, notest, locallib, mirror, mirror_only, installdeps, cpanm, use_sudo):
|
2014-03-12 21:44:03 +01:00
|
|
|
# this code should use "%s" like everything else and just return early but not fixing all of it now.
|
|
|
|
# don't copy stuff like this
|
2012-11-28 18:47:20 +01:00
|
|
|
if from_path:
|
|
|
|
cmd = "{cpanm} {path}".format(cpanm=cpanm, path=from_path)
|
|
|
|
else:
|
|
|
|
cmd = "{cpanm} {name}".format(cpanm=cpanm, name=name)
|
|
|
|
|
|
|
|
if notest is True:
|
|
|
|
cmd = "{cmd} -n".format(cmd=cmd)
|
|
|
|
|
|
|
|
if locallib is not None:
|
|
|
|
cmd = "{cmd} -l {locallib}".format(cmd=cmd, locallib=locallib)
|
|
|
|
|
|
|
|
if mirror is not None:
|
|
|
|
cmd = "{cmd} --mirror {mirror}".format(cmd=cmd, mirror=mirror)
|
|
|
|
|
2014-10-13 15:05:39 +02:00
|
|
|
if mirror_only is True:
|
|
|
|
cmd = "{cmd} --mirror-only".format(cmd=cmd)
|
|
|
|
|
2015-08-20 09:57:46 +02:00
|
|
|
if installdeps is True:
|
|
|
|
cmd = "{cmd} --installdeps".format(cmd=cmd)
|
|
|
|
|
2015-09-23 00:53:58 +02:00
|
|
|
if use_sudo is True:
|
|
|
|
cmd = "{cmd} --sudo".format(cmd=cmd)
|
|
|
|
|
2012-11-28 18:47:20 +01:00
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2012-11-27 23:49:09 +01:00
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
2012-11-29 01:04:10 +01:00
|
|
|
name=dict(default=None, required=False, aliases=['pkg']),
|
2012-11-27 23:49:09 +01:00
|
|
|
from_path=dict(default=None, required=False),
|
2014-03-03 19:37:32 +01:00
|
|
|
notest=dict(default=False, type='bool'),
|
2012-11-27 23:49:09 +01:00
|
|
|
locallib=dict(default=None, required=False),
|
2015-02-12 23:02:06 +01:00
|
|
|
mirror=dict(default=None, required=False),
|
2014-10-13 15:05:39 +02:00
|
|
|
mirror_only=dict(default=False, type='bool'),
|
2015-08-20 09:57:46 +02:00
|
|
|
installdeps=dict(default=False, type='bool'),
|
2015-10-02 01:59:51 +02:00
|
|
|
system_lib=dict(default=False, type='bool', aliases=['use_sudo']),
|
2015-11-12 21:38:15 +01:00
|
|
|
version=dict(default=None, required=False),
|
2012-11-27 23:49:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=arg_spec,
|
|
|
|
required_one_of=[['name', 'from_path']],
|
|
|
|
)
|
|
|
|
|
2014-10-13 15:05:39 +02:00
|
|
|
cpanm = module.get_bin_path('cpanm', True)
|
|
|
|
name = module.params['name']
|
|
|
|
from_path = module.params['from_path']
|
|
|
|
notest = module.boolean(module.params.get('notest', False))
|
|
|
|
locallib = module.params['locallib']
|
|
|
|
mirror = module.params['mirror']
|
|
|
|
mirror_only = module.params['mirror_only']
|
2015-08-20 09:57:46 +02:00
|
|
|
installdeps = module.params['installdeps']
|
2015-10-02 01:59:51 +02:00
|
|
|
use_sudo = module.params['system_lib']
|
2015-11-12 21:38:15 +01:00
|
|
|
version = module.params['version']
|
2012-11-27 23:49:09 +01:00
|
|
|
|
2014-03-12 21:44:03 +01:00
|
|
|
changed = False
|
2012-11-27 23:49:09 +01:00
|
|
|
|
2015-11-12 21:38:15 +01:00
|
|
|
installed = _is_package_installed(module, name, locallib, cpanm, version)
|
2012-11-27 23:49:09 +01:00
|
|
|
|
2012-11-28 18:47:20 +01:00
|
|
|
if not installed:
|
|
|
|
out_cpanm = err_cpanm = ''
|
2015-10-25 22:02:25 +01:00
|
|
|
cmd = _build_cmd_line(name, from_path, notest, locallib, mirror, mirror_only, installdeps, cpanm, use_sudo)
|
2012-11-27 23:49:09 +01:00
|
|
|
|
2014-03-03 19:37:32 +01:00
|
|
|
rc_cpanm, out_cpanm, err_cpanm = module.run_command(cmd, check_rc=False)
|
2012-11-27 23:49:09 +01:00
|
|
|
|
2012-11-28 18:47:20 +01:00
|
|
|
if rc_cpanm != 0:
|
|
|
|
module.fail_json(msg=err_cpanm, cmd=cmd)
|
2012-11-27 23:49:09 +01:00
|
|
|
|
2012-11-28 18:47:20 +01:00
|
|
|
if err_cpanm and 'is up to date' not in err_cpanm:
|
|
|
|
changed = True
|
2012-11-27 23:49:09 +01:00
|
|
|
|
|
|
|
module.exit_json(changed=changed, binary=cpanm, name=name)
|
|
|
|
|
2014-03-03 19:37:32 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
2012-11-27 23:49:09 +01:00
|
|
|
main()
|