#!/bin/bash #================================================= # GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= # Exit if an error occurs during the execution of the script ynh_abort_if_errors #================================================= # LOAD SETTINGS #================================================= app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get $app domain) path_url=$(ynh_app_setting_get $app path) # used during nginx configuration is_public=$(ynh_app_setting_get $app is_public) port=$(ynh_app_setting_get $app port) mattermost_user="$app" root_path="$(pwd)/.." final_path="/var/www/$app" data_path="/home/yunohost.app/$app" logs_path="/var/log/$app" #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= # Backup the current version of the app # (NB: `tee` uses process substitution rather than a pipe, # to avoid spawning a subshell that would not save global # variables defined by `ynh_backup_before_upgrade`.) ynh_backup_before_upgrade > >(tee "upgrade.log") # Ensure the backup can be restored if grep -q "mattermost: Warning$" "upgrade.log"; then can_restore_backup=false else can_restore_backup=true fi # If the upgrade fails… ynh_clean_setup () { if [ "$can_restore_backup" = true ]; then # Stop attempting to restart the app if $(systemctl -q is-active "$app"); then systemctl stop "$app" fi # Restore the backup ynh_restore_upgradebackup else # Backup restoration is not available: # let's try at least to restart the server. systemctl start "$app" fi } # Exit if an error occurs during the execution of the script ynh_abort_if_errors #================================================= # STOP SERVER #================================================= # Stop the server (if the app is already using systemd) if $(systemctl -q is-active "$app"); then systemctl stop "$app" fi # Legacy, for previous versions of this app which used supervisor if [ -f "/etc/supervisor/conf.d/${app}.conf" ]; then supervisorctl stop "$app" rm -f "/etc/supervisor/conf.d/${app}.conf" fi #================================================= # BACKUP CONFIGURATION FILE #================================================= config_file="$final_path/config/config.json" backup_config_file="/tmp/config.json" cp -f "$config_file" "$backup_config_file" #================================================= # MIGRATE SETTINGS FROM PREVIOUS VERSIONS #================================================= # Convert is_public from "Yes"/"No" to 1 / 0 if [[ $is_public == "Yes" ]]; then is_public=1 elif [[ $is_public == "No" ]]; then is_public=0 fi ynh_app_setting_set "$app" is_public "$is_public" # Save the port used if not present if ! [[ "$port" ]]; then port=8065 ynh_app_setting_set "$app" port "$port" fi #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= ynh_app_setting_set "$app" final_path "$final_path" ynh_setup_source "$final_path" #================================================= # RESTORE CONFIGURATION FILE #================================================= cp -f "$backup_config_file" "$config_file" #================================================= # NGINX CONFIGURATION #================================================= ynh_add_nginx_config #================================================= # SYSTEMD CONFIGURATION #================================================= ynh_add_systemd_config #================================================= # ADVERTISE SERVICE IN ADMIN PANEL #================================================= yunohost service add "$app" --log "$logs_path/mattermost.log" #================================================= # SPECIFIC UPGRADE STEPS #================================================= # Fix log FileLocation path (changed in Mattermost 3.8, makes Mattermost >= 4.2 crash) # https://docs.mattermost.com/administration/changelog.html#release-v3-8-3 ynh_replace_string --match "\"FileLocation\": \"/var/log/mattermost.log\"" --replace "\"FileLocation\": \"/var/log\"" --target "$config_file" # Move log files to a directory (rather than directly in /var/log) # See https://github.com/YunoHost-Apps/mattermost_ynh/issues/61 mkdir -p "$logs_path" ynh_replace_string --match "\"FileLocation\": \"/var/log\"" --replace "\"FileLocation\": \"$logs_path\"" --target "$config_file" if [ -f "/var/log/${app}.log" ]; then mv "/var/log/${app}.log" "$logs_path/" fi #================================================= # RESTORE FILE PERMISSIONS #================================================= chown -R "$mattermost_user:www-data" "$final_path" chown -R "$mattermost_user:www-data" "$data_path" chown -R "$mattermost_user:adm" "$logs_path" #================================================= # RELOAD NGINX #================================================= service nginx reload #================================================= # START SERVER #================================================= systemctl start "$app"