1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/lutim_ynh.git synced 2024-09-03 19:36:24 +02:00
lutim_ynh/scripts/upgrade
2019-01-31 11:53:22 +01:00

316 lines
11 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
# Load common variables for all scripts.
source _variables
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Load settings" --weight=4
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)
always_encrypt=$(ynh_app_setting_get $app always_encrypt)
final_path=$(ynh_app_setting_get $app final_path)
secret=$(ynh_app_setting_get $app secret)
overwrite_settings=$(ynh_app_setting_get $app overwrite_settings)
overwrite_nginx=$(ynh_app_setting_get $app overwrite_nginx)
overwrite_systemd=$(ynh_app_setting_get $app overwrite_systemd)
# Optional parameters from config-panel feature
antiflood=$(ynh_app_setting_get $app antiflood)
delay=$(ynh_app_setting_get $app delay)
#=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensure downward compatibility" --weight=2
# Fix is_public as a boolean
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set $app is_public 1
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set $app is_public 0
is_public=0
fi
# if final_path isn't set, which can happens with old scripts, set final_path.
if [ "${#final_path}" -eq 0 ]
then
final_path=/var/www/$app
ynh_app_setting_set $app final_path $final_path
fi
# Fix always_encrypt as a boolean
if [ "$always_encrypt" = "Yes" ]; then
ynh_app_setting_set $app always_encrypt 1
always_encrypt=1
elif [ "$always_encrypt" = "No" ]; then
ynh_app_setting_set $app always_encrypt 0
always_encrypt=0
fi
# If overwrite_settings doesn't exist, create it
if [ -z "$overwrite_settings" ]; then
overwrite_settings=1
ynh_app_setting_set $app overwrite_settings $overwrite_settings
fi
# If overwrite_nginx doesn't exist, create it
if [ -z "$overwrite_nginx" ]; then
overwrite_nginx=1
ynh_app_setting_set $app overwrite_nginx $overwrite_nginx
fi
# If overwrite_systemd doesn't exist, create it
if [ -z "$overwrite_systemd" ]; then
overwrite_systemd=1
ynh_app_setting_set $app overwrite_systemd $overwrite_systemd
fi
# If secret doesn't exist, create it
if [ -z "$secret" ]; then
secret=$(grep "secrets *=>" "$final_path/lutim.conf" | cut -d\' -f2)
ynh_app_setting_set $app secret $secret
fi
# Close opened port
if yunohost firewall list | grep -q "\- $port$"
then
ynh_exec_quiet yunohost firewall disallow TCP $port
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backup the app before upgrading" --weight=15
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# Clean installation remaining that are not handle by the remove script.
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
#=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
path_url=$(ynh_normalize_url_path $path_url)
#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================
ynh_script_progression --message="Activate maintenance mode" --weight=2
ynh_maintenance_mode_ON
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Download, check and unpack source"
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source "$final_path"
fi
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrade dependencies" --weight=9
ynh_install_app_dependencies $app_depencencies
#=================================================
# NGINX CONFIGURATION
#=================================================
# Overwrite the nginx configuration only if it's allowed
if [ $overwrite_nginx -eq 1 ]
then
ynh_script_progression --message="Reconfigure nginx" --weight=2
ynh_add_nginx_config
fi
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Create a dedicated user"
# Create a dedicated user (if not existing)
ynh_system_user_create $app
#=================================================
# SPECIFIC UPGRADE
#=================================================
# SETUP LUTIM
#=================================================
ynh_script_progression --message="Reconfigure Lutim"
# Overwrite the settings config file only if it's allowed
if [ $overwrite_settings -eq 1 ]
then
# Configure Lutim
# Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
ynh_backup_if_checksum_is_different "$final_path/lutim.conf"
cp ../conf/lutim.conf.template "$final_path/lutim.conf"
ynh_replace_string "__DOMAIN__" "$domain" "$final_path/lutim.conf"
ynh_replace_string "__PATH__" "$path_url" "$final_path/lutim.conf"
ynh_replace_string "__PORT__" "$port" "$final_path/lutim.conf"
ynh_replace_string "__ENCRYPT__" "$always_encrypt" "$final_path/lutim.conf"
ynh_replace_string "__SECRET__" "$secret" "$final_path/lutim.conf"
# Recalculate and store the checksum of the file for the next upgrade.
ynh_store_file_checksum "$final_path/lutim.conf"
# Optional parameters from config-panel feature
if [ -n "$antiflood" ]; then
ynh_replace_string ".*anti_flood_delay *=>.*" " anti_flood_delay => $antiflood," "$final_path/lutim.conf"
# Disable anti_flood_delay if the delay is 0
if [ $antiflood = 0 ]; then
ynh_replace_string "\(anti_flood_delay *=>.*\)" "#\1" "$final_path/lutim.conf"
fi
fi
if [ -n "$delay" ]; then
ynh_replace_string ".*default_delay *=>.*" " default_delay => $delay," "$final_path/lutim.conf"
fi
fi
#=================================================
# SETUP SYSTEMD
#=================================================
# Overwrite the systemd configuration only if it's allowed
if [ $overwrite_systemd -eq 1 ]
then
ynh_script_progression --message="Reconfigure systemd" --weight=2
ynh_add_systemd_config
fi
#=================================================
# SETUP CRON
#=================================================
cp ../conf/cron_lutim /etc/cron.d/$app
ynh_replace_string "__FINALPATH__" "$final_path/" /etc/cron.d/$app
#=================================================
# UPDATE LUTIM WITH CARTON
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrade Lutim with carton" --weight=4
(cd $final_path
carton install 2>&1 | tee -a "/var/log/$app/setup_carton.log")
fi
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
# Set permissions on app files
chown -R $app: $final_path
#=================================================
# GENERIC FINALISATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Reconfigure logrotate"
ynh_use_logrotate --non-append
chown $app -R /var/log/$app
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Reconfigure SSOwat"
ynh_app_setting_set $app skipped_uris "/"
if [ $is_public -eq 0 ]
then
# If the app is private, viewing images stays publicly accessible.
# 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/stats$","$domain_regex$path_url/manifest.webapp$","$domain_regex$path_url/$","$domain_regex$path_url/d/.*$","$domain_regex$path_url/m/.*$"
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reload nginx"
ynh_systemd_action --action=reload --service_name=nginx
#=================================================
# START AND CHECK LUTIM BOOTING
#=================================================
ynh_script_progression --message="Restart Lutim" --weight=4
# Wait for lutim to be fully started
ynh_systemd_action --action=restart --line_match="Manager.*started" --log_path="/var/log/$app/production.log" --timeout="120"
#=================================================
# DEACTIVE MAINTENANCE MODE
#=================================================
ynh_script_progression --message="Disable maintenance mode" --weight=7
ynh_maintenance_mode_OFF
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
# Get main domain and buid the url of the admin panel of the app.
admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
# Build the changelog
ynh_app_changelog || true
echo "You can find a config file at $final_path/lutim.conf
You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.
If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/lutim_ynh__URL_TAG3__.
---
Changelog since your last upgrade:
$(cat changelog)" > mail_to_send
ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="root" --type="upgrade"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade completed" --last