From 7ab746008904f4c68a43d14753b55656dfcc2d01 Mon Sep 17 00:00:00 2001 From: "Paul Emm. Katsoulakis" <34388743+paulkatsoulakis@users.noreply.github.com> Date: Sun, 19 May 2019 23:37:18 +0300 Subject: 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..?) --- netdata-installer.sh | 9 +- packaging/installer/functions.sh | 14 ++ packaging/installer/netdata-uninstaller.sh | 208 ++++++++++++++++++++++++++--- 3 files changed, 207 insertions(+), 24 deletions(-) diff --git a/netdata-installer.sh b/netdata-installer.sh index f32994e9d4..4ed4050fd7 100755 --- a/netdata-installer.sh +++ b/netdata-installer.sh @@ -515,20 +515,24 @@ progress "Fix generated files permissions" run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \; # ----------------------------------------------------------------------------- -progress "Add user netdata to required user groups" +progress "Creating standard user and groups for netdata" NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody" NETDATA_ADDED_TO_GROUPS="" if [ "${UID}" -eq 0 ]; then + progress "Adding group 'netdata'" portable_add_group netdata || : + + progress "Adding user 'netdata'" portable_add_user netdata "${NETDATA_PREFIX}/var/lib/netdata" || : + progress "Assign user 'netdata' to required groups" for g in ${NETDATA_WANTED_GROUPS}; do # shellcheck disable=SC2086 portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}" done else - run_failed "The installer does not run as root." + run_failed "The installer does not run as root. Nothing to do for user and groups" fi # ----------------------------------------------------------------------------- @@ -567,6 +571,7 @@ else fi NETDATA_GROUP="$(id -g -n "${NETDATA_USER}")" [ -z "${NETDATA_GROUP}" ] && NETDATA_GROUP="${NETDATA_USER}" +echo >&2 "Netdata user and group is finally set to: ${NETDATA_USER}/${NETDATA_GROUP}" # the owners of the web files NETDATA_WEB_USER="$(config_option "web" "web files owner" "${NETDATA_USER}")" diff --git a/packaging/installer/functions.sh b/packaging/installer/functions.sh index 4270c4ee05..d1e944878f 100644 --- a/packaging/installer/functions.sh +++ b/packaging/installer/functions.sh @@ -606,6 +606,11 @@ portable_add_user() { run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0 fi + # mac OS + if command -v sysadminctl 1> /dev/null 2>&1; then + run sysadminctl -addUser ${username} && return 0 + fi + echo >&2 "Failed to add ${username} user account !" return 1 @@ -637,6 +642,11 @@ portable_add_group() { run addgroup "${groupname}" && return 0 fi + # mac OS + if command -v dseditgroup 1> /dev/null 2>&1; then + dseditgroup -o create "${groupname}" && return 0 + fi + echo >&2 "Failed to add ${groupname} user group !" return 1 } @@ -674,6 +684,10 @@ portable_add_user_to_group() { run addgroup "${username}" "${groupname}" && return 0 fi + # mac OS + if command -v dseditgroup 1> /dev/null 2>&1; then + dseditgroup -u "${username}" "${groupname}" && return 0 + fi echo >&2 "Failed to add user ${username} to group ${groupname} !" return 1 fi diff --git a/packaging/installer/netdata-uninstaller.sh b/packaging/installer/netdata-uninstaller.sh index 96dd629060..cfd858c02d 100755 --- a/packaging/installer/netdata-uninstaller.sh +++ b/packaging/installer/netdata-uninstaller.sh @@ -1,11 +1,15 @@ #!/usr/bin/env bash #shellcheck disable=SC2181 - -# this script will uninstall netdata - +# +# This is the netdata uninstaller script # Variables needed by script and taken from '.environment' file: # - NETDATA_PREFIX # - NETDATA_ADDED_TO_GROUPS +# +# Copyright: SPDX-License-Identifier: GPL-3.0-or-later +# +# Author: Paul Emm. Katsoulakis +# usage="$(basename "$0") [-h] [-f ] -- program to calculate the answer to life, the universe and everything @@ -46,13 +50,13 @@ while :; do done if [ "$YES" != "1" ]; then - echo "This script will REMOVE netdata from your system." - echo "Run it again with --yes to do it." + echo >&2 "This script will REMOVE netdata from your system." + echo >&2 "Run it again with --yes to do it." exit 1 fi if [[ $EUID -ne 0 ]]; then - echo "This script SHOULD be run as root or otherwise it won't delete all installed components." + echo >&2 "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 @@ -60,34 +64,191 @@ if [[ $EUID -ne 0 ]]; then fi fi -function quit_msg() { +# ----------------------------------------------------------------------------- + +setup_terminal() { + TPUT_RESET="" + TPUT_YELLOW="" + TPUT_WHITE="" + TPUT_BGRED="" + TPUT_BGGREEN="" + TPUT_BOLD="" + TPUT_DIM="" + + # Is stderr on the terminal? If not, then fail + test -t 2 || return 1 + + if command -v tput 1>/dev/null 2>&1; then + if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then + # Enable colors + TPUT_RESET="$(tput sgr 0)" + TPUT_YELLOW="$(tput setaf 3)" + TPUT_WHITE="$(tput setaf 7)" + TPUT_BGRED="$(tput setab 1)" + TPUT_BGGREEN="$(tput setab 2)" + TPUT_BOLD="$(tput bold)" + TPUT_DIM="$(tput dim)" + fi + fi + + return 0 +} +setup_terminal || echo >/dev/null + +run_ok() { + printf >&2 "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET} ${*} \n\n" +} + +run_failed() { + printf >&2 "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET} ${*} \n\n" +} + +ESCAPED_PRINT_METHOD= +printf "%q " test >/dev/null 2>&1 +[ $? -eq 0 ] && ESCAPED_PRINT_METHOD="printfq" +escaped_print() { + if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then + printf "%q " "${@}" + else + printf "%s" "${*}" + fi + return 0 +} + +run_logfile="/dev/null" +run() { + local user="${USER--}" dir="${PWD}" info info_console + + if [ "${UID}" = "0" ]; then + info="[root ${dir}]# " + info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# " + else + info="[${user} ${dir}]$ " + info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ " + fi + + printf >>"${run_logfile}" "${info}" + escaped_print >>"${run_logfile}" "${@}" + printf >>"${run_logfile}" " ... " + + printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" + escaped_print >&2 "${@}" + printf >&2 "${TPUT_RESET}\n" + + "${@}" + + local ret=$? + if [ ${ret} -ne 0 ]; then + run_failed + printf >>"${run_logfile}" "FAILED with exit code ${ret}\n" + else + run_ok + printf >>"${run_logfile}" "OK\n" + fi + + return ${ret} +} + +portable_del_group() { + local groupname="${1}" + + # Check if group exist + echo >&2 "Removing ${groupname} user group ..." + + # Linux + if command -v groupdel 1>/dev/null 2>&1; then + run groupdel -f "${groupname}" && return 0 + fi + + # mac OS + if command -v dseditgroup 1> /dev/null 2>&1; then + if dseditgroup -o read netdata 1> /dev/null 2>&1; then + run dseditgroup -o delete "${groupname}" && return 0 + else + echo >&2 "Could not find group ${groupname}, nothing to do" + fi + fi + + echo >&2 "Group ${groupname} was not automatically removed, you might have to remove it manually" + return 1 +} + +portable_del_user() { + local username="${1}" + echo >&2 "Deleting ${username} user account ..." + + # Linux + if command -v userdel 1>/dev/null 2>&1; then + run userdel -f "${username}" && return 0 + fi + + # mac OS + if command -v sysadminctl 1>/dev/null 2>&1; then + run sysadminctl -deleteUser "${username}" && return 0 + fi + + echo >&2 "User ${username} could not be deleted from system, you might have to remove it manually" + return 1 +} + +portable_del_user_from_group() { + local groupname="${1}" username="${2}" + + # username is not in group + echo >&2 "Deleting ${username} user from ${groupname} group ..." + + # Linux + if command -v gpasswd 1>/dev/null 2>&1; then + run gpasswd -d "netdata" "${group}" && return 0 + fi + + # FreeBSD + if command -v pw 1>/dev/null 2>&1; then + run pw groupmod "${groupname}" -d "${username}" && return 0 + fi + + # BusyBox + if command -v delgroup 1>/dev/null 2>&1; then + run delgroup "${username}" "${groupname}" && return 0 + fi + + # mac OS + if command -v dseditgroup 1> /dev/null 2>&1; then + run dseditgroup -o delete -u "${username}" "${groupname}" && return 0 + fi + + echo >&2 "Failed to delete user ${username} from group ${groupname} !" + return 1 +} + +quit_msg() { echo if [ "$FILE_REMOVAL_STATUS" -eq 0 ]; then - echo "Something went wrong :(" + echo >&2 "Something went wrong :(" else - echo "Netdata files were successfully removed from your system" + echo >&2 "Netdata files were successfully removed from your system" fi } -function user_input() { +user_input() { TEXT="$1" if [ "${INTERACTIVITY}" == "-i" ]; then read -r -p "$TEXT" >&2 fi } -function rm_file() { +rm_file() { FILE="$1" if [ -f "${FILE}" ]; then - rm -v ${INTERACTIVITY} "${FILE}" + run rm -v ${INTERACTIVITY} "${FILE}" fi } -function rm_dir() { +rm_dir() { DIR="$1" if [ -n "$DIR" ] && [ -d "$DIR" ]; then user_input "Press ENTER to recursively delete directory '$DIR' > " - rm -v -f -R "${DIR}" + run rm -v -f -R "${DIR}" fi } @@ -116,14 +277,14 @@ trap quit_msg EXIT source "${ENVIRONMENT_FILE}" || exit 1 #### STOP NETDATA -echo "Stopping a possibly running netdata..." +echo >&2 "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 + echo >&2 "Forcefully stopping netdata with pid ${p}" + run kill -9 "${p}" + run sleep 2 break fi sleep 1 @@ -155,15 +316,18 @@ fi FILE_REMOVAL_STATUS=1 -#### REMOVE NETDATA USER & GROUP +#### REMOVE NETDATA USER FROM ADDED GROUPS 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}" + portable_del_user_from_group "${group}" "netdata" done fi +#### REMOVE USER user_input "Press ENTER to delete 'netdata' system user > " -userdel -f netdata || : +portable_del_user "netdata" || : + +### REMOVE GROUP user_input "Press ENTER to delete 'netdata' system group > " -groupdel -f netdata || : +portable_del_group "netdata" || : -- cgit v1.2.3