2017-10-26 13:01:33 +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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# RETRIEVE ARGUMENTS FROM THE MANIFEST
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
username=$YNH_APP_ARG_USERNAME
|
|
|
|
password=$YNH_APP_ARG_PASSWORD
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STORE SETTINGS FROM MANIFEST
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Storing installation settings..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=username --value=$username
|
|
|
|
ynh_app_setting_set --app=$app --key=password --value=$password
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STANDARD MODIFICATIONS
|
|
|
|
#=================================================
|
|
|
|
# FIND AND OPEN A PORT
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Finding an available port..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
# Find an available port
|
|
|
|
port=$(ynh_find_port --port=1883)
|
|
|
|
ynh_app_setting_set --app=$app --key=port --value=$port
|
|
|
|
|
|
|
|
# Open the port
|
|
|
|
ynh_script_progression --message="Configuring firewall..." --weight=1
|
|
|
|
ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $port
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# INSTALL DEPENDENCIES
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Installing dependencies..." --weight=20
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
# SPECIFIC SETUP
|
2017-10-26 13:01:33 +02:00
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
# CONFIGURE MOSQUITTO
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring Mosquitto Password..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
cp ../conf/passwd "$password_file"
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_replace_string --match_string="__USERNAME__" --replace_string="$username" --target_file="$password_file"
|
|
|
|
ynh_replace_string --match_string="__PASSWORD__" --replace_string="$password" --target_file="$password_file"
|
|
|
|
mosquitto_passwd -U "$password_file"
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_store_file_checksum --file="$password_file"
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Configuring Mosquitto..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
cp ../conf/mosquitto.conf "$config_file"
|
|
|
|
|
|
|
|
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$config_file"
|
|
|
|
ynh_replace_string --match_string="__PASSWORD_FILE__" --replace_string="$password_file" --target_file="$config_file"
|
|
|
|
|
|
|
|
ynh_store_file_checksum --file="$config_file"
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
# RESTART SYSTEMD SERVICE
|
2017-10-26 13:01:33 +02:00
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Restarting systemd service..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_systemd_action --service_name="$app" --action=restart
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# SETUP LOGROTATE
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Configuring log rotation..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
# Use logrotate to manage application logfile(s)
|
|
|
|
ynh_use_logrotate
|
|
|
|
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
2017-10-26 13:01:33 +02:00
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
yunohost service add $app --description="Allows MQTT clients to send/receive data" --log="/var/log/$app/$app.log" --needs_exposed_ports="$port"
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
# SETUP FAIL2BAN
|
2017-10-26 13:01:33 +02:00
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Configuring Fail2Ban..." --weight=1
|
2017-10-26 13:01:33 +02:00
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
# Create a dedicated Fail2Ban config
|
|
|
|
#ynh_add_fail2ban_config --logpath="/var/log/$app/$app.log" --ports="$port" --failregex="Regex to match into the log for a failed login"
|
2017-10-26 13:01:33 +02:00
|
|
|
|
|
|
|
#=================================================
|
2021-02-13 18:14:54 +01:00
|
|
|
# END OF SCRIPT
|
2017-10-26 13:01:33 +02:00
|
|
|
#=================================================
|
|
|
|
|
2021-02-13 18:14:54 +01:00
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|
|
|
|
|