@cryptotaxi247 / netdata-1 / commits / a46e36aef

Add support to updater for updating native DEB/RPM installs with our official packages. (#11753)

* Restructure update type detection. * Add proper handling for binary package installs. * Add proper support for per-arch static installs. * Address review feedback. * Improved error messages for unsupported install types. * Fixed install type detection for static builds. * Improve handling around custom installs.

Austin S. Hemmelgarn committed Jan 10, 2022 at 10:48 UTC a46e36aef9397ded995090928042ad16229e5e9b
1 file changed +219 -46
packaging/installer/netdata-updater.sh
+219 -46
@@ -40,6 +40,12 @@ fi
40
41 PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
42
43 +if [ ! -t 1 ]; then
44 + INTERACTIVE=0
45 +else
46 + INTERACTIVE=1
47 +fi
48 +
49 info() {
50 echo >&3 "$(date) : INFO: " "${@}"
51 }
@@ -62,12 +68,16 @@ if [ "${ENVIRONMENT_FILE}" = "THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT" ]; th
68 if [ -r "${envpath}" ]; then
69 ENVIRONMENT_FILE="${envpath}"
70 else
65 - error "Cannot find environment file, unable to update."
66 - exit 1
71 + fatal "Cannot find environment file, unable to update."
72 fi
73 fi
74 fi
75
76 +str_in_list() {
77 + printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
78 + return $?
79 +}
80 +
81 safe_sha256sum() {
82 # Within the context of the installer, we only use -c option that is common between the two commands
83 # We will have to reconsider if we start non-common options
@@ -310,7 +320,7 @@ set_tarball_urls() {
320 fi
321 }
322
313 -update() {
323 +update_build() {
324 [ -z "${logfile}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
325
326 RUN_INSTALLER=0
@@ -384,6 +394,175 @@ update() {
394 return 0
395 }
396
397 +update_static() {
398 + ndtmpdir="$(create_tmp_directory)"
399 + PREVDIR="$(pwd)"
400 +
401 + info "Entering ${ndtmpdir}"
402 + cd "${ndtmpdir}" || exit 1
403 +
404 + if update_available; then
405 + sysarch="$(uname -m)"
406 + download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt"
407 + download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-${sysarch}-latest.gz.run"
408 + if ! grep netdata-latest.gz.run "${ndtmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
409 + 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."
410 + fi
411 +
412 + if [ -e /opt/netdata/etc/netdata/.install-type ] ; then
413 + install_type="$(cat /opt/netdata/etc/netdata/.install-type)"
414 + else
415 + install_type="INSTALL_TYPE='legacy-static'"
416 + fi
417 +
418 + # Do not pass any options other than the accept, for now
419 + # shellcheck disable=SC2086
420 + if sh "${ndtmpdir}/netdata-${sysarch}-latest.gz.run" --accept -- ${REINSTALL_OPTIONS}; then
421 + rm -r "${ndtmpdir}"
422 + else
423 + info "NOTE: did not remove: ${ndtmpdir}"
424 + fi
425 +
426 + echo "${install_type}" > /opt/netdata/etc/netdata/.install-type
427 + fi
428 +
429 + if [ -e "${PREVDIR}" ]; then
430 + info "Switching back to ${PREVDIR}"
431 + cd "${PREVDIR}"
432 + fi
433 + [ -n "${logfile}" ] && rm "${logfile}" && logfile=
434 + exit 0
435 +}
436 +
437 +update_binpkg() {
438 + os_release_file=
439 + if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
440 + os_release_file="/etc/os-release"
441 + elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
442 + os_release_file="/usr/lib/os-release"
443 + else
444 + fatal "Cannot find an os-release file ..." F0401
445 + fi
446 +
447 + # shellcheck disable=SC1090
448 + . "${os_release_file}"
449 +
450 + DISTRO="${ID}"
451 +
452 + supported_compat_names="debian ubuntu centos fedora opensuse"
453 +
454 + if str_in_list "${DISTRO}" "${supported_compat_names}"; then
455 + DISTRO_COMPAT_NAME="${DISTRO}"
456 + else
457 + case "${DISTRO}" in
458 + opensuse-leap)
459 + DISTRO_COMPAT_NAME="opensuse"
460 + ;;
461 + rhel)
462 + DISTRO_COMPAT_NAME="centos"
463 + ;;
464 + *)
465 + DISTRO_COMPAT_NAME="unknown"
466 + ;;
467 + esac
468 + fi
469 +
470 + if [ "${INTERACTIVE}" = "0" ]; then
471 + interactive_opts="-y"
472 + env="DEBIAN_FRONTEND=noninteractive"
473 + else
474 + interactive_opts=""
475 + env=""
476 + fi
477 +
478 + case "${DISTRO_COMPAT_NAME}" in
479 + debian)
480 + pm_cmd="apt-get"
481 + repo_subcmd="update"
482 + upgrade_cmd="upgrade"
483 + pkg_install_opts="${interactive_opts}"
484 + repo_update_opts="${interactive_opts}"
485 + pkg_installed_check="dpkg -l"
486 + INSTALL_TYPE="binpkg-deb"
487 + ;;
488 + ubuntu)
489 + pm_cmd="apt-get"
490 + repo_subcmd="update"
491 + upgrade_cmd="upgrade"
492 + pkg_install_opts="${interactive_opts}"
493 + repo_update_opts="${interactive_opts}"
494 + pkg_installed_check="dpkg -l"
495 + INSTALL_TYPE="binpkg-deb"
496 + ;;
497 + centos)
498 + if command -v dnf > /dev/null; then
499 + pm_cmd="dnf"
500 + repo_subcmd="makecache"
501 + else
502 + pm_cmd="yum"
503 + fi
504 + upgrade_cmd="upgrade"
505 + pkg_install_opts="${interactive_opts}"
506 + repo_update_opts="${interactive_opts}"
507 + pkg_installed_check="rpm -q"
508 + INSTALL_TYPE="binpkg-rpm"
509 + ;;
510 + fedora)
511 + if command -v dnf > /dev/null; then
512 + pm_cmd="dnf"
513 + repo_subcmd="makecache"
514 + else
515 + pm_cmd="yum"
516 + fi
517 + upgrade_cmd="upgrade"
518 + pkg_install_opts="${interactive_opts}"
519 + repo_update_opts="${interactive_opts}"
520 + pkg_installed_check="rpm -q"
521 + INSTALL_TYPE="binpkg-rpm"
522 + ;;
523 + opensuse)
524 + pm_cmd="zypper"
525 + repo_subcmd="--gpg-auto-import-keys refresh"
526 + upgrade_cmd="upgrade"
527 + pkg_install_opts="${interactive_opts} --allow-unsigned-rpm"
528 + repo_update_opts=""
529 + pkg_installed_check="rpm -q"
530 + INSTALL_TYPE="binpkg-rpm"
531 + ;;
532 + *)
533 + warning "We do not provide native packages for ${DISTRO}."
534 + return 2
535 + ;;
536 + esac
537 +
538 + if [ -n "${repo_subcmd}" ]; then
539 + # shellcheck disable=SC2086
540 + env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts} || fatal "Failed to update repository metadata."
541 + fi
542 +
543 + for repopkg in netdata-repo netdata-repo-edge; do
544 + if ${pkg_installed_check} ${repopkg} > /dev/null 2>&1; then
545 + # shellcheck disable=SC2086
546 + env ${env} ${pm_cmd} ${upgrade_cmd} ${pkg_install_opts} ${repopkg} || fatal "Failed to update Netdata repository config."
547 + # shellcheck disable=SC2086
548 + env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts} || fatal "Failed to update repository metadata."
549 + fi
550 + done
551 +
552 + # shellcheck disable=SC2086
553 + env ${env} ${pm_cmd} ${upgrade_cmd} ${pkg_install_opts} netdata || fatal "Failed to update Netdata package."
554 +}
555 +
556 +# Simple function to encapsulate original updater behavior.
557 +update_legacy() {
558 + set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
559 + if [ "${IS_NETDATA_STATIC_BINARY}" = "yes" ]; then
560 + update_static && exit 0
561 + else
562 + update_build && exit 0
563 + fi
564 +}
565 +
566 logfile=
567 ndtmpdir=
568
@@ -419,6 +598,11 @@ fi
598 # shellcheck source=/dev/null
599 . "${ENVIRONMENT_FILE}" || exit 1
600
601 +if [ -f "$(dirname "${ENVIRONMENT_FILE}")/.install-type" ]; then
602 + # shellcheck source=/dev/null
603 + . "$(dirname "${ENVIRONMENT_FILE}")/.install-type" || exit 1
604 +fi
605 +
606 # We dont expect to find lib dir variable on older installations, so load this path if none found
607 export NETDATA_LIB_DIR="${NETDATA_LIB_DIR:-${NETDATA_PREFIX}/var/lib/netdata}"
608
@@ -446,46 +630,35 @@ fi
630
631 self_update
632
449 -set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
450 -
451 -if [ "${IS_NETDATA_STATIC_BINARY}" = "yes" ]; then
452 - ndtmpdir="$(create_tmp_directory)"
453 - PREVDIR="$(pwd)"
454 -
455 - info "Entering ${ndtmpdir}"
456 - cd "${ndtmpdir}" || exit 1
457 -
458 - if update_available; then
459 - download "${NETDATA_TARBALL_CHECKSUM_URL}" "${ndtmpdir}/sha256sum.txt"
460 - download "${NETDATA_TARBALL_URL}" "${ndtmpdir}/netdata-latest.gz.run"
461 - if ! grep netdata-latest.gz.run "${ndtmpdir}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
462 - 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."
463 - fi
464 -
465 - if [ -e /opt/netdata/etc/netdata/.install-type ] ; then
466 - install_type="$(cat /opt/netdata/etc/netdata/.install-type)"
467 - else
468 - install_type="INSTALL_TYPE='legacy-static'"
469 - fi
470 -
471 - # Do not pass any options other than the accept, for now
472 - # shellcheck disable=SC2086
473 - if sh "${ndtmpdir}/netdata-latest.gz.run" --accept -- ${REINSTALL_OPTIONS} >&3 2>&3; then
474 - rm -rf "${ndtmpdir}" >&3 2>&3
475 - else
476 - info "NOTE: did not remove: ${ndtmpdir}"
477 - fi
478 -
479 - echo "${install_type}" > /opt/netdata/etc/netdata/.install-type
480 - fi
481 -
482 - if [ -e "${PREVDIR}" ]; then
483 - info "Switching back to ${PREVDIR}"
484 - cd "${PREVDIR}"
485 - fi
486 - [ -n "${logfile}" ] && rm "${logfile}" && logfile=
487 - exit 0
488 -else
489 - # the installer updates this script - so we run and exit in a single line
490 - update && exit 0
491 -fi
633 +# shellcheck disable=SC2153
634 +case "${INSTALL_TYPE}" in
635 + *-build)
636 + set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
637 + update_build && exit 0
638 + ;;
639 + *-static*)
640 + set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
641 + update_static && exit 0
642 + ;;
643 + *binpkg*)
644 + update_binpkg && exit 0
645 + ;;
646 + "") # Fallback case for no `.install-type` file. This just works like the old install type detection.
647 + update_legacy
648 + ;;
649 + custom)
650 + # At this point, we _should_ have a valid `.environment` file, but it0s best to just check.
651 + # If we do, then behave like the legacy updater.
652 + if [ -n "${RELEASE_CHANNEL}" ] && [ -n "${NETDATA_PREFIX}" ] && [ -n "${REINSTALL_OPTIONS}" ]; then
653 + update_legacy
654 + else
655 + fatal "This script does not support updating custom installations."
656 + fi
657 + ;;
658 + oci)
659 + fatal "This script does not support updating Netdata inside our official Docker containers, please instead update the container itself."
660 + ;;
661 + *)
662 + fatal "Unrecognized installation type (${INSTALL_TYPE}), unable to update."
663 + ;;
664 +esac