freedombone-app-bludit 10KB

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