diff --git a/.env b/.env index 63828bb..235203d 100644 --- a/.env +++ b/.env @@ -1,6 +1,6 @@ -export MYSQL_DB='evoting' -export MYSQL_User='root' -export MYSQL_Pass='root' -export MYSQL_Host='localhost' -export smtp_sender='webmail@iitp.ac.in' -export smtp_pass='password' +MYSQL_DATABASE=evoting_db +MYSQL_PASSWORD=root +MYSQL_ROOT_PASSWORD=root +MYSQL_HOST=db +smtp_sender=your-webmail@iitp.ac.in +smtp_pass=password diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0b52bc0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.7 +ENV PYTHONUNBUFFERED 1 +RUN mkdir /evoting +WORKDIR /evoting +ADD . /evoting/ +RUN pip install -r REQUIREMENTS.txt && \ + chmod +x init_migrate_db.sh diff --git a/README.md b/README.md index 1106301..91df4e7 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,12 @@ Polling app with a simple, intuitive UX that also takes care of the complexities like privacy, integrity and security behind the scenes. +## Docker Deployment: +1. `docker-compose --env-file .env build` +2. `docker-compose --env-file .env up -d` +3. `docker-compose exec web bash /evoting/init_migrate_db.sh --load-template-data` + + ## apt/brew Dependencies 1. Python : >=3.5 2. Chrome : >=68 (for running functional tests) diff --git a/REQUIREMENTS.txt b/REQUIREMENTS.txt index 94b0473..9ae552a 100644 --- a/REQUIREMENTS.txt +++ b/REQUIREMENTS.txt @@ -1,3 +1,4 @@ -django -pymysql -selenium +django==3.0.2 +pymysql==0.9.3 +mysqlclient==1.3.13 +selenium \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..470df68 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "2.1" +services: + db: + image: mysql:5.7 + ports: + - '3306:3306' + env_file: + - .env + restart: on-failure + healthcheck: + test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] + timeout: 20s + retries: 10 + web: + build: . + command: python manage.py runserver 0.0.0.0:8000 + env_file: + - .env + # volumes: + # - .:/evoting + ports: + - "8000:8000" + restart: on-failure + depends_on: + db: + condition: service_healthy \ No newline at end of file diff --git a/evoting/__init__.py b/evoting/__init__.py index aa60bed..133b9cc 100644 --- a/evoting/__init__.py +++ b/evoting/__init__.py @@ -1,3 +1,7 @@ import pymysql +# Pre-Docker deploy debug: +# https://stackoverflow.com/a/59591269/2736932 +pymysql.version_info = (1, 3, 13, "final", 0) + pymysql.install_as_MySQLdb() \ No newline at end of file diff --git a/evoting/mysql.conf b/evoting/mysql.conf deleted file mode 100644 index 13707df..0000000 --- a/evoting/mysql.conf +++ /dev/null @@ -1,5 +0,0 @@ -[client] -database = 'evoting' -user = 'root' -password = 'toor' -default-character-set = utf8 \ No newline at end of file diff --git a/evoting/settings.py b/evoting/settings.py index 02f140f..03ae8f5 100644 --- a/evoting/settings.py +++ b/evoting/settings.py @@ -82,11 +82,11 @@ # } DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.mysql', - 'NAME': os.environ.get('MYSQL_DB'), - 'USER': os.environ.get('MYSQL_User'), - 'PASSWORD': os.environ.get('MYSQL_Pass'), - 'HOST': os.environ.get('MYSQL_Host'), + 'ENGINE': 'django.db.backends.mysql', + 'NAME': os.environ.get('MYSQL_DATABASE'), + 'USER': 'root', # os.environ.get('MYSQL_USER') + 'PASSWORD': os.environ.get('MYSQL_ROOT_PASSWORD'), + 'HOST': os.environ.get('MYSQL_HOST'), 'PORT': '3306', } } diff --git a/init_migrate_db.sh b/init_migrate_db.sh new file mode 100755 index 0000000..cfbb206 --- /dev/null +++ b/init_migrate_db.sh @@ -0,0 +1,23 @@ +#!/bin/bash +set -e + +python manage.py makemigrations +python manage.py migrate +if [[ $? -ne 0 ]]; then + echo "Couldn't load migrations into DB." + exit 1 +fi +if [ $1 == "--load-template-data" ]; then + if [[ ! -f /evoting/templateMigrationData.lock ]]; then + python manage.py loaddata /evoting/templateMigrationData.json + now="$(date)" + echo "templateMigrationData loaded at $now" > /evoting/templateMigrationData.lock + echo "Loaded templateMigrationData.json data into DB." + else + echo "Unable to load templateMigrationData.json as already loaded once:" + cat /evoting/templateMigrationData.lock + echo "Reloading would result in losing all generated data in the DB." + fi +else + echo "Not loading tempalte data. Use '--load-template-data'." +fi