Skip to content

Commit

Permalink
Add compat layer to enable using STORAGES
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Dellweg <2500@gmx.de>
  • Loading branch information
pedro-psb and mdellweg committed Jan 9, 2025
1 parent 630c2bd commit 3e3492a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
11 changes: 11 additions & 0 deletions CHANGES/5404.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Marked django's `DEFAULT_FILE_STORAGE` and `STATIC_FILE_STORAGE` settings to be
removed in pulpcore 3.85. Users should upgrade to use the
[`STORAGES`](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES)
setting instead.

The [django-upgrade](https://github.com/adamchainz/django-upgrade?tab=readme-ov-file#django-42)
tool can handle simple cases. If cloud storages are being used, refer to django-storages
to adapt their specific storage options. E.g:

* <https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html>
* <https://django-storages.readthedocs.io/en/latest/backends/azure.html>
12 changes: 11 additions & 1 deletion pulpcore/app/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from importlib import import_module

from django import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import connection, transaction
from django.db.models.signals import post_migrate
Expand Down Expand Up @@ -68,6 +67,15 @@ class PulpPluginAppConfig(apps.AppConfig):

def __init__(self, app_name, app_module):
super().__init__(app_name, app_module)
# begin Compatilibity layer for DEFAULT_FILE_STORAGE deprecation
# Remove on pulpcore=3.85 or pulpcore=4.0
# * Workaround for getting the up-to-date settings instance, otherwise is doesnt
# get the patch in settings.py
# * Update code in signal handlers to use module-level imports again
from django.conf import settings

self.settings = settings
# end

try:
self.version
Expand Down Expand Up @@ -314,6 +322,7 @@ def _populate_system_id(sender, apps, verbosity, **kwargs):


def _ensure_default_domain(sender, **kwargs):
settings = sender.settings
table_names = connection.introspection.table_names()
if "core_domain" in table_names:
from pulpcore.app.util import get_default_domain
Expand Down Expand Up @@ -393,6 +402,7 @@ def _get_permission(perm):


def _populate_artifact_serving_distribution(sender, apps, verbosity, **kwargs):
settings = sender.settings
if (
settings.STORAGES["default"]["BACKEND"] == "pulpcore.app.models.storage.FileSystem"
or not settings.REDIRECT_TO_OBJECT_STORAGE
Expand Down
48 changes: 40 additions & 8 deletions pulpcore/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from pathlib import Path

from cryptography.fernet import Fernet
from django.core.files.storage import storages
from django.conf import global_settings
from django.core.exceptions import ImproperlyConfigured
from django.db import connection

Expand Down Expand Up @@ -56,7 +58,28 @@
STATIC_URL = "/assets/"
STATIC_ROOT = DEPLOY_ROOT / STATIC_URL.strip("/")

DEFAULT_FILE_STORAGE = "pulpcore.app.models.storage.FileSystem"
# begin compatilibity layer for DEFAULT_FILE_STORAGE
# Remove on pulpcore=3.85 or pulpcore=4.0

# - What is this?
# We shouldnt use STORAGES or DEFAULT_FILE_STORAGE directly because those are
# mutually exclusive by django, which constraints users to use whatever we use.
# This is a hack/workaround to set Pulp's default while still enabling users to choose
# the legacy or the new storage setting.
_DEFAULT_FILE_STORAGE = "pulpcore.app.models.storage.FileSystem"
_STORAGES = {
"default": {
"BACKEND": "pulpcore.app.models.storage.FileSystem",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}

setattr(global_settings, "DEFAULT_FILE_STORAGE", _DEFAULT_FILE_STORAGE)
setattr(global_settings, "STORAGES", _STORAGES)
# end DEFAULT_FILE_STORAGE deprecation layer

REDIRECT_TO_OBJECT_STORAGE = True

WORKING_DIRECTORY = DEPLOY_ROOT / "tmp"
Expand Down Expand Up @@ -371,16 +394,18 @@
from dynaconf import DjangoDynaconf, Validator # noqa

# Validators
storage_keys = ("STORAGES.default.BACKEND", "DEFAULT_FILE_STORAGE")
storage_validator = (
Validator("REDIRECT_TO_OBJECT_STORAGE", eq=False)
| Validator("DEFAULT_FILE_STORAGE", eq="pulpcore.app.models.storage.FileSystem")
| Validator("DEFAULT_FILE_STORAGE", eq="storages.backends.azure_storage.AzureStorage")
| Validator("DEFAULT_FILE_STORAGE", eq="storages.backends.s3boto3.S3Boto3Storage")
| Validator("DEFAULT_FILE_STORAGE", eq="storages.backends.gcloud.GoogleCloudStorage")
| Validator(*storage_keys, eq="pulpcore.app.models.storage.FileSystem")
| Validator(*storage_keys, eq="storages.backends.azure_storage.AzureStorage")
| Validator(*storage_keys, eq="storages.backends.s3boto3.S3Boto3Storage")
| Validator(*storage_keys, eq="storages.backends.gcloud.GoogleCloudStorage")
)
storage_validator.messages["combined"] = (
"'REDIRECT_TO_OBJECT_STORAGE=True' is only supported with the local file, S3, GCP or Azure"
"storage backend configured in DEFAULT_FILE_STORAGE."
"'REDIRECT_TO_OBJECT_STORAGE=True' is only supported with the local file, S3, GCP or Azure "
"storage backend configured in STORAGES['default']['BACKEND'] "
"(deprecated DEFAULT_FILE_STORAGE)."
)

cache_enabled_validator = Validator("CACHE_ENABLED", eq=True)
Expand Down Expand Up @@ -485,7 +510,14 @@ def otel_middleware_hook(settings):
],
post_hooks=otel_middleware_hook,
)
# HERE ENDS DYNACONF EXTENSION LOAD (No more code below this line)

# begin compatilibity layer for DEFAULT_FILE_STORAGE
# Remove on pulpcore=3.85 or pulpcore=4.0

# Ensures the cached property storage.backends uses the the right value
storages._backends = settings.STORAGES.copy()
storages.backends
# end compatibility layer

_logger = getLogger(__name__)

Expand Down

0 comments on commit 3e3492a

Please sign in to comment.