freedombone-app-bludit 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_variables=(ONION_ONLY
  35. BLUDIT_DOMAIN_NAME
  36. BLUDIT_CODE
  37. DDNS_PROVIDER
  38. MY_USERNAME)
  39. function logging_on_bludit {
  40. echo -n ''
  41. }
  42. function logging_off_bludit {
  43. echo -n ''
  44. }
  45. function remove_user_bludit {
  46. remove_username="$1"
  47. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp bludit
  48. }
  49. function add_user_bludit {
  50. new_username="$1"
  51. new_user_password="$2"
  52. "${PROJECT_NAME}-pass" -u "$new_username" -a bludit -p "$new_user_password"
  53. echo '0'
  54. }
  55. function install_interactive_bludit {
  56. if [ ! "$ONION_ONLY" ]; then
  57. ONION_ONLY='no'
  58. fi
  59. if [[ "$ONION_ONLY" != "no" ]]; then
  60. BLUDIT_DOMAIN_NAME='bludit.local'
  61. write_config_param "BLUDIT_DOMAIN_NAME" "$BLUDIT_DOMAIN_NAME"
  62. else
  63. interactive_site_details "bludit" "BLUDIT_DOMAIN_NAME" "BLUDIT_CODE"
  64. fi
  65. APP_INSTALLED=1
  66. }
  67. function change_password_bludit {
  68. curr_username="$1"
  69. new_user_password="$2"
  70. read_config_param 'BLUDIT_DOMAIN_NAME'
  71. "${PROJECT_NAME}-pass" -u "$curr_username" -a bludit -p "$new_user_password"
  72. }
  73. function reconfigure_bludit {
  74. # This is used if you need to switch identity. Dump old keys and generate new ones
  75. echo -n ''
  76. }
  77. function upgrade_bludit {
  78. CURR_BLUDIT_COMMIT=$(get_completion_param "bludit commit")
  79. if [[ "$CURR_BLUDIT_COMMIT" == "$BLUDIT_COMMIT" ]]; then
  80. return
  81. fi
  82. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  83. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  84. fi
  85. # update to the next commit
  86. set_repo_commit "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" "bludit commit" "$BLUDIT_COMMIT" $BLUDIT_REPO
  87. chown -R www-data:www-data "/var/www/${BLUDIT_DOMAIN_NAME}/htdocs"
  88. }
  89. function backup_local_bludit {
  90. BLUDIT_DOMAIN_NAME='bludit'
  91. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  92. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  93. fi
  94. source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  95. suspend_site "${BLUDIT_DOMAIN_NAME}"
  96. dest_directory=bludit
  97. backup_directory_to_usb "$source_directory" $dest_directory
  98. restart_site
  99. }
  100. function restore_local_bludit {
  101. if ! grep -q "bludit domain" "$COMPLETION_FILE"; then
  102. return
  103. fi
  104. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  105. if [ "$BLUDIT_DOMAIN_NAME" ]; then
  106. temp_restore_dir=/root/tempbludit
  107. bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  108. restore_directory_from_usb $temp_restore_dir bludit
  109. if [ -d $temp_restore_dir ]; then
  110. if [ -d "$temp_restore_dir$bludit_dir" ]; then
  111. cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/
  112. else
  113. if [ ! -d "$bludit_dir" ]; then
  114. mkdir "$bludit_dir"
  115. fi
  116. cp -rp "$temp_restore_dir"/* "$bludit_dir"/
  117. fi
  118. chown -R www-data:www-data "$bludit_dir"
  119. rm -rf $temp_restore_dir
  120. fi
  121. fi
  122. }
  123. function backup_remote_bludit {
  124. BLUDIT_DOMAIN_NAME='bludit'
  125. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  126. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  127. fi
  128. source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  129. suspend_site "${BLUDIT_DOMAIN_NAME}"
  130. dest_directory=bludit
  131. backup_directory_to_friend "$source_directory" $dest_directory
  132. restart_site
  133. }
  134. function restore_remote_bludit {
  135. if ! grep -q "bludit domain" "$COMPLETION_FILE"; then
  136. return
  137. fi
  138. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  139. if [ "$BLUDIT_DOMAIN_NAME" ]; then
  140. temp_restore_dir=/root/tempbludit
  141. bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  142. restore_directory_from_friend $temp_restore_dir bludit
  143. if [ -d $temp_restore_dir ]; then
  144. if [ -d "$temp_restore_dir$bludit_dir" ]; then
  145. cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/
  146. else
  147. if [ ! -d "$bludit_dir" ]; then
  148. mkdir "$bludit_dir"
  149. fi
  150. cp -rp $temp_restore_dir/* "$bludit_dir"/
  151. fi
  152. chown -R www-data:www-data "$bludit_dir"
  153. rm -rf $temp_restore_dir
  154. fi
  155. fi
  156. }
  157. function remove_bludit {
  158. nginx_dissite "$BLUDIT_DOMAIN_NAME"
  159. remove_certs "$BLUDIT_DOMAIN_NAME"
  160. if [ -d "/var/www/$BLUDIT_DOMAIN_NAME" ]; then
  161. rm -rf "/var/www/$BLUDIT_DOMAIN_NAME"
  162. fi
  163. if [ -f "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME" ]; then
  164. rm "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME"
  165. fi
  166. remove_onion_service bludit ${BLUDIT_ONION_PORT}
  167. if grep -q "bludit" /etc/crontab; then
  168. sed -i "/bludit/d" /etc/crontab
  169. fi
  170. remove_app bludit
  171. remove_completion_param install_bludit
  172. sed -i '/bludit/d' "$COMPLETION_FILE"
  173. remove_ddns_domain "$BLUDIT_DOMAIN_NAME"
  174. }
  175. function install_bludit {
  176. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  177. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  178. if [ ! "$BLUDIT_DOMAIN_NAME" ]; then
  179. echo $'No domain name was given'
  180. exit 3568356
  181. fi
  182. if [ -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then
  183. rm -rf "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  184. fi
  185. if [ -d /repos/bludit ]; then
  186. mkdir "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  187. cp -r -p /repos/bludit/. "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  188. cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 324687356
  189. git pull
  190. else
  191. git_clone $BLUDIT_REPO "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  192. fi
  193. if [ ! -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then
  194. echo $'Unable to clone bludit repo'
  195. exit 87525
  196. fi
  197. cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 36587356
  198. git checkout $BLUDIT_COMMIT -b $BLUDIT_COMMIT
  199. set_completion_param "bludit commit" "$BLUDIT_COMMIT"
  200. chmod g+w "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  201. chown -R www-data:www-data "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  202. add_ddns_domain "$BLUDIT_DOMAIN_NAME"
  203. BLUDIT_ONION_HOSTNAME=$(add_onion_service bludit 80 ${BLUDIT_ONION_PORT})
  204. bludit_nginx_site=/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME
  205. if [[ "$ONION_ONLY" == "no" ]]; then
  206. nginx_http_redirect "$BLUDIT_DOMAIN_NAME" "index index.php"
  207. { echo 'server {';
  208. echo ' listen 443 ssl;';
  209. echo ' #listen [::]:443 ssl;';
  210. echo " server_name $BLUDIT_DOMAIN_NAME;";
  211. echo ''; } >> "$bludit_nginx_site"
  212. nginx_compress "$BLUDIT_DOMAIN_NAME"
  213. echo '' >> "$bludit_nginx_site"
  214. echo ' # Security' >> "$bludit_nginx_site"
  215. nginx_ssl "$BLUDIT_DOMAIN_NAME"
  216. nginx_security_options "$BLUDIT_DOMAIN_NAME"
  217. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  218. echo '';
  219. echo ' # Logs';
  220. echo ' access_log /dev/null;';
  221. echo ' error_log /dev/null;';
  222. echo '';
  223. echo ' # Root';
  224. echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;";
  225. echo '';
  226. echo ' index index.php;';
  227. echo ' location ~ \.php {';
  228. echo ' include snippets/fastcgi-php.conf;';
  229. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  230. echo ' fastcgi_read_timeout 30;';
  231. echo ' }';
  232. echo '';
  233. echo ' # Location';
  234. echo ' location / {'; } >> "$bludit_nginx_site"
  235. nginx_limits "$BLUDIT_DOMAIN_NAME" '15m'
  236. { echo " try_files \$uri \$uri/ /index.php?\$args;";
  237. echo ' }';
  238. echo '}'; } >> "$bludit_nginx_site"
  239. else
  240. echo -n '' > "$bludit_nginx_site"
  241. fi
  242. { echo 'server {';
  243. echo " listen 127.0.0.1:$BLUDIT_ONION_PORT default_server;";
  244. echo " server_name $BLUDIT_ONION_HOSTNAME;";
  245. echo ''; } >> "$bludit_nginx_site"
  246. nginx_compress "$BLUDIT_DOMAIN_NAME"
  247. echo '' >> "$bludit_nginx_site"
  248. nginx_security_options "$BLUDIT_DOMAIN_NAME"
  249. { echo '';
  250. echo ' # Logs';
  251. echo ' access_log /dev/null;';
  252. echo ' error_log /dev/null;';
  253. echo '';
  254. echo ' # Root';
  255. echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;";
  256. echo '';
  257. echo ' index index.php;';
  258. echo ' location ~ \.php {';
  259. echo ' include snippets/fastcgi-php.conf;';
  260. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  261. echo ' fastcgi_read_timeout 30;';
  262. echo ' }';
  263. echo '';
  264. echo ' # Location';
  265. echo ' location / {'; } >> "$bludit_nginx_site"
  266. nginx_limits "$BLUDIT_DOMAIN_NAME" '15m'
  267. { echo " try_files \$uri \$uri/ index.php?\$args;";
  268. echo ' }';
  269. echo '}'; } >> "$bludit_nginx_site"
  270. configure_php
  271. create_site_certificate "$BLUDIT_DOMAIN_NAME" 'yes'
  272. nginx_ensite "$BLUDIT_DOMAIN_NAME"
  273. systemctl restart php7.0-fpm
  274. systemctl restart nginx
  275. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bludit -p "$BLUDIT_ADMIN_PASSWORD"
  276. set_completion_param "bludit domain" "$BLUDIT_DOMAIN_NAME"
  277. APP_INSTALLED=1
  278. }
  279. # NOTE: deliberately there is no "exit 0"