From 81122d069343c93ca6dccf46d42f3881acf82c35 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Tue, 7 Dec 2021 21:34:19 -0600 Subject: [PATCH 01/26] Updated the useage function to use printf Using a hear doc means we have to exactly specify the formatting. Useing printf handles formatting for us --- salt/common/tools/sbin/so-status | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index cf068c86b..debf45279 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -15,21 +15,26 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +MYNAME="${0##*/}" + if ! [ "$(id -u)" = 0 ]; then - echo "This command must be run as root" + echo "${MYNAME}: This command must be run as root" exit 1 fi display_help() { -cat < Date: Tue, 7 Dec 2021 21:36:49 -0600 Subject: [PATCH 02/26] Only print color codes if we're printing to a tty If we're not printing to a tty the escape sequences can only clutter the screen. Also removed a redundant function to print lines if not printing to a tty. It was only called if docker wasn't running, not if the output wasn't a tty. --- salt/common/tools/sbin/so-status | 98 +++++++++++++++++--------------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index debf45279..78b34f157 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -175,57 +175,50 @@ parse_status() { # {% raw %} print_line() { - local service_name=${1} - local service_state="$( parse_status ${1} ${2} )" - local columns=$(tput cols) - local state_color="\e[0m" + is_tty + local service_name="${1}" + local service_state ; service_state="$( parse_status "${1}" "${2}" )" + # XXX: What will we do if tput isn't avalable? + if [ "${__tty}" -eq 1 ]; then + NC="$(tput sgr0)" # no color + red="$(tput setaf 1 bold)" + green="$(tput setaf 2 bold)" + yellow="$(tput setaf 3 bold)" + fi + state_color="${NC}" - local PADDING_CONSTANT=15 + local PADDING_CONSTANT=16 # two tabs wide - if [[ $service_state = "$ERROR_STRING" ]] || [[ $service_state = "$MISSING_STRING" ]]; then - state_color="\e[1;31m" - if [[ "$EXITCODE" -eq 0 ]]; then - EXITCODE=1 - fi + if [[ $service_state = "$ERROR_STRING" ]] \ + || [[ $service_state = "$MISSING_STRING" ]]; then + state_color="${red:-}" + if [[ "$EXITCODE" -eq 0 ]]; then + EXITCODE=1 + fi elif [[ $service_state = "$SUCCESS_STRING" ]]; then - state_color="\e[1;32m" - elif [[ $service_state = "$PENDING_STRING" ]] || [[ $service_state = "$DISABLED_STRING" ]] || [[ $service_state = "$STARTING_STRING" ]] || [[ $service_state = "$WAIT_START_STRING" ]]; then - state_color="\e[1;33m" - EXITCODE=2 + state_color="${green:-}" + elif [[ $service_state = "$PENDING_STRING" ]] \ + || [[ $service_state = "$DISABLED_STRING" ]] \ + || [[ $service_state = "$STARTING_STRING" ]] \ + || [[ $service_state = "$WAIT_START_STRING" ]]; then + state_color="${yellow:-}" + EXITCODE=2 fi - printf " $service_name " - for i in $(seq 0 $(( $columns - $PADDING_CONSTANT - ${#service_name} - ${#service_state} ))); do - printf "${state_color}%b\e[0m" "-" - done - printf " [ " - printf "${state_color}%b\e[0m" "$service_state" - printf "%s \n" " ]" -} - -non_term_print_line() { - local service_name=${1} - local service_state="$( parse_status ${1} ${2} )" - - if [[ $service_state = "$ERROR_STRING" ]] || [[ $service_state = "$MISSING_STRING" ]]; then - if [[ "$EXITCODE" -eq 0 ]]; then - EXITCODE=1 - fi - elif [[ $service_state = "$PENDING_STRING" ]] || [[ $service_state = "$DISABLED_STRING" ]] || [[ $service_state = "$STARTING_STRING" ]] || [[ $service_state = "$WAIT_START_STRING" ]]; then - EXITCODE=2 + line="" + if [ "${__tty}" -eq 1 ]; then + # construct a line of '------' so that the names and states are all aligned + local columns ; columns=$(tput cols) + linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} )) + for i in $(seq 0 "${linewidth}"); do + line="${line}-" + done fi - - printf " $service_name " - for i in $(seq 0 $(( 35 - ${#service_name} - ${#service_state} ))); do - printf "-" - done - printf " [ " - printf "$service_state" - printf "%s \n" " ]" + service_state="${state_color:-}${service_state}${NC:-}" + printf "\t%s %s [ %s ]\n" "${service_name}" "${line}" "${service_state}" } main() { - # if running from salt if [ "$CALLER" == 'salt-call' ] || [ "$CALLER" == 'salt-minion' ]; then printf "\n" @@ -233,9 +226,9 @@ main() { systemctl is-active --quiet docker if [[ $? = 0 ]]; then - non_term_print_line "Docker" "running" + print_line "Docker" "running" else - non_term_print_line "Docker" "exited" + print_line "Docker" "exited" fi populate_container_lists @@ -246,7 +239,7 @@ main() { local num_containers=${#container_name_list[@]} for i in $(seq 0 $(($num_containers - 1 ))); do - non_term_print_line ${container_name_list[$i]} ${container_state_list[$i]} + print_line ${container_name_list[$i]} ${container_state_list[$i]} done printf "\n" @@ -311,6 +304,21 @@ while getopts ':hq' OPTION; do esac done + + +is_tty() { + __tty=0 + # don't print colors if NO_COLOR is set to anything + if [ "${#NO_COLOR}" -eq 0 ] + then + __tty=0 + return "${__tty}" + fi + tty -s 2<&- >/dev/null \ + && __tty=1 + return "${__tty}" +} + main exit $EXITCODE From a048de65ca8d42a3770400fa60a7da3dd0a2efcf Mon Sep 17 00:00:00 2001 From: James Conroy Date: Tue, 7 Dec 2021 21:55:45 -0600 Subject: [PATCH 03/26] Print help message if not running as root --- salt/common/tools/sbin/so-status | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 78b34f157..0350acc4b 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -17,11 +17,6 @@ MYNAME="${0##*/}" -if ! [ "$(id -u)" = 0 ]; then - echo "${MYNAME}: This command must be run as root" - exit 1 -fi - display_help() { printf "%s" "${MYNAME}" printf " [%s]" "-h" "-q|--quiet" @@ -30,11 +25,13 @@ display_help() { "h" "Show this message." \ "q,--quiet" "suppress the output and return a single status code of overall status" printf "\nReturn Codes:\n" - printf "\t%s:%s\n" \ + printf "\t%s - %s\n" \ "0" "Ok" \ "1" "Error" \ "2" "Starting/Pending"\ "99" "Installing SO" + # exit with code 0, unless passed a number when called + exit "${1:-0}" } # Constants @@ -319,6 +316,19 @@ is_tty() { return "${__tty}" } +if ! [ "$(id -u)" = 0 ]; then + echo "${MYNAME}: This command must be run as root" + display_help 1 +fi + +while getopts ':hq' OPTION; do + case "$OPTION" in + h) display_help 0 ;; + q) QUIET=true ;; + \?) display_help 1 ;; + esac +done + main exit $EXITCODE From caa06b026ffe1cf938ce22d9aa7a2511d094d632 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Tue, 7 Dec 2021 21:56:21 -0600 Subject: [PATCH 04/26] Refactored to reduce length and number of lines --- salt/common/tools/sbin/so-status | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 0350acc4b..9ff381872 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -184,8 +184,6 @@ print_line() { fi state_color="${NC}" - local PADDING_CONSTANT=16 # two tabs wide - if [[ $service_state = "$ERROR_STRING" ]] \ || [[ $service_state = "$MISSING_STRING" ]]; then state_color="${red:-}" @@ -205,6 +203,8 @@ print_line() { line="" if [ "${__tty}" -eq 1 ]; then # construct a line of '------' so that the names and states are all aligned + local PADDING_CONSTANT=16 # two tabs wide + PADDING_CONSTANT=$((PADDING_CONSTANT + PADDING_CONSTANT / 2)) local columns ; columns=$(tput cols) linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} )) for i in $(seq 0 "${linewidth}"); do @@ -285,28 +285,10 @@ main() { # {% endraw %} -while getopts ':hq' OPTION; do - case "$OPTION" in - h) - display_help - exit 0 - ;; - q) - QUIET=true - ;; - \?) - display_help - exit 0 - ;; - esac -done - - - is_tty() { __tty=0 # don't print colors if NO_COLOR is set to anything - if [ "${#NO_COLOR}" -eq 0 ] + if [ "${#NO_COLOR}" -ne 0 ] then __tty=0 return "${__tty}" From 715f9da6e28511a205f2f75205fb3e6a15552b0a Mon Sep 17 00:00:00 2001 From: James Conroy Date: Wed, 8 Dec 2021 18:15:24 -0600 Subject: [PATCH 05/26] Reworked tty detection and status printing I was able to reduce the line count and make the script more reliable --- salt/common/tools/sbin/so-status | 65 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 9ff381872..7fc5e761a 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -174,15 +174,25 @@ parse_status() { print_line() { is_tty local service_name="${1}" - local service_state ; service_state="$( parse_status "${1}" "${2}" )" + local service_state="" ; service_state="$( parse_status "${1}" "${2}" )" # XXX: What will we do if tput isn't avalable? - if [ "${__tty}" -eq 1 ]; then - NC="$(tput sgr0)" # no color - red="$(tput setaf 1 bold)" - green="$(tput setaf 2 bold)" - yellow="$(tput setaf 3 bold)" + if (( __tty == 1 )); then + local NC; NC="$(tput sgr0)" # no color + local red=""; red="$(tput setaf 1 bold)" + local green; green="$(tput setaf 2 bold)" + local yellow; yellow="$(tput setaf 3 bold)" + + # construct a line of '------' so that the names and states are all aligned + local PADDING_CONSTANT=16 # two tabs wide + PADDING_CONSTANT=$((PADDING_CONSTANT + PADDING_CONSTANT / 2)) + local columns ; columns=$(tput cols) + linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} )) + + local line="" + for i in $(seq 0 "${linewidth}"); do + line="${line}-" + done fi - state_color="${NC}" if [[ $service_state = "$ERROR_STRING" ]] \ || [[ $service_state = "$MISSING_STRING" ]]; then @@ -200,19 +210,9 @@ print_line() { EXITCODE=2 fi - line="" - if [ "${__tty}" -eq 1 ]; then - # construct a line of '------' so that the names and states are all aligned - local PADDING_CONSTANT=16 # two tabs wide - PADDING_CONSTANT=$((PADDING_CONSTANT + PADDING_CONSTANT / 2)) - local columns ; columns=$(tput cols) - linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} )) - for i in $(seq 0 "${linewidth}"); do - line="${line}-" - done - fi service_state="${state_color:-}${service_state}${NC:-}" - printf "\t%s %s [ %s ]\n" "${service_name}" "${line}" "${service_state}" + line="${state_color:-}${line:-}${NC:-}" + printf "\t%s %s [ %s ]\n" "${service_name}" "${line:-}" "${service_state}" } main() { @@ -230,8 +230,7 @@ main() { populate_container_lists - printf "\n" - printf "Checking container statuses\n\n" + printf "\nChecking container statuses\n\n" local num_containers=${#container_name_list[@]} @@ -252,9 +251,13 @@ main() { else print_or_parse="print_line" - local focus_color="\e[1;34m" - printf "\n" - printf "${focus_color}%b\e[0m" "Checking Docker status\n\n" + if (( __tty == 1 )) + then + local focus_color="" focus_color="$(tput setaf 3 bold)" + local NC="" + NC="$(tput sgr0)" # no color + fi + printf "\n${focus_color:-}%s${NC:-}\n\n" "Checking Docker status" fi systemctl is-active --quiet docker @@ -267,8 +270,7 @@ main() { populate_container_lists if [ "$QUIET" = false ]; then - printf "\n" - printf "${focus_color}%b\e[0m" "Checking container statuses\n\n" + printf "\n${focus_color:-}%s${NC:-}\n\n" "Checking container statuses" fi local num_containers=${#container_name_list[@]} @@ -287,14 +289,11 @@ main() { is_tty() { __tty=0 - # don't print colors if NO_COLOR is set to anything - if [ "${#NO_COLOR}" -ne 0 ] - then - __tty=0 - return "${__tty}" - fi - tty -s 2<&- >/dev/null \ + [ -t 1 ] \ && __tty=1 + # don't print colors if NO_COLOR is set to anything + [ "${#NO_COLOR}" -ne 0 ] \ + && __tty=0 return "${__tty}" } From ac5527e1ab4ed1efe1061b6589702c2d3a76d187 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Wed, 8 Dec 2021 18:16:46 -0600 Subject: [PATCH 06/26] Added Comments for future enhancements --- salt/common/tools/sbin/so-status | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 7fc5e761a..1b5e1d561 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -103,9 +103,13 @@ create_expected_container_list() { } populate_container_lists() { + # TODO: check exit code directly, not with $? systemctl is-active --quiet docker if [[ $? = 0 ]]; then + # TODO: why use curl when you can just run docker ps? + # TODO: why use an indexed array when bash has associtive arrays? + # TODO: why use an array when you can just use a string? mapfile -t docker_raw_list < <(curl -s --unix-socket /var/run/docker.sock http:/v1.40/containers/json?all=1 \ | jq -c '.[] | { Name: .Names[0], State: .State }' \ | tr -d '/{"}') From 2993a2094703864a8759e8b0af3e70f16c2098d2 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Wed, 8 Dec 2021 18:18:27 -0600 Subject: [PATCH 07/26] Moved line declaration out of tty conditional This way it will always be set to "" --- salt/common/tools/sbin/so-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 1b5e1d561..ab0842d46 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -180,6 +180,7 @@ print_line() { local service_name="${1}" local service_state="" ; service_state="$( parse_status "${1}" "${2}" )" # XXX: What will we do if tput isn't avalable? + local line="" if (( __tty == 1 )); then local NC; NC="$(tput sgr0)" # no color local red=""; red="$(tput setaf 1 bold)" @@ -192,7 +193,6 @@ print_line() { local columns ; columns=$(tput cols) linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} )) - local line="" for i in $(seq 0 "${linewidth}"); do line="${line}-" done From fd9a03a77f61c2bfe79c03b9707b49b95d8100dd Mon Sep 17 00:00:00 2001 From: James Conroy Date: Mon, 7 Feb 2022 20:47:35 -0600 Subject: [PATCH 08/26] Added Changes Suggested by Reviewer Added a missing semi colon between a local variable's declaration and assignment Removed an unused return value Made a TODO more descriptive --- salt/common/tools/sbin/so-status | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index ab0842d46..7b76eeb4e 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -107,9 +107,9 @@ populate_container_lists() { systemctl is-active --quiet docker if [[ $? = 0 ]]; then - # TODO: why use curl when you can just run docker ps? - # TODO: why use an indexed array when bash has associtive arrays? - # TODO: why use an array when you can just use a string? + # TODO: look into using docker templates instead of curl and jq + # Ex docker ps --format "{{.Names}}\t{{.State}}" + # TODO: convert the output to an associtive array mapfile -t docker_raw_list < <(curl -s --unix-socket /var/run/docker.sock http:/v1.40/containers/json?all=1 \ | jq -c '.[] | { Name: .Names[0], State: .State }' \ | tr -d '/{"}') @@ -255,9 +255,8 @@ main() { else print_or_parse="print_line" - if (( __tty == 1 )) - then - local focus_color="" focus_color="$(tput setaf 3 bold)" + if (( __tty == 1 )) ; then + local focus_color="" ; focus_color="$(tput setaf 3 bold)" local NC="" NC="$(tput sgr0)" # no color fi @@ -298,7 +297,6 @@ is_tty() { # don't print colors if NO_COLOR is set to anything [ "${#NO_COLOR}" -ne 0 ] \ && __tty=0 - return "${__tty}" } if ! [ "$(id -u)" = 0 ]; then From 7f694c17edb4ae691bff4916d037685d0b198fcf Mon Sep 17 00:00:00 2001 From: James Conroy Date: Tue, 8 Feb 2022 20:33:09 -0600 Subject: [PATCH 09/26] Revert improvements to usage function Made to make it more consistent with the rest of the scripts in Security Onion --- salt/common/tools/sbin/so-status | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 7b76eeb4e..15f4fd0ae 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -18,20 +18,15 @@ MYNAME="${0##*/}" display_help() { - printf "%s" "${MYNAME}" - printf " [%s]" "-h" "-q|--quiet" - printf "\n" - printf "\t-%s\t%s\n" \ - "h" "Show this message." \ - "q,--quiet" "suppress the output and return a single status code of overall status" - printf "\nReturn Codes:\n" - printf "\t%s - %s\n" \ - "0" "Ok" \ - "1" "Error" \ - "2" "Starting/Pending"\ - "99" "Installing SO" - # exit with code 0, unless passed a number when called - exit "${1:-0}" +cat < Date: Tue, 8 Feb 2022 20:42:45 -0600 Subject: [PATCH 10/26] Removed MYNAME variable Preferring to just use the value of $0 instead --- salt/common/tools/sbin/so-status | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 15f4fd0ae..428631b51 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -15,7 +15,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -MYNAME="${0##*/}" display_help() { cat < Date: Tue, 8 Feb 2022 20:54:39 -0600 Subject: [PATCH 11/26] Always print a line of '-' Even when not printing to a tty This is behavior preferred by the team --- salt/common/tools/sbin/so-status | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 428631b51..b4d9649f0 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -175,23 +175,24 @@ print_line() { local service_state="" ; service_state="$( parse_status "${1}" "${2}" )" # XXX: What will we do if tput isn't avalable? local line="" + local columns=35 # value used if not printing to a tty + local PADDING_CONSTANT=16 # two tabs wide + if (( __tty == 1 )); then local NC; NC="$(tput sgr0)" # no color - local red=""; red="$(tput setaf 1 bold)" + local red; red="$(tput setaf 1 bold)" local green; green="$(tput setaf 2 bold)" local yellow; yellow="$(tput setaf 3 bold)" - # construct a line of '------' so that the names and states are all aligned - local PADDING_CONSTANT=16 # two tabs wide - PADDING_CONSTANT=$((PADDING_CONSTANT + PADDING_CONSTANT / 2)) - local columns ; columns=$(tput cols) - linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} )) - - for i in $(seq 0 "${linewidth}"); do - line="${line}-" - done + columns=$(tput cols) fi + # construct a line of '------' so that the names and states are all aligned + linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} )) + for i in $(seq 0 "${linewidth}"); do + line="${line}-" + done + if [[ $service_state = "$ERROR_STRING" ]] \ || [[ $service_state = "$MISSING_STRING" ]]; then state_color="${red:-}" From 6b4549499def5b6469bfda3c3924dca9cbef9e88 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Tue, 8 Feb 2022 20:56:06 -0600 Subject: [PATCH 12/26] Don't split lines after standalone tests This is to make the formatting consistent with the rest of the scripts --- salt/common/tools/sbin/so-status | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index b4d9649f0..4e4331321 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -287,11 +287,9 @@ main() { is_tty() { __tty=0 - [ -t 1 ] \ - && __tty=1 + [ -t 1 ] && __tty=1 # don't print colors if NO_COLOR is set to anything - [ "${#NO_COLOR}" -ne 0 ] \ - && __tty=0 + [ "${#NO_COLOR}" -ne 0 ] && __tty=0 } if ! [ "$(id -u)" = 0 ]; then From 163182c858e35ca52c8f1106da7f43e6aee631a5 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Wed, 9 Feb 2022 19:31:31 -0600 Subject: [PATCH 13/26] Don't set the padding constant if not in a tty This will preserve the original width from before my changes --- salt/common/tools/sbin/so-status | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 4e4331321..fe353e126 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -170,19 +170,19 @@ parse_status() { # {% raw %} print_line() { - is_tty local service_name="${1}" local service_state="" ; service_state="$( parse_status "${1}" "${2}" )" # XXX: What will we do if tput isn't avalable? local line="" + local PADDING_CONSTANT="" local columns=35 # value used if not printing to a tty - local PADDING_CONSTANT=16 # two tabs wide if (( __tty == 1 )); then local NC; NC="$(tput sgr0)" # no color local red; red="$(tput setaf 1 bold)" local green; green="$(tput setaf 2 bold)" local yellow; yellow="$(tput setaf 3 bold)" + PADDING_CONSTANT=16 # two tabs wide columns=$(tput cols) fi From bfcfad2e7da8c01ab5b8881f5acf795df5117c38 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Wed, 9 Feb 2022 19:41:51 -0600 Subject: [PATCH 14/26] Check for tty in main So that the value is set every time it is checked --- salt/common/tools/sbin/so-status | 1 + 1 file changed, 1 insertion(+) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index fe353e126..52046d0cc 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -215,6 +215,7 @@ print_line() { } main() { + is_tty # if running from salt if [ "$CALLER" == 'salt-call' ] || [ "$CALLER" == 'salt-minion' ]; then printf "\n" From b9b3876069d73f6b2ac11c385ba05470b8478b62 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Wed, 9 Feb 2022 19:44:07 -0600 Subject: [PATCH 15/26] Exit with an error code if the user isn't root --- salt/common/tools/sbin/so-status | 1 + 1 file changed, 1 insertion(+) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 52046d0cc..a2b430ea8 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -295,6 +295,7 @@ is_tty() { if ! [ "$(id -u)" = 0 ]; then echo "${0}: This command must be run as root" + exit 1 fi while getopts ':hq' OPTION; do From dfcabb57227be1572dac1c53be9e6cc188a5f302 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:32:14 -0600 Subject: [PATCH 16/26] Seperate bold attribute from colors As suggested by @rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index a2b430ea8..16b5233d0 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -178,10 +178,11 @@ print_line() { local columns=35 # value used if not printing to a tty if (( __tty == 1 )); then - local NC; NC="$(tput sgr0)" # no color - local red; red="$(tput setaf 1 bold)" - local green; green="$(tput setaf 2 bold)" - local yellow; yellow="$(tput setaf 3 bold)" + local reset_attr; reset_attr="$(tput sgr0)" # reset all attributes + local bold; bold="$(tput bold)" + local red; red="$(tput setaf 1)" + local green; green="$(tput setaf 2)" + local yellow; yellow="$(tput setaf 3)" PADDING_CONSTANT=16 # two tabs wide columns=$(tput cols) From c69e9687903b58a125eb8200fbb091318ba5816a Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:33:25 -0600 Subject: [PATCH 17/26] Renamed Colors to Attributes As suggested by @rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 16b5233d0..8f6885d0a 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -210,7 +210,7 @@ print_line() { EXITCODE=2 fi - service_state="${state_color:-}${service_state}${NC:-}" + service_state="${bold:-}${state_color:-}${service_state}${reset_attr:-}" line="${state_color:-}${line:-}${NC:-}" printf "\t%s %s [ %s ]\n" "${service_name}" "${line:-}" "${service_state}" } From dabae3888f3223b9f1d16888bec87b8b6ed3ae43 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:34:19 -0600 Subject: [PATCH 18/26] Renamed colors to attributes As suggested by rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 8f6885d0a..48fbd4464 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -211,7 +211,7 @@ print_line() { fi service_state="${bold:-}${state_color:-}${service_state}${reset_attr:-}" - line="${state_color:-}${line:-}${NC:-}" + line="${bold:-}${state_color:-}${line:-}${reset_attr:-}" printf "\t%s %s [ %s ]\n" "${service_name}" "${line:-}" "${service_state}" } From 3adb6c1389b9c92caf6c4b1905a8a6f1eef20e6a Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:35:08 -0600 Subject: [PATCH 19/26] Renamed colors to attributes Also correctly used tput to assign blue color As suggested by @rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 48fbd4464..f553b13b7 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -253,9 +253,9 @@ main() { print_or_parse="print_line" if (( __tty == 1 )) ; then - local focus_color="" ; focus_color="$(tput setaf 3 bold)" - local NC="" - NC="$(tput sgr0)" # no color + local bold; bold="$(tput bold)" + local focus_color; focus_color="$(tput setaf 4)" + local reset_attr; reset_attr="$(tput sgr0)" # reset all attributes fi printf "\n${focus_color:-}%s${NC:-}\n\n" "Checking Docker status" fi From 01346cbb069f73418a4fc5135dffc4920acd2e69 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:35:44 -0600 Subject: [PATCH 20/26] Changed color variables to Attributes As suggested by @rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index f553b13b7..eb75a3ef1 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -257,7 +257,7 @@ main() { local focus_color; focus_color="$(tput setaf 4)" local reset_attr; reset_attr="$(tput sgr0)" # reset all attributes fi - printf "\n${focus_color:-}%s${NC:-}\n\n" "Checking Docker status" + printf "\n${bold}${focus_color:-}%s${reset_attr:-}\n\n" "Checking Docker status" fi systemctl is-active --quiet docker From 6a1e586b8cebaa4d02eb397a3c03a9d1a4d57ad2 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:36:05 -0600 Subject: [PATCH 21/26] Changed color variables to Attributes As suggested by @rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index eb75a3ef1..9bdfd8971 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -270,7 +270,7 @@ main() { populate_container_lists if [ "$QUIET" = false ]; then - printf "\n${focus_color:-}%s${NC:-}\n\n" "Checking container statuses" + printf "\n${bold}${focus_color:-}%s${reset_attr:-}\n\n" "Checking container statuses" fi local num_containers=${#container_name_list[@]} From 3046e811f027bdb2bdaf58c0624c95241252e13e Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:36:59 -0600 Subject: [PATCH 22/26] Use spaces to define centerd justification output As suggested by @rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 9bdfd8971..a81cb9d27 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -212,7 +212,7 @@ print_line() { service_state="${bold:-}${state_color:-}${service_state}${reset_attr:-}" line="${bold:-}${state_color:-}${line:-}${reset_attr:-}" - printf "\t%s %s [ %s ]\n" "${service_name}" "${line:-}" "${service_state}" + printf " %s %s [ %s ] \n" "${service_name}" "${line:-}" "${service_state}" } main() { From 95b4f7b4efb30eb891946dfc295e82da6fa6214e Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 10 Feb 2022 19:37:23 -0600 Subject: [PATCH 23/26] Update the PADDING_CONSTENT to 15 As suggested by @rwwiv Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index a81cb9d27..a8032a849 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -183,7 +183,7 @@ print_line() { local red; red="$(tput setaf 1)" local green; green="$(tput setaf 2)" local yellow; yellow="$(tput setaf 3)" - PADDING_CONSTANT=16 # two tabs wide + PADDING_CONSTANT=15 # whitespace + brackets + 1 columns=$(tput cols) fi From a43ac2aea2b59c5560aab914d8cc50586eaa36b8 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Sat, 12 Feb 2022 12:22:59 -0600 Subject: [PATCH 24/26] Move the jinja endraw directive below is_tty This will prevent jninja from interpreting the shell string length expansion as the start of jninja comments --- salt/common/tools/sbin/so-status | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index a8032a849..ef3f98176 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -285,8 +285,6 @@ main() { fi } -# {% endraw %} - is_tty() { __tty=0 [ -t 1 ] && __tty=1 @@ -294,6 +292,8 @@ is_tty() { [ "${#NO_COLOR}" -ne 0 ] && __tty=0 } +# {% endraw %} + if ! [ "$(id -u)" = 0 ]; then echo "${0}: This command must be run as root" exit 1 From b774e62dfa0f60cc427fe60da2e9161a9e2af29c Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 17 Feb 2022 20:37:25 -0600 Subject: [PATCH 25/26] Update salt/common/tools/sbin/so-status Add salt raw directive Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 1 + 1 file changed, 1 insertion(+) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index ef3f98176..bce9e92bb 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -96,6 +96,7 @@ create_expected_container_list() { } +# {% raw %} populate_container_lists() { # TODO: check exit code directly, not with $? systemctl is-active --quiet docker From 91c207cd38eec362c307c5cbf41b0c71fd1f470b Mon Sep 17 00:00:00 2001 From: James Conroy Date: Thu, 17 Feb 2022 20:37:43 -0600 Subject: [PATCH 26/26] Update salt/common/tools/sbin/so-status Removed # {% raw %} from line 170 Co-authored-by: William Wernert --- salt/common/tools/sbin/so-status | 1 - 1 file changed, 1 deletion(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index bce9e92bb..61db01ada 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -168,7 +168,6 @@ parse_status() { fi } -# {% raw %} print_line() { local service_name="${1}"