install_re2o.sh 14.3 KB
Newer Older
1
#!/bin/bash
2 3 4 5

setup_ldap() {
	apt-get -y install slapd

6
	echo "Hashing the LDAP password..."
7 8 9
	hashed_ldap_passwd=$(slappasswd -s $1)

	echo $hashed_ldap_passwd
10
	echo "Building the LDAP config files"
11 12 13
	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

14
	echo "Deleting exisitng LDAP configuration"
15 16 17 18
	service slapd stop
	rm -rf /etc/ldap/slapd.d/*
	rm -rf /var/lib/ldap/*

19
	echo "Setting up the new LDAP configuration"
20 21 22
	slapadd -n 0 -l /tmp/schema -F /etc/ldap/slapd.d/
	slapadd -n 1 -l /tmp/db

23
	echo "Fixing the LDAP files permissions and restarting slapd"
24 25 26 27 28 29 30
	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 "Re2o setup !
This tool will help you setup re2o. It is highly recommended to use a Debian clean server for this operation.
Installing sudo and dialog packages..."
34

35 36
export DEBIAN_FRONTEND=noninteractive

37
apt-get -y install sudo dialog
38

39 40
HEIGHT=15
WIDTH=40
41 42 43 44
CHOICE_HEIGHT=4

TITLE="Re2o setup !"
MSGBOX="This tool will help you setup re2o. It is highly recommended to use a Debian clean server for this operation."
45
init=$(dialog --clear \
46 47 48 49 50
        --title "$TITLE" \
        --msgbox "$MSGBOX" \
        $HEIGHT $WIDTH \
        2>&1 >/dev/tty)

51 52


53

54 55 56 57 58



BACKTITLE="Re2o preconfiguration of the database"
TITLE="Database engine"
59
MENU="Which engine should be used as the database ?"
60 61 62 63 64 65 66 67 68 69 70 71 72
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


73 74 75



76 77
TITLE="Local extension"
INPUTBOX="The local extension to use (e.g. 'example.net'). This is used in the LDAP configuration."
78
extension_locale=$(dialog --title "$TITLE" \
79
                    --backtitle "$BACKTITLE" \
80
                    --inputbox "$INPUTBOX" \
81 82
                    $HEIGHT $WIDTH \
                    2>&1 >/dev/tty)
83 84
clear

Gabriel Detraz's avatar
Gabriel Detraz committed
85 86 87 88 89 90 91 92 93 94
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

95 96 97 98




99 100 101 102
TITLE="SQL location"
MENU="Where to install the SQL database ?
* 'Local' will setup everything automatically but is not recommended for production
* 'Remote' will ask you to manually perform some setup commands on the remote server)"
103
OPTIONS=(1 "Local"
104
         2 "Remote")
105 106 107 108 109 110 111 112 113 114
sql_is_local=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear

115 116
TITLE="SQL password"
INPUTBOX="The password to access the SQL database"
117 118
sql_password=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
119
        --inputbox "$INPUTBOX" $HEIGHT $WIDTH \
120 121 122 123 124
        2>&1 >/dev/tty)
clear


if [ $sql_is_local == 2 ]
125
then
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    TITLE="SQL username"
    INPUTBOX="The username to access the remote SQL database"
    sql_login=$(dialog --title "$TITLE" \
    	--backtitle "$BACKTITLE" \
            --inputbox "$INPUTBOX" $HEIGHT $WIDTH \
            2>&1 >/dev/tty)
    clear
    TITLE="SQL database name"
    INPUTBOX="The name of the remote SQL database"
    sql_name=$(dialog --title "$TITLE" \
    	--backtitle "$BACKTITLE" \
            --inputbox "$INPUTBOX" $HEIGHT $WIDTH \
            2>&1 >/dev/tty)
    clear
    TITLE="SQL host"
    INPUTBOX="The host of the remote SQL database"
    sql_host=$(dialog --title "$TITLE" \
    	--backtitle "$BACKTITLE" \
            --inputbox "$INPUTBOX" $HEIGHT $WIDTH \
            2>&1 >/dev/tty)
    clear
Gabriel Detraz's avatar
Gabriel Detraz committed
147
else
148 149 150
    sql_name="re2o"
    sql_login="re2o"
    sql_host="localhost"
Gabriel Detraz's avatar
Gabriel Detraz committed
151 152 153 154
fi



155 156 157 158



BACKTITLE="Re2o preconfiguration of the active directory"
159

160
TITLE="LDAP location"
161 162 163
MENU="Where to install the LDAP ?
* 'Local' will setup everything automatically but is not recommended for production
* 'Remote' will ask you to manually perform some setup commands on the remote server)"
164
OPTIONS=(1 "Local"
165
         2 "Remote")
166 167 168 169 170 171 172
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
173

174
TITLE="LDAP password"
175
INPUTBOX="The password to access the LDAP"
176 177
ldap_password=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
178
        --inputbox "$INPUTBOX" $HEIGHT $WIDTH \
179 180 181
        2>&1 >/dev/tty)
clear
if [ $ldap_is_local == 2 ]
182
then
183 184
    TITLE="CN of amdin user"
    INPUTBOX="The CN entry for the admin user of the remote LDAP"
185 186
    ldap_cn=$(dialog --title "$TITLE" \
               --backtitle "$BACKTITLE" \
187
               --inputbox "$INPUTBOX" $HEIGHT $WIDTH \
188 189
               2>&1 >/dev/tty)
    clear
190 191
    TITLE="LDAP host"
    INPUTBOX="The host of the remote LDAP"
192 193
    ldap_host=$(dialog --title "$TITLE" \
                 --backtitle "$BACKTITLE" \
194
                 --inputbox "$INPUTBOX" $HEIGHT $WIDTH \
195 196
                 2>&1 >/dev/tty)
    clear
197
    TITLE="Activate TLS for remote LDAP ?"
198 199 200 201 202 203 204 205 206
    OPTIONS=(1 "Yes"
             2 "No")
    ldap_tls=$(dialog --title "$TITLE" \
                --backtitle "$BACKTITLE" \
                --MENU "$MENU"\
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)
    clear
Gabriel Detraz's avatar
Gabriel Detraz committed
207
else
208 209 210 211
    ldap_cn="cn=admin,"
    ldap_cn+=$ldap_dn
    ldap_host="localhost"
    ldap_tls=2
Gabriel Detraz's avatar
Gabriel Detraz committed
212 213 214
fi


215 216 217 218



BACKTITLE="Re2o preconfiguration of the mail server"
219 220 221

TITLE="Mail server host"
INPUTBOX="The host of the mail server to use"
222 223
email_host=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
224 225
        --inputbox "$TITLE" \
        $HEIGHT $WIDTH \
226 227
        2>&1 >/dev/tty)

228 229
TITLE="Mail server Port"
MENU="Which port (thus which protocol) to use to contact the mail server"
230 231 232
OPTIONS=(25 "SMTP"
         465 "SMTPS"
	 587 "Submission")
233 234 235 236 237 238 239 240 241 242
email_port=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)
clear


243 244


245 246 247 248 249 250 251
TITLE="Re2o setup !"
MSGBOX="Setup of the required packages"
install_base=$(dialog --clear \
	--title "$TITLE" \
        --msgbox "$MSGBOX" \
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
252

253
echo "Setup of the required packages"
254 255 256
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 python3-git libjs-jquery libjs-jquery-uil libjs-jquery-timepicker libjs-bootstrap
pip3 install django-bootstrap3 django-ldapdb==0.9.0 django-macaddress

Gabriel Detraz's avatar
Gabriel Detraz committed
257

258 259 260 261



echo "SQL Database setup"
262
if [ $sql_bdd_type == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
263
then
264
    apt-get -y install python3-mysqldb mysql-client
265 266 267 268
    mysql_command="CREATE DATABASE $sql_name collate='utf8_general_ci';
        CREATE USER '$sql_login'@'localhost' IDENTIFIED BY '$sql_password';
        GRANT ALL PRIVILEGES ON $sql_name.* TO '$sql_login'@'localhost';
        FLUSH PRIVILEGES;"
269
    if [ $sql_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
270
    then
271 272
        apt-get -y install mysql-server
        mysql -u root --execute="$mysql_command"
Gabriel Detraz's avatar
Gabriel Detraz committed
273
    else
274 275 276 277 278 279 280 281 282 283 284
        echo "Please execute the following command on the remote SQL server and then continue"
        echo "$mysql_command"
        while true
	do
            read -p "Continue (y/n)?" choice
            case "$choice" in
                y|Y ) break;;
                n|N ) exit;;
                * ) echo "Invalid";;
            esac
        done
Gabriel Detraz's avatar
Gabriel Detraz committed
285
    fi
286
else
287 288 289 290
    apt-get -y install postgresql-client python3-psycopg2
    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;"
291
    if [ $sql_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
292
    then
293 294 295 296
        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"
297
    else
298 299 300 301 302 303 304 305 306 307 308 309 310
        echo "Please execute the following commands on the remote SQL server and then continue"
        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
            case "$choice" in
                y|Y ) break;;
                n|N ) exit;;
                * ) echo "Invalid";;
            esac
        done
Gabriel Detraz's avatar
Gabriel Detraz committed
311
    fi
312
fi
Gabriel Detraz's avatar
Gabriel Detraz committed
313

314 315 316 317 318




echo "LDAP setup"
319
if [ $ldap_is_local == 1 ]
Gabriel Detraz's avatar
Gabriel Detraz committed
320
then
321 322 323 324 325 326 327 328 329 330
    setup_ldap $ldap_password $ldap_dn
else
    TITLE="LDAP server setup"
    MSGBOX="Please manually setup the remote LDAP server by launching the following commands: ./install_re2o.sh ldap $ldap_password $ldap_dn"
    ldap_setup=$(dialog --clear \
                  --title "$TITLE" \
                  --msgbox "$MSGBOX" \
                  $HEIGHT $WIDTH \
                  2>&1 >/dev/tty)
fi
Gabriel Detraz's avatar
Gabriel Detraz committed
331 332


333

Gabriel Detraz's avatar
Gabriel Detraz committed
334

335 336

echo "Writing of the settings_local.py file"
Gabriel Detraz's avatar
Gabriel Detraz committed
337

338
django_secret_key=$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(50)]))")
339
aes_key=$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(32)]))")
Gabriel Detraz's avatar
Gabriel Detraz committed
340 341

cp re2o/settings_local.example.py re2o/settings_local.py
342 343 344 345 346 347
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
348
sed -i 's/SUPER_SECRET_KEY/'"$django_secret_key"'/g' re2o/settings_local.py
Gabriel Detraz's avatar
Gabriel Detraz committed
349
sed -i 's/SUPER_SECRET_DB/'"$sql_password"'/g' re2o/settings_local.py
350
sed -i 's/A_SECRET_AES_KEY/'"$aes_key"'/g' re2o/settings_local.py
Gabriel Detraz's avatar
Gabriel Detraz committed
351 352 353 354
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
355 356 357 358
if [ $ldap_tls == 2 ]
then
    sed -i "s/'TLS': True,/# 'TLS': True,#/g" re2o/settings_local.py
fi
Gabriel Detraz's avatar
Gabriel Detraz committed
359 360 361 362
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
363 364
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
365

366 367 368 369 370



TITLE="Django setup"
MSGBOX="Applying the Django database migrations"
371
migrations=$(dialog --clear \
372 373
	--title "$TITLE" \
        --msgbox "$MSGBOX" \
374 375
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
376

377 378
python3 manage.py migrate

379 380 381 382 383 384 385





TITLE="Django setup"
MSGBOX="Collecting statics"
386
static=$(dialog --clear \
387 388
	--title "$TITLE" \
        --msgbox "$MSGBOX" \
389 390
	$HEIGHT $WIDTH \
	2>&1 >/dev/tty)
391

chirac's avatar
chirac committed
392
python3 manage.py collectstatic
393

394 395 396 397



BACKTITLE="Web server"
398

399
TITLE="Web server to use"
400
MENU="Which web server to install for accessing Re2o web frontend (automatic setup of nginx is not supported) ?"
401 402 403 404 405 406 407 408 409 410 411 412
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

413 414
TITLE="Web URL"
INPUTBOX="URL for accessing the web server (e.g. re2o.example.net). Be sure that this URL is accessible and correspond to a DNS entry if applicable."
415 416
url_server=$(dialog --title "$TITLE" \
	--backtitle "$BACKTITLE" \
417
        --inputbox "$INPUTBOX" \
418
	$HEIGHT $WIDTH \
419 420 421
        2>&1 >/dev/tty)
clear

422 423 424 425
TITLE="TLS on web server"
MENU="Would you like to activate the TLS (with Let'Encrypt) on the web server ?"
OPTIONS=(1 "Yes"
         2 "No")
426 427 428 429 430 431 432 433 434 435
is_tls=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear

436 437
sed -i 's/URL_SERVER/'"$url_server"'/g' re2o/settings_local.py

438 439
if [ $web_serveur == 1 ]
then
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    apt-get -y install apache2 libapache2-mod-wsgi-py3
    a2enmod ssl
    a2enmod wsgi
    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
        sed -i 's/LE_PATH/'"$url_server"'/g' /etc/apache2/sites-available/re2o.conf
    else
        cp install_utils/apache2/re2o.conf /etc/apache2/sites-available/re2o.conf
    fi
    rm /etc/apache2/sites-enabled/000-default.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
459
else
460
    TITLE="Web server setup"
461
    MSGBOX="Nginx automatic setup is not supported. Please configure it manually."
462 463 464 465 466
    web_server=$(dialog --clear \
                  --title "$TITLE" \
                  --msgbox "$MSGBOX" \
                  $HEIGHT $WIDTH \
                  2>&1 >/dev/tty)
467
fi
Gabriel Detraz's avatar
Gabriel Detraz committed
468

469 470
python3 manage.py createsuperuser

471 472 473 474 475 476




TITLE="End of the setup"
MSGBOX="You can now visit $url_server and connect with the credentials you just entered. This user hhas the superuser rights, meaning he can access and do everything."
477
end=$(dialog --clear \
478
	--title "$TITLE" \
479 480 481
        --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)
482 483
}

484

485
main_function() {
486 487 488 489 490 491
    if [ ! -z "$1" ]
    then
        if [ $1 == ldap ]
        then
            if [ ! -z "$2" ]
            then
492
                echo "Installation du ldap"
493 494
                setup_ldap $2 $3
            else
495
                echo "Arguments invalides !"
496 497 498 499 500 501
                exit
            fi
        fi
    else
        install_re2o_server
    fi
502 503 504
}

main_function $1 $2 $3