fix install type in netdata-uninstaller.sh (#12438)
* fix install type in netdata-uninstaller.sh * add function from kickstart to detect install type * add functions from kickstart to create tmp directory * fix
maneamarius committed
Mar 18, 2022 at 12:22 UTC
5137e88debdda61211ead10959dd5b36abe54230
1 file changed
+114
-4
packaging/installer/netdata-uninstaller.sh
+114
-4
@@ -83,7 +83,117 @@ user_input() {
83
fi
84
}
85
86
-if [ -x "$(command -v apt-get)" ]; then
86
+_cannot_use_tmpdir() {
87
+ testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
88
+ ret=0
89
+
90
+ if [ -z "${testfile}" ]; then
91
+ return "${ret}"
92
+ fi
93
+
94
+ if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
95
+ if chmod +x "${testfile}"; then
96
+ if [ "$("${testfile}")" = "SUCCESS" ]; then
97
+ ret=1
98
+ fi
99
+ fi
100
+ fi
101
+
102
+ rm -f "${testfile}"
103
+ return "${ret}"
104
+}
105
+
106
+create_tmp_directory() {
107
+ if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
108
+ if _cannot_use_tmpdir /tmp; then
109
+ if _cannot_use_tmpdir "${PWD}"; then
110
+ fatal "Unable to find a usable temporary directory. Please set \$TMPDIR to a path that is both writable and allows execution of files and try again." F0400
111
+ else
112
+ TMPDIR="${PWD}"
113
+ fi
114
+ else
115
+ TMPDIR="/tmp"
116
+ fi
117
+ fi
118
+
119
+ mktemp -d -t netdata-kickstart-XXXXXXXXXX
120
+}
121
+
122
+tmpdir="$(create_tmp_directory)"
123
+
124
+detect_existing_install() {
125
+ if pkg_installed netdata; then
126
+ ndprefix="/"
127
+ else
128
+ if [ -n "${INSTALL_PREFIX}" ]; then
129
+ searchpath="${INSTALL_PREFIX}/bin:${INSTALL_PREFIX}/sbin:${INSTALL_PREFIX}/usr/bin:${INSTALL_PREFIX}/usr/sbin:${PATH}"
130
+ searchpath="${INSTALL_PREFIX}/netdata/bin:${INSTALL_PREFIX}/netdata/sbin:${INSTALL_PREFIX}/netdata/usr/bin:${INSTALL_PREFIX}/netdata/usr/sbin:${searchpath}"
131
+ else
132
+ searchpath="${PATH}"
133
+ fi
134
+
135
+ ndpath="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
136
+
137
+ if [ -z "$ndpath" ] && [ -x /opt/netdata/bin/netdata ]; then
138
+ ndpath="/opt/netdata/bin/netdata"
139
+ fi
140
+
141
+ if [ -n "${ndpath}" ]; then
142
+ ndprefix="$(dirname "$(dirname "${ndpath}")")"
143
+ fi
144
+
145
+ if echo "${ndprefix}" | grep -Eq '/usr$'; then
146
+ ndprefix="$(dirname "${ndprefix}")"
147
+ fi
148
+ fi
149
+
150
+ if [ -n "${ndprefix}" ]; then
151
+ typefile="${ndprefix}/etc/netdata/.install-type"
152
+ envfile="${ndprefix}/etc/netdata/.environment"
153
+ if [ -r "${typefile}" ]; then
154
+ ${ROOTCMD} sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
155
+ # shellcheck disable=SC1090,SC1091
156
+ . "${tmpdir}/install-type"
157
+ else
158
+ INSTALL_TYPE="unknown"
159
+ fi
160
+
161
+ if [ "${INSTALL_TYPE}" = "unknown" ] || [ "${INSTALL_TYPE}" = "custom" ]; then
162
+ if [ -r "${envfile}" ]; then
163
+ ${ROOTCMD} sh -c "cat \"${envfile}\" > \"${tmpdir}/environment\""
164
+ # shellcheck disable=SC1091
165
+ . "${tmpdir}/environment"
166
+ if [ -n "${NETDATA_IS_STATIC_INSTALL}" ]; then
167
+ if [ "${NETDATA_IS_STATIC_INSTALL}" = "yes" ]; then
168
+ INSTALL_TYPE="legacy-static"
169
+ else
170
+ INSTALL_TYPE="legacy-build"
171
+ fi
172
+ fi
173
+ fi
174
+ fi
175
+ fi
176
+}
177
+
178
+pkg_installed() {
179
+ case "${DISTRO_COMPAT_NAME}" in
180
+ debian|ubuntu)
181
+ dpkg-query --show --showformat '${Status}' "${1}" 2>&1 | cut -f 1 -d ' ' | grep -q '^install$'
182
+ return $?
183
+ ;;
184
+ centos|fedora|opensuse|ol)
185
+ rpm -q "${1}" > /dev/null 2>&1
186
+ return $?
187
+ ;;
188
+ *)
189
+ return 1
190
+ ;;
191
+ esac
192
+}
193
+
194
+detect_existing_install
195
+
196
+if [ -x "$(command -v apt-get)" ] && [ "${INSTALL_TYPE}" = "binpkg-deb" ]; then
197
if dpkg -s netdata > /dev/null; then
198
echo "Found netdata native installation"
199
if user_input "Do you want to remove netdata? "; then
@@ -101,7 +211,7 @@ if [ -x "$(command -v apt-get)" ]; then
211
fi
212
exit 0
213
fi
104
-elif [ -x "$(command -v dnf)" ]; then
214
+elif [ -x "$(command -v dnf)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
215
if rpm -q netdata > /dev/null; then
216
echo "Found netdata native installation."
217
if user_input "Do you want to remove netdata? "; then
@@ -119,7 +229,7 @@ elif [ -x "$(command -v dnf)" ]; then
229
fi
230
exit 0
231
fi
122
-elif [ -x "$(command -v yum)" ]; then
232
+elif [ -x "$(command -v yum)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
233
if rpm -q netdata > /dev/null; then
234
echo "Found netdata native installation."
235
if user_input "Do you want to remove netdata? "; then
@@ -137,7 +247,7 @@ elif [ -x "$(command -v yum)" ]; then
247
fi
248
exit 0
249
fi
140
-elif [ -x "$(command -v zypper)" ]; then
250
+elif [ -x "$(command -v zypper)" ] && [ "${INSTALL_TYPE}" = "binpkg-rpm" ]; then
251
if [ "${FLAG}" = "-y" ]; then
252
FLAG=-n
253
fi