freedombone-app-smilodon 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Smilodon ActivityPub app
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim social'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  34. SMILODON_REPO="https://github.com/bashrc/smilodon"
  35. SMILODON_COMMIT='e17dad10f9d4c00516b9c93a587e4298b3639af3'
  36. SMILODON_ADMIN_PASSWORD=
  37. SMILODON_ONION_PORT=8054
  38. SMILODON_PATH=/etc/smilodon
  39. SMILODON_SECRET_KEY=
  40. smilodon_variables=(SMILODON_REPO
  41. SMILODON_ADMIN_PASSWORD
  42. USB_MOUNT
  43. SMILODON_SECRET_KEY
  44. MY_EMAIL_ADDRESS
  45. MY_USERNAME)
  46. function logging_on_smilodon {
  47. echo -n ''
  48. }
  49. function logging_off_smilodon {
  50. echo -n ''
  51. }
  52. function remove_user_smilodon {
  53. remove_username="$1"
  54. }
  55. function add_user_smilodon {
  56. new_username="$1"
  57. new_user_password="$2"
  58. echo '0'
  59. }
  60. function install_interactive_smilodon {
  61. echo -n ''
  62. APP_INSTALLED=1
  63. }
  64. function change_password_smilodon {
  65. curr_username="$1"
  66. new_user_password="$2"
  67. #${PROJECT_NAME}-pass -u "$curr_username" -a smilodon -p "$new_user_password"
  68. }
  69. function smilodon_create_database {
  70. if [ -f $IMAGE_PASSWORD_FILE ]; then
  71. SMILODON_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  72. else
  73. if [ ! $SMILODON_ADMIN_PASSWORD ]; then
  74. SMILODON_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  75. fi
  76. fi
  77. if [ ! $SMILODON_ADMIN_PASSWORD ]; then
  78. return
  79. fi
  80. function_check create_database_mongodb
  81. create_database_mongodb smilodon "$SMILODON_ADMIN_PASSWORD" smilodon
  82. }
  83. function reconfigure_smilodon {
  84. echo -n ''
  85. }
  86. function upgrade_smilodon {
  87. CURR_SMILODON_COMMIT=$(get_completion_param "smilodon commit")
  88. if [[ "$CURR_SMILODON_COMMIT" == "$SMILODON_COMMIT" ]]; then
  89. return
  90. fi
  91. if [[ $(app_is_installed smilodon) == "1" ]]; then
  92. systemctl stop smilodon
  93. function_check set_repo_commit
  94. set_repo_commit $SMILODON_PATH "smilodon commit" "$SMILODON_COMMIT" $SMILODON_REPO
  95. chown -R smilodon:smilodon $SMILODON_PATH
  96. systemctl start smilodon
  97. fi
  98. }
  99. function backup_local_smilodon {
  100. if [ -d $SMILODON_PATH ]; then
  101. systemctl stop smilodon
  102. USE_MONGODB=1
  103. function_check backup_database_to_usb
  104. backup_database_to_usb smilodon
  105. backup_directory_to_usb $SMILODON_PATH smilodon
  106. systemctl start smilodon
  107. fi
  108. }
  109. function restore_local_smilodon {
  110. temp_restore_dir=/root/tempsmilodon
  111. systemctl stop smilodon
  112. function_check smilodon_create_database
  113. smilodon_create_database
  114. USE_MONGODB=1
  115. restore_database smilodon
  116. if [ -d ${SMILODON_PATH} ]; then
  117. if [ -d $temp_restore_dir${SMILODON_PATH} ]; then
  118. if [ -d $temp_restore_dir${SMILODON_PATH} ]; then
  119. rm -rf ${SMILODON_PATH}
  120. mv $temp_restore_dir$SMILODON_PATH ${SMILODON_PATH}/
  121. else
  122. cp -r $temp_restore_dir/* ${SMILODON_PATH}/
  123. fi
  124. if [ ! "$?" = "0" ]; then
  125. function_check backup_unmount_drive
  126. backup_unmount_drive
  127. systemctl start smilodon
  128. exit 528823
  129. fi
  130. chown -R smilodon:smilodon ${SMILODON_PATH}
  131. fi
  132. fi
  133. if [ -d $USB_MOUNT/backup/smilodon ]; then
  134. chown -R smilodon:smilodon ${SMILODON_PATH}
  135. if [ -d $temp_restore_dir ]; then
  136. rm -rf $temp_restore_dir
  137. fi
  138. fi
  139. systemctl start smilodon
  140. }
  141. function backup_remote_smilodon {
  142. if [ -d $SMILODON_PATH ]; then
  143. function_check suspend_site
  144. suspend_site smilodon
  145. systemctl stop smilodon
  146. USE_MONGODB=1
  147. function_check backup_database_to_friend
  148. backup_database_to_friend smilodon
  149. function_check backup_directory_to_friend
  150. backup_directory_to_friend $SMILODON_PATH smilodon
  151. systemctl start smilodon
  152. function_check restart_site
  153. restart_site
  154. else
  155. echo $"Smilodon domain specified but not found in $SMILODON_PATH"
  156. fi
  157. }
  158. function restore_remote_smilodon {
  159. temp_restore_dir=/root/tempsmilodon
  160. if grep -q "smilodon domain" $COMPLETION_FILE; then
  161. echo $"Restoring smilodon"
  162. systemctl stop smilodon
  163. function_check restore_database_from_friend
  164. function_check smilodon_create_database
  165. smilodon_create_database
  166. USE_MONGODB=1
  167. restore_database_from_friend smilodon
  168. if [ -d $SMILODON_PATH ]; then
  169. if [ -d $temp_restore_dir$SMILODON_PATH ]; then
  170. rm -rf $SMILODON_PATH
  171. mv $temp_restore_dir$SMILODON_PATH ${SMILODON_PATH}/
  172. else
  173. cp -r $temp_restore_dir/* ${SMILODON_PATH}/
  174. fi
  175. if [ ! "$?" = "0" ]; then
  176. systemctl start smilodon
  177. exit 6391643
  178. fi
  179. fi
  180. if [ -d $SERVER_DIRECTORY/backup/smilodon ]; then
  181. chown -R smilodon:smilodon ${SMILODON_PATH}
  182. fi
  183. if [ -d /root/tempsmilodon ]; then
  184. rm -rf /root/tempsmilodon
  185. fi
  186. systemctl start smilodon
  187. echo $"Restore of smilodon complete"
  188. fi
  189. }
  190. function remove_smilodon {
  191. nginx_dissite smilodon
  192. systemctl stop smilodon
  193. systemctl disable smilodon
  194. rm /etc/systemd/system/smilodon.service
  195. function_check remove_onion_service
  196. remove_onion_service smilodon ${SMILODON_ONION_PORT}
  197. if [ -f /etc/nginx/sites-available/smilodon ]; then
  198. rm /etc/nginx/sites-available/smilodon
  199. fi
  200. groupdel -f smilodon
  201. userdel -r smilodon
  202. if [ -d $SMILODON_PATH ]; then
  203. rm -rf $SMILODON_PATH
  204. fi
  205. function_check remove_mongodb_user
  206. remove_mongodb_user smilodon
  207. function_check drop_database_mongodb
  208. drop_database_mongodb smilodon
  209. function_check remove_mongodb
  210. remove_mongodb smilodon
  211. remove_completion_param install_smilodon
  212. sed -i '/smilodon/d' $COMPLETION_FILE
  213. }
  214. function install_smilodon {
  215. apt-get -yq install python3-pip
  216. if [ -d $SMILODON_PATH ]; then
  217. rm -rf $SMILODON_PATH
  218. fi
  219. if [ -d /repos/smilodon ]; then
  220. mkdir $SMILODON_PATH
  221. cp -r -p /repos/smilodon/. $SMILODON_PATH
  222. cd $SMILODON_PATH
  223. git pull
  224. else
  225. function_check git_clone
  226. git_clone $SMILODON_REPO $SMILODON_PATH
  227. fi
  228. if [ ! -d $SMILODON_PATH ]; then
  229. echo $'Could not clone smilodon repo'
  230. exit 6784783
  231. fi
  232. cd $SMILODON_PATH
  233. git checkout $SMILODON_COMMIT -b $SMILODON_COMMIT
  234. set_completion_param "smilodon commit" "$SMILODON_COMMIT"
  235. groupadd smilodon
  236. useradd -c "Smilodon system account" -d $SMILODON_PATH -m -r -g smilodon smilodon
  237. function_check install_mongodb
  238. install_mongodb smilodon
  239. smilodon_create_database
  240. SMILODON_ONION_HOSTNAME=$(add_onion_service smilodon 80 ${SMILODON_ONION_PORT})
  241. pip3 install -r requirements.txt
  242. if [ ! "$?" = "0" ]; then
  243. echo $'Unable to install smilodon dependencies'
  244. exit 87352835
  245. fi
  246. echo 'server {' > /etc/nginx/sites-available/smilodon
  247. echo " listen 127.0.0.1:${SMILODON_ONION_PORT} default_server;" >> /etc/nginx/sites-available/smilodon
  248. echo " server_name $SMILODON_ONION_HOSTNAME;" >> /etc/nginx/sites-available/smilodon
  249. echo '' >> /etc/nginx/sites-available/smilodon
  250. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/smilodon
  251. echo ' error_log /dev/null;' >> /etc/nginx/sites-available/smilodon
  252. echo '' >> /etc/nginx/sites-available/smilodon
  253. echo ' location / {' >> /etc/nginx/sites-available/smilodon
  254. echo ' proxy_pass http://localhost:5000;' >> /etc/nginx/sites-available/smilodon
  255. echo ' }' >> /etc/nginx/sites-available/smilodon
  256. echo '}' >> /etc/nginx/sites-available/smilodon
  257. nginx_ensite smilodon
  258. systemctl enable mongodb
  259. systemctl restart mongodb
  260. systemctl restart nginx
  261. chown -R smilodon:smilodon ${SMILODON_PATH}
  262. if [ ! $SMILODON_SECRET_KEY ]; then
  263. SMILODON_SECRET_KEY="$(create_password 30)$(create_password 30)$(create_password 30)$(create_password 30)"
  264. fi
  265. echo '#!/bin/bash' > ${SMILODON_PATH}/run_smilodon.sh
  266. echo "cd ${SMILODON_PATH}" >> ${SMILODON_PATH}/run_smilodon.sh
  267. echo "export smilodon_domain_name=$SMILODON_ONION_HOSTNAME" >> ${SMILODON_PATH}/run_smilodon.sh
  268. echo "export secret_key='$SMILODON_SECRET_KEY'" >> ${SMILODON_PATH}/run_smilodon.sh
  269. echo "export mongodb_username='smilodon'" >> ${SMILODON_PATH}/run_smilodon.sh
  270. echo "export mongodb_password='$SMILODON_ADMIN_PASSWORD'" >> ${SMILODON_PATH}/run_smilodon.sh
  271. echo "export smilodon_admin_address=$MY_EMAIL_ADDRESS" >> ${SMILODON_PATH}/run_smilodon.sh
  272. echo "export MAIL_SERVER='localhost'" >> ${SMILODON_PATH}/run_smilodon.sh
  273. echo "export MAIL_PORT=25" >> ${SMILODON_PATH}/run_smilodon.sh
  274. echo "python3 run.py" >> ${SMILODON_PATH}/run_smilodon.sh
  275. chmod +x ${SMILODON_PATH}/run_smilodon.sh
  276. chown smilodon:smilodon ${SMILODON_PATH}/run_smilodon.sh
  277. echo '[Unit]' > /etc/systemd/system/smilodon.service
  278. echo 'Description=Smilodon ActivityPub messenger' >> /etc/systemd/system/smilodon.service
  279. echo 'After=network.target mongodb.service' >> /etc/systemd/system/smilodon.service
  280. echo 'After=tor.service' >> /etc/systemd/system/smilodon.service
  281. echo '' >> /etc/systemd/system/smilodon.service
  282. echo '[Service]' >> /etc/systemd/system/smilodon.service
  283. echo 'User=smilodon' >> /etc/systemd/system/smilodon.service
  284. echo 'Group=smilodon' >> /etc/systemd/system/smilodon.service
  285. echo "WorkingDirectory=${SMILODON_PATH}/" >> /etc/systemd/system/smilodon.service
  286. echo "ExecStart=${SMILODON_PATH}/run_smilodon.sh" >> /etc/systemd/system/smilodon.service
  287. echo 'Restart=on-failure' >> /etc/systemd/system/smilodon.service
  288. echo 'RestartSec=10' >> /etc/systemd/system/smilodon.service
  289. echo '' >> /etc/systemd/system/smilodon.service
  290. echo '[Install]' >> /etc/systemd/system/smilodon.service
  291. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/smilodon.service
  292. systemctl enable smilodon
  293. systemctl daemon-reload
  294. systemctl start smilodon
  295. ${PROJECT_NAME}-pass -u $MY_USERNAME -a smilodon -p "$SMILODON_ADMIN_PASSWORD"
  296. APP_INSTALLED=1
  297. }
  298. # NOTE: deliberately no exit 0