Skip to content

Commit

Permalink
prepared for deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
agas0077 committed Oct 19, 2023
1 parent 2a1f279 commit 8512671
Show file tree
Hide file tree
Showing 53 changed files with 615 additions and 282 deletions.
17 changes: 17 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SECRET_KEY=some-secret-key
SEND_EMAIL=False

EMAIL_HOST=
EMAIL_PORT=
EMAIL_USE_SSL=
DEFAULT_FROM_EMAIL=
EMAIL_HOST_USER=
EMAIL_HOST_PASSWORD=
EMAIL_BACKEND=

POSTGRES_ENGINE=django.db.backends.postgresql
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=
POSTGRES_HOST=
POSTGRES_PORT=5432
58 changes: 58 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Main todo-tracker workflow

on:
push:
branches:
- master

jobs:
build_and_push_to_docker_hub:
name: Push Docker image to DockerHub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push to DockerHub
uses: docker/build-push-action@v4
with:
context: ./todo_tracker/
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/todo_tracker:latest

deploy:
runs-on: ubuntu-latest
needs:
- build_and_push_to_docker_hub

steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Copy docker-compose.yml via ssh
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "docker-compose.yml"
target: "todo_tracker"
- name: Executing remote ssh commands to deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd todo_tracker
git pull origin
sudo docker compose -f docker-compose.yml pull
sudo docker compose -f docker-compose.yml down
sudo docker compose -f docker-compose.yml up -d
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\todo_tracker\\manage.py",
"program": "${workspaceFolder}\\scm\\manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true
"justMyCode": false
}
]
}
35 changes: 35 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"isort.importStrategy": "useBundled",
"markdown-pdf.convertOnSave": true,
"markdown-pdf.convertOnSaveExclude": [
"^work",
"work.md$",
"^(?![Rr][Ee][Aa][Dd][Mm][Ee]).*$",
"work|test",
"[0-9][0-9][0-9][0-9]-work",
"work\\test" // All '\' need to be written as '\\' (Windows)
],
"markdown-pdf.type": [
"pdf"
],
"python.analysis.autoImportCompletions": true,
"python.analysis.completeFunctionParens": false,
"python.formatting.provider": "none",
"python.formatting.blackArgs": [
"--line-length",
"79"
],
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.insertSpaces": true,
"editor.tabSize": 4
},
"python.analysis.typeCheckingMode": "off"
}
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.3'

volumes:
pg_data:
static_todo_tracker:
media_todo_tracker:


services:
db:
image: postgres:13
volumes:
- pg_data:/var/lib/postgresql/data
env_file:
- .env

todo_tracker:
image: agas0077/todo_tracker:latest
env_file: .env
depends_on:
- db
restart: on-failure:2
volumes:
- static_todo_tracker:/app/staticfiles/
- media_todo_tracker:/app/media/
command: >
bash -c "python manage.py migrate
&& python manage.py collectstatic --no-input
&& gunicorn --bind 0.0.0.0:8000 todo_tracker.wsgi"
nginx:
depends_on:
- todo_tracker
image: nginx:1.19.3
ports:
- "8004:80"
volumes:
- ./nginx/todo_tracker.conf:/etc/nginx/conf.d/default.conf
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- static_todo_tracker:/static
- media_todo_tracker:/media
2 changes: 2 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM nginx:1.22.1
COPY nginx.conf /etc/nginx/conf.d/default.conf
32 changes: 32 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 100m;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}
18 changes: 18 additions & 0 deletions nginx/todo_tracker.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
server {
listen 80;

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header X-XSRF-TOKEN;
proxy_set_header Host $http_host;
proxy_pass http://todo_tracker:8000/;
}

location /static/ {
alias /static/;
}

location /media/ {
alias /media/;
}
}
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.black]
line-length = 79
include = 'todo_tracker/.*.py'
extend-exclude = '''
(
migrations/* |
static/*
)
'''
41 changes: 41 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[flake8]
accept-encodings = utf-8
statistics = False
max-line-length = 79
doctests = False
enable-extensions = G
isort-show-traceback = True

exclude =
.git,
.venv,
.vscode/*.py,
pycache,
/site-packages//*.py,
migrations

extend-ignore = E203

ignore =
C901,
F40,
E211,
E225,
E226,
E231,
E303,
E711,
E712,

[isort]
profile = black
import_heading_stdlib=Standard Library
import_heading_thirdparty=Third Party Library
import_heading_localfolder=Local Modules
known_local_folder = src
line_length = 79
multi_line_output = 3
include_trailing_comma = True
force_grid_wrap = 0
use_parentheses = True
ensure_newline_before_comments = True
6 changes: 6 additions & 0 deletions todo_tracker/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.venv
.git
__pycache__
db.sqlite3
.env
.vscode
13 changes: 13 additions & 0 deletions todo_tracker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.9

WORKDIR /app

RUN pip install gunicorn==20.1.0

COPY ./requirements.txt .

RUN pip install -r /app/requirements.txt --no-cache-dir

COPY . .

CMD [ "gunicorn", "--bind", "0.0.0.0:8000", "todo_tracker.wsgi" ]
1 change: 1 addition & 0 deletions todo_tracker/api/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Third Party Library
from django.contrib import admin

# Register your models here.
5 changes: 3 additions & 2 deletions todo_tracker/api/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Third Party Library
from django.apps import AppConfig


class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'
default_auto_field = "django.db.models.BigAutoField"
name = "api"
1 change: 1 addition & 0 deletions todo_tracker/api/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Third Party Library
from django.db import models

# Create your models here.
20 changes: 10 additions & 10 deletions todo_tracker/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Standard Library
import base64
import datetime as dt

# Third Party Library
from django.core.files.base import ContentFile
from error_messages.ValidationErrors import DEADLINE_ERROR
from rest_framework import serializers
from rest_framework.relations import SlugRelatedField

from todo.models import Task
from error_messages.ValidationErrors import DEADLINE_ERROR


class DateField(serializers.DateTimeField):

def to_internal_value(self, value):
value = dt.datetime.strptime(value, r'%Y-%m-%d')
value = dt.datetime.strptime(value, r"%Y-%m-%d")
return super().to_internal_value(value)

def to_representation(self, value):
Expand All @@ -22,22 +22,22 @@ def to_representation(self, value):

class Base64ImageField(serializers.ImageField):
def to_internal_value(self, data):
if isinstance(data, str) and data.startswith('data:image'):
format, imgstr = data.split(';base64,')
ext = format.split('/')[-1]
data = ContentFile(base64.b64decode(imgstr), name='temp.' + ext)
if isinstance(data, str) and data.startswith("data:image"):
format, imgstr = data.split(";base64,")
ext = format.split("/")[-1]
data = ContentFile(base64.b64decode(imgstr), name="temp." + ext)

return super().to_internal_value(data)


class TaskSerializer(serializers.ModelSerializer):
created_by = SlugRelatedField(slug_field='username', read_only=True)
created_by = SlugRelatedField(slug_field="username", read_only=True)
image = Base64ImageField(required=False, allow_null=True)
create_date = DateField(required=False, read_only=True)
deadline_date = DateField(required=True)

class Meta:
fields = '__all__'
fields = "__all__"
model = Task

def validate_deadline_date(self, value):
Expand Down
1 change: 1 addition & 0 deletions todo_tracker/api/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Third Party Library
from django.test import TestCase

# Create your tests here.
Loading

0 comments on commit 8512671

Please sign in to comment.