freedombone-utils-go 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Go 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. # For reasons unknown we initially have to upgrade to an intermediate version
  31. # before getting to the version we want
  32. GO_INTERMEDIATE_VERSION=1.4.2
  33. GO_VERSION=1.7
  34. GO_REPO="https://go.googlesource.com/go"
  35. GO_PACKAGE_MANAGER_REPO="https://github.com/gpmgo/gopm"
  36. GVM_HOME=/home/git/gvm
  37. GVM_REPO="https://github.com/moovweb/gvm"
  38. GVM_COMMIT='25ea8ae158e2861c92e2b22c458e60840157832f'
  39. function select_go_version {
  40. if [ ! -d $GVM_HOME/bin ]; then
  41. echo $'GVM was not installed'
  42. exit 629532
  43. fi
  44. export GVM_ROOT=$GVM_HOME
  45. if ! grep -q "GVM_ROOT=" ~/.bashrc; then
  46. echo "export GVM_ROOT=$GVM_ROOT" >> ~/.bashrc
  47. else
  48. sed -i "s|export GVM_ROOT=.*|export GVM_ROOT=$GVM_ROOT|g" ~/.bashrc
  49. fi
  50. cd $GVM_ROOT/bin
  51. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  52. gvm use go${GO_VERSION} --default
  53. if [ ${#GOPATH} -lt 2 ]; then
  54. echo $'GOPATH was not set'
  55. exit 629825
  56. fi
  57. systemctl set-environment GOPATH=$GOPATH
  58. }
  59. function mesh_upgrade_golang_from_source {
  60. chroot "$rootdir" adduser --disabled-login --gecos 'go' ipfs
  61. git clone $GO_REPO $rootdir/home/go/go${GO_VERSION}
  62. cd $rootdir/home/go/go${GO_VERSION}
  63. git checkout go${GO_VERSION} -b go${GO_VERSION}
  64. git clone $GO_REPO $rootdir/home/go/go${GO_INTERMEDIATE_VERSION}
  65. cd $rootdir/home/go/go${GO_INTERMEDIATE_VERSION}
  66. git checkout go${GO_INTERMEDIATE_VERSION} -b go${GO_INTERMEDIATE_VERSION}
  67. cat <<EOF > ${rootdir}/root/upgrade_golang.sh
  68. #!/bin/bash
  69. apt-get -y install build-essential libc6-dev
  70. apt-get -y install gcc-multilib g++-multilib make
  71. apt-get -y install curl git mercurial binutils bison
  72. if [ -d /home/go/Maildir ]; then
  73. rm -rf /home/go/Maildir
  74. fi
  75. export GOROOT=/home/go
  76. export GOPATH=/home/go/go${GO_INTERMEDIATE_VERSION}
  77. cd /home/go/go${GO_INTERMEDIATE_VERSION}/src
  78. ./all.bash
  79. if [ ! -f /home/go/go${GO_INTERMEDIATE_VERSION}/bin/go ]; then
  80. exit 63722
  81. fi
  82. export GOROOT_BOOTSTRAP=/home/go/go${GO_INTERMEDIATE_VERSION}
  83. cd /home/go/go${GO_VERSION}/src
  84. ./all.bash
  85. if [ ! -f /home/go/go${GO_VERSION}/bin/go ]; then
  86. exit 528352
  87. fi
  88. chown -R go:go /home/go
  89. export GOPATH=/home/go/go${GO_VERSION}
  90. export GOROOT=/home/go
  91. echo "export GOPATH=/home/go/go${GO_VERSION}" >> /home/go/.bashrc
  92. echo "export GOPATH=/home/go/go${GO_VERSION}" >> ~/.bashrc
  93. echo "export GOROOT=/home/go" >> /home/go/.bashrc
  94. echo "export GOROOT=/home/go" >> ~/.bashrc
  95. echo "systemctl set-environment GOPATH=\$GOPATH" >> ~/.bashrc
  96. echo "systemctl set-environment GOROOT=\$GOROOT" >> ~/.bashrc
  97. echo "systemctl set-environment GOPATH=\$GOPATH" >> /home/go/.bashrc
  98. echo "systemctl set-environment GOROOT=\$GOROOT" >> /home/go/.bashrc
  99. systemctl set-environment GOPATH=\$GOPATH
  100. systemctl set-environment GOROOT=\$GOROOT
  101. exit 0
  102. EOF
  103. chroot ${rootdir} chmod +x /root/upgrade_golang.sh
  104. chroot ${rootdir} /root/upgrade_golang.sh
  105. if [ ! -f ${rootdir}/home/go/go${GO_VERSION}/bin/go ]; then
  106. echo $'Failed to upgrade golang'
  107. cat ${rootdir}/root/upgrade_golang.sh
  108. rm -f ${rootdir}/root/upgrade_golang.sh
  109. exit 836535
  110. fi
  111. rm -f ${rootdir}/root/upgrade_golang.sh
  112. }
  113. function mesh_upgrade_golang {
  114. chroot "$rootdir" adduser --disabled-login --gecos 'go' go
  115. GOARCH=
  116. if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
  117. GOARCH=386
  118. fi
  119. if [[ $ARCHITECTURE == *"amd64" ]]; then
  120. GOARCH=amd64
  121. fi
  122. if [[ $ARCHITECTURE == *"arm"* ]]; then
  123. GOARCH=armv6l
  124. fi
  125. if [ ! $GOARCH ]; then
  126. return
  127. fi
  128. GO_SOURCE=https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GOARCH}.tar.gz
  129. if [ ! -d ${rootdir}${INSTALL_DIR} ]; then
  130. chroot "$rootdir" mkdir -p ${INSTALL_DIR}
  131. fi
  132. cd ${rootdir}${INSTALL_DIR}
  133. wget ${GO_SOURCE}
  134. if [ ! -f ${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz ]; then
  135. exit 26524
  136. fi
  137. chroot "$rootdir" tar -C /home/go -xzf ${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz
  138. if [ ! -d ${rootdir}/home/go/go/bin ]; then
  139. echo 'Go binary not installed'
  140. exit 763562
  141. fi
  142. mv ${rootdir}/home/go/go ${rootdir}/home/go/go${GO_VERSION}
  143. echo "export GOROOT=/home/go" >> $rootdir/root/.bashrc
  144. echo "export GOROOT=/home/go" >> $rootdir/home/$MY_USERNAME/.bashrc
  145. echo "export GOROOT=/home/go" >> $rootdir/home/go/.bashrc
  146. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> $rootdir/root/.bashrc
  147. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> $rootdir/home/$MY_USERNAME/.bashrc
  148. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> $rootdir/home/go/.bashrc
  149. echo 'export PATH=$PATH:$GOPATH' >> $rootdir/root/.bashrc
  150. echo 'export PATH=$PATH:$GOPATH' >> $rootdir/home/$MY_USERNAME/.bashrc
  151. echo 'export PATH=$PATH:$GOPATH' >> $rootdir/home/go/.bashrc
  152. chroot "$rootdir" chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME
  153. chroot "$rootdir" chown -R go:go /home/go
  154. cp ${rootdir}/home/go/go${GO_VERSION}/bin/* ${rootdir}/usr/bin
  155. }
  156. function upgrade_golang {
  157. if [[ $SYSTEM_TYPE == "$VARIANT_WRITER" || $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CLOUD" || $SYSTEM_TYPE == "$VARIANT_SOCIAL" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  158. return
  159. fi
  160. # NOTE: this is annoyingly hacky and going in the opposite
  161. # direction of a pure blend, but it's necessary if you want
  162. # to run the latest version of gogs
  163. # update to the next commit
  164. function_check set_repo_commit
  165. set_repo_commit $INSTALL_DIR/gvm "gvm commit" "$GVM_COMMIT" $GVM_REPO
  166. if grep -Fxq "upgrade_golang" $COMPLETION_FILE; then
  167. return
  168. fi
  169. export GVM_ROOT=$GVM_HOME
  170. apt-get -y install build-essential libc6-dev-i386 gcc-multilib g++-multilib
  171. apt-get -y install gcc curl git mercurial make binutils bison
  172. if [ ! -d $INSTALL_DIR ]; then
  173. mkdir $INSTALL_DIR
  174. fi
  175. cd $INSTALL_DIR
  176. function_check git_clone
  177. git_clone $GVM_REPO gvm
  178. cd $INSTALL_DIR/gvm
  179. git checkout $GVM_COMMIT -b $GVM_COMMIT
  180. if [ ! -f binscripts/gvm-installer ]; then
  181. echo $'gvm installer not found'
  182. fi
  183. chmod +x binscripts/gvm-installer
  184. if [ -d /root/.gvm ]; then
  185. rm -rf /root/.gvm
  186. fi
  187. if [ -d $GVM_ROOT ]; then
  188. rm -rf $GVM_ROOT
  189. fi
  190. sed -i "s|export GVM_ROOT=.*|export GVM_ROOT=$GVM_ROOT|g" ~/.bashrc
  191. if [ ! -d /home/git ]; then
  192. # add a gogs user account within which the gvm home directory will exist
  193. adduser --disabled-login --gecos 'Gogs' git
  194. fi
  195. if [ -d /home/git/Maildir ]; then
  196. rm -rf /home/git/Maildir
  197. fi
  198. # TODO: this script is all over the place
  199. # and contains hardcoded github. See if you can do better
  200. ./binscripts/gvm-installer master /home/git
  201. if [ ! -d $GVM_ROOT ]; then
  202. echo $'Unable to install gvm'
  203. exit 83537
  204. fi
  205. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  206. if [ ! -f $GVM_ROOT/bin/gvm ]; then
  207. echo $'gvm was not installed'
  208. fi
  209. if ! grep -q "export GVM_ROOT=" ~/.bashrc; then
  210. echo "export GVM_ROOT=$GVM_ROOT" >> ~/.bashrc
  211. fi
  212. if ! grep -q 'PATH=$PATH:$GVM_ROOT/bin' ~/.bashrc; then
  213. echo 'PATH=$PATH:$GVM_ROOT/bin' >> ~/.bashrc
  214. fi
  215. export GOROOT_BOOTSTRAP=$GOROOT
  216. cd $GVM_ROOT/bin
  217. gvm install go${GO_INTERMEDIATE_VERSION}
  218. gvm use go${GO_INTERMEDIATE_VERSION}
  219. gvm install go${GO_VERSION}
  220. if [ ! "$?" = "0" ]; then
  221. echo $'Unable to upgrade golang'
  222. exit 529252
  223. fi
  224. gvm use go${GO_VERSION} --default
  225. chown -R git:git $GVM_HOME
  226. if ! grep -q "gvm commit" $COMPLETION_FILE; then
  227. echo "gvm commit:$GVM_COMMIT" >> $COMPLETION_FILE
  228. else
  229. sed -i "s/gvm commit.*/gvm commit:$GVM_COMMIT/g" $COMPLETION_FILE
  230. fi
  231. echo 'upgrade_golang' >> $COMPLETION_FILE
  232. }
  233. # NOTE: deliberately there is no "exit 0"