install_re2o.sh 11.8 KB
Newer Older
1
#!/bin/bash
2 3 4 5 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

setup_ldap() {
	apt-get -y install slapd

	echo "Hashage du mot de passe ldap..."
	hashed_ldap_passwd=$(slappasswd -s $1)

	echo $hashed_ldap_passwd
	echo "Formatage des fichiers de config ldap"
	sed 's|dc=example,dc=org|'"$2"'|g' install_utils/db.ldiff | sed 's|FILL_IT|'"$hashed_ldap_passwd"'|g' > /tmp/db
	sed 's|dc=example,dc=org|'"$2"'|g' install_utils/schema.ldiff | sed 's|FILL_IT|'"$hashed_ldap_passwd"'|g' > /tmp/schema

	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
}


install_re2o_server() {
31 32 33
echo "Installation de Re2o !
Cet utilitaire va procéder à l'installation initiale de re2o. Le serveur présent doit être vierge.
Preconfiguration..."
34

35 36
export DEBIAN_FRONTEND=noninteractive

37
apt-get -y install sudo dialog
38

39 40 41 42 43 44 45 46 47
HEIGHT=15
WIDTH=40
init=$(dialog --clear \
	--title "Installation de Re2o !" \
        --msgbox "Cet utilitaire va procéder à l'installation initiale de re2o. Le serveur présent doit être vierge de préférence. Preconfiguration..." \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)


48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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
77 78 79 80 81 82 83 84 85 86
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

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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
102

103 104 105 106 107 108 109 110
sql_password=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear


if [ $sql_is_local == 2 ]
111
then
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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
130 131 132 133 134 135
else
sql_name="re2o"
sql_login="re2o"
sql_host="localhost"
fi

136
mysql_command="CREATE DATABASE $sql_name collate='utf8_general_ci';
Gabriel Detraz's avatar
Gabriel Detraz committed
137 138 139 140
CREATE USER '$sql_login'@'localhost' IDENTIFIED BY '$sql_password';
GRANT ALL PRIVILEGES ON $sql_name.* TO '$sql_login'@'localhost';
FLUSH PRIVILEGES;"

141 142 143
pgsql_command1="CREATE DATABASE $sql_name ENCODING 'UTF8' LC_COLLATE='fr_FR.UTF-8' LC_CTYPE='fr_FR.UTF-8';"
pgsql_command2="CREATE USER $sql_login with password '$sql_password';"
pgsql_command3="ALTER DATABASE $sql_name owner to $sql_login;"
Gabriel Detraz's avatar
Gabriel Detraz committed
144

145 146 147 148 149 150 151 152 153 154 155
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
156

157 158 159 160 161 162 163 164

HEIGHT=15
WIDTH=40
instal_ldap=$(dialog --clear \
	--title "Installation de Re2o !" \
        --msgbox "Vous devrez fournir un login/host dans le cas où le ldap est non local" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
165 166 167 168 169 170 171 172

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 ]
173
then
174 175 176 177 178 179 180 181 182 183 184 185
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
186 187 188 189 190 191 192
else
ldap_cn="cn=admin,"
ldap_cn+=$ldap_dn
ldap_host="localhost"
fi


193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
TITLE="Hôte pour l'envoi de mail"
email_host=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)

TITLE="Port du serveur mail"
OPTIONS=(25 "25 (SMTP)"
         465 "465 (SMTPS)"
	 587 "587 (Submission)")

email_port=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)
clear
if [ $ldap_is_local == 2 ]
213
then
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
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
else
ldap_cn="cn=admin,"
ldap_cn+=$ldap_dn
ldap_host="localhost"
fi

232 233 234 235 236 237 238
HEIGHT=15
WIDTH=40
install_base=$(dialog --clear \
	--title "Installation de Re2o !" \
        --msgbox "Installation des paquets de base" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
239

240
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 python3-crypto
Gabriel Detraz's avatar
Gabriel Detraz committed
241 242 243
pip3 install django-bootstrap3
pip3 install django-ldapdb
pip3 install django-macaddress
244
pip3 install pycrypto
Gabriel Detraz's avatar
Gabriel Detraz committed
245

246
if [ $sql_bdd_type == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
247
then
248
    apt-get -y install python3-mysqldb mysql-client
249
    if [ $sql_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
250 251
    then
    apt-get -y install mysql-server
252
    mysql -u root --execute="$mysql_command"
Gabriel Detraz's avatar
Gabriel Detraz committed
253 254
    else
    echo "Veuillez saisir la commande suivante sur le serveur sql distant, puis validez"
255
    echo $mysql_command
Gabriel Detraz's avatar
Gabriel Detraz committed
256 257
    while true; do
	read -p "Continue (y/n)?" choice
258
	case "$choice" in
Gabriel Detraz's avatar
Gabriel Detraz committed
259 260 261 262 263 264
	y|Y ) break;;
	n|N ) exit;;
	* ) echo "invalid";;
	esac
    done
    fi
265 266 267
else
    apt-get -y install postgresql-client
    apt-get -y install python3-psycopg2
268
    if [ $sql_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
269
    then
270 271 272 273 274 275 276 277 278 279 280
    apt-get -y install postgresql
    sudo -u postgres psql --command="$pgsql_command1"
    sudo -u postgres psql --command="$pgsql_command2"
    sudo -u postgres psql --command="$pgsql_command3"
    else
    echo "Veuillez saisir la commande suivante sur le serveur sql distant, puis validez"
    echo sudo -u postgres psql $pgsql_command1
    echo sudo -u postgres psql $pgsql_command2
    echo sudo -u postgres psql $pgsql_command3
    while true; do
	read -p "Continue (y/n)?" choice
281
	case "$choice" in
282 283 284 285 286
	y|Y ) break;;
	n|N ) exit;;
	* ) echo "invalid";;
	esac
    done
