mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
cpanm: using CmdRunner (#5485)
* cpanm: using CmdRunner * add changelog fragment
This commit is contained in:
parent
8758f6a43f
commit
b696aa72b2
3 changed files with 32 additions and 29 deletions
2
changelogs/fragments/5485-cpanm-cmd-runner.yml
Normal file
2
changelogs/fragments/5485-cpanm-cmd-runner.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- cpanm - refactored module to use ``CmdRunner`` to execute ``cpanm`` (https://github.com/ansible-collections/community.general/pull/5485).
|
|
@ -134,12 +134,11 @@ EXAMPLES = '''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from ansible_collections.community.general.plugins.module_utils.module_helper import (
|
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
|
||||||
ModuleHelper, CmdMixin, ArgFormat
|
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class CPANMinus(CmdMixin, ModuleHelper):
|
class CPANMinus(ModuleHelper):
|
||||||
output_params = ['name', 'version']
|
output_params = ['name', 'version']
|
||||||
module = dict(
|
module = dict(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
|
@ -160,13 +159,13 @@ class CPANMinus(CmdMixin, ModuleHelper):
|
||||||
)
|
)
|
||||||
command = 'cpanm'
|
command = 'cpanm'
|
||||||
command_args_formats = dict(
|
command_args_formats = dict(
|
||||||
notest=dict(fmt="--notest", style=ArgFormat.BOOLEAN),
|
notest=cmd_runner_fmt.as_bool("--notest"),
|
||||||
locallib=dict(fmt=('--local-lib', '{0}'),),
|
locallib=cmd_runner_fmt.as_opt_val('--local-lib'),
|
||||||
mirror=dict(fmt=('--mirror', '{0}'),),
|
mirror=cmd_runner_fmt.as_opt_val('--mirror'),
|
||||||
mirror_only=dict(fmt="--mirror-only", style=ArgFormat.BOOLEAN),
|
mirror_only=cmd_runner_fmt.as_bool("--mirror-only"),
|
||||||
installdeps=dict(fmt="--installdeps", style=ArgFormat.BOOLEAN),
|
installdeps=cmd_runner_fmt.as_bool("--installdeps"),
|
||||||
|
pkg_spec=cmd_runner_fmt.as_list(),
|
||||||
)
|
)
|
||||||
check_rc = True
|
|
||||||
|
|
||||||
def __init_module__(self):
|
def __init_module__(self):
|
||||||
v = self.vars
|
v = self.vars
|
||||||
|
@ -181,15 +180,17 @@ class CPANMinus(CmdMixin, ModuleHelper):
|
||||||
self.vars.set("binary", self.command)
|
self.vars.set("binary", self.command)
|
||||||
|
|
||||||
def _is_package_installed(self, name, locallib, version):
|
def _is_package_installed(self, name, locallib, version):
|
||||||
|
def process(rc, out, err):
|
||||||
|
return rc == 0
|
||||||
|
|
||||||
if name is None or name.endswith('.tar.gz'):
|
if name is None or name.endswith('.tar.gz'):
|
||||||
return False
|
return False
|
||||||
version = "" if version is None else " " + version
|
version = "" if version is None else " " + version
|
||||||
|
|
||||||
env = {"PERL5LIB": "%s/lib/perl5" % locallib} if locallib else {}
|
env = {"PERL5LIB": "%s/lib/perl5" % locallib} if locallib else {}
|
||||||
cmd = ['perl', '-le', 'use %s%s;' % (name, version)]
|
runner = CmdRunner(self.module, ["perl", "-le"], {"mod": cmd_runner_fmt.as_list()}, check_rc=False, environ_update=env)
|
||||||
rc, out, err = self.module.run_command(cmd, check_rc=False, environ_update=env)
|
with runner("mod", output_process=process) as ctx:
|
||||||
|
return ctx.run(mod='use %s%s;' % (name, version))
|
||||||
return rc == 0
|
|
||||||
|
|
||||||
def sanitize_pkg_spec_version(self, pkg_spec, version):
|
def sanitize_pkg_spec_version(self, pkg_spec, version):
|
||||||
if version is None:
|
if version is None:
|
||||||
|
@ -207,6 +208,13 @@ class CPANMinus(CmdMixin, ModuleHelper):
|
||||||
return pkg_spec + version
|
return pkg_spec + version
|
||||||
|
|
||||||
def __run__(self):
|
def __run__(self):
|
||||||
|
def process(rc, out, err):
|
||||||
|
if self.vars.mode == "compatibility" and rc != 0:
|
||||||
|
self.do_raise(msg=err, cmd=self.vars.cmd_args)
|
||||||
|
return 'is up to date' not in err and 'is up to date' not in out
|
||||||
|
|
||||||
|
runner = CmdRunner(self.module, self.command, self.command_args_formats, check_rc=True)
|
||||||
|
|
||||||
v = self.vars
|
v = self.vars
|
||||||
pkg_param = 'from_path' if v.from_path else 'name'
|
pkg_param = 'from_path' if v.from_path else 'name'
|
||||||
|
|
||||||
|
@ -214,22 +222,14 @@ class CPANMinus(CmdMixin, ModuleHelper):
|
||||||
if self._is_package_installed(v.name, v.locallib, v.version):
|
if self._is_package_installed(v.name, v.locallib, v.version):
|
||||||
return
|
return
|
||||||
pkg_spec = v[pkg_param]
|
pkg_spec = v[pkg_param]
|
||||||
self.changed = self.run_command(
|
|
||||||
params=['notest', 'locallib', 'mirror', 'mirror_only', 'installdeps', {'name': pkg_spec}],
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
installed = self._is_package_installed(v.name_check, v.locallib, v.version) if v.name_check else False
|
installed = self._is_package_installed(v.name_check, v.locallib, v.version) if v.name_check else False
|
||||||
if installed:
|
if installed:
|
||||||
return
|
return
|
||||||
pkg_spec = self.sanitize_pkg_spec_version(v[pkg_param], v.version)
|
pkg_spec = self.sanitize_pkg_spec_version(v[pkg_param], v.version)
|
||||||
self.changed = self.run_command(
|
|
||||||
params=['notest', 'locallib', 'mirror', 'mirror_only', 'installdeps', {'name': pkg_spec}],
|
|
||||||
)
|
|
||||||
|
|
||||||
def process_command_output(self, rc, out, err):
|
with runner(['notest', 'locallib', 'mirror', 'mirror_only', 'installdeps', 'pkg_spec'], output_process=process) as ctx:
|
||||||
if self.vars.mode == "compatibility" and rc != 0:
|
self.changed = ctx.run(pkg_spec=pkg_spec)
|
||||||
self.do_raise(msg=err, cmd=self.vars.cmd_args)
|
|
||||||
return 'is up to date' not in err and 'is up to date' not in out
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# Author: Alexei Znamensky (russoz@gmail.com)
|
# Author: Alexei Znamensky (russoz@gmail.com)
|
||||||
# Largely adapted from test_redhat_subscription by
|
# Largely adapted from test_redhat_subscription by
|
||||||
# Jiri Hnidek (jhnidek@redhat.com)
|
# Jiri Hnidek (jhnidek@redhat.com)
|
||||||
|
@ -25,7 +26,7 @@ def patch_cpanm(mocker):
|
||||||
"""
|
"""
|
||||||
Function used for mocking some parts of redhat_subscription module
|
Function used for mocking some parts of redhat_subscription module
|
||||||
"""
|
"""
|
||||||
mocker.patch('ansible_collections.community.general.plugins.module_utils.module_helper.AnsibleModule.get_bin_path',
|
mocker.patch('ansible.module_utils.basic.AnsibleModule.get_bin_path',
|
||||||
return_value='/testbin/cpanm')
|
return_value='/testbin/cpanm')
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,8 +37,8 @@ TEST_CASES = [
|
||||||
'id': 'install_dancer_compatibility',
|
'id': 'install_dancer_compatibility',
|
||||||
'run_command.calls': [
|
'run_command.calls': [
|
||||||
(
|
(
|
||||||
['perl', '-le', 'use Dancer;'],
|
['/testbin/cpanm', '-le', 'use Dancer;'],
|
||||||
{'environ_update': {}, 'check_rc': False},
|
{'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||||
(2, '', 'error, not installed',), # output rc, out, err
|
(2, '', 'error, not installed',), # output rc, out, err
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
|
@ -55,8 +56,8 @@ TEST_CASES = [
|
||||||
'id': 'install_dancer_already_installed_compatibility',
|
'id': 'install_dancer_already_installed_compatibility',
|
||||||
'run_command.calls': [
|
'run_command.calls': [
|
||||||
(
|
(
|
||||||
['perl', '-le', 'use Dancer;'],
|
['/testbin/cpanm', '-le', 'use Dancer;'],
|
||||||
{'environ_update': {}, 'check_rc': False},
|
{'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
|
||||||
(0, '', '',), # output rc, out, err
|
(0, '', '',), # output rc, out, err
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in a new issue