install_go.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 'armhf::arm'
  37. )
  38. if [ -z "${NETDATA_DISABLE_GO+x}" ]; then
  39. ARCH="${DEB_TARGET_ARCH}"
  40. for index in "${ARCH_MAP[@]}" ; do
  41. KEY="${index%%::*}"
  42. VALUE="${index##*::}"
  43. if [ "$KEY" = "$ARCH" ]; then
  44. ARCH="${VALUE}"
  45. break
  46. fi
  47. done
  48. OS=$(uname -s | tr '[:upper:]' '[:lower:]')
  49. echo >&2 "Install go.d.plugin (ARCH=${ARCH}, OS=${OS})"
  50. tmp=$(mktemp -d /tmp/netdata-go-XXXXXX)
  51. GO_PACKAGE_BASENAME="go.d.plugin-${GO_PACKAGE_VERSION}.${OS}-${ARCH}.tar.gz"
  52. download_go "https://github.com/netdata/go.d.plugin/releases/download/${GO_PACKAGE_VERSION}/${GO_PACKAGE_BASENAME}" "${tmp}/${GO_PACKAGE_BASENAME}"
  53. download_go "https://github.com/netdata/go.d.plugin/releases/download/${GO_PACKAGE_VERSION}/config.tar.gz" "${tmp}/config.tar.gz"
  54. if [ ! -f "${tmp}/${GO_PACKAGE_BASENAME}" ] || [ ! -f "${tmp}/config.tar.gz" ] || [ ! -s "${tmp}/config.tar.gz" ] || [ ! -s "${tmp}/${GO_PACKAGE_BASENAME}" ]; then
  55. echo >&2 "Either check the error or consider disabling it by issuing '--disable-go' in the installer"
  56. echo >&2
  57. return 1
  58. fi
  59. grep "${GO_PACKAGE_BASENAME}\$" "packaging/go.d.checksums" >"${tmp}/sha256sums.txt" 2>/dev/null
  60. grep "config.tar.gz" "packaging/go.d.checksums" >>"${tmp}/sha256sums.txt" 2>/dev/null
  61. # Checksum validation
  62. if ! (cd "${tmp}" && safe_sha256sum -c "sha256sums.txt"); then
  63. echo >&2 "go.d plugin checksum validation failure."
  64. echo >&2 "Either check the error or consider disabling it by issuing '--disable-go' in the installer"
  65. echo >&2
  66. echo "go.d.plugin package files checksum validation failed."
  67. exit 1
  68. fi
  69. # Install files
  70. tar -xf "${tmp}/config.tar.gz" -C "${LIB_DIR}/conf.d/"
  71. tar xf "${tmp}/${GO_PACKAGE_BASENAME}"
  72. mv "${GO_PACKAGE_BASENAME/\.tar\.gz/}" "${LIBEXEC_DIR}/plugins.d/go.d.plugin"
  73. rm -rf "${tmp}"
  74. fi
  75. return 0
  76. }
  77. install_go