add media files to production compose

This commit is contained in:
godd0t 2023-05-10 13:19:19 +02:00
parent 1ae2664984
commit 70a802350a
25 changed files with 129 additions and 123 deletions

View File

@ -4,10 +4,10 @@
if [ "$APP_ENV" != "prod" ]; then
python manage.py makemigrations --noinput
python manage.py migrate --noinput
python manage.py runserver "$APP_HOST":"$APP_PORT"
else
python manage.py makemigrations --noinput
python manage.py migrate --noinput
python manage.py collectstatic --noinput
fi
gunicorn "$APP_NAME".wsgi:application --bind "$APP_HOST":"$APP_PORT" --workers 3 --log-level=debug
fi

View File

@ -14,6 +14,7 @@ services:
- ./src:/usr/src/app/
- ./deployment/scripts:/app/deployment/scripts/
- static_files:/usr/src/app/static
- media_files:/usr/src/app/media
labels:
- "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-backend.rule=Host(`${APP_DOMAIN}`)"
@ -21,6 +22,8 @@ services:
- "traefik.http.services.${APP_NAME}-backend.loadbalancer.server.port=${APP_PORT}"
- "traefik.http.routers.${APP_NAME}-backend.tls.certresolver=letsencrypt"
env_file: .env
expose:
- "${APP_PORT:-8000}"
depends_on:
- db
- redis
@ -33,8 +36,8 @@ services:
volumes:
- postgres_data_dir:/var/lib/postgresql/data/
env_file: .env
ports:
- "5432:5432"
expose:
- "${POSTGRES_PORT:-5432}"
shm_size: 1g
@ -70,9 +73,10 @@ services:
volumes:
- ./deployment/scripts/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- static_files:/usr/src/app/static
- media_files:/usr/src/app/media
labels:
- "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-nginx.rule=Host(`${APP_HOST}`) && PathPrefix(`/static`)"
- "traefik.http.routers.${APP_NAME}-nginx.rule=Host(`${APP_HOST}`) && (PathPrefix(`/static`) || PathPrefix(`/media`))"
- "traefik.http.routers.${APP_NAME}-nginx.entrypoints=web"
- "traefik.http.services.${APP_NAME}-nginx.loadbalancer.server.port=80"
depends_on:
@ -101,6 +105,7 @@ services:
volumes:
static_files:
media_files:
postgres_data_dir:
redis_data:
letsencrypt:

View File

@ -13,13 +13,9 @@ services:
volumes:
- ./src:/usr/src/app/
- ./deployment/scripts:/app/deployment/scripts/
- static_files:/usr/src/app/static
labels:
- "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-backend.rule=Host(`${APP_HOST}`)"
- "traefik.http.routers.${APP_NAME}-backend.entrypoints=web"
- "traefik.http.services.${APP_NAME}-backend.loadbalancer.server.port=${APP_PORT}"
env_file: .env
ports:
- "${APP_PORT}:${APP_PORT}"
depends_on:
- db
- redis
@ -32,8 +28,8 @@ services:
volumes:
- postgres_data_dir:/var/lib/postgresql/data/
env_file: .env
ports:
- "5432:5432"
expose:
- "${POSTGRES_PORT:-5432}"
shm_size: 1g
@ -63,41 +59,6 @@ services:
container_name: "${APP_NAME}-celery-beat"
command: [ "/bin/sh", "/app/deployment/scripts/celery/start-beat.sh" ]
nginx:
image: nginx:latest
container_name: "${APP_NAME}-nginx"
volumes:
- ./deployment/scripts/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- static_files:/usr/src/app/static
labels:
- "traefik.enable=true"
- "traefik.http.routers.${APP_NAME}-nginx.rule=Host(`${APP_HOST}`) && PathPrefix(`/static`)"
- "traefik.http.routers.${APP_NAME}-nginx.entrypoints=web"
- "traefik.http.services.${APP_NAME}-nginx.loadbalancer.server.port=80"
depends_on:
- backend
traefik:
image: traefik:v2.5
container_name: "${APP_NAME}-traefik"
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
networks:
- default
depends_on:
- db
- backend
- redis
volumes:
postgres_data_dir:
redis_data:
static_files:

View File

