8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00

Split install_re2o.sh into functions

This commit is contained in:
Maël Kervella 2018-05-16 19:44:11 +00:00
parent fa9a6f7173
commit 03aaba1545

View file

@ -48,8 +48,357 @@ setup_ldap() {
}
install_re2o_server() {
### Usage: install_re2o_server
install_requirements() {
### Usage: install_requirements
#
# This function will install the required packages from APT repository
# and Pypi repository. Those packages are qll required for Re2o to work
# properly.
###
echo "Setting up the required packages ..."
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 \
javascript-common \
libjs-jquery \
libjs-jquery-ui \
libjs-jquery-timepicker \
libjs-bootstrap
pip3 install django-bootstrap3 django-ldapdb==0.9.0 django-macaddress
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 ..."
engine_type=$1
local_setup=$2
db_name=$3
username=$4
password=$5
if [ $engine_type == 1 ]; then
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;"
if [ $local_setup == 1 ]; then
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"
while true; do
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
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;"
if [ $local_setup == 1 ]; then
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"
while true; do
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
fi
fi
echo "Setting up the database: Done"
}
init_django() {
### Usage: init_django
#
# This function will initialise the Django project by applying the migrations,
# creating a first user with the superuser rights and collecting the statics
###
echo "Applying Django migrations ..."
python3 manage.py migrate
echo "Applying Django migrations: Done"
echo "Creating a superuser ..."
python3 manage.py createsuperuser
echo "Creating a superuser: Done"
echo "Collecting web frontend statics ..."
python3 manage.py collectstatic --noinput
echo "Collecting web frontend statics: Done"
}
install_active_directory() {
### Usage: install_active_directory <local_setup> <password> <domain>
#
# This function will install the active directory
#
# Parameters:
# * local_setup: Should the Active Directory be installed locally ?
# 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
###
echo "Setting up the active direcory ..."
local_setup=$1
password=$2
domain=$3
if [ $local_setup == 1 ]; then
echo "Setting up local active directory ..."
setup_ldap $password $domain
echo "Setting up local active directory: Done"
else
echo "Please execute the following command on the remote LDAP server and then continue"
echo "./install_re2o.sh ldap $password $domain"
while true; do
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
fi
echo "Setting up the active directory: Done"
}
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
# * ldap_cn: The CN entry for the Active Directory admin in LDAP notation
# * ldap_tls: Should the TLS be activated to contact the Active Directory
# 1 = yes
# 2 = no
# * ldap_password: The password to use to connect to the Active Directoryy
# * ldap_hostname: The hostname for contacting the Active Directory
# * ldap_domain: The local domain for the Active Directory in LDAP notation
# * 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 ..."
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}
SETTINGS_LOCAL_FILE='re2o/settings_local.py'
SETTINGS_EXAMPLE_FILE='re2o/settings_local.example.py'
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)]))")
if [ $db_engine_type == 1 ]; then
sed -i 's/db_engine/django.db.backends.mysql/g' $SETTINGS_LOCAL_FILE
else
sed -i 's/db_engine/django.db.backends.postgresql_psycopg2/g' $SETTINGS_LOCAL_FILE
fi
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
if [ $ldap_tls == 2 ]; then
sed -i "s/'TLS': True,/# 'TLS': True,#/g" $SETTINGS_LOCAL_FILE
fi
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=org/'"$ldap_domain"'/g' $SETTINGS_LOCAL_FILE
sed -i 's/example.org/'"$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
echo "Writing of the settings_local.py file: Done"
}
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 ..."
engine_type=$1
tls=$2
url=$3
if [ $engine_type == 1 ]; then
echo "Setting up Apache2 web server ..."
apt-get -y install apache2 libapache2-mod-wsgi-py3
a2enmod ssl
a2enmod wsgi
a2enconf javascript-common
if [ $tls == 1 ]; then
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
certbot certonly --rsa-key-size 4096 --apache -d $url
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
sed -i 's|PATH|'"$(pwd)"'|g' /etc/apache2/sites-available/re2o.conf
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."
echo "Please onfirm you have acknowledged this message."
while true; do
read -p "Acknowledged (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
fi
echo "Setting up web server: Done"
}
interactive_guide() {
### Usage: interactive_guide
#
# This function will guide through the automated setup of Re2o by asking
# the user for some informations and some installation choices. It will
@ -304,260 +653,25 @@ install_re2o_server() {
clear
###############################
## Install required packages ##
###############################
echo "Setting up the required packages ..."
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 \
javascript-common \
libjs-jquery \
libjs-jquery-ui \
libjs-jquery-timepicker \
libjs-bootstrap
pip3 install django-bootstrap3 django-ldapdb==0.9.0 django-macaddress
echo "Setting up the required packages: Done"
################################
## Perform the actual actions ##
################################
install_requirements
install_database $sql_bdd_type $sql_is_local $sql_name $sql_login $sql_password
install_active_directory $ldap_is_local $ldap_password $ldap_dn
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
####################
## Setup database ##
####################
echo "Setting up the database ..."
if [ $sql_bdd_type == 1 ]; then
echo "Installing MySQL client ..."
apt-get -y install python3-mysqldb mysql-client
echo "Installing MySQL client: Done"
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;"
if [ $sql_is_local == 1 ]; then
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"
while true; do
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
fi
else
echo "Installing PostgreSQL client ..."
apt-get -y install postgresql-client python3-psycopg2
echo "Installing PostgreSQL client: Done"
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;"
if [ $sql_is_local == 1 ]; then
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"
while true; do
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
fi
fi
echo "Setting up the database: Done"
############################
## Setup active directory ##
############################
echo "Setting up the active direcory ..."
if [ $ldap_is_local == 1 ]; then
echo "Setting up local active directory ..."
setup_ldap $ldap_password $ldap_dn
echo "Setting up local active directory: Done"
else
echo "Please execute the following command on the remote LDAP server and then continue"
echo "./install_re2o.sh ldap $ldap_password $ldap_dn"
while true; do
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
fi
echo "Setting up the active directory: Done"
###################################
## Setup settings_locale.py file ##
###################################
echo "Writing of the settings_local.py 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)]))")
init_django
cp re2o/settings_local.example.py re2o/settings_local.py
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
sed -i 's/SUPER_SECRET_KEY/'"$django_secret_key"'/g' re2o/settings_local.py
sed -i 's/SUPER_SECRET_DB/'"$sql_password"'/g' re2o/settings_local.py
sed -i 's/A_SECRET_AES_KEY/'"$aes_key"'/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
if [ $ldap_tls == 2 ]; then
sed -i "s/'TLS': True,/# 'TLS': True,#/g" re2o/settings_local.py
fi
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
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
sed -i 's/URL_SERVER/'"$url_server"'/g' re2o/settings_local.py
echo "Writing of the settings_local.py file: Done"
#############################
## Apply Django migrations ##
#############################
echo "Applying Django migrations ..."
python3 manage.py migrate
echo "Applying Django migrations: Done"
######################
## Create superuser ##
######################
echo "Creating a superuser ..."
python3 manage.py createsuperuser
echo "Creating a superuser: Done"
##################################
## Collect web frontend statics ##
##################################
echo "Collecting web frontend statics ..."
python3 manage.py collectstatic --noinput
echo "Collecting web frontend statics: Done"
#######################
## Set up web server ##
#######################
echo "Setting up web server ..."
if [ $web_serveur == 1 ]; then
echo "Setting up Apache2 web server ..."
apt-get -y install apache2 libapache2-mod-wsgi-py3
a2enmod ssl
a2enmod wsgi
a2enconf javascript-common
if [ $is_tls == 1 ]; then
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
certbot certonly --rsa-key-size 4096 --apache -d $url_server
sed -i 's/LE_PATH/'"$url_server"'/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_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
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."
echo "Please onfirm you have acknowledged this message."
while true; do
read -p "Acknowledged (y/n)?" choice
case "$choice" in
y|Y ) break;;
n|N ) exit;;
* ) echo "Invalid";;
esac
done
fi
echo "Setting up web server: Done"
install_webserver $web_serveur $is_tls $url_server