mautrix_telegram_ynh/scripts/install
2021-04-05 16:40:54 +02:00

312 lines
14 KiB
Bash
Executable file

#!/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
#=================================================
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
# TODO new settings specific to telegram
apiid=$YNH_APP_ARG_APIID
apihash=$YNH_APP_ARG_APIHASH
bottoken=$YNH_APP_ARG_BOTTOKEN
app=$YNH_APP_INSTANCE_NAME
final_path=/opt/yunohost/$app
# TODO check (in manifest?) if the selected synapse instance is not already connected to a mautrix_bridge bridge
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"
#=================================================
# SET CONSTANTS
#=================================================
#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 IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Configuring firewall..." --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)
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=7
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
ynh_app_setting_set --app=$app --key=botname --value=$botname
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
ynh_app_setting_set --app=$app --key=mautrix_bridge_db_name --value=$mautrix_bridge_db_name
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
ynh_app_setting_set --app=$app --key=mautrix_version --value=$upstream_version
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=97
### `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
#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Creating a PostgreSQL database..." --weight=3
ynh_print_OFF
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
ynh_print_ON
# Create postgresql database
ynh_psql_test_if_first_run
ynh_print_OFF
ynh_psql_create_user $mautrix_bridge_db_user $mautrix_bridge_db_pwd
ynh_print_ON
ynh_psql_execute_as_root \
--sql="CREATE DATABASE ""$mautrix_bridge_db_name"" ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ""$mautrix_bridge_db_user"";"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=3
### `ynh_setup_source` is used to install an app from a zip or tar.gz file,
### downloaded from an upstream source, like a git repository.
### `ynh_setup_source` use the file conf/app.src
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# 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
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path/src"
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=1
# Create a system user
ynh_system_user_create --username=$mautrix_bridge_user
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=20
# Create systemd config for Mautrix-Bridge
#cp ../conf/default_mautrix-facebook /etc/default/$app
ynh_add_systemd_config --service=$app
#=================================================
#=================================================
# SET MAUTRIX-BRIDGE CONFIG
#=================================================
ynh_script_progression --message="Configuring Mautrix-Bridge..." --weight=2
# WARNING : theses command are used in INSTALL, UPGRADE, CONFIG, CHANGE-URL (4 times)
# For any update do it in all files
mautrix_config_path="$final_path/config.yaml"
# ynh_replace_string --match_string=__AS_TOKEN__ --replace_string="$generated" --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__HS_TOKEN__ --replace_string="$generated" --target_file="$mautrix_config_path"
as_token="\"This value is generated when generating the registration\""
hs_token="\"This value is generated when generating the registration\""
write_bridge_config
#Copy example-config.yaml to config.yaml
# cp ../conf/config.yaml "$mautrix_config_path"
# ynh_replace_string --match_string=__DOMAIN__ --replace_string="$domain" --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__SERVER_NAME__ --replace_string=$server_name --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__VERIFY_SERVER_SSL_CERTIFICATES__ --replace_string=true --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__MATRIX_SERVER_SUPPORTS_ASMUX__ --replace_string=false --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__PORT__ --replace_string=$port --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__MAUTRIX_BRIDGE_USER__ --replace_string=$mautrix_bridge_user --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__MAUTRIX_BRIDGE_DB_PWD__ --replace_string=$mautrix_bridge_db_pwd --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__MAUTRIX_BRIDGE_DB_NAME__ --replace_string=$mautrix_bridge_db_name --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__BOTNAME__ --replace_string=$botname --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__ENCRYPTION__ --replace_string=$encryption --target_file="$mautrix_config_path"
# if [ "$botusers" = "local" ]
# then
# ynh_replace_string --match_string=__BOTUSERS__ --replace_string=$server_name --target_file="$mautrix_config_path"
# else
# ynh_replace_string --match_string=__BOTUSERS__ --replace_string=$botusers --target_file="$mautrix_config_path"
# fi
# ynh_replace_string --match_string=__BOTADMIN__ --replace_string=$botadmin --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__APP__ --replace_string=$app --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__LOG_FILENAME__ --replace_string="/var/log/$app/$app.log" --target_file="$mautrix_config_path"
# # Options: debug, info, warn, error, fatal
# ynh_replace_string --match_string=__LOG_LEVEL__ --replace_string="error" --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__TELEGRAM_API_ID__ --replace_string=$apiid --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__TELEGRAM_API_HASH__ --replace_string=$apihash --target_file="$mautrix_config_path"
# ynh_replace_string --match_string=__TELEGRAM_BOT_TOKEN__ --replace_string=$bottoken --target_file="$mautrix_config_path"
#=================================================
# INSTALL 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 # HACK fixes "error: invalid command 'bdist_wheel'"
$final_path/bin/pip3 install --upgrade $final_path/src/mautrix-telegram.tar.gz[postgres,speedups,qr_login] # metrics,hq_thumbnails
# -r optional-requirements.txt
$final_path/bin/python3 -m mautrix_telegram -g -c "$mautrix_config_path" -r "$app_service_registration_path/$app.yaml"
# initialize the database
# HACK alembic won't find its script dir using a relative path unless when started from $final_path
# therefore we replace the script_location
ynh_replace_string --match_string="script_location = alembic" \
--replace_string="script_location = $final_path/alembic" \
--target_file="$final_path/alembic.ini"
$final_path/bin/alembic -c "$final_path/alembic.ini" -x config="$mautrix_config_path" upgrade head
/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
#=================================================
# STORE THE CONFIG FILE CHECKSUM
#=================================================
### `ynh_store_file_checksum` is used to store the checksum of a file.
### That way, during the upgrade script, by using `ynh_backup_if_checksum_is_different`,
### you can make a backup of this file before modifying it again if the admin had modified it.
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$mautrix_config_path"
ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml"
ynh_store_file_checksum --file="$final_path/alembic.ini"
#=================================================
# 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.
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
# For any update do it in all files
chown $mautrix_bridge_user:root -R $final_path
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=3
# Use logrotate to manage application logfile(s)
# TODO maybe --specific-user??
ynh_use_logrotate --logfile "/var/log/$app/$app.log"
chown $mautrix_bridge_user:root -R /var/log/$app
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
#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
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
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="Installation of $app completed" --last