Skip to content

Commit 5023fe7

Browse files
committed
Sync backend with boilerplate
1 parent cea61b3 commit 5023fe7

17 files changed

+319
-226
lines changed

.env.example

Lines changed: 0 additions & 7 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ __pycache__/
22
*.py[cod]
33
.idea/
44
db.sqlite3
5-
.pytest_cache/
6-
.env
5+
.pytest_cache/

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
language: python
22
python:
3-
- "3.6"
4-
install:
5-
- pip install pipenv
6-
- pipenv install --dev
3+
- "3.7"
4+
services:
5+
- docker
76
script:
8-
- pipenv run pytest
7+
- docker-compose -f docker-compose-ci.yml run backend /app/scripts/dev/run_tests.sh

Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ RUN apk update \
99
&& apk add postgresql-dev \
1010
&& apk add build-base linux-headers pcre-dev
1111

12+
RUN pip install awscli
13+
1214
EXPOSE 3031
13-
WORKDIR /code
14-
COPY Pipfile Pipfile.lock ./
15+
WORKDIR /app
16+
COPY Pipfile Pipfile.lock /app/
1517
RUN pip install --upgrade pip
1618
RUN pip install pipenv
1719
RUN pipenv install --system --dev
18-
COPY . .
20+
COPY . /app
1921

20-
CMD ["./run-backend.sh"]
22+
CMD ["/app/scripts/run-backend.sh"]

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ djangorestframework = "~=3.10"
99
djangorestframework-jwt = "~=1.11"
1010
django-hashid-field = "~=2.1"
1111
dj-database-url = "*"
12+
uwsgi = "*"
13+
boto3 = "==1.9.93"
14+
"psycopg2-binary" = "*"
1215

1316
[dev-packages]
1417
factory_boy = "==2.10.0"

Pipfile.lock

Lines changed: 107 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose-ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: '3'
2+
3+
services:
4+
db:
5+
image: postgres:9.6.8
6+
ports:
7+
- "5432:5432"
8+
env_file: test.env
9+
10+
localstack:
11+
image: localstack/localstack:0.10.2
12+
ports:
13+
- "4567-4584:4567-4584"
14+
environment:
15+
- SERVICES=secretsmanager
16+
- DEBUG=${DEBUG- }
17+
- DATA_DIR=${DATA_DIR- }
18+
- PORT_WEB_UI=${PORT_WEB_UI- }
19+
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
20+
- LAMBDA_REMOTE_DOCKER=false
21+
- KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
22+
- DOCKER_HOST=unix:///var/run/docker.sock
23+
- AWS_EXECUTION_ENV=True
24+
- AWS_DEFAULT_REGION=us-east-1
25+
- AWS_ACCESS_KEY_ID=foo
26+
- AWS_SECRET_ACCESS_KEY=bar
27+
28+
backend:
29+
image: ${BACKEND_IMAGE}
30+
depends_on:
31+
- db
32+
- localstack
33+
restart: on-failure
34+
env_file: test.env
35+
command: ./scripts/dev/run_tests.sh

docker-compose.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

restauth/settings.py

100644100755
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import os
2+
import json
23
import dj_database_url
4+
import boto3
35

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

9+
secrets_manager = boto3.client(
10+
'secretsmanager', endpoint_url=os.environ.get('SECRET_MANAGER_ENDPOINT_URL', None))
11+
12+
db_secret_arn = os.environ['DB_SECRET_ARN']
13+
14+
db_secret_value = secrets_manager.get_secret_value(SecretId=db_secret_arn)
15+
# contains host, username, password and port
16+
db_connection_config = json.loads(db_secret_value.get('SecretString'))
717

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

6676
DATABASES = {
67-
'default': dj_database_url.parse('sqlite:///db.sqlite3'),
77+
"default": {
78+
"ENGINE": "django.db.backends.postgresql",
79+
"NAME": os.getenv("POSTGRES_DB"),
80+
"USER": db_connection_config.get('username'),
81+
"PASSWORD": db_connection_config.get('password'),
82+
"HOST": db_connection_config.get('host'),
83+
# Persistent connections avoid the overhead of re-establishing a connection
84+
# to the database in each request
85+
"CONN_MAX_AGE": int(os.getenv("POSTGRES_CONN_MAX_AGE", '60')),
86+
"PORT": db_connection_config.get('port'),
87+
}
6888
}
6989

7090

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
function wait_for_secretsmanager {
6+
until aws --no-sign-request --endpoint-url="$SECRET_MANAGER_ENDPOINT_URL" secretsmanager list-secrets; do
7+
>&2 echo "Secretsmanager is unavailable - sleeping"
8+
sleep 1
9+
done
10+
}
11+
12+
function install_db_secret {
13+
SECRET_STRING="{\"host\": \"db\", \"username\": \"$POSTGRES_USER\", \"password\": \"$POSTGRES_PASSWORD\", \"port\": $POSTGRES_PORT}"
14+
15+
aws --endpoint-url="$SECRET_MANAGER_ENDPOINT_URL" secretsmanager create-secret \
16+
--name "$DB_SECRET_ARN" \
17+
--secret-string "$SECRET_STRING"
18+
}

0 commit comments

Comments
 (0)