@cryptotaxi247 / netdata-1 / commits / 52f743c5a

Fix the code that checks for available updates. (#11870)

* Make update availability check it0s own function. And use it to check for updates for static builds too. * Update existing version detection logic to be far more robust. This brings it into consistency with the existing install detection code in the new kickstart script, which correctly handles almost all cases of existing installs. * Fix typo. * Assorted fixes. * Apply suggestions from code review Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud> Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud>

Austin S. Hemmelgarn committed Dec 17, 2021 at 09:51 UTC 52f743c5a7cdd3b56c36ac4c054bf182c5e193b1
1 file changed +69 -47
packaging/installer/netdata-updater.sh
+69 -47
@@ -38,6 +38,8 @@ else
38 script_source="${script_dir}/netdata-updater.sh"
39 fi
40
41 +PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
42 +
43 info() {
44 echo >&3 "$(date) : INFO: " "${@}"
45 }
@@ -257,6 +259,40 @@ get_latest_version() {
259 fi
260 }
261
262 +update_available() {
263 + basepath="$(dirname "$(dirname "$(dirname "${NETDATA_LIB_DIR}")")")"
264 + searchpath="${basepath}/bin:${basepath}/sbin:${basepath}/usr/bin:${basepath}/usr/sbin:${PATH}"
265 + searchpath="${basepath}/netdata/bin:${basepath}/netdata/sbin:${basepath}/netdata/usr/bin:${basepath}/netdata/usr/sbin:${searchpath}"
266 + ndbinary="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
267 +
268 + if [ -z "${ndbinary}" ]; then
269 + current_version=0
270 + else
271 + current_version="$(parse_version "$(${ndbinary} -v | cut -f 2 -d ' ')")"
272 + fi
273 +
274 + latest_tag="$(get_latest_version)"
275 + latest_version="$(parse_version "${latest_tag}")"
276 + path_version="$(echo "${latest_tag}" | cut -f 1 -d "-")"
277 +
278 + # If we can't get the current version for some reason assume `0`
279 + current_version="${current_version:-0}"
280 +
281 + # If we can't get the latest version for some reason assume `0`
282 + latest_version="${latest_version:-0}"
283 +
284 + info "Current Version: ${current_version}"
285 + info "Latest Version: ${latest_version}"
286 +
287 + if [ "${latest_version}" -gt 0 ] && [ "${current_version}" -gt 0 ] && [ "${current_version}" -ge "${latest_version}" ]; then
288 + info "Newest version (current=${current_version} >= latest=${latest_version}) is already installed"
289 + return 1
290 + else
291 + info "Update available"
292 + return 0
293 + fi
294 +}
295 +
296 set_tarball_urls() {
297 extension="tar.gz"
298
@@ -281,37 +317,21 @@ update() {
317 ndtmpdir=$(create_tmp_directory)
318 cd "$ndtmpdir" || exit 1
319
284 - download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt" >&3 2>&3
285 -
286 - current_version="$(command -v netdata > /dev/null && parse_version "$(netdata -v | cut -f 2 -d ' ')")"
287 - latest_tag="$(get_latest_version)"
288 - latest_version="$(parse_version "${latest_tag}")"
289 - path_version="$(echo "${latest_tag}" | cut -f 1 -d "-")"
290 -
291 - # If we can't get the current version for some reason assume `0`
292 - current_version="${current_version:-0}"
293 -
294 - # If we can't get the latest version for some reason assume `0`
295 - latest_version="${latest_version:-0}"
296 -
297 - info "Current Version: ${current_version}"
298 - info "Latest Version: ${latest_version}"
299 -
300 - if [ "${latest_version}" -gt 0 ] && [ "${current_version}" -gt 0 ] && [ "${current_version}" -ge "${latest_version}" ]; then
301 - info "Newest version (current=${current_version} >= latest=${latest_version}) is already installed"
302 - elif [ -n "${NETDATA_TARBALL_CHECKSUM}" ] && grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3; then
303 - info "Newest version is already installed"
304 - else
320 + if update_available; then
321 + download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt" >&3 2>&3
322 download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-latest.tar.gz"
306 - if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
307 - 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."
323 + if [ -n "${NETDATA_TARBALL_CHECKSUM}" ] && grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3; then
324 + info "Newest version is already installed"
325 + else
326 + if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
327 + 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."
328 + fi
329 + NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2> /dev/null | cut -d' ' -f1)"
330 + tar -xf netdata-latest.tar.gz >&3 2>&3
331 + rm netdata-latest.tar.gz >&3 2>&3
332 + cd "$(find . -maxdepth 1 -name "netdata-${path_version}*" | head -n 1)" || exit 1
333 + RUN_INSTALLER=1
334 fi
309 - NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2> /dev/null | cut -d' ' -f1)"
310 - tar -xf netdata-latest.tar.gz >&3 2>&3
311 - rm netdata-latest.tar.gz >&3 2>&3
312 - cd "$(find . -maxdepth 1 -name "netdata-${path_version}*" | head -n 1)" || exit 1
313 - RUN_INSTALLER=1
314 - cd "${NETDATA_LOCAL_TARBALL_OVERRIDE}" || exit 1
335 fi
336
337 # We got the sources, run the update now
@@ -434,27 +454,29 @@ if [ "${IS_NETDATA_STATIC_BINARY}" = "yes" ]; then
454 info "Entering ${ndtmpdir}"
455 cd "${ndtmpdir}" || exit 1
456
437 - download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt"
438 - download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-latest.gz.run"
439 - if ! grep netdata-latest.gz.run "${ndtmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
440 - 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."
441 - fi
457 + if update_available; then
458 + download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt"
459 + download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-latest.gz.run"
460 + if ! grep netdata-latest.gz.run "${ndtmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
461 + 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."
462 + fi
463
443 - if [ -e /opt/netdata/etc/netdata/.install-type ] ; then
444 - install_type="$(cat /opt/netdata/etc/netdata/.install-type)"
445 - else
446 - install_type="INSTALL_TYPE='legacy-static'"
447 - fi
464 + if [ -e /opt/netdata/etc/netdata/.install-type ] ; then
465 + install_type="$(cat /opt/netdata/etc/netdata/.install-type)"
466 + else
467 + install_type="INSTALL_TYPE='legacy-static'"
468 + fi
469
449 - # Do not pass any options other than the accept, for now
450 - # shellcheck disable=SC2086
451 - if sh "${ndtmpdir}/netdata-latest.gz.run" --accept -- ${REINSTALL_OPTIONS} >&3 2>&3; then
452 - rm -rf "${ndtmpdir}" >&3 2>&3
453 - else
454 - info "NOTE: did not remove: ${ndtmpdir}"
455 - fi
470 + # Do not pass any options other than the accept, for now
471 + # shellcheck disable=SC2086
472 + if sh "${ndtmpdir}/netdata-latest.gz.run" --accept -- ${REINSTALL_OPTIONS} >&3 2>&3; then
473 + rm -rf "${ndtmpdir}" >&3 2>&3
474 + else
475 + info "NOTE: did not remove: ${ndtmpdir}"
476 + fi
477
457 - echo "${install_type}" > /opt/netdata/etc/netdata/.install-type
478 + echo "${install_type}" > /opt/netdata/etc/netdata/.install-type
479 + fi
480
481 if [ -e "${PREVDIR}" ]; then
482 info "Switching back to ${PREVDIR}"