install_requirements.sh 4.07 KB
#!/bin/bash

# prevent globbing interpreting (for SQL_SCRIPT variable)
set -f

PYTHON='python3.5'
INSTALL_DIR='install'
VENV_NAME="venv_py3_pyros"

USERNAME="root"

DB_USER="pyros"
DB_PASSWORD="DjangoPyros"
MYSQL=0

# String variables

ASK_USE_MYSQL="-Do you wish to use mysql in your project ? Only if mysql-server is installed. (Y/N)"
ASK_MYSQL_USERNAME="-MySQL server login (default 'root'): "
SQL_SCRIPT="CREATE DATABASE IF NOT EXISTS pyros; GRANT USAGE ON *.* TO '$DB_USER'; DROP USER '$DB_USER'; CREATE USER '$DB_USER' IDENTIFIED BY '$DB_PASSWORD'; GRANT ALL PRIVILEGES ON pyros.* TO $DB_USER;"
SQL_FAIL="-Failed to execute database script, probably due to a wrong username or password"
TELL_MYSQL_CREATIONS="-Created pyros database.\n-Created new user '$DB_USER' with password '$DB_PASSWORD'"
USE_SQLITE="-Sqlite will be used by default. There is no required configuration."

WRONG_DIRECTORY="-Wrong directory : please run this script from the '$INSTALL_DIR' directory"
PRIVATE_DIR_CREATE="-Created 'private' directory"
PYTHON_NOT_INSTALLED="-Cannot find $PYTHON, please install it or configure PYTHON variable in this script"
PYTHON_FOUND="-Found $PYTHON at"
VENV_CREATE="-Creating virtualenv $VENV_NAME... "
VENV_ACTIVATE="-Activating virtual environment... "
VENV_FAIL="-Failed to activate virtualenv. Ask project administrators for further information"
DONE="done."
UPGRADE_PIP="-Upgrading pip:"
UPGRADE_WHEEL="-Upgrading wheel:"
INSTALL_PACKAGES="-Installing required packages from install/REQUIREMENTS.txt"
CREATE_DATABASE_CONTENT="-Creating database tables :"
CREATE_SUPERUSER="-Creating database superuser :"
CREATE_SUPERUSER_SCRIPT="from django.contrib.auth.models import User; User.objects.filter(username='$DB_USER').delete(); User.objects.create_superuser('$DB_USER', 'admin@example.com', '$DB_PASSWORD')"
INSTALLATION_FINISHED="-Installation finished"
BAD_SQL_CONFIGURATION="-Migration cannot be applied to the database : check your database configuration, or use sqlite instead"
TELL_SUPERUSER_CREATION="-Superuser created for django admin interface. Login: $DB_USER , password: $DB_PASSWORD"

# Getting python location
PYTHON_DIR=`which $PYTHON`
if [ $? -ne 0 ]; then
    echo $PYTHON_NOT_INSTALLED
    exit
fi
echo $PYTHON_FOUND $PYTHON_DIR

# Getting current path to test if we are in the install directory
CURRENT_PATH=`pwd`
CURRENT_PATH=${CURRENT_PATH##*/}

if [ "$CURRENT_PATH" != $INSTALL_DIR ]; then
    echo $WRONG_DIRECTORY
    exit
fi

# Create a virtual env for Python3
cd ../

mkdir -p private

echo $PRIVATE_DIR_CREATE

cd private/

# create a venv_py3_pyros/ folder inside PYROS/private/
echo $VENV_CREATE
virtualenv $VENV_NAME -p $PYTHON_DIR

# Activate the virtual env
echo $VENV_ACTIVATE
source ./$VENV_NAME/bin/activate

if [ $? -ne 0 ]; then
    echo $VENV_FAIL
    exit
fi

echo "Python version :"
python -V

# Ask for using MySQL
echo $ASK_USE_MYSQL
read ANSWER
if echo "$ANSWER" | grep -iq "^y"; then
    echo -n $ASK_MYSQL_USERNAME
    read ANSWER
    if [ "$ANSWER" != "" ]; then
		USERNAME="$ANSWER"
    fi
    
    echo $SQL_SCRIPT | mysql -u "$USERNAME" -p

    if [ $? -ne 0 ]; then
		echo $SQL_FAIL
		exit
    fi

    echo -e $TELL_MYSQL_CREATIONS

    sed -i -e "s/MYSQL = False/MYSQL = True/g" ../src/pyros/settings.py
	MYSQL=1
else
    sed -i -e "s/MYSQL = True/MYSQL = False/g" ../src/pyros/settings.py
    echo $USE_SQLITE
	MYSQL=0
fi

read -n1 -r -p "Press any key to continue..." key

# Upgrade pip
echo $UPGRADE_PIP
pip install --upgrade pip

# Upgrade wheel
echo $UPGRADE_WHEEL
pip install --upgrade wheel

# Install the needed python packages
echo $INSTALL_PACKAGES
if [ $MYSQL -ne 0 ]; then
	pip install -r ../install/REQUIREMENTS.txt
else
	pip install -r ../install/REQUIREMENTS_SQLITE.txt
fi

# Create the database content and a superuser pyros
cd ../src/
echo $CREATE_DATABASE_CONTENT
python manage.py makemigrations
python manage.py migrate

if [ $? -ne 0 ]; then
    echo $BAD_SQL_CONFIGURATION
    exit
fi

echo $CREATE_SUPERUSER
echo $CREATE_SUPERUSER_SCRIPT | python ../src/manage.py shell

echo $TELL_SUPERUSER_CREATION
read -n1 -r -p "Press any key to continue..." key

echo $INSTALLATION_FINISHED