Skip to content

Commit

Permalink
Add env vars to allow other postgres targets
Browse files Browse the repository at this point in the history
Also, support SSL for AWS RDS postgres. If needed in the future,
we could add more flexibility to what certificate to use, but
I didn't want to make the .env too complicated for now.
  • Loading branch information
hbradio committed Mar 8, 2018
1 parent eb8cb30 commit f9312dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
19 changes: 17 additions & 2 deletions compose/django/entrypoint.sh
Expand Up @@ -12,16 +12,31 @@ export REDIS_URL=redis://redis:6379
if [ -z "$POSTGRES_USER" ]; then
export POSTGRES_USER=postgres
fi
if [ -z "$POSTGRES_HOST" ]; then
export POSTGRES_HOST=postgres
fi
if [ -z "$POSTGRES_DB" ]; then
export POSTGRES_DB=postgres
fi

apt-get install -y ca-certificates wget
wget https://s3.amazonaws.com/rds-downloads/rds-ca-2015-root.pem -P /usr/local/share/ca-certificates/
update-ca-certificates


export DATABASE_URL=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres:5432/$POSTGRES_USER
export DATABASE_URL=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB


function postgres_ready(){
python << END
import sys
import psycopg2
if "$POSTGRES_USE_AWS_SSL".lower() == 'true':
kwargs = {"sslrootcert": "rds-ca-2015-root.pem", "sslmode": "require"}
else:
kwargs = {}
try:
conn = psycopg2.connect(dbname="$POSTGRES_USER", user="$POSTGRES_USER", password="$POSTGRES_PASSWORD", host="postgres")
conn = psycopg2.connect(dbname="$POSTGRES_DB", user="$POSTGRES_USER", password="$POSTGRES_PASSWORD", host="$POSTGRES_HOST", **kwargs)
except psycopg2.OperationalError:
sys.exit(-1)
sys.exit(0)
Expand Down
3 changes: 3 additions & 0 deletions config/settings/common.py
Expand Up @@ -111,6 +111,9 @@
}
DATABASES['default']['ATOMIC_REQUESTS'] = True

if env.bool('POSTGRES_USE_AWS_SSL', False):
DATABASES['default']['OPTIONS'] = {'sslrootcert': 'rds-ca-2015-root.pem', 'sslmode': 'require'}


# GENERAL CONFIGURATION
# ------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions env.example
@@ -1,7 +1,10 @@

# PostgreSQL
POSTGRES_HOST=postgres
POSTGRES_DB=postgresuser
POSTGRES_PASSWORD=mysecretpass
POSTGRES_USER=postgresuser
POSTGRES_USE_AWS_SSL=false

# General settings
DJANGO_ADMIN_URL=
Expand Down

0 comments on commit f9312dc

Please sign in to comment.