From fa3593aea138dbb71a697ee3027c5ccf9103caa8 Mon Sep 17 00:00:00 2001 From: Alexander Lutay Date: Tue, 1 Oct 2019 14:18:30 +0200 Subject: [PATCH] TT#63758 Add --summary-only option support Sometimes (e.g. on 'push-parallel') we need to call ngcpcfg on remote host and generating summary only output about the results. It simplifies complex 'push-parallel' codebase which is critical for huge Carrier installation. P.S. we cannot print summary message all the time to console output, as a lot of code depends on the ngcpcfg output, e.g.: > root@web01a:~# ngcp-ppa gerrit_alutay_63758_push_parallel_2 > ERROR: There are uncommitted changes in config files: > 2019-10-02 11:59:38 web01a: Successfully executed 'diff' on 'web01a' > ERROR: Please commit them or discard > root@web01a:~# Instead, let's be backward compatible with printing on console, but always print this message to log file (as we sometimes need to know ngcpcfg exit code and it was impossible to find it in log file previously.) Change-Id: I9aad0912796500cddb23d1f3eaa428b0b39152d0 --- docs/ngcpcfg.txt | 5 +++++ functions/logs | 17 +++++++++++------ sbin/ngcpcfg | 25 +++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/docs/ngcpcfg.txt b/docs/ngcpcfg.txt index 8b58ad73..08b79eb8 100644 --- a/docs/ngcpcfg.txt +++ b/docs/ngcpcfg.txt @@ -277,6 +277,11 @@ some initial files have to be generated from templates before ngcpcfg final setu Ignore schema validation results for YAML files (syntax check is still performed). + **--summary-only** []:: + +Hide all the output except the last summary line. +Useful for integration of ngcpcfg in other scripts/tools. + **--validate** []:: Force schema validation for YAML files even if validation is disabled in config. diff --git a/functions/logs b/functions/logs index 7b71a5e9..e593019d 100644 --- a/functions/logs +++ b/functions/logs @@ -21,35 +21,40 @@ console_output() { # }}} ## logging functions {{{ -log_info() { +log_only() { logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "$*" +} + +log_info() { + log_only "$*" console_output "$*\n" } # info without ending newline log_info_n() { - logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "$*" + log_only "$*" console_output "$*" } log_warn() { - logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Warning: $*" + log_only "Warning: $*" console_output "Warning: $*\n" } +# warning without ending newline log_warn_n() { - logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Warning: $*" + log_only "Warning: $*" console_output "Warning: $*" } log_error() { - logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Error: $*" + log_only "Error: $*" console_output "Error: $*\n" >&2 } log_debug() { if [ -n "${DEBUG:-}" ] ; then - logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Debug: $*" + log_only "Debug: $*" console_output "DEBUG: $*\n" >&2 fi } diff --git a/sbin/ngcpcfg b/sbin/ngcpcfg index 15e7b408..f98fc79b 100755 --- a/sbin/ngcpcfg +++ b/sbin/ngcpcfg @@ -33,10 +33,30 @@ ngcpcfg_restore_perms() { } action() { + local rc=0 ACTION="$1" shift - log_debug "${SCRIPTS}/${ACTION} $*" - "${SCRIPTS}/${ACTION}" "$@" + + [ -z "${SUMMARY_ONLY:-}" ] && SUMMARY_ONLY=0 + + if [ "${SUMMARY_ONLY}" == "1" ]; then + log_debug "${SCRIPTS}/${ACTION} $* >/dev/null 2>&1" + "${SCRIPTS}/${ACTION}" "$@" >/dev/null 2>&1 || rc=$? + else + log_debug "${SCRIPTS}/${ACTION} $*" + "${SCRIPTS}/${ACTION}" "$@" || rc=$? + fi + + if [ "${rc}" == "0" ]; then + local msg="Successfully executed '${ACTION}' on '${HNAME}'" + [ "${SUMMARY_ONLY}" == "1" ] || log_only "${msg}" + [ "${SUMMARY_ONLY}" == "0" ] || log_info "${msg}" + else + local msg="Failed to call action '${ACTION}' on '${HNAME}' (see logs on '${HNAME}')" + [ "${SUMMARY_ONLY}" == "1" ] || log_only "Error: ${msg}" + [ "${SUMMARY_ONLY}" == "0" ] || log_error "${msg}" + exit "${rc}" + fi } usage() { @@ -172,6 +192,7 @@ case ${1:-} in --no-action-failure) export NO_ACTION_FAILURE=1; shift ; "$0" "$@" ;; --no-check-origin) export NO_CHECK_ORIGIN=1; shift ; "$0" "$@" ;; --validate) export VALIDATE=1; shift ; "$0" "$@" ;; + --summary-only) export SUMMARY_ONLY=1; shift ; "$0" "$@" ;; -h|--help|help) usage ; exit 0;; -v|--version|version) version ; exit 0;; *) usage 1; exit 1;;