1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/ihatemoney_ynh.git synced 2024-09-03 19:26:15 +02:00
ihatemoney_ynh/scripts/upgrade

236 lines
8.4 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source ynh_send_readme_to_admin__2
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
#REMOVEME? ynh_script_progression --message="Loading installation settings..." --weight=1
#REMOVEME? app=$YNH_APP_INSTANCE_NAME
#REMOVEME? domain=$(ynh_app_setting_get --app=$app --key=domain)
#REMOVEME? path=$(ynh_app_setting_get --app=$app --key=path)
#REMOVEME? #REMOVEME? install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
#REMOVEME? db_name=$(ynh_app_setting_get --app=$app --key=db_name)
#REMOVEME? db_user=$db_name
#REMOVEME? db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
#REMOVEME? secret_key=$(ynh_app_setting_get --app=$app --key=secret_key)
#REMOVEME? hashed_password=$(ynh_app_setting_get --app=$app --key=hashed_password)
#=================================================
# CHECK VERSION
#=================================================
ynh_script_progression --message="Checking version..."
upgrade_type=$(ynh_check_app_version_changed)
if ynh_compare_current_package_version --comparison le --version "4.1.5~ynh2"; then
upgrade_from_opt=true
else
upgrade_from_opt=false
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
#REMOVEME? ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=3
# Backup the current version of the app
#REMOVEME? ynh_backup_before_upgrade
#REMOVEME? ynh_clean_setup () {
ynh_clean_check_starting
# Restore it if the upgrade fails
#REMOVEME? ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
#REMOVEME? ynh_abort_if_errors
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
if [[ "$upgrade_from_opt" == "false" ]]; then
ynh_script_progression --message="Stopping a systemd service..." --weight=1
ynh_systemd_action --service_name=$app --action="stop"
fi
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
# Cleaning legacy permissions
#REMOVEME? if ynh_legacy_permissions_exists; then
#REMOVEME? ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
# MIGRATION: Remove old code (from pre-4.1.5 versions, not using venv)
if [[ "$upgrade_from_opt" == "true" ]]; then
# Remove legacy install dir
ynh_secure_remove /opt/yunohost/ihatemoney
# Remove legacy Supervisor config
rm -f /etc/supervisor/conf.d/ihatemoney.conf
if [ -e /etc/ihatemoney/settings.py ]; then
# Strip out the no longer used part of the settings
python3 -c "d = open('/etc/ihatemoney/settings.py').read().replace('try:\n from settings import *\nexcept ImportError:\n pass\n', ''); open('/etc/ihatemoney/settings.py', 'w').write(d)"
# Rename
mv /etc/ihatemoney/settings.py "/etc/ihatemoney/ihatemoney.cfg"
fi
for old_file in "/etc/ihatemoney/ihatemoney.cfg" "/etc/$app/gunicorn.conf.py"; do
ynh_backup_if_checksum_is_different --file="$old_file"
ynh_delete_file_checksum --file="$old_file"
done
#REMOVEME?
install_dir=/var/www/$app
#REMOVEME? ynh_app_setting_set --app=$app --key=install_dir --value=$install_dir
db_name=ihatemoney
db_user=$db_name
#REMOVEME? ynh_app_setting_set --app=$app --key=db_name --value=$db_name
fi
# If secret_key doesn't exist, create it
if [ -z "$secret_key" ]; then
secret_key=$(ynh_string_random --length=32)
ynh_app_setting_set --app=$app --key=secret_key --value=$secret_key
fi
# If hashed_password doesn't exist, create it
if [ -z "$hashed_password" ] && [ test -f "${install_dir}/venv/bin/activate" ]; then
password=$(ynh_string_random --length=16)
#run source in a 'sub shell'
(
set +o nounset
source "${install_dir}/venv/bin/activate"
set -o nounset
python3 ../conf/hash_generator.py $password > ${install_dir}/key.txt
)
hashed_password=$(cat $install_dir/key.txt)
#REMOVEME? ynh_secure_remove --file="$install_dir/key.txt"
#REMOVEME? ynh_app_setting_set --app=$app --key=hashed_password --value=$hashed_password
ynh_send_readme_to_admin --app_message="../conf/upgrade.msg" --type="upgrade"
fi
if [ ! -d "/var/log/$app" ]; then
# Configure log directory
mkdir -p "/var/log/$app"
chown -R $app:$app "/var/log/$app"
fi
#=================================================
# CREATE DEDICATED USER
#=================================================
#REMOVEME? ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
# Create a dedicated user (if not existing)
#REMOVEME? ynh_system_user_create --username=$app --home_dir="$install_dir"
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
#REMOVEME? ynh_script_progression --message="Upgrading dependencies..." --weight=1
#REMOVEME? ynh_install_app_dependencies "${pkg_dependencies[@]}"
#=================================================
# SPECIFIC UPGRADE
#=================================================
# BUILD VENV
#=================================================
ynh_script_progression --message="Building venv..." --weight=6
#REMOVEME? ynh_secure_remove --file="$install_dir/venv"
__ynh_python_venv_setup --venv_dir="$install_dir/venv" --packages "${pip_dependencies[*]}"
python_venv_site_packages=$(__ynh_python_venv_get_site_packages_dir -d "$install_dir/venv")
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1
# Create a dedicated NGINX config
## Needs $python_venv_site_packages
ynh_add_nginx_config
#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a configuration file..."
ynh_add_config --template="../conf/gunicorn.conf.py" --destination="$install_dir/gunicorn.conf.py"
chmod 640 "$install_dir/gunicorn.conf.py"
chown $app:$app "$install_dir/gunicorn.conf.py"
# Allows to comment some config lines if not using sub path
sub_path_only="$(if [[ "$path" == "/" ]]; then echo '# ' ; else echo ''; fi)"
ynh_add_config --template="../conf/ihatemoney.cfg" --destination="$install_dir/ihatemoney.cfg"
chmod 640 "$install_dir/ihatemoney.cfg"
chown $app:$app "$install_dir/ihatemoney.cfg"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
yunohost service add $app --description="$app daemon for IHateMoney" --log=systemd
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
ynh_systemd_action --service_name=$app --action="start" --line_match="Listening at"
# line_match isn't enough because ihatemoney may stop if database upgrades
for _ in {1..20}; do
test -S /tmp/budget.gunicorn_$app.sock && break
sleep 1
done
#=================================================
# RELOAD NGINX
#=================================================
#REMOVEME? ynh_script_progression --message="Reloading NGINX web server..." --weight=1
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last