@cryptotaxi247 / netdata-1 / commits / b81bba75f

Use tarballs from GCS in kickstart.sh (#5185)

* always use built-in "command" * autoformat code * do not mix function declarations with main code * remove unused variables * modify kickstart to use GCS tarballs instead of git repo * fix codacy

Paweł Krupa committed Jan 17, 2019 at 21:05 UTC b81bba75f1fc6f275d0acb4a3fc68a7b8c062c65
1 file changed +183 -301
packaging/installer/kickstart.sh
+183 -301
@@ -10,7 +10,6 @@
10 # bash <(curl -Ss https://my-netdata.io/kickstart.sh) all
11 #
12 # Other options:
13 -# --src-dir PATH keep netdata.git at PATH/netdata.git
13 # --dont-wait do not prompt for user input
14 # --non-interactive do not prompt for user input
15 # --no-updates do not install script for daily updates
@@ -20,194 +19,186 @@
19 # 1. install all netdata compilation dependencies
20 # using the package manager of the system
21 #
23 -# 2. download netdata source code in /usr/src/netdata.git
22 +# 2. download netdata nightly package to temporary directory
23 #
24 # 3. install netdata
25
27 -# shellcheck disable=SC1117,SC2016,SC2034,SC2039,SC2059,SC2086,SC2119,SC2120,SC2129,SC2162,SC2166,SC2181
26 +# shellcheck disable=SC2039,SC2059,SC2086
27
29 -umask 022
30 -
31 -[ -z "${UID}" ] && UID="$(id -u)"
28 +# External files
29 +PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata-demo-site/master/install-required-packages.sh"
30 +NIGHTLY_PACKAGE_TARBALL="https://storage.googleapis.com/netdata-nightlies/netdata-latest.tar.gz"
31 +NIGHTLY_PACKAGE_CHECKSUM="https://storage.googleapis.com/netdata-nightlies/sha256sums.txt"
32
33 # ---------------------------------------------------------------------------------------------------------------------
34 # library functions copied from packaging/installer/functions.sh
35
36 -which_cmd() {
37 - # shellcheck disable=SC2230
38 - which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
36 +setup_terminal() {
37 + TPUT_RESET=""
38 + TPUT_YELLOW=""
39 + TPUT_WHITE=""
40 + TPUT_BGRED=""
41 + TPUT_BGGREEN=""
42 + TPUT_BOLD=""
43 + TPUT_DIM=""
44 +
45 + # Is stderr on the terminal? If not, then fail
46 + test -t 2 || return 1
47 +
48 + if command -v tput >/dev/null 2>&1; then
49 + if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
50 + # Enable colors
51 + TPUT_RESET="$(tput sgr 0)"
52 + TPUT_YELLOW="$(tput setaf 3)"
53 + TPUT_WHITE="$(tput setaf 7)"
54 + TPUT_BGRED="$(tput setab 1)"
55 + TPUT_BGGREEN="$(tput setab 2)"
56 + TPUT_BOLD="$(tput bold)"
57 + TPUT_DIM="$(tput dim)"
58 + fi
59 + fi
60 +
61 + return 0
62 }
63
41 -check_cmd() {
42 - which_cmd "${1}" >/dev/null 2>&1 && return 0
43 - return 1
64 +progress() {
65 + echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
66 }
67
46 -setup_terminal() {
47 - TPUT_RESET=""
48 - TPUT_BLACK=""
49 - TPUT_RED=""
50 - TPUT_GREEN=""
51 - TPUT_YELLOW=""
52 - TPUT_BLUE=""
53 - TPUT_PURPLE=""
54 - TPUT_CYAN=""
55 - TPUT_WHITE=""
56 - TPUT_BGBLACK=""
57 - TPUT_BGRED=""
58 - TPUT_BGGREEN=""
59 - TPUT_BGYELLOW=""
60 - TPUT_BGBLUE=""
61 - TPUT_BGPURPLE=""
62 - TPUT_BGCYAN=""
63 - TPUT_BGWHITE=""
64 - TPUT_BOLD=""
65 - TPUT_DIM=""
66 - TPUT_UNDERLINED=""
67 - TPUT_BLINK=""
68 - TPUT_INVERTED=""
69 - TPUT_STANDOUT=""
70 - TPUT_BELL=""
71 - TPUT_CLEAR=""
72 -
73 - # Is stderr on the terminal? If not, then fail
74 - test -t 2 || return 1
75 -
76 - if check_cmd tput
77 - then
78 - if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ]
79 - then
80 - # Enable colors
81 - TPUT_RESET="$(tput sgr 0)"
82 - TPUT_BLACK="$(tput setaf 0)"
83 - TPUT_RED="$(tput setaf 1)"
84 - TPUT_GREEN="$(tput setaf 2)"
85 - TPUT_YELLOW="$(tput setaf 3)"
86 - TPUT_BLUE="$(tput setaf 4)"
87 - TPUT_PURPLE="$(tput setaf 5)"
88 - TPUT_CYAN="$(tput setaf 6)"
89 - TPUT_WHITE="$(tput setaf 7)"
90 - TPUT_BGBLACK="$(tput setab 0)"
91 - TPUT_BGRED="$(tput setab 1)"
92 - TPUT_BGGREEN="$(tput setab 2)"
93 - TPUT_BGYELLOW="$(tput setab 3)"
94 - TPUT_BGBLUE="$(tput setab 4)"
95 - TPUT_BGPURPLE="$(tput setab 5)"
96 - TPUT_BGCYAN="$(tput setab 6)"
97 - TPUT_BGWHITE="$(tput setab 7)"
98 - TPUT_BOLD="$(tput bold)"
99 - TPUT_DIM="$(tput dim)"
100 - TPUT_UNDERLINED="$(tput smul)"
101 - TPUT_BLINK="$(tput blink)"
102 - TPUT_INVERTED="$(tput rev)"
103 - TPUT_STANDOUT="$(tput smso)"
104 - TPUT_BELL="$(tput bel)"
105 - TPUT_CLEAR="$(tput clear)"
106 - fi
107 - fi
108 -
109 - return 0
68 +escaped_print() {
69 + if printf "%q " test >/dev/null 2>&1; then
70 + printf "%q " "${@}"
71 + else
72 + printf "%s" "${*}"
73 + fi
74 + return 0
75 }
111 -setup_terminal || echo >/dev/null
76
113 -progress() {
114 - echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
77 +run() {
78 + local dir="${PWD}" info_console
79 +
80 + if [ "${UID}" = "0" ]; then
81 + info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
82 + else
83 + info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
84 + fi
85 +
86 + escaped_print "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" "${@}" "${TPUT_RESET}\n" >&2
87 +
88 + "${@}"
89 +
90 + local ret=$?
91 + if [ ${ret} -ne 0 ]; then
92 + printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
93 + else
94 + printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
95 + fi
96 +
97 + return ${ret}
98 }
99
117 -run_ok() {
118 - printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
100 +warning() {
101 + printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*} \n\n"
102 + if [ "${INTERACTIVE}" = "0" ]; then
103 + fatal "Stopping due to non-interactive mode. Fix the issue or retry installation in an interactive mode."
104 + else
105 + read -r -p "Press ENTER to attempt netdata installation > "
106 + progress "OK, let's give it a try..."
107 + fi
108 }
109
121 -run_failed() {
122 - printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
110 +fatal() {
111 + printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
112 + exit 1
113 }
114
125 -ESCAPED_PRINT_METHOD=
126 -printf "%q " test >/dev/null 2>&1
127 -[ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq"
128 -escaped_print() {
129 - if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]
130 - then
131 - printf "%q " "${@}"
132 - else
133 - printf "%s" "${*}"
134 - fi
135 - return 0
115 +download() {
116 + url="${1}"
117 + dest="${2}"
118 + if command -v wget >/dev/null 2>&1; then
119 + run wget -O - "${url}" >"${dest}" || fatal "Cannot download ${url}"
120 + elif command -v curl >/dev/null 2>&1; then
121 + run curl "${url}" >"${dest}" || fatal "Cannot download ${url}"
122 + else
123 + fatal "I need curl or wget to proceed, but neither is available on this system."
124 + fi
125 }
126
138 -run_logfile="/dev/null"
139 -run() {
140 - local user="${USER--}" dir="${PWD}" info info_console
141 -
142 - if [ "${UID}" = "0" ]
143 - then
144 - info="[root ${dir}]# "
145 - info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
146 - else
147 - info="[${user} ${dir}]$ "
148 - info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
149 - fi
150 -
151 - printf >> "${run_logfile}" "${info}"
152 - escaped_print >> "${run_logfile}" "${@}"
153 - printf >> "${run_logfile}" " ... "
154 -
155 - printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
156 - escaped_print >&2 "${@}"
157 - printf >&2 "${TPUT_RESET}\n"
158 -
159 - "${@}"
160 -
161 - local ret=$?
162 - if [ ${ret} -ne 0 ]
163 - then
164 - run_failed
165 - printf >> "${run_logfile}" "FAILED with exit code ${ret}\n"
166 - else
167 - run_ok
168 - printf >> "${run_logfile}" "OK\n"
169 - fi
170 -
171 - return ${ret}
127 +detect_bash4() {
128 + bash="${1}"
129 + if [ -z "${BASH_VERSION}" ]; then
130 + # we don't run under bash
131 + if [ -n "${bash}" ] && [ -x "${bash}" ]; then
132 + # shellcheck disable=SC2016
133 + BASH_MAJOR_VERSION=$(${bash} -c 'echo "${BASH_VERSINFO[0]}"')
134 + fi
135 + else
136 + # we run under bash
137 + BASH_MAJOR_VERSION="${BASH_VERSINFO[0]}"
138 + fi
139 +
140 + if [ -z "${BASH_MAJOR_VERSION}" ]; then
141 + echo >&2 "No BASH is available on this system"
142 + return 1
143 + elif [ $((BASH_MAJOR_VERSION)) -lt 4 ]; then
144 + echo >&2 "No BASH v4+ is available on this system (installed bash is v${BASH_MAJOR_VERSION}"
145 + return 1
146 + fi
147 + return 0
148 }
149
150 +umask 022
151 +
152 +sudo=""
153 +[ -z "${UID}" ] && UID="$(id -u)"
154 +[ "${UID}" -ne "0" ] && sudo="sudo"
155 +export PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
156 +
157 +setup_terminal || echo >/dev/null
158
159 # ---------------------------------------------------------------------------------------------------------------------
176 -# collect system information
160 +# try to update using autoupdater in the first place
161 +
162 +updater=""
163 +[ -x /etc/periodic/daily/netdata-updater ] && updater=/etc/periodic/daily/netdata-updater
164 +[ -x /etc/cron.daily/netdata-updater ] && updater=/etc/cron.daily/netdata-updater
165 +if [ -n "${updater}" ]; then
166 + # attempt to run the updater, to respect any compilation settings already in place
167 + progress "Re-installing netdata..."
168 + run "${sudo}" "${updater}" -f || fatal "Failed to forcefully update netdata"
169 + exit 0
170 +fi
171
178 -fatal() {
179 - printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${*} \n\n"
180 - exit 1
181 -}
172 +# ---------------------------------------------------------------------------------------------------------------------
173 +# install required system packages
174
183 -export PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
175 +INTERACTIVE=1
176 +PACKAGES_INSTALLER_OPTIONS="netdata"
177 +NETDATA_INSTALLER_OPTIONS=""
178 +NETDATA_UPDATES="--auto-update"
179 +while [ -n "${1}" ]; do
180 + if [ "${1}" = "all" ]; then
181 + PACKAGES_INSTALLER_OPTIONS="netdata-all"
182 + shift 1
183 + elif [ "${1}" = "--dont-wait" ] || [ "${1}" = "--non-interactive" ]; then
184 + INTERACTIVE=0
185 + shift 1
186 + elif [ "${1}" = "--no-updates" ]; then
187 + # echo >&2 "netdata will not auto-update"
188 + NETDATA_UPDATES=
189 + shift 1
190 + else
191 + break
192 + fi
193 +done
194
185 -curl="$(which_cmd curl)"
186 -wget="$(which_cmd wget)"
187 -bash="$(which_cmd bash)"
188 -
189 -if [ -z "${BASH_VERSION}" ]
190 -then
191 - # we don't run under bash
192 - if [ ! -z "${bash}" -a -x "${bash}" ]
193 - then
194 - BASH_MAJOR_VERSION=$(${bash} -c 'echo "${BASH_VERSINFO[0]}"')
195 - fi
196 -else
197 - # we run under bash
198 - BASH_MAJOR_VERSION="${BASH_VERSINFO[0]}"
195 +if [ "${INTERACTIVE}" = "0" ]; then
196 + PACKAGES_INSTALLER_OPTIONS="--dont-wait --non-interactive ${PACKAGES_INSTALLER_OPTIONS}"
197 + NETDATA_INSTALLER_OPTIONS="--dont-wait"
198 fi
199
201 -HAS_BASH4=1
202 -if [ -z "${BASH_MAJOR_VERSION}" ]
203 -then
204 - echo >&2 "No BASH is available on this system"
205 - HAS_BASH4=0
206 -elif [ $((BASH_MAJOR_VERSION)) -lt 4 ]
207 -then
208 - echo >&2 "No BASH v4+ is available on this system (installed bash is v${BASH_MAJOR_VERSION}"
209 - HAS_BASH4=0
210 -fi
200 +# ---------------------------------------------------------------------------------------------------------------------
201 +# detect system parameters and install dependencies
202
203 SYSTEM="$(uname -s)"
204 OS="$(uname -o)"
@@ -220,158 +211,49 @@ Machine : ${MACHINE}
211 BASH major version: ${BASH_MAJOR_VERSION}
212 EOF
213
223 -sudo=""
224 -[ "${UID}" -ne "0" ] && sudo="sudo"
225 -
214 +tmpdir="$(mktemp -d /tmp/netdata-kickstart-XXXXXX)"
215 +cd "${tmpdir}" || :
216
227 -# ---------------------------------------------------------------------------------------------------------------------
228 -# install required system packages
229 -
230 -INTERACTIVE=1
231 -PACKAGES_INSTALLER_OPTIONS="netdata"
232 -NETDATA_INSTALLER_OPTIONS=""
233 -NETDATA_UPDATES="-u"
234 -SOURCE_DST="/usr/src"
235 -while [ ! -z "${1}" ]
236 -do
237 - if [ "${1}" = "all" ]
238 - then
239 - PACKAGES_INSTALLER_OPTIONS="netdata-all"
240 - shift 1
241 - elif [ "${1}" = "--dont-wait" -o "${1}" = "--non-interactive" ]
242 - then
243 - INTERACTIVE=0
244 - shift 1
245 - elif [ "${1}" = "--src-dir" ]
246 - then
247 - SOURCE_DST="${2}"
248 - # echo >&2 "netdata source will be installed at ${SOURCE_DST}/netdata.git"
249 - shift 2
250 - elif [ "${1}" = "--no-updates" ]
251 - then
252 - # echo >&2 "netdata will not auto-update"
253 - NETDATA_UPDATES=
254 - shift 1
255 - else
256 - break
257 - fi
258 -done
259 -
260 -if [ "${INTERACTIVE}" = "0" ]
261 -then
262 - PACKAGES_INSTALLER_OPTIONS="--dont-wait --non-interactive ${PACKAGES_INSTALLER_OPTIONS}"
263 - NETDATA_INSTALLER_OPTIONS="--dont-wait"
264 -fi
265 -
266 -# echo "PACKAGES_INSTALLER_OPTIONS=${PACKAGES_INSTALLER_OPTIONS}"
267 -# echo "NETDATA_INSTALLER_OPTIONS=${NETDATA_INSTALLER_OPTIONS} ${*}"
268 -
269 -if [ "${OS}" = "GNU/Linux" -o "${SYSTEM}" = "Linux" ]
270 -then
271 - if [ "${HAS_BASH4}" = "1" ]
272 - then
273 - tmp="$(mktemp /tmp/netdata-kickstart-XXXXXX)"
274 - url="https://raw.githubusercontent.com/netdata/netdata-demo-site/master/install-required-packages.sh"
275 -
276 - progress "Downloading script to detect required packages..."
277 - if [ ! -z "${curl}" ]
278 - then
279 - run ${curl} "${url}" >"${tmp}" || fatal "Cannot download ${url}"
280 - elif [ ! -z "${wget}" ]
281 - then
282 - run "${wget}" -O - "${url}" >"${tmp}" || fatal "Cannot download ${url}"
283 - else
284 - rm "${tmp}"
285 - fatal "I need curl or wget to proceed, but neither is available on this system."
286 - fi
287 -
288 - ask=0
289 - if [ -s "${tmp}" ]
290 - then
291 - progress "Running downloaded script to detect required packages..."
292 - run ${sudo} "${bash}" "${tmp}" ${PACKAGES_INSTALLER_OPTIONS} || ask=1
293 - rm "${tmp}"
294 - else
295 - rm "${tmp}"
296 - fatal "Downloaded script is empty..."
297 - fi
298 -
299 - if [ "${ask}" = "1" ]
300 - then
301 - echo >&2 "It failed to install all the required packages, but I can try to install netdata."
302 - read -p "Press ENTER to continue to netdata installation > "
303 - progress "OK, let's give it a try..."
304 - fi
305 - else
306 - echo >&2 "WARNING"
307 - echo >&2 "Cannot detect the packages to be installed in this system, without BASH v4+."
308 - echo >&2 "We can only attempt to install netdata..."
309 - echo >&2
310 - fi
217 +if [ "${OS}" != "GNU/Linux" ] && [ "${SYSTEM}" != "Linux" ]; then
218 + warning "Cannot detect the packages to be installed on a ${SYSTEM} - ${OS} system."
219 else
312 - echo >&2 "WARNING"
313 - echo >&2 "Cannot detect the packages to be installed on a ${SYSTEM} - ${OS} system."
314 - echo >&2 "We can only attempt to install netdata..."
315 - echo >&2
220 + bash="$(command -v bash 2>/dev/null)"
221 + if ! detect_bash4 "${bash}"; then
222 + warning "Cannot detect packages to be installed in this system, without BASH v4+."
223 + else
224 + progress "Downloading script to detect required packages..."
225 + download "${PACKAGES_SCRIPT}" "${tmpdir}/install-required-packages.sh"
226 + if [ ! -s "${tmpdir}/install-required-packages.sh" ]; then
227 + warning "Downloaded dependency installation script is empty."
228 + else
229 + progress "Running downloaded script to detect required packages..."
230 + if run ${sudo} "${bash}" "${tmpdir}/install-required-packages.sh" ${PACKAGES_INSTALLER_OPTIONS}; then
231 + warning "It failed to install all the required packages, but installation might still be possible."
232 + fi
233 + fi
234 +
235 + fi
236 fi
237
318 -
238 # ---------------------------------------------------------------------------------------------------------------------
320 -# download netdata source
321 -
322 -# this has to checked after we have installed the required packages
323 -git="$(which_cmd git)"
324 -
325 -NETDATA_SOURCE_DIR=
326 -if [ ! -z "${git}" -a -x "${git}" ]
327 -then
328 - [ ! -d "${SOURCE_DST}" ] && run ${sudo} mkdir -p "${SOURCE_DST}"
329 -
330 - if [ ! -d "${SOURCE_DST}/netdata.git" ]
331 - then
332 - progress "Downloading netdata source code..."
333 - run ${sudo} ${git} clone https://github.com/netdata/netdata.git "${SOURCE_DST}/netdata.git" || fatal "Cannot download netdata source"
334 - cd "${SOURCE_DST}/netdata.git" || fatal "Cannot cd to netdata source tree"
335 - else
336 - progress "Updating netdata source code..."
337 - cd "${SOURCE_DST}/netdata.git" || fatal "Cannot cd to netdata source tree"
338 - run ${sudo} ${git} fetch --all || fatal "Cannot fetch netdata source updates"
339 - run ${sudo} ${git} reset --hard origin/master || fatal "Cannot update netdata source tree"
340 - fi
341 - NETDATA_SOURCE_DIR="${SOURCE_DST}/netdata.git"
342 -else
343 - fatal "Cannot find the command 'git' to download the netdata source code."
344 -fi
239 +# download netdata nightly package
240
241 +download "${NIGHTLY_PACKAGE_CHECKSUM}" "${tmpdir}/sha256sum.txt"
242 +download "${NIGHTLY_PACKAGE_TARBALL}" "${tmpdir}/netdata-latest.tar.gz"
243 +if ! grep netdata-latest.tar.gz sha256sum.txt | sha256sum --check - >/dev/null 2>&1; then
244 + failed "Tarball checksum validation failed. Stopping netdata installation and leaving tarball in ${tmpdir}"
245 +fi
246 +run tar -xf netdata-latest.tar.gz
247 +rm -rf netdata-latest.tar.gz >/dev/null 2>&1
248 +cd netdata-* || fatal "Cannot cd to netdata source tree"
249
250 # ---------------------------------------------------------------------------------------------------------------------
251 # install netdata from source
252
350 -if [ ! -z "${NETDATA_SOURCE_DIR}" -a -d "${NETDATA_SOURCE_DIR}" ]
351 -then
352 - cd "${NETDATA_SOURCE_DIR}" || fatal "Cannot cd to netdata source tree"
353 -
354 - install=0
355 - if [ -x netdata-updater.sh ]
356 - then
357 - # attempt to run the updater, to respect any compilation settings already in place
358 - progress "Re-installing netdata..."
359 - run ${sudo} ./netdata-updater.sh -f || install=1
360 - else
361 - install=1
362 - fi
363 -
364 - if [ "${install}" = "1" ]
365 - then
366 - if [ -x netdata-installer.sh ]
367 - then
368 - progress "Installing netdata..."
369 - run ${sudo} ./netdata-installer.sh ${NETDATA_UPDATES} ${NETDATA_INSTALLER_OPTIONS} "${@}" || \
370 - fatal "netdata-installer.sh exited with error"
371 - else
372 - fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh)."
373 - fi
374 - fi
253 +if [ -x netdata-installer.sh ]; then
254 + progress "Installing netdata..."
255 + run ${sudo} ./netdata-installer.sh ${NETDATA_UPDATES} ${NETDATA_INSTALLER_OPTIONS} "${@}" || fatal "netdata-installer.sh exited with error"
256 + rm -rf "${tmpdir}" >/dev/null 2>&1
257 else
376 - fatal "Cannot install netdata from source, on this system (cannot download the source code)."
258 + fatal "Cannot install netdata from source (the source directory does not include netdata-installer.sh). Leaving all files in ${tmpdir}"
259 fi