2017-05-16 17:13:31 +02:00
|
|
|
#!/bin/bash
|
2016-09-28 20:36:48 +02:00
|
|
|
|
2017-05-16 17:13:31 +02:00
|
|
|
# The credentials of the database. Can be public since they will only be used in
|
|
|
|
# the local development environment
|
2016-09-28 20:36:48 +02:00
|
|
|
DBUSER="event_gestion"
|
|
|
|
DBNAME="event_gestion"
|
|
|
|
DBPASSWD="4KZt3nGPLVeWSvtBZPsd9jdssdJMds78"
|
|
|
|
|
2017-05-16 17:13:31 +02:00
|
|
|
# Not critical either
|
|
|
|
REDIS_PASSWD="dummy"
|
|
|
|
|
|
|
|
# It is used in quite a few places
|
2018-08-20 14:32:52 +02:00
|
|
|
SETTINGS="poulpe.settings.dev"
|
2017-05-16 20:52:59 +02:00
|
|
|
|
|
|
|
# Fills a "templated file" with the information specified in the variables above
|
|
|
|
# e.g. every occurrence of {{DBUSER}} in the file will be replaced by the value
|
|
|
|
# of the variable $DBUSER
|
|
|
|
function fill_template {
|
|
|
|
sed "s/{{DBUSER}}/$DBUSER/" -i $1
|
|
|
|
sed "s/{{DBNAME}}/$DBNAME/" -i $1
|
|
|
|
sed "s/{{DBPASSWD}}/$DBPASSWD/" -i $1
|
|
|
|
sed "s/{{REDIS_PASSWD}}/$REDIS_PASSWD/" -i $1
|
|
|
|
sed "s/{{SETTINGS}}/$SETTINGS/" -i $1
|
|
|
|
}
|
2017-05-16 17:13:31 +02:00
|
|
|
|
|
|
|
# ---
|
|
|
|
# Installs the dependencies
|
|
|
|
# ---
|
|
|
|
|
|
|
|
# System packages
|
2017-05-17 11:33:05 +02:00
|
|
|
apt-get update && apt-get upgrade -y
|
2017-05-16 12:43:40 +02:00
|
|
|
apt-get install -y python3-pip python3-dev python3-venv libpq-dev postgresql \
|
2017-05-16 17:13:31 +02:00
|
|
|
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
|
|
|
|
|
2016-09-28 20:36:48 +02:00
|
|
|
|
2017-05-16 17:13:31 +02:00
|
|
|
# ---
|
|
|
|
# Setup the production-like environment
|
|
|
|
# ---
|
|
|
|
|
|
|
|
# Database and User
|
2016-09-28 20:36:48 +02:00
|
|
|
sudo -u postgres createdb $DBNAME
|
|
|
|
sudo -u postgres createuser -SDR $DBUSER
|
|
|
|
sudo -u postgres psql -c "ALTER USER $DBUSER WITH PASSWORD '$DBPASSWD';"
|
2016-10-02 14:23:39 +02:00
|
|
|
sudo -u postgres psql -c "ALTER USER $DBUSER CREATEDB;"
|
2016-09-28 22:06:05 +02:00
|
|
|
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;"
|
2016-09-28 20:36:48 +02:00
|
|
|
|
2017-05-16 17:13:31 +02:00
|
|
|
# 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
|
2017-05-16 20:52:59 +02:00
|
|
|
fill_template /etc/systemd/system/$service
|
2017-05-16 17:13:31 +02:00
|
|
|
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
|
|
|
|
# ---
|
|
|
|
|
2017-05-16 20:52:59 +02:00
|
|
|
cd /vagrant
|
|
|
|
|
|
|
|
# Setup the secrets
|
2018-08-20 14:32:52 +02:00
|
|
|
sudo -H -u vagrant cp poulpe/settings/secret_example.py \
|
|
|
|
poulpe/settings/secret.py
|
|
|
|
fill_template poulpe/settings/secret.py
|
2017-05-16 20:52:59 +02:00
|
|
|
|
|
|
|
# Run the usual django admin commands
|
2017-05-16 17:13:31 +02:00
|
|
|
function venv_python {
|
|
|
|
sudo -H -u vagrant DJANGO_SETTINGS_MODULE=$SETTINGS \
|
|
|
|
~vagrant/venv/bin/python \
|
|
|
|
$@
|
|
|
|
}
|
|
|
|
|
|
|
|
venv_python manage.py collectstatic --noinput
|
|
|
|
venv_python manage.py migrate
|
|
|
|
|
|
|
|
|
|
|
|
# ---
|
|
|
|
# Setup a friendly environment for the user
|
|
|
|
# ---
|
|
|
|
|
2017-05-16 23:00:00 +02:00
|
|
|
cat > ~vagrant/.bash_profile <<EOF
|
|
|
|
# Use the .bashrc file provided by Debian
|
|
|
|
source .bashrc
|
2017-05-16 12:43:40 +02:00
|
|
|
|
2017-05-16 22:43:30 +02:00
|
|
|
# Use the development settings
|
|
|
|
export DJANGO_SETTINGS_MODULE="$SETTINGS"
|
2016-09-28 20:36:48 +02:00
|
|
|
|
2017-05-16 22:43:30 +02:00
|
|
|
# Move to the code's folder
|
2016-09-28 20:36:48 +02:00
|
|
|
cd /vagrant
|
2017-05-16 12:43:40 +02:00
|
|
|
|
2017-05-16 22:43:30 +02:00
|
|
|
# Activate the virtualenv
|
2017-05-16 12:43:40 +02:00
|
|
|
source ~vagrant/venv/bin/activate
|
2016-09-28 20:36:48 +02:00
|
|
|
EOF
|
2017-05-16 12:43:40 +02:00
|
|
|
chown vagrant: ~vagrant/.bashrc
|