@cryptotaxi247 / netdata-1 / commits / 0dbd4fb0c

GCS-based updater (#5174)

* updater using GCS * new lifecycle test * modify lifecycle test * Update netdata-updater.sh * simplify conditions in installer * less quotes

Paweł Krupa committed Jan 16, 2019 at 12:48 UTC 0dbd4fb0cba640347d8fd88fec6071a5510828e5
3 files changed +63 -93
netdata-installer.sh
+17 -22
@@ -917,52 +917,47 @@ To start netdata run:
917 END
918 echo >&2 "Uninstall script is located at: ${TPUT_RED}${TPUT_BOLD}./netdata-uninstaller.sh${TPUT_RESET}"
919
920 -if [ -d .git ]; then
920 +
921 +if [ "${AUTOUPDATE}" = "1" ]; then
922 if [ "${UID}" -ne "0" ]; then
922 - [ "${AUTOUPDATE}" = "1" ] && echo >&2 "You need to run the installer as root for auto-updating via cron."
923 + echo >&2 "You need to run the installer as root for auto-updating via cron."
924 else
925 crondir=
926 [ -d "/etc/periodic/daily" ] && crondir="/etc/periodic/daily"
927 [ -d "/etc/cron.daily" ] && crondir="/etc/cron.daily"
928
929 if [ -z "${crondir}" ]; then
929 - [ "${AUTOUPDATE}" = "1" ] && echo >&2 "Cannot figure out the cron directory to install netdata-updater"
930 + echo >&2 "Cannot figure out the cron directory to install netdata-updater"
931 else
932 if [ -f "${crondir}/netdata-updater.sh" ]; then
933 progress "Removing incorrect netdata-updater filename in cron"
934 rm -f "${crondir}/netdata-updater.sh"
935 fi
935 -
936 - if [ "${AUTOUPDATE}" = "1" ]; then
937 - progress "Installing new netdata-updater in cron"
938 - sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${REINSTALL_PWD}|" ./packaging/installer/netdata-updater.sh > ${crondir}/netdata-updater
939 - chmod 0755 ${crondir}/netdata-updater
940 - echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${crondir}/netdata-updater${TPUT_RESET}"
941 - echo >&2
942 - echo >&2 "By default ${TPUT_DIM}${TPUT_BOLD}netdata-updater${TPUT_RESET}${TPUT_DIM} works from cron. It will trigger an email from cron"
943 - echo >&2 "only if it fails (it should not print anything when it can update netdata).${TPUT_RESET}"
944 - else
945 - echo >&2 "${TPUT_DIM}Run this to automatically check and install netdata updates once per day:${TPUT_RESET}"
946 - echo >&2
947 - echo >&2 "${TPUT_YELLOW}${TPUT_BOLD}sudo ${crondir}/netdata-updater${TPUT_RESET}"
948 - fi
936 + progress "Installing new netdata-updater in cron"
937 + sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" ./packaging/installer/netdata-updater.sh > ${crondir}/netdata-updater
938 + chmod 0755 ${crondir}/netdata-updater
939 + echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${crondir}/netdata-updater${TPUT_RESET}"
940 + echo >&2
941 + echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater${TPUT_RESET}${TPUT_DIM} works from cron. It will trigger an email from cron"
942 + echo >&2 "only if it fails (it should not print anything when it can update netdata).${TPUT_RESET}"
943 fi
944 fi
951 -else
952 - [ -f "netdata-updater.sh" ] && rm "netdata-updater.sh"
953 - [ "${AUTOUPDATE}" = "1" ] && echo >&2 "Your installation method does not support daily auto-updating via cron."
945 fi
946
947 # Save environment variables
957 -cat <<EOF > packaging/installer/.environment.sh
948 +cat <<EOF > ${NETDATA_USER_CONFIG_DIR}/.environment
949 +# Created by installer
950 PATH="${PATH}"
951 CFLAGS="${CFLAGS}"
952 NETDATA_PREFIX="${NETDATA_PREFIX}"
953 NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS}"
954 NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS}"
955 INSTALL_UID="${UID}"
964 -REINSTALL_PWD="${REINSTALL_PWD}"
956 REINSTALL_COMMAND="${REINSTALL_COMMAND}"
957 +# next 3 values are meant to be populated by autoupdater (if enabled)
958 +NETDATA_TARBALL_URL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.tar.gz"
959 +NETDATA_TARBALL_CHECKSUM_URL="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
960 +NETDATA_TARBALL_CHECKSUM="new_installation"
961 EOF
962
963 # Opt-out from telemetry program
packaging/installer/netdata-updater.sh
+39 -57
@@ -1,4 +1,5 @@
1 #!/usr/bin/env bash
2 +#shellcheck disable=SC2164
3
4 # this script will uninstall netdata
5
@@ -6,33 +7,28 @@
7 # - PATH
8 # - CFLAGS
9 # - NETDATA_CONFIGURE_OPTIONS
9 -# - REINSTALL_PWD
10 # - REINSTALL_COMMAND
11 +# - NETDATA_TARBALL_URL
12 +# - NETDATA_TARBALL_CHECKSUM_URL
13 +# - NETDATA_TARBALL_CHECKSUM
14
12 -force=0
13 -[ "${1}" = "-f" ] && force=1
15
15 -# make sure we cd to the working directory
16 -REINSTALL_PWD="THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT"
17 -cd "${REINSTALL_PWD}" || exit 1
16 +# Usually stored in /etc/netdata/.environment
17 +: "${ENVIRONMENT_FILE:=THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT}"
18
19 -#shellcheck source=/dev/null
20 -source packaging/installer/.environment.sh || exit 1
19 +# shellcheck source=/dev/null
20 +source "${ENVIRONMENT_FILE}" || exit 1
21
22 -if [ "${INSTALL_UID}" != "$(id -u)" ]
23 - then
24 - echo >&2 "You are running this script as user with uid $(id -u). We recommend to run this script as root (user with uid 0)"
25 - exit 1
22 +if [ "${INSTALL_UID}" != "$(id -u)" ]; then
23 + echo >&2 "You are running this script as user with uid $(id -u). We recommend to run this script as root (user with uid 0)"
24 + exit 1
25 fi
26
28 -# make sure there is .git here
29 -[ ${force} -eq 0 -a ! -d .git ] && echo >&2 "No git structures found at: ${REINSTALL_PWD} (use -f for force re-install)" && exit 1
30 -
27 # signal netdata to start saving its database
28 # this is handy if your database is big
29 pids=$(pidof netdata)
30 do_not_start=
35 -if [ ! -z "${pids}" ]; then
31 +if [ -n "${pids}" ]; then
32 #shellcheck disable=SC2086
33 kill -USR1 ${pids}
34 else
@@ -57,10 +53,6 @@ info() {
53 echo >&3 "$(date) : INFO: " "${@}"
54 }
55
60 -emptyline() {
61 - echo >&3
62 -}
63 -
56 error() {
57 echo >&3 "$(date) : ERROR: " "${@}"
58 }
@@ -69,56 +61,46 @@ error() {
61 failed() {
62 error "FAILED TO UPDATE NETDATA : ${1}"
63
72 - if [ ! -z "${tmp}" ]; then
64 + if [ -n "${tmp}" ]; then
65 cat >&2 "${tmp}"
66 rm "${tmp}"
67 fi
68 exit 1
69 }
70
79 -get_latest_commit_id() {
80 - git rev-parse HEAD 2>&3
81 -}
82 -
71 update() {
72 [ -z "${tmp}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
73
86 - emptyline
87 -
88 - if [ -d .git ]; then
89 - info "Updating netdata source from github..."
90 -
91 - last_commit="$(get_latest_commit_id)"
92 - if [ ${force} -eq 0 ] && [ -z "${last_commit}" ]; then
93 - failed "CANNOT GET LAST COMMIT ID (use -f for force re-install)"
94 - fi
95 -
96 - popmsg="$(git stash pop 2>&1)"
97 - if [ $? -eq 0 ] ; then
98 - info "Stashing local git changes. You can use ${popmsg} to reapply your changes."
99 - fi
100 -
101 - git stash 2>&3 >&3
102 - git fetch --all 2>&3 >&3
103 - git fetch --tags 2>&3 >&3
104 - git checkout master 2>&3 >&3
105 - git reset --hard origin/master 2>&3 >&3
106 - git pull 2>&3 >&3
107 -
108 - new_commit="$(get_latest_commit_id)"
109 - if [ ${force} -eq 0 ]; then
110 - [ -z "${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID (use -f for force re-install)"
111 - [ "${new_commit}" = "${last_commit}" ] && info "Nothing to be done! (use -f to force re-install)" && exit 0
112 - fi
113 - elif [ ${force} -eq 0 ]; then
114 - failed "CANNOT FIND GIT STRUCTURES IN $(pwd) (use -f for force re-install)"
74 + dir=$(mktemp -d)
75 +
76 + cd "$dir"
77 +
78 + wget "${NETDATA_TARBALL_CHECKSUM_URL}" -O sha256sum.txt >&3 2>&3
79 + if grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3; then
80 + info "Newest version is already installed"
81 + exit 0
82 fi
83
117 - emptyline
84 + wget "${NETDATA_TARBALL_URL}" -O netdata-latest.tar.gz >&3 2>&3
85 + if ! grep netdata-latest.tar.gz sha256sum.txt | sha256sum --check - >&3 2>&3; then
86 + failed "Tarball checksum validation failed. Stopping netdata upgrade and leaving tarball in ${dir}"
87 + fi
88 + NEW_CHECKSUM="$(sha256sum netdata-latest.tar.gz 2>/dev/null| cut -d' ' -f1)"
89 + tar -xf netdata-latest.tar.gz >&3 2>&3
90 + rm netdata-latest.tar.gz >&3 2>&3
91 + cd netdata-*
92 +
93 info "Re-installing netdata..."
94 ${REINSTALL_COMMAND} --dont-wait ${do_not_start} >&3 2>&3 || failed "FAILED TO COMPILE/INSTALL NETDATA"
120 -
121 - [ ! -z "${tmp}" ] && rm "${tmp}" && tmp=
95 + sed -i '/NETDATA_TARBALL/d' "${ENVIRONMENT_FILE}"
96 + cat <<EOF >>"${ENVIRONMENT_FILE}"
97 +NETDATA_TARBALL_URL="$NETDATA_TARBALL_URL"
98 +NETDATA_TARBALL_CHECKSUM_URL="$NETDATA_TARBALL_CHECKSUM_URL"
99 +NETDATA_TARBALL_CHECKSUM="$NEW_CHECKSUM"
100 +EOF
101 +
102 + rm -rf "${dir}" >&3 2>&3
103 + [ -n "${tmp}" ] && rm "${tmp}" && tmp=
104 return 0
105 }
106
tests/lifecycle.sh
+7 -14
@@ -11,24 +11,17 @@ fi
11 WORKSPACE=$(mktemp -d)
12 cp -r ./ "${WORKSPACE}/"
13 cd "${WORKSPACE}"
14 -git config user.email "test@example.com"
15 -git config user.name "test"
14
15 echo "========= INSTALL ========="
16 ./netdata-installer.sh --dont-wait --dont-start-it --auto-update --install /tmp &>/dev/null
19 -# Copy uninstaller as upgrader will overwrite it with a version from master branch
17 cp netdata-uninstaller.sh /tmp/netdata-uninstaller.sh
18 +ls /tmp
19
22 -echo "========= ADD GARBAGE ========="
23 -touch garbagefile
24 -git add garbagefile
25 -git commit -m 'test commit'
26 -touch new_file
27 -git status
28 -
20 +rm -rf "${WORKSPACE}"
21 echo "========= UPDATE ========="
30 -/etc/cron.daily/netdata-updater
22 +ENVIRONMENT_FILE=/tmp/netdata/etc/netdata/.environment /etc/cron.daily/netdata-updater
23
32 -echo "========= UNINSTALL ========="
33 -mv /tmp/netdata-uninstaller.sh ./netdata-uninstaller.sh
34 -./netdata-uninstaller.sh --yes --force
24 +#TODO(paulfantom): Enable with #5031
25 +#echo "========= UNINSTALL ========="
26 +#ENVIRONMENT_FILE=/tmp/netdata/etc/netdata/.environment /tmp/netdata-uninstaller.sh --yes --force
27 +#[ -f /tmp/netdata/usr/sbin/netdata ] && exit 1