Merge pull request #4703 from hbl0307106015/tdb
[feed/packages.git] / net / mosquitto / files / etc / init.d / mosquitto
1 #!/bin/sh /etc/rc.common
2 # Basic init script for mosquitto
3 # April 2012, OpenWrt.org
4 # Provides support for the luci-app-mosquitto package, if installed
5
6 START=80
7 USE_PROCD=1
8 TCONF=/tmp/mosquitto.generated.conf
9
10 # Usage: append_if cfg uci_name output_name
11 # add a config line of the form "output_name <value>"
12 # if the "uci_name" was found.
13 # output_name defaults to uci_name if not specified.
14 append_if() {
15 local cfg="$1"
16 local uci_name="$2"
17 local out_name="$3"
18 if [ -z "$out_name" ]; then
19 out_name=$uci_name
20 fi
21 config_get val $cfg $uci_name
22 if [ -n "$val" ]; then
23 echo "$out_name $val" >> $TCONF
24 fi
25 }
26
27 # mosquitto uses true/false, uci uses 1/0
28 # note that this is not shell truthy, but equality with 1!
29 append_bool() {
30 if [ $2 -eq 1 ]; then
31 echo "$1 true" >> $TCONF
32 else
33 echo "$1 false" >> $TCONF
34 fi
35 }
36
37 # as per append_if, but gets the value as a uci bool, not raw
38 append_optional_bool() {
39 local cfg="$1"
40 local uci_name="$2"
41 local out_name="$3"
42 config_get val $cfg $uci_name
43 if [ -n "$val" ]; then
44 config_get_bool real $cfg $uci_name
45 append_bool $out_name $real
46 fi
47 }
48
49
50 convert_mosq_general() {
51 local cfg="$1"
52 config_get destinations "$1" log_dest
53 for dest in $destinations; do
54 echo "log_dest $dest" >> $TCONF
55 done
56
57 config_get_bool no_remote "$1" no_remote_access 0
58 if [ "$no_remote" -eq 1 ]; then
59 echo "bind_address localhost" >> $TCONF
60 fi
61
62 config_get port "$1" port 1883
63 echo "port $port" >> $TCONF
64 append_if "$1" protocol
65 append_if "$1" max_inflight_messages
66 append_if "$1" max_queued_messages
67 append_if "$1" sys_interval
68 }
69
70 convert_persistence() {
71 local cfg="$1"
72
73 append_if "$cfg" client_expiration persistent_client_expiration
74 append_if "$cfg" autosave_interval
75 append_optional_bool "$cfg" autosave_on_changes autosave_on_changes
76 append_optional_bool "$cfg" persistence persistence
77 append_if "$cfg" file persistence_file
78 config_get loc "$cfg" location
79 if [ -n "$loc" ]; then
80 [ -d "$loc" ] || {
81 mkdir -p "$loc";
82 chown mosquitto "$loc";
83 }
84 echo "persistence_location $loc" >> $TCONF
85 fi
86 }
87
88 add_listener() {
89 echo "" >> $TCONF
90 config_get port "$1" port
91 if [ -z "$port" ]; then
92 echo "Ignoring listener section without port"
93 return
94 fi
95 config_get_bool no_remote "$1" no_remote_access 0
96 if [ "$no_remote" -eq 1 ]; then
97 echo "listener $port 127.0.0.1" >> $TCONF
98 else
99 echo "listener $port" >> $TCONF
100 fi
101
102 append_if "$1" protocol
103 append_if "$1" http_dir
104 append_optional_bool "$1" use_username_as_clientid use_username_as_clientid
105 append_if "$1" cafile
106 append_if "$1" capath
107 append_if "$1" certfile
108 append_if "$1" keyfile
109 append_if "$1" tls_version
110 append_optional_bool "$1" require_certificate require_certificate
111 append_optional_bool "$1" use_identity_as_username use_identity_as_username
112 append_if "$1" crlfile
113 append_if "$1" ciphers
114 append_if "$1" psk_hint
115 }
116
117 add_topic() {
118 echo "topic $1" >> $TCONF
119 }
120
121 add_bridge() {
122 config_get conn "$1" connection
123 config_get addr "$1" address
124 if [ -z "$conn" -o -z "$addr" ]; then
125 echo "Ignoring bridge section, misisng connection/address"
126 return
127 fi
128 echo "" >> $TCONF
129 echo "# Bridge connection from UCI section" >> $TCONF
130 append_if "$1" connection
131 append_if "$1" address
132
133 config_list_foreach "$1" topic add_topic
134 append_optional_bool "$1" cleansession cleansession
135 append_optional_bool "$1" try_private try_private
136 append_optional_bool "$1" notifications notifications
137 append_optional_bool "$1" round_robin round_robin
138
139 # Note, deprecated upstream, preserve old uci configs
140 append_if "$1" clientid remote_clientid
141 append_if "$1" remote_clientid
142 append_if "$1" local_clientid
143 append_if "$1" notification_topic
144 append_if "$1" keepalive_interval
145 append_if "$1" start_type
146 append_if "$1" restart_timeout
147 append_if "$1" idle_timeout
148 append_if "$1" threshold
149 append_if "$1" protocol_version bridge_protocol_version
150 append_optional_bool "$1" attempt_unsubscribe bridge_attempt_unsubscribe
151 append_if "$1" identity bridge_identity
152 append_if "$1" psk bridge_psk
153 append_if "$1" tls_version bridge_tls_version
154
155 append_if "$1" restart_timeout
156 append_if "$1" capath bridge_capath
157 append_if "$1" cafile bridge_cafile
158 append_if "$1" certfile bridge_certfile
159 append_if "$1" keyfile bridge_keyfile
160 # Note, deprecated upstream, preserve old uci configs
161 append_if "$1" username remote_username
162 # Note, deprecated upstream, preserve old uci configs
163 append_if "$1" password remote_password
164 append_if "$1" remote_username
165 append_if "$1" remote_password
166 }
167
168
169 convert_uci() {
170 rm -rf $TCONF
171 echo "Generating mosquitto config file in $TCONF"
172 echo "# mosquitto.conf file generated from UCI config." >>$TCONF
173 # Don't include a timestamp, it makes md5sum compares fail
174
175 config_load mosquitto
176 config_foreach convert_mosq_general "mosquitto"
177 config_foreach convert_persistence "persistence"
178 config_foreach add_listener "listener"
179 config_foreach add_bridge "bridge"
180 }
181
182 start_service_real() {
183 local cfg="$1"
184 local use_uci write_pid
185 config_get use_uci "$cfg" use_uci
186 if [ "$use_uci" -eq 1 ]; then
187 CONF=$TCONF
188 convert_uci
189 else
190 CONF=/etc/mosquitto/mosquitto.conf
191 fi
192 config_get write_pid "$cfg" write_pid 0
193
194 procd_open_instance
195 procd_set_param command mosquitto
196 procd_append_param command -c $CONF
197 # Makes /etc/init.d/mosquitto reload work if you edit the final file.
198 procd_set_param file $CONF
199 [ "$write_pid" -eq 1 ] && procd_set_param pidfile /var/run/mosquitto.pid
200 procd_close_instance
201 }
202
203 start_service() {
204 config_load mosquitto
205 config_foreach start_service_real owrt
206 }
207
208 service_triggers() {
209 # Makes "reload_config" work
210 procd_add_reload_trigger "mosquitto"
211 }