Gabriel Detraz's avatar
Gabriel Detraz committed
287
    fi
288
fi
Gabriel Detraz's avatar
Gabriel Detraz committed
289

290
if [ $ldap_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
291 292
then

293
setup_ldap $ldap_password $ldap_dn
Gabriel Detraz's avatar
Gabriel Detraz committed
294 295

else
296 297 298 299 300 301 302 303

HEIGHT=15
WIDTH=40
ldap_setup=$(dialog --clear \
	--title "Setup ldap" \
        --msgbox "Vous devrez manuellement effectuer les opérations de setup de la base ldap sur le serveurs distant. Lancez la commande : ./install_re2o.sh ldap $ldap_password $ldap_dn" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
Gabriel Detraz's avatar
Gabriel Detraz committed
304 305 306 307
fi

echo "Ecriture de settings_local"

308
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
309 310

cp re2o/settings_local.example.py re2o/settings_local.py
311 312 313 314 315 316
if [ $sql_bdd_type == 1 ]
then
    sed -i 's/db_engine/django.db.backends.mysql/g' re2o/settings_local.py
else
    sed -i 's/db_engine/django.db.backends.postgresql_psycopg2/g' re2o/settings_local.py
fi
317
sed -i 's/SUPER_SECRET_KEY/'"$django_secret_key"'/g' re2o/settings_local.py
Gabriel Detraz's avatar
Gabriel Detraz committed
318 319 320 321 322 323 324 325 326
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
327 328
sed -i 's/MY_EMAIL_HOST/'"$email_host"'/g' re2o/settings_local.py
sed -i 's/MY_EMAIL_PORT/'"$email_port"'/g' re2o/settings_local.py
Gabriel Detraz's avatar
Gabriel Detraz committed
329

330 331 332 333 334 335 336
HEIGHT=15
WIDTH=40
migrations=$(dialog --clear \
	--title "Setup django" \
        --msgbox "Application des migrations" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
337 338
python3 manage.py migrate

339 340 341 342 343 344 345
HEIGHT=15
WIDTH=40
static=$(dialog --clear \
	--title "Setup django" \
        --msgbox "Collecte des statiques" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
chirac's avatar
chirac committed
346
python3 manage.py collectstatic
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

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

363
TITLE="Url où servir le serveur web (ex : re2o.example.org). Assurez-vous que ce tld existe bien et répond auprès du DNS"
364 365 366 367 368 369
url_server=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
        --inputbox "$TITLE" $HEIGHT $WIDTH \
        2>&1 >/dev/tty)
clear

370 371 372 373 374 375 376 377 378 379 380 381 382 383
TITLE="Utiliser tls et générer automatiquement le certificat LE ?"
OPTIONS=(1 "Oui"
         2 "Non")

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

clear

384 385
sed -i 's/URL_SERVER/'"$url_server"'/g' re2o/settings_local.py

386

387 388 389
if [ $web_serveur == 1 ]
then
apt-get -y install apache2 libapache2-mod-wsgi-py3
390
a2enmod ssl
391
a2enmod wsgi
392 393 394 395 396 397
if [ $is_tls == 1 ]
then
cp install_utils/apache2/re2o-tls.conf /etc/apache2/sites-available/re2o.conf
apt-get -y install certbot
apt-get -y install python-certbot-apache
certbot certonly --rsa-key-size 4096 --apache -d $url_server
398
sed -i 's/LE_PATH/'"$url_server"'/g' /etc/apache2/sites-available/re2o.conf
399
else
400
cp install_utils/apache2/re2o.conf /etc/apache2/sites-available/re2o.conf
401
fi
chirac's avatar
chirac committed
402
rm /etc/apache2/sites-enabled/000-default.conf
403
sed -i 's|URL_SERVER|'"$url_server"'|g' /etc/apache2/sites-available/re2o.conf
404 405 406 407 408
current_path=$(pwd)
sed -i 's|PATH|'"$current_path"'|g' /etc/apache2/sites-available/re2o.conf
a2ensite re2o
service apache2 reload
else
409 410 411 412 413 414 415 416
HEIGHT=15
WIDTH=40
web_server=$(dialog --clear \
	--title "Setup serveur web" \
        --msgbox "Nginx non supporté, vous devrez installer manuellement" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)

417
fi
Gabriel Detraz's avatar
Gabriel Detraz committed
418

419 420
python3 manage.py createsuperuser

421 422 423 424 425 426 427
HEIGHT=15
WIDTH=40
end=$(dialog --clear \
	--title "Installation terminée" \
        --msgbox "Vous pouvez à présent vous rendre sur $url_server, et vous connecter. Votre utilisateur dispose des privilèges superuser" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
428 429 430 431 432 433 434
}

main_function() {
if [ ! -z "$1" ]
then
if [ $1 == ldap ]
then
435
if [ ! -z "$2" ]
436 437 438 439 440 441 442 443 444 445 446 447 448 449
then
echo Installation du ldap
setup_ldap $2 $3
else
echo Arguments invalides !
exit
fi
fi
else
install_re2o_server
fi
}

main_function $1 $2 $3