1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/piwigo_ynh.git synced 2024-09-03 20:06:03 +02:00
piwigo_ynh/scripts/upgrade

283 lines
10 KiB
Text
Raw Normal View History

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
2017-01-03 18:32:00 +01:00
#=================================================
# LOAD SETTINGS
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Loading installation settings..." --weight=2
2019-02-17 19:28:37 +01:00
app=$YNH_APP_INSTANCE_NAME
2017-01-03 18:32:00 +01:00
2019-05-18 15:03:55 +02:00
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
is_public=$(ynh_app_setting_get --app=$app --key=is_public)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
language=$(ynh_app_setting_get --app=$app --key=language)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
admin_pwd=$(ynh_app_setting_get --app=$app --key=admin_pwd)
#=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
2017-09-22 21:21:52 +02:00
2019-02-17 19:28:37 +01:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Ensuring downward compatibility..."
2017-09-22 21:21:52 +02:00
2019-02-17 19:28:37 +01:00
# If db_name doesn't exist, create it
2019-05-18 15:03:55 +02:00
if [ -z "$db_name" ]; then
db_name=$(ynh_sanitize_dbid --db_name=$app)
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
fi
2017-09-22 21:21:52 +02:00
2019-02-17 19:28:37 +01:00
# If final_path doesn't exist, create it
2019-05-18 15:03:55 +02:00
if [ -z "$final_path" ]; then
2019-02-17 19:28:37 +01:00
final_path=/var/www/$app
2019-05-18 15:03:55 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
fi
2017-09-22 21:21:52 +02:00
# Compatibility with previous version; password was not set
if [ -z "$admin_pwd" ] ; then
# Generate a new password
2019-05-18 15:03:55 +02:00
admin_pwd=$(ynh_string_random --length=24)
2019-02-17 19:28:37 +01:00
# Compute password hash with the Piwigo function
cp ../conf/hash_password.php $final_path
hashed_password=$(cd $final_path ; php hash_password.php $admin_pwd)
2019-02-17 19:28:37 +01:00
# Update password hash in database
2019-05-18 15:03:55 +02:00
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "UPDATE users SET password='$hashed_password' WHERE username='$admin';"
ynh_app_setting_set --app=$app --key=admin_pwd --value="$admin_pwd"
2019-02-17 19:28:37 +01:00
2017-09-22 21:21:52 +02:00
# Remove the temporary hash generation script
2019-05-18 15:03:55 +02:00
ynh_secure_remove --file="$final_path/hash_password.php"
fi
2017-09-22 21:21:52 +02:00
2019-02-17 19:28:37 +01:00
# Use path instead of path_url in settings.yml...
if [ -z "$path_url" ]
then
2019-05-18 15:03:55 +02:00
path_url=$(ynh_app_setting_get --app=$app --key=path_url)
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_delete --app=$app --key=path_url
fi
#=================================================
2019-02-17 19:28:37 +01:00
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=6
2019-02-17 19:28:37 +01:00
# 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
2017-09-23 10:25:23 +02:00
#=================================================
2019-02-17 19:28:37 +01:00
# CHECK THE PATH
2017-09-23 10:25:23 +02:00
#=================================================
2019-02-17 19:28:37 +01:00
# Normalize the URL path syntax
2019-05-18 15:03:55 +02:00
path_url=$(ynh_normalize_url_path --path_url=$path_url)
2017-09-23 10:25:23 +02:00
2019-02-17 19:28:37 +01:00
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2019-05-18 15:03:55 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --weight=3
# Create tmp directory and fetch app inside
tmpdir="$(ynh_smart_mktemp --min_size=300)"
ynh_setup_source --dest_dir="$tmpdir"
2019-02-17 19:28:37 +01:00
2019-05-18 15:03:55 +02:00
# Fetch needed plugins
mkdir -p $tmpdir/plugins/Ldap_Login
ynh_setup_source --dest_dir="$tmpdir/plugins/Ldap_Login" --source_id=ldap_plugin
ynh_setup_source --dest_dir="$tmpdir/plugins" --source_id=log_failed_logins_plugin
fi
2019-02-17 19:28:37 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Upgrading nginx web server configuration..." --weight=2
2019-02-17 19:28:37 +01:00
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Upgrading dependencies..." --weight=6
2019-02-17 19:28:37 +01:00
ynh_install_app_dependencies "$pkg_dependencies"
#=================================================
# CREATE DEDICATED USER
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Making sure dedicated system user exists..."
2019-02-17 19:28:37 +01:00
# Create a dedicated user (if not existing)
2019-05-18 15:03:55 +02:00
ynh_system_user_create --username=$app
2019-02-17 19:28:37 +01:00
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Upgrading php-fpm configuration..." --weight=3
2019-02-17 19:28:37 +01:00
# Create a dedicated php-fpm config
ynh_add_fpm_config
#=================================================
2019-02-17 19:28:37 +01:00
# SPECIFIC UPGRADE
#=================================================
# COPY FILES TO $FINAL_PATH
#=================================================
2019-02-17 19:28:37 +01:00
# sets extended pattern matching options in the bash shell
shopt -s extglob
datapath=/home/yunohost.app/$app
# Install files and set permissions
cp -a $tmpdir/!(upload|_data|galleries) $final_path
# Backward compatibility:
# If the _data subdirectory wasn't already moved to /home/yunohost.app/$app,
# then move it there
if [ ! -h $final_path/_data ] ; then
mv $final_path/_data $datapath
ln -sd $datapath/_data $final_path/_data
fi
# Backward compatibility:
# If the galleries subdirectory wasn't already moved to /home/yunohost.app/$app,
# then move it there
if [ ! -h $final_path/galleries ] ; then
mv $final_path/galleries $datapath
ln -sd $datapath/galleries $final_path/galleries
fi
chown -R $app: $final_path
chown -R $app: $datapath
chmod 755 -R $final_path/_data
2019-05-18 15:03:55 +02:00
ynh_secure_remove --file="$tmpdir"
2019-02-17 19:28:37 +01:00
#=================================================
2019-02-17 19:28:37 +01:00
# UPGRADE APPLICATION WITH CURL
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Upgrading piwigo with Curl..." --weight=6
2019-05-18 15:03:55 +02:00
ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
2019-02-17 19:28:37 +01:00
# Reload SSOwat config
yunohost app ssowatconf
2019-02-17 19:28:37 +01:00
# Reload Nginx
2019-05-18 15:03:55 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2019-02-17 19:28:37 +01:00
if [ "$language" = "fr" ] ; then
applanguage="fr_FR"
else
applanguage="en_UK"
fi
# Upgrade piwigo via curl
ynh_local_curl "/upgrade.php?language=$applanguage&now=true" "language=$applanguage" "username=$admin" "password=$admin_pwd"
2017-09-22 21:21:52 +02:00
#=================================================
# CONFIGURE PIWIGO
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Configuring piwigo..."
# Make a backup of the original config file if modified
2019-05-18 15:03:55 +02:00
ynh_backup_if_checksum_is_different --file="$final_path/local/config/config.inc.php"
2019-02-17 19:28:37 +01:00
# Change local config
cp ../conf/config.inc.php $final_path/local/config/
2019-02-17 19:28:37 +01:00
# Calculate and store the config file checksum
2019-05-18 15:03:55 +02:00
ynh_store_file_checksum --file="$final_path/local/config/config.inc.php"
# Make a backup of the original database config file if modified
2019-05-18 15:03:55 +02:00
ynh_backup_if_checksum_is_different --file="$final_path/local/config/database.inc.php"
2019-02-17 19:28:37 +01:00
# Setup database in local/config/database.inc.php
2019-05-18 15:03:55 +02:00
ynh_replace_string --match_string="__DBTOCHANGE__" --replace_string="$db_name" --target_file=../conf/database.inc.php
ynh_replace_string --match_string="__USERTOCHANGE__" --replace_string="$db_name" --target_file=../conf/database.inc.php
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
ynh_replace_string --match_string="__PASSTOCHANGE__" --replace_string="$db_pwd" --target_file=../conf/database.inc.php
2019-02-17 19:28:37 +01:00
cp ../conf/database.inc.php $final_path/local/config/database.inc.php
2019-02-17 19:28:37 +01:00
# Calculate and store the database config file checksum
2019-05-18 15:03:55 +02:00
ynh_store_file_checksum --file="$final_path/local/config/database.inc.php"
2017-09-22 21:21:52 +02:00
#=================================================
2019-02-17 19:28:37 +01:00
# ADD LDAP PLUGINS
2017-09-22 21:21:52 +02:00
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Configuring LDAP plugin..."
2017-09-22 21:21:52 +02:00
2017-11-19 19:42:54 +01:00
# Configure and activate LDAP plugin
2019-05-18 15:03:55 +02:00
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "UPDATE plugins SET state='active' WHERE id='Ldap_Login';"
2017-11-19 19:42:54 +01:00
cp ../conf/data.dat $final_path/plugins/Ldap_Login
2019-02-17 19:28:37 +01:00
#=================================================
# UPGRADE FAIL2BAN
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Reconfiguring fail2ban..." --weight=8
2019-02-17 19:28:37 +01:00
# Configure and activate log_failed_logins plugin
2019-05-18 15:03:55 +02:00
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "INSERT INTO plugins (id,state,version) VALUES ('log_failed_logins','active','1.2');" 2>&1 > /dev/null || ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "UPDATE plugins SET state='active' WHERE id='log_failed_logins';"
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "INSERT INTO config (param, value) VALUES ('logFailedLoginsFilename','/var/log/${app}FailedLogins.log');" 2>&1 > /dev/null || ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "UPDATE config SET value='/var/log/${app}FailedLogins.log' WHERE param='logFailedLoginsFilename';"
2019-02-17 19:28:37 +01:00
touch "/var/log/${app}FailedLogins.log"
chown $app: "/var/log/${app}FailedLogins.log"
2019-02-17 19:28:37 +01:00
ynh_add_fail2ban_config --logpath="/var/log/${app}FailedLogins.log" --failregex="ip=<HOST>" --max_retry=6
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP SSOWAT
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Upgrading SSOwat configuration..."
# Protect URIs if private
2019-02-17 19:28:37 +01:00
if [ $is_public -eq 0 ]
then
2019-05-18 15:03:55 +02:00
ynh_app_setting_delete --app=$app --key=unprotected_uris
ynh_app_setting_set --app=$app --key=protected_uris --value="/"
fi
#=================================================
# RELOAD NGINX
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Reloading nginx web server..."
2019-02-17 19:28:37 +01:00
2019-05-18 15:03:55 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2019-02-17 19:28:37 +01:00
2019-02-17 20:58:31 +01:00
#=================================================
# END OF SCRIPT
#=================================================
2019-05-18 15:03:55 +02:00
ynh_script_progression --message="Upgrade of $app completed" --last