aboutsummaryrefslogtreecommitdiff
path: root/net/nut/files/nut-server.init
blob: 70cb21f8713eb857ea49071bef83e772426b2b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/sh /etc/rc.common
# Copyright © 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
START=70
STOP=30

USERS_C=/var/etc/nut/upsd.users
UPSD_C=/var/etc/nut/upsd.conf
UPS_C=/var/etc/nut/ups.conf

USE_PROCD=1

get_write_driver_config() {
	local cfg="$1"
	local var="$2"
	local def="$3"
	local flag="$4"
	local val

	[ -z "$flag" ] && {
		config_get val "$cfg" "$var" "$def"
		[ -n "$val" ] && [ "$val" != "0" ] && echo "$var = $val" >>"$UPS_C"
	}

	[ -n "$flag" ] && {
		config_get_bool val "$cfg" "$var" "$def"
		[ "$val" = 1 ] && echo "$var" >>"$UPS_C"
	}
}

upsd_statepath() {
	local statepath

	config_get statepath upsd statepath /var/run/nut
	STATEPATH="$statepath"
}

upsd_runas() {
	local runas

	[ -n "$RUNAS" ] && return 0

	config_get runas upsd runas nut
	RUNAS="$runas"
}

listen_address() {
    local cfg="$1"

    config_get address "$cfg" address "::1"
    config_get port "$cfg" port
    echo "LISTEN $address $port" >>"$UPSD_C"
}

