check-for-go-toolchain.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2024 Netdata Inc.
  4. # SPDX-License-Identifier: GPL-v3+
  5. #
  6. # Check if we need to install a Go toolchain.
  7. #
  8. # Scripts that use this should call the ensure_go_toolchain function
  9. # after sourcing this file to handle things correctly.
  10. #
  11. # If a working Go toolchain is either present or was installed, then the
  12. # function will return 0. If a working Go toolchain is not present and one
  13. # cannot be installed, then it will instead return 1, with the variable
  14. # GOLANG_FAILURE_REASON set to an error message indicating what went wrong.
  15. GOLANG_MIN_MAJOR_VERSION='1'
  16. GOLANG_MIN_MINOR_VERSION='22'
  17. GOLANG_MIN_PATCH_VERSION='8'
  18. GOLANG_MIN_VERSION="${GOLANG_MIN_MAJOR_VERSION}.${GOLANG_MIN_MINOR_VERSION}.${GOLANG_MIN_PATCH_VERSION}"
  19. GOLANG_TEMP_PATH="${TMPDIR}/go-toolchain"
  20. check_go_version() {
  21. version="$("${go}" version | awk '{ print $3 }' | sed 's/^go//')"
  22. version_major="$(echo "${version}" | cut -f 1 -d '.')"
  23. version_minor="$(echo "${version}" | cut -f 2 -d '.')"
  24. version_patch="$(echo "${version}" | cut -f 3 -d '.')"
  25. if [ -z "${version_major}" ] || [ "${version_major}" -lt "${GOLANG_MIN_MAJOR_VERSION}" ]; then
  26. return 1
  27. elif [ "${version_major}" -gt "${GOLANG_MIN_MAJOR_VERSION}" ]; then
  28. return 0
  29. fi
  30. if [ -z "${version_minor}" ] || [ "${version_minor}" -lt "${GOLANG_MIN_MINOR_VERSION}" ]; then
  31. return 1
  32. elif [ "${version_minor}" -gt "${GOLANG_MIN_MINOR_VERSION}" ]; then
  33. return 0
  34. fi
  35. if [ -n "${version_patch}" ] && [ "${version_patch}" -ge "${GOLANG_MIN_PATCH_VERSION}" ]; then
  36. return 0
  37. fi
  38. return 1
  39. }
  40. install_go_toolchain() {
  41. GOLANG_ARCHIVE_NAME="${GOLANG_TEMP_PATH}/golang.tar.gz"
  42. GOLANG_CHECKSUM_FILE="${GOLANG_TEMP_PATH}/golang.sha256sums"
  43. case "$(uname -s)" in
  44. Linux)
  45. case "$(uname -m)" in
  46. i?86)
  47. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.linux-386.tar.gz"
  48. GOLANG_ARCHIVE_CHECKSUM="0c8e9f824bf443f51e06ac017b9ae402ea066d761b309d880dbb2ca5793db8a2"
  49. ;;
  50. x86_64)
  51. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.linux-amd64.tar.gz"
  52. GOLANG_ARCHIVE_CHECKSUM="5f467d29fc67c7ae6468cb6ad5b047a274bae8180cac5e0b7ddbfeba3e47e18f"
  53. ;;
  54. aarch64)
  55. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.linux-arm64.tar.gz"
  56. GOLANG_ARCHIVE_CHECKSUM="5c616b32dab04bb8c4c8700478381daea0174dc70083e4026321163879278a4a"
  57. ;;
  58. armv*)
  59. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.linux-armv6l.tar.gz"
  60. GOLANG_ARCHIVE_CHECKSUM="5191e87a51a85d88edddc028ab30dfbfa2d7c37cf35d536655e7a063bfb2c9d2"
  61. ;;
  62. ppc64le)
  63. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.linux-ppc64le.tar.gz"
  64. GOLANG_ARCHIVE_CHECKSUM="c546f27866510bf8e54e86fe6f58c705af0e894341e5572c91f197a734152c27"
  65. ;;
  66. riscv64)
  67. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.linux-riscv64.tar.gz"
  68. GOLANG_ARCHIVE_CHECKSUM="f53174ee946b206afe66e043646a6f37af9375d5a9ce420c0f974790508f9e39"
  69. ;;
  70. s390x)
  71. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.linux-s390x.tar.gz"
  72. GOLANG_ARCHIVE_CHECKSUM="fabb3adc241474e28ae151a00e1421983deb35184d31cc76e90025b1b389f6bf"
  73. ;;
  74. *)
  75. GOLANG_FAILURE_REASON="Linux $(uname -m) platform is not supported out-of-box by Go, you must install a toolchain for it yourself."
  76. return 1
  77. ;;
  78. esac
  79. ;;
  80. FreeBSD)
  81. case "$(uname -m)" in
  82. 386)
  83. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.freebsd-386.tar.gz"
  84. GOLANG_ARCHIVE_CHECKSUM="854cffbfb089438397442be4a0c64239da50be4ed037606ea00ed8d86eb89514"
  85. ;;
  86. amd64)
  87. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.freebsd-amd64.tar.gz"
  88. GOLANG_ARCHIVE_CHECKSUM="d7dfa0b309d9ef9f63ad07c63300982ce3e658d7cbac20b031bd31e91afcf209"
  89. ;;
  90. arm)
  91. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.freebsd-arm.tar.gz"
  92. GOLANG_ARCHIVE_CHECKSUM="5d532d05082524748f24948f3028c7a21e1804130ffd624bce4a3d0bee60ce39"
  93. ;;
  94. arm64)
  95. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.freebsd-arm64.tar.gz"
  96. GOLANG_ARCHIVE_CHECKSUM="f7d2664896ad6c773eafbab0748497bec62ff57beb4e25fe6dea12c443d05639"
  97. ;;
  98. riscv64)
  99. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.22.8.freebsd-riscv64.tar.gz"
  100. GOLANG_ARCHIVE_CHECKSUM="ef7d2dbf341d8a8f2a15f2841216ef30329b1f5f301047bd256317480b22a033"
  101. ;;
  102. *)
  103. GOLANG_FAILURE_REASON="FreeBSD $(uname -m) platform is not supported out-of-box by Go, you must install a toolchain for it yourself."
  104. return 1
  105. ;;
  106. esac
  107. ;;
  108. *)
  109. GOLANG_FAILURE_REASON="We do not support automatic handling of a Go toolchain on this system, you must install one manually."
  110. return 1
  111. ;;
  112. esac
  113. if [ -d '/usr/local/go' ]; then
  114. if [ -f '/usr/local/go/.installed-by-netdata' ]; then
  115. rm -rf /usr/local/go
  116. else
  117. GOLANG_FAILURE_REASON="Refusing to overwrite existing Go toolchain install at /usr/local/go, it needs to be updated manually."
  118. return 1
  119. fi
  120. fi
  121. mkdir -p "${GOLANG_TEMP_PATH}"
  122. if ! curl --fail -q -sSL --connect-timeout 10 --retry 3 --output "${GOLANG_ARCHIVE_NAME}" "${GOLANG_ARCHIVE_URL}"; then
  123. GOLANG_FAILURE_REASON="Failed to download Go toolchain."
  124. return 1
  125. fi
  126. echo "${GOLANG_ARCHIVE_CHECKSUM} ${GOLANG_ARCHIVE_NAME}" > "${GOLANG_CHECKSUM_FILE}"
  127. if ! sha256sum -c "${GOLANG_CHECKSUM_FILE}"; then
  128. GOLANG_FAILURE_REASON="Invalid checksum for downloaded Go toolchain."
  129. return 1
  130. fi
  131. if ! tar -C /usr/local/ -xzf "${GOLANG_ARCHIVE_NAME}"; then
  132. GOLANG_FAILURE_REASON="Failed to extract Go toolchain."
  133. return 1
  134. fi
  135. touch /usr/local/go/.installed-by-netdata
  136. rm -rf "${GOLANG_TEMP_PATH}"
  137. }
  138. ensure_go_toolchain() {
  139. go="$(PATH="/usr/local/go/bin:${PATH}" command -v go 2>/dev/null)"
  140. need_go_install=0
  141. if [ -z "${go}" ]; then
  142. need_go_install=1
  143. elif ! check_go_version; then
  144. need_go_install=1
  145. fi
  146. if [ "${need_go_install}" -eq 1 ]; then
  147. if ! install_go_toolchain; then
  148. return 1
  149. fi
  150. rm -rf "${GOLANG_TEMP_PATH}" || true
  151. fi
  152. return 0
  153. }