netdata-updater.sh 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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. ndtmpdir="$(create_tmp_directory)"
  441. commit_check_file="${ndtmpdir}/latest-commit.json"
  442. commit_check_url="https://api.github.com/repos/netdata/netdata/commits?path=packaging%2Finstaller%2Fnetdata-updater.sh&page=1&per_page=1"
  443. python_version_check="
  444. from __future__ import print_function
  445. import sys, json
  446. try:
  447. data = json.load(sys.stdin)
  448. except:
  449. print('')
  450. else:
  451. print(data[0]['commit']['committer']['date'] if isinstance(data, list) and data else '')
  452. "
  453. _safe_download "${commit_check_url}" "${commit_check_file}"
  454. if command -v jq > /dev/null 2>&1; then
  455. commit_date="$(jq '.[0].commit.committer.date' 2>/dev/null < "${commit_check_file}" | tr -d '"')"
  456. elif command -v python > /dev/null 2>&1;then
  457. commit_date="$(python -c "${python_version_check}" < "${commit_check_file}")"
  458. elif command -v python3 > /dev/null 2>&1;then
  459. commit_date="$(python3 -c "${python_version_check}" < "${commit_check_file}")"
  460. fi
  461. if [ -z "${commit_date}" ] ; then
  462. return 0
  463. elif [ "$(uname)" = "Linux" ]; then
  464. commit_date="$(date -d "${commit_date}" +%s)"
  465. else # assume BSD-style `date` if we are not on Linux
  466. commit_date="$(/bin/date -j -f "%Y-%m-%dT%H:%M:%SZ" "${commit_date}" +%s 2>/dev/null)"
  467. if [ -z "${commit_date}" ]; then
  468. return 0
  469. fi
  470. fi
  471. if [ -e "${script_source}" ]; then
  472. script_date="$(date -r "${script_source}" +%s)"
  473. else
  474. script_date="$(date +%s)"
  475. fi
  476. [ "${commit_date}" -ge "${script_date}" ]
  477. }
  478. self_update() {
  479. if [ -z "${NETDATA_NO_UPDATER_SELF_UPDATE}" ] && newer_commit_date; then
  480. info "Downloading newest version of updater script."
  481. ndtmpdir=$(create_tmp_directory)
  482. cd "$ndtmpdir" || exit 1
  483. if _safe_download "https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-updater.sh" ./netdata-updater.sh; then
  484. chmod +x ./netdata-updater.sh || exit 1
  485. export ENVIRONMENT_FILE="${ENVIRONMENT_FILE}"
  486. force_update=""
  487. [ "$NETDATA_FORCE_UPDATE" = "1" ] && force_update="--force-update"
  488. interactive=""
  489. [ "$INTERACTIVE" = "0" ] && interactive="--non-interactive"
  490. exec ./netdata-updater.sh --not-running-from-cron --no-updater-self-update "$force_update" "$interactive" --tmpdir-path "$(pwd)"
  491. else
  492. error "Failed to download newest version of updater script, continuing with current version."
  493. fi
  494. fi
  495. }
  496. parse_version() {
  497. r="${1}"
  498. if [ "${r}" = "latest" ]; then
  499. # If we get ‘latest’ as a version, return the largest possible
  500. # version value.
  501. printf "99999999999999"
  502. return 0
  503. elif echo "${r}" | grep -q '^v.*'; then
  504. # shellcheck disable=SC2001
  505. # XXX: Need a regex group substitution here.
  506. r="$(echo "${r}" | sed -e 's/^v\(.*\)/\1/')"
  507. fi
  508. tmpfile="$(mktemp)"
  509. echo "${r}" | tr '-' ' ' > "${tmpfile}"
  510. read -r v b _ < "${tmpfile}"
  511. if echo "${b}" | grep -vEq "^[0-9]+$"; then
  512. b="0"
  513. fi
  514. echo "${v}" | tr '.' ' ' > "${tmpfile}"
  515. read -r maj min patch _ < "${tmpfile}"
  516. rm -f "${tmpfile}"
  517. printf "%03d%03d%03d%05d" "${maj}" "${min}" "${patch}" "${b}"
  518. }
  519. get_latest_version() {
  520. if [ "${RELEASE_CHANNEL}" = "stable" ]; then
  521. get_netdata_latest_tag "${NETDATA_STABLE_BASE_URL}"
  522. else
  523. get_netdata_latest_tag "${NETDATA_NIGHTLY_BASE_URL}"
  524. fi
  525. }
  526. validate_environment_file() {
  527. if [ -n "${NETDATA_PREFIX+SET_BUT_NULL}" ] && [ -n "${REINSTALL_OPTIONS+SET_BUT_NULL}" ]; then
  528. return 0
  529. else
  530. fatal "Environment file located at ${ENVIRONMENT_FILE} is not valid, unable to update." U0007
  531. fi
  532. }
  533. update_available() {
  534. if [ "$NETDATA_FORCE_UPDATE" = "1" ]; then
  535. info "Force update requested"
  536. return 0
  537. fi
  538. basepath="$(dirname "$(dirname "$(dirname "${NETDATA_LIB_DIR}")")")"
  539. searchpath="${basepath}/bin:${basepath}/sbin:${basepath}/usr/bin:${basepath}/usr/sbin:${PATH}"
  540. searchpath="${basepath}/netdata/bin:${basepath}/netdata/sbin:${basepath}/netdata/usr/bin:${basepath}/netdata/usr/sbin:${searchpath}"
  541. ndbinary="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
  542. if [ -z "${ndbinary}" ]; then
  543. current_version=0
  544. else
  545. current_version="$(parse_version "$(${ndbinary} -v | cut -f 2 -d ' ')")"
  546. fi
  547. latest_tag="$(get_latest_version)"
  548. latest_version="$(parse_version "${latest_tag}")"
  549. path_version="$(echo "${latest_tag}" | cut -f 1 -d "-")"
  550. # If we can't get the current version for some reason assume `0`
  551. current_version="${current_version:-0}"
  552. # If we can't get the latest version for some reason assume `0`
  553. latest_version="${latest_version:-0}"
  554. info "Current Version: ${current_version}"
  555. info "Latest Version: ${latest_version}"
  556. if [ "${latest_version}" -gt 0 ] && [ "${current_version}" -gt 0 ] && [ "${current_version}" -ge "${latest_version}" ]; then
  557. info "Newest version (current=${current_version} >= latest=${latest_version}) is already installed"
  558. return 1
  559. else
  560. info "Update available"
  561. if [ "${current_version}" -ne 0 ] && [ "${latest_version}" -ne 0 ]; then
  562. current_major="$(${ndbinary} -v | cut -f 2 -d ' ' | cut -f 1 -d '.' | tr -d 'v')"
  563. latest_major="$(echo "${latest_tag}" | cut -f 1 -d '.' | tr -d 'v')"
  564. if [ "${current_major}" -ne "${latest_major}" ]; then
  565. update_safe=0
  566. for v in ${NETDATA_ACCEPT_MAJOR_VERSIONS}; do
  567. if [ "${latest_major}" -eq "${v}" ]; then
  568. update_safe=1
  569. break
  570. fi
  571. done
  572. if [ "${update_safe}" -eq 0 ]; then
  573. warn_major_update
  574. fi
  575. fi
  576. fi
  577. return 0
  578. fi
  579. }
  580. set_tarball_urls() {
  581. filename="netdata-latest.tar.gz"
  582. if [ "$2" = "yes" ]; then
  583. if [ -e /opt/netdata/etc/netdata/.install-type ]; then
  584. # shellcheck disable=SC1091
  585. . /opt/netdata/etc/netdata/.install-type
  586. [ -z "${PREBUILT_ARCH:-}" ] && PREBUILT_ARCH="$(uname -m)"
  587. filename="netdata-${PREBUILT_ARCH}-latest.gz.run"
  588. else
  589. filename="netdata-x86_64-latest.gz.run"
  590. fi
  591. fi
  592. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  593. path="$(cd "${NETDATA_OFFLINE_INSTALL_SOURCE}" || exit 1; pwd)"
  594. export NETDATA_TARBALL_URL="file://${path}/${filename}"
  595. export NETDATA_TARBALL_CHECKSUM_URL="file://${path}/sha256sums.txt"
  596. elif [ "$1" = "stable" ]; then
  597. latest="$(get_netdata_latest_tag "${NETDATA_STABLE_BASE_URL}")"
  598. export NETDATA_TARBALL_URL="${NETDATA_STABLE_BASE_URL}/download/$latest/${filename}"
  599. export NETDATA_TARBALL_CHECKSUM_URL="${NETDATA_STABLE_BASE_URL}/download/$latest/sha256sums.txt"
  600. else
  601. tag="$(get_netdata_latest_tag "${NETDATA_NIGHTLY_BASE_URL}")"
  602. export NETDATA_TARBALL_URL="${NETDATA_NIGHTLY_BASE_URL}/download/${tag}/${filename}"
  603. export NETDATA_TARBALL_CHECKSUM_URL="${NETDATA_NIGHTLY_BASE_URL}/download/${tag}/sha256sums.txt"
  604. fi
  605. }
  606. update_build() {
  607. [ -z "${logfile}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
  608. RUN_INSTALLER=0
  609. ndtmpdir=$(create_tmp_directory)
  610. cd "$ndtmpdir" || fatal "Failed to change current working directory to ${ndtmpdir}" U0016
  611. install_build_dependencies
  612. if update_available; then
  613. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt" >&3 2>&3
  614. download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-latest.tar.gz"
  615. if [ -n "${NETDATA_TARBALL_CHECKSUM}" ] &&
  616. grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3 &&
  617. [ "$NETDATA_FORCE_UPDATE" != "1" ]; then
  618. info "Newest version is already installed"
  619. else
  620. if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
  621. 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
  622. fi
  623. NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2> /dev/null | cut -d' ' -f1)"
  624. tar -xf netdata-latest.tar.gz >&3 2>&3
  625. rm netdata-latest.tar.gz >&3 2>&3
  626. if [ -z "$path_version" ]; then
  627. latest_tag="$(get_latest_version)"
  628. path_version="$(echo "${latest_tag}" | cut -f 1 -d "-")"
  629. fi
  630. cd "$(find . -maxdepth 1 -type d -name "netdata-${path_version}*" | head -n 1)" || fatal "Failed to switch to build directory" U0017
  631. RUN_INSTALLER=1
  632. fi
  633. fi
  634. # We got the sources, run the update now
  635. if [ ${RUN_INSTALLER} -eq 1 ]; then
  636. # signal netdata to start saving its database
  637. # this is handy if your database is big
  638. possible_pids=$(pidof netdata)
  639. do_not_start=
  640. if [ -n "${possible_pids}" ]; then
  641. # shellcheck disable=SC2086
  642. kill -USR1 ${possible_pids}
  643. else
  644. # netdata is currently not running, so do not start it after updating
  645. do_not_start="--dont-start-it"
  646. fi
  647. env="env TMPDIR=${TMPDIR}"
  648. if [ -n "${NETDATA_SELECTED_DASHBOARD}" ]; then
  649. env="${env} NETDATA_SELECTED_DASHBOARD=${NETDATA_SELECTED_DASHBOARD}"
  650. fi
  651. if [ ! -x ./netdata-installer.sh ]; then
  652. if [ "$(find . -mindepth 1 -maxdepth 1 -type d | wc -l)" -eq 1 ] && [ -x "$(find . -mindepth 1 -maxdepth 1 -type d)/netdata-installer.sh" ]; then
  653. cd "$(find . -mindepth 1 -maxdepth 1 -type d)" || fatal "Failed to switch to build directory" U0018
  654. fi
  655. fi
  656. if [ -e "${NETDATA_PREFIX}/etc/netdata/.install-type" ] ; then
  657. install_type="$(cat "${NETDATA_PREFIX}"/etc/netdata/.install-type)"
  658. else
  659. install_type="INSTALL_TYPE='legacy-build'"
  660. fi
  661. if [ "${INSTALL_TYPE}" = "custom" ] && [ -f "${NETDATA_PREFIX}" ]; then
  662. install_type="INSTALL_TYPE='legacy-build'"
  663. fi
  664. info "Re-installing netdata..."
  665. export NETDATA_SAVE_WARNINGS=1
  666. export NETDATA_PROPAGATE_WARNINGS=1
  667. export NETDATA_WARNINGS="${NETDATA_WARNINGS}"
  668. export NETDATA_SCRIPT_STATUS_PATH="${NETDATA_SCRIPT_STATUS_PATH}"
  669. # shellcheck disable=SC2086
  670. if ! ${env} ./netdata-installer.sh ${REINSTALL_OPTIONS} --dont-wait ${do_not_start} >&3 2>&3; then
  671. if [ -r "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
  672. # shellcheck disable=SC1090
  673. . "${NETDATA_SCRIPT_STATUS_PATH}"
  674. rm -f "${NETDATA_SCRIPT_STATUS_PATH}"
  675. fi
  676. if [ -n "${EXIT_REASON}" ]; then
  677. fatal "Failed to rebuild existing netdata install: ${EXIT_REASON}" "U${EXIT_CODE}"
  678. else
  679. fatal "Failed to rebuild existing netdata reinstall." UI0000
  680. fi
  681. fi
  682. # We no longer store checksum info here. but leave this so that we clean up all environment files upon next update.
  683. sed -i '/NETDATA_TARBALL/d' "${ENVIRONMENT_FILE}"
  684. info "Updating tarball checksum info"
  685. echo "${NEW_CHECKSUM}" > "${NETDATA_LIB_DIR}/netdata.tarball.checksum"
  686. echo "${install_type}" > "${NETDATA_PREFIX}/etc/netdata/.install-type"
  687. fi
  688. rm -rf "${ndtmpdir}" >&3 2>&3
  689. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  690. return 0
  691. }
  692. update_static() {
  693. ndtmpdir="$(create_tmp_directory)"
  694. PREVDIR="$(pwd)"
  695. info "Entering ${ndtmpdir}"
  696. cd "${ndtmpdir}" || fatal "Failed to change current working directory to ${ndtmpdir}" U0019
  697. if update_available; then
  698. sysarch="${PREBUILT_ARCH}"
  699. [ -z "$sysarch" ] && sysarch="$(uname -m)"
  700. download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt"
  701. download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-${sysarch}-latest.gz.run"
  702. if ! grep "netdata-${sysarch}-latest.gz.run" "${ndtmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
  703. 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
  704. fi
  705. if [ -e /opt/netdata/etc/netdata/.install-type ] ; then
  706. install_type="$(cat /opt/netdata/etc/netdata/.install-type)"
  707. else
  708. install_type="INSTALL_TYPE='legacy-static'"
  709. fi
  710. # Do not pass any options other than the accept, for now
  711. # shellcheck disable=SC2086
  712. if sh "${ndtmpdir}/netdata-${sysarch}-latest.gz.run" --accept -- ${REINSTALL_OPTIONS} >&3 2>&3; then
  713. rm -r "${ndtmpdir}"
  714. else
  715. info "NOTE: did not remove: ${ndtmpdir}"
  716. fi
  717. echo "${install_type}" > /opt/netdata/etc/netdata/.install-type
  718. fi
  719. if [ -e "${PREVDIR}" ]; then
  720. info "Switching back to ${PREVDIR}"
  721. cd "${PREVDIR}"
  722. fi
  723. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  724. exit 0
  725. }
  726. get_new_binpkg_major() {
  727. case "${pm_cmd}" in
  728. apt-get) apt-get --just-print upgrade 2>&1 | grep Inst | grep ' netdata ' | cut -f 3 -d ' ' | tr -d '[]' | cut -f 1 -d '.' ;;
  729. yum) yum check-update netdata | grep -E '^netdata ' | awk '{print $2}' | cut -f 1 -d '.' ;;
  730. dnf) dnf check-update netdata | grep -E '^netdata ' | awk '{print $2}' | cut -f 1 -d '.' ;;
  731. zypper) zypper list-updates | grep '| netdata |' | cut -f 5 -d '|' | tr -d ' ' | cut -f 1 -d '.' ;;
  732. esac
  733. }
  734. update_binpkg() {
  735. os_release_file=
  736. if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
  737. os_release_file="/etc/os-release"
  738. elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
  739. os_release_file="/usr/lib/os-release"
  740. else
  741. fatal "Cannot find an os-release file ..." U000B
  742. fi
  743. # shellcheck disable=SC1090
  744. . "${os_release_file}"
  745. DISTRO="${ID}"
  746. supported_compat_names="debian ubuntu centos fedora opensuse ol amzn"
  747. if str_in_list "${DISTRO}" "${supported_compat_names}"; then
  748. DISTRO_COMPAT_NAME="${DISTRO}"
  749. else
  750. case "${DISTRO}" in
  751. opensuse-leap|opensuse-tumbleweed)
  752. DISTRO_COMPAT_NAME="opensuse"
  753. ;;
  754. cloudlinux|almalinux|centos-stream|rocky|rhel)
  755. DISTRO_COMPAT_NAME="centos"
  756. ;;
  757. *)
  758. DISTRO_COMPAT_NAME="unknown"
  759. ;;
  760. esac
  761. fi
  762. interactive_opts=""
  763. env=""
  764. case "${DISTRO_COMPAT_NAME}" in
  765. debian|ubuntu)
  766. if [ "${INTERACTIVE}" = "0" ]; then
  767. upgrade_subcmd="-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold --only-upgrade install"
  768. interactive_opts="-y"
  769. env="DEBIAN_FRONTEND=noninteractive"
  770. else
  771. upgrade_subcmd="--only-upgrade install"
  772. fi
  773. pm_cmd="apt-get"
  774. repo_subcmd="update"
  775. install_subcmd="install"
  776. mark_auto_cmd="apt-mark auto"
  777. pkg_install_opts="${interactive_opts}"
  778. repo_update_opts="${interactive_opts}"
  779. pkg_installed_check="dpkg-query -s"
  780. INSTALL_TYPE="binpkg-deb"
  781. ;;
  782. centos|fedora|ol|amzn)
  783. if [ "${INTERACTIVE}" = "0" ]; then
  784. interactive_opts="-y"
  785. fi
  786. if command -v dnf > /dev/null; then
  787. pm_cmd="dnf"
  788. repo_subcmd="makecache"
  789. mark_auto_cmd="dnf mark remove"
  790. else
  791. pm_cmd="yum"
  792. mark_auto_cmd="yumdb set reason dep"
  793. fi
  794. upgrade_subcmd="upgrade"
  795. install_subcmd="install"
  796. pkg_install_opts="${interactive_opts}"
  797. repo_update_opts="${interactive_opts}"
  798. pkg_installed_check="rpm -q"
  799. INSTALL_TYPE="binpkg-rpm"
  800. ;;
  801. opensuse)
  802. if [ "${INTERACTIVE}" = "0" ]; then
  803. upgrade_subcmd="--non-interactive update"
  804. else
  805. upgrade_subcmd="update"
  806. fi
  807. pm_cmd="zypper"
  808. repo_subcmd="--gpg-auto-import-keys refresh"
  809. install_subcmd="install"
  810. mark_auto_cmd=""
  811. pkg_install_opts=""
  812. repo_update_opts=""
  813. pkg_installed_check="rpm -q"
  814. INSTALL_TYPE="binpkg-rpm"
  815. ;;
  816. *)
  817. warning "We do not provide native packages for ${DISTRO}."
  818. return 2
  819. ;;
  820. esac
  821. if [ -n "${repo_subcmd}" ]; then
  822. # shellcheck disable=SC2086
  823. env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts} >&3 2>&3 || fatal "Failed to update repository metadata." U000C
  824. fi
  825. for repopkg in netdata-repo netdata-repo-edge; do
  826. if ${pkg_installed_check} ${repopkg} > /dev/null 2>&1; then
  827. # shellcheck disable=SC2086
  828. env ${env} ${pm_cmd} ${upgrade_subcmd} ${pkg_install_opts} ${repopkg} >&3 2>&3 || fatal "Failed to update Netdata repository config." U000D
  829. # shellcheck disable=SC2086
  830. if [ -n "${repo_subcmd}" ]; then
  831. env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts} >&3 2>&3 || fatal "Failed to update repository metadata." U000E
  832. fi
  833. fi
  834. done
  835. current_major="$(netdata -v | cut -f 2 -d ' ' | cut -f 1 -d '.' | tr -d 'v')"
  836. latest_major="$(get_new_binpkg_major)"
  837. if [ -n "${latest_major}" ] && [ "${latest_major}" -ne "${current_major}" ]; then
  838. update_safe=0
  839. for v in ${NETDATA_ACCEPT_MAJOR_VERSIONS}; do
  840. if [ "${latest_major}" -eq "${v}" ]; then
  841. update_safe=1
  842. break
  843. fi
  844. done
  845. if [ "${update_safe}" -eq 0 ]; then
  846. warn_major_update
  847. fi
  848. fi
  849. # shellcheck disable=SC2086
  850. env ${env} ${pm_cmd} ${upgrade_subcmd} ${pkg_install_opts} netdata >&3 2>&3 || fatal "Failed to update Netdata package." U000F
  851. if ${pkg_installed_check} systemd > /dev/null 2>&1; then
  852. if [ "${NETDATA_NO_SYSTEMD_JOURNAL}" -eq 0 ]; then
  853. if ! ${pkg_installed_check} netdata-plugin-systemd-journal > /dev/null 2>&1; then
  854. env ${env} ${pm_cmd} ${install_subcmd} ${pkg_install_opts} netdata-plugin-systemd-journal >&3 2>&3
  855. if [ -n "${mark_auto_cmd}" ]; then
  856. # shellcheck disable=SC2086
  857. env ${env} ${mark_auto_cmd} netdata-plugin-systemd-journal >&3 2>&3
  858. fi
  859. fi
  860. fi
  861. fi
  862. [ -n "${logfile}" ] && rm "${logfile}" && logfile=
  863. return 0
  864. }
  865. # Simple function to encapsulate original updater behavior.
  866. update_legacy() {
  867. set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
  868. case "${IS_NETDATA_STATIC_BINARY}" in
  869. yes) update_static && exit 0 ;;
  870. *) update_build && exit 0 ;;
  871. esac
  872. }
  873. logfile=
  874. ndtmpdir=
  875. trap cleanup EXIT
  876. if [ -t 2 ] || [ "${GITHUB_ACTIONS}" ]; then
  877. # we are running on a terminal or under CI
  878. # open fd 3 and send it to stderr
  879. exec 3>&2
  880. else
  881. # we are headless
  882. # create a temporary file for the log
  883. logfile="$(mktemp -t netdata-updater.log.XXXXXX)"
  884. # open fd 3 and send it to logfile
  885. exec 3> "${logfile}"
  886. fi
  887. : "${ENVIRONMENT_FILE:=THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT}"
  888. if [ "${ENVIRONMENT_FILE}" = "THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT" ]; then
  889. if [ -r "${script_dir}/../../../etc/netdata/.environment" ] || [ -r "${script_dir}/../../../etc/netdata/.install-type" ]; then
  890. ENVIRONMENT_FILE="${script_dir}/../../../etc/netdata/.environment"
  891. elif [ -r "/etc/netdata/.environment" ] || [ -r "/etc/netdata/.install-type" ]; then
  892. ENVIRONMENT_FILE="/etc/netdata/.environment"
  893. elif [ -r "/opt/netdata/etc/netdata/.environment" ] || [ -r "/opt/netdata/etc/netdata/.install-type" ]; then
  894. ENVIRONMENT_FILE="/opt/netdata/etc/netdata/.environment"
  895. else
  896. 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)"
  897. 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)"
  898. if [ -r "${envpath}" ]; then
  899. ENVIRONMENT_FILE="${envpath}"
  900. elif [ -r "${itpath}" ]; then
  901. ENVIRONMENT_FILE="$(dirname "${itpath}")/.environment"
  902. else
  903. fatal "Cannot find environment file or install type file, unable to update." U0010
  904. fi
  905. fi
  906. fi
  907. if [ -r "${ENVIRONMENT_FILE}" ] ; then
  908. # shellcheck source=/dev/null
  909. . "${ENVIRONMENT_FILE}" || fatal "Failed to source ${ENVIRONMENT_FILE}" U0014
  910. fi
  911. if [ -r "$(dirname "${ENVIRONMENT_FILE}")/.install-type" ]; then
  912. # shellcheck source=/dev/null
  913. . "$(dirname "${ENVIRONMENT_FILE}")/.install-type" || fatal "Failed to source $(dirname "${ENVIRONMENT_FILE}")/.install-type" U0015
  914. fi
  915. UPDATER_CONFIG_PATH="$(dirname "${ENVIRONMENT_FILE}")/netdata-updater.conf"
  916. if [ -r "${UPDATER_CONFIG_PATH}" ]; then
  917. # shellcheck source=/dev/null
  918. . "${UPDATER_CONFIG_PATH}"
  919. fi
  920. [ -z "${NETDATA_ACCEPT_MAJOR_VERSIONS}" ] && NETDATA_ACCEPT_MAJOR_VERSIONS="${NETDATA_DEFAULT_ACCEPT_MAJOR_VERSIONS}"
  921. while [ -n "${1}" ]; do
  922. case "${1}" in
  923. --not-running-from-cron) NETDATA_NOT_RUNNING_FROM_CRON=1 ;;
  924. --no-updater-self-update) NETDATA_NO_UPDATER_SELF_UPDATE=1 ;;
  925. --force-update) NETDATA_FORCE_UPDATE=1 ;;
  926. --non-interactive) INTERACTIVE=0 ;;
  927. --interactive) INTERACTIVE=1 ;;
  928. --offline-install-source)
  929. NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
  930. shift 1
  931. ;;
  932. --tmpdir-path)
  933. NETDATA_TMPDIR_PATH="${2}"
  934. shift 1
  935. ;;
  936. --enable-auto-updates)
  937. enable_netdata_updater "${2}"
  938. exit $?
  939. ;;
  940. --disable-auto-updates)
  941. disable_netdata_updater
  942. exit $?
  943. ;;
  944. *) fatal "Unrecognized option ${1}" U001A ;;
  945. esac
  946. shift 1
  947. done
  948. if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
  949. NETDATA_NO_UPDATER_SELF_UPDATE=1
  950. NETDATA_UPDATER_JITTER=0
  951. NETDATA_FORCE_UPDATE=1
  952. fi
  953. # If we seem to be running under anacron, act as if we’re not running from cron.
  954. # This is mostly to disable jitter, which should not be needed when run from anacron.
  955. if running_under_anacron; then
  956. NETDATA_NOT_RUNNING_FROM_CRON="${NETDATA_NOT_RUNNING_FROM_CRON:-1}"
  957. fi
  958. # Random sleep to alleviate stampede effect of Agents upgrading
  959. # and disconnecting/reconnecting at the same time (or near to).
  960. # But only we're not a controlling terminal (tty)
  961. # Randomly sleep between 1s and 60m
  962. if [ ! -t 1 ] && \
  963. [ -z "${GITHUB_ACTIONS}" ] && \
  964. [ -z "${NETDATA_NOT_RUNNING_FROM_CRON}" ] && \
  965. is_integer "${NETDATA_UPDATER_JITTER}" && \
  966. [ "${NETDATA_UPDATER_JITTER}" -gt 1 ]; then
  967. rnd="$(awk "
  968. BEGIN { srand()
  969. printf(\"%d\\n\", ${NETDATA_UPDATER_JITTER} * rand())
  970. }")"
  971. sleep $(((rnd % NETDATA_UPDATER_JITTER) + 1))
  972. fi
  973. # We dont expect to find lib dir variable on older installations, so load this path if none found
  974. export NETDATA_LIB_DIR="${NETDATA_LIB_DIR:-${NETDATA_PREFIX}/var/lib/netdata}"
  975. # Source the tarball checksum, if not already available from environment (for existing installations with the old logic)
  976. [ -z "${NETDATA_TARBALL_CHECKSUM}" ] && [ -f "${NETDATA_LIB_DIR}/netdata.tarball.checksum" ] && NETDATA_TARBALL_CHECKSUM="$(cat "${NETDATA_LIB_DIR}/netdata.tarball.checksum")"
  977. if echo "$INSTALL_TYPE" | grep -qv ^binpkg && [ "${INSTALL_UID}" != "$(id -u)" ]; then
  978. 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
  979. fi
  980. self_update
  981. # shellcheck disable=SC2153
  982. case "${INSTALL_TYPE}" in
  983. *-build)
  984. validate_environment_file
  985. set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
  986. update_build && exit 0
  987. ;;
  988. *-static*)
  989. validate_environment_file
  990. set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
  991. update_static && exit 0
  992. ;;
  993. *binpkg*) update_binpkg && exit 0 ;;
  994. "") # Fallback case for no `.install-type` file. This just works like the old install type detection.
  995. validate_environment_file
  996. update_legacy
  997. ;;
  998. custom)
  999. # At this point, we _should_ have a valid `.environment` file, but it's best to just check.
  1000. # If we do, then behave like the legacy updater.
  1001. if validate_environment_file && [ -n "${IS_NETDATA_STATIC_BINARY}" ]; then
  1002. update_legacy
  1003. else
  1004. fatal "This script does not support updating custom installations without valid environment files." U0012
  1005. fi
  1006. ;;
  1007. oci) fatal "This script does not support updating Netdata inside our official Docker containers, please instead update the container itself." U0013 ;;
  1008. *) fatal "Unrecognized installation type (${INSTALL_TYPE}), unable to update." U0014 ;;
  1009. esac