2021-01-04 12:14:25 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC START
|
|
|
|
#=================================================
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
source _common.sh
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# MANAGE SCRIPT FAILURE
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
ynh_clean_setup () {
|
|
|
|
### Remove this function if there's nothing to clean before calling the remove script.
|
|
|
|
true
|
|
|
|
}
|
|
|
|
# Exit if an error occurs during the execution of the script
|
|
|
|
ynh_abort_if_errors
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# RETRIEVE ARGUMENTS FROM THE MANIFEST
|
|
|
|
#=================================================
|
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
synapsenumber=$YNH_APP_ARG_SYNAPSENUMBER
|
2021-01-04 12:14:25 +01:00
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
2021-01-22 22:44:36 +01:00
|
|
|
final_path=/opt/yunohost/$app
|
|
|
|
|
2021-02-10 21:07:18 +01:00
|
|
|
# ToDo check (in manifest?) if the selected synapse instance is not already connected to a mautrix_bridge bridge
|
2021-01-22 22:44:36 +01:00
|
|
|
if [ $synapsenumber -eq "1" ]
|
|
|
|
then
|
|
|
|
synapse_instance="synapse"
|
|
|
|
else
|
|
|
|
synapse_instance="synapse__$synapsenumber"
|
|
|
|
fi
|
|
|
|
server_name=$(ynh_app_setting_get --app $synapse_instance --key server_name)
|
|
|
|
domain=$(ynh_app_setting_get --app $synapse_instance --key domain)
|
|
|
|
synapse_config_path="/etc/matrix-$synapse_instance"
|
|
|
|
app_service_registration_path="/etc/matrix-$synapse_instance/app-service"
|
|
|
|
synapse_name="matrix-$synapse_instance"
|
|
|
|
synapse_user="matrix-$synapse_instance"
|
|
|
|
synapse_db_name="matrix_$synapse_instance"
|
|
|
|
synapse_db_user="matrix_$synapse_instance"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-06-22 18:28:47 +02:00
|
|
|
botname=$YNH_APP_ARG_BOTNAME
|
|
|
|
encryption=$YNH_APP_ARG_ENCRYPTION
|
|
|
|
botadmin=$YNH_APP_ARG_BOTADMIN
|
|
|
|
if [ "$YNH_APP_ARG_BOTUSERS" = "local" ]
|
|
|
|
then
|
|
|
|
botusers=$server_name
|
|
|
|
elif [ "$YNH_APP_ARG_BOTUSERS" = "admin" ]
|
|
|
|
then
|
|
|
|
botusers=$botadmin
|
|
|
|
else
|
|
|
|
botusers=$YNH_APP_ARG_BOTUSERS
|
|
|
|
fi
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
# SET CONSTANTS
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
|
|
|
|
2021-02-10 21:07:18 +01:00
|
|
|
#botname_synapse_db_user="@$botname:$server_name"
|
|
|
|
mautrix_bridge_user=$app
|
|
|
|
mautrix_bridge_db_name=$app
|
|
|
|
mautrix_bridge_db_user=$app
|
2021-01-22 22:44:36 +01:00
|
|
|
upstream_version=$(ynh_app_upstream_version)
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Validating installation parameters..." --weight=1
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STANDARD MODIFICATIONS
|
|
|
|
#=================================================
|
|
|
|
# FIND AND OPEN A PORT
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Configuring firewall..." --weight=1
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-02-10 21:07:18 +01:00
|
|
|
# Find a free port for communication between your local synapse instance (home server) and its app service mautrix_bridge.
|
2021-01-22 22:44:36 +01:00
|
|
|
port=$(ynh_find_port --port=8449)
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
#=================================================
|
|
|
|
# STORE SETTINGS FROM MANIFEST
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Storing installation settings..." --weight=7
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=domain --value=$domain
|
|
|
|
ynh_app_setting_set --app=$app --key=server_name --value=$server_name
|
|
|
|
ynh_app_setting_set --app=$app --key=port --value=$port
|
2021-02-10 21:07:18 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=botname --value=$botname
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=synapse_instance --value=$synapse_instance
|
|
|
|
ynh_app_setting_set --app=$app --key=app_service_registration_path --value=$app_service_registration_path
|
|
|
|
ynh_app_setting_set --app=$app --key=encryption --value=$encryption
|
2021-02-10 21:07:18 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=mautrix_bridge_db_name --value=$mautrix_bridge_db_name
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=botadmin --value=$botadmin
|
|
|
|
ynh_app_setting_set --app=$app --key=botusers --value=$botusers
|
|
|
|
ynh_app_setting_set --app=$app --key=mautrix_version --value=$upstream_version
|
|
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# INSTALL DEPENDENCIES
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Installing dependencies..." --weight=97
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
### `ynh_install_app_dependencies` allows you to add any "apt" dependencies to the package.
|
|
|
|
### Those deb packages will be installed as dependencies of this package.
|
|
|
|
### If you're not using this helper:
|
|
|
|
### - Remove the section "REMOVE DEPENDENCIES" in the remove script
|
|
|
|
### - Remove the variable "pkg_dependencies" in _common.sh
|
|
|
|
### - As well as the section "REINSTALL DEPENDENCIES" in the restore script
|
|
|
|
### - And the section "UPGRADE DEPENDENCIES" in the upgrade script
|
|
|
|
|
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
|
|
|
|
2021-06-22 18:28:47 +02:00
|
|
|
ynh_install_extra_app_dependencies --repo="https://updates.signald.org unstable main" --package="signald" --key="https://updates.signald.org/apt-signing-key.asc"
|
2021-02-11 22:17:53 +01:00
|
|
|
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
# CREATE A POSTGRESQL DATABASE
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Creating a PostgreSQL database..." --weight=3
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_print_OFF
|
2021-02-10 21:07:18 +01:00
|
|
|
mautrix_bridge_db_pwd=$(ynh_string_random --length=30)
|
|
|
|
ynh_app_setting_set --app=$app --key=mautrix_bridge_db_pwd --value=$mautrix_bridge_db_pwd
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_print_ON
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
# Create postgresql database
|
|
|
|
ynh_psql_test_if_first_run
|
|
|
|
ynh_print_OFF
|
2021-02-10 21:07:18 +01:00
|
|
|
ynh_psql_create_user $mautrix_bridge_db_user $mautrix_bridge_db_pwd
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_print_ON
|
|
|
|
ynh_psql_execute_as_root \
|
2021-02-10 21:07:18 +01:00
|
|
|
--sql="CREATE DATABASE ""$mautrix_bridge_db_name"" ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ""$mautrix_bridge_db_user"";"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Setting up source files..." --weight=3
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
# WARNING : theses command are used in INSTALL, UPGRADE (2 times)
|
|
|
|
# For any update do it in all files
|
|
|
|
#if [ -n "$(uname -m | grep 64)" ]
|
|
|
|
#then
|
|
|
|
# ynh_setup_source --dest_dir=$final_path/ --source_id="amd64_$(lsb_release --codename --short)"
|
|
|
|
#else
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
# Download, check integrity, uncompress and patch the source from app.src
|
2021-01-23 21:06:11 +01:00
|
|
|
ynh_setup_source --dest_dir="$final_path/src"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CREATE DEDICATED USER
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Configuring system user..." --weight=1
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
# Create a system user
|
2021-02-10 21:07:18 +01:00
|
|
|
ynh_system_user_create --username=$mautrix_bridge_user
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
# SETUP SYSTEMD
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Configuring a systemd service..." --weight=20
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-02-10 21:07:18 +01:00
|
|
|
# Create systemd config for Mautrix-Bridge
|
2021-01-22 22:44:36 +01:00
|
|
|
#cp ../conf/default_mautrix-facebook /etc/default/$app
|
|
|
|
ynh_add_systemd_config --service=$app
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
#=================================================
|
2021-02-10 21:07:18 +01:00
|
|
|
# SET MAUTRIX-BRIDGE CONFIG
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2021-02-10 21:07:18 +01:00
|
|
|
ynh_script_progression --message="Configuring Mautrix-Bridge..." --weight=2
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
# WARNING : theses command are used in INSTALL, UPGRADE, CONFIG, CHANGE-URL (4 times)
|
|
|
|
# For any update do it in all files
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
mautrix_config_path="$final_path/config.yaml"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-06-22 18:28:47 +02:00
|
|
|
verify_server_ssl_certificates="true"
|
|
|
|
matrix_server_supports_asmux="false"
|
|
|
|
log_filename="/var/log/$app/$app.log"
|
2021-01-22 22:44:36 +01:00
|
|
|
# Options: debug, info, warn, error, fatal
|
2021-06-22 18:28:47 +02:00
|
|
|
log_level="error"
|
2021-01-22 22:44:36 +01:00
|
|
|
|
2021-06-22 18:28:47 +02:00
|
|
|
ynh_add_config --template="../conf/config.yaml" --destination="$mautrix_config_path"
|
2021-02-06 00:40:51 +01:00
|
|
|
|
|
|
|
#=================================================
|
2021-02-10 21:07:18 +01:00
|
|
|
# INSTALL MAUTRIX-BRIDGE PYTHON MODULE
|
2021-02-06 00:40:51 +01:00
|
|
|
#=================================================
|
|
|
|
|
2021-02-06 00:43:01 +01:00
|
|
|
mkdir -p /var/log/$app
|
2021-02-10 21:07:18 +01:00
|
|
|
# Configure Mautrix-Bridge
|
2021-01-23 01:00:48 +01:00
|
|
|
python3 -m venv $final_path
|
2021-02-13 12:06:35 +01:00
|
|
|
#$final_path/bin/pip3 install --upgrade setuptools wheel
|
2021-02-13 12:47:59 +01:00
|
|
|
$final_path/bin/pip3 install $final_path/src/mautrix-signal.tar.gz[metrics,formattednumbers,qrlink]
|
2021-02-13 11:59:56 +01:00
|
|
|
# -r optional-requirements.txt
|
2021-02-10 21:07:18 +01:00
|
|
|
$final_path/bin/python3 -m mautrix_signal -g -c $mautrix_config_path -r $app_service_registration_path/$app.yaml
|
2021-01-23 01:00:48 +01:00
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
/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
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STORE THE CONFIG FILE CHECKSUM
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Calculate and store the config file checksum into the app settings
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml"
|
|
|
|
ynh_store_file_checksum --file="$mautrix_config_path"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# SECURE FILES AND DIRECTORIES
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
### For security reason, any app should set the permissions to root: before anything else.
|
|
|
|
### Then, if write authorization is needed, any access should be given only to directories
|
|
|
|
### that really need such authorization.
|
|
|
|
|
|
|
|
# Set permissions to app files
|
|
|
|
chown -R root: $final_path
|
|
|
|
|
2021-01-22 22:44:36 +01:00
|
|
|
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
|
|
|
|
# For any update do it in all files
|
2021-02-10 21:07:18 +01:00
|
|
|
chown $mautrix_bridge_user:root -R $final_path
|
2021-01-22 22:44:36 +01:00
|
|
|
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
|
|
|
# SETUP LOGROTATE
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Configuring log rotation..." --weight=3
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
# Use logrotate to manage application logfile(s)
|
2021-06-22 18:28:47 +02:00
|
|
|
ynh_use_logrotate --logfile "$log_filename"
|
2021-02-10 21:07:18 +01:00
|
|
|
chown $mautrix_bridge_user:root -R /var/log/$app
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
# ADVERTISE SERVICE IN ADMIN PANEL
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
#yunohost service add $app --log "/var/log/$app/log.log"
|
|
|
|
# if using yunohost version 3.2 or more in the 'manifest.json', a description can be added
|
2021-06-22 18:28:47 +02:00
|
|
|
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
|
|
|
|
#=================================================
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=1
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
# Start a systemd service
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_systemd_action --service_name=$app --action="start"
|
|
|
|
# Wait until the synapse user is created
|
2021-02-07 20:42:56 +01:00
|
|
|
sleep 30
|
2021-02-06 00:48:13 +01:00
|
|
|
# # (Note that, by default, non-admins might not have your homeserver's permission to create communities.)
|
|
|
|
# if [ "$bot_is_synapse_admin" = true ]
|
|
|
|
# then
|
2021-02-10 21:07:18 +01:00
|
|
|
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
|
2021-02-06 00:48:13 +01:00
|
|
|
# fi
|
2021-02-07 20:42:56 +01:00
|
|
|
ynh_systemd_action --service_name=$app --action="restart"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# END OF SCRIPT
|
|
|
|
#=================================================
|
|
|
|
|
2021-02-05 23:47:33 +01:00
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|