Update cpanm module to newer module patterns

This commit is contained in:
James Tanner 2014-03-03 13:37:32 -05:00
parent 8642544221
commit 5292c7050c

View file

@ -69,12 +69,12 @@ notes:
author: Franck Cuny author: Franck Cuny
''' '''
def _is_package_installed(name, locallib, cpanm): def _is_package_installed(module, name, locallib, cpanm):
cmd = "" cmd = ""
if locallib: if locallib:
cmd = "PERL5LIB={locallib}/lib/perl5".format(locallib=locallib) cmd = "PERL5LIB={locallib}/lib/perl5".format(locallib=locallib)
cmd = "{cmd} perl -M{name} -e '1'".format(cmd=cmd, name=name) cmd = "{cmd} perl -M{name} -e '1'".format(cmd=cmd, name=name)
res, stdout, stderr = _run(cmd) res, stdout, stderr = module.run_command(cmd, check_rc=False)
installed = True if res == 0 else False installed = True if res == 0 else False
return installed return installed
@ -97,18 +97,11 @@ def _build_cmd_line(name, from_path, notest, locallib, mirror, cpanm):
return cmd return cmd
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(): def main():
arg_spec = dict( arg_spec = dict(
name=dict(default=None, required=False, aliases=['pkg']), name=dict(default=None, required=False, aliases=['pkg']),
from_path=dict(default=None, required=False), from_path=dict(default=None, required=False),
notest=dict(default=False, choices=BOOLEANS), notest=dict(default=False, type='bool'),
locallib=dict(default=None, required=False), locallib=dict(default=None, required=False),
mirror=dict(default=None, required=False) mirror=dict(default=None, required=False)
) )
@ -124,16 +117,17 @@ def main():
from_path = module.params['from_path'] from_path = module.params['from_path']
notest = module.boolean(module.params.get('notest', False)) notest = module.boolean(module.params.get('notest', False))
locallib = module.params['locallib'] locallib = module.params['locallib']
mirror = module.params['mirror']
changed = False changed = False
installed = _is_package_installed(name, locallib, cpanm) installed = _is_package_installed(module, name, locallib, cpanm)
if not installed: if not installed:
out_cpanm = err_cpanm = '' out_cpanm = err_cpanm = ''
cmd = _build_cmd_line(name, from_path, notest, locallib, mirror, cpanm) cmd = _build_cmd_line(name, from_path, notest, locallib, mirror, cpanm)
rc_cpanm, out_cpanm, err_cpanm = _run(cmd) rc_cpanm, out_cpanm, err_cpanm = module.run_command(cmd, check_rc=False)
if rc_cpanm != 0: if rc_cpanm != 0:
module.fail_json(msg=err_cpanm, cmd=cmd) module.fail_json(msg=err_cpanm, cmd=cmd)
@ -144,6 +138,7 @@ def main():
module.exit_json(changed=changed, binary=cpanm, name=name) module.exit_json(changed=changed, binary=cpanm, name=name)
# include magic from lib/ansible/module_common.py # import module snippets
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> from ansible.module_utils.basic import *
main() main()