netdata-updater.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env bash
  2. #shellcheck disable=SC2164
  3. # this script will uninstall netdata
  4. # Variables needed by script:
  5. # - PATH
  6. # - CFLAGS
  7. # - NETDATA_CONFIGURE_OPTIONS
  8. # - REINSTALL_COMMAND
  9. # - NETDATA_TARBALL_URL
  10. # - NETDATA_TARBALL_CHECKSUM_URL
  11. # - NETDATA_TARBALL_CHECKSUM
  12. info() {
  13. echo >&3 "$(date) : INFO: " "${@}"
  14. }
  15. error() {
  16. echo >&3 "$(date) : ERROR: " "${@}"
  17. }
  18. safe_sha256sum() {
  19. # Within the contexct of the installer, we only use -c option that is common between the two commands
  20. # We will have to reconsider if we start non-common options
  21. if command -v sha256sum >/dev/null 2>&1; then
  22. sha256sum $@
  23. elif command -v shasum >/dev/null 2>&1; then
  24. shasum -a 256 $@
  25. else
  26. fatal "I could not find a suitable checksum binary to use"
  27. fi
  28. }
  29. # this is what we will do if it fails (head-less only)
  30. fatal() {
  31. error "FAILED TO UPDATE NETDATA : ${1}"
  32. if [ -n "${logfile}" ]; then
  33. cat >&2 "${logfile}"
  34. rm "${logfile}"
  35. fi
  36. exit 1
  37. }
  38. create_tmp_directory() {
  39. # Check if tmp is mounted as noexec
  40. if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
  41. pattern="$(pwd)/netdata-updater-XXXXXX"
  42. else
  43. pattern="/tmp/netdata-updater-XXXXXX"
  44. fi
  45. mktemp -d "$pattern"
  46. }
  47. download() {
  48. url="${1}"
  49. dest="${2}"
  50. if command -v curl >/dev/null 2>&1; then
  51. curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${dest}" || fatal "Cannot download ${url}"
  52. elif command -v wget >/dev/null 2>&1; then
  53. wget -T 15 -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
  54. else
  55. fatal "I need curl or wget to proceed, but neither is available on this system."
  56. fi
  57. }
  58. set_tarball_urls() {
  59. if [ ! -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
  60. info "Not fetching remote tarballs, local override was given"
  61. return
  62. fi
  63. if [ "$1" == "stable" ]; then
  64. local latest
  65. # Simple version
  66. # latest="$(curl -sSL https://api.github.com/repos/netdata/netdata/releases/latest | grep tag_name | cut -d'"' -f4)"
  67. latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
  68. export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.tar.gz"
  69. export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
  70. else
  71. export NETDATA_TARBALL_URL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.tar.gz"
  72. export NETDATA_TARBALL_CHECKSUM_URL="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
  73. fi
  74. }
  75. update() {
  76. [ -z "${logfile}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
  77. RUN_INSTALLER=0
  78. tmpdir=$(create_tmp_directory)
  79. cd "$tmpdir"
  80. if [ -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
  81. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt" >&3 2>&3
  82. if grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3; then
  83. info "Newest version is already installed"
  84. else
  85. download "${NETDATA_TARBALL_URL}" "${tmpdir}/netdata-latest.tar.gz"
  86. if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
  87. fatal "Tarball checksum validation failed. Stopping netdata upgrade and leaving tarball in ${tmpdir}"
  88. fi
  89. NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2>/dev/null| cut -d' ' -f1)"
  90. tar -xf netdata-latest.tar.gz >&3 2>&3
  91. rm netdata-latest.tar.gz >&3 2>&3
  92. cd netdata-*
  93. RUN_INSTALLER=1
  94. fi
  95. else
  96. info "!!Local tarball override detected!! - Entering directory ${NETDATA_LOCAL_TARBAL_OVERRIDE} for installation, not downloading anything"
  97. RUN_INSTALLER=1
  98. cd ${NETDATA_LOCAL_TARBAL_OVERRIDE}
  99. fi
  100. # We got the sources, run the update now
  101. if [ ${RUN_INSTALLER} -eq 1 ]; then
  102. # signal netdata to start saving its database
  103. # this is handy if your database is big
  104. pids=$(pidof netdata)
  105. do_not_start=
  106. if [ -n "${pids}" ]; then
  107. #shellcheck disable=SC2086
  108. kill -USR1 ${pids}
  109. else
  110. # netdata is currently not running, so do not start it after updating
  111. do_not_start="--dont-start-it"
  112. fi
  113. info "Re-installing netdata..."
  114. eval "${REINSTALL_COMMAND} --dont-wait ${do_not_start}" >&3 2>&3 || fatal "FAILED TO COMPILE/INSTALL NETDATA"
  115. sed -i '/NETDATA_TARBALL/d' "${ENVIRONMENT_FILE}"
  116. cat <<EOF >>"${ENVIRONMENT_FILE}"
  117. NETDATA_TARBALL_CHECKSUM="$NEW_CHECKSUM"
  118. EOF
  119. fi
  120. rm -rf "${tmpdir}" >&3 2>&3
  121. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  122. return
  123. }
  124. # Usually stored in /etc/netdata/.environment
  125. : "${ENVIRONMENT_FILE:=THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT}"
  126. # shellcheck source=/dev/null
  127. source "${ENVIRONMENT_FILE}" || exit 1
  128. if [ "${INSTALL_UID}" != "$(id -u)" ]; then
  129. fatal "You are running this script as user with uid $(id -u). We recommend to run this script as root (user with uid 0)"
  130. fi
  131. logfile=
  132. if [ -t 2 ]; then
  133. # we are running on a terminal
  134. # open fd 3 and send it to stderr
  135. exec 3>&2
  136. else
  137. # we are headless
  138. # create a temporary file for the log
  139. logfile=$(mktemp ${logfile}/netdata-updater.log.XXXXXX)
  140. # open fd 3 and send it to logfile
  141. exec 3>"${logfile}"
  142. fi
  143. set_tarball_urls "${RELEASE_CHANNEL}"
  144. # the installer updates this script - so we run and exit in a single line
  145. update && exit 0