@ -13,7 +13,11 @@ testpaths = [
]
pythonpath = [".", "src"]
python_files = "tests.py test_*.py *_tests.py"
DJANGO_SETTINGS_MODULE = "conf.settings.test"
DJANGO_SETTINGS_MODULE = "project_name.settings.test"
filterwarnings = [
'ignore::DeprecationWarning:kombu.*:',
'ignore::DeprecationWarning:celery.*:',
]
[tool.coverage.report]
fail_under = 85
@ -58,6 +62,7 @@ extend-exclude = '''
[tool.ruff]
format = "grouped"
line-length = 88 # black default
extend-exclude = [
"*/migrations/*",

View File

@ -2,5 +2,5 @@
APP_PATH="src"
black $APP_PATH
ruff $APP_PATH --fix
black $APP_PATH

View File

@ -2,5 +2,5 @@
APP_PATH="src"
black $APP_PATH --check
ruff $APP_PATH
black $APP_PATH --check

View File

@ -6,7 +6,7 @@ import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
@ -18,5 +18,5 @@ def main():
execute_from_command_line(sys.argv)
if __name__ == '__main__':
if __name__ == "__main__":
main()

View File

@ -2,4 +2,4 @@
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)
__all__ = ("celery_app",)

View File

@ -11,6 +11,6 @@ import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
application = get_asgi_application()

View File

@ -1,17 +1,18 @@
import os
from celery import Celery
from celery.schedules import crontab
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.dev')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.dev")
app = Celery('project_name')
app = Celery("project_name")
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django apps.
app.autodiscover_tasks()
@ -19,4 +20,15 @@ app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
print(f"Request: {self.request!r}")
app.conf.beat_schedule = {
"delete_job_files": {
"task": "test_periodic_task",
# Every 1 minute for testing purposes
"schedule": crontab(minute="*/1"),
},
}
app.conf.timezone = "UTC"

View File

@ -6,7 +6,9 @@ from django.core.management.utils import get_random_secret_key
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
SECRET_KEY = os_getenv("SECRET_KEY", get_random_secret_key()) # If SECRET_KEY is not set, generate a random one
SECRET_KEY = os_getenv(
"SECRET_KEY", get_random_secret_key()
) # If SECRET_KEY is not set, generate a random one
APP_ENV = os_getenv("APP_ENV", "dev")
DEBUG = os_getenv("DEBUG", "true").lower() in ["True", "true", "1", "yes", "y"]
@ -16,61 +18,65 @@ ALLOWED_HOSTS = os_getenv("ALLOWED_HOSTS", "localhost").split(",")
if DEBUG:
CORS_ORIGIN_ALLOW_ALL = True
else:
CSRF_TRUSTED_ORIGINS = os_getenv("CSRF_TRUSTED_ORIGINS", "http://localhost").split(",")
CORS_ALLOWED_ORIGINS = os_getenv("CORS_ALLOWED_ORIGINS", "http://localhost").split(",")
CSRF_TRUSTED_ORIGINS = os_getenv("CSRF_TRUSTED_ORIGINS", "http://localhost").split(
","
)
CORS_ALLOWED_ORIGINS = os_getenv("CORS_ALLOWED_ORIGINS", "http://localhost").split(
","
)
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"corsheaders",
"test_app",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
'django.contrib.sessions.middleware.SessionMiddleware',
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware", # CorsMiddleware should be placed as high as possible,
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = 'project_name.urls'
ROOT_URLCONF = "project_name.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',
"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 = 'project_name.wsgi.application'
WSGI_APPLICATION = "project_name.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
"default": {
"ENGINE": "django.db.backends.postgresql",
'NAME': os_getenv("POSTGRES_DB", "postgres"),
"NAME": os_getenv("POSTGRES_DB", "postgres"),
"USER": os_getenv("POSTGRES_USER", "postgres"),
"PASSWORD": os_getenv("POSTGRES_PASSWORD", "postgres"),
"HOST": os_getenv("POSTGRES_HOST", "db"),
@ -83,25 +89,25 @@ DATABASES = {
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"
TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"
USE_I18N = True
@ -110,15 +116,15 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = os_path.join(BASE_DIR, 'static')
MEDIA_URL = 'media/'
MEDIA_ROOT = os_path.join(BASE_DIR, 'media')
STATIC_URL = "static/"
STATIC_ROOT = os_path.join(BASE_DIR, "static")
MEDIA_URL = "media/"
MEDIA_ROOT = os_path.join(BASE_DIR, "media")
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# Redis
REDIS_DB_KEYS = {

View File

@ -1 +1,9 @@
from .base import * # noqa
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}

View File

@ -5,7 +5,7 @@ from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("admin/", admin.site.urls),
path("test_app/", include("test_app.urls")),
]

View File

@ -11,6 +11,6 @@ import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.dev')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.dev")
application = get_wsgi_application()

View File

@ -1,3 +1,8 @@
from django.contrib import admin # noqa
from django.contrib import admin
# Register your models here.
from test_app.models import TestModel
@admin.register(TestModel)
class TestModelAdmin(admin.ModelAdmin):
list_display = ("name", "description")

View File

@ -2,5 +2,5 @@ from django.apps import AppConfig
class TestAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'test_app'
default_auto_field = "django.db.models.BigAutoField"
name = "test_app"

View File

@ -1,3 +1,10 @@
from django.db import models # noqa
# Create your models here.
class TestModel(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
file = models.FileField(upload_to="test_app/files/")
def __str__(self):
return self.name

6
src/test_app/tasks.py Normal file
View File

@ -0,0 +1,6 @@
from project_name.celery import app
@app.task(bind=True, name="test_periodic_task")
def test_periodic_task(self): # noqa: Adding self since we are using bind=True
print("Hello from periodic task")

View File

@ -1,3 +0,0 @@
from django.test import TestCase # noqa
# Create your tests here.

View File

@ -1,3 +1 @@
from django.shortcuts import render # noqa
# Create your views here.

View File

@ -13,7 +13,5 @@ def enable_db_access_for_all_tests(db):
@pytest.fixture
def test_user():
return User.objects.create_user(
username='test_user',
email="test_user@test.com",
password="test_password"
username="test_user", email="test_user@test.com", password="test_password"
)

View File

@ -1,5 +1,3 @@
def test_example(test_user):
assert test_user.username == 'test_user'
assert test_user.username == "test_user"
assert test_user.email is not None