netdata/packaging: install/uninstall fixes for macOS case (#6045)
* netdata/packaging: install/uninstall fixes for macOS case 1) fix add/remove user and add/remove group to handle when running on mac 2) fix uninstaller to be more portable (function declarations that wont work everywhere) 3) add some portability code in the uninstaller, for add/remove user. Previously we just attempd a blind delete for linux 4) colorize uninstaller a little bit * netdata/packaging: remove unused, as per coday (really..?)
Paul Emm. Katsoulakis committed
May 19, 2019 at 23:37 UTC
7ab746008904f4c68a43d14753b55656dfcc2d01
3 files changed
+207
-24
netdata-installer.sh
+7
-2
@@ -515,20 +515,24 @@ progress "Fix generated files permissions"
515
run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \;
516
517
# -----------------------------------------------------------------------------
518
-progress "Add user netdata to required user groups"
518
+progress "Creating standard user and groups for netdata"
519
520
NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody"
521
NETDATA_ADDED_TO_GROUPS=""
522
if [ "${UID}" -eq 0 ]; then
523
+ progress "Adding group 'netdata'"
524
portable_add_group netdata || :
525
+
526
+ progress "Adding user 'netdata'"
527
portable_add_user netdata "${NETDATA_PREFIX}/var/lib/netdata" || :
528
529
+ progress "Assign user 'netdata' to required groups"
530
for g in ${NETDATA_WANTED_GROUPS}; do
531
# shellcheck disable=SC2086
532
portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}"
533
done
534
else
531
- run_failed "The installer does not run as root."
535
+ run_failed "The installer does not run as root. Nothing to do for user and groups"
536
fi
537
538
# -----------------------------------------------------------------------------
@@ -567,6 +571,7 @@ else
571
fi
572
NETDATA_GROUP="$(id -g -n "${NETDATA_USER}")"
573
[ -z "${NETDATA_GROUP}" ] && NETDATA_GROUP="${NETDATA_USER}"
574
+echo >&2 "Netdata user and group is finally set to: ${NETDATA_USER}/${NETDATA_GROUP}"
575
576
# the owners of the web files
577
NETDATA_WEB_USER="$(config_option "web" "web files owner" "${NETDATA_USER}")"
packaging/installer/functions.sh
+14
@@ -606,6 +606,11 @@ portable_add_user() {
606
run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
607
fi
608
609
+ # mac OS
610
+ if command -v sysadminctl 1> /dev/null 2>&1; then
611
+ run sysadminctl -addUser ${username} && return 0
612
+ fi
613
+
614
echo >&2 "Failed to add ${username} user account !"
615
616
return 1
@@ -637,6 +642,11 @@ portable_add_group() {
642
run addgroup "${groupname}" && return 0
643
fi
644
645
+ # mac OS
646
+ if command -v dseditgroup 1> /dev/null 2>&1; then
647
+ dseditgroup -o create "${groupname}" && return 0
648
+ fi
649
+
650
echo >&2 "Failed to add ${groupname} user group !"
651
return 1
652
}
@@ -674,6 +684,10 @@ portable_add_user_to_group() {
684
run addgroup "${username}" "${groupname}" && return 0
685
fi
686
687
+ # mac OS
688
+ if command -v dseditgroup 1> /dev/null 2>&1; then
689
+ dseditgroup -u "${username}" "${groupname}" && return 0
690
+ fi
691
echo >&2 "Failed to add user ${username} to group ${groupname} !"
692
return 1
693
fi
packaging/installer/netdata-uninstaller.sh
+186
-22
@@ -1,11 +1,15 @@
1
#!/usr/bin/env bash
2
#shellcheck disable=SC2181
3
-
4
-# this script will uninstall netdata
5
-
3
+#
4
+# This is the netdata uninstaller script
5
# Variables needed by script and taken from '.environment' file:
6
# - NETDATA_PREFIX
7
# - NETDATA_ADDED_TO_GROUPS
8
+#
9
+# Copyright: SPDX-License-Identifier: GPL-3.0-or-later
10
+#
11
+# Author: Paul Emm. Katsoulakis <paul@netdata.cloud>
12
+#
13
14
usage="$(basename "$0") [-h] [-f ] -- program to calculate the answer to life, the universe and everything
15
@@ -46,13 +50,13 @@ while :; do
50
done
51
52
if [ "$YES" != "1" ]; then
49
- echo "This script will REMOVE netdata from your system."
50
- echo "Run it again with --yes to do it."
53
+ echo >&2 "This script will REMOVE netdata from your system."
54
+ echo >&2 "Run it again with --yes to do it."
55
exit 1
56
fi
57
58
if [[ $EUID -ne 0 ]]; then
55
- echo "This script SHOULD be run as root or otherwise it won't delete all installed components."
59
+ echo >&2 "This script SHOULD be run as root or otherwise it won't delete all installed components."
60
key="n"
61
read -r -s -n 1 -p "Do you want to continue as non-root user [y/n] ? " key
62
if [ "$key" != "y" ] && [ "$key" != "Y" ]; then
@@ -60,34 +64,191 @@ if [[ $EUID -ne 0 ]]; then
64
fi
65
fi
66
63
-function quit_msg() {
67
+# -----------------------------------------------------------------------------
68
+
69
+setup_terminal() {
70
+ TPUT_RESET=""
71
+ TPUT_YELLOW=""
72
+ TPUT_WHITE=""
73
+ TPUT_BGRED=""
74
+ TPUT_BGGREEN=""
75
+ TPUT_BOLD=""
76
+ TPUT_DIM=""
77
+
78
+ # Is stderr on the terminal? If not, then fail
79
+ test -t 2 || return 1
80
+
81
+ if command -v tput 1>/dev/null 2>&1; then
82
+ if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
83
+ # Enable colors
84
+ TPUT_RESET="$(tput sgr 0)"
85
+ TPUT_YELLOW="$(tput setaf 3)"
86
+ TPUT_WHITE="$(tput setaf 7)"
87
+ TPUT_BGRED="$(tput setab 1)"
88
+ TPUT_BGGREEN="$(tput setab 2)"
89
+ TPUT_BOLD="$(tput bold)"
90
+ TPUT_DIM="$(tput dim)"
91
+ fi
92
+ fi
93
+
94
+ return 0
95
+}
96
+setup_terminal || echo >/dev/null
97
+
98
+run_ok() {
99
+ printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n"
100
+}
101
+
102
+run_failed() {
103
+ printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n"
104
+}
105
+
106
+ESCAPED_PRINT_METHOD=
107
+printf "%q " test >/dev/null 2>&1
108
+[ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq"
109
+escaped_print() {
110
+ if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
111
+ printf "%q " "${@}"
112
+ else
113
+ printf "%s" "${*}"
114
+ fi
115
+ return 0
116
+}
117
+
118
+run_logfile="/dev/null"
119
+run() {
120
+ local user="${USER--}" dir="${PWD}" info info_console
121
+
122
+ if [ "${UID}" = "0" ]; then
123
+ info="[root ${dir}]# "
124
+ info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
125
+ else
126
+ info="[${user} ${dir}]$ "
127
+ info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
128
+ fi
129
+
130
+ printf >>"${run_logfile}" "${info}"
131
+ escaped_print >>"${run_logfile}" "${@}"
132
+ printf >>"${run_logfile}" " ... "
133
+
134
+ printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}"
135
+ escaped_print >&2 "${@}"
136
+ printf >&2 "${TPUT_RESET}\n"
137
+
138
+ "${@}"
139
+
140
+ local ret=$?
141
+ if [ ${ret} -ne 0 ]; 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
+}
151
+
152
+portable_del_group() {
153
+ local groupname="${1}"
154
+
155
+ # Check if group exist
156
+ echo >&2 "Removing ${groupname} user group ..."
157
+
158
+ # Linux
159
+ if command -v groupdel 1>/dev/null 2>&1; then
160
+ run groupdel -f "${groupname}" && return 0
161
+ fi
162
+
163
+ # mac OS
164
+ if command -v dseditgroup 1> /dev/null 2>&1; then
165
+ if dseditgroup -o read netdata 1> /dev/null 2>&1; then
166
+ run dseditgroup -o delete "${groupname}" && return 0
167
+ else
168
+ echo >&2 "Could not find group ${groupname}, nothing to do"
169
+ fi
170
+ fi
171
+
172
+ echo >&2 "Group ${groupname} was not automatically removed, you might have to remove it manually"
173
+ return 1
174
+}
175
+
176
+portable_del_user() {
177
+ local username="${1}"
178
+ echo >&2 "Deleting ${username} user account ..."
179
+
180
+ # Linux
181
+ if command -v userdel 1>/dev/null 2>&1; then
182
+ run userdel -f "${username}" && return 0
183
+ fi
184
+
185
+ # mac OS
186
+ if command -v sysadminctl 1>/dev/null 2>&1; then
187
+ run sysadminctl -deleteUser "${username}" && return 0
188
+ fi
189
+
190
+ echo >&2 "User ${username} could not be deleted from system, you might have to remove it manually"
191
+ return 1
192
+}
193
+
194
+portable_del_user_from_group() {
195
+ local groupname="${1}" username="${2}"
196
+
197
+ # username is not in group
198
+ echo >&2 "Deleting ${username} user from ${groupname} group ..."
199
+
200
+ # Linux
201
+ if command -v gpasswd 1>/dev/null 2>&1; then
202
+ run gpasswd -d "netdata" "${group}" && return 0
203
+ fi
204
+
205
+ # FreeBSD
206
+ if command -v pw 1>/dev/null 2>&1; then
207
+ run pw groupmod "${groupname}" -d "${username}" && return 0
208
+ fi
209
+
210
+ # BusyBox
211
+ if command -v delgroup 1>/dev/null 2>&1; then
212
+ run delgroup "${username}" "${groupname}" && return 0
213
+ fi
214
+
215
+ # mac OS
216
+ if command -v dseditgroup 1> /dev/null 2>&1; then
217
+ run dseditgroup -o delete -u "${username}" "${groupname}" && return 0
218
+ fi
219
+
220
+ echo >&2 "Failed to delete user ${username} from group ${groupname} !"
221
+ return 1
222
+}
223
+
224
+quit_msg() {
225
echo
226
if [ "$FILE_REMOVAL_STATUS" -eq 0 ]; then
66
- echo "Something went wrong :("
227
+ echo >&2 "Something went wrong :("
228
else
68
- echo "Netdata files were successfully removed from your system"
229
+ echo >&2 "Netdata files were successfully removed from your system"
230
fi
231
}
232
72
-function user_input() {
233
+user_input() {
234
TEXT="$1"
235
if [ "${INTERACTIVITY}" == "-i" ]; then
236
read -r -p "$TEXT" >&2
237
fi
238
}
239
79
-function rm_file() {
240
+rm_file() {
241
FILE="$1"
242
if [ -f "${FILE}" ]; then
82
- rm -v ${INTERACTIVITY} "${FILE}"
243
+ run rm -v ${INTERACTIVITY} "${FILE}"
244
fi
245
}
246
86
-function rm_dir() {
247
+rm_dir() {
248
DIR="$1"
249
if [ -n "$DIR" ] && [ -d "$DIR" ]; then
250
user_input "Press ENTER to recursively delete directory '$DIR' > "
90
- rm -v -f -R "${DIR}"
251
+ run rm -v -f -R "${DIR}"
252
fi
253
}
254
@@ -116,14 +277,14 @@ trap quit_msg EXIT
277
source "${ENVIRONMENT_FILE}" || exit 1
278
279
#### STOP NETDATA
119
-echo "Stopping a possibly running netdata..."
280
+echo >&2 "Stopping a possibly running netdata..."
281
for p in $(netdata_pids); do
282
i=0
283
while kill "${p}" 2>/dev/null; do
284
if [ "$i" -gt 30 ]; then
124
- echo "Forcefully stopping netdata with pid ${p}"
125
- kill -9 "${p}"
126
- sleep 2
285
+ echo >&2 "Forcefully stopping netdata with pid ${p}"
286
+ run kill -9 "${p}"
287
+ run sleep 2
288
break
289
fi
290
sleep 1
@@ -155,15 +316,18 @@ fi
316
317
FILE_REMOVAL_STATUS=1
318
158
-#### REMOVE NETDATA USER & GROUP
319
+#### REMOVE NETDATA USER FROM ADDED GROUPS
320
if [ -n "$NETDATA_ADDED_TO_GROUPS" ]; then
321
user_input "Press ENTER to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' > "
322
for group in $NETDATA_ADDED_TO_GROUPS; do
162
- gpasswd -d netdata "${group}"
323
+ portable_del_user_from_group "${group}" "netdata"
324
done
325
fi
326
327
+#### REMOVE USER
328
user_input "Press ENTER to delete 'netdata' system user > "
167
-userdel -f netdata || :
329
+portable_del_user "netdata" || :
330
+
331
+### REMOVE GROUP
332
user_input "Press ENTER to delete 'netdata' system group > "
169
-groupdel -f netdata || :
333
+portable_del_group "netdata" || :