diff --git a/Dockerfile b/Dockerfile index 664d2e7..dc6d453 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,5 +14,4 @@ ADD . /code/ RUN python -m pip install gunicorn -RUN python manage.py collectstatic --no-input --clear -CMD bash -c "python manage.py migrate && gunicorn --workers 1 --threads 4 -b 0.0.0.0:8000 --capture-output --error-logfile - --access-logfile - spejstore.wsgi:application" +CMD bash -c "python manage.py collectstatic --no-input --clear && python manage.py migrate && gunicorn --workers 1 --threads 4 -b 0.0.0.0:8000 --capture-output --error-logfile - --access-logfile - spejstore.wsgi:application" diff --git a/docker-compose.yml b/docker-compose.yml index 7f31116..6381fc3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,7 +17,7 @@ services: web: build: . restart: always - command: bash -c "python manage.py migrate && gunicorn --workers 1 --threads 4 -b 0.0.0.0:8000 --capture-output --error-logfile - --access-logfile - spejstore.wsgi:application" + command: bash -c "python manage.py collectstatic --no-input --clear && python manage.py migrate && gunicorn --workers 1 --threads 4 -b 0.0.0.0:8000 --capture-output --error-logfile - --access-logfile - spejstore.wsgi:application" volumes: - .:/code - /code/build_static diff --git a/requirements.txt b/requirements.txt index c05e442..ffbe938 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,7 @@ django-appconf==1.0.5 django-hstore==1.4.2 django-markdown2==0.3.1 django-select2==8.1.2 +django-storages[s3]==1.14.2 # Django-tree https://github.com/Palid/django-tree/archive/master.zip djangorestframework==3.14.0 diff --git a/spejstore/settings.py b/spejstore/settings.py index a1bd2a1..e2ab05b 100644 --- a/spejstore/settings.py +++ b/spejstore/settings.py @@ -51,6 +51,7 @@ INSTALLED_APPS = [ "django.contrib.messages", "django.contrib.staticfiles", "django.contrib.postgres", + "storages", # django-storages s3boto support "social_django", "tree", "django_select2", @@ -168,14 +169,52 @@ SOCIAL_AUTH_PIPELINE = ( "auth.pipeline.staff_me_up", ) -STORAGES = { - "default": { - "BACKEND": "django.core.files.storage.FileSystemStorage", - }, - "staticfiles": { - "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", - }, -} +# Determines the storage type for Django static files and media. +FILE_STORAGE_TYPE = env("FILE_STORAGE_TYPE", "filesystem") +if FILE_STORAGE_TYPE == "filesystem": + STORAGES = { + "default": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + "staticfiles": { + "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage", + }, + } +elif FILE_STORAGE_TYPE == "s3": + S3_BUCKET_NAME = env("S3_BUCKET_NAME", "inventory") + S3_ENDPOINT_URL = env("S3_ENDPOINT_URL", "https://object.ceph-eu.hswaw.net") + S3_DOMAIN_NAME = env("S3_DOMAIN_NAME", "object.ceph-eu.hswaw.net") + S3_ACCESS_KEY = env("S3_ACCESS_KEY", "") + S3_SECRET_KEY = env("S3_SECRET_KEY", "=") + S3_STATIC_LOCATION = "static" + S3_MEDIA_LOCATION = "media" + STORAGES = { + "default": { + "BACKEND": "storages.backends.s3.S3Storage", + "OPTIONS": { + "access_key": S3_ACCESS_KEY, + "secret_key": S3_SECRET_KEY, + "endpoint_url": S3_ENDPOINT_URL, + "bucket_name": S3_BUCKET_NAME, + "default_acl": "public-read", + "location": S3_MEDIA_LOCATION, + "custom_domain": f"{S3_DOMAIN_NAME}/{S3_BUCKET_NAME}", + "file_overwrite": False, + }, + }, + "staticfiles": { + "BACKEND": "storages.backends.s3.S3Storage", + "OPTIONS": { + "access_key": S3_ACCESS_KEY, + "secret_key": S3_SECRET_KEY, + "endpoint_url": S3_ENDPOINT_URL, + "bucket_name": S3_BUCKET_NAME, + "default_acl": "public-read", + "location": S3_STATIC_LOCATION, + "custom_domain": f"{S3_DOMAIN_NAME}/{S3_BUCKET_NAME}", + }, + }, + } # Internationalization # https://docs.djangoproject.com/en/1.10/topics/i18n/