freedombone-app-ipfs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # ipfs functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  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='mesh'
  31. IPFS_GO_REPO="https://github.com/ipfs/go-ipfs"
  32. IPFS_COMMIT='20b06a4cbce8884f5b194da6e98cb11f2c77f166'
  33. IPFS_PORT=4001
  34. function reconfigure_ipfs {
  35. echo -n ''
  36. }
  37. function upgrade_ipfs {
  38. if ! grep -Fxq "install_ipfs" $COMPLETION_FILE; then
  39. return
  40. fi
  41. function_check select_go_version
  42. select_go_version
  43. function_check set_repo_commit
  44. set_repo_commit $GOPATH/src/github.com/ipfs/go-ipfs "ipfs commit" "$IPFS_COMMIT" $IPFS_REPO
  45. }
  46. function backup_local_ipfs {
  47. if ! grep -q "Admin user" $COMPLETION_FILE; then
  48. return
  49. fi
  50. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  51. source_directory=/home/$ADMIN_USERNAME/.ipfs
  52. if [ -d $source_directory ]; then
  53. dest_directory=ipfs
  54. echo $"Backing up $source_directory to $dest_directory"
  55. function_check backup_directory_to_usb
  56. backup_directory_to_usb $source_directory $dest_directory
  57. echo $"Backup to $dest_directory complete"
  58. fi
  59. }
  60. function restore_local_ipfs {
  61. if ! grep -q "Admin user" $COMPLETION_FILE; then
  62. return
  63. fi
  64. if [ -d $USB_MOUNT/backup/ipfs ]; then
  65. echo $"Restoring IPFS"
  66. temp_restore_dir=/root/tempipfs
  67. function_check restore_directory_from_usb
  68. restore_directory_from_usb $temp_restore_dir ipfs
  69. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  70. cp -rf $temp_restore_dir/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  71. if [ ! "$?" = "0" ]; then
  72. rm -rf $temp_restore_dir
  73. function_check set_user_permissions
  74. set_user_permissions
  75. function_check backup_unmount_drive
  76. backup_unmount_drive
  77. exit 27627
  78. fi
  79. rm -rf $temp_restore_dir
  80. echo $"Restore of IPFS complete"
  81. fi
  82. }
  83. function backup_remote_ipfs {
  84. if ! grep -q "Admin user" $COMPLETION_FILE; then
  85. return
  86. fi
  87. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  88. if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
  89. echo $"Backing up IPFS"
  90. backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs
  91. echo $"Backup of IPFS complete"
  92. fi
  93. }
  94. function restore_remote_ipfs {
  95. if [ -d $SERVER_DIRECTORY/backup/ipfs ]; then
  96. echo $"Restoring IPFS"
  97. temp_restore_dir=/root/tempipfs
  98. function_check restore_directory_from_friend
  99. restore_directory_from_friend $temp_restore_dir ipfs
  100. cp -rf $temp_restore_dir/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  101. if [ ! "$?" = "0" ]; then
  102. function_check set_user_permissions
  103. set_user_permissions
  104. rm -rf $temp_restore_dir
  105. exit 276357
  106. fi
  107. rm -rf $temp_restore_dir
  108. echo $"Restore of IPFS complete"
  109. fi
  110. }
  111. function remove_ipfs {
  112. if ! grep -Fxq "install_ipfs" $COMPLETION_FILE; then
  113. return
  114. fi
  115. function_check select_go_version
  116. select_go_version
  117. systemctl stop ipfs
  118. systemctl disable ipfs
  119. systemctl daemon-reload
  120. rm /etc/systemd/system/ipfs.service
  121. rm -rf $GOPATH/src/github.com/ipfs
  122. iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  123. function_check save_firewall_settings
  124. save_firewall_settings
  125. sed -i '/install_ipfs/d' $COMPLETION_FILE
  126. sed -i '/ipfs /d' $COMPLETION_FILE
  127. sed -i '/configure_firewall_for_ipfs/d' $COMPLETION_FILE
  128. }
  129. function configure_firewall_for_ipfs {
  130. if grep -Fxq "configure_firewall_for_ipfs" $COMPLETION_FILE; then
  131. return
  132. fi
  133. if [[ $ONION_ONLY != "no" ]]; then
  134. return
  135. fi
  136. iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  137. function_check save_firewall_settings
  138. save_firewall_settings
  139. OPEN_PORTS+=("IPFS $IPFS_PORT")
  140. echo 'configure_firewall_for_ipfs' >> $COMPLETION_FILE
  141. }
  142. function install_ipfs {
  143. if grep -Fxq "install_ipfs" $COMPLETION_FILE; then
  144. return
  145. fi
  146. function_check select_go_version
  147. select_go_version
  148. apt-get -y install golang libpam0g-dev fuse
  149. if [ ! -d /home/git ]; then
  150. # add a gogs user account
  151. adduser --disabled-login --gecos 'Gogs' git
  152. # install Go
  153. if ! grep -q "export GOPATH=" ~/.bashrc; then
  154. echo "export GOPATH=$GOPATH" >> ~/.bashrc
  155. else
  156. sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" ~/.bashrc
  157. fi
  158. systemctl set-environment GOPATH=$GOPATH
  159. if ! grep -q "systemctl set-environment GOPATH=" ~/.bashrc; then
  160. echo "systemctl set-environment GOPATH=$GOPATH" >> ~/.bashrc
  161. else
  162. sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
  163. fi
  164. if [ ! -d $GOPATH ]; then
  165. mkdir -p $GOPATH
  166. fi
  167. fi
  168. IPFS_PATH=$GOPATH/bin
  169. export PATH="$GOPATH/bin:$PATH:"
  170. if ! grep -q 'GOPATH/bin' ~/.bashrc; then
  171. echo 'export PATH="$GOPATH/bin:$PATH:";' >> ~/.bashrc
  172. else
  173. sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
  174. fi
  175. # set gopath for the user
  176. if ! grep -q "GOPATH=" /home/$MY_USERNAME/.bashrc; then
  177. echo "export GOPATH=$GOPATH" >> /home/$MY_USERNAME/.bashrc
  178. echo 'export PATH="$GOPATH/bin:$PATH:";' >> /home/$MY_USERNAME/.bashrc
  179. else
  180. sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" /home/$MY_USERNAME/.bashrc
  181. fi
  182. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.bashrc
  183. IPFS_GO_REPO2=$(echo "$IPFS_GO_REPO" | sed 's|https://||g')
  184. go get -u ${IPFS_GO_REPO2}/cmd/ipfs
  185. if [ ! "$?" = "0" ]; then
  186. exit 8242
  187. fi
  188. cd $GOPATH/src/$IPFS_GO_REPO2
  189. git checkout $IPFS_COMMIT -b $IPFS_COMMIT
  190. if ! grep -q "ipfs commit" $COMPLETION_FILE; then
  191. echo "ipfs commit:$IPFS_COMMIT" >> $COMPLETION_FILE
  192. else
  193. sed -i "s/ipfs commit.*/ipfs commit:$IPFS_COMMIT/g" $COMPLETION_FILE
  194. fi
  195. # initialise
  196. su -c "$IPFS_PATH/ipfs init -b 4096" - $MY_USERNAME
  197. if [ ! -d /home/$MY_USERNAME/.ipfs ]; then
  198. echo "IPFS could not be initialised for user $MY_USERNAME"
  199. exit 7358
  200. fi
  201. # directories to mount to
  202. if [ ! -d /ipfs ]; then
  203. mkdir /ipfs
  204. mkdir /ipns
  205. chown $MY_USERNAME:$MY_USERNAME /ipfs
  206. chown $MY_USERNAME:$MY_USERNAME /ipns
  207. fi
  208. if [ -f /etc/fuse.conf ]; then
  209. chown $MY_USERNAME:$MY_USERNAME /etc/fuse.conf
  210. fi
  211. if [ -f /dev/fuse ]; then
  212. chown $MY_USERNAME:$MY_USERNAME /dev/fuse
  213. fi
  214. echo '[Unit]' > /etc/systemd/system/ipfs.service
  215. echo 'Description=IPFS daemon' >> /etc/systemd/system/ipfs.service
  216. echo 'After=syslog.target' >> /etc/systemd/system/ipfs.service
  217. echo 'After=network.target' >> /etc/systemd/system/ipfs.service
  218. echo '' >> /etc/systemd/system/ipfs.service
  219. echo '[Service]' >> /etc/systemd/system/ipfs.service
  220. echo 'Type=simple' >> /etc/systemd/system/ipfs.service
  221. echo "User=$MY_USERNAME" >> /etc/systemd/system/ipfs.service
  222. echo "Group=$MY_USERNAME" >> /etc/systemd/system/ipfs.service
  223. echo "WorkingDirectory=/home/$MY_USERNAME" >> /etc/systemd/system/ipfs.service
  224. echo "ExecStart=$IPFS_PATH/ipfs daemon --mount" >> /etc/systemd/system/ipfs.service
  225. echo 'Restart=on-failure' >> /etc/systemd/system/ipfs.service
  226. echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\" \"GOPATH=$GOPATH\" \"GVM_ROOT=$GVM_HOME\"" >> /etc/systemd/system/ipfs.service
  227. echo '' >> /etc/systemd/system/ipfs.service
  228. echo '[Install]' >> /etc/systemd/system/ipfs.service
  229. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/ipfs.service
  230. systemctl enable ipfs
  231. systemctl daemon-reload
  232. systemctl restart ipfs
  233. if [ -d /etc/avahi ]; then
  234. su -c "echo $($IPFS_PATH/ipfs id | grep '\"ID\":' | awk -F '\"' '{print $4}') > /tmp/ipfsid" - $MY_USERNAME
  235. if [ ! -f /tmp/ipfsid ]; then
  236. echo 'No IPFS identity was created'
  237. exit 37895
  238. fi
  239. IPFS_PEER_ID=$(cat /tmp/ipfsid)
  240. if [ ${#IPFS_PEER_ID} -lt 10 ]; then
  241. echo 'Invalid IPFS peer ID'
  242. echo "$IPFS_PEER_ID"
  243. exit 74782
  244. fi
  245. # Add an avahi service
  246. function_check create_avahi_service
  247. create_avahi_service ipfs "ipfs" udp $IPFS_PORT "$IPFS_PEER_ID"
  248. rm /tmp/ipfsid
  249. fi
  250. function_check configure_firewall_for_ipfs
  251. configure_firewall_for_ipfs
  252. echo 'install_ipfs' >> $COMPLETION_FILE
  253. }