0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-28 14:42:31 +00:00

self-contained uninstaller ()

* self-contained uninstaller

* move lifecycle test to BATS

* remove all possible netdata service files

* add docs
This commit is contained in:
Paweł Krupa 2019-01-16 16:56:18 +01:00 committed by GitHub
parent 9c4f5fbb3e
commit d5be47e476
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 162 additions and 157 deletions

View file

@ -41,7 +41,7 @@ jobs:
script: ./packaging/docker/build.sh
env: DEVEL="true"
- name: ubuntu 18.04 + lifecycle
script: docker run -it -v "${PWD}:/code:rw" -w /code "netdata/os-test:ubuntu1804" ./tests/lifecycle.sh
script: docker run -it -v "${PWD}:/code:rw" -w /code "netdata/os-test:ubuntu1804" bats --tap tests/lifecycle.bats
- name: CentOS 7
script: docker run -it -v "${PWD}:/code:rw" -w /code "netdata/os-test:centos7" ./netdata-installer.sh --dont-wait --dont-start-it --install /tmp
- name: CentOS 6

View file

@ -889,12 +889,6 @@ SETUID_WARNING
fi
fi
# -----------------------------------------------------------------------------
progress "Create netdata-uninstaller.sh"
cp ./packaging/installer/netdata-uninstaller.sh netdata-uninstaller.sh
chmod 750 netdata-uninstaller.sh
# -----------------------------------------------------------------------------
progress "Basic netdata instructions"

View file

@ -1,38 +1,22 @@
# Uninstalling netdata
## netdata was installed from source (or `kickstart.sh`)
The script `netdata-installer.sh` generates another script called `netdata-uninstaller.sh`.
To uninstall netdata, run:
## Uninstalling netdata
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:
```
cd /path/to/netdata.git
./netdata-uninstaller.sh --yes
NETDATA_PREFIX
NETDATA_ADDED_TO_GROUPS
```
The uninstaller will ask you to confirm all deletions.
A workflow for uninstallation looks like this:
## netdata was installed with `kickstart-static64.sh` package
1. Find your .environment file
2. If you cannot find that file and would like to uninstall netdata, then create new file with following content:
```
NETDATA_PREFIX="<installation prefix>" # put what you used as a parameter to shell installed `--install` flag. Otherwise it should be empty
NETDATA_ADDED_TO_GROUPS="<additional groups>" # Additional groups for a user running netdata process
```
3. Run ./packaging/installer/netdata-uninstaller.sh --yes --env <path_to_environment_file>
4. Done
Stop netdata with one of the following:
- `service netdata stop` (non-systemd systems)
- `systemctl stop netdata` (systemd systems)
Disable running netdata at startup, with one of the following (based on your distro):
- `rc-update del netdata`
- `update-rc.d netdata disable`
- `chkconfig netdata off`
- `systemctl disable netdata`
Delete the netdata files:
1. `rm -rf /opt/netdata`
2. `groupdel netdata`
3. `userdel netdata`
4. `rm /etc/logrotate.d/netdata`
5. `rm /etc/systemd/system/netdata.service` or `rm /etc/init.d/netdata`, depending on the distro.
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.
[![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)]()

213
packaging/installer/netdata-uninstaller.sh Normal file → Executable file
View file

