mirror of
https://github.com/YunoHost-Apps/synapse_ynh.git
synced 2024-09-03 20:26:38 +02:00
242 lines
9.1 KiB
Bash
242 lines
9.1 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
source experimental_helper.sh
|
|
source /usr/share/yunohost/helpers
|
|
|
|
ynh_app_setting_set --key=php_upload_max_filesize --value=100M
|
|
|
|
#=================================================
|
|
# RETRIEVE ARGUMENTS FROM THE MANIFEST
|
|
#=================================================
|
|
|
|
if [ "$server_name" == "Same than the domain" ]; then
|
|
server_name="$domain"
|
|
ynh_app_setting_set --key=server_name --value="$server_name"
|
|
fi
|
|
|
|
#=================================================
|
|
## SET STANDARD SETTINGS FROM DEFAULT CONFIG
|
|
#=================================================
|
|
|
|
ynh_script_progression "Storing installation settings..."
|
|
|
|
ensure_vars_set
|
|
|
|
if [ "$is_free_registration" -eq 0 ]
|
|
then
|
|
enable_registration=false
|
|
password_enabled=false
|
|
else
|
|
enable_registration=true
|
|
password_enabled=true
|
|
fi
|
|
|
|
ynh_app_setting_set --key=password_enabled --value="$password_enabled"
|
|
ynh_app_setting_set --key=enable_registration --value="$enable_registration"
|
|
|
|
#=================================================
|
|
# Check datadir empty
|
|
#=================================================
|
|
|
|
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"
|
|
mkdir -p "$old_data_dir_path"
|
|
mv -t "$old_data_dir_path" "$data_dir"/*
|
|
fi
|
|
|
|
#=================================================
|
|
# CREATE A DH FILE
|
|
#=================================================
|
|
ynh_script_progression "Creating a dh file..."
|
|
|
|
# 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_hide_warnings openssl dhparam -out /etc/ssl/private/dh2048.pem -outform PEM -dsaparam 2048
|
|
chown root:ssl-cert /etc/ssl/private/dh2048.pem
|
|
chmod 640 /etc/ssl/private/dh2048.pem
|
|
fi
|
|
|
|
#=================================================
|
|
# CREATE DEDICATED USER
|
|
#=================================================
|
|
ynh_script_progression 'Configuring system groups'
|
|
|
|
adduser "$app" ssl-cert
|
|
adduser turnserver ssl-cert
|
|
|
|
#=================================================
|
|
# FIX DB CONFIG
|
|
#=================================================
|
|
ynh_script_progression "Fixing database type..."
|
|
|
|
ynh_psql_db_shell \
|
|
<<< "update pg_database set datcollate='C', datctype='C' where datname='$db_name';"
|
|
|
|
#=================================================
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
#=================================================
|
|
ynh_script_progression "Setting up source files..."
|
|
|
|
# Create empty dir for synapse
|
|
|
|
# WARNING : theses command are used in INSTALL, UPGRADE
|
|
# For any update do it in all files
|
|
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
|
|
|
|
# Install synapse in virtualenv
|
|
install_sources
|
|
|
|
#=================================================
|
|
# CREATE SMALL CAS SERVER
|
|
#=================================================
|
|
# WARNING : theses command are used in INSTALL, UPGRADE
|
|
# For any update do it in all files
|
|
|
|
cp ../sources/cas_server.php "$install_dir"/
|
|
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod u=rwX,g=rX,o= -R "$install_dir"
|
|
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown "$app:$app" -R "$install_dir"
|
|
#=================================================
|
|
# CREATE SYNAPSE CONFIG
|
|
#=================================================
|
|
|
|
ynh_script_progression "Creating Synapse config..."
|
|
|
|
# Generate config
|
|
"$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
|
|
|
|
# Get random values from config
|
|
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
|
|
ynh_app_setting_set --key=registration_shared_secret --value="$registration_shared_secret"
|
|
ynh_app_setting_set --key=form_secret --value="$form_secret"
|
|
ynh_app_setting_set --key=macaroon_secret_key --value="$macaroon_secret_key"
|
|
|
|
#=================================================
|
|
# SETUP SYSTEMD
|
|
#=================================================
|
|
ynh_script_progression "Configuring $app's systemd service..."
|
|
|
|
# Create systemd service for synapse and turnserver
|
|
ynh_config_add_systemd --template=synapse.service
|
|
|
|
cp ../conf/default_coturn /etc/matrix-"$app"/coturn_env
|
|
ynh_config_add_systemd --service="$app"-coturn --template=synapse-coturn.service
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression "Configuring NGINX web server..."
|
|
|
|
# Create a dedicated php-fpm config
|
|
ynh_script_progression "Configuring application..."
|
|
|
|
ynh_config_add_phpfpm
|
|
|
|
configure_nginx
|
|
|
|
#=================================================
|
|
# SET SYNAPSE CONFIG
|
|
#=================================================
|
|
ynh_script_progression "Configuring Synapse..."
|
|
|
|
ynh_config_add --jinja --template="homeserver.yaml" --destination="/etc/matrix-$app/homeserver.yaml"
|
|
ynh_config_add --template="log.yaml" --destination="/etc/matrix-$app/log.yaml"
|
|
|
|
#=================================================
|
|
# SET COTURN CONFIG
|
|
#=================================================
|
|
ynh_script_progression "Configuring Coturn..."
|
|
|
|
configure_coturn
|
|
|
|
#=================================================
|
|
# SETUP LOGROTATE
|
|
#=================================================
|
|
ynh_script_progression "Configuring log rotation..."
|
|
|
|
ynh_config_add_logrotate "/var/log/matrix-$app"
|
|
|
|
#=================================================
|
|
# ADD SCRIPT FOR COTURN CRON AND APP SERVICE
|
|
#=================================================
|
|
|
|
# WARNING : theses command are used in INSTALL, UPGRADE
|
|
# For any update do it in all files
|
|
|
|
ynh_config_add --template="../sources/Coturn_config_rotate.sh" --destination="$code_dir/Coturn_config_rotate.sh"
|
|
ynh_config_add --template="../sources/update_synapse_for_appservice.sh" --destination="$code_dir/update_synapse_for_appservice.sh"
|
|
ynh_config_add --template=../sources/set_admin_user.sh --destination="$code_dir"/set_admin_user.sh
|
|
|
|
#=================================================
|
|
# SETUP PERMISSIONS
|
|
#=================================================
|
|
|
|
ynh_script_progression "Configuring permissions..."
|
|
|
|
if yunohost --output-as plain domain list | grep -q "^$server_name$"; then
|
|
ynh_""permission_create --permission=server_client_infos --url="$server_name"/.well-known/matrix \
|
|
--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"
|
|
fi
|
|
|
|
#=================================================
|
|
# UPDATE HOOKS
|
|
#=================================================
|
|
|
|
# WARNING : theses command are used in INSTALL, UPGRADE
|
|
# For any update do it in all files
|
|
ynh_replace --match=__APP__ --replace="$app" --file=../hooks/post_cert_update
|
|
ynh_replace --match=__DOMAIN__ --replace="$domain" --file=../hooks/post_cert_update
|
|
|
|
#=================================================
|
|
# SECURE FILES AND DIRECTORIES
|
|
#=================================================
|
|
|
|
ynh_script_progression "Protecting directories..."
|
|
set_permissions data
|
|
|
|
#=================================================
|
|
# ADVERTISE SERVICE IN ADMIN PANEL
|
|
#=================================================
|
|
|
|
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.'
|
|
|
|
#=================================================
|
|
# RELOAD SERVICES
|
|
#=================================================
|
|
ynh_script_progression "Restarting Synapse services..."
|
|
|
|
ynh_systemctl --service="$app"-coturn.service --action=restart
|
|
ynh_systemctl --service="$app".service --action=restart --wait_until="Synapse now listening on TCP port $port_synapse_tls" --log_path="/var/log/matrix-$app/homeserver.log" --timeout=300
|
|
|
|
#=================================================
|
|
# SETUP FAIL2BAN
|
|
#=================================================
|
|
ynh_script_progression "Configuring Fail2Ban..."
|
|
|
|
ynh_config_add_fail2ban
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression "Installation of $app completed"
|