sulu: Fix restarting of mqtt broker

This commit is contained in:
Vivek Dutta 2025-08-21 18:39:36 +05:30 committed by Vivek Kumar Dutta
parent e69fba300a
commit dd18f6f9ed
No known key found for this signature in database
GPG key ID: 4E09F5AD8265FD4C
7 changed files with 61 additions and 134 deletions

View file

@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=sulu-builder
PKG_VERSION:=5.0.5
PKG_VERSION:=5.1.0
PKG_RELEASE:=1
PKG_SOURCE_PROTO:=git
@ -46,6 +46,7 @@ define Package/sulu-builder
TITLE += (Builder)
VARIANT:=builder
DEPENDS+=+sulu-base $(foreach plugin,$(SULU_PLUGINS), +PACKAGE_$(plugin):$(plugin)) $(foreach plugin,$(SULU_EXTRA), +PACKAGE_$(plugin):$(plugin))
DEPENDS+=+@OBUSPA_LOCAL_MQTT_LISTENER
endef
define Package/sulu/description
@ -95,13 +96,8 @@ endef
define Package/sulu/install/Default
$(INSTALL_DIR) $(1)/sulu/
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_BIN) ./files/etc/config/sulu $(1)/etc/config/sulu
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) ./files/etc/init.d/sulu $(1)/etc/init.d/sulu
$(INSTALL_DIR) $(1)/etc/sulu
$(INSTALL_BIN) ./files/etc/sulu/sulu.sh $(1)/etc/sulu/
$(INSTALL_DATA) ./files/etc/sulu/nginx.locations $(1)/etc/sulu/
@ -109,9 +105,9 @@ define Package/sulu/install/Default
$(INSTALL_DATA) ./files/etc/users/roles/*.json $(1)/etc/users/roles/
$(INSTALL_DIR) $(1)/etc/uci-defaults
$(INSTALL_BIN) ./files/etc/uci-defaults/40-add-sulu-config $(1)/etc/uci-defaults/
$(INSTALL_DATA) ./files/etc/uci-defaults/40-add-sulu-config $(1)/etc/uci-defaults/
ifneq ($(CONFIG_SULU_DEFAULT_UI)$(CONFIG_SULU_BUILDER_DEFAULT_UI),)
$(INSTALL_BIN) ./files/etc/uci-defaults/41-make-sulu-default-ui $(1)/etc/uci-defaults/
$(INSTALL_DATA) ./files/etc/uci-defaults/41-make-sulu-default-ui $(1)/etc/uci-defaults/
endif
endef
@ -128,7 +124,6 @@ define Package/sulu/install
$(Package/sulu/install/Post)
endef
define Package/sulu-builder/install
$(Package/sulu/install/Default)
$(INSTALL_DIR) $(1)/sulu/presets

View file

@ -1,2 +0,0 @@
config global 'global'
option enabled '1'

View file

@ -1,47 +0,0 @@
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=90
STOP=01
. /lib/functions.sh
log() {
echo "${@}" | logger -t sulu.init -p debug
}
validate_sulu_global_section() {
uci_validate_section sulu global global \
'enabled:bool:1'
}
start_service() {
local enabled
config_load sulu
procd_open_instance sulu
validate_sulu_global_section || return 0
# append sulu connection injection
if [ "${enabled}" -eq "0" ]; then
procd_close_instance
return 0
fi
if [ "${1}" = "update" ]; then
log "Reloading related services"
/etc/sulu/sulu.sh -r
fi
procd_close_instance
}
reload_service() {
stop
start update
}
service_triggers() {
procd_add_reload_trigger "sulu" "userinterface" "mosquitto"
procd_add_reload_trigger "config.change" "mapcontroller" /etc/sulu/sulu.sh -q
}

View file

@ -23,7 +23,9 @@ location /wss {
proxy_set_header X-Forwarded-Proto $scheme;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header Pragma 'no-cache';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,Content-Type,Range' always;
expires 0;
proxy_pass http://websocket;
}
@ -44,6 +46,7 @@ location / {
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,Content-Type,Range' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
}
expires -1;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
add_header Pragma 'no-cache';
expires 0;
}

View file

@ -4,18 +4,16 @@
. /lib/functions.sh
. /usr/share/libubox/jshn.sh
. /lib/functions/iopsys-environment.sh
#. /lib/functions/iopsys-environment.sh
RESTART_REQ=0
_RESTART_SERVICES="0"
mkdir -p /tmp/sulu/
function slog() {
slog() {
echo "$*" | logger -t sulu.sh -p debug
}
function _get_agent_id() {
_get_agent_id() {
local oui serial endpointid
endpointid="$(uci_get obuspa localagent EndpointID)"
@ -30,7 +28,7 @@ function _get_agent_id() {
fi
}
function _get_sulu_user_roles() {
_get_sulu_user_roles() {
roles=$(uci -q get userinterface._sulu_s.role)
for role in ${roles}; do
@ -41,35 +39,32 @@ function _get_sulu_user_roles() {
sulu_user_roles=$(echo -e "${sulu_user_roles// /\\n}" | sort | uniq)
fi
echo ${sulu_user_roles}
echo "${sulu_user_roles}"
}
function _get_sulu_root() {
_get_sulu_root() {
local root
root="$(uci -q get nginx._sulu_s.root)"
echo "${root:-/sulu}"
}
function _get_sulu_connection_config() {
_get_sulu_connection_config() {
local config
config="$(_get_sulu_root)/presets/connection-config.json"
echo "${config}"
}
function _get_sulu_session_mode() {
echo "$(uci -q get sulu.global.SessionMode)"
_get_sulu_session_mode() {
uci -q get sulu.global.SessionMode
}
function _get_sulu_acl_file() {
local file
file="$(uci -q get mosquitto.sulu.acl_file)"
echo "${file}"
_get_sulu_acl_file() {
uci -q get mosquitto.sulu.acl_file
}
function generate_sulu_conn_config() {
generate_sulu_conn_config() {
local users SCONFIG session
users="$(_get_sulu_user_roles)"
@ -109,10 +104,10 @@ function generate_sulu_conn_config() {
json_close_object
}
json_dump >${SCONFIG}
json_dump >"${SCONFIG}"
}
function _update_obuspa_config_rbac() {
_update_obuspa_config_rbac() {
local agent users session
agent="$(_get_agent_id)"
@ -124,45 +119,45 @@ function _update_obuspa_config_rbac() {
# Add mqtt
section="mqtt_sulu_${user}"
if ! uci_get obuspa ${section} >/dev/null 2>&1; then
uci_add obuspa mqtt ${section}
uci_set obuspa ${section} BrokerAddress "127.0.0.1"
uci_set obuspa ${section} BrokerPort "1883"
uci_set obuspa ${section} TransportProtocol "TCP/IP"
if ! uci_get obuspa "${section}" >/dev/null 2>&1; then
uci_add obuspa mqtt "${section}"
uci_set obuspa "${section}" BrokerAddress "127.0.0.1"
uci_set obuspa "${section}" BrokerPort "1883"
uci_set obuspa "${section}" TransportProtocol "TCP/IP"
RESTART_REQ=1
fi
# Add mtp
section="mtp_sulu_${user}"
if ! uci_get obuspa ${section} >/dev/null 2>&1; then
uci_add obuspa mtp ${section}
uci_set obuspa ${section} Protocol "MQTT"
uci_set obuspa ${section} ResponseTopicConfigured "/usp/${agent}/${user}/endpoint"
uci_set obuspa ${section} mqtt "mqtt_sulu_$user"
if ! uci_get obuspa "${section}" >/dev/null 2>&1; then
uci_add obuspa mtp "${section}"
uci_set obuspa "${section}" Protocol "MQTT"
uci_set obuspa "${section}" ResponseTopicConfigured "/usp/${agent}/${user}/endpoint"
uci_set obuspa "${section}" mqtt "mqtt_sulu_$user"
RESTART_REQ=1
fi
# Add controller
section="controller_sulu_${user}"
if ! uci_get obuspa ${section} >/dev/null 2>&1; then
uci_add obuspa controller ${section}
uci_set obuspa ${section} EndpointID "self::sulu-${user}"
uci_set obuspa ${section} Protocol "MQTT"
uci_set obuspa ${section} Topic "/usp/${agent}/${user}/controller"
uci_set obuspa ${section} mqtt "mqtt_sulu_$user"
uci_set obuspa ${section} assigned_role_name "$user"
if ! uci_get obuspa "${section}" >/dev/null 2>&1; then
uci_add obuspa controller "${section}"
uci_set obuspa "${section}" EndpointID "self::sulu-${user}"
uci_set obuspa "${section}" Protocol "MQTT"
uci_set obuspa "${section}" Topic "/usp/${agent}/${user}/controller"
uci_set obuspa "${section}" mqtt "mqtt_sulu_$user"
uci_set obuspa "${section}" assigned_role_name "$user"
RESTART_REQ=1
fi
obMode="$(uci_get obuspa ${section} SessionMode)"
obMode="$(uci_get obuspa "${section}" SessionMode)"
if [ "${session}" != "${obMode}" ]; then
uci_set obuspa ${section} SessionMode "${session}"
uci_set obuspa "${section}" SessionMode "${session}"
RESTART_REQ=1
fi
done
}
function _create_mosquitto_acl() {
_create_mosquitto_acl() {
local agentid users
local ACL_FILE acl_users
@ -175,9 +170,9 @@ function _create_mosquitto_acl() {
users="$(_get_sulu_user_roles)"
if [ -f "${ACL_FILE}" ]; then
acl_users="$(awk '/^user/ {print $2}' ${ACL_FILE})"
acl_users="$(awk '/^user/ {print $2}' "${ACL_FILE}")"
for user in ${users}; do
if ! grep -q "$user" ${acl_users}; then
if ! grep -q "$user" "${acl_users}"; then
rm -f "${ACL_FILE}"
fi
done
@ -186,36 +181,38 @@ function _create_mosquitto_acl() {
agentid="$(_get_agent_id)"
for user in ${users}; do
if ! grep -q "user $user" ${ACL_FILE}; then
echo "user ${user}" >>${ACL_FILE}
echo "topic read /usp/${agentid}/${user}/controller/reply-to/#" >>${ACL_FILE}
echo "topic write /usp/${agentid}/${user}/endpoint/#" >>${ACL_FILE}
echo "topic read /usp/${agentid}/${user}/controller/#" >>${ACL_FILE}
echo "" >>${ACL_FILE}
if ! grep -q "user $user" "${ACL_FILE}"; then
{
echo "user ${user}"
echo "topic read /usp/${agentid}/${user}/controller/reply-to"
echo "topic write /usp/${agentid}/${user}/endpoint"
echo "topic read /usp/${agentid}/${user}/controller"
echo ""
} >> "${ACL_FILE}"
RESTART_REQ="1"
fi
done
if [ "${_RESTART_SERVICES}" -eq "1" -a "${RESTART_REQ}" -gt "0" ]; then
if [ "${_RESTART_SERVICES}" -eq "1" ] && [ "${RESTART_REQ}" -gt "0" ]; then
slog "Restarting mosquitto..."
ubus call uci commit '{"config":"mosquitto"}'
fi
}
function update_obuspa_config() {
update_obuspa_config() {
RESTART_REQ=0
uci_load obuspa
_update_obuspa_config_rbac
uci_commit obuspa
if [ "${_RESTART_SERVICES}" -eq "1" -a "${RESTART_REQ}" -gt "0" ]; then
if [ "${_RESTART_SERVICES}" -eq "1" ] && [ "${RESTART_REQ}" -gt "0" ]; then
slog "Restarting obuspa..."
ubus call uci commit '{"config":"obuspa"}'
fi
}
function configure_sulu() {
configure_sulu() {
_create_mosquitto_acl
update_obuspa_config
generate_sulu_conn_config

View file

@ -12,19 +12,13 @@ if [ ! -f "${UCI_TEMPLATE}" ]; then
return 0
fi
function _get_usp_upstream_port() {
local port
port="$(uci -q get mosquitto.sulu.port)"
echo "${port:-9009}"
}
update_nginx_uci_template()
{
local port
UCI_TEMPLATE="/etc/nginx/uci.conf.template"
port="$(_get_usp_upstream_port)"
port="$(uci -q get mosquitto.sulu.port)"
port="${port:-9009}"
if ! grep -q "upstream websocket" ${UCI_TEMPLATE}; then
sed -i '/#UCI_HTTP_CONFIG$/i\ map $http_upgrade $connection_upgrade { default upgrade; "" close; }' ${UCI_TEMPLATE}
sed -i "/#UCI_HTTP_CONFIG$/i\ upstream websocket { server 127.0.0.1:${port}; }" ${UCI_TEMPLATE}
@ -45,18 +39,6 @@ add_sulu_config_to_mosquitto()
fi
}
add_obuspa_config_to_mosquitto()
{
uci_load mosquitto
if ! uci_get mosquitto obuspa >/dev/null 2>&1; then
uci_add mosquitto listener obuspa
uci_set mosquitto obuspa enabled 1
uci_set mosquitto obuspa port '1883'
uci_set mosquitto obuspa no_remote_access '1'
uci_set mosquitto obuspa allow_anonymous '1'
fi
}
add_sulu_userinterface_uci()
{
uci_load userinterface
@ -83,9 +65,8 @@ add_sulu_userinterface_uci()
fi
}
add_obuspa_config_to_mosquitto
add_sulu_config_to_mosquitto
update_nginx_uci_template
add_sulu_userinterface_uci
update_nginx_uci_template
/etc/sulu/sulu.sh

View file