diff options
Diffstat (limited to 'packaging/installer/functions.sh')
-rw-r--r-- | packaging/installer/functions.sh | 439 |
1 files changed, 159 insertions, 280 deletions
diff --git a/packaging/installer/functions.sh b/packaging/installer/functions.sh index 0d0450842..7016b4f34 100644 --- a/packaging/installer/functions.sh +++ b/packaging/installer/functions.sh @@ -6,19 +6,6 @@ [ -z "${UID}" ] && UID="$(id -u)" # ----------------------------------------------------------------------------- -# checking the availability of commands - -which_cmd() { - # shellcheck disable=SC2230 - which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null -} - -check_cmd() { - which_cmd "${1}" >/dev/null 2>&1 && return 0 - return 1 -} - -# ----------------------------------------------------------------------------- setup_terminal() { TPUT_RESET="" @@ -50,7 +37,7 @@ setup_terminal() { # Is stderr on the terminal? If not, then fail test -t 2 || return 1 - if check_cmd tput; then + 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)" @@ -117,19 +104,20 @@ netdata_banner() { # ----------------------------------------------------------------------------- # portable service command -service_cmd="$(which_cmd service)" -rcservice_cmd="$(which_cmd rc-service)" -systemctl_cmd="$(which_cmd systemctl)" +service_cmd="$(command -v service 2>/dev/null)" +rcservice_cmd="$(command -v rc-service 2>/dev/null)" +systemctl_cmd="$(command -v systemctl 2>/dev/null)" service() { + local cmd="${1}" action="${2}" - if [ ! -z "${systemctl_cmd}" ]; then + if [ -n "${systemctl_cmd}" ]; then run "${systemctl_cmd}" "${action}" "${cmd}" return $? - elif [ ! -z "${service_cmd}" ]; then + elif [ -n "${service_cmd}" ]; then run "${service_cmd}" "${cmd}" "${action}" return $? - elif [ ! -z "${rcservice_cmd}" ]; then + elif [ -n "${rcservice_cmd}" ]; then run "${rcservice_cmd}" "${cmd}" "${action}" return $? fi @@ -139,9 +127,9 @@ service() { # ----------------------------------------------------------------------------- # portable pidof -pidof_cmd="$(which_cmd pidof)" -pidof() { - if [ ! -z "${pidof_cmd}" ]; then +safe_pidof() { + local pidof_cmd="$(command -v pidof 2>/dev/null)" + if [ -n "${pidof_cmd}" ]; then ${pidof_cmd} "${@}" return $? else @@ -154,39 +142,21 @@ pidof() { } # ----------------------------------------------------------------------------- -# portable delete recursively interactively - -portable_deletedir_recursively_interactively() { - if [ ! -z "$1" -a -d "$1" ]; then - if [ "$(uname -s)" = "Darwin" ]; then - echo >&2 - read >&2 -p "Press ENTER to recursively delete directory '$1' > " - echo >&2 "Deleting directory '$1' ..." - run rm -R "$1" - else - echo >&2 - echo >&2 "Deleting directory '$1' ..." - run rm -I -R "$1" - fi - else - echo "Directory '$1' does not exist." - fi -} - -# ----------------------------------------------------------------------------- - -export SYSTEM_CPUS=1 -portable_find_processors() { +find_processors() { + local cpus if [ -f "/proc/cpuinfo" ]; then # linux - SYSTEM_CPUS=$(grep -c ^processor /proc/cpuinfo) + cpus=$(grep -c ^processor /proc/cpuinfo) else # freebsd - SYSTEM_CPUS=$(sysctl hw.ncpu 2>/dev/null | grep ^hw.ncpu | cut -d ' ' -f 2) + cpus=$(sysctl hw.ncpu 2>/dev/null | grep ^hw.ncpu | cut -d ' ' -f 2) + fi + if [ -z "${cpus}" ] || [ $((cpus)) -lt 1 ]; then + echo 1 + else + echo "${cpus}" fi - [ -z "${SYSTEM_CPUS}" -o $((SYSTEM_CPUS)) -lt 1 ] && SYSTEM_CPUS=1 } -portable_find_processors # ----------------------------------------------------------------------------- fatal() { @@ -248,145 +218,10 @@ run() { return ${ret} } -getent_cmd="$(which_cmd getent)" -portable_check_user_exists() { - local username="${1}" found= - - if [ ! -z "${getent_cmd}" ]; then - "${getent_cmd}" passwd "${username}" >/dev/null 2>&1 - return $? - fi - - found="$(cut -d ':' -f 1 </etc/passwd | grep "^${username}$")" - [ "${found}" = "${username}" ] && return 0 - return 1 -} - -portable_check_group_exists() { - local groupname="${1}" found= - - if [ ! -z "${getent_cmd}" ]; then - "${getent_cmd}" group "${groupname}" >/dev/null 2>&1 - return $? - fi - - found="$(cut -d ':' -f 1 </etc/group | grep "^${groupname}$")" - [ "${found}" = "${groupname}" ] && return 0 - return 1 -} - -portable_check_user_in_group() { - local username="${1}" groupname="${2}" users= - - if [ ! -z "${getent_cmd}" ]; then - users="$(getent group "${groupname}" | cut -d ':' -f 4)" - else - users="$(grep "^${groupname}:" </etc/group | cut -d ':' -f 4)" - fi - - [[ ",${users}," =~ ,${username}, ]] && return 0 - return 1 -} - -portable_add_user() { - local username="${1}" homedir="${2}" - - [ -z "${homedir}" ] && homedir="/tmp" - - portable_check_user_exists "${username}" - [ $? -eq 0 ] && echo >&2 "User '${username}' already exists." && return 0 - - echo >&2 "Adding ${username} user account with home ${homedir} ..." - - # shellcheck disable=SC2230 - local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')" - - # Linux - if check_cmd useradd; then - run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0 - fi - - # FreeBSD - if check_cmd pw; then - run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0 - fi - - # BusyBox - if check_cmd adduser; then - run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0 - fi - - echo >&2 "Failed to add ${username} user account !" - - return 1 -} - -portable_add_group() { - local groupname="${1}" - - portable_check_group_exists "${groupname}" - [ $? -eq 0 ] && echo >&2 "Group '${groupname}' already exists." && 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}" - - portable_check_group_exists "${groupname}" - [ $? -ne 0 ] && echo >&2 "Group '${groupname}' does not exist." && return 1 - - # find the user is already in the group - if portable_check_user_in_group "${username}" "${groupname}"; then - # username is already there - echo >&2 "User '${username}' is already in group '${groupname}'." - return 0 - 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 - - # 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 -} - iscontainer() { # man systemd-detect-virt - local cmd=$(which_cmd systemd-detect-virt) - if [ ! -z "${cmd}" -a -x "${cmd}" ]; then + local cmd=$(command -v systemd-detect-virt 2>/dev/null) + if [ -n "${cmd}" ] && [ -x "${cmd}" ]; then "${cmd}" --container >/dev/null 2>&1 && return 0 fi @@ -394,15 +229,15 @@ iscontainer() { # http://stackoverflow.com/a/37016302 local pid=$(cat /proc/1/sched 2>/dev/null | head -n 1 | { IFS='(),#:' read name pid th threads - echo $pid + echo "$pid" }) - if [ ! -z "${pid}" ]; then + if [ -n "${pid}" ]; then pid=$(( pid + 0 )) [ ${pid} -gt 1 ] && return 0 fi # lxc sets environment variable 'container' - [ ! -z "${container}" ] && return 0 + [ -n "${container}" ] && return 0 # docker creates /.dockerenv # http://stackoverflow.com/a/25518345 @@ -425,23 +260,23 @@ issystemd() { # if there is no systemctl command, it is not systemd # shellcheck disable=SC2230 - systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null) + systemctl=$(command -v systemctl 2>/dev/null) [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1 # if pid 1 is systemd, it is systemd [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0 # if systemd is not running, it is not systemd - pids=$(pidof systemd 2>/dev/null) + pids=$(safe_pidof systemd 2>/dev/null) [ -z "${pids}" ] && return 1 # check if the running systemd processes are not in our namespace myns="$(readlink /proc/self/ns/pid 2>/dev/null)" for p in ${pids}; do - ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)" + ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)" # if pid of systemd is in our namespace, it is systemd - [ ! -z "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0 + [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0 done # else, it is not systemd @@ -460,7 +295,7 @@ install_non_systemd_init() { key=$(</etc/redhat-release) fi - if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]; then + if [ -d /etc/init.d ] && [ ! -f /etc/init.d/netdata ]; then if [[ ${key} =~ ^(gentoo|alpine).* ]]; then echo >&2 "Installing OpenRC init file..." run cp system/netdata-openrc /etc/init.d/netdata && @@ -468,10 +303,7 @@ install_non_systemd_init() { run rc-update add netdata default && return 0 - elif [ "${key}" = "debian-7" \ - -o "${key}" = "ubuntu-12.04" \ - -o "${key}" = "ubuntu-14.04" \ - ]; then + elif [ "${key}" = "debian-7" ] || [ "${key}" = "ubuntu-12.04" ] || [ "${key}" = "ubuntu-14.04" ]; then echo >&2 "Installing LSB init file..." run cp system/netdata-lsb /etc/init.d/netdata && run chmod 755 /etc/init.d/netdata && @@ -551,10 +383,10 @@ install_netdata_service() { local ret=$? if [ ${ret} -eq 0 ]; then - if [ ! -z "${service_cmd}" ]; then + if [ -n "${service_cmd}" ]; then NETDATA_START_CMD="service netdata start" NETDATA_STOP_CMD="service netdata stop" - elif [ ! -z "${rcservice_cmd}" ]; then + elif [ -n "${rcservice_cmd}" ]; then NETDATA_START_CMD="rc-service netdata start" NETDATA_STOP_CMD="rc-service netdata stop" fi @@ -582,10 +414,10 @@ pidisnetdata() { stop_netdata_on_pid() { local pid="${1}" ret=0 count=0 - pidisnetdata ${pid} || return 0 + pidisnetdata "${pid}" || return 0 - printf >&2 "Stopping netdata on pid ${pid} ..." - while [ ! -z "$pid" -a ${ret} -eq 0 ]; do + printf >&2 "Stopping netdata on pid %s ..." "${pid}" + while [ -n "$pid" ] && [ ${ret} -eq 0 ]; do if [ ${count} -gt 45 ]; then echo >&2 "Cannot stop the running netdata on pid ${pid}." return 1 @@ -593,7 +425,7 @@ stop_netdata_on_pid() { count=$((count + 1)) - run kill ${pid} 2>/dev/null + run kill "${pid}" 2>/dev/null ret=$? test ${ret} -eq 0 && printf >&2 "." && sleep 2 @@ -619,11 +451,11 @@ netdata_pids() { 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)" + $(safe_pidof netdata 2>/dev/null); do + ns="$(readlink "/proc/${p}/ns/pid" 2>/dev/null)" - if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]; then - pidisnetdata ${p} && echo "${p}" + if [ -z "${myns}" ] || [ -z "${ns}" ] || [ "${myns}" = "${ns}" ]; then + pidisnetdata "${p}" && echo "${p}" fi done } @@ -631,6 +463,7 @@ netdata_pids() { stop_all_netdata() { local p for p in $(netdata_pids); do + # shellcheck disable=SC2086 stop_netdata_on_pid ${p} done } @@ -651,7 +484,7 @@ restart_netdata() { stop_all_netdata service netdata restart && started=1 - if [ ${started} -eq 1 -a -z "$(netdata_pids)" ]; then + if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then echo >&2 "Ooops! it seems netdata is not started." started=0 fi @@ -661,7 +494,7 @@ restart_netdata() { fi fi - if [ ${started} -eq 1 -a -z "$(netdata_pids)" ]; then + if [ ${started} -eq 1 ] && [ -z "$(netdata_pids)" ]; then echo >&2 "Hm... it seems netdata is still not started." started=0 fi @@ -681,7 +514,7 @@ restart_netdata() { # install netdata logrotate install_netdata_logrotate() { - if [ ${UID} -eq 0 ]; then + if [ "${UID}" -eq 0 ]; then if [ -d /etc/logrotate.d ]; then if [ ! -f /etc/logrotate.d/netdata ]; then run cp system/netdata.logrotate /etc/logrotate.d/netdata @@ -699,103 +532,149 @@ install_netdata_logrotate() { } # ----------------------------------------------------------------------------- -# download netdata.conf +# create netdata.conf -fix_netdata_conf() { - local owner="${1}" +create_netdata_conf() { + local path="${1}" url="${2}" - if [ "${UID}" -eq 0 ]; then - run chown "${owner}" "${filename}" - fi - run chmod 0664 "${filename}" -} - -generate_netdata_conf() { - local owner="${1}" filename="${2}" url="${3}" - - if [ ! -s "${filename}" ]; then - cat >"${filename}" <<EOFCONF -# netdata can generate its own config. -# Get it with: -# -# wget -O ${filename} "${url}" -# -# or -# -# curl -s -o ${filename} "${url}" -# -EOFCONF - fix_netdata_conf "${owner}" + if [ -s "${path}" ]; then + return 0 fi -} -download_netdata_conf() { - local owner="${1}" filename="${2}" url="${3}" - - if [ ! -s "${filename}" ]; then - echo >&2 - echo >&2 "-------------------------------------------------------------------------------" - echo >&2 + if [ -n "$url" ]; then echo >&2 "Downloading default configuration from netdata..." sleep 5 - # remove a possibly obsolete download - [ -f "${filename}.new" ] && rm "${filename}.new" + # remove a possibly obsolete configuration file + [ -f "${path}.new" ] && rm "${path}.new" # disable a proxy to get data from the local netdata export http_proxy= export https_proxy= - # try curl - run curl -s -o "${filename}.new" "${url}" - ret=$? - - if [ ${ret} -ne 0 -o ! -s "${filename}.new" ]; then - # try wget - run wget -O "${filename}.new" "${url}" - ret=$? + if command -v curl 1>/dev/null 2>&1; then + run curl -sSL --connect-timeout 10 --retry 3 "${url}" >"${path}.new" + elif command -v wget 1>/dev/null 2>&1; then + run wget -T 15 -O - "${url}" >"${path}.new" fi - if [ ${ret} -eq 0 -a -s "${filename}.new" ]; then - run mv "${filename}.new" "${filename}" - run_ok "New configuration saved for you to edit at ${filename}" + if [ -s "${path}.new" ]; then + run mv "${path}.new" "${path}" + run_ok "New configuration saved for you to edit at ${path}" else - [ -f "${filename}.new" ] && rm "${filename}.new" + [ -f "${path}.new" ] && rm "${path}.new" run_failed "Cannnot download configuration from netdata daemon using url '${url}'" - - generate_netdata_conf "${owner}" "${filename}" "${url}" + url='' fi + fi - fix_netdata_conf "${owner}" + if [ -z "$url" ]; then + echo "# netdata can generate its own config which is available at 'http://<netdata_ip>/netdata.conf'" >"${path}" + echo "# You can download it with command like: 'wget -O ${path} http://localhost:19999/netdata.conf'" >>"${path}" fi + } -# ----------------------------------------------------------------------------- -# add netdata user and group +portable_add_user() { + local username="${1}" homedir="${2}" -NETDATA_WANTED_GROUPS="docker nginx varnish haproxy adm nsd proxy squid ceph nobody" -NETDATA_ADDED_TO_GROUPS="" -add_netdata_user_and_group() { - local homedir="${1}" g + [ -z "${homedir}" ] && homedir="/tmp" + + # Check if user exists + if cut -d ':' -f 1 </etc/passwd | grep "^${username}$" 1>/dev/null 2>&1; then + echo >&2 "User '${username}' already exists." + return 0 + fi - if [ ${UID} -eq 0 ]; then - portable_add_group netdata || return 1 - portable_add_user netdata "${homedir}" || return 1 + echo >&2 "Adding ${username} user account with home ${homedir} ..." - for g in ${NETDATA_WANTED_GROUPS}; do - portable_add_user_to_group ${g} netdata && NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS} ${g}" - done + # shellcheck disable=SC2230 + local nologin="$(command -v nologin >/dev/null 2>&1 || echo '/bin/false')" - [ ~netdata = / ] && cat <<USERMOD + # Linux + if command -v useradd 1>/dev/null 2>&1; then + run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0 + fi -The netdata user has its home directory set to / -You may want to change it, using this command: + # FreeBSD + if command -v pw 1>/dev/null 2>&1; then + run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0 + fi -# usermod -d "${homedir}" netdata + # BusyBox + if command -v adduser 1>/dev/null 2>&1; then + run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0 + fi -USERMOD - return 0 + echo >&2 "Failed to add ${username} user account !" + + return 1 +} + +portable_add_group() { + local groupname="${1}" + + # Check if group exist + if cut -d ':' -f 1 </etc/group | grep "^${groupname}$" 1>/dev/null 2>&1; then + echo >&2 "Group '${groupname}' already exists." + return 0 + fi + + echo >&2 "Adding ${groupname} user group ..." + + # Linux + if command -v groupadd 1>/dev/null 2>&1; then + run groupadd -r "${groupname}" && return 0 fi + # FreeBSD + if command -v pw 1>/dev/null 2>&1; then + run pw groupadd "${groupname}" && return 0 + fi + + # BusyBox + if command -v addgroup 1>/dev/null 2>&1; 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}" + + # Check if group exist + if ! cut -d ':' -f 1 </etc/group | grep "^${groupname}$" >/dev/null 2>&1; then + echo >&2 "Group '${groupname}' does not exist." + return 1 + fi + + # Check if user is in group + if [[ ",$(grep "^${groupname}:" </etc/group | cut -d ':' -f 4)," =~ ,${username}, ]]; then + # username is already there + echo >&2 "User '${username}' is already in group '${groupname}'." + return 0 + else + # username is not in group + echo >&2 "Adding ${username} user to the ${groupname} group ..." + + # Linux + if command -v usermod 1>/dev/null 2>&1; then + run usermod -a -G "${groupname}" "${username}" && return 0 + fi + + # FreeBSD + if command -v pw 1>/dev/null 2>&1; then + run pw groupmod "${groupname}" -m "${username}" && return 0 + fi + + # BusyBox + if command -v addgroup 1>/dev/null 2>&1; then + run addgroup "${username}" "${groupname}" && return 0 + fi + + echo >&2 "Failed to add user ${username} to group ${groupname} !" + return 1 + fi +} |