matrix-puppet-discord_ynh/scripts/install

211 lines
7.5 KiB
Bash
Executable file

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# SET CONSTANTS
#=================================================
app=$YNH_APP_INSTANCE_NAME
final_path=/opt/yunohost/$app
synapsenumber=$YNH_APP_ARG_SYNAPSENUMBER
if [ $synapsenumber -eq "1" ]
then
synapse_instance="synapse"
else
synapse_instance="synapse__$synapsenumber"
fi
synapse_config_path="/etc/matrix-$synapse_instance"
# Check Synapse is installed or die early
if [ ! -d $synapse_config_path ]
then
ynh_die "Could not find $synapse_config_path config directory. Ensure that you installed Matrix Synapse first and that you entered a correct \"synapse instance number\""
fi
server_name=$(ynh_app_setting_get --app $synapse_instance --key server_name)
domain=$(ynh_app_setting_get --app $synapse_instance --key domain)
app_service_registration_path="/etc/matrix-$synapse_instance/app-service"
log_path="/var/log/$app"
config_path="$final_path/config.yaml"
ynh_print_OFF
db_password=$(ynh_string_random --length=30)
ynh_print_ON
#=================================================
# 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"
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=1
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=log_path --value=$log_path
ynh_app_setting_set --app=$app --key=app_service_registration_path --value=$app_service_registration_path
ynh_print_OFF
ynh_app_setting_set --app=$app --key=db_password --value=$db_password
ynh_print_ON
ynh_app_setting_set --app=$app --key=synapse_instance --value=$synapse_instance
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Finding an available port..." --weight=1
### Use these lines if you have to open a port for the application
### `ynh_find_port` will find the first available port starting from the given port.
### If you're not using these lines:
### - Remove the section "CLOSE A PORT" in the remove script
# Find an available port
port=$(ynh_find_port --port=8434)
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=4
ynh_install_nodejs --nodejs_version=$NODEJS_VERSION
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=1
# Create a system user
ynh_system_user_create --username=$app --home_dir="$final_path"
#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Creating a PostgresSql database..." --weight=10
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_password
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=1
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
ynh_setup_source --dest_dir="$final_path"
setup_final_path_rights
#=================================================
# INSTALL NODE DEPENDENCIES
#=================================================
ynh_script_progression --message="Building Node dependencies..." --weight=30
install_node_deps
#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=1
any_account_of_domain="@.*:${domain//\./\\\\\.}"
# TODO Add a way to override the config.yaml file
ynh_add_config --template="base.config.yaml" --destination="$config_path"
chmod 400 "$config_path"
chown $app:$app "$config_path"
#=================================================
# STORE THE CONFIG FILE CHECKSUM
#=================================================
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml"
ynh_store_file_checksum --file="$config_path"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=1
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
yunohost service add $app --description "$app daemon for bridging Discord and Matrix messages"
#=================================================
# CREATE LOG DIR
#=================================================
mkdir -p -m 700 "$log_path"
chown $app:$app "$log_path"
#=================================================
# REGISTER MODULE IN SYNAPSE
#=================================================
ynh_script_progression --message="Register module in Synapse" --weight=1
run_registration
cp "$final_path/$app.yaml" $app_service_registration_path/$app.yaml
/opt/yunohost/matrix-$synapse_instance/update_synapse_for_appservice.sh \
|| ynh_die "Synapse can't restart with the appservice configuration"
#=================================================
# STORE THE CONFIG FILE CHECKSUM
#=================================================
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml"
ynh_store_file_checksum --file="$config_path"
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
ynh_script_progression --message="Sending a readme for the admin..."
# ynh_send_readme_to_admin --app_message="../conf/msg_install" --recipients=$admin_email --type='install'
#=================================================
# 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"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last