#!/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) #================================================= # 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" #================================================= # 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" 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" #================================================= # 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 #================================================= # 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 #================================================= ynh_script_progression --message="Configuring Mautrix-Bridge..." --weight=2 mautrix_config_path="$final_path/config.yaml" ### 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" # https://docs.python.org/3.6/library/logging.html#logging-levels log_level="INFO" ynh_add_config --template="../conf/config.yaml" --destination="$mautrix_config_path" # 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 #================================================= # GENERIC FINALIZATION #================================================= # SECURE FILES AND DIRECTORIES #================================================= # Set permissions on app files chown -R root: $final_path chown $mautrix_bridge_user:root -R $final_path # Fix possible permission issues with a previous signald version, esp. with stickers chmod -R g+rwX /var/lib/signald/{avatars,attachments,stickers} #================================================= # 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 # Use logrotate to manage app-specific logfile(s) # ynh_use_logrotate --non-append #================================================= # 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" #================================================= # 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" #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Upgrade of $app completed" --time --last