Déploiement poil au dents

This commit is contained in:
Hugo LEVY-FALK 2018-03-08 18:25:43 +01:00
parent e5a90006b5
commit 9145f37a18
4 changed files with 71 additions and 7 deletions

View file

@ -3,6 +3,14 @@
PYTHON_INTERPRETER = "/usr/bin/python3" PYTHON_INTERPRETER = "/usr/bin/python3"
printf "\033[0;32m > Installation de MySQL \033[0m\n"
apt-get -y install python3-mysqldb mysql-client mysql-server
printf "\033[0;32m > Installation de Apache \033[0m\n"
apt-get -y install apache2 libapache2-mod-wsgi-py3
a2enmod ssl
a2enmod wsgi
printf "\033[0;32m > Création du virtualenv \033[0m\n" printf "\033[0;32m > Création du virtualenv \033[0m\n"
virtualenv env_site -p "$PYTHON_INTERPRETER" virtualenv env_site -p "$PYTHON_INTERPRETER"
source env_site/bin/activate source env_site/bin/activate
@ -10,7 +18,6 @@ source env_site/bin/activate
printf "\033[0;32m > Installation des dépendances \033[0m\n" printf "\033[0;32m > Installation des dépendances \033[0m\n"
pip install -r requirements.txt pip install -r requirements.txt
printf "\033[0;32m > Génération des settings locaux. \033[0m\n"
printf "\033[0;32m > Génération de la secret_key \033[0m\n" printf "\033[0;32m > Génération de la secret_key \033[0m\n"
django_secret_key=$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(50)]))") django_secret_key=$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(50)]))")
@ -19,10 +26,18 @@ cp site_tps/settings_local.example.py site_tps/settings_local.py
sed -i 's/SUPER_SECRET_KEY/'"$django_secret_key"'/g' site_tps/settings_local.py sed -i 's/SUPER_SECRET_KEY/'"$django_secret_key"'/g' site_tps/settings_local.py
printf "\033[0;32m > Configuration de MySQL \033[0m\n" printf "\033[0;32m > Configuration de MySQL \033[0m\n"
read -p "Hôte > " db_host sql_name="festart"
read -p "Nom de la base de données > " db_name sql_login="festart"
read -p "Utilisateur MySQL > " db_user sql_host="localhost"
read -p -s "Mot de passe > " db_pass sql_password=$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(10)]))")
mysql_command="CREATE DATABASE $sql_name collate='utf8_general_ci';
CREATE USER '$sql_login'@'localhost' IDENTIFIED BY '$sql_password';
GRANT ALL PRIVILEGES ON $sql_name.* TO '$sql_login'@'localhost';
FLUSH PRIVILEGES;"
mysql -u root --execute="$mysql_command"
sed -i 's/db_engine/django.db.backends.mysql/g' site_tps/settings_local.py sed -i 's/db_engine/django.db.backends.mysql/g' site_tps/settings_local.py
sed -i 's/db_name/'"$db_name"'/g' site_tps/settings_local.py sed -i 's/db_name/'"$db_name"'/g' site_tps/settings_local.py
@ -37,11 +52,26 @@ sed -i 's/db_host/'"$db_host"'/g' site_tps/settings_local.py
printf "\033[0;32m > Domaine\033[0m\n" printf "\033[0;32m > Domaine\033[0m\n"
read -p "Domaine autorisé > " url_server read -p "Domaine autorisé > " url_server
sed -i 's/URL_SERVER/'"$url_server"'/g' site_tps/settings_local.py sed -i 's,URL_SERVER,'"$url_server"',g' site_tps/settings_local.py
printf "\033[0;32m > settings_local.py créé \033[0m\n" printf "\033[0;32m > settings_local.py créé \033[0m\n"
printf "\033[0;32m > Export du wsgi.py de production \033[0m\n"
cp wsgi_prod.py site_tps/wsgi.py
current_path=$(pwd)
sed -i 's,INSTALL_PATH,'"$current_path"',g' site_tps/wsgi.py
printf "\033[0;32m > Configuration de Apache \033[0m\n"
cp site_tps.conf /etc/apache2/sites-available
sed -i 's,URL_SERVER,'"$url_server"',g' /etc/apache2/sites-available/site_tps.conf
sed -i 's,INSTALL_PATH,'"$current_path"',g' /etc/apache2/sites-available/site_tps.conf
a2ensite re2o
service apache2 reload
printf "\033[0;32m > Application des migrations \033[0m\n" printf "\033[0;32m > Application des migrations \033[0m\n"
python manage.py migrate python manage.py migrate
printf "\033[0;32m > Collecte des statiques \033[0m\n"
python manage.py collectstatic
printf "\033[0;32m > Création du groupe admin \033[0m\n" printf "\033[0;32m > Création du groupe admin \033[0m\n"
python manage.py create_admin python manage.py create_admin
printf "\033[0;32m > Création d'un super utilisateur\033[0m\n" printf "\033[0;32m > Création d'un super utilisateur\033[0m\n"

17
site_tps.conf Normal file
View file

@ -0,0 +1,17 @@
<VirtualHost *:80>
ServerName URL_SERVER
LogLevel info
ErrorLog ${APACHE_LOG_DIR}/site_tps-error.log
CustomLog ${APACHE_LOG_DIR}/site_tps-access.log combined
<Directory /var/www/site_tps>
Order allow,deny
Allow from all
</Directory>
Alias /static INSTALL_PATH/static
WSGIScriptAlias / INSTALL_PATH/site_tps/wsgi.py
WSGIProcessGroup site_tps
WSGIDaemonProcess site_tps processes=2 threads=16 maximum-requests=1000 display-name=site_tps
</VirtualHost>

View file

@ -8,7 +8,7 @@ SECRET_KEY = 'SUPER_SECRET_KEY'
DB_PASSWORD = 'db_pass' DB_PASSWORD = 'db_pass'
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = True
# ADMINS = [('My Beloved Administrator', 'admin_mail')] # ADMINS = [('My Beloved Administrator', 'admin_mail')]

17
wsgi_prod.py Normal file
View file

@ -0,0 +1,17 @@
import os
import sys
# Activation de l'environnement virtuel
activate_env=os.path.join('INSTALL_PATH/env_site', 'bin/activate_this.py')
exec(compile(open(activate_env, "rb").read(), activate_env, 'exec'), {'__file__':activate_env})
# Ajout du répertoire du site au PATH
sys.path.append('INSTALL_PATH')
sys.path.append('INSTALL_PATH/site_tps')
# Les trucs par défaut de Django
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site_tps.settings")
application = get_wsgi_application()