freedombone-app-tahoelafs 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Distributed storage system introducer
  12. # http://tahoe-lafs.readthedocs.io/en/latest/anonymity-configuration.html
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU Affero General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU Affero General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU Affero General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. VARIANTS='full full-vim cloud'
  32. IN_DEFAULT_INSTALL=0
  33. SHOW_ON_ABOUT=1
  34. TAHOELAFS_REPO="https://github.com/tahoe-lafs/tahoe-lafs"
  35. TAHOELAFS_COMMIT='bb782b0331a60de438136a593bba18338d8d866b'
  36. TAHOELAFS_PORT=50213
  37. TAHOELAFS_ONION_PORT=8096
  38. TAHOELAFS_SHARED_DIR='Shared'
  39. TAHOE_COMMAND="cd /home/tahoelafs/tahoelafs && venv/bin/tahoe"
  40. tahoelafs_variables=(ONION_ONLY
  41. TAHOELAFS_REPO
  42. TAHOELAFS_PORT)
  43. function tahoelafs_setup_config {
  44. config_file=$1
  45. if ! grep -q "[node]" $config_file; then
  46. echo '' >> $config_file
  47. echo '[node]' >> $config_file
  48. fi
  49. if ! grep -q "[connections]" $config_file; then
  50. echo '' >> $config_file
  51. echo '[connections]' >> $config_file
  52. fi
  53. if ! grep -q "reveal-IP-address" $config_file; then
  54. sed -i '/[node]/a reveal-IP-address = False' $config_file
  55. else
  56. sed -i 's|reveal-IP-address.*|reveal-IP-address = False|g' >> $config_file
  57. fi
  58. if ! grep -q "tcp =" $config_file; then
  59. sed -i '/[connections]/a tcp = tor' $config_file
  60. else
  61. sed -i 's|tcp =.*|tcp = tor|g' >> $config_file
  62. fi
  63. if ! grep -q "tub.location =" $config_file; then
  64. sed -i '/[node]/a tub.location = disabled' >> $config_file
  65. fi
  66. if ! grep -q "tub.port =" $config_file; then
  67. sed -i "/[node]/a tub.port = tcp:${TAHOELAFS_ONION_PORT}:interface=127.0.0.1" >> $config_file
  68. fi
  69. sed -i "s|tub.port.*|tub.port = tcp:${TAHOELAFS_ONION_PORT}:interface=127.0.0.1|g" >> $config_file
  70. sed -i "s|tub.location.*|tub.location = tor:${TAHOELAFS_ONION_HOSTNAME}:${TAHOELAFS_PORT}|g" >> $config_file
  71. sed -i "s|nickname =.*|nickname = $HOSTNAME|g" $config_file
  72. if ! grep -q "[storage]" $config_file; then
  73. echo '' >> $config_file
  74. echo '[storage]' >> $config_file
  75. echo 'enabled = false' >> $config_file
  76. fi
  77. }
  78. function install_interactive_tahoelafs {
  79. echo -n ''
  80. APP_INSTALLED=1
  81. }
  82. function upgrade_tahoelafs {
  83. systemctl stop tahoelafs
  84. function_check set_repo_commit
  85. set_repo_commit /home/tahoelafs/tahoelafs "tahoelafs commit" "$TAHOELAFS_COMMIT" $TAHOELAFS_REPO
  86. cd /home/tahoelafs/tahoelafs
  87. git submodule update --init --recursive
  88. virtualenv venv
  89. venv/bin/pip install --editable .
  90. chown -R tahoelafs:tahoelafs /home/tahoelafs
  91. systemctl start tahoelafs
  92. }
  93. function backup_local_tahoelafs {
  94. source_directory=/home/tahoelafs/.tahoe-introducer
  95. if [ ! -d $source_directory ]; then
  96. return
  97. fi
  98. systemctl stop tahoelafs
  99. dest_directory=tahoelafs
  100. function_check backup_directory_to_usb
  101. backup_directory_to_usb $source_directory $dest_directory
  102. systemctl start tahoelafs
  103. }
  104. function restore_local_tahoelafs {
  105. echo $"Restoring Tahoe-LAFS introducer"
  106. systemctl stop tahoelafs
  107. temp_restore_dir=/root/temptahoelafs
  108. restore_directory_from_usb $temp_restore_dir tahoelafs
  109. mv /home/tahoelafs/.tahoe-introducer /home/tahoelafs/.tahoe-introducer-old
  110. cp -r $temp_restore_dir/home/tahoelafs/.tahoe-introducer /home/tahoelafs/
  111. if [ ! "$?" = "0" ]; then
  112. mv /home/tahoelafs/.tahoe-introducer-old /home/tahoelafs/.tahoe-introducer
  113. exit 246833
  114. fi
  115. rm -rf /home/tahoelafs/.tahoe-introducer
  116. chown -R tahoelafs:tahoelafs /home/tahoelafs
  117. systemctl start tahoelafs
  118. echo $"Restore complete"
  119. }
  120. function backup_remote_tahoelafs {
  121. source_directory=/home/tahoelafs/.tahoe-introducer
  122. if [ ! -d $source_directory ]; then
  123. return
  124. fi
  125. systemctl stop tahoelafs
  126. dest_directory=tahoelafs
  127. function_check backup_directory_to_usb
  128. backup_directory_to_friend $source_directory $dest_directory
  129. systemctl start tahoelafs
  130. }
  131. function restore_remote_tahoelafs {
  132. echo $"Restoring Tahoe-LAFS introducer"
  133. systemctl stop tahoelafs
  134. temp_restore_dir=/root/temptahoelafs
  135. restore_directory_from_friend $temp_restore_dir tahoelafs
  136. mv /home/tahoelafs/.tahoe-introducer /home/tahoelafs/.tahoe-introducer-old
  137. cp -r $temp_restore_dir/home/tahoelafs/.tahoe-introducer /home/tahoelafs/
  138. if [ ! "$?" = "0" ]; then
  139. mv /home/tahoelafs/.tahoe-introducer-old /home/tahoelafs/.tahoe-introducer
  140. exit 623925
  141. fi
  142. rm -rf /home/tahoelafs/.tahoe-introducer-old
  143. chown -R tahoelafs:tahoelafs /home/tahoelafs
  144. systemctl start tahoelafs
  145. echo $"Restore complete"
  146. }
  147. function reconfigure_tahoelafs {
  148. echo -n ''
  149. }
  150. function remove_tahoelafs {
  151. systemctl stop tahoelafs
  152. systemctl disable tahoelafs
  153. rm /etc/systemd/system/tahoelafs.service
  154. firewall_remove ${TAHOELAFS_PORT}
  155. rm -rf /var/lib/tahoelafs
  156. remove_completion_param install_tahoelafs
  157. remove_completion_param configure_firewall_for_tahoelafs
  158. function_check remove_onion_service
  159. remove_onion_service tahoelafs ${TAHOELAFS_ONION_PORT}
  160. deluser tahoelafs
  161. if [ -d /home/tahoelafs ]; then
  162. rm -rf /home/tahoelafs
  163. fi
  164. remove_app tahoelafs
  165. }
  166. function configure_firewall_for_tahoelafs {
  167. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  168. return
  169. fi
  170. firewall_add Tahoe-LAFS ${TAHOELAFS_PORT}
  171. mark_completed $FUNCNAME
  172. }
  173. function install_tahoelafs {
  174. if [ $INSTALLING_MESH ]; then
  175. return
  176. fi
  177. apt-get -yq install build-essential python-pip python-dev libffi-dev libssl-dev
  178. apt-get -yq install libcrypto++-dev python-pycryptopp python-cffi python-virtualenv
  179. # create a user to run the introducer
  180. if [ ! -d /home/tahoelafs ]; then
  181. # add a gogs user account
  182. adduser --disabled-login --gecos 'tahoe-lafs' tahoelafs
  183. fi
  184. if [ -d /home/tahoelafs/Maildir ]; then
  185. rm -rf /home/tahoelafs/Maildir
  186. fi
  187. if [ ! -d /home/tahoelafs/.tahoe-introducer ]; then
  188. mkdir /home/tahoelafs/.tahoe-introducer
  189. fi
  190. git_clone $TAHOELAFS_REPO /home/tahoelafs/tahoelafs
  191. cd /home/tahoelafs/tahoelafs
  192. git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT
  193. git submodule update --init --recursive
  194. virtualenv venv
  195. venv/bin/pip install setuptools==11.3
  196. venv/bin/pip install six==1.10.0 packaging==16.8 attrs==16.3.0 appdirs==1.4.2 pycrypto==2.1.0 cffi==1.9.1
  197. venv/bin/pip install cryptography==1.7.2
  198. venv/bin/pip install --editable .
  199. configure_firewall_for_tahoelafs
  200. su -c "$TAHOE_COMMAND create-introducer /home/tahoelafs/.tahoe-introducer" - tahoelafs
  201. TAHOELAFS_CONFIG=/home/tahoelafs/.tahoe-introducer/tahoe.cfg
  202. if [ ! -f $TAHOELAFS_CONFIG ]; then
  203. exit 62831
  204. fi
  205. TAHOELAFS_ONION_HOSTNAME=$(add_onion_service tahoelafs ${TAHOELAFS_PORT} ${TAHOELAFS_ONION_PORT})
  206. tahoelafs_setup_config $TAHOELAFS_CONFIG
  207. chown -R tahoelafs:tahoelafs /home/tahoelafs
  208. TAHOELAFS_DAEMON_FILE=/etc/systemd/system/tahoelafs.service
  209. echo '[Unit]' > $TAHOELAFS_DAEMON_FILE
  210. echo 'Description=Tahoe-LAFS introducer' >> $TAHOELAFS_DAEMON_FILE
  211. echo 'After=syslog.target' >> $TAHOELAFS_DAEMON_FILE
  212. echo 'After=network.target' >> $TAHOELAFS_DAEMON_FILE
  213. echo '' >> $TAHOELAFS_DAEMON_FILE
  214. echo '[Service]' >> $TAHOELAFS_DAEMON_FILE
  215. echo 'Type=simple' >> $TAHOELAFS_DAEMON_FILE
  216. echo "User=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  217. echo "Group=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  218. echo "WorkingDirectory=/home/tahoelafs/tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  219. echo "ExecStart=venv/bin/tahoe start /home/tahoelafs/.tahoe-introducer" >> $TAHOELAFS_DAEMON_FILE
  220. echo "ExecStop=venv/bin/tahoe stop /home/tahoelafs/.tahoe-introducer" >> $TAHOELAFS_DAEMON_FILE
  221. echo 'Restart=on-failure' >> $TAHOELAFS_DAEMON_FILE
  222. echo "Environment=\"USER=tahoelafs\" \"HOME=/home/tahoelafs\"" >> $TAHOELAFS_DAEMON_FILE
  223. echo '' >> $TAHOELAFS_DAEMON_FILE
  224. echo '[Install]' >> $TAHOELAFS_DAEMON_FILE
  225. echo 'WantedBy=multi-user.target' >> $TAHOELAFS_DAEMON_FILE
  226. systemctl enable tahoelafs
  227. systemctl daemon-reload
  228. systemctl start tahoelafs
  229. set_completion_param "tahoelafs commit" "$TAHOELAFS_COMMIT"
  230. APP_INSTALLED=1
  231. }
  232. # NOTE: deliberately no exit 0