1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/mattermost_ynh.git synced 2024-09-03 19:36:29 +02:00
mattermost_ynh/scripts/upgrade
2023-03-26 17:00:36 +02:00

153 lines
5.4 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..." --weight=2
#team_display_name=$(ynh_app_setting_get --app=$app --key=team_display_name)
email=$(ynh_user_get_info --username=$admin --key=mail)
#=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
# Save the language used if not present
if [ -z "${language:-}" ]; then
language="en"
ynh_app_setting_set --app=$app --key=language --value=$language
fi
# If version setting doesn't exist
if [ -z "${version:-}" ]; then
version="Enterprise"
ynh_app_setting_set --app=$app --key=version --value=$version
fi
#=================================================
# MIGRATING DATABASE
#=================================================
# Check if using MariaDB
# This migration should be done before the upgrade
if mysqlshow | grep -q "^| $db_name "; then
# Mattermost only support MySQL and PostgreSQL (not MariaDB...)
# Migrate the database from MariaDB to PostgreSQL
remove_psql_in_case_of_error=1
mariadb-to-pg
fi
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=3
ynh_systemd_action --service_name=$app --action=stop --log_path="/var/log/$app/$app.log"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --weight=2
# Create a temporary directory
tmpdir="$(mktemp -d)"
# Backup the config file and local plugins in the temp dir
cp -a "$install_dir/config/config.json" "$tmpdir/config.json"
cp -ar "$install_dir/plugins" "$tmpdir/plugins"
# Remove the app directory securely
ynh_secure_remove --file="$install_dir"
if [ "$version" = "Enterprise" ]; then
ynh_setup_source --dest_dir="$install_dir" --source_id="enterprise"
elif [ "$version" = "Team" ]; then
ynh_setup_source --dest_dir="$install_dir" --source_id="main"
fi
# Copy the admin saved settings and plugins from tmp directory to final path
cp -a "$tmpdir/config.json" "$install_dir/config/config.json"
cp -ar --no-clobber "$tmpdir/plugins" "$install_dir/"
# Remove the tmp directory securely
ynh_secure_remove --file="$tmpdir"
fi
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"
#=================================================
# REAPPLY SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression --message="Upgrading system configurations related to $app..." --weight=3
# Create a dedicated NGINX config
ynh_add_nginx_config
# Create a dedicated systemd config
ynh_add_systemd_config
yunohost service add $app --description="Collaboration platform built for developers" --log="/var/log/$app/$app.log"
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
# Fix old migrations
#=================================================
# Crazy fix for old unupgraded version
# IMPORTANT: THIS fix should be done after setup new sources and running Mattermost
if ynh_compare_current_package_version --comparison lt --version 5.37.1~ynh1
then
read -r -d '' fix_old_version_sql << EOM
ALTER TABLE ChannelMembers ALTER COLUMN mentioncountroot SET DEFAULT '0'::bigint;
UPDATE ChannelMembers SET mentioncountroot=0 WHERE mentioncountroot IS NULL;
ALTER TABLE ChannelMembers ALTER COLUMN msgcountroot SET DEFAULT '0'::bigint;
UPDATE ChannelMembers SET msgcountroot=0 WHERE msgcountroot IS NULL;
ALTER TABLE Channels ALTER COLUMN totalmsgcountroot SET DEFAULT '0'::bigint;
UPDATE Channels SET totalmsgcountroot=0 WHERE totalmsgcountroot IS NULL;
UPDATE SidebarCategories SET collapsed=False where collapsed IS NULL;
UPDATE SidebarCategories SET muted=False where muted IS NULL;
UPDATE SidebarCategories set sorting = 'manual' where sorting='';
UPDATE SidebarCategories set sorting = 'manual' where sorting IS NULL;
EOM
ynh_psql_execute_as_root --sql="$fix_old_version_sql" --database=$db_name
# Note: it's possible that some instances need other fixes
# If nothing is displayed in the sidebar it may be needed to change the Id of SidebarCategories...
fi
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=2
# Start a systemd service
ynh_systemd_action --service_name=$app --action=start --log_path=systemd --line_match="Started Mattermost"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last