mirror of
https://github.com/YunoHost-Apps/lutim_ynh.git
synced 2024-09-03 19:36:24 +02:00
08a5b22c9c
* Remove -- deployment that make upgrade to fail * Stop before removing the systemd service
348 lines
13 KiB
Bash
348 lines
13 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC STARTING
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
# Load common variables for all scripts.
|
|
source _common.sh
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# LOAD SETTINGS
|
|
#=================================================
|
|
ynh_script_progression --message="Loading installation settings..." --weight=4
|
|
|
|
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)
|
|
always_encrypt=$(ynh_app_setting_get --app=$app --key=always_encrypt)
|
|
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
|
|
secret=$(ynh_app_setting_get --app=$app --key=secret)
|
|
|
|
overwrite_settings=$(ynh_app_setting_get --app=$app --key=overwrite_settings)
|
|
overwrite_nginx=$(ynh_app_setting_get --app=$app --key=overwrite_nginx)
|
|
overwrite_systemd=$(ynh_app_setting_get --app=$app --key=overwrite_systemd)
|
|
admin_mail_html=$(ynh_app_setting_get --app=$app --key=admin_mail_html)
|
|
|
|
# Optional parameters from config-panel feature
|
|
antiflood=$(ynh_app_setting_get --app=$app --key=antiflood)
|
|
delay=$(ynh_app_setting_get --app=$app --key=delay)
|
|
|
|
#=================================================
|
|
# CHECK VERSION
|
|
#=================================================
|
|
|
|
upgrade_type=$(ynh_check_app_version_changed)
|
|
|
|
#=================================================
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
#=================================================
|
|
ynh_script_progression --message="Ensuring downward compatibility..." --weight=2
|
|
|
|
# Fix is_public as a boolean
|
|
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
|
|
|
|
skipped_uris=$(ynh_app_setting_get --app=$app --key=skipped_uris)
|
|
|
|
# Unused with the permission system
|
|
if [ ! -z "$skipped_uris" ]; then
|
|
ynh_app_setting_delete --app=$app --key=skipped_uris
|
|
fi
|
|
|
|
# Create the permission "upload images" only if it doesn't exist.
|
|
if ! ynh_permission_exists --permission="upload images"
|
|
then
|
|
# This is a fake permission without any URL.
|
|
# The purpose of this permission is only to trigger hooks post_app_add/removeaccess when it's modified.
|
|
# We can't use a real permission for now because the actual permision system doesn't support regex.
|
|
ynh_permission_create --permission="upload images" --allowed="visitors"
|
|
|
|
if [ $is_public -eq 0 ]
|
|
then
|
|
# If the app is private, viewing images stays publicly accessible.
|
|
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-m]/.*$"
|
|
|
|
# If the app is not public, then the "visitors" group doesn't have this permission
|
|
ynh_permission_update --permission="upload images" --remove="visitors"
|
|
fi
|
|
fi
|
|
|
|
# if final_path isn't set, which can happens with old scripts, set final_path.
|
|
if [ -z "$final_path" ]; then
|
|
final_path=/var/www/$app
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
|
fi
|
|
|
|
# Fix always_encrypt as a boolean
|
|
if [ "$always_encrypt" = "Yes" ]; then
|
|
ynh_app_setting_set --app=$app --key=always_encrypt --value=1
|
|
always_encrypt=1
|
|
elif [ "$always_encrypt" = "No" ]; then
|
|
ynh_app_setting_set --app=$app --key=always_encrypt --value=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=$app --key=overwrite_settings --value=$overwrite_settings
|
|
fi
|
|
|
|
# If overwrite_nginx doesn't exist, create it
|
|
if [ -z "$overwrite_nginx" ]; then
|
|
overwrite_nginx=1
|
|
ynh_app_setting_set --app=$app --key=overwrite_nginx --value=$overwrite_nginx
|
|
fi
|
|
|
|
# If overwrite_systemd doesn't exist, create it
|
|
if [ -z "$overwrite_systemd" ]; then
|
|
overwrite_systemd=1
|
|
ynh_app_setting_set --app=$app --key=overwrite_systemd --value=$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=$app --key=secret --value=$secret
|
|
fi
|
|
|
|
# Close opened port
|
|
if yunohost firewall list | grep -q "\- $port$"
|
|
then
|
|
ynh_exec_quiet yunohost firewall disallow TCP $port
|
|
fi
|
|
|
|
# Replace skipped_uris by unprotected_uris for the migration to the new permission system.
|
|
ynh_app_setting_delete --app=$app --key=skipped_uris
|
|
ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
|
|
|
|
#=================================================
|
|
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
|
|
#=================================================
|
|
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --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
|
|
|
|
#=================================================
|
|
# ACTIVATE MAINTENANCE MODE
|
|
#=================================================
|
|
ynh_script_progression --message="Activating 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="Upgrading source files..."
|
|
# Download, check integrity, uncompress and patch the source from app.src
|
|
ynh_setup_source --dest_dir="$final_path"
|
|
fi
|
|
|
|
#=================================================
|
|
# UPGRADE DEPENDENCIES
|
|
#=================================================
|
|
ynh_script_progression --message="Upgrading dependencies..." --weight=9
|
|
|
|
ynh_install_app_dependencies $pkg_depencencies
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
|
|
# Overwrite the nginx configuration only if it's allowed
|
|
if [ $overwrite_nginx -eq 1 ]
|
|
then
|
|
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2
|
|
ynh_add_nginx_config
|
|
fi
|
|
|
|
#=================================================
|
|
# CREATE DEDICATED USER
|
|
#=================================================
|
|
ynh_script_progression --message="Making sure dedicated system user exists..."
|
|
|
|
# Create a dedicated user (if not existing)
|
|
ynh_system_user_create --username=$app
|
|
|
|
#=================================================
|
|
# SPECIFIC UPGRADE
|
|
#=================================================
|
|
# SETUP LUTIM
|
|
#=================================================
|
|
ynh_script_progression --message="Reconfiguring $app..."
|
|
|
|
# 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 --file="$final_path/lutim.conf"
|
|
cp ../conf/lutim.conf.template "$final_path/lutim.conf"
|
|
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$final_path/lutim.conf"
|
|
ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$final_path/lutim.conf"
|
|
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$final_path/lutim.conf"
|
|
ynh_replace_string --match_string="__ENCRYPT__" --replace_string="$always_encrypt" --target_file="$final_path/lutim.conf"
|
|
ynh_replace_string --match_string="__SECRET__" --replace_string="$secret" --target_file="$final_path/lutim.conf"
|
|
# Set the number of process for Lutim to twice the number of CPU core.
|
|
ynh_replace_string --match_string="__WORKERS__" --replace_string="$(( $(nproc) * 2 ))" --target_file="$final_path/lutim.conf"
|
|
# Recalculate and store the checksum of the file for the next upgrade.
|
|
ynh_store_file_checksum --file="$final_path/lutim.conf"
|
|
|
|
# Optional parameters from config-panel feature
|
|
if [ -n "$antiflood" ]; then
|
|
ynh_replace_string --match_string=".*anti_flood_delay *=>.*" --replace_string=" anti_flood_delay => $antiflood," --target_file="$final_path/lutim.conf"
|
|
# Disable anti_flood_delay if the delay is 0
|
|
if [ $antiflood = 0 ]; then
|
|
ynh_replace_string --match_string="\(anti_flood_delay *=>.*\)" --replace_string="#\1" --target_file="$final_path/lutim.conf"
|
|
fi
|
|
fi
|
|
if [ -n "$delay" ]; then
|
|
ynh_replace_string --match_string=".*default_delay *=>.*" --replace_string=" default_delay => $delay," --target_file="$final_path/lutim.conf"
|
|
fi
|
|
fi
|
|
|
|
#=================================================
|
|
# SETUP HOOKS FILE
|
|
#=================================================
|
|
|
|
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="../hooks/post_app_addaccess"
|
|
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="../hooks/post_app_removeaccess"
|
|
|
|
#=================================================
|
|
# SETUP SYSTEMD
|
|
#=================================================
|
|
|
|
# Overwrite the systemd configuration only if it's allowed
|
|
if [ $overwrite_systemd -eq 1 ]
|
|
then
|
|
ynh_script_progression --message="Upgrading systemd configuration..." --weight=2
|
|
ynh_add_systemd_config
|
|
fi
|
|
|
|
#=================================================
|
|
# SETUP CRON
|
|
#=================================================
|
|
|
|
cp ../conf/cron_lutim /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="__APP__" --replace_string="$app" --target_file=/etc/cron.d/$app
|
|
|
|
#=================================================
|
|
# UPDATE LUTIM WITH CARTON
|
|
#=================================================
|
|
|
|
if [ "$upgrade_type" == "UPGRADE_APP" ]
|
|
then
|
|
ynh_script_progression --message="Upgrading $app with Carton..." --weight=4
|
|
pushd "$final_path"
|
|
ynh_secure_remove --file="$final_path/local"
|
|
carton install --without=mysql --without=htpasswd --without=test
|
|
popd
|
|
fi
|
|
|
|
#=================================================
|
|
# SECURING FILES AND DIRECTORIES
|
|
#=================================================
|
|
|
|
# Set permissions on app files
|
|
chown -R $app: $final_path
|
|
|
|
#=================================================
|
|
# GENERIC FINALISATION
|
|
#=================================================
|
|
# SETUP LOGROTATE
|
|
#=================================================
|
|
ynh_script_progression --message="Upgrading logrotate configuration..."
|
|
|
|
ynh_use_logrotate --non-append
|
|
chown $app -R /var/log/$app
|
|
|
|
#=================================================
|
|
# ADVERTISE SERVICE IN ADMIN PANEL
|
|
#=================================================
|
|
|
|
yunohost service add $app --log="$final_path/log/production.log"
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
ynh_script_progression --message="Reloading NGINX web server..."
|
|
|
|
ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
#=================================================
|
|
# START AND CHECK LUTIM BOOTING
|
|
#=================================================
|
|
ynh_script_progression --message="Restarting 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="Disabling 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 of $app completed" --last
|