freedombone-app-bludit 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # License
  10. # =======
  11. #
  12. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  13. #
  14. # This program is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU Affero General Public License as published by
  16. # the Free Software Foundation, either version 3 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU Affero General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU Affero General Public License
  25. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. VARIANTS='full full-vim'
  27. IN_DEFAULT_INSTALL=0
  28. SHOW_ON_ABOUT=1
  29. BLUDIT_DOMAIN_NAME=
  30. BLUDIT_CODE=
  31. BLUDIT_ONION_PORT=9844
  32. BLUDIT_REPO="https://github.com/bludit/bludit"
  33. BLUDIT_COMMIT='0e27e31a84421b3e6bd000a77bc89c2dff3c446a'
  34. BLUDIT_SHORT_DESCRIPTION=$'Bludit Blog'
  35. BLUDIT_DESCRIPTION=$'Simple Markdown blogging'
  36. BLUDIT_MOBILE_APP_URL=
  37. bludit_variables=(ONION_ONLY
  38. BLUDIT_DOMAIN_NAME
  39. BLUDIT_CODE
  40. DDNS_PROVIDER
  41. MY_USERNAME)
  42. function logging_on_bludit {
  43. echo -n ''
  44. }
  45. function logging_off_bludit {
  46. echo -n ''
  47. }
  48. function remove_user_bludit {
  49. remove_username="$1"
  50. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp bludit
  51. }
  52. function add_user_bludit {
  53. new_username="$1"
  54. new_user_password="$2"
  55. "${PROJECT_NAME}-pass" -u "$new_username" -a bludit -p "$new_user_password"
  56. echo '0'
  57. }
  58. function install_interactive_bludit {
  59. if [ ! "$ONION_ONLY" ]; then
  60. ONION_ONLY='no'
  61. fi
  62. if [[ "$ONION_ONLY" != "no" ]]; then
  63. BLUDIT_DOMAIN_NAME='bludit.local'
  64. write_config_param "BLUDIT_DOMAIN_NAME" "$BLUDIT_DOMAIN_NAME"
  65. else
  66. interactive_site_details "bludit" "BLUDIT_DOMAIN_NAME" "BLUDIT_CODE"
  67. fi
  68. APP_INSTALLED=1
  69. }
  70. function change_password_bludit {
  71. curr_username="$1"
  72. new_user_password="$2"
  73. read_config_param 'BLUDIT_DOMAIN_NAME'
  74. "${PROJECT_NAME}-pass" -u "$curr_username" -a bludit -p "$new_user_password"
  75. }
  76. function reconfigure_bludit {
  77. # This is used if you need to switch identity. Dump old keys and generate new ones
  78. echo -n ''
  79. }
  80. function upgrade_bludit {
  81. CURR_BLUDIT_COMMIT=$(get_completion_param "bludit commit")
  82. if [[ "$CURR_BLUDIT_COMMIT" == "$BLUDIT_COMMIT" ]]; then
  83. return
  84. fi
  85. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  86. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  87. fi
  88. # update to the next commit
  89. set_repo_commit "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" "bludit commit" "$BLUDIT_COMMIT" $BLUDIT_REPO
  90. chown -R www-data:www-data "/var/www/${BLUDIT_DOMAIN_NAME}/htdocs"
  91. }
  92. function backup_local_bludit {
  93. BLUDIT_DOMAIN_NAME='bludit'
  94. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  95. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  96. fi
  97. source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  98. suspend_site "${BLUDIT_DOMAIN_NAME}"
  99. dest_directory=bludit
  100. backup_directory_to_usb "$source_directory" $dest_directory
  101. restart_site
  102. }
  103. function restore_local_bludit {
  104. if ! grep -q "bludit domain" "$COMPLETION_FILE"; then
  105. return
  106. fi
  107. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  108. if [ "$BLUDIT_DOMAIN_NAME" ]; then
  109. temp_restore_dir=/root/tempbludit
  110. bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  111. restore_directory_from_usb $temp_restore_dir bludit
  112. if [ -d $temp_restore_dir ]; then
  113. if [ -d "$temp_restore_dir$bludit_dir" ]; then
  114. cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/
  115. else
  116. if [ ! -d "$bludit_dir" ]; then
  117. mkdir "$bludit_dir"
  118. fi
  119. cp -rp "$temp_restore_dir"/* "$bludit_dir"/
  120. fi
  121. chown -R www-data:www-data "$bludit_dir"
  122. rm -rf $temp_restore_dir
  123. fi
  124. fi
  125. }
  126. function backup_remote_bludit {
  127. BLUDIT_DOMAIN_NAME='bludit'
  128. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  129. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  130. fi
  131. source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  132. suspend_site "${BLUDIT_DOMAIN_NAME}"
  133. dest_directory=bludit
  134. backup_directory_to_friend "$source_directory" $dest_directory
  135. restart_site
  136. }
  137. function restore_remote_bludit {
  138. if ! grep -q "bludit domain" "$COMPLETION_FILE"; then
  139. return
  140. fi
  141. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  142. if [ "$BLUDIT_DOMAIN_NAME" ]; then
  143. temp_restore_dir=/root/tempbludit
  144. bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  145. restore_directory_from_friend $temp_restore_dir bludit
  146. if [ -d $temp_restore_dir ]; then
  147. if [ -d "$temp_restore_dir$bludit_dir" ]; then
  148. cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/
  149. else
  150. if [ ! -d "$bludit_dir" ]; then
  151. mkdir "$bludit_dir"
  152. fi
  153. cp -rp $temp_restore_dir/* "$bludit_dir"/
  154. fi
  155. chown -R www-data:www-data "$bludit_dir"
  156. rm -rf $temp_restore_dir
  157. fi
  158. fi
  159. }
  160. function remove_bludit {
  161. nginx_dissite "$BLUDIT_DOMAIN_NAME"
  162. remove_certs "$BLUDIT_DOMAIN_NAME"
  163. if [ -d "/var/www/$BLUDIT_DOMAIN_NAME" ]; then
  164. rm -rf "/var/www/$BLUDIT_DOMAIN_NAME"
  165. fi
  166. if [ -f "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME" ]; then
  167. rm "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME"
  168. fi
  169. remove_onion_service bludit ${BLUDIT_ONION_PORT}
  170. if grep -q "bludit" /etc/crontab; then
  171. sed -i "/bludit/d" /etc/crontab
  172. fi
  173. remove_app bludit
  174. remove_completion_param install_bludit
  175. sed -i '/bludit/d' "$COMPLETION_FILE"
  176. remove_ddns_domain "$BLUDIT_DOMAIN_NAME"
  177. }
  178. function install_bludit {
  179. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  180. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  181. if [ ! "$BLUDIT_DOMAIN_NAME" ]; then
  182. echo $'No domain name was given'
  183. exit 3568356
  184. fi
  185. if [ -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then
  186. rm -rf "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  187. fi
  188. if [ -d /repos/bludit ]; then
  189. mkdir "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  190. cp -r -p /repos/bludit/. "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  191. cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 324687356
  192. git pull
  193. else
  194. git_clone $BLUDIT_REPO "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  195. fi
  196. if [ ! -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then
  197. echo $'Unable to clone bludit repo'
  198. exit 87525
  199. fi
  200. cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 36587356
  201. git checkout $BLUDIT_COMMIT -b $BLUDIT_COMMIT
  202. set_completion_param "bludit commit" "$BLUDIT_COMMIT"
  203. chmod g+w "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  204. chown -R www-data:www-data "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  205. add_ddns_domain "$BLUDIT_DOMAIN_NAME"
  206. BLUDIT_ONION_HOSTNAME=$(add_onion_service bludit 80 ${BLUDIT_ONION_PORT})
  207. bludit_nginx_site=/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME
  208. if [[ "$ONION_ONLY" == "no" ]]; then
  209. nginx_http_redirect "$BLUDIT_DOMAIN_NAME" "index index.php"
  210. { echo 'server {';
  211. echo ' listen 443 ssl;';
  212. echo ' #listen [::]:443 ssl;';
  213. echo " server_name $BLUDIT_DOMAIN_NAME;";
  214. echo ''; } >> "$bludit_nginx_site"
  215. nginx_compress "$BLUDIT_DOMAIN_NAME"
  216. echo '' >> "$bludit_nginx_site"
  217. echo ' # Security' >> "$bludit_nginx_site"
  218. nginx_ssl "$BLUDIT_DOMAIN_NAME"
  219. nginx_security_options "$BLUDIT_DOMAIN_NAME"
  220. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  221. echo '';
  222. echo ' # Logs';
  223. echo ' access_log /dev/null;';
  224. echo ' error_log /dev/null;';
  225. echo '';
  226. echo ' # Root';
  227. echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;";
  228. echo '';
  229. echo ' index index.php;';
  230. echo ' location ~ \.php {';
  231. echo ' include snippets/fastcgi-php.conf;';
  232. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  233. echo ' fastcgi_read_timeout 30;';
  234. echo ' }';
  235. echo '';
  236. echo ' # Location';
  237. echo ' location / {'; } >> "$bludit_nginx_site"
  238. nginx_limits "$BLUDIT_DOMAIN_NAME" '15m'
  239. { echo " try_files \$uri \$uri/ /index.php?\$args;";
  240. echo ' }';
  241. echo '}'; } >> "$bludit_nginx_site"
  242. else
  243. echo -n '' > "$bludit_nginx_site"
  244. fi
  245. { echo 'server {';
  246. echo " listen 127.0.0.1:$BLUDIT_ONION_PORT default_server;";
  247. echo " server_name $BLUDIT_ONION_HOSTNAME;";
  248. echo ''; } >> "$bludit_nginx_site"
  249. nginx_compress "$BLUDIT_DOMAIN_NAME"
  250. echo '' >> "$bludit_nginx_site"
  251. nginx_security_options "$BLUDIT_DOMAIN_NAME"
  252. { echo '';
  253. echo ' # Logs';
  254. echo ' access_log /dev/null;';
  255. echo ' error_log /dev/null;';
  256. echo '';
  257. echo ' # Root';
  258. echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;";
  259. echo '';
  260. echo ' index index.php;';
  261. echo ' location ~ \.php {';
  262. echo ' include snippets/fastcgi-php.conf;';
  263. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  264. echo ' fastcgi_read_timeout 30;';
  265. echo ' }';
  266. echo '';
  267. echo ' # Location';
  268. echo ' location / {'; } >> "$bludit_nginx_site"
  269. nginx_limits "$BLUDIT_DOMAIN_NAME" '15m'
  270. { echo " try_files \$uri \$uri/ index.php?\$args;";
  271. echo ' }';
  272. echo '}'; } >> "$bludit_nginx_site"
  273. configure_php
  274. create_site_certificate "$BLUDIT_DOMAIN_NAME" 'yes'
  275. nginx_ensite "$BLUDIT_DOMAIN_NAME"
  276. systemctl restart php7.0-fpm
  277. systemctl restart nginx
  278. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bludit -p "$BLUDIT_ADMIN_PASSWORD"
  279. set_completion_param "bludit domain" "$BLUDIT_DOMAIN_NAME"
  280. APP_INSTALLED=1
  281. }
  282. # NOTE: deliberately there is no "exit 0"