check-for-go-toolchain.sh 6.1 KB

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