1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/teampass_ynh.git synced 2024-09-03 20:26:37 +02:00
teampass_ynh/scripts/install
2022-07-24 15:39:53 +02:00

239 lines
9 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
ynh_clean_check_starting
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
password=$YNH_APP_ARG_PASSWORD
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=1
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..."
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=2
# Create a system user
ynh_system_user_create --username=$app --home_dir="$final_path"
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
ynh_script_progression --message="Creating a MySQL database..." --weight=2
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=7
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
# Delete the install directory.
ynh_secure_remove "$final_path/install"
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring PHP-FPM..." --weight=2
# Create a dedicated PHP-FPM config
ynh_add_fpm_config
phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..." --weight=2
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
# FILL THE DATABASE
#=================================================
ynh_script_progression --message="Filling the database..."
version=$(ynh_app_upstream_version)
bcrypt_mdp="$(php $final_path/mdphash.php $password)"
timezone="$(cat /etc/timezone)"
time="$(date +%s)"
# Remplacement des variables dans le fichier sql
if [ $(echo $LANG | cut -c1-2) == "fr" ]
then
langue=french
folders="partagés"
roles=utilisateurs
else
langue=english
folders=shared
roles=users
fi
ynh_add_config --template="../conf/populate.sql" --destination="$final_path/populate.sql"
# Enregistre les infos dans la config YunoHost
ynh_app_setting_set --app=$app --key=langue --value=$langue
# Import du fichier SQL
ynh_mysql_connect_as $db_name $db_pwd $db_name < $final_path/populate.sql
ynh_secure_remove --file="$final_path/populate.sql"
#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..."
ynh_add_config --template="../conf/settings.php" --destination="$final_path/includes/config/settings.php"
#=================================================
# CREATE TP.CONFIG.PHP FILE
#=================================================
ynh_script_progression --message="Creating tp.config.php file..."
pushd $final_path
ynh_exec_as $app php$phpversion ./maketpconfig.php
popd
#=================================================
# CREATE A SALTKEY
#=================================================
ynh_script_progression --message="Creating a saltkey..."
saltkey=$(ynh_string_random --length=32)
mkdir /etc/$app/
ynh_add_config --template="../conf/sk.php" --destination="/etc/$app/sk.php"
chown -R $app /etc/$app/
chmod 750 /etc/$app/
#=================================================
# CREATE CSRFP
#=================================================
ynh_script_progression --message="Creating a csrfp..."
cp $final_path/includes/libraries/csrfp/libs/csrfp.config.sample.php $final_path/includes/libraries/csrfp/libs/csrfp.config.php # Créer le fichier de config de csrfp
ynh_replace_string "CSRFP_TOKEN\" => \"" "&$(head -n40 /dev/urandom | tr -c -d 'a-f0-9' | head -c50)" $final_path/includes/libraries/csrfp/libs/csrfp.config.php # Renseigne un token, valide en hexadécimal
ynh_replace_string "jsUrl\" => \"" "&includes/libraries/csrfp/js/csrfprotector.js" $final_path/includes/libraries/csrfp/libs/csrfp.config.php # Renseigne l'adresse de csrfprotector.js
#=================================================
# CREATE THE USERS
#=================================================
# Not used anymore, because the current version support ldap.
# But still I keep that here, just in case...
# Ajout des utilisateurs actuels dans la base yunohost
# teampass_users=$(ldapsearch -h localhost -b ou=users,dc=yunohost,dc=org -x objectClass=mailAccount uid | grep uid: | sed 's/uid: //' | xargs)
# id=1
# for teampassuser in $teampass_users
# do
# real_password=$(ynh_string_random) # Génère un mot de passe aléatoire
# password=$(php $final_path/mdphash.php $real_password)
# mail=$(ldapsearch -h localhost -b ou=users,dc=yunohost,dc=org -x uid=$teampassuser mail | grep mail: | sed 's/mail: //' | head -n1)
# Creation de l'utilisateur
# ynh_mysql_execute_as_root "INSERT INTO teampass_users (id, login, pw, groupes_visibles, derniers, key_tempo, last_pw_change, last_pw, admin, fonction_id, groupes_interdits, last_connexion, gestionnaire, email, favourites, latest_items, personal_folder, can_create_root_folder) VALUES (NULL, '$teampassuser', '$password', '1', '', '', '', '', '0', '1', '', '', '0', '$mail', '', '', '1', '1');" $app
# Creation du repertoire personnel
# id_user=$(ynh_mysql_execute_as_root "SELECT id from teampass_users where login='$teampassuser';" $app)
# ynh_mysql_execute_as_root "INSERT INTO teampass_nested_tree (id, parent_id, title, nleft, nright, nlevel, bloquer_creation, bloquer_modification, personal_folder, renewal_period) VALUES (NULL, 0, '$id_user', 0, 0, 1, 0, 0, 1, 0);" $app
#
# ((id++))
# done
#=================================================
# CREATE A CRON FILE FOR AN AUTOMATIC BACKUP
#=================================================
ynh_script_progression --message="Creating a cron file for an automatic backup..."
echo "0 0 * * 0 $app cd $final_path/backups && php script.backup.php" > /etc/cron.d/$app
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
# Les fichiers appartiennent à root
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"
# Sauf certains dossiers includes, files et upload
chown -R $app $final_path/{includes,files,upload}
# Restreint l'accès au dossier de backup
mkdir -p $final_path/backups
chmod 750 $final_path/backups
#=================================================
# GENERIC FINALISATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last