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
#=================================================
2022-07-25 03:40:05 +02:00
ynh_clean_setup () {
2022-09-05 21:45:47 +02:00
true
2022-07-25 03:40:05 +02:00
}
2021-09-11 16:08:56 +02:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
2022-07-25 03:40:05 +02:00
# RETRIEVE ARGUMENTS FROM THE MANIFEST
2021-09-11 16:08:56 +02:00
#=================================================
2022-07-25 03:40:05 +02:00
synapsenumber=$YNH_APP_ARG_SYNAPSENUMBER
2021-09-11 16:08:56 +02:00
app=$YNH_APP_INSTANCE_NAME
2022-07-25 03:40:05 +02:00
2021-09-12 14:49:16 +02:00
final_path=/opt/yunohost/$app
if [ $synapsenumber -eq "1" ]
then
2022-07-25 03:40:05 +02:00
synapse_instance="synapse"
2021-09-12 14:49:16 +02:00
else
2022-07-25 03:40:05 +02:00
synapse_instance="synapse__$synapsenumber"
2021-09-12 14:49:16 +02:00
fi
synapse_config_path="/etc/matrix-$synapse_instance"
# Check Synapse is installed or die early
if [ ! -d $synapse_config_path ]
then
2022-07-25 03:40:05 +02:00
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)
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
# 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
#=================================================
2022-09-05 21:45:47 +02:00
ynh_script_progression --message="Creating a PostgreSQL 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
2022-09-05 21:45:47 +02:00
ynh_psql_test_if_first_run
2021-09-17 16:21:24 +02:00
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
2022-09-05 21:45:47 +02:00
db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
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
2022-07-25 03:40:05 +02:00
# Download, check integrity, uncompress and patch the source from app.src
2021-09-11 16:08:56 +02:00
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
2022-09-05 21:45:47 +02:00
#=================================================
# SPECIFIC SETUP
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"
2022-07-25 03:40:05 +02:00
ynh_use_nodejs
ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' npm install
2021-09-17 16:21:24 +02:00
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
#=================================================
2022-05-29 22:17:59 +02:00
ynh_script_progression --message="Install yq..." --weight=1
2022-07-25 03:40:05 +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
#=================================================
# 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
2022-07-25 03:40:05 +02:00
ynh_add_config --template="../conf/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
# 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"
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"
2022-07-25 03:40:05 +02:00
ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH NODE_ENV=production $final_path/run.sh -r -f "$app.yaml"
2021-09-17 16:21:24 +02:00
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 \
2022-07-25 03:40:05 +02:00
|| 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
#=================================================
2022-07-25 03:40:05 +02:00
# GENERIC FINALIZATION
2021-09-11 16:08:56 +02:00
#=================================================
2022-07-25 03:40:05 +02:00
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2021-09-11 16:08:56 +02:00
2022-09-05 21:45:47 +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
# 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
2022-07-25 03:40:05 +02:00
#=================================================
# 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'
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