Fixes error/warnings found by shellcheck for ./packaging/installer/netdata-updater.sh (#7938)
* Re-formatted ./packaging/installer/netdata-updater.sh with shfmt -w -i 2 -ci -sr * Fixed errors/warnings founds with shellcheck for ./packaging/installer/netdata-updater.sh * Fixed logic for removing TMPDIR * Fixed logic for removing TMPDIR proper * Fixed another shellcheck warning that ReviewDog found * Fixed exit 1 on bad cd * Fixed expansion of pids in kill
James Mills committed
Feb 5, 2020 at 06:08 UTC
2ec5d0682cb5865e9be774817e6ccfe52d5ac98e
1 file changed
+15
-19
packaging/installer/netdata-updater.sh
+15
-19
@@ -1,6 +1,5 @@
1
#!/usr/bin/env bash
2
-#shellcheck disable=SC2164
3
-#
2
+
3
# Netdata updater utility
4
#
5
# Variables needed by script:
@@ -32,9 +31,9 @@ safe_sha256sum() {
31
# Within the contexct of the installer, we only use -c option that is common between the two commands
32
# We will have to reconsider if we start non-common options
33
if command -v sha256sum > /dev/null 2>&1; then
35
- sha256sum $@
34
+ sha256sum "$@"
35
elif command -v shasum > /dev/null 2>&1; then
37
- shasum -a 256 $@
36
+ shasum -a 256 "$@"
37
else
38
fatal "I could not find a suitable checksum binary to use"
39
fi
@@ -77,7 +76,7 @@ download() {
76
set_tarball_urls() {
77
local extension="tar.gz"
78
80
- if [ ! -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
79
+ if [ -n "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
80
info "Not fetching remote tarballs, local override was given"
81
return
82
fi
@@ -104,7 +103,7 @@ update() {
103
104
RUN_INSTALLER=0
105
tmpdir=$(create_tmp_directory)
107
- cd "$tmpdir"
106
+ cd "$tmpdir" || exit 1
107
108
if [ -z "${NETDATA_LOCAL_TARBAL_OVERRIDE}" ]; then
109
download "${NETDATA_TARBALL_CHECKSUM_URL}" "${tmpdir}/sha256sum.txt" >&3 2>&3
@@ -118,24 +117,24 @@ update() {
117
NEW_CHECKSUM="$(safe_sha256sum netdata-latest.tar.gz 2> /dev/null | cut -d' ' -f1)"
118
tar -xf netdata-latest.tar.gz >&3 2>&3
119
rm netdata-latest.tar.gz >&3 2>&3
121
- cd netdata-*
120
+ cd netdata-* || exit 1
121
RUN_INSTALLER=1
122
fi
123
else
124
info "!!Local tarball override detected!! - Entering directory ${NETDATA_LOCAL_TARBAL_OVERRIDE} for installation, not downloading anything"
125
RUN_INSTALLER=1
127
- cd ${NETDATA_LOCAL_TARBAL_OVERRIDE}
126
+ cd "${NETDATA_LOCAL_TARBAL_OVERRIDE}" || exit 1
127
fi
128
129
# We got the sources, run the update now
130
if [ ${RUN_INSTALLER} -eq 1 ]; then
131
# signal netdata to start saving its database
132
# this is handy if your database is big
134
- pids=$(pidof netdata)
133
+ possible_pids=$(pidof netdata)
134
do_not_start=
136
- if [ -n "${pids}" ]; then
137
- #shellcheck disable=SC2086
138
- kill -USR1 ${pids}
135
+ if [ -n "${possible_pids}" ]; then
136
+ read -r -a pids_to_kill <<< "${possible_pids}"
137
+ kill -USR1 "${pids_to_kill[@]}"
138
else
139
# netdata is currently not running, so do not start it after updating
140
do_not_start="--dont-start-it"
@@ -181,7 +180,7 @@ if [ -t 2 ]; then
180
else
181
# we are headless
182
# create a temporary file for the log
184
- logfile=$(mktemp ${logfile}/netdata-updater.log.XXXXXX)
183
+ logfile="$(mktemp "${logfile}"/netdata-updater.log.XXXXXX)"
184
# open fd 3 and send it to logfile
185
exec 3> "${logfile}"
186
fi
@@ -193,7 +192,7 @@ if [ "${IS_NETDATA_STATIC_BINARY}" == "yes" ]; then
192
PREVDIR="$(pwd)"
193
194
echo >&2 "Entering ${TMPDIR}"
196
- cd "${TMPDIR}"
195
+ cd "${TMPDIR}" || exit 1
196
197
download "${NETDATA_TARBALL_CHECKSUM_URL}" "${TMPDIR}/sha256sum.txt"
198
download "${NETDATA_TARBALL_URL}" "${TMPDIR}/netdata-latest.gz.run"
@@ -202,16 +201,13 @@ if [ "${IS_NETDATA_STATIC_BINARY}" == "yes" ]; then
201
fi
202
203
# 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
204
+ if sh "${TMPDIR}/netdata-latest.gz.run" --accept "${REINSTALL_OPTIONS}"; then
205
rm -r "${TMPDIR}"
206
else
207
echo >&2 "NOTE: did not remove: ${TMPDIR}"
208
fi
209
echo >&2 "Switching back to ${PREVDIR}"
214
- cd "${PREVDIR}"
210
+ cd "${PREVDIR}" || exit 1
211
else
212
# the installer updates this script - so we run and exit in a single line
213
update && exit 0