Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.git
.github
.venv
*.pyc
__pycache__
*.egg-info
tags

# Docker
docker/
!docker/entrypoint.sh
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ celerybeat-schedule

# dotenv
.env
docker/.env.*
!docker/.env.*.example

# virtualenv
.venv
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,21 @@ PostgreSQL 11, la imagen `pgautoupgrade` se encargará de migrar automáticament
datos al formato de PostgreSQL 14 en el primer arranque. Este proceso puede tardar
unos minutos dependiendo del tamaño de la base de datos.

Se puede editar el fichero docker-settings.py para modificar el settings
del proyecto django antes de crear las imágenes del contenedor.
La configuración se realiza mediante variables de entorno. Copiar los
ficheros de ejemplo y editarlos según sea necesario:

$ cd docker
$ cp .env.decide.example .env.decide
$ cp .env.postgres.example .env.postgres

Crear imágenes y lanzar contenedores:

$ cd docker
$ docker-compose up -d

Generar ficheros estáticos (solo si es necesario):

$ docker exec -ti decide_web ./manage.py collectstatic

Parar contenedores:

$ docker-compose down
Expand Down
52 changes: 41 additions & 11 deletions decide/decide/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

import os


def env_list(env_name, default=None):
"""Get environment var and convert in python list. Example .env: APPS=x1,y2,z3"""
if default is None:
default = []
list_vars = os.environ.get(env_name, None)
return list_vars.split(",") if list_vars else default


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -20,12 +29,12 @@
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^##ydkswfu0+=ofw0l#$kv^8n)0$i(qd&d&ol#p9!b$8*5%j1+'
SECRET_KEY = os.environ.get('SECRET_KEY', '^##ydkswfu0+=ofw0l#$kv^8n)0$i(qd&d&ol#p9!b$8*5%j1+')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = os.environ.get('DEBUG', 'True') == 'True'

ALLOWED_HOSTS = []
ALLOWED_HOSTS = env_list('ALLOWED_HOSTS', [])


# Application definition
Expand Down Expand Up @@ -78,7 +87,19 @@
'voting',
]

BASEURL = 'http://localhost:8000'
BASEURL = os.environ.get('BASEURL', 'http://localhost:8000')

APIS = {
'authentication': os.environ.get('API_AUTHENTICATION', BASEURL),
'base': os.environ.get('API_BASE', BASEURL),
'booth': os.environ.get('API_BOOTH', BASEURL),
'census': os.environ.get('API_CENSUS', BASEURL),
'mixnet': os.environ.get('API_MIXNET', BASEURL),
'postproc': os.environ.get('API_POSTPROC', BASEURL),
'store': os.environ.get('API_STORE', BASEURL),
'visualizer': os.environ.get('API_VISUALIZER', BASEURL),
'voting': os.environ.get('API_VOTING', BASEURL),
}

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
Expand Down Expand Up @@ -116,12 +137,12 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'decide',
'USER': 'decide',
'PASSWORD': 'decide',
'HOST': 'localhost',
'PORT': '5432',
'ENGINE': os.environ.get('DB_ENGINE', 'django.db.backends.postgresql'),
'NAME': os.environ.get('DB_NAME', 'decide'),
'USER': os.environ.get('DB_USER', 'decide'),
'PASSWORD': os.environ.get('DB_PASSWORD', 'decide'),
'HOST': os.environ.get('DB_HOST', 'localhost'),
'PORT': os.environ.get('DB_PORT', '5432'),
}
}

Expand Down Expand Up @@ -160,7 +181,16 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'
STATIC_URL = os.environ.get('STATIC_URL', '/static/')
STATIC_ROOT = os.environ.get('STATIC_ROOT', os.path.join(BASE_DIR, 'static'))

MEDIA_URL = os.environ.get('MEDIA_URL', '/media/')
MEDIA_ROOT = os.environ.get('MEDIA_ROOT', os.path.join(BASE_DIR, 'media'))

CSRF_TRUSTED_ORIGINS = env_list('CSRF_TRUSTED_ORIGINS', [])

_secure_proxy_ssl = env_list('SECURE_PROXY_SSL_HEADER')
SECURE_PROXY_SSL_HEADER = tuple(_secure_proxy_ssl) if _secure_proxy_ssl else None

# number of bits for the key, all auths should use the same number of bits
KEYBITS = 256
Expand Down
40 changes: 40 additions & 0 deletions docker/.env.decide.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
SECRET_KEY=^##ydkswfu0+=ofw0l#$kv^8n)0$i(qd&d&ol#p9!b$8*5%j1+
DEBUG=True

# Database
DB_ENGINE=django.db.backends.postgresql
DB_NAME=postgres
DB_USER=postgres
DB_PASSWORD=
DB_HOST=db
DB_PORT=5432

# Hosts
ALLOWED_HOSTS=*
CSRF_TRUSTED_ORIGINS=http://10.5.0.1:8000
BASEURL=http://10.5.0.1:8000

# Static & Media
STATIC_URL=/static/
STATIC_ROOT=/app/static/
MEDIA_URL=/media/
MEDIA_ROOT=/app/static/media/

# Gunicorn
GUNICORN_WORKERS=5
GUNICORN_BIND=0.0.0.0:5000
GUNICORN_TIMEOUT=500

# SSL Proxy (uncomment and set if behind a reverse proxy with SSL)
#SECURE_PROXY_SSL_HEADER=HTTP_X_FORWARDED_PROTO,https

# APIs (default to BASEURL if not set)
#API_AUTHENTICATION=http://10.5.0.1:8000
#API_BASE=http://10.5.0.1:8000
#API_BOOTH=http://10.5.0.1:8000
#API_CENSUS=http://10.5.0.1:8000
#API_MIXNET=http://10.5.0.1:8000
#API_POSTPROC=http://10.5.0.1:8000
#API_STORE=http://10.5.0.1:8000
#API_VISUALIZER=http://10.5.0.1:8000
#API_VOTING=http://10.5.0.1:8000
1 change: 1 addition & 0 deletions docker/.env.postgres.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_PASSWORD=
13 changes: 6 additions & 7 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ RUN pip install gunicorn==25.0.3

WORKDIR /app

RUN git clone https://github.com/decide-update-4-1/decide-update-4.1.git .
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .

WORKDIR /app/decide

# local settings.py
ADD docker-settings.py /app/decide/local_settings.py

RUN ./manage.py collectstatic

#CMD ["gunicorn", "-w 5", "decide.wsgi", "--timeout=500", "-b 0.0.0.0:5000"]
ENTRYPOINT ["/entrypoint.sh"]
11 changes: 7 additions & 4 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ services:
- db:/var/lib/postgresql/data
networks:
- decide
environment:
- POSTGRES_PASSWORD=
env_file:
- .env.postgres
web:
restart: always
container_name: decide_web
image: decide_web:latest
build: .
command: ash -c "python manage.py collectstatic --noinput --clear && python manage.py migrate && gunicorn -w 5 decide.wsgi --timeout=500 -b 0.0.0.0:5000"
build:
context: ..
dockerfile: docker/Dockerfile
expose:
- "5000"
volumes:
Expand All @@ -25,6 +26,8 @@ services:
- db
networks:
- decide
env_file:
- .env.decide
nginx:
restart: always
container_name: decide_nginx
Expand Down
45 changes: 0 additions & 45 deletions docker/docker-settings.py

This file was deleted.

28 changes: 28 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh
set -e

GUNICORN_WORKERS=${GUNICORN_WORKERS:-5}
GUNICORN_BIND=${GUNICORN_BIND:-"0.0.0.0:5000"}
GUNICORN_TIMEOUT=${GUNICORN_TIMEOUT:-500}

COMMAND=${1:-""}

case "$COMMAND" in
start)
exec gunicorn -w "$GUNICORN_WORKERS" decide.wsgi --timeout="$GUNICORN_TIMEOUT" -b "$GUNICORN_BIND"
;;
migrate)
exec python manage.py migrate
;;
collectstatic)
exec python manage.py collectstatic --noinput
;;
manage)
shift
exec python manage.py "$@"
;;
*)
python manage.py migrate
exec gunicorn -w "$GUNICORN_WORKERS" decide.wsgi --timeout="$GUNICORN_TIMEOUT" -b "$GUNICORN_BIND"
;;
esac