Refactored static kickstart script (#5397)
* refactor static kickstart to be in-line with kickstart.sh * run automatic linter
Paweł Krupa committed
Feb 18, 2019 at 15:41 UTC
45bf6721ae81c6845fd31c1abbc88cb0bec04231
1 file changed
+106
-197
packaging/installer/kickstart-static64.sh
+106
-197
@@ -1,238 +1,149 @@
1
#!/usr/bin/env sh
2
# SPDX-License-Identifier: GPL-3.0-or-later
3
-# shellcheck disable=SC1117,SC2016,SC2034,SC2039,SC2059,SC2086,SC2119,SC2120,SC2129,SC2162,SC2166,SC2181
3
+# shellcheck disable=SC1117,SC2039,SC2059,SC2086
4
5
-umask 022
6
-
7
-# make sure UID is set
8
-# shellcheck disable=SC2155
9
-[ -z "${UID}" ] && export UID="$(id -u)"
5
+# External files
6
+NIGHTLY_PACKAGE_TARBALL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.gz.run"
7
+NIGHTLY_PACKAGE_CHECKSUM="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
8
9
# ---------------------------------------------------------------------------------------------------------------------
10
# library functions copied from packaging/installer/functions.sh
11
14
-which_cmd() {
15
- # shellcheck disable=SC2230
16
- which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
17
-}
18
-
19
-check_cmd() {
20
- which_cmd "${1}" >/dev/null 2>&1 && return 0
21
- return 1
22
-}
23
-
12
setup_terminal() {
25
- TPUT_RESET=""
26
- TPUT_BLACK=""
27
- TPUT_RED=""
28
- TPUT_GREEN=""
29
- TPUT_YELLOW=""
30
- TPUT_BLUE=""
31
- TPUT_PURPLE=""
32
- TPUT_CYAN=""
33
- TPUT_WHITE=""
34
- TPUT_BGBLACK=""
35
- TPUT_BGRED=""
36
- TPUT_BGGREEN=""
37
- TPUT_BGYELLOW=""
38
- TPUT_BGBLUE=""
39
- TPUT_BGPURPLE=""
40
- TPUT_BGCYAN=""
41
- TPUT_BGWHITE=""
42
- TPUT_BOLD=""
43
- TPUT_DIM=""
44
- TPUT_UNDERLINED=""
45
- TPUT_BLINK=""
46
- TPUT_INVERTED=""
47
- TPUT_STANDOUT=""
48
- TPUT_BELL=""
49
- TPUT_CLEAR=""
50
-
51
- # Is stderr on the terminal? If not, then fail
52
- test -t 2 || return 1
53
-
54
- if check_cmd tput
55
- then
56
- if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ]
57
- then
58
- # Enable colors
59
- TPUT_RESET="$(tput sgr 0)"
60
- TPUT_BLACK="$(tput setaf 0)"
61
- TPUT_RED="$(tput setaf 1)"
62
- TPUT_GREEN="$(tput setaf 2)"
63
- TPUT_YELLOW="$(tput setaf 3)"
64
- TPUT_BLUE="$(tput setaf 4)"
65
- TPUT_PURPLE="$(tput setaf 5)"
66
- TPUT_CYAN="$(tput setaf 6)"
67
- TPUT_WHITE="$(tput setaf 7)"
68
- TPUT_BGBLACK="$(tput setab 0)"
69
- TPUT_BGRED="$(tput setab 1)"
70
- TPUT_BGGREEN="$(tput setab 2)"
71
- TPUT_BGYELLOW="$(tput setab 3)"
72
- TPUT_BGBLUE="$(tput setab 4)"
73
- TPUT_BGPURPLE="$(tput setab 5)"
74
- TPUT_BGCYAN="$(tput setab 6)"
75
- TPUT_BGWHITE="$(tput setab 7)"
76
- TPUT_BOLD="$(tput bold)"
77
- TPUT_DIM="$(tput dim)"
78
- TPUT_UNDERLINED="$(tput smul)"
79
- TPUT_BLINK="$(tput blink)"
80
- TPUT_INVERTED="$(tput rev)"
81
- TPUT_STANDOUT="$(tput smso)"
82
- TPUT_BELL="$(tput bel)"
83
- TPUT_CLEAR="$(tput clear)"
84
- fi
85
- fi
86
-
87
- return 0
13
+ TPUT_RESET=""
14
+ TPUT_YELLOW=""
15
+ TPUT_WHITE=""
16
+ TPUT_BGRED=""
17
+ TPUT_BGGREEN=""
18
+ TPUT_BOLD=""
19
+ TPUT_DIM=""
20
+
21
+ # Is stderr on the terminal? If not, then fail
22
+ test -t 2 || return 1
23
+
24
+ if command -v tput >/dev/null 2>&1; then
25
+ if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
26
+ # Enable colors
27
+ TPUT_RESET="$(tput sgr 0)"
28
+ TPUT_YELLOW="$(tput setaf 3)"
29
+ TPUT_WHITE="$(tput setaf 7)"
30
+ TPUT_BGRED="$(tput setab 1)"
31
+ TPUT_BGGREEN="$(tput setab 2)"
32
+ TPUT_BOLD="$(tput bold)"
33
+ TPUT_DIM="$(tput dim)"
34
+ fi
35
+ fi
36
+
37
+ return 0
38
}
89
-setup_terminal || echo >/dev/null
39
40
progress() {
92
- echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
93
-}
94
-
95
-run_ok() {
96
- printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
97
-}
98
-
99
-run_failed() {
100
- printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
41
+ echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
42
}
43
103
-ESCAPED_PRINT_METHOD=
104
-printf "%q " test >/dev/null 2>&1
105
-[ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq"
44
escaped_print() {
107
- if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]
108
- then
109
- printf "%q " "${@}"
110
- else
111
- printf "%s" "${*}"
112
- fi
113
- return 0
45
+ if printf "%q " test >/dev/null 2>&1; then
46
+ printf "%q " "${@}"
47
+ else
48
+ printf "%s" "${*}"
49
+ fi
50
+ return 0
51
}
52
116
-run_logfile="/dev/null"
53
run() {
118
- local user="${USER--}" dir="${PWD}" info info_console
119
-
120
- if [ "${UID}" = "0" ]
121
- then
122
- info="[root ${dir}]# "
123
- info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
124
- else
125
- info="[${user} ${dir}]$ "
126
- info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
127
- fi
128
-
129
- printf >> "${run_logfile}" "${info}"
130
- escaped_print >> "${run_logfile}" "${@}"
131
- printf >> "${run_logfile}" " ... "
132
-
133
- printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
134
- escaped_print >&2 "${@}"
135
- printf >&2 "${TPUT_RESET}\n"
136
-
137
- "${@}"
138
-
139
- local ret=$?
140
- if [ ${ret} -ne 0 ]
141
- then
142
- run_failed
143
- printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
144
- else
145
- run_ok
146
- printf >> "${run_logfile}" "OK\n"
147
- fi
148
-
149
- return ${ret}
150
-}
54
+ local dir="${PWD}" info_console
55
56
+ if [ "${UID}" = "0" ]; then
57
+ info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
58
+ else
59
+ info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
60
+ fi
61
153
-# ---------------------------------------------------------------------------------------------------------------------
62
+ escaped_print "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" "${@}" "${TPUT_RESET}\n" >&2
63
155
-fatal() {
156
- printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
157
- exit 1
64
+ "${@}"
65
+
66
+ local ret=$?
67
+ if [ ${ret} -ne 0 ]; then
68
+ printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
69
+ else
70
+ printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
71
+ fi
72
+
73
+ return ${ret}
74
}
75
76
# ---------------------------------------------------------------------------------------------------------------------
77
162
-if [ "$(uname -m)" != "x86_64" ]
163
- then
164
- fatal "Static binary versions of netdata are available only for 64bit Intel/AMD CPUs (x86_64), but yours is: $(uname -m)."
165
-fi
78
+fatal() {
79
+ printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
80
+ exit 1
81
+}
82
167
-if [ "$(uname -s)" != "Linux" ]
168
- then
169
- fatal "Static binary versions of netdata are available only for Linux, but this system is $(uname -s)"
170
-fi
83
+download() {
84
+ url="${1}"
85
+ dest="${2}"
86
+ if command -v wget >/dev/null 2>&1; then
87
+ run wget -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
88
+ elif command -v curl >/dev/null 2>&1; then
89
+ run curl "${url}" >"${dest}" || fatal "Cannot download ${url}"
90
+ else
91
+ fatal "I need curl or wget to proceed, but neither is available on this system."
92
+ fi
93
+}
94
172
-curl="$(which_cmd curl)"
173
-wget="$(which_cmd wget)"
95
+umask 022
96
175
-# ---------------------------------------------------------------------------------------------------------------------
97
+sudo=""
98
+[ -z "${UID}" ] && UID="$(id -u)"
99
+[ "${UID}" -ne "0" ] && sudo="sudo"
100
177
-progress "Checking the latest version of static build..."
101
+setup_terminal || echo >/dev/null
102
179
-BASE='https://raw.githubusercontent.com/netdata/binary-packages/master'
103
+# ---------------------------------------------------------------------------------------------------------------------
104
181
-LATEST=
182
-if [ ! -z "${curl}" -a -x "${curl}" ]
183
-then
184
- LATEST="$(run ${curl} "${BASE}/netdata-latest.gz.run")"
185
-elif [ ! -z "${wget}" -a -x "${wget}" ]
186
-then
187
- LATEST="$(run ${wget} -O - "${BASE}/netdata-latest.gz.run")"
188
-else
189
- fatal "curl or wget are needed for this script to work."
105
+if [ "$(uname -m)" != "x86_64" ]; then
106
+ fatal "Static binary versions of netdata are available only for 64bit Intel/AMD CPUs (x86_64), but yours is: $(uname -m)."
107
fi
108
192
-if [ -z "${LATEST}" ]
193
- then
194
- fatal "Cannot find the latest static binary version of netdata."
109
+if [ "$(uname -s)" != "Linux" ]; then
110
+ fatal "Static binary versions of netdata are available only for Linux, but this system is $(uname -s)"
111
fi
112
113
# ---------------------------------------------------------------------------------------------------------------------
114
199
-progress "Downloading static netdata binary: ${LATEST}"
200
-
201
-ret=1
202
-if [ ! -z "${curl}" -a -x "${curl}" ]
203
-then
204
- run ${curl} "${BASE}/${LATEST}" >"/tmp/${LATEST}"
205
- ret=$?
206
-elif [ ! -z "${wget}" -a -x "${wget}" ]
207
-then
208
- run ${wget} -O "/tmp/${LATEST}" "${BASE}/${LATEST}"
209
- ret=$?
115
+# Check if tmp is mounted as noexec
116
+if grep -Eq '^[^ ]+ /tmp [^ ]+ ([^ ]*,)?noexec[, ]' /proc/mounts; then
117
+ pattern="$(pwd)/netdata-kickstart-static-XXXXXX"
118
else
211
- fatal "curl or wget are needed for this script to work."
119
+ pattern="/tmp/netdata-kickstart-static-XXXXXX"
120
fi
121
214
-if [ ${ret} -ne 0 -o ! -s "/tmp/${LATEST}" ]
215
- then
216
- fatal "Failed to download the latest static binary version of netdata."
122
+tmpdir="$(mktemp -d $pattern)"
123
+cd "${tmpdir}" || :
124
+
125
+progress "Downloading static netdata binary: ${NIGHTLY_PACKAGE_TARBALL}"
126
+
127
+download "${NIGHTLY_PACKAGE_CHECKSUM}" "${tmpdir}/sha256sum.txt"
128
+download "${NIGHTLY_PACKAGE_TARBALL}" "${tmpdir}/netdata-latest.gz.run"
129
+if ! grep netdata-latest.gz.run sha256sum.txt | sha256sum --check - >/dev/null 2>&1; then
130
+ failed "Static binary checksum validation failed. Stopping netdata installation and leaving binary in ${tmpdir}"
131
fi
132
133
# ---------------------------------------------------------------------------------------------------------------------
134
135
opts=
136
inner_opts=
223
-while [ ! -z "${1}" ]
224
-do
225
- if [ "${1}" = "--dont-wait" -o "${1}" = "--non-interactive" -o "${1}" = "--accept" ]
226
- then
227
- opts="${opts} --accept"
228
- elif [ "${1}" = "--dont-start-it" ]
229
- then
230
- inner_opts="${inner_opts} ${1}"
231
- else
232
- echo >&2 "Unknown option '${1}'"
233
- exit 1
234
- fi
235
- shift
137
+while [ ! -z "${1}" ]; do
138
+ if [ "${1}" = "--dont-wait" ] || [ "${1}" = "--non-interactive" ] || [ "${1}" = "--accept" ]; then
139
+ opts="${opts} --accept"
140
+ elif [ "${1}" = "--dont-start-it" ]; then
141
+ inner_opts="${inner_opts} ${1}"
142
+ else
143
+ echo >&2 "Unknown option '${1}'"
144
+ exit 1
145
+ fi
146
+ shift
147
done
148
[ ! -z "${inner_opts}" ] && inner_opts="-- ${inner_opts}"
149
@@ -240,13 +151,11 @@ done
151
152
progress "Installing netdata"
153
243
-sudo=
244
-[ "${UID}" != "0" ] && sudo="sudo"
245
-run ${sudo} sh "/tmp/${LATEST}" ${opts} ${inner_opts}
154
+run ${sudo} sh "${tmpdir}/netdata-latest.gz.run" ${opts} ${inner_opts}
155
247
-if [ $? -eq 0 ]
248
- then
249
- rm "/tmp/${LATEST}"
156
+#shellcheck disable=SC2181
157
+if [ $? -eq 0 ]; then
158
+ rm "${tmpdir}/netdata-latest.gz.run"
159
else
251
- echo >&2 "NOTE: did not remove: /tmp/${LATEST}"
160
+ echo >&2 "NOTE: did not remove: ${tmpdir}/netdata-latest.gz.run"
161
fi