From: Costa Tsaousis Date: Tue, 20 Dec 2016 19:48:57 +0000 (+0200) Subject: portability fixes for adding user/group netdata to the system X-Git-Tag: v1.5.0~112^2~3 X-Git-Url: https://arthur.barton.de/gitweb/?p=netdata.git;a=commitdiff_plain;h=13ba361b51ccfd89587369181497776ea874a85f portability fixes for adding user/group netdata to the system --- diff --git a/netdata-installer.sh b/netdata-installer.sh index e005a4cf..e7ac435f 100755 --- a/netdata-installer.sh +++ b/netdata-installer.sh @@ -548,42 +548,123 @@ do done echo >&2 "Fixing permissions ..." -run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -exec chmod 755 {} \; -NETDATA_ADDED_TO_DOCKER=0 -if [ ${UID} -eq 0 ] +check_cmd() { + which "${1}" >/dev/null 2>&1 && return 0 + command -v "${1}" >/dev/null 2>&1 && return 0 + return 1 +} + +portable_add_user() { + local username="${1}" + + getent passwd "${username}" > /dev/null 2>&1 + [ $? -eq 0 ] && return 0 + + echo >&2 "Adding ${username} user account ..." + + local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')" + + # Linux + if check_cmd useradd then - getent group netdata > /dev/null - if [ $? -ne 0 ] - then - echo >&2 "Adding netdata user group ..." - run groupadd -r netdata + run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0 fi - getent passwd netdata > /dev/null - if [ $? -ne 0 ] - then - echo >&2 "Adding netdata user account ..." - run useradd -r -g netdata -c netdata -s $(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false') -d / netdata + # FreeBSD + if check_cmd pw + then + run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0 fi - getent group docker > /dev/null - if [ $? -eq 0 ] + # BusyBox + if check_cmd adduser + then + run adduser -D -G "${username}" "${username}" && return 0 + fi + + echo >&2 "Failed to add ${username} user account !" + + return 1 +} + +portable_add_group() { + local groupname="${1}" + + getent group "${groupname}" > /dev/null 2>&1 + [ $? -eq 0 ] && return 0 + + echo >&2 "Adding ${groupname} user group ..." + + # Linux + if check_cmd groupadd + then + run groupadd -r "${groupname}" && return 0 + fi + + # FreeBSD + if check_cmd pw + then + run pw groupadd "${groupname}" && return 0 + fi + + # BusyBox + if check_cmd addgroup + then + run addgroup "${groupname}" && return 0 + fi + + echo >&2 "Failed to add ${groupname} user group !" + return 1 +} + +portable_add_user_to_group() { + local groupname="${1}" username="${2}" + + getent group "${groupname}" > /dev/null 2>&1 + [ $? -ne 0 ] && return 1 + + # find the users in the docker group + local users=$(getent group "${groupname}" | cut -d ':' -f 4) + if [[ ",${users}," =~ ,${username}, ]] then - # find the users in the docker group - docker=$(getent group docker | cut -d ':' -f 4) - if [[ ",${docker}," =~ ,netdata, ]] - then - # netdata is already there - : - else - # netdata is not in docker group - echo >&2 "Adding netdata user to the docker group (needed to get container names) ..." - run usermod -a -G docker netdata + # username is already there + : + else + # username is not in group + echo >&2 "Adding ${username} user to the ${groupname} group ..." + + # Linux + if check_cmd usermod + then + run usermod -a -G "${groupname}" "${username}" && return 0 fi - # let the uninstall script know - NETDATA_ADDED_TO_DOCKER=1 + + # FreeBSD + if check_cmd pw + then + run pw groupmod "${groupname}" -m "${username}" && return 0 + fi + + # BusyBox + if check_cmd addgroup + then + run addgroup "${username}" "${groupname}" && return 0 + fi + + echo >&2 "Failed to add user ${username} to group ${groupname} !" + return 1 fi +} + +run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -exec chmod 755 {} \; + +NETDATA_ADDED_TO_DOCKER=0 +if [ ${UID} -eq 0 ] + then + portable_add_group netdata + portable_add_user netdata + portable_add_user_to_group docker netdata && NETDATA_ADDED_TO_DOCKER=1 if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ] then