mosquitto: convert init script to procd

Adds support for persistence settings.
Bumps package version for new init script.

Signed-off-by: Karl Palsson <karlp@etactica.com>
This commit is contained in:
Karl Palsson 2016-02-16 16:29:53 +00:00 committed by Karl Palsson
parent 05aa35194e
commit 02caa85cb3
3 changed files with 157 additions and 151 deletions

View File

@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=mosquitto
PKG_VERSION:=1.4.7
PKG_RELEASE:=1
PKG_RELEASE:=2
PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE.txt

View File

@ -4,25 +4,165 @@
# Provides support for the luci-app-mosquitto package, if installed
START=80
APP=`which mosquitto`
USE_UCI_CONFIG=$(uci -q get mosquitto.owrt.use_uci)
if [ $? -eq 1 ]; then
USE_UCI_CONFIG=0
fi
USE_PROCD=1
TCONF=/tmp/mosquitto.generated.conf
SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1
# Usage: append_if cfg uci_name output_name
# add a config line of the form "output_name <value>"
# if the "uci_name" was found.
# output_name defaults to uci_name if not specified.
append_if() {
local cfg="$1"
local uci_name="$2"
local out_name="$3"
if [ -z "$out_name" ]; then
out_name=$uci_name
fi
config_get val $cfg $uci_name
if [ -n "$val" ]; then
echo "$out_name $val" >> $TCONF
fi
}
start() {
if [ "$USE_UCI_CONFIG" -eq 1 ]; then
CONF=/tmp/mosquitto.converted.$$.conf
mosquitto.uci.convert -f $CONF
else
CONF=/etc/mosquitto/mosquitto.conf
# mosquitto uses true/false, uci uses 1/0
# note that this is not shell truthy, but equality with 1!
append_bool() {
if [ $2 -eq 1 ]; then
echo "$1 true" >> $TCONF
else
echo "$1 false" >> $TCONF
fi
}
# as per append_if, but gets the value as a uci bool, not raw
append_optional_bool() {
local cfg="$1"
local uci_name="$2"
local out_name="$3"
config_get val $cfg $uci_name
if [ -n "$val" ]; then
config_get_bool real $cfg $uci_name
append_bool $out_name $real
fi
}
convert_mosq_general() {
local cfg="$1"
config_get destinations "$1" log_dest
for dest in $destinations; do
echo "log_dest $dest" >> $TCONF
done
config_get_bool no_remote "$1" no_remote_access 0
if [ "$no_remote" -eq 1 ]; then
echo "bind_address 127.0.0.1" >> $TCONF
fi
config_get port "$1" port 1883
echo "port $port" >> $TCONF
append_if "$1" protocol
append_if "$1" max_inflight_messages
append_if "$1" max_queued_messages
}
convert_persistence() {
local cfg="$1"
append_if "$cfg" client_expiration persistent_client_expiration
append_if "$cfg" autosave_interval
append_optional_bool "$cfg" autosave_on_changes autosave_on_changes
append_optional_bool "$cfg" persistence persistence
append_if "$cfg" file persistence_file
config_get loc "$cfg" location
if [ -n "$loc" ]; then
[ -d "$loc" ] || mkdir -p "$loc";
echo "persistence_location $loc" >> $TCONF
fi
}
add_listener() {
echo "" >> $TCONF
config_get port "$1" port
if [ -z "$port" ]; then
echo "Ignoring listener section without port"
return
fi
config_get_bool no_remote "$1" no_remote_access 0
if [ "$no_remote" -eq 1 ]; then
echo "listener $port 127.0.0.1" >> $TCONF
else
echo "listener $port" >> $TCONF
fi
append_if "$1" protocol
}
add_topic() {
echo "topic $1" >> $TCONF
}
add_bridge() {
config_get conn "$1" connection
config_get addr "$1" address
if [ -z "$conn" -o -z "$addr" ]; then
echo "Ignoring bridge section, misisng connection/address"
return
fi
echo "" >> $TCONF
echo "# Bridge connection from UCI section" >> $TCONF
append_if "$1" connection
append_if "$1" address
config_list_foreach "$1" topic add_topic
append_optional_bool "$1" cleansession cleansession
append_optional_bool "$1" try_private try_private
append_if "$1" clientid
append_if "$1" identity bridge_identity
append_if "$1" psk bridge_psk
append_if "$1" tls_version bridge_tls_version
}
convert_uci() {
rm -rf $TCONF
echo "Generating mosquitto config file in $TCONF"
echo "# mosquitto.conf file generated from UCI config." >>$TCONF
# Don't include a timestamp, it makes md5sum compares fail
config_load mosquitto
config_foreach convert_mosq_general "mosquitto"
config_foreach convert_persistence "persistence"
config_foreach add_listener "listener"
config_foreach add_bridge "bridge"
}
start_service_real() {
local cfg="$1"
local use_uci
config_get use_uci "$cfg" use_uci
if [ "$use_uci" -eq 1 ]; then
CONF=$TCONF
convert_uci
else
CONF=/etc/mosquitto/mosquitto.conf
fi
service_start $APP -c $CONF
procd_open_instance
procd_set_param command mosquitto
procd_append_param command -c $CONF
# Makes /etc/init.d/mosquitto reload work if you edit the final file.
procd_set_param file $CONF
procd_close_instance
}
stop() {
service_stop $APP
start_service() {
config_load mosquitto
config_foreach start_service_real owrt
}
service_triggers() {
# Makes "reload_config" work
procd_add_reload_trigger "mosquitto"
}

View File

@ -1,134 +0,0 @@
#!/bin/sh
# Converts a uci config file into an appropriate mosquitto.conf snippet
# expected to be used in an init file to generate a config file to run from
# Karl Palsson <karlp@remake.is> 2012.
# Considered to be released into the public domain
[ -f $IPKG_INSTROOT/lib/functions.sh ] && . $IPKG_INSTROOT/lib/functions.sh
TCONF=/tmp/mosquitto.generated.$$.conf
while getopts "f:" o; do
case $o in
f)
TCONF=$OPTARG
;;
esac
done
if [ -e $TCONF ]; then
echo "Odd, same temporary generated config file already existed: $TCONF"
exit 1
fi
echo "Generating mosquitto config file in $TCONF"
NOW=$(date)
echo "# mosquitto.conf file generated from UCI config." >>$TCONF
echo "# Config snippet generated by $0 on $NOW" >>$TCONF
echo "#" >> $TCONF
# Usage: append_if cfg uci_name output_name
# add a config line of the form "output_name <value>"
# if the "uci_name" was found.
# output_name defaults to uci_name if not specified.
append_if() {
local cfg="$1"
local uci_name="$2"
local out_name="$3"
if [ -z "$out_name" ]; then
out_name=$uci_name
fi
config_get val $cfg $uci_name
if [ -n "$val" ]; then
echo "$out_name $val" >> $TCONF
fi
}
# mosquitto uses true/false, uci uses 1/0
# note that this is not shell truthy, but equality with 1!
append_bool() {
if [ $2 -eq 1 ]; then
echo "$1 true" >> $TCONF
else
echo "$1 false" >> $TCONF
fi
}
# as per append_if, but gets the value as a uci bool, not raw
append_optional_bool() {
local cfg="$1"
local uci_name="$2"
local out_name="$3"
config_get val $cfg $uci_name
if [ -n "$val" ]; then
config_get_bool real $cfg $uci_name
append_bool $out_name $real
fi
}
mosq_general() {
config_get destinations "$1" log_dest
for dest in $destinations; do
echo "log_dest $dest" >> $TCONF
done
config_get_bool no_remote "$1" no_remote_access 0
if [ "$no_remote" -eq 1 ]; then
echo "bind_address 127.0.0.1" >> $TCONF
fi
config_get port "$1" port 1883
echo "port $port" >> $TCONF
append_if "$1" protocol
append_if "$1" max_inflight_messages
append_if "$1" max_queued_messages
}
add_listener() {
echo "" >> $TCONF
config_get port "$1" port
if [ -z "$port" ]; then
echo "Ignoring listener section without port"
return
fi
config_get_bool no_remote "$1" no_remote_access 0
if [ "$no_remote" -eq 1 ]; then
echo "listener $port 127.0.0.1" >> $TCONF
else
echo "listener $port" >> $TCONF
fi
append_if "$1" protocol
}
add_topic() {
echo "topic $1" >> $TCONF
}
add_bridge() {
config_get conn "$1" connection
config_get addr "$1" address
if [ -z "$conn" -o -z "$addr" ]; then
echo "Ignoring bridge section, misisng connection/address"
return
fi
echo "" >> $TCONF
echo "# Bridge connection from UCI section" >> $TCONF
append_if "$1" connection
append_if "$1" address
config_list_foreach "$1" topic add_topic
append_optional_bool "$1" cleansession cleansession
append_optional_bool "$1" try_private try_private
append_if "$1" clientid
append_if "$1" identity bridge_identity
append_if "$1" psk bridge_psk
append_if "$1" tls_version bridge_tls_version
}
config_load "mosquitto"
config_foreach mosq_general "mosquitto"
config_foreach add_listener "listener"
config_foreach add_bridge "bridge"