make netdata-uninstaller.sh POSIX compatibility and add --uninstall flag… (#12195)
* make netdata-uninstaller POSIX compatibility and add --uninstall flag in kickstart.sh * disable shellcheck * update * fix shellcheck * update kickstart.sh
maneamarius committed
Mar 1, 2022 at 14:24 UTC
61681f7be607455056847c3480d405f03a4b8450
2 files changed
+182
-43
packaging/installer/kickstart.sh
+38
-4
@@ -80,6 +80,7 @@ USAGE: kickstart.sh [options]
80
--claim-only If there is an existing install, only try to claim it, not update it.
81
--claim-* Specify other options for the claiming script.
82
--no-cleanup Don't do any cleanup steps. This is intended to help with debugging the installer.
83
+ --uninstall Uninstall netdata.
84
85
Additionally, this script may use the following environment variables:
86
@@ -265,14 +266,14 @@ run_failed() {
266
267
ESCAPED_PRINT_METHOD=
268
# shellcheck disable=SC3050
268
-if printf "%q " test > /dev/null 2>&1; then
269
+if printf "%s " test > /dev/null 2>&1; then
270
ESCAPED_PRINT_METHOD="printfq"
271
fi
272
273
escaped_print() {
274
if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
275
# shellcheck disable=SC3050
275
- printf "%q " "${@}"
276
+ printf "%s " "${@}"
277
else
278
printf "%s" "${*}"
279
fi
@@ -532,6 +533,27 @@ update() {
533
fi
534
}
535
536
+uninstall() {
537
+ get_system_info
538
+ detect_existing_install
539
+
540
+ if [ $INTERACTIVE = 0 ]; then
541
+ FLAGS="--yes --force"
542
+ else
543
+ FLAGS="--yes"
544
+ fi
545
+
546
+ if [ -f "${INSTALL_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh" ]; then
547
+ echo "Found existing netdata-uninstaller. Running it.."
548
+ ${ROOTCMD} "${INSTALL_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh" $FLAGS
549
+ else
550
+ echo "Downloading netdata-uninstaller ..."
551
+ wget https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-uninstaller.sh -O "${tmpdir}/netdata-uninstaller.sh"
552
+ chmod +x "${tmpdir}/netdata-uninstaller.sh"
553
+ ${ROOTCMD} "${tmpdir}/netdata-uninstaller.sh" $FLAGS
554
+ fi
555
+}
556
+
557
detect_existing_install() {
558
if pkg_installed netdata; then
559
ndprefix="/"
@@ -564,7 +586,7 @@ detect_existing_install() {
586
typefile="${ndprefix}/etc/netdata/.install-type"
587
if [ -r "${typefile}" ]; then
588
${ROOTCMD} sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
567
- # shellcheck disable=SC1091
589
+ # shellcheck disable=SC1090,SC1091
590
. "${tmpdir}/install-type"
591
else
592
INSTALL_TYPE="unknown"
@@ -572,6 +594,8 @@ detect_existing_install() {
594
fi
595
596
INSTALL_PREFIX="${ndprefix}"
597
+
598
+ export INSTALL_PREFIX="${INSTALL_PREFIX}"
599
}
600
601
handle_existing_install() {
@@ -1091,7 +1115,7 @@ try_static_install() {
1115
if [ -f "${install_type_file}" ]; then
1116
${ROOTCMD} sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
1117
${ROOTCMD} chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
1094
- # shellcheck disable=SC1091
1118
+ # shellcheck disable=SC1090,SC1091
1119
. "${tmpdir}/install-type"
1120
cat > "${tmpdir}/install-type" <<- EOF
1121
INSTALL_TYPE='kickstart-static'
@@ -1365,6 +1389,9 @@ while [ -n "${1}" ]; do
1389
INSTALL_PREFIX="${2}"
1390
shift 1
1391
;;
1392
+ "--uninstall")
1393
+ ACTION="uninstall"
1394
+ ;;
1395
"--native-only")
1396
NETDATA_ONLY_NATIVE=1
1397
NETDATA_ONLY_STATIC=0
@@ -1423,6 +1450,13 @@ confirm_root_support
1450
get_system_info
1451
confirm_install_prefix
1452
1453
+if [ "${ACTION}" = "uninstall" ]; then
1454
+ uninstall
1455
+ cleanup
1456
+ trap - EXIT
1457
+ exit 0
1458
+fi
1459
+
1460
tmpdir="$(create_tmp_directory)"
1461
progress "Using ${tmpdir} as a temporary directory."
1462
cd "${tmpdir}" || exit 1
packaging/installer/netdata-uninstaller.sh
+144
-39
@@ -1,4 +1,4 @@
1
-#!/usr/bin/env bash
1
+#!/bin/sh
2
#
3
# This is the netdata uninstaller script
4
#
@@ -21,6 +21,7 @@ where:
21
22
FILE_REMOVAL_STATUS=0
23
ENVIRONMENT_FILE="/etc/netdata/.environment"
24
+# shellcheck disable=SC2034
25
INTERACTIVITY="-i"
26
YES=0
27
while :; do
@@ -35,6 +36,7 @@ while :; do
36
;;
37
-y | --yes)
38
YES=1
39
+ FLAG=-y
40
shift
41
;;
42
-e | --env)
@@ -55,15 +57,109 @@ if [ "$YES" != "1" ]; then
57
exit 1
58
fi
59
58
-if [[ $EUID -ne 0 ]]; then
60
+if [ "$(id -u)" -ne 0 ]; then
61
echo >&2 "This script SHOULD be run as root or otherwise it won't delete all installed components."
62
key="n"
61
- read -r -s -n 1 -p "Do you want to continue as non-root user [y/n] ? " key
63
+ read -r 1 -p "Do you want to continue as non-root user [y/n] ? " key
64
if [ "$key" != "y" ] && [ "$key" != "Y" ]; then
65
exit 1
66
fi
67
fi
68
69
+user_input() {
70
+ if [ "${INTERACTIVITY}" = "-i" ]; then
71
+ TEXT="$1 [y/n]"
72
+
73
+ while true; do
74
+ echo "$TEXT"
75
+ read -r yn
76
+
77
+ case "$yn" in
78
+ [Yy]*) return 0;;
79
+ [Nn]*) return 1;;
80
+ *) echo "Please answer yes or no.";;
81
+ esac
82
+ done
83
+ fi
84
+}
85
+
86
+if [ -x "$(command -v apt-get)" ]; then
87
+ if dpkg -s netdata > /dev/null; then
88
+ echo "Found netdata native installation"
89
+ if user_input "Do you want to remove netdata? "; then
90
+ apt-get remove netdata ${FLAG}
91
+ fi
92
+ if dpkg -s netdata-repo-edge > /dev/null; then
93
+ if user_input "Do you want to remove netdata-repo-edge? "; then
94
+ apt-get remove netdata-repo-edge ${FLAG}
95
+ fi
96
+ fi
97
+ if dpkg -s netdata-repo > /dev/null; then
98
+ if user_input "Do you want to remove netdata-repo? "; then
99
+ apt-get remove netdata-repo ${FLAG}
100
+ fi
101
+ fi
102
+ exit 0
103
+ fi
104
+elif [ -x "$(command -v dnf)" ]; then
105
+ if rpm -q netdata > /dev/null; then
106
+ echo "Found netdata native installation."
107
+ if user_input "Do you want to remove netdata? "; then
108
+ dnf remove netdata ${FLAG}
109
+ fi
110
+ if rpm -q netdata-repo-edge > /dev/null; then
111
+ if user_input "Do you want to remove netdata-repo-edge? "; then
112
+ dnf remove netdata-repo-edge ${FLAG}
113
+ fi
114
+ fi
115
+ if rpm -q netdata-repo > /dev/null; then
116
+ if user_input "Do you want to remove netdata-repo? "; then
117
+ dnf remove netdata-repo ${FLAG}
118
+ fi
119
+ fi
120
+ exit 0
121
+ fi
122
+elif [ -x "$(command -v yum)" ]; then
123
+ if rpm -q netdata > /dev/null; then
124
+ echo "Found netdata native installation."
125
+ if user_input "Do you want to remove netdata? "; then
126
+ yum remove netdata ${FLAG}
127
+ fi
128
+ if rpm -q netdata-repo-edge > /dev/null; then
129
+ if user_input "Do you want to remove netdata-repo-edge? "; then
130
+ yum remove netdata-repo-edge ${FLAG}
131
+ fi
132
+ fi
133
+ if rpm -q netdata-repo > /dev/null; then
134
+ if user_input "Do you want to remove netdata-repo? "; then
135
+ yum remove netdata-repo ${FLAG}
136
+ fi
137
+ fi
138
+ exit 0
139
+ fi
140
+elif [ -x "$(command -v zypper)" ]; then
141
+ if [ "${FLAG}" = "-y" ]; then
142
+ FLAG=-n
143
+ fi
144
+ if zypper search -i netdata > /dev/null; then
145
+ echo "Found netdata native installation."
146
+ if user_input "Do you want to remove netdata? "; then
147
+ zypper ${FLAG} remove netdata
148
+ fi
149
+ if zypper search -i netdata-repo-edge > /dev/null; then
150
+ if user_input "Do you want to remove netdata-repo-edge? "; then
151
+ zypper ${FLAG} remove netdata-repo-edge
152
+ fi
153
+ fi
154
+ if zypper search -i netdata-repo > /dev/null; then
155
+ if user_input "Do you want to remove netdata-repo? "; then
156
+ zypper ${FLAG} remove netdata-repo
157
+ fi
158
+ fi
159
+ exit 0
160
+ fi
161
+fi
162
+
163
# -----------------------------------------------------------------------------
164
# portable service command
165
@@ -72,7 +168,8 @@ rcservice_cmd="$(command -v rc-service 2> /dev/null)"
168
systemctl_cmd="$(command -v systemctl 2> /dev/null)"
169
service() {
170
75
- local cmd="${1}" action="${2}"
171
+ cmd="${1}"
172
+ action="${2}"
173
174
if [ -n "${systemctl_cmd}" ]; then
175
run "${systemctl_cmd}" "${action}" "${cmd}"
@@ -127,12 +224,12 @@ run_failed() {
224
}
225
226
ESCAPED_PRINT_METHOD=
130
-if printf "%q " test > /dev/null 2>&1; then
227
+if printf "%s " test > /dev/null 2>&1; then
228
ESCAPED_PRINT_METHOD="printfq"
229
fi
230
escaped_print() {
231
if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
135
- printf "%q " "${@}"
232
+ printf "%s " "${@}"
233
else
234
printf "%s" "${*}"
235
fi
@@ -141,9 +238,10 @@ escaped_print() {
238
239
run_logfile="/dev/null"
240
run() {
144
- local user="${USER--}" dir="${PWD}" info info_console
241
+ user="${USER--}"
242
+ dir="${PWD}"
243
146
- if [ "${UID}" = "0" ]; then
244
+ if [ "$(id -u)" = "0" ]; then
245
info="[root ${dir}]# "
246
info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
247
else
@@ -163,7 +261,7 @@ run() {
261
262
"${@}"
263
166
- local ret=$?
264
+ ret=$?
265
if [ ${ret} -ne 0 ]; then
266
run_failed
267
printf >> "${run_logfile}" "FAILED with exit code %s\n" "${ret}"
@@ -176,7 +274,7 @@ run() {
274
}
275
276
portable_del_group() {
179
- local groupname="${1}"
277
+ groupname="${1}"
278
279
# Check if group exist
280
echo >&2 "Removing ${groupname} user group ..."
@@ -206,7 +304,11 @@ portable_del_group() {
304
}
305
306
issystemd() {
209
- local pids p myns ns systemctl
307
+ pids=''
308
+ p=''
309
+ myns=''
310
+ ns=''
311
+ systemctl=''
312
313
# if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
314
if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
@@ -240,7 +342,7 @@ issystemd() {
342
}
343
344
portable_del_user() {
243
- local username="${1}"
345
+ username="${1}"
346
echo >&2 "Deleting ${username} user account ..."
347
348
# Linux
@@ -258,7 +360,8 @@ portable_del_user() {
360
}
361
362
portable_del_user_from_group() {
261
- local groupname="${1}" username="${2}"
363
+ groupname="${1}"
364
+ username="${2}"
365
366
# username is not in group
367
echo >&2 "Deleting ${username} user from ${groupname} group ..."
@@ -296,30 +399,25 @@ quit_msg() {
399
fi
400
}
401
299
-user_input() {
300
- TEXT="$1"
301
- if [ "${INTERACTIVITY}" = "-i" ]; then
302
- read -r -p "$TEXT" >&2
303
- fi
304
-}
305
-
402
rm_file() {
403
FILE="$1"
404
if [ -f "${FILE}" ]; then
309
- run rm -v ${INTERACTIVITY} "${FILE}"
405
+ if user_input "Do you want to delete this file '$FILE' ? "; then
406
+ run rm -v "${FILE}"
407
+ fi
408
fi
409
}
410
411
rm_dir() {
412
DIR="$1"
413
if [ -n "$DIR" ] && [ -d "$DIR" ]; then
316
- user_input "Press ENTER to recursively delete directory '$DIR' > "
317
- run rm -v -f -R "${DIR}"
414
+ if user_input "Do you want to delete this directory '$DIR' ? "; then
415
+ run rm -v -f -R "${DIR}"
416
+ fi
417
fi
418
}
419
420
safe_pidof() {
322
- local pidof_cmd
421
pidof_cmd="$(command -v pidof 2> /dev/null)"
422
if [ -n "${pidof_cmd}" ]; then
423
${pidof_cmd} "${@}"
@@ -345,7 +443,9 @@ pidisnetdata() {
443
}
444
445
stop_netdata_on_pid() {
348
- local pid="${1}" ret=0 count=0
446
+ pid="${1}"
447
+ ret=0
448
+ count=0
449
450
pidisnetdata "${pid}" || return 0
451
@@ -386,7 +486,8 @@ stop_netdata_on_pid() {
486
}
487
488
netdata_pids() {
389
- local p myns ns
489
+ p=''
490
+ ns=''
491
492
myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
493
@@ -403,9 +504,9 @@ netdata_pids() {
504
}
505
506
stop_all_netdata() {
406
- local p
507
+ p=''
508
408
- if [ "${UID}" -eq 0 ]; then
509
+ if [ "$(id -u)" -eq 0 ]; then
510
uname="$(uname 2> /dev/null)"
511
512
# Any of these may fail, but we need to not bail if they do.
@@ -428,7 +529,7 @@ stop_all_netdata() {
529
fi
530
fi
531
431
- if [ -n "$(netdata_pids)" ] && [ -n "$(builtin type -P netdatacli)" ]; then
532
+ if [ -n "$(netdata_pids)" ] && [ -n "$(command -v netdatacli)" ]; then
533
netdatacli shutdown-agent
534
sleep 20
535
fi
@@ -441,8 +542,9 @@ stop_all_netdata() {
542
543
trap quit_msg EXIT
544
444
-#shellcheck source=/dev/null
445
-source "${ENVIRONMENT_FILE}" || exit 1
545
+# shellcheck source=/dev/null
546
+# shellcheck disable=SC1090
547
+. "${INSTALL_PREFIX}${ENVIRONMENT_FILE}" || exit 1
548
549
#### STOP NETDATA
550
echo >&2 "Stopping a possibly running netdata..."
@@ -484,16 +586,19 @@ FILE_REMOVAL_STATUS=1
586
587
#### REMOVE NETDATA USER FROM ADDED GROUPS
588
if [ -n "$NETDATA_ADDED_TO_GROUPS" ]; then
487
- user_input "Press ENTER to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' > "
488
- for group in $NETDATA_ADDED_TO_GROUPS; do
489
- portable_del_user_from_group "${group}" "netdata"
490
- done
589
+ if user_input "Do you want to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' ? "; then
590
+ for group in $NETDATA_ADDED_TO_GROUPS; do
591
+ portable_del_user_from_group "${group}" "netdata"
592
+ done
593
+ fi
594
fi
595
596
#### REMOVE USER
494
-user_input "Press ENTER to delete 'netdata' system user > "
495
-portable_del_user "netdata" || :
597
+if user_input "Do you want to delete 'netdata' system user ? "; then
598
+ portable_del_user "netdata" || :
599
+fi
600
601
### REMOVE GROUP
498
-user_input "Press ENTER to delete 'netdata' system group > "
499
-portable_del_group "netdata" || :
602
+if user_input "Do you want to delete 'netdata' system group ? "; then
603
+ portable_del_group "netdata" || :
604
+fi