From 22766906b036cc211447cc4d4c4d0ad64283edc4 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Tue, 27 Nov 2012 14:49:09 -0800 Subject: [PATCH] Simple module to install Perl libraries via cpanm. cpanm is a popular alternative to the CPAN client for installing Perl libraries. Unfortunately it can't uninstall already installed module, so it can't handle states. --- library/cpanm | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 library/cpanm diff --git a/library/cpanm b/library/cpanm new file mode 100644 index 00000000000..3316ac09e9e --- /dev/null +++ b/library/cpanm @@ -0,0 +1,118 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2012, Franck Cuny +# +# 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 . +# + +DOCUMENTATION = ''' +--- +module: cpanm +short_description: Manages Perl library dependencies. +description: + - Manage Perl library dependencies. +options: + name: + description: + - The name of the Perl library to install + required: false + default: null + 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 +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). +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 +''' + +def _run(cmd): + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, shell=True) + stdout, stderr = process.communicate() + return (process.returncode, stdout, stderr) + + +def main(): + arg_spec = dict( + name=dict(default=None, required=False), + from_path=dict(default=None, required=False), + notest=dict(default=False, required=False), + locallib=dict(default=None, required=False), + ) + + 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'] + notest = module.params['notest'] + locallib = module.params['locallib'] + + changed = False + out_cpanm = '' + err_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) + + rc_cpanm, out_cpanm, err_cpanm = _run(cmd) + + if rc_cpanm != 0: + module.fail_json(msg=err_cpanm, cmd=cmd) + + if err_cpanm and 'is up to date' not in err_cpanm: + changed = True + + module.exit_json(changed=changed, binary=cpanm, name=name) + + +# include magic from lib/ansible/module_common.py +#<> +main()