2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2012, Franck Cuny <franck@lumberjaph.net>
|
|
|
|
# Copyright (c) 2021, Alexei Znamensky <russoz@gmail.com>
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: cpanm
|
2022-11-09 07:18:40 +01:00
|
|
|
short_description: Manages Perl library dependencies
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Manage Perl library dependencies using cpanminus.
|
2023-02-24 09:25:20 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
2020-11-13 13:41:11 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-06-15 15:46:33 +02:00
|
|
|
- The Perl library to install. Valid values change according to the O(mode), see notes for more details.
|
|
|
|
- Note that for installing from a local path the parameter O(from_path) should be used.
|
2021-04-18 10:55:47 +02:00
|
|
|
aliases: [pkg]
|
2020-03-09 10:11:07 +01:00
|
|
|
from_path:
|
2020-11-13 13:41:11 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- The local directory or C(tar.gz) file to install from.
|
2020-03-09 10:11:07 +01:00
|
|
|
notest:
|
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Do not run unit tests.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 19:59:13 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
locallib:
|
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Specify the install base to install modules.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: path
|
|
|
|
mirror:
|
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Specifies the base URL for the CPAN mirror to use.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
mirror_only:
|
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Use the mirror's index file instead of the CPAN Meta DB.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 19:59:13 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
installdeps:
|
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Only install dependencies.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 19:59:13 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
version:
|
|
|
|
description:
|
2023-06-15 15:46:33 +02:00
|
|
|
- Version specification for the perl module. When O(mode) is V(new), C(cpanm) version operators are accepted.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
executable:
|
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Override the path to the cpanm executable.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: path
|
2021-04-18 10:55:47 +02:00
|
|
|
mode:
|
|
|
|
description:
|
|
|
|
- Controls the module behavior. See notes below for more details.
|
2023-06-15 15:46:33 +02:00
|
|
|
- Default is V(compatibility) but that behavior is deprecated and will be changed to V(new) in community.general 9.0.0.
|
2021-04-18 10:55:47 +02:00
|
|
|
type: str
|
|
|
|
choices: [compatibility, new]
|
|
|
|
version_added: 3.0.0
|
|
|
|
name_check:
|
|
|
|
description:
|
2023-06-15 15:46:33 +02:00
|
|
|
- When O(mode=new), this parameter can be used to check if there is a module O(name) installed (at O(version), when specified).
|
2021-04-18 10:55:47 +02:00
|
|
|
type: str
|
|
|
|
version_added: 3.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Please note that U(http://search.cpan.org/dist/App-cpanminus/bin/cpanm, cpanm) must be installed on the remote host.
|
2023-06-15 15:46:33 +02:00
|
|
|
- "This module now comes with a choice of execution O(mode): V(compatibility) or V(new)."
|
|
|
|
- "O(mode=compatibility): When using V(compatibility) mode, the module will keep backward compatibility. This is the default mode.
|
|
|
|
O(name) must be either a module name or a distribution file. If the perl module given by O(name) is installed (at the exact O(version)
|
|
|
|
when specified), then nothing happens. Otherwise, it will be installed using the C(cpanm) executable. O(name) cannot be an URL, or a git URL.
|
|
|
|
C(cpanm) version specifiers do not work in this mode."
|
|
|
|
- "O(mode=new): When using V(new) mode, the module will behave differently. The O(name) parameter may refer to a module name, a distribution file,
|
|
|
|
a HTTP URL or a git repository URL as described in C(cpanminus) documentation. C(cpanm) version specifiers are recognized."
|
2021-04-18 10:55:47 +02:00
|
|
|
author:
|
|
|
|
- "Franck Cuny (@fcuny)"
|
|
|
|
- "Alexei Znamensky (@russoz)"
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install Dancer perl package
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: Dancer
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install version 0.99_05 of the Plack perl package
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: MIYAGAWA/Plack-0.99_05.tar.gz
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install Dancer into the specified locallib
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: Dancer
|
|
|
|
locallib: /srv/webapps/my_app/extlib
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install perl dependencies from local directory
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
from_path: /srv/webapps/my_app/src/
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install Dancer perl package without running the unit tests in indicated locallib
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: Dancer
|
2022-09-06 20:42:17 +02:00
|
|
|
notest: true
|
2020-03-09 10:11:07 +01:00
|
|
|
locallib: /srv/webapps/my_app/extlib
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install Dancer perl package from a specific mirror
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: Dancer
|
|
|
|
mirror: 'http://cpan.cpantesters.org/'
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install Dancer perl package into the system root path
|
2022-08-24 19:59:13 +02:00
|
|
|
become: true
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: Dancer
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Install Dancer if it is not already installed OR the installed version is older than version 1.0
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.cpanm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: Dancer
|
|
|
|
version: '1.0'
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2022-11-07 20:15:00 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
|
2021-04-18 10:55:47 +02:00
|
|
|
|
|
|
|
|
2022-11-07 20:15:00 +01:00
|
|
|
class CPANMinus(ModuleHelper):
|
2021-04-18 10:55:47 +02:00
|
|
|
output_params = ['name', 'version']
|
|
|
|
module = dict(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(type='str', aliases=['pkg']),
|
|
|
|
version=dict(type='str'),
|
|
|
|
from_path=dict(type='path'),
|
|
|
|
notest=dict(type='bool', default=False),
|
|
|
|
locallib=dict(type='path'),
|
|
|
|
mirror=dict(type='str'),
|
|
|
|
mirror_only=dict(type='bool', default=False),
|
|
|
|
installdeps=dict(type='bool', default=False),
|
|
|
|
executable=dict(type='path'),
|
2023-05-29 18:52:02 +02:00
|
|
|
mode=dict(type='str', choices=['compatibility', 'new']),
|
2021-04-18 10:55:47 +02:00
|
|
|
name_check=dict(type='str')
|
|
|
|
),
|
|
|
|
required_one_of=[('name', 'from_path')],
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
)
|
2021-04-18 10:55:47 +02:00
|
|
|
command = 'cpanm'
|
|
|
|
command_args_formats = dict(
|
2022-11-07 20:15:00 +01:00
|
|
|
notest=cmd_runner_fmt.as_bool("--notest"),
|
|
|
|
locallib=cmd_runner_fmt.as_opt_val('--local-lib'),
|
|
|
|
mirror=cmd_runner_fmt.as_opt_val('--mirror'),
|
|
|
|
mirror_only=cmd_runner_fmt.as_bool("--mirror-only"),
|
|
|
|
installdeps=cmd_runner_fmt.as_bool("--installdeps"),
|
|
|
|
pkg_spec=cmd_runner_fmt.as_list(),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
2021-04-18 10:55:47 +02:00
|
|
|
|
|
|
|
def __init_module__(self):
|
|
|
|
v = self.vars
|
2023-05-29 18:52:02 +02:00
|
|
|
if v.mode is None:
|
|
|
|
self.deprecate(
|
|
|
|
"The default value 'compatibility' for parameter 'mode' is being deprecated "
|
|
|
|
"and it will be replaced by 'new'",
|
|
|
|
version="9.0.0",
|
|
|
|
collection_name="community.general"
|
|
|
|
)
|
|
|
|
v.mode = "compatibility"
|
2021-04-18 10:55:47 +02:00
|
|
|
if v.mode == "compatibility":
|
|
|
|
if v.name_check:
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise("Parameter name_check can only be used with mode=new")
|
2021-04-18 10:55:47 +02:00
|
|
|
else:
|
|
|
|
if v.name and v.from_path:
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise("Parameters 'name' and 'from_path' are mutually exclusive when 'mode=new'")
|
2021-04-18 10:55:47 +02:00
|
|
|
|
2023-04-22 22:58:02 +02:00
|
|
|
self.command = self.get_bin_path(v.executable if v.executable else self.command)
|
2021-04-18 10:55:47 +02:00
|
|
|
self.vars.set("binary", self.command)
|
|
|
|
|
|
|
|
def _is_package_installed(self, name, locallib, version):
|
2022-11-07 20:15:00 +01:00
|
|
|
def process(rc, out, err):
|
|
|
|
return rc == 0
|
|
|
|
|
2021-04-18 10:55:47 +02:00
|
|
|
if name is None or name.endswith('.tar.gz'):
|
|
|
|
return False
|
|
|
|
version = "" if version is None else " " + version
|
|
|
|
|
|
|
|
env = {"PERL5LIB": "%s/lib/perl5" % locallib} if locallib else {}
|
2022-11-07 20:15:00 +01:00
|
|
|
runner = CmdRunner(self.module, ["perl", "-le"], {"mod": cmd_runner_fmt.as_list()}, check_rc=False, environ_update=env)
|
|
|
|
with runner("mod", output_process=process) as ctx:
|
|
|
|
return ctx.run(mod='use %s%s;' % (name, version))
|
2021-04-18 10:55:47 +02:00
|
|
|
|
2022-05-23 07:19:24 +02:00
|
|
|
def sanitize_pkg_spec_version(self, pkg_spec, version):
|
2021-04-18 10:55:47 +02:00
|
|
|
if version is None:
|
|
|
|
return pkg_spec
|
|
|
|
if pkg_spec.endswith('.tar.gz'):
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise(msg="parameter 'version' must not be used when installing from a file")
|
2021-04-18 10:55:47 +02:00
|
|
|
if os.path.isdir(pkg_spec):
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise(msg="parameter 'version' must not be used when installing from a directory")
|
2021-04-18 10:55:47 +02:00
|
|
|
if pkg_spec.endswith('.git'):
|
|
|
|
if version.startswith('~'):
|
2022-05-23 07:19:24 +02:00
|
|
|
self.do_raise(msg="operator '~' not allowed in version parameter when installing from git repository")
|
2021-04-18 10:55:47 +02:00
|
|
|
version = version if version.startswith('@') else '@' + version
|
|
|
|
elif version[0] not in ('@', '~'):
|
|
|
|
version = '~' + version
|
|
|
|
return pkg_spec + version
|
|
|
|
|
|
|
|
def __run__(self):
|
2022-11-07 20:15:00 +01:00
|
|
|
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)
|
|
|
|
|
2021-04-18 10:55:47 +02:00
|
|
|
v = self.vars
|
|
|
|
pkg_param = 'from_path' if v.from_path else 'name'
|
|
|
|
|
|
|
|
if v.mode == 'compatibility':
|
|
|
|
if self._is_package_installed(v.name, v.locallib, v.version):
|
|
|
|
return
|
|
|
|
pkg_spec = v[pkg_param]
|
|
|
|
else:
|
|
|
|
installed = self._is_package_installed(v.name_check, v.locallib, v.version) if v.name_check else False
|
|
|
|
if installed:
|
|
|
|
return
|
|
|
|
pkg_spec = self.sanitize_pkg_spec_version(v[pkg_param], v.version)
|
2022-11-07 20:15:00 +01:00
|
|
|
|
|
|
|
with runner(['notest', 'locallib', 'mirror', 'mirror_only', 'installdeps', 'pkg_spec'], output_process=process) as ctx:
|
|
|
|
self.changed = ctx.run(pkg_spec=pkg_spec)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-04-18 10:55:47 +02:00
|
|
|
def main():
|
2021-08-17 10:43:18 +02:00
|
|
|
CPANMinus.execute()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|