blob: 2bde8b280fae66373a06855f4ccbcb708b10bee3 (
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
|
#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2016 OpenWrt.org
START=80
STOP=10
extra_command "export_storage" "<PATH>
- export the storage into the specified folder
- <PATH> can be directly used with the default storage backend of Radicale 2.x.x."
CFGDIR=/var/etc/radicale
SYSCFG=$CFGDIR/config
LOGCFG=$CFGDIR/logging
DATADIR="/srv/radicale"
LOGDIR=""
PGREP="ps | grep '[p]ython.*[r]adicale' 2>/dev/null | awk '{print \$1}' "
# we could start with empty configuration file using defaults
[ -f /etc/config/radicale ] || touch /etc/config/radicale
_uci2radicale() {
local _SYSTMP="$SYSCFG.tmp"
local _LOGTMP="$LOGCFG.tmp"
local _LOPT # list option name
local _LVAL # list option value
local _STYPE # section type
local _SNAME # section name
local _console_level="ERROR" # logging console level
local _file_level="INFO" # logging file level
local _file_path="/var/log/radicale" # logging file path
local _file_maxbytes="8196" # logging file maxBytes
local _file_backupcount="1" # logging file backupCount
local _syslog_level="WARNING" # logging syslog level
# write list values to config
_write_list() {
_write_value "$_LOPT" "$_LVAL" # there might be spaces in _LVAL
_LOPT=""
_LVAL=""
}
_write_value() {
# $1 option
# $2 value
local __OPT=$1
local __VAL=$2
# section "server" ignore option "daemon" and "pid"
[ "$_SNAME" = "server" -a "$__OPT" = "daemon" ] && return 0
[ "$_SNAME" = "server" -a "$__OPT" = "pid" ] && return 0
# section "logging" ignore option "config" (logging config file)
[ "$_SNAME" = "logging" -a "$__OPT" = "config" ] && return 0
# section "auth" ignore option "htpasswd_filename" (htpasswd file)
[ "$_SNAME" = "auth" -a "$__OPT" = "htpasswd_filename" ] && return 0
# section "rights" ignore option "file" (reg-based rights file)
[ "$_SNAME" = "rights" -a "$__OPT" = "file" ] && return 0
# section "headers" replace "_" with "-" in option (UCI problem)
[ "$_SNAME" = "headers" ] && __OPT=$(echo "$__OPT" | sed -e "s#_#-#g")
# save data driectory
[ "$_SNAME" = "storage" -a "$__OPT" = "filesystem_folder" ] && DATADIR="$__VAL"
# special handling for well-known, value needs single quotes
[ "$_SNAME" = "well-known" -a "${__VAL#*\%\(}" != "$__VAL" ] && __VAL="'$__VAL'"
# handling of log settings
if [ "$_STYPE" = "logging" -a "$_SNAME" = "logger" ]; then
eval "_$__OPT='$__VAL'" # set to environment for later use
else
# handle bool
[ "$__VAL" = "0" ] && __VAL="False"
[ "$__VAL" = "1" ] && __VAL="True"
# append data to the corresponding section
sed -i "/\[$_SNAME\]/a $__OPT = $__VAL" $_SYSTMP
fi
}
# redefined callback for sections when calling config_load
config_cb() {
# $1 "Type"
# $2 "Name"
# write out last list option
[ -n "$_LOPT" ] && _write_list
# mark invalid
_STYPE=""
_SNAME=""
# check section type
[ "$1" = "setting" -o "$1" = "logging" ] && {
_STYPE="$1"
_SNAME="$2"
}
# translate section name
[ "$2" = "well_known" ] && _SNAME="well-known"
return 0
}
# redefined callback for lists when calling config_load
list_cb() {
# $1 name of variable
# $2 value
# invalid section type then ignore
[ -z "$_STYPE" -o -z "$_SNAME" ] && return 0
# write out last list option if new list starts
[ -n "$_LOPT" -a "$_LOPT" != "$1" ] && _write_list
# new list option
if [ -z "$_LOPT" ]; then
_LOPT="$1"
_LVAL="$2"
else
_LVAL="$_LVAL, $2"
fi
return 0
}
# redefined callback for options when calling config_load
option_cb() {
# $1 name of variable
# $2 value
local __OPT="$1"
local __VAL="$2"
# invalid section type then ignore
[ -z "$_STYPE" -o -z "$_SNAME" ] && return 0
# ignore list entrys will be handled by list_cb()
[ "${__OPT#*_ITEM}" != "$__OPT" ] && return 0 # ignore lists *_ITEM*
[ "${__OPT#*_LENGTH}" != "$__OPT" ] && return 0 # ignore lists *_LENGTH
# write out last list option and clear
[ -n "$_LOPT" ] && _write_list
# write to file
_write_value "$__OPT" "$__VAL" # there might be spaces in __VAL
return 0
}
# temporary config file
# radicale need read access
mkdir -m0755 -p $CFGDIR
cp /etc/radicale/config.template $_SYSTMP
config_load radicale # calling above config_cb()/option_cb()/list_cb() and write into $_SYSTMP
sed -i "/\[logging\]/a config = /var/etc/radicale/logging" $_SYSTMP # hard-code logging config
sed -i "/\[auth\]/a htpasswd_filename = /etc/radicale/users" $_SYSTMP # hard-code htpasswd
sed -i "/\[rights\]/a file = /etc/radicale/rights" $_SYSTMP # hard-code regexp-based rights
# temporary logging config file
cp /etc/radicale/logging.template $_LOGTMP
LOGDIR="$_file_path"
sed -i "/\[handler_console\]/a level = $_console_level" $_LOGTMP
sed -i "/\[handler_file\]/a level = $_file_level" $_LOGTMP
sed -i "/\[handler_file\]/a args = ('$_file_path/radicale','a',$_file_maxbytes,$_file_backupcount)" $_LOGTMP
sed -i "/\[handler_syslog\]/a level = $_syslog_level" $_LOGTMP
# move tmp to final
mv -f $_SYSTMP $SYSCFG
mv -f $_LOGTMP $LOGCFG
}
_set_permission() {
# config file permissions (read access for group)
chmod 644 $SYSCFG $LOGCFG
chgrp -R radicale $CFGDIR
# log directory (full access and owner)
[ -d $LOGDIR ] || mkdir -m0755 -p $LOGDIR
chown -R radicale:radicale $LOGDIR
# data directory does not exist
[ -d $DATADIR ] || {
logger -p user.error -t "radicale[----]" "Data directory '$DATADIR' does not exists. Startup failed !!!"
exit 1
}
chgrp -R radicale $DATADIR
}
export_storage() {
# if already running do nothing
local _PID=$(eval "$PGREP")
kill -1 $_PID 2>/dev/null && {
echo "Export failed !!! - Service running !" >&2
logger -p user.error -t "radicale[$_PID]" "Export failed !!! - Service running !"
return 1
}
[ $# -ne 1 ] || [ ! -d $1 ] && {
echo "Export failed !!! Directory not given or does not exist !" >&2
logger -p user.error -t "radicale[----]" "Export failed !!! Directory not given or does not exist !"
return 1
}
_uci2radicale
_set_permission
chmod 775 $1
chgrp radicale $1
radicale --config=$SYSCFG --export-storage $1/export
}
boot() {
# wait a given time (default 10 seconds) before startup
# to wait for interfaces to come up / not using hotplug events during boot
_start() {
[ $1 -gt 0 ] && sleep $1
start
}
local _DELAY
_DELAY=$(uci_get "radicale" "system" "boot_delay" "10")
_start $_DELAY &
return 0
}
shutdown() {
rm -f /tmp/radicale.hotplug
stop
}
start() {
_running() {
sleep 2 # give radicale time to completely come up
local _PID=$(eval "$PGREP")
kill -1 $_PID 2>/dev/null
[ $? -eq 0 ] \
&& logger -p user.notice -t "radicale[$_PID]" "Service started successfully"\
|| logger -p user.warn -t "radicale[----]" "Service failed to start"
}
# if already running do nothing
local _PID=$(eval "$PGREP")
kill -1 $_PID 2>/dev/null && return 0
_uci2radicale
_set_permission
radicale --daemon --config=$SYSCFG
touch /tmp/radicale.hotplug
_running & # check if running and syslog
return 0
}
reload() {
# reload is also used by luci
local _PID=$(eval "$PGREP")
kill -1 $_PID 2>/dev/null
if [ $? -eq 0 ]; then
# only restart if already running
restart
else
# only start if enabled
enabled && start
fi
return 0
}
stop() {
local _PID=$(eval "$PGREP")
[ -z "$_PID" ] && return 0 # not running
kill -15 $_PID 2>/dev/null
sleep 3 # give time to shutdown
local _tmp=$(eval "$PGREP")
if [ -z "$_tmp" ]; then
logger -p user.notice -t "radicale[$_PID]" "Service shutdown successfully"
else
kill -9 $_tmp # Normally never come here
logger -p user.warn -t "radicale[----]" "Service shutdown FORCED"
fi
return 0
}
|