netdata-updater.sh 37 KB

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