1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/synapse_ynh.git synced 2024-09-03 20:26:38 +02:00
synapse_ynh/scripts/install

252 lines
9.8 KiB
Text
Raw Normal View History

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
source /usr/share/yunohost/helpers
2018-01-30 23:44:49 +01:00
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
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"
fi
2018-01-30 23:44:49 +01:00
#=================================================
## SET STANDARD SETTINGS FROM DEFAULT CONFIG
2018-01-30 23:44:49 +01:00
#=================================================
2017-02-13 20:43:41 +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
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
#=================================================
# Check datadir empty
#=================================================
2024-04-27 01:21:40 +02:00
if [ -n "$(ls -A "$data_dir")" ]; then
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"
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
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
adduser turnserver ssl-cert
2017-02-13 20:43:41 +01:00
2023-11-01 19:25:19 +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';"
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
install_sources
2018-01-30 23:44:49 +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"
#=================================================
2019-02-08 11:24:08 +01:00
# CREATE SYNAPSE CONFIG
#=================================================
ynh_script_progression --message="Creating Synapse config..." --weight=3
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-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)"
# 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-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
# Create systemd service for synapse and turnserver
2024-04-27 01:21:40 +02:00
ynh_add_systemd_config --service="$app" --template=synapse.service
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
ynh_add_systemd_config --service=$app-sliding-proxy --template=synapse-sliding-proxy.service
2018-01-30 23:44:49 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..." --weight=2
2018-01-30 23:44:49 +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
configure_nginx
2018-01-30 23:44:49 +01:00
#=================================================
# SET SYNAPSE CONFIG
#=================================================
ynh_script_progression --message="Configuring Synapse..." --weight=2
2018-01-30 23:44:49 +01: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"
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
#=================================================
ynh_script_progression --message="Configuring Coturn..." --weight=1
2018-01-30 23:44:49 +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
ynh_use_logrotate --logfile="/var/log/matrix-$app"
2017-02-13 20:43:41 +01:00
#=================================================
2020-07-24 23:33:53 +02:00
# ADD SCRIPT FOR COTURN CRON AND APP SERVICE
#=================================================
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
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-01-30 23:44:49 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP PERMISSIONS
2018-01-30 23:44:49 +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
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
#=================================================
# 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
2018-01-30 23:44:49 +01:00
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
ynh_script_progression --message="Protecting directories..." --weight=3
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.'
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
#=================================================
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
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
#=================================================
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