@ -3,19 +3,21 @@
# this script will uninstall netdata
# Variables needed by script:
# Variables needed by script and taken from '.environment' file:
# - NETDATA_PREFIX
# - NETDATA_ADDED_TO_GROUPS
usage="$(basename "$0") [-h] [-f ] -- program to calculate the answer to life, the universe and everything
where:
-h show this help text
-e, --env path to environment file (defauls to '/etc/netdata/.environment'
-f, --force force uninstallation and do not ask any questions
-h show this help text
-y, --yes flag needs to be set to proceed with uninstallation"
RM_FLAGS="-i"
INTERACTIVE=1
FILE_REMOVAL_STATUS=0
ENVIRONMENT_FILE="/etc/netdata/.environment"
INTERACTIVITY="-i"
YES=0
while :; do
case "$1" in
@ -24,14 +26,17 @@ while :; do
exit 1
;;
-f | --force)
RM_FLAGS="-f"
INTERACTIVE=0
INTERACTIVITY="-f"
shift
;;
-y | --yes)
YES=1
shift
;;
-e | --env)
ENVIRONMENT_FILE="$2"
shift 2
;;
-*)
echo "$usage" >&2
exit 1
@ -41,102 +46,124 @@ while :; do
done
if [ "$YES" != "1" ]; then
echo >&2 "This script will REMOVE netdata from your system."
echo >&2 "Run it again with --yes to do it."
echo "This script will REMOVE netdata from your system."
echo "Run it again with --yes to do it."
exit 1
fi
#shellcheck source=/dev/null
source packaging/installer/.environment.sh || exit 1
#shellcheck source=/dev/null
source packaging/installer/functions.sh || exit 1
if [[ $EUID -ne 0 ]]; then
echo "This script SHOULD be run as root or otherwise it won't delete all installed components."
key="n"
read -r -s -n 1 -p "Do you want to continue as non-root user [y/n] ? " key
if [ "$key" != "y" ] && [ "$key" != "Y" ]; then
exit 1
fi
fi
echo >&2 "Stopping a possibly running netdata..."
for p in $(stop_all_netdata netdata); do run kill "$p"; done
function quit_msg() {
echo
if [ "$FILE_REMOVAL_STATUS" -eq 0 ]; then
echo "Something went wrong :("
else
echo "Netdata files were successfully removed from your system"
fi
}
function user_input() {
TEXT="$1"
if [ "${INTERACTIVITY}" == "-i" ]; then
read -r -p "$TEXT" >&2
fi
}
function rm_file() {
FILE="$1"
if [ -f "${FILE}" ]; then
rm -v ${INTERACTIVITY} "${FILE}"
fi
}
function rm_dir() {
DIR="$1"
if [ -n "$DIR" ] && [ -d "$DIR" ]; then
user_input "Press ENTER to recursively delete directory '$DIR' > "
rm -v -f -R "${DIR}"
fi
}
netdata_pids() {
local p myns ns
myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
for p in \
$(cat /var/run/netdata.pid 2>/dev/null) \
$(cat /var/run/netdata/netdata.pid 2>/dev/null) \
$(pidof netdata 2>/dev/null); do
ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)"
#shellcheck disable=SC2002
if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then
name="$(cat "/proc/${p}/stat" 2>/dev/null | cut -d '(' -f 2 | cut -d ')' -f 1)"
if [ "${name}" = "netdata" ]; then
echo "${p}"
fi
fi
done
}
trap quit_msg EXIT
#shellcheck source=/dev/null
source "${ENVIRONMENT_FILE}" || exit 1
#### STOP NETDATA
echo "Stopping a possibly running netdata..."
for p in $(netdata_pids); do
i=0
while kill "${p}" 2>/dev/null; do
if [ "$i" -gt 30 ]; then
echo "Forcefully stopping netdata with pid ${p}"
kill -9 "${p}"
sleep 2
break
fi
sleep 1
i=$((i + 1))
done
done
sleep 2
if [ ! -z "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
# installation prefix was given
if [ $INTERACTIVE -eq 1 ]; then
portable_deletedir_recursively_interactively "${NETDATA_PREFIX}"
else
run rm -f -R "${NETDATA_PREFIX}"
fi
#### REMOVE NETDATA FILES
rm_file /etc/logrotate.d/netdata
rm_file /etc/systemd/system/netdata.service
rm_file /lib/systemd/system/netdata.service
rm_file /usr/lib/systemd/system/netdata.service
rm_file /etc/init.d/netdata
rm_file /etc/periodic/daily/netdata-updater
rm_file /etc/cron.daily/netdata-updater
if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
rm_dir "${NETDATA_PREFIX}"
else
# installation prefix was NOT given
rm_file "/usr/sbin/netdata"
rm_dir "/usr/share/netdata"
rm_dir "/usr/libexec/netdata"
rm_dir "/var/lib/netdata"
rm_dir "/var/cache/netdata"
rm_dir "/var/log/netdata"
rm_dir "/etc/netdata"
fi
if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]; then
echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
run rm ${RM_FLAGS} "${NETDATA_PREFIX}/usr/sbin/netdata"
fi
FILE_REMOVAL_STATUS=1
for dir in "${NETDATA_PREFIX}/etc/netdata" \
"${NETDATA_PREFIX}/usr/share/netdata" \
"${NETDATA_PREFIX}/usr/libexec/netdata" \
"${NETDATA_PREFIX}/var/lib/netdata" \
"${NETDATA_PREFIX}/var/cache/netdata" \
"${NETDATA_PREFIX}/var/log/netdata"
do
if [ $INTERACTIVE -eq 1 ]; then
portable_deletedir_recursively_interactively "${dir}"
else
run rm -f -R "${dir}"
fi
#### REMOVE NETDATA USER & GROUP
if [ -n "$NETDATA_ADDED_TO_GROUPS" ]; then
user_input "Press ENTER to delete 'netdata' from following groups: '$NETDATA_ADDED_TO_GROUPS' > "
for group in $NETDATA_ADDED_TO_GROUPS; do
gpasswd -d netdata "${group}"
done
fi
if [ -f /etc/logrotate.d/netdata ]; then
echo "Deleting /etc/logrotate.d/netdata ..."
run rm ${RM_FLAGS} /etc/logrotate.d/netdata
fi
if [ -f /etc/systemd/system/netdata.service ]; then
echo "Deleting /etc/systemd/system/netdata.service ..."
run rm ${RM_FLAGS} /etc/systemd/system/netdata.service
fi
if [ -f /lib/systemd/system/netdata.service ]; then
echo "Deleting /lib/systemd/system/netdata.service ..."
run rm ${RM_FLAGS} /lib/systemd/system/netdata.service
fi
if [ -f /etc/init.d/netdata ]; then
echo "Deleting /etc/init.d/netdata ..."
run rm ${RM_FLAGS} /etc/init.d/netdata
fi
if [ -f /etc/periodic/daily/netdata-updater ]; then
echo "Deleting /etc/periodic/daily/netdata-updater ..."
run rm ${RM_FLAGS} /etc/periodic/daily/netdata-updater
fi
if [ -f /etc/cron.daily/netdata-updater ]; then
echo "Deleting /etc/cron.daily/netdata-updater ..."
run rm ${RM_FLAGS} /etc/cron.daily/netdata-updater
fi
portable_check_user_exists netdata
if [ $? -eq 0 ]; then
echo
echo "You may also want to remove the user netdata"
echo "by running:"
echo " userdel netdata"
fi
portable_check_group_exists netdata >/dev/null
if [ $? -eq 0 ]; then
echo
echo "You may also want to remove the group netdata"
echo "by running:"
echo " groupdel netdata"
fi
for g in ${NETDATA_ADDED_TO_GROUPS}; do
portable_check_group_exists "$g" >/dev/null
if [ $? -eq 0 ]; then
echo
echo "You may also want to remove the netdata user from the $g group"
echo "by running:"
echo " gpasswd -d netdata $g"
fi
done
user_input "Press ENTER to delete 'netdata' system user > "
userdel -f netdata || :
user_input "Press ENTER to delete 'netdata' system group > "
groupdel -f netdata || :

