freedombone-app-ipfs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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_BASE="github.com/ipfs/go-ipfs"
  32. IPFS_GO_REPO="https://${IPFS_GO_REPO_BASE}"
  33. IPFS_COMMIT='20b06a4cbce8884f5b194da6e98cb11f2c77f166'
  34. IPFS_PORT=4001
  35. IPFS_NODE_VERSION='6.2.2'
  36. IPFS_N_VERSION='2.1.4'
  37. IPFS_JS_VERSION='0.14.3'
  38. IPFS_JS_RONIN_VERSION='0.3.11'
  39. IPFS_KEY_LENGTH=2048
  40. function reconfigure_ipfs {
  41. echo -n ''
  42. }
  43. function upgrade_ipfs_go {
  44. if ! grep -Fxq "install_ipfs_go" $COMPLETION_FILE; then
  45. return
  46. fi
  47. function_check select_go_version
  48. select_go_version
  49. function_check set_repo_commit
  50. set_repo_commit $GOPATH/src/github.com/ipfs/go-ipfs "ipfs commit" "$IPFS_COMMIT" $IPFS_REPO
  51. }
  52. function upgrade_ipfs_js {
  53. if ! grep -Fxq "install_ipfs_js" $COMPLETION_FILE; then
  54. return
  55. fi
  56. npm cache clean -f
  57. npm install -g n
  58. n ${IPFS_NODE_VERSION}
  59. npm install ronin@${IPFS_JS_RONIN_VERSION} --global
  60. npm install ipfs@${IPFS_JS_VERSION} --global
  61. }
  62. function upgrade_ipfs {
  63. upgrade_ipfs_js
  64. upgrade_ipfs_go
  65. }
  66. function backup_local_ipfs {
  67. if ! grep -q "Admin user" $COMPLETION_FILE; then
  68. return
  69. fi
  70. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  71. source_directory=/home/$ADMIN_USERNAME/.ipfs
  72. if [ -d $source_directory ]; then
  73. dest_directory=ipfs
  74. echo $"Backing up $source_directory to $dest_directory"
  75. function_check backup_directory_to_usb
  76. backup_directory_to_usb $source_directory $dest_directory
  77. echo $"Backup to $dest_directory complete"
  78. fi
  79. }
  80. function restore_local_ipfs {
  81. if ! grep -q "Admin user" $COMPLETION_FILE; then
  82. return
  83. fi
  84. if [ -d $USB_MOUNT/backup/ipfs ]; then
  85. echo $"Restoring IPFS"
  86. temp_restore_dir=/root/tempipfs
  87. function_check restore_directory_from_usb
  88. restore_directory_from_usb $temp_restore_dir ipfs
  89. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  90. cp -rf $temp_restore_dir/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  91. if [ ! "$?" = "0" ]; then
  92. rm -rf $temp_restore_dir
  93. function_check set_user_permissions
  94. set_user_permissions
  95. function_check backup_unmount_drive
  96. backup_unmount_drive
  97. exit 27627
  98. fi
  99. rm -rf $temp_restore_dir
  100. echo $"Restore of IPFS complete"
  101. fi
  102. }
  103. function backup_remote_ipfs {
  104. if ! grep -q "Admin user" $COMPLETION_FILE; then
  105. return
  106. fi
  107. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  108. if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
  109. echo $"Backing up IPFS"
  110. backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs
  111. echo $"Backup of IPFS complete"
  112. fi
  113. }
  114. function restore_remote_ipfs {
  115. if [ -d $SERVER_DIRECTORY/backup/ipfs ]; then
  116. echo $"Restoring IPFS"
  117. temp_restore_dir=/root/tempipfs
  118. function_check restore_directory_from_friend
  119. restore_directory_from_friend $temp_restore_dir ipfs
  120. cp -rf $temp_restore_dir/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  121. if [ ! "$?" = "0" ]; then
  122. function_check set_user_permissions
  123. set_user_permissions
  124. rm -rf $temp_restore_dir
  125. exit 276357
  126. fi
  127. rm -rf $temp_restore_dir
  128. echo $"Restore of IPFS complete"
  129. fi
  130. }
  131. function remove_ipfs_go {
  132. if ! grep -Fxq "install_ipfs_go" $COMPLETION_FILE; then
  133. return
  134. fi
  135. function_check select_go_version
  136. select_go_version
  137. systemctl stop ipfs
  138. systemctl disable ipfs
  139. systemctl daemon-reload
  140. rm /etc/systemd/system/ipfs.service
  141. rm -rf $GOPATH/src/github.com/ipfs
  142. iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  143. function_check save_firewall_settings
  144. save_firewall_settings
  145. sed -i '/install_ipfs/d' $COMPLETION_FILE
  146. sed -i '/ipfs/d' $COMPLETION_FILE
  147. sed -i '/configure_firewall_for_ipfs/d' $COMPLETION_FILE
  148. }
  149. function remove_ipfs_js {
  150. if ! grep -Fxq "install_ipfs_js" $COMPLETION_FILE; then
  151. return
  152. fi
  153. systemctl stop ipfs
  154. systemctl disable ipfs
  155. rm /etc/systemd/system/ipfs.service
  156. systemctl daemon-reload
  157. npm uninstall ipfs --global
  158. npm uninstall ronin --global
  159. iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  160. function_check save_firewall_settings
  161. save_firewall_settings
  162. sed -i '/install_ipfs/d' $COMPLETION_FILE
  163. sed -i '/ipfs/d' $COMPLETION_FILE
  164. sed -i '/configure_firewall_for_ipfs/d' $COMPLETION_FILE
  165. }
  166. function remove_ipfs {
  167. remove_ipfs_js
  168. remove_ipfs_go
  169. }
  170. function configure_firewall_for_ipfs {
  171. if grep -Fxq "configure_firewall_for_ipfs" $COMPLETION_FILE; then
  172. return
  173. fi
  174. if [[ $ONION_ONLY != "no" ]]; then
  175. return
  176. fi
  177. iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  178. function_check save_firewall_settings
  179. save_firewall_settings
  180. OPEN_PORTS+=("IPFS $IPFS_PORT")
  181. echo 'configure_firewall_for_ipfs' >> $COMPLETION_FILE
  182. }
  183. function mesh_install_ipfs_js {
  184. if grep -Fxq "mesh_install_ipfs_js" $COMPLETION_FILE; then
  185. return
  186. fi
  187. chroot ${rootdir} apt-get -y install nodejs
  188. chroot ${rootdir} apt-get -y install npm curl
  189. chroot ${rootdir} apt-get -y install libpam0g-dev fuse
  190. if [ ! -f ${rootdir}/usr/bin/nodejs ]; then
  191. echo $'nodejs was not installed'
  192. exit 63962
  193. fi
  194. cat <<EOF > ${rootdir}/root/install-ipfs.sh
  195. #!/bin/bash
  196. PATH="/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/bin"
  197. NODE_PATH="/usr/lib/node_modules"
  198. cp /usr/bin/nodejs /usr/local/bin/node
  199. cp /usr/bin/nodejs /usr/bin/node
  200. /usr/bin/curl -0 -L https://npmjs.org/install.sh | sh
  201. npm install -g n@${IPFS_N_VERSION} --save
  202. n ${IPFS_NODE_VERSION}
  203. npm install -g ronin@${IPFS_JS_RONIN_VERSION} --save
  204. npm install -g ipfs@${IPFS_JS_VERSION} --save
  205. exit 0
  206. EOF
  207. chroot ${rootdir} chmod +x /root/install-ipfs.sh
  208. chroot ${rootdir} /root/install-ipfs.sh
  209. rm -f ${rootdir}/root/install-ipfs.sh
  210. IPFS_PATH=/usr/bin
  211. if [ ! -f ${rootdir}$IPFS_PATH/jsipfs ]; then
  212. exit 637292
  213. fi
  214. # directories to mount to
  215. if [ ! -d ${rootdir}/ipfs ]; then
  216. chroot ${rootdir} mkdir /ipfs
  217. chroot ${rootdir} mkdir /ipns
  218. chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /ipfs
  219. chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /ipns
  220. fi
  221. if [ -f ${rootdir}/etc/fuse.conf ]; then
  222. chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /etc/fuse.conf
  223. fi
  224. if [ -f ${rootdir}/dev/fuse ]; then
  225. chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /dev/fuse
  226. fi
  227. IPFS_DAEMON_NAME=ipfs
  228. IPFS_DAEMON_FILE=${rootdir}/etc/systemd/system/${IPFS_DAEMON_NAME}.service
  229. echo '[Unit]' > $IPFS_DAEMON_FILE
  230. echo 'Description=IPFS javascript daemon' >> $IPFS_DAEMON_FILE
  231. echo 'After=syslog.target' >> $IPFS_DAEMON_FILE
  232. echo 'After=network.target' >> $IPFS_DAEMON_FILE
  233. echo '' >> $IPFS_DAEMON_FILE
  234. echo '[Service]' >> $IPFS_DAEMON_FILE
  235. echo 'Type=simple' >> $IPFS_DAEMON_FILE
  236. echo "User=$MY_USERNAME" >> $IPFS_DAEMON_FILE
  237. echo "Group=$MY_USERNAME" >> $IPFS_DAEMON_FILE
  238. echo "WorkingDirectory=/home/$MY_USERNAME" >> $IPFS_DAEMON_FILE
  239. echo "ExecStart=${IPFS_PATH}/jsipfs daemon --mount" >> $IPFS_DAEMON_FILE
  240. echo 'Restart=on-failure' >> $IPFS_DAEMON_FILE
  241. echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\"" >> $IPFS_DAEMON_FILE
  242. echo '' >> $IPFS_DAEMON_FILE
  243. echo '[Install]' >> $IPFS_DAEMON_FILE
  244. echo 'WantedBy=multi-user.target' >> $IPFS_DAEMON_FILE
  245. chroot ${rootdir} systemctl enable ${IPFS_DAEMON_NAME}
  246. echo 'mesh install_ipfs_js' >> $COMPLETION_FILE
  247. }
  248. function install_ipfs_js {
  249. if [ $INSTALLING_MESH ]; then
  250. mesh_install_ipfs_js
  251. return
  252. fi
  253. if grep -Fxq "install_ipfs_js" $COMPLETION_FILE; then
  254. return
  255. fi
  256. apt-get -y install nodejs
  257. apt-get -y install npm
  258. apt-get -y install libpam0g-dev fuse
  259. if [ ! -f /usr/bin/nodejs ]; then
  260. echo $'nodejs was not installed'
  261. exit 63962
  262. fi
  263. cp /usr/bin/nodejs /usr/local/bin/node
  264. cp /usr/bin/nodejs /usr/bin/node
  265. /usr/bin/curl -0 -L https://npmjs.org/install.sh | sh
  266. /usr/local/bin/npm install -g n@${IPFS_N_VERSION}
  267. /usr/local/bin/n ${IPFS_NODE_VERSION}
  268. /usr/local/bin/npm install -g ronin@${IPFS_JS_RONIN_VERSION}
  269. /usr/local/bin/npm install -g ipfs@${IPFS_JS_VERSION}
  270. IPFS_PATH=/usr/bin
  271. if [ ! -f $IPFS_PATH/jsipfs ]; then
  272. exit 637292
  273. fi
  274. # initialise
  275. su -c "$IPFS_PATH/jsipfs init -b $IPFS_KEY_LENGTH" - $MY_USERNAME
  276. if [ ! -d /home/$MY_USERNAME/.ipfs ]; then
  277. echo "IPFS could not be initialised for user $MY_USERNAME"
  278. exit 7358
  279. fi
  280. # directories to mount to
  281. if [ ! -d /ipfs ]; then
  282. mkdir /ipfs
  283. mkdir /ipns
  284. chown $MY_USERNAME:$MY_USERNAME /ipfs
  285. chown $MY_USERNAME:$MY_USERNAME /ipns
  286. fi
  287. if [ -f /etc/fuse.conf ]; then
  288. chown $MY_USERNAME:$MY_USERNAME /etc/fuse.conf
  289. fi
  290. if [ -f /dev/fuse ]; then
  291. chown $MY_USERNAME:$MY_USERNAME /dev/fuse
  292. fi
  293. IPFS_DAEMON_NAME=ipfs
  294. IPFS_DAEMON_FILE=/etc/systemd/system/${IPFS_DAEMON_NAME}.service
  295. echo '[Unit]' > $IPFS_DAEMON_FILE
  296. echo 'Description=IPFS javascript daemon' >> $IPFS_DAEMON_FILE
  297. echo 'After=syslog.target' >> $IPFS_DAEMON_FILE
  298. echo 'After=network.target' >> $IPFS_DAEMON_FILE
  299. echo '' >> $IPFS_DAEMON_FILE
  300. echo '[Service]' >> $IPFS_DAEMON_FILE
  301. echo 'Type=simple' >> $IPFS_DAEMON_FILE
  302. echo "User=$MY_USERNAME" >> $IPFS_DAEMON_FILE
  303. echo "Group=$MY_USERNAME" >> $IPFS_DAEMON_FILE
  304. echo "WorkingDirectory=/home/$MY_USERNAME" >> $IPFS_DAEMON_FILE
  305. echo "ExecStart=${IPFS_PATH}/jsipfs daemon --mount" >> $IPFS_DAEMON_FILE
  306. echo 'Restart=on-failure' >> $IPFS_DAEMON_FILE
  307. echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\"" >> $IPFS_DAEMON_FILE
  308. echo '' >> $IPFS_DAEMON_FILE
  309. echo '[Install]' >> $IPFS_DAEMON_FILE
  310. echo 'WantedBy=multi-user.target' >> $IPFS_DAEMON_FILE
  311. systemctl enable ${IPFS_DAEMON_NAME}
  312. systemctl daemon-reload
  313. systemctl restart ${IPFS_DAEMON_NAME}
  314. if [ -d /etc/avahi ]; then
  315. su -c "echo $($IPFS_PATH/jsipfs id | grep '\"ID\":' | awk -F '\"' '{print $4}') > /tmp/ipfsid" - $MY_USERNAME
  316. if [ ! -f /tmp/ipfsid ]; then
  317. echo 'No IPFS identity was created'
  318. exit 37895
  319. fi
  320. IPFS_PEER_ID=$(cat /tmp/ipfsid)
  321. if [ ${#IPFS_PEER_ID} -lt 10 ]; then
  322. echo 'Invalid IPFS peer ID'
  323. echo "$IPFS_PEER_ID"
  324. exit 74782
  325. fi
  326. rm /tmp/ipfsid
  327. fi
  328. function_check configure_firewall_for_ipfs
  329. configure_firewall_for_ipfs
  330. echo 'install_ipfs_js' >> $COMPLETION_FILE
  331. }
  332. function mesh_install_ipfs_go {
  333. chroot ${rootdir} apt-get -y install golang libpam0g-dev fuse
  334. mesh_upgrade_golang
  335. GOPATH=/home/git/gvm/pkgsets/go${GO_VERSION}/global
  336. cat <<EOF > ${rootdir}/root/install_ipfs_go.sh
  337. #!/bin/bash
  338. export GOPATH=$GOPATH
  339. if [ ${#GOPATH} -lt 1 ]; then
  340. echo $'GOPATH was not set'
  341. exit 62925
  342. fi
  343. if [ ! -d /home/git ]; then
  344. adduser --disabled-login --gecos 'Gogs' git
  345. # install Go
  346. if ! grep -q "export GOPATH=" ~/.bashrc; then
  347. echo "export GOPATH=\$GOPATH" >> ~/.bashrc
  348. else
  349. sed -i "s|export GOPATH=.*|export GOPATH=\$GOPATH|g" ~/.bashrc
  350. fi
  351. systemctl set-environment GOPATH=\$GOPATH
  352. if ! grep -q "systemctl set-environment GOPATH=" ~/.bashrc; then
  353. echo "systemctl set-environment GOPATH=\$GOPATH" >> ~/.bashrc
  354. else
  355. sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=\$GOPATH|g" ~/.bashrc
  356. fi
  357. if [ ! -d \$GOPATH ]; then
  358. mkdir -p \$GOPATH
  359. fi
  360. fi
  361. IPFS_PATH=\$GOPATH/bin
  362. export PATH="\$GOPATH/bin:\$PATH:"
  363. echo 'export PATH="\$GOPATH/bin:\$PATH:";' >> /home/$MY_USERNAME/.bashrc
  364. if grep -q 'GOPATH/bin' ~/.bashrc; then
  365. sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=\$GOPATH|g" ~/.bashrc
  366. fi
  367. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.bashrc
  368. echo "go get -u ${IPFS_GO_REPO_BASE}/cmd/ipfs"
  369. go get -u ${IPFS_GO_REPO_BASE}/cmd/ipfs
  370. if [ ! "$?" = "0" ]; then
  371. exit 8242
  372. fi
  373. if [ ! -d $GOPATH/src/$IPFS_GO_REPO_BASE ]; then
  374. echo $'go get failed to get ipfs'
  375. exit 63923
  376. fi
  377. cd \$GOPATH/src/$IPFS_GO_REPO_BASE
  378. git checkout $IPFS_COMMIT -b $IPFS_COMMIT
  379. if [ ! "$?" = "0" ]; then
  380. exit 735639
  381. fi
  382. exit 0
  383. EOF
  384. chroot ${rootdir} chmod +x /root/install_ipfs_go.sh
  385. chroot ${rootdir} /root/install_ipfs_go.sh
  386. if [ ! "$?" = "0" ]; then
  387. echo $'Failed to install ipfs go'
  388. cat ${rootdir}/root/install_ipfs_go.sh
  389. rm -f ${rootdir}/root/install_ipfs_go.sh
  390. exit 365728
  391. fi
  392. chroot ${rootdir} rm -f /root/install_ipfs_go.sh
  393. echo '[Unit]' > ${rootdir}/etc/systemd/system/ipfs.service
  394. echo 'Description=IPFS go daemon' >> ${rootdir}/etc/systemd/system/ipfs.service
  395. echo 'After=syslog.target' >> ${rootdir}/etc/systemd/system/ipfs.service
  396. echo 'After=network.target' >> ${rootdir}/etc/systemd/system/ipfs.service
  397. echo '' >> ${rootdir}/etc/systemd/system/ipfs.service
  398. echo '[Service]' >> ${rootdir}/etc/systemd/system/ipfs.service
  399. echo 'Type=simple' >> ${rootdir}/etc/systemd/system/ipfs.service
  400. echo "User=$MY_USERNAME" >> ${rootdir}/etc/systemd/system/ipfs.service
  401. echo "Group=$MY_USERNAME" >> ${rootdir}/etc/systemd/system/ipfs.service
  402. echo "WorkingDirectory=/home/$MY_USERNAME" >> ${rootdir}/etc/systemd/system/ipfs.service
  403. echo "ExecStart=$IPFS_PATH/ipfs daemon --mount" >> ${rootdir}/etc/systemd/system/ipfs.service
  404. echo 'Restart=on-failure' >> ${rootdir}/etc/systemd/system/ipfs.service
  405. echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\" \"GOPATH=$GOPATH\" \"GVM_ROOT=$GVM_HOME\"" >> ${rootdir}/etc/systemd/system/ipfs.service
  406. echo '' >> ${rootdir}/etc/systemd/system/ipfs.service
  407. echo '[Install]' >> ${rootdir}/etc/systemd/system/ipfs.service
  408. echo 'WantedBy=multi-user.target' >> ${rootdir}/etc/systemd/system/ipfs.service
  409. chroot ${rootdir} systemctl enable ipfs
  410. }
  411. function install_ipfs_go {
  412. if [ $INSTALLING_MESH ]; then
  413. mesh_install_ipfs_go
  414. return
  415. fi
  416. if grep -Fxq "install_ipfs_go" $COMPLETION_FILE; then
  417. return
  418. fi
  419. function_check select_go_version
  420. select_go_version
  421. apt-get -y install golang libpam0g-dev fuse
  422. if [ ! -d /home/git ]; then
  423. # add a gogs user account
  424. adduser --disabled-login --gecos 'Gogs' git
  425. # install Go
  426. if ! grep -q "export GOPATH=" ~/.bashrc; then
  427. echo "export GOPATH=$GOPATH" >> ~/.bashrc
  428. else
  429. sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" ~/.bashrc
  430. fi
  431. systemctl set-environment GOPATH=$GOPATH
  432. if ! grep -q "systemctl set-environment GOPATH=" ~/.bashrc; then
  433. echo "systemctl set-environment GOPATH=$GOPATH" >> ~/.bashrc
  434. else
  435. sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
  436. fi
  437. if [ ! -d $GOPATH ]; then
  438. mkdir -p $GOPATH
  439. fi
  440. fi
  441. IPFS_PATH=$GOPATH/bin
  442. export PATH="$GOPATH/bin:$PATH:"
  443. if ! grep -q 'GOPATH/bin' ~/.bashrc; then
  444. echo 'export PATH="$GOPATH/bin:$PATH:";' >> ~/.bashrc
  445. else
  446. sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
  447. fi
  448. # set gopath for the user
  449. if ! grep -q "GOPATH=" /home/$MY_USERNAME/.bashrc; then
  450. echo "export GOPATH=$GOPATH" >> /home/$MY_USERNAME/.bashrc
  451. echo 'export PATH="$GOPATH/bin:$PATH:";' >> /home/$MY_USERNAME/.bashrc
  452. else
  453. sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" /home/$MY_USERNAME/.bashrc
  454. fi
  455. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.bashrc
  456. go get -u ${IPFS_GO_REPO_BASE}/cmd/ipfs
  457. if [ ! "$?" = "0" ]; then
  458. exit 8242
  459. fi
  460. if [ ! -d $GOPATH/src/$IPFS_GO_REPO_BASE ]; then
  461. echo $'go get failed to get ipfs'
  462. exit 63923
  463. fi
  464. cd $GOPATH/src/$IPFS_GO_REPO_BASE
  465. git checkout $IPFS_COMMIT -b $IPFS_COMMIT
  466. if [ ! "$?" = "0" ]; then
  467. exit 735639
  468. fi
  469. if ! grep -q "ipfs commit" $COMPLETION_FILE; then
  470. echo "ipfs commit:$IPFS_COMMIT" >> $COMPLETION_FILE
  471. else
  472. sed -i "s/ipfs commit.*/ipfs commit:$IPFS_COMMIT/g" $COMPLETION_FILE
  473. fi
  474. # initialise
  475. su -c "$IPFS_PATH/ipfs init -b 4096" - $MY_USERNAME
  476. if [ ! -d /home/$MY_USERNAME/.ipfs ]; then
  477. echo "IPFS could not be initialised for user $MY_USERNAME"
  478. exit 7358
  479. fi
  480. # directories to mount to
  481. if [ ! -d /ipfs ]; then
  482. mkdir /ipfs
  483. mkdir /ipns
  484. chown $MY_USERNAME:$MY_USERNAME /ipfs
  485. chown $MY_USERNAME:$MY_USERNAME /ipns
  486. fi
  487. if [ -f /etc/fuse.conf ]; then
  488. chown $MY_USERNAME:$MY_USERNAME /etc/fuse.conf
  489. fi
  490. if [ -f /dev/fuse ]; then
  491. chown $MY_USERNAME:$MY_USERNAME /dev/fuse
  492. fi
  493. echo '[Unit]' > /etc/systemd/system/ipfs.service
  494. echo 'Description=IPFS go daemon' >> /etc/systemd/system/ipfs.service
  495. echo 'After=syslog.target' >> /etc/systemd/system/ipfs.service
  496. echo 'After=network.target' >> /etc/systemd/system/ipfs.service
  497. echo '' >> /etc/systemd/system/ipfs.service
  498. echo '[Service]' >> /etc/systemd/system/ipfs.service
  499. echo 'Type=simple' >> /etc/systemd/system/ipfs.service
  500. echo "User=$MY_USERNAME" >> /etc/systemd/system/ipfs.service
  501. echo "Group=$MY_USERNAME" >> /etc/systemd/system/ipfs.service
  502. echo "WorkingDirectory=/home/$MY_USERNAME" >> /etc/systemd/system/ipfs.service
  503. echo "ExecStart=$IPFS_PATH/ipfs daemon --mount" >> /etc/systemd/system/ipfs.service
  504. echo 'Restart=on-failure' >> /etc/systemd/system/ipfs.service
  505. echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\" \"GOPATH=$GOPATH\" \"GVM_ROOT=$GVM_HOME\"" >> /etc/systemd/system/ipfs.service
  506. echo '' >> /etc/systemd/system/ipfs.service
  507. echo '[Install]' >> /etc/systemd/system/ipfs.service
  508. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/ipfs.service
  509. systemctl enable ipfs
  510. systemctl daemon-reload
  511. systemctl restart ipfs
  512. if [ -d /etc/avahi ]; then
  513. su -c "echo $($IPFS_PATH/ipfs id | grep '\"ID\":' | awk -F '\"' '{print $4}') > /tmp/ipfsid" - $MY_USERNAME
  514. if [ ! -f /tmp/ipfsid ]; then
  515. echo 'No IPFS identity was created'
  516. exit 37895
  517. fi
  518. IPFS_PEER_ID=$(cat /tmp/ipfsid)
  519. if [ ${#IPFS_PEER_ID} -lt 10 ]; then
  520. echo 'Invalid IPFS peer ID'
  521. echo "$IPFS_PEER_ID"
  522. exit 74782
  523. fi
  524. # Add an avahi service
  525. function_check create_avahi_service
  526. create_avahi_service ipfs "ipfs" udp $IPFS_PORT "$IPFS_PEER_ID"
  527. rm /tmp/ipfsid
  528. fi
  529. function_check configure_firewall_for_ipfs
  530. configure_firewall_for_ipfs
  531. echo 'install_ipfs_go' >> $COMPLETION_FILE
  532. }
  533. function install_ipfs {
  534. #install_ipfs_js
  535. install_ipfs_go
  536. }