2017-02-13 20:43:41 +01:00
#!/bin/bash
2018-01-30 23:44:49 +01:00
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2019-04-30 19:15:33 +02:00
source _common.sh
source experimental_helper.sh
2017-07-21 22:28:49 +02:00
source /usr/share/yunohost/helpers
2018-01-30 23:44:49 +01:00
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
2023-11-01 18:52:07 +01:00
if [ "$server_name" == "Same than the domain" ]; then
2024-04-27 01:21:40 +02:00
server_name="$domain"
ynh_app_setting_set --app="$app" --key=server_name --value="$server_name"
2019-10-30 21:15:22 +01:00
fi
2018-01-30 23:44:49 +01:00
#=================================================
2023-11-01 18:52:07 +01:00
## SET STANDARD SETTINGS FROM DEFAULT CONFIG
2018-01-30 23:44:49 +01:00
#=================================================
2017-02-13 20:43:41 +01:00
2023-11-01 18:52:07 +01:00
ynh_script_progression --message="Storing installation settings..." --weight=1
2020-12-07 16:34:41 +01:00
2024-04-05 01:03:01 +02:00
ensure_vars_set
2018-01-30 23:44:49 +01:00
2023-11-01 18:52:07 +01:00
if [ "$is_free_registration" -eq 0 ]
2023-01-11 17:11:43 +01:00
then
2024-04-05 01:03:01 +02:00
enable_registration=false
password_enabled=false
2023-01-11 17:11:43 +01:00
else
2024-04-05 01:03:01 +02:00
enable_registration=true
password_enabled=true
2023-01-11 17:11:43 +01:00
fi
2024-04-05 01:03:01 +02:00
ynh_app_setting_set --app="$app" --key=password_enabled --value="$password_enabled"
ynh_app_setting_set --app="$app" --key=enable_registration --value="$enable_registration"
2023-01-06 14:38:14 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
# STANDARD MODIFICATIONS
2024-02-20 21:18:29 +01:00
#=================================================
# Check datadir empty
#=================================================
2024-04-27 01:21:40 +02:00
if [ -n "$(ls -A "$data_dir")" ]; then
2024-02-20 21:18:29 +01:00
old_data_dir_path="${data_dir}_$(date '+%Y%m%d.%H%M%S')"
ynh_print_warn "Data directory was not empty. Data was moved to $old_data_dir_path"
2024-04-27 01:21:40 +02:00
mkdir -p "$old_data_dir_path"
2024-02-20 21:18:29 +01:00
mv -t "$old_data_dir_path" "$data_dir"/*
fi
2019-02-08 11:24:08 +01:00
#=================================================
# CREATE A DH FILE
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Creating a dh file..." --weight=3
2019-02-08 11:24:08 +01:00
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
# For any update do it in all files
2019-04-30 19:15:33 +02:00
# Make dh cert for synapse if it doesn't exist
if [ ! -e /etc/ssl/private/dh2048.pem ]
2019-02-08 11:24:08 +01:00
then
2024-04-10 23:55:58 +02:00
ynh_exec_warn_less openssl dhparam -out /etc/ssl/private/dh2048.pem -outform PEM -dsaparam 2048
2019-02-08 11:24:08 +01:00
chown root:ssl-cert /etc/ssl/private/dh2048.pem
chmod 640 /etc/ssl/private/dh2048.pem
fi
2018-01-30 23:44:49 +01:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2024-02-06 21:04:05 +01:00
ynh_script_progression --message='Configuring system groups'
2018-01-30 23:44:49 +01:00
2024-04-27 01:21:40 +02:00
adduser "$app" ssl-cert
2023-11-01 18:52:07 +01:00
adduser turnserver ssl-cert
2017-02-13 20:43:41 +01:00
2023-11-01 19:25:19 +01:00
#=================================================
2024-02-26 21:20:00 +01:00
# FIX DB CONFIG AND CREATE SYLING PROXY DB
2023-11-01 19:25:19 +01:00
#=================================================
ynh_script_progression --message="Fixing database type..." --weight=1
ynh_psql_execute_as_root \
--sql="update pg_database set datcollate='C', datctype='C' where datname='$db_name';"
2024-02-26 21:20:00 +01:00
ynh_''psql_setup_db --db_user=$db_user --db_pwd=$db_pwd --db_name=$db_name_slidingproxy
2023-11-01 19:25:19 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Setting up source files..." --weight=50
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
# Create empty dir for synapse
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, UPGRADE
2018-01-30 23:44:49 +01:00
# For any update do it in all files
2024-04-27 01:21:40 +02:00
mkdir -p /var/log/matrix-"$app"
mkdir -p /etc/matrix-"$app"/conf.d
mkdir -p /etc/matrix-"$app"/app-service
touch /etc/matrix-"$app"/conf.d/app_service.yaml
2018-01-30 23:44:49 +01:00
# Install synapse in virtualenv
2020-07-29 22:52:58 +02:00
install_sources
2018-01-30 23:44:49 +01:00
2020-02-22 00:42:32 +01:00
#=================================================
# CREATE SMALL CAS SERVER
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
2024-04-27 01:21:40 +02:00
cp ../sources/cas_server.php "$install_dir"/
chmod u=rwX,g=rX,o= -R "$install_dir"
chown "$app:$app" -R "$install_dir"
2020-02-22 00:42:32 +01:00
2018-08-01 00:32:10 +02:00
#=================================================
2019-02-08 11:24:08 +01:00
# CREATE SYNAPSE CONFIG
2018-08-01 00:32:10 +02:00
#=================================================
2020-02-22 00:42:32 +01:00
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Creating Synapse config..." --weight=3
2018-08-01 00:32:10 +02:00
2019-02-08 11:24:08 +01:00
# Generate config
2024-04-27 01:21:40 +02:00
$code_dir/bin/python -m synapse.app.homeserver --keys-directory /etc/matrix-"$app"/ --generate-config --server-name "$server_name" --report-stats=no -c homeserver.yml
2018-08-01 00:32:10 +02:00
2018-08-21 07:27:01 +02:00
# Get random values from config
2024-04-27 01:21:40 +02:00
registration_shared_secret="$(grep -E "^registration_shared_secret:" homeserver.yml | cut -d'"' -f2)"
form_secret="$(grep -E "^form_secret:" homeserver.yml | cut -d'"' -f2)"
macaroon_secret_key="$(grep -E "^macaroon_secret_key:" homeserver.yml | cut -d'"' -f2)"
2018-08-01 00:32:10 +02:00
# store in yunohost settings
2024-04-27 01:21:40 +02:00
ynh_app_setting_set --app="$app" --key=registration_shared_secret --value="$registration_shared_secret"
ynh_app_setting_set --app="$app" --key=form_secret --value="$form_secret"
ynh_app_setting_set --app="$app" --key=macaroon_secret_key --value="$macaroon_secret_key"
2018-08-01 00:32:10 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Configuring a systemd service..." --weight=2
2017-02-16 22:54:57 +01:00
2018-01-19 22:05:39 +01:00
# Create systemd service for synapse and turnserver
2024-04-27 01:21:40 +02:00
ynh_add_systemd_config --service="$app" --template=synapse.service
2018-01-19 22:05:39 +01:00
2024-04-27 01:21:40 +02:00
cp ../conf/default_coturn /etc/matrix-"$app"/coturn_env
ynh_add_systemd_config --service="$app"-coturn --template=synapse-coturn.service
2017-02-13 20:43:41 +01:00
2024-02-26 21:20:00 +01:00
ynh_add_systemd_config --service=$app-sliding-proxy --template=synapse-sliding-proxy.service
2018-01-30 23:44:49 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring NGINX web server..." --weight=2
2018-01-30 23:44:49 +01:00
2020-02-22 00:42:32 +01:00
# Create a dedicated php-fpm config
ynh_script_progression --message="Configuring application..."
2023-08-26 12:07:09 +02:00
ynh_add_fpm_config --usage=low --footprint=low
2020-02-22 00:42:32 +01:00
2023-11-01 18:52:07 +01:00
configure_nginx
2020-05-07 14:41:35 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# SET SYNAPSE CONFIG
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring Synapse..." --weight=2
2018-01-30 23:44:49 +01:00
2024-04-05 01:02:47 +02:00
ynh_add_jinja_config --template="homeserver.yaml" --destination="/etc/matrix-$app/homeserver.yaml"
ynh_add_config --template="log.yaml" --destination="/etc/matrix-$app/log.yaml"
2024-02-26 21:20:00 +01:00
ynh_add_config --template=sliding_proxy.conf --destination=/etc/matrix-$app/sliding_proxy.conf
2018-01-30 23:44:49 +01:00
#=================================================
# SET COTURN CONFIG
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring Coturn..." --weight=1
2018-01-30 23:44:49 +01:00
2023-11-01 18:52:07 +01:00
configure_coturn
2018-01-30 23:44:49 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Configuring log rotation..." --weight=2
2017-02-13 20:43:41 +01:00
2024-04-26 21:57:26 +02:00
ynh_use_logrotate --logfile="/var/log/matrix-$app"
2017-02-13 20:43:41 +01:00
2018-06-20 19:16:01 +02:00
#=================================================
2020-07-24 23:33:53 +02:00
# ADD SCRIPT FOR COTURN CRON AND APP SERVICE
2018-06-20 19:16:01 +02:00
#=================================================
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, UPGRADE
2018-06-20 19:16:01 +02:00
# For any update do it in all files
2023-11-01 18:52:07 +01:00
ynh_add_config --template="../sources/Coturn_config_rotate.sh" --destination="$code_dir/Coturn_config_rotate.sh"
ynh_add_config --template="../sources/update_synapse_for_appservice.sh" --destination="$code_dir/update_synapse_for_appservice.sh"
2024-04-27 01:21:40 +02:00
ynh_add_config --template=../sources/set_admin_user.sh --destination="$code_dir"/set_admin_user.sh
2018-06-20 19:16:01 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2020-12-15 22:25:29 +01:00
# SETUP PERMISSIONS
2018-01-30 23:44:49 +01:00
#=================================================
2020-12-15 22:25:29 +01:00
2023-10-31 22:55:01 +01:00
ynh_script_progression --message="Configuring permissions..." --weight=1
2018-01-30 23:44:49 +01:00
2021-05-09 15:25:02 +02:00
if yunohost --output-as plain domain list | grep -q "^$server_name$"; then
2024-04-27 01:21:40 +02:00
ynh_""permission_create --permission=server_client_infos --url="$server_name"/.well-known/matrix \
2021-05-09 15:25:02 +02:00
--label="Server info for clients. (well-known)" --show_tile=false --allowed=visitors \
--auth_header=false --protected=true
2024-02-20 21:18:09 +01:00
else
ynh_print_warn "Note yunohost won't be able to manage the required config for $server_name. So please add the needed DNS config as described on the documentation"
2021-05-09 15:25:02 +02:00
fi
2022-09-15 13:55:46 +02:00
2019-09-03 15:26:09 +02:00
#=================================================
# UPDATE HOOKS
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
2024-04-27 01:21:40 +02:00
ynh_replace_string __APP__ "$app" ../hooks/post_cert_update
ynh_replace_string __DOMAIN__ "$domain" ../hooks/post_cert_update
2019-09-03 15:26:09 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2023-11-01 18:52:07 +01:00
ynh_script_progression --message="Protecting directories..." --weight=3
2024-02-29 08:12:20 +01:00
set_permissions data
2018-01-30 23:44:49 +01:00
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
2018-01-13 01:07:17 +01:00
2024-04-27 01:21:40 +02:00
yunohost service add "$app" --log "/var/log/matrix-$app/homeserver.log" --needs_exposed_ports "$port_synapse_tls" --description 'Main matrix server service.'
yunohost service add "$app"-coturn --needs_exposed_ports "$port_turnserver_tls" --description 'Turn server for matrix server. Used for audio and video call.'
2024-02-26 21:20:00 +01:00
yunohost service add $app-sliding-proxy --description 'Matrix v2 service for clients.'
2017-05-26 15:29:12 +02:00
2018-01-30 23:44:49 +01:00
#=================================================
# RELOAD SERVICES
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Restarting Synapse services..." --weight=11
2018-01-30 23:44:49 +01:00
2024-04-27 01:21:40 +02:00
ynh_systemd_action --service_name="$app"-coturn.service --action=restart
ynh_systemd_action --service_name="$app".service --action=restart --line_match="Synapse now listening on TCP port $port_synapse_tls" --log_path="/var/log/matrix-$app/homeserver.log" --timeout=300
2024-02-26 21:20:00 +01:00
ynh_systemd_action --service_name=$app-sliding-proxy.service --action=restart
2018-03-13 16:50:27 +01:00
#=================================================
2019-02-12 21:24:25 +01:00
# SETUP FAIL2BAN
#=================================================
2022-10-28 10:59:40 +02:00
ynh_script_progression --message="Configuring Fail2Ban..." --weight=10
2019-02-12 21:24:25 +01:00
2019-04-30 19:15:33 +02:00
ynh_add_fail2ban_config --use_template
2019-02-12 21:24:25 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# END OF SCRIPT
#=================================================
2018-03-13 16:50:27 +01:00
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Installation of $app completed" --last