check-for-go-toolchain.sh 6.1 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='23'
  17. GOLANG_MIN_PATCH_VERSION='3'
  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.23.3.linux-386.tar.gz"
  48. GOLANG_ARCHIVE_CHECKSUM="3d7b00191a43c50d28e0903a0c576104bc7e171a8670de419d41111c08dfa299"
  49. ;;
  50. x86_64)
  51. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.linux-amd64.tar.gz"
  52. GOLANG_ARCHIVE_CHECKSUM="a0afb9744c00648bafb1b90b4aba5bdb86f424f02f9275399ce0c20b93a2c3a8"
  53. ;;
  54. aarch64)
  55. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.linux-arm64.tar.gz"
  56. GOLANG_ARCHIVE_CHECKSUM="1f7cbd7f668ea32a107ecd41b6488aaee1f5d77a66efd885b175494439d4e1ce"
  57. ;;
  58. armv*)
  59. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.linux-armv6l.tar.gz"
  60. GOLANG_ARCHIVE_CHECKSUM="5f0332754beffc65af65a7b2da76e9dd997567d0d81b6f4f71d3588dc7b4cb00"
  61. ;;
  62. ppc64le)
  63. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.linux-ppc64le.tar.gz"
  64. GOLANG_ARCHIVE_CHECKSUM="e3b926c81e8099d3cee6e6e270b85b39c3bd44263f8d3df29aacb4d7e00507c8"
  65. ;;
  66. riscv64)
  67. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.linux-riscv64.tar.gz"
  68. GOLANG_ARCHIVE_CHECKSUM="324e03b6f59be841dfbaeabc466224b0f0905f5ad3a225b7c0703090e6c4b1a5"
  69. ;;
  70. s390x)
  71. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.linux-s390x.tar.gz"
  72. GOLANG_ARCHIVE_CHECKSUM="6bd72fcef72b046b6282c2d1f2c38f31600e4fe9361fcd8341500c754fb09c38"
  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.23.3.freebsd-386.tar.gz"
  84. GOLANG_ARCHIVE_CHECKSUM="69479fa016ec5b4605885643ce0c2dd5c583e02353978feb6de38c961863b9cc"
  85. ;;
  86. amd64)
  87. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.freebsd-amd64.tar.gz"
  88. GOLANG_ARCHIVE_CHECKSUM="bf1de22a900646ef4f79480ed88337856d47089cc610f87e6fef46f6b8db0e1f"
  89. ;;
  90. arm)
  91. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.freebsd-arm.tar.gz"
  92. GOLANG_ARCHIVE_CHECKSUM="e461f866479bc36bdd4cfec32bfecb1bb243152268a1b3223de109410dec3407"
  93. ;;
  94. arm64)
  95. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.freebsd-arm64.tar.gz"
  96. GOLANG_ARCHIVE_CHECKSUM="24154b4018a45540aefeb6b5b9ffdcc8d9a8cdb78cd7fec262787b89fed19997"
  97. ;;
  98. riscv64)
  99. GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.23.3.freebsd-riscv64.tar.gz"
  100. GOLANG_ARCHIVE_CHECKSUM="218f3f1532e61dd65c330c2a5fc85bec18cc3690489763e62ffa9bb9fc85a68e"
  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. }