Skip to content
Snippets Groups Projects
Commit cac16471 authored by florimondmanca's avatar florimondmanca
Browse files

migrate to PostgreSQL database, update requirements.txt, move setup to...

migrate to PostgreSQL database, update requirements.txt, move setup to start.sh, activate gunicorn hot reload
parent 567a5d2c
No related branches found
No related tags found
No related merge requests found
...@@ -11,5 +11,6 @@ script: ...@@ -11,5 +11,6 @@ script:
# Check that docker images are running # Check that docker images are running
- docker ps | grep nginx_01 - docker ps | grep nginx_01
- docker ps | grep django_01 - docker ps | grep django_01
- docker ps | grep ps_01
# Run the tests inside the Django container # Run the tests inside the Django container
- docker exec django_01 python manage.py test - docker exec django_01 python manage.py test
...@@ -4,20 +4,13 @@ version: '3' ...@@ -4,20 +4,13 @@ version: '3'
volumes: volumes:
static: static:
media: media:
data: pgdata:
services: services:
# Django web app on Gunicorn WSGI server # NOTE: having volumes static and media both defined in nginx and
gunicorn: # gunicorn allows them to share the same data, i.e. it allows
restart: always # Nginx to access static and media files in the Django project's folder.
build: ./oser-backend
container_name: django_01
volumes:
- data:/oser-backend/
# Mount static and media file volumes
- static:/oser-backend/static
- media:/oser-backend/media
# Nginx server # Nginx server
nginx: nginx:
...@@ -28,7 +21,35 @@ services: ...@@ -28,7 +21,35 @@ services:
- 8000:8000 - 8000:8000
volumes: volumes:
- ./nginx/:/etc/nginx/conf.d - ./nginx/:/etc/nginx/conf.d
- static:/static/ - static:/static/ # access static files
- media:/media/ - media:/media/ # access media files
depends_on: depends_on:
# start Django container before this one
- gunicorn - gunicorn
# Django web app on Gunicorn WSGI server
gunicorn:
restart: always
build: ./oser-backend
container_name: django_01
volumes:
# Sync local project folder to container's, allowing
# to see effects of code changes without need to rebuild image.
- ./oser-backend/:/oser-backend/
# Mount static and media file volumes
- static:/oser-backend/static # expose static files
- media:/oser-backend/media # expose media files
depends_on:
# start db container before this one
- db
# PostgreSQL database
db:
# default username/password is postgres/postgres
restart: always
image: postgres:9.6
container_name: ps_01
expose:
- "5432"
volumes:
- pgdata:/var/lib/postgresql/data/
upstream web {
ip_hash;
server gunicorn:8000; # match the Django service in docker-compose.yml
}
server { server {
server_name oser-cs.fr; server_name oser-cs.fr;
listen 8000; listen 8000;
location /static/ { location /static/ {
# Serve static files collected by Django's collectstatic command, # Serve static files
# located in the static folder.
autoindex on; autoindex on;
alias /static/; alias /static/;
} }
location /media/ { location /media/ {
# Serve user uploaded media, uploaded to the media folder. # Serve user uploaded media
autoindex on; autoindex on;
alias /media/; alias /media/;
} }
location / { location / {
# Pass requests to the Gunicorn server running the Django app # Pass requests to the Gunicorn server running the Django app
proxy_pass http://gunicorn:8000; # (upstream web is defined at top of file)
proxy_pass http://web;
# Send full host in header (default is service name, i.e. gunicorn). # Send full host in header (default is service name, i.e. gunicorn).
# Ensures that absolute URLs on Django app are accessible (e.g. in # Ensures that absolute URLs on Django app are accessible (e.g. in
# discoverable API). # discoverable API).
......
...@@ -13,9 +13,4 @@ RUN ls ...@@ -13,9 +13,4 @@ RUN ls
RUN pip3 install -r ../requirements.txt RUN pip3 install -r ../requirements.txt
RUN pip3 install gunicorn RUN pip3 install gunicorn
# Setup database
RUN python3 manage.py makemigrations
RUN python3 manage.py migrate
RUN python3 manage.py initadmin
CMD sh ../start.sh CMD sh ../start.sh
...@@ -118,7 +118,7 @@ REST_FRAMEWORK = { ...@@ -118,7 +118,7 @@ REST_FRAMEWORK = {
} }
# Pymdown-extensions Emoji configuration # Pymdown-extensions Emoji configuration
import pymdownx.emoji import pymdownx.emoji # noqa
extension_configs = { extension_configs = {
'emoji_index': pymdownx.emoji.twemoji, 'emoji_index': pymdownx.emoji.twemoji,
...@@ -146,10 +146,19 @@ MARKDOWNX_MARKDOWN_EXTENSION_CONFIGS = { ...@@ -146,10 +146,19 @@ MARKDOWNX_MARKDOWN_EXTENSION_CONFIGS = {
# Database # Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases # https://docs.djangoproject.com/en/1.11/ref/settings/#databases
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
} }
} }
...@@ -193,9 +202,6 @@ USE_TZ = True ...@@ -193,9 +202,6 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.0/howto/static-files/ # https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(dn(BASE_DIR), 'static') STATIC_ROOT = os.path.join(dn(BASE_DIR), 'static')
# User-uploaded media files # User-uploaded media files
......
"""Users app system checks.""" """Users app system checks."""
from django.db.utils import OperationalError from django.db.utils import OperationalError, ProgrammingError
from django.core.checks import Warning, Info, Error, register, Tags from django.core.checks import Warning, Info, Error, register, Tags
from users.permissions import setup_groups from users.permissions import setup_groups
...@@ -27,7 +27,7 @@ def check_groups(app_configs, **kwargs): ...@@ -27,7 +27,7 @@ def check_groups(app_configs, **kwargs):
'Unexpected error: ' + str(e), 'Unexpected error: ' + str(e),
id='users.E001', id='users.E001',
)) ))
except OperationalError: except (OperationalError, ProgrammingError):
errors.append(Warning( errors.append(Warning(
'Could not create groups', 'Could not create groups',
hint=( hint=(
......
...@@ -9,3 +9,4 @@ dry_rest_permissions>~0.1.10 ...@@ -9,3 +9,4 @@ dry_rest_permissions>~0.1.10
django-guardian>~1.4.9 django-guardian>~1.4.9
django-admin-sortable2>=0.6.19 django-admin-sortable2>=0.6.19
pymdown-extensions>=4.8 pymdown-extensions>=4.8
psycopg2
#!/usr/bin/env bash #!/usr/bin/env bash
echo "Migrating database"
python manage.py makemigrations
python manage.py migrate
echo "Initializing admin"
python manage.py initadmin
echo "Collecting static files" echo "Collecting static files"
python manage.py collectstatic --noinput python manage.py collectstatic --noinput
...@@ -7,4 +14,5 @@ python manage.py collectstatic --noinput ...@@ -7,4 +14,5 @@ python manage.py collectstatic --noinput
exec gunicorn oser_backend.wsgi:application \ exec gunicorn oser_backend.wsgi:application \
--bind 0.0.0.0:8000 \ --bind 0.0.0.0:8000 \
--workers 3 \ --workers 3 \
--access-logfile=- --access-logfile=- \
--reload
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment