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/upgrade

195 lines
6.8 KiB
Text
Raw Normal View History

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2016-12-14 16:02:43 +01:00
source _common.sh
2016-12-14 16:02:43 +01:00
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
app=$YNH_APP_INSTANCE_NAME
2021-07-11 16:47:21 +02:00
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
2021-07-11 15:46:51 +02:00
phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
2021-07-11 16:47:21 +02:00
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
2016-12-14 16:02:43 +01:00
2018-05-22 20:01:55 +02:00
#=================================================
# CHECK VERSION
#=================================================
2018-07-13 17:37:28 +02:00
upgrade_type=$(ynh_check_app_version_changed)
2018-05-22 20:01:55 +02:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
2018-11-21 16:19:57 +01:00
if [ -z "$final_path" ]; then # Si final_path n'est pas renseigné dans app setting
2018-05-22 20:01:55 +02:00
final_path=/var/www/$app
ynh_app_setting_set $app final_path $final_path
fi
2018-11-21 16:19:57 +01:00
if [ -z "$db_name" ]; then # Si db_name n'est pas renseigné dans app setting
2018-05-22 20:01:55 +02:00
db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set $app db_name $db_name
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2018-05-22 20:01:55 +02:00
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
2018-05-22 20:01:55 +02:00
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
2018-05-22 20:01:55 +02:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
2018-05-22 20:01:55 +02:00
#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_ON
#=================================================
2021-07-11 16:47:21 +02:00
# CREATE DEDICATED USER
#=================================================
2021-07-11 16:50:32 +02:00
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
2021-07-11 16:47:21 +02:00
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir="$final_path"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2015-12-18 14:11:35 +01:00
2018-07-13 17:37:28 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2021-07-11 16:47:21 +02:00
ynh_setup_source "$final_path"
2018-07-13 17:37:28 +02:00
# Delete the install directory.
2018-11-21 16:19:57 +01:00
# Keep it for the manual upgrade process...
# ynh_secure_remove "$final_path/install"
2018-07-13 17:37:28 +02:00
fi
2018-05-22 20:01:55 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2021-07-11 16:47:21 +02:00
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1
2015-12-18 14:11:35 +01:00
2021-07-11 16:47:21 +02:00
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
2021-07-11 16:47:21 +02:00
ynh_script_progression --message="Upgrading PHP-FPM configuration..." --weight=1
2021-07-11 16:47:21 +02:00
# Create a dedicated PHP-FPM config
ynh_add_fpm_config
#=================================================
# SPECIFIC UPGRADE
#=================================================
2018-05-22 20:01:55 +02:00
# UPDATE TP.CONFIG.PHP FILE
#=================================================
2018-05-22 20:01:55 +02:00
# 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
#=================================================
2018-05-22 20:01:55 +02:00
echo "0 0 * * 0 $app cd $final_path/backups && php script.backup.php" > /etc/cron.d/$app
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
2018-11-21 16:19:57 +01:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
2018-05-22 20:01:55 +02:00
then
2018-11-21 16:19:57 +01:00
# 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
2021-07-11 16:47:21 +02:00
#ynh_mysql_execute_as_root "ALTER TABLE teampass_misc ADD id INT(12) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id);" $app >&2
2018-11-21 16:19:57 +01:00
# Upgrade to 2.1.24.4
2021-07-11 16:47:21 +02:00
#ynh_mysql_execute_as_root "ALTER TABLE teampass_items CHANGE pw_len pw_len INT(5) NOT NULL DEFAULT '0';" $app >&2
2018-11-21 16:19:57 +01:00
# Upgrade to 2.1.25.2
2021-07-11 16:47:21 +02:00
#ynh_mysql_execute_as_root "INSERT INTO teampass_misc (id, type, intitule, valeur) VALUES (NULL, 'admin', 'encryption_protocol', 'ctr');" $app >&2
2018-11-21 16:19:57 +01:00
# 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
2018-05-22 20:01:55 +02:00
fi
2018-11-21 16:19:57 +01:00
#=================================================
# UPDATE SETTINGS.PHP
#=================================================
2021-07-11 16:37:26 +02:00
path_sk_file="/etc/$app/"
ynh_add_config --template="../conf/settings.php" --destination="$final_path/includes/config/settings.php"
#=================================================
# GENERIC FINALISATION
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
2015-12-18 14:11:35 +01:00
2016-12-14 16:02:43 +01:00
# Les fichiers appartiennent à root
2018-05-22 20:01:55 +02:00
chown -R root: $final_path
2018-11-21 16:19:57 +01:00
# 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
2018-05-22 20:01:55 +02:00
chmod 750 $final_path/backups
#=================================================
# RELOAD NGINX
#=================================================
2021-07-11 16:50:32 +02:00
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2021-07-11 16:50:32 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2018-05-22 20:01:55 +02:00
#=================================================
# DEACTIVE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_OFF
2021-07-11 16:50:32 +02:00
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last