mirror of
https://github.com/YunoHost-Apps/teampass_ynh.git
synced 2024-09-03 20:26:37 +02:00
196 lines
7 KiB
Bash
196 lines
7 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC STARTING
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# LOAD SETTINGS
|
|
#=================================================
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
domain=$(ynh_app_setting_get $app domain)
|
|
path_url=$(ynh_app_setting_get $app path)
|
|
final_path=$(ynh_app_setting_get $app final_path)
|
|
|
|
#=================================================
|
|
# CHECK VERSION
|
|
#=================================================
|
|
|
|
upgrade_type=$(ynh_check_app_version_changed)
|
|
|
|
#=================================================
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
#=================================================
|
|
|
|
if [ -z "$final_path" ]; then # Si final_path n'est pas renseigné dans app setting
|
|
final_path=/var/www/$app
|
|
ynh_app_setting_set $app final_path $final_path
|
|
fi
|
|
|
|
if [ -z "$db_name" ]; then # Si db_name n'est pas renseigné dans app setting
|
|
db_name=$(ynh_sanitize_dbid $app)
|
|
ynh_app_setting_set $app db_name $db_name
|
|
fi
|
|
|
|
#=================================================
|
|
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
|
|
#=================================================
|
|
|
|
# Backup the current version of the app
|
|
ynh_backup_before_upgrade
|
|
ynh_clean_setup () {
|
|
# restore it if the upgrade fails
|
|
ynh_restore_upgradebackup
|
|
}
|
|
# Exit if an error occurs during the execution of the script
|
|
ynh_abort_if_errors
|
|
|
|
#=================================================
|
|
# CHECK THE PATH
|
|
#=================================================
|
|
|
|
path_url=$(ynh_normalize_url_path $path_url) # Vérifie et corrige la syntaxe du path.
|
|
|
|
#=================================================
|
|
# ACTIVATE MAINTENANCE MODE
|
|
#=================================================
|
|
|
|
ynh_maintenance_mode_ON
|
|
|
|
#=================================================
|
|
# STANDARD UPGRADE STEPS
|
|
#=================================================
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
#=================================================
|
|
|
|
if [ "$upgrade_type" == "UPGRADE_APP" ]
|
|
then
|
|
ynh_setup_source "$final_path" # Télécharge la source, décompresse et copie dans $final_path
|
|
|
|
# Delete the install directory.
|
|
# Keep it for the manual upgrade process...
|
|
# ynh_secure_remove "$final_path/install"
|
|
fi
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
|
|
ynh_add_nginx_config
|
|
|
|
#=================================================
|
|
# CREATE DEDICATED USER
|
|
#=================================================
|
|
|
|
ynh_system_user_create $app # Create the dedicated user, if not exist
|
|
|
|
#=================================================
|
|
# PHP-FPM CONFIGURATION
|
|
#=================================================
|
|
|
|
ynh_add_fpm_config # Créer le fichier de configuration du pool php-fpm et le configure.
|
|
|
|
#=================================================
|
|
# SPECIFIC UPGRADE
|
|
#=================================================
|
|
# UPDATE TP.CONFIG.PHP FILE
|
|
#=================================================
|
|
|
|
# The file tp.config.php is a dump of the admin part of the database.
|
|
tp_config_file="$final_path/includes/config/tp.config.php"
|
|
|
|
echo "<?php
|
|
global \$SETTINGS;
|
|
\$SETTINGS = array (" > $tp_config_file
|
|
|
|
while read settings
|
|
do
|
|
echo -n " '$(echo $settings | awk '{ print $1 }')'" >> $tp_config_file
|
|
echo " => '$(echo $settings | cut -d' ' -f2-)'," >> $tp_config_file
|
|
done <<< "$(ynh_mysql_execute_as_root "SELECT intitule, valeur FROM teampass_misc" $app)"
|
|
echo ");" >> $tp_config_file
|
|
|
|
#=================================================
|
|
# CREATE A CRON FILE FOR AUTOMATIC BACKUP
|
|
#=================================================
|
|
|
|
echo "0 0 * * 0 $app cd $final_path/backups && php script.backup.php" > /etc/cron.d/$app
|
|
|
|
#=================================================
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
#=================================================
|
|
|
|
if [ "$upgrade_type" == "UPGRADE_APP" ]
|
|
then
|
|
# Move settings.php from old teampass version
|
|
if [ ! -e "$final_path/includes/config/settings.php" ]
|
|
then
|
|
mv "$final_path/includes/settings.php" "$final_path/includes/config/settings.php"
|
|
fi
|
|
|
|
# Create csrfp.config.php
|
|
if [ ! -e "$final_path/includes/libraries/csrfp/libs/csrfp.config.php" ]
|
|
then
|
|
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
|
|
fi
|
|
|
|
# Run database upgrades
|
|
# Upgrade to 2.1.23.4
|
|
ynh_mysql_execute_as_root "ALTER TABLE teampass_misc ADD id INT(12) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id);" $app >&2
|
|
# Upgrade to 2.1.24.4
|
|
ynh_mysql_execute_as_root "ALTER TABLE teampass_items CHANGE pw_len pw_len INT(5) NOT NULL DEFAULT '0';" $app >&2
|
|
# Upgrade to 2.1.25.2
|
|
ynh_mysql_execute_as_root "INSERT INTO teampass_misc (id, type, intitule, valeur) VALUES (NULL, 'admin', 'encryption_protocol', 'ctr');" $app >&2
|
|
# Upgrade to 2.1.27.x
|
|
ynh_mysql_execute_as_root "ALTER TABLE teampass_misc CHANGE id increment_id INT(12) NOT NULL AUTO_INCREMENT;" $app >&2
|
|
fi
|
|
|
|
#=================================================
|
|
# UPDATE SETTINGS.PHP
|
|
#=================================================
|
|
|
|
# Remplacement des variables dans le fichier settings.php
|
|
ynh_replace_string "__DBUSER__" "$db_name" ../conf/settings.php
|
|
db_pwd=$(ynh_app_setting_get $app mysqlpwd)
|
|
ynh_replace_string "__DBPWD__" "$db_pwd" ../conf/settings.php
|
|
ynh_replace_string "__FINALPATH__" "$final_path" ../conf/settings.php
|
|
path_sk_file=/etc/$app/
|
|
ynh_replace_string "__SKPATH__" "$path_sk_file" ../conf/settings.php
|
|
|
|
ynh_backup_if_checksum_is_different "$final_path/includes/config/settings.php"
|
|
cp ../conf/settings.php $final_path/includes/config/settings.php
|
|
ynh_store_file_checksum "$final_path/includes/config/settings.php" # Enregistre la somme de contrôle du fichier de config
|
|
|
|
#=================================================
|
|
# GENERIC FINALISATION
|
|
#=================================================
|
|
# SECURING FILES AND DIRECTORIES
|
|
#=================================================
|
|
|
|
# Les fichiers appartiennent à root
|
|
chown -R root: $final_path
|
|
# Sauf certains dossiers includes, install, files et upload
|
|
chown -R $app $final_path/{includes,install,files,upload}
|
|
# Restreint l'accès au dossier de backup
|
|
chmod 750 $final_path/backups
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
|
|
ynh_system_reload --service_name=nginx
|
|
|
|
#=================================================
|
|
# DEACTIVE MAINTENANCE MODE
|
|
#=================================================
|
|
|
|
ynh_maintenance_mode_OFF
|