Skip to content

Commit

Permalink
Merge pull request #1 from hackademymx/fix-heroku
Browse files Browse the repository at this point in the history
Added Heroku deploy files and improved boilerplate config
  • Loading branch information
martinezga authored Apr 7, 2022
2 parents be4ec00 + 9f9da19 commit 83ef63d
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 44 deletions.
28 changes: 28 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
**/__pycache__
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md
**/.vagrant
Vagrantfile
Procfile
25 changes: 13 additions & 12 deletions .env-example
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
ENV="dev"

DJANGO_SECRET_KEY="l%+z&)&7i8casdfjrfnuqkwc*ja!^sdfb2-m1l^3(y78+^+@5@q^!9eq1fg"
DJANGO_ALLOWED_HOSTS="*"
CORS_ALLOW_ALL_ORIGINS=""
CORS_ALLOWED_ORIGINS=""
CSRF_TRUSTED_ORIGINS=""
DJANGO_ALLOWED_HOSTS='*'
CORS_ALLOW_ALL_ORIGINS=True
CORS_ALLOWED_ORIGINS=
CSRF_TRUSTED_ORIGINS=
CSRF_COOKIE_SECURE=True
DEBUG=True


# DB variables
POSTGRES_DB="testing"
POSTGRES_USER="test"
POSTGRES_PASSWORD="test"
POSTGRES_HOST="localhost"
# DB variables para base de datos local
POSTGRES_DB=testing
POSTGRES_USER=test
POSTGRES_PASSWORD=test
POSTGRES_HOST=localhost
POSTGRES_PORT=5432

USE_DATABASE_URL=True
# DATABASE_URL format
# <database_type>://<username>:<password>@<hostname>:<database_port>/<database_name>

DATABASE_URL=""
DATABASE_URL=<database_type>://<username>:<password>@<hostname>:<database_port>/<database_name>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ cython_debug/

