install_re2o.sh 30.4 KB
Newer Older
1
#!/bin/bash
2

3 4
SETTINGS_LOCAL_FILE='re2o/settings_local.py'
SETTINGS_EXAMPLE_FILE='re2o/settings_local.example.py'
5

6 7 8 9 10 11
APT_REQ_FILE="apt_requirements.txt"
PIP_REQ_FILE="pip_requirements.txt"

LDIF_DB_FILE="install_utils/db.ldiff"
LDIF_SCHEMA_FILE="install_utils/schema.ldiff"

12

13 14 15 16 17 18 19 20 21 22 23 24 25
VALUE= # global value used to return values by some functions

_ask_value() {
    ### Usage _ask_value <text> [<option1> [<option2> ... ] ]
    #
    #   This function is a utility function to force a user to enter a value
    #   available in a set of options.
    #
    #   Parameters:
    #     * text: The text to display
    #     * option#: A possible option for the user. If no option is specifed,
    #       all inputs are considered valid
    #
26
    #   Echo: The value entered by the user. Should be one of the choices if
27 28 29 30 31 32 33 34 35 36
    #     not ommited
    ###

    shopt -s extglob

    input_text="$1"
    shift
    if [ "$#" -ne 0 ]; then
        choices="("
        while [ "$#" -ne 1 ]; do
37
            choices+="$1|"
38 39
            shift
        done
40 41 42
        choices+="$1)"
        input_text+=" $choices: "
        choices="@$choices"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    else
        input_text+=": "
        choices="@(*)"
    fi

    while true; do
        read -p "$input_text" choice
        case "$choice" in
            $choices ) break;;
            * ) echo "Invalid option";;
        esac
    done

    VALUE="$choice"
}


60

61
install_requirements() {
62
    ### Usage: install_requirements
63 64
    #
    #   This function will install the required packages from APT repository
65
    #   and Pypi repository. Those packages are all required for Re2o to work
66 67 68 69
    #   properly.
    ###

    echo "Setting up the required packages ..."
70 71
    cat $APT_REQ_FILE | xargs apt-get -y install
    pip3 install -r $PIP_REQ_FILE
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    echo "Setting up the required packages: Done"
}



install_database() {
    ### Usage: install_database <engine_type> <local_setup> <db_name> <username> <password>
    #
    #   This function will install the database by downloading the correct APT packages
    #   and initiating the database schema.
    #
    #   Parameters:
    #     * engine_type: The DB engine to use.
    #       1 = mysql
    #       2 = postgresql
    #     * local_setup: Should the database be installed locally
    #       1 = yes
    #       2 = no
    #     * db_name: The name of the database itself
    #     * username: The username to access the database
    #     * password: The password of the user to access the database
    ###

    echo "Setting up the database ..."

97 98 99 100 101
    engine_type="$1"
    local_setup="$2"
    db_name="$3"
    username="$4"
    password="$5"
102

103
    if [ "$engine_type" == 1 ]; then
104 105 106 107 108 109 110 111 112 113

        echo "Installing MySQL client ..."
        apt-get -y install python3-mysqldb mysql-client
        echo "Installing MySQL client: Done"

        mysql_command="CREATE DATABASE $db_name collate='utf8_general_ci';
            CREATE USER '$username'@'localhost' IDENTIFIED BY '$password';
            GRANT ALL PRIVILEGES ON $db_name.* TO '$username'@'localhost';
            FLUSH PRIVILEGES;"

114
        if [ "$local_setup" == 1 ]; then
115 116 117 118 119 120 121
            echo "Setting up local MySQL server ..."
            apt-get -y install mysql-server
            mysql -u root --execute="$mysql_command"
            echo "Setting up local MySQL server: Done"
        else
            echo "Please execute the following command on the remote SQL server and then continue"
            echo "$mysql_command"
122
            _ask_value "Continue" "yes" "no"; if [ "$VALUE" == "no" ]; then exit; fi
123 124 125 126 127 128 129 130 131 132 133 134
        fi

    else

        echo "Installing PostgreSQL client ..."
        apt-get -y install postgresql-client python3-psycopg2
        echo "Installing PostgreSQL client: Done"

        pgsql_command1="CREATE DATABASE $db_name ENCODING 'UTF8' LC_COLLATE='fr_FR.UTF-8' LC_CTYPE='fr_FR.UTF-8';"
        pgsql_command2="CREATE USER $username with password '$password';"
        pgsql_command3="ALTER DATABASE $db_name owner to $username;"

135
        if [ "$local_setup" == 1 ]; then
136 137 138 139 140 141 142 143 144 145 146
            echo "Setting up local PostgreSQL server ..."
            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"
            echo "Setting up local PostgreSQL server: Done"
        else
            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"
147
            _ask_value "Continue" "yes" "no"; if [ "$VALUE" == "no" ]; then exit; fi
148 149 150 151 152 153 154 155 156
        fi

    fi

    echo "Setting up the database: Done"
}



