1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/lstu_ynh.git synced 2024-09-03 19:36:12 +02:00
lstu_ynh/scripts/upgrade
grenagit e0b816021d
Add script progression
Add script progress tracking with `ynh_script_progression' to improve information to users
2020-05-19 15:55:21 +02:00

224 lines
7.7 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get $app domain)
path_url=$(ynh_app_setting_get $app path)
is_public=$(ynh_app_setting_get $app is_public)
port=$(ynh_app_setting_get $app port)
final_path=$(ynh_app_setting_get $app final_path)
secret=$(ynh_app_setting_get $app secret)
db_name=$(ynh_app_setting_get $app db_name)
db_user=$db_name
db_pwd=$(ynh_app_setting_get $app psqlpwd)
theme=$(ynh_app_setting_get $app theme)
hashed_password=$(ynh_app_setting_get $app hashed_password)
#=================================================
# FIX OLD THINGS
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set $app is_public 1 # Fixe is_public en booléen
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set $app is_public 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
fi
if [ -z "$db_pwd" ]; then
db_pwd=$(ynh_app_setting_get $app db_pwd) # Fix old db_pwd
if [ -z "$db_pwd" ]; then
db_name=$(ynh_sanitize_dbid "$app")
db_user=$db_name
# Initialize database and store postgres password for upgrade
ynh_psql_setup_db "$db_name" "$db_user"
ynh_app_setting_set "$app" db_name "$db_name"
db_pwd=$(ynh_app_setting_get $app psqlpwd) # Password created in ynh_psql_setup_db function
else
ynh_app_setting_delete $app db_pwd
ynh_app_setting_set $app psqlpwd $db_pwd
fi
fi
if [ -z "$theme" ]; then
theme="milligram"
ynh_app_setting_set $app theme $theme
fi
if [ -z "$hashed_password" ]; then
# Generate random password
password=$(ynh_string_random 8)
hashed_password=$(echo -n $password | sha256sum | cut -d' ' -f1)
echo "The new version of LSTU provide an admin and a stats area which required a password." > mail_to_send
echo "" >> mail_to_send
echo "This password is: $password" >> mail_to_send
ynh_send_readme_to_admin --app_message="mail_to_send" --type="upgrade"
ynh_app_setting_set $app hashed_password $hashed_password
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --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
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Upgrading source files..."
ynh_install_app_dependencies build-essential libssl-dev zlib1g-dev libpng-dev libpq-dev memcached postgresql cpanminus
ynh_setup_source "$final_path"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading nginx web server configuration..."
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..."
# Create a dedicated user (if not existing)
ynh_system_user_create $app
#=================================================
# SPECIFIC UPGRADE
#=================================================
# SETUP LSTU
#=================================================
ynh_script_progression --message="Upgrading lstu configuration..."
ynh_backup_if_checksum_is_different "$final_path/lstu.conf"
cp ../conf/lstu.conf.template "${final_path}/lstu.conf"
ynh_replace_string "__DOMAIN__" "$domain" "${final_path}/lstu.conf"
ynh_replace_string "__PATH__" "$path_url" "${final_path}/lstu.conf"
ynh_replace_string "__PORT__" "$port" "${final_path}/lstu.conf"
ynh_replace_string "__DB_NAME__" "$db_name" "${final_path}/lstu.conf"
ynh_replace_string "__DB_USER__" "$db_user" "${final_path}/lstu.conf"
ynh_replace_string "__DB_PWD__" "$db_pwd" "${final_path}/lstu.conf"
ynh_replace_string "__SELECTED_THEME__" "$theme" "${final_path}/lstu.conf"
ynh_replace_string "__PASSWORD_HASHED__" "$hashed_password" "${final_path}/lstu.conf"
ynh_replace_string "__SECRET__" "$secret" "${final_path}/lstu.conf"
if [ $is_public -eq 0 ];
then
ynh_replace_string "__IS_PUBLIC__" "" "${final_path}/lstu.conf"
else
ynh_replace_string "__IS_PUBLIC__" "#" "${final_path}/lstu.conf"
fi
ynh_store_file_checksum "${final_path}/lstu.conf"
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
chown -R www-data $final_path
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..."
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# Install lstu's dependencies via carton
#=================================================
pushd $final_path
carton install --deployment --without=sqlite --without=mysql
popd
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..."
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add $app --log "/var/log/$app.log"
#=================================================
# RESTART LSTU
#=================================================
ynh_systemd_action -n $app -a reload -l "Reloaded Shortened URLs service." -p "systemd"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Upgrading SSOwat configuration..."
ynh_app_setting_set $app unprotected_uris "/"
if [ $is_public -eq 0 ]
then
# If the app is private, only the shortened URLs are publics.
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 protected_regex "$domain_regex$path_url/login$","$domain_regex$path_url/logout$","$domain_regex$path_url/api$","$domain_regex$path_url/extensions$","$domain_regex$path_url/stats$","$domain_regex$path_url/d/.*$","$domain_regex$path_url/a$","$domain_regex$path_url/$"
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
systemctl reload nginx
yunohost app ssowatconf
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed"