freedombone-utils-go 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Go functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # For reasons unknown we initially have to upgrade to an intermediate version
  29. # before getting to the version we want
  30. GO_INTERMEDIATE_VERSION=1.4.2
  31. GO_VERSION=1.9.4
  32. GO_REPO="https://go.googlesource.com/go"
  33. GO_PACKAGE_MANAGER_REPO="https://github.com/gpmgo/gopm"
  34. GVM_HOME=/home/git/gvm
  35. GVM_REPO="https://github.com/moovweb/gvm"
  36. GVM_COMMIT='25ea8ae158e2861c92e2b22c458e60840157832f'
  37. function select_go_version {
  38. if [ ! -d $GVM_HOME/bin ]; then
  39. echo $'GVM was not installed'
  40. exit 629532
  41. fi
  42. export GVM_ROOT=$GVM_HOME
  43. if ! grep -q "GVM_ROOT=" ~/.bashrc; then
  44. echo "export GVM_ROOT=$GVM_ROOT" >> ~/.bashrc
  45. else
  46. sed -i "s|export GVM_ROOT=.*|export GVM_ROOT=$GVM_ROOT|g" ~/.bashrc
  47. fi
  48. cd "$GVM_ROOT/bin" || exit 3873658
  49. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  50. gvm use go${GO_VERSION} --default
  51. if [ ${#GOPATH} -lt 2 ]; then
  52. echo $'GOPATH was not set'
  53. exit 629825
  54. fi
  55. systemctl set-environment GOPATH="$GOPATH"
  56. }
  57. function mesh_upgrade_golang_from_source {
  58. chroot "$rootdir" adduser --disabled-login --gecos 'go' ipfs
  59. git clone "$GO_REPO" "$rootdir/home/go/go${GO_VERSION}"
  60. cd "$rootdir/home/go/go${GO_VERSION}" || exit 3463635
  61. git checkout "go${GO_VERSION}" -b "go${GO_VERSION}"
  62. git clone "$GO_REPO" "$rootdir/home/go/go${GO_INTERMEDIATE_VERSION}"
  63. cd "$rootdir/home/go/go${GO_INTERMEDIATE_VERSION}" || exit 672845624
  64. git checkout go${GO_INTERMEDIATE_VERSION} -b go${GO_INTERMEDIATE_VERSION}
  65. cat <<EOF > "${rootdir}/root/upgrade_golang.sh"
  66. #!/bin/bash
  67. apt-get -yq install build-essential libc6-dev
  68. apt-get -yq install gcc-multilib g++-multilib make
  69. apt-get -yq install curl git mercurial binutils bison
  70. if [ -d /home/go/Maildir ]; then
  71. rm -rf /home/go/Maildir
  72. fi
  73. export GOROOT=/home/go
  74. export GOPATH=/home/go/go${GO_INTERMEDIATE_VERSION}
  75. cd /home/go/go${GO_INTERMEDIATE_VERSION}/src
  76. ./all.bash
  77. if [ ! -f /home/go/go${GO_INTERMEDIATE_VERSION}/bin/go ]; then
  78. exit 63722
  79. fi
  80. export GOROOT_BOOTSTRAP=/home/go/go${GO_INTERMEDIATE_VERSION}
  81. cd /home/go/go${GO_VERSION}/src
  82. ./all.bash
  83. if [ ! -f /home/go/go${GO_VERSION}/bin/go ]; then
  84. exit 528352
  85. fi
  86. chown -R go:go /home/go
  87. export GOPATH=/home/go/go${GO_VERSION}
  88. export GOROOT=/home/go
  89. echo "export GOPATH=/home/go/go${GO_VERSION}" >> /home/go/.bashrc
  90. echo "export GOPATH=/home/go/go${GO_VERSION}" >> ~/.bashrc
  91. echo "export GOROOT=/home/go" >> /home/go/.bashrc
  92. echo "export GOROOT=/home/go" >> ~/.bashrc
  93. echo "systemctl set-environment GOPATH=\$GOPATH" >> ~/.bashrc
  94. echo "systemctl set-environment GOROOT=\$GOROOT" >> ~/.bashrc
  95. echo "systemctl set-environment GOPATH=\$GOPATH" >> /home/go/.bashrc
  96. echo "systemctl set-environment GOROOT=\$GOROOT" >> /home/go/.bashrc
  97. systemctl set-environment GOPATH=\$GOPATH
  98. systemctl set-environment GOROOT=\$GOROOT
  99. exit 0
  100. EOF
  101. chroot "${rootdir}" chmod +x /root/upgrade_golang.sh
  102. chroot "${rootdir}" /root/upgrade_golang.sh
  103. if [ ! -f "${rootdir}/home/go/go${GO_VERSION}/bin/go" ]; then
  104. echo $'Failed to upgrade golang'
  105. cat "${rootdir}/root/upgrade_golang.sh"
  106. rm -f "${rootdir}/root/upgrade_golang.sh"
  107. exit 836535
  108. fi
  109. rm -f "${rootdir}/root/upgrade_golang.sh"
  110. }
  111. function mesh_upgrade_golang {
  112. prefix=
  113. if [ "$rootdir" ]; then
  114. prefix="chroot $rootdir"
  115. fi
  116. $prefix adduser --disabled-login --gecos 'go' go
  117. GOARCH=
  118. if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
  119. GOARCH=386
  120. fi
  121. if [[ $ARCHITECTURE == *"amd64" || $ARCHITECTURE == "x86_64" ]]; then
  122. GOARCH=amd64
  123. fi
  124. if [[ $ARCHITECTURE == *"arm"* ]]; then
  125. GOARCH=armv6l
  126. fi
  127. if [[ $ARCHITECTURE == *"aarch"* || $ARCHITECTURE == *"arm64"* ]]; then
  128. GOARCH=arm64
  129. fi
  130. if [ ! $GOARCH ]; then
  131. echo $'No architecture specified'
  132. ARCHITECTURE=$(uname -m)
  133. if [[ $ARCHITECTURE == "arm"* ]]; then
  134. GOARCH=armv6l
  135. echo $"Using $GOARCH"
  136. fi
  137. if [[ $ARCHITECTURE == *"aarch"* || $ARCHITECTURE == *"arm64"* ]]; then
  138. GOARCH=arm64
  139. echo $"Using $GOARCH"
  140. fi
  141. if [[ $ARCHITECTURE == "amd"* || $ARCHITECTURE == "x86_64" ]]; then
  142. GOARCH=amd64
  143. echo $"Using $GOARCH"
  144. fi
  145. if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
  146. GOARCH=386
  147. echo $"Using $GOARCH"
  148. fi
  149. fi
  150. if [ ! $GOARCH ]; then
  151. echo $'System architecture was not detected when installing Go'
  152. echo "uname reports: $(uname -m)"
  153. exit 63945284
  154. fi
  155. GO_SOURCE=https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GOARCH}.tar.gz
  156. if [ ! -d "${rootdir}${INSTALL_DIR}" ]; then
  157. chroot "$rootdir" mkdir -p "${INSTALL_DIR}"
  158. fi
  159. cd "${rootdir}${INSTALL_DIR}" || exit 236487365
  160. if [ ! -f "${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz" ]; then
  161. wget ${GO_SOURCE}
  162. fi
  163. if [ ! -f "${rootdir}${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz" ]; then
  164. exit 26524
  165. fi
  166. $prefix tar -C /home/go -xzf "${INSTALL_DIR}/go${GO_VERSION}.linux-${GOARCH}.tar.gz"
  167. if [ ! -d "${rootdir}/home/go/go/bin" ]; then
  168. echo 'Go binary not installed'
  169. exit 763562
  170. fi
  171. # shellcheck disable=SC2086
  172. mv ${rootdir}/home/go/go ${rootdir}/home/go/go${GO_VERSION}
  173. echo "export GOROOT=/home/go" >> "${rootdir}/root/.bashrc"
  174. echo "export GOROOT=/home/go" >> "${rootdir}/etc/skel/.bashrc"
  175. echo "export GOROOT=/home/go" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
  176. echo "export GOROOT=/home/go" >> "${rootdir}/home/go/.bashrc"
  177. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/root/.bashrc"
  178. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/etc/skel/.bashrc"
  179. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
  180. echo "export GOPATH=\$GOROOT/go${GO_VERSION}/bin" >> "${rootdir}/home/go/.bashrc"
  181. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/root/.bashrc"
  182. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/etc/skel/.bashrc"
  183. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/home/$MY_USERNAME/.bashrc"
  184. echo "export PATH=\$PATH:\$GOPATH" >> "${rootdir}/home/go/.bashrc"
  185. $prefix chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME"
  186. $prefix chown -R go:go /home/go
  187. if [ ! -d "${rootdir}/home/go/go${GO_VERSION}/bin" ]; then
  188. echo $"golang binaries directory not found ${rootdir}/home/go/go${GO_VERSION}/bin"
  189. exit 356356785
  190. fi
  191. if ! cp "${rootdir}/home/go/go${GO_VERSION}/bin/"* "${rootdir}/usr/bin"; then
  192. ls -la "${rootdir}/home/go/go${GO_VERSION}"
  193. echo $'Copying golang binaries failed'
  194. exit 246824628
  195. fi
  196. }
  197. function upgrade_golang {
  198. if grep -Fxq "upgrade_golang:$GO_VERSION" "$COMPLETION_FILE"; then
  199. return
  200. fi
  201. rootdir=
  202. mesh_upgrade_golang
  203. # alter the go version used by the gogs daemon
  204. if [ -f /etc/systemd/system/gogs.service ]; then
  205. sed -i "s|Environment=.*|Environment=\"USER=gogs\" \"HOME=/home/gogs\" \"GOPATH=/home/go/go${GO_VERSION}\"|g" /etc/systemd/system/gogs.service
  206. systemctl daemon-reload
  207. systemctl restart gogs
  208. fi
  209. set_completion_param "${FUNCNAME[0]}" "$GO_VERSION"
  210. }
  211. # NOTE: deliberately there is no "exit 0"