Commit e4d7f839 authored by David Haynes's avatar David Haynes 🙆
Browse files

Merge branch '32-mysql-move' into 'upgrade-django'

Resolve "Move to MySQL as the backend database"

See merge request !14
parents 182d3fef 51bf279f
Pipeline #1217 passed with stage
in 47 seconds
services:
- mysql:latest
variables:
MYSQL_DATABASE: wopen
MYSQL_ROOT_PASSWORD: root
types:
- test
before_script:
- apt-get update -qy
- apt-get install -y mysql-client libmysqlclient-dev python-mysqldb
- pip install -r requirements/test.txt
- cd whats_open/
- export WOPEN_SECRET_KEY=$(dd if=/dev/urandom count=100 | tr -dc "A-Za-z0-9" | fold -w 60 | head -n1 2>/dev/null)
- export WOPEN_EMAIL_DOMAIN="@masonlive.gmu.edu"
- export WOPEN_DB_NAME="wopen"
- export WOPEN_DB_USER="root"
- export WOPEN_DB_PASSWORD="root"
- export WOPEN_DB_HOST="mysql"
- export WOPEN_DB_PORT=3306
- export WOPEN_SUPERUSER=admin
- python manage.py makemigrations
- python manage.py makemigrations website
- python manage.py migrate
......
......@@ -2,72 +2,16 @@
# Dockerfile to build What's Open Django App
############################################################
# Instructions:
#
# Note: You need to edit the empty string on the line that
# says "ENV SECRET_KEY" to be a secure random value.
#
# Any initialized data that needs to be imported should be
# placed in a directory called "fixtures" in the context
# of the build.
# (https://docs.djangoproject.com/en/dev/howto/initial-data/)
#
# Build Command: sudo docker build -t whats_open .
# Run Command: sudo docker run -p 8000:80 -i -t -d whats_open
#
# (You'll need to reverse proxy port 8000 via nginx)
# Set the base image to Ubuntu
FROM python:3.6
ENV PYTHONUNBUFFERED 1
# File Author / Maintainer
# MAINTAINER Student-Run Computing and Technology - GMU
# Update the sources list
RUN apt-get update
RUN apt-get install netcat -y
RUN mkdir /whats_open
WORKDIR /whats_open
ADD /requirements/ /whats_open/
RUN pip install -r base.txt
ADD . /whats_open/
# Install basic applications
# RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
# # Install Python and Basic Python Tools
# RUN apt-get install -y python python-dev python-distribute python-pip
# # Clone down SRCT-Web
# RUN git clone https://github.com/srct/whats-open.git whats-open
# # Get pip to download and install requirements:
# RUN pip install -r /whats-open/requirements.txt
# # Set this to a unique, secure value before building
# # (http://www.miniwebtool.com/django-secret-key-generator/)
# ENV SECRET_KEY ""
# # Set the default directory where CMD will execute
# WORKDIR /whats-open/whats_open
# # Setup database
# RUN python manage.py syncdb --noinput
# RUN python manage.py migrate website --noinput
# # Generate static files in the STATIC_ROOT location
# # (https://docs.djangoproject.com/en/dev/howto/static-files/deployment/)
# RUN python manage.py collectstatic --noinput
# # Add any inital data fixtures
# # (https://docs.djangoproject.com/en/dev/howto/initial-data/)
# ADD fixtures /whats-open/whats_open/
# # Import the loaded fixtures
# RUN python manage.py loaddata users schedules
# # Expose ports
# EXPOSE 80
# # Use Gunicorn to server the application
# CMD gunicorn whats_open.wsgi:application -b 0.0.0.0:80
......@@ -160,15 +160,106 @@ Open a terminal and run the following command:
Next, with:
sudo apt install python3 python3-dev python3-pip
sudo apt install python3 python3-dev python3-pip
sudo pip3 install virtualenv
you install `python`, `pip`, and `virtualenv`.
Next with,
#### Database Setup
What's Open is built on top of a `MySQL` database and thus, we must set it up.
Run:
sudo apt install mysql-server mysql-client libmysqlclient-dev python-mysqldb
to install mysql packages onto your system.
Load up the mysql shell by running
mysql -u root -p
and putting in your mysql password.
Create the database by running:
CREATE DATABASE wopen;
You can choose a different name for your database if you desire.
Double check your database was created by running:
SHOW DATABASES;
Though you can use an existing user to access this database, here's how to create
a new user and give them the necessary permissions to your newly created database:
CREATE USER 'wopen'@'localhost' IDENTIFIED BY 'password';
For local development, password strength is less important, but use a strong
passphrase for deployment. You can choose a different username.
GRANT ALL ON wopen.* TO 'wopen'@'localhost';
This allows your database user to create all the tables it needs on the What's
Open database.
Run:
GRANT ALL ON test_wopen.* TO 'wopen'@'localhost'; FLUSH PRIVILEGES;
When running test cases, django creates a test database so your 'real' database
doesn't get screwed up. This database is called 'test_' + whatever your normal
database is named. Note that for permissions it doesn't matter that this database
hasn't yet been created.
The .* is to grant access all tables in the database, and 'flush privileges'
reloads privileges to ensure that your user is ready to go.
Exit the mysql shell by typing:
exit
At this point we will need to set some environment variables.
If you are using bash then just copy paste the following into your terminal:
```
export WOPEN_SECRET_KEY=$(dd if=/dev/urandom count=100 | tr -dc "A-Za-z0-9" | fold -w 60 | head -n1 2>/dev/null)
export WOPEN_EMAIL_DOMAIN="@masonlive.gmu.edu"
export WOPEN_DB_NAME="wopen"
export WOPEN_DB_USER="wopen"
export WOPEN_DB_PASSWORD="password"
export WOPEN_DB_PORT=3306
export WOPEN_DB_HOST=
```
## The Virtual Enviornment
Virtual environments are used to keep separate project packages from the main
computer, so you can use different versions of packages across different
projects and ease deployment server setup.
It's often recommended to create a special directory to store all of your
virtual environments together (ie. development/virtualenv/), though they can be
placed wherever is most convenient.
Then in your virtual environment directory run:
virtualenv -p python3 whats_open
source whats_open/bin/activate
to create your virtual environment and activate it. If you ever need to exit
your virtual environment, simply run:
deactivate
Now, the packages you need to install for Go are in in the top level of the
project's directory structure(whats-open/).
Next with,
pip install -r requirements.txt
cd whats_open/
python3 manage.py makemigrations
......
version: '2'
services:
web:
wopen_web:
build: .
restart: always
ports:
- '8000:8000'
command: /bin/bash ./startup.sh -python whats_open/manage.py runserver 0.0.0.0:8000
command: /bin/bash ./startup.sh
volumes:
- .:/whats_open
environment:
- superuser=admin
- email_domain=@masonlive.gmu.edu
\ No newline at end of file
depends_on:
- wopen_db
environment:
- WOPEN_EMAIL_DOMAIN=@masonlive.gmu.edu
- WOPEN_DB_NAME=wopen
- WOPEN_DB_USER=wopen
- WOPEN_DB_PASSWORD=wopen
- WOPEN_DB_HOST=wopen_db
- WOPEN_DB_PORT=3306
- WOPEN_SUPERUSER=admin
wopen_db:
image: mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_DATABASE: wopen
MYSQL_USER: wopen
MYSQL_PASSWORD: wopen
\ No newline at end of file
......@@ -2,4 +2,5 @@ Django>1.10, <1.11
django-autoslug==1.9.3
django-cas-client==1.2.0
djangorestframework==3.6.2
django-model-utils==2.6.1
\ No newline at end of file
django-model-utils==2.6.1
mysqlclient==1.3.10
\ No newline at end of file
until nc -z wopen_db 3306; do
echo "waiting for database to start..."
sleep 1
done
export WOPEN_SECRET_KEY=$(dd if=/dev/urandom count=100 | tr -dc "A-Za-z0-9" | fold -w 60 | head -n1 2>/dev/null)
python whats_open/manage.py flush --no-input
python whats_open/manage.py makemigrations
python whats_open/manage.py makemigrations website
python whats_open/manage.py migrate
echo "from django.contrib.auth.models import User; User.objects.filter(email='$superuser$email_domain').delete(); User.objects.create_superuser('$superuser$email_domain', '$superuser', 'admin')" | python whats_open/manage.py shell
echo "from django.contrib.auth.models import User; User.objects.filter(email='$WOPEN_SUPERUSER$WOPEN_EMAIL_DOMAIN').delete(); User.objects.create_superuser('$WOPEN_SUPERUSER$WOPEN_EMAIL_DOMAIN', '$WOPEN_SUPERUSER', 'admin')" | python whats_open/manage.py shell
python whats_open/manage.py runserver 0.0.0.0:8000
......@@ -55,25 +55,8 @@ class OpenTimeViewSet(viewsets.ModelViewSet):
queryset = OpenTime.objects.all()
serializer_class = OpenTimeSerializer
#def facility_grid(request):
# """Display the facilities in a grid. Main page."""
# if 'sort' in request.GET:
# if request.GET['sort'] == 'location':
# # Display the grid by location (instead of listing alphabetically)
# pass # Not implemented yet
# return render_to_response('facility_grid.html',
# context_instance=RequestContext(request))
def gen_etag(request):
return hashlib.sha1(str(OpenTime.objects.all())).hexdigest()
def gen_last_modified(request):
return TimeStampedModel.objects.all().order_by('-last_modified')[0].last_modified
#@condition(etag_func=gen_etag, last_modified_func=gen_last_modified)
#def ajax_schedule_data(request):
# # Wrapping up in an object to avoid possible CSRF attack on top-level
# # arrays in JSON objects
# return HttpResponse(json.dumps({'data': export_data()}, indent=4),
# content_type="application/json")
""""Base Django settings for whats_open."""
import os
import sys
from os.path import abspath, basename, dirname, join, normpath
from sys import path
......@@ -19,16 +20,6 @@ SITE_NAME = basename(DJANGO_ROOT)
path.append(DJANGO_ROOT)
########## END PATH CONFIGURATION
########## DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = False
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
TEMPLATE_DEBUG = DEBUG
########## END DEBUG CONFIGURATION
########## MANAGER CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
# Insert a ('Name', 'Email') inside ADMINS tuple
......@@ -40,22 +31,6 @@ ADMINS = (
MANAGERS = ADMINS
########## END MANAGER CONFIGURATION
########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': normpath(join(DJANGO_ROOT, 'database/database.db')),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
########## END DATABASE CONFIGURATION
########## GENERAL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
......@@ -124,7 +99,6 @@ ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
########## END STATIC FILE CONFIGURATION
......@@ -150,6 +124,19 @@ FIXTURE_DIRS = (
)
########## END FIXTURE CONFIGURATION
########## DATABASE CONFIGURATION
# Use the same DB everywhere.
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['WOPEN_DB_NAME'],
'USER': os.environ['WOPEN_DB_USER'],
'PASSWORD': os.environ['WOPEN_DB_PASSWORD'],
'HOST': os.environ['WOPEN_DB_HOST'],
'PORT': os.environ['WOPEN_DB_PORT'],
}
}
########## TEMPLATE CONFIGURATION
TEMPLATES = [
......@@ -178,8 +165,6 @@ TEMPLATES = [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
# https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
'debug': DEBUG
}
}
]
......@@ -227,7 +212,7 @@ CACHE_MIDDLEWARE_KEY_PREFIX = ''
########## APP CONFIGURATION
DJANGO_APPS = (
INSTALLED_APPS = (
# Default Django apps:
'django.contrib.auth',
'django.contrib.contenttypes',
......@@ -236,24 +221,15 @@ DJANGO_APPS = (
'django.contrib.messages',
'django.contrib.staticfiles',
# Useful template tags:
# 'django.contrib.humanize',
# Admin panel and documentation:
'django.contrib.admin',
'django.contrib.admindocs',
)
# Apps specific for this project go here.
LOCAL_APPS = (
# Apps specific for this project go here.
'website',
#'guardian',
'rest_framework',
)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
########## END APP CONFIGURATION
......@@ -294,7 +270,12 @@ LOGGING = {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'stream': sys.stdout
},
},
'loggers': {
'django.request': {
......@@ -302,19 +283,12 @@ LOGGING = {
'level': 'ERROR',
'propagate': True,
},
'django': {
'handlers': ['console'],
'level': 'INFO',
'propogate': True
},
}
}
########## END LOGGING CONFIGURATION
########## MANAGEMENT CONFIGURATION
#CAS_GATEWAY=True
#ANONYMOUS_USER_ID = -1
#AUTHENTICATION_BACKENDS = (
# 'django.contrib.auth.backends.ModelBackend', # this is default
# 'cas.middleware.CASMiddleware',
# #'guardian.backends.ObjectPermissionBackend',
#)
#
#LOGIN_URL='/management/login/'
#LOGIN_REDIRECT='/management/'
########## END MANAGEMENT CONFIGURATION
########## END LOGGING CONFIGURATION
......@@ -2,8 +2,6 @@
from __future__ import absolute_import
from os.path import join, normpath
from .base import *
......@@ -12,28 +10,6 @@ from .base import *
DEBUG = True
########## END DEBUG CONFIGURATION
########## EMAIL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
########## END EMAIL CONFIGURATION
########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': normpath(join(DJANGO_ROOT, 'database/database.db')),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
########## END DATABASE CONFIGURATION
########## CACHE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
CACHES = {
......
"""Production settings and globals."""
from __future__ import absolute_import
# Future Imports
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from os import environ
......@@ -24,45 +26,10 @@ def get_env_setting(setting):
ALLOWED_HOSTS = ['*']
########## END HOST CONFIGURATION
########## EMAIL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-host
EMAIL_HOST = environ.get('EMAIL_HOST', 'smtp.gmail.com')
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-host-password
EMAIL_HOST_PASSWORD = environ.get('EMAIL_HOST_PASSWORD', '')
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-host-user
EMAIL_HOST_USER = environ.get('EMAIL_HOST_USER', 'your_email@example.com')
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-port
EMAIL_PORT = environ.get('EMAIL_PORT', 587)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
EMAIL_SUBJECT_PREFIX = '[%s] ' % SITE_NAME
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-use-tls
EMAIL_USE_TLS = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#server-email
SERVER_EMAIL = EMAIL_HOST_USER
########## END EMAIL CONFIGURATION
########## DATABASE CONFIGURATION
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': normpath(join(DJANGO_ROOT, 'database.db')),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
########## END DATABASE CONFIGURATION
########## DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = False
########## END DEBUG CONFIGURATION
########## CACHE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
......@@ -73,7 +40,6 @@ CACHES = {
}
########## END CACHE CONFIGURATION
########## SECRET CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
SECRET_KEY = get_env_setting('SECRET_KEY')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment