1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

django_manage - added splitting the command parameter for running (#3283)

* added splitting the command parameter for running

* added changelog fragment

* refactored variable names for improved readability
This commit is contained in:
Alexei Znamensky 2021-08-31 17:14:08 +12:00 committed by GitHub
parent baa721ac22
commit b2bb7e3f9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 18 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- django_manage - argument ``command`` is being splitted again as it should (https://github.com/ansible-collections/community.general/issues/3215).

View file

@ -158,6 +158,7 @@ EXAMPLES = """
import os import os
import sys import sys
import shlex
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -273,61 +274,62 @@ def main():
), ),
) )
command = module.params['command'] command_split = shlex.split(module.params['command'])
command_bin = command_split[0]
project_path = module.params['project_path'] project_path = module.params['project_path']
virtualenv = module.params['virtualenv'] virtualenv = module.params['virtualenv']
for param in specific_params: for param in specific_params:
value = module.params[param] value = module.params[param]
if value and param not in command_allowed_param_map[command]: if value and param not in command_allowed_param_map[command_bin]:
module.fail_json(msg='%s param is incompatible with command=%s' % (param, command)) module.fail_json(msg='%s param is incompatible with command=%s' % (param, command_bin))
for param in command_required_param_map.get(command, ()): for param in command_required_param_map.get(command_bin, ()):
if not module.params[param]: if not module.params[param]:
module.fail_json(msg='%s param is required for command=%s' % (param, command)) module.fail_json(msg='%s param is required for command=%s' % (param, command_bin))
_ensure_virtualenv(module) _ensure_virtualenv(module)
cmd = ["./manage.py", command] run_cmd_args = ["./manage.py"] + command_split
if command in noinput_commands: if command_bin in noinput_commands and '--noinput' not in command_split:
cmd.append("--noinput") run_cmd_args.append("--noinput")
for param in general_params: for param in general_params:
if module.params[param]: if module.params[param]:
cmd.append('--%s=%s' % (param, module.params[param])) run_cmd_args.append('--%s=%s' % (param, module.params[param]))
for param in specific_boolean_params: for param in specific_boolean_params:
if module.params[param]: if module.params[param]:
cmd.append('--%s' % param) run_cmd_args.append('--%s' % param)
# these params always get tacked on the end of the command # these params always get tacked on the end of the command
for param in end_of_command_params: for param in end_of_command_params:
if module.params[param]: if module.params[param]:
cmd.append(module.params[param]) run_cmd_args.append(module.params[param])
rc, out, err = module.run_command(cmd, cwd=project_path) rc, out, err = module.run_command(run_cmd_args, cwd=project_path)
if rc != 0: if rc != 0:
if command == 'createcachetable' and 'table' in err and 'already exists' in err: if command_bin == 'createcachetable' and 'table' in err and 'already exists' in err:
out = 'already exists.' out = 'already exists.'
else: else:
if "Unknown command:" in err: if "Unknown command:" in err:
_fail(module, cmd, err, "Unknown django command: %s" % command) _fail(module, run_cmd_args, err, "Unknown django command: %s" % command_bin)
_fail(module, cmd, out, err, path=os.environ["PATH"], syspath=sys.path) _fail(module, run_cmd_args, out, err, path=os.environ["PATH"], syspath=sys.path)
changed = False changed = False
lines = out.split('\n') lines = out.split('\n')
filt = globals().get(command + "_filter_output", None) filt = globals().get(command_bin + "_filter_output", None)
if filt: if filt:
filtered_output = list(filter(filt, lines)) filtered_output = list(filter(filt, lines))
if len(filtered_output): if len(filtered_output):
changed = True changed = True
check_changed = globals().get("{0}_check_changed".format(command), None) check_changed = globals().get("{0}_check_changed".format(command_bin), None)
if check_changed: if check_changed:
changed = check_changed(out) changed = check_changed(out)
module.exit_json(changed=changed, out=out, cmd=cmd, app_path=project_path, project_path=project_path, module.exit_json(changed=changed, out=out, cmd=run_cmd_args, app_path=project_path, project_path=project_path,
virtualenv=virtualenv, settings=module.params['settings'], pythonpath=module.params['pythonpath']) virtualenv=virtualenv, settings=module.params['settings'], pythonpath=module.params['pythonpath'])

View file

@ -121,3 +121,4 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.1/howto/static-files/ # https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATIC_ROOT = '/tmp/django-static'

View file

@ -48,3 +48,9 @@
pythonpath: "{{ tmp_django_root.path }}/1045-single-app-project/" pythonpath: "{{ tmp_django_root.path }}/1045-single-app-project/"
command: check command: check
virtualenv: "{{ tmp_django_root.path }}/venv" virtualenv: "{{ tmp_django_root.path }}/venv"
- name: Run collectstatic --noinput on simple project
community.general.django_manage:
project_path: "{{ tmp_django_root.path }}/simple_project/p1"
command: collectstatic --noinput
virtualenv: "{{ tmp_django_root.path }}/venv"