diff --git a/changelogs/fragments/8944-django-command-fix.yml b/changelogs/fragments/8944-django-command-fix.yml new file mode 100644 index 0000000000..755bf5628a --- /dev/null +++ b/changelogs/fragments/8944-django-command-fix.yml @@ -0,0 +1,3 @@ +bugfixes: + - python_runner module utils - parameter ``path_prefix`` was being handled as string when it should be a list (https://github.com/ansible-collections/community.general/pull/8944). + - django_command - option ``command`` is now split lexically before passed to underlying PythonRunner (https://github.com/ansible-collections/community.general/pull/8944). diff --git a/plugins/module_utils/python_runner.py b/plugins/module_utils/python_runner.py index f678f247b4..b65867c61e 100644 --- a/plugins/module_utils/python_runner.py +++ b/plugins/module_utils/python_runner.py @@ -22,10 +22,12 @@ class PythonRunner(CmdRunner): if (os.path.isabs(python) or '/' in python): self.python = python elif self.has_venv: - path_prefix = os.path.join(venv, "bin") + if path_prefix is None: + path_prefix = [] + path_prefix.append(os.path.join(venv, "bin")) if environ_update is None: environ_update = {} - environ_update["PATH"] = "%s:%s" % (path_prefix, os.environ["PATH"]) + environ_update["PATH"] = "%s:%s" % (":".join(path_prefix), os.environ["PATH"]) environ_update["VIRTUAL_ENV"] = venv python_cmd = [self.python] + _ensure_list(command) diff --git a/plugins/modules/django_command.py b/plugins/modules/django_command.py index 788f4a100e..dcb8d26313 100644 --- a/plugins/modules/django_command.py +++ b/plugins/modules/django_command.py @@ -57,6 +57,8 @@ run_info: returned: success and O(verbosity) >= 3 """ +import shlex + from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper from ansible_collections.community.general.plugins.module_utils.cmd_runner import cmd_runner_fmt @@ -74,6 +76,9 @@ class DjangoCommand(DjangoModuleHelper): ) django_admin_arg_order = "extra_args" + def __init_module__(self): + self.vars.command = shlex.split(self.vars.command) + def main(): DjangoCommand.execute() diff --git a/tests/integration/targets/django_command/aliases b/tests/integration/targets/django_command/aliases new file mode 100644 index 0000000000..ae3c2623a0 --- /dev/null +++ b/tests/integration/targets/django_command/aliases @@ -0,0 +1,21 @@ +# Copyright (c) Ansible Project +# 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 + +azp/posix/2 +skip/python2 +skip/freebsd +skip/macos +skip/osx +skip/rhel8.2 +skip/rhel8.3 +skip/rhel8.4 +skip/rhel8.5 +skip/rhel8.6 +skip/rhel8.7 +skip/rhel8.8 +skip/rhel9.0 +skip/rhel9.1 +skip/rhel9.2 +skip/rhel9.3 +skip/rhel9.4 diff --git a/tests/integration/targets/django_command/files/base_test/1045-single-app-project/single_app_project/core/settings.py b/tests/integration/targets/django_command/files/base_test/1045-single-app-project/single_app_project/core/settings.py new file mode 100644 index 0000000000..881221c066 --- /dev/null +++ b/tests/integration/targets/django_command/files/base_test/1045-single-app-project/single_app_project/core/settings.py @@ -0,0 +1,6 @@ +# Copyright (c) Ansible Project +# 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 + +# single_app_project/core/settings.py +SECRET_KEY = 'testtesttesttesttest' diff --git a/tests/integration/targets/django_command/files/base_test/1045-single-app-project/single_app_project/manage.py b/tests/integration/targets/django_command/files/base_test/1045-single-app-project/single_app_project/manage.py new file mode 100755 index 0000000000..4b4eddcb67 --- /dev/null +++ b/tests/integration/targets/django_command/files/base_test/1045-single-app-project/single_app_project/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# Copyright (c) Ansible Project +# 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 + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +# single_app_project/manage.py +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'single_app_project.core.settings') + from django.core.management import execute_from_command_line + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/tests/integration/targets/django_command/files/base_test/simple_project/p1/manage.py b/tests/integration/targets/django_command/files/base_test/simple_project/p1/manage.py new file mode 100755 index 0000000000..be3140f44d --- /dev/null +++ b/tests/integration/targets/django_command/files/base_test/simple_project/p1/manage.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# Copyright (c) Ansible Project +# 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 + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'p1.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/tests/integration/targets/django_command/files/base_test/simple_project/p1/p1/settings.py b/tests/integration/targets/django_command/files/base_test/simple_project/p1/p1/settings.py new file mode 100644 index 0000000000..86b3ae64c6 --- /dev/null +++ b/tests/integration/targets/django_command/files/base_test/simple_project/p1/p1/settings.py @@ -0,0 +1,133 @@ +# -*- coding: utf-8 -*- +# Copyright (c) Ansible Project +# 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 + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +""" +Django settings for p1 project. + +Generated by 'django-admin startproj' using Django 3.1.5. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +import os +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '%g@gyhl*q@@g(_ab@t^76dao^#b9-v8mw^50)x_bv6wpl+mukj' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'p1.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'p1.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = '/static/' +STATIC_ROOT = '/tmp/django-static' + +if "DJANGO_ANSIBLE_RAISE" in os.environ: + raise ValueError("DJANGO_ANSIBLE_RAISE={0}".format(os.environ["DJANGO_ANSIBLE_RAISE"])) diff --git a/tests/integration/targets/django_command/files/base_test/simple_project/p1/p1/urls.py b/tests/integration/targets/django_command/files/base_test/simple_project/p1/p1/urls.py new file mode 100644 index 0000000000..36cb592756 --- /dev/null +++ b/tests/integration/targets/django_command/files/base_test/simple_project/p1/p1/urls.py @@ -0,0 +1,28 @@ +# Copyright (c) Ansible Project +# 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 + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +"""p1 URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/tests/integration/targets/django_command/files/base_test/startproj/.keep b/tests/integration/targets/django_command/files/base_test/startproj/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/targets/django_command/meta/main.yml b/tests/integration/targets/django_command/meta/main.yml new file mode 100644 index 0000000000..4a216308a2 --- /dev/null +++ b/tests/integration/targets/django_command/meta/main.yml @@ -0,0 +1,8 @@ +--- +# Copyright (c) Ansible Project +# 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 + +dependencies: + - setup_pkg_mgr + - setup_os_pkg_name diff --git a/tests/integration/targets/django_command/tasks/main.yaml b/tests/integration/targets/django_command/tasks/main.yaml new file mode 100644 index 0000000000..9d052dc44f --- /dev/null +++ b/tests/integration/targets/django_command/tasks/main.yaml @@ -0,0 +1,91 @@ +# Test code for django_command module +# +# Copyright (c) 2020, Alexei Znamensky +# 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 +- name: Create temporary test directory + tempfile: + state: directory + suffix: .django_command + register: tmp_django_root + +- name: Install OS package virtualenv + package: + name: "{{ os_package_name.virtualenv }}" + state: present + +- name: Ensure virtualenv is created + command: >- + virtualenv {{ tmp_django_root.path }}/venv + +- name: Update python package pip + pip: + name: pip + state: latest + virtualenv: "{{ tmp_django_root.path }}/venv" + +- name: Install python package django + pip: + name: django + state: present + virtualenv: "{{ tmp_django_root.path }}/venv" + +- name: Copy files + copy: + src: base_test/ + dest: "{{ tmp_django_root.path }}" + mode: preserve + +- name: Create project + command: + chdir: "{{ tmp_django_root.path }}/startproj" + cmd: "{{ tmp_django_root.path }}/venv/bin/django-admin startproject test_django_command_1" + +- name: Create app + command: + chdir: "{{ tmp_django_root.path }}/startproj" + cmd: "{{ tmp_django_root.path }}/venv/bin/django-admin startapp app1" + +- name: Check + community.general.django_command: + pythonpath: "{{ tmp_django_root.path }}/startproj/test_django_command_1" + settings: test_django_command_1.settings + command: check + venv: "{{ tmp_django_root.path }}/venv" + +- name: Check simple_project + community.general.django_command: + pythonpath: "{{ tmp_django_root.path }}/simple_project/p1" + settings: p1.settings + command: check + venv: "{{ tmp_django_root.path }}/venv" + +- name: Check custom project + community.general.django_command: + pythonpath: "{{ tmp_django_root.path }}/1045-single-app-project/single_app_project" + settings: core.settings + command: check + venv: "{{ tmp_django_root.path }}/venv" + +- name: Run collectstatic --noinput on simple project + community.general.django_command: + pythonpath: "{{ tmp_django_root.path }}/simple_project/p1" + settings: p1.settings + command: collectstatic --noinput + venv: "{{ tmp_django_root.path }}/venv" + +- name: Trigger exception with environment variable + community.general.django_command: + pythonpath: "{{ tmp_django_root.path }}/simple_project/p1" + settings: p1.settings + command: collectstatic --noinput + venv: "{{ tmp_django_root.path }}/venv" + environment: + DJANGO_ANSIBLE_RAISE: blah + ignore_errors: true + register: env_raise + +- name: Check env variable reached manage.py + ansible.builtin.assert: + that: + - "'ValueError: DJANGO_ANSIBLE_RAISE=blah' in env_raise.msg"