poulpe/provisioning/bootstrap.sh
Martin Pépin e64c3d0b37 Setup a production-like environment in vagrant
- We already use Daphne and channels to prepare the future use of
  websockets.
- The app is served behind an nginx reverse-proxy
- The services we need are managed by systemctl

The production-like running instance of GestionÉvénementiel can be
accessed on localhost:8080 outside vagrant.
2017-05-16 16:13:31 +01:00

116 lines
3.1 KiB
Bash

#!/bin/bash
# The credentials of the database. Can be public since they will only be used in
# the local development environment
DBUSER="event_gestion"
DBNAME="event_gestion"
DBPASSWD="4KZt3nGPLVeWSvtBZPsd9jdssdJMds78"
# Not critical either
REDIS_PASSWD="dummy"
# It is used in quite a few places
SETTINGS="evenementiel.settings_dev"
# ---
# Installs the dependencies
# ---
# System packages
apt-get update && apt-get upgrade
apt-get install -y python3-pip python3-dev python3-venv libpq-dev postgresql \
postgresql-contrib libjpeg-dev nginx redis-server
# Python packages, in a virtual environment
sudo -H -u vagrant python3 -m venv ~vagrant/venv
sudo -H -u vagrant ~vagrant/venv/bin/pip install -U pip wheel
sudo -H -u vagrant ~vagrant/venv/bin/pip install -U -r /vagrant/requirements-devel.txt
# ---
# Setup the production-like environment
# ---
# Database and User
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 "ALTER USER $DBUSER CREATEDB;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;"
# The working directory for Daphne and cie
mkdir -p /srv/GE/{media,static}
chown -R vagrant:vagrant /srv/GE
# Nginx
cp /vagrant/provisioning/nginx.conf /etc/nginx/sites-available/ge.conf
if [ ! -h /etc/nginx/sites-enabled/ge.conf ]
then
# If the configuration file is not activated yet, activates it
ln -s /etc/nginx/sites-available/ge.conf /etc/nginx/sites-enabled/ge.conf
fi
rm -f /etc/nginx/sites-enabled/default # We do not need this
service nginx restart
# Daphne and the worker(s)
for service in {daphne,worker}.service
do
cp /vagrant/provisioning/$service /etc/systemd/system/$service
sed "s/{{DBUSER}}/$DBUSER/" -i /etc/systemd/system/$service
sed "s/{{DBNAME}}/$DBNAME/" -i /etc/systemd/system/$service
sed "s/{{DBPASSWD}}/$DBPASSWD/" -i /etc/systemd/system/$service
sed "s/{{SETTINGS}}/$SETTINGS/" -i /etc/systemd/system/$service
systemctl enable $service
systemctl start $service
done
# Redis
redis-cli CONFIG SET requirepass $REDIS_PASSWD
if [ ! $? ]
then
# In case the requirepass command failed, checks that it was because the
# password was already set *to the right value*.
redis-cli AUTH $REDIS_PASSWD
fi
redis-cli -a $REDIS_PASSWD CONFIG REWRITE
# ---
# Prepare Django
# ---
function venv_python {
sudo -H -u vagrant DJANGO_SETTINGS_MODULE=$SETTINGS \
DBUSER=$DBUSER DBNAME=$DBNAME DBPASSWD=$DBPASSWD \
~vagrant/venv/bin/python \
$@
}
cd /vagrant
venv_python manage.py collectstatic --noinput
venv_python manage.py migrate
unset venv_python
# ---
# Setup a friendly environment for the user
# ---
cat >> ~vagrant/.bashrc <<EOF
# On utilise les settings de développement
export DJANGO_SETTINGS_MODULE='evenementiel.settings_dev'
# Identifiants postgres
export DBUSER="$DBUSER"
export DBNAME="$DBNAME"
export DBPASSWD="$DBPASSWD"
# On va dans /vagrant où se trouve le code
cd /vagrant
# On active le virtualenv
source ~vagrant/venv/bin/activate
EOF
chown vagrant: ~vagrant/.bashrc