install_go.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env bash
  2. GO_PACKAGE_VERSION="$1"
  3. LIB_DIR="$2"
  4. LIBEXEC_DIR="$3"
  5. # ############################################################
  6. # Package Go within netdata (TBD: Package it separately)
  7. safe_sha256sum() {
  8. # Within the context of the installer, we only use -c option that is common between the two commands
  9. # We will have to reconsider if we start non-common options
  10. if command -v sha256sum >/dev/null 2>&1; then
  11. sha256sum $@
  12. elif command -v shasum >/dev/null 2>&1; then
  13. shasum -a 256 $@
  14. else
  15. fatal "I could not find a suitable checksum binary to use"
  16. fi
  17. }
  18. download_go() {
  19. url="${1}"
  20. dest="${2}"
  21. if command -v curl >/dev/null 2>&1; then
  22. curl -sSL --connect-timeout 10 --retry 3 "${url}" > "${dest}"
  23. elif command -v wget >/dev/null 2>&1; then
  24. wget -T 15 -O - "${url}" > "${dest}"
  25. else
  26. echo >&2
  27. echo >&2 "Downloading go.d plugin from '${url}' failed because of missing mandatory packages."
  28. echo >&2 "Either add packages or disable it by issuing '--disable-go' in the installer"
  29. echo >&2
  30. exit 1
  31. fi
  32. }
  33. install_go() {
  34. ARCH_MAP=(
  35. 'i386::386'
  36. 'i686::386'
  37. 'x86_64::amd64'
  38. 'aarch64::arm64'
  39. 'armv64::arm64'
  40. 'armv6l::arm'
  41. 'armv7l::arm'
  42. 'armv5tel::arm'
  43. )
  44. if [ -z "${NETDATA_DISABLE_GO+x}" ]; then
  45. echo >&2 "Install go.d.plugin"
  46. ARCH=$(uname -m)
  47. OS=$(uname -s | tr '[:upper:]' '[:lower:]')
  48. for index in "${ARCH_MAP[@]}" ; do
  49. KEY="${index%%::*}"
  50. VALUE="${index##*::}"
  51. if [ "$KEY" = "$ARCH" ]; then
  52. ARCH="${VALUE}"
  53. break
  54. fi
  55. done
  56. tmp=$(mktemp -d /tmp/netdata-go-XXXXXX)
  57. GO_PACKAGE_BASENAME="go.d.plugin-${GO_PACKAGE_VERSION}.${OS}-${ARCH}.tar.gz"
  58. download_go "https://github.com/netdata/go.d.plugin/releases/download/${GO_PACKAGE_VERSION}/${GO_PACKAGE_BASENAME}" "${tmp}/${GO_PACKAGE_BASENAME}"
  59. download_go "https://github.com/netdata/go.d.plugin/releases/download/${GO_PACKAGE_VERSION}/config.tar.gz" "${tmp}/config.tar.gz"
  60. if [ ! -f "${tmp}/${GO_PACKAGE_BASENAME}" ] || [ ! -f "${tmp}/config.tar.gz" ] || [ ! -s "${tmp}/config.tar.gz" ] || [ ! -s "${tmp}/${GO_PACKAGE_BASENAME}" ]; then
  61. echo >&2 "Either check the error or consider disabling it by issuing '--disable-go' in the installer"
  62. echo >&2
  63. return 1
  64. fi
  65. grep "${GO_PACKAGE_BASENAME}\$" "packaging/go.d.checksums" > "${tmp}/sha256sums.txt" 2>/dev/null
  66. grep "config.tar.gz" "packaging/go.d.checksums" >> "${tmp}/sha256sums.txt" 2>/dev/null
  67. # Checksum validation
  68. if ! (cd "${tmp}" && safe_sha256sum -c "sha256sums.txt"); then
  69. echo >&2 "go.d plugin checksum validation failure."
  70. echo >&2 "Either check the error or consider disabling it by issuing '--disable-go' in the installer"
  71. echo >&2
  72. echo "go.d.plugin package files checksum validation failed."
  73. exit 1
  74. fi
  75. # Install files
  76. tar -xf "${tmp}/config.tar.gz" -C "${LIB_DIR}/conf.d/"
  77. tar xf "${tmp}/${GO_PACKAGE_BASENAME}"
  78. mv "${GO_PACKAGE_BASENAME/\.tar\.gz/}" "${LIBEXEC_DIR}/plugins.d/go.d.plugin"
  79. rm -rf "${tmp}"
  80. fi
  81. return 0
  82. }
  83. install_go