netdata-updater.sh 37 KB

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