mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ansible_galaxy_install: using CmdRunner (#5477)
* ansible_galaxy_install: using CmdRunner * fix sanity checks * add changelog fragment
This commit is contained in:
parent
fc817601bc
commit
fb90b5cbe8
2 changed files with 47 additions and 44 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- ansible_galaxy_install - refactored module to use ``CmdRunner`` to execute ``ansible-galaxy`` (https://github.com/ansible-collections/community.general/pull/5477).
|
|
@ -184,10 +184,11 @@ RETURN = """
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from ansible_collections.community.general.plugins.module_utils.module_helper import CmdModuleHelper, ArgFormat
|
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt as fmt
|
||||||
|
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
|
||||||
|
|
||||||
|
|
||||||
class AnsibleGalaxyInstall(CmdModuleHelper):
|
class AnsibleGalaxyInstall(ModuleHelper):
|
||||||
_RE_GALAXY_VERSION = re.compile(r'^ansible-galaxy(?: \[core)? (?P<version>\d+\.\d+\.\d+)(?:\.\w+)?(?:\])?')
|
_RE_GALAXY_VERSION = re.compile(r'^ansible-galaxy(?: \[core)? (?P<version>\d+\.\d+\.\d+)(?:\.\w+)?(?:\])?')
|
||||||
_RE_LIST_PATH = re.compile(r'^# (?P<path>.*)$')
|
_RE_LIST_PATH = re.compile(r'^# (?P<path>.*)$')
|
||||||
_RE_LIST_COLL = re.compile(r'^(?P<elem>\w+\.\w+)\s+(?P<version>[\d\.]+)\s*$')
|
_RE_LIST_COLL = re.compile(r'^(?P<elem>\w+\.\w+)\s+(?P<version>[\d\.]+)\s*$')
|
||||||
|
@ -216,28 +217,33 @@ class AnsibleGalaxyInstall(CmdModuleHelper):
|
||||||
|
|
||||||
command = 'ansible-galaxy'
|
command = 'ansible-galaxy'
|
||||||
command_args_formats = dict(
|
command_args_formats = dict(
|
||||||
type=dict(fmt=lambda v: [] if v == 'both' else [v]),
|
type=fmt.as_func(lambda v: [] if v == 'both' else [v]),
|
||||||
galaxy_cmd=dict(),
|
galaxy_cmd=fmt.as_list(),
|
||||||
requirements_file=dict(fmt=('-r', '{0}'),),
|
requirements_file=fmt.as_opt_val('-r'),
|
||||||
dest=dict(fmt=('-p', '{0}'),),
|
dest=fmt.as_opt_val('-p'),
|
||||||
force=dict(fmt="--force", style=ArgFormat.BOOLEAN),
|
force=fmt.as_bool("--force"),
|
||||||
no_deps=dict(fmt="--no-deps", style=ArgFormat.BOOLEAN),
|
no_deps=fmt.as_bool("--no-deps"),
|
||||||
|
version=fmt.as_bool("--version"),
|
||||||
|
name=fmt.as_list(),
|
||||||
)
|
)
|
||||||
force_lang = "en_US.UTF-8"
|
force_lang = "en_US.UTF-8"
|
||||||
check_rc = True
|
check_rc = True
|
||||||
|
|
||||||
def _get_ansible_galaxy_version(self):
|
def _get_ansible_galaxy_version(self):
|
||||||
ansible_galaxy = self.get_bin_path("ansible-galaxy", required=True)
|
def process(rc, out, err):
|
||||||
dummy, out, dummy = self.module.run_command([ansible_galaxy, "--version"], check_rc=True)
|
|
||||||
line = out.splitlines()[0]
|
line = out.splitlines()[0]
|
||||||
match = self._RE_GALAXY_VERSION.match(line)
|
match = self._RE_GALAXY_VERSION.match(line)
|
||||||
if not match:
|
if not match:
|
||||||
raise RuntimeError("Unable to determine ansible-galaxy version from: {0}".format(line))
|
self.do_raise("Unable to determine ansible-galaxy version from: {0}".format(line))
|
||||||
version = match.group("version")
|
version = match.group("version")
|
||||||
version = tuple(int(x) for x in version.split('.')[:3])
|
version = tuple(int(x) for x in version.split('.')[:3])
|
||||||
return version
|
return version
|
||||||
|
|
||||||
|
with self.runner("version", check_rc=True, output_process=process) as ctx:
|
||||||
|
return ctx.run(version=True)
|
||||||
|
|
||||||
def __init_module__(self):
|
def __init_module__(self):
|
||||||
|
self.runner = CmdRunner(self.module, command=self.command, arg_formats=self.command_args_formats, force_lang=self.force_lang)
|
||||||
self.ansible_version = self._get_ansible_galaxy_version()
|
self.ansible_version = self._get_ansible_galaxy_version()
|
||||||
if self.ansible_version < (2, 11) and not self.vars.ack_min_ansiblecore211:
|
if self.ansible_version < (2, 11) and not self.vars.ack_min_ansiblecore211:
|
||||||
self.module.deprecate(
|
self.module.deprecate(
|
||||||
|
@ -260,18 +266,13 @@ class AnsibleGalaxyInstall(CmdModuleHelper):
|
||||||
r'|- (?P<role>\w+\.\w+) \((?P<rversion>[\d\.]+)\))'
|
r'|- (?P<role>\w+\.\w+) \((?P<rversion>[\d\.]+)\))'
|
||||||
r' was installed successfully$')
|
r' was installed successfully$')
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _process_output_list(*args):
|
|
||||||
if "None of the provided paths were usable" in args[1]:
|
|
||||||
return []
|
|
||||||
return args[1].splitlines()
|
|
||||||
|
|
||||||
def _list_element(self, _type, path_re, elem_re):
|
def _list_element(self, _type, path_re, elem_re):
|
||||||
params = ({'type': _type}, {'galaxy_cmd': 'list'}, 'dest')
|
def process(rc, out, err):
|
||||||
elems = self.run_command(params=params,
|
return [] if "None of the provided paths were usable" in out else out.splitlines()
|
||||||
publish_rc=False, publish_out=False, publish_err=False, publish_cmd=False,
|
|
||||||
process_output=self._process_output_list,
|
with self.runner('type galaxy_cmd dest', output_process=process, check_rc=False) as ctx:
|
||||||
check_rc=False)
|
elems = ctx.run(type=_type, galaxy_cmd='list')
|
||||||
|
|
||||||
elems_dict = {}
|
elems_dict = {}
|
||||||
current_path = None
|
current_path = None
|
||||||
for line in elems:
|
for line in elems:
|
||||||
|
@ -316,16 +317,7 @@ class AnsibleGalaxyInstall(CmdModuleHelper):
|
||||||
self.vars.installed_collections = self._list_collections()
|
self.vars.installed_collections = self._list_collections()
|
||||||
|
|
||||||
def __run__(self):
|
def __run__(self):
|
||||||
if self.is_ansible29:
|
def process(rc, out, err):
|
||||||
if self.vars.type == 'both':
|
|
||||||
raise ValueError("Type 'both' not supported in Ansible 2.9")
|
|
||||||
self._setup29()
|
|
||||||
else:
|
|
||||||
self._setup210plus()
|
|
||||||
params = ('type', {'galaxy_cmd': 'install'}, 'force', 'no_deps', 'dest', 'requirements_file', 'name')
|
|
||||||
self.run_command(params=params)
|
|
||||||
|
|
||||||
def process_command_output(self, rc, out, err):
|
|
||||||
for line in out.splitlines():
|
for line in out.splitlines():
|
||||||
match = self._RE_INSTALL_OUTPUT.match(line)
|
match = self._RE_INSTALL_OUTPUT.match(line)
|
||||||
if not match:
|
if not match:
|
||||||
|
@ -339,6 +331,15 @@ class AnsibleGalaxyInstall(CmdModuleHelper):
|
||||||
if self.is_ansible29:
|
if self.is_ansible29:
|
||||||
self.vars.ansible29_change = True
|
self.vars.ansible29_change = True
|
||||||
|
|
||||||
|
if self.is_ansible29:
|
||||||
|
if self.vars.type == 'both':
|
||||||
|
raise ValueError("Type 'both' not supported in Ansible 2.9")
|
||||||
|
self._setup29()
|
||||||
|
else:
|
||||||
|
self._setup210plus()
|
||||||
|
with self.runner("type galaxy_cmd force no_deps dest requirements_file name", output_process=process) as ctx:
|
||||||
|
ctx.run(galaxy_cmd="install")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
galaxy = AnsibleGalaxyInstall()
|
galaxy = AnsibleGalaxyInstall()
|
||||||
|
|
Loading…
Reference in a new issue