staticfiles/
.vscode
.vagrant/*
67 changes: 67 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8.12-bullseye as base

FROM base as builder

RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libpq-dev \
shared-mime-info \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir /install
WORKDIR /install

COPY requirements.txt /requirements.txt

RUN pip install --prefix=/install -r /requirements.txt

FROM python:3.8.12-slim-bullseye

EXPOSE 8000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

ENV LANG C.UTF-8

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
locales \
tzdata \
libpq-dev \
&& echo "America/Mazatlan" > /etc/timezone \
&& dpkg-reconfigure tzdata \
&& sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
&& echo 'LANG="en_US.UTF-8"'>/etc/default/locale \
&& dpkg-reconfigure --frontend=noninteractive locales \
&& update-locale LANG=en_US.UTF-8 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

COPY --from=builder /install /usr/local

COPY entrypoint.sh /usr/local/bin/entrypoint.sh

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN groupadd -g 1000 appuser \
&& useradd --no-log-init --shell /bin/bash -u 1000 -g 1000 -o -c "" -m appuser \
&& cp -r /etc/skel/. /home/appuser \
&& chown -R 1000:1000 /home/appuser

USER appuser

COPY --chown=appuser:appuser . /home/appuser/app

WORKDIR /home/appuser/app/api

ENTRYPOINT [ "/usr/local/bin/entrypoint.sh" ]

#CMD gunicorn --bind 0.0.0.0:$PORT core.wsgi --error-logfile - --access-logfile - --workers 4
CMD gunicorn --bind 0.0.0.0:$PORT core.wsgi
24 changes: 24 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8.12-bullseye

EXPOSE 8000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN groupadd -g 1000 appuser \
&& useradd --no-log-init --shell /bin/bash -u 1000 -g 1000 -o -c "" -m appuser \
&& cp -r /etc/skel/. /home/appuser \
&& chown -R 1000:1000 /home/appuser

USER appuser

WORKDIR /home/appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
41 changes: 19 additions & 22 deletions api/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
SECRET_KEY = config('DJANGO_SECRET_KEY', default='change_this_in_production!')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
DEBUG = config('DEBUG', default=False, cast=bool)

ALLOWED_HOSTS = config('DJANGO_ALLOWED_HOSTS', cast=Csv(), default='')
ALLOWED_HOSTS = config('DJANGO_ALLOWED_HOSTS', cast=Csv(), default='*')

CORS_ALLOW_ALL_ORIGINS = config('CORS_ALLOW_ALL_ORIGINS', default=False, cast=bool)

CORS_ALLOWED_ORIGINS = config('CORS_ALLOWED_ORIGINS', default='')
CORS_ALLOWED_ORIGINS = config('CORS_ALLOWED_ORIGINS', cast=Csv(), default='')

CSRF_TRUSTED_ORIGINS = config('CSRF_TRUSTED_ORIGINS', default='')
CSRF_TRUSTED_ORIGINS = config('CSRF_TRUSTED_ORIGINS', cast=Csv(), default='')

CSRF_COOKIE_SECURE = config('CSRF_COOKIE_SECURE', default=False, cast=bool)

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand Down Expand Up @@ -99,21 +99,28 @@
# }
# }

PG_DB = config('POSTGRES_DB', default='')
PG_USER = config('POSTGRES_USER', default='')
PG_PASSWD = config('POSTGRES_PASSWORD', default='')
PG_HOST = config('POSTGRES_HOST', default='127.0.0.1')
PG_PORT = config('POSTGRES_PORT', cast=int, default=5432)

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('POSTGRES_DB', default=''),
'USER': config('POSTGRES_USER', default=''),
'PASSWORD': config('POSTGRES_PASSWORD', default=''),
'HOST': config('POSTGRES_HOST', default=''),
'PORT': config('POSTGRES_PORT', default=''),
'NAME': PG_DB,
'USER': PG_USER,
'PASSWORD': PG_PASSWD,
'HOST': PG_HOST,
'PORT': PG_PORT,
}
}

DATABASE_URL = config('DATABASE_URL', default='')
USE_DATABASE_URL = config('USE_DATABASE_URL', cast=bool, default=False)

if DATABASE_URL:
if USE_DATABASE_URL:
import dj_database_url
DATABASE_URL = config('DATABASE_URL', default='')
db_from_env_django = dj_database_url.parse(DATABASE_URL, conn_max_age=600, ssl_require=True)
DATABASES['default'].update(db_from_env_django)

Expand Down Expand Up @@ -176,13 +183,3 @@
'rest_framework.parsers.JSONParser',
]
}

# Always keep the following lines at the end of this file

if ENV == 'dev':
from .dev import *
elif ENV == 'qa':
from .qa import *
elif ENV == 'prod':
# Extra settings for production
from .prod import *
6 changes: 1 addition & 5 deletions api/core/settings/dev.py
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
DEBUG = True

ALLOWED_HOSTS = ['*']

CORS_ALLOW_ALL_ORIGINS = True
from base import *
1 change: 1 addition & 0 deletions api/core/settings/prod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from base import *
1 change: 1 addition & 0 deletions api/core/settings/qa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from base import *
Empty file modified api/manage.py
100755 → 100644
Empty file.
8 changes: 3 additions & 5 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ asgiref==3.5.0; python_version >= '3.7'
backports.zoneinfo==0.2.1; python_version < '3.9'
black==22.1.0
brotli==1.0.9
click==8.0.4; python_version >= '3.6'
click==8.1.2; python_version >= '3.7'
dj-database-url==0.5.0
django-cors-headers==3.11.0
django-heroku==0.3.1
django==4.0.3
djangorestframework==3.13.1
flake8==4.0.1
Expand All @@ -27,12 +26,11 @@ mypy-extensions==0.4.3
pathspec==0.9.0
platformdirs==2.5.1; python_version >= '3.7'
psycopg2-binary==2.9.3
psycopg2==2.9.3; python_version >= '3.6'
pycodestyle==2.8.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
pyflakes==2.4.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
python-decouple==3.6
pytz==2021.3
setuptools==60.9.3; python_version >= '3.7'
pytz==2022.1
setuptools==62.0.0; python_version >= '3.7'
sqlparse==0.4.2; python_version >= '3.5'
tomli==2.0.1; python_version >= '3.7'
typing-extensions==4.1.1; python_version < '3.10'
Expand Down
32 changes: 32 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3.4'

services:
equipoback:
image: equipoback
build:
context: .
dockerfile: ./Dockerfile.dev
volumes:
- ./api:/home/appuser/app
ports:
- 8000:8000
tty: true
stdin_open: true
command: /bin/bash

db:
image: postgres:13
restart: unless-stopped
ports:
- 5435:5432
environment:
- name=value
- POSTGRES_PASSWORD=passdb
- POSTGRES_USER=db_user
- POSTGRES_DB=db_name
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:

10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.4'

services:
equipoback:
image: equipoback
build:
context: .
dockerfile: ./Dockerfile
ports:
- 8000:8000
7 changes: 7 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

cd /home/appuser/app/api

python manage.py migrate

exec "$@"
8 changes: 8 additions & 0 deletions heroku.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
setup:
addons:
- plan: heroku-postgresql
as: DATABASE
build:
docker:
web: Dockerfile
#Esto es opcional, si no esta presente usa el CMD del dockerfile

0 comments on commit 83ef63d

Please sign in to comment.