1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/lufi_ynh.git synced 2024-09-03 19:36:28 +02:00
lufi_ynh/scripts/upgrade
2020-05-29 22:49:32 +02:00

278 lines
10 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info --message="Loading installation settings..."
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
is_public=$(ynh_app_setting_get --app=$app --key=is_public)
port=$(ynh_app_setting_get --app=$app --key=port)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
secret=$(ynh_app_setting_get --app=$app --key=secret)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
max_file_size=$(ynh_app_setting_get --app=$app --key=max_file_size)
#=================================================
# CHECK VERSION
#=================================================
ynh_print_info --message="Checking version..."
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_print_info --message="Ensuring downward compatibility..."
# Fix is_public as a boolean value
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set --app=$app --key=is_public --value=1
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set --app=$app --key=is_public --value=0
is_public=0
fi
if [ "${#final_path}" -eq 0 ]
then # Si final_path n'est pas renseigné dans la config yunohost, cas d'ancien script, code final_path en dur
final_path=/var/www/$app
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
fi
if [ -z "$db_pwd" ]; then
# Need to install new dependencies before creating the new database
need_migration_from_sqlite_to_psql=1
else
need_migration_from_sqlite_to_psql=0
fi
if [ -z "$max_file_size" ]; then
max_file_size=100 # 100 Mo
ynh_app_setting_set --app=$app --key=max_file_size --value=$max_file_size
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_print_info --message="Backing up the app before upgrading (may take a while)..."
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
ynh_clean_check_starting
# 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
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_print_info --message="Stopping a systemd service..."
ynh_systemd_action --service_name=$app --action="stop" --log_path="$final_path/log/production.log"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_print_info --message="Upgrading source files..."
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
fi
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_print_info --message="Upgrading nginx web server configuration..."
# Create a dedicated nginx config
ynh_add_nginx_config max_file_size
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_print_info --message="Upgrading dependencies..."
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_print_info --message="Making sure dedicated system user exists..."
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app
#=================================================
# SPECIFIC UPGRADE
#=================================================
# CREATE A POSTGRESQL DATABASE IF NEEDED
#=================================================
if [ $need_migration_from_sqlite_to_psql -eq 1 ]; then
ynh_print_info --message="Creating a PostgreSQL database..."
# Create postgresql database
ynh_psql_test_if_first_run
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
# Initialize database and store postgres password for upgrade
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd) # Password created in ynh_psql_setup_db function
fi
#=================================================
# SETUP LUFI
#=================================================
ynh_print_info --message="Configuring lufi..."
config=${final_path}/lufi.conf
ynh_backup_if_checksum_is_different --file="$config"
cp ../conf/lufi.conf.template "$config"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$config"
ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$config"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$config"
ynh_replace_string --match_string="__DB_NAME__" --replace_string="$db_name" --target_file="$config"
ynh_replace_string --match_string="__DB_USER__" --replace_string="$db_user" --target_file="$config"
ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$config"
ynh_replace_string --match_string="__MAX_FILE_SIZE__" --replace_string="$max_file_size" --target_file="$config"
if [ $max_file_size -eq 0 ]; then # Comment the limitation line if no limit
ynh_replace_string --match_string="max_file_size" --replace_string="#max_file_size" --target_file="$config"
fi
ynh_replace_string --match_string="__SECRET__" --replace_string="$secret" --target_file="$config"
if [ $is_public -eq 0 ];
then
ynh_replace_string --match_string="__IS_PUBLIC__" --replace_string="" --target_file="$config"
else
ynh_replace_string --match_string="__IS_PUBLIC__" --replace_string="#" --target_file="$config"
fi
ynh_store_file_checksum --file="$config"
#=================================================
# INSTALL LUFI'S DEPENDENCIES VIA CARTON
#=================================================
ynh_print_info --message="Installing lufi..."
pushd $final_path
# Migrate from SQLite to PostgreSQL
if [ $need_migration_from_sqlite_to_psql -eq 1 ]; then
# Preinstall with sqlite
carton install --deployment --without=mysql --without=htpasswd --without=test
# if /var/log/$app/production.log is a symbolic link, then move it to $final_path/log/production.log
if [ ! -L "/var/log/$app/production.log" ]
then
mv "/var/log/$app/production.log" "$final_path/log/production.log"
chown -R $app: "$final_path/log/production.log"
fi
# Restart with news parameters
ynh_systemd_action --service_name=$app --action="restart" --log_path="$final_path/log/production.log" --line_match="Creating process id file"
carton exec script/lufi sqliteToOtherDB
fi
carton install --deployment --without=sqlite --without=mysql --without=htpasswd --without=test
popd
#=================================================
# SETUP CRON
#=================================================
ynh_print_info --message="Setuping cron..."
cp ../conf/cron_lufi /etc/cron.d/$app
ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path/" --target_file="/etc/cron.d/$app"
ynh_replace_string --match_string="__USER__" --replace_string="$app" --target_file="/etc/cron.d/$app"
chmod +x $final_path/script/lufi
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add $app --description "lufi service" --log "$final_path/log/production.log"
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_print_info --message="Upgrading logrotate configuration..."
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_print_info --message="Upgrading systemd configuration..."
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
ynh_print_info --message="Securing files and directories..."
# Set permissions on app files
chown -R $app: $final_path
#=================================================
# SETUP SSOWAT
#=================================================
ynh_print_info --message="Upgrading SSOwat configuration..."
ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
if [ $is_public -eq 0 ]
then
if [ "$path_url" == "/" ]; then
# If the path is /, clear it to prevent any error with the regex.
path_url=""
fi
# Modify the domain to be used in a regex
domain_regex=$(echo "$domain" | sed 's@-@.@g')
ynh_app_setting_set --app=$app --key=protected_regex --value="$domain_regex$path_url/stats$","$domain_regex$path_url/manifest.webapp$","$domain_regex$path_url/$","$domain_regex$path_url/d/.*$","$domain_regex$path_url/m/.*$"
fi
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_print_info --message="Starting a systemd service..."
ynh_systemd_action --service_name=$app --action="restart" --log_path="$final_path/log/production.log" --line_match="Creating process id file"
#=================================================
# RELOAD NGINX
#=================================================
ynh_print_info --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_print_info --message="Upgrade of $app completed"