netdata-updater.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/env bash
  2. #shellcheck disable=SC2164
  3. #
  4. # Netdata updater utility
  5. #
  6. # Variables needed by script:
  7. # - PATH
  8. # - CFLAGS
  9. # - NETDATA_CONFIGURE_OPTIONS
  10. # - REINSTALL_COMMAND
  11. # - NETDATA_TARBALL_URL
  12. # - NETDATA_TARBALL_CHECKSUM_URL
  13. # - NETDATA_TARBALL_CHECKSUM
  14. # - NETDATA_PREFIX / NETDATA_LIB_DIR (After 1.16.1 we will only depend on lib dir)
  15. #
  16. # Copyright: SPDX-License-Identifier: GPL-3.0-or-later
  17. #
  18. # Author: Paweł Krupa <paulfantom@gmail.com>
  19. # Author: Pavlos Emm. Katsoulakis <paul@netdata.cloud>
  20. info() {
  21. echo >&3 "$(date) : INFO: " "${@}"
  22. }
  23. error() {
  24. echo >&3 "$(date) : ERROR: " "${@}"
  25. }
  26. safe_sha256sum() {
  27. # Within the contexct of the installer, we only use -c option that is common between the two commands
  28. # We will have to reconsider if we start non-common options
  29. if command -v sha256sum >/dev/null 2>&1; then
  30. sha256sum $@
  31. elif command -v shasum >/dev/null 2>&1; then
  32. shasum -a 256 $@
  33. else
  34. fatal "I could not find a suitable checksum binary to use"
  35. fi
  36. }
  37. # this is what we will do if it fails (head-less only)
  38. fatal() {
  39. error "FAILED TO UPDATE NETDATA : ${1}"
  40. if [ -n "${logfile}" ]; then
  41. cat >&2 "${logfile}"
  42. rm "${logfile}"
  43. fi
  44. exit 1
  45. }
  46. create_tmp_directory() {
  47. # Check if tmp is mounted as noexec
  48. if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
  49. pattern="$(pwd)/netdata-updater-XXXXXX"
  50. else
  51. pattern="/tmp/netdata-updater-XXXXXX"
  52. fi
  53. mktemp -d "$pattern"
  54. }
  55. download() {
  56. url="${1}"
  57. dest="${2}"
  58. if command -v curl >/dev/null 2>&1; then
  59. curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${dest}" || fatal "Cannot download ${url}"
  60. elif command -v wget >/dev/null 2>&1; then
  61. wget -T 15 -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
  62. else
  63. fatal "I need curl or wget to proceed, but neither is available on this system."
  64. fi
  65. }
  66. set_tarball_urls() {
  67. local extension="tar.gz"
  68. if [ ! -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
  69. info "Not fetching remote tarballs, local override was given"
  70. return
  71. fi
  72. if [ "$2" == "yes" ]; then
  73. extension="gz.run"
  74. fi
  75. if [ "$1" = "stable" ]; then
  76. local latest
  77. # Simple version
  78. # latest="$(curl -sSL https://api.github.com/repos/netdata/netdata/releases/latest | grep tag_name | cut -d'"' -f4)"
  79. latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
  80. export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.${extension}"
  81. export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
  82. else
  83. export NETDATA_TARBALL_URL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.${extension}"
  84. export NETDATA_TARBALL_CHECKSUM_URL="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
  85. fi
  86. }
  87. update() {
  88. [ -z "${logfile}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
  89. RUN_INSTALLER=0
  90. tmpdir=$(create_tmp_directory)
  91. cd "$tmpdir"
  92. if [ -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
  93. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt" >&3 2>&3
  94. if [[ -n "${NETDATA_TARBALL_CHECKSUM}" ]] && grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3; then
  95. info "Newest version is already installed"
  96. else
  97. download "${NETDATA_TARBALL_URL}" "${tmpdir}/netdata-latest.tar.gz"
  98. if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
  99. fatal "Tarball checksum validation failed. Stopping netdata upgrade and leaving tarball in ${tmpdir}"
  100. fi
  101. NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2>/dev/null| cut -d' ' -f1)"
  102. tar -xf netdata-latest.tar.gz >&3 2>&3
  103. rm netdata-latest.tar.gz >&3 2>&3
  104. cd netdata-*
  105. RUN_INSTALLER=1
  106. fi
  107. else
  108. info "!!Local tarball override detected!! - Entering directory ${NETDATA_LOCAL_TARBAL_OVERRIDE} for installation, not downloading anything"
  109. RUN_INSTALLER=1
  110. cd ${NETDATA_LOCAL_TARBAL_OVERRIDE}
  111. fi
  112. # We got the sources, run the update now
  113. if [ ${RUN_INSTALLER} -eq 1 ]; then
  114. # signal netdata to start saving its database
  115. # this is handy if your database is big
  116. pids=$(pidof netdata)
  117. do_not_start=
  118. if [ -n "${pids}" ]; then
  119. #shellcheck disable=SC2086
  120. kill -USR1 ${pids}
  121. else
  122. # netdata is currently not running, so do not start it after updating
  123. do_not_start="--dont-start-it"
  124. fi
  125. info "Re-installing netdata..."
  126. eval "${REINSTALL_COMMAND} --dont-wait ${do_not_start}" >&3 2>&3 || fatal "FAILED TO COMPILE/INSTALL NETDATA"
  127. # We no longer store checksum info here. but leave this so that we clean up all environment files upon next update.
  128. sed -i '/NETDATA_TARBALL/d' "${ENVIRONMENT_FILE}"
  129. info "Updating tarball checksum info"
  130. echo "${NEW_CHECKSUM}" > "${NETDATA_LIB_DIR}/netdata.tarball.checksum"
  131. fi
  132. rm -rf "${tmpdir}" >&3 2>&3
  133. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  134. return
  135. }
  136. # Usually stored in /etc/netdata/.environment
  137. : "${ENVIRONMENT_FILE:=THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT}"
  138. # shellcheck source=/dev/null
  139. source "${ENVIRONMENT_FILE}" || exit 1
  140. # We dont expect to find lib dir variable on older installations, so load this path if none found
  141. export NETDATA_LIB_DIR="${NETDATA_LIB_DIR:-${NETDATA_PREFIX}/var/lib/netdata}"
  142. # Source the tarbal checksum, if not already available from environment (for existing installations with the old logic)
  143. [[ -z "${NETDATA_TARBALL_CHECKSUM}" ]] && [[ -f ${NETDATA_LIB_DIR}/netdata.tarball.checksum ]] && NETDATA_TARBALL_CHECKSUM="$(cat "${NETDATA_LIB_DIR}/netdata.tarball.checksum")"
  144. if [ "${INSTALL_UID}" != "$(id -u)" ]; then
  145. fatal "You are running this script as user with uid $(id -u). We recommend to run this script as root (user with uid 0)"
  146. fi
  147. logfile=
  148. if [ -t 2 ]; then
  149. # we are running on a terminal
  150. # open fd 3 and send it to stderr
  151. exec 3>&2
  152. else
  153. # we are headless
  154. # create a temporary file for the log
  155. logfile=$(mktemp ${logfile}/netdata-updater.log.XXXXXX)
  156. # open fd 3 and send it to logfile
  157. exec 3>"${logfile}"
  158. fi
  159. set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
  160. if [ "${IS_NETDATA_STATIC_BINARY}" == "yes" ]; then
  161. TMPDIR="$(create_tmp_directory)"
  162. PREVDIR="$(pwd)"
  163. echo >&2 "Entering ${TMPDIR}"
  164. cd "${TMPDIR}"
  165. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${TMPDIR}/sha256sum.txt"
  166. download "${NETDATA_TARBALL_URL}" "${TMPDIR}/netdata-latest.gz.run"
  167. if ! grep netdata-latest.gz.run "${TMPDIR}/sha256sum.txt" | safe_sha256sum -c - >/dev/null 2>&1; then
  168. fatal "Static binary checksum validation failed. Stopping netdata installation and leaving binary in ${TMPDIR}"
  169. fi
  170. # Do not pass any options other than the accept, for now
  171. sh "${TMPDIR}/netdata-latest.gz.run" --accept
  172. #shellcheck disable=SC2181
  173. if [ $? -eq 0 ]; then
  174. rm -r "${TMPDIR}"
  175. else
  176. echo >&2 "NOTE: did not remove: ${TMPDIR}"
  177. fi
  178. echo >&2 "Switching back to ${PREVDIR}"
  179. cd "${PREVDIR}"
  180. else
  181. # the installer updates this script - so we run and exit in a single line
  182. update && exit 0
  183. fi