netdata-updater.sh 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. #!/bin/sh
  2. # Netdata updater utility
  3. #
  4. # Variables needed by script:
  5. # - PATH
  6. # - CFLAGS
  7. # - LDFLAGS
  8. # - MAKEOPTS
  9. # - IS_NETDATA_STATIC_BINARY
  10. # - NETDATA_CONFIGURE_OPTIONS
  11. # - REINSTALL_OPTIONS
  12. # - NETDATA_TARBALL_URL
  13. # - NETDATA_TARBALL_CHECKSUM_URL
  14. # - NETDATA_TARBALL_CHECKSUM
  15. # - NETDATA_PREFIX
  16. # - NETDATA_LIB_DIR
  17. #
  18. # Optional environment options:
  19. #
  20. # - TMPDIR (set to a usable temporary directory)
  21. # - NETDATA_NIGHTLIES_BASEURL (set the base url for downloading the dist tarball)
  22. #
  23. # Copyright: 2018-2023 Netdata Inc.
  24. # SPDX-License-Identifier: GPL-3.0-or-later
  25. #
  26. # Author: Paweł Krupa <paulfantom@gmail.com>
  27. # Author: Pavlos Emm. Katsoulakis <paul@netdata.cloud>
  28. # Author: Austin S. Hemmelgarn <austin@netdata.cloud>
  29. # Next unused error code: U001D
  30. set -e
  31. PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/install-required-packages.sh"
  32. NETDATA_STABLE_BASE_URL="${NETDATA_BASE_URL:-https://github.com/netdata/netdata/releases}"
  33. NETDATA_NIGHTLY_BASE_URL="${NETDATA_BASE_URL:-https://github.com/netdata/netdata-nightlies/releases}"
  34. NETDATA_DEFAULT_ACCEPT_MAJOR_VERSIONS="1 2"
  35. # Following variables are intended to be overridden by the updater config file.
  36. NETDATA_UPDATER_JITTER=3600
  37. NETDATA_NO_SYSTEMD_JOURNAL=0
  38. NETDATA_ACCEPT_MAJOR_VERSIONS=''
  39. script_dir="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"
  40. if [ -x "${script_dir}/netdata-updater" ]; then
  41. script_source="${script_dir}/netdata-updater"
  42. else
  43. script_source="${script_dir}/netdata-updater.sh"
  44. fi
  45. PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
  46. if [ ! -t 1 ]; then
  47. INTERACTIVE=0
  48. else
  49. INTERACTIVE=1
  50. fi
  51. if [ -n "${script_source}" ]; then
  52. script_name="$(basename "${script_source}")"
  53. else
  54. script_name="netdata-updater.sh"
  55. fi
  56. info() {
  57. echo >&3 "$(date) : INFO: ${script_name}: " "${1}"
  58. }
  59. warning() {
  60. echo >&3 "$(date) : WARNING: ${script_name}: " "${@}"
  61. }
  62. error() {
  63. echo >&3 "$(date) : ERROR: ${script_name}: " "${1}"
  64. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  65. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${1}"
  66. fi
  67. }
  68. fatal() {
  69. echo >&3 "$(date) : FATAL: ${script_name}: FAILED TO UPDATE NETDATA: " "${1}"
  70. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  71. NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${1}"
  72. fi
  73. exit_reason "${1}" "${2}"
  74. exit 1
  75. }
  76. exit_reason() {
  77. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  78. EXIT_REASON="${1}"
  79. EXIT_CODE="${2}"
  80. if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
  81. if [ -n "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  82. {
  83. echo "EXIT_REASON=\"${EXIT_REASON}\""
  84. echo "EXIT_CODE=\"${EXIT_CODE}\""
  85. echo "NETDATA_WARNINGS=\"${NETDATA_WARNINGS}\""
  86. } >> "${NETDATA_SCRIPT_STATUS_PATH}"
  87. else
  88. export EXIT_REASON
  89. export EXIT_CODE
  90. export NETDATA_WARNINGS
  91. fi
  92. fi
  93. fi
  94. }
  95. is_integer () {
  96. case "${1#[+-]}" in
  97. *[!0123456789]*) return 1 ;;
  98. '') return 1 ;;
  99. *) return 0 ;;
  100. esac
  101. }
  102. issystemd() {
  103. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  104. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  105. return 1
  106. fi
  107. # if there is no systemctl command, it is not systemd
  108. systemctl=$(command -v systemctl 2> /dev/null)
  109. if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
  110. return 1
  111. fi
  112. # if pid 1 is systemd, it is systemd
  113. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
  114. # if systemd is not running, it is not systemd
  115. pids=$(safe_pidof systemd 2> /dev/null)
  116. [ -z "${pids}" ] && return 1
  117. # check if the running systemd processes are not in our namespace
  118. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  119. for p in ${pids}; do
  120. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  121. # if pid of systemd is in our namespace, it is systemd
  122. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
  123. done
  124. # else, it is not systemd
  125. return 1
  126. }
  127. _get_intervaldir() {
  128. if [ -d /etc/cron.daily ]; then
  129. echo /etc/cron.daily
  130. elif [ -d /etc/periodic/daily ]; then
  131. echo /etc/periodic/daily
  132. else
  133. return 1
  134. fi
  135. return 0
  136. }
  137. _get_scheduler_type() {
  138. if _get_intervaldir > /dev/null ; then
  139. echo 'interval'
  140. elif issystemd ; then
  141. echo 'systemd'
  142. elif [ -d /etc/cron.d ] ; then
  143. echo 'crontab'
  144. else
  145. echo 'none'
  146. fi
  147. }
  148. confirm() {
  149. prompt="${1} [y/n]"
  150. while true; do
  151. echo "${prompt}"
  152. read -r yn
  153. case "$yn" in
  154. [Yy]*) return 0;;
  155. [Nn]*) return 1;;
  156. *) echo "Please answer yes or no.";;
  157. esac
  158. done
  159. }
  160. warn_major_update() {
  161. nmv_suffix="New major versions generally involve breaking changes, and may not work in the same way as older versions."
  162. if [ "${INTERACTIVE}" -eq 0 ]; then
  163. warning "Would update to a new major version of Netdata. ${nmv_suffix}"
  164. warning "To install the new major version anyway, either run the updater interactively, or include the new major version number in the NETDATA_ACCEPT_MAJOR_VERSIONS variable in ${UPDATER_CONFIG_PATH}."
  165. fatal "Aborting update to new major version to avoid breaking things." U001B
  166. else
  167. warning "This update will install a new major version of Netdata. ${nmv_suffix}"
  168. if confirm "Are you sure you want to update to a new major version of Netdata?"; then
  169. notice "User accepted update to new major version of Netdata."
  170. else
  171. fatal "Aborting update to new major version at user request." U001C
  172. fi
  173. fi
  174. }
  175. install_build_dependencies() {
  176. bash="$(command -v bash 2> /dev/null)"
  177. if [ -z "${bash}" ] || [ ! -x "${bash}" ]; then
  178. error "Unable to find a usable version of \`bash\` (required for local build)."
  179. return 1
  180. fi
  181. info "Fetching dependency handling script..."
  182. download "${PACKAGES_SCRIPT}" "./install-required-packages.sh" || true
  183. if [ ! -s "./install-required-packages.sh" ]; then
  184. error "Downloaded dependency installation script is empty."
  185. else
  186. info "Running dependency handling script..."
  187. opts="--dont-wait --non-interactive"
  188. # shellcheck disable=SC2086
  189. if ! "${bash}" "./install-required-packages.sh" ${opts} netdata >&3 2>&3; then
  190. error "Installing build dependencies failed. The update should still work, but you might be missing some features."
  191. fi
  192. fi
  193. }
  194. enable_netdata_updater() {
  195. updater_type="$(echo "${1}" | tr '[:upper:]' '[:lower:]')"
  196. case "${updater_type}" in
  197. systemd|interval|crontab)
  198. updater_type="${1}"
  199. ;;
  200. "")
  201. updater_type="$(_get_scheduler_type)"
  202. ;;
  203. *)
  204. fatal "Unrecognized updater type ${updater_type} requested. Supported types are 'systemd', 'interval', and 'crontab'." U0001
  205. ;;
  206. esac
  207. case "${updater_type}" in
  208. "systemd")
  209. if issystemd; then
  210. systemctl enable netdata-updater.timer
  211. info "Auto-updating has been ENABLED using a systemd timer unit.\n"
  212. info "If the update process fails, the failure will be logged to the systemd journal just like a regular service failure."
  213. info "Successful updates should produce empty logs."
  214. else
  215. error "Systemd-based auto-update scheduling requested, but this does not appear to be a systemd system. Auto-updates have NOT been enabled."
  216. return 1
  217. fi
  218. ;;
  219. "interval")
  220. if _get_intervaldir > /dev/null; then
  221. ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "$(_get_intervaldir)/netdata-updater"
  222. info "Auto-updating has been ENABLED through cron, updater script linked to $(_get_intervaldir)/netdata-updater\n"
  223. info "If the update process fails and you have email notifications set up correctly for cron on this system, you should receive an email notification of the failure."
  224. info "Successful updates will not send an email."
  225. else
  226. error "Interval-based auto-update scheduling requested, but I could not find an interval scheduling directory. Auto-updates have NOT been enabled."
  227. return 1
  228. fi
  229. ;;
  230. "crontab")
  231. if [ -d "/etc/cron.d" ]; then
  232. [ -f "/etc/cron.d/netdata-updater" ] && rm -f "/etc/cron.d/netdata-updater"
  233. install -p -m 0644 -o 0 -g 0 "${NETDATA_PREFIX}/usr/lib/system/cron/netdata-updater-daily" "/etc/cron.d/netdata-updater-daily"
  234. info "Auto-updating has been ENABLED through cron, using a crontab at /etc/cron.d/netdata-updater\n"
  235. info "If the update process fails and you have email notifications set up correctly for cron on this system, you should receive an email notification of the failure."
  236. info "Successful updates will not send an email."
  237. else
  238. error "Crontab-based auto-update scheduling requested, but there is no '/etc/cron.d'. Auto-updates have NOT been enabled."
  239. return 1
  240. fi
  241. ;;
  242. *)
  243. error "Unable to determine what type of auto-update scheduling to use. Auto-updates have NOT been enabled."
  244. return 1
  245. esac
  246. return 0
  247. }
  248. disable_netdata_updater() {
  249. if issystemd && ( systemctl list-units --full -all | grep -Fq "netdata-updater.timer" ) ; then
  250. systemctl disable netdata-updater.timer
  251. fi
  252. if [ -d /etc/cron.daily ]; then
  253. rm -f /etc/cron.daily/netdata-updater.sh
  254. rm -f /etc/cron.daily/netdata-updater
  255. fi
  256. if [ -d /etc/periodic/daily ]; then
  257. rm -f /etc/periodic/daily/netdata-updater.sh
  258. rm -f /etc/periodic/daily/netdata-updater
  259. fi
  260. if [ -d /etc/cron.d ]; then
  261. rm -f /etc/cron.d/netdata-updater
  262. rm -f /etc/cron.d/netdata-updater-daily
  263. fi
  264. info "Auto-updates have been DISABLED."
  265. return 0
  266. }
  267. str_in_list() {
  268. printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
  269. return $?
  270. }
  271. safe_sha256sum() {
  272. # Within the context of the installer, we only use -c option that is common between the two commands
  273. # We will have to reconsider if we start non-common options
  274. if command -v shasum > /dev/null 2>&1; then
  275. shasum -a 256 "$@"
  276. elif command -v sha256sum > /dev/null 2>&1; then
  277. sha256sum "$@"
  278. else
  279. fatal "I could not find a suitable checksum binary to use" U0002
  280. fi
  281. }
  282. cleanup() {
  283. if [ -n "${logfile}" ]; then
  284. cat >&2 "${logfile}"
  285. rm "${logfile}"
  286. fi
  287. if [ -n "$ndtmpdir" ] && [ -d "$ndtmpdir" ]; then
  288. rm -rf "$ndtmpdir"
  289. fi
  290. }
  291. _cannot_use_tmpdir() {
  292. testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
  293. ret=0
  294. if [ -z "${testfile}" ] ; then
  295. return "${ret}"
  296. fi
  297. if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}" ; then
  298. if chmod +x "${testfile}" ; then
  299. if [ "$("${testfile}" 2>/dev/null)" = "SUCCESS" ] ; then
  300. ret=1
  301. fi
  302. fi
  303. fi
  304. rm -f "${testfile}"
  305. return "${ret}"
  306. }
  307. create_tmp_directory() {
  308. if [ -n "${NETDATA_TMPDIR_PATH}" ]; then
  309. echo "${NETDATA_TMPDIR_PATH}"
  310. else
  311. if [ -z "${NETDATA_TMPDIR}" ] || _cannot_use_tmpdir "${NETDATA_TMPDIR}" ; then
  312. if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}" ; then
  313. if _cannot_use_tmpdir /tmp ; then
  314. if _cannot_use_tmpdir "${PWD}" ; then
  315. fatal "Unable to find a usable temporary directory. Please set \$TMPDIR to a path that is both writable and allows execution of files and try again." U0003
  316. else
  317. TMPDIR="${PWD}"
  318. fi
  319. else
  320. TMPDIR="/tmp"
  321. fi
  322. fi
  323. else
  324. TMPDIR="${NETDATA_TMPDIR}"
  325. fi
  326. mktemp -d -t netdata-updater-XXXXXXXXXX
  327. fi
  328. }
  329. check_for_curl() {
  330. if [ -z "${curl}" ]; then
  331. curl="$(PATH="${PATH}:/opt/netdata/bin" command -v curl 2>/dev/null && true)"
  332. fi
  333. }
  334. _safe_download() {
  335. url="${1}"
  336. dest="${2}"
  337. if echo "${url}" | grep -Eq "^file:///"; then
  338. run cp "${url#file://}" "${dest}" || return 1
  339. return 0
  340. fi
  341. check_for_curl
  342. if [ -n "${curl}" ]; then
  343. "${curl}" -fsSL --connect-timeout 10 --retry 3 "${url}" > "${dest}"
  344. return $?
  345. elif command -v wget > /dev/null 2>&1; then
  346. wget -T 15 -O - "${url}" > "${dest}"
  347. return $?
  348. else
  349. return 255
  350. fi
  351. }
  352. download() {
  353. url="${1}"
  354. dest="${2}"
  355. _safe_download "${url}" "${dest}"
  356. ret=$?
  357. if [ ${ret} -eq 0 ]; then
  358. return 0
  359. elif [ ${ret} -eq 255 ]; then
  360. fatal "I need curl or wget to proceed, but neither is available on this system." U0004
  361. else
  362. fatal "Cannot download ${url}" U0005
  363. fi
  364. }
  365. get_netdata_latest_tag() {
  366. url="${1}/latest"
  367. check_for_curl
  368. if [ -n "${curl}" ]; then
  369. tag=$("${curl}" "${url}" -s -L -I -o /dev/null -w '%{url_effective}' | grep -Eom 1 '[^/]*/?$')
  370. elif command -v wget >/dev/null 2>&1; then
  371. tag=$(wget -S -O /dev/null "${url}" 2>&1 | grep -m 1 Location | grep -Eo '[^/]*/?$')
  372. else
  373. fatal "I need curl or wget to proceed, but neither of them are available on this system." U0006
  374. fi
  375. # Fallback case for simpler local testing.
  376. if echo "${tag}" | grep -Eq 'latest/?$'; then
  377. if _safe_download "${url}/latest-version.txt" ./ndupdate-version.txt; then
  378. tag="$(cat ./ndupdate-version.txt)"
  379. if grep -q 'Not Found' ./ndupdate-version.txt; then
  380. tag="latest"
  381. fi
  382. rm -f ./ndupdate-version.txt
  383. else
  384. tag="latest"
  385. fi
  386. fi
  387. echo "${tag}"
  388. }
  389. newer_commit_date() {
  390. info "Checking if a newer version of the updater script is available."
  391. commit_check_url="https://api.github.com/repos/netdata/netdata/commits?path=packaging%2Finstaller%2Fnetdata-updater.sh&page=1&per_page=1"
  392. python_version_check="from __future__ import print_function;import sys,json;data = json.load(sys.stdin);print(data[0]['commit']['committer']['date'] if isinstance(data, list) else '')"
  393. if command -v jq > /dev/null 2>&1; then
  394. commit_date="$(_safe_download "${commit_check_url}" /dev/stdout | jq '.[0].commit.committer.date' 2>/dev/null | tr -d '"')"
  395. elif command -v python > /dev/null 2>&1;then
  396. commit_date="$(_safe_download "${commit_check_url}" /dev/stdout | python -c "${python_version_check}")"
  397. elif command -v python3 > /dev/null 2>&1;then
  398. commit_date="$(_safe_download "${commit_check_url}" /dev/stdout | python3 -c "${python_version_check}")"
  399. fi
  400. if [ -z "${commit_date}" ] ; then
  401. return 0
  402. elif [ "$(uname)" = "Linux" ]; then
  403. commit_date="$(date -d "${commit_date}" +%s)"
  404. else # assume BSD-style `date` if we are not on Linux
  405. commit_date="$(/bin/date -j -f "%Y-%m-%dT%H:%M:%SZ" "${commit_date}" +%s 2>/dev/null)"
  406. if [ -z "${commit_date}" ]; then
  407. return 0
  408. fi
  409. fi
  410. if [ -e "${script_source}" ]; then
  411. script_date="$(date -r "${script_source}" +%s)"
  412. else
  413. script_date="$(date +%s)"
  414. fi
  415. [ "${commit_date}" -ge "${script_date}" ]
  416. }
  417. self_update() {
  418. if [ -z "${NETDATA_NO_UPDATER_SELF_UPDATE}" ] && newer_commit_date; then
  419. info "Downloading newest version of updater script."
  420. ndtmpdir=$(create_tmp_directory)
  421. cd "$ndtmpdir" || exit 1
  422. if _safe_download "https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-updater.sh" ./netdata-updater.sh; then
  423. chmod +x ./netdata-updater.sh || exit 1
  424. export ENVIRONMENT_FILE="${ENVIRONMENT_FILE}"
  425. force_update=""
  426. [ "$NETDATA_FORCE_UPDATE" = "1" ] && force_update="--force-update"
  427. exec ./netdata-updater.sh --not-running-from-cron --no-updater-self-update "$force_update" --tmpdir-path "$(pwd)"
  428. else
  429. error "Failed to download newest version of updater script, continuing with current version."
  430. fi
  431. fi
  432. }
  433. parse_version() {
  434. r="${1}"
  435. if [ "${r}" = "latest" ]; then
  436. # If we get ‘latest’ as a version, return the largest possible
  437. # version value.
  438. printf "99999999999999"
  439. return 0
  440. elif echo "${r}" | grep -q '^v.*'; then
  441. # shellcheck disable=SC2001
  442. # XXX: Need a regex group substitution here.
  443. r="$(echo "${r}" | sed -e 's/^v\(.*\)/\1/')"
  444. fi
  445. tmpfile="$(mktemp)"
  446. echo "${r}" | tr '-' ' ' > "${tmpfile}"
  447. read -r v b _ < "${tmpfile}"
  448. if echo "${b}" | grep -vEq "^[0-9]+$"; then
  449. b="0"
  450. fi
  451. echo "${v}" | tr '.' ' ' > "${tmpfile}"
  452. read -r maj min patch _ < "${tmpfile}"
  453. rm -f "${tmpfile}"
  454. printf "%03d%03d%03d%05d" "${maj}" "${min}" "${patch}" "${b}"
  455. }
  456. get_latest_version() {
  457. if [ "${RELEASE_CHANNEL}" = "stable" ]; then
  458. get_netdata_latest_tag "${NETDATA_STABLE_BASE_URL}"
  459. else
  460. get_netdata_latest_tag "${NETDATA_NIGHTLY_BASE_URL}"
  461. fi
  462. }
  463. validate_environment_file() {
  464. if [ -n "${NETDATA_PREFIX+SET_BUT_NULL}" ] && [ -n "${REINSTALL_OPTIONS+SET_BUT_NULL}" ]; then
  465. return 0
  466. else
  467. fatal "Environment file located at ${ENVIRONMENT_FILE} is not valid, unable to update." U0007
  468. fi
  469. }
  470. update_available() {
  471. if [ "$NETDATA_FORCE_UPDATE" = "1" ]; then
  472. info "Force update requested"
  473. return 0
  474. fi
  475. basepath="$(dirname "$(dirname "$(dirname "${NETDATA_LIB_DIR}")")")"
  476. searchpath="${basepath}/bin:${basepath}/sbin:${basepath}/usr/bin:${basepath}/usr/sbin:${PATH}"
  477. searchpath="${basepath}/netdata/bin:${basepath}/netdata/sbin:${basepath}/netdata/usr/bin:${basepath}/netdata/usr/sbin:${searchpath}"
  478. ndbinary="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
  479. if [ -z "${ndbinary}" ]; then
  480. current_version=0
  481. else
  482. current_version="$(parse_version "$(${ndbinary} -v | cut -f 2 -d ' ')")"
  483. fi
  484. latest_tag="$(get_latest_version)"
  485. latest_version="$(parse_version "${latest_tag}")"
  486. path_version="$(echo "${latest_tag}" | cut -f 1 -d "-")"
  487. # If we can't get the current version for some reason assume `0`
  488. current_version="${current_version:-0}"
  489. # If we can't get the latest version for some reason assume `0`
  490. latest_version="${latest_version:-0}"
  491. info "Current Version: ${current_version}"
  492. info "Latest Version: ${latest_version}"
  493. if [ "${latest_version}" -gt 0 ] && [ "${current_version}" -gt 0 ] && [ "${current_version}" -ge "${latest_version}" ]; then
  494. info "Newest version (current=${current_version} >= latest=${latest_version}) is already installed"
  495. return 1
  496. else
  497. info "Update available"
  498. if [ "${current_version}" -ne 0 ] && [ "${latest_version}" -ne 0 ]; then
  499. current_major="$(${ndbinary} -v | cut -f 2 -d ' ' | cut -f 1 -d '.' | tr -d 'v')"
  500. latest_major="$(echo "${latest_tag}" | cut -f 1 -d '.' | tr -d 'v')"
  501. if [ "${current_major}" -ne "${latest_major}" ]; then
  502. update_safe=0
  503. for v in ${NETDATA_ACCEPT_MAJOR_VERSIONS}; do
  504. if [ "${latest_major}" -eq "${v}" ]; then
  505. update_safe=1
  506. break
  507. fi
  508. done
  509. if [ "${update_safe}" -eq 0 ]; then
  510. warn_major_update
  511. fi
  512. fi
  513. fi
  514. return 0
  515. fi
  516. }
  517. set_tarball_urls() {
  518. filename="netdata-latest.tar.gz"
  519. if [ "$2" = "yes" ]; then
  520. if [ -e /opt/netdata/etc/netdata/.install-type ]; then
  521. # shellcheck disable=SC1091
  522. . /opt/netdata/etc/netdata/.install-type
  523. [ -z "${PREBUILT_ARCH:-}" ] && PREBUILT_ARCH="$(uname -m)"
  524. filename="netdata-${PREBUILT_ARCH}-latest.gz.run"
  525. else
  526. filename="netdata-x86_64-latest.gz.run"
  527. fi
  528. fi
  529. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  530. path="$(cd "${NETDATA_OFFLINE_INSTALL_SOURCE}" || exit 1; pwd)"
  531. export NETDATA_TARBALL_URL="file://${path}/${filename}"
  532. export NETDATA_TARBALL_CHECKSUM_URL="file://${path}/sha256sums.txt"
  533. elif [ "$1" = "stable" ]; then
  534. latest="$(get_netdata_latest_tag "${NETDATA_STABLE_BASE_URL}")"
  535. export NETDATA_TARBALL_URL="${NETDATA_STABLE_BASE_URL}/download/$latest/${filename}"
  536. export NETDATA_TARBALL_CHECKSUM_URL="${NETDATA_STABLE_BASE_URL}/download/$latest/sha256sums.txt"
  537. else
  538. tag="$(get_netdata_latest_tag "${NETDATA_NIGHTLY_BASE_URL}")"
  539. export NETDATA_TARBALL_URL="${NETDATA_NIGHTLY_BASE_URL}/download/${tag}/${filename}"
  540. export NETDATA_TARBALL_CHECKSUM_URL="${NETDATA_NIGHTLY_BASE_URL}/download/${tag}/sha256sums.txt"
  541. fi
  542. }
  543. update_build() {
  544. [ -z "${logfile}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
  545. RUN_INSTALLER=0
  546. ndtmpdir=$(create_tmp_directory)
  547. cd "$ndtmpdir" || fatal "Failed to change current working directory to ${ndtmpdir}" U0016
  548. install_build_dependencies
  549. if update_available; then
  550. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt" >&3 2>&3
  551. download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-latest.tar.gz"
  552. if [ -n "${NETDATA_TARBALL_CHECKSUM}" ] &&
  553. grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3 &&
  554. [ "$NETDATA_FORCE_UPDATE" != "1" ]; then
  555. info "Newest version is already installed"
  556. else
  557. if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
  558. fatal "Tarball checksum validation failed. Stopping netdata upgrade and leaving tarball in ${ndtmpdir}\nUsually this is a result of an older copy of the tarball or checksum file being cached somewhere upstream and can be resolved by retrying in an hour." U0008
  559. fi
  560. NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2> /dev/null | cut -d' ' -f1)"
  561. tar -xf netdata-latest.tar.gz >&3 2>&3
  562. rm netdata-latest.tar.gz >&3 2>&3
  563. if [ -z "$path_version" ]; then
  564. latest_tag="$(get_latest_version)"
  565. path_version="$(echo "${latest_tag}" | cut -f 1 -d "-")"
  566. fi
  567. cd "$(find . -maxdepth 1 -type d -name "netdata-${path_version}*" | head -n 1)" || fatal "Failed to switch to build directory" U0017
  568. RUN_INSTALLER=1
  569. fi
  570. fi
  571. # We got the sources, run the update now
  572. if [ ${RUN_INSTALLER} -eq 1 ]; then
  573. # signal netdata to start saving its database
  574. # this is handy if your database is big
  575. possible_pids=$(pidof netdata)
  576. do_not_start=
  577. if [ -n "${possible_pids}" ]; then
  578. # shellcheck disable=SC2086
  579. kill -USR1 ${possible_pids}
  580. else
  581. # netdata is currently not running, so do not start it after updating
  582. do_not_start="--dont-start-it"
  583. fi
  584. env="env TMPDIR=${TMPDIR}"
  585. if [ -n "${NETDATA_SELECTED_DASHBOARD}" ]; then
  586. env="${env} NETDATA_SELECTED_DASHBOARD=${NETDATA_SELECTED_DASHBOARD}"
  587. fi
  588. if [ ! -x ./netdata-installer.sh ]; then
  589. if [ "$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && [ -x "$(find . -mindepth 1 -maxdepth 1 -type d)/netdata-installer.sh" ]; then
  590. cd "$(find . -mindepth 1 -maxdepth 1 -type d)" || fatal "Failed to switch to build directory" U0018
  591. fi
  592. fi
  593. if [ -e "${NETDATA_PREFIX}/etc/netdata/.install-type" ] ; then
  594. install_type="$(cat "${NETDATA_PREFIX}"/etc/netdata/.install-type)"
  595. else
  596. install_type="INSTALL_TYPE='legacy-build'"
  597. fi
  598. if [ "${INSTALL_TYPE}" = "custom" ] && [ -f "${NETDATA_PREFIX}" ]; then
  599. install_type="INSTALL_TYPE='legacy-build'"
  600. fi
  601. info "Re-installing netdata..."
  602. export NETDATA_SAVE_WARNINGS=1
  603. export NETDATA_PROPAGATE_WARNINGS=1
  604. export NETDATA_WARNINGS="${NETDATA_WARNINGS}"
  605. export NETDATA_SCRIPT_STATUS_PATH="${NETDATA_SCRIPT_STATUS_PATH}"
  606. # shellcheck disable=SC2086
  607. if ! ${env} ./netdata-installer.sh ${REINSTALL_OPTIONS} --dont-wait ${do_not_start} >&3 2>&3; then
  608. if [ -r "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  609. # shellcheck disable=SC1090
  610. . "${NETDATA_SCRIPT_STATUS_PATH}"
  611. rm -f "${NETDATA_SCRIPT_STATUS_PATH}"
  612. fi
  613. if [ -n "${EXIT_REASON}" ]; then
  614. fatal "Failed to rebuild existing netdata install: ${EXIT_REASON}" "U${EXIT_CODE}"
  615. else
  616. fatal "Failed to rebuild existing netdata reinstall." UI0000
  617. fi
  618. fi
  619. # We no longer store checksum info here. but leave this so that we clean up all environment files upon next update.
  620. sed -i '/NETDATA_TARBALL/d' "${ENVIRONMENT_FILE}"
  621. info "Updating tarball checksum info"
  622. echo "${NEW_CHECKSUM}" > "${NETDATA_LIB_DIR}/netdata.tarball.checksum"
  623. echo "${install_type}" > "${NETDATA_PREFIX}/etc/netdata/.install-type"
  624. fi
  625. rm -rf "${ndtmpdir}" >&3 2>&3
  626. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  627. return 0
  628. }
  629. update_static() {
  630. ndtmpdir="$(create_tmp_directory)"
  631. PREVDIR="$(pwd)"
  632. info "Entering ${ndtmpdir}"
  633. cd "${ndtmpdir}" || fatal "Failed to change current working directory to ${ndtmpdir}" U0019
  634. if update_available; then
  635. sysarch="${PREBUILT_ARCH}"
  636. [ -z "$sysarch" ] && sysarch="$(uname -m)"
  637. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt"
  638. download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-${sysarch}-latest.gz.run"
  639. if ! grep "netdata-${sysarch}-latest.gz.run" "${ndtmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  640. fatal "Static binary checksum validation failed. Stopping netdata installation and leaving binary in ${ndtmpdir}\nUsually this is a result of an older copy of the file being cached somewhere and can be resolved by simply retrying in an hour." U000A
  641. fi
  642. if [ -e /opt/netdata/etc/netdata/.install-type ] ; then
  643. install_type="$(cat /opt/netdata/etc/netdata/.install-type)"
  644. else
  645. install_type="INSTALL_TYPE='legacy-static'"
  646. fi
  647. # Do not pass any options other than the accept, for now
  648. # shellcheck disable=SC2086
  649. if sh "${ndtmpdir}/netdata-${sysarch}-latest.gz.run" --accept -- ${REINSTALL_OPTIONS} >&3 2>&3; then
  650. rm -r "${ndtmpdir}"
  651. else
  652. info "NOTE: did not remove: ${ndtmpdir}"
  653. fi
  654. echo "${install_type}" > /opt/netdata/etc/netdata/.install-type
  655. fi
  656. if [ -e "${PREVDIR}" ]; then
  657. info "Switching back to ${PREVDIR}"
  658. cd "${PREVDIR}"
  659. fi
  660. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  661. exit 0
  662. }
  663. get_new_binpkg_major() {
  664. case "${pm_cmd}" in
  665. apt-get) apt-get --just-print upgrade 2>&1 | grep Inst | grep ' netdata ' | cut -f 3 -d ' ' | tr -d '[]' | cut -f 1 -d '.' ;;
  666. yum) yum check-update netdata | grep -E '^netdata ' | awk '{print $2}' | cut -f 1 -d '.' ;;
  667. dnf) dnf check-update netdata | grep -E '^netdata ' | awk '{print $2}' | cut -f 1 -d '.' ;;
  668. zypper) zypper list-updates | grep '| netdata |' | cut -f 5 -d '|' | tr -d ' ' | cut -f 1 -d '.' ;;
  669. esac
  670. }
  671. update_binpkg() {
  672. os_release_file=
  673. if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
  674. os_release_file="/etc/os-release"
  675. elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
  676. os_release_file="/usr/lib/os-release"
  677. else
  678. fatal "Cannot find an os-release file ..." U000B
  679. fi
  680. # shellcheck disable=SC1090
  681. . "${os_release_file}"
  682. DISTRO="${ID}"
  683. supported_compat_names="debian ubuntu centos fedora opensuse ol amzn"
  684. if str_in_list "${DISTRO}" "${supported_compat_names}"; then
  685. DISTRO_COMPAT_NAME="${DISTRO}"
  686. else
  687. case "${DISTRO}" in
  688. opensuse-leap|opensuse-tumbleweed)
  689. DISTRO_COMPAT_NAME="opensuse"
  690. ;;
  691. cloudlinux|almalinux|centos-stream|rocky|rhel)
  692. DISTRO_COMPAT_NAME="centos"
  693. ;;
  694. *)
  695. DISTRO_COMPAT_NAME="unknown"
  696. ;;
  697. esac
  698. fi
  699. interactive_opts=""
  700. env=""
  701. case "${DISTRO_COMPAT_NAME}" in
  702. debian|ubuntu)
  703. if [ "${INTERACTIVE}" = "0" ]; then
  704. upgrade_subcmd="-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold --only-upgrade install"
  705. interactive_opts="-y"
  706. env="DEBIAN_FRONTEND=noninteractive"
  707. else
  708. upgrade_subcmd="--only-upgrade install"
  709. fi
  710. pm_cmd="apt-get"
  711. repo_subcmd="update"
  712. install_subcmd="install"
  713. mark_auto_cmd="apt-mark auto"
  714. pkg_install_opts="${interactive_opts}"
  715. repo_update_opts="${interactive_opts}"
  716. pkg_installed_check="dpkg-query -s"
  717. INSTALL_TYPE="binpkg-deb"
  718. ;;
  719. centos|fedora|ol|amzn)
  720. if [ "${INTERACTIVE}" = "0" ]; then
  721. interactive_opts="-y"
  722. fi
  723. if command -v dnf > /dev/null; then
  724. pm_cmd="dnf"
  725. repo_subcmd="makecache"
  726. mark_auto_cmd="dnf mark remove"
  727. else
  728. pm_cmd="yum"
  729. mark_auto_cmd="yumdb set reason dep"
  730. fi
  731. upgrade_subcmd="upgrade"
  732. install_subcmd="install"
  733. pkg_install_opts="${interactive_opts}"
  734. repo_update_opts="${interactive_opts}"
  735. pkg_installed_check="rpm -q"
  736. INSTALL_TYPE="binpkg-rpm"
  737. ;;
  738. opensuse)
  739. if [ "${INTERACTIVE}" = "0" ]; then
  740. upgrade_subcmd="--non-interactive update"
  741. else
  742. upgrade_subcmd="update"
  743. fi
  744. pm_cmd="zypper"
  745. repo_subcmd="--gpg-auto-import-keys refresh"
  746. install_subcmd="install"
  747. mark_auto_cmd=""
  748. pkg_install_opts=""
  749. repo_update_opts=""
  750. pkg_installed_check="rpm -q"
  751. INSTALL_TYPE="binpkg-rpm"
  752. ;;
  753. *)
  754. warning "We do not provide native packages for ${DISTRO}."
  755. return 2
  756. ;;
  757. esac
  758. if [ -n "${repo_subcmd}" ]; then
  759. # shellcheck disable=SC2086
  760. env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts} >&3 2>&3 || fatal "Failed to update repository metadata." U000C
  761. fi
  762. for repopkg in netdata-repo netdata-repo-edge; do
  763. if ${pkg_installed_check} ${repopkg} > /dev/null 2>&1; then
  764. # shellcheck disable=SC2086
  765. env ${env} ${pm_cmd} ${upgrade_subcmd} ${pkg_install_opts} ${repopkg} >&3 2>&3 || fatal "Failed to update Netdata repository config." U000D
  766. # shellcheck disable=SC2086
  767. if [ -n "${repo_subcmd}" ]; then
  768. env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts} >&3 2>&3 || fatal "Failed to update repository metadata." U000E
  769. fi
  770. fi
  771. done
  772. current_major="$(netdata -v | cut -f 2 -d ' ' | cut -f 1 -d '.' | tr -d 'v')"
  773. latest_major="$(get_new_binpkg_major)"
  774. if [ -n "${latest_major}" ] && [ "${latest_major}" -ne "${current_major}" ]; then
  775. update_safe=0
  776. for v in ${NETDATA_ACCEPT_MAJOR_VERSIONS}; do
  777. if [ "${latest_major}" -eq "${v}" ]; then
  778. update_safe=1
  779. break
  780. fi
  781. done
  782. if [ "${update_safe}" -eq 0 ]; then
  783. warn_major_update
  784. fi
  785. fi
  786. # shellcheck disable=SC2086
  787. env ${env} ${pm_cmd} ${upgrade_subcmd} ${pkg_install_opts} netdata >&3 2>&3 || fatal "Failed to update Netdata package." U000F
  788. if ${pkg_installed_check} systemd > /dev/null 2>&1; then
  789. if [ "${NETDATA_NO_SYSTEMD_JOURNAL}" -eq 0 ]; then
  790. if ! ${pkg_installed_check} netdata-plugin-systemd-journal > /dev/null 2>&1; then
  791. env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} netdata-plugin-systemd-journal >&3 2>&3
  792. if [ -n "${mark_auto_cmd}" ]; then
  793. # shellcheck disable=SC2086
  794. env ${env} ${mark_auto_cmd} netdata-plugin-systemd-journal >&3 2>&3
  795. fi
  796. fi
  797. fi
  798. fi
  799. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  800. return 0
  801. }
  802. # Simple function to encapsulate original updater behavior.
  803. update_legacy() {
  804. set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
  805. case "${IS_NETDATA_STATIC_BINARY}" in
  806. yes) update_static && exit 0 ;;
  807. *) update_build && exit 0 ;;
  808. esac
  809. }
  810. logfile=
  811. ndtmpdir=
  812. trap cleanup EXIT
  813. if [ -t 2 ] || [ "${GITHUB_ACTIONS}" ]; then
  814. # we are running on a terminal or under CI
  815. # open fd 3 and send it to stderr
  816. exec 3>&2
  817. else
  818. # we are headless
  819. # create a temporary file for the log
  820. logfile="$(mktemp -t netdata-updater.log.XXXXXX)"
  821. # open fd 3 and send it to logfile
  822. exec 3> "${logfile}"
  823. fi
  824. : "${ENVIRONMENT_FILE:=THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT}"
  825. if [ "${ENVIRONMENT_FILE}" = "THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT" ]; then
  826. if [ -r "${script_dir}/../../../etc/netdata/.environment" ] || [ -r "${script_dir}/../../../etc/netdata/.install-type" ]; then
  827. ENVIRONMENT_FILE="${script_dir}/../../../etc/netdata/.environment"
  828. elif [ -r "/etc/netdata/.environment" ] || [ -r "/etc/netdata/.install-type" ]; then
  829. ENVIRONMENT_FILE="/etc/netdata/.environment"
  830. elif [ -r "/opt/netdata/etc/netdata/.environment" ] || [ -r "/opt/netdata/etc/netdata/.install-type" ]; then
  831. ENVIRONMENT_FILE="/opt/netdata/etc/netdata/.environment"
  832. else
  833. envpath="$(find / -type d \( -path /sys -o -path /proc -o -path /dev \) -prune -false -o -path '*netdata/.environment' -type f 2> /dev/null | head -n 1)"
  834. itpath="$(find / -type d \( -path /sys -o -path /proc -o -path /dev \) -prune -false -o -path '*netdata/.install-type' -type f 2> /dev/null | head -n 1)"
  835. if [ -r "${envpath}" ]; then
  836. ENVIRONMENT_FILE="${envpath}"
  837. elif [ -r "${itpath}" ]; then
  838. ENVIRONMENT_FILE="$(dirname "${itpath}")/.environment"
  839. else
  840. fatal "Cannot find environment file or install type file, unable to update." U0010
  841. fi
  842. fi
  843. fi
  844. if [ -r "${ENVIRONMENT_FILE}" ] ; then
  845. # shellcheck source=/dev/null
  846. . "${ENVIRONMENT_FILE}" || fatal "Failed to source ${ENVIRONMENT_FILE}" U0014
  847. fi
  848. if [ -r "$(dirname "${ENVIRONMENT_FILE}")/.install-type" ]; then
  849. # shellcheck source=/dev/null
  850. . "$(dirname "${ENVIRONMENT_FILE}")/.install-type" || fatal "Failed to source $(dirname "${ENVIRONMENT_FILE}")/.install-type" U0015
  851. fi
  852. UPDATER_CONFIG_PATH="$(dirname "${ENVIRONMENT_FILE}")/netdata-updater.conf"
  853. if [ -r "${UPDATER_CONFIG_PATH}" ]; then
  854. # shellcheck source=/dev/null
  855. . "${UPDATER_CONFIG_PATH}"
  856. fi
  857. [ -z "${NETDATA_ACCEPT_MAJOR_VERSIONS}" ] && NETDATA_ACCEPT_MAJOR_VERSIONS="${NETDATA_DEFAULT_ACCEPT_MAJOR_VERSIONS}"
  858. while [ -n "${1}" ]; do
  859. case "${1}" in
  860. --not-running-from-cron) NETDATA_NOT_RUNNING_FROM_CRON=1 ;;
  861. --no-updater-self-update) NETDATA_NO_UPDATER_SELF_UPDATE=1 ;;
  862. --force-update) NETDATA_FORCE_UPDATE=1 ;;
  863. --non-interactive) INTERACTIVE=0 ;;
  864. --interactive) INTERACTIVE=1 ;;
  865. --offline-install-source)
  866. NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
  867. shift 1
  868. ;;
  869. --tmpdir-path)
  870. NETDATA_TMPDIR_PATH="${2}"
  871. shift 1
  872. ;;
  873. --enable-auto-updates)
  874. enable_netdata_updater "${2}"
  875. exit $?
  876. ;;
  877. --disable-auto-updates)
  878. disable_netdata_updater
  879. exit $?
  880. ;;
  881. *) fatal "Unrecognized option ${1}" U001A ;;
  882. esac
  883. shift 1
  884. done
  885. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  886. NETDATA_NO_UPDATER_SELF_UPDATE=1
  887. NETDATA_UPDATER_JITTER=0
  888. NETDATA_FORCE_UPDATE=1
  889. fi
  890. # Random sleep to alleviate stampede effect of Agents upgrading
  891. # and disconnecting/reconnecting at the same time (or near to).
  892. # But only we're not a controlling terminal (tty)
  893. # Randomly sleep between 1s and 60m
  894. if [ ! -t 1 ] && \
  895. [ -z "${GITHUB_ACTIONS}" ] && \
  896. [ -z "${NETDATA_NOT_RUNNING_FROM_CRON}" ] && \
  897. is_integer "${NETDATA_UPDATER_JITTER}" && \
  898. [ "${NETDATA_UPDATER_JITTER}" -gt 1 ]; then
  899. rnd="$(awk "
  900. BEGIN { srand()
  901. printf(\"%d\\n\", ${NETDATA_UPDATER_JITTER} * rand())
  902. }")"
  903. sleep $(((rnd % NETDATA_UPDATER_JITTER) + 1))
  904. fi
  905. # We dont expect to find lib dir variable on older installations, so load this path if none found
  906. export NETDATA_LIB_DIR="${NETDATA_LIB_DIR:-${NETDATA_PREFIX}/var/lib/netdata}"
  907. # Source the tarball checksum, if not already available from environment (for existing installations with the old logic)
  908. [ -z "${NETDATA_TARBALL_CHECKSUM}" ] && [ -f "${NETDATA_LIB_DIR}/netdata.tarball.checksum" ] && NETDATA_TARBALL_CHECKSUM="$(cat "${NETDATA_LIB_DIR}/netdata.tarball.checksum")"
  909. if echo "$INSTALL_TYPE" | grep -qv ^binpkg && [ "${INSTALL_UID}" != "$(id -u)" ]; then
  910. fatal "You are running this script as user with uid $(id -u). We recommend to run this script as root (user with uid 0)" U0011
  911. fi
  912. self_update
  913. # shellcheck disable=SC2153
  914. case "${INSTALL_TYPE}" in
  915. *-build)
  916. validate_environment_file
  917. set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
  918. update_build && exit 0
  919. ;;
  920. *-static*)
  921. validate_environment_file
  922. set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
  923. update_static && exit 0
  924. ;;
  925. *binpkg*) update_binpkg && exit 0 ;;
  926. "") # Fallback case for no `.install-type` file. This just works like the old install type detection.
  927. validate_environment_file
  928. update_legacy
  929. ;;
  930. custom)
  931. # At this point, we _should_ have a valid `.environment` file, but it's best to just check.
  932. # If we do, then behave like the legacy updater.
  933. if validate_environment_file && [ -n "${IS_NETDATA_STATIC_BINARY}" ]; then
  934. update_legacy
  935. else
  936. fatal "This script does not support updating custom installations without valid environment files." U0012
  937. fi
  938. ;;
  939. oci) fatal "This script does not support updating Netdata inside our official Docker containers, please instead update the container itself." U0013 ;;
  940. *) fatal "Unrecognized installation type (${INSTALL_TYPE}), unable to update." U0014 ;;
  941. esac