Make netdata-updater.sh POSIX compliant. (#11755)
Austin S. Hemmelgarn committed
Dec 9, 2021 at 07:36 UTC
9e08ce63f746a1bbf70c5f74360085b21fda2ea6
1 file changed
+27
-25
packaging/installer/netdata-updater.sh
+27
-25
@@ -1,4 +1,4 @@
1
-#!/usr/bin/env bash
1
+#!/bin/sh
2
3
# Netdata updater utility
4
#
@@ -48,7 +48,7 @@ error() {
48
49
: "${ENVIRONMENT_FILE:=THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT}"
50
51
-if [ "${ENVIRONMENT_FILE}" == "THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT" ]; then
51
+if [ "${ENVIRONMENT_FILE}" = "THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT" ]; then
52
if [ -r "${script_dir}/../../../etc/netdata/.environment" ]; then
53
ENVIRONMENT_FILE="${script_dir}/../../../etc/netdata/.environment"
54
elif [ -r "/etc/netdata/.environment" ]; then
@@ -96,7 +96,6 @@ cleanup() {
96
}
97
98
_cannot_use_tmpdir() {
99
- local testfile ret
99
testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
100
ret=0
101
@@ -173,9 +172,8 @@ download() {
172
}
173
174
get_netdata_latest_tag() {
176
- local dest="${1}"
177
- local url="https://github.com/netdata/netdata/releases/latest"
178
- local tag
175
+ dest="${1}"
176
+ url="https://github.com/netdata/netdata/releases/latest"
177
178
if command -v curl >/dev/null 2>&1; then
179
tag=$(curl "${url}" -s -L -I -o /dev/null -w '%{url_effective}' | grep -m 1 -o '[^/]*$')
@@ -185,7 +183,7 @@ get_netdata_latest_tag() {
183
fatal "I need curl or wget to proceed, but neither of them are available on this system."
184
fi
185
188
- if [[ ! $tag =~ ^v[0-9]+\..+ ]]; then
186
+ if echo "${tag}" | grep -vEq "^v[0-9]+\..+"; then
187
fatal "Cannot download latest stable tag from ${url}"
188
fi
189
@@ -241,22 +239,24 @@ parse_version() {
239
r="$(echo "${r}" | sed -e 's/^v\(.*\)/\1/')"
240
fi
241
244
- read -r -a p <<< "$(echo "${r}" | tr '-' ' ')"
242
+ tmpfile="$(mktemp)"
243
+ echo "${r}" | tr '-' ' ' > "${tmpfile}"
244
+ read -r v b _ < "${tmpfile}"
245
246
- v="${p[0]}"
247
- b="${p[1]}"
248
- _="${p[2]}" # ignore the SHA
249
-
250
- if [[ ! "${b}" =~ ^[0-9]+$ ]]; then
246
+ if echo "${b}" | grep -vEq "^[0-9]+$"; then
247
b="0"
248
fi
249
254
- read -r -a pp <<< "$(echo "${v}" | tr '.' ' ')"
255
- printf "%03d%03d%03d%05d" "${pp[0]}" "${pp[1]}" "${pp[2]}" "${b}"
250
+ echo "${v}" | tr '.' ' ' > "${tmpfile}"
251
+ read -r maj min patch _ < "${tmpfile}"
252
+
253
+ rm -f "${tmpfile}"
254
+
255
+ printf "%03d%03d%03d%05d" "${maj}" "${min}" "${patch}" "${b}"
256
}
257
258
get_latest_version() {
259
- if [ "${RELEASE_CHANNEL}" == "stable" ]; then
259
+ if [ "${RELEASE_CHANNEL}" = "stable" ]; then
260
get_netdata_latest_tag /dev/stdout
261
else
262
download "$NETDATA_NIGHTLIES_BASEURL/latest-version.txt" /dev/stdout
@@ -264,14 +264,13 @@ get_latest_version() {
264
}
265
266
set_tarball_urls() {
267
- local extension="tar.gz"
267
+ extension="tar.gz"
268
269
- if [ "$2" == "yes" ]; then
269
+ if [ "$2" = "yes" ]; then
270
extension="gz.run"
271
fi
272
273
if [ "$1" = "stable" ]; then
274
- local latest
274
latest="$(get_netdata_latest_tag /dev/stdout)"
275
export NETDATA_TARBALL_URL="https://github.com/netdata/netdata/releases/download/$latest/netdata-$latest.${extension}"
276
export NETDATA_TARBALL_CHECKSUM_URL="https://github.com/netdata/netdata/releases/download/$latest/sha256sums.txt"
@@ -328,8 +327,7 @@ update() {
327
possible_pids=$(pidof netdata)
328
do_not_start=
329
if [ -n "${possible_pids}" ]; then
331
- read -r -a pids_to_kill <<< "${possible_pids}"
332
- kill -USR1 "${pids_to_kill[@]}"
330
+ kill -USR1 "${possible_pids}"
331
else
332
# netdata is currently not running, so do not start it after updating
333
do_not_start="--dont-start-it"
@@ -396,17 +394,21 @@ done
394
# But only we're not a controlling terminal (tty)
395
# Randomly sleep between 1s and 60m
396
if [ ! -t 1 ] && [ -z "${NETDATA_NOT_RUNNING_FROM_CRON}" ]; then
399
- sleep $(((RANDOM % 3600) + 1))
397
+ rnd="$(awk '
398
+ BEGIN { srand()
399
+ printf("%d\n", 3600 * rand())
400
+ }')"
401
+ sleep $(((rnd % 3600) + 1))
402
fi
403
404
# shellcheck source=/dev/null
403
-source "${ENVIRONMENT_FILE}" || exit 1
405
+. "${ENVIRONMENT_FILE}" || exit 1
406
407
# We dont expect to find lib dir variable on older installations, so load this path if none found
408
export NETDATA_LIB_DIR="${NETDATA_LIB_DIR:-${NETDATA_PREFIX}/var/lib/netdata}"
409
410
# Source the tarball checksum, if not already available from environment (for existing installations with the old logic)
409
-[[ -z "${NETDATA_TARBALL_CHECKSUM}" ]] && [[ -f ${NETDATA_LIB_DIR}/netdata.tarball.checksum ]] && NETDATA_TARBALL_CHECKSUM="$(cat "${NETDATA_LIB_DIR}/netdata.tarball.checksum")"
411
+[ -z "${NETDATA_TARBALL_CHECKSUM}" ] && [ -f "${NETDATA_LIB_DIR}/netdata.tarball.checksum" ] && NETDATA_TARBALL_CHECKSUM="$(cat "${NETDATA_LIB_DIR}/netdata.tarball.checksum")"
412
413
# Grab the nightlies baseurl (defaulting to our Google Storage bucket)
414
export NETDATA_NIGHTLIES_BASEURL="${NETDATA_NIGHTLIES_BASEURL:-https://storage.googleapis.com/netdata-nightlies}"
@@ -431,7 +433,7 @@ self_update
433
434
set_tarball_urls "${RELEASE_CHANNEL}" "${IS_NETDATA_STATIC_BINARY}"
435
434
-if [ "${IS_NETDATA_STATIC_BINARY}" == "yes" ]; then
436
+if [ "${IS_NETDATA_STATIC_BINARY}" = "yes" ]; then
437
ndtmpdir="$(create_tmp_directory)"
438
PREVDIR="$(pwd)"
439