157 158
install_ldap() {
    ### Usage: install_ldap <local_setup> <password> <domain>
159
    #
160
    #   This function will install the LDAP
161 162
    #
    #   Parameters:
163
    #     * local_setup: Should the LDAP be installed locally ?
164 165 166 167 168 169
    #       1 = yes
    #       2 = no
    #     * password: the clear password for the admin user of the LDAP
    #     * domain: the domain extension to use for the LDAP structure in LDAP notation
    ###

170
    echo "Setting up the LDAP ..."
171

172 173 174
    local_setup="$1"
    password="$2"
    domain="$3"
175

176
    if [ "$local_setup" == 1 ]; then
177

178 179 180 181 182
        echo "Installing slapd package ..."
        apt-get -y install slapd
        echo "Installing slapd package: Done"

        echo "Hashing the LDAP password ..."
Maël Kervella's avatar
Maël Kervella committed
183
        hashed_ldap_passwd="$(slappasswd -s $password)"
184 185 186
        echo "Hash of the password: $hashed_ldap_passwd"

        echo "Building the LDAP config files ..."
187 188
        sed 's|dc=example,dc=net|'"$domain"'|g' $LDIF_DB_FILE | sed 's|FILL_IT|'"$hashed_ldap_passwd"'|g' > /tmp/db
        sed 's|dc=example,dc=net|'"$domain"'|g' $LDIF_SCHEMA_FILE | sed 's|FILL_IT|'"$hashed_ldap_passwd"'|g' > /tmp/schema
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        echo "Building the LDAP config files: Done"

        echo "Stopping slapd service ..."
        service slapd stop
        echo "Stopping slapd service: Done"

        echo "Deleting exisitng LDAP configuration ..."
        rm -rf /etc/ldap/slapd.d/*
        rm -rf /var/lib/ldap/*
        echo "Deleting existing LDAP configuration: Done"

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

        echo "Fixing the LDAP files permissions ..."
        chown -R openldap:openldap /etc/ldap/slapd.d
        chown -R openldap:openldap /var/lib/ldap
        echo "Fixing the LDAP files permissions: Done"

        echo "Starting slapd service ..."
        service slapd start
        echo "Starting slapd service: Done"
213 214 215 216

    else

        echo "Please execute the following command on the remote LDAP server and then continue"
217
        echo "./install_re2o.sh setup-ldap $password $domain"
218
        _ask_value "Continue" "yes" "no"; if [ "$VALUE" == "no" ]; then exit; fi
219 220 221

    fi

222
    echo "Setting up the LDAP: Done"
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
}



write_settings_file() {
    ### Usage: write_settings_file <db_engine_type> <sql_hostname> <sql_db_name> <sql_username> <sql_password>
    #                              <ldap_cn> <ldap_tls> <ldap_password> <ldap_hostname> <ldap_domain>
    #                              <email_hostname> <email_port> <extension> <url>
    #
    #   This function will write a clean local settings file based on the example.
    #
    #   Parameters:
    #     * db_engine_type: The engine for the database
    #       1 = MySQL
    #       2 = PostgreSQL
    #     * sql_hostname: The hostname for contacting the database
    #     * sql_db_name: The name of the database itself
    #     * sql_username: The user to use to access the database
    #     * sql_password: The password to use to access the database
242 243
    #     * ldap_cn: The CN entry for the LDAP admin in LDAP notation
    #     * ldap_tls: Should the TLS be activated to contact the LDAP
244 245
    #       1 = yes
    #       2 = no
246 247 248
    #     * ldap_password: The password to use to connect to the LDAP
    #     * ldap_hostname: The hostname for contacting the LDAP
    #     * ldap_domain: The local domain for the LDAP in LDAP notation
249 250 251 252 253 254 255 256
    #     * email_hostname: The hostname for contacting the mail server
    #     * email_port: The port for contacting the mail server
    #     * extension: The extension to use
    #     * url: The main URL to use for Re2o
    ###

    echo "Writing of the settings_local.py file ..."

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    db_engine_type="$1"
    sql_hostname="$2"
    sql_db_name="$3"
    sql_username="$4"
    sql_password="$5"
    ldap_cn="$6"
    ldap_tls="$7"
    ldap_password="$8"
    ldap_hostname="$9"
    ldap_domain="${10}"
    email_hostname="${11}"
    email_port="${12}"
    extension="${13}"
    url="${14}"

    cp "$SETTINGS_EXAMPLE_FILE" "$SETTINGS_LOCAL_FILE"

    django_secret_key="$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(50)]))")"
    aes_key="$(python -c "import random; print(''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789%=+') for i in range(32)]))")"
276

277 278
    if [ "$db_engine_type" == 1 ]; then
        sed -i 's/db_engine/django.db.backends.mysql/g' "$SETTINGS_LOCAL_FILE"
279
    else
280
        sed -i 's/db_engine/django.db.backends.postgresql_psycopg2/g' "$SETTINGS_LOCAL_FILE"
281
    fi
282 283 284 285 286 287 288
    sed -i 's/SUPER_SECRET_KEY/'"$django_secret_key"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/SUPER_SECRET_DB/'"$sql_password"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/A_SECRET_AES_KEY/'"$aes_key"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/db_name_value/'"$sql_db_name"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/db_user_value/'"$sql_username"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/db_host_value/'"$sql_hostname"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/ldap_dn/'"$ldap_cn"'/g' "$SETTINGS_LOCAL_FILE"
289
    if [ $ldap_tls == 2 ]; then
290
        sed -i "s/'TLS': True,/# 'TLS': True,/g" "$SETTINGS_LOCAL_FILE"
291
    fi
292 293 294 295 296 297 298
    sed -i 's/SUPER_SECRET_LDAP/'"$ldap_password"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/ldap_host_ip/'"$ldap_hostname"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/dc=example,dc=net/'"$ldap_domain"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/example.net/'"$extension"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/MY_EMAIL_HOST/'"$email_hostname"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/MY_EMAIL_PORT/'"$email_port"'/g' "$SETTINGS_LOCAL_FILE"
    sed -i 's/URL_SERVER/'"$url"'/g' "$SETTINGS_LOCAL_FILE"
299 300 301 302 303 304

    echo "Writing of the settings_local.py file: Done"
}



305 306 307 308 309 310 311 312 313 314 315 316 317 318
update_django() {
    ### Usage: update_django
    #
    #   This function will update the Django project by applying the migrations
    #   and collecting the statics
    ###

    echo "Applying Django migrations ..."
    python3 manage.py migrate
    echo "Applying Django migrations: Done"

    echo "Collecting web frontend statics ..."
    python3 manage.py collectstatic --noinput
    echo "Collecting web frontend statics: Done"
319 320 321 322

    echo "Generating locales ..."
    python3 manage.py compilemessages
    echo "Generating locales: Done"
323 324 325 326
}



327 328 329 330 331 332 333
copy_templates_files() {
    ### Usage: copy_templates_files
    #
    #   This will copy LaTeX templates in the media root.

    echo "Copying LaTeX templates ..."
    mkdir -p media/templates/
334 335
    cp cotisations/templates/cotisations/factures.tex media/templates/default_invoice.tex
    cp cotisations/templates/cotisations/voucher.tex media/templates/default_voucher.tex
336 337 338 339 340
    echo "Copying LaTeX templates: Done"
}



341 342 343 344 345 346 347 348 349 350 351 352
create_superuser() {
    ### Usage: create_superuser
    #
    #   This will create a user with the superuser rights for the project.

    echo "Creating a superuser ..."
    python3 manage.py createsuperuser
    echo "Creating a superuser: Done"
}



353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
install_webserver() {
    ### Usage: install_webserver <engine_type> <tls> <url>
    #
    #   This function will install the web server by installing the correct APT packages
    #   and configure it
    #
    #   Parameters:
    #     * engine_type: The engine to use as a web server
    #       1 = Apache2
    #       2 = NginX
    #     * tls: Should the TLS (with LE) be generated and activated
    #       1 = yes
    #       2 = no
    #     * url: The url to access Re2o. This parameter is only used if TLS is activated
    #       for generating the certifcate with the right domain name
    ###

    echo "Setting up web server ..."

372 373 374
    engine_type="$1"
    tls="$2"
    url="$3"
375

376
    if [ "$engine_type" == 1 ]; then
377 378 379 380 381 382 383 384

        echo "Setting up Apache2 web server ..."

        apt-get -y install apache2 libapache2-mod-wsgi-py3
        a2enmod ssl
        a2enmod wsgi
        a2enconf javascript-common

385
        if [ "$tls" == 1 ]; then
386 387 388 389
            echo "Setting up TLS with LE for Apache2 web server ..."
            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
390
            certbot certonly --rsa-key-size 4096 --apache -d "$url"
391 392 393 394 395 396 397 398
            sed -i 's/LE_PATH/'"$url"'/g' /etc/apache2/sites-available/re2o.conf
            echo "Setting up TLS with LE for Apache2 web server: Done"
        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"'|g' /etc/apache2/sites-available/re2o.conf
399
        sed -i 's|PATH|'"$(pwd)"'|g' /etc/apache2/sites-available/re2o.conf
400 401 402 403 404 405 406 407 408 409 410
        a2ensite re2o

        echo "Setting up Apache2 web server: Done"

        echo "Reloading Apache2 service ..."
        service apache2 reload
        echo "Reloading Apache2 service: Done"

    else

        echo "Nginx automatic setup is not supported. Please configure it manually."
411 412
        echo "Please confirm you have acknowledged this message."
        _ask_value "Acknowledged" "yes"
413 414 415 416 417 418 419 420 421 422

    fi

    echo "Setting up web server: Done"
}



interactive_guide() {
    ### Usage: interactive_guide
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    #
    #   This function will guide through the automated setup of Re2o by asking
    #   the user for some informations and some installation choices. It will
    #   then proceed to setup and configuration of the required tools according
    #   to the user choices.
    ###

    echo "Re2o setup !"
    echo "This tool will help you setup re2o. It is highly recommended to use a Debian clean server for this operation."

    echo "Installing basic packages required for this script to work  ..."
    apt-get -y install sudo dialog
    echo "Installing basic packages required for this script to work: Done"

    # Common setup for the dialog prompts
    export DEBIAN_FRONTEND=noninteractive
439 440
    HEIGHT=20
    WIDTH=60
441 442 443 444 445 446 447 448 449 450 451 452 453
    CHOICE_HEIGHT=4



    #############
    ## Welcome ##
    #############

    BACKTITLE="Re2o setup"

    # Welcome prompt
    TITLE="Welcome"
    MSGBOX="This tool will help you setup re2o. It is highly recommended to use a Debian clean server for this operation."
454
    init="$(dialog --clear --backtitle "$BACKTITLE" \
455
        --title "$TITLE" --msgbox "$MSGBOX" \
456
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
457 458 459 460 461 462 463 464 465 466 467 468 469 470



    ######################
    ## Database options ##
    ######################

    BACKTITLE="Re2o setup - configuration of the database"

    # Prompt for choosing the database engine
    TITLE="Database engine"
    MENU="Which engine should be used as the database ?"
    OPTIONS=(1 "mysql"
             2 "postgresql")
471
    sql_bdd_type="$(dialog --clear --backtitle "$BACKTITLE" \
472
        --title "$TITLE" --menu "$MENU" \
473
        $HEIGHT $WIDTH $CHOICE_HEIGHT "${OPTIONS[@]}" 2>&1 >/dev/tty)"
474 475 476 477 478

    # Prompt for choosing the database location
    TITLE="SQL location"
    MENU="Where to install the SQL database ?
    * 'Local' will setup everything automatically but is not recommended for production
479
    * 'Remote' will ask you to manually perform some setup commands on the remote server"
480 481
    OPTIONS=(1 "Local"
             2 "Remote")
482
    sql_is_local="$(dialog --clear --backtitle "$BACKTITLE" \
483
        --title "$TITLE" --menu "$MENU" \
484
        $HEIGHT $WIDTH $CHOICE_HEIGHT "${OPTIONS[@]}" 2>&1 >/dev/tty)"
485 486 487 488 489

    if [ $sql_is_local == 2 ]; then
        # Prompt to enter the remote database hostname
        TITLE="SQL hostname"
        INPUTBOX="The hostname of the remote SQL database"
490
        sql_host="$(dialog --clear --backtitle "$BACKTITLE" \
491
            --title "$TITLE" --inputbox "$INPUTBOX" \
492
            $HEIGHT $WIDTH 2>&1 >/dev/tty)"
493

494 495 496
        # Prompt to enter the remote database name
        TITLE="SQL database name"
        INPUTBOX="The name of the remote SQL database"
497
        sql_name="$(dialog --clear --backtitle "$BACKTITLE" \
498
            --title "$TITLE" --inputbox "$INPUTBOX" \
499
            $HEIGHT $WIDTH 2>&1 >/dev/tty)"
500 501 502 503

        # Prompt to enter the remote database username
        TITLE="SQL username"
        INPUTBOX="The username to access the remote SQL database"
504
        sql_login="$(dialog --clear --backtitle "$BACKTITLE" \
505
            --title "$TITLE" --inputbox "$INPUTBOX" \
506
            $HEIGHT $WIDTH 2>&1 >/dev/tty)"
507 508 509 510 511 512 513
        clear
    else
        # Use of default values for local setup
        sql_name="re2o"
        sql_login="re2o"
        sql_host="localhost"
    fi
514

515 516 517
    # Prompt to enter the database password
    TITLE="SQL password"
    INPUTBOX="The password to access the SQL database"
518
    sql_password="$(dialog --clear --backtitle "$BACKTITLE" \
519
        --title "$TITLE" --inputbox "$INPUTBOX" \
520
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
521 522 523



524 525 526
    ##################
    ## LDAP options ##
    ##################
527

528
    BACKTITLE="Re2o setup - configuration of the LDAP"
529 530 531 532 533

    # Prompt to choose the LDAP location
    TITLE="LDAP location"
    MENU="Where would you like to install the LDAP ?
    * 'Local' will setup everything automatically but is not recommended for production
534
    * 'Remote' will ask you to manually perform some setup commands on the remote server"
535 536
    OPTIONS=(1 "Local"
             2 "Remote")
537
    ldap_is_local="$(dialog --clear --backtitle "$BACKTITLE" \
538
        --title "$TITLE" --menu "$MENU" \
539
        $HEIGHT $WIDTH $CHOICE_HEIGHT "${OPTIONS[@]}" 2>&1 >/dev/tty)"
540

541 542 543
    # Prompt to enter the LDAP domain extension
    TITLE="Domain extension"
    INPUTBOX="The local domain extension to use (e.g. 'example.net'). This is used in the LDAP configuration."
544
    extension_locale="$(dialog --clear --backtitle "$BACKTITLE" \
545
        --title "$TITLE" --inputbox "$INPUTBOX" \
546
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
547

548 549 550 551 552 553
    # Building the DN of the LDAP from the extension
    IFS='.' read -a extension_locale_array <<< $extension_locale
    for i in "${extension_locale_array[@]}"
    do
        ldap_dn+="dc=$i,"
    done
554
    ldap_dn="${ldap_dn::-1}"
555

556
    if [ "$ldap_is_local" == 2 ]; then
557 558 559
        # Prompt to enter the remote LDAP hostname
        TITLE="LDAP hostname"
        INPUTBOX="The hostname of the remote LDAP"
560
        ldap_host="$(dialog --clear --backtitle "$BACKTITLE" \
561
            --title "$TITLE" --inputbox "$INPUTBOX" \
562
            $HEIGHT $WIDTH 2>&1 >/dev/tty)"
563

564 565 566 567 568
        # Prompt to choose if TLS should be activated or not for the LDAP
        TITLE="TLS on LDAP"
        MENU="Would you like to activate TLS for communicating with the remote LDAP ?"
        OPTIONS=(1 "Yes"
                 2 "No")
569
        ldap_tls="$(dialog --clear --backtitle "$BACKTITLE" \
570
            --title "$TITLE" --MENU "$MENU" \
571
            $HEIGHT $WIDTH $CHOICE_HEIGHT "${OPTIONS[@]}" 2>&1 >/dev/tty)"
572 573 574 575

        # Prompt to enter the admin's CN of the remote LDAP
        TITLE="CN of amdin user"
        INPUTBOX="The CN entry for the admin user of the remote LDAP"
576
        ldap_cn="$(dialog --clear --backtitle "$BACKTITLE" \
577
            --title "$TITLE" --inputbox "$INPUTBOX" \
578
            $HEIGHT $WIDTH 2>&1 >/dev/tty)"
579 580
    else
        ldap_cn="cn=admin,"
581
        ldap_cn+="$ldap_dn"
582 583 584
        ldap_host="localhost"
        ldap_tls=2
    fi
585

586 587 588
    # Prompt to enter the LDAP password
    TITLE="LDAP password"
    INPUTBOX="The password to access the LDAP"
589
    ldap_password="$(dialog --clear --backtitle "$BACKTITLE" \
590
        --title "$TITLE" --inputbox "$INPUTBOX" \
591
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
592 593 594 595 596 597 598 599



    #########################
    ## Mail server options ##
    #########################

    BACKTITLE="Re2o setup - configuration of the mail server"
600

601 602 603
    # Prompt to enter the hostname of the mail server
    TITLE="Mail server hostname"
    INPUTBOX="The hostname of the mail server to use"
604
    email_host="$(dialog --clear --backtitle "$BACKTITLE" \
605
        --title "$TITLE" --inputbox "$TITLE" \
606
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
607

608
    # Prompt to choose the port of the mail server
609 610 611 612 613
    TITLE="Mail server port"
    MENU="Which port (thus which protocol) to use to contact the mail server"
    OPTIONS=(25 "SMTP"
             465 "SMTPS"
             587 "Submission")
614
    email_port="$(dialog --clear --backtitle "$BACKTITLE" \
615
        --title "$TITLE" --menu "$MENU" \
616
        $HEIGHT $WIDTH $CHOICE_HEIGHT "${OPTIONS[@]}" 2>&1 >/dev/tty)"
617 618 619 620 621 622 623 624



    ########################
    ## Web server options ##
    ########################

    BACKTITLE="Re2o setup - configuration of the web server"
625

626 627 628 629 630
    # Prompt to choose the web server
    TITLE="Web server to use"
    MENU="Which web server to install for accessing Re2o web frontend (automatic setup of nginx is not supported) ?"
    OPTIONS=(1 "apache2"
             2 "nginx")
631
    web_serveur="$(dialog --clear --backtitle "$BACKTITLE" \
632
        --title "$TITLE" --menu "$MENU" \
633
        $HEIGHT $WIDTH $CHOICE_HEIGHT "${OPTIONS[@]}" 2>&1 >/dev/tty)"
634

635 636 637
    # Prompt to enter the requested URL for the web frontend
    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)."
638
    url_server="$(dialog --clear --backtitle "$BACKTITLE" \
639
        --title "$TITLE" --inputbox "$INPUTBOX" \
640
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
641

642 643 644 645 646
    # Prompt to choose if the TLS should be setup or not for the web server
    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")
647
    is_tls="$(dialog --clear --backtitle "$BACKTITLE" \
648
        --title "$TITLE" --menu "$MENU" \
649
        $HEIGHT $WIDTH $CHOICE_HEIGHT "${OPTIONS[@]}" 2>&1 >/dev/tty)"
650 651


652

653 654 655
    ###############################
    ## End of configuration step ##
    ###############################
656

657
    BACKTITLE="Re2o setup"
658

659 660 661 662 663 664 665 666 667 668 669
    # Prompt to inform the config setup is over
    TITLE="End of configuration step"
    MSGBOX="The configuration step is now finished. The script will now perform the following actions:
    * Install the required packages
    * Install and setup the requested database if 'local' has been selected
    * Install and setup the ldap if 'local' has been selected
    * Write a local version of 'settings_local.py' file with the previously given informations
    * Apply the Django migrations for the project
    * Collect the statics for the web interface
    * Install and setup the requested web server
    * Install and setup a TLS certificate for the web server if requested"
670
    end_config="$(dialog --clear --backtitle "$BACKTITLE" \
671
        --title "$TITLE" --msgbox "$MSGBOX" \
672
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
673

674
    clear
675

676

677

678 679 680
    ################################
    ## Perform the actual actions ##
    ################################
681

682
    install_requirements
683

684
    install_database "$sql_bdd_type" "$sql_is_local" "$sql_name" "$sql_login" "$sql_password"
685

686
    install_ldap "$ldap_is_local" "$ldap_password" "$ldap_dn"
687 688


689 690 691
    write_settings_file "$sql_bdd_type" "$sql_host" "$sql_name" "$sql_login" "$sql_password" \
                        "$ldap_cn" "$ldap_tls" "$ldap_password" "$ldap_host" "$ldap_dn" \
                        "$email_host" "$email_port" "$extension_locale" "$url_server"
692

693 694 695
    update_django

    create_superuser
696

697
    install_webserver "$web_serveur" "$is_tls" "$url_server"
698

699 700 701 702 703 704 705 706 707 708


    ###########################
    ## End of the setup step ##
    ###########################

    BACKTITLE="Re2o setup"

    # Prompt to inform the installation process is over
    TITLE="End of the setup"
709
    MSGBOX="You can now visit $url_server and connect with the credentials you just entered. This user has the superuser rights, meaning he can access and do everything."
710
    end="$(dialog --clear --backtitle "$BACKTITLE" \
Maël Kervella's avatar
Maël Kervella committed
711
        --title "$TITLE" --msgbox "$MSGBOX" \
712
        $HEIGHT $WIDTH 2>&1 >/dev/tty)"
713 714
}

715

716 717 718 719 720 721 722 723


interactive_update_settings() {
    ### Usage: interactvie_update_settings
    #
    #   This function will take the parameters in the example settings file, retrieve the
    #   existing parameters from the local settings file and ask the user for the missing parameters
    ###
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    _ask_value "Database engine" "mysql" "postgresql"; if [ "$VALUE" == "mysql" ]; then db_engine_type=1; else db_engine_type=2; fi
    _ask_value "Database hostname"; sql_hostname="$VALUE"
    _ask_value "Database name"; sql_db_name="$VALUE"
    _ask_value "Database username"; sql_username="$VALUE"
    _ask_value "Database password"; sql_password="$VALUE"
    _ask_value "LDAP hostname"; ldap_hostname="$VALUE"
    _ask_value "Activate TLS on LDAP" "yes" "no"; if [ "$VALUE" == "mysql" ]; then ldap_tls=1; else ldap_tls=2; fi
    _ask_value "LDAP domain (e.g. 'dc=example,dc=net')"; ldap_domain="$VALUE"
    _ask_value "LDAP admin CN entry (e.g. 'cn=admin,dc=example,dc=net')"; ldap_cn="$VALUE"
    _ask_value "LDAP password"; ldap_password="$VALUE"
    _ask_value "Mail server hostname"; email_hostname="$VALUE"
    _ask_value "Mail server port" "25" "465" "587"; email_port="$VALUE"
    _ask_value "Extension de domain (e.g. 'example.net')"; extension="$VALUE"
    _ask_value "Main URL"; url="$VALUE"
    write_settings_file "$db_engine_type" "$sql_hostname" "$sql_db_name" "$sql_username" "$sql_password" \
                        "$ldap_cn" "$ldap_tls" "$ldap_password" "$ldap_hostname" "$ldap_domain" \
                        "$email_hostname" "$email_port" "$extension" "$url"
741 742 743 744 745

}



746
main_function() {
747
    ### Usage: main_function [subcommand [options]]
748 749
    #
    #   This function will parse the arguments to determine which part of the tool to start.
750
    #   Refer to the help message below for the details of the parameters
751 752
    ###

753 754 755 756 757 758 759 760 761 762 763 764
    if [ -z "$1" ] || [ "$1" == "help" ]; then
        echo ""
        echo "Usage: install_re2o [subcommand [options]]"
        echo ""
        echo "The 'install_re2o' script is a utility script to setup, configure, reset and update"
        echo "some components of re2o. Please refer to the following details for more."
        echo ""
        echo "Sub-commands:"
        echo "  * [no subcommand] - Display this quick usage documentation"
        echo "  * {help} ---------- Display this quick usage documentation"
        echo "  * {setup} --------- Launch the full interactive guide to setup entirely"
        echo "                      re2o from scratch"
765
        echo "  * {update} -------- Collect frontend statics, install the missing APT and copy LaTeX templates files"
766 767
        echo "                      and pip packages and apply the migrations to the DB"
        echo "  * {update-django} - Apply Django migration and collect frontend statics"
768
        echo "  * {copy-template-files} - Copy LaTeX templates files to media/templates"
769 770 771
        echo "  * {update-packages} Install the missing APT and pip packages"
        echo "  * {update-settings} Interactively rewrite the settings file"
        echo "  * {reset-db} ------ Erase the previous local database, setup a new empty"
772 773
        echo "                      one and apply the Django migrations on it and collect"
        echo "                      Django statics."
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
        echo "      Parameters:"
        echo "        * <db_password> -- the clear-text password to connect to the database"
        echo "        * [db_engine_type] the SQL engine to use ('mysql' or 'postgresql')."
        echo "                           Default is 'mysql'."
        echo "        * [db_name] ------ the name of the database itself."
        echo "                           Default is 're2o'."
        echo "        * [db_username] -- the username to connect to the database."
        echo "                           Default is 're2o'."
        echo "  * {reset-ldap} ---- Erase the previous local LDAP and setup a new empty one"
        echo "      Parameters:"
        echo "        * <ldap_password> the clear-text password for the admin user of the"
        echo "                          LDAP"
        echo "        * <local_domain>  the domain extension to use for the LDAP structure"
        echo "                          in LDAP notation"
        echo ""
    else
790
        subcmd="$1"
791 792 793

        case "$subcmd" in

794 795 796 797
        setup )
           interactive_guide
           ;;

798 799 800
        update )
            install_requirements
            update_django
801 802 803 804 805
            copy_templates_files
            ;;

        copy-templates-files )
            copy_templates_files
806
            ;;
807 808 809

        update-django )
            update_django
810
            ;;
811 812 813

        update-packages )
            install_requirements
814
            ;;
815 816 817

        update-settings )
            interactive_update_settings
818
            ;;
819 820

        reset-db )
821
            if [ ! -z "$2" ]; then
822
                db_password="$2"
823
                case "$3" in
824 825
                mysql )
                    db_engine_type=1;;
826
                postresql )
827
                    db_engine_type=2;;
828
                * )
829
                    db_engine_type=1;;
830
                esac
831 832 833 834 835 836 837 838 839 840 841
                if [ ! -z "$4" ]; then
                    db_name="$4"
                else
                    db_name="re2o"
                fi
                if [ ! -z "$5" ]; then
                    db_username="$5"
                else
                    db_username="re2o"
                fi
                install_database "$db_engine_type" 1 "$db_name" "$db_username" "$db_password"
842
		update-django
843
            else
844
                echo "Invalid arguments !"
845 846
                echo "Usage: install_re2o setup-db <db_password> [<db_engine_type>] [<db_name>] [<db_username>]"
		echo "See 'install_re2o help' for further help"
847
            fi
848
            ;;
849 850 851

        reset-ldap )
            if [ ! -z "$2" ] && [ ! -z "$3" ]; then
852 853 854
                ldap_password="$2"
                local_domain="$3"
                install_ldap 1 "$ldap_password" "$local_domain"
855 856
            else
                echo "Invalid arguments !"
857 858
                echo "Usage: install_re2o setup-ldap <ldap_password> <local_domain>"
		echo "See 'install_re2o help' for further help"
859
            fi
860
            ;;
861 862

        * )
863 864
            echo "Unknown subcommand: $subcmd"
            echo "Use 'install_re2o help' to display some help"
865
            ;;
866 867

        esac
868
    fi
869 870
}

871
main_function "$@"