From 47e2c52ca05b273ebef2ea584fcb960db0f6ddf9 Mon Sep 17 00:00:00 2001 From: Krakinou Date: Sat, 27 May 2023 18:13:49 +0200 Subject: [PATCH] mosquitto for v2 --- conf/domoticz_mosquitto.conf | 6 ++-- conf/mqtt_nginx.conf | 2 +- manifest.toml | 10 +++--- scripts/_common.sh | 19 ---------- scripts/backup | 4 +-- scripts/install | 44 +++++++++-------------- scripts/remove | 2 +- scripts/restore | 12 +++---- scripts/upgrade | 38 ++++++++++---------- tests.toml | 68 ++++++++++++++++++------------------ 10 files changed, 86 insertions(+), 119 deletions(-) diff --git a/conf/domoticz_mosquitto.conf b/conf/domoticz_mosquitto.conf index fbf5e45..95e7326 100644 --- a/conf/domoticz_mosquitto.conf +++ b/conf/domoticz_mosquitto.conf @@ -1,9 +1,9 @@ log_timestamp_format %Y-%m-%dT%H:%M:%S -listener __MQTT_PORT__ 127.0.0.1 -listener __MQTT_PORT__ ::1 +listener __PORT_MQTT__ 127.0.0.1 +listener __PORT_MQTT__ ::1 -listener __MQTT_WEBSOCKET_PORT__ +listener __PORT_MQTT_WEBSOCKET__ protocol websockets allow_anonymous false diff --git a/conf/mqtt_nginx.conf b/conf/mqtt_nginx.conf index 912b649..d4c489a 100644 --- a/conf/mqtt_nginx.conf +++ b/conf/mqtt_nginx.conf @@ -2,7 +2,7 @@ location / { #Settings for mqtt server from outside proxy_http_version 1.1; - proxy_pass http://localhost:__MQTT_WEBSOCKET_PORT__; + proxy_pass http://localhost:__PORT_MQTT_WEBSOCKET__; proxy_read_timeout 90; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; diff --git a/manifest.toml b/manifest.toml index 1b1c7fc..490e3f4 100644 --- a/manifest.toml +++ b/manifest.toml @@ -81,13 +81,11 @@ ram.runtime = "50M" [resources.ports] main.default = 8080 #to be checked if it can become conditionnal - #mqtt.default = 1883 - #mqtt_websocket.default = 8883 + #ports will always be provisionned... don't know if it's an issue... + mqtt.default = 1883 + mqtt_websocket.default = 8883 [resources.apt] packages = "libudev-dev, python3-dev, libcurl4, libusb-0.1-4" + packages_from_raw_bash = '''if [ "$mqtt_domain" != "$domain" ]; then echo mosquitto mosquitto-clients; fi''' - #[[resources.apt.extras]] - #repo = "deb https://repo.mosquitto.org/debian buster main" - #key = "http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key" - #packages = "#FIXME#$extra_pkg_dependencies \ No newline at end of file diff --git a/scripts/_common.sh b/scripts/_common.sh index 1cb1ebb..944a65e 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -4,25 +4,6 @@ # COMMON VARIABLES #================================================= -#mosquitto and mosquitto-clients are required for mqtt -extra_pkg_dependencies="mosquitto mosquitto-clients" - -default_mqtt_port=1883 -default_mqtt_websocket_port=8883 - - -lowercase(){ - echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" -} - -OS=`lowercase \`uname -s\`` -mach=`uname -m` -if [ ${mach} = "armv6l" ] -then - mach="armv7l" -fi -sha256=$(wget -qO- https://releases.domoticz.com/releases/release/domoticz_${os}_${mach}.tgz.sha256sum | sed 's/ update.tgz//' | sed 's/ domoticz_linux_x86_64.tgz//') - #================================================= # PERSONAL HELPERS #================================================= diff --git a/scripts/backup b/scripts/backup index c518beb..c30f632 100755 --- a/scripts/backup +++ b/scripts/backup @@ -27,7 +27,7 @@ ynh_backup --src_path="$install_dir" ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf" ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/api_"$app".conf" -[[ "$mqtt_domain" != "$domain" ]] && ynh_backup --src_path="/etc/nginx/conf.d/$mqtt_domain.d/mqtt_$app.conf" --not_mandatory +[[ "$domain" != "$mqtt_domain" ]] && ynh_backup --src_path="/etc/nginx/conf.d/$mqtt_domain.d/mqtt_$app.conf" --not_mandatory #================================================= # SPECIFIC BACKUP @@ -54,7 +54,7 @@ ynh_backup --src_path="/etc/systemd/system/$app.service" # BACKUP VARIOUS FILES #================================================= -[[ ! -z "$mqtt_domain" ]] && ynh_backup --src_path="/etc/mosquitto/conf.d" --not_mandatory +[[ "$domain" != "$mqtt_domain" ]] && ynh_backup --src_path="/etc/mosquitto/conf.d" --not_mandatory ynh_backup --src_path="/usr/share/yunohost/hooks/conf_regen/95-nginx_domoticz" diff --git a/scripts/install b/scripts/install index 6d06bc6..6fbb089 100755 --- a/scripts/install +++ b/scripts/install @@ -15,18 +15,20 @@ source /usr/share/yunohost/helpers ynh_script_progression --message="Storing installation settings..." #Will be used in restore script to check that we're restoring on the same OS/Board type +lowercase(){ + echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" +} + +OS=`lowercase \`uname -s\`` +mach=`uname -m` +if [ ${mach} = "armv6l" ] +then + mach="armv7l" +fi + ynh_app_setting_set --app="$app" --key=OS --value="$OS" ynh_app_setting_set --app="$app" --key=mach --value="$mach" -#Settings for mosquitto. We store empty values so that they may be automatically retrieved and bound in other scripts. -if [ "$domain" == "$mqtt_domain" ]; then - mqtt_domain="" - #packaging v2 : settings are automatically stored. We overide the values - ynh_app_setting_set --app="$app" --key=mqtt_domain --value="$mqtt_domain" - ynh_app_setting_set --app="$app" --key=mqtt_port --value="" - ynh_app_setting_set --app="$app" --key=mqtt_websocket_port --value="" -fi - #path used by api to read/update domoticz #Set dedicated variables if [ "$path" == "/" ]; then @@ -76,21 +78,9 @@ chown -R "$app":"$app" "$install_dir" #================================================= # SET MOSQUITTO SETTINGS #================================================= -if [ ! -z "$mqtt_domain" ]; then +if [ "$domain" != "$mqtt_domain" ]; then ynh_script_progression --message="Setting up mosquitto..." --weight=5 - - ynh_script_progression --message="Finding available ports for Mosquitto..." - mqtt_port=$(ynh_find_port --port="$default_mqtt_port") - ynh_app_setting_set --app="$app" --key=mqtt_port --value="$mqtt_port" - - mqtt_websocket_port=$(ynh_find_port --port="$default_mqtt_websocket_port") - ynh_app_setting_set --app="$app" --key=mqtt_websocket_port --value="$mqtt_websocket_port" - - - #Installing packages - ynh_install_extra_app_dependencies --repo="deb https://repo.mosquitto.org/debian buster main" --package="$extra_pkg_dependencies" --key="http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key" - #Setting up conf file for access ynh_add_config --template="../conf/domoticz_mosquitto.conf" --destination="/etc/mosquitto/conf.d/"$app"_mosquitto.conf" chmod 644 /etc/mosquitto/conf.d/"$app"_mosquitto.conf @@ -110,7 +100,7 @@ fi #================================================= ynh_script_progression --message="Configuring NGINX web server..." --weight=2 -[[ ! -z "$mqtt_domain" ]] && ynh_add_config --template="../conf/mqtt_nginx.conf" --destination="/etc/nginx/conf.d/"$mqtt_domain".d/mqtt_"$app".conf" +[[ "$domain" != "$mqtt_domain" ]] && ynh_add_config --template="../conf/mqtt_nginx.conf" --destination="/etc/nginx/conf.d/"$mqtt_domain".d/mqtt_"$app".conf" #Set Hook for nginx domain cp -R ../sources/hooks/conf_regen/95-nginx_domoticz /usr/share/yunohost/hooks/conf_regen/ @@ -142,7 +132,7 @@ chown -R domoticz: /var/log/"$app" # Use logrotate to manage application logfile(s) ynh_use_logrotate -[[ ! -z "$mqtt_domain" ]] && ynh_use_logrotate --logfile="/var/log/mosquitto" +[[ "$domain" != "$mqtt_domain" ]] && ynh_use_logrotate --logfile="/var/log/mosquitto" #================================================= # INTEGRATE SERVICE IN YUNOHOST @@ -150,7 +140,7 @@ ynh_use_logrotate ynh_script_progression --message="Integrating service in YunoHost..." yunohost service add "$app" --description="Domotique open sources" --log="/var/log/$app/$app.log" -[[ ! -z "$mqtt_domain" ]] && yunohost service add mosquitto --description="Serveur MQTT pour domoticz" --log="/var/log/mosquitto/mosquitto.log" +[[ "$domain" != "$mqtt_domain" ]] && yunohost service add mosquitto --description="Serveur MQTT pour domoticz" --log="/var/log/mosquitto/mosquitto.log" #================================================= @@ -162,7 +152,7 @@ ynh_script_progression --message="Starting a systemd service..." ynh_systemd_action --service_name="$app" --action="start" #Restarting mosquitto to take changes into account -[[ ! -z "$mqtt_domain" ]] && ynh_systemd_action --service_name=mosquitto --action="restart" +[[ "$domain" != "$mqtt_domain" ]] && ynh_systemd_action --service_name=mosquitto --action="restart" #================================================= # SETUP FAIL2BAN @@ -190,7 +180,7 @@ ynh_script_progression --message="Configuring permissions..." #API & MQTT should stay publicly accessible. #ynh_permission_create --permission="domoticz_API" --label="api" --url="$domain$api_path" --allowed="visitors" --show_tile="false" --protected="true" -[[ ! -z "$mqtt_domain" ]] && ynh_permission_create --permission="domoticz_MQTT" --label="MQTT" --url="$mqtt_domain" --allowed="visitors" --show_tile="false" --protected="true" +[[ "$domain" != "$mqtt_domain" ]] && ynh_permission_create --permission="domoticz_MQTT" --label="MQTT" --url="$mqtt_domain" --allowed="visitors" --show_tile="false" --protected="true" #================================================= diff --git a/scripts/remove b/scripts/remove index 2e5b416..e38e5c3 100755 --- a/scripts/remove +++ b/scripts/remove @@ -15,7 +15,7 @@ source /usr/share/yunohost/helpers # REMOVE SERVICE INTEGRATION IN YUNOHOST #================================================= -if [ ! -z "$mqtt_domain" ]; then +if [ "$domain" != "$mqtt_domain" ]; then ynh_script_progression --message="Removing system configurations related to Mosquitto..." --weight=1 yunohost service remove mosquitto ynh_secure_remove --file="/etc/mosquitto/conf.d" diff --git a/scripts/restore b/scripts/restore index 4c47fa0..b508e68 100755 --- a/scripts/restore +++ b/scripts/restore @@ -11,8 +11,6 @@ source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers #Have to reload them as otherwise they are all the same -#backup_OS=$(ynh_app_setting_get --app="$app" --key=OS) -#backup_mach=$(ynh_app_setting_get --app="$app" --key=mach) current_os=`lowercase \`uname -s\`` current_mach=`uname -m` @@ -23,9 +21,9 @@ current_mach=`uname -m` #on the same system type. If we are restoring on another system type it won't work and in that #case we must go through a reinstall process. test "$OS" = "$current_os" \ - || ynh_die --message="Cannot restore : previous OS is $backup_OS, current OS is $current_os, please reinstall" + || ynh_die --message="Cannot restore : previous OS is $OS, current OS is $current_os, please reinstall" test "$mach" = "$current_mach" \ - || ynh_die --message="Cannot restore : previous machine type is $backup_mach, current machine type is $current_mach, please reinstall" + || ynh_die --message="Cannot restore : previous machine type is $mach, current machine type is $current_mach, please reinstall" ynh_app_setting_set --app="$app" --key=OS --value="$OS" ynh_app_setting_set --app="$app" --key=mach --value="$mach" @@ -69,7 +67,7 @@ chown -R "$app":"$app" "$install_dir" #================================================= # SET MOSQUITTO SETTINGS #================================================= -if [ ! -z "$mqtt_domain" ]; then +if [ "$domain" != "$mqtt_domain" ]; then ynh_script_progression --message="Reinstalling up mosquitto..." --weight=5 #Installing packages @@ -92,7 +90,7 @@ systemctl enable "$app".service --quiet ynh_script_progression --message="Integrating service in YunoHost..." yunohost service add "$app" --description="Domotique open sources" --log="/var/log/$app/$app.log" -[[ ! -z "$mqtt_domain" ]] && yunohost service add mosquitto --description="Serveur MQTT pour domoticz" --log="/var/log/mosquitto/mosquitto.log" +[[ "$domain" != "$mqtt_domain" ]] && yunohost service add mosquitto --description="Serveur MQTT pour domoticz" --log="/var/log/mosquitto/mosquitto.log" #================================================= @@ -103,7 +101,7 @@ ynh_script_progression --message="Starting a systemd service..." ynh_systemd_action --service_name="$app" --action="start" --log_path="/var/log/$app/$app.log" #Restarting mosquitto to take changes into account -[[ ! -z "$mqtt_domain" ]] && ynh_systemd_action --service_name=mosquitto --action="restart" +[[ "$domain" != "$mqtt_domain" ]] && ynh_systemd_action --service_name=mosquitto --action="restart" #================================================= # RESTORE THE LOGROTATE CONFIGURATION diff --git a/scripts/upgrade b/scripts/upgrade index 020f1b0..7a9f689 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -57,34 +57,34 @@ if [ -z "$api_path" ]; then ynh_app_setting_set --app="$app" --key=api_path --value="$api_path" fi -#Need to be kept for backward compatibility : previous version did not have settings stored and +#Previous version did not have settings stored and #variable may not be bound. mqtt_domain=$(ynh_app_setting_get --app="$app" --key=mqtt_domain) if [[ -z "$mqtt_domain" ]]; then - ynh_app_setting_set --app="$app" --key=mqtt_domain --value="$mqtt_domain" + ynh_app_setting_set --app="$app" --key=mqtt_domain --value="$domain" fi #Port to listen for MQTT internal #first we read from manifest to ensure variable is bound -mqtt_port=$(ynh_app_setting_get --app="$app" --key=mqtt_port) +#mqtt_port=$(ynh_app_setting_get --app="$app" --key=mqtt_port) #then we store the setting in the manifest so it is automatically loaded for other scritps -ynh_app_setting_set --app="$app" --key=mqtt_port --value="$mqtt_port" +#ynh_app_setting_set --app="$app" --key=mqtt_port --value="$mqtt_port" #then if a domain has been provided (manually for example to install mosquitto afterward), we assign a port in case it is empty. -if [[ -z "$mqtt_port" && ! -z "$mqtt_domain" ]]; then - mqtt_port=$(ynh_find_port --port="$default_mqtt_port") - ynh_app_setting_set --app="$app" --key=mqtt_port --value="$mqtt_port" -fi +#if [[ -z "$mqtt_port" && ! -z "$mqtt_domain" ]]; then +# mqtt_port=$(ynh_find_port --port="$default_mqtt_port") +# ynh_app_setting_set --app="$app" --key=mqtt_port --value="$mqtt_port" +#fi #Port to listen for MQTT websocket #first we read from manifest to ensure variable is bound -mqtt_websocket_port=$(ynh_app_setting_get --app="$app" --key=mqtt_websocket_port) +#mqtt_websocket_port=$(ynh_app_setting_get --app="$app" --key=mqtt_websocket_port) #then we store the setting in the manifest so it is automatically loaded for other scritps -ynh_app_setting_set --app="$app" --key=mqtt_websocket_port --value="$mqtt_websocket_port" +#ynh_app_setting_set --app="$app" --key=mqtt_websocket_port --value="$mqtt_websocket_port" #then if a domain has been provided (manually for example to install mosquitto afterward), we assign a port in case it is empty. -if [[ -z "$mqtt_websocket_port" && ! -z "$mqtt_domain" ]]; then - mqtt_websocket_port=$(ynh_find_port --port="$default_mqtt_websocket_port") - ynh_app_setting_set --app="$app" --key=mqtt_websocket_port --value="$mqtt_websocket_port" -fi +#if [[ -z "$mqtt_websocket_port" && ! -z "$mqtt_domain" ]]; then +# mqtt_websocket_port=$(ynh_find_port --port="$default_mqtt_websocket_port") +# ynh_app_setting_set --app="$app" --key=mqtt_websocket_port --value="$mqtt_websocket_port" +#fi # Create the permission "domoticz_API" only if it doesn't exist. #if ! ynh_permission_exists --permission="domoticz_API" @@ -94,7 +94,7 @@ fi #fi # Create the permission "domoticz_MQTT" only if it doesn't exist. -if [ ! -z "$mqtt_domain" ]; then +if [ "$domain" != "$mqtt_domain" ]; then if ! ynh_permission_exists --permission="domoticz_MQTT" then # API Authorization with dedicated URL @@ -145,7 +145,7 @@ chown -R "$app":"$app" "$install_dir" #================================================= # SET MOSQUITTO SETTINGS #================================================= -if [ ! -z "$mqtt_domain" ]; then +if [ "$domain" != "$mqtt_domain" ]; then ynh_script_progression --message="Setting up mosquitto..." --weight=5 #Installing packages @@ -172,7 +172,7 @@ fi #================================================= ynh_script_progression --message="Upgrading NGINX web server configuration..." -[[ ! -z "$mqtt_domain" ]] && ynh_add_config --template="../conf/mqtt_nginx.conf" --destination="/etc/nginx/conf.d/"$mqtt_domain".d/mqtt_"$app".conf" +[[ "$domain" != "$mqtt_domain" ]] && ynh_add_config --template="../conf/mqtt_nginx.conf" --destination="/etc/nginx/conf.d/"$mqtt_domain".d/mqtt_"$app".conf" #Set Hook for nginx domain cp -R ../sources/hooks/conf_regen/95-nginx_domoticz /usr/share/yunohost/hooks/conf_regen/ @@ -233,7 +233,7 @@ ynh_add_fail2ban_config --logpath="$log_file" --failregex="^.*Error: Failed logi ynh_script_progression --message="Integrating service in YunoHost..." yunohost service add "$app" --description="Domotique open sources" --log="/var/log/$app/$app.log" -[[ ! -z "$mqtt_domain" ]] && yunohost service add mosquitto --description="Serveur MQTT pour domoticz" --log="/var/log/mosquitto/mosquitto.log" +[[ "$domain" != "$mqtt_domain" ]] && yunohost service add mosquitto --description="Serveur MQTT pour domoticz" --log="/var/log/mosquitto/mosquitto.log" #================================================= @@ -244,7 +244,7 @@ ynh_script_progression --message="Starting a systemd service..." --weight=3 ynh_systemd_action --service_name="$app" --action="start" #Restarting mosquitto to take changes from /etc/mosquitto/conf.d/*.conf into account -[[ ! -z "$mqtt_domain" ]] && ynh_systemd_action --service_name=mosquitto --action="restart" +[[ "$domain" != "$mqtt_domain" ]] && ynh_systemd_action --service_name=mosquitto --action="restart" #================================================= # END OF SCRIPT diff --git a/tests.toml b/tests.toml index 371f428..ff09d92 100644 --- a/tests.toml +++ b/tests.toml @@ -7,37 +7,37 @@ test_format = 1.0 test_upgrade_from.27311ad.args.mqtt_domain="sub.domain.tld" -#[With_Mosquitto_upgrade_with_mosquitto] -# preinstall = """ -# set -euxo pipefail -# yunohost domain add mqtt.domain.tld -# """ -# preupgrade = """ -# set -euxo pipefail -# yunohost app setting domoticz mqtt_domain -v mqtt.domain.tld -# """ -# -# args.mqtt_domain="mqtt.domain.tld" -# -# exclude = ["package_linter"] -# -# test_upgrade_from.27311ad.name = "2022.2 with mosquitto" -# test_upgrade_from.27311ad.args.mqtt_domain="mqtt.domain.tld" -# -# -#[With_Mosquitto_upgrade_without_mosquitto] -# preinstall = """ -# set -euxo pipefail -# yunohost domain add mqtt.domain.tld -# """ -# -# preupgrade = """ -# set -euxo pipefail -# yunohost app setting domoticz mqtt_domain -v mqtt.domain.tld -# """ -# args.mqtt_domain="mqtt.domain.tld" -# -# exclude = ["package_linter", "install.private", "install.multi", "backup_restore", "change_url"] -# -# test_upgrade_from.27311ad.name = "2022.2 without mosquitto" -# test_upgrade_from.27311ad.args.mqtt_domain="sub.domain.tld" \ No newline at end of file +[With_Mosquitto_upgrade_with_mosquitto] + preinstall = """ + set -euxo pipefail + yunohost domain add mqtt.domain.tld + """ + preupgrade = """ + set -euxo pipefail + yunohost app setting domoticz mqtt_domain -v mqtt.domain.tld + """ + + args.mqtt_domain="mqtt.domain.tld" + + exclude = ["package_linter"] + + test_upgrade_from.27311ad.name = "2022.2 with mosquitto" + test_upgrade_from.27311ad.args.mqtt_domain="mqtt.domain.tld" + + +[With_Mosquitto_upgrade_without_mosquitto] + preinstall = """ + set -euxo pipefail + yunohost domain add mqtt.domain.tld + """ + + preupgrade = """ + set -euxo pipefail + yunohost app setting domoticz mqtt_domain -v mqtt.domain.tld + """ + args.mqtt_domain="mqtt.domain.tld" + + exclude = ["package_linter", "install.private", "install.multi", "backup_restore", "change_url"] + + test_upgrade_from.27311ad.name = "2022.2 without mosquitto" + test_upgrade_from.27311ad.args.mqtt_domain="sub.domain.tld" \ No newline at end of file