install_requirements.sh
3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/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"
# 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"
# 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
# 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
pip install -r ../install/REQUIREMENTS.txt
# 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_SCRIPT
exit
fi
echo -e $TELL_MYSQL_CREATIONS
sed -i -e "s/MYSQL = False/MYSQL = True/g" ../src/pyros/settings.py
else
sed -i -e "s/MYSQL = True/MYSQL = False/g" ../src/pyros/settings.py
echo $USE_SQLITE
fi
read -n1 -r -p "Press any key to continue..." key
# 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 $INSTALLATION_FINISHED