Skip to content

Commit

Permalink
Merge pull request #1 from Team-MailedIt/develop
Browse files Browse the repository at this point in the history
�feat: initiate django server
  • Loading branch information
14hhan authored Nov 25, 2021
2 parents 7f74e2e + d825dd1 commit 377ad2e
Show file tree
Hide file tree
Showing 21 changed files with 551 additions and 1 deletion.
56 changes: 56 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Deploy to EC2
on: [push]
jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master

- name: create env file
run: |
touch .env
echo "${{ secrets.ENV_VARS }}" >> .env
- name: create remote directory
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ubuntu
key: ${{ secrets.KEY }}
script: mkdir -p /home/ubuntu/srv/ubuntu

- name: copy source via ssh key
uses: burnett01/[email protected]
with:
switches: -avzr --delete
remote_path: /home/ubuntu/srv/ubuntu/
remote_host: ${{ secrets.HOST }}
remote_user: ubuntu
remote_key: ${{ secrets.KEY }}

- name: executing remote ssh commands using password
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ubuntu
key: ${{ secrets.KEY }}
script: |
sh /home/ubuntu/srv/ubuntu/config/scripts/deploy.sh
cleanup:
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/master'
steps:
- name: delete useless images and containers
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ubuntu
key: ${{ secrets.KEY }}
script: |
sudo yes y | sudo docker image prune
sudo yes y | sudo docker container prune
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ celerybeat.pid

# Environments
.env
.env.prod
.venv
env/
venv/
Expand Down Expand Up @@ -321,4 +322,6 @@ fabric.properties
# Cython debug symbols

# End of https://www.toptal.com/developers/gitignore/api/pycharm,django,python
.idea
.idea

.DS_Store
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM python:3.8.3-alpine
ENV PYTHONUNBUFFERED 1

RUN mkdir /app
WORKDIR /app

# dependencies for psycopg2-binary
RUN apk add --no-cache mariadb-connector-c-dev
RUN apk update && apk add python3 python3-dev mariadb-dev build-base libffi-dev && pip3 install mysqlclient && apk del python3-dev mariadb-dev build-base

# dependencies for pillow
RUN apk add --no-cache jpeg-dev zlib-dev
RUN apk add --no-cache --virtual .build-deps build-base linux-headers


# By copying over requirements first, we make sure that Docker will cache
# our installed requirements rather than reinstall them on every build
RUN python3 -m pip install --upgrade pip
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt

# Now copy in our code, and run it
COPY . /app/
71 changes: 71 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# BUILDER #
###########

# pull official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/app


# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update && apk add python3 python3-dev mariadb-dev build-base libffi-dev && pip3 install mysqlclient && apk del python3-dev mariadb-dev build-base

# dependencies for pillow
RUN apk add --no-cache jpeg-dev zlib-dev
RUN apk add --no-cache --virtual .build-deps build-base linux-headers

# install dependencies
RUN python3 -m pip install --upgrade pip
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.8.3-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/static
RUN mkdir $APP_HOME/media
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add --no-cache mariadb-dev
RUN python3 -m pip install --upgrade pip
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install mysqlclient
RUN pip install --no-cache /wheels/*
RUN apk del build-deps

# copy entrypoint-prod.sh
COPY ./config/docker/entrypoint.prod.sh $APP_HOME

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app
Empty file added config/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions config/docker/entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

python manage.py collectstatic --no-input

exec "$@"
4 changes: 4 additions & 0 deletions config/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:1.20.2-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
33 changes: 33 additions & 0 deletions config/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
upstream vote_mailedit {
server web:8000;
}

server {

listen 443 ssl;

server_name vote-mailedit.kro.kr;

ssl_certificate /etc/nginx/ssl/vote-mailedit.kro.kr/fullchain1.pem;
ssl_certificate_key /etc/nginx/ssl/vote-mailedit.kro.kr/privkey1.pem;

location / {
proxy_pass http://vote_mailedit;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}

location /static/ {
alias /home/app/web/static/;
}

location /media/ {
alias /home/app/web/media/;
}

location ~/.well-known/acme-challenge {
allow all;
root /var/www/certbot;
}
}
27 changes: 27 additions & 0 deletions config/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Installing docker engine if not exists
if ! type docker > /dev/null
then
echo "docker does not exist"
echo "Start installing docker"
sudo apt-get update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install -y docker-ce
fi

# Installing docker-compose if not exists
if ! type docker-compose > /dev/null
then
echo "docker-compose does not exist"
echo "Start installing docker-compose"
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi

echo "start docker-compose up: ubuntu"
sudo docker-compose -f /home/ubuntu/srv/ubuntu/docker-compose.prod.yml up --build -d
41 changes: 41 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: '3.8'
services:

web:
container_name: web
build:
context: ./
dockerfile: Dockerfile.prod
command: gunicorn vote_mailedit.wsgi:application --bind 0.0.0.0:8000
environment:
DJANGO_SETTINGS_MODULE: vote_mailedit.settings.prod
env_file:
- .env
volumes:
- static:/home/app/web/static
- media:/home/app/web/media
expose:
- 8000
entrypoint:
- sh
- config/docker/entrypoint.prod.sh

nginx:
container_name: nginx
build: ./config/nginx
volumes:
- type: bind
source: /home/app/web/certbot/conf/archive
target: /etc/nginx/ssl
- /home/app/web/certbot/data:/var/www/certbot
- static:/home/app/web/static
- media:/home/app/web/media
ports:
- "80:80"
- "443:443"
depends_on:
- web

volumes:
static:
media:
41 changes: 41 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: '3'
services:

db:
container_name: db
image: mariadb:latest
restart: always
environment:
MYSQL_ROOT_HOST: '%'
MYSQL_ROOT_PASSWORD: mysql
expose:
- 3306
ports:
- "3307:3306"
env_file:
- .env
volumes:
- dbdata:/var/lib/mysql

web:
container_name: web
build: .
command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
environment:
MYSQL_ROOT_PASSWORD: mysql
DATABASE_NAME: mysql
DATABASE_USER: 'root'
DATABASE_PASSWORD: mysql
DATABASE_PORT: 3306
DATABASE_HOST: db
DJANGO_SETTINGS_MODULE: vote_mailedit.settings.dev
restart: always
ports:
- "8000:8000"
volumes:
- .:/app
depends_on:
- db
volumes:
app:
dbdata:
21 changes: 21 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'vote_mailedit.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()
10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
asgiref==3.4.1
cffi==1.15.0
cryptography==35.0.0
Django==3.0.8
django-environ==0.4.5
gunicorn==20.1.0
pycparser==2.21
PyMySQL==1.0.2
pytz==2021.3
sqlparse==0.4.2
Empty file added vote_mailedit/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions vote_mailedit/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for vote_mailedit 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/3.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'vote_mailedit.settings')

application = get_asgi_application()
1 change: 1 addition & 0 deletions vote_mailedit/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .dev import *
Loading

0 comments on commit 377ad2e

Please sign in to comment.