install_re2o.sh 7.23 KB
Newer Older
1
#!/bin/bash
Gabriel Detraz's avatar
Gabriel Detraz committed
2 3 4 5
echo "Installation de Re2o ! 
Cet utilitaire va procéder à l'installation initiale de re2o. Le serveur présent doit être vierge.
Preconfiguration..."

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
export DEBIAN_FRONTEND=noninteractive

apt-get -y install dialog

HEIGHT=15
WIDTH=40
CHOICE_HEIGHT=4
BACKTITLE="Preconfiguration re2o"
MENU="Choisir une option"

TITLE="Choix du moteur bdd"
OPTIONS=(1 "mysql"
         2 "postgresql")

sql_bdd_type=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear


TITLE="Extension locale (ex : example.org)"

extension_locale=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear

Gabriel Detraz's avatar
Gabriel Detraz committed
39 40 41 42 43 44 45 46 47 48
IFS='.' read -a extension_locale_array <<< $extension_locale


for i in "${extension_locale_array[@]}"
do
    ldap_dn+="dc=$i,"
done
ldap_dn=${ldap_dn::-1}
echo $ldap_dn

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
TITLE="Emplacement de la bdd"
OPTIONS=(1 "Local"
         2 "Distant")

sql_is_local=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear

TITLE="Mot de passe sql"
Gabriel Detraz's avatar
Gabriel Detraz committed
64

65 66 67 68 69 70 71 72
sql_password=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear


if [ $sql_is_local == 2 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
73
then 
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
TITLE="Login sql"
sql_login=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear
TITLE="Nom de la bdd sql"
sql_name=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear
TITLE="Hote de la base de donnée"
sql_host=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear
Gabriel Detraz's avatar
Gabriel Detraz committed
92 93 94 95 96 97
else
sql_name="re2o"
sql_login="re2o"
sql_host="localhost"
fi

Gabriel Detraz's avatar
Gabriel Detraz committed
98
sql_command="CREATE DATABASE $sql_name collate='utf8_general_ci';
Gabriel Detraz's avatar
Gabriel Detraz committed
99 100 101 102 103
CREATE USER '$sql_login'@'localhost' IDENTIFIED BY '$sql_password';
GRANT ALL PRIVILEGES ON $sql_name.* TO '$sql_login'@'localhost';
FLUSH PRIVILEGES;"


104 105 106 107 108 109 110 111 112 113 114
TITLE="Emplacement du ldap"
OPTIONS=(1 "Local"
         2 "Distant")

ldap_is_local=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)
Gabriel Detraz's avatar
Gabriel Detraz committed
115

116 117 118 119 120 121 122 123 124
echo "Vous devrez fournir un login/host dans le cas où le ldap est non local"

TITLE="Mot de passe ldap"
ldap_password=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear
if [ $ldap_is_local == 2 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
125
then 
126 127 128 129 130 131 132 133 134 135 136 137
TITLE="Cn ldap admin"
ldap_cn=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear
TITLE="Hote ldap"
ldap_host=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear
Gabriel Detraz's avatar
Gabriel Detraz committed
138 139 140 141 142 143 144 145 146 147 148 149 150
else
ldap_cn="cn=admin,"
ldap_cn+=$ldap_dn
ldap_host="localhost"
fi


echo "Installation des paquets de base"
apt-get -y install python3-django python3-dateutil texlive-latex-base texlive-fonts-recommended python3-djangorestframework python3-django-reversion python3-pip libsasl2-dev libldap2-dev libssl-dev
pip3 install django-bootstrap3
pip3 install django-ldapdb
pip3 install django-macaddress

151
if [ $sql_bdd_type == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
152
then
153
    if [ $sql_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
154 155
    then
    apt-get -y install mysql-server
156
    mysql -u root --execute="$sql_command"
Gabriel Detraz's avatar
Gabriel Detraz committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170
    else
    echo "Veuillez saisir la commande suivante sur le serveur sql distant, puis validez"
    echo $sql_command
    while true; do
	read -p "Continue (y/n)?" choice
	case "$choice" in 
	y|Y ) break;;
	n|N ) exit;;
	* ) echo "invalid";;
	esac
    done
    fi
    apt-get -y install python3-mysqldb mysql-client
    else
171
    if [ $sql_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
172 173 174 175 176 177
    then
    apt-get -y install postgresql-server
    fi
    apt-get -y install postgresql-client
fi 

178
if [ $ldap_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
179 180 181 182
then
apt-get -y install slapd

echo "Hashage du mot de passe ldap..."
183
hashed_ldap_passwd=$(slappasswd -s $ldap_password)
Gabriel Detraz's avatar
Gabriel Detraz committed
184 185 186

echo $hashed_ldap_passwd
echo "Formatage des fichiers de config ldap"
187 188
sed 's|dc=example,dc=org|'"$ldap_dn"'|g' install_utils/db.ldiff | sed 's|FILL_IT|'"$hashed_ldap_passwd"'|g' > /tmp/db
sed 's|dc=example,dc=org|'"$ldap_dn"'|g' install_utils/schema.ldiff | sed 's|FILL_IT|'"$hashed_ldap_passwd"'|g' > /tmp/schema
Gabriel Detraz's avatar
Gabriel Detraz committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

echo "Destruction config ldap existante"
service slapd stop
rm -rf /etc/ldap/slapd.d/*
rm -rf /var/lib/ldap/*

echo "Ecriture de la configuration actuelle"
slapadd -n 0 -l /tmp/schema -F /etc/ldap/slapd.d/
slapadd -n 1 -l /tmp/db

echo "Reparation des permissions et redémarage de slapd"
chown -R openldap:openldap /etc/ldap/slapd.d
chown -R openldap:openldap /var/lib/ldap
service slapd start

else
echo "Vous devrez manuellement effectuer les opérations de setup de la base ldap sur le serveurs distant.
Le mot de passe ldap a été placé dans le fichier re2o/settings_local"
fi

echo "Ecriture de settings_local"

211
django_secret_key=$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(50)]))")
Gabriel Detraz's avatar
Gabriel Detraz committed
212 213

cp re2o/settings_local.example.py re2o/settings_local.py
214
sed -i 's/SUPER_SECRET_KEY/'"$django_secret_key"'/g' re2o/settings_local.py
Gabriel Detraz's avatar
Gabriel Detraz committed
215 216 217 218 219 220 221 222 223 224 225
sed -i 's/SUPER_SECRET_DB/'"$sql_password"'/g' re2o/settings_local.py
sed -i 's/db_name_value/'"$sql_name"'/g' re2o/settings_local.py
sed -i 's/db_user_value/'"$sql_login"'/g' re2o/settings_local.py
sed -i 's/db_host_value/'"$sql_host"'/g' re2o/settings_local.py
sed -i 's/ldap_dn/'"$ldap_cn"'/g' re2o/settings_local.py
sed -i 's/SUPER_SECRET_LDAP/'"$ldap_password"'/g' re2o/settings_local.py
sed -i 's/ldap_host_ip/'"$ldap_host"'/g' re2o/settings_local.py
sed -i 's/dc=example,dc=org/'"$ldap_dn"'/g' re2o/settings_local.py
sed -i 's/example.org/'"$extension_locale"'/g' re2o/settings_local.py

echo "Application des migrations"
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
python3 manage.py migrate

echo "Collecte des statics"
python3 manage.py collectstatics

BACKTITLE="Fin de l'installation"
TITLE="Serveur web à utiliser"
OPTIONS=(1 "apache2"
         2 "nginx")

web_serveur=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear

TITLE="Url où servir le serveur web (ex : re2o.example.org)"
url_server=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear

if [ $web_serveur == 1 ]
then
apt-get -y install apache2 libapache2-mod-wsgi-py3
cp install_utils/apache2/re2o.conf /etc/apache2/sites-available/re2o.conf
sed -i 's/URL_SERVER/'"$url_server"'/g' /etc/apache2/sites-available/re2o.conf
current_path=$(pwd)
sed -i 's|PATH|'"$current_path"'|g' /etc/apache2/sites-available/re2o.conf
a2ensite re2o
service apache2 reload
else
echo "Nginx non supporté, vous devrez installer manuellement"
fi
Gabriel Detraz's avatar
Gabriel Detraz committed
265