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
|
|
|
|
#=================================================
|
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
botname=$YNH_APP_ARG_BOTNAME
|
|
|
|
bot_synapse_adm=true
|
|
|
|
#encryption=$YNH_APP_ARG_ENCRYPTION
|
|
|
|
encryption=false
|
|
|
|
botadmin=$YNH_APP_ARG_BOTADMIN
|
|
|
|
botusers=$YNH_APP_ARG_BOTUSERS
|
2022-02-02 11:01:47 +01:00
|
|
|
if [ "$botusers" == "admin" ]
|
2022-01-25 09:49:10 +01:00
|
|
|
then
|
|
|
|
botusers=$botadmin
|
|
|
|
fi
|
2021-01-22 22:44:36 +01:00
|
|
|
synapsenumber=$YNH_APP_ARG_SYNAPSENUMBER
|
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)
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
mautrix_version=$(ynh_app_upstream_version)
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
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
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
final_path=/opt/yunohost/$app
|
2021-01-22 22:44:36 +01:00
|
|
|
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
# SET CONSTANTS
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
enable_relaybot=true
|
|
|
|
bot_synapse_db_user="@$botname:$server_name"
|
|
|
|
synapse_db_name="matrix_$synapse_instance"
|
|
|
|
|
|
|
|
signald_user="signald" # This is actually chosen by the signald dependency
|
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
|
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
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=bot_synapse_adm --value=$bot_synapse_adm
|
2021-01-22 22:44:36 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=encryption --value=$encryption
|
|
|
|
ynh_app_setting_set --app=$app --key=botadmin --value=$botadmin
|
|
|
|
ynh_app_setting_set --app=$app --key=botusers --value=$botusers
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=mautrix_version --value=$mautrix_version
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
# STANDARD MODIFICATIONS
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
# FIND AND OPEN A PORT
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Finding an available port..." --weight=1
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
# Find a free port for communication between your local synapse instance (home server) and its app service mautrix_bridge.
|
|
|
|
port=$(ynh_find_port --port=8449)
|
|
|
|
ynh_app_setting_set --app=$app --key=port --value=$port
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
# INSTALL DEPENDENCIES
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Installing dependencies..." --weight=10
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_install_extra_app_dependencies --repo="http://http.debian.net/debian buster-backports main" --package="libolm-dev" --key="https://keyserver.ubuntu.com/pks/lookup?search=0x0E98404D386FA1D9&op=get"
|
|
|
|
|
|
|
|
ynh_script_progression --message="Installing dependencies..." --weight=10
|
|
|
|
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
|
|
|
|
#=================================================
|
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-08-19 17:41:14 +02:00
|
|
|
# Add the user to the signald group. The signald group was created when the signald
|
|
|
|
# package was installed from the extra repository
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_system_user_create --username=$app --groups="$signald_user"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
# CREATE A POSTGRESQL DATABASE
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Creating a PostgreSQL database..." --weight=3
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
db_name=$(ynh_sanitize_dbid --db_name=$app)
|
|
|
|
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
|
|
|
|
db_pwd=$(ynh_string_random --length=30)
|
|
|
|
ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
|
|
|
|
ynh_psql_test_if_first_run
|
|
|
|
ynh_psql_setup_db --db_user=$db_name --db_name=$db_name --db_pwd=$db_pwd
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
2021-01-04 12:14:25 +01:00
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Setting up source files..." --weight=3
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
# Download, check integrity, uncompress and patch the source from app.src
|
|
|
|
ynh_setup_source --dest_dir="$final_path/src"
|
|
|
|
chmod 750 "$final_path"
|
|
|
|
chown $app:$app "$final_path"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
#=================================================
|
|
|
|
# ADD A CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring Mautrix Bridge..." --weight=2
|
2021-01-04 12:14:25 +01:00
|
|
|
|
2021-08-19 17:41:14 +02:00
|
|
|
# https://docs.python.org/3.6/library/logging.html#logging-levels
|
2021-08-20 12:42:20 +02:00
|
|
|
log_level="INFO"
|
2021-01-22 22:44:36 +01:00
|
|
|
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_add_config --template="../conf/config.yaml" --destination="$final_path/config.yaml"
|
|
|
|
chmod -R 750 "$final_path"
|
|
|
|
chown -R $app:$app "$final_path"
|
2021-02-06 00:40:51 +01:00
|
|
|
|
2021-08-19 17:41:14 +02:00
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
# SETUP SYSTEMD
|
2021-08-19 17:41:14 +02:00
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Configuring a systemd service..." --weight=3
|
|
|
|
|
|
|
|
# Create a dedicated systemd config
|
|
|
|
ynh_add_systemd_config
|
2021-08-19 17:41:14 +02:00
|
|
|
|
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
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Configuring python3 virtual environment" --weight=6
|
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-08-19 17:41:14 +02:00
|
|
|
$final_path/bin/pip3 install --upgrade pip setuptools wheel
|
2022-01-25 09:49:10 +01:00
|
|
|
|
|
|
|
ynh_script_progression --message="Installing Bridge with pip" --weight=6
|
|
|
|
|
|
|
|
# Pre-compiled python-olm to avoid libolm-dev, python3-dev -> does not work for arm
|
|
|
|
#$final_path/bin/pip3 install --upgrade python-olm --extra-index-url https://gitlab.matrix.org/api/v4/projects/27/packages/pypi/simple
|
|
|
|
# Build with libolm3 end-to-bridge encryption
|
|
|
|
$final_path/bin/pip3 install $final_path/src/mautrix-signal.tar.gz[metrics,e2be,formattednumbers,qrlink,stickers]
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# REGISTER SYNAPSE APP-SERVICE
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
$final_path/bin/python3 -m mautrix_signal -g -c $final_path/config.yaml -r /etc/matrix-$synapse_instance/app-service/$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"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STORE THE CONFIG FILE CHECKSUM
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Calculate and store the config file checksum into the app settings
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_store_file_checksum --file="/etc/matrix-$synapse_instance/app-service/$app.yaml"
|
|
|
|
ynh_store_file_checksum --file="$final_path/config.yaml"
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# 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)
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_use_logrotate
|
|
|
|
chmod -R 600 "/var/log/$app"
|
|
|
|
chmod 700 "/var/log/$app"
|
|
|
|
chown -R $app:$app /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
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
|
|
|
|
yunohost service add $app --description="Bridging Signal and Matrix messages" --log=/var/log/$app/$app.log
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# START SYSTEMD SERVICE
|
|
|
|
#=================================================
|
2022-01-25 09:49:10 +01:00
|
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=2
|
2021-01-04 12:14:25 +01:00
|
|
|
|
|
|
|
# Start a systemd service
|
2022-01-25 09:49:10 +01:00
|
|
|
sleep 2
|
|
|
|
chown -R $app:$app "$final_path"
|
|
|
|
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
|
|
|
|
## 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_synapse_adm" = true ]
|
|
|
|
# then
|
|
|
|
# ynh_psql_execute_as_root --database=$synapse_db_name --sql="UPDATE users SET admin = 1 WHERE name = ""$botname"";"
|
2021-02-10 21:07:18 +01:00
|
|
|
# #yunohost app action run $synapse_instance set_admin_user -a username=$botname
|
2022-01-25 09:49:10 +01:00
|
|
|
# fi
|
|
|
|
#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
|