From c97eaeb9dfad7190e03c0ca264ea6ae76f404edd Mon Sep 17 00:00:00 2001 From: "Costa Tsaousis (ktsaou)" Date: Sun, 12 Feb 2017 23:29:44 +0200 Subject: [PATCH] compatibility fixes at the installer --- installer/functions.sh | 91 +++++++++++++++++++++++------------------ netdata-installer.sh | 92 +++++++++++++++++++++++------------------- 2 files changed, 102 insertions(+), 81 deletions(-) diff --git a/installer/functions.sh b/installer/functions.sh index 5c81f88b..02b061d8 100644 --- a/installer/functions.sh +++ b/installer/functions.sh @@ -20,43 +20,45 @@ setup_terminal() { # Is stderr on the terminal? If not, then fail test -t 2 || return 1 - if [ ! -z "$TPUT_CMD" ] + if check_cmd tput then - if [ $[$($TPUT_CMD colors 2>/dev/null)] -ge 8 ] + if [ $(( $(tput colors 2>/dev/null) )) -ge 8 ] then # Enable colors - COLOR_RESET="\e[0m" - COLOR_BLACK="\e[30m" - COLOR_RED="\e[31m" - COLOR_GREEN="\e[32m" - COLOR_YELLOW="\e[33m" - COLOR_BLUE="\e[34m" - COLOR_PURPLE="\e[35m" - COLOR_CYAN="\e[36m" - COLOR_WHITE="\e[37m" - COLOR_BGBLACK="\e[40m" - COLOR_BGRED="\e[41m" - COLOR_BGGREEN="\e[42m" - COLOR_BGYELLOW="\e[43m" - COLOR_BGBLUE="\e[44m" - COLOR_BGPURPLE="\e[45m" - COLOR_BGCYAN="\e[46m" - COLOR_BGWHITE="\e[47m" - COLOR_BOLD="\e[1m" - COLOR_DIM="\e[2m" - COLOR_UNDERLINED="\e[4m" - COLOR_BLINK="\e[5m" - COLOR_INVERTED="\e[7m" + TPUT_RESET="$(tput sgr 0)" + TPUT_BLACK="$(tput setaf 0)" + TPUT_RED="$(tput setaf 1)" + TPUT_GREEN="$(tput setaf 2)" + TPUT_YELLOW="$(tput setaf 3)" + TPUT_BLUE="$(tput setaf 4)" + TPUT_PURPLE="$(tput setaf 5)" + TPUT_CYAN="$(tput setaf 6)" + TPUT_WHITE="$(tput setaf 7)" + TPUT_BGBLACK="$(tput setab 0)" + TPUT_BGRED="$(tput setab 1)" + TPUT_BGGREEN="$(tput setab 2)" + TPUT_BGYELLOW="$(tput setab 3)" + TPUT_BGBLUE="$(tput setab 4)" + TPUT_BGPURPLE="$(tput setab 5)" + TPUT_BGCYAN="$(tput setab 6)" + TPUT_BGWHITE="$(tput setab 7)" + TPUT_BOLD="$(tput bold)" + TPUT_DIM="$(tput dim)" + TPUT_UNDERLINED="$(tput smul)" + TPUT_BLINK="$(tput blink)" + TPUT_INVERTED="$(tput rev)" + TPUT_STANDOUT="$(tput smso)" + TPUT_BELL="$(tput bel)" + TPUT_CLEAR="$(tput clear)" fi fi return 0 } -TPUT_CMD="$(which_cmd tput)" setup_terminal progress() { - printf >&2 " --- ${COLOR_DIM}${COLOR_BOLD}${*}${COLOR_RESET} --- \n" + echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- " } # ----------------------------------------------------------------------------- @@ -67,7 +69,7 @@ netdata_banner() { l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \ l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \ sp=" " \ - netdata="netdata" start end msg="${*}" chartcolor="${COLOR_DIM}" + netdata="netdata" start end msg="${*}" chartcolor="${TPUT_DIM}" [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}" [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..." @@ -77,10 +79,10 @@ netdata_banner() { end=$(( ${start} + ${#msg} + 4 )) echo >&2 - echo >&2 -e "${chartcolor}${l1}${COLOR_RESET}" - echo >&2 -e "${chartcolor}${l2:0:start}${sp:0:2}${COLOR_RESET}${COLOR_BOLD}${COLOR_GREEN}${netdata}${COLOR_RESET}${chartcolor}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}${COLOR_RESET}" - echo >&2 -e "${chartcolor}${l3:0:start}${sp:0:2}${COLOR_RESET}${COLOR_BOLD}${COLOR_CYAN}${msg}${COLOR_RESET}${chartcolor}${sp:0:2}${l3:end:$((${#l2} - end))}${COLOR_RESET}" - echo >&2 -e "${chartcolor}${l4}${COLOR_RESET}" + echo >&2 "${chartcolor}${l1}${TPUT_RESET}" + echo >&2 "${chartcolor}${l2:0:start}${sp:0:2}${TPUT_RESET}${TPUT_BOLD}${TPUT_GREEN}${netdata}${TPUT_RESET}${chartcolor}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}${TPUT_RESET}" + echo >&2 "${chartcolor}${l3:0:start}${sp:0:2}${TPUT_RESET}${TPUT_BOLD}${TPUT_CYAN}${msg}${TPUT_RESET}${chartcolor}${sp:0:2}${l3:end:$((${#l2} - end))}${TPUT_RESET}" + echo >&2 "${chartcolor}${l4}${TPUT_RESET}" echo >&2 } @@ -106,6 +108,14 @@ service() { # ----------------------------------------------------------------------------- +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" +} + run_logfile="/dev/null" run() { local user="${USER}" dir="$(basename "${PWD}")" info info_console @@ -113,29 +123,29 @@ run() { if [ "${UID}" = "0" ] then info="[root ${dir}]# " - info_console="[${COLOR_DIM}${dir}${COLOR_RESET}]# " + info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# " else info="[${user} ${dir}]$ " - info_console="[${COLOR_DIM}${dir}${COLOR_RESET}]$ " + info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ " fi printf >> "${run_logfile}" "${info}" printf >> "${run_logfile}" "%q " "${@}" printf >> "${run_logfile}" " ... " - printf >&2 "${info_console}${COLOR_BOLD}${COLOR_YELLOW}" + printf >&2 "${info_console}${TPUT_BOLD}${TPUT_YELLOW}" printf >&2 "%q " "${@}" - printf >&2 "${COLOR_RESET}\n" + printf >&2 "${TPUT_RESET}\n" "${@}" local ret=$? if [ ${ret} -ne 0 ] then - printf >&2 "${COLOR_BGRED}${COLOR_WHITE}${COLOR_BOLD} FAILED ${COLOR_RESET}\n\n" + run_failed printf >> "${run_logfile}" "FAILED with exit code ${ret}\n" else - printf >&2 "${COLOR_BGGREEN}${COLOR_WHITE}${COLOR_BOLD} OK ${COLOR_RESET}\n\n" + run_ok printf >> "${run_logfile}" "OK\n" fi @@ -146,7 +156,7 @@ portable_add_user() { local username="${1}" getent passwd "${username}" > /dev/null 2>&1 - [ $? -eq 0 ] && return 0 + [ $? -eq 0 ] && echo >&2 "User '${username}' already exists." && return 0 echo >&2 "Adding ${username} user account ..." @@ -179,7 +189,7 @@ portable_add_group() { local groupname="${1}" getent group "${groupname}" > /dev/null 2>&1 - [ $? -eq 0 ] && return 0 + [ $? -eq 0 ] && echo >&2 "Group '${groupname}' already exists." && return 0 echo >&2 "Adding ${groupname} user group ..." @@ -209,13 +219,14 @@ portable_add_user_to_group() { local groupname="${1}" username="${2}" getent group "${groupname}" > /dev/null 2>&1 - [ $? -ne 0 ] && return 1 + [ $? -ne 0 ] && echo >&2 "Group '${groupname}' does not exist." && return 1 # find the user is already in the group local users=$(getent group "${groupname}" | cut -d ':' -f 4) if [[ ",${users}," =~ ,${username}, ]] then # username is already there + echo >&2 "User '${username}' is already in group '${groupname}'." return 0 else # username is not in group diff --git a/netdata-installer.sh b/netdata-installer.sh index f8a7deea..584e2e67 100755 --- a/netdata-installer.sh +++ b/netdata-installer.sh @@ -202,25 +202,32 @@ do done netdata_banner "real-time performance monitoring, done right!" -cat <&2 -ep \$'\001${COLOR_BOLD}${COLOR_GREEN}\002Press ENTER to build and install netdata to \'\001${COLOR_CYAN}\002${NETDATA_PREFIX}\001${COLOR_YELLOW}\002\'\001${COLOR_RESET}\002 > ' -e -r REPLY" + eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to \'\001${TPUT_CYAN}\002${NETDATA_PREFIX}\001${TPUT_YELLOW}\002\'\001${TPUT_RESET}\002 > ' -e -r REPLY" else - eval "read >&2 -ep \$'\001${COLOR_BOLD}${COLOR_GREEN}\002Press ENTER to build and install netdata to your system\001${COLOR_RESET}\002 > ' -e -r REPLY" + eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to your system\001${TPUT_RESET}\002 > ' -e -r REPLY" fi fi @@ -480,7 +487,8 @@ do if [ -z "${md5sum}" -o ! -x "${md5sum}" ] then # we don't have md5sum - keep it - cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}" + echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RET}is not known to distribution${TPUT_RESET}. Keeping it." + run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}" else # find it relative filename f="${x/*\/etc\/netdata\//}" @@ -499,15 +507,15 @@ do if [ "${configs_signatures[${md5}]}" = "${f}" ] then # it is a stock version - don't keep it - echo >&2 "File '${x}' is stock version." + echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' is stock version." else # edited by user - keep it - echo >&2 "File '${x}' has been edited by user." - cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}" + echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RED} has been edited by user${TPUT_RESET}. Keeping it." + run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}" fi else - echo >&2 "File '${x}' cannot be check for custom edits." - cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}" + echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RET}cannot be checked for custom edits${TPUT_RESET}. Keeping it." + run cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}" fi fi @@ -525,14 +533,14 @@ run make install || exit 1 # ----------------------------------------------------------------------------- -progress "Restore netdata configuration files" +progress "Restore user edited netdata configuration files" for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f) do if [ -f "${x}.installer_backup.${installer_backup_suffix}" ] then - cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}" - rm -f "${x}.installer_backup.${installer_backup_suffix}" + run cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}" && \ + run rm -f "${x}.installer_backup.${installer_backup_suffix}" fi done @@ -560,9 +568,11 @@ if [ ${UID} -eq 0 ] portable_add_user_to_group varnish netdata && NETDATA_ADDED_TO_VARNISH=1 portable_add_user_to_group haproxy netdata && NETDATA_ADDED_TO_HAPROXY=1 portable_add_user_to_group adm netdata && NETDATA_ADDED_TO_ADM=1 + run_ok +else + run_failed "The installer does not run as root." fi - # ----------------------------------------------------------------------------- progress "Install logrotate configuration for netdata" @@ -858,7 +868,7 @@ stop_all_netdata() { myns="$(readlink /proc/self/ns/pid 2>/dev/null)" - echo >&2 "Stopping a (possibly) running netdata..." + # echo >&2 "Stopping a (possibly) running netdata (namespace '${myns}')..." for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \ $(cat /var/run/netdata.pid 2>/dev/null) \ @@ -974,15 +984,15 @@ progress "Check KSM (kernel memory deduper)" ksm_is_available_but_disabled() { cat </sys/kernel/mm/ksm/run${COLOR_RESET}") - $(printf "${COLOR_YELLOW}${COLOR_BOLD}echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs${COLOR_RESET}") + ${TPUT_YELLOW}${TPUT_BOLD}echo 1 >/sys/kernel/mm/ksm/run${TPUT_RESET} + ${TPUT_YELLOW}${TPUT_BOLD}echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs${TPUT_RESET} If you enable it, you will save 40-60% of netdata memory. @@ -992,7 +1002,7 @@ KSM1 ksm_is_not_available() { cat <&2 -e "Uninstall script generated: ${COLOR_YELLOW}${COLOR_BOLD}./netdata-uninstaller.sh${COLOR_RESET}" +echo >&2 "Uninstall script generated: ${TPUT_YELLOW}${TPUT_BOLD}./netdata-uninstaller.sh${TPUT_RESET}" if [ -d .git ] then @@ -1337,7 +1347,7 @@ update && exit 0 REINSTALL chmod 755 netdata-updater.sh.new mv -f netdata-updater.sh.new netdata-updater.sh - echo >&2 -e "Update script generated : ${COLOR_YELLOW}${COLOR_BOLD}./netdata-updater.sh${COLOR_RESET}" + echo >&2 "Update script generated : ${TPUT_YELLOW}${TPUT_BOLD}./netdata-updater.sh${TPUT_RESET}" elif [ -f "netdata-updater.sh" ] then rm "netdata-updater.sh" -- 2.39.2