Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit 2927af5

Browse files
committed
Merge branch '165-all-checks-glued-together-several-times' into 'master'
Resolve "All checks glued together several times" Closes #165 See merge request postgres.ai/postgres-health-check!158
2 parents fd04f26 + 3755a86 commit 2927af5

File tree

1 file changed

+74
-57
lines changed

1 file changed

+74
-57
lines changed

check

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ read_possible_args() {
191191
#######################################
192192
process_cli_args() {
193193
local cli_arg_cur_value i
194-
local re='^-+'
194+
local re='^-'
195195
local while_loops_cnt=0
196196
local argvalue
197197
dbg "Valid CLI args possible count: ${CLI_ARGS_POSSIBLE}+1"
@@ -205,31 +205,26 @@ process_cli_args() {
205205
if [[ ! -z "${1+x}" ]]; then
206206
case $1 in
207207
"-${SHORT_NAME[$i]}" | "--${FULL_NAME[$i]}" )
208-
if [[ ! -z "${2+x}" ]]; then
209-
argvalue="$2"
210-
# set true for arguments without value (e.g., '--force')
211-
elif [[ "${ARG_TYPE[$i]}" = "None" ]]; then
212-
argvalue="true"
213-
else
214-
argvalue="None"
215-
err "empty value for variable '--${FULL_NAME[$i]}'"
216-
exit 1
217-
fi
218-
[[ -z "$argvalue" ]] && exit 1
219-
dbg "argname: '$1', argvalue '${argvalue}' "
220-
if [[ "$argvalue" =~ $re ]]; then
221-
err "empty value in '--${FULL_NAME[$i]}'"
222-
exit 1
223-
fi
224-
# save argument value as an indexed array
225-
ARG_VALUE[$i]="$argvalue"
226-
ARG_IS_GIVEN[$i]="true"
227-
CLI_ARGS_CNT=$(( CLI_ARGS_CNT + 1 ))
208+
# argument without value (like '--force', ARG_TYPE is 'None')
228209
if [[ "${ARG_TYPE[$i]}" = "None" ]]; then
210+
ARG_VALUE[$i]="true"
211+
ARG_IS_GIVEN[$i]="true"
229212
shift 1
213+
# argument with value
230214
else
215+
if [[ -z "${2+x}" ]]; then
216+
err "empty value for variable '--${FULL_NAME[$i]}'"
217+
exit 1
218+
fi
219+
if [[ "${2}" =~ $re ]]; then
220+
err "empty value for variable '--${FULL_NAME[$i]}'"
221+
exit 1
222+
fi
223+
ARG_VALUE[$i]="${2}"
224+
ARG_IS_GIVEN[$i]="true"
231225
shift 2
232226
fi
227+
CLI_ARGS_CNT=$(( CLI_ARGS_CNT + 1 ))
233228
esac
234229
fi
235230
done
@@ -545,7 +540,7 @@ generate_report_json() {
545540
> "$tmp_output_json_fname"
546541
mv "$tmp_output_json_fname" "${json_output_fname}"
547542

548-
msg "Artifacts saved at: '${json_output_fname}'"
543+
msg "JSON report saved at: '${json_output_fname}'"
549544
}
550545

551546
#######################################
@@ -705,34 +700,36 @@ check_bin_deps() {
705700
}
706701

707702
#######################################
708-
# Add the newly generated report to a fileф
703+
# Glue all .md file together
709704
# (makes final report)
710705
# Globals:
711706
# PROJECT_DIR, FULL_REPORT_FNAME, EPOCH,
712-
# DBNAME, HOST
707+
# DBNAME, HOST, MD_REPORTS_DIR
713708
# Arguments:
714-
# md_reports_dir, report_fname
709+
# None
715710
# Returns:
716711
# Integer
717712
#######################################
718-
append_to_final_report() {
719-
local md_reports_dir="$1"
720-
local report_fname="$2" # (e.g., 'A001*')
721-
713+
glue_md_reports() {
722714
local out_fname="${PROJECT_DIR}/e${EPOCH}_${FULL_REPORT_FNAME}"
723715

724-
# make a header for a FULL_REPORT_FNAME at the first run
725-
if [[ ! -f "${out_fname}" ]]; then
726-
echo "# Project '${PROJECT}' PostgreSQL health check" > "${out_fname}"
727-
echo "## Epoch: '${EPOCH}'" >> "${out_fname}"
728-
echo "NOTICE: some reports may contain information for a whole cluster databases" >> "${out_fname}"
729-
echo >> "${out_fname}"
730-
echo "Started at: " $(date +'%Y-%m-%d %H:%M:%S %z') >> "${out_fname}"
731-
echo >> "${out_fname}"
732-
fi
733-
734-
# glue together
735-
cat "${md_reports_dir}/${report_fname}" >> "${out_fname}"
716+
# do not re-generate full report if '--file' is given
717+
[[ "${FILE}" != "None" ]] && return 0
718+
719+
# make header
720+
echo "# Project '${PROJECT}' PostgreSQL health check" > "${out_fname}"
721+
echo "## Epoch: '${EPOCH}'" >> "${out_fname}"
722+
echo "NOTICE: some reports may contain information for a whole cluster databases" >> "${out_fname}"
723+
echo >> "${out_fname}"
724+
echo "Last modified at: " $(date +'%Y-%m-%d %H:%M:%S %z') >> "${out_fname}"
725+
echo >> "${out_fname}"
726+
727+
# glue reports together
728+
local cur_report=""
729+
for cur_report in "${MD_REPORTS_DIR}"/*.md; do
730+
[[ -e "${cur_report}" ]] || continue
731+
cat "${cur_report}" >> "${out_fname}"
732+
done
736733
}
737734

738735
#######################################
@@ -924,7 +921,8 @@ update_nodes_json() {
924921
run_checks() {
925922
# perform pre-checks here
926923

927-
local pghrep_failed_jsons_stack=""
924+
local pghrep_failed_md_stack=""
925+
local check_failed_json_stack=""
928926

929927
export PROJECT_DIR="${SCRIPT_DIR}/artifacts/${PROJECT}"
930928
test -d "${PROJECT_DIR}" || mkdir -p "${PROJECT_DIR}"
@@ -941,13 +939,13 @@ run_checks() {
941939
if [[ "${FILE}" != "None" ]]; then
942940
CURRENT_CHECK_FNAME="${FILE}"
943941
if ! [[ -e "${CURRENT_CHECK_FNAME}" ]]; then
944-
err "Can't open file: '{CURRENT_CHECK_FNAME}'"
942+
err "Can't open file: '${CURRENT_CHECK_FNAME}'"
945943
fi
946944
fi
947945
msg "========== Perform check: =========="
948-
msg "$CURRENT_CHECK_FNAME"
946+
msg "${CURRENT_CHECK_FNAME}"
949947

950-
local check_is_failed=false
948+
local check_is_failed="false"
951949

952950
# fix timestamp before check
953951
export TIMESTAMP_DIR=$(date +'%Y_%m_%dT%H_%M_%S_%z') # for use in directories names
@@ -969,7 +967,12 @@ run_checks() {
969967

970968
if [[ "$check_is_failed" == "true" ]]; then
971969
msg "WARNING: check '${check_id}' failed with error ^^^"
972-
continue
970+
check_failed_json_stack="${check_failed_json_stack} ${check_id}_${check_name}.json"
971+
if [[ "${FILE}" != "None" ]]; then
972+
break
973+
else
974+
continue
975+
fi
973976
else
974977
msg "check '$check_id' is finished"
975978
fi
@@ -986,9 +989,9 @@ run_checks() {
986989
if ! pghrep_output=$(${PGHREP_BIN} --checkdata="${JSON_REPORTS_DIR}/${check_id}_${check_name}.json" \
987990
--outdir="${MD_REPORTS_DIR}" 2>&1) ; then
988991
msg "WARNING: ${pghrep_output}"
989-
pghrep_failed_jsons_stack="${pghrep_failed_jsons_stack} ${check_id}_${check_name}.json"
992+
pghrep_failed_md_stack="${pghrep_failed_md_stack} ${check_id}_${check_name}.md"
990993
else
991-
append_to_final_report "${MD_REPORTS_DIR}" "${check_id}_${check_name}.md"
994+
msg "Markdown report saved at: '${MD_REPORTS_DIR}/${check_id}_${check_name}.md'"
992995
fi
993996

994997
# parse only one file if check is given by CLI
@@ -999,22 +1002,35 @@ run_checks() {
9991002

10001003
msg
10011004
msg "All checks has been finished for host '$HOST'!"
1002-
1003-
if ! [[ -z "${pghrep_failed_jsons_stack}" ]]; then
1005+
1006+
# print stacks with failed reports
1007+
if ! [[ -z "${check_failed_json_stack}" ]]; then
1008+
msg
1009+
msg "WARNING: Can't generate JSON reports (see errors above):"
1010+
local i=""
1011+
for i in ${check_failed_json_stack}; do
1012+
echo " '${i}'"
1013+
done
1014+
msg "Markdown reports are also not created for these checks ^^"
1015+
fi
1016+
if ! [[ -z "${pghrep_failed_md_stack}" ]]; then
10041017
msg
1005-
msg "WARNING: Can't generate md reports for this json's:"
1006-
local i
1007-
for i in ${pghrep_failed_jsons_stack}; do
1018+
msg "WARNING: Can't generate Markdown reports (see errors above):"
1019+
local i=""
1020+
for i in ${pghrep_failed_md_stack}; do
10081021
echo " '${i}'"
10091022
done
10101023
fi
10111024

1012-
msg
1013-
msg "Final .md report for host '${HOST}' and database '${DBNAME}' is ready at:"
1014-
echo " '${PROJECT_DIR}/e${EPOCH}_${FULL_REPORT_FNAME}'"
1025+
# generate final report
1026+
glue_md_reports
10151027

10161028
msg
1017-
msg "ALL DONE!"
1029+
if [[ "${FILE}" = "None" ]]; then
1030+
msg "Final .md report is ready at:"
1031+
echo " '${PROJECT_DIR}/e${EPOCH}_${FULL_REPORT_FNAME}'"
1032+
msg
1033+
fi
10181034
}
10191035

10201036
######### COMMANDS WRAPPERS ###########
@@ -1047,6 +1063,7 @@ main() {
10471063
build_pghrep
10481064
host_pre_start_checks
10491065
run_checks
1066+
msg "ALL DONE!"
10501067
exit 0
10511068
}
10521069

0 commit comments

Comments
 (0)