2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-10-22 08:42:52 +02:00
|
|
|
# Copyright (c) 2022, Alexei Znamensky <russoz@gmail.com>
|
2022-08-08 14:24:58 +02:00
|
|
|
# Copyright (c) 2013, Scott Anderson <scottanderson42@gmail.com>
|
2022-08-05 12:28:29 +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: django_manage
|
2022-10-22 08:42:52 +02:00
|
|
|
short_description: Manages a Django application
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- Manages a Django application using the C(manage.py) application frontend to C(django-admin). With the
|
|
|
|
I(virtualenv) parameter, all management commands will be executed by the given C(virtualenv) installation.
|
2023-02-20 17:29:41 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
command:
|
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- The name of the Django management command to run. The commands listed below are built in this module and have some basic parameter validation.
|
|
|
|
- >
|
|
|
|
C(cleanup) - clean up old data from the database (deprecated in Django 1.5). This parameter will be
|
|
|
|
removed in community.general 9.0.0. Use C(clearsessions) instead.
|
|
|
|
- C(collectstatic) - Collects the static files into C(STATIC_ROOT).
|
|
|
|
- C(createcachetable) - Creates the cache tables for use with the database cache backend.
|
|
|
|
- C(flush) - Removes all data from the database.
|
|
|
|
- C(loaddata) - Searches for and loads the contents of the named I(fixtures) into the database.
|
|
|
|
- C(migrate) - Synchronizes the database state with models and migrations.
|
|
|
|
- >
|
|
|
|
C(syncdb) - Synchronizes the database state with models and migrations (deprecated in Django 1.7).
|
|
|
|
This parameter will be removed in community.general 9.0.0. Use C(migrate) instead.
|
|
|
|
- C(test) - Runs tests for all installed apps.
|
|
|
|
- >
|
|
|
|
C(validate) - Validates all installed models (deprecated in Django 1.7). This parameter will be
|
|
|
|
removed in community.general 9.0.0. Use C(check) instead.
|
|
|
|
- Other commands can be entered, but will fail if they are unknown to Django. Other commands that may
|
2020-10-26 07:54:57 +01:00
|
|
|
prompt for user input should be run with the C(--noinput) flag.
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- The path to the root of the Django application where C(manage.py) lives.
|
2020-10-26 07:54:57 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
2020-11-08 23:04:57 +01:00
|
|
|
aliases: [app_path, chdir]
|
2020-03-09 10:11:07 +01:00
|
|
|
settings:
|
|
|
|
description:
|
2020-10-26 07:54:57 +01:00
|
|
|
- The Python path to the application's settings module, such as C(myapp.settings).
|
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
pythonpath:
|
|
|
|
description:
|
2020-10-26 07:54:57 +01:00
|
|
|
- A directory to add to the Python path. Typically used to include the settings module if it is located
|
|
|
|
external to the application directory.
|
2022-10-22 08:42:52 +02:00
|
|
|
- This would be equivalent to adding I(pythonpath)'s value to the C(PYTHONPATH) environment variable.
|
2020-10-26 07:54:57 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
2020-10-26 07:54:57 +01:00
|
|
|
aliases: [python_path]
|
2020-03-09 10:11:07 +01:00
|
|
|
virtualenv:
|
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- An optional path to a C(virtualenv) installation to use while running the manage application.
|
2020-10-26 07:54:57 +01:00
|
|
|
type: path
|
|
|
|
aliases: [virtual_env]
|
2020-03-09 10:11:07 +01:00
|
|
|
apps:
|
|
|
|
description:
|
2020-10-26 07:54:57 +01:00
|
|
|
- A list of space-delimited apps to target. Used by the C(test) command.
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
cache_table:
|
|
|
|
description:
|
2020-10-26 07:54:57 +01:00
|
|
|
- The name of the table used for database-backed caching. Used by the C(createcachetable) command.
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
clear:
|
|
|
|
description:
|
|
|
|
- Clear the existing files before trying to copy or link the original file.
|
2021-09-07 06:22:46 +02:00
|
|
|
- Used only with the C(collectstatic) command. The C(--noinput) argument will be added automatically.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
2022-08-24 20:00:11 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
|
|
|
database:
|
|
|
|
description:
|
2020-10-26 07:54:57 +01:00
|
|
|
- The database to target. Used by the C(createcachetable), C(flush), C(loaddata), C(syncdb),
|
|
|
|
and C(migrate) commands.
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
failfast:
|
|
|
|
description:
|
2020-10-26 07:54:57 +01:00
|
|
|
- Fail the command immediately if a test fails. Used by the C(test) command.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
2020-10-26 07:54:57 +01:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2020-10-26 07:54:57 +01:00
|
|
|
aliases: [fail_fast]
|
2020-03-09 10:11:07 +01:00
|
|
|
fixtures:
|
|
|
|
description:
|
2020-10-26 07:54:57 +01:00
|
|
|
- A space-delimited list of fixture file names to load in the database. B(Required) by the C(loaddata) command.
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
skip:
|
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- Will skip over out-of-order missing migrations, you can only use this parameter with C(migrate) command.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
merge:
|
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- Will run out-of-order or missing migrations as they are not rollback migrations, you can only use this
|
|
|
|
parameter with C(migrate) command.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
link:
|
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- Will create links to the files instead of copying them, you can only use this parameter with
|
|
|
|
C(collectstatic) command.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
type: bool
|
2020-10-26 07:54:57 +01:00
|
|
|
testrunner:
|
|
|
|
description:
|
2022-10-22 08:42:52 +02:00
|
|
|
- Controls the test runner class that is used to execute tests.
|
2020-10-26 07:54:57 +01:00
|
|
|
- This parameter is passed as-is to C(manage.py).
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
aliases: [test_runner]
|
2022-10-23 16:33:14 +02:00
|
|
|
ack_venv_creation_deprecation:
|
|
|
|
description:
|
|
|
|
- >-
|
|
|
|
When a I(virtualenv) is set but the virtual environment does not exist, the current behavior is
|
|
|
|
to create a new virtual environment. That behavior is deprecated and if that case happens it will
|
|
|
|
generate a deprecation warning. Set this flag to C(true) to suppress the deprecation warning.
|
|
|
|
- Please note that you will receive no further warning about this being removed until the module
|
|
|
|
will start failing in such cases from community.general 9.0.0 on.
|
|
|
|
type: bool
|
|
|
|
version_added: 5.8.0
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
2022-10-22 08:42:52 +02:00
|
|
|
- >
|
|
|
|
B(ATTENTION - DEPRECATION): Support for Django releases older than 4.1 will be removed in
|
|
|
|
community.general version 9.0.0 (estimated to be released in May 2024).
|
|
|
|
Please notice that Django 4.1 requires Python 3.8 or greater.
|
2021-09-07 06:22:46 +02:00
|
|
|
- C(virtualenv) (U(http://www.virtualenv.org)) must be installed on the remote host if the I(virtualenv) parameter
|
2022-10-23 16:33:14 +02:00
|
|
|
is specified. This requirement is deprecated and will be removed in community.general version 9.0.0.
|
2021-09-07 06:22:46 +02:00
|
|
|
- This module will create a virtualenv if the I(virtualenv) parameter is specified and a virtual environment does not already
|
2022-10-23 16:33:14 +02:00
|
|
|
exist at the given location. This behavior is deprecated and will be removed in community.general version 9.0.0.
|
|
|
|
- The parameter I(virtualenv) will remain in use, but it will require the specified virtualenv to exist.
|
|
|
|
The recommended way to create one in Ansible is by using M(ansible.builtin.pip).
|
2020-10-26 07:54:57 +01:00
|
|
|
- This module assumes English error messages for the C(createcachetable) command to detect table existence,
|
|
|
|
unfortunately.
|
|
|
|
- To be able to use the C(migrate) command with django versions < 1.7, you must have C(south) installed and added
|
|
|
|
as an app in your settings.
|
|
|
|
- To be able to use the C(collectstatic) command, you must have enabled staticfiles in your settings.
|
|
|
|
- Your C(manage.py) application must be executable (rwxr-xr-x), and must have a valid shebang,
|
|
|
|
i.e. C(#!/usr/bin/env python), for invoking the appropriate Python interpreter.
|
2022-10-22 08:42:52 +02:00
|
|
|
seealso:
|
|
|
|
- name: django-admin and manage.py Reference
|
|
|
|
description: Reference for C(django-admin) or C(manage.py) commands.
|
|
|
|
link: https://docs.djangoproject.com/en/4.1/ref/django-admin/
|
|
|
|
- name: Django Download page
|
|
|
|
description: The page showing how to get Django and the timeline of supported releases.
|
|
|
|
link: https://www.djangoproject.com/download/
|
|
|
|
- name: What Python version can I use with Django?
|
|
|
|
description: From the Django FAQ, the response to Python requirements for the framework.
|
|
|
|
link: https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements: [ "virtualenv", "django" ]
|
2022-10-22 08:42:52 +02:00
|
|
|
author:
|
|
|
|
- Alexei Znamensky (@russoz)
|
|
|
|
- Scott Anderson (@tastychutney)
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Run cleanup on the application installed in django_dir
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.django_manage:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: cleanup
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path: "{{ django_dir }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Load the initial_data fixture into the application
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.django_manage:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: loaddata
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path: "{{ django_dir }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
fixtures: "{{ initial_data }}"
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Run syncdb on the application
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.django_manage:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: syncdb
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path: "{{ django_dir }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
settings: "{{ settings_app_name }}"
|
|
|
|
pythonpath: "{{ settings_dir }}"
|
|
|
|
virtualenv: "{{ virtualenv_dir }}"
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Run the SmokeTest test case from the main app. Useful for testing deploys
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.django_manage:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: test
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path: "{{ django_dir }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
apps: main.SmokeTest
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Create an initial superuser
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.django_manage:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: "createsuperuser --noinput --username=admin --email=admin@example.com"
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path: "{{ django_dir }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2021-08-31 07:14:08 +02:00
|
|
|
import shlex
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def _fail(module, cmd, out, err, **kwargs):
|
|
|
|
msg = ''
|
|
|
|
if out:
|
|
|
|
msg += "stdout: %s" % (out, )
|
|
|
|
if err:
|
|
|
|
msg += "\n:stderr: %s" % (err, )
|
|
|
|
module.fail_json(cmd=cmd, msg=msg, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def _ensure_virtualenv(module):
|
|
|
|
|
|
|
|
venv_param = module.params['virtualenv']
|
|
|
|
if venv_param is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
vbin = os.path.join(venv_param, 'bin')
|
|
|
|
activate = os.path.join(vbin, 'activate')
|
|
|
|
|
|
|
|
if not os.path.exists(activate):
|
2022-10-23 16:33:14 +02:00
|
|
|
# In version 9.0.0, if the venv is not found, it should fail_json() here.
|
|
|
|
if not module.params['ack_venv_creation_deprecation']:
|
|
|
|
module.deprecate(
|
|
|
|
'The behavior of "creating the virtual environment when missing" is being '
|
|
|
|
'deprecated and will be removed in community.general version 9.0.0. '
|
|
|
|
'Set the module parameter `ack_venv_creation_deprecation: true` to '
|
|
|
|
'prevent this message from showing up when creating a virtualenv.',
|
|
|
|
version='9.0.0',
|
|
|
|
collection_name='community.general',
|
|
|
|
)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
virtualenv = module.get_bin_path('virtualenv', True)
|
|
|
|
vcmd = [virtualenv, venv_param]
|
|
|
|
rc, out_venv, err_venv = module.run_command(vcmd)
|
|
|
|
if rc != 0:
|
|
|
|
_fail(module, vcmd, out_venv, err_venv)
|
|
|
|
|
|
|
|
os.environ["PATH"] = "%s:%s" % (vbin, os.environ["PATH"])
|
|
|
|
os.environ["VIRTUAL_ENV"] = venv_param
|
|
|
|
|
|
|
|
|
2020-09-29 06:18:34 +02:00
|
|
|
def createcachetable_check_changed(output):
|
|
|
|
return "already exists" not in output
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def flush_filter_output(line):
|
|
|
|
return "Installed" in line and "Installed 0 object" not in line
|
|
|
|
|
|
|
|
|
|
|
|
def loaddata_filter_output(line):
|
|
|
|
return "Installed" in line and "Installed 0 object" not in line
|
|
|
|
|
|
|
|
|
|
|
|
def syncdb_filter_output(line):
|
2020-10-26 07:54:57 +01:00
|
|
|
return ("Creating table " in line) \
|
|
|
|
or ("Installed" in line and "Installed 0 object" not in line)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def migrate_filter_output(line):
|
2020-10-26 07:54:57 +01:00
|
|
|
return ("Migrating forwards " in line) \
|
|
|
|
or ("Installed" in line and "Installed 0 object" not in line) \
|
|
|
|
or ("Applying" in line)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def collectstatic_filter_output(line):
|
|
|
|
return line and "0 static files" not in line
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
command_allowed_param_map = dict(
|
|
|
|
cleanup=(),
|
|
|
|
createcachetable=('cache_table', 'database', ),
|
|
|
|
flush=('database', ),
|
|
|
|
loaddata=('database', 'fixtures', ),
|
|
|
|
syncdb=('database', ),
|
2021-03-09 21:23:20 +01:00
|
|
|
test=('failfast', 'testrunner', 'apps', ),
|
2020-03-09 10:11:07 +01:00
|
|
|
validate=(),
|
|
|
|
migrate=('apps', 'skip', 'merge', 'database',),
|
|
|
|
collectstatic=('clear', 'link', ),
|
|
|
|
)
|
|
|
|
|
|
|
|
command_required_param_map = dict(
|
|
|
|
loaddata=('fixtures', ),
|
|
|
|
)
|
|
|
|
|
|
|
|
# forces --noinput on every command that needs it
|
|
|
|
noinput_commands = (
|
|
|
|
'flush',
|
|
|
|
'syncdb',
|
|
|
|
'migrate',
|
|
|
|
'test',
|
|
|
|
'collectstatic',
|
|
|
|
)
|
|
|
|
|
|
|
|
# These params are allowed for certain commands only
|
2021-03-09 21:23:20 +01:00
|
|
|
specific_params = ('apps', 'clear', 'database', 'failfast', 'fixtures', 'testrunner')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# These params are automatically added to the command if present
|
|
|
|
general_params = ('settings', 'pythonpath', 'database',)
|
|
|
|
specific_boolean_params = ('clear', 'failfast', 'skip', 'merge', 'link')
|
|
|
|
end_of_command_params = ('apps', 'cache_table', 'fixtures')
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2020-10-26 07:54:57 +01:00
|
|
|
command=dict(required=True, type='str'),
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path=dict(required=True, type='path', aliases=['app_path', 'chdir']),
|
2021-07-28 08:22:18 +02:00
|
|
|
settings=dict(type='path'),
|
|
|
|
pythonpath=dict(type='path', aliases=['python_path']),
|
|
|
|
virtualenv=dict(type='path', aliases=['virtual_env']),
|
|
|
|
|
|
|
|
apps=dict(),
|
|
|
|
cache_table=dict(type='str'),
|
|
|
|
clear=dict(default=False, type='bool'),
|
|
|
|
database=dict(type='str'),
|
|
|
|
failfast=dict(default=False, type='bool', aliases=['fail_fast']),
|
|
|
|
fixtures=dict(type='str'),
|
|
|
|
testrunner=dict(type='str', aliases=['test_runner']),
|
|
|
|
skip=dict(type='bool'),
|
|
|
|
merge=dict(type='bool'),
|
|
|
|
link=dict(type='bool'),
|
2022-10-23 16:33:14 +02:00
|
|
|
ack_venv_creation_deprecation=dict(type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-08-31 07:14:08 +02:00
|
|
|
command_split = shlex.split(module.params['command'])
|
|
|
|
command_bin = command_split[0]
|
2020-11-08 23:04:57 +01:00
|
|
|
project_path = module.params['project_path']
|
2020-03-09 10:11:07 +01:00
|
|
|
virtualenv = module.params['virtualenv']
|
|
|
|
|
2022-10-22 08:42:52 +02:00
|
|
|
try:
|
|
|
|
_deprecation = dict(
|
|
|
|
cleanup="clearsessions",
|
|
|
|
syncdb="migrate",
|
|
|
|
validate="check",
|
|
|
|
)
|
|
|
|
module.deprecate(
|
|
|
|
'The command {0} has been deprecated as it is no longer supported in recent Django versions.'
|
|
|
|
'Please use the command {1} instead that provide similar capability.'.format(command_bin, _deprecation[command_bin]),
|
|
|
|
version='9.0.0',
|
|
|
|
collection_name='community.general'
|
|
|
|
)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
for param in specific_params:
|
|
|
|
value = module.params[param]
|
2021-08-31 07:14:08 +02:00
|
|
|
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_bin))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-31 07:14:08 +02:00
|
|
|
for param in command_required_param_map.get(command_bin, ()):
|
2020-03-09 10:11:07 +01:00
|
|
|
if not module.params[param]:
|
2021-08-31 07:14:08 +02:00
|
|
|
module.fail_json(msg='%s param is required for command=%s' % (param, command_bin))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
_ensure_virtualenv(module)
|
|
|
|
|
2021-08-31 07:14:08 +02:00
|
|
|
run_cmd_args = ["./manage.py"] + command_split
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-31 07:14:08 +02:00
|
|
|
if command_bin in noinput_commands and '--noinput' not in command_split:
|
|
|
|
run_cmd_args.append("--noinput")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
for param in general_params:
|
|
|
|
if module.params[param]:
|
2021-08-31 07:14:08 +02:00
|
|
|
run_cmd_args.append('--%s=%s' % (param, module.params[param]))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
for param in specific_boolean_params:
|
2021-07-28 08:22:18 +02:00
|
|
|
if module.params[param]:
|
2021-08-31 07:14:08 +02:00
|
|
|
run_cmd_args.append('--%s' % param)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# these params always get tacked on the end of the command
|
|
|
|
for param in end_of_command_params:
|
|
|
|
if module.params[param]:
|
2021-09-07 06:22:46 +02:00
|
|
|
if param in ('fixtures', 'apps'):
|
|
|
|
run_cmd_args.extend(shlex.split(module.params[param]))
|
|
|
|
else:
|
|
|
|
run_cmd_args.append(module.params[param])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-31 07:14:08 +02:00
|
|
|
rc, out, err = module.run_command(run_cmd_args, cwd=project_path)
|
2020-03-09 10:11:07 +01:00
|
|
|
if rc != 0:
|
2021-08-31 07:14:08 +02:00
|
|
|
if command_bin == 'createcachetable' and 'table' in err and 'already exists' in err:
|
2020-09-29 06:18:34 +02:00
|
|
|
out = 'already exists.'
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
if "Unknown command:" in err:
|
2021-08-31 07:14:08 +02:00
|
|
|
_fail(module, run_cmd_args, err, "Unknown django command: %s" % command_bin)
|
|
|
|
_fail(module, run_cmd_args, out, err, path=os.environ["PATH"], syspath=sys.path)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
lines = out.split('\n')
|
2021-08-31 07:14:08 +02:00
|
|
|
filt = globals().get(command_bin + "_filter_output", None)
|
2020-03-09 10:11:07 +01:00
|
|
|
if filt:
|
|
|
|
filtered_output = list(filter(filt, lines))
|
|
|
|
if len(filtered_output):
|
|
|
|
changed = True
|
2021-08-31 07:14:08 +02:00
|
|
|
check_changed = globals().get("{0}_check_changed".format(command_bin), None)
|
2020-09-29 06:18:34 +02:00
|
|
|
if check_changed:
|
|
|
|
changed = check_changed(out)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-08-31 07:14:08 +02:00
|
|
|
module.exit_json(changed=changed, out=out, cmd=run_cmd_args, app_path=project_path, project_path=project_path,
|
2020-11-08 23:04:57 +01:00
|
|
|
virtualenv=virtualenv, settings=module.params['settings'], pythonpath=module.params['pythonpath'])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|