From 56afeb86c15d94af0ccc70c721784b0696d49908 Mon Sep 17 00:00:00 2001 From: Norman Gilmore Date: Mon, 13 Mar 2017 16:25:40 -0700 Subject: [PATCH] Move Django static files from host directory to container. (#75) Set file permissions explicitly when ./manage.py collectstatic runs. --- .gitignore | 2 +- init_django.sh | 6 ++++-- init_thresher.sh | 3 ++- thresher_backend/settings.py | 3 ++- thresher_backend/storage.py | 9 +++++++++ 5 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 thresher_backend/storage.py diff --git a/.gitignore b/.gitignore index 73e365f..c0796dd 100644 --- a/.gitignore +++ b/.gitignore @@ -29,7 +29,7 @@ __pycache__/ /thresher/bin/ # django artifacts -staticfiles +/staticfiles/ _postgres_data # gunicorn artifacts diff --git a/init_django.sh b/init_django.sh index 64db35b..00c12e4 100755 --- a/init_django.sh +++ b/init_django.sh @@ -1,3 +1,5 @@ #!/bin/bash -docker-compose run thresher_api sh /home/thresher/docker/thresher_api/init_django.sh -docker-compose run thresher_api sh /home/thresher/docker/thresher_api/init_users.sh +# Must use 'exec' since ./manage.py collectstatic modifies the container +docker-compose exec thresher_api sh /home/thresher/docker/thresher_api/init_django.sh +# DB updates can use either 'run' or 'exec' +docker-compose exec thresher_api sh /home/thresher/docker/thresher_api/init_users.sh diff --git a/init_thresher.sh b/init_thresher.sh index 235ac90..cb98f02 100755 --- a/init_thresher.sh +++ b/init_thresher.sh @@ -1,2 +1,3 @@ #!/bin/bash -docker-compose run thresher_api sh /home/thresher/docker/thresher_api/init_all.sh +# Must use 'exec' since ./manage.py collectstatic modifies the container +docker-compose exec thresher_api sh /home/thresher/docker/thresher_api/init_all.sh diff --git a/thresher_backend/settings.py b/thresher_backend/settings.py index 04e0dc6..a89988f 100644 --- a/thresher_backend/settings.py +++ b/thresher_backend/settings.py @@ -147,7 +147,8 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.10/howto/static-files/ -STATIC_ROOT = 'staticfiles' +STATICFILES_STORAGE='thresher_backend.storage.TTStaticFilesStorage' +STATIC_ROOT = '/var/www/staticfiles' STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "vendor") diff --git a/thresher_backend/storage.py b/thresher_backend/storage.py new file mode 100644 index 0000000..0ded2ca --- /dev/null +++ b/thresher_backend/storage.py @@ -0,0 +1,9 @@ +from django.contrib.staticfiles import storage + +# Configure the permissions used by ./manage.py collectstatic +# See https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/ +class TTStaticFilesStorage(storage.StaticFilesStorage): + def __init__(self, *args, **kwargs): + kwargs['file_permissions_mode'] = 0o644 + kwargs['directory_permissions_mode'] = 0o755 + super(TTStaticFilesStorage, self).__init__(*args, **kwargs)