2021-09-11 16:08:56 +02:00
#!/bin/bash
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
2024-03-29 15:26:17 +01:00
# INITIALIZE AND STORE SETTINGS
2021-09-11 16:08:56 +02:00
#=================================================
2024-03-29 15:26:17 +01:00
if [ $synapsenumber -eq "1" ]; then
synapse_instance="synapse"
2021-09-12 14:49:16 +02:00
else
2024-03-29 15:26:17 +01:00
synapse_instance="synapse__$synapsenumber"
2021-09-12 14:49:16 +02:00
fi
synapse_config_path="/etc/matrix-$synapse_instance"
2024-03-29 15:26:17 +01:00
2021-09-12 14:49:16 +02:00
# Check Synapse is installed or die early
2024-03-29 15:26:17 +01:00
if [ ! -d "$synapse_config_path" ]; then
ynh_die --message="Could not find $synapse_config_path config directory. Ensure that you installed Matrix Synapse first and that you entered a correct \"synapse instance number\""
2021-09-12 14:49:16 +02:00
fi
server_name=$(ynh_app_setting_get --app $synapse_instance --key server_name)
domain=$(ynh_app_setting_get --app $synapse_instance --key domain)
2024-03-29 15:26:17 +01:00
2021-09-12 14:49:16 +02:00
app_service_registration_path="/etc/matrix-$synapse_instance/app-service"
log_path="/var/log/$app"
2024-03-29 15:26:17 +01:00
base_config_path="$install_dir/base.config.yaml"
2021-09-12 20:03:12 +02:00
user_config_path="/etc/$app/user.config.yaml"
2021-09-12 14:49:16 +02:00
2021-09-11 16:08:56 +02:00
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2024-03-29 15:26:17 +01:00
ynh_script_progression --message="Installing NodeJS..." --weight=4
2021-09-11 16:08:56 +02:00
2024-03-29 15:26:17 +01:00
ynh_install_nodejs --nodejs_version="$NODEJS_VERSION"
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
2022-07-25 03:40:05 +02:00
# Download, check integrity, uncompress and patch the source from app.src
2024-03-29 15:26:17 +01:00
ynh_setup_source --dest_dir="$install_dir/source"
2021-09-11 16:08:56 +02:00
2024-03-29 15:26:17 +01:00
chmod -R o-rwx "$install_dir"
chown -R "$app:$app" "$install_dir"
2021-09-12 20:03:12 +02:00
#=================================================
# ADD CONFIGURATION FILES
#=================================================
ynh_script_progression --message="Adding configuration files..." --weight=1
2024-03-29 15:26:17 +01:00
# Create startup script
ynh_add_config --template="run.sh" --destination="$install_dir/run.sh"
chmod 750 "$install_dir/run.sh"
# TODO Add a way to override the config.yaml file
ynh_add_config --template="base.config.yaml" --destination="$install_dir/base.config.yaml"
chmod 400 "$install_dir/base.config.yaml"
chown "$app:$app" "$install_dir/base.config.yaml"
2021-09-12 20:03:12 +02:00
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
ynh_add_config --template="user.config.yaml" --destination="$user_config_path"
2021-09-12 20:43:14 +02:00
chmod 600 "$user_config_path"
2024-03-29 15:26:17 +01:00
chown "$app:$app" "$user_config_path"
2021-09-11 16:08:56 +02:00
#=================================================
2024-03-29 15:26:17 +01:00
# INSTALL NODE DEPENDENCIES
2021-09-11 16:08:56 +02:00
#=================================================
2024-03-29 15:26:17 +01:00
ynh_script_progression --message="Building Node dependencies..." --weight=30
2021-09-11 16:08:56 +02:00
2024-03-29 15:26:17 +01:00
pushd "$install_dir/source"
ynh_use_nodejs
ynh_exec_warn_less ynh_exec_as "$app" env "$ynh_node_load_PATH" npx yarn install
popd
2021-09-17 16:21:24 +02:00
2024-03-29 15:26:17 +01:00
#=================================================
# SETUP PIP (FOR YQ)
#=================================================
ynh_script_progression --message="Install yq..." --weight=1
2021-09-12 20:03:12 +02:00
2024-03-29 15:26:17 +01:00
_venv_install
"$venvpy" -m pip install yq
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
#=================================================
2022-07-25 03:40:05 +02:00
2021-09-12 14:49:16 +02:00
mkdir -p -m 700 "$log_path"
2024-03-29 15:26:17 +01:00
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
2024-03-29 15:26:17 +01:00
pushd "$install_dir/source"
ynh_exec_warn_less ynh_exec_as "$app" env "$ynh_node_load_PATH" NODE_ENV=production \
"$install_dir/run.sh" -r -f "$install_dir/$app.yaml"
2021-09-17 16:21:24 +02:00
popd
2024-03-29 15:26:17 +01:00
cp "$install_dir/$app.yaml" "$app_service_registration_path/$app.yaml"
"/opt/yunohost/matrix-$synapse_instance/update_synapse_for_appservice.sh" \
|| ynh_die --message="Synapse can't restart with the appservice configuration"
2021-09-17 22:02:27 +02:00
ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml"
2021-09-11 16:08:56 +02:00
#=================================================
2024-03-29 15:26:17 +01:00
# SYSTEM CONFIGURATION
2022-07-25 03:40:05 +02:00
#=================================================
2024-03-29 15:26:17 +01:00
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1
2021-09-11 16:08:56 +02:00
2024-03-29 15:26:17 +01:00
# Create a dedicated systemd config
ynh_add_systemd_config
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
# START SYSTEMD SERVICE
2021-09-11 16:08:56 +02:00
#=================================================
2024-03-29 15:26:17 +01:00
ynh_script_progression --message="Starting $app's systemd service..." --weight=1
2021-09-11 16:08:56 +02:00
2021-09-12 14:49:16 +02:00
# Start a systemd service
2024-03-29 15:26:17 +01:00
ynh_systemd_action --service_name="$app" --action="start"
2022-07-25 03:40:05 +02:00
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