Re-formatted ./packaging/installer/netdata-updater.sh with shfmt -w -i 2 -ci -sr (#7937)
James Mills committed
Feb 5, 2020 at 06:05 UTC
22d341461c2d6732042ef4b90f46a8c76ed2cc1f
1 file changed
+145
-147
packaging/installer/netdata-updater.sh
+145
-147
@@ -21,141 +21,140 @@
21
# Author: Pavlos Emm. Katsoulakis <paul@netdata.cloud>
22
23
info() {
24
- echo >&3 "$(date) : INFO: " "${@}"
24
+ echo >&3 "$(date) : INFO: " "${@}"
25
}
26
27
error() {
28
- echo >&3 "$(date) : ERROR: " "${@}"
28
+ echo >&3 "$(date) : ERROR: " "${@}"
29
}
30
31
safe_sha256sum() {
32
- # Within the contexct of the installer, we only use -c option that is common between the two commands
33
- # We will have to reconsider if we start non-common options
34
- if command -v sha256sum >/dev/null 2>&1; then
35
- sha256sum $@
36
- elif command -v shasum >/dev/null 2>&1; then
37
- shasum -a 256 $@
38
- else
39
- fatal "I could not find a suitable checksum binary to use"
40
- fi
32
+ # Within the contexct of the installer, we only use -c option that is common between the two commands
33
+ # We will have to reconsider if we start non-common options
34
+ if command -v sha256sum > /dev/null 2>&1; then
35
+ sha256sum $@
36
+ elif command -v shasum > /dev/null 2>&1; then
37
+ shasum -a 256 $@
38
+ else
39
+ fatal "I could not find a suitable checksum binary to use"
40
+ fi
41
}
42
43
# this is what we will do if it fails (head-less only)
44
fatal() {
45
- error "FAILED TO UPDATE NETDATA : ${1}"
45
+ error "FAILED TO UPDATE NETDATA : ${1}"
46
47
- if [ -n "${logfile}" ]; then
48
- cat >&2 "${logfile}"
49
- rm "${logfile}"
50
- fi
51
- exit 1
47
+ if [ -n "${logfile}" ]; then
48
+ cat >&2 "${logfile}"
49
+ rm "${logfile}"
50
+ fi
51
+ exit 1
52
}
53
54
create_tmp_directory() {
55
- # Check if tmp is mounted as noexec
56
- if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
57
- pattern="$(pwd)/netdata-updater-XXXXXX"
58
- else
59
- pattern="/tmp/netdata-updater-XXXXXX"
60
- fi
61
-
62
- mktemp -d "$pattern"
55
+ # Check if tmp is mounted as noexec
56
+ if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
57
+ pattern="$(pwd)/netdata-updater-XXXXXX"
58
+ else
59
+ pattern="/tmp/netdata-updater-XXXXXX"
60
+ fi
61
+
62
+ mktemp -d "$pattern"
63
}
64
65
download() {
66
- url="${1}"
67
- dest="${2}"
68
- if command -v curl >/dev/null 2>&1; then
69
- curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${dest}" || fatal "Cannot download ${url}"
70
- elif command -v wget >/dev/null 2>&1; then
71
- wget -T 15 -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
72
- else
73
- fatal "I need curl or wget to proceed, but neither is available on this system."
74
- fi
66
+ url="${1}"
67
+ dest="${2}"
68
+ if command -v curl > /dev/null 2>&1; then
69
+ curl -sSL --connect-timeout 10 --retry 3 "${url}" > "${dest}" || fatal "Cannot download ${url}"
70
+ elif command -v wget > /dev/null 2>&1; then
71
+ wget -T 15 -O - "${url}" > "${dest}" || fatal "Cannot download ${url}"
72
+ else
73
+ fatal "I need curl or wget to proceed, but neither is available on this system."
74
+ fi
75
}
76
77
set_tarball_urls() {
78
- local extension="tar.gz"
79
-
80
- if [ ! -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
81
- info "Not fetching remote tarballs, local override was given"
82
- return
83
- fi
84
-
85
- if [ "$2" == "yes" ]; then
86
- extension="gz.run"
87
- fi
88
-
89
- if [ "$1" = "stable" ]; then
90
- local latest
91
- # Simple version
92
- # latest="$(curl -sSL https://api.github.com/repos/netdata/netdata/releases/latest | grep tag_name | cut -d'"' -f4)"
93
- latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
94
- export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.${extension}"
95
- export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
96
- else
97
- export NETDATA_TARBALL_URL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.${extension}"
98
- export NETDATA_TARBALL_CHECKSUM_URL="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
99
- fi
78
+ local extension="tar.gz"
79
+
80
+ if [ ! -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
81
+ info "Not fetching remote tarballs, local override was given"
82
+ return
83
+ fi
84
+
85
+ if [ "$2" == "yes" ]; then
86
+ extension="gz.run"
87
+ fi
88
+
89
+ if [ "$1" = "stable" ]; then
90
+ local latest
91
+ # Simple version
92
+ # latest="$(curl -sSL https://api.github.com/repos/netdata/netdata/releases/latest | grep tag_name | cut -d'"' -f4)"
93
+ latest="$(download "https://api.github.com/repos/netdata/netdata/releases/latest" /dev/stdout | grep tag_name | cut -d'"' -f4)"
94
+ export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.${extension}"
95
+ export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
96
+ else
97
+ export NETDATA_TARBALL_URL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.${extension}"
98
+ export NETDATA_TARBALL_CHECKSUM_URL="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
99
+ fi
100
}
101
102
update() {
103
- [ -z "${logfile}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
104
-
105
- RUN_INSTALLER=0
106
- tmpdir=$(create_tmp_directory)
107
- cd "$tmpdir"
108
-
109
- if [ -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
110
- download "${NETDATA_TARBALL_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt" >&3 2>&3
111
- if [[ -n "${NETDATA_TARBALL_CHECKSUM}" ]] && grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3; then
112
- info "Newest version is already installed"
113
- else
114
- download "${NETDATA_TARBALL_URL}" "${tmpdir}/netdata-latest.tar.gz"
115
- if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
116
- fatal "Tarball checksum validation failed. Stopping netdata upgrade and leaving tarball in ${tmpdir}"
117
- fi
118
- NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2>/dev/null| cut -d' ' -f1)"
119
- tar -xf netdata-latest.tar.gz >&3 2>&3
120
- rm netdata-latest.tar.gz >&3 2>&3
121
- cd netdata-*
122
- RUN_INSTALLER=1
123
- fi
124
- else
125
- info "!!Local tarball override detected!! - Entering directory ${NETDATA_LOCAL_TARBAL_OVERRIDE} for installation, not downloading anything"
126
- RUN_INSTALLER=1
127
- cd ${NETDATA_LOCAL_TARBAL_OVERRIDE}
128
- fi
129
-
130
-
131
- # We got the sources, run the update now
132
- if [ ${RUN_INSTALLER} -eq 1 ]; then
133
- # signal netdata to start saving its database
134
- # this is handy if your database is big
135
- pids=$(pidof netdata)
136
- do_not_start=
137
- if [ -n "${pids}" ]; then
138
- #shellcheck disable=SC2086
139
- kill -USR1 ${pids}
140
- else
141
- # netdata is currently not running, so do not start it after updating
142
- do_not_start="--dont-start-it"
143
- fi
144
-
145
- info "Re-installing netdata..."
146
- eval "./netdata-installer.sh ${REINSTALL_OPTIONS} --dont-wait ${do_not_start}" >&3 2>&3 || fatal "FAILED TO COMPILE/INSTALL NETDATA"
147
-
148
- # We no longer store checksum info here. but leave this so that we clean up all environment files upon next update.
149
- sed -i '/NETDATA_TARBALL/d' "${ENVIRONMENT_FILE}"
150
-
151
- info "Updating tarball checksum info"
152
- echo "${NEW_CHECKSUM}" > "${NETDATA_LIB_DIR}/netdata.tarball.checksum"
153
- fi
154
-
155
- rm -rf "${tmpdir}" >&3 2>&3
156
- [ -n "${logfile}" ] && rm "${logfile}" && logfile=
157
-
158
- return 0
103
+ [ -z "${logfile}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
104
+
105
+ RUN_INSTALLER=0
106
+ tmpdir=$(create_tmp_directory)
107
+ cd "$tmpdir"
108
+
109
+ if [ -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
110
+ download "${NETDATA_TARBALL_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt" >&3 2>&3
111
+ if [[ -n "${NETDATA_TARBALL_CHECKSUM}" ]] && grep "${NETDATA_TARBALL_CHECKSUM}" sha256sum.txt >&3 2>&3; then
112
+ info "Newest version is already installed"
113
+ else
114
+ download "${NETDATA_TARBALL_URL}" "${tmpdir}/netdata-latest.tar.gz"
115
+ if ! grep netdata-latest.tar.gz sha256sum.txt | safe_sha256sum -c - >&3 2>&3; then
116
+ fatal "Tarball checksum validation failed. Stopping netdata upgrade and leaving tarball in ${tmpdir}"
117
+ fi
118
+ NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2> /dev/null | cut -d' ' -f1)"
119
+ tar -xf netdata-latest.tar.gz >&3 2>&3
120
+ rm netdata-latest.tar.gz >&3 2>&3
121
+ cd netdata-*
122
+ RUN_INSTALLER=1
123
+ fi
124
+ else
125
+ info "!!Local tarball override detected!! - Entering directory ${NETDATA_LOCAL_TARBAL_OVERRIDE} for installation, not downloading anything"
126
+ RUN_INSTALLER=1
127
+ cd ${NETDATA_LOCAL_TARBAL_OVERRIDE}
128
+ fi
129
+
130
+ # We got the sources, run the update now
131
+ if [ ${RUN_INSTALLER} -eq 1 ]; then
132
+ # signal netdata to start saving its database
133
+ # this is handy if your database is big
134
+ pids=$(pidof netdata)
135
+ do_not_start=
136
+ if [ -n "${pids}" ]; then
137
+ #shellcheck disable=SC2086
138
+ kill -USR1 ${pids}
139
+ else
140
+ # netdata is currently not running, so do not start it after updating
141
+ do_not_start="--dont-start-it"
142
+ fi
143
+
144
+ info "Re-installing netdata..."
145
+ eval "./netdata-installer.sh ${REINSTALL_OPTIONS} --dont-wait ${do_not_start}" >&3 2>&3 || fatal "FAILED TO COMPILE/INSTALL NETDATA"
146
+
147
+ # We no longer store checksum info here. but leave this so that we clean up all environment files upon next update.
148
+ sed -i '/NETDATA_TARBALL/d' "${ENVIRONMENT_FILE}"
149
+
150
+ info "Updating tarball checksum info"
151
+ echo "${NEW_CHECKSUM}" > "${NETDATA_LIB_DIR}/netdata.tarball.checksum"
152
+ fi
153
+
154
+ rm -rf "${tmpdir}" >&3 2>&3
155
+ [ -n "${logfile}" ] && rm "${logfile}" && logfile=
156
+
157
+ return 0
158
}
159
160
# Usually stored in /etc/netdata/.environment
@@ -171,50 +170,49 @@ export NETDATA_LIB_DIR="${NETDATA_LIB_DIR:-${NETDATA_PREFIX}/var/lib/netdata}"
170
[[ -z "${NETDATA_TARBALL_CHECKSUM}" ]] && [[ -f ${NETDATA_LIB_DIR}/netdata.tarball.checksum ]] && NETDATA_TARBALL_CHECKSUM="$(cat "${NETDATA_LIB_DIR}/netdata.tarball.checksum")"
171
172
if [ "${INSTALL_UID}" != "$(id -u)" ]; then
174
- fatal "You are running this script as user with uid $(id -u). We recommend to run this script as root (user with uid 0)"
173
+ fatal "You are running this script as user with uid $(id -u). We recommend to run this script as root (user with uid 0)"
174
fi
175
176
logfile=
177
if [ -t 2 ]; then
179
- # we are running on a terminal
180
- # open fd 3 and send it to stderr
181
- exec 3>&2
178
+ # we are running on a terminal
179
+ # open fd 3 and send it to stderr
180
+ exec 3>&2
181
else
183
- # we are headless
184
- # create a temporary file for the log
185
- logfile=$(mktemp ${logfile}/netdata-updater.log.XXXXXX)
186
- # open fd 3 and send it to logfile
187
- exec 3>"${logfile}"
182
+ # we are headless
183
+ # create a temporary file for the log
184
+ logfile=$(mktemp ${logfile}/netdata-updater.log.XXXXXX)
185
+ # open fd 3 and send it to logfile
186
+ exec 3> "${logfile}"
187
fi
188
189
set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
190
191
if [ "${IS_NETDATA_STATIC_BINARY}" == "yes" ]; then
193
- TMPDIR="$(create_tmp_directory)"
194
- PREVDIR="$(pwd)"
195
-
196
- echo >&2 "Entering ${TMPDIR}"
197
- cd "${TMPDIR}"
198
-
199
- download "${NETDATA_TARBALL_CHECKSUM_URL}" "${TMPDIR}/sha256sum.txt"
200
- download "${NETDATA_TARBALL_URL}" "${TMPDIR}/netdata-latest.gz.run"
201
- if ! grep netdata-latest.gz.run "${TMPDIR}/sha256sum.txt" | safe_sha256sum -c - >/dev/null 2>&1; then
202
- fatal "Static binary checksum validation failed. Stopping netdata installation and leaving binary in ${TMPDIR}"
203
- fi
204
-
205
- # Do not pass any options other than the accept, for now
206
- sh "${TMPDIR}/netdata-latest.gz.run" --accept ${REINSTALL_OPTIONS}
207
-
208
- #shellcheck disable=SC2181
209
- if [ $? -eq 0 ]; then
210
- rm -r "${TMPDIR}"
211
- else
212
- echo >&2 "NOTE: did not remove: ${TMPDIR}"
213
- fi
214
- echo >&2 "Switching back to ${PREVDIR}"
215
- cd "${PREVDIR}"
192
+ TMPDIR="$(create_tmp_directory)"
193
+ PREVDIR="$(pwd)"
194
+
195
+ echo >&2 "Entering ${TMPDIR}"
196
+ cd "${TMPDIR}"
197
+
198
+ download "${NETDATA_TARBALL_CHECKSUM_URL}" "${TMPDIR}/sha256sum.txt"
199
+ download "${NETDATA_TARBALL_URL}" "${TMPDIR}/netdata-latest.gz.run"
200
+ if ! grep netdata-latest.gz.run "${TMPDIR}/sha256sum.txt" | safe_sha256sum -c - > /dev/null 2>&1; then
201
+ fatal "Static binary checksum validation failed. Stopping netdata installation and leaving binary in ${TMPDIR}"
202
+ fi
203
+
204
+ # Do not pass any options other than the accept, for now
205
+ sh "${TMPDIR}/netdata-latest.gz.run" --accept ${REINSTALL_OPTIONS}
206
+
207
+ #shellcheck disable=SC2181
208
+ if [ $? -eq 0 ]; then
209
+ rm -r "${TMPDIR}"
210
+ else
211
+ echo >&2 "NOTE: did not remove: ${TMPDIR}"
212
+ fi
213
+ echo >&2 "Switching back to ${PREVDIR}"
214
+ cd "${PREVDIR}"
215
else
217
- # the installer updates this script - so we run and exit in a single line
218
- update && exit 0
216
+ # the installer updates this script - so we run and exit in a single line
217
+ update && exit 0
218
fi
220
-