@cryptotaxi247 / netdata-1 / commits / d5be47e47

self-contained uninstaller (#5121)

* self-contained uninstaller * move lifecycle test to BATS * remove all possible netdata service files * add docs

Paweł Krupa committed Jan 16, 2019 at 16:56 UTC d5be47e4760354cbdfccdc2740b7ff091daaf2f9
6 files changed +155 -150
.travis.yml
+1 -1
@@ -41,7 +41,7 @@ jobs:
41 script: ./packaging/docker/build.sh
42 env: DEVEL="true"
43 - name: ubuntu 18.04 + lifecycle
44 - script: docker run -it -v "${PWD}:/code:rw" -w /code "netdata/os-test:ubuntu1804" ./tests/lifecycle.sh
44 + script: docker run -it -v "${PWD}:/code:rw" -w /code "netdata/os-test:ubuntu1804" bats --tap tests/lifecycle.bats
45 - name: CentOS 7
46 script: docker run -it -v "${PWD}:/code:rw" -w /code "netdata/os-test:centos7" ./netdata-installer.sh --dont-wait --dont-start-it --install /tmp
47 - name: CentOS 6
netdata-installer.sh
-6
@@ -889,12 +889,6 @@ SETUID_WARNING
889 fi
890 fi
891
892 -# -----------------------------------------------------------------------------
893 -progress "Create netdata-uninstaller.sh"
894 -
895 -cp ./packaging/installer/netdata-uninstaller.sh netdata-uninstaller.sh
896 -chmod 750 netdata-uninstaller.sh
897 -
892 # -----------------------------------------------------------------------------
893 progress "Basic netdata instructions"
894
packaging/installer/UNINSTALL.md
+14 -30
@@ -1,38 +1,22 @@
1 -# Uninstalling netdata
2 -
3 -## netdata was installed from source (or `kickstart.sh`)
4 -
5 -The script `netdata-installer.sh` generates another script called `netdata-uninstaller.sh`.
6 -
7 -To uninstall netdata, run:
1 +## Uninstalling netdata
2
3 +Our self-contained uninstaller is able to remove netdata installations created with shell installer. It doesn't need any other netdata repository files to be run. All it needs is an .environment file, which is created during installation (with shell installer) and put in ${NETDATA_USER_CONFIG_DIR}/.environment (by default /etc/netdata/.environment). That file contains some parameters which are passed to our installer and which are needed during uninstallation process. Mainly two parameters are needed:
4 ```
10 -cd /path/to/netdata.git
11 -./netdata-uninstaller.sh --yes
5 +NETDATA_PREFIX
6 +NETDATA_ADDED_TO_GROUPS
7 ```
8
14 -The uninstaller will ask you to confirm all deletions.
15 -
16 -## netdata was installed with `kickstart-static64.sh` package
17 -
18 -Stop netdata with one of the following:
9 +A workflow for uninstallation looks like this:
10
20 -- `service netdata stop` (non-systemd systems)
21 -- `systemctl stop netdata` (systemd systems)
22 -
23 -Disable running netdata at startup, with one of the following (based on your distro):
24 -
25 -- `rc-update del netdata`
26 -- `update-rc.d netdata disable`
27 -- `chkconfig netdata off`
28 -- `systemctl disable netdata`
29 -
30 -Delete the netdata files:
11 +1. Find your .environment file
12 +2. If you cannot find that file and would like to uninstall netdata, then create new file with following content:
13 +```
14 +NETDATA_PREFIX="<installation prefix>" # put what you used as a parameter to shell installed `--install` flag. Otherwise it should be empty
15 +NETDATA_ADDED_TO_GROUPS="<additional groups>" # Additional groups for a user running netdata process
16 +```
17 +3. Run ./packaging/installer/netdata-uninstaller.sh --yes --env <path_to_environment_file>
18 +4. Done
19
32 -1. `rm -rf /opt/netdata`
33 -2. `groupdel netdata`
34 -3. `userdel netdata`
35 -4. `rm /etc/logrotate.d/netdata`
36 -5. `rm /etc/systemd/system/netdata.service` or `rm /etc/init.d/netdata`, depending on the distro.
20 +Note: This uninstallation method assumes previous installation with netdata-installer.sh or kickstart script. Currently using it when netdata was installed by a package manager can work or cause unexpected results.
21
22 [![analytics](https://www.google-analytics.com/collect?v=1&aip=1&t=pageview&_s=1&ds=github&dr=https%3A%2F%2Fgithub.com%2Fnetdata%2Fnetdata&dl=https%3A%2F%2Fmy-netdata.io%2Fgithub%2Finstaller%2FUNINSTALL&_u=MAC~&cid=5792dfd7-8dc4-476b-af31-da2fdb9f93d2&tid=UA-64295674-3)]()
packaging/installer/netdata-uninstaller.sh
+113 -86
@@ -3,19 +3,21 @@
3
4 # this script will uninstall netdata
5
6 -# Variables needed by script:
6 +# Variables needed by script and taken from '.environment' file:
7 # - NETDATA_PREFIX
8 # - NETDATA_ADDED_TO_GROUPS
9
10 usage="$(basename "$0") [-h] [-f ] -- program to calculate the answer to life, the universe and everything
11
12 where:
13 - -h show this help text
13 + -e, --env path to environment file (defauls to '/etc/netdata/.environment'
14 -f, --force force uninstallation and do not ask any questions
15 + -h show this help text
16 -y, --yes flag needs to be set to proceed with uninstallation"
17
17 -RM_FLAGS="-i"
18 -INTERACTIVE=1
18 +FILE_REMOVAL_STATUS=0
19 +ENVIRONMENT_FILE="/etc/netdata/.environment"
20 +INTERACTIVITY="-i"
21 YES=0
22 while :; do
23 case "$1" in
@@ -24,14 +26,17 @@ while :; do
26 exit 1
27 ;;
28 -f | --force)
27 - RM_FLAGS="-f"
28 - INTERACTIVE=0
29 + INTERACTIVITY="-f"
30 shift
31 ;;
32 -y | --yes)
33 YES=1
34 shift
35 ;;
36 + -e | --env)
37 + ENVIRONMENT_FILE="$2"
38 + shift 2
39 + ;;
40 -*)
41 echo "$usage" >&2
42 exit 1
@@ -41,102 +46,124 @@ while :; do
46 done
47
48 if [ "$YES" != "1" ]; then
44 - echo >&2 "This script will REMOVE netdata from your system."
45 - echo >&2 "Run it again with --yes to do it."
49 + echo "This script will REMOVE netdata from your system."
50 + echo "Run it again with --yes to do it."
51 exit 1
52 fi
53
49 -#shellcheck source=/dev/null
50 -source packaging/installer/.environment.sh || exit 1
51 -#shellcheck source=/dev/null
52 -source packaging/installer/functions.sh || exit 1
53 -
54 -echo >&2 "Stopping a possibly running netdata..."
55 -for p in $(stop_all_netdata netdata); do run kill "$p"; done
56 -sleep 2
54 +if [[ $EUID -ne 0 ]]; then
55 + echo "This script SHOULD be run as root or otherwise it won't delete all installed components."
56 + key="n"
57 + read -r -s -n 1 -p "Do you want to continue as non-root user [y/n] ? " key
58 + if [ "$key" != "y" ] && [ "$key" != "Y" ]; then
59 + exit 1
60 + fi
61 +fi
62
58 -if [ ! -z "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
59 - # installation prefix was given
60 - if [ $INTERACTIVE -eq 1 ]; then
61 - portable_deletedir_recursively_interactively "${NETDATA_PREFIX}"
63 +function quit_msg() {
64 + echo
65 + if [ "$FILE_REMOVAL_STATUS" -eq 0 ]; then
66 + echo "Something went wrong :("
67 else
63 - run rm -f -R "${NETDATA_PREFIX}"
68 + echo "Netdata files were successfully removed from your system"
69 fi
65 -else
66 - # installation prefix was NOT given
70 +}
71 +
72 +function user_input() {
73 + TEXT="$1"
74 + if [ "${INTERACTIVITY}" == "-i" ]; then
75 + read -r -p "$TEXT" >&2
76 + fi
77 +}
78
68 - if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]; then
69 - echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
70 - run rm ${RM_FLAGS} "${NETDATA_PREFIX}/usr/sbin/netdata"
79 +function rm_file() {
80 + FILE="$1"
81 + if [ -f "${FILE}" ]; then
82 + rm -v ${INTERACTIVITY} "${FILE}"
83 fi
84 +}
85
73 - for dir in "${NETDATA_PREFIX}/etc/netdata" \
74 - "${NETDATA_PREFIX}/usr/share/netdata" \
75 - "${NETDATA_PREFIX}/usr/libexec/netdata" \
76 - "${NETDATA_PREFIX}/var/lib/netdata" \
77 - "${NETDATA_PREFIX}/var/cache/netdata" \
78 - "${NETDATA_PREFIX}/var/log/netdata"
79 - do
80 - if [ $INTERACTIVE -eq 1 ]; then
81 - portable_deletedir_recursively_interactively "${dir}"
82 - else
83 - run rm -f -R "${dir}"
86 +function rm_dir() {
87 + DIR="$1"
88 + if [ -n "$DIR" ] && [ -d "$DIR" ]; then
89 + user_input "Press ENTER to recursively delete directory '$DIR' > "
90 + rm -v -f -R "${DIR}"
91 + fi
92 +}
93 +
94 +netdata_pids() {
95 + local p myns ns
96 + myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
97 + for p in \
98 + $(cat /var/run/netdata.pid 2>/dev/null) \
99 + $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
100 + $(pidof netdata 2>/dev/null); do
101 +
102 + ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)"
103 + #shellcheck disable=SC2002
104 + if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
105 + name="$(cat "/proc/${p}/stat" 2>/dev/null | cut -d '(' -f 2 | cut -d ')' -f 1)"
106 + if [ "${name}" = "netdata" ]; then
107 + echo "${p}"
108 + fi
109 fi
110 done
86 -fi
87 -
88 -if [ -f /etc/logrotate.d/netdata ]; then
89 - echo "Deleting /etc/logrotate.d/netdata ..."
90 - run rm ${RM_FLAGS} /etc/logrotate.d/netdata
91 -fi
111 +}
112
93 -if [ -f /etc/systemd/system/netdata.service ]; then
94 - echo "Deleting /etc/systemd/system/netdata.service ..."
95 - run rm ${RM_FLAGS} /etc/systemd/system/netdata.service
96 -fi
97 -
98 -if [ -f /lib/systemd/system/netdata.service ]; then
99 - echo "Deleting /lib/systemd/system/netdata.service ..."
100 - run rm ${RM_FLAGS} /lib/systemd/system/netdata.service
101 -fi
102 -
103 -if [ -f /etc/init.d/netdata ]; then
104 - echo "Deleting /etc/init.d/netdata ..."
105 - run rm ${RM_FLAGS} /etc/init.d/netdata
106 -fi
113 +trap quit_msg EXIT
114
108 -if [ -f /etc/periodic/daily/netdata-updater ]; then
109 - echo "Deleting /etc/periodic/daily/netdata-updater ..."
110 - run rm ${RM_FLAGS} /etc/periodic/daily/netdata-updater
111 -fi
115 +#shellcheck source=/dev/null
116 +source "${ENVIRONMENT_FILE}" || exit 1
117 +
118 +#### STOP NETDATA
119 +echo "Stopping a possibly running netdata..."
120 +for p in $(netdata_pids); do
121 + i=0
122 + while kill "${p}" 2>/dev/null; do
123 + if [ "$i" -gt 30 ]; then
124 + echo "Forcefully stopping netdata with pid ${p}"
125 + kill -9 "${p}"
126 + sleep 2
127 + break
128 + fi
129 + sleep 1
130 + i=$((i + 1))
131 + done
132 +done
133 +sleep 2
134
113 -if [ -f /etc/cron.daily/netdata-updater ]; then
114 - echo "Deleting /etc/cron.daily/netdata-updater ..."
115 - run rm ${RM_FLAGS} /etc/cron.daily/netdata-updater
135 +#### REMOVE NETDATA FILES
136 +rm_file /etc/logrotate.d/netdata
137 +rm_file /etc/systemd/system/netdata.service
138 +rm_file /lib/systemd/system/netdata.service
139 +rm_file /usr/lib/systemd/system/netdata.service
140 +rm_file /etc/init.d/netdata
141 +rm_file /etc/periodic/daily/netdata-updater
142 +rm_file /etc/cron.daily/netdata-updater
143 +
144 +if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
145 + rm_dir "${NETDATA_PREFIX}"
146 +else
147 + rm_file "/usr/sbin/netdata"
148 + rm_dir "/usr/share/netdata"
149 + rm_dir "/usr/libexec/netdata"
150 + rm_dir "/var/lib/netdata"
151 + rm_dir "/var/cache/netdata"
152 + rm_dir "/var/log/netdata"
153 + rm_dir "/etc/netdata"
154 fi
155
118 -portable_check_user_exists netdata
119 -if [ $? -eq 0 ]; then
120 - echo
121 - echo "You may also want to remove the user netdata"
122 - echo "by running:"
123 - echo " userdel netdata"
124 -fi
156 +FILE_REMOVAL_STATUS=1
157
126 -portable_check_group_exists netdata >/dev/null
127 -if [ $? -eq 0 ]; then
128 - echo
129 - echo "You may also want to remove the group netdata"
130 - echo "by running:"
131 - echo " groupdel netdata"
158 +#### REMOVE NETDATA USER & GROUP
159 +if [ -n "$NETDATA_ADDED_TO_GROUPS" ]; then
160 + user_input "Press ENTER to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' > "
161 + for group in $NETDATA_ADDED_TO_GROUPS; do
162 + gpasswd -d netdata "${group}"
163 + done
164 fi
165
134 -for g in ${NETDATA_ADDED_TO_GROUPS}; do
135 - portable_check_group_exists "$g" >/dev/null
136 - if [ $? -eq 0 ]; then
137 - echo
138 - echo "You may also want to remove the netdata user from the $g group"
139 - echo "by running:"
140 - echo " gpasswd -d netdata $g"
141 - fi
142 -done
166 +user_input "Press ENTER to delete 'netdata' system user > "
167 +userdel -f netdata || :
168 +user_input "Press ENTER to delete 'netdata' system group > "
169 +groupdel -f netdata || :
tests/lifecycle.bats new
+27
@@ -0,0 +1,27 @@
1 +#!/usr/bin/env bats
2 +
3 +INSTALLATION="$BATS_TMPDIR/installation"
4 +ENV="${INSTALLATION}/netdata/etc/netdata/.environment"
5 +
6 +setup() {
7 + if [ ! -f .gitignore ]; then
8 + echo "Run as ./tests/lifecycle/$(basename "$0") from top level directory of git repository"
9 + exit 1
10 + fi
11 +}
12 +
13 +@test "install netdata" {
14 + ./netdata-installer.sh --dont-wait --dont-start-it --auto-update --install "${INSTALLATION}"
15 +}
16 +
17 +@test "update netdata" {
18 + export ENVIRONMENT_FILE="${ENV}"
19 + /etc/cron.daily/netdata-updater
20 + ! grep "new_installation" "${ENV}"
21 +}
22 +
23 +@test "uninstall netdata" {
24 + ./packaging/installer/netdata-uninstaller.sh --yes --force --env "${ENV}"
25 + [ ! -f "${INSTALLATION}/netdata/usr/sbin/netdata" ]
26 + [ ! -f "/etc/cron.daily/netdata-updater" ]
27 +}
tests/lifecycle.sh deleted
-27
@@ -1,27 +0,0 @@
1 -#!/bin/bash
2 -
3 -set -e
4 -
5 -if [ ! -f .gitignore ]
6 -then
7 - echo "Run as ./tests/$(basename "$0") from top level directory of git repository"
8 - exit 1
9 -fi
10 -
11 -WORKSPACE=$(mktemp -d)
12 -cp -r ./ "${WORKSPACE}/"
13 -cd "${WORKSPACE}"
14 -
15 -echo "========= INSTALL ========="
16 -./netdata-installer.sh --dont-wait --dont-start-it --auto-update --install /tmp &>/dev/null
17 -cp netdata-uninstaller.sh /tmp/netdata-uninstaller.sh
18 -ls /tmp
19 -
20 -rm -rf "${WORKSPACE}"
21 -echo "========= UPDATE ========="
22 -ENVIRONMENT_FILE=/tmp/netdata/etc/netdata/.environment /etc/cron.daily/netdata-updater
23 -
24 -#TODO(paulfantom): Enable with #5031
25 -#echo "========= UNINSTALL ========="
26 -#ENVIRONMENT_FILE=/tmp/netdata/etc/netdata/.environment /tmp/netdata-uninstaller.sh --yes --force
27 -#[ -f /tmp/netdata/usr/sbin/netdata ] && exit 1