86 lines
2.7 KiB
Bash
86 lines
2.7 KiB
Bash
#!/bin/sh
|
|
|
|
# Stop if an error is encountered
|
|
set -e
|
|
|
|
PROJECTNAME=$(basename $1)
|
|
SETTINGS_MODULE="$PROJECTNAME.settings.vagrant"
|
|
|
|
# Configuration de la base de données. Le mot de passe est constant car c'est
|
|
# pour une installation de dév locale qui ne sera accessible que depuis la
|
|
# machine virtuelle.
|
|
DBUSER=$PROJECTNAME
|
|
DBNAME=$PROJECTNAME
|
|
DBPASSWD="O1LxCADDA6Px5SiKvifjvdp3DSjfbp"
|
|
|
|
# Installation de paquets utiles.
|
|
# Installe les paquets mentionnés dans `package.list`, en excluant les lignes
|
|
# commençant par #.
|
|
apt-get update && apt-get upgrade -y
|
|
apt-get install -y $(awk '! /^ *#/' /vagrant/provisioning/package.list)
|
|
|
|
# Postgresql
|
|
# On teste si la db existe déjà pour ne pas essayer de la recréer
|
|
DB_EXISTS=$(sudo -u postgres psql -lqt | cut -d \| -f 1 | grep -cw $DBNAME || true)
|
|
if [ $DB_EXISTS -eq 0 ]
|
|
then
|
|
sudo -u postgres createdb $DBNAME
|
|
sudo -u postgres createuser -SdR $DBUSER
|
|
sudo -u postgres psql -c "ALTER USER $DBUSER WITH PASSWORD '$DBPASSWD';"
|
|
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;"
|
|
fi
|
|
|
|
|
|
# Redis
|
|
REDIS_PASSWD="dummy"
|
|
redis-cli CONFIG SET requirepass $REDIS_PASSWD
|
|
redis-cli -a $REDIS_PASSWD CONFIG REWRITE
|
|
|
|
# Contenu statique
|
|
mkdir -p /srv/$PROJECTNAME/static
|
|
ln -sf /vagrant/media /srv/$PROJECTNAME/media
|
|
chown -R vagrant:www-data /srv/$PROJECTNAME
|
|
|
|
# Nginx
|
|
rm -f /etc/nginx/sites-enabled/default
|
|
sed "s/\_\_PROJECTNAME__/$PROJECTNAME/g" /vagrant/provisioning/nginx.conf > /etc/nginx/sites-enabled/$PROJECTNAME.conf
|
|
systemctl reload nginx
|
|
|
|
# Environnement virtuel python
|
|
sudo -H -u vagrant python3 -m venv ~vagrant/venv
|
|
sudo -H -u vagrant ~vagrant/venv/bin/pip install -U pip
|
|
sudo -H -u vagrant ~vagrant/venv/bin/pip install -r /vagrant/requirements-prod.txt -r /vagrant/requirements-dev.txt
|
|
|
|
# Préparation de Django
|
|
cd /vagrant
|
|
sudo -H -u vagrant \
|
|
DJANGO_SETTINGS_MODULE=$SETTINGS_MODULE \
|
|
bash -c ". ~/venv/bin/activate && bash provisioning/prepare_django.sh"
|
|
sudo -H -u vagrant /home/vagrant/venv/bin/python manage.py collectstatic --noinput --settings $SETTINGS_MODULE
|
|
|
|
# Mails
|
|
mkdir -p /var/mail/django
|
|
chown -R vagrant:www-data /var/mail/django
|
|
|
|
# Service files
|
|
for file in /vagrant/provisioning/*.service
|
|
do
|
|
# failsafe si aucun fichier .service n'existe
|
|
[ -f $file ] || break
|
|
SERVICE=$(basename $file)
|
|
|
|
# On copie en remplaçant si nécessaire le template
|
|
sed "s/\_\_PROJECTNAME__/$PROJECTNAME/g" $file > /etc/systemd/system/$SERVICE
|
|
systemctl enable $SERVICE
|
|
systemctl start $SERVICE
|
|
done
|
|
|
|
# Mise en place du .bash_profile pour tout configurer lors du `vagrant ssh`
|
|
cat >> ~vagrant/.bashrc <<EOF
|
|
export DJANGO_SETTINGS_MODULE=$SETTINGS_MODULE
|
|
|
|
# Charge le virtualenv
|
|
source ~/venv/bin/activate
|
|
|
|
cd /vagrant
|
|
EOF
|