27
tests/lifecycle.bats Executable file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env bats
INSTALLATION="$BATS_TMPDIR/installation"
ENV="${INSTALLATION}/netdata/etc/netdata/.environment"
setup() {
if [ ! -f .gitignore ]; then
echo "Run as ./tests/lifecycle/$(basename "$0") from top level directory of git repository"
exit 1
fi
}
@test "install netdata" {
./netdata-installer.sh --dont-wait --dont-start-it --auto-update --install "${INSTALLATION}"
}
@test "update netdata" {
export ENVIRONMENT_FILE="${ENV}"
/etc/cron.daily/netdata-updater
! grep "new_installation" "${ENV}"
}
@test "uninstall netdata" {
./packaging/installer/netdata-uninstaller.sh --yes --force --env "${ENV}"
[ ! -f "${INSTALLATION}/netdata/usr/sbin/netdata" ]
[ ! -f "/etc/cron.daily/netdata-updater" ]
}

View file

@ -1,27 +0,0 @@
#!/bin/bash
set -e
if [ ! -f .gitignore ]
then
echo "Run as ./tests/$(basename "$0") from top level directory of git repository"
exit 1
fi
WORKSPACE=$(mktemp -d)
cp -r ./ "${WORKSPACE}/"
cd "${WORKSPACE}"
echo "========= INSTALL ========="
./netdata-installer.sh --dont-wait --dont-start-it --auto-update --install /tmp &>/dev/null
cp netdata-uninstaller.sh /tmp/netdata-uninstaller.sh
ls /tmp
rm -rf "${WORKSPACE}"
echo "========= UPDATE ========="
ENVIRONMENT_FILE=/tmp/netdata/etc/netdata/.environment /etc/cron.daily/netdata-updater
#TODO(paulfantom): Enable with #5031
#echo "========= UNINSTALL ========="
#ENVIRONMENT_FILE=/tmp/netdata/etc/netdata/.environment /tmp/netdata-uninstaller.sh --yes --force
#[ -f /tmp/netdata/usr/sbin/netdata ] && exit 1