freedombone-app-radicale 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Radicale calendar system
  12. #
  13. # Configuration based upon:
  14. # https://gigacog.com/blog/2016/01/radicale-and-uwsgi-on-nginx-with-systemd
  15. #
  16. # License
  17. # =======
  18. #
  19. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  20. #
  21. # This program is free software: you can redistribute it and/or modify
  22. # it under the terms of the GNU Affero General Public License as published by
  23. # the Free Software Foundation, either version 3 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU Affero General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU Affero General Public License
  32. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. VARIANTS='full full-vim'
  34. IN_DEFAULT_INSTALL=1
  35. SHOW_ON_ABOUT=1
  36. RADICALE_PASSWORD=
  37. RADICALE_ONION_PORT=8106
  38. RADICALE_PORT=5232
  39. RADICALE_DIRECTORY='/etc/radicale'
  40. radicale_variables=(ONION_ONLY
  41. MY_USERNAME
  42. RADICALE_PASSWORD
  43. DEFAULT_DOMAIN_NAME)
  44. function remove_user_radicale {
  45. remove_username="$1"
  46. if grep "$remove_username:" ${RADICALE_DIRECTORY}/users; then
  47. sed -i "/$remove_username/d" ${RADICALE_DIRECTORY}/users
  48. systemctl reload radicale
  49. fi
  50. }
  51. function add_user_radicale {
  52. new_username="$1"
  53. new_user_password="$2"
  54. if ! grep "$new_username:$new_user_password" ${RADICALE_DIRECTORY}/users; then
  55. printf "$new_username:$new_user_password\n" >> ${RADICALE_DIRECTORY}/users
  56. systemctl reload radicale
  57. fi
  58. echo '0'
  59. }
  60. function install_interactive_radicale {
  61. echo -n ''
  62. APP_INSTALLED=1
  63. }
  64. function change_password_radicale {
  65. echo -n ''
  66. }
  67. function reconfigure_radicale {
  68. echo -n ''
  69. }
  70. function upgrade_radicale {
  71. if [ -f /etc/init.d/radicale ]; then
  72. systemctl stop radicale
  73. rm /etc/init.d/radicale
  74. systemctl daemon-reload
  75. systemctl start radicale
  76. fi
  77. }
  78. function backup_local_radicale {
  79. source_directory=${RADICALE_DIRECTORY}
  80. if [ -d $source_directory ]; then
  81. dest_directory=radicale
  82. function_check backup_directory_to_usb
  83. backup_directory_to_usb $source_directory $dest_directory
  84. fi
  85. }
  86. function restore_local_radicale {
  87. if [ -d ${RADICALE_DIRECTORY} ]; then
  88. temp_restore_dir=/root/tempradicale
  89. function_check restore_directory_from_usb
  90. restore_directory_from_usb $temp_restore_dir radicale
  91. cp -r $temp_restore_dir${RADICALE_DIRECTORY}/* ${RADICALE_DIRECTORY}
  92. if [ ! "$?" = "0" ]; then
  93. function_check backup_unmount_drive
  94. backup_unmount_drive
  95. exit 46872
  96. fi
  97. rm -rf $temp_restore_dir
  98. systemctl restart radicale
  99. fi
  100. }
  101. function backup_remote_radicale {
  102. if [ -d ${RADICALE_DIRECTORY} ]; then
  103. echo $"Backing up the radicale settings"
  104. backup_directory_to_friend ${RADICALE_DIRECTORY} radicale
  105. echo $"Backup of radicale settings complete"
  106. fi
  107. }
  108. function restore_remote_radicale {
  109. if [ -d ${RADICALE_DIRECTORY} ]; then
  110. temp_restore_dir=/root/tempradicale
  111. function_check restore_directory_from_friend
  112. restore_directory_from_friend $temp_restore_dir radicale
  113. cp -r $temp_restore_dir${RADICALE_DIRECTORY}/* ${RADICALE_DIRECTORY}
  114. if [ ! "$?" = "0" ]; then
  115. exit 463735
  116. fi
  117. rm -rf $temp_restore_dir
  118. systemctl restart radicale
  119. fi
  120. }
  121. function configure_firewall_for_radicale {
  122. if [ ! -d ${RADICALE_DIRECTORY} ]; then
  123. return
  124. fi
  125. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  126. # docker does its own firewalling
  127. return
  128. fi
  129. if [[ $ONION_ONLY != "no" ]]; then
  130. return
  131. fi
  132. firewall_add radicale ${RADICALE_PORT} tcp
  133. mark_completed $FUNCNAME
  134. }
  135. function remove_radicale {
  136. nginx_dissite radicale
  137. systemctl stop radicale
  138. systemctl stop uwsgi_rundir
  139. systemctl disable radicale
  140. systemctl disable uwsgi_rundir
  141. if [ -f /etc/systemd/system/uwsgi_rundir.service ]; then
  142. rm /etc/systemd/system/uwsgi_rundir.service
  143. fi
  144. if [ -f /etc/systemd/system/radicale.service ]; then
  145. rm /etc/systemd/system/radicale.service
  146. fi
  147. if [ -f /etc/nginx/sites-available/radicale ]; then
  148. rm /etc/nginx/sites-available/radicale
  149. fi
  150. if [ -f /usr/local/bin/uwsgi_rundir.sh ]; then
  151. rm /usr/local/bin/uwsgi_rundir.sh
  152. fi
  153. firewall_remove ${RADICALE_PORT} tcp
  154. function_check remove_onion_service
  155. remove_onion_service radicale ${RADICALE_ONION_PORT}
  156. apt-get -yq remove --purge radicale python-radicale
  157. if [ -d ${RADICALE_DIRECTORY} ]; then
  158. rm -rf ${RADICALE_DIRECTORY}
  159. fi
  160. if [ -d /var/www/radicale ]; then
  161. rm -rf /var/www/radicale
  162. fi
  163. remove_completion_param install_radicale
  164. sed -i '/radicale/d' $COMPLETION_FILE
  165. sed -i '/Radicale/d' /home/$MY_USERNAME/README
  166. }
  167. function install_radicale {
  168. if [[ $ONION_ONLY == 'no' ]]; then
  169. # obtain a cert for the default domain
  170. if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
  171. echo $'Obtaining certificate for the main domain'
  172. create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
  173. fi
  174. fi
  175. apt-get -yq install python-radicale uwsgi uwsgi-plugin-python radicale
  176. if [ ! -d ${RADICALE_DIRECTORY} ]; then
  177. echo $"ERROR: radicale does not appear to have installed"
  178. exit 46372
  179. fi
  180. systemctl stop radicale
  181. if [ -f /etc/init.d/radicale ]; then
  182. rm /etc/init.d/radicale
  183. fi
  184. if [ ! -d ${RADICALE_DIRECTORY}/collections ]; then
  185. mkdir -p ${RADICALE_DIRECTORY}/collections
  186. fi
  187. chown www-data:www-data ${RADICALE_DIRECTORY}/collections
  188. echo '[auth]' > ${RADICALE_DIRECTORY}/config
  189. echo 'type = htpasswd' >> ${RADICALE_DIRECTORY}/config
  190. echo 'htpasswd_filename = ${RADICALE_DIRECTORY}/users' >> ${RADICALE_DIRECTORY}/config
  191. echo 'htpasswd_encryption = crypt' >> ${RADICALE_DIRECTORY}/config
  192. echo '' >> ${RADICALE_DIRECTORY}/config
  193. echo '[rights]' >> ${RADICALE_DIRECTORY}/config
  194. echo 'type = owner_only' >> ${RADICALE_DIRECTORY}/config
  195. echo '' >> ${RADICALE_DIRECTORY}/config
  196. echo '[storage]' >> ${RADICALE_DIRECTORY}/config
  197. echo 'type = filesystem' >> ${RADICALE_DIRECTORY}/config
  198. echo 'filesystem_folder = ${RADICALE_DIRECTORY}/collections' >> ${RADICALE_DIRECTORY}/config
  199. if [ ${#RADICALE_PASSWORD} -lt 8 ]; then
  200. if [ -f $IMAGE_PASSWORD_FILE ]; then
  201. RADICALE_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  202. else
  203. RADICALE_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  204. fi
  205. fi
  206. add_user_radicale "$MY_USERNAME" "$RADICALE_PASSWORD"
  207. if [ ! -f /var/www/radicale ]; then
  208. mkdir /var/www/radicale
  209. fi
  210. echo 'import radicale' > /var/www/radicale/radicale.py
  211. echo 'radicale.log.start()' >> /var/www/radicale/radicale.py
  212. echo 'application = radicale.Application()' >> /var/www/radicale/radicale.py
  213. echo '[uwsgi]' > /var/www/radicale/uwsgi.ini
  214. echo 'plugins = python' >> /var/www/radicale/uwsgi.ini
  215. echo 'socket = /var/run/uwsgi/radicale.sock' >> /var/www/radicale/uwsgi.ini
  216. echo 'chmod-socket = 660' >> /var/www/radicale/uwsgi.ini
  217. echo '' >> /var/www/radicale/uwsgi.ini
  218. echo 'wsgi-file = /var/www/radicale/radicale.py' >> /var/www/radicale/uwsgi.ini
  219. echo 'master' >> /var/www/radicale/uwsgi.ini
  220. echo 'workers = 1' >> /var/www/radicale/uwsgi.ini
  221. echo 'max-requests = 100' >> /var/www/radicale/uwsgi.ini
  222. echo 'harakiri = 30' >> /var/www/radicale/uwsgi.ini
  223. echo 'die-on-term' >> /var/www/radicale/uwsgi.ini
  224. echo '#!/bin/sh' > /usr/local/bin/uwsgi_rundir.sh
  225. echo 'mkdir -p /var/run/uwsgi' >> /usr/local/bin/uwsgi_rundir.sh
  226. echo 'chown www-data:www-data /var/run/uwsgi' >> /usr/local/bin/uwsgi_rundir.sh
  227. chmod +x /usr/local/bin/uwsgi_rundir.sh
  228. echo '[Unit]' > /etc/systemd/system/uwsgi_rundir.service
  229. echo 'Description=UWSGI socket directory' >> /etc/systemd/system/uwsgi_rundir.service
  230. echo 'After=network.target' >> /etc/systemd/system/uwsgi_rundir.service
  231. echo '' >> /etc/systemd/system/uwsgi_rundir.service
  232. echo '[Service]' >> /etc/systemd/system/uwsgi_rundir.service
  233. echo 'Type=simple' >> /etc/systemd/system/uwsgi_rundir.service
  234. echo 'User=root' >> /etc/systemd/system/uwsgi_rundir.service
  235. echo 'ExecStart=/usr/local/bin/uwsgi_rundir.sh' >> /etc/systemd/system/uwsgi_rundir.service
  236. echo '' >> /etc/systemd/system/uwsgi_rundir.service
  237. echo '[Install]' >> /etc/systemd/system/uwsgi_rundir.service
  238. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/uwsgi_rundir.service
  239. systemctl enable uwsgi_rundir
  240. echo '[Unit]' > /etc/systemd/system/radicale.service
  241. echo 'Description=Start uwsgi for radicale' >> /etc/systemd/system/radicale.service
  242. echo 'After=network.target' >> /etc/systemd/system/radicale.service
  243. echo 'Requires=uwsgi_rundir.service' >> /etc/systemd/system/radicale.service
  244. echo '' >> /etc/systemd/system/radicale.service
  245. echo '[Service]' >> /etc/systemd/system/radicale.service
  246. echo 'Type=simple' >> /etc/systemd/system/radicale.service
  247. echo 'User=www-data' >> /etc/systemd/system/radicale.service
  248. echo 'Group=www-data' >> /etc/systemd/system/radicale.service
  249. echo 'ExecStart=/usr/bin/uwsgi --ini /var/www/radicale/uwsgi.ini' >> /etc/systemd/system/radicale.service
  250. echo '' >> /etc/systemd/system/radicale.service
  251. echo '[Install]' >> /etc/systemd/system/radicale.service
  252. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/radicale.service
  253. systemctl enable radicale
  254. systemctl start radicale
  255. RADICALE_ONION_HOSTNAME=$(add_onion_service radicale 80 ${RADICALE_ONION_PORT})
  256. if [[ $ONION_ONLY == 'no' ]]; then
  257. echo 'server {' > /etc/nginx/sites-available/radicale
  258. echo " listen ${RADICALE_PORT} ssl;" >> /etc/nginx/sites-available/radicale
  259. echo " listen [::]:${RADICALE_PORT} ssl;" >> /etc/nginx/sites-available/radicale
  260. echo '' >> /etc/nginx/sites-available/radicale
  261. function_check nginx_ssl
  262. nginx_ssl $DEFAULT_DOMAIN_NAME
  263. function_check nginx_disable_sniffing
  264. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  265. echo '' >> /etc/nginx/sites-available/radicale
  266. echo " server_name $DEFAULT_DOMAIN_NAME;" >> /etc/nginx/sites-available/radicale
  267. echo '' >> /etc/nginx/sites-available/radicale
  268. echo ' location / {' >> /etc/nginx/sites-available/radicale
  269. echo ' uwsgi_pass unix:/var/run/uwsgi/radicale.sock;' >> /etc/nginx/sites-available/radicale
  270. echo ' include uwsgi_params;' >> /etc/nginx/sites-available/radicale
  271. echo ' }' >> /etc/nginx/sites-available/radicale
  272. echo '}' >> /etc/nginx/sites-available/radicale
  273. echo '' >> /etc/nginx/sites-available/radicale
  274. else
  275. echo -n '' > /etc/nginx/sites-available/radicale
  276. fi
  277. echo 'server {' >> /etc/nginx/sites-available/radicale
  278. echo " listen 127.0.0.1:${RADICALE_ONION_PORT} default_server;" >> /etc/nginx/sites-available/radicale
  279. echo '' >> /etc/nginx/sites-available/radicale
  280. echo " server_name ${RADICALE_ONION_HOSTNAME};" >> /etc/nginx/sites-available/radicale
  281. echo '' >> /etc/nginx/sites-available/radicale
  282. echo ' location / {' >> /etc/nginx/sites-available/radicale
  283. echo ' uwsgi_pass unix:/var/run/uwsgi/radicale.sock;' >> /etc/nginx/sites-available/radicale
  284. echo ' include uwsgi_params;' >> /etc/nginx/sites-available/radicale
  285. echo ' }' >> /etc/nginx/sites-available/radicale
  286. echo '}' >> /etc/nginx/sites-available/radicale
  287. # create a certificate
  288. #if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
  289. # if [[ "$(cert_exists $DEFAULT_DOMAIN_NAME)" == "0" ]]; then
  290. # ${PROJECT_NAME}-addcert -h $DEFAULT_DOMAIN_NAME --dhkey ${DH_KEYLENGTH}
  291. # check_certificates $DEFAULT_DOMAIN_NAME
  292. # fi
  293. #fi
  294. update_default_domain
  295. nginx_ensite radicale
  296. systemctl reload nginx
  297. set_completion_param "radicale onion domain" "${RADICALE_ONION_HOSTNAME}"
  298. if ! grep -q "Your RADICALE password is" /home/$MY_USERNAME/README; then
  299. echo '' >> /home/$MY_USERNAME/README
  300. echo $'# RADICALE' >> /home/$MY_USERNAME/README
  301. echo $"RADICALE onion domain: ${RADICALE_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  302. echo $"Your RADICALE password is: ${RADICALE_PASSWORD}" >> /home/$MY_USERNAME/README
  303. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  304. chmod 600 /home/$MY_USERNAME/README
  305. fi
  306. function_check configure_firewall_for_radicale
  307. configure_firewall_for_radicale
  308. APP_INSTALLED=1
  309. }
  310. # NOTE: deliberately no exit 0