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

212 lines
7.5 KiB
Text
Raw Normal View History

#!/bin/bash
#=================================================
2022-03-12 15:39:54 +01:00
# GENERIC START
#=================================================
# 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
#=================================================
2022-03-12 15:39:54 +01:00
ynh_script_progression --message="Loading installation 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:51:51 +02:00
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
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
#=================================================
2022-03-12 15:39:54 +01:00
ynh_script_progression --message="Checking version..."
2018-05-22 20:01:55 +02:00
2018-07-13 17:37:28 +02:00
upgrade_type=$(ynh_check_app_version_changed)
2018-05-22 20:01:55 +02:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2022-03-12 15:39:54 +01:00
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."
2018-05-22 20:01:55 +02:00
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
2022-03-12 15:39:54 +01:00
# Restore it if the upgrade fails
2018-05-22 20:01:55 +02:00
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
2022-03-12 15:39:54 +01:00
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# 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
# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
#=================================================
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
2022-03-12 15:39:54 +01:00
ynh_script_progression --message="Upgrading source files..."
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$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
2021-07-11 17:31:18 +02:00
#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"
#=================================================
# 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
2022-03-12 15:39:54 +01:00
#=================================================
# GENERIC FINALISATION
#=================================================
# 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