2016-11-26 16:25:18 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-06-03 18:13:29 +02:00
|
|
|
#=================================================
|
2021-05-16 19:41:11 +02:00
|
|
|
# GENERIC START
|
2017-06-03 18:13:29 +02:00
|
|
|
#=================================================
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
#=================================================
|
2016-11-26 16:25:18 +01:00
|
|
|
|
2017-06-03 18:13:29 +02:00
|
|
|
source _common.sh
|
|
|
|
source /usr/share/yunohost/helpers
|
2017-01-03 18:32:00 +01:00
|
|
|
|
2017-06-03 18:13:29 +02:00
|
|
|
#=================================================
|
|
|
|
# LOAD SETTINGS
|
|
|
|
#=================================================
|
2019-05-18 15:03:55 +02:00
|
|
|
ynh_script_progression --message="Loading installation settings..." --weight=2
|
2016-11-26 16:25:18 +01:00
|
|
|
|
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)
|
2022-06-24 05:33:16 +02:00
|
|
|
language=$(ynh_app_setting_get --app=$app --key=language)
|
2019-05-18 15:03:55 +02:00
|
|
|
admin=$(ynh_app_setting_get --app=$app --key=admin)
|
|
|
|
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
|
|
|
|
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
|
2021-06-16 12:38:41 +02:00
|
|
|
db_user=$db_name
|
2021-02-18 19:52:56 +01:00
|
|
|
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
|
2022-07-30 14:04:46 +02:00
|
|
|
admin_pwd=$(ynh_app_setting_get --app=$app --key=admin_pwd)
|
|
|
|
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
|
2019-05-18 15:03:55 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CHECK VERSION
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
upgrade_type=$(ynh_check_app_version_changed)
|
2017-09-22 21:21:52 +02:00
|
|
|
|
2021-05-16 19:41:11 +02:00
|
|
|
#=================================================
|
|
|
|
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=6
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STANDARD UPGRADE STEPS
|
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
|
|
|
|
2022-06-24 05:33:16 +02:00
|
|
|
# Cleaning legacy permissions
|
|
|
|
if ynh_legacy_permissions_exists; then
|
|
|
|
ynh_legacy_permissions_delete_all
|
|
|
|
fi
|
|
|
|
|
2022-07-29 23:56:30 +02:00
|
|
|
# If datadir doesn't exist, create it
|
|
|
|
if [ -z "$datadir" ]; then
|
|
|
|
datadir=/home/yunohost.app/$app
|
|
|
|
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
|
|
|
|
fi
|
|
|
|
|
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
|
2017-06-03 18:13:29 +02:00
|
|
|
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
|
2017-06-03 18:13:29 +02:00
|
|
|
fi
|
2017-09-22 21:21:52 +02:00
|
|
|
|
|
|
|
# Compatibility with previous version; password was not set
|
2017-06-03 18:13:29 +02:00
|
|
|
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
|
|
|
|
2017-06-03 18:13:29 +02:00
|
|
|
# Compute password hash with the Piwigo function
|
2017-09-11 22:00:57 +02:00
|
|
|
cp ../conf/hash_password.php $final_path
|
2017-06-03 18:13:29 +02:00
|
|
|
hashed_password=$(cd $final_path ; php hash_password.php $admin_pwd)
|
2019-02-17 19:28:37 +01:00
|
|
|
|
2017-06-03 18:13:29 +02: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"
|
2017-06-03 18:13:29 +02:00
|
|
|
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
|
2017-06-03 18:13:29 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
#=================================================
|
2021-05-16 19:41:11 +02:00
|
|
|
# CREATE DEDICATED USER
|
2017-06-03 18:13:29 +02:00
|
|
|
#=================================================
|
2022-11-06 15:28:57 +01:00
|
|
|
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
|
2017-06-03 18:13:29 +02:00
|
|
|
|
2021-05-16 19:41:11 +02:00
|
|
|
# Create a dedicated user (if not existing)
|
2022-06-24 05:33:16 +02:00
|
|
|
ynh_system_user_create --username=$app --home_dir="$final_path"
|
2017-06-03 18:13:29 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
|
|
#=================================================
|
|
|
|
|
2021-05-18 03:42:43 +02:00
|
|
|
# sets extended pattern matching options in the bash shell
|
|
|
|
shopt -s extglob
|
|
|
|
|
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
|
2021-05-16 19:41:11 +02:00
|
|
|
tmpdir="$(ynh_smart_mktemp --min_size=300)"
|
|
|
|
ynh_setup_source --dest_dir="$tmpdir"
|
2019-02-17 19:28:37 +01:00
|
|
|
|
2021-05-16 19:41:11 +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
|
2021-05-18 03:42:43 +02:00
|
|
|
|
|
|
|
# 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
|
2022-07-29 23:56:30 +02:00
|
|
|
mv $final_path/_data $datadir
|
|
|
|
ln -sd $datadir/_data $final_path/_data
|
2021-05-18 03:42:43 +02:00
|
|
|
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
|
2022-07-29 23:56:30 +02:00
|
|
|
mv $final_path/galleries $datadir
|
|
|
|
ln -sd $datadir/galleries $final_path/galleries
|
2021-05-18 03:42:43 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
ynh_secure_remove --file="$tmpdir"
|
2019-05-18 15:03:55 +02:00
|
|
|
fi
|
2016-11-26 16:25:18 +01:00
|
|
|
|
2022-07-29 23:56:30 +02:00
|
|
|
chmod 750 "$datadir"
|
|
|
|
chmod -R o-rwx "$datadir"
|
|
|
|
chown -R $app:www-data "$datadir"
|
2021-05-18 03:42:43 +02:00
|
|
|
|
|
|
|
chmod 750 "$final_path"
|
|
|
|
chmod -R o-rwx "$final_path"
|
|
|
|
chown -R $app:www-data "$final_path"
|
|
|
|
|
2019-02-17 19:28:37 +01:00
|
|
|
#=================================================
|
|
|
|
# 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
|
|
|
|
2021-02-18 19:52:56 +01:00
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
2019-02-17 19:28:37 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# PHP-FPM CONFIGURATION
|
|
|
|
#=================================================
|
2020-11-12 18:28:43 +01:00
|
|
|
ynh_script_progression --message="Upgrading PHP-FPM configuration..." --weight=3
|
2019-02-17 19:28:37 +01:00
|
|
|
|
2020-11-12 18:28:43 +01:00
|
|
|
# Create a dedicated PHP-FPM config
|
2022-06-24 05:33:16 +02:00
|
|
|
ynh_add_fpm_config
|
2021-08-26 23:39:52 +02:00
|
|
|
phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
|
2017-06-03 18:13:29 +02:00
|
|
|
|
|
|
|
#=================================================
|
2022-06-24 05:33:16 +02:00
|
|
|
# NGINX CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2
|
|
|
|
|
|
|
|
# Create a dedicated NGINX config
|
|
|
|
ynh_add_nginx_config
|
|
|
|
|
|
|
|
#=================================================
|
2019-02-17 19:28:37 +01:00
|
|
|
# SPECIFIC UPGRADE
|
2017-06-03 18:13:29 +02:00
|
|
|
#=================================================
|
2019-02-17 19:28:37 +01:00
|
|
|
# UPGRADE APPLICATION WITH CURL
|
2017-06-03 18:13:29 +02:00
|
|
|
#=================================================
|
2020-11-12 18:28:43 +01:00
|
|
|
ynh_script_progression --message="Upgrading Piwigo with cURL..." --weight=6
|
2017-06-03 18:13:29 +02:00
|
|
|
|
2021-02-18 19:52:56 +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
|
|
|
|
|
2021-02-18 19:52:56 +01:00
|
|
|
# Upgrade Piwigo via cURL
|
2019-02-17 19:28:37 +01:00
|
|
|
ynh_local_curl "/upgrade.php?language=$applanguage&now=true" "language=$applanguage" "username=$admin" "password=$admin_pwd"
|
2017-02-08 18:32:46 +01:00
|
|
|
|
2017-09-22 21:21:52 +02:00
|
|
|
#=================================================
|
|
|
|
# CONFIGURE PIWIGO
|
|
|
|
#=================================================
|
2020-11-12 18:28:43 +01:00
|
|
|
ynh_script_progression --message="Configuring Piwigo..."
|
2017-06-03 18:13:29 +02:00
|
|
|
|
|
|
|
# Change local config
|
2021-06-16 00:09:21 +02:00
|
|
|
ynh_add_config --template="../conf/config.inc.php" --destination="$final_path/local/config/config.inc.php"
|
2019-02-17 19:28:37 +01:00
|
|
|
|
2021-06-16 00:09:21 +02:00
|
|
|
# Setup database in local/config/database.inc.php
|
2021-02-18 19:52:56 +01:00
|
|
|
ynh_add_config --template="../conf/database.inc.php" --destination="$final_path/local/config/database.inc.php"
|
2019-02-17 19:28:37 +01:00
|
|
|
|
2021-06-16 00:09:21 +02:00
|
|
|
chmod 750 "$final_path"
|
|
|
|
chmod -R o-rwx "$final_path"
|
|
|
|
chown -R $app:www-data "$final_path"
|
2017-06-03 18:13:29 +02:00
|
|
|
|
2017-09-22 21:21:52 +02:00
|
|
|
#=================================================
|
2020-02-29 21:56:40 +01:00
|
|
|
# CONFIGURE LDAP PLUGIN
|
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
|
|
|
|
2020-03-28 10:17:23 +01:00
|
|
|
# Disable LDAP plugin to avoid warning messages during API call
|
|
|
|
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "DELETE FROM plugins WHERE id='Ldap_Login';"
|
|
|
|
|
2020-02-29 21:56:40 +01:00
|
|
|
# Activate the LDAP plugin using the WS API
|
|
|
|
|
|
|
|
# Login with admin account
|
|
|
|
ynh_local_curl "/ws.php?format=json" "method=pwg.session.login" "username=$admin" "password=$admin_pwd"
|
2021-08-26 23:39:52 +02:00
|
|
|
# Get session token
|
2020-02-29 21:56:40 +01:00
|
|
|
status=$(ynh_local_curl "/ws.php?format=json" "method=pwg.session.getStatus")
|
|
|
|
pwg_token=$(jq --raw-output .result.pwg_token <<< $status)
|
|
|
|
# Activate the Ldap_Login plugin
|
|
|
|
ynh_local_curl "/ws.php?format=json" "method=pwg.plugins.performAction" "action=activate" "plugin=Ldap_Login" "pwg_token=$pwg_token"
|
2021-08-26 23:39:52 +02:00
|
|
|
# Log out
|
2020-02-29 21:56:40 +01:00
|
|
|
ynh_local_curl "/ws.php?format=json" "method=pwg.session.logout"
|
|
|
|
|
|
|
|
# Edit Ldap_Login plugin configuration
|
|
|
|
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "UPDATE piwigo_ldap_login_config SET value='ou=users,dc=yunohost,dc=org' WHERE param = 'ld_basedn';
|
|
|
|
UPDATE piwigo_ldap_login_config SET value='uid' WHERE param = 'ld_user_attr';
|
|
|
|
UPDATE piwigo_ldap_login_config SET value='' WHERE param = 'ld_binddn';
|
|
|
|
UPDATE piwigo_ldap_login_config SET value='0' WHERE param = 'allow_new_users';
|
|
|
|
UPDATE piwigo_ldap_login_config SET value='0' WHERE param = 'ld_group_user_active';"
|
|
|
|
# Remove configuration file for older plugin version
|
|
|
|
if [ -f $final_path/plugins/Ldap_Login/data.dat ] ; then
|
|
|
|
ynh_secure_remove --file=$final_path/plugins/Ldap_Login/data.dat
|
|
|
|
fi
|
2017-06-03 18:13:29 +02:00
|
|
|
|
2021-05-16 19:41:11 +02:00
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
2019-02-17 19:28:37 +01:00
|
|
|
#=================================================
|
|
|
|
# UPGRADE FAIL2BAN
|
|
|
|
#=================================================
|
2020-11-12 18:28:43 +01:00
|
|
|
ynh_script_progression --message="Reconfiguring Fail2Ban..." --weight=8
|
2019-02-17 19:28:37 +01:00
|
|
|
|
2017-06-18 18:24:10 +02: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
|
|
|
|
2017-09-11 22:00:57 +02:00
|
|
|
touch "/var/log/${app}FailedLogins.log"
|
|
|
|
chown $app: "/var/log/${app}FailedLogins.log"
|
2017-06-18 18:24:10 +02:00
|
|
|
|
2019-02-17 19:28:37 +01:00
|
|
|
ynh_add_fail2ban_config --logpath="/var/log/${app}FailedLogins.log" --failregex="ip=<HOST>" --max_retry=6
|
|
|
|
|
2017-06-03 18:13:29 +02:00
|
|
|
#=================================================
|
|
|
|
# RELOAD NGINX
|
|
|
|
#=================================================
|
2022-11-06 15:28:57 +01:00
|
|
|
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
|
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
|
|
|
|
#=================================================
|
|
|
|
|
2021-05-16 19:41:11 +02:00
|
|
|
ynh_script_progression --message="Upgrade of $app completed" --last
|