functions.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # -----------------------------------------------------------------------------
  4. # allow running the jobs by hand
  5. [ -z "${NETDATA_BUILD_WITH_DEBUG}" ] && export NETDATA_BUILD_WITH_DEBUG=0
  6. [ -z "${NETDATA_INSTALL_PATH}" ] && export NETDATA_INSTALL_PATH="${1-/opt/netdata}"
  7. [ -z "${NETDATA_MAKESELF_PATH}" ] && NETDATA_MAKESELF_PATH="$(dirname "${0}")/../.."
  8. [ "${NETDATA_MAKESELF_PATH:0:1}" != "/" ] && NETDATA_MAKESELF_PATH="$(pwd)/${NETDATA_MAKESELF_PATH}"
  9. [ -z "${NETDATA_SOURCE_PATH}" ] && export NETDATA_SOURCE_PATH="${NETDATA_MAKESELF_PATH}/../.."
  10. export NETDATA_MAKESELF_PATH NETDATA_MAKESELF_PATH
  11. export NULL=
  12. # make sure the path does not end with /
  13. if [ "${NETDATA_INSTALL_PATH:$((${#NETDATA_INSTALL_PATH} - 1)):1}" = "/" ]; then
  14. export NETDATA_INSTALL_PATH="${NETDATA_INSTALL_PATH:0:$((${#NETDATA_INSTALL_PATH} - 1))}"
  15. fi
  16. # find the parent directory
  17. NETDATA_INSTALL_PARENT="$(dirname "${NETDATA_INSTALL_PATH}")"
  18. export NETDATA_INSTALL_PARENT
  19. # -----------------------------------------------------------------------------
  20. # bash strict mode
  21. set -euo pipefail
  22. # -----------------------------------------------------------------------------
  23. fetch() {
  24. local dir="${1}" url="${2}" sha256="${3}" key="${4}"
  25. local tar
  26. tar="$(basename "${2}")"
  27. local cache="${NETDATA_SOURCE_PATH}/artifacts/cache/${BUILDARCH}/${key}"
  28. if [ -d "${NETDATA_MAKESELF_PATH}/tmp/${dir}" ]; then
  29. rm -rf "${NETDATA_MAKESELF_PATH}/tmp/${dir}"
  30. fi
  31. if [ -d "${cache}/${dir}" ]; then
  32. echo "Found cached copy of build directory for ${key}, using it."
  33. cp -a "${cache}/${dir}" "${NETDATA_MAKESELF_PATH}/tmp/"
  34. CACHE_HIT=1
  35. else
  36. echo "No cached copy of build directory for ${key} found, fetching sources instead."
  37. if [ ! -f "${NETDATA_MAKESELF_PATH}/tmp/${tar}" ]; then
  38. run wget -O "${NETDATA_MAKESELF_PATH}/tmp/${tar}" "${url}"
  39. fi
  40. # Check SHA256 of gzip'd tar file (apparently alpine's sha256sum requires
  41. # two empty spaces between the checksum and the file's path)
  42. set +e
  43. echo "${sha256} ${NETDATA_MAKESELF_PATH}/tmp/${tar}" | sha256sum --c --status
  44. local rc=$?
  45. if [ ${rc} -ne 0 ]; then
  46. echo >&2 "SHA256 verification of tar file ${tar} failed (rc=${rc})"
  47. echo >&2 "expected: ${sha256}, got $(sha256sum "${NETDATA_MAKESELF_PATH}/tmp/${tar}")"
  48. exit 1
  49. fi
  50. set -e
  51. cd "${NETDATA_MAKESELF_PATH}/tmp"
  52. run tar -axpf "${tar}"
  53. cd -
  54. CACHE_HIT=0
  55. fi
  56. run cd "${NETDATA_MAKESELF_PATH}/tmp/${dir}"
  57. }
  58. store_cache() {
  59. key="${1}"
  60. src="${2}"
  61. cache="${NETDATA_SOURCE_PATH}/artifacts/cache/${BUILDARCH}/${key}"
  62. if [ "${CACHE_HIT:-0}" -eq 0 ]; then
  63. if [ -d "${cache}" ]; then
  64. rm -rf "${cache}"
  65. fi
  66. mkdir -p "${cache}"
  67. cp -a "${src}" "${cache}"
  68. fi
  69. }
  70. # -----------------------------------------------------------------------------
  71. # load the functions of the netdata-installer.sh
  72. # shellcheck source=packaging/installer/functions.sh
  73. . "${NETDATA_SOURCE_PATH}/packaging/installer/functions.sh"
  74. # -----------------------------------------------------------------------------
  75. # debug
  76. echo "ME=${0}"
  77. echo "NETDATA_INSTALL_PARENT=${NETDATA_INSTALL_PARENT}"
  78. echo "NETDATA_INSTALL_PATH=${NETDATA_INSTALL_PATH}"
  79. echo "NETDATA_MAKESELF_PATH=${NETDATA_MAKESELF_PATH}"
  80. echo "NETDATA_SOURCE_PATH=${NETDATA_SOURCE_PATH}"
  81. echo "PROCESSORS=$(nproc)"