2021-09-11 16:08:56 +02:00
#!/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
#=================================================
2021-09-12 14:49:16 +02:00
# SET CONSTANTS
2021-09-11 16:08:56 +02:00
#=================================================
app=$YNH_APP_INSTANCE_NAME
2021-09-12 14:49:16 +02:00
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"
2021-09-12 20:03:12 +02:00
base_config_path="$final_path/base.config.yaml"
user_config_path="/etc/$app/user.config.yaml"
2021-09-12 14:49:16 +02:00
2021-09-11 16:08:56 +02:00
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Validating installation parameters..." --weight=1
2021-09-11 16:08:56 +02:00
2021-09-12 14:49:16 +02:00
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
2021-09-11 16:08:56 +02:00
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Storing installation settings..." --weight=1
2021-09-11 16:08:56 +02:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
2021-09-12 14:49:16 +02:00
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_app_setting_set --app=$app --key=synapse_instance --value=$synapse_instance
2021-09-11 16:08:56 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Finding an available port..." --weight=1
2021-09-11 16:08:56 +02:00
### 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
2021-09-12 14:49:16 +02:00
port=$(ynh_find_port --port=8434)
2021-09-11 16:08:56 +02:00
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Installing dependencies..." --weight=4
2021-09-11 16:08:56 +02:00
2021-09-12 14:49:16 +02:00
ynh_install_nodejs --nodejs_version=$NODEJS_VERSION
2021-09-11 16:08:56 +02:00
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE DEDICATED USER
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Configuring system user..." --weight=1
2021-09-11 16:08:56 +02:00
# Create a system user
ynh_system_user_create --username=$app --home_dir="$final_path"
#=================================================
2021-09-12 14:49:16 +02:00
# CREATE A POSTGRESQL DATABASE
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Creating a PostgresSql database..." --weight=10
2021-09-11 16:08:56 +02:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
2021-09-17 16:21:24 +02:00
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
2021-09-11 16:08:56 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Setting up source files..." --weight=1
2021-09-11 16:08:56 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
ynh_setup_source --dest_dir="$final_path"
2021-09-17 16:21:24 +02:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path"
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
# INSTALL NODE DEPENDENCIES
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Building Node dependencies..." --weight=30
2021-09-11 16:08:56 +02:00
2021-09-17 16:21:24 +02:00
pushd "$final_path"
ynh_use_nodejs
ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH npm install
popd
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 20:03:12 +02:00
# SETUP PIP (FOR YQ)
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 20:03:12 +02:00
pip_path=$final_path/.pip
python3 -m venv $pip_path
$pip_path/bin/pip3 install yq
#=================================================
# ADD CONFIGURATION FILES
#=================================================
ynh_script_progression --message="Adding configuration files..." --weight=1
etc_path=$(dirname $user_config_path)
ynh_app_setting_set --app=$app --key=etc_path --value=$etc_path
2021-09-17 16:21:24 +02:00
mkdir -p -m 750 "$etc_path"
chown "$app:$app" "$etc_path"
2021-09-11 16:08:56 +02:00
2021-09-12 20:43:14 +02:00
any_account_of_domain="@.*:${domain//\./\\\.}"
2021-09-12 20:03:12 +02:00
2021-09-12 14:49:16 +02:00
# TODO Add a way to override the config.yaml file
2021-09-12 20:03:12 +02:00
ynh_add_config --template="base.config.yaml" --destination="$base_config_path"
ynh_add_config --template="user.config.yaml" --destination="$user_config_path"
2021-09-11 16:08:56 +02:00
2021-09-12 20:43:14 +02:00
chmod 400 "$base_config_path"
chown $app:$app "$base_config_path"
chmod 600 "$user_config_path"
chown $app:$app "$user_config_path"
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
# STORE THE CONFIG FILE CHECKSUM
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml"
2021-09-12 20:43:14 +02:00
# Probably not worth anymore, because the overrides should
# be now in $user_config_path
# ynh_store_file_checksum --file="$base_config_path"
2021-09-11 16:08:56 +02:00
#=================================================
# SETUP SYSTEMD
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Configuring a systemd service..." --weight=1
2021-09-11 16:08:56 +02:00
2021-09-12 20:03:12 +02:00
# Create startup script
ynh_add_config --template="../sources/extra_files/app/run.sh" --destination="$final_path/run.sh"
2021-09-17 16:21:24 +02:00
chmod 750 "$final_path/run.sh"
chown "$app:$app" "$final_path/run.sh"
2021-09-12 20:03:12 +02:00
2021-09-11 16:08:56 +02:00
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
2021-09-12 14:49:16 +02:00
# INTEGRATE SERVICE IN YUNOHOST
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2021-09-11 16:08:56 +02:00
2021-09-12 14:49:16 +02:00
yunohost service add $app --description "$app daemon for bridging Discord and Matrix messages"
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
# CREATE LOG DIR
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
mkdir -p -m 700 "$log_path"
chown $app:$app "$log_path"
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
# REGISTER MODULE IN SYNAPSE
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Register module in Synapse" --weight=1
2021-09-11 16:08:56 +02:00
2021-09-17 16:21:24 +02:00
pushd "$final_path"
ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH NODE_ENV=production $final_path/run.sh -r -f "$app.yaml"
popd
2021-09-12 14:49:16 +02:00
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"
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
# SEND A README FOR THE ADMIN
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Sending a readme for the admin..."
2021-09-11 16:08:56 +02:00
2021-09-12 14:49:16 +02:00
# ynh_send_readme_to_admin --app_message="../conf/msg_install" --recipients=$admin_email --type='install'
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
# START SYSTEMD SERVICE
2021-09-11 16:08:56 +02:00
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Starting a systemd service..." --weight=1
2021-09-11 16:08:56 +02:00
2021-09-12 14:49:16 +02:00
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start"
2021-09-11 16:08:56 +02:00
#=================================================
# END OF SCRIPT
#=================================================
2021-09-12 14:49:16 +02:00
ynh_script_progression --message="Installation of $app completed" --last