1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/mautrix_signal_ynh.git synced 2024-09-03 19:46:07 +02:00
mautrix_signal_ynh/scripts/upgrade

248 lines
9.9 KiB
Text
Raw Normal View History

2021-01-04 12:14:25 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --time --weight=1
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
# language=$(ynh_app_setting_get --app=$app --key=language)
server_name=$(ynh_app_setting_get --app=$app --key=server_name)
port=$(ynh_app_setting_get --app=$app --key=port)
botname=$(ynh_app_setting_get --app=$app --key=botname)
synapse_instance=$(ynh_app_setting_get --app=$app --key=synapse_instance)
app_service_registration_path=$(ynh_app_setting_get --app=$app --key=app_service_registration_path)
# bot_is_synapse_admin=$(ynh_app_setting_get --app=$app --key=bot_is_synapse_admin)
encryption=$(ynh_app_setting_get --app=$app --key=encryption)
botadmin=$(ynh_app_setting_get --app=$app --key=botadmin)
botusers=$(ynh_app_setting_get --app=$app --key=botusers)
mautrix_config_path="$final_path/config.yaml"
mautrix_version=$(ynh_app_setting_get --app=$app --key=mautrix_version)
ynh_print_OFF
mautrix_bridge_db_pwd=$(ynh_app_setting_get --app=$app --key=mautrix_bridge_db_pwd)
ynh_print_ON
#=================================================
# SET CONSTANTS
#=================================================
log_filename="/var/log/$app/$app.log"
synapse_db_name="matrix_$synapse_instance"
# botname_synapse_db_user="@$botname:$server_name"
mautrix_bridge_user=$app
mautrix_bridge_db_name=$app
mautrix_bridge_db_user=$app
upstream_version=$(ynh_app_upstream_version)
2021-01-04 12:14:25 +01:00
#=================================================
# CHECK VERSION
#=================================================
### This helper will compare the version of the currently installed app and the version of the upstream package.
### $upgrade_type can have 2 different values
### - UPGRADE_APP if the upstream app version has changed
### - UPGRADE_PACKAGE if only the YunoHost package has changed
### ynh_check_app_version_changed will stop the upgrade if the app is up to date.
### UPGRADE_APP should be used to upgrade the core app only if there's an upgrade to do.
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --time --weight=1
#
# N.B. : the followings setting migrations snippets are provided as *EXAMPLES*
# of what you may want to do in some cases (e.g. a setting was not defined on
# some legacy installs and you therefore want to initiaze stuff during upgrade)
#
# If db_name doesn't exist, create it
#if [ -z "$db_name" ]; then
# db_name=$(ynh_sanitize_dbid --db_name=$app)
# ynh_app_setting_set --app=$app --key=db_name --value=$db_name
#fi
# If final_path doesn't exist, create it
#if [ -z "$final_path" ]; then
# final_path=/var/www/$app
# ynh_app_setting_set --app=$app --key=final_path --value=$final_path
#fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --time --weight=1
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# 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
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --time --weight=1
ynh_systemd_action --service_name=$app --action="stop" --log_path="$log_filename"
2021-01-04 12:14:25 +01:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --time --weight=1
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path/src"
2021-01-04 12:14:25 +01:00
fi
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --time --weight=1
ynh_install_app_dependencies $pkg_dependencies
ynh_install_extra_app_dependencies --repo="https://updates.signald.org unstable main" --package="$extra_dependencies" --key="https://updates.signald.org/apt-signing-key.asc"
2021-01-04 12:14:25 +01:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --time --weight=1
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$mautrix_bridge_user
2021-01-04 12:14:25 +01:00
#=================================================
# SPECIFIC UPGRADE
#=================================================
# ...
#=================================================
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --time --weight=1
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# SET MAUTRIX-BRIDGE CONFIG
2021-01-04 12:14:25 +01:00
#=================================================
ynh_script_progression --message="Configuring Mautrix-Bridge..." --weight=2
mautrix_config_path="$final_path/config.yaml"
2021-01-04 12:14:25 +01:00
### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it.
ynh_backup_if_checksum_is_different --file="$app_service_registration_path/$app.yaml"
ynh_backup_if_checksum_is_different --file="$mautrix_config_path"
verify_server_ssl_certificates="true"
matrix_server_supports_asmux="false"
2021-08-20 12:41:56 +02:00
# https://docs.python.org/3.6/library/logging.html#logging-levels
log_level="INFO"
2021-01-04 12:14:25 +01:00
ynh_add_config --template="../conf/config.yaml" --destination="$mautrix_config_path"
2021-01-04 12:14:25 +01:00
# Recalculate and store the checksum of the file for the next upgrade.
ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml"
ynh_store_file_checksum --file="$mautrix_config_path"
#=================================================
# UPGRADE MAUTRIX-BRIDGE PYTHON MODULE
#=================================================
mkdir -p /var/log/$app
# Configure Mautrix-Bridge
python3 -m venv $final_path
#$final_path/bin/pip3 install --upgrade setuptools wheel
$final_path/bin/pip3 install --upgrade $final_path/src/mautrix-signal.tar.gz[metrics,formattednumbers,qrlink,stickers]
# -r optional-requirements.txt
#=================================================
# REGISTER SYNAPSE APP-SERVICE
#=================================================
$final_path/bin/python3 -m mautrix_signal -g -c $mautrix_config_path -r $app_service_registration_path/$app.yaml
/opt/yunohost/matrix-$synapse_instance/update_synapse_for_appservice.sh \
|| ynh_die "Synapse can't restart with the appservice configuration"
# Handled by synapse: synapse_ynh adds all registration files added in $app_service_registration_path to the app_service_config_files list
2021-01-04 12:14:25 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions on app files
chown -R root: $final_path
chown $mautrix_bridge_user:root -R $final_path
2021-01-04 12:14:25 +01:00
2021-08-20 12:42:35 +02:00
# Fix possible permission issues with a previous signald version, esp. with stickers
chmod -R g+rwX /var/lib/signald/{avatars,attachments,stickers}
2021-01-04 12:14:25 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --time --weight=1
# Use logrotate to manage application logfile(s)
ynh_use_logrotate --logfile "$log_filename"
chown $mautrix_bridge_user:root -R /var/log/$app
2021-01-04 12:14:25 +01:00
# Use logrotate to manage app-specific logfile(s)
# ynh_use_logrotate --non-append
2021-01-04 12:14:25 +01:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --time --weight=1
yunohost service add $app --description="$app daemon for bridging Signal and Matrix messages" --log="$log_filename"
2021-01-04 12:14:25 +01:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --time --weight=1
# ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
ynh_systemd_action --service_name=$app --action="start"
# # Wait until the synapse user is created
# sleep 30
# # # (Note that, by default, non-admins might not have your homeserver's permission to create communities.)
# # if [ "$bot_is_synapse_admin" = true ]
# # then
# ynh_psql_execute_as_root --database=$synapse_db_name --sql="UPDATE users SET admin = 1 WHERE name = ""$botname"";"
# # #yunohost app action run $synapse_instance set_admin_user -a username=$botname
# # fi
# ynh_systemd_action --service_name=$app --action="restart"
2021-01-04 12:14:25 +01:00
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --time --last