@cryptotaxi247 / netdata-1 / commits / d7ab436fc

Add script to ensure a usable Go toolchain is installed. (#16815)

Austin S. Hemmelgarn committed Jan 22, 2024 at 11:36 UTC d7ab436fc1f6d53c5a481afc9e35dd0bd8c9787d
1 file changed +133
packaging/check-for-go-toolchain.sh new
+133
@@ -0,0 +1,133 @@
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 +
16 +GOLANG_MIN_MAJOR_VERSION='1'
17 +GOLANG_MIN_MINOR_VERSION='21'
18 +GOLANG_MIN_PATCH_VERSION='0'
19 +
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 +
26 + if [ -z "${version_major}" ] || [ "${version_major}" -lt "${GOLANG_MIN_MAJOR_VERSION}" ]; then
27 + return 1
28 + elif [ "${version_major}" -gt "${GOLANG_MIN_MAJOR_VERSION}" ]; then
29 + return 0
30 + fi
31 +
32 + if [ -z "${version_minor}" ] || [ "${version_minor}" -lt "${GOLANG_MIN_MINOR_VERSION}" ]; then
33 + return 1
34 + elif [ "${version_minor}" -gt "${GOLANG_MIN_MINOR_VERSION}" ]; then
35 + return 0
36 + fi
37 +
38 + if [ -n "${version_patch}" ] && [ "${version_patch}" -ge "${GOLANG_MIN_PATCH_VERSION}" ]; then
39 + return 0
40 + fi
41 +
42 + return 1
43 +}
44 +
45 +install_go_toolchain() {
46 + GOLANG_TEMP_PATH="${TMPDIR}/go-toolchain"
47 + GOLANG_ARCHIVE_NAME="${GOLANG_TEMP_PATH}/golang.tar.gz"
48 + GOLANG_CHECKSUM_FILE="${GOLANG_TEMP_PATH}/golang.sha256sums"
49 +
50 + if [ "$(uname -s)" != "Linux" ]; then
51 + GOLANG_FAILURE_REASON="We do not support automatic handling of a Go toolchain on this system, you must install one manually."
52 + return 1
53 + fi
54 +
55 + case "$(uname -m)" in
56 + i?86)
57 + GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.21.6.linux-386.tar.gz"
58 + GOLANG_ARCHIVE_CHECKSUM="05d09041b5a1193c14e4b2db3f7fcc649b236c567f5eb93305c537851b72dd95"
59 + ;;
60 + x86_64)
61 + GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.21.6.linux-amd64.tar.gz"
62 + GOLANG_ARCHIVE_CHECKSUM="3f934f40ac360b9c01f616a9aa1796d227d8b0328bf64cb045c7b8c4ee9caea4"
63 + ;;
64 + aarch64)
65 + GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.21.6.linux-arm64.tar.gz"
66 + GOLANG_ARCHIVE_CHECKSUM="e2e8aa88e1b5170a0d495d7d9c766af2b2b6c6925a8f8956d834ad6b4cacbd9a"
67 + ;;
68 + armv*)
69 + GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.21.6.linux-armv6l.tar.gz"
70 + GOLANG_ARCHIVE_CHECKSUM="6a8eda6cc6a799ff25e74ce0c13fdc1a76c0983a0bb07c789a2a3454bf6ec9b2"
71 + ;;
72 + ppc64le)
73 + GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.21.6.linux-ppc64le.tar.gz"
74 + GOLANG_ARCHIVE_CHECKSUM="e872b1e9a3f2f08fd4554615a32ca9123a4ba877ab6d19d36abc3424f86bc07f"
75 + ;;
76 + riscv64)
77 + GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.21.6.linux-riscv64.tar.gz"
78 + GOLANG_ARCHIVE_CHECKSUM="86a2fe6597af4b37d98bca632f109034b624786a8d9c1504d340661355ed31f7"
79 + ;;
80 + s390x)
81 + GOLANG_ARCHIVE_URL="https://go.dev/dl/go1.21.6.linux-s390x.tar.gz"
82 + GOLANG_ARCHIVE_CHECKSUM="92894d0f732d3379bc414ffdd617eaadad47e1d72610e10d69a1156db03fc052"
83 + ;;
84 + *)
85 + GOLANG_FAILURE_REASON="Linux $(uname -m) platform is not supported out-of-box by Go, you must install a toolchain for it yourself."
86 + return 1
87 + ;;
88 + esac
89 +
90 + if [ -d '/usr/local/go' ]; then
91 + GOLANG_FAILURE_REASON="Refusing to overwrite existing Go toolchain install at /usr/local/go, it needs to be updated manually."
92 + return 1
93 + fi
94 +
95 + if ! curl --fail -q -sSL --connect-timeout 10 --retry 3 --output "/tmp/${GOLANG_ARCHIVE_NAME}" "${GOLANG_ARCHIVE_URL}"; then
96 + GOLANG_FAILURE_REASON="Failed to download Go toolchain."
97 + return 1
98 + fi
99 +
100 + echo "${GOLANG_ARCHIVE_CHECKSUM} ${GOLANG_ARCHIVE_NAME}" > "${GOLANG_CHECKSUM_FILE}"
101 +
102 + if ! sha256sum -c "${GOLANG_CHECKSUM_FILE}"; then
103 + GOLANG_FAILURE_REASON="Invalid checksum for downloaded Go toolchain."
104 + return 1
105 + fi
106 +
107 + if ! tar -C /usr/local/ -xzf "${GOLANG_ARCHIVE_NAME}"; then
108 + GOLANG_FAILURE_REASON="Failed to extract Go toolchain."
109 + return 1
110 + fi
111 +
112 + rm -rf "${GOLANG_TEMP_PATH}"
113 +}
114 +
115 +ensure_go_toolchain() {
116 + go="$(PATH="/usr/local/go/bin:${PATH}" command -v go 2>/dev/null)"
117 +
118 + need_go_install=0
119 +
120 + if [ -z "${go}" ]; then
121 + need_go_install=1
122 + elif ! check_go_version; then
123 + need_go_install=1
124 + fi
125 +
126 + if [ "${need_go_install}" -eq 1 ]; then
127 + if ! install_go_toolchain; then
128 + return 1
129 + fi
130 + fi
131 +
132 + return 0
133 +}