Skip to content

Commit

Permalink
Sync backend with boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
mkleszcz committed Sep 16, 2019
1 parent cea61b3 commit 77bd4e3
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 225 deletions.
7 changes: 0 additions & 7 deletions .env.example

This file was deleted.

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ __pycache__/
*.py[cod]
.idea/
db.sqlite3
.pytest_cache/
.env
.pytest_cache/
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: python
python:
- "3.6"
install:
- pip install pipenv
- pipenv install --dev
services:
- docker
script:
- pipenv run pytest
- BACKEND_IMAGE=backend docker-compose -f docker-compose-ci.yml run backend /app/scripts/dev/run_tests.sh
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ RUN apk update \
&& apk add postgresql-dev \
&& apk add build-base linux-headers pcre-dev

RUN pip install awscli

EXPOSE 3031
WORKDIR /code
COPY Pipfile Pipfile.lock ./
WORKDIR /app
COPY Pipfile Pipfile.lock /app/
RUN pip install --upgrade pip
RUN pip install pipenv
RUN pipenv install --system --dev
COPY . .
COPY . /app

CMD ["./run-backend.sh"]
CMD ["/app/scripts/run-backend.sh"]
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ djangorestframework = "~=3.10"
djangorestframework-jwt = "~=1.11"
django-hashid-field = "~=2.1"
dj-database-url = "*"
uwsgi = "*"
boto3 = "==1.9.93"
"psycopg2-binary" = "*"

[dev-packages]
factory_boy = "==2.10.0"
Expand Down
111 changes: 107 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions docker-compose-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '3'

services:
db:
image: postgres:9.6.8
ports:
- "5432:5432"
env_file: test.env

localstack:
image: localstack/localstack:0.10.2
ports:
- "4567-4584:4567-4584"
environment:
- SERVICES=secretsmanager
- DEBUG=${DEBUG- }
- DATA_DIR=${DATA_DIR- }
- PORT_WEB_UI=${PORT_WEB_UI- }
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
- LAMBDA_REMOTE_DOCKER=false
- KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
- DOCKER_HOST=unix:///var/run/docker.sock
- AWS_EXECUTION_ENV=True
- AWS_DEFAULT_REGION=us-east-1
- AWS_ACCESS_KEY_ID=foo
- AWS_SECRET_ACCESS_KEY=bar

backend:
image: ${BACKEND_IMAGE}
depends_on:
- db
- localstack
restart: on-failure
env_file: test.env
command: ./scripts/dev/run_tests.sh
26 changes: 0 additions & 26 deletions docker-compose.yml

This file was deleted.

22 changes: 21 additions & 1 deletion restauth/settings.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import os
import json
import dj_database_url
import boto3

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

secrets_manager = boto3.client(
'secretsmanager', endpoint_url=os.environ.get('SECRET_MANAGER_ENDPOINT_URL', None))

db_secret_arn = os.environ['DB_SECRET_ARN']

db_secret_value = secrets_manager.get_secret_value(SecretId=db_secret_arn)
# contains host, username, password and port
db_connection_config = json.loads(db_secret_value.get('SecretString'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
Expand Down Expand Up @@ -64,7 +74,17 @@
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
'default': dj_database_url.parse('sqlite:///db.sqlite3'),
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("POSTGRES_DB"),
"USER": db_connection_config.get('username'),
"PASSWORD": db_connection_config.get('password'),
"HOST": db_connection_config.get('host'),
# Persistent connections avoid the overhead of re-establishing a connection
# to the database in each request
"CONN_MAX_AGE": int(os.getenv("POSTGRES_CONN_MAX_AGE", '60')),
"PORT": db_connection_config.get('port'),
}
}


Expand Down
18 changes: 18 additions & 0 deletions scripts/dev/install_localstack_fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

set -e

function wait_for_secretsmanager {
until aws --no-sign-request --endpoint-url="$SECRET_MANAGER_ENDPOINT_URL" secretsmanager list-secrets; do
>&2 echo "Secretsmanager is unavailable - sleeping"
sleep 1
done
}

function install_db_secret {
SECRET_STRING="{\"host\": \"db\", \"username\": \"$POSTGRES_USER\", \"password\": \"$POSTGRES_PASSWORD\", \"port\": $POSTGRES_PORT}"

aws --endpoint-url="$SECRET_MANAGER_ENDPOINT_URL" secretsmanager create-secret \
--name "$DB_SECRET_ARN" \
--secret-string "$SECRET_STRING"
}
21 changes: 21 additions & 0 deletions scripts/dev/run-backend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -e

. $(dirname "$0")/install_localstack_fixtures.sh

# wait untill secretsmanager become ready
wait_for_secretsmanager
echo "Secrets manager is up"

# install all localstack fixtures
{
install_db_secret &&
echo "DB secrets set"
} || {
echo "DB secrets NOT set"
}

python ./scripts/dev/wait_for_postgres.py &&
./manage.py migrate &&
./manage.py runserver 0.0.0.0:8000
Loading

0 comments on commit 77bd4e3

Please sign in to comment.