2017-02-13 20:43:41 +01:00
#!/bin/bash
2018-01-30 23:44:49 +01:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2019-04-30 19:15:33 +02:00
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source ../settings/scripts/experimental_helper.sh
2017-07-21 22:28:49 +02:00
source /usr/share/yunohost/helpers
2017-02-13 20:43:41 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
# Clean installation remainings that are not handled by the remove script.
ynh_clean_check_starting
}
# Exit if an error occurs during the execution of the script
2017-07-21 22:28:49 +02:00
ynh_abort_if_errors
2017-02-13 20:43:41 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading settings..."
app=$YNH_APP_INSTANCE_NAME
2019-12-14 15:11:33 +01:00
domain=$(ynh_app_setting_get --app=$app --key=domain)
2019-10-30 21:15:22 +01:00
server_name=$(ynh_app_setting_get --app=$app --key=server_name)
2019-12-14 15:11:33 +01:00
path_url=$(ynh_app_setting_get --app=$app --key=path)
2019-04-30 19:15:33 +02:00
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
synapse_tls_port=$(ynh_app_setting_get --app=$app --key=synapse_tls_port)
turnserver_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_tls_port)
turnserver_alt_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_alt_tls_port)
2021-03-12 20:08:14 +01:00
phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
2019-06-02 00:09:14 +02:00
synapse_db_pwd=$(ynh_app_setting_get --app=$app --key=synapse_db_pwd)
2022-05-26 17:19:33 +02:00
synapse_user_app_pwd=$(ynh_app_setting_get --app=$app --key=synapse_user_app_pwd)
2022-06-24 13:10:12 +02:00
main_domain=$(yunohost domain list --output-as json | jq -r .main)
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
# SET ALL CONSTANT
#=================================================
synapse_user="matrix-$app"
2022-05-26 17:27:30 +02:00
synapse_user_app="$app"
2018-01-30 23:44:49 +01:00
synapse_db_name="matrix_$app"
synapse_db_user="matrix_$app"
2021-02-18 09:16:37 +01:00
synapse_db_name="matrix_$app"
2018-01-30 23:44:49 +01:00
upstream_version=$(ynh_app_upstream_version)
2020-02-22 00:42:32 +01:00
final_www_path="/var/www/$app"
2020-12-07 16:34:41 +01:00
data_path="/home/yunohost.app/matrix-$app"
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# CHECK IF THE APP CAN BE RESTORED
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Validating restoration parameters..." --weight=2
2018-01-30 23:44:49 +01:00
2019-04-30 19:15:33 +02:00
test ! -d $final_path \
|| ynh_die --message="There is already a directory: $final_path "
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# STANDARD RESTORATION STEPS
2020-08-01 14:17:33 +02:00
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Reinstalling dependencies..." --weight=70
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
# For any update do it in all files
2022-10-28 10:50:43 +02:00
ynh_exec_warn_less ynh_install_app_dependencies $dependances
2020-08-01 14:17:33 +02:00
2021-03-07 11:42:53 +01:00
#=================================================
# RECREATE THE DEDICATED USER
#=================================================
ynh_script_progression --message="Recreating the dedicated system user..." --weight=3
# Create the dedicated user (if not existing)
2021-03-15 11:54:27 +01:00
ynh_system_user_create --username=$synapse_user --home_dir=$final_path
2022-05-26 17:27:30 +02:00
yunohost user create $synapse_user_app -f Synapse -l Application -d $domain -p "$synapse_user_app_pwd"
2021-03-07 11:42:53 +01:00
adduser $synapse_user ssl-cert
adduser turnserver ssl-cert
2019-04-30 19:15:33 +02:00
#=================================================
2019-06-01 21:37:12 +02:00
# RESTORE ALL CONFIG AND DATA
2018-01-30 23:44:49 +01:00
#=================================================
2019-06-01 21:37:12 +02:00
ynh_script_progression --message="Restoring directory and configuration..." --weight=10
ynh_restore
2020-09-20 13:58:22 +02:00
mkdir -p /etc/matrix-$app/app-service
2019-02-08 11:24:08 +01:00
2020-07-29 22:52:58 +02:00
# Check that the good python version is installed
# If not upgrade the source
2020-08-01 14:17:33 +02:00
ynh_script_progression --message="Check for source up to date..." --weight=5
2020-07-29 22:52:58 +02:00
install_sources
2019-04-30 19:15:33 +02:00
#=================================================
# RESTORE FAIL2BAN CONFIGURATION
#=================================================
2019-06-01 21:37:12 +02:00
ynh_script_progression --message="Reload fail2ban..." --weight=6
2019-04-30 19:15:33 +02:00
ynh_systemd_action --action=restart --service_name=fail2ban
2019-02-08 11:24:08 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# RESTORE THE POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the PostgreSQL database..." --weight=13
ynh_psql_test_if_first_run
ynh_psql_create_user $synapse_db_user $synapse_db_pwd
ynh_psql_execute_as_root \
--sql="CREATE DATABASE $synapse_db_name
ENCODING 'UTF8'
LC_COLLATE='C'
LC_CTYPE='C'
template=template0
OWNER $synapse_db_user;"
ynh_psql_execute_file_as_root --file="${YNH_CWD}/dump.sql" --database="$synapse_db_name"
#=================================================
# RESTORE SYSTEMD
#=================================================
2019-06-01 21:37:12 +02:00
ynh_script_progression --message="Enable systemd services" --weight=2
2019-04-30 19:15:33 +02:00
# systemctl daemon-reload
2020-12-12 15:04:21 +01:00
systemctl enable matrix-$app.service --quiet
systemctl enable coturn-$app.service --quiet
2019-04-30 19:15:33 +02:00
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
2020-07-24 00:34:52 +02:00
yunohost service add matrix-$app --log "/var/log/matrix-$app/homeserver.log" --needs_exposed_ports $synapse_tls_port
2020-08-23 10:03:10 +02:00
yunohost service add coturn-$app --needs_exposed_ports $turnserver_tls_port
2019-04-30 19:15:33 +02:00
#=================================================
# CREATE A DH FILE
#=================================================
ynh_script_progression --message="Creating a dh file..." --weight=40
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
# For any update do it in all files
# Make dh cert for synapse if it doesn't exist
if [ ! -e /etc/ssl/private/dh2048.pem ]
then
ynh_exec_warn_less openssl dhparam -out /etc/ssl/private/dh2048.pem -outform PEM -2 2048 -dsaparam
chown root:ssl-cert /etc/ssl/private/dh2048.pem
chmod 640 /etc/ssl/private/dh2048.pem
fi
2018-05-10 15:44:15 +02:00
#=================================================
# RECONFIGURE THE TURNSERVER
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Reconfiguring coturn..." --weight=23
2018-05-10 15:44:15 +02:00
2018-07-05 21:46:24 +02:00
# To be sure that at the restoration the IP address in coturn config is the same as the real address we remake the coturn config
2018-05-10 15:44:15 +02:00
2018-07-05 21:46:24 +02:00
# Retrieve specific settings
2019-04-30 19:15:33 +02:00
turnserver_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_tls_port)
turnserver_alt_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_alt_tls_port)
cli_port=$(ynh_app_setting_get --app=$app --key=cli_port)
2019-06-02 00:09:14 +02:00
turnserver_pwd=$(ynh_app_setting_get --app=$app --key=turnserver_pwd)
2018-05-10 15:44:15 +02:00
2018-07-05 21:46:24 +02:00
# WARNING : these commands are used in INSTALL, UPGRADE
2018-05-10 15:44:15 +02:00
# For any update do it in all files
# Get public IP and set as external IP for coturn
# note : '|| true' is used to ignore the errors if we can't get the public ipv4 or ipv6
2021-06-04 21:21:42 +02:00
public_ip4="$(curl -s ip.yunohost.org)" || true
public_ip6="$(curl -s ipv6.yunohost.org)" || true
2018-05-10 15:44:15 +02:00
2020-12-15 22:25:29 +01:00
turn_external_ip=""
2019-05-01 11:22:49 +02:00
if [ -n "$public_ip4" ] && ynh_validate_ip4 --ip_address="$public_ip4"
2018-05-10 15:44:15 +02:00
then
2021-03-07 11:42:53 +01:00
turn_external_ip+="\nexternal-ip=$public_ip4"
2018-05-10 15:44:15 +02:00
fi
2019-09-10 20:15:48 +02:00
if [ -n "$public_ip6" ] && ynh_validate_ip6 --ip_address="$public_ip6"
2018-05-10 15:44:15 +02:00
then
2021-03-07 11:42:53 +01:00
turn_external_ip+="\nexternal-ip=$public_ip6"
2018-05-10 15:44:15 +02:00
fi
2020-12-15 22:25:29 +01:00
ynh_add_config --template="turnserver.conf" --destination="/etc/matrix-$app/coturn.conf"
2018-05-10 15:44:15 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# OPEN THE PORT
#=================================================
# Ouvre le port dans le firewall
2019-04-30 19:15:33 +02:00
ynh_exec_warn_less yunohost firewall allow TCP $synapse_tls_port
ynh_exec_warn_less yunohost firewall allow Both $turnserver_tls_port
ynh_exec_warn_less yunohost firewall allow Both $turnserver_alt_tls_port
2018-05-10 20:28:26 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# SETUP LOGROTATE
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Configuring log rotation..."
2018-01-30 23:44:49 +01:00
2019-09-30 10:37:22 +02:00
ynh_use_logrotate --logfile /var/log/matrix-$app
2017-07-21 22:28:49 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2018-08-01 00:32:10 +02:00
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE (3 times)
2018-01-30 23:44:49 +01:00
# For any update do it in all files
2021-03-07 11:42:53 +01:00
ynh_script_progression --message="Configuring file permission..."
2018-01-30 23:44:49 +01:00
chown $synapse_user:root -R $final_path
2018-08-20 12:45:35 +02:00
chmod 770 $final_path/Coturn_config_rotate.sh
2020-07-24 23:33:53 +02:00
chmod 700 $final_path/update_synapse_for_appservice.sh
2020-12-07 16:34:41 +01:00
chown $synapse_user:root -R $data_path
2018-01-30 23:44:49 +01:00
chown $synapse_user:root -R /var/log/matrix-$app
chown $synapse_user:root -R /etc/matrix-$app
chmod u=rwX,g=rX,o= -R /etc/matrix-$app
2019-05-11 15:01:17 +02:00
chmod 600 /etc/matrix-$app/$server_name.signing.key
2018-01-30 23:44:49 +01:00
setfacl -R -m user:turnserver:rX /etc/matrix-$app
setfacl -R -m user:turnserver:rwX /var/log/matrix-$app
2020-02-22 00:42:32 +01:00
chmod u=rwX,g=rX,o= -R $final_www_path
chown $synapse_user:root -R $final_www_path
2018-01-30 23:44:49 +01:00
#=================================================
# RELOAD NGINX, SYNAPSE AND COTURN
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Restarting synapse services..." --weight=7
2018-01-30 23:44:49 +01:00
2019-04-30 19:15:33 +02:00
ynh_systemd_action --service_name=coturn-$app.service --action=restart
ynh_systemd_action --service_name=matrix-$app --action=restart --line_match="Synapse now listening on TCP port $synapse_tls_port" --log_path="/var/log/matrix-$app/homeserver.log" --timeout=300
2019-02-12 21:24:25 +01:00
2018-03-13 16:50:27 +01:00
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, RESTORE
2018-06-20 19:32:48 +02:00
# For any update do it in all files
2019-04-30 19:15:33 +02:00
echo "To federate this app you need to add this line in your DNS configuration:
2018-03-13 16:50:27 +01:00
_matrix._tcp.$domain. 3600 IN SRV 10 0 $synapse_tls_port $domain.
You also need to open the TCP port $synapse_tls_port on your ISP box if it's not automatically done.
2018-07-05 21:46:24 +02:00
Your synapse server also implements a turnserver (for VoIP), to have this fully functional please read the 'Turnserver' section in the README available here: https://github.com/YunoHost-Apps/synapse_ynh .
2018-03-13 16:50:27 +01:00
2019-04-30 19:15:33 +02:00
If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/synapse_ynh" > mail_to_send
ynh_send_readme_to_admin --app_message="mail_to_send" --type="restore"
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
2021-03-07 11:42:53 +01:00
ynh_systemd_action --service_name=php$phpversion-fpm --action=reload
2019-04-30 19:15:33 +02:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2018-03-13 16:50:27 +01:00
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Restoration completed for $app" --last