freedombone-app-rocketchat 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #!/bin/bash
  2. #
  3. # _____ _ _
  4. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  5. # | __| _| -_| -_| . | . | | . | . | | -_|
  6. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  7. #
  8. # Freedom in the Cloud
  9. #
  10. # License
  11. # =======
  12. #
  13. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. VARIANTS='full full-vim'
  28. IN_DEFAULT_INSTALL=0
  29. SHOW_ON_ABOUT=1
  30. NOT_ON_ARM=1
  31. ROCKETCHAT_DOMAIN_NAME=
  32. ROCKETCHAT_CODE=
  33. ROCKETCHAT_ONION_PORT=9722
  34. ROCKETCHAT_PORT_INTERNAL=3000
  35. rocketchat_variables=(ONION_ONLY
  36. ROCKETCHAT_DOMAIN_NAME
  37. ROCKETCHAT_CODE
  38. DDNS_PROVIDER
  39. MY_USERNAME)
  40. function logging_on_rocketchat {
  41. echo -n ''
  42. }
  43. function logging_off_rocketchat {
  44. echo -n ''
  45. }
  46. function remove_user_rocketchat {
  47. remove_username="$1"
  48. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp rocketchat
  49. }
  50. function add_user_rocketchat {
  51. new_username="$1"
  52. new_user_password="$2"
  53. "${PROJECT_NAME}-pass" -u "$new_username" -a rocketchat -p "$new_user_password"
  54. echo '0'
  55. }
  56. function install_interactive_rocketchat {
  57. if [ ! "$ONION_ONLY" ]; then
  58. ONION_ONLY='no'
  59. fi
  60. if [[ "$ONION_ONLY" != "no" ]]; then
  61. ROCKETCHAT_DOMAIN_NAME='rocketchat.local'
  62. write_config_param "ROCKETCHAT_DOMAIN_NAME" "$ROCKETCHAT_DOMAIN_NAME"
  63. else
  64. interactive_site_details "rocketchat" "ROCKETCHAT_DOMAIN_NAME" "ROCKETCHAT_CODE"
  65. fi
  66. APP_INSTALLED=1
  67. }
  68. function change_password_rocketchat {
  69. curr_username="$1"
  70. new_user_password="$2"
  71. read_config_param 'ROCKETCHAT_DOMAIN_NAME'
  72. "${PROJECT_NAME}-pass" -u "$curr_username" -a rocketchat -p "$new_user_password"
  73. }
  74. function reconfigure_rocketchat {
  75. # This is used if you need to switch identity. Dump old keys and generate new ones
  76. echo -n ''
  77. }
  78. function upgrade_rocketchat {
  79. echo -n ''
  80. }
  81. function backup_local_rocketchat {
  82. ROCKETCHAT_DOMAIN_NAME='rocketchat'
  83. if grep -q "rocketchat domain" "$COMPLETION_FILE"; then
  84. ROCKETCHAT_DOMAIN_NAME=$(get_completion_param "rocketchat domain")
  85. fi
  86. source_directory=/var/snap/rocketchat-server
  87. suspend_site "${ROCKETCHAT_DOMAIN_NAME}"
  88. systemctl stop rocketchat
  89. dest_directory=rocketchat
  90. backup_directory_to_usb "$source_directory" $dest_directory
  91. restart_site
  92. systemctl start rocketchat
  93. }
  94. function restore_local_rocketchat {
  95. if ! grep -q "rocketchat domain" "$COMPLETION_FILE"; then
  96. return
  97. fi
  98. ROCKETCHAT_DOMAIN_NAME=$(get_completion_param "rocketchat domain")
  99. if [ ! "$ROCKETCHAT_DOMAIN_NAME" ]; then
  100. return
  101. fi
  102. suspend_site "${ROCKETCHAT_DOMAIN_NAME}"
  103. systemctl stop rocketchat
  104. temp_restore_dir=/root/temprocketchat
  105. rocketchat_dir=/var/snap/rocketchat-server
  106. restore_directory_from_usb $temp_restore_dir rocketchat
  107. if [ -d $temp_restore_dir ]; then
  108. if [ -d "$temp_restore_dir$rocketchat_dir" ]; then
  109. cp -rp "$temp_restore_dir$rocketchat_dir"/* "$rocketchat_dir"/
  110. else
  111. if [ ! -d "$rocketchat_dir" ]; then
  112. mkdir "$rocketchat_dir"
  113. fi
  114. cp -rp "$temp_restore_dir"/* "$rocketchat_dir"/
  115. fi
  116. rm -rf $temp_restore_dir
  117. fi
  118. systemctl start rocketchat
  119. restart_site
  120. }
  121. function backup_remote_rocketchat {
  122. ROCKETCHAT_DOMAIN_NAME='rocketchat'
  123. if grep -q "rocketchat domain" "$COMPLETION_FILE"; then
  124. ROCKETCHAT_DOMAIN_NAME=$(get_completion_param "rocketchat domain")
  125. fi
  126. source_directory=/var/snap/rocketchat-server
  127. suspend_site "${ROCKETCHAT_DOMAIN_NAME}"
  128. systemctl stop rocketchat
  129. dest_directory=rocketchat
  130. backup_directory_to_friend "$source_directory" $dest_directory
  131. systemctl start rocketchat
  132. restart_site
  133. }
  134. function restore_remote_rocketchat {
  135. if ! grep -q "rocketchat domain" "$COMPLETION_FILE"; then
  136. return
  137. fi
  138. ROCKETCHAT_DOMAIN_NAME=$(get_completion_param "rocketchat domain")
  139. if [ ! "$ROCKETCHAT_DOMAIN_NAME" ]; then
  140. return
  141. fi
  142. suspend_site "${ROCKETCHAT_DOMAIN_NAME}"
  143. systemctl stop rocketchat
  144. temp_restore_dir=/root/temprocketchat
  145. rocketchat_dir=/var/snap/rocketchat-server
  146. restore_directory_from_friend $temp_restore_dir rocketchat
  147. if [ -d $temp_restore_dir ]; then
  148. if [ -d "$temp_restore_dir$rocketchat_dir" ]; then
  149. cp -rp "$temp_restore_dir$rocketchat_dir"/* "$rocketchat_dir"/
  150. else
  151. if [ ! -d "$rocketchat_dir" ]; then
  152. mkdir "$rocketchat_dir"
  153. fi
  154. cp -rp $temp_restore_dir/* "$rocketchat_dir"/
  155. fi
  156. rm -rf $temp_restore_dir
  157. fi
  158. systemctl start rocketchat
  159. restart_site
  160. }
  161. function remove_rocketchat {
  162. nginx_dissite "$ROCKETCHAT_DOMAIN_NAME"
  163. remove_certs "$ROCKETCHAT_DOMAIN_NAME"
  164. remove_nodejs rocketchat
  165. if [ -d "/var/www/$ROCKETCHAT_DOMAIN_NAME" ]; then
  166. rm -rf "/var/www/$ROCKETCHAT_DOMAIN_NAME"
  167. fi
  168. if [ -f "/etc/nginx/sites-available/$ROCKETCHAT_DOMAIN_NAME" ]; then
  169. rm "/etc/nginx/sites-available/$ROCKETCHAT_DOMAIN_NAME"
  170. fi
  171. remove_onion_service rocketchat "${ROCKETCHAT_ONION_PORT}"
  172. if grep -q "rocketchat" /etc/crontab; then
  173. sed -i "/rocketchat/d" /etc/crontab
  174. fi
  175. remove_app rocketchat
  176. remove_completion_param install_rocketchat
  177. sed -i '/rocketchat/d' "$COMPLETION_FILE"
  178. remove_ddns_domain "$ROCKETCHAT_DOMAIN_NAME"
  179. remove_snap rocketchat-server
  180. }
  181. function install_rocketchat {
  182. install_snap rocketchat-server
  183. install_nodejs rocketchat
  184. if [ ! "$ROCKETCHAT_DOMAIN_NAME" ]; then
  185. echo $'No domain name was given'
  186. exit 3568356
  187. fi
  188. if [ -d "/var/www/$ROCKETCHAT_DOMAIN_NAME/htdocs" ]; then
  189. rm -rf "/var/www/$ROCKETCHAT_DOMAIN_NAME/htdocs"
  190. fi
  191. mkdir -p "/var/www/$ROCKETCHAT_DOMAIN_NAME/htdocs"
  192. chmod g+w "/var/www/$ROCKETCHAT_DOMAIN_NAME/htdocs"
  193. chown -R www-data:www-data "/var/www/$ROCKETCHAT_DOMAIN_NAME/htdocs"
  194. add_ddns_domain "$ROCKETCHAT_DOMAIN_NAME"
  195. ROCKETCHAT_ONION_HOSTNAME=$(add_onion_service rocketchat 80 "${ROCKETCHAT_ONION_PORT}")
  196. rocketchat_nginx_site=/etc/nginx/sites-available/$ROCKETCHAT_DOMAIN_NAME
  197. if [[ "$ONION_ONLY" == "no" ]]; then
  198. nginx_http_redirect "$ROCKETCHAT_DOMAIN_NAME" "index index.html"
  199. { echo 'server {';
  200. echo ' listen 443 ssl;';
  201. echo ' #listen [::]:443 ssl;';
  202. echo " server_name $ROCKETCHAT_DOMAIN_NAME;";
  203. echo ''; } >> "$rocketchat_nginx_site"
  204. nginx_compress "$ROCKETCHAT_DOMAIN_NAME"
  205. echo '' >> "$rocketchat_nginx_site"
  206. echo ' # Security' >> "$rocketchat_nginx_site"
  207. nginx_ssl "$ROCKETCHAT_DOMAIN_NAME"
  208. nginx_security_options "$ROCKETCHAT_DOMAIN_NAME"
  209. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  210. echo '';
  211. echo ' # Logs';
  212. echo ' access_log /dev/null;';
  213. echo ' error_log /dev/null;';
  214. echo '';
  215. echo ' # Root';
  216. echo " root /var/www/$ROCKETCHAT_DOMAIN_NAME/htdocs;";
  217. echo '';
  218. echo ' index index.html;';
  219. echo ' # Location';
  220. echo ' location / {'; } >> "$rocketchat_nginx_site"
  221. nginx_limits "$ROCKETCHAT_DOMAIN_NAME" '15m'
  222. { echo " proxy_pass http://localhost:$ROCKETCHAT_PORT_INTERNAL;";
  223. echo ' proxy_http_version 1.1;';
  224. echo " proxy_set_header Upgrade \$http_upgrade;";
  225. echo " proxy_set_header Connection \"upgrade\";"
  226. echo " proxy_set_header Host \$http_host;"
  227. echo '';
  228. echo " proxy_set_header X-Real-IP \$remote_addr;";
  229. echo " proxy_set_header X-Forward-For \$proxy_add_x_forwarded_for;";
  230. echo ' proxy_set_header X-Forward-Proto http;';
  231. echo ' proxy_set_header X-Nginx-Proxy true;';
  232. echo '';
  233. echo ' proxy_redirect off;';
  234. echo ' }';
  235. echo '}'; } >> "$rocketchat_nginx_site"
  236. else
  237. echo -n '' > "$rocketchat_nginx_site"
  238. fi
  239. { echo 'server {';
  240. echo " listen 127.0.0.1:$ROCKETCHAT_ONION_PORT default_server;";
  241. echo " server_name $ROCKETCHAT_ONION_HOSTNAME;";
  242. echo ''; } >> "$rocketchat_nginx_site"
  243. nginx_compress "$ROCKETCHAT_DOMAIN_NAME"
  244. echo '' >> "$rocketchat_nginx_site"
  245. nginx_security_options "$ROCKETCHAT_DOMAIN_NAME"
  246. { echo '';
  247. echo ' # Logs';
  248. echo ' access_log /dev/null;';
  249. echo ' error_log /dev/null;';
  250. echo '';
  251. echo ' # Root';
  252. echo " root /var/www/$ROCKETCHAT_DOMAIN_NAME/htdocs;";
  253. echo '';
  254. echo ' index index.html;';
  255. echo ' # Location';
  256. echo ' location / {'; } >> "$rocketchat_nginx_site"
  257. nginx_limits "$ROCKETCHAT_DOMAIN_NAME" '15m'
  258. { echo " proxy_pass http://localhost:$ROCKETCHAT_PORT_INTERNAL;";
  259. echo ' proxy_http_version 1.1;';
  260. echo " proxy_set_header Upgrade \$http_upgrade;";
  261. echo " proxy_set_header Connection \"upgrade\";"
  262. echo " proxy_set_header Host \$http_host;"
  263. echo '';
  264. echo " proxy_set_header X-Real-IP \$remote_addr;";
  265. echo " proxy_set_header X-Forward-For \$proxy_add_x_forwarded_for;";
  266. echo ' proxy_set_header X-Forward-Proto http;';
  267. echo ' proxy_set_header X-Nginx-Proxy true;';
  268. echo '';
  269. echo ' proxy_redirect off;';
  270. echo ' }';
  271. echo '}'; } >> "$rocketchat_nginx_site"
  272. # If content security is enabled then the https site won't load
  273. sed -i 's|add_header Content-Security-Policy|#add_header Content-Security-Policy|g' "$rocketchat_nginx_site"
  274. create_site_certificate "$ROCKETCHAT_DOMAIN_NAME" 'yes'
  275. nginx_ensite "$ROCKETCHAT_DOMAIN_NAME"
  276. systemctl restart nginx
  277. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a rocketchat -p "$ROCKETCHAT_ADMIN_PASSWORD"
  278. set_completion_param "rocketchat domain" "$ROCKETCHAT_DOMAIN_NAME"
  279. APP_INSTALLED=1
  280. }
  281. # NOTE: deliberately there is no "exit 0"