| 1 |
# Copyright (c) 2024, Alexei Znamensky <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import typing as t |
| 11 |
|
| 12 |
from ansible.module_utils.common.dict_transformations import dict_merge |
| 13 |
|
| 14 |
from ansible_collections.community.general.plugins.module_utils import _cmd_runner_fmt as cmd_runner_fmt |
| 15 |
from ansible_collections.community.general.plugins.module_utils._module_helper import ModuleHelper |
| 16 |
from ansible_collections.community.general.plugins.module_utils._python_runner import PythonRunner |
| 17 |
|
| 18 |
if t.TYPE_CHECKING: |
| 19 |
from ansible.module_utils.basic import AnsibleModule |
| 20 |
|
| 21 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import ArgFormatter |
| 22 |
|
| 23 |
|
| 24 |
django_std_args = dict( |
| 25 |
# environmental options |
| 26 |
venv=dict(type="path"), |
| 27 |
# default options of django-admin |
| 28 |
settings=dict(type="str", required=True), |
| 29 |
pythonpath=dict(type="path"), |
| 30 |
traceback=dict(type="bool"), |
| 31 |
verbosity=dict(type="int", choices=[0, 1, 2, 3]), |
| 32 |
skip_checks=dict(type="bool"), |
| 33 |
) |
| 34 |
_database_dash = dict( |
| 35 |
database=dict(type="str", default="default"), |
| 36 |
) |
| 37 |
_data = dict( |
| 38 |
excludes=dict(type="list", elements="str"), |
| 39 |
format=dict(type="str", default="json", choices=["xml", "json", "jsonl", "yaml"]), |
| 40 |
) |
| 41 |
_pks = dict( |
| 42 |
primary_keys=dict(type="list", elements="str"), |
| 43 |
) |
| 44 |
|
| 45 |
_django_std_arg_fmts: dict[str, ArgFormatter] = dict( |
| 46 |
all=cmd_runner_fmt.as_bool("--all"), |
| 47 |
app=cmd_runner_fmt.as_opt_val("--app"), |
| 48 |
apps=cmd_runner_fmt.as_list(), |
| 49 |
apps_models=cmd_runner_fmt.as_list(), |
| 50 |
check=cmd_runner_fmt.as_bool("--check"), |
| 51 |
command=cmd_runner_fmt.as_list(), |
| 52 |
database_dash=cmd_runner_fmt.as_opt_eq_val("--database"), |
| 53 |
database_stacked_dash=cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_val)("--database"), |
| 54 |
deploy=cmd_runner_fmt.as_bool("--deploy"), |
| 55 |
dry_run=cmd_runner_fmt.as_bool("--dry-run"), |
| 56 |
excludes=cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_val)("--exclude"), |
| 57 |
fail_level=cmd_runner_fmt.as_opt_val("--fail-level"), |
| 58 |
fixture=cmd_runner_fmt.as_opt_val("--output"), |
| 59 |
fixtures=cmd_runner_fmt.as_list(), |
| 60 |
format=cmd_runner_fmt.as_opt_val("--format"), |
| 61 |
ignore_non_existent=cmd_runner_fmt.as_bool("--ignorenonexistent"), |
| 62 |
indent=cmd_runner_fmt.as_opt_val("--indent"), |
| 63 |
natural_foreign=cmd_runner_fmt.as_bool("--natural-foreign"), |
| 64 |
natural_primary=cmd_runner_fmt.as_bool("--natural-primary"), |
| 65 |
no_color=cmd_runner_fmt.as_fixed("--no-color"), |
| 66 |
noinput=cmd_runner_fmt.as_fixed("--noinput"), |
| 67 |
primary_keys=lambda v: ["--pks", ",".join(v)], |
| 68 |
pythonpath=cmd_runner_fmt.as_opt_eq_val("--pythonpath"), |
| 69 |
settings=cmd_runner_fmt.as_opt_eq_val("--settings"), |
| 70 |
skip_checks=cmd_runner_fmt.as_bool("--skip-checks"), |
| 71 |
tags=cmd_runner_fmt.stack(cmd_runner_fmt.as_opt_val)("--tag"), |
| 72 |
traceback=cmd_runner_fmt.as_bool("--traceback"), |
| 73 |
verbosity=cmd_runner_fmt.as_opt_val("--verbosity"), |
| 74 |
version=cmd_runner_fmt.as_fixed("--version"), |
| 75 |
) |
| 76 |
|
| 77 |
# keys can be used in _django_args |
| 78 |
_args_menu = dict( |
| 79 |
std=(django_std_args, _django_std_arg_fmts), |
| 80 |
database_dash=(_database_dash, {}), |
| 81 |
data=(_data, {}), |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
class _DjangoRunner(PythonRunner): |
| 86 |
def __init__(self, module: AnsibleModule, arg_formats=None, **kwargs) -> None: |
| 87 |
arg_fmts = dict(arg_formats) if arg_formats else {} |
| 88 |
arg_fmts.update(_django_std_arg_fmts) |
| 89 |
|
| 90 |
super().__init__(module, ["-m", "django"], arg_formats=arg_fmts, **kwargs) |
| 91 |
|
| 92 |
def __call__(self, output_process=None, check_mode_skip=False, check_mode_return=None, **kwargs): |
| 93 |
args_order = ( |
| 94 |
"command", |
| 95 |
"no_color", |
| 96 |
"settings", |
| 97 |
"pythonpath", |
| 98 |
"traceback", |
| 99 |
"verbosity", |
| 100 |
"skip_checks", |
| 101 |
) + self._prepare_args_order(self.default_args_order) |
| 102 |
return super().__call__( |
| 103 |
args_order, output_process, check_mode_skip=check_mode_skip, check_mode_return=check_mode_return, **kwargs |
| 104 |
) |
| 105 |
|
| 106 |
def bare_context(self, *args, **kwargs): |
| 107 |
return super().__call__(*args, **kwargs) |
| 108 |
|
| 109 |
|
| 110 |
class DjangoModuleHelper(ModuleHelper): |
| 111 |
module = {} |
| 112 |
django_admin_cmd: str | None = None |
| 113 |
arg_formats: dict[str, ArgFormatter] = {} |
| 114 |
django_admin_arg_order: tuple[str, ...] | str = () |
| 115 |
_django_args: list[str] = [] |
| 116 |
_check_mode_arg: str = "" |
| 117 |
|
| 118 |
def __init__(self) -> None: |
| 119 |
self.module["argument_spec"], self.arg_formats = self._build_args( |
| 120 |
self.module.get("argument_spec", {}), self.arg_formats, *(["std"] + self._django_args) |
| 121 |
) |
| 122 |
super().__init__(self.module) |
| 123 |
if self.django_admin_cmd is not None: |
| 124 |
self.vars.command = self.django_admin_cmd |
| 125 |
|
| 126 |
@staticmethod |
| 127 |
def _build_args(arg_spec, arg_format, *names) -> tuple[dict[str, t.Any], dict[str, ArgFormatter]]: |
| 128 |
res_arg_spec: dict[str, t.Any] = {} |
| 129 |
res_arg_fmts: dict[str, ArgFormatter] = {} |
| 130 |
for name in names: |
| 131 |
args, fmts = _args_menu[name] |
| 132 |
res_arg_spec = dict_merge(res_arg_spec, args) |
| 133 |
res_arg_fmts = dict_merge(res_arg_fmts, fmts) |
| 134 |
res_arg_spec = dict_merge(res_arg_spec, arg_spec) |
| 135 |
res_arg_fmts = dict_merge(res_arg_fmts, arg_format) |
| 136 |
|
| 137 |
return res_arg_spec, res_arg_fmts |
| 138 |
|
| 139 |
def __run__(self): |
| 140 |
runner = _DjangoRunner( |
| 141 |
self.module, |
| 142 |
default_args_order=self.django_admin_arg_order, |
| 143 |
arg_formats=self.arg_formats, |
| 144 |
venv=self.vars.venv, |
| 145 |
check_rc=True, |
| 146 |
) |
| 147 |
|
| 148 |
run_params = self.vars.as_dict() |
| 149 |
if self._check_mode_arg: |
| 150 |
run_params.update({self._check_mode_arg: self.check_mode}) |
| 151 |
|
| 152 |
rc, out, err = runner.bare_context("version").run() |
| 153 |
self.vars.version = out.strip() |
| 154 |
|
| 155 |
with runner() as ctx: |
| 156 |
results = ctx.run(**run_params) |
| 157 |
self.vars.stdout = ctx.results_out |
| 158 |
self.vars.stderr = ctx.results_err |
| 159 |
self.vars.cmd = ctx.cmd |
| 160 |
self.vars.set("run_info", ctx.run_info, verbosity=3) |
| 161 |
|
| 162 |
return results |
| 163 |
|
| 164 |
@classmethod |
| 165 |
def execute(cls): |
| 166 |
cls().run() |