2016-05-26 04:06:17 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-12-04 16:09:59 +01:00
|
|
|
# Arête le script quand :
|
|
|
|
# - une erreur survient
|
|
|
|
# - on essaie d'utiliser une variable non définie
|
|
|
|
# - on essaie d'écraser un fichier avec une redirection (>).
|
|
|
|
set -euC
|
2018-02-11 17:01:26 +01:00
|
|
|
|
2020-12-04 16:09:59 +01:00
|
|
|
# Configuration de la base de données, redis, Django, etc.
|
|
|
|
# Tous les mots de passe sont constant et en clair dans le fichier car c'est
|
2016-05-26 04:06:17 +02:00
|
|
|
# pour une installation de dév locale qui ne sera accessible que depuis la
|
|
|
|
# machine virtuelle.
|
2020-12-04 16:09:59 +01:00
|
|
|
readonly DBUSER="cof_gestion"
|
|
|
|
readonly DBNAME="cof_gestion"
|
|
|
|
readonly DBPASSWD="4KZt3nGPLVeWSvtBZPSM3fSzXpzEU4"
|
|
|
|
readonly REDIS_PASSWD="dummy"
|
|
|
|
readonly DJANGO_SETTINGS_MODULE="cof.settings.dev"
|
|
|
|
|
2016-05-26 04:06:17 +02:00
|
|
|
|
|
|
|
# Installation de paquets utiles
|
2020-12-04 16:09:59 +01:00
|
|
|
apt-get update
|
|
|
|
# https://github.com/chef/bento/issues/661
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
apt-get -y upgrade
|
|
|
|
# -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
|
|
|
|
# upgrade
|
2017-08-09 23:07:49 +02:00
|
|
|
apt-get install -y python3-pip python3-dev python3-venv libpq-dev postgresql \
|
2020-12-04 16:09:59 +01:00
|
|
|
postgresql-contrib libjpeg-dev nginx git redis-server \
|
|
|
|
libldap2-dev libsasl2-dev slapd ldap-utils
|
2016-05-26 04:06:17 +02:00
|
|
|
|
2017-08-09 23:07:49 +02:00
|
|
|
# Postgresql
|
2020-12-04 16:09:59 +01:00
|
|
|
pg_user_exists () {
|
|
|
|
sudo -u postgres psql postgres -tAc \
|
|
|
|
"SELECT 1 FROM pg_roles WHERE rolname='$1'" \
|
|
|
|
| grep -q '^1$'
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_db_exists () {
|
|
|
|
sudo -u postgres psql postgres -tAc \
|
|
|
|
"SELECT 1 FROM pg_database WHERE datname='$1'" \
|
|
|
|
| grep -q '^1$'
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_db_exists "$DBNAME" || sudo -u postgres createdb "$DBNAME"
|
|
|
|
pg_user_exists "$DBUSER" || sudo -u postgres createuser -SdR "$DBUSER"
|
2017-08-09 23:07:49 +02:00
|
|
|
sudo -u postgres psql -c "ALTER USER $DBUSER WITH PASSWORD '$DBPASSWD';"
|
|
|
|
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER;"
|
2016-05-26 04:06:17 +02:00
|
|
|
|
|
|
|
|
2017-08-09 23:07:49 +02:00
|
|
|
# Redis
|
2020-12-04 16:09:59 +01:00
|
|
|
redis-cli CONFIG SET requirepass "$REDIS_PASSWD"
|
|
|
|
redis-cli -a "$REDIS_PASSWD" CONFIG REWRITE
|
2017-04-24 22:27:01 +02:00
|
|
|
|
2017-08-13 15:36:45 +02:00
|
|
|
# Contenu statique
|
|
|
|
mkdir -p /srv/gestiocof/media
|
|
|
|
mkdir -p /srv/gestiocof/static
|
2020-12-04 16:09:59 +01:00
|
|
|
chown -R vagrant:www-data /srv/gestiocof
|
2017-08-09 23:07:49 +02:00
|
|
|
|
|
|
|
# Nginx
|
|
|
|
ln -s -f /vagrant/provisioning/nginx.conf /etc/nginx/sites-enabled/gestiocof.conf
|
|
|
|
rm -f /etc/nginx/sites-enabled/default
|
|
|
|
systemctl reload nginx
|
|
|
|
|
|
|
|
# Environnement virtuel python
|
2020-12-04 16:09:59 +01:00
|
|
|
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-devel.txt \
|
2017-08-09 23:07:49 +02:00
|
|
|
|
2017-08-10 17:55:36 +02:00
|
|
|
# Préparation de Django
|
|
|
|
cd /vagrant
|
|
|
|
ln -s -f secret_example.py cof/settings/secret.py
|
2020-12-04 16:09:59 +01:00
|
|
|
sudo -H -u vagrant \
|
|
|
|
DJANGO_SETTINGS_MODULE="$DJANGO_SETTINGS_MODULE"\
|
|
|
|
/bin/sh -c ". ~vagrant/venv/bin/activate && /bin/sh provisioning/prepare_django.sh"
|
|
|
|
~vagrant/venv/bin/python manage.py collectstatic \
|
|
|
|
--noinput \
|
|
|
|
--settings "$DJANGO_SETTINGS_MODULE"
|
2017-08-10 17:55:36 +02:00
|
|
|
|
|
|
|
# Installation du cron pour les mails de rappels
|
2020-12-04 16:09:59 +01:00
|
|
|
# TODO: FIXME
|
|
|
|
# sudo -H -u vagrant crontab provisioning/cron.dev
|
2017-08-10 17:55:36 +02:00
|
|
|
|
2017-08-09 23:07:49 +02:00
|
|
|
# Daphne + runworker
|
2020-12-04 16:09:59 +01:00
|
|
|
# TODO: explain
|
|
|
|
ln -sf /vagrant/provisioning/daphne.service /etc/systemd/system/daphne.service
|
|
|
|
ln -sf /vagrant/provisioning/worker.service /etc/systemd/system/worker.service
|
|
|
|
systemctl enable --now daphne.service
|
|
|
|
systemctl enable --now worker.service
|
|
|
|
|
|
|
|
# Configure le bash de l'utilisateur 'vagrant' pour utiliser le bon fichier de
|
|
|
|
# settings et et bon virtualenv.
|
|
|
|
# On utilise .bash_aliases au lieu de .bashrc pour ne pas écraser la
|
|
|
|
# configuration par défaut.
|
|
|
|
rm -f ~vagrant/.bash_aliases
|
|
|
|
cat > ~vagrant/.bash_aliases <<EOF
|
2016-05-26 04:06:17 +02:00
|
|
|
# On utilise la version de développement de GestioCOF
|
2020-12-04 16:09:59 +01:00
|
|
|
export DJANGO_SETTINGS_MODULE='$DJANGO_SETTINGS_MODULE'
|
2016-05-26 04:06:17 +02:00
|
|
|
|
2016-11-06 15:39:50 +01:00
|
|
|
# Charge le virtualenv
|
2020-12-04 16:09:59 +01:00
|
|
|
. ~/venv/bin/activate
|
2016-11-06 15:39:50 +01:00
|
|
|
|
2016-05-26 04:06:17 +02:00
|
|
|
# On va dans /vagrant où se trouve le code de gestioCOF
|
|
|
|
cd /vagrant
|
|
|
|
EOF
|