feat: productionize images for deploy

Signed-off-by: Ameya Shenoy <shenoy.ameya@gmail.com>
This commit is contained in:
Ameya Shenoy 2021-02-12 00:58:26 +05:30
parent 0b53c00be8
commit fd61d1231f
Signed by: codingcoffee
GPG key ID: F7D58AAC5DACF8D3
13 changed files with 155 additions and 18 deletions

View file

@ -12,15 +12,28 @@ RUN set -ex \
&& apk add --no-cache --virtual .build-deps \ && apk add --no-cache --virtual .build-deps \
gcc \ gcc \
musl-dev \ musl-dev \
linux-headers \
postgresql-dev \ postgresql-dev \
python3-dev \ python3-dev \
&& apk add --no-cache \ && apk add --no-cache \
libpq \ libpq \
&& pip install -r /requirements.txt \ && pip install --no-cache-dir uwsgi==2.0.19.1 \
&& pip install --no-cache-dir -r /requirements.txt \
&& rm -rf requirements.txt \ && rm -rf requirements.txt \
&& apk del .build-deps && apk del .build-deps
COPY . /code COPY . /code
CMD sh /code/entrypoint.sh ARG APP_USER=app
RUN addgroup -S ${APP_USER} && adduser -S ${APP_USER} -G ${APP_USER}
EXPOSE 8000
ENV DJANGO_SETTINGS_MODULE=backend.settings
ENV UWSGI_WSGI_FILE=backend/wsgi.py
ENV UWSGI_HTTP=:8000 UWSGI_MASTER=1 UWSGI_HTTP_AUTO_CHUNKED=1 UWSGI_HTTP_KEEPALIVE=1 UWSGI_LAZY_APPS=1 UWSGI_WSGI_ENV_BEHAVIOR=holy
ENV UWSGI_WORKERS=2 UWSGI_THREADS=4
ENV UWSGI_STATIC_MAP="/static/=/code/static/" UWSGI_STATIC_EXPIRES_URI="/static/.*\.[a-f0-9]{12,}\.(css|js|png|jpg|jpeg|gif|ico|woff|ttf|otf|svg|scss|map|txt) 315360000"
USER ${APP_USER}:${APP_USER}
ENTRYPOINT ["sh", "/code/prod-entrypoint.sh"]

26
backend/Dockerfile.dev Normal file
View file

@ -0,0 +1,26 @@
FROM python:3.9.1-alpine3.13
MAINTAINER "Ameya Shenoy <shenoy.ameya@gmail.com>"
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /
RUN set -ex \
&& apk add --no-cache --virtual .build-deps \
gcc \
musl-dev \
postgresql-dev \
python3-dev \
&& apk add --no-cache \
libpq \
&& pip install -r /requirements.txt \
&& rm -rf requirements.txt \
&& apk del .build-deps
COPY . /code
CMD sh /code/entrypoint.sh

View file

@ -41,9 +41,8 @@ def fetch_bhav_copy_equity_data(curr_date=None):
with ZipFile(BytesIO(resp.content)) as bhavcopy_zf: with ZipFile(BytesIO(resp.content)) as bhavcopy_zf:
csv_file = bhavcopy_zf.namelist()[0] csv_file = bhavcopy_zf.namelist()[0]
with bhavcopy_zf.open(csv_file, 'r') as infile: with bhavcopy_zf.open(csv_file, 'r') as infile:
data = list(csv.reader(TextIOWrapper(infile, 'utf-8'))) # TODO: potentially remove list data = list(csv.reader(TextIOWrapper(infile, 'utf-8')))
return data return data
# schedule again in 5 minutes
return None return None
@ -96,8 +95,8 @@ def populate_bhav_copy_data():
tdcloindi=stock[13], tdcloindi=stock[13],
) )
except: except:
# potential code for alerting if needed goes here # Potentially add code for alerting if needed
# Repeat job after 10 mins if fails at 6 pm # Repeat job after 10 mins if job fails
scheduler = django_rq.get_scheduler('default') scheduler = django_rq.get_scheduler('default')
scheduler.schedule( scheduler.schedule(
scheduled_time=datetime.datetime.now()+datetime.timedelta(minutes=10), scheduled_time=datetime.datetime.now()+datetime.timedelta(minutes=10),

View file

@ -42,6 +42,7 @@ class EmptyRespoinseView(generics.RetrieveAPIView):
class BhavCopyEquityCustomRedisView(generics.RetrieveAPIView): class BhavCopyEquityCustomRedisView(generics.RetrieveAPIView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
# TODO: try stringified json
pipe = cache.pipeline() pipe = cache.pipeline()
stocks = cache.lrange("stocks", 0, -1) stocks = cache.lrange("stocks", 0, -1)
if len(stocks) == 0: if len(stocks) == 0:

View file

@ -22,13 +22,14 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# TODO: remove this key # TODO: remove this key
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
# SECRET_KEY = 'eo$1gb^g#gx25$b$(9hp#i^av^t0e8ge#kjcr$g(=yimc#j1-l'
SECRET_KEY = os.environ.get('SECRET_KEY') SECRET_KEY = os.environ.get('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(os.environ.get('DEBUG')) DEBUG = bool(os.environ.get('DEBUG'))
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS').split(',') ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS')
if ALLOWED_HOSTS:
ALLOWED_HOSTS = ALLOWED_HOSTS.split(',')
# Application definition # Application definition
@ -143,7 +144,9 @@ USE_L10N = True
USE_TZ = True USE_TZ = True
# TODO: fix hardcoded cross allowed origins # TODO: fix hardcoded cross allowed origins
CORS_ALLOWED_ORIGINS = os.environ.get('CORS_ALLOWED_ORIGINS').split(',') CORS_ALLOWED_ORIGINS = os.environ.get('CORS_ALLOWED_ORIGINS')
if CORS_ALLOWED_ORIGINS:
CORS_ALLOWED_ORIGINS = CORS_ALLOWED_ORIGINS.split(',')
RQ_QUEUES = { RQ_QUEUES = {
'default': { 'default': {

View file

@ -0,0 +1,11 @@
#!/bin/sh
# Apply database migrations
echo "Apply database migrations"
python manage.py migrate collectstatic --noinput
# Start server
echo "Starting server"
uwsgi --show-config

View file

@ -4,9 +4,10 @@ version: '3.8'
services: services:
backend: backend:
image: codingcoffee/bullish-backend image: codingcoffee/bullish-backend-dev
build: build:
context: ./backend context: ./backend
dockerfile: Dockerfile.dev
volumes: volumes:
- ./backend:/code - ./backend:/code
env_file: env_file:
@ -20,9 +21,10 @@ services:
- 8000:8000 - 8000:8000
frontend: frontend:
image: codingcoffee/bullish-frontend image: codingcoffee/bullish-frontend-dev
build: build:
context: ./frontend context: ./frontend
dockerfile: Dockerfile.dev
volumes: volumes:
- ./frontend:/code - ./frontend:/code
ports: ports:
@ -34,9 +36,10 @@ services:
# - 6379:6379 # - 6379:6379
rqworker: rqworker:
image: codingcoffee/bullish-backend image: codingcoffee/bullish-backend-dev
build: build:
context: ./backend context: ./backend
dockerfile: Dockerfile.dev
env_file: env_file:
- dev.env - dev.env
volumes: volumes:
@ -46,9 +49,10 @@ services:
- redis - redis
rqscheduler: rqscheduler:
image: codingcoffee/bullish-backend image: codingcoffee/bullish-backend-dev
build: build:
context: ./backend context: ./backend
dockerfile: Dockerfile.dev
env_file: env_file:
- dev.env - dev.env
volumes: volumes:
@ -62,7 +66,6 @@ services:
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- bullish-db-data:/var/lib/postgresql/data/pgdata - bullish-db-data:/var/lib/postgresql/data/pgdata
# TODO: remove pass
env_file: env_file:
- dev.env - dev.env
# ports: # ports:

2
frontend/.dockerignore Normal file
View file

@ -0,0 +1,2 @@
**/node_modules
**/dist

4
frontend/.vuerc Normal file
View file

@ -0,0 +1,4 @@
{
"packageManager": "npm",
"useTaobaoRegistry": false
}

View file

@ -1,11 +1,19 @@
FROM node:15.8.0-alpine3.10 FROM node:15.8.0-alpine3.10 as builder
MAINTAINER "Ameya Shenoy <shenoy.ameya@gmail.com>" MAINTAINER "Ameya Shenoy <shenoy.ameya@gmail.com>"
WORKDIR /code WORKDIR /code
COPY package.json package-lock.json /code/
COPY .vuerc /root/
RUN set -ex \ RUN set -ex \
npm i -g npm \ && npm i -g npm@7.5.3 @vue/cli \
npm i && npm i
COPY src /code/src
RUN NODE_ENV=production npm run build
FROM nginx:1.19.6-alpine
COPY --from=builder /code/dist/ /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
ENTRYPOINT sh /code/entrypoint.sh

12
frontend/Dockerfile.dev Normal file
View file

@ -0,0 +1,12 @@
FROM node:15.8.0-alpine3.10
MAINTAINER "Ameya Shenoy <shenoy.ameya@gmail.com>"
WORKDIR /code
RUN set -ex \
&& npm i -g npm \
&& npm i
ENTRYPOINT sh /code/entrypoint.sh

43
frontend/nginx.conf Normal file
View file

@ -0,0 +1,43 @@
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;
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;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}

12
prod-build.sh Executable file
View file

@ -0,0 +1,12 @@
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$DIR/backend"
docker build -t codingcoffee/bullish-backend .
cd "$DIR/frontend"
docker build -t codingcoffee/bullish-frontend .