diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b9bd98b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +# Elastic Beanstalk Files +.elasticbeanstalk/* +.git +.gitignore \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9695f16..64b74d3 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,8 @@ img.png *.sqlite3 dump.rdb *.dump +keys/* +# Elastic Beanstalk Files +.elasticbeanstalk/* +!.elasticbeanstalk/*.cfg.yml +!.elasticbeanstalk/*.global.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9624a6f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.7-buster + +RUN mkdir -p /opt/app +RUN mkdir -p /opt/app/pip_cache +RUN mkdir -p /opt/app/hackx +COPY requirements.txt /opt/app/ +RUN pip3 install -r /opt/app/requirements.txt --cache-dir /opt/app/pip_cache +COPY . /opt/app/hackx/ +WORKDIR /opt/app/hackx/ +RUN python3 manage.py collectstatic --no-input +# RUN chown -R www-data:www-data /opt/app + +EXPOSE 5000 +ENTRYPOINT ["/opt/app/hackx/prodrunserver.sh"] \ No newline at end of file diff --git a/Procfile b/Procfile index 43494a9..4438a4a 100644 --- a/Procfile +++ b/Procfile @@ -1,3 +1,2 @@ release: python3 manage.py migrate -web: bin/start-pgbouncer-stunnel daphne hoohacks.asgi:application --port $PORT --bind 0.0.0.0 -v2 -worker: python manage.py runworker channels -v2 \ No newline at end of file +web: daphne -b :: -p 5000 hoohacks.asgi:application \ No newline at end of file diff --git a/docs/deployment.md b/docs/deployment.md new file mode 100644 index 0000000..212c43b --- /dev/null +++ b/docs/deployment.md @@ -0,0 +1,34 @@ +# Steps to Deploy onto AWS + +## Elastic Beanstalk + +1. `eb create ENV_NAME` + +2. Add Configuration Variables +[https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-ssl.html](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-ssl.html) + +3. Create a new certificate through AWS ACM. + +4. Follow instructions to add CNAME to verify domain. + +5. Add port forwarding from 443 to 80 with the new certificate. + +## Update version + +`eb deploy --staged -v` + +## Active Migration Guide of Databases + +From Heroku-managed Database to AWS RDS, use the following steps to migrate. + +1. `heroku maintenance:on` + +2. `heroku pg:backups:capture` + +3. `heroku pg:backups:download` + +4. `pg_restore --verbose --clean --no-acl --no-owner -h HOST_URL -U USERNAME -d DATABASE_NAME DUMP_FILE` + +5. Ensure AWS version is working. + +6. Change `DATABASE_URL` in Heroku diff --git a/hoohacks/settings.py b/hoohacks/settings.py index e0703e4..33d55ad 100644 --- a/hoohacks/settings.py +++ b/hoohacks/settings.py @@ -30,7 +30,7 @@ # SECURITY WARNING: don't run with debug turned on in production -ON_HEROKU = 'ON_HEROKU' in os.environ +ON_HEROKU = 'ON_HEROKU' in os.environ or 'ON_AWS' in os.environ DEBUG = False if ON_HEROKU else True @@ -104,6 +104,18 @@ } } +if 'RDS_HOSTNAME' in os.environ: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': os.environ['RDS_DB_NAME'], + 'USER': os.environ['RDS_USERNAME'], + 'PASSWORD': os.environ['RDS_PASSWORD'], + 'HOST': os.environ['RDS_HOSTNAME'], + 'PORT': os.environ['RDS_PORT'], + } + } + # Password validation # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators @@ -279,6 +291,12 @@ PROD_URL = os.environ.get('PROD_URL', 'http://localhost:8000/') +if not DEBUG: + SECURE_SSL_REDIRECT = True + SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') + import dj_database_url + DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True) + try: # Configure Django App for Heroku. import django_heroku @@ -295,10 +313,8 @@ dsn=os.environ['SENTRY_DSN'], integrations=[DjangoIntegration()] ) - SECURE_SSL_REDIRECT = True - SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') except ImportError: - found = False + pass ALLOWED_HOSTS = [ PROD_URL, diff --git a/prodrunserver.sh b/prodrunserver.sh new file mode 100755 index 0000000..213ba67 --- /dev/null +++ b/prodrunserver.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +daphne -b :: -p 5000 hoohacks.asgi:application diff --git a/requirements.txt b/requirements.txt index e943746..1ce9df6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,6 +17,7 @@ daphne==2.3.0 DateJS==0.5 Django==2.2.13 django-heroku +dj-database-url dropbox==11.0.0 hyperlink==20.0.1 idna==2.10