Skip to content

Commit

Permalink
python_runner/django_command: bugfixes (#8944)
Browse files Browse the repository at this point in the history
* python_runner/django_command: bugfixes

* fix indentation

* join path_prefix with : when concatenating with PATH

* add changelog frag
  • Loading branch information
russoz authored Sep 30, 2024
1 parent ab84f16 commit a7d1b0f
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/8944-django-command-fix.yml
Original file line number Diff line number Diff line change
@@ -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).
6 changes: 4 additions & 2 deletions plugins/module_utils/python_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def __init__(self, module, command, arg_formats=None, default_args_order=(),
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)
Expand Down
5 changes: 5 additions & 0 deletions plugins/modules/django_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
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

Expand All @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/targets/django_command/aliases
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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"]))
Original file line number Diff line number Diff line change
@@ -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),
]
Empty file.
8 changes: 8 additions & 0 deletions tests/integration/targets/django_command/meta/main.yml
Original file line number Diff line number Diff line change
@@ -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
91 changes: 91 additions & 0 deletions tests/integration/targets/django_command/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Test code for django_command module
#
# Copyright (c) 2020, Alexei Znamensky <[email protected]>
# 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"

0 comments on commit a7d1b0f

Please sign in to comment.