2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:45:15 +02:00
|
|
|
# Copyright (c) 2012, Franck Cuny <franck@lumberjaph.net>
|
|
|
|
# Copyright (c) 2021, Alexei Znamensky <russoz@gmail.com>
|
2022-08-05 13:17:19 +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:33:03 +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.
|
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:
|
2021-04-18 10:55:47 +02:00
|
|
|
- The Perl library to install. Valid values change according to the I(mode), see notes for more details.
|
|
|
|
- Note that for installing from a local path the parameter I(from_path) should be used.
|
|
|
|
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 20:46:30 +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 20:46:30 +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 20:46:30 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
version:
|
|
|
|
description:
|
2021-04-18 10:55:47 +02:00
|
|
|
- Version specification for the perl module. When I(mode) is C(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.
|
|
|
|
type: str
|
|
|
|
choices: [compatibility, new]
|
|
|
|
default: compatibility
|
|
|
|
version_added: 3.0.0
|
|
|
|
name_check:
|
|
|
|
description:
|
|
|
|
- When in C(new) mode, this parameter can be used to check if there is a module I(name) installed (at I(version), when specified).
|
|
|
|
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.
|
|
|
|
- "This module now comes with a choice of execution I(mode): C(compatibility) or C(new)."
|
|
|
|
- "C(compatibility) mode:"
|
|
|
|
- When using C(compatibility) mode, the module will keep backward compatibility. This is the default mode.
|
|
|
|
- I(name) must be either a module name or a distribution file.
|
|
|
|
- >
|
|
|
|
If the perl module given by I(name) is installed (at the exact I(version) when specified), then nothing happens.
|
|
|
|
Otherwise, it will be installed using the C(cpanm) executable.
|
|
|
|
- I(name) cannot be an URL, or a git URL.
|
|
|
|
- C(cpanm) version specifiers do not work in this mode.
|
|
|
|
- "C(new) mode:"
|
|
|
|
- "When using C(new) mode, the module will behave differently"
|
|
|
|
- >
|
|
|
|
The I(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.
|
|
|
|
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 21:07:46 +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 20:46:30 +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
|
|
|
|
|
2021-04-18 10:55:47 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.module_helper import (
|
2022-05-23 07:32:54 +02:00
|
|
|
ModuleHelper, CmdMixin, ArgFormat
|
2021-04-18 10:55:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CPANMinus(CmdMixin, ModuleHelper):
|
|
|
|
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'),
|
|
|
|
mode=dict(type='str', choices=['compatibility', 'new'], default='compatibility'),
|
|
|
|
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(
|
|
|
|
notest=dict(fmt="--notest", style=ArgFormat.BOOLEAN),
|
|
|
|
locallib=dict(fmt=('--local-lib', '{0}'),),
|
|
|
|
mirror=dict(fmt=('--mirror', '{0}'),),
|
|
|
|
mirror_only=dict(fmt="--mirror-only", style=ArgFormat.BOOLEAN),
|
|
|
|
installdeps=dict(fmt="--installdeps", style=ArgFormat.BOOLEAN),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
2021-04-18 10:55:47 +02:00
|
|
|
check_rc = True
|
|
|
|
|
|
|
|
def __init_module__(self):
|
|
|
|
v = self.vars
|
|
|
|
if v.mode == "compatibility":
|
|
|
|
if v.name_check:
|
2022-05-23 07:32:54 +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:32:54 +02:00
|
|
|
self.do_raise("Parameters 'name' and 'from_path' are mutually exclusive when 'mode=new'")
|
2021-04-18 10:55:47 +02:00
|
|
|
|
|
|
|
self.command = self.module.get_bin_path(v.executable if v.executable else self.command)
|
|
|
|
self.vars.set("binary", self.command)
|
|
|
|
|
|
|
|
def _is_package_installed(self, name, locallib, version):
|
|
|
|
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 {}
|
|
|
|
cmd = ['perl', '-le', 'use %s%s;' % (name, version)]
|
|
|
|
rc, out, err = self.module.run_command(cmd, check_rc=False, environ_update=env)
|
|
|
|
|
|
|
|
return rc == 0
|
|
|
|
|
2022-05-23 07:32:54 +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:32:54 +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:32:54 +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:32:54 +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):
|
|
|
|
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]
|
|
|
|
self.changed = self.run_command(
|
2021-10-12 13:56:15 +02:00
|
|
|
params=['notest', 'locallib', 'mirror', 'mirror_only', 'installdeps', {'name': pkg_spec}],
|
2021-04-18 10:55:47 +02:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
self.changed = self.run_command(
|
|
|
|
params=['notest', 'locallib', 'mirror', 'mirror_only', 'installdeps', {'name': pkg_spec}],
|
|
|
|
)
|
|
|
|
|
|
|
|
def process_command_output(self, rc, out, err):
|
|
|
|
if self.vars.mode == "compatibility" and rc != 0:
|
2022-05-23 07:32:54 +02:00
|
|
|
self.do_raise(msg=err, cmd=self.vars.cmd_args)
|
2021-04-18 10:55:47 +02:00
|
|
|
return 'is up to date' not in err and 'is up to date' not in out
|
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()
|