From a589bc5284276e17ab14434d59e8c431ed344751 Mon Sep 17 00:00:00 2001 From: Two-Trick-Pony-NL Date: Thu, 12 Oct 2023 22:01:09 +0200 Subject: [PATCH] added django --- .github/workflows/deploy.yml | 115 ++++++++++++++++++++ AWS/deploymentconfig.json | 9 ++ AWS/nginx/Dockerfile | 17 +++ AWS/nginx/default.conf | 25 +++++ AWS/publicendpoint.json | 11 ++ core/__init__.py | 0 core/__pycache__/__init__.cpython-312.pyc | Bin 0 -> 176 bytes core/__pycache__/settings.cpython-312.pyc | Bin 0 -> 2544 bytes core/__pycache__/urls.cpython-312.pyc | Bin 0 -> 1040 bytes core/__pycache__/wsgi.cpython-312.pyc | Bin 0 -> 657 bytes core/asgi.py | 16 +++ core/settings.py | 123 ++++++++++++++++++++++ core/urls.py | 22 ++++ core/wsgi.py | 16 +++ db.sqlite3 | 0 manage.py | 22 ++++ requirements.txt | 14 +++ 17 files changed, 390 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 AWS/deploymentconfig.json create mode 100644 AWS/nginx/Dockerfile create mode 100644 AWS/nginx/default.conf create mode 100644 AWS/publicendpoint.json create mode 100644 core/__init__.py create mode 100644 core/__pycache__/__init__.cpython-312.pyc create mode 100644 core/__pycache__/settings.cpython-312.pyc create mode 100644 core/__pycache__/urls.cpython-312.pyc create mode 100644 core/__pycache__/wsgi.cpython-312.pyc create mode 100644 core/asgi.py create mode 100644 core/settings.py create mode 100644 core/urls.py create mode 100644 core/wsgi.py create mode 100644 db.sqlite3 create mode 100755 manage.py create mode 100644 requirements.txt diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..8211c3e --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,115 @@ +name: 🚀 Deployment +# Only trigger, when the build workflow succeeded +on: + push: + branches: [ "main" ] +permissions: + contents: read + +env: + AWS_REGION: ${{ secrets.AWS_REGION }} + AWS_LIGHTSAIL_SERVICE_NAME: ${{ secrets.AWS_SERVICE_NAME }} +concurrency: + group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}' + cancel-in-progress: true +jobs: + + buildnginx: + name: 🌎 building nginx webserver + runs-on: ubuntu-latest + steps: + - name: 🗂 Getting code from Github + uses: actions/checkout@v2 + - name: ⚙️ Updating to the latest versions + run: | + sudo apt-get update + sudo apt-get install -y jq unzip + - name: 🏢 Install Amazon Client + run: | + curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" + unzip awscliv2.zip + sudo ./aws/install || true + aws --version + curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "lightsailctl" + sudo mv "lightsailctl" "/usr/local/bin/lightsailctl" + sudo chmod +x /usr/local/bin/lightsailctl + - name: 🤐 Log in to AWS Lightsail with Secrets + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-region: ${{ env.AWS_REGION }} + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + - name: 🐳 Create a Docker Container for NGINX + run: docker build -t nginx:latest -f ./AWS/nginx/Dockerfile . + - name: 📬 Upload NGINX image to AWS container register + run: | + service_name=${{ env.AWS_LIGHTSAIL_SERVICE_NAME }} + aws lightsail push-container-image \ + --region ${{ env.AWS_REGION }} \ + --service-name ${service_name} \ + --label nginx \ + --image nginx:latest + - name: =========== All done. Cleaning up ♻️ =========== + run: ls + + builddjango: + name: 🚨 Deploying + runs-on: ubuntu-latest + steps: + - name: 🗂 Getting code from Github + uses: actions/checkout@v2 + - name: ⚙️ Updating to the latest versions + run: | + sudo apt-get update + sudo apt-get install -y jq unzip + - name: 🏢 Install Amazon Client + run: | + curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" + unzip awscliv2.zip + sudo ./aws/install || true + aws --version + curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "lightsailctl" + sudo mv "lightsailctl" "/usr/local/bin/lightsailctl" + sudo chmod +x /usr/local/bin/lightsailctl + - name: 🤐 Log in to AWS Lightsail with Secrets + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-region: ${{ env.AWS_REGION }} + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + - name: 🤐 Make envfile + uses: SpicyPizza/create-envfile@v1.3 + with: + envkey_DB_USER: ${{ secrets.DB_USER }} + envkey_DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + envkey_DB_HOST: ${{ secrets.DB_HOST }} + envkey_DB_NAME: ${{ secrets.DB_NAME }} + envkey_DB_PORT: ${{ secrets.DB_PORT }} + envkey_S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }} + envkey_S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} + envkey_S3_AWS_STORAGE_BUCKET_NAME: ${{ secrets.S3_AWS_STORAGE_BUCKET_NAME }} + envkey_SECURE_SETTINGS: True + envkey_DEBUG: false + directory: core + file_name: .env + fail_on_empty: false + - name: Installing dependancies + run: | + sudo pip3 install --upgrade pip + sudo pip3 install -r requirements.txt + - name: 🐳 Create a Docker Container for DJANGO + run: docker build -t countdowntimer:latest . + - name: 📬 Upload Backend image to AWS container register + run: | + service_name=${{ env.AWS_LIGHTSAIL_SERVICE_NAME }} + aws lightsail push-container-image \ + --region ${{ env.AWS_REGION }} \ + --service-name ${AWS_LIGHTSAIL_SERVICE_NAME} \ + --label countdowntimer \ + --image countdowntimer:latest + - name: 🚀 Launching the Containers + run: | + aws lightsail create-container-service-deployment --service-name ${{ env.AWS_LIGHTSAIL_SERVICE_NAME }} \ + --containers file://AWS/deploymentconfig.json \ + --public-endpoint file://AWS/publicendpoint.json + \ No newline at end of file diff --git a/AWS/deploymentconfig.json b/AWS/deploymentconfig.json new file mode 100644 index 0000000..df047d4 --- /dev/null +++ b/AWS/deploymentconfig.json @@ -0,0 +1,9 @@ +{"countdowntimer": { + "image": ":countdowntimer.countdowntimer.latest", + "ports": {"8000": "HTTP"} + }, + "countdowntimer-nginx": { + "image": ":countdowntimer.nginx.latest", + "ports": {"80": "HTTP"} + } +} \ No newline at end of file diff --git a/AWS/nginx/Dockerfile b/AWS/nginx/Dockerfile new file mode 100644 index 0000000..4b1aca4 --- /dev/null +++ b/AWS/nginx/Dockerfile @@ -0,0 +1,17 @@ +# This needs to be run from the root directory as you want to call files from mapmaker/static +# to do so use this build command: +# docker build -t nginx:latest -f Infrastructure/nginx/Dockerfile . + +FROM nginx:1.19.0-alpine +# Adding temp file for cache +RUN mkdir /tmp/nginx + +#Adding Django Static files into nginx so they dont have to be answered by Django +COPY staticfiles/ ./static +COPY staticfiles/ ./staticfiles + + +#Specific settings so nginx works as a reverse proxy +COPY AWS/nginx/default.conf ./etc/nginx/conf.d/default.conf + +EXPOSE 80 \ No newline at end of file diff --git a/AWS/nginx/default.conf b/AWS/nginx/default.conf new file mode 100644 index 0000000..589bc76 --- /dev/null +++ b/AWS/nginx/default.conf @@ -0,0 +1,25 @@ +proxy_cache_path /tmp/nginx keys_zone=mycache:1m; + + +server { + location / { + #Proxy settings for Django server upstream + proxy_pass http://localhost:8000; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_redirect off; + } + location /healthcheck { + #This is for AWS To hit healthchecks + add_header Content-Type text/plain; + return 200 'Up and running!'; + } + location /static/ { + proxy_cache mycache; + proxy_cache_background_update on; + proxy_cache_use_stale updating; + proxy_cache_valid 60s; + alias /static/; + } +} \ No newline at end of file diff --git a/AWS/publicendpoint.json b/AWS/publicendpoint.json new file mode 100644 index 0000000..864c8eb --- /dev/null +++ b/AWS/publicendpoint.json @@ -0,0 +1,11 @@ +{ + "containerName": "countdowntimer-nginx", + "containerPort": 80, + "healthCheck": { + "healthyThreshold": 2, + "unhealthyThreshold": 10, + "timeoutSeconds": 60, + "intervalSeconds": 300, + "path": "/healthcheck", + "successCodes": "200-299" +}} \ No newline at end of file diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/__pycache__/__init__.cpython-312.pyc b/core/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..268d2055114e38939cc1e9b0905b364538af019f GIT binary patch literal 176 zcmX@j%ge<81T*|JQbF`%5P=Rpvj9b=GgLBYGWxA#C}INgK7-W!^4AY7PAw|dFGwv( zEz)<%PcF?(%_}L^4=BpdN=+_F&C_?uO3X{ok9Q0Yj`zt-&nPKQ%*=@oNzEJmtkIamWj77{q763uwEqed} literal 0 HcmV?d00001 diff --git a/core/__pycache__/settings.cpython-312.pyc b/core/__pycache__/settings.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..72e40d18023e141ca8f8d0c828e7be270ee2912b GIT binary patch literal 2544 zcmb7GJ8#=o6ejh4TUO-QNm_t`^O9XtNS#Mp1ZilAc7({1DoSz7p#;r)EnkNsWs*u_ zPTD)PTQg+JR^+F2iP0ejCj*@dbdVEt$<#wivIW^dT|y%7J&*64``vRc`Fk=Mqu_c~ zxrKjDP}HC9WORjwgO7j1;8zM!B86z=Q|Y!(^wE@$!anSOOAoAeq9eDH=iA|M$BS_y zcSmQ*#1Qg}VH6M}ElP~GC=`4{i!peH;2B3@F@ZpX0?5_H-q3_b@z;SCEhbSyOra!7 z;Sid#;}nI`uYH6Tr_eOYfYmg*;N7#{eTLAjfS5)X#c4DvW+2`L_-E0aID;;U7xymb zuE;r9R;;)qYb~8Iv1JjhWing3!6>?cnU10FV8vo%u{Eq=L$)wtHuspD$a7edQJZLt zX~~8)w7ki5O@LsAh}FkZ6G2mMx-d4E(HZ0?~+yzkuL4wryD*^P7Ag z>59n?VvI6S^mZO=c>v5?dWR@xew1x~0B>z$W~-~Jj7m(4(YH=kBWnl(VPY`PWwndV z{}ao=TVsL#ayK+bJAs;PZQCnD7KmnIrEB1&-TCYDyU#Y4b{Fl%NAl+V&UN+r^0mdz zt-BUlc=>2+kt|AN={Z{d`tE&2zoE?k*vN(K>;Q&Ubj>ozChL)EUzn73A>x$@q3eP* z%i8N;)1IABn%Fdf=F^HcDC8D+T%Po>U}F?_JIq|zUK(oK1R)i_kPXaw>m=45E4T}W z!TPbzzB36n49|}F3j^7hSMGEW*y*;eu?6q(AEu_U#TMZ9l;9U*ruQduk%7GNJUEyG zZ$$A5am;aY5hJR`o!1BSjd|TWV-YG*p6$rWGiV0(`&C13<7&sPdUMP#>h>7hHB{5i z4?ZhP4UO7>EqWc-7^+PtF-B+O|m7U9iq%U(E# zrGa;$T`YTXXnS0tE}dr}yENO&6UN6Bp5ZXk^8#FfrW0^56_FV zPBhZ)w#!&+S=%T6-=5ch&>*#8jwb+*&I?fKwy{B!(+EMVEp<)D-)t1@Fr4ntD0%zy zAuKT2))7`sc7?Q{k@Qu9RaD8%I$=qIgIkiEa0iY@m24V*pw@{MU zV(X=1vCMC9b>2zTtJQ|otd|Sb%BmA>@awfQ*Wd*wy&EmVz`14QM2lR5bHfVG z9M^0-mTH_JY*gz-=_yw(fwBtKmn?IYwI;X5ONDBY2f)%gFNswsnMe~7F5UjB;)L8$ zLxjv5fLw5+JDDQC$~DUk36QJOYKbowf1^ISFO5&tyjwYDzJo1nb80ZOfF2DvcUG?-u{@n~3e~eTqDQZRo1_p>YH7-H#I!zE!BcpQh;} zUyKg?Ge^;vjzUx-{Y&JhhwtN8_v2Uph|j-|FYLz`-p6n6$8Yx{{S=j|)4fj?>cM{c=26g}2AiN~bD4@xy-SOIzYqK(-lR4@`EGnt xA53PFpZVTVbNw6C^jt4_n3(QG593q4$RQ*h>L;kwEF>DEF5cazGR8IF;$PEMOx*wg literal 0 HcmV?d00001 diff --git a/core/__pycache__/urls.cpython-312.pyc b/core/__pycache__/urls.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..49313184ff739388ba1f1e13b007bfa1f6d13904 GIT binary pat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I config for core project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') + +application = get_asgi_application() diff --git a/core/settings.py b/core/settings.py new file mode 100644 index 0000000..5586fda --- /dev/null +++ b/core/settings.py @@ -0,0 +1,123 @@ +""" +Django settings for core project. + +Generated by 'django-admin startproject' using Django 4.2.6. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-q#%#qkb-q+z+@ab7p%l%3$+p(5td*x@f+i+_i-vd3957co&c#^' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'core.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'core.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/core/urls.py b/core/urls.py new file mode 100644 index 0000000..3b2f61e --- /dev/null +++ b/core/urls.py @@ -0,0 +1,22 @@ +""" +URL configuration for core project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/core/wsgi.py b/core/wsgi.py new file mode 100644 index 0000000..f44964d --- /dev/null +++ b/core/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for core project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') + +application = get_wsgi_application() diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..e69de29 diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..f2a662c --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..715959b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,14 @@ +Django>=4.1,<4.2 +django-environ +requests==2.28.2 +django_extensions==3.2.1 +django-jsonfield==1.4.1 +qrcode +uwsgi +mysqlclient==2.1.1 +environ==1.0 +Pillow==9.5.0 +whitenoise +boto3 +django-storages +pyyaml \ No newline at end of file