diff --git a/.travis.yml b/.travis.yml index fc3d74e..6e957b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,5 @@ python: services: - docker script: + - docker build -t backend . - BACKEND_IMAGE=backend docker-compose -f docker-compose-ci.yml run backend /app/scripts/dev/run_tests.sh \ No newline at end of file diff --git a/docker-compose-ci.yml b/docker-compose-ci.yml index f673cca..375a2a1 100644 --- a/docker-compose-ci.yml +++ b/docker-compose-ci.yml @@ -4,7 +4,7 @@ services: db: image: postgres:9.6.8 ports: - - "5432:5432" + - "5432" env_file: test.env localstack: diff --git a/restauth/settings.py b/restauth/settings.py index 0a3bb5f..ecbb741 100755 --- a/restauth/settings.py +++ b/restauth/settings.py @@ -18,9 +18,9 @@ # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ -SECRET_KEY = os.environ.get('SECRET_KEY') +SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY') DEBUG = os.environ.get('DEBUG') -ALLOWED_HOSTS = list(filter(lambda s: len(s) > 0, map(str.strip, os.environ.get('ALLOWED_HOSTS', '').split(',')))) +ALLOWED_HOSTS = list(filter(lambda s: len(s) > 0, map(str.strip, os.environ.get('ALLOWED_HOSTS', '*').split(',')))) # Application definition diff --git a/restauth/urls.py b/restauth/urls.py old mode 100644 new mode 100755 index 0c23fff..7fcac72 --- a/restauth/urls.py +++ b/restauth/urls.py @@ -25,5 +25,8 @@ url(r'^doc/', get_swagger_view(title='Documentation')), path('auth/', include(user_patterns)), - path('password-reset/', include(password_reset_patterns)) + path('password-reset/', include(password_reset_patterns)), + + path('', views.HomeView.as_view(), name='home'), + path('status/elb', views.HealthCheckView.as_view(), name='elb_status'), ] diff --git a/restauth/views.py b/restauth/views.py old mode 100644 new mode 100755 index dbbab8c..c56d959 --- a/restauth/views.py +++ b/restauth/views.py @@ -1,6 +1,11 @@ from rest_framework import generics from rest_framework import permissions +from rest_framework import views +from rest_framework import response +from rest_framework import status from rest_framework.throttling import AnonRateThrottle +from django.db import DEFAULT_DB_ALIAS, connections +from django.db.migrations.executor import MigrationExecutor from . import serializers @@ -51,4 +56,26 @@ class PasswordResetConfirmationView(generics.CreateAPIView): Set new password, it requires to provide the new password to set. """ permission_classes = (permissions.AllowAny,) - serializer_class = serializers.PasswordResetConfirmationSerializer \ No newline at end of file + serializer_class = serializers.PasswordResetConfirmationSerializer + + +class HomeView(views.APIView): + permission_classes = [] + + def get(self, request): + return response.Response(status=status.HTTP_200_OK) + + +class HealthCheckView(views.APIView): + authentication_classes = () + permission_classes = (permissions.AllowAny,) + + @staticmethod + def get(request): + executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) + plan = executor.migration_plan(executor.loader.graph.leaf_nodes()) + + if plan: + return response.Response(status=status.HTTP_503_SERVICE_UNAVAILABLE) + + return response.Response(status=status.HTTP_200_OK) diff --git a/scripts/dev/run_tests.sh b/scripts/dev/run_tests.sh index 7b9a810..07ef4dd 100755 --- a/scripts/dev/run_tests.sh +++ b/scripts/dev/run_tests.sh @@ -20,7 +20,7 @@ echo "Waiting for DB" python ./scripts/dev/wait_for_postgres.py echo "Run migrations" -/app/manage.py makemigrations --dry-run --check || { echo "ERROR: there were changes in the models, but migration listed above have not been created and are not saved in version control"; exit 1; } +python /app/manage.py makemigrations --dry-run --check || { echo "ERROR: there were changes in the models, but migration listed above have not been created and are not saved in version control"; exit 1; } echo "Run pytest" pytest --maxfail=1 --junitxml=/test-results/report.xml \ No newline at end of file diff --git a/scripts/run-backend.sh b/scripts/run-backend.sh index c65f633..187b797 100755 --- a/scripts/run-backend.sh +++ b/scripts/run-backend.sh @@ -2,5 +2,4 @@ set -e -python manage.py migrate --no-input uwsgi --ini uwsgi.ini diff --git a/scripts/run-migrations.sh b/scripts/run-migrations.sh new file mode 100755 index 0000000..a0c1ffd --- /dev/null +++ b/scripts/run-migrations.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -e + +python manage.py migrate --no-input diff --git a/test.env b/test.env index 1694c41..963434e 100644 --- a/test.env +++ b/test.env @@ -1,5 +1,9 @@ DEBUG=on +DJANGO_SECRET_KEY=secret-key + +ALLOWED_HOSTS=* + POSTGRES_PASSWORD=test-app-secret POSTGRES_DB=test-app POSTGRES_USER=test-user