2012-11-27 22:49:09 +00: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.
|
2012-11-28 17:47:20 +00:00
|
|
|
version_added: "1.0"
|
2012-11-27 22:49:09 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the Perl library to install
|
|
|
|
required: false
|
|
|
|
default: null
|
2012-11-29 00:04:10 +00:00
|
|
|
aliases: ["pkg"]
|
2012-11-27 22:49:09 +00: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 17:47:20 +00:00
|
|
|
mirror:
|
|
|
|
description:
|
|
|
|
- Specifies the base URL for the CPAN mirror to use
|
|
|
|
required: false
|
|
|
|
default: false
|
2012-11-27 22:49:09 +00:00
|
|
|
examples:
|
|
|
|
- code: "cpanm: name=Dancer"
|
|
|
|
description: Install I(Dancer) perl package.
|
|
|
|
- code: "cpanm: name=Dancer locallib=/srv/webapps/my_app/extlib"
|
|
|
|
description: "Install I(Dancer) (U(http://perldancer.org/)) into the specified I(locallib)"
|
|
|
|
- code: "cpanm: from_path=/srv/webapps/my_app/src/"
|
|
|
|
description: Install perl dependencies from local directory.
|
|
|
|
- code: "cpanm: name=Dancer notest=True locallib=/srv/webapps/my_app/extlib"
|
|
|
|
description: Install I(Dancer) perl package without running the unit tests in indicated I(locallib).
|
2012-11-28 17:47:20 +00:00
|
|
|
- code: "cpanm: name=Dancer mirror=http://cpan.cpantesters.org/"
|
|
|
|
description: Install I(Dancer) perl package from a specific mirror
|
2012-11-27 22:49:09 +00:00
|
|
|
notes:
|
|
|
|
- Please note that U(http://search.cpan.org/dist/App-cpanminus/bin/cpanm, cpanm) must be installed on the remote host.
|
|
|
|
author: Franck Cuny
|
|
|
|
'''
|
|
|
|
|
2014-03-03 18:37:32 +00:00
|
|
|
def _is_package_installed(module, name, locallib, cpanm):
|
2012-11-28 17:47:20 +00:00
|
|
|
cmd = ""
|
|
|
|
if locallib:
|
|
|
|
cmd = "PERL5LIB={locallib}/lib/perl5".format(locallib=locallib)
|
|
|
|
cmd = "{cmd} perl -M{name} -e '1'".format(cmd=cmd, name=name)
|
2014-03-03 18:37:32 +00:00
|
|
|
res, stdout, stderr = module.run_command(cmd, check_rc=False)
|
2012-11-28 17:47:20 +00:00
|
|
|
installed = True if res == 0 else False
|
|
|
|
return installed
|
|
|
|
|
|
|
|
|
|
|
|
def _build_cmd_line(name, from_path, notest, locallib, mirror, cpanm):
|
|
|
|
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)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2012-11-27 22:49:09 +00:00
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
2012-11-29 00:04:10 +00:00
|
|
|
name=dict(default=None, required=False, aliases=['pkg']),
|
2012-11-27 22:49:09 +00:00
|
|
|
from_path=dict(default=None, required=False),
|
2014-03-03 18:37:32 +00:00
|
|
|
notest=dict(default=False, type='bool'),
|
2012-11-27 22:49:09 +00:00
|
|
|
locallib=dict(default=None, required=False),
|
2012-11-28 17:47:20 +00:00
|
|
|
mirror=dict(default=None, required=False)
|
2012-11-27 22:49:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=arg_spec,
|
|
|
|
required_one_of=[['name', 'from_path']],
|
|
|
|
)
|
|
|
|
|
|
|
|
cpanm = module.get_bin_path('cpanm', True)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
from_path = module.params['from_path']
|
2012-11-28 17:47:20 +00:00
|
|
|
notest = module.boolean(module.params.get('notest', False))
|
2012-11-27 22:49:09 +00:00
|
|
|
locallib = module.params['locallib']
|
2014-03-03 18:37:32 +00:00
|
|
|
mirror = module.params['mirror']
|
2012-11-27 22:49:09 +00:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
2014-03-03 18:37:32 +00:00
|
|
|
installed = _is_package_installed(module, name, locallib, cpanm)
|
2012-11-27 22:49:09 +00:00
|
|
|
|
2012-11-28 17:47:20 +00:00
|
|
|
if not installed:
|
|
|
|
out_cpanm = err_cpanm = ''
|
|
|
|
cmd = _build_cmd_line(name, from_path, notest, locallib, mirror, cpanm)
|
2012-11-27 22:49:09 +00:00
|
|
|
|
2014-03-03 18:37:32 +00:00
|
|
|
rc_cpanm, out_cpanm, err_cpanm = module.run_command(cmd, check_rc=False)
|
2012-11-27 22:49:09 +00:00
|
|
|
|
2012-11-28 17:47:20 +00:00
|
|
|
if rc_cpanm != 0:
|
|
|
|
module.fail_json(msg=err_cpanm, cmd=cmd)
|
2012-11-27 22:49:09 +00:00
|
|
|
|
2012-11-28 17:47:20 +00:00
|
|
|
if err_cpanm and 'is up to date' not in err_cpanm:
|
|
|
|
changed = True
|
2012-11-27 22:49:09 +00:00
|
|
|
|
|
|
|
module.exit_json(changed=changed, binary=cpanm, name=name)
|
|
|
|
|
|
|
|
|
2014-03-03 18:37:32 +00:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
2012-11-27 22:49:09 +00:00
|
|
|
main()
|