You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.5 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2016 OpenWrt.org
  3. START=80
  4. STOP=10
  5. extra_command "export_storage" "<PATH>
  6. - export the storage into the specified folder
  7. - <PATH> can be directly used with the default storage backend of Radicale 2.x.x."
  8. CFGDIR=/var/etc/radicale
  9. SYSCFG=$CFGDIR/config
  10. LOGCFG=$CFGDIR/logging
  11. DATADIR="/srv/radicale"
  12. LOGDIR=""
  13. PGREP="ps | grep '[p]ython.*[r]adicale' 2>/dev/null | awk '{print \$1}' "
  14. # we could start with empty configuration file using defaults
  15. [ -f /etc/config/radicale ] || touch /etc/config/radicale
  16. _uci2radicale() {
  17. local _SYSTMP="$SYSCFG.tmp"
  18. local _LOGTMP="$LOGCFG.tmp"
  19. local _LOPT # list option name
  20. local _LVAL # list option value
  21. local _STYPE # section type
  22. local _SNAME # section name
  23. local _console_level="ERROR" # logging console level
  24. local _file_level="INFO" # logging file level
  25. local _file_path="/var/log/radicale" # logging file path
  26. local _file_maxbytes="8196" # logging file maxBytes
  27. local _file_backupcount="1" # logging file backupCount
  28. local _syslog_level="WARNING" # logging syslog level
  29. # write list values to config
  30. _write_list() {
  31. _write_value "$_LOPT" "$_LVAL" # there might be spaces in _LVAL
  32. _LOPT=""
  33. _LVAL=""
  34. }
  35. _write_value() {
  36. # $1 option
  37. # $2 value
  38. local __OPT=$1
  39. local __VAL=$2
  40. # section "server" ignore option "daemon" and "pid"
  41. [ "$_SNAME" = "server" -a "$__OPT" = "daemon" ] && return 0
  42. [ "$_SNAME" = "server" -a "$__OPT" = "pid" ] && return 0
  43. # section "logging" ignore option "config" (logging config file)
  44. [ "$_SNAME" = "logging" -a "$__OPT" = "config" ] && return 0
  45. # section "auth" ignore option "htpasswd_filename" (htpasswd file)
  46. [ "$_SNAME" = "auth" -a "$__OPT" = "htpasswd_filename" ] && return 0
  47. # section "rights" ignore option "file" (reg-based rights file)
  48. [ "$_SNAME" = "rights" -a "$__OPT" = "file" ] && return 0
  49. # section "headers" replace "_" with "-" in option (UCI problem)
  50. [ "$_SNAME" = "headers" ] && __OPT=$(echo "$__OPT" | sed -e "s#_#-#g")
  51. # save data driectory
  52. [ "$_SNAME" = "storage" -a "$__OPT" = "filesystem_folder" ] && DATADIR="$__VAL"
  53. # special handling for well-known, value needs single quotes
  54. [ "$_SNAME" = "well-known" -a "${__VAL#*\%\(}" != "$__VAL" ] && __VAL="'$__VAL'"
  55. # handling of log settings
  56. if [ "$_STYPE" = "logging" -a "$_SNAME" = "logger" ]; then
  57. eval "_$__OPT='$__VAL'" # set to environment for later use
  58. else
  59. # handle bool
  60. [ "$__VAL" = "0" ] && __VAL="False"
  61. [ "$__VAL" = "1" ] && __VAL="True"
  62. # append data to the corresponding section
  63. sed -i "/\[$_SNAME\]/a $__OPT = $__VAL" $_SYSTMP
  64. fi
  65. }
  66. # redefined callback for sections when calling config_load
  67. config_cb() {
  68. # $1 "Type"
  69. # $2 "Name"
  70. # write out last list option
  71. [ -n "$_LOPT" ] && _write_list
  72. # mark invalid
  73. _STYPE=""
  74. _SNAME=""
  75. # check section type
  76. [ "$1" = "setting" -o "$1" = "logging" ] && {
  77. _STYPE="$1"
  78. _SNAME="$2"
  79. }
  80. # translate section name
  81. [ "$2" = "well_known" ] && _SNAME="well-known"
  82. return 0
  83. }
  84. # redefined callback for lists when calling config_load
  85. list_cb() {
  86. # $1 name of variable
  87. # $2 value
  88. # invalid section type then ignore
  89. [ -z "$_STYPE" -o -z "$_SNAME" ] && return 0
  90. # write out last list option if new list starts
  91. [ -n "$_LOPT" -a "$_LOPT" != "$1" ] && _write_list
  92. # new list option
  93. if [ -z "$_LOPT" ]; then
  94. _LOPT="$1"
  95. _LVAL="$2"
  96. else
  97. _LVAL="$_LVAL, $2"
  98. fi
  99. return 0
  100. }
  101. # redefined callback for options when calling config_load
  102. option_cb() {
  103. # $1 name of variable
  104. # $2 value
  105. local __OPT="$1"
  106. local __VAL="$2"
  107. # invalid section type then ignore
  108. [ -z "$_STYPE" -o -z "$_SNAME" ] && return 0
  109. # ignore list entrys will be handled by list_cb()
  110. [ "${__OPT#*_ITEM}" != "$__OPT" ] && return 0 # ignore lists *_ITEM*
  111. [ "${__OPT#*_LENGTH}" != "$__OPT" ] && return 0 # ignore lists *_LENGTH
  112. # write out last list option and clear
  113. [ -n "$_LOPT" ] && _write_list
  114. # write to file
  115. _write_value "$__OPT" "$__VAL" # there might be spaces in __VAL
  116. return 0
  117. }
  118. # temporary config file
  119. # radicale need read access
  120. mkdir -m0755 -p $CFGDIR
  121. cp /etc/radicale/config.template $_SYSTMP
  122. config_load radicale # calling above config_cb()/option_cb()/list_cb() and write into $_SYSTMP
  123. sed -i "/\[logging\]/a config = /var/etc/radicale/logging" $_SYSTMP # hard-code logging config
  124. sed -i "/\[auth\]/a htpasswd_filename = /etc/radicale/users" $_SYSTMP # hard-code htpasswd
  125. sed -i "/\[rights\]/a file = /etc/radicale/rights" $_SYSTMP # hard-code regexp-based rights
  126. # temporary logging config file
  127. cp /etc/radicale/logging.template $_LOGTMP
  128. LOGDIR="$_file_path"
  129. sed -i "/\[handler_console\]/a level = $_console_level" $_LOGTMP
  130. sed -i "/\[handler_file\]/a level = $_file_level" $_LOGTMP
  131. sed -i "/\[handler_file\]/a args = ('$_file_path/radicale','a',$_file_maxbytes,$_file_backupcount)" $_LOGTMP
  132. sed -i "/\[handler_syslog\]/a level = $_syslog_level" $_LOGTMP
  133. # move tmp to final
  134. mv -f $_SYSTMP $SYSCFG
  135. mv -f $_LOGTMP $LOGCFG
  136. }
  137. _set_permission() {
  138. # config file permissions (read access for group)
  139. chmod 644 $SYSCFG $LOGCFG
  140. chgrp -R radicale $CFGDIR
  141. # log directory (full access and owner)
  142. [ -d $LOGDIR ] || mkdir -m0755 -p $LOGDIR
  143. chown -R radicale:radicale $LOGDIR
  144. # data directory does not exist
  145. [ -d $DATADIR ] || {
  146. logger -p user.error -t "radicale[----]" "Data directory '$DATADIR' does not exists. Startup failed !!!"
  147. exit 1
  148. }
  149. chgrp -R radicale $DATADIR
  150. }
  151. export_storage() {
  152. # if already running do nothing
  153. local _PID=$(eval "$PGREP")
  154. kill -1 $_PID 2>/dev/null && {
  155. echo "Export failed !!! - Service running !" >&2
  156. logger -p user.error -t "radicale[$_PID]" "Export failed !!! - Service running !"
  157. return 1
  158. }
  159. [ $# -ne 1 ] || [ ! -d $1 ] && {
  160. echo "Export failed !!! Directory not given or does not exist !" >&2
  161. logger -p user.error -t "radicale[----]" "Export failed !!! Directory not given or does not exist !"
  162. return 1
  163. }
  164. _uci2radicale
  165. _set_permission
  166. chmod 775 $1
  167. chgrp radicale $1
  168. radicale --config=$SYSCFG --export-storage $1/export
  169. }
  170. boot() {
  171. # wait a given time (default 10 seconds) before startup
  172. # to wait for interfaces to come up / not using hotplug events during boot
  173. _start() {
  174. [ $1 -gt 0 ] && sleep $1
  175. start
  176. }
  177. local _DELAY
  178. _DELAY=$(uci_get "radicale" "system" "boot_delay" "10")
  179. _start $_DELAY &
  180. return 0
  181. }
  182. shutdown() {
  183. rm -f /tmp/radicale.hotplug
  184. stop
  185. }
  186. start() {
  187. _running() {
  188. sleep 2 # give radicale time to completely come up
  189. local _PID=$(eval "$PGREP")
  190. kill -1 $_PID 2>/dev/null
  191. [ $? -eq 0 ] \
  192. && logger -p user.notice -t "radicale[$_PID]" "Service started successfully"\
  193. || logger -p user.warn -t "radicale[----]" "Service failed to start"
  194. }
  195. # if already running do nothing
  196. local _PID=$(eval "$PGREP")
  197. kill -1 $_PID 2>/dev/null && return 0
  198. _uci2radicale
  199. _set_permission
  200. radicale --daemon --config=$SYSCFG
  201. touch /tmp/radicale.hotplug
  202. _running & # check if running and syslog
  203. return 0
  204. }
  205. reload() {
  206. # reload is also used by luci
  207. local _PID=$(eval "$PGREP")
  208. kill -1 $_PID 2>/dev/null
  209. if [ $? -eq 0 ]; then
  210. # only restart if already running
  211. restart
  212. else
  213. # only start if enabled
  214. enabled && start
  215. fi
  216. return 0
  217. }
  218. stop() {
  219. local _PID=$(eval "$PGREP")
  220. [ -z "$_PID" ] && return 0 # not running
  221. kill -15 $_PID 2>/dev/null
  222. sleep 3 # give time to shutdown
  223. local _tmp=$(eval "$PGREP")
  224. if [ -z "$_tmp" ]; then
  225. logger -p user.notice -t "radicale[$_PID]" "Service shutdown successfully"
  226. else
  227. kill -9 $_tmp # Normally never come here
  228. logger -p user.warn -t "radicale[----]" "Service shutdown FORCED"
  229. fi
  230. return 0
  231. }