freedombone-utils-go 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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-2018 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. # 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.9.4
  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" || exit 3873658
  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}" || exit 3463635
  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}" || exit 672845624
  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 -yq install build-essential libc6-dev
  70. apt-get -yq install gcc-multilib g++-multilib make
  71. apt-get -yq 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. prefix=
  115. if [ "$rootdir" ]; then
  116. prefix="chroot $rootdir"
  117. fi
  118. $prefix adduser --disabled-login --gecos 'go' go
  119. GOARCH=
  120. if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
  121. GOARCH=386
  122. fi
  123. if [[ $ARCHITECTURE == *"amd64" || $ARCHITECTURE == "x86_64" ]]; then
  124. GOARCH=amd64
  125. fi
  126. if [[ $ARCHITECTURE == *"arm"* ]]; then
  127. GOARCH=armv6l
  128. fi
  129. if [[ $ARCHITECTURE == *"aarch"* || $ARCHITECTURE == *"arm64"* ]]; then
  130. GOARCH=arm64
  131. fi
  132. if [ ! $GOARCH ]; then
  133. echo $'No architecture specified'
  134. ARCHITECTURE=$(uname -m)
  135. if [[ $ARCHITECTURE == "arm"* ]]; then
  136. GOARCH=armv6l
  137. echo $"Using $GOARCH"
  138. fi
  139. if [[ $ARCHITECTURE == *"aarch"* || $ARCHITECTURE == *"arm64"* ]]; then
  140. GOARCH=arm64
  141. echo $"Using $GOARCH"
  142. fi
  143. if [[ $ARCHITECTURE == "amd"* || $ARCHITECTURE == "x86_64" ]]; then
  144. GOARCH=amd64
  145. echo $"Using $GOARCH"
  146. fi
  147. if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
  148. GOARCH=386
  149. echo $"Using $GOARCH"
  150. fi
  151. fi
  152. if [ ! $GOARCH ]; then
  153. echo $'System architecture was not detected when installing Go'
  154. echo "uname reports: $(uname -m)"
  155. exit 63945284
  156. fi
  157. GO_SOURCE=https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GOARCH}.tar.gz
  158. if [ ! -d "${rootdir}${INSTALL_DIR}" ]; then
  159. chroot "$rootdir" mkdir -p "${INSTALL_DIR}"
  160. fi
  161. cd "${rootdir}${INSTALL_DIR}" || exit 236487365
  162. if [ ! -f "${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz" ]; then
  163. wget ${GO_SOURCE}
  164. fi
  165. if [ ! -f "${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz" ]; then
  166. exit 26524
  167. fi
  168. $prefix tar -C /home/go -xzf "${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz"
  169. if [ ! -d "${rootdir}/home/go/go/bin" ]; then
  170. echo 'Go binary not installed'
  171. exit 763562
  172. fi
  173. # shellcheck disable=SC2086
  174. mv ${rootdir}/home/go/go ${rootdir}/home/go/go${GO_VERSION}
  175. echo "export GOROOT=/home/go" >> "${rootdir}/root/.bashrc"
  176. echo "export GOROOT=/home/go" >> "${rootdir}/etc/skel/.bashrc"
  177. echo "export GOROOT=/home/go" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
  178. echo "export GOROOT=/home/go" >> "${rootdir}/home/go/.bashrc"
  179. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/root/.bashrc"
  180. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/etc/skel/.bashrc"
  181. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
  182. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/home/go/.bashrc"
  183. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/root/.bashrc"
  184. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/etc/skel/.bashrc"
  185. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
  186. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/home/go/.bashrc"
  187. $prefix chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME"
  188. $prefix chown -R go:go /home/go
  189. if [ ! -d "${rootdir}/home/go/go${GO_VERSION}/bin" ]; then
  190. echo $"golang binaries directory not found ${rootdir}/home/go/go${GO_VERSION}/bin"
  191. exit 356356785
  192. fi
  193. if ! cp "${rootdir}/home/go/go${GO_VERSION}/bin/"* "${rootdir}/usr/bin"; then
  194. ls -la "${rootdir}/home/go/go${GO_VERSION}"
  195. echo $'Copying golang binaries failed'
  196. exit 246824628
  197. fi
  198. }
  199. function upgrade_golang {
  200. if grep -Fxq "upgrade_golang:$GO_VERSION" "$COMPLETION_FILE"; then
  201. return
  202. fi
  203. rootdir=
  204. mesh_upgrade_golang
  205. # alter the go version used by the gogs daemon
  206. if [ -f /etc/systemd/system/gogs.service ]; then
  207. sed -i "s|Environment=.*|Environment=\"USER=gogs\" \"HOME=/home/gogs\" \"GOPATH=/home/go/go${GO_VERSION}\"|g" /etc/systemd/system/gogs.service
  208. systemctl daemon-reload
  209. systemctl restart gogs
  210. fi
  211. set_completion_param "${FUNCNAME[0]}" "$GO_VERSION"
  212. }
  213. # NOTE: deliberately there is no "exit 0"