mautrix_telegram_ynh/scripts/install

217 lines
8.2 KiB
Text
Raw Normal View History

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
2022-09-04 23:58:47 +02:00
true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
appserviceid=$YNH_APP_INSTANCE_NAME
synapsenumber=$YNH_APP_ARG_SYNAPSENUMBER
botname=$YNH_APP_ARG_BOTNAME
encryption=$YNH_APP_ARG_ENCRYPTION
botadmin=$YNH_APP_ARG_BOTADMIN
botusers=$YNH_APP_ARG_BOTUSERS
apiid=$YNH_APP_ARG_APIID
apihash=$YNH_APP_ARG_APIHASH
bottoken=$YNH_APP_ARG_BOTTOKEN
app=$YNH_APP_INSTANCE_NAME
2022-09-05 00:44:56 +02:00
# ToDo check (in manifest?) if the selected synapse instance is not already connected to a mautrix_bridge bridge
if [ $synapsenumber -eq "1" ]
then
2022-09-04 23:58:47 +02:00
synapse_instance="synapse"
else
2022-09-04 23:58:47 +02:00
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)
2022-09-04 23:58:47 +02:00
synapse_registration_path="/etc/matrix-$synapse_instance/app-service"
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
2022-07-16 01:38:42 +02:00
final_path=/opt/yunohost/$app
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=7
2022-09-04 23:58:47 +02:00
ynh_app_setting_set --app=$app --key=appserviceid --value=$appserviceid
ynh_app_setting_set --app=$app --key=botname --value=$botname
ynh_app_setting_set --app=$app --key=encryption --value=$encryption
2021-04-05 13:28:00 +02: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=apiid --value=$apiid
ynh_app_setting_set --app=$app --key=apihash --value=$apihash
ynh_app_setting_set --app=$app --key=bottoken --value=$bottoken
2022-09-04 23:58:47 +02:00
ynh_app_setting_set --app=$app --key=synapse_instance --value=$synapse_instance
ynh_app_setting_set --app=$app --key=server_name --value=$server_name
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=synapse_registration_path --value=$synapse_registration_path
2022-07-16 01:38:42 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Finding an available port..." --weight=1
# Find a free port for communication between your local synapse instance (home server) and its app service mautrix_bridge.
port=$(ynh_find_port --port=29317)
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=97
2022-08-27 23:33:17 +02:00
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
2022-07-16 01:38:42 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=1
# Create a system user
2022-09-04 23:58:47 +02:00
ynh_system_user_create --username=$app --home_dir="$final_path"
2022-07-16 01:38:42 +02:00
#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Creating a PostgreSQL database..." --weight=3
2022-09-04 23:58:47 +02:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_psql_test_if_first_run
2022-09-04 23:58:47 +02:00
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=3
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path/src"
2022-09-04 23:58:47 +02:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path"
#=================================================
2022-07-16 01:38:42 +02:00
# SPECIFIC SETUP
#=================================================
2022-09-04 23:58:47 +02:00
# ADD A CONFIGURATION
#=================================================
2022-09-04 23:58:47 +02:00
ynh_script_progression --message="Adding a configuration file..." --weight=2
# WARNING : theses command are used in INSTALL, UPGRADE, CONFIG, CHANGE-URL (4 times)
# For any update do it in all files
2021-04-05 15:20:06 +02:00
as_token="\"This value is generated when generating the registration\""
hs_token="\"This value is generated when generating the registration\""
is_encryption_enabled="$encryption"
2022-04-12 18:25:33 +02:00
if [ $encryption -eq 1 ]; then
is_encryption_enabled="true"
2022-04-12 18:25:33 +02:00
fi
2022-09-04 23:58:47 +02:00
if [ "$botusers" = "local" ]
then
botusers="$server_name"
fi
ynh_add_config --template="config.yaml" --destination="$final_path/config.yaml"
chmod 400 "$final_path/config.yaml"
chown $app:$app "$final_path/config.yaml"
2021-04-05 13:28:00 +02:00
#=================================================
# INSTALL MAUTRIX-BRIDGE PYTHON MODULE
#=================================================
2022-09-04 23:58:47 +02:00
ynh_script_progression --message="Installing Mautrix-Bridge Python Module..." --weight=6
mkdir -p /var/log/$app
# Configure Mautrix-Bridge
python3 -m venv $final_path
2022-09-04 23:58:47 +02:00
export HOME=$final_path
2022-03-23 23:45:52 +01:00
$final_path/bin/pip3 install --upgrade pip setuptools wheel
$final_path/bin/pip3 install --upgrade $final_path/src/mautrix-telegram.tar.gz[e2be,speedups,qr_login] # metrics,hq_thumbnails,sqlite,formattednumbers
2022-04-12 18:25:33 +02:00
#=================================================
2022-09-04 23:58:47 +02:00
# REGISTER SYNAPSE APP-SERVICE
#=================================================
2022-09-04 23:58:47 +02:00
ynh_script_progression --message="Registering Synapse app-service" --weight=1
$final_path/bin/python3 -m mautrix_telegram -g -c $final_path/config.yaml -r "$synapse_registration_path/$app.yaml"
/opt/yunohost/matrix-$synapse_instance/update_synapse_for_appservice.sh || ynh_die --message="Synapse can't restart with the appservice configuration"
2022-09-04 23:58:47 +02:00
chown -R $app:$app "$final_path"
ynh_store_file_checksum --file="$synapse_registration_path/$app.yaml"
ynh_store_file_checksum --file="$final_path/config.yaml"
#=================================================
2022-07-16 01:38:42 +02:00
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=20
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=3
# Use logrotate to manage application logfile(s)
2021-04-05 15:20:06 +02:00
# TODO maybe --specific-user??
2022-09-04 23:58:47 +02:00
ynh_use_logrotate --logfile "/var/log/$app/$app.log" --specific_user $app
chown -R $app:$app /var/log/$app
#=================================================
2022-07-16 01:38:42 +02:00
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2022-07-16 01:38:42 +02:00
ynh_script_progression --message="Integrating service in YunoHost..."
2022-09-04 23:58:47 +02:00
yunohost service add $app --description="$app daemon for bridging Telegram and Matrix messages" --log="/var/log/$app/$app.log"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
# Start a systemd service
2022-09-04 23:58:47 +02:00
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last