django_command/django_check/django_createcachetable: add return value version (#9063)

* add return value version

* add changelog frag

* reformat yaml
pull/9067/head
Alexei Znamensky 2024-10-27 21:36:38 +13:00 committed by GitHub
parent 39f3b151e8
commit 107df41d9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 104 additions and 35 deletions

View File

@ -0,0 +1,5 @@
minor_changes:
- django module utils - always retrieve version (https://github.com/ansible-collections/community.general/pull/9063).
- django_check - add return value ``version`` (https://github.com/ansible-collections/community.general/pull/9063).
- django_command - add return value ``version`` (https://github.com/ansible-collections/community.general/pull/9063).
- django_createcachetable - add return value ``version`` (https://github.com/ansible-collections/community.general/pull/9063).

View File

@ -32,6 +32,7 @@ _django_std_arg_fmts = dict(
verbosity=cmd_runner_fmt.as_opt_val("--verbosity"), verbosity=cmd_runner_fmt.as_opt_val("--verbosity"),
no_color=cmd_runner_fmt.as_fixed("--no-color"), no_color=cmd_runner_fmt.as_fixed("--no-color"),
skip_checks=cmd_runner_fmt.as_bool("--skip-checks"), skip_checks=cmd_runner_fmt.as_bool("--skip-checks"),
version=cmd_runner_fmt.as_fixed("--version"),
) )
_django_database_args = dict( _django_database_args = dict(
@ -60,6 +61,9 @@ class _DjangoRunner(PythonRunner):
) )
return super(_DjangoRunner, self).__call__(args_order, output_process, ignore_value_none, check_mode_skip, check_mode_return, **kwargs) return super(_DjangoRunner, self).__call__(args_order, output_process, ignore_value_none, check_mode_skip, check_mode_return, **kwargs)
def bare_context(self, *args, **kwargs):
return super(_DjangoRunner, self).__call__(*args, **kwargs)
class DjangoModuleHelper(ModuleHelper): class DjangoModuleHelper(ModuleHelper):
module = {} module = {}
@ -98,16 +102,20 @@ class DjangoModuleHelper(ModuleHelper):
arg_formats=self.arg_formats, arg_formats=self.arg_formats,
venv=self.vars.venv, venv=self.vars.venv,
check_rc=True) check_rc=True)
run_params = self.vars.as_dict()
if self._check_mode_arg:
run_params.update({self._check_mode_arg: self.check_mode})
rc, out, err = runner.bare_context("version").run()
self.vars.version = out.strip()
with runner() as ctx: with runner() as ctx:
run_params = self.vars.as_dict()
if self._check_mode_arg:
run_params.update({self._check_mode_arg: self.check_mode})
results = ctx.run(**run_params) results = ctx.run(**run_params)
self.vars.stdout = ctx.results_out self.vars.stdout = ctx.results_out
self.vars.stderr = ctx.results_err self.vars.stderr = ctx.results_err
self.vars.cmd = ctx.cmd self.vars.cmd = ctx.cmd
if self.verbosity >= 3: self.vars.set("run_info", ctx.run_info, verbosity=3)
self.vars.run_info = ctx.run_info
return results return results

View File

@ -8,48 +8,49 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
DOCUMENTATION = """ DOCUMENTATION = """
---
module: django_check module: django_check
author: author:
- Alexei Znamensky (@russoz) - Alexei Znamensky (@russoz)
short_description: Wrapper for C(django-admin check) short_description: Wrapper for C(django-admin check)
version_added: 9.1.0 version_added: 9.1.0
description: description:
- This module is a wrapper for the execution of C(django-admin check). - This module is a wrapper for the execution of C(django-admin check).
extends_documentation_fragment: extends_documentation_fragment:
- community.general.attributes - community.general.attributes
- community.general.django - community.general.django
options: options:
database: database:
description: description:
- Specify databases to run checks against. - Specify databases to run checks against.
- If not specified, Django will not run database tests. - If not specified, Django will not run database tests.
type: list type: list
elements: str elements: str
deploy: deploy:
description: description:
- Include additional checks relevant in a deployment setting. - Include additional checks relevant in a deployment setting.
type: bool type: bool
default: false default: false
fail_level: fail_level:
description: description:
- Message level that will trigger failure. - Message level that will trigger failure.
- Default is the Django default value. Check the documentation for the version being used. - Default is the Django default value. Check the documentation for the version being used.
type: str type: str
choices: [CRITICAL, ERROR, WARNING, INFO, DEBUG] choices: [CRITICAL, ERROR, WARNING, INFO, DEBUG]
tags: tags:
description: description:
- Restrict checks to specific tags. - Restrict checks to specific tags.
type: list type: list
elements: str elements: str
apps: apps:
description: description:
- Restrict checks to specific applications. - Restrict checks to specific applications.
- Default is to check all applications. - Default is to check all applications.
type: list type: list
elements: str elements: str
notes: notes:
- The outcome of the module is found in the common return values RV(ignore:stdout), RV(ignore:stderr), RV(ignore:rc). - The outcome of the module is found in the common return values RV(ignore:stdout), RV(ignore:stderr), RV(ignore:rc).
- The module will fail if RV(ignore:rc) is not zero. - The module will fail if RV(ignore:rc) is not zero.
attributes: attributes:
check_mode: check_mode:
support: full support: full
@ -58,6 +59,7 @@ attributes:
""" """
EXAMPLES = """ EXAMPLES = """
---
- name: Check the entire project - name: Check the entire project
community.general.django_check: community.general.django_check:
settings: myproject.settings settings: myproject.settings
@ -65,18 +67,25 @@ EXAMPLES = """
- name: Create the project using specific databases - name: Create the project using specific databases
community.general.django_check: community.general.django_check:
database: database:
- somedb - somedb
- myotherdb - myotherdb
settings: fancysite.settings settings: fancysite.settings
pythonpath: /home/joedoe/project/fancysite pythonpath: /home/joedoe/project/fancysite
venv: /home/joedoe/project/fancysite/venv venv: /home/joedoe/project/fancysite/venv
""" """
RETURN = """ RETURN = """
---
run_info: run_info:
description: Command-line execution information. description: Command-line execution information.
type: dict type: dict
returned: success and C(verbosity) >= 3 returned: success and C(verbosity) >= 3
version:
description: Version of Django.
type: str
returned: always
sample: 5.1.2
version_added: 10.0.0
""" """
from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper

View File

@ -8,16 +8,17 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
DOCUMENTATION = """ DOCUMENTATION = """
---
module: django_command module: django_command
author: author:
- Alexei Znamensky (@russoz) - Alexei Znamensky (@russoz)
short_description: Run Django admin commands short_description: Run Django admin commands
version_added: 9.0.0 version_added: 9.0.0
description: description:
- This module allows the execution of arbitrary Django admin commands. - This module allows the execution of arbitrary Django admin commands.
extends_documentation_fragment: extends_documentation_fragment:
- community.general.attributes - community.general.attributes
- community.general.django - community.general.django
attributes: attributes:
check_mode: check_mode:
support: none support: none
@ -26,17 +27,18 @@ attributes:
options: options:
command: command:
description: description:
- Django admin command. It must be a valid command accepted by C(python -m django) at the target system. - Django admin command. It must be a valid command accepted by C(python -m django) at the target system.
type: str type: str
required: true required: true
extra_args: extra_args:
type: list type: list
elements: str elements: str
description: description:
- List of extra arguments passed to the django admin command. - List of extra arguments passed to the django admin command.
""" """
EXAMPLES = """ EXAMPLES = """
---
- name: Check the project - name: Check the project
community.general.django_command: community.general.django_command:
command: check command: check
@ -51,10 +53,17 @@ EXAMPLES = """
""" """
RETURN = """ RETURN = """
---
run_info: run_info:
description: Command-line execution information. description: Command-line execution information.
type: dict type: dict
returned: success and O(verbosity) >= 3 returned: success and O(verbosity) >= 3
version:
description: Version of Django.
type: str
returned: always
sample: 5.1.2
version_added: 10.0.0
""" """
import shlex import shlex

View File

@ -8,17 +8,18 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
DOCUMENTATION = """ DOCUMENTATION = """
---
module: django_createcachetable module: django_createcachetable
author: author:
- Alexei Znamensky (@russoz) - Alexei Znamensky (@russoz)
short_description: Wrapper for C(django-admin createcachetable) short_description: Wrapper for C(django-admin createcachetable)
version_added: 9.1.0 version_added: 9.1.0
description: description:
- This module is a wrapper for the execution of C(django-admin createcachetable). - This module is a wrapper for the execution of C(django-admin createcachetable).
extends_documentation_fragment: extends_documentation_fragment:
- community.general.attributes - community.general.attributes
- community.general.django - community.general.django
- community.general.django.database - community.general.django.database
attributes: attributes:
check_mode: check_mode:
support: full support: full
@ -27,6 +28,7 @@ attributes:
""" """
EXAMPLES = """ EXAMPLES = """
---
- name: Create cache table in the default database - name: Create cache table in the default database
community.general.django_createcachetable: community.general.django_createcachetable:
settings: myproject.settings settings: myproject.settings
@ -40,10 +42,17 @@ EXAMPLES = """
""" """
RETURN = """ RETURN = """
---
run_info: run_info:
description: Command-line execution information. description: Command-line execution information.
type: dict type: dict
returned: success and O(verbosity) >= 3 returned: success and O(verbosity) >= 3
version:
description: Version of Django.
type: str
returned: always
sample: 5.1.2
version_added: 10.0.0
""" """
from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper

View File

@ -7,11 +7,18 @@
- id: success - id: success
input: input:
settings: whatever.settings settings: whatever.settings
output:
version: "5.1.2"
mocks: mocks:
run_command: run_command:
- command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings] - command: [/testbin/python, -m, django, --version]
environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
rc: 0 rc: 0
out: "5.1.2\n"
err: ""
- command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings]
environ: *env-def
rc: 0
out: "whatever\n" out: "whatever\n"
err: "" err: ""
- id: multiple_databases - id: multiple_databases
@ -20,8 +27,15 @@
database: database:
- abc - abc
- def - def
output:
version: "5.1.2"
mocks: mocks:
run_command: run_command:
- command: [/testbin/python, -m, django, --version]
environ: *env-def
rc: 0
out: "5.1.2\n"
err: ""
- command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, --database, abc, --database, def] - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, --database, abc, --database, def]
environ: *env-def environ: *env-def
rc: 0 rc: 0

View File

@ -15,9 +15,14 @@
settings: whatever.settings settings: whatever.settings
mocks: mocks:
run_command: run_command:
- command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo] - command: [/testbin/python, -m, django, --version]
environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
rc: 0 rc: 0
out: "5.1.2\n"
err: ""
- command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo]
environ: *env-def
rc: 0
out: "whatever\n" out: "whatever\n"
err: "" err: ""
- id: command_fail - id: command_fail
@ -33,6 +38,11 @@
failed: true failed: true
mocks: mocks:
run_command: run_command:
- command: [/testbin/python, -m, django, --version]
environ: *env-def
rc: 0
out: "5.1.2\n"
err: ""
- command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo] - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo]
environ: *env-def environ: *env-def
rc: 1 rc: 1

View File

@ -9,8 +9,13 @@
settings: whatever.settings settings: whatever.settings
mocks: mocks:
run_command: run_command:
- command: [/testbin/python, -m, django, --version]
environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
rc: 0
out: "5.1.2\n"
err: ""
- command: [/testbin/python, -m, django, createcachetable, --no-color, --settings=whatever.settings, --noinput, --database=default] - command: [/testbin/python, -m, django, createcachetable, --no-color, --settings=whatever.settings, --noinput, --database=default]
environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} environ: *env-def
rc: 0 rc: 0
out: "whatever\n" out: "whatever\n"
err: "" err: ""