From 5c105f3875642e27588808ef6bdc3ac6fc23de06 Mon Sep 17 00:00:00 2001 From: Saswat Padhi Date: Tue, 1 Oct 2024 23:26:49 -0700 Subject: [PATCH] minimize down time: loop over all actions per verb --- _scripts/test_comp.sh | 18 ++++----- comp | 88 +++++++++++++++++++------------------------ 2 files changed, 47 insertions(+), 59 deletions(-) diff --git a/_scripts/test_comp.sh b/_scripts/test_comp.sh index fa08fb0..64b1ff7 100755 --- a/_scripts/test_comp.sh +++ b/_scripts/test_comp.sh @@ -57,7 +57,7 @@ function CHECK { else echo " ${_fg_red_}${_bold_}-${_normal_} $2: ${_fg_red_}${_bold_}FAILURE${_normal_}" FINAL_EXIT_CODE=1 - [ -z "${3:-}" ] || FINAL + [ -z "${3:-}" ] || _EXIT "Aborting early. " fi } @@ -71,7 +71,6 @@ function _WAIT () { COMP_COUNTER=1 # _INIT [name prefix] [name suffix] function _INIT () { - [ -z "$SKIPPING_THIS" ] || return local TARGET_COMP_VAR="TARGET_COMP_$COMP_COUNTER" local TARGET_COMP_DISPLAY_VAR="TARGET_COMP_${COMP_COUNTER}_DISPLAY" echo ; echo -n "${_fg_white_}${_bg_black_}${_bold_} I ${_normal_} Initializing ${_fg_magenta_}$1${_normal_} -> " @@ -84,12 +83,13 @@ function _INIT () { (( COMP_COUNTER++ )) } -function FINAL () { +# _EXIT [message] +function _EXIT () { echo ; printf '%.0s-' {1..100} ; echo ; echo if [ "$FINAL_EXIT_CODE" -eq 0 ] ; then - echo "${_fg_white_}${_bg_black_}${_bold_} S ${_normal_} All checks ${_fg_green_}SUCCESSFUL${_normal_}." + echo "${_fg_white_}${_bg_black_}${_bold_} S ${_normal_} ${1:-}All checks ${_fg_green_}SUCCESSFUL${_normal_}." else - echo "${_fg_white_}${_bg_black_}${_bold_} F ${_normal_} Some checks ${_fg_red_}${_bold_}FAILED${_normal_}!" + echo "${_fg_white_}${_bg_black_}${_bold_} F ${_normal_} ${1:-}Some checks ${_fg_red_}${_bold_}FAILED${_normal_}!" fi exit $FINAL_EXIT_CODE } @@ -103,9 +103,7 @@ CHECK $'[ $COMP_EXIT_CODE -eq $EXIT_CODE_USAGE_ERROR ]' \ SETUP "status of non-existent composition" \ "status unknown_comp" -CHECK $'grep -qs "Executing status on unknown_comp" "$COMP_OUT_PATH"' \ - "Info at beginning of execution" -CHECK $'grep -qs "is not a base directory" "$COMP_ERR_PATH"' \ +CHECK $'grep -qs "unknown_comp is not a base directory" "$COMP_ERR_PATH"' \ "Helpful message on stderr" CHECK $'[ $COMP_EXIT_CODE -eq $EXIT_CODE_COMPOSITION_NOT_FOUND ]'\ "EXIT_CODE_COMPOSITION_NOT_FOUND" @@ -217,7 +215,7 @@ echo "DEVICES=nyet" > $TARGET_COMP_1/options.override.conf SETUP "stopping $TARGET_COMP_1_DISPLAY with a bad option & $TARGET_COMP_2_DISPLAY without options" \ "down -P $TARGET_COMP_1 $TARGET_COMP_2" -CHECK $'grep -Pqs "Invalid value \'nyet\' for \'devices\' option in options\.override\.conf" "$COMP_ERR_PATH"' \ +CHECK $'grep -qs "Invalid value \'nyet\' for \'DEVICES\' in $TARGET_COMP_1/options\.override\.conf" "$COMP_ERR_PATH"' \ "Configuration error reported for $TARGET_COMP_1_DISPLAY" \ exit_on_failure CHECK $'grep -Pqsv "Container $TARGET_COMP_1.* Stopping" "$COMP_ERR_PATH"' \ @@ -282,4 +280,4 @@ CHECK $'[ $COMP_EXIT_CODE -eq 0 ]' \ "EXIT_CODE = 0; best-effort guess works for now" \ exit_on_failure -FINAL +_EXIT "Ran all checks. " diff --git a/comp b/comp index 0d48f73..a0cd470 100755 --- a/comp +++ b/comp @@ -167,6 +167,10 @@ __maybe_fail_fast () { return "$1" } +__print_sepator_line () { + [ -n "$COMP_INTERNAL_CALL" ] || printf '%.0s-' {1..80} +} + __read_option () { local OPTION="OPTION_$1" local OVERRIDE_OPTION="" @@ -180,9 +184,7 @@ __read_option () { [ -n "${!OPTION}" ] || printf -v "$OPTION" "%s" "yes" if [ "${!OPTION}" != "yes" ] ; then - echo -n "$opt_line_head: $1 " - [ -z "$OVERRIDE_OPTION" ] && printf "(arg) " || printf "(conf) " - opt_line_head='' + [ -z "$OVERRIDE_OPTION" ] && disabled_options+=("$1 (arg)") || disabled_options+=("$1 (conf)") else if [ -f "docker-compose.${1,,}.yml" ] ; then COMPOSE_FILES+=( "docker-compose.${1,,}.yml" ) @@ -427,7 +429,7 @@ validate_and_set_option () { yes | no ) printf -v "$OPTION" "%s" "$1" ; return 0 esac if [ "$3" = "override" ] ; then - echo ; __error "Invalid value '$1' for '${2,,}' option in options.override.conf" + echo ; __error "Invalid value '$1' for '$2' in $comp/options.override.conf" else usage "Invalid value '$1' for '${2,,}' option" fi @@ -500,39 +502,37 @@ fi COMPOSITIONS=( "$@" ) OPTIONS_FROM_ARGS=( "$OPTION_DEVICES" "$OPTION_HOOKS" "$OPTION_LABELS" "$OPTION_LOGGING" "$OPTION_PORTS" ) -perform () { - local SIMPLE_VERB="$1" - for comp in "${COMPOSITIONS[@]}" ; do - comp="${comp%/}" - echo -ne "\n${_fg_white_}${_bg_black_}${_bold_} ${SIMPLE_VERB:0:1} ${_normal_} Executing ${_bold_}$SIMPLE_VERB${_normal_} on " - [ -z "$COMP_INTERNAL_CALL" ] || printf 'pre-req ' - echo "${_bold_}$comp${_normal_} ... " +__print_sepator_line +for comp in "${COMPOSITIONS[@]}" ; do + comp="${comp%/}" - if ! { [ "$comp" = "$(basename "$comp")" ] && [ -d "$SELF_DIR/$comp" ]; } ; then - __error "${_bold_}$comp${_normal_} is not a base directory at '$SELF_DIR'!" - __maybe_fail_fast $EXIT_CODE_COMPOSITION_NOT_FOUND || continue - fi - if ! [ -f "$SELF_DIR/$comp/docker-compose.yml" ] ; then - __error "No 'docker-compose.yml' found under ${_bold_}$comp${_normal_}!" - __maybe_fail_fast $EXIT_CODE_COMPOSITION_NOT_FOUND || continue - fi + if ! { [ "$comp" = "$(basename "$comp")" ] && [ -d "$SELF_DIR/$comp" ]; } ; then + echo ; __error "${_bold_}$comp${_normal_} is not a base directory at '$SELF_DIR'!" + __maybe_fail_fast $EXIT_CODE_COMPOSITION_NOT_FOUND || continue + fi + if ! [ -f "$SELF_DIR/$comp/docker-compose.yml" ] ; then + echo ; __error "No 'docker-compose.yml' found under ${_bold_}$comp${_normal_}!" + __maybe_fail_fast $EXIT_CODE_COMPOSITION_NOT_FOUND || continue + fi - cd "$SELF_DIR/$comp" - COMPOSE_FILES=( "docker-compose.yml" ) - if [ "$FLAG_SKIP_OVERRIDES" != "yes" ] && [ -f "docker-compose.override.yml" ] ; then - COMPOSE_FILES+=( "docker-compose.override.yml") - fi + cd "$SELF_DIR/$comp" + COMPOSE_FILES=( "docker-compose.yml" ) + if [ "$FLAG_SKIP_OVERRIDES" != "yes" ] && [ -f "docker-compose.override.yml" ] ; then + COMPOSE_FILES+=( "docker-compose.override.yml") + fi - read -r OPTION_DEVICES OPTION_HOOKS OPTION_LABELS OPTION_LOGGING OPTION_PORTS <<<"${OPTIONS_FROM_ARGS[*]}" - local opt_error=0 - opt_line_head='[o] Disabled options' - for opt in DEVICES HOOKS LABELS LOGGING PORTS ; do - __read_option $opt && continue - opt_error=1 - done - [ "$opt_error" -eq 0 ] \ - || __maybe_fail_fast $EXIT_CODE_OPTIONS_CONF_ERROR || continue - [ -n "$opt_line_head" ] || echo + read -r OPTION_DEVICES OPTION_HOOKS OPTION_LABELS OPTION_LOGGING OPTION_PORTS <<<"${OPTIONS_FROM_ARGS[*]}" + disabled_options=() ; opt_error=0 + for opt in DEVICES HOOKS LABELS LOGGING PORTS ; do + __read_option $opt || opt_error=1 + done + [ "$opt_error" -eq 0 ] || __maybe_fail_fast $EXIT_CODE_OPTIONS_CONF_ERROR || continue + + for SIMPLE_VERB in $VERBS ; do + echo -ne "\n${_fg_white_}${_bg_black_}${_bold_} ${SIMPLE_VERB:0:1} ${_normal_} Executing ${_bold_}$SIMPLE_VERB${_normal_} on " + [ -z "$COMP_INTERNAL_CALL" ] || printf 'pre-req ' + echo "${_bold_}$comp${_normal_} ... " + [ ${#disabled_options[@]} -eq 0 ] || echo "[o] Disabled options: ${disabled_options[*]}" [ "$SIMPLE_VERB" = "validate" ] || do_validate shallow @@ -554,13 +554,12 @@ perform () { || __maybe_fail_fast $EXIT_CODE_SETUP_ERROR || continue fi - local verb_exit=0 - "do_${SIMPLE_VERB}" ; verb_exit=$? + verb_ret=0 ; "do_${SIMPLE_VERB}" ; verb_ret=$? - [ "$SIMPLE_VERB" != "validate" ] || [ $verb_exit -ne 0 ] || echo "[=] ${_bold_}$comp${_normal_} is valid!" - [ "$SIMPLE_VERB" != "status" ] || [ $verb_exit -ne 0 ] || echo "[=] ${_bold_}$comp${_normal_} is healthy!" + [ "$SIMPLE_VERB" != "validate" ] || [ $verb_ret -ne 0 ] || echo "[=] ${_bold_}$comp${_normal_} is valid!" + [ "$SIMPLE_VERB" != "status" ] || [ $verb_ret -ne 0 ] || echo "[=] ${_bold_}$comp${_normal_} is healthy!" - [ $verb_exit -eq 0 ] \ + [ $verb_ret -eq 0 ] \ || __maybe_fail_fast $EXIT_CODE_SIMPLE_VERB_FAILURE || continue if __will_invoke_compose "$SIMPLE_VERB" ; then @@ -571,15 +570,6 @@ perform () { [ "$OPTION_HOOKS" != "yes" ] || __run_hooks "$SIMPLE_VERB" post \ || __maybe_fail_fast $EXIT_CODE_POST_HOOK_SCRIPT_ERROR done -} - -print_sepator_line () { - [ -n "$COMP_INTERNAL_CALL" ] || printf '%.0s-' {1..80} -} - -print_sepator_line -for VERB in $VERBS ; do - perform "$VERB" - print_sepator_line + __print_sepator_line done echo ; exit "$FINAL_EXIT_CODE"