upsd_config() {
    local cfg="$1"
    local maxage maxconn certfile runas statepath

    # Note runas support requires you make sure USB device file is readable by
    # the runas user
    config_get runas "$cfg" runas nut
    RUNAS="$runas"

    config_get statepath "$cfg" statepath /var/run/nut
    STATEPATH="$statepath"

    config_get maxage "$cfg" maxage
    [ -n "$maxage" ] && echo "MAXAGE $maxage" >>"$UPSD_C"

    [ -n "$statepath" ] && echo "STATEPATH $statepath" >>"$UPSD_C"

    config_get maxconn "$cfg" maxconn
    [ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>"$UPSD_C"

    #NOTE: certs only apply to SSL-enabled version
    config_get certfile "$cfg" certfile
    [ -n "$certfile" ] && echo "CERTFILE $certfile" >>"$UPSD_C"
}

nut_user_add() {
	local cfg="$1"
	local a
	local val

	config_get val "$cfg" username "$1"
	echo "[$val]" >> "$USERS_C"

	config_get val "$cfg" password
	echo "  password = $val" >> "$USERS_C"

	config_get val "$cfg" actions
	for a in $val; do
	    echo "  actions = $a" >> "$USERS_C"
	done

	instcmd() {
		local val="$1"
		echo "  instcmds = $val" >> "$USERS_C"
	}

	config_list_foreach "$cfg" instcmd instcmd

	config_get val "$cfg" upsmon
	if [ -n "$val" ]; then
	    echo "  upsmon $val" >> "$USERS_C"
	fi
}

build_server_config() {
        mkdir -p "$(dirname "$UPSD_C")"
	chmod 0640 "$UPS_C"
	rm -f "$USERS_C"
	rm -f "$UPSD_C"
	rm -f /var/etc/nut/nut.conf

	echo "# Config file automatically generated from UCI config" > "$USERS_C"
	echo "# Config file automatically generated from UCI config" > "$UPSD_C"

	config_foreach nut_user_add user
	config_foreach listen_address listen_address
	config_foreach upsd_config upsd
	echo "MODE=netserver" >>/var/etc/nut/nut.conf

	chmod 0640 "$USERS_C"
	chmod 0640 "$UPSD_C"
	chmod 0644 /var/etc/nut/nut.conf

	[ -d "${STATEPATH}" ] || {
		mkdir -p "${STATEPATH}"
		chmod 0750 "${STATEPATH}"
	}

	if [ -n "$RUNAS" ]; then
		chown "$RUNAS":"$(id -gn "$RUNAS")" "${STATEPATH}"
		chgrp "$(id -gn "$RUNAS")" "$USERS_C"
		chgrp "$(id -gn "$RUNAS")" "$UPSD_C"
	fi
	haveserver=1
}

build_driver_config() {
	local cfg="$1"

	echo "[$cfg]" >>"$UPS_C"

	get_write_driver_config "$cfg" bus
	get_write_driver_config "$cfg" cable
	get_write_driver_config "$cfg" community
	get_write_driver_config "$cfg" desc
	get_write_driver_config "$cfg" driver "usbhid-ups"
	get_write_driver_config "$cfg" ignorelb 0 1
	get_write_driver_config "$cfg" interruptonly 0 1
	get_write_driver_config "$cfg" interruptsize
	get_write_driver_config "$cfg" maxreport
	get_write_driver_config "$cfg" maxstartdelay
	get_write_driver_config "$cfg" mfr
	get_write_driver_config "$cfg" model
	get_write_driver_config "$cfg" nolock 0 1
	get_write_driver_config "$cfg" notransferoids 0 1
	get_write_driver_config "$cfg" offdelay
	get_write_driver_config "$cfg" ondelay
	get_write_driver_config "$cfg" pollfreq
	get_write_driver_config "$cfg" port "auto"
	get_write_driver_config "$cfg" product
	get_write_driver_config "$cfg" productid
	get_write_driver_config "$cfg" retrydelay
	get_write_driver_config "$cfg" sdorder
	get_write_driver_config "$cfg" sdtime
	get_write_driver_config "$cfg" serial
	get_write_driver_config "$cfg" snmp_version
	get_write_driver_config "$cfg" snmp_retries
	get_write_driver_config "$cfg" snmp_timeout
	get_write_driver_config "$cfg" synchronous
	get_write_driver_config "$cfg" vendor
	get_write_driver_config "$cfg" vendorid

	# Params specific to NetXML driver
        get_write_driver_config "$cfg" login
        get_write_driver_config "$cfg" password
        get_write_driver_config "$cfg" subscribe 0 1

	defoverride() {
		local overvar="$1"
		local defover="$2"
		local overtype="$(echo "$overvar" | tr '_' '.')"
		local overval

		config_get overval "${defover}_${overvar}" value
		[ -n "$overval" ] && echo "${defover}.${overtype} = $overval" >>"$UPS_C"
	}

	config_list_foreach "$cfg" override defoverride override
	config_list_foreach "$cfg" default defoverride default

	other() {
		local othervar="$1"
		local othervarflag="$2"
		local otherval

		if [ "$othervarflag" = "otherflag" ]; then
			config_get_bool otherval "${othervarflag}_${othervar}" value
			[ "$otherval" = "1" ] && echo "${othervar}" >>"$UPS_C"
		else
			config_get otherval "${othervarflag}_${othervar}" value
			[ -n "$otherval" ] && echo "${othervar} = $otherval" >>"$UPS_C"
		fi
	}

	config_list_foreach "$cfg" other other other
	config_list_foreach "$cfg" otherflag other otherflag
	echo "" >>$UPS_C
	havedriver=1
}

build_global_driver_config() {
	local cfg="$1"

	# Global driver config
	get_write_driver_config "$cfg" chroot
	get_write_driver_config "$cfg" driverpath
	get_write_driver_config "$cfg" maxstartdelay
	get_write_driver_config "$cfg" maxretry
	get_write_driver_config "$cfg" retrydelay
	get_write_driver_config "$cfg" pollinterval
	get_write_driver_config "$cfg" synchronous
	config_get runas "$cfg" user nut
	RUNAS="$runas"

	echo "" >>"$UPS_C"
}

build_config() {
	local STATEPATH=/var/run/nut

        mkdir -p "$(dirname "$UPS_C")"
	rm -f "$UPS_C"
	echo "# Config file automatically generated from UCI config" > "$UPS_C"
	chmod 0640 "$UPS_C"

	config_load nut_server

	upsd_runas
	config_foreach build_global_driver_config driver_global
	config_foreach build_driver_config driver
	upsd_statepath
	build_server_config
	[ -n "$RUNAS" ] && chgrp "$(id -gn "$RUNAS")" "$UPS_C"
}

start_driver_instance() {
	local cfg="$1"
	local requested="$2"
	local driver
	local STATEPATH=/var/run/nut
	local RUNAS=nut

	[ "$havedriver" != 1 ] && return

	# If wanting a specific instance, only start it
	if [ "$requested" != "$cfg" ] && [ "$request" != "" ]; then
		return 0
	fi

	mkdir -p "$(dirname "$UPS_C")"
	chmod 0755 "$UPS_C"

	upsd_statepath
	build_config

	# Avoid hotplug inadvertenly restarting driver during
	# forced shutdown
	[ -f /var/run/killpower ] && return 0
	if [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ]; then
	       return 0
	fi

	if [ -n "$RUNAS" ]; then
		chown "$RUNAS":"$(id -gn "$RUNAS")" "${STATEPATH}"
		chgrp "$(id -gn "$RUNAS")" "$UPS_C"
	fi

	config_get driver "$cfg" driver "usbhid-ups"
	procd_open_instance "$cfg"
	procd_set_param respawn
	procd_set_param stderr 0
	procd_set_param stdout 1
	procd_set_param command /lib/nut/"${driver}" -D -a "$cfg" ${RUNAS:+-u "$RUNAS"}
	procd_close_instance
}

interface_triggers() {
	local action="$1"
	local triggerlist trigger

	config_get triggerlist upsd triggerlist

	. /lib/functions/network.sh

	if [ -n "$triggerlist" ]; then
		for trigger in $triggerlist; do
			if [ "$action" = "add_trigger" ]; then
				procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/nut-server reload
			else
				network_is_up "$trigger" && return 0
			fi
		done
	else
		if [ "$action" = "add_trigger" ]; then
			procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/nut-server reload
		else
			ubus call network.device status | grep -q '"up": true' && return 0
		fi
	fi
	[ "$action" = "add_trigger" ] || return 1
}

start_server_instance() {
	local cfg="$1"

	[ "$haveserver" != 1 ] && return
	interface_triggers "check_interface_up" || return

	procd_open_instance "$cfg"
	procd_set_param respawn
	procd_set_param stderr 0
	procd_set_param stdout 1
	procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u "$RUNAS"}
	procd_close_instance
}

start_service() {
	local STATEPATH=/var/run/nut

	# Avoid hotplug inadvertenly restarting driver during
	# forced shutdown
	[ -f /var/run/killpower ] && return 0

	config_load nut_server
	build_config

	case $@ in
	"")
		config_foreach start_driver_instance driver "$@"
		start_server_instance upsd
		;;
	*upsd*)
		start_server_instance upsd
		;;
	*)
		config_foreach start_driver_instance driver "$@"
		;;
	esac
}

reload_service() {
	stop_service "$@"
	sleep 2
	start_service "$@"
}

service_triggers() {
	config_load nut_server

	interface_triggers "add_trigger"
	procd_add_reload_trigger "nut_server"
}