From 04e4b709ffcf97c6d3216b28e1d83a029799e5cc Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Mon, 27 Feb 2023 00:50:22 +0530 Subject: [PATCH 01/12] updated the dependencies --- requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index f2ae9919..610dee3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ attrs==19.1.0 black==19.3b0 cachetools==3.1.1 certifi==2019.6.16 -cffi==1.14.4 +cffi==1.15.1 chardet==3.0.4 Click==7.0 cryptography==3.3.1 @@ -12,9 +12,9 @@ django-cors-headers==3.0.2 django-grappelli==2.13.1 django-rest-knox==4.1.0 djangorestframework==3.10.3 -google-auth==1.6.3 +google-auth-2.16.1 idna==2.8 -Pillow==8.0.1 +Pillow==9.4.0 psycopg2-binary==2.8.3 pyasn1==0.4.6 pyasn1-modules==0.2.6 From cdaeb964a6838ed65aaa49a72956ae6d3d310d3a Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 02:21:59 +0530 Subject: [PATCH 02/12] [UPDATE] Dockerized the project --- Digital_Fortress_Backend/settings.py | 4 +-- Dockerfile | 12 +++++++++ docker-compose.yml | 37 ++++++++++++++++++++++++++++ entrypoint.sh | 7 ++++++ nginx/Dockerfile | 3 +++ nginx/default.conf | 14 +++++++++++ requirements.txt | 5 ++-- 7 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 entrypoint.sh create mode 100644 nginx/Dockerfile create mode 100644 nginx/default.conf diff --git a/Digital_Fortress_Backend/settings.py b/Digital_Fortress_Backend/settings.py index 8ae05e38..a157b692 100644 --- a/Digital_Fortress_Backend/settings.py +++ b/Digital_Fortress_Backend/settings.py @@ -28,8 +28,8 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config('DEBUG',default=False, cast=bool) -ALLOWED_HOSTS = ["*"] #ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv()) +ALLOWED_HOSTS = ['*'] GOOGLE_MAPS_API_KEY = config('GOOGLE_MAPS_API_KEY') @@ -109,7 +109,7 @@ 'NAME': config('DB_NAME'), 'USER': config('DB_USER'), 'PASSWORD': config('DB_PASSWORD'), - 'HOST': 'localhost', + 'HOST': 'db', 'PORT': '', } } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..4852762e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.7-slim-buster + +RUN pip install --upgrade pip + +COPY ./requirements.txt . +RUN pip install -r requirements.txt + +COPY . . + +# WORKDIR /app + +ENTRYPOINT [ "sh", "entrypoint.sh" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..01299073 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: '3.8' + +services: + django_gunicorn: + volumes: + - static:/static + env_file: + - .env + build: + context: . + ports: + - "8000:8000" + depends_on: + - db + + db: + image: postgres:13.0-alpine + volumes: + - db_data:/var/lib/postgresql/data/ + environment: + - POSTGRES_USER=dfuser + - POSTGRES_PASSWORD=password + - POSTGRES_DB=df + + nginx: + build: ./nginx + volumes: + - static:/static + ports: + - "80:80" + depends_on: + - db + - django_gunicorn + +volumes: + db_data: + static: \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..07c5c9e4 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +python manage.py makemigrations --no-input +python manage.py migrate --no-input +python manage.py collectstatic --no-input + +gunicorn Digital_Fortress_Backend.wsgi:application --bind 0.0.0.0:8000 \ No newline at end of file diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 00000000..ec370358 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,3 @@ +FROM nginx:1.19.0-alpine + +COPY ./default.conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 00000000..dbb7832b --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,14 @@ +upstream django_gunicorn { + server django_gunicorn:8000; +} + +server { + listen 80; + location /static/ { + alias /static/; + } + location / { + proxy_pass http://django_gunicorn; + } + +} diff --git a/requirements.txt b/requirements.txt index 610dee3e..6d92ceb6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,7 @@ django-cors-headers==3.0.2 django-grappelli==2.13.1 django-rest-knox==4.1.0 djangorestframework==3.10.3 -google-auth-2.16.1 +google-auth==2.16.1 idna==2.8 Pillow==9.4.0 psycopg2-binary==2.8.3 @@ -23,7 +23,8 @@ python-decouple==3.1 pytz==2019.2 requests==2.22.0 rsa==4.0 -six==1.12.0 +six==1.15.0 sqlparse==0.3.0 toml==0.10.0 urllib3==1.25.3 +gunicorn==20.0.4 \ No newline at end of file From 385b8c83db7742b85d39c6f1b48593161fe3592a Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 02:29:53 +0530 Subject: [PATCH 03/12] resolved merge conflicts --- Digital_Fortress_Backend/settings.py | 4 ++-- requirements.txt | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Digital_Fortress_Backend/settings.py b/Digital_Fortress_Backend/settings.py index a157b692..9a67fdb9 100644 --- a/Digital_Fortress_Backend/settings.py +++ b/Digital_Fortress_Backend/settings.py @@ -29,7 +29,7 @@ DEBUG = config('DEBUG',default=False, cast=bool) #ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv()) -ALLOWED_HOSTS = ['*'] +ALLOWED_HOSTS = ["*"] GOOGLE_MAPS_API_KEY = config('GOOGLE_MAPS_API_KEY') @@ -109,7 +109,7 @@ 'NAME': config('DB_NAME'), 'USER': config('DB_USER'), 'PASSWORD': config('DB_PASSWORD'), - 'HOST': 'db', + 'HOST': 'localhost', 'PORT': '', } } diff --git a/requirements.txt b/requirements.txt index 6d92ceb6..09b995df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ attrs==19.1.0 black==19.3b0 cachetools==3.1.1 certifi==2019.6.16 -cffi==1.15.1 +cffi==1.14.4 chardet==3.0.4 Click==7.0 cryptography==3.3.1 @@ -12,9 +12,9 @@ django-cors-headers==3.0.2 django-grappelli==2.13.1 django-rest-knox==4.1.0 djangorestframework==3.10.3 -google-auth==2.16.1 +google-auth==1.6.3 idna==2.8 -Pillow==9.4.0 +Pillow==8.0.1 psycopg2-binary==2.8.3 pyasn1==0.4.6 pyasn1-modules==0.2.6 @@ -23,7 +23,7 @@ python-decouple==3.1 pytz==2019.2 requests==2.22.0 rsa==4.0 -six==1.15.0 +six==1.12.0 sqlparse==0.3.0 toml==0.10.0 urllib3==1.25.3 From c82e39e5c688a32c5abf495a6c138ccc057c7fa6 Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 02:31:23 +0530 Subject: [PATCH 04/12] resolved merge conflicts --- Digital_Fortress_Backend/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Digital_Fortress_Backend/settings.py b/Digital_Fortress_Backend/settings.py index 9a67fdb9..8ae05e38 100644 --- a/Digital_Fortress_Backend/settings.py +++ b/Digital_Fortress_Backend/settings.py @@ -28,8 +28,8 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config('DEBUG',default=False, cast=bool) -#ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv()) ALLOWED_HOSTS = ["*"] +#ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv()) GOOGLE_MAPS_API_KEY = config('GOOGLE_MAPS_API_KEY') From 9d7d00b302d8d3564a8d0e0bc19abc6e2f82d8ea Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 03:05:06 +0530 Subject: [PATCH 05/12] removed docker db credentials --- docker-compose.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 01299073..201c217f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,9 +18,9 @@ services: volumes: - db_data:/var/lib/postgresql/data/ environment: - - POSTGRES_USER=dfuser - - POSTGRES_PASSWORD=password - - POSTGRES_DB=df + - POSTGRES_USER= + - POSTGRES_PASSWORD= + - POSTGRES_DB= nginx: build: ./nginx From 6a923ec9f58c7cdc953ad587ab8a59268d3f9bd1 Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 19:50:44 +0530 Subject: [PATCH 06/12] [UPDATE] updated db host as env variable --- Digital_Fortress_Backend/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Digital_Fortress_Backend/settings.py b/Digital_Fortress_Backend/settings.py index 8ae05e38..4e3c1742 100644 --- a/Digital_Fortress_Backend/settings.py +++ b/Digital_Fortress_Backend/settings.py @@ -109,7 +109,7 @@ 'NAME': config('DB_NAME'), 'USER': config('DB_USER'), 'PASSWORD': config('DB_PASSWORD'), - 'HOST': 'localhost', + 'HOST': config('DB_HOST'), 'PORT': '', } } From 4bdf3359d08afbad0d0d49fa22e9b4d6f32addc8 Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 19:51:35 +0530 Subject: [PATCH 07/12] [UPDATE] updated the docker db environment variables --- .env.example | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/.env.example b/.env.example index 390dad84..f2e536de 100644 --- a/.env.example +++ b/.env.example @@ -1,12 +1,15 @@ -SECRET_KEY=djangosecretkey -CLIENT_ID=407408718192.apps.googleusercontent.com -APP_ID=facebookoauthid -APP_SECRET=facebookoauthsecret -DOWNLOAD=passwordtodownloadleaderboard -DB_NAME=df -DB_USER=blahaja -DB_PASSWORD=notmypass -DB_HOST=localhost -DB_PORT=5432 -DEBUG=True -GOOGLE_MAPS_API_KEY= \ No newline at end of file +SECRET_KEY= +CLIENT_ID= +APP_ID= +APP_SECRET= +DOWNLOAD= +DB_NAME= +DB_USER= +DB_PASSWORD= +DB_HOST= +DB_PORT= +DEBUG= +GOOGLE_MAPS_API_KEY= +POSTGRES_USER= +POSTGRES_PASSWORD= +POSTGRES_DB= \ No newline at end of file From 187817178a81b0c29eee41ae77cf05211ef49163 Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 19:52:17 +0530 Subject: [PATCH 08/12] [UPDATE] configured env varibales to db image --- docker-compose.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 201c217f..eb6ed4af 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,10 +17,8 @@ services: image: postgres:13.0-alpine volumes: - db_data:/var/lib/postgresql/data/ - environment: - - POSTGRES_USER= - - POSTGRES_PASSWORD= - - POSTGRES_DB= + env_file: + - .env nginx: build: ./nginx From d602be19b8932767dfabf2a9493908102938828e Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Sun, 11 Jun 2023 21:30:03 +0530 Subject: [PATCH 09/12] added .env --- .env | 15 +++++++++++++++ .gitignore | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 00000000..349cb9e7 --- /dev/null +++ b/.env @@ -0,0 +1,15 @@ +SECRET_KEY=djangosecretkey +CLIENT_ID=407408718192.apps.googleusercontent.com +APP_ID=facebookoauthid +APP_SECRET=facebookoauthsecret +DOWNLOAD=passwordtodownloadleaderboard +DB_NAME=df +DB_USER=dfuser +DB_PASSWORD=password +DB_HOST=db +DB_PORT=5432 +DEBUG=True +GOOGLE_MAPS_API_KEY= +POSTGRES_USER=dfuser +POSTGRES_PASSWORD=password +POSTGRES_DB=df \ No newline at end of file diff --git a/.gitignore b/.gitignore index 90842d0d..aa4f28f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.env + /env env #VS Code From 54736911d8151496b7aa25949e3cf59141e7d12a Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Mon, 12 Jun 2023 00:04:05 +0530 Subject: [PATCH 10/12] [FIX] fixed the disallowed host error --- .gitignore | 2 +- nginx/default.conf | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index aa4f28f4..90842d0d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +.env /env env #VS Code diff --git a/nginx/default.conf b/nginx/default.conf index dbb7832b..517bb9f6 100644 --- a/nginx/default.conf +++ b/nginx/default.conf @@ -4,10 +4,16 @@ upstream django_gunicorn { server { listen 80; + server_name dfapi.nitdgplug.org; + location /static/ { alias /static/; } location / { + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://django_gunicorn; } From 86caa67db2ec3d132ad5a6d3eaaf2190d531debf Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Mon, 12 Jun 2023 00:04:22 +0530 Subject: [PATCH 11/12] [ADD] Deployment instructions added --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/README.md b/README.md index 77186331..d43c0f12 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,49 @@ This is the backend repository built on Django framework and utilises Django Res 2. Then, apply the migrations using `python manange.py migrate` 3. Run the server using `python manage.py runserver --settings=Digital_Fortress_Backend.dev_settings` + +## How to deploy + +### Build the docker containers + +1. Clone the project + +```bash + git clone https://github.com/lugnitdgp/Digital_Fortress_Backend.git +``` + +2. Install docker + + + +3. Set-up environment variables + +```bash + cp .env.example .env +``` + +4. Spin-up the docker conatainers + +```bash + sudo docker-compose up -d --build +``` +5. Create admin + +```bash + sudo docker exec -it [django_container_name] python manage.py createsuperuser +``` + +### Stopping the conatainers + +```bash + sudo docker-compose stop +``` + +### Removing the volumes and the container +```bash + sudo docker-compose down -v +``` + ### Status Codes: 200 : Success From 61fdc9682128a0ac6a17a9b7572faeb632171e07 Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Mon, 12 Jun 2023 00:08:02 +0530 Subject: [PATCH 12/12] [DELETE] deleted .env file --- .env | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index 349cb9e7..00000000 --- a/.env +++ /dev/null @@ -1,15 +0,0 @@ -SECRET_KEY=djangosecretkey -CLIENT_ID=407408718192.apps.googleusercontent.com -APP_ID=facebookoauthid -APP_SECRET=facebookoauthsecret -DOWNLOAD=passwordtodownloadleaderboard -DB_NAME=df -DB_USER=dfuser -DB_PASSWORD=password -DB_HOST=db -DB_PORT=5432 -DEBUG=True -GOOGLE_MAPS_API_KEY= -POSTGRES_USER=dfuser -POSTGRES_PASSWORD=password -POSTGRES_DB=df \ No newline at end of file