From ffc2c87a215e02689ca77fddd0537f6348237214 Mon Sep 17 00:00:00 2001 From: Rithik Yelisetty Date: Wed, 3 Mar 2021 20:21:06 -0500 Subject: [PATCH 1/3] Start working on dockerizing --- .dockerignore | 4 ++++ .gitignore | 5 +++++ Dockerfile | 13 +++++++++++++ Procfile | 3 +-- docs/deployment.md | 22 ++++++++++++++++++++++ hoohacks/settings.py | 12 ++++++++++++ prodrunserver.sh | 3 +++ 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docs/deployment.md create mode 100755 prodrunserver.sh 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..251942e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +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 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..08d325a --- /dev/null +++ b/docs/deployment.md @@ -0,0 +1,22 @@ +# Steps to Deploy onto AWS + +## Create an EC2 Instance + +- t2.micro +- Amazon Linux +- Security Group with HTTP, HTTPS and SSH + +## Connect via SSH + +ssh -i "keys/HackX.pem" ec2-user@ec2-3-83-1-229.compute-1.amazonaws.com + +## Install python3 and git + +`sudo yum install git python3`\ + +## EBS + +sudo yum install postgresql-devel + + +`eb init -p python-3.6 hackx-main` diff --git a/hoohacks/settings.py b/hoohacks/settings.py index e0703e4..36f1781 100644 --- a/hoohacks/settings.py +++ b/hoohacks/settings.py @@ -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 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 From 03f2cb4c74d4b0c132c8786300fe8620a7405604 Mon Sep 17 00:00:00 2001 From: Rithik Yelisetty Date: Thu, 11 Mar 2021 22:34:12 -0500 Subject: [PATCH 2/3] Fully working on EB --- Dockerfile | 1 + docs/deployment.md | 22 +++++++++------------- hoohacks/settings.py | 10 ++++++---- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 251942e..9624a6f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,7 @@ 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 diff --git a/docs/deployment.md b/docs/deployment.md index 08d325a..95dbc06 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1,22 +1,18 @@ # Steps to Deploy onto AWS -## Create an EC2 Instance +## Elastic Beanstalk -- t2.micro -- Amazon Linux -- Security Group with HTTP, HTTPS and SSH +1. `eb create ENV_NAME` -## Connect via SSH +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) -ssh -i "keys/HackX.pem" ec2-user@ec2-3-83-1-229.compute-1.amazonaws.com +3. Create a new certificate through AWS ACM. -## Install python3 and git +4. Follow instructions to add CNAME to verify domain. -`sudo yum install git python3`\ +5. Add port forwarding from 443 to 80 with the new certificate. -## EBS +## Update version -sudo yum install postgresql-devel - - -`eb init -p python-3.6 hackx-main` +`eb deploy --staged -v` diff --git a/hoohacks/settings.py b/hoohacks/settings.py index 36f1781..230cb1f 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 @@ -291,6 +291,10 @@ 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') + try: # Configure Django App for Heroku. import django_heroku @@ -307,10 +311,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, From 8f4e437b93d80ff0cc7c91b5a47f51ef8248f6b8 Mon Sep 17 00:00:00 2001 From: Rithik Yelisetty Date: Tue, 16 Mar 2021 14:10:01 -0400 Subject: [PATCH 3/3] Add deployment instructions --- docs/deployment.md | 16 ++++++++++++++++ hoohacks/settings.py | 4 +++- requirements.txt | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/deployment.md b/docs/deployment.md index 95dbc06..212c43b 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -16,3 +16,19 @@ ## 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 230cb1f..33d55ad 100644 --- a/hoohacks/settings.py +++ b/hoohacks/settings.py @@ -294,7 +294,9 @@ 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 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