netdata-updater.sh 37 KB

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