@cryptotaxi247 / netdata-1 / commits / c8d2e6c7d

netdata/packaging/installer: netdata-installer.sh script (and subscripts) refactoring wrap-up (#5736)

* remove dead quickfix * unify global variable naming * do not use double negation * simplify setcap detection * simplify logic of setting capabilities to apps.plugin * always set group in chown * simplify parameter parsing and banner notifications * use built-in command for checking program availability * lint functions.sh * fix errors returned by shellcheck * remove unused functions and use portable_service * move user management functions closer together and remove "check" functions to reduce levels of indirection * extract add_netdata_user_and_group and move it into installers for better code readability * improve readability by not using global variable holding number of processors * move netdata.conf file creation into one function * revert migration to portable_service * Less verbose setcap * remove TODOs * do not show output when not needed * fix checking for group existence * fix variable name * netdata/packaging/installer: Dont spill out unnecessary output in stdout/stderr, it may confuse our users The second commit was a follow up cleanup on nits. * access: change codeowner * Revert "access: change codeowner" This reverts commit 6ee51ccae960584d5b1b7c243f5aabf046a99eff. sorry, wrong repo * netdata/packaging/installer: bug fix - do not use pidof as the name of the helper method 1) rename to safe_pidof, which is actually more accurate and describe what this method serves for (safe pid detection) 2) Renaming it also avoids the bug introduced by the usage of , that resulted in endless nested calls of the BASH method pidif (Refer to command -v usage)

Paul Katsoulakis committed Mar 29, 2019 at 14:52 UTC c8d2e6c7d3b955b6d5781e2710b02bccc29a3c79
6 files changed +387 -586
netdata-installer.sh
+198 -284
@@ -1,43 +1,47 @@
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 -# shellcheck disable=SC1090,SC1091,SC1117,SC2002,SC2034,SC2044,SC2046,SC2086,SC2129,SC2162,SC2166,SC2181
3 +# shellcheck disable=SC2046,SC2086,SC2166
4
5 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
6 uniquepath() {
7 local path=""
8 - while read; do
8 + while read -r; do
9 if [[ ! ${path} =~ (^|:)"${REPLY}"(:|$) ]]; then
10 - [ ! -z "${path}" ] && path="${path}:"
10 + [ -n "${path}" ] && path="${path}:"
11 path="${path}${REPLY}"
12 fi
13 - done < <(echo "${PATH}" | tr ":" "\n")
13 + done < <(echo "${PATH}" | tr ":" "\\n")
14
15 - [ ! -z "${path}" ] && [[ ${PATH} =~ /bin ]] && [[ ${PATH} =~ /sbin ]] && export PATH="${path}"
15 + [ -n "${path}" ] && [[ ${PATH} =~ /bin ]] && [[ ${PATH} =~ /sbin ]] && export PATH="${path}"
16 }
17 uniquepath
18
19 -netdata_source_dir="$(pwd)"
20 -installer_dir="$(dirname "${0}")"
19 +PROGRAM="$0"
20 +NETDATA_SOURCE_DIR="$(pwd)"
21 +INSTALLER_DIR="$(dirname "${PROGRAM}")"
22
22 -if [ "${netdata_source_dir}" != "${installer_dir}" -a "${installer_dir}" != "." ]; then
23 - echo >&2 "Warning: you are currently in '${netdata_source_dir}' but the installer is in '${installer_dir}'."
23 +if [ "${NETDATA_SOURCE_DIR}" != "${INSTALLER_DIR}" ] && [ "${INSTALLER_DIR}" != "." ]; then
24 + echo >&2 "Warning: you are currently in '${NETDATA_SOURCE_DIR}' but the installer is in '${INSTALLER_DIR}'."
25 fi
26
27 # -----------------------------------------------------------------------------
28 # reload the user profile
29
30 +# shellcheck source=/dev/null
31 [ -f /etc/profile ] && . /etc/profile
32
33 # make sure /etc/profile does not change our current directory
32 -cd "${netdata_source_dir}" || exit 1
34 +cd "${NETDATA_SOURCE_DIR}" || exit 1
35
36 # -----------------------------------------------------------------------------
37 # load the required functions
38
37 -if [ -f "${installer_dir}/packaging/installer/functions.sh" ]; then
38 - source "${installer_dir}/packaging/installer/functions.sh" || exit 1
39 +if [ -f "${INSTALLER_DIR}/packaging/installer/functions.sh" ]; then
40 + # shellcheck source=packaging/installer/functions.sh
41 + source "${INSTALLER_DIR}/packaging/installer/functions.sh" || exit 1
42 else
40 - source "${netdata_source_dir}/packaging/installer/functions.sh" || exit 1
43 + # shellcheck source=packaging/installer/functions.sh
44 + source "${NETDATA_SOURCE_DIR}/packaging/installer/functions.sh" || exit 1
45 fi
46
47 download() {
@@ -74,199 +78,164 @@ CFLAGS="${CFLAGS--O2}"
78 [ "z${CFLAGS}" = "z-O3" ] && CFLAGS="-O2"
79
80 # keep a log of this command
77 -printf "\n# " >>netdata-installer.log
81 +# shellcheck disable=SC2129
82 +printf "\\n# " >>netdata-installer.log
83 date >>netdata-installer.log
84 printf 'CFLAGS="%s" ' "${CFLAGS}" >>netdata-installer.log
80 -printf "%q " "$0" "${@}" >>netdata-installer.log
81 -printf "\n" >>netdata-installer.log
85 +printf "%q " "${PROGRAM}" "${@}" >>netdata-installer.log
86 +printf "\\n" >>netdata-installer.log
87
83 -REINSTALL_PWD="${PWD}"
88 REINSTALL_COMMAND="$(
85 - printf "%q " "$0" "${@}"
86 - printf "\n"
89 + printf "%q " "${PROGRAM}" "${@}"
90 + printf "\\n"
91 )"
92 # remove options that shown not be inherited by netdata-updater.sh
93 REINSTALL_COMMAND="${REINSTALL_COMMAND// --dont-wait/}"
94 REINSTALL_COMMAND="${REINSTALL_COMMAND// --dont-start-it/}"
91 -[ "${REINSTALL_COMMAND:0:1}" != "." -a "${REINSTALL_COMMAND:0:1}" != "/" -a -f "./${0}" ] && REINSTALL_COMMAND="./${REINSTALL_COMMAND}"
92 -
93 -# shellcheck disable=SC2230
94 -setcap="$(which setcap 2>/dev/null || command -v setcap 2>/dev/null)"
95 -
96 -ME="$0"
97 -DONOTSTART=0
98 -DONOTWAIT=0
99 -AUTOUPDATE=0
100 -NETDATA_PREFIX=
101 -LIBS_ARE_HERE=0
102 -NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS-}"
103 -RELEASE_CHANNEL="nightly"
104 -
105 -usage() {
106 - netdata_banner "installer command line options"
107 - cat <<USAGE
108 -
109 -${ME} <installer options>
110 -
111 -Valid <installer options> are:
112 -
113 - --install /PATH/TO/INSTALL
114 -
115 - If you give: --install /opt
116 - netdata will be installed in /opt/netdata
117 -
118 - --dont-start-it
119 -
120 - Do not (re)start netdata.
121 - Just install it.
122 -
123 - --dont-wait
124 -
125 - Do not wait for the user to press ENTER.
126 - Start immediately building it.
127 -
128 - --auto-update | -u
129 -
130 - Install netdata-updater to cron,
131 - to update netdata automatically once per day
132 -
133 - --stable-channel
134 -
135 - Auto-updater will update netdata only when new release is published
136 - in GitHub release pages. This results in less frequent updates.
137 - Default: Use packages from GCS (nightly release channel).
138 -
139 - --enable-plugin-freeipmi
140 - --disable-plugin-freeipmi
95 +if [ "${REINSTALL_COMMAND:0:1}" != "." ] && [ "${REINSTALL_COMMAND:0:1}" != "/" ] && [ -f "./${PROGRAM}" ]; then
96 + REINSTALL_COMMAND="./${REINSTALL_COMMAND}"
97 +fi
98
142 - Enable/disable the FreeIPMI plugin.
143 - Default: enable it when libipmimonitoring is available.
99 +banner_nonroot_install() {
100 + cat <<NONROOTNOPREFIX
101
145 - --enable-plugin-nfacct
146 - --disable-plugin-nfacct
102 + ${TPUT_RED}${TPUT_BOLD}Sorry! This will fail!${TPUT_RESET}
103
148 - Enable/disable the nfacct plugin.
149 - Default: enable it when libmnl and libnetfilter_acct are available.
104 + You are attempting to install netdata as non-root, but you plan
105 + to install it in system paths.
106
151 - --enable-plugin-xenstat
152 - --disable-plugin-xenstat
107 + Please set an installation prefix, like this:
108
154 - Enable/disable the xenstat plugin.
155 - Default: enable it when libxenstat and libyajl are available.
109 + $PROGRAM ${@} --install /tmp
110
157 - --enable-lto
158 - --disable-lto
111 + or, run the installer as root:
112
160 - Enable/disable Link-Time-Optimization
161 - Default: enabled
113 + sudo $PROGRAM ${@}
114
163 - --disable-x86-sse
115 + We suggest to install it as root, or certain data collectors will
116 + not be able to work. Netdata drops root privileges when running.
117 + So, if you plan to keep it, install it as root to get the full
118 + functionality.
119
165 - Disable SSE instructions
166 - Default: enabled
120 +NONROOTNOPREFIX
121 +}
122
168 - --zlib-is-really-here
169 - --libs-are-really-here
123 +banner_root_notify() {
124 + cat <<NONROOT
125
171 - If you get errors about missing zlib,
172 - or libuuid but you know it is available,
173 - you have a broken pkg-config.
174 - Use this option to allow it continue
175 - without checking pkg-config.
126 + ${TPUT_RED}${TPUT_BOLD}IMPORTANT${TPUT_RESET}:
127 + You are about to install netdata as a non-root user.
128 + Netdata will work, but a few data collection modules that
129 + require root access will fail.
130
177 - --disable-telemetry
131 + If you installing netdata permanently on your system, run
132 + the installer like this:
133
179 - Use this flag to opt-out from our anonymous telemetry progam.
134 + ${TPUT_YELLOW}${TPUT_BOLD}sudo $PROGRAM ${@}${TPUT_RESET}
135
181 - --disable-go
136 +NONROOT
137 +}
138
183 - Flag to disable installation of go.d.plugin
139 +usage() {
140 + netdata_banner "installer command line options"
141 + cat <<HEREDOC
142 +
143 +USAGE: ${PROGRAM} [options]
144 + where options include:
145 +
146 + --install <path> Install netdata in <path>. Ex. --install /opt will put netdata in /opt/netdata
147 + --dont-start-it Do not (re)start netdata after installation
148 + --dont-wait Run installation in non-interactive mode
149 + --auto-update or -u Install netdata-updater in cron to update netdata automatically once per day
150 + --stable-channel Use packages from GitHub release pages instead of GCS (nightly updates).
151 + This results in less frequent updates.
152 + --disable-go Disable installation of go.d.plugin.
153 + --enable-plugin-freeipmi Enable the FreeIPMI plugin. Default: enable it when libipmimonitoring is available.
154 + --disable-plugin-freeipmi
155 + --enable-plugin-nfacct Enable nfacct plugin. Default: enable it when libmnl and libnetfilter_acct are available.
156 + --disable-plugin-nfacct
157 + --enable-plugin-xenstat Enable the xenstat plugin. Default: enable it when libxenstat and libyajl are available
158 + --disable-plugin-xenstat Disable the xenstat plugin.
159 + --enable-lto Enable Link-Time-Optimization. Default: enabled
160 + --disable-lto
161 + --disable-x86-sse Disable SSE instructions. By default SSE optimizations are enabled.
162 + --zlib-is-really-here or
163 + --libs-are-really-here If you get errors about missing zlib or libuuid but you know it is available, you might
164 + have a broken pkg-config. Use this option to proceed without checking pkg-config.
165 + --disable-telemetry Use this flag to opt-out from our anonymous telemetry progam.
166
167 Netdata will by default be compiled with gcc optimization -O2
168 If you need to pass different CFLAGS, use something like this:
169
188 - CFLAGS="<gcc options>" ${ME} <installer options>
170 + CFLAGS="<gcc options>" ${PROGRAM} [options]
171
190 -For the installer to complete successfully, you will need
191 -these packages installed:
172 +For the installer to complete successfully, you will need these packages installed:
173
193 - gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel)
194 - uuid-dev (or libuuid-devel)
174 + gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel) uuid-dev (or libuuid-devel)
175
176 For the plugins, you will at least need:
177
198 - curl, bash v4+, python v2 or v3, node.js
178 + curl, bash v4+, python v2 or v3, node.js
179
200 -USAGE
180 +HEREDOC
181 }
182
203 -while [ ! -z "${1}" ]; do
204 - if [ "$1" = "--install" ]; then
205 - NETDATA_PREFIX="${2}/netdata"
206 - shift 2
207 - elif [ "$1" = "--zlib-is-really-here" -o "$1" = "--libs-are-really-here" ]; then
208 - LIBS_ARE_HERE=1
209 - shift 1
210 - elif [ "$1" = "--dont-start-it" ]; then
211 - DONOTSTART=1
212 - shift 1
213 - elif [ "$1" = "--dont-wait" ]; then
214 - DONOTWAIT=1
215 - shift 1
216 - elif [ "$1" = "--auto-update" -o "$1" = "-u" ]; then
217 - AUTOUPDATE=1
218 - shift 1
219 - elif [ "$1" = "--stable-channel" ]; then
220 - RELEASE_CHANNEL="stable"
221 - shift 1
222 - elif [ "$1" = "--enable-plugin-freeipmi" ]; then
223 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-freeipmi/} --enable-plugin-freeipmi"
224 - shift 1
225 - elif [ "$1" = "--disable-plugin-freeipmi" ]; then
226 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-freeipmi/} --disable-plugin-freeipmi"
227 - shift 1
228 - elif [ "$1" = "--enable-plugin-nfacct" ]; then
229 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-nfacct/} --enable-plugin-nfacct"
230 - shift 1
231 - elif [ "$1" = "--disable-plugin-nfacct" ]; then
232 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-nfacct/} --disable-plugin-nfacct"
233 - shift 1
234 - elif [ "$1" = "--enable-plugin-xenstat" ]; then
235 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-xenstat/} --enable-plugin-xenstat"
236 - shift 1
237 - elif [ "$1" = "--disable-plugin-xenstat" ]; then
238 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-xenstat/} --disable-plugin-xenstat"
239 - shift 1
240 - elif [ "$1" = "--enable-lto" ]; then
241 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-lto/} --enable-lto"
242 - shift 1
243 - elif [ "$1" = "--disable-lto" ]; then
244 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-lto/} --disable-lto"
245 - shift 1
246 - elif [ "$1" = "--disable-x86-sse" ]; then
247 - NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-x86-sse/} --disable-x86-sse"
248 - shift 1
249 - elif [ "$1" = "--disable-telemetry" ]; then
250 - NETDATA_DISABLE_TELEMETRY=1
251 - shift 1
252 - elif [ "$1" = "--disable-go" ]; then
253 - NETDATA_DISABLE_GO=1
254 - shift 1
255 - elif [ "$1" = "--help" -o "$1" = "-h" ]; then
256 - usage
257 - exit 1
258 - else
259 - echo >&2
260 - echo >&2 "ERROR:"
261 - echo >&2 "I cannot understand option '$1'."
262 - usage
263 - exit 1
264 - fi
183 +DONOTSTART=0
184 +DONOTWAIT=0
185 +AUTOUPDATE=0
186 +NETDATA_PREFIX=
187 +LIBS_ARE_HERE=0
188 +NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS-}"
189 +RELEASE_CHANNEL="nightly"
190 +while [ -n "${1}" ]; do
191 + case "${1}" in
192 + "--zlib-is-really-here") LIBS_ARE_HERE=1;;
193 + "--libs-are-really-here") LIBS_ARE_HERE=1;;
194 + "--dont-start-it") DONOTSTART=1;;
195 + "--dont-wait") DONOTWAIT=1;;
196 + "--auto-update"|"-u") AUTOUPDATE=1;;
197 + "--stable-channel") RELEASE_CHANNEL="stable";;
198 + "--enable-plugin-freeipmi") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-freeipmi/} --enable-plugin-freeipmi";;
199 + "--disable-plugin-freeipmi") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-freeipmi/} --disable-plugin-freeipmi";;
200 + "--enable-plugin-nfacct") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-nfacct/} --enable-plugin-nfacct";;
201 + "--disable-plugin-nfacct") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-nfacct/} --disable-plugin-nfacct";;
202 + "--enable-plugin-xenstat") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-xenstat/} --enable-plugin-xenstat";;
203 + "--disable-plugin-xenstat") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-xenstat/} --disable-plugin-xenstat";;
204 + "--enable-lto") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-lto/} --enable-lto";;
205 + "--disable-lto") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-lto/} --disable-lto";;
206 + "--disable-x86-sse") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-x86-sse/} --disable-x86-sse";;
207 + "--disable-telemetry") NETDATA_DISABLE_TELEMETRY=1;;
208 + "--disable-go") NETDATA_DISABLE_GO=1;;
209 + "--install")
210 + NETDATA_PREFIX="${2}/netdata"
211 + shift 1
212 + ;;
213 + "--help"|"-h")
214 + usage
215 + exit 1
216 + ;;
217 + *)
218 + run_failed "I cannot understand option '$1'."
219 + usage
220 + exit 1
221 + ;;
222 + esac
223 + shift 1
224 done
225
226 # replace multiple spaces with a single space
227 NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS// / }"
228
229 +if [ "${UID}" -ne 0 ]; then
230 + if [ -z "${NETDATA_PREFIX}" ]; then
231 + netdata_banner "wrong command line options!"
232 + banner_nonroot_install "${@}"
233 + exit 1
234 + else
235 + banner_root_notify "${@}"
236 + fi
237 +fi
238 +
239 netdata_banner "real-time performance monitoring, done right!"
240 cat <<BANNER1
241
@@ -295,49 +264,6 @@ cat <<BANNER3
264
265 BANNER3
266
298 -if [ "${UID}" -ne 0 ]; then
299 - if [ -z "${NETDATA_PREFIX}" ]; then
300 - netdata_banner "wrong command line options!"
301 - cat <<NONROOTNOPREFIX
302 -
303 - ${TPUT_RED}${TPUT_BOLD}Sorry! This will fail!${TPUT_RESET}
304 -
305 - You are attempting to install netdata as non-root, but you plan
306 - to install it in system paths.
307 -
308 - Please set an installation prefix, like this:
309 -
310 - $0 ${@} --install /tmp
311 -
312 - or, run the installer as root:
313 -
314 - sudo $0 ${@}
315 -
316 - We suggest to install it as root, or certain data collectors will
317 - not be able to work. Netdata drops root privileges when running.
318 - So, if you plan to keep it, install it as root to get the full
319 - functionality.
320 -
321 -NONROOTNOPREFIX
322 - exit 1
323 -
324 - else
325 - cat <<NONROOT
326 -
327 - ${TPUT_RED}${TPUT_BOLD}IMPORTANT${TPUT_RESET}:
328 - You are about to install netdata as a non-root user.
329 - Netdata will work, but a few data collection modules that
330 - require root access will fail.
331 -
332 - If you installing netdata permanently on your system, run
333 - the installer like this:
334 -
335 - ${TPUT_YELLOW}${TPUT_BOLD}sudo $0 ${@}${TPUT_RESET}
336 -
337 -NONROOT
338 - fi
339 -fi
340 -
267 have_autotools=
268 if [ "$(type autoreconf 2>/dev/null)" ]; then
269 autoconf_maj_min() {
@@ -381,13 +307,16 @@ EOF
307 fi
308
309 if [ ${DONOTWAIT} -eq 0 ]; then
384 - if [ ! -z "${NETDATA_PREFIX}" ]; then
385 - eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to \'\001${TPUT_CYAN}\002${NETDATA_PREFIX}\001${TPUT_YELLOW}\002\'\001${TPUT_RESET}\002 > ' -e -r REPLY"
386 - [ $? -ne 0 ] && exit 1
310 + if [ -n "${NETDATA_PREFIX}" ]; then
311 + echo -n "${TPUT_BOLD}${TPUT_GREEN}Press ENTER to build and install netdata to '${TPUT_CYAN}${NETDATA_PREFIX}${TPUT_YELLOW}'${TPUT_RESET} > "
312 else
388 - eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to your system\001${TPUT_RESET}\002 > ' -e -r REPLY"
389 - [ $? -ne 0 ] && exit 1
313 + echo -n "${TPUT_BOLD}${TPUT_GREEN}Press ENTER to build and install netdata to your system${TPUT_RESET} > "
314 + fi
315 + read -ern1
316 + if [ "$REPLY" != '' ]; then
317 + exit 1
318 fi
319 +
320 fi
321
322 build_error() {
@@ -466,7 +395,7 @@ run make clean
395 # -----------------------------------------------------------------------------
396 progress "Compile netdata"
397
469 -run make -j${SYSTEM_CPUS} || exit 1
398 +run make -j$(find_processors) || exit 1
399
400 # -----------------------------------------------------------------------------
401 progress "Migrate configuration files for node.d.plugin and charts.d.plugin"
@@ -506,7 +435,7 @@ fi
435 # -----------------------------------------------------------------------------
436
437 # shellcheck disable=SC2230
509 -md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null || command -v md5 2>/dev/null)"
438 +md5sum="$(command -v md5sum 2>/dev/null || command -v md5 2>/dev/null)"
439
440 deleted_stock_configs=0
441 if [ ! -f "${NETDATA_PREFIX}/etc/netdata/.installer-cleanup-of-stock-configs-done" ]; then
@@ -583,9 +512,19 @@ run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name
512 # -----------------------------------------------------------------------------
513 progress "Add user netdata to required user groups"
514
586 -homedir="${NETDATA_PREFIX}/var/lib/netdata"
587 -[ ! -z "${NETDATA_PREFIX}" ] && homedir="${NETDATA_PREFIX}"
588 -add_netdata_user_and_group "${homedir}" || run_failed "The installer does not run as root."
515 +NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody"
516 +NETDATA_ADDED_TO_GROUPS=""
517 +if [ "${UID}" -eq 0 ]; then
518 + portable_add_group netdata || :
519 + portable_add_user netdata "${NETDATA_PREFIX}/var/lib/netdata" || :
520 +
521 + for g in ${NETDATA_WANTED_GROUPS}; do
522 + # shellcheck disable=SC2086
523 + portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}"
524 + done
525 +else
526 + run_failed "The installer does not run as root."
527 +fi
528
529 # -----------------------------------------------------------------------------
530 progress "Install logrotate configuration for netdata"
@@ -621,14 +560,14 @@ else
560 NETDATA_USER="${USER}"
561 ROOT_USER="${NETDATA_USER}"
562 fi
624 -NETDATA_GROUP="$(id -g -n ${NETDATA_USER})"
563 +NETDATA_GROUP="$(id -g -n "${NETDATA_USER}")"
564 [ -z "${NETDATA_GROUP}" ] && NETDATA_GROUP="${NETDATA_USER}"
565
566 # the owners of the web files
567 NETDATA_WEB_USER="$(config_option "web" "web files owner" "${NETDATA_USER}")"
568 NETDATA_WEB_GROUP="${NETDATA_GROUP}"
630 -if [ "${UID}" = "0" -a "${NETDATA_USER}" != "${NETDATA_WEB_USER}" ]; then
631 - NETDATA_WEB_GROUP="$(id -g -n ${NETDATA_WEB_USER})"
569 +if [ "${UID}" = "0" ] && [ "${NETDATA_USER}" != "${NETDATA_WEB_USER}" ]; then
570 + NETDATA_WEB_GROUP="$(id -g -n "${NETDATA_WEB_USER}")"
571 [ -z "${NETDATA_WEB_GROUP}" ] && NETDATA_WEB_GROUP="${NETDATA_WEB_USER}"
572 fi
573 NETDATA_WEB_GROUP="$(config_option "web" "web files group" "${NETDATA_WEB_GROUP}")"
@@ -733,7 +672,7 @@ run chmod 755 "${NETDATA_LOG_DIR}"
672
673 # --- plugins ----
674
736 -if [ ${UID} -eq 0 ]; then
675 +if [ "${UID}" -eq 0 ]; then
676 # find the admin group
677 admin_group=
678 test -z "${admin_group}" && getent group root >/dev/null 2>&1 && admin_group="root"
@@ -741,47 +680,38 @@ if [ ${UID} -eq 0 ]; then
680 test -z "${admin_group}" && admin_group="${NETDATA_GROUP}"
681
682 run chown "${NETDATA_USER}:${admin_group}" "${NETDATA_LOG_DIR}"
744 - run chown -R root "${NETDATA_PREFIX}/usr/libexec/netdata"
683 + run chown -R "root:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata"
684 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
685 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0644 {} \;
686 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.plugin -exec chmod 0755 {} \;
687 run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.sh -exec chmod 0755 {} \;
688
689 if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" ]; then
751 - setcap_ret=1
752 - if ! iscontainer; then
753 - if [ ! -z "${setcap}" ]; then
754 - run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
755 - run chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
756 - run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
757 - setcap_ret=$?
758 - fi
759 -
760 - if [ ${setcap_ret} -eq 0 ]; then
761 - # if we managed to setcap
762 - # but we fail to execute apps.plugin
763 - # trigger setuid to root
764 - "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -t >/dev/null 2>&1
765 - setcap_ret=$?
690 + run chown "root:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
691 + capabilities=0
692 + if ! iscontainer && command -v setcap 1>/dev/null 2>&1; then
693 + run chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
694 + if run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"; then
695 + # if we managed to setcap, but we fail to execute apps.plugin setuid to root
696 + "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -t >/dev/null 2>&1 && capabilities=1 || capabilities=0
697 fi
698 fi
699
769 - if [ ${setcap_ret} -ne 0 ]; then
700 + if [ $capabilities -eq 0 ]; then
701 # fix apps.plugin to be setuid to root
771 - run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
702 run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
703 fi
704 fi
705
706 if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin" ]; then
777 - run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
707 + run chown "root:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
708 run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
709 fi
710
781 - if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/nfacct.plugin" ]; then
782 - run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/nfacct.plugin"
783 - run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/nfacct.plugin"
784 - fi
711 + if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/nfacct.plugin" ]; then
712 + run chown "root:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/nfacct.plugin"
713 + run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/nfacct.plugin"
714 + fi
715
716 if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/xenstat.plugin" ]; then
717 run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/xenstat.plugin"
@@ -789,12 +719,12 @@ if [ ${UID} -eq 0 ]; then
719 fi
720
721 if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network" ]; then
792 - run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network"
722 + run chown "root:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network"
723 run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network"
724 fi
725
726 if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network-helper.sh" ]; then
797 - run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network-helper.sh"
727 + run chown "root:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network-helper.sh"
728 run chmod 0550 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network-helper.sh"
729 fi
730
@@ -841,8 +771,8 @@ install_go() {
771 download "https://github.com/netdata/go.d.plugin/releases/download/$GO_PACKAGE_VERSION/$GO_PACKAGE_BASENAME" "${tmp}/$GO_PACKAGE_BASENAME"
772
773 download "https://github.com/netdata/go.d.plugin/releases/download/$GO_PACKAGE_VERSION/config.tar.gz" "${tmp}/config.tar.gz"
844 - grep "${GO_PACKAGE_BASENAME}\$" "${installer_dir}/packaging/go.d.checksums" > "${tmp}/sha256sums.txt" 2>/dev/null
845 - grep "config.tar.gz" "${installer_dir}/packaging/go.d.checksums" >> "${tmp}/sha256sums.txt" 2>/dev/null
774 + grep "${GO_PACKAGE_BASENAME}\$" "${INSTALLER_DIR}/packaging/go.d.checksums" > "${tmp}/sha256sums.txt" 2>/dev/null
775 + grep "config.tar.gz" "${INSTALLER_DIR}/packaging/go.d.checksums" >> "${tmp}/sha256sums.txt" 2>/dev/null
776
777 # Checksum validation
778 if ! (cd "${tmp}" && sha256sum -c "sha256sums.txt"); then
@@ -857,8 +787,8 @@ install_go() {
787 run chown -R "${ROOT_USER}:${NETDATA_GROUP}" "${NETDATA_STOCK_CONFIG_DIR}"
788
789 run mv "${tmp}/$GO_PACKAGE_BASENAME" "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/go.d.plugin"
860 - if [ ${UID} -eq 0 ]; then
861 - run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/go.d.plugin"
790 + if [ "${UID}" -eq 0 ]; then
791 + run chown "root:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/go.d.plugin"
792 fi
793 run chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/go.d.plugin"
794 fi
@@ -866,11 +796,6 @@ install_go() {
796 }
797 install_go
798
869 -# --- fix #1292 bug ---
870 -
871 -[ -d "${NETDATA_PREFIX}/usr/libexec" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/libexec"
872 -[ -d "${NETDATA_PREFIX}/usr/share/netdata" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/share/netdata"
873 -
799 # -----------------------------------------------------------------------------
800 progress "Install netdata at system init"
801
@@ -880,28 +805,23 @@ install_netdata_service || run_failed "Cannot install netdata init service."
805 # -----------------------------------------------------------------------------
806 # check if we can re-start netdata
807
808 +# TODO(paulfantom): Creation of configuration file should be handled by a build system. Additionally we shouldn't touch configuration files in /etc/netdata/...
809 started=0
810 if [ ${DONOTSTART} -eq 1 ]; then
885 - generate_netdata_conf "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:${NETDATA_PORT}/netdata.conf"
886 -
811 + create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
812 else
888 - restart_netdata ${NETDATA_PREFIX}/usr/sbin/netdata "${@}"
889 - if [ $? -ne 0 ]; then
890 - echo >&2
891 - echo >&2 "SORRY! FAILED TO START NETDATA!"
892 - echo >&2
893 - exit 1
813 + if ! restart_netdata "${NETDATA_PREFIX}/usr/sbin/netdata" "${@}"; then
814 + fatal "Cannot start netdata!"
815 fi
816
817 started=1
897 - echo >&2 "OK. NetData Started!"
898 - echo >&2
899 -
900 - # -----------------------------------------------------------------------------
901 - # save a config file, if it is not already there
902 -
903 - download_netdata_conf "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:${NETDATA_PORT}/netdata.conf"
818 + run_ok "netdata started!"
819 + create_netdata_conf "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:${NETDATA_PORT}/netdata.conf"
820 +fi
821 +if [ "${UID}" -eq 0 ]; then
822 + run chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
823 fi
824 +run chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
825
826 if [ "$(uname)" = "Linux" ]; then
827 # -------------------------------------------------------------------------
@@ -941,7 +861,7 @@ KSM2
861 }
862
863 if [ -f "/sys/kernel/mm/ksm/run" ]; then
944 - if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]; then
864 + if [ "$(cat "/sys/kernel/mm/ksm/run")" != "1" ]; then
865 ksm_is_available_but_disabled
866 fi
867 else
@@ -1044,17 +964,11 @@ if [ "${AUTOUPDATE}" = "1" ]; then
964 fi
965 progress "Installing new netdata-updater in cron"
966
1047 - rm ${installer_dir}/netdata-updater.sh || : #TODO(paulfantom): this workaround should be removed after v1.13.0-rc1. It just needs to be propagated
1048 -
967 rm -f "${crondir}/netdata-updater"
1050 - if [ -f "${installer_dir}/packaging/installer/netdata-updater.sh" ]; then
1051 - sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${installer_dir}/packaging/installer/netdata-updater.sh" > ${crondir}/netdata-updater || exit 1
1052 - #TODO(paulfantom): Following line is a workaround and should be removed after v1.13.0-rc1. It just needs time to be propagated.
1053 - sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${installer_dir}/packaging/installer/netdata-updater.sh" > ${installer_dir}/netdata-updater.sh || exit 1
968 + if [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
969 + sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${crondir}/netdata-updater" || exit 1
970 else
1055 - sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${netdata_source_dir}/packaging/installer/netdata-updater.sh" > ${crondir}/netdata-updater || exit 1
1056 - #TODO(paulfantom): Following line is a workaround and should be removed after v1.13.0-rc1. It just needs time to be propagated.
1057 - sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${netdata_source_dir}/packaging/installer/netdata-updater.sh" > ${installer_source_dir}/netdata-updater.sh || exit 1
971 + sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${crondir}/netdata-updater" || exit 1
972 fi
973
974 chmod 0755 ${crondir}/netdata-updater
@@ -1067,7 +981,7 @@ if [ "${AUTOUPDATE}" = "1" ]; then
981 fi
982
983 # Save environment variables
1070 -cat <<EOF > ${NETDATA_USER_CONFIG_DIR}/.environment
984 +cat <<EOF > "${NETDATA_USER_CONFIG_DIR}/.environment"
985 # Created by installer
986 PATH="${PATH}"
987 CFLAGS="${CFLAGS}"
@@ -1083,7 +997,7 @@ EOF
997
998 # Opt-out from telemetry program
999 if [ -n "${NETDATA_DISABLE_TELEMETRY+x}" ]; then
1086 - touch ${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics
1000 + touch "${NETDATA_USER_CONFIG_DIR}/.opt-out-from-anonymous-statistics"
1001 fi
1002
1003 # -----------------------------------------------------------------------------
packaging/installer/functions.sh
+159 -280
@@ -5,19 +5,6 @@
5 # make sure we have a UID
6 [ -z "${UID}" ] && UID="$(id -u)"
7
8 -# -----------------------------------------------------------------------------
9 -# checking the availability of commands
10 -
11 -which_cmd() {
12 - # shellcheck disable=SC2230
13 - which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
14 -}
15 -
16 -check_cmd() {
17 - which_cmd "${1}" >/dev/null 2>&1 && return 0
18 - return 1
19 -}
20 -
8 # -----------------------------------------------------------------------------
9
10 setup_terminal() {
@@ -50,7 +37,7 @@ setup_terminal() {
37 # Is stderr on the terminal? If not, then fail
38 test -t 2 || return 1
39
53 - if check_cmd tput; then
40 + if command -v tput 1>/dev/null 2>&1; then
41 if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
42 # Enable colors
43 TPUT_RESET="$(tput sgr 0)"
@@ -117,19 +104,20 @@ netdata_banner() {
104 # -----------------------------------------------------------------------------
105 # portable service command
106
120 -service_cmd="$(which_cmd service)"
121 -rcservice_cmd="$(which_cmd rc-service)"
122 -systemctl_cmd="$(which_cmd systemctl)"
107 +service_cmd="$(command -v service 2>/dev/null)"
108 +rcservice_cmd="$(command -v rc-service 2>/dev/null)"
109 +systemctl_cmd="$(command -v systemctl 2>/dev/null)"
110 service() {
111 +
112 local cmd="${1}" action="${2}"
113
126 - if [ ! -z "${systemctl_cmd}" ]; then
114 + if [ -n "${systemctl_cmd}" ]; then
115 run "${systemctl_cmd}" "${action}" "${cmd}"
116 return $?
129 - elif [ ! -z "${service_cmd}" ]; then
117 + elif [ -n "${service_cmd}" ]; then
118 run "${service_cmd}" "${cmd}" "${action}"
119 return $?
132 - elif [ ! -z "${rcservice_cmd}" ]; then
120 + elif [ -n "${rcservice_cmd}" ]; then
121 run "${rcservice_cmd}" "${cmd}" "${action}"
122 return $?
123 fi
@@ -139,9 +127,9 @@ service() {
127 # -----------------------------------------------------------------------------
128 # portable pidof
129
142 -pidof_cmd="$(which_cmd pidof)"
143 -pidof() {
144 - if [ ! -z "${pidof_cmd}" ]; then
130 +safe_pidof() {
131 + local pidof_cmd="$(command -v pidof 2>/dev/null)"
132 + if [ -n "${pidof_cmd}" ]; then
133 ${pidof_cmd} "${@}"
134 return $?
135 else
@@ -154,39 +142,21 @@ pidof() {
142 }
143
144 # -----------------------------------------------------------------------------
157 -# portable delete recursively interactively
158 -
159 -portable_deletedir_recursively_interactively() {
160 - if [ ! -z "$1" -a -d "$1" ]; then
161 - if [ "$(uname -s)" = "Darwin" ]; then
162 - echo >&2
163 - read >&2 -p "Press ENTER to recursively delete directory '$1' > "
164 - echo >&2 "Deleting directory '$1' ..."
165 - run rm -R "$1"
166 - else
167 - echo >&2
168 - echo >&2 "Deleting directory '$1' ..."
169 - run rm -I -R "$1"
170 - fi
171 - else
172 - echo "Directory '$1' does not exist."
173 - fi
174 -}
175 -
176 -# -----------------------------------------------------------------------------
177 -
178 -export SYSTEM_CPUS=1
179 -portable_find_processors() {
145 +find_processors() {
146 + local cpus
147 if [ -f "/proc/cpuinfo" ]; then
148 # linux
182 - SYSTEM_CPUS=$(grep -c ^processor /proc/cpuinfo)
149 + cpus=$(grep -c ^processor /proc/cpuinfo)
150 else
151 # freebsd
185 - SYSTEM_CPUS=$(sysctl hw.ncpu 2>/dev/null | grep ^hw.ncpu | cut -d ' ' -f 2)
152 + cpus=$(sysctl hw.ncpu 2>/dev/null | grep ^hw.ncpu | cut -d ' ' -f 2)
153 + fi
154 + if [ -z "${cpus}" ] || [ $((cpus)) -lt 1 ]; then
155 + echo 1
156 + else
157 + echo "${cpus}"
158 fi
187 - [ -z "${SYSTEM_CPUS}" -o $((SYSTEM_CPUS)) -lt 1 ] && SYSTEM_CPUS=1
159 }
189 -portable_find_processors
160
161 # -----------------------------------------------------------------------------
162 fatal() {
@@ -248,145 +218,10 @@ run() {
218 return ${ret}
219 }
220
251 -getent_cmd="$(which_cmd getent)"
252 -portable_check_user_exists() {
253 - local username="${1}" found=
254 -
255 - if [ ! -z "${getent_cmd}" ]; then
256 - "${getent_cmd}" passwd "${username}" >/dev/null 2>&1
257 - return $?
258 - fi
259 -
260 - found="$(cut -d ':' -f 1 </etc/passwd | grep "^${username}$")"
261 - [ "${found}" = "${username}" ] && return 0
262 - return 1
263 -}
264 -
265 -portable_check_group_exists() {
266 - local groupname="${1}" found=
267 -
268 - if [ ! -z "${getent_cmd}" ]; then
269 - "${getent_cmd}" group "${groupname}" >/dev/null 2>&1
270 - return $?
271 - fi
272 -
273 - found="$(cut -d ':' -f 1 </etc/group | grep "^${groupname}$")"
274 - [ "${found}" = "${groupname}" ] && return 0
275 - return 1
276 -}
277 -
278 -portable_check_user_in_group() {
279 - local username="${1}" groupname="${2}" users=
280 -
281 - if [ ! -z "${getent_cmd}" ]; then
282 - users="$(getent group "${groupname}" | cut -d ':' -f 4)"
283 - else
284 - users="$(grep "^${groupname}:" </etc/group | cut -d ':' -f 4)"
285 - fi
286 -
287 - [[ ",${users}," =~ ,${username}, ]] && return 0
288 - return 1
289 -}
290 -
291 -portable_add_user() {
292 - local username="${1}" homedir="${2}"
293 -
294 - [ -z "${homedir}" ] && homedir="/tmp"
295 -
296 - portable_check_user_exists "${username}"
297 - [ $? -eq 0 ] && echo >&2 "User '${username}' already exists." && return 0
298 -
299 - echo >&2 "Adding ${username} user account with home ${homedir} ..."
300 -
301 - # shellcheck disable=SC2230
302 - local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
303 -
304 - # Linux
305 - if check_cmd useradd; then
306 - run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
307 - fi
308 -
309 - # FreeBSD
310 - if check_cmd pw; then
311 - run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
312 - fi
313 -
314 - # BusyBox
315 - if check_cmd adduser; then
316 - run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
317 - fi
318 -
319 - echo >&2 "Failed to add ${username} user account !"
320 -
321 - return 1
322 -}
323 -
324 -portable_add_group() {
325 - local groupname="${1}"
326 -
327 - portable_check_group_exists "${groupname}"
328 - [ $? -eq 0 ] && echo >&2 "Group '${groupname}' already exists." && return 0
329 -
330 - echo >&2 "Adding ${groupname} user group ..."
331 -
332 - # Linux
333 - if check_cmd groupadd; then
334 - run groupadd -r "${groupname}" && return 0
335 - fi
336 -
337 - # FreeBSD
338 - if check_cmd pw; then
339 - run pw groupadd "${groupname}" && return 0
340 - fi
341 -
342 - # BusyBox
343 - if check_cmd addgroup; then
344 - run addgroup "${groupname}" && return 0
345 - fi
346 -
347 - echo >&2 "Failed to add ${groupname} user group !"
348 - return 1
349 -}
350 -
351 -portable_add_user_to_group() {
352 - local groupname="${1}" username="${2}"
353 -
354 - portable_check_group_exists "${groupname}"
355 - [ $? -ne 0 ] && echo >&2 "Group '${groupname}' does not exist." && return 1
356 -
357 - # find the user is already in the group
358 - if portable_check_user_in_group "${username}" "${groupname}"; then
359 - # username is already there
360 - echo >&2 "User '${username}' is already in group '${groupname}'."
361 - return 0
362 - else
363 - # username is not in group
364 - echo >&2 "Adding ${username} user to the ${groupname} group ..."
365 -
366 - # Linux
367 - if check_cmd usermod; then
368 - run usermod -a -G "${groupname}" "${username}" && return 0
369 - fi
370 -
371 - # FreeBSD
372 - if check_cmd pw; then
373 - run pw groupmod "${groupname}" -m "${username}" && return 0
374 - fi
375 -
376 - # BusyBox
377 - if check_cmd addgroup; then
378 - run addgroup "${username}" "${groupname}" && return 0
379 - fi
380 -
381 - echo >&2 "Failed to add user ${username} to group ${groupname} !"
382 - return 1
383 - fi
384 -}
385 -
221 iscontainer() {
222 # man systemd-detect-virt
388 - local cmd=$(which_cmd systemd-detect-virt)
389 - if [ ! -z "${cmd}" -a -x "${cmd}" ]; then
223 + local cmd=$(command -v systemd-detect-virt 2>/dev/null)
224 + if [ -n "${cmd}" ] && [ -x "${cmd}" ]; then
225 "${cmd}" --container >/dev/null 2>&1 && return 0
226 fi
227
@@ -394,15 +229,15 @@ iscontainer() {
229 # http://stackoverflow.com/a/37016302
230 local pid=$(cat /proc/1/sched 2>/dev/null | head -n 1 | {
231 IFS='(),#:' read name pid th threads
397 - echo $pid
232 + echo "$pid"
233 })
399 - if [ ! -z "${pid}" ]; then
234 + if [ -n "${pid}" ]; then
235 pid=$(( pid + 0 ))
236 [ ${pid} -gt 1 ] && return 0
237 fi
238
239 # lxc sets environment variable 'container'
405 - [ ! -z "${container}" ] && return 0
240 + [ -n "${container}" ] && return 0
241
242 # docker creates /.dockerenv
243 # http://stackoverflow.com/a/25518345
@@ -425,23 +260,23 @@ issystemd() {
260
261 # if there is no systemctl command, it is not systemd
262 # shellcheck disable=SC2230
428 - systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
263 + systemctl=$(command -v systemctl 2>/dev/null)
264 [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
265
266 # if pid 1 is systemd, it is systemd
267 [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
268
269 # if systemd is not running, it is not systemd
435 - pids=$(pidof systemd 2>/dev/null)
270 + pids=$(safe_pidof systemd 2>/dev/null)
271 [ -z "${pids}" ] && return 1
272
273 # check if the running systemd processes are not in our namespace
274 myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
275 for p in ${pids}; do
441 - ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
276 + ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)"
277
278 # if pid of systemd is in our namespace, it is systemd
444 - [ ! -z "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
279 + [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
280 done
281
282 # else, it is not systemd
@@ -460,7 +295,7 @@ install_non_systemd_init() {
295 key=$(</etc/redhat-release)
296 fi
297
463 - if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]; then
298 + if [ -d /etc/init.d ] && [ ! -f /etc/init.d/netdata ]; then
299 if [[ ${key} =~ ^(gentoo|alpine).* ]]; then
300 echo >&2 "Installing OpenRC init file..."
301 run cp system/netdata-openrc /etc/init.d/netdata &&
@@ -468,10 +303,7 @@ install_non_systemd_init() {
303 run rc-update add netdata default &&
304 return 0
305
471 - elif [ "${key}" = "debian-7" \
472 - -o "${key}" = "ubuntu-12.04" \
473 - -o "${key}" = "ubuntu-14.04" \
474 - ]; then
306 + elif [ "${key}" = "debian-7" ] || [ "${key}" = "ubuntu-12.04" ] || [ "${key}" = "ubuntu-14.04" ]; then
307 echo >&2 "Installing LSB init file..."
308 run cp system/netdata-lsb /etc/init.d/netdata &&
309 run chmod 755 /etc/init.d/netdata &&
@@ -551,10 +383,10 @@ install_netdata_service() {
383 local ret=$?
384
385 if [ ${ret} -eq 0 ]; then
554 - if [ ! -z "${service_cmd}" ]; then
386 + if [ -n "${service_cmd}" ]; then
387 NETDATA_START_CMD="service netdata start"
388 NETDATA_STOP_CMD="service netdata stop"
557 - elif [ ! -z "${rcservice_cmd}" ]; then
389 + elif [ -n "${rcservice_cmd}" ]; then
390 NETDATA_START_CMD="rc-service netdata start"
391 NETDATA_STOP_CMD="rc-service netdata stop"
392 fi
@@ -582,10 +414,10 @@ pidisnetdata() {
414 stop_netdata_on_pid() {
415 local pid="${1}" ret=0 count=0
416
585 - pidisnetdata ${pid} || return 0
417 + pidisnetdata "${pid}" || return 0
418
587 - printf >&2 "Stopping netdata on pid ${pid} ..."
588 - while [ ! -z "$pid" -a ${ret} -eq 0 ]; do
419 + printf >&2 "Stopping netdata on pid %s ..." "${pid}"
420 + while [ -n "$pid" ] && [ ${ret} -eq 0 ]; do
421 if [ ${count} -gt 45 ]; then
422 echo >&2 "Cannot stop the running netdata on pid ${pid}."
423 return 1
@@ -593,7 +425,7 @@ stop_netdata_on_pid() {
425
426 count=$((count + 1))
427
596 - run kill ${pid} 2>/dev/null
428 + run kill "${pid}" 2>/dev/null
429 ret=$?
430
431 test ${ret} -eq 0 && printf >&2 "." && sleep 2
@@ -619,11 +451,11 @@ netdata_pids() {
451 for p in \
452 $(cat /var/run/netdata.pid 2>/dev/null) \
453 $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
622 - $(pidof netdata 2>/dev/null); do
623 - ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
454 + $(safe_pidof netdata 2>/dev/null); do
455 + ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)"
456
625 - if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]; then
626 - pidisnetdata ${p} && echo "${p}"
457 + if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
458 + pidisnetdata "${p}" && echo "${p}"
459 fi
460 done
461 }
@@ -631,6 +463,7 @@ netdata_pids() {
463 stop_all_netdata() {
464 local p
465 for p in $(netdata_pids); do
466 + # shellcheck disable=SC2086
467 stop_netdata_on_pid ${p}
468 done
469 }
@@ -651,7 +484,7 @@ restart_netdata() {
484 stop_all_netdata
485 service netdata restart && started=1
486
654 - if [ ${started} -eq 1 -a -z "$(netdata_pids)" ]; then
487 + if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then
488 echo >&2 "Ooops! it seems netdata is not started."
489 started=0
490 fi
@@ -661,7 +494,7 @@ restart_netdata() {
494 fi
495 fi
496
664 - if [ ${started} -eq 1 -a -z "$(netdata_pids)" ]; then
497 + if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then
498 echo >&2 "Hm... it seems netdata is still not started."
499 started=0
500 fi
@@ -681,7 +514,7 @@ restart_netdata() {
514 # install netdata logrotate
515
516 install_netdata_logrotate() {
684 - if [ ${UID} -eq 0 ]; then
517 + if [ "${UID}" -eq 0 ]; then
518 if [ -d /etc/logrotate.d ]; then
519 if [ ! -f /etc/logrotate.d/netdata ]; then
520 run cp system/netdata.logrotate /etc/logrotate.d/netdata
@@ -699,103 +532,149 @@ install_netdata_logrotate() {
532 }
533
534 # -----------------------------------------------------------------------------
702 -# download netdata.conf
535 +# create netdata.conf
536
704 -fix_netdata_conf() {
705 - local owner="${1}"
537 +create_netdata_conf() {
538 + local path="${1}" url="${2}"
539
707 - if [ "${UID}" -eq 0 ]; then
708 - run chown "${owner}" "${filename}"
709 - fi
710 - run chmod 0664 "${filename}"
711 -}
712 -
713 -generate_netdata_conf() {
714 - local owner="${1}" filename="${2}" url="${3}"
715 -
716 - if [ ! -s "${filename}" ]; then
717 - cat >"${filename}" <<EOFCONF
718 -# netdata can generate its own config.
719 -# Get it with:
720 -#
721 -# wget -O ${filename} "${url}"
722 -#
723 -# or
724 -#
725 -# curl -s -o ${filename} "${url}"
726 -#
727 -EOFCONF
728 - fix_netdata_conf "${owner}"
540 + if [ -s "${path}" ]; then
541 + return 0
542 fi
730 -}
543
732 -download_netdata_conf() {
733 - local owner="${1}" filename="${2}" url="${3}"
734 -
735 - if [ ! -s "${filename}" ]; then
736 - echo >&2
737 - echo >&2 "-------------------------------------------------------------------------------"
738 - echo >&2
544 + if [ -n "$url" ]; then
545 echo >&2 "Downloading default configuration from netdata..."
546 sleep 5
547
742 - # remove a possibly obsolete download
743 - [ -f "${filename}.new" ] && rm "${filename}.new"
548 + # remove a possibly obsolete configuration file
549 + [ -f "${path}.new" ] && rm "${path}.new"
550
551 # disable a proxy to get data from the local netdata
552 export http_proxy=
553 export https_proxy=
554
749 - # try curl
750 - run curl -s -o "${filename}.new" "${url}"
751 - ret=$?
752 -
753 - if [ ${ret} -ne 0 -o ! -s "${filename}.new" ]; then
754 - # try wget
755 - run wget -O "${filename}.new" "${url}"
756 - ret=$?
555 + if command -v curl 1>/dev/null 2>&1; then
556 + run curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${path}.new"
557 + elif command -v wget 1>/dev/null 2>&1; then
558 + run wget -T 15 -O - "${url}" >"${path}.new"
559 fi
560
759 - if [ ${ret} -eq 0 -a -s "${filename}.new" ]; then
760 - run mv "${filename}.new" "${filename}"
761 - run_ok "New configuration saved for you to edit at ${filename}"
561 + if [ -s "${path}.new" ]; then
562 + run mv "${path}.new" "${path}"
563 + run_ok "New configuration saved for you to edit at ${path}"
564 else
763 - [ -f "${filename}.new" ] && rm "${filename}.new"
565 + [ -f "${path}.new" ] && rm "${path}.new"
566 run_failed "Cannnot download configuration from netdata daemon using url '${url}'"
765 -
766 - generate_netdata_conf "${owner}" "${filename}" "${url}"
567 + url=''
568 fi
569 + fi
570
769 - fix_netdata_conf "${owner}"
571 + if [ -z "$url" ]; then
572 + echo "# netdata can generate its own config which is available at 'http://<netdata_ip>/netdata.conf'" >"${path}"
573 + echo "# You can download it with command like: 'wget -O ${path} http://localhost:19999/netdata.conf'" >>"${path}"
574 fi
575 +
576 }
577
773 -# -----------------------------------------------------------------------------
774 -# add netdata user and group
578 +portable_add_user() {
579 + local username="${1}" homedir="${2}"
580
776 -NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody"
777 -NETDATA_ADDED_TO_GROUPS=""
778 -add_netdata_user_and_group() {
779 - local homedir="${1}" g
581 + [ -z "${homedir}" ] && homedir="/tmp"
582 +
583 + # Check if user exists
584 + if cut -d ':' -f 1 </etc/passwd | grep "^${username}$" 1>/dev/null 2>&1; then
585 + echo >&2 "User '${username}' already exists."
586 + return 0
587 + fi
588
781 - if [ ${UID} -eq 0 ]; then
782 - portable_add_group netdata || return 1
783 - portable_add_user netdata "${homedir}" || return 1
589 + echo >&2 "Adding ${username} user account with home ${homedir} ..."
590
785 - for g in ${NETDATA_WANTED_GROUPS}; do
786 - portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}"
787 - done
591 + # shellcheck disable=SC2230
592 + local nologin="$(command -v nologin >/dev/null 2>&1 || echo '/bin/false')"
593
789 - [ ~netdata = / ] && cat <<USERMOD
594 + # Linux
595 + if command -v useradd 1>/dev/null 2>&1; then
596 + run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
597 + fi
598
791 -The netdata user has its home directory set to /
792 -You may want to change it, using this command:
599 + # FreeBSD
600 + if command -v pw 1>/dev/null 2>&1; then
601 + run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
602 + fi
603
794 -# usermod -d "${homedir}" netdata
604 + # BusyBox
605 + if command -v adduser 1>/dev/null 2>&1; then
606 + run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
607 + fi
608
796 -USERMOD
797 - return 0
609 + echo >&2 "Failed to add ${username} user account !"
610 +
611 + return 1
612 +}
613 +
614 +portable_add_group() {
615 + local groupname="${1}"
616 +
617 + # Check if group exist
618 + if cut -d ':' -f 1 </etc/group | grep "^${groupname}$" 1>/dev/null 2>&1; then
619 + echo >&2 "Group '${groupname}' already exists."
620 + return 0
621 + fi
622 +
623 + echo >&2 "Adding ${groupname} user group ..."
624 +
625 + # Linux
626 + if command -v groupadd 1>/dev/null 2>&1; then
627 + run groupadd -r "${groupname}" && return 0
628 fi
629
630 + # FreeBSD
631 + if command -v pw 1>/dev/null 2>&1; then
632 + run pw groupadd "${groupname}" && return 0
633 + fi
634 +
635 + # BusyBox
636 + if command -v addgroup 1>/dev/null 2>&1; then
637 + run addgroup "${groupname}" && return 0
638 + fi
639 +
640 + echo >&2 "Failed to add ${groupname} user group !"
641 return 1
642 }
643 +
644 +portable_add_user_to_group() {
645 + local groupname="${1}" username="${2}"
646 +
647 + # Check if group exist
648 + if ! cut -d ':' -f 1 </etc/group | grep "^${groupname}$" >/dev/null 2>&1; then
649 + echo >&2 "Group '${groupname}' does not exist."
650 + return 1
651 + fi
652 +
653 + # Check if user is in group
654 + if [[ ",$(grep "^${groupname}:" </etc/group | cut -d ':' -f 4)," =~ ,${username}, ]]; then
655 + # username is already there
656 + echo >&2 "User '${username}' is already in group '${groupname}'."
657 + return 0
658 + else
659 + # username is not in group
660 + echo >&2 "Adding ${username} user to the ${groupname} group ..."
661 +
662 + # Linux
663 + if command -v usermod 1>/dev/null 2>&1; then
664 + run usermod -a -G "${groupname}" "${username}" && return 0
665 + fi
666 +
667 + # FreeBSD
668 + if command -v pw 1>/dev/null 2>&1; then
669 + run pw groupmod "${groupname}" -m "${username}" && return 0
670 + fi
671 +
672 + # BusyBox
673 + if command -v addgroup 1>/dev/null 2>&1; then
674 + run addgroup "${username}" "${groupname}" && return 0
675 + fi
676 +
677 + echo >&2 "Failed to add user ${username} to group ${groupname} !"
678 + return 1
679 + fi
680 +}
packaging/makeself/install-or-update.sh
+27 -19
@@ -71,18 +71,27 @@ fi
71 # -----------------------------------------------------------------------------
72 progress "Add user netdata to required user groups"
73
74 -NETDATA_USER="root"
75 -NETDATA_GROUP="root"
76 -add_netdata_user_and_group "/opt/netdata"
77 -if [ $? -eq 0 ]
78 - then
79 - NETDATA_USER="netdata"
80 - NETDATA_GROUP="netdata"
74 +NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody"
75 +NETDATA_ADDED_TO_GROUPS=""
76 +if [ "${UID}" -eq 0 ]; then
77 + if ! portable_add_group netdata; then
78 + run_failed "Failed to add netdata group"
79 + NETDATA_GROUP="root"
80 + fi
81 + if ! portable_add_user netdata "/opt/netdata"; then
82 + run_failed "Failed to add netdata user"
83 + NETDATA_USER="root"
84 + fi
85 +
86 + for g in ${NETDATA_WANTED_GROUPS}; do
87 + # shellcheck disable=SC2086
88 + portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}" || run_failed "Failed to add netdata user to secondary groups"
89 + done
90 else
82 - run_failed "Failed to add netdata user and group"
91 + run_failed "Failed to add netdata user and group"
92 + run_failed "The installer does not run as root."
93 fi
94
85 -
95 # -----------------------------------------------------------------------------
96 progress "Check SSL certificates paths"
97
@@ -206,20 +215,19 @@ fi
215
216 # -----------------------------------------------------------------------------
217
209 -if [ ${STARTIT} -eq 1 ]
210 -then
218 +if [ ${STARTIT} -eq 0 ]; then
219 + create_netdata_conf "/opt/netdata/etc/netdata/netdata.conf"
220 + netdata_banner "is installed now!"
221 +else
222 progress "starting netdata"
223
213 - restart_netdata "/opt/netdata/bin/netdata"
214 - if [ $? -eq 0 ]
215 - then
216 - download_netdata_conf "${NETDATA_USER}:${NETDATA_GROUP}" "/opt/netdata/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
224 + if ! restart_netdata "/opt/netdata/bin/netdata"; then
225 + create_netdata_conf "/opt/netdata/etc/netdata/netdata.conf"
226 netdata_banner "is installed and running now!"
227 else
219 - generate_netdata_conf "${NETDATA_USER}:${NETDATA_GROUP}" "/opt/netdata/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
228 + create_netdata_conf "/opt/netdata/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
229 netdata_banner "is installed now!"
230 fi
222 -else
223 - generate_netdata_conf "${NETDATA_USER}:${NETDATA_GROUP}" "/opt/netdata/etc/netdata/netdata.conf" "http://localhost:19999/netdata.conf"
224 - netdata_banner "is installed now!"
231 fi
232 +run chown "${NETDATA_USER}:${NETDATA_GROUP}" "/opt/netdata/etc/netdata/netdata.conf"
233 +run chmod 0664 "/opt/netdata/etc/netdata/netdata.conf"
packaging/makeself/jobs/50-bash-4.4.18.install.sh
+1 -1
@@ -38,7 +38,7 @@ run ./configure \
38
39
40 run make clean
41 -run make -j${SYSTEM_CPUS}
41 +run make -j$(find_processors)
42
43 cat >examples/loadables/Makefile <<EOF
44 all:
packaging/makeself/jobs/50-curl-7.60.0.install.sh
+1 -1
@@ -25,7 +25,7 @@ run ./configure \
25 run sed -i -e "s/curl_LDFLAGS =/curl_LDFLAGS = -all-static/" src/Makefile
26
27 run make clean
28 -run make -j${SYSTEM_CPUS}
28 +run make -j$(find_processors)
29 run make install
30
31 if [ ${NETDATA_BUILD_WITH_DEBUG} -eq 0 ]
packaging/makeself/jobs/50-fping-4.2.install.sh
+1 -1
@@ -20,7 +20,7 @@ install:
20 EOF
21
22 run make clean
23 -run make -j${SYSTEM_CPUS}
23 +run make -j$(find_processors)
24 run make install
25
26 if [ ${NETDATA_BUILD_WITH_DEBUG} -